V-ART
scenenode.cpp
Go to the documentation of this file.
1 
5 #include "vart/scenenode.h"
6 #include "vart/joint.h"
7 #include "vart/meshobject.h"
8 #include "vart/transform.h"
9 #include "vart/sgpath.h"
10 #include "vart/snoperator.h"
11 #include "vart/snlocator.h"
12 
13 #include <cassert>
14 using namespace std;
15 
17 
19 {
20 }
21 
22 // FixMe: Implement a recursive copy that works on existing objects.
24 {
25  cerr << "\aWarning: SceneNode::RecursiveCopy() is deprecated.\n";
26  VART::SceneNode * thisCopy;
27  std::list<VART::SceneNode*>::iterator iter;
28 
29  thisCopy = this->Copy();
30  thisCopy->childList.clear();
31  for( iter = childList.begin(); iter !=childList.end(); iter++ )
32  thisCopy->AddChild( *(*iter)->RecursiveCopy() );
33  return thisCopy;
34 }
35 
37 {
38  //~ cout << "VART::SceneNode::~SceneNode(): " << GetDescription() << endl;
39 }
40 
42 {
43  childList = node.childList;
44  description = node.description;
45 }
46 
48 {
49  childList = node.childList;
50  description = node.description;
51  return *this;
52 }
53 
55 {
56  childList.push_back(&child);
57 }
58 
60 {
61  assert(childPtr != NULL);
62  list<VART::SceneNode*>::iterator iter = childList.begin();
63  while (iter != childList.end())
64  {
65  if ((*iter) == childPtr)
66  {
67  childList.erase(iter);
68  return true;
69  }
70  else
71  ++iter;
72  }
73  return false;
74 }
75 
77 {
78  bool result = DrawInstanceOGL();
79  list<VART::SceneNode*>::const_iterator iter = childList.begin();
80  for (; iter != childList.end(); ++iter)
81  result = (result && (*iter)->DrawOGL());
82  return result;
83 }
84 
86 {
87  list<VART::SceneNode*>::const_iterator iter;
88  for (iter = childList.begin(); iter != childList.end(); ++iter)
89  {
90  (*iter)->AutoDeleteChildren();
91  if ((*iter)->autoDelete)
92  delete (*iter);
93  }
94 }
95 
96 VART::SceneNode* VART::SceneNode::FindChildByName(const std::string& name) const
97 {
98  list<VART::SceneNode*>::const_iterator iter;
99  VART::SceneNode* result;
100  for (iter = childList.begin(); iter != childList.end(); ++iter)
101  {
102  if ((*iter)->GetDescription() == name)
103  return *iter;
104  else{
105  result = (*iter)->FindChildByName(name);
106  if (result) return result;
107  }
108  }
109  return NULL;
110 }
111 
112 list<VART::SceneNode*> VART::SceneNode::GetChilds()
113 // deprecated
114 {
115  cerr << "\aWarning: SceneNode::GetChilds() is deprecated.\n";
116  return childList;
117 }
118 
119 bool VART::SceneNode::FindPathTo(SceneNode* targetPtr, SGPath* resultPtr) const
120 {
121  assert(resultPtr != NULL);
122  assert(targetPtr != NULL);
123  resultPtr->Clear(); // erase previous elements, if any.
124  return RecursiveFindPathTo(targetPtr, resultPtr);
125 }
126 
127 bool VART::SceneNode::FindPathTo(const string& targetName, SGPath* resultPtr) const
128 {
129  assert(resultPtr != NULL);
130  resultPtr->Clear(); // erase previous elements, if any.
131  return RecursiveFindPathTo(targetName, resultPtr);
132 }
133 
134 bool VART::SceneNode::RecursiveFindPathTo(SceneNode* targetPtr, SGPath* resultPtr) const
135 {
136  list<VART::SceneNode*>::const_iterator iter;
137 
138  if (targetPtr == this)
139  return true;
140  else
141  {
142  for (iter = childList.begin(); iter != childList.end(); ++iter)
143  {
144  if ((*iter)->RecursiveFindPathTo(targetPtr, resultPtr))
145  {
146  resultPtr->PushFront(*iter);
147  return true;
148  }
149  }
150  }
151  return false;
152 }
153 
154 bool VART::SceneNode::RecursiveFindPathTo(const string& targetName, SGPath* resultPtr) const
155 {
156  list<VART::SceneNode*>::const_iterator iter;
157 
158  if (description == targetName)
159  return true;
160  else
161  {
162  for (iter = childList.begin(); iter != childList.end(); ++iter)
163  {
164  if ((*iter)->RecursiveFindPathTo(targetName, resultPtr))
165  {
166  resultPtr->PushFront(*iter);
167  return true;
168  }
169  }
170  }
171  return false;
172 }
173 
174 // virtual
176 {
177  operatorPtr->OperateOn(this); // process this
178 
179  // process children
180  list<SceneNode*>::const_iterator iter = childList.begin();
181  for (; iter != childList.end(); ++iter)
182  {
183  (*iter)->TraverseDepthFirst(operatorPtr);
184  }
185 }
186 
187 // virtual
189 {
190  list<const SceneNode*> queue;
191  const SceneNode* nodePtr;
192 
193  queue.push_back(this);
194  while (!queue.empty())
195  {
196  nodePtr = queue.front();
197  queue.pop_front();
198  operatorPtr->OperateOn(nodePtr);
199  for (list<SceneNode*>::const_iterator iter = nodePtr->childList.begin();
200  iter != nodePtr->childList.end(); ++iter)
201  queue.push_back(*iter);
202  }
203 }
204 
205 // virtual
207 {
208  locatorPtr->OperateOn(this); // process this
209  if (locatorPtr->NotFinished())
210  {
211  // process children
212  list<SceneNode*>::const_iterator iter = childList.begin();
213  for (; locatorPtr->NotFinished() && (iter != childList.end()); ++iter)
214  {
215  (*iter)->LocateDepthFirst(locatorPtr);
216  }
217  if (locatorPtr->Finished()) // if target has been found...
218  {
219  --iter; // iter is beyond target
220  locatorPtr->AddNodeToPath(*iter);
221  }
222  }
223 }
224 
225 // virtual
227 {
228  list<const SceneNode*> queue;
229  const SceneNode* nodePtr;
230  queue.push_back(this);
231  while (locatorPtr->NotFinished() && (!queue.empty()))
232  {
233  nodePtr = queue.front();
234  queue.pop_front();
235  locatorPtr->OperateOn(nodePtr);
236  list<SceneNode*>::const_iterator iter = nodePtr->childList.begin();
237  for (; iter != nodePtr->childList.end(); ++iter)
238  queue.push_back(*iter);
239  }
240 }
241 
242 int VART::SceneNode::GetNodeTypeList( TypeID type, std::list<SceneNode*>& nodeList )
243 // deprecated
244 {
245  list<VART::SceneNode*>::const_iterator iter;
246  int i=0;
247 
248  cerr << "\aWaring: SceneNode::GetNodeTypeList is deprecated. Please use SceneNode::TraverseDepthFirst.\n";
249  switch ( type )
250  {
251  case JOINT:
252  if ( dynamic_cast <VART::Joint*>(this) != NULL )
253  {
254  nodeList.push_back( this );
255  i++;
256  }
257  break;
258  case TRANSFORM:
259  if ( dynamic_cast <VART::Transform*>(this) != NULL )
260  {
261  nodeList.push_back( this );
262  i++;
263  }
264  break;
265  case MESH_OBJECT:
266  if ( dynamic_cast <VART::MeshObject*>(this) != NULL )
267  {
268  nodeList.push_back( this );
269  i++;
270  }
271  break;
272  default: ; // avoid compiler complaints about cases not handled
273  }
274 
275  for (iter = childList.begin(); iter != childList.end(); ++iter)
276  i = i + (*iter)->GetNodeTypeList( type, nodeList);
277 
278  return i;
279 }
280 
281 void VART::SceneNode::XmlPrintOn(ostream& os, unsigned int indent) const
282 // virtual method
283 {
284  list<SceneNode*>::const_iterator iter = childList.begin();
285  string indentStr(indent,' ');
286 
287  os << indentStr << "Unimplemented XmlPrintOn for " << GetID() << "\n";
288  if (recursivePrinting)
289  while (iter != childList.end())
290  {
291  (*iter)->XmlPrintOn(os, indent + 2);
292  ++iter;
293  }
294 }
virtual void TraverseDepthFirst(SNOperator *operatorPtr) const
Process all children in depth-first order.
Definition: scenenode.cpp:175
Base class for objects that compose a scene graph.
Definition: scenenode.h:25
std::list< SceneNode * > childList
Child list.
Definition: scenenode.h:160
virtual void TraverseBreadthFirst(SNOperator *operatorPtr) const
Process all children in breadth-first order.
Definition: scenenode.cpp:188
Header file for V-ART class "SceneNode".
virtual SceneNode * RecursiveCopy()
Definition: scenenode.cpp:23
void AddNodeToPath(SceneNode *nodePtr)
Adds a node to internal Path.
Definition: snlocator.cpp:14
virtual void OperateOn(const SceneNode *nodePtr)=0
Process given node.
SceneNode()
Creates an uninitialized scene node.
Definition: scenenode.cpp:18
std::string description
Textual identification.
Definition: scenenode.h:162
virtual void OperateOn(const SceneNode *nodePtr)=0
Objects that process scene nodes.
Definition: snoperator.h:19
virtual void XmlPrintOn(std::ostream &os, unsigned int indent) const
Recursively outputs XML representation of the scene node.
Definition: scenenode.cpp:281
virtual void LocateBreadthFirst(SNLocator *locatorPtr) const
Seaches for a particular scene node (breadth first)
Definition: scenenode.cpp:226
void AutoDeleteChildren() const
Deletes (dealocate memory) recursively all children marked as 'autoDelete'.
Definition: scenenode.cpp:85
Header file for V-ART class "SnOperator".
Header file for V-ART class "Joint".
virtual void LocateDepthFirst(SNLocator *locatorPtr) const
Seaches for a particular scene node (depth first)
Definition: scenenode.cpp:206
SceneNode & operator=(const SceneNode &node)
Definition: scenenode.cpp:47
Header file for V-ART class "Transform".
std::list< SceneNode * > GetChilds()
Definition: scenenode.cpp:112
bool Finished() const
Indicates whether the traversal has finished.
Definition: snlocator.h:46
bool RecursiveFindPathTo(SceneNode *targetPtr, SGPath *resultPtr) const
Recursive auxiliar method for FindPathTo.
Definition: scenenode.cpp:134
Header file for V-ART class "SGPath".
static bool recursivePrinting
Definition: scenenode.h:147
void PushFront(SceneNode *nodePtr)
Adds a node to the path beginning.
Definition: sgpath.h:31
bool NotFinished() const
Indicates whether the traversal has not finished.
Definition: snlocator.h:40
virtual bool DrawOGL() const
Recursive drawing using OpenGL commands.
Definition: scenenode.cpp:76
Header file for V-ART class "SNLocator".
SceneNode * FindChildByName(const std::string &name) const
Recusively searches its children for a given name.
Definition: scenenode.cpp:96
Scene Node Locator – used to find a particular object in the scene graph.
Definition: snlocator.h:25
Header file for V-ART class "MeshObject".
virtual ~SceneNode()
Definition: scenenode.cpp:36
bool FindPathTo(SceneNode *targetPtr, SGPath *resultPtr) const
Search target among children.
Definition: scenenode.cpp:119
void Clear()
Erases all elements.
Definition: sgpath.h:29
Scene Graph Path.
Definition: sgpath.h:23
int GetNodeTypeList(TypeID type, std::list< SceneNode * > &nodeList)
Find all the nodes of with typeID 'type'.
Definition: scenenode.cpp:242
bool DetachChild(SceneNode *childPtr)
Removes a child from the child list.
Definition: scenenode.cpp:59
void AddChild(SceneNode &child)
Add a child at the end of child list.
Definition: scenenode.cpp:54