H3D API  2.4.1
Node.h
Go to the documentation of this file.
1 // Copyright 2004-2019, SenseGraphics AB
3 //
4 // This file is part of H3D API.
5 //
6 // H3D API is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // H3D API is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with H3D API; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // A commercial license is also available. Please contact us at
21 // www.sensegraphics.com for more information.
22 //
23 //
24 //
27 //
29 #ifndef __NODE_H__
30 #define __NODE_H__
31 
32 #include <H3D/Instantiate.h>
33 #include <H3D/H3DNodeDatabase.h>
34 #include <H3D/TraverseInfo.h>
36 #include <H3DUtil/Console.h>
37 
38 namespace H3D {
39  class X3DPrototypeInstance;
40 
46  class H3DAPI_API Node: public RefCountedClass {
47  public:
48 
49  typedef HAPI::Collision::IntersectionInfo IntersectionInfo;
50 
53  typedef std::map < Node*, Node* > DeepCopyMap;
54 
56  Node();
57 
71  virtual Node* clone ( bool deepCopy= true, DeepCopyMap *deepCopyMap = NULL );
72 
74  virtual ~Node();
75 
76  template< class N >
77  static Node *newInstance() { return new N; }
78 
83  return proto_parent;
84  }
85 
89  proto_parent = p;
90  }
91 
94  virtual void render() { };
95 
103  virtual void traverseSG( TraverseInfo &ti );
104 
114  struct H3DAPI_API NodeIntersectResult {
115  // Constructor.
116  NodeIntersectResult( void * _user_data = 0,
117  bool _override_no_collision = false,
118  bool _collide_invisible = false ) :
119  user_data( _user_data ),
120  override_no_collision( _override_no_collision ),
121  collide_invisible( _collide_invisible ) {
122  current_matrix.push( Matrix4f() );
123  }
124 
127  vector< Node * > theNodes;
128 
134  vector< IntersectionInfo > result;
135 
138  void * user_data;
139 
143 
148 
151  inline void addTransform() {
152  geometry_transforms.push_back( getCurrentTransform() );
153  }
154 
156  inline const vector< Matrix4f > &getGeometryTransforms() {
157  return geometry_transforms;
158  }
159 
162  inline const Matrix4f &getCurrentTransform() {
163  return current_matrix.top();
164  }
165 
168  inline void pushTransform( const Matrix4f &matrix ) {
169  current_matrix.push( current_matrix.top() * matrix );
170  }
171 
173  inline void popTransform() {
174  current_matrix.pop();
175  }
176 
179  inline void transformResult() {
180  for( unsigned int i = 0; i < result.size(); ++i ) {
181  result[i].point = geometry_transforms[i] * result[i].point;
182  result[i].normal = geometry_transforms[i].getRotationPart() *
183  result[i].normal;
184  }
185  }
186 
188  inline void addResults( IntersectionInfo &temp_result,
189  Node * the_node,
190  bool add_transform = true ) {
191  result.push_back( temp_result );
192  theNodes.push_back( the_node );
193  if( add_transform )
194  addTransform();
195  }
196 
199  virtual void clearData() {
200  theNodes.clear();
201  result.clear();
202  geometry_transforms.clear();
203  while( !current_matrix.empty() )
204  current_matrix.pop();
205  current_matrix.push( Matrix4f() );
206  }
207 
208  protected:
212  vector< Matrix4f > geometry_transforms;
213 
217  stack< Matrix4f > current_matrix;
218 
219  };
220 
224  struct H3DAPI_API LineIntersectResult : public NodeIntersectResult {
225 
227  LineIntersectResult( bool _override_no_collision = false,
228  bool _detect_pt_device = false,
229  void *_user_data = 0,
230  bool _collide_invisible = false ) :
231  NodeIntersectResult( _user_data, _override_no_collision,
232  _collide_invisible ),
233  detect_pt_device( _detect_pt_device ) {}
234 
238 
243  struct H3DAPI_API PointingDeviceResultStruct {
244  list< Node * > x3dptd;
245  list< Node * > lowest_enabled;
246  Matrix4f global_to_local;
247  };
248 
251  PointingDeviceResultStruct temp_struct;
252  if( !current_pt_device.empty() )
253  temp_struct = current_pt_device.top();
254  current_pt_device.push( tmp );
255  list< Node * > &tmp_list = current_pt_device.top().x3dptd;
256  for( list< Node * >::iterator i = tmp_list.begin();
257  i != tmp_list.end(); ++i ) {
258  current_pt_device.top().lowest_enabled.push_back( *i );
259  }
260  tmp_list.insert( tmp_list.end(),
261  temp_struct.x3dptd.begin(),
262  temp_struct.x3dptd.end() );
263  }
264 
266  inline void popCurrentPtDevice() {
267  current_pt_device.pop();
268  }
269 
271  inline void addPtDevMap() {
272  if( detect_pt_device && !current_pt_device.empty() ) {
273  geom_ptd_map[ (unsigned int) result.size() - 1 ] = current_pt_device.top();
274  }
275  }
276 
277  typedef map< unsigned int,
278  PointingDeviceResultStruct > GeomX3DPtdMap;
279 
283  GeomX3DPtdMap geom_ptd_map;
284 
287  virtual void clearData() {
288  NodeIntersectResult::clearData();
289  while( !current_pt_device.empty() )
290  current_pt_device.pop();
291  }
292 
295  return !current_pt_device.empty();
296  }
297 
298  protected:
301  stack< PointingDeviceResultStruct > current_pt_device;
302  };
303 
310  virtual bool lineIntersect( const Vec3f &from,
311  const Vec3f &to,
312  LineIntersectResult &result );
313 
319  virtual void closestPoint( const Vec3f &p,
320  NodeIntersectResult &result );
321 
330  virtual bool movingSphereIntersect( H3DFloat radius,
331  const Vec3f &from,
332  const Vec3f &to,
333  NodeIntersectResult &result );
334 
338  virtual string defaultXMLContainerField() {
339  return "children";
340  }
341 
343  virtual Field *getField( const string &_name ) const;
344 
347  int addDestructCallback( void (*func)( Node *, void * ), void *args );
348 
351  int removeDestructCallback( void (*func)( Node *, void * ), void *args );
352 
353  static int getClosestPointIndex( NodeIntersectResult* result, Vec3f& point ){
354  H3DDouble dist = 10000.0;
355  unsigned int index = 0;
356  for( unsigned int i = 0; i < result->result.size(); ++i ) {
357  H3DDouble d = (result->result[i].point - point).length();
358  if( d < dist ){
359  dist = d;
360  index = i;
361  }
362  }
363  return index;
364  };
365 
366 
367  friend class Field;
368  template <class Type>
369  friend class SField;
370  template <class Type>
371  friend class MField;
372  template<class RefClass, class BaseField>
373  friend class RefCountSField;
374 
376  inline static unsigned int nrNodesAlive() {
377  return nr_nodes_alive;
378  }
379 
381  inline static int nrNodesCreated() {
382  return nr_nodes_created;
383  }
384 
385  protected:
386 
397  static Node* getClonedInstance ( Node* original, bool deepCopy, DeepCopyMap& deepCopyMap );
398 
403  void cloneFieldValue ( Field& _from, Field& _to, bool deepCopy, DeepCopyMap& deepCopyMap );
404 
405  static H3DNodeDatabase database;
406  int id;
407  static int nr_nodes_created;
408  typedef vector< pair< void (*)(Node *, void *), void * > > DestructCallbacks;
409  DestructCallbacks destruct_callbacks;
410  X3DPrototypeInstance *proto_parent;
411 
412  // the number of nodes which have been constructed but not destructed yet.
413  static unsigned int nr_nodes_alive;
414 
415  };
416 
417 }
418 
419 #endif
Header file for H3DNodeDatabase.
Contains the Inst struct.
Header file for TraverseInfo.
The Field class.
Definition: Field.h:46
Node is the base class for all classes that can be part of the H3D scene-graph.
Definition: Node.h:46
std::map< Node *, Node * > DeepCopyMap
A map type used during a deep copy clone() to map from original nodes to cloned nodes.
Definition: Node.h:53
static int nrNodesCreated()
Returns the number of nodes created in total since the start of the program.
Definition: Node.h:381
virtual void render()
the render() function is used for the depth-first rendering traversal of the scene-graph.
Definition: Node.h:94
X3DPrototypeInstance * getProtoInstanceParent()
If this node is the root node in the proto body of a X3DPrototypeInstance then this will return that ...
Definition: Node.h:82
virtual string defaultXMLContainerField()
Returns the default xml containerField attribute value.
Definition: Node.h:338
static unsigned int nrNodesAlive()
Returns the nr of nodes currently alive, i.e. nodes created but not destructed.
Definition: Node.h:376
void setProtoInstanceParent(X3DPrototypeInstance *p)
Set the X3DPrototypeInstance this node is the the root node if applicaple.
Definition: Node.h:88
TraverseInfo is a structure that is passed along when traversing the scene graph.
Definition: TraverseInfo.h:57
This abstract node type is the base type for all prototype instances in the X3D system.
Definition: X3DPrototypeInstance.h:65
H3DDouble length() const
double H3DDouble
float H3DFloat
H3D API namespace.
Definition: Anchor.h:38
The H3DNodeDatabase contains a mapping between a name of a Node and the constructor for the Node with...
Definition: H3DNodeDatabase.h:194
Contains a list of Nodes (X3DPointingDeviceNodes) and a transformation matrix from global space to lo...
Definition: Node.h:243
Used as input to lineIntersect functions.
Definition: Node.h:224
virtual void clearData()
Clears data so the struct can be used in a closestPoint function again.
Definition: Node.h:287
bool hasCurrentPointingDevice()
Check if there aer any pointing device sensors added.
Definition: Node.h:294
stack< PointingDeviceResultStruct > current_pt_device
The top of the stack contains the X3DPointingDeviceNodes that is to be considered.
Definition: Node.h:301
bool detect_pt_device
Flag used to know if lineintersect should bother with keeping track of X3DPointingDeviceSensorNodes.
Definition: Node.h:237
GeomX3DPtdMap geom_ptd_map
Used in order to keep track of DEF/USE feature of X3D.
Definition: Node.h:283
LineIntersectResult(bool _override_no_collision=false, bool _detect_pt_device=false, void *_user_data=0, bool _collide_invisible=false)
Constructor.
Definition: Node.h:227
void addPtDevMap()
Add current pointing device struct to geom_ptd_map.
Definition: Node.h:271
void pushCurrentPtDevice(PointingDeviceResultStruct &tmp)
Push struct to current_pt_device stack.
Definition: Node.h:250
void popCurrentPtDevice()
Pop struct from current_pt_device stack.
Definition: Node.h:266
Used as input to intersection functions.
Definition: Node.h:114
bool override_no_collision
Flag used to know if collision function should be called for the children in the Collision Node regar...
Definition: Node.h:142
vector< Node * > theNodes
A vector of pointers to nodes.
Definition: Node.h:127
void addResults(IntersectionInfo &temp_result, Node *the_node, bool add_transform=true)
Convenience function to add results to the struct.
Definition: Node.h:188
virtual void clearData()
Clears data so the struct can be used in a closestPoint function again.
Definition: Node.h:199
void * user_data
Optional user_data in case someone want to do add some extra feature to collision functions in one or...
Definition: Node.h:138
void popTransform()
Remove the top on the stack of current transforms.
Definition: Node.h:173
const Matrix4f & getCurrentTransform()
Get the current matrix that transforms from local coordinate space to global coordinate space.
Definition: Node.h:162
const vector< Matrix4f > & getGeometryTransforms()
Get the info in geometry_transforms.
Definition: Node.h:156
vector< IntersectionInfo > result
A vector of HAPI::IntersectionInfo that stores result of intersection such as point and normal.
Definition: Node.h:134
void transformResult()
Transforms point and normal in the IntersectionInfo vector from local to global space.
Definition: Node.h:179
vector< Matrix4f > geometry_transforms
A vector of matrices from the local coordinate space to global space for each node that the line inte...
Definition: Node.h:212
stack< Matrix4f > current_matrix
The top of the stack is the current matrix that transforms from the local coordinate space where this...
Definition: Node.h:217
void pushTransform(const Matrix4f &matrix)
Push onto stack of current transforms.
Definition: Node.h:168
bool collide_invisible
Flag used to know if collision function should be called for the children in a ToogleGroup Node even ...
Definition: Node.h:147
void addTransform()
Adds the current transform from local coordinate space to global coordinate space to geometry_transfo...
Definition: Node.h:151