Group 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSG_GROUP
  14. #define OSG_GROUP 1
  15. #include <osg/Node>
  16. #include <osg/NodeVisitor>
  17. namespace osg {
  18. typedef std::vector< ref_ptr<Node> > NodeList;
  19. /** General group node which maintains a list of children.
  20. * Children are reference counted. This allows children to be shared
  21. * with memory management handled automatically via osg::Referenced.
  22. */
  23. class OSG_EXPORT Group : public Node
  24. {
  25. public :
  26. Group();
  27. /** Copy constructor using CopyOp to manage deep vs shallow copy. */
  28. Group(const Group&,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  29. META_Node(osg, Group);
  30. virtual Group* asGroup() { return this; }
  31. virtual const Group* asGroup() const { return this; }
  32. virtual void traverse(NodeVisitor& nv);
  33. /** Add Node to Group.
  34. * If node is not NULL then increment its
  35. * reference count, add it to the child list and dirty the bounding
  36. * sphere to force it to recompute on next getBound() and return true for success.
  37. * Otherwise return false. Scene nodes can't be added as child nodes.
  38. */
  39. virtual bool addChild( Node *child );
  40. template<class T> bool addChild( const ref_ptr<T>& child ) { return addChild(child.get()); }
  41. /** Insert Node to Group at specific location.
  42. * The new child node is inserted into the child list
  43. * before the node at the specified index. No nodes
  44. * are removed from the group with this operation.
  45. */
  46. virtual bool insertChild( unsigned int index, Node *child );
  47. template<class T> bool insertChild( unsigned int index, const ref_ptr<T>& child ) { return insertChild(index, child.get()); }
  48. /** Remove Node from Group.
  49. * If Node is contained in Group then remove it from the child
  50. * list, decrement its reference count, and dirty the
  51. * bounding sphere to force it to recompute on next getBound() and
  52. * return true for success. If Node is not found then return false
  53. * and do not change the reference count of the Node.
  54. * Note, do not override, only override removeChildren(,) is required.
  55. */
  56. virtual bool removeChild( Node *child );
  57. template<class T> bool removeChild( const ref_ptr<T>& child ) { return removeChild(child.get()); }
  58. /** Remove Node from Group.
  59. * If Node is contained in Group then remove it from the child
  60. * list, decrement its reference count, and dirty the
  61. * bounding sphere to force it to recompute on next getBound() and
  62. * return true for success. If Node is not found then return false
  63. * and do not change the reference count of the Node.
  64. * Note, do not override, only override removeChildren(,) is required.
  65. */
  66. inline bool removeChild( unsigned int pos, unsigned int numChildrenToRemove=1 )
  67. {
  68. if (pos<_children.size()) return removeChildren(pos,numChildrenToRemove);
  69. else return false;
  70. }
  71. /** Remove children from Group.
  72. * Note, must be override by subclasses of Group which add per child attributes.*/
  73. virtual bool removeChildren(unsigned int pos,unsigned int numChildrenToRemove);
  74. /** Replace specified child Node with another Node.
  75. * Equivalent to setChild(getChildIndex(orignChild),node)
  76. * See docs for setChild for further details on implementation.
  77. */
  78. virtual bool replaceChild( Node *origChild, Node* newChild );
  79. template<class T, class R> bool replaceChild( const ref_ptr<T>& origChild, const ref_ptr<R>& newChild ) { return replaceChild( origChild.get(), newChild.get()); }
  80. /** Return the number of children nodes. */
  81. virtual unsigned int getNumChildren() const;
  82. /** Set child node at position i.
  83. * Return true if set correctly, false on failure (if node==NULL || i is out of range).
  84. * When Set can be successful applied, the algorithm is : decrement the reference count origNode and increment the
  85. * reference count of newNode, and dirty the bounding sphere
  86. * to force it to recompute on next getBound() and return true.
  87. * If origNode is not found then return false and do not
  88. * add newNode. If newNode is NULL then return false and do
  89. * not remove origNode. Also returns false if newChild is a Scene node.
  90. */
  91. virtual bool setChild( unsigned int i, Node* node );
  92. /** Return child node at position i. */
  93. inline Node* getChild( unsigned int i ) { return _children[i].get(); }
  94. /** Return child node at position i. */
  95. inline const Node* getChild( unsigned int i ) const { return _children[i].get(); }
  96. /** Return true if node is contained within Group. */
  97. inline bool containsNode( const Node* node ) const
  98. {
  99. for (NodeList::const_iterator itr=_children.begin();
  100. itr!=_children.end();
  101. ++itr)
  102. {
  103. if (itr->get()==node) return true;
  104. }
  105. return false;
  106. }
  107. template<class T> bool containsNode(const ref_ptr<T>& node) const { return containsNode(node.get()); }
  108. /** Get the index number of child, return a value between
  109. * 0 and _children.size()-1 if found, if not found then
  110. * return _children.size().
  111. */
  112. inline unsigned int getChildIndex( const Node* node ) const
  113. {
  114. for (unsigned int childNum=0;childNum<_children.size();++childNum)
  115. {
  116. if (_children[childNum]==node) return childNum;
  117. }
  118. return static_cast<unsigned int>(_children.size()); // node not found.
  119. }
  120. /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
  121. virtual void setThreadSafeRefUnref(bool threadSafe);
  122. /** Resize any per context GLObject buffers to specified size. */
  123. virtual void resizeGLObjectBuffers(unsigned int maxSize);
  124. /** If State is non-zero, this function releases any associated OpenGL objects for
  125. * the specified graphics context. Otherwise, releases OpenGL objects
  126. * for all graphics contexts. */
  127. virtual void releaseGLObjects(osg::State* = 0) const;
  128. virtual BoundingSphere computeBound() const;
  129. protected:
  130. virtual ~Group();
  131. virtual void childRemoved(unsigned int /*pos*/, unsigned int /*numChildrenToRemove*/) {}
  132. virtual void childInserted(unsigned int /*pos*/) {}
  133. NodeList _children;
  134. };
  135. }
  136. #endif