OpenWalnut  1.4.0
WGEGroupNode.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WGEGROUPNODE_H
26 #define WGEGROUPNODE_H
27 
28 #include <queue>
29 
30 #ifndef Q_MOC_RUN
31 #include <boost/thread.hpp>
32 #endif
33 
34 #include <osg/MatrixTransform>
35 #include <osg/NodeCallback>
36 
37 #include "../common/WPredicateHelper.h"
38 
39 
40 /**
41  * Class to wrap around the osg Group node and providing a thread safe add/removal mechanism. Please be sure to use
42  * addUpdateCallback() to set your own update callbacks instead of setUpdateCallback, as this class already has set a callback,
43  * which would be overwritten by a subsequent call to setUpdateCallback(). It is derived from osg::MatrixTransform to allow easy transformations
44  * of a whole bunch of nodes.
45  *
46  * \ingroup GE
47  */
48 class WGEGroupNode: public osg::MatrixTransform
49 {
50 public:
51  /**
52  * Default constructor.
53  */
54  WGEGroupNode();
55 
56  /**
57  * Adds the specified node to the child list of this node in a safe manner. OSG officially requires nodes to be added
58  * exclusively during update callbacks. Using this method it is ensured to be added during update cycle.
59  *
60  * \param node the node to add.
61  *
62  * \note the node may not be added instantly. So do not assume that containsNode ( node ) will return true.
63  */
64  void insert( osg::ref_ptr< osg::Node > node );
65 
66  /**
67  * Removes the specified node from this group in a thread safe manner. It returns if the node has been removed.
68  *
69  * \param node the node to remove
70  */
71  void remove( osg::ref_ptr< osg::Node > node );
72 
73  /**
74  * The base type of predicate. Use a specific WPredicateHelper::ArbitraryPredicate instance. For details, see
75  * \ref WPredicateHelper::ArbitraryPredicateBase.
76  */
78 
79  /**
80  * Removes a node if the specified predicate evaluates to true.
81  * \see WPredicateHelper::ArbitraryPredicate for details.
82  *
83  * \param predicate the predicate.
84  */
85  void remove_if( boost::shared_ptr< WGEGroupNode::NodePredicate > predicate );
86 
87  /**
88  * Removes all children from this node.
89  */
90  void clear();
91 
92 protected:
93  /**
94  * Destructor.
95  */
96  virtual ~WGEGroupNode();
97 
98  /**
99  * Update callback which inserts and removes nodes from m_childRemovalQueue and m_childInsertionQueue to the group node.
100  * This ensures thread safe modification of the osg root node.
101  */
102  class SafeUpdaterCallback : public osg::NodeCallback
103  {
104  public:
105  /**
106  * Callback method called by the NodeVisitor when visiting a node.
107  * This inserts and removes enqueued nodes from this group node instance.
108  *
109  * \param node the node calling this update
110  * \param nv The node visitor which performs the traversal. Should be an
111  * update visitor.
112  */
113  virtual void operator()( osg::Node* node, osg::NodeVisitor* nv );
114  };
115 
116  /**
117  * Node callback used to update this root node.
118  */
119  osg::ref_ptr< SafeUpdaterCallback > m_nodeUpdater;
120 
121  /**
122  * The type of operation to perform.
123  */
124  typedef enum
125  {
126  INSERT = 0, //! insert the specified node
127  REMOVE, //! remove the specified node
128  REMOVE_IF, //! remove all items where the predicate evaluates to true
129  CLEAR //! clear group node completely
130  }
132 
133  /**
134  * A struct denoting an operation on this group. The operation itself, as well as the item and predicate are stored.
135  */
137  {
138  /**
139  * Constructs instance and fills members properly.
140  *
141  * \param what the operation to make
142  * \param item the child to delete
143  */
144  ChildOperation( ChildOperationType what, osg::ref_ptr< osg::Node > item ):
145  m_operation( what ),
146  m_item( item ),
147  m_predicate()
148  {
149  };
150 
151  /**
152  * Constructs instance and fills members properly.
153  *
154  * \param what the operation to make
155  * \param predicate the predicate to use for conditional operations (REMOVE_IF)
156  */
157  ChildOperation( ChildOperationType what, boost::shared_ptr< NodePredicate > predicate ):
158  m_operation( what ),
159  m_item(),
160  m_predicate( predicate )
161  {
162  };
163 
164  ChildOperationType m_operation; //!< the operation to take
165  osg::ref_ptr< osg::Node > m_item; //!< the item to delete/add
166  boost::shared_ptr< NodePredicate > m_predicate; //!< the predicate used by conditional operations
167  };
168 
169  /**
170  * Queue of childs that need to be added/removed during the next update cycle. It is a pair per operation, where the bool is denoting removal
171  * or insertion.
172  */
173  std::queue< boost::shared_ptr< ChildOperation > > m_childOperationQueue;
174 
175  /**
176  * Lock used for inserting and removing childs into the child insertion/removal queue.
177  */
178  boost::shared_mutex m_childOperationQueueLock;
179 
180  /**
181  * Flag denoting whether the m_childOperationQueue should be considered during the next update of the node.
182  */
184 
185  /**
186  * True whenever all child nodes should be removed.
187  */
189 
190 private:
191 };
192 
193 #endif // WGEGROUPNODE_H
194 
Update callback which inserts and removes nodes from m_childRemovalQueue and m_childInsertionQueue to...
Definition: WGEGroupNode.h:102
remove all items where the predicate evaluates to true
Definition: WGEGroupNode.h:129
virtual ~WGEGroupNode()
Destructor.
osg::ref_ptr< osg::Node > m_item
the item to delete/add
Definition: WGEGroupNode.h:165
remove the specified node
Definition: WGEGroupNode.h:128
A struct denoting an operation on this group.
Definition: WGEGroupNode.h:136
virtual void operator()(osg::Node *node, osg::NodeVisitor *nv)
Callback method called by the NodeVisitor when visiting a node.
std::queue< boost::shared_ptr< ChildOperation > > m_childOperationQueue
Queue of childs that need to be added/removed during the next update cycle.
Definition: WGEGroupNode.h:173
boost::shared_ptr< NodePredicate > m_predicate
the predicate used by conditional operations
Definition: WGEGroupNode.h:166
boost::shared_mutex m_childOperationQueueLock
Lock used for inserting and removing childs into the child insertion/removal queue.
Definition: WGEGroupNode.h:178
void insert(osg::ref_ptr< osg::Node > node)
Adds the specified node to the child list of this node in a safe manner.
ChildOperation(ChildOperationType what, boost::shared_ptr< NodePredicate > predicate)
Constructs instance and fills members properly.
Definition: WGEGroupNode.h:157
WPredicateHelper::ArbitraryPredicateBase< osg::ref_ptr< osg::Node > const > NodePredicate
The base type of predicate.
Definition: WGEGroupNode.h:77
osg::ref_ptr< SafeUpdaterCallback > m_nodeUpdater
Node callback used to update this root node.
Definition: WGEGroupNode.h:119
ChildOperation(ChildOperationType what, osg::ref_ptr< osg::Node > item)
Constructs instance and fills members properly.
Definition: WGEGroupNode.h:144
Class to wrap around the osg Group node and providing a thread safe add/removal mechanism.
Definition: WGEGroupNode.h:48
void clear()
Removes all children from this node.
WGEGroupNode()
Default constructor.
bool m_removeAll
True whenever all child nodes should be removed.
Definition: WGEGroupNode.h:188
insert the specified node
Definition: WGEGroupNode.h:127
ChildOperationType
The type of operation to perform.
Definition: WGEGroupNode.h:124
ChildOperationType m_operation
the operation to take
Definition: WGEGroupNode.h:162
bool m_childOperationQueueDirty
Flag denoting whether the m_childOperationQueue should be considered during the next update of the no...
Definition: WGEGroupNode.h:183
void remove_if(boost::shared_ptr< WGEGroupNode::NodePredicate > predicate)
Removes a node if the specified predicate evaluates to true.
This class builds the base for wrapping around nearly every possible predicates like functors...