TrueReality  v0.1.1912
trCore/Nodes/NodeVisitor.cpp
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2021 Acid Rain Studios LLC
4 *
5 * This library is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License as published by the Free
7 * Software Foundation; either version 3.0 of the License, or (at your option)
8 * any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * @author Maxim Serebrennik
20 */
22 
23 #include <osg/Group>
24 #include <osg/Node>
25 #include <osg/NodeVisitor>
26 
27 namespace trCore::Nodes
28 {
38  osg::NodeVisitor::VisitorType ToVisitorType(NodeVisitor::VisitorType type)
39  {
40  switch (type)
41  {
42  case NodeVisitor::VisitorType::NODE_VISITOR:
43  return osg::NodeVisitor::VisitorType::NODE_VISITOR;
44  break;
45  case NodeVisitor::VisitorType::UPDATE_VISITOR:
46  return osg::NodeVisitor::VisitorType::UPDATE_VISITOR;
47  break;
48  case NodeVisitor::VisitorType::EVENT_VISITOR:
49  return osg::NodeVisitor::VisitorType::EVENT_VISITOR;
50  break;
51  case NodeVisitor::VisitorType::COLLECT_OCCLUDER_VISITOR:
52  return osg::NodeVisitor::VisitorType::COLLECT_OCCLUDER_VISITOR;
53  break;
54  case NodeVisitor::VisitorType::CULL_VISITOR:
55  return osg::NodeVisitor::VisitorType::CULL_VISITOR;
56  break;
57  case NodeVisitor::VisitorType::INTERSECTION_VISITOR:
58  return osg::NodeVisitor::VisitorType::INTERSECTION_VISITOR;
59  break;
60  default:
61  return osg::NodeVisitor::VisitorType::NODE_VISITOR;
62  break;
63  }
64  }
65 
75  NodeVisitor::VisitorType ToVisitorType(osg::NodeVisitor::VisitorType type)
76  {
77  switch (type)
78  {
79  case osg::NodeVisitor::VisitorType::NODE_VISITOR:
80  return NodeVisitor::VisitorType::NODE_VISITOR;
81  break;
82  case osg::NodeVisitor::VisitorType::UPDATE_VISITOR:
83  return NodeVisitor::VisitorType::UPDATE_VISITOR;
84  break;
85  case osg::NodeVisitor::VisitorType::EVENT_VISITOR:
86  return NodeVisitor::VisitorType::EVENT_VISITOR;
87  break;
88  case osg::NodeVisitor::VisitorType::COLLECT_OCCLUDER_VISITOR:
89  return NodeVisitor::VisitorType::COLLECT_OCCLUDER_VISITOR;
90  break;
91  case osg::NodeVisitor::VisitorType::CULL_VISITOR:
92  return NodeVisitor::VisitorType::CULL_VISITOR;
93  break;
94  case osg::NodeVisitor::VisitorType::INTERSECTION_VISITOR:
95  return NodeVisitor::VisitorType::INTERSECTION_VISITOR;
96  break;
97  default:
98  return NodeVisitor::VisitorType::NODE_VISITOR;
99  break;
100  }
101  }
102 
112  osg::NodeVisitor::TraversalMode ToTraversalMode(NodeVisitor::TraversalMode mode)
113  {
114  switch (mode)
115  {
116  case NodeVisitor::TraversalMode::TRAVERSE_NONE:
117  return osg::NodeVisitor::TraversalMode::TRAVERSE_NONE;
118  break;
119  case NodeVisitor::TraversalMode::TRAVERSE_PARENTS:
120  return osg::NodeVisitor::TraversalMode::TRAVERSE_PARENTS;
121  break;
122  case NodeVisitor::TraversalMode::TRAVERSE_ALL_CHILDREN:
123  return osg::NodeVisitor::TraversalMode::TRAVERSE_ALL_CHILDREN;
124  break;
125  case NodeVisitor::TraversalMode::TRAVERSE_ACTIVE_CHILDREN:
126  return osg::NodeVisitor::TraversalMode::TRAVERSE_ACTIVE_CHILDREN;
127  break;
128  default:
129  return osg::NodeVisitor::TraversalMode::TRAVERSE_NONE;
130  break;
131  }
132  }
133 
143  NodeVisitor::TraversalMode ToTraversalMode(osg::NodeVisitor::TraversalMode mode)
144  {
145  switch (mode)
146  {
147  case osg::NodeVisitor::TraversalMode::TRAVERSE_NONE:
148  return NodeVisitor::TraversalMode::TRAVERSE_NONE;
149  break;
150  case osg::NodeVisitor::TraversalMode::TRAVERSE_PARENTS:
151  return NodeVisitor::TraversalMode::TRAVERSE_PARENTS;
152  break;
153  case osg::NodeVisitor::TraversalMode::TRAVERSE_ALL_CHILDREN:
154  return NodeVisitor::TraversalMode::TRAVERSE_ALL_CHILDREN;
155  break;
156  case osg::NodeVisitor::TraversalMode::TRAVERSE_ACTIVE_CHILDREN:
157  return NodeVisitor::TraversalMode::TRAVERSE_ACTIVE_CHILDREN;
158  break;
159  default:
160  return NodeVisitor::TraversalMode::TRAVERSE_NONE;
161  break;
162  }
163  }
164 
165  const trUtil::RefStr NodeVisitor::CLASS_TYPE = trUtil::RefStr("trCore::Nodes::NodeVisitor");
166 
168  NodeVisitor::NodeVisitor(TraversalMode tm, const std::string name)
169  : NodeVisitor(NODE_VISITOR, tm, name)
170  {
171  }
172 
174  NodeVisitor::NodeVisitor(VisitorType type, TraversalMode tm, const std::string name)
175  {
176  mNodeVisitor = new osg::NodeVisitor(ToVisitorType(type), tm);
177  mNodeVisitor->setName(name);
178  }
179 
181  const std::string& NodeVisitor::GetType() const
182  {
183  return CLASS_TYPE;
184  }
185 
187  void NodeVisitor::SetName(const std::string& name)
188  {
189  BaseClass::SetName(name);
190  mNodeVisitor->setName(name);
191  }
192 
194  osg::NodeVisitor* NodeVisitor::AsOSGVisitor()
195  {
196  return mNodeVisitor.Get();
197  }
198 
200  const osg::NodeVisitor* NodeVisitor::AsOSGVisitor() const
201  {
202  return mNodeVisitor.Get();
203  }
204 
207  {
208  mNodeVisitor->reset();
209  }
210 
213  {
214  mNodeVisitor->setVisitorType(ToVisitorType(type));
215  }
216 
219  {
220  return ToVisitorType(mNodeVisitor->getVisitorType());
221  }
222 
224  inline void NodeVisitor::SetTraversalNumber(unsigned int fn)
225  {
226  mNodeVisitor->setTraversalNumber(fn);
227  }
228 
230  inline unsigned int NodeVisitor::GetTraversalNumber() const
231  {
232  return mNodeVisitor->getTraversalNumber();
233  }
234 
237  {
238  mNodeVisitor->setFrameStamp(fs->AsOSGClass());
239  }
240 
243  {
244  return new FrameStamp(*mNodeVisitor->getFrameStamp());
245  }
246 
249  {
250  mNodeVisitor->setTraversalMask(mask);
251  }
252 
255  {
256  return mNodeVisitor->getTraversalMask();
257  }
258 
261  {
262  mNodeVisitor->setNodeMaskOverride(mask);
263  }
264 
267  {
268  return mNodeVisitor->getNodeMaskOverride();
269  }
270 
272  inline bool NodeVisitor::ValidNodeMask(const Nodes::Node & node) const
273  {
274  return mNodeVisitor->validNodeMask(*node.AsOSGNode());
275  }
276 
279  {
280  mNodeVisitor->setTraversalMode(ToTraversalMode(mode));
281  }
282 
285  {
286  return ToTraversalMode(mNodeVisitor->getTraversalMode());
287  }
288 
290  inline void NodeVisitor::Traverse(Node & node)
291  {
292  mNodeVisitor->traverse(*node.AsOSGNode());
293  }
294 
297  {
298  mNodeVisitor->pushOntoNodePath(node->AsOSGNode());
299  }
300 
303  {
304  mNodeVisitor->popFromNodePath();
305  }
306 
309  {
310  osg::NodePath* osgNodes = &mNodeVisitor->getNodePath();
311  NodePath* path = new NodePath;
312 
313  // Copy the node path from osg nodes to TR
314  for (int i = 0; osgNodes->size() - 1; ++i)
315  {
316  trBase::SmrtPtr<trCore::Nodes::Node> newNode = new trCore::Nodes::Node(*osgNodes->at(i), osgNodes->at(i)->getName());
317  path->push_back(newNode);
318  }
319 
320  return path;
321  }
322 
325  {
326  osg::NodePath* osgNodes = &mNodeVisitor->getNodePath();
327  NodePath* path = new NodePath;
328 
329  // Copy the node path from osg nodes to TR
330  for (int i = 0; osgNodes->size() - 1; ++i)
331  {
332  trBase::SmrtPtr<trCore::Nodes::Node> newNode = new trCore::Nodes::Node(*osgNodes->at(i), osgNodes->at(i)->getName());
333  path->push_back(newNode);
334  }
335 
336  return path;
337  }
338 
341  {
342  }
343 }
NodeVisitor(TraversalMode tm=TRAVERSE_NONE, const std::string name=CLASS_TYPE)
Holds the class type name for efficient comparisons.
virtual osg::NodeVisitor * AsOSGVisitor()
Returns a pointer to the internal OSG Node.
osg::NodeVisitor::VisitorType ToVisitorType(NodeVisitor::VisitorType type)
std::vector< trBase::SmrtPtr< Node > > NodePath
A vector of Nodes pointers which is used to describe the path from a root node to a descendant...
unsigned int NodeMask
Holds the class type name for efficient comparisons.
Smart pointer for handling referenced counted objects.
Definition: SmrtPtr.h:36
Class which encapsulates the frame number, reference time and calendar time of specific frame...
Definition: FrameStamp.h:54
osg::NodeVisitor::TraversalMode ToTraversalMode(NodeVisitor::TraversalMode mode)
Converts TR traversal mode to a osg traversal mode.
A string wrapper that will make sure that all of the strings with the same value will point to the sa...
Definition: RefStr.h:50
const FrameStamp * GetFrameStamp() const
Get the FrameStamp that this traversal is associated with.
Node::NodeMask GetTraversalMask() const
Get the TraversalMask.
NodePath * GetNodePath()
Get the non const NodePath from the top most node applied down to the current Node being visited...
virtual void Reset()
Method to call to reset visitor.
TraversalMode GetTraversalMode() const
Get the traversal mode.
void PopFromNodePath()
Method called by Node::Accept() method after a call to NodeVisitor::Apply(..).
Visitor for type safe operations on trCore::Nodes.
VisitorType GetVisitorType() const
Get the VisitorType.
Class for wrapping the osg node Internally it contains an OSG Group, that can be accessed by user if ...
void SetNodeMaskOverride(Node::NodeMask mask)
void PushOntoNodePath(Node *node)
Method called by Node::Accept() method before a call to the NodeVisitor::Apply(..).
virtual const std::string & GetType() const override
Returns the class type.
virtual void SetName(const std::string &name)
Sets this instances name.
bool ValidNodeMask(const Nodes::Node &node) const
Method to called by Node and its subclass&#39; Node::Accept() method, if the result is true it is used to...
void Traverse(Node &node)
Method for handling traversal of a nodes.
virtual osg::FrameStamp * AsOSGClass()
Returns a pointer to the internal OSG FrameStamp.
Definition: FrameStamp.cpp:59
void SetTraversalNumber(unsigned int fn)
Set the traversal number.
static const trUtil::RefStr CLASS_TYPE
Adds an easy and swappable access to the base class.
virtual void SetName(const std::string &name)
Sets this instances name.
Definition: Base.cpp:43
void SetFrameStamp(FrameStamp *fs)
Set the FrameStamp that this traversal is associated with.
trBase::SmrtPtr< osg::NodeVisitor > mNodeVisitor
The node visitor.
virtual osg::Node * AsOSGNode()
Returns a pointer to the internal OSG Node.
T * Get() const
Returns the stored internal pointer.
Definition: SmrtPtr.h:73
void SetTraversalMode(TraversalMode mode)
Set the traversal mode for Node::Traverse() to use when deciding which children of a node to traverse...
void SetVisitorType(VisitorType type)
Set the VisitorType, used to distinguish different visitors during traversal of the scene...
Node::NodeMask GetNodeMaskOverride() const
unsigned int GetTraversalNumber() const
Get the traversal number.
void SetTraversalMask(Node::NodeMask mask)
Set the TraversalMask of this NodeVisitor.