OpenWalnut  1.5.0dev
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 <memory>
29 #include <queue>
30 #include <shared_mutex>
31 
32 #include <boost/thread.hpp>
33 #include <osg/MatrixTransform>
34 #include <osg/NodeCallback>
35 
36 #include "../common/WPredicateHelper.h"
37 
38 
39 /**
40  * Class to wrap around the osg Group node and providing a thread safe add/removal mechanism. Please be sure to use
41  * addUpdateCallback() to set your own update callbacks instead of setUpdateCallback, as this class already has set a callback,
42  * which would be overwritten by a subsequent call to setUpdateCallback(). It is derived from osg::MatrixTransform to allow easy transformations
43  * of a whole bunch of nodes.
44  *
45  * \ingroup GE
46  */
47 class WGEGroupNode: public osg::MatrixTransform
48 {
49 public:
50  /**
51  * Default constructor.
52  */
53  WGEGroupNode();
54 
55  /**
56  * Adds the specified node to the child list of this node in a safe manner. OSG officially requires nodes to be added
57  * exclusively during update callbacks. Using this method it is ensured to be added during update cycle.
58  *
59  * \param node the node to add.
60  *
61  * \note the node may not be added instantly. So do not assume that containsNode ( node ) will return true.
62  */
63  void insert( osg::ref_ptr< osg::Node > node );
64 
65  /**
66  * Removes the specified node from this group in a thread safe manner. It returns if the node has been removed.
67  *
68  * \param node the node to remove
69  */
70  void remove( osg::ref_ptr< osg::Node > node );
71 
72  /**
73  * The base type of predicate. Use a specific WPredicateHelper::ArbitraryPredicate instance. For details, see
74  * \ref WPredicateHelper::ArbitraryPredicateBase.
75  */
77 
78  /**
79  * Removes a node if the specified predicate evaluates to true.
80  * \see WPredicateHelper::ArbitraryPredicate for details.
81  *
82  * \param predicate the predicate.
83  */
84  void remove_if( std::shared_ptr< WGEGroupNode::NodePredicate > predicate );
85 
86  /**
87  * Removes all children from this node.
88  */
89  void clear();
90 
91 protected:
92  /**
93  * Destructor.
94  */
95  virtual ~WGEGroupNode();
96 
97  /**
98  * Update callback which inserts and removes nodes from m_childRemovalQueue and m_childInsertionQueue to the group node.
99  * This ensures thread safe modification of the osg root node.
100  */
101  class SafeUpdaterCallback : public osg::NodeCallback
102  {
103  public:
104  /**
105  * Callback method called by the NodeVisitor when visiting a node.
106  * This inserts and removes enqueued nodes from this group node instance.
107  *
108  * \param node the node calling this update
109  * \param nv The node visitor which performs the traversal. Should be an
110  * update visitor.
111  */
112  virtual void operator()( osg::Node* node, osg::NodeVisitor* nv );
113  };
114 
115  /**
116  * Node callback used to update this root node.
117  */
118  osg::ref_ptr< SafeUpdaterCallback > m_nodeUpdater;
119 
120  /**
121  * The type of operation to perform.
122  */
123  typedef enum
124  {
125  INSERT = 0, //! insert the specified node
126  REMOVE, //! remove the specified node
127  REMOVE_IF, //! remove all items where the predicate evaluates to true
128  CLEAR //! clear group node completely
129  }
131 
132  /**
133  * A struct denoting an operation on this group. The operation itself, as well as the item and predicate are stored.
134  */
136  {
137  /**
138  * Constructs instance and fills members properly.
139  *
140  * \param what the operation to make
141  * \param item the child to delete
142  */
143  ChildOperation( ChildOperationType what, osg::ref_ptr< osg::Node > item ):
144  m_operation( what ),
145  m_item( item ),
146  m_predicate()
147  {
148  };
149 
150  /**
151  * Constructs instance and fills members properly.
152  *
153  * \param what the operation to make
154  * \param predicate the predicate to use for conditional operations (REMOVE_IF)
155  */
156  ChildOperation( ChildOperationType what, std::shared_ptr< NodePredicate > predicate ):
157  m_operation( what ),
158  m_item(),
159  m_predicate( predicate )
160  {
161  };
162 
163  ChildOperationType m_operation; //!< the operation to take
164  osg::ref_ptr< osg::Node > m_item; //!< the item to delete/add
165  std::shared_ptr< NodePredicate > m_predicate; //!< the predicate used by conditional operations
166  };
167 
168  /**
169  * 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
170  * or insertion.
171  */
172  std::queue< std::shared_ptr< ChildOperation > > m_childOperationQueue;
173 
174  /**
175  * Lock used for inserting and removing childs into the child insertion/removal queue.
176  */
177  std::shared_mutex m_childOperationQueueLock;
178 
179  /**
180  * Flag denoting whether the m_childOperationQueue should be considered during the next update of the node.
181  */
183 
184  /**
185  * True whenever all child nodes should be removed.
186  */
188 
189 private:
190 };
191 
192 #endif // WGEGROUPNODE_H
193 
Update callback which inserts and removes nodes from m_childRemovalQueue and m_childInsertionQueue to...
Definition: WGEGroupNode.h:102
virtual void operator()(osg::Node *node, osg::NodeVisitor *nv)
Callback method called by the NodeVisitor when visiting a node.
Class to wrap around the osg Group node and providing a thread safe add/removal mechanism.
Definition: WGEGroupNode.h:48
void remove_if(std::shared_ptr< WGEGroupNode::NodePredicate > predicate)
Removes a node if the specified predicate evaluates to true.
bool m_childOperationQueueDirty
Flag denoting whether the m_childOperationQueue should be considered during the next update of the no...
Definition: WGEGroupNode.h:182
virtual ~WGEGroupNode()
Destructor.
std::queue< std::shared_ptr< ChildOperation > > m_childOperationQueue
Queue of childs that need to be added/removed during the next update cycle.
Definition: WGEGroupNode.h:172
std::shared_mutex m_childOperationQueueLock
Lock used for inserting and removing childs into the child insertion/removal queue.
Definition: WGEGroupNode.h:177
void remove(osg::ref_ptr< osg::Node > node)
Removes the specified node from this group in a thread safe manner.
bool m_removeAll
True whenever all child nodes should be removed.
Definition: WGEGroupNode.h:187
WPredicateHelper::ArbitraryPredicateBase< osg::ref_ptr< osg::Node > const > NodePredicate
The base type of predicate.
Definition: WGEGroupNode.h:76
osg::ref_ptr< SafeUpdaterCallback > m_nodeUpdater
Node callback used to update this root node.
Definition: WGEGroupNode.h:118
WGEGroupNode()
Default constructor.
ChildOperationType
The type of operation to perform.
Definition: WGEGroupNode.h:124
@ CLEAR
remove all items where the predicate evaluates to true
Definition: WGEGroupNode.h:128
@ REMOVE_IF
remove the specified node
Definition: WGEGroupNode.h:127
@ REMOVE
insert the specified node
Definition: WGEGroupNode.h:126
void clear()
Removes all children from this node.
void insert(osg::ref_ptr< osg::Node > node)
Adds the specified node to the child list of this node in a safe manner.
This class builds the base for wrapping around nearly every possible predicates like functors,...
A struct denoting an operation on this group.
Definition: WGEGroupNode.h:136
osg::ref_ptr< osg::Node > m_item
the item to delete/add
Definition: WGEGroupNode.h:164
ChildOperation(ChildOperationType what, std::shared_ptr< NodePredicate > predicate)
Constructs instance and fills members properly.
Definition: WGEGroupNode.h:156
std::shared_ptr< NodePredicate > m_predicate
the predicate used by conditional operations
Definition: WGEGroupNode.h:165
ChildOperationType m_operation
the operation to take
Definition: WGEGroupNode.h:161
ChildOperation(ChildOperationType what, osg::ref_ptr< osg::Node > item)
Constructs instance and fills members properly.
Definition: WGEGroupNode.h:143