openscenegraph
Group
Go to the documentation of this file.
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
20namespace osg {
21
22typedef 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*/
28class 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 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 template<class T> bool addChild( const ref_ptr<T>& child ) { return addChild(child.get()); }
54
55 /** Insert Node to Group at specific location.
56 * The new child node is inserted into the child list
57 * before the node at the specified index. No nodes
58 * are removed from the group with this operation.
59 */
60 virtual bool insertChild( unsigned int index, Node *child );
61
62 template<class T> bool insertChild( unsigned int index, const ref_ptr<T>& child ) { return insertChild(index, child.get()); }
63
64 /** Remove Node from Group.
65 * If Node is contained in Group then remove it from the child
66 * list, decrement its reference count, and dirty the
67 * bounding sphere to force it to recompute on next getBound() and
68 * return true for success. If Node is not found then return false
69 * and do not change the reference count of the Node.
70 * Note, do not override, only override removeChildren(,) is required.
71 */
72 virtual bool removeChild( Node *child );
73
74 template<class T> bool removeChild( const ref_ptr<T>& child ) { return removeChild(child.get()); }
75
76 /** Remove Node from Group.
77 * If Node is contained in Group then remove it from the child
78 * list, decrement its reference count, and dirty the
79 * bounding sphere to force it to recompute on next getBound() and
80 * return true for success. If Node is not found then return false
81 * and do not change the reference count of the Node.
82 * Note, do not override, only override removeChildren(,) is required.
83 */
84 inline bool removeChild( unsigned int pos, unsigned int numChildrenToRemove=1 )
85 {
86 if (pos<_children.size()) return removeChildren(pos,numChildrenToRemove);
87 else return false;
88 }
89
90 /** Remove children from Group.
91 * Note, must be override by subclasses of Group which add per child attributes.*/
92 virtual bool removeChildren(unsigned int pos,unsigned int numChildrenToRemove);
93
94 /** Replace specified child Node with another Node.
95 * Equivalent to setChild(getChildIndex(orignChild),node)
96 * See docs for setChild for further details on implementation.
97 */
98 virtual bool replaceChild( Node *origChild, Node* newChild );
99
100 template<class T, class R> bool replaceChild( const ref_ptr<T>& origChild, const ref_ptr<R>& newChild ) { return replaceChild( origChild.get(), newChild.get()); }
101
102 /** Return the number of children nodes. */
103 virtual unsigned int getNumChildren() const;
104
105 /** Set child node at position i.
106 * Return true if set correctly, false on failure (if node==NULL || i is out of range).
107 * When Set can be successful applied, the algorithm is : decrement the reference count origNode and increment the
108 * reference count of newNode, and dirty the bounding sphere
109 * to force it to recompute on next getBound() and return true.
110 * If origNode is not found then return false and do not
111 * add newNode. If newNode is NULL then return false and do
112 * not remove origNode. Also returns false if newChild is a Scene node.
113 */
114 virtual bool setChild( unsigned int i, Node* node );
115
116 /** Return child node at position i. */
117 inline Node* getChild( unsigned int i ) { return _children[i].get(); }
118
119 /** Return child node at position i. */
120 inline const Node* getChild( unsigned int i ) const { return _children[i].get(); }
121
122 /** Return true if node is contained within Group. */
123 inline bool containsNode( const Node* node ) const
124 {
125
126 for (NodeList::const_iterator itr=_children.begin();
127 itr!=_children.end();
128 ++itr)
129 {
130 if (itr->get()==node) return true;
131 }
132 return false;
133 }
134
135 template<class T> bool containsNode(const ref_ptr<T>& node) const { return containsNode(node.get()); }
136
137 /** Get the index number of child, return a value between
138 * 0 and _children.size()-1 if found, if not found then
139 * return _children.size().
140 */
141 inline unsigned int getChildIndex( const Node* node ) const
142 {
143 for (unsigned int childNum=0;childNum<_children.size();++childNum)
144 {
145 if (_children[childNum]==node) return childNum;
146 }
147 return static_cast<unsigned int>(_children.size()); // node not found.
148 }
149
150 /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
151 virtual void setThreadSafeRefUnref(bool threadSafe);
152
153 /** Resize any per context GLObject buffers to specified size. */
154 virtual void resizeGLObjectBuffers(unsigned int maxSize);
155
156 /** If State is non-zero, this function releases any associated OpenGL objects for
157 * the specified graphics context. Otherwise, releases OpenGL objects
158 * for all graphics contexts. */
159 virtual void releaseGLObjects(osg::State* = 0) const;
160
161 virtual BoundingSphere computeBound() const;
162
163 protected:
164
165 virtual ~Group();
166
167 virtual void childRemoved(unsigned int /*pos*/, unsigned int /*numChildrenToRemove*/) {}
168 virtual void childInserted(unsigned int /*pos*/) {}
169
170 NodeList _children;
171
172
173};
174
175}
176
177#endif