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 | |
---|
14 | #ifndef OSG_GROUP |
---|
15 | #define OSG_GROUP 1 |
---|
16 | |
---|
17 | #include <osg/Node> |
---|
18 | #include <osg/NodeVisitor> |
---|
19 | |
---|
20 | namespace osg { |
---|
21 | |
---|
22 | typedef std::vector< ref_ptr<Node> > NodeList; |
---|
23 | |
---|
24 | /** General group node which maintains a list of children. |
---|
25 | * Children are reference counted. This allows children to be shared |
---|
26 | * with memory management handled automatically via osg::Referenced. |
---|
27 | */ |
---|
28 | class OSG_EXPORT Group : public Node |
---|
29 | { |
---|
30 | public : |
---|
31 | |
---|
32 | |
---|
33 | Group(); |
---|
34 | |
---|
35 | /** Copy constructor using CopyOp to manage deep vs shallow copy. */ |
---|
36 | Group(const Group&,const CopyOp& copyop=CopyOp::SHALLOW_COPY); |
---|
37 | |
---|
38 | META_Node(osg, Group); |
---|
39 | |
---|
40 | virtual Group* asGroup() { return this; } |
---|
41 | virtual const Group* asGroup() const { return this; } |
---|
42 | |
---|
43 | virtual void traverse(NodeVisitor& nv); |
---|
44 | |
---|
45 | /** Add Node to Group. |
---|
46 | * If node is not NULL and is not contained in Group then increment its |
---|
47 | * reference count, add it to the child list and dirty the bounding |
---|
48 | * sphere to force it to recompute on next getBound() and return true for success. |
---|
49 | * Otherwise return false. Scene nodes can't be added as child nodes. |
---|
50 | */ |
---|
51 | virtual bool addChild( Node *child ); |
---|
52 | |
---|
53 | /** Insert Node to Group at specific location. |
---|
54 | * The new child node is inserted into the child list |
---|
55 | * before the node at the specified index. No nodes |
---|
56 | * are removed from the group with this operation. |
---|
57 | */ |
---|
58 | virtual bool insertChild( unsigned int index, Node *child ); |
---|
59 | |
---|
60 | /** Remove Node from Group. |
---|
61 | * If Node is contained in Group then remove it from the child |
---|
62 | * list, decrement its reference count, and dirty the |
---|
63 | * bounding sphere to force it to recompute on next getBound() and |
---|
64 | * return true for success. If Node is not found then return false |
---|
65 | * and do not change the reference count of the Node. |
---|
66 | * Note, do not override, only override removeChildren(,) is required. |
---|
67 | */ |
---|
68 | inline bool removeChild( Node *child ) |
---|
69 | { |
---|
70 | unsigned int pos = getChildIndex(child); |
---|
71 | if (pos<_children.size()) return removeChildren(pos,1); |
---|
72 | else return false; |
---|
73 | } |
---|
74 | |
---|
75 | /** Remove Node from Group. |
---|
76 | * If Node is contained in Group then remove it from the child |
---|
77 | * list, decrement its reference count, and dirty the |
---|
78 | * bounding sphere to force it to recompute on next getBound() and |
---|
79 | * return true for success. If Node is not found then return false |
---|
80 | * and do not change the reference count of the Node. |
---|
81 | * Note, do not override, only override removeChildren(,) is required. |
---|
82 | */ |
---|
83 | inline bool removeChild( unsigned int pos, unsigned int numChildrenToRemove=1 ) |
---|
84 | { |
---|
85 | if (pos<_children.size()) return removeChildren(pos,numChildrenToRemove); |
---|
86 | else return false; |
---|
87 | } |
---|
88 | |
---|
89 | /** Remove children from Group. |
---|
90 | * Note, must be override by subclasses of Group which add per child attributes.*/ |
---|
91 | virtual bool removeChildren(unsigned int pos,unsigned int numChildrenToRemove); |
---|
92 | |
---|
93 | /** Replace specified Node with another Node. |
---|
94 | * Equivalent to setChild(getChildIndex(orignChild),node) |
---|
95 | * See docs for setChild for further details on implementation. |
---|
96 | */ |
---|
97 | virtual bool replaceChild( Node *origChild, Node* newChild ); |
---|
98 | |
---|
99 | /** Return the number of children nodes. */ |
---|
100 | inline unsigned int getNumChildren() const { return static_cast<unsigned int>(_children.size()); } |
---|
101 | |
---|
102 | /** Set child node at position i. |
---|
103 | * Return true if set correctly, false on failure (if node==NULL || i is out of range). |
---|
104 | * When Set can be successful applied, the algorithm is : decrement the reference count origNode and increment the |
---|
105 | * reference count of newNode, and dirty the bounding sphere |
---|
106 | * to force it to recompute on next getBound() and return true. |
---|
107 | * If origNode is not found then return false and do not |
---|
108 | * add newNode. If newNode is NULL then return false and do |
---|
109 | * not remove origNode. Also returns false if newChild is a Scene node. |
---|
110 | */ |
---|
111 | virtual bool setChild( unsigned int i, Node* node ); |
---|
112 | |
---|
113 | /** Return child node at position i. */ |
---|
114 | inline Node* getChild( unsigned int i ) { return _children[i].get(); } |
---|
115 | |
---|
116 | /** Return child node at position i. */ |
---|
117 | inline const Node* getChild( unsigned int i ) const { return _children[i].get(); } |
---|
118 | |
---|
119 | /** Return true if node is contained within Group. */ |
---|
120 | inline bool containsNode( const Node* node ) const |
---|
121 | { |
---|
122 | |
---|
123 | for (NodeList::const_iterator itr=_children.begin(); |
---|
124 | itr!=_children.end(); |
---|
125 | ++itr) |
---|
126 | { |
---|
127 | if (itr->get()==node) return true; |
---|
128 | } |
---|
129 | return false; |
---|
130 | } |
---|
131 | |
---|
132 | /** Get the index number of child, return a value between |
---|
133 | * 0 and _children.size()-1 if found, if not found then |
---|
134 | * return _children.size(). |
---|
135 | */ |
---|
136 | inline unsigned int getChildIndex( const Node* node ) const |
---|
137 | { |
---|
138 | for (unsigned int childNum=0;childNum<_children.size();++childNum) |
---|
139 | { |
---|
140 | if (_children[childNum]==node) return childNum; |
---|
141 | } |
---|
142 | return static_cast<unsigned int>(_children.size()); // node not found. |
---|
143 | } |
---|
144 | |
---|
145 | /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/ |
---|
146 | virtual void setThreadSafeRefUnref(bool threadSafe); |
---|
147 | |
---|
148 | /** Resize any per context GLObject buffers to specified size. */ |
---|
149 | virtual void resizeGLObjectBuffers(unsigned int maxSize); |
---|
150 | |
---|
151 | /** If State is non-zero, this function releases any associated OpenGL objects for |
---|
152 | * the specified graphics context. Otherwise, releases OpenGL objects |
---|
153 | * for all graphics contexts. */ |
---|
154 | virtual void releaseGLObjects(osg::State* = 0) const; |
---|
155 | |
---|
156 | virtual BoundingSphere computeBound() const; |
---|
157 | |
---|
158 | protected: |
---|
159 | |
---|
160 | virtual ~Group(); |
---|
161 | |
---|
162 | virtual void childRemoved(unsigned int /*pos*/, unsigned int /*numChildrenToRemove*/) {} |
---|
163 | virtual void childInserted(unsigned int /*pos*/) {} |
---|
164 | |
---|
165 | NodeList _children; |
---|
166 | |
---|
167 | |
---|
168 | }; |
---|
169 | |
---|
170 | } |
---|
171 | |
---|
172 | #endif |
---|