H3D API  2.4.1
TraverseInfo.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 __TRAVERSEINFO_H__
30 #define __TRAVERSEINFO_H__
31 
32 #include <H3D/H3DTypes.h>
33 #include <HAPI/HAPIForceEffect.h>
34 #include <HAPI/HAPIHapticShape.h>
35 #include <H3DUtil/AutoRefVector.h>
36 #include <sstream>
37 #include <stack>
38 #include <map>
39 
40 
41 namespace H3D {
42  // forward declarations
43  class H3DSurfaceNode;
44  class H3DHapticsDevice;
45  class X3DGeometryNode;
46  class X3DLightNode;
47 
48  typedef AutoRefVector< HAPI::HAPIHapticShape > HapticShapeVector;
49  typedef AutoRefVector< HAPI::HAPIForceEffect > HapticEffectVector;
50 
57  class H3DAPI_API TraverseInfo {
58  public:
59 
64 
69  TraverseInfo( const vector< H3DHapticsDevice * > &_haptics_devices );
70 
73  inline unsigned int nrLayers() {
74  if( haptic_shapes.size() > 0 ) {
75  return (unsigned int) haptic_shapes[0].size();
76  } else {
77  return 0;
78  }
79  }
80 
83  inline void initializeLayers( unsigned int nr_layers ) {
84  for( unsigned int i = 0; i < haptic_shapes.size(); ++i ) {
85  if( haptic_shapes[i].size() < nr_layers )
86  haptic_shapes[i].resize( nr_layers );
87  }
88  }
89 
93  inline void setCurrentLayer( unsigned int layer ) {
94  current_layer = layer;
95  }
96 
98  inline unsigned int getCurrentLayer() {
99  return current_layer;
100  }
101 
105  int index = 0;
106  for( vector< H3DHapticsDevice * >::iterator i = haptics_devices.begin();
107  i != haptics_devices.end();
108  ++i, ++index ) {
109  if( (*i) == hd ) return index;
110  }
111  return -1;
112  }
113 
120  void addHapticShape( int device_index,
121  HAPI::HAPIHapticShape *shape );
122 
128  inline void addHapticShape( H3DHapticsDevice *hd,
129  HAPI::HAPIHapticShape *shape ) {
130  int device_index = getHapticsDeviceIndex( hd );
131  addHapticShape( device_index, shape );
132  }
133 
138  void addHapticShapeToAll( HAPI::HAPIHapticShape *shape );
139 
143  inline const HapticShapeVector &getHapticShapes(int device_index ){
144  if( device_index < 0 || device_index >= (int)haptics_devices.size() ) {
145  stringstream s;
146  s << "TraverseInfo only has " << (unsigned int) haptics_devices.size()
147  << " haptics device available. ";
148  throw InvalidHapticsDeviceIndex( device_index,
149  s.str(),
151  }
152  return haptic_shapes[ device_index ][ current_layer ];
153  }
154 
157  inline const HapticShapeVector
159  int device_index = getHapticsDeviceIndex( hd );
160  return getHapticShapes( device_index );
161  }
162 
169  void addForceEffect( int device_index, HAPI::HAPIForceEffect *effect );
170 
176  inline void addForceEffect( H3DHapticsDevice *hd,
177  HAPI::HAPIForceEffect *effect ) {
178  int device_index = getHapticsDeviceIndex( hd );
179  addForceEffect( device_index, effect );
180  }
181 
186  void addForceEffectToAll( HAPI::HAPIForceEffect *effect );
187 
191  inline const HapticEffectVector
192  &getForceEffects( int device_index){
193  if( device_index < 0 || device_index >= (int)haptics_devices.size() ) {
194  stringstream s;
195  s << "TraverseInfo only has " << (unsigned int)haptics_devices.size()
196  << " haptics device available. ";
197  throw InvalidHapticsDeviceIndex( device_index,
198  s.str(),
200  }
201  return haptic_effects[ device_index ];
202  }
203 
206  inline const HapticEffectVector
208  int device_index = getHapticsDeviceIndex( hd );
209  return getForceEffects( device_index );
210  }
211 
213  inline void setCurrentSurface( H3DSurfaceNode *s ) {
214  current_surface = s;
215  }
216 
219  return current_surface;
220  }
221 
222  inline H3DHapticsDevice *getHapticsDevice( int device_index ) {
223  return haptics_devices[ device_index ];
224  }
225 
227  inline const vector< H3DHapticsDevice * > &getHapticsDevices() {
228  return haptics_devices;
229  }
230 
233  inline void disableHaptics( int i = -1 ) {
234  if( i == -1 ) {
235  for( unsigned int j = 0; j < haptics_enabled.size(); ++j ) {
236  haptics_enabled[j] = false;
237  }
238  } else {
239  haptics_enabled[i] = false;
240  }
241  }
242 
245  inline void enableHaptics( int i = -1 ) {
246  if( i == -1 ) {
247  for( unsigned int j = 0; j < haptics_enabled.size(); ++j ) {
248  haptics_enabled[j] = true;
249  }
250  } else {
251  haptics_enabled[i] = true;
252  }
253  }
254 
260  inline void disableGraphics() {
261  graphics_enabled = false;
262  }
263 
268  inline void enableGraphics() {
269  graphics_enabled = true;
270  }
271 
277  inline void setMultiPassTransparency( bool b ) {
278  multi_pass_transparency = b;
279  }
280 
282  inline bool getMultiPassTransparency() {
283  return multi_pass_transparency;
284  }
285 
293  inline bool hapticsEnabled( unsigned int i = 0 ) {
294  if( i < haptics_enabled.size() ) {
295  return haptics_enabled[i];
296  } else {
297  return false;
298  }
299  }
300 
302  inline bool hapticsEnabledForAll() {
303  for( unsigned int i = 0; i < haptics_enabled.size(); ++i ) {
304  if( !haptics_enabled[i] ) return false;
305  }
306  return true;
307  }
308 
310  inline bool hapticsDisabledForAll() {
311  for( unsigned int i = 0; i < haptics_enabled.size(); ++i ) {
312  if( haptics_enabled[i] ) return false;
313  }
314  return true;
315  }
316 
319  inline const vector< bool >&getHapticsEnabled() {
320  return haptics_enabled;
321  }
322 
327  inline int setHapticsEnabled( const vector< bool > &enabled) {
328  if( enabled.size() != haptics_enabled.size() ) return -1;
329  else {
330  haptics_enabled = enabled;
331  return 0;
332  }
333  }
334 
336  inline bool graphicsEnabled() {
337  return graphics_enabled;
338  }
339 
347  inline void pushMatrices( const Matrix4f &forward,
348  const Matrix4f &inverse ) {
349  const TransformInfo &top = transform_stack.top();
350  transform_stack.push( TransformInfo( top.acc_frw * forward,
351  inverse * top.acc_inv ) );
352  }
353 
356  inline void popMatrices() {
357  transform_stack.pop();
358  }
359 
363  inline const Matrix4f &getAccForwardMatrix() {
364  return transform_stack.top().acc_frw;
365  }
366 
370  inline const Matrix4f &getAccInverseMatrix() {
371  return transform_stack.top().acc_inv;
372  }
373 
376  inline int getGeometryCount( X3DGeometryNode *geom ) {
377  return geometry_count[geom];
378  }
379 
382  inline void setUserData( const string &key, void *data ) {
383  user_data[key] = data;
384  }
385 
387  inline bool haveUserData( const string &key ) {
388  return user_data.find( key ) != user_data.end();
389  }
390 
392  inline int deleteUserData( const string &key ) {
393  std::map<string, void *>::iterator i = user_data.find( key );
394  if( i != user_data.end() ) {
395  user_data.erase( i );
396  return 0;
397  } else {
398  return -1;
399  }
400  }
401 
404  inline int getUserData( const string &key, void ** data ) {
405  std::map<string, void *>::iterator i = user_data.find( key );
406  if( i != user_data.end() ) {
407  *data = (*i).second;
408  return 0;
409  } else {
410  return -1;
411  }
412  }
413 
415  typedef void (*CallbackFunc)(TraverseInfo &ti, void *data);
416 
419  inline void addPostTraverseCallback( CallbackFunc func, void *data ) {
420  post_traverse_callbacks.push_back( make_pair( func, data ) );
421  }
422 
424  void callPostTraverseCallbacks();
425 
427  class H3DAPI_API LightInfo {
428  public:
430  LightInfo( X3DLightNode *, const Matrix4f &transform );
432  X3DLightNode *getLight() const;
434  inline const Matrix4f &getLightTransform() const { return transform; }
435  protected:
436  AutoRef< Node > light;
437  Matrix4f transform;
438  };
439 
441  typedef vector< LightInfo > LightVector;
442 
447  void addActiveLightNode( X3DLightNode *light, const Matrix4f &transform );
448 
451  void removeActiveLightNode( X3DLightNode *light, const Matrix4f &transform );
452 
454  const LightVector &getActiveLightNodes();
455 
456  protected:
457  LightVector x3dlightnode_vector;
458 
459  class TransformInfo {
460  public:
461  TransformInfo( const Matrix4f &_acc_frw,
462  const Matrix4f &_acc_inv ):
463  acc_frw( _acc_frw ),
464  acc_inv( _acc_inv ) {}
465  Matrix4f acc_frw, acc_inv;
466  };
467  stack< TransformInfo > transform_stack;
468 
469  typedef std::list< std::pair< CallbackFunc, void * > > CallbackList;
470  // A list of the callback functions to run after scene traversal.
471  CallbackList post_traverse_callbacks;
472 
473  unsigned int current_layer;
474  H3DSurfaceNode *current_surface;
475  vector< H3DHapticsDevice * > haptics_devices;
476  vector< vector< HapticShapeVector > > haptic_shapes;
477  vector< HapticEffectVector > haptic_effects;
478  bool graphics_enabled;
479  bool multi_pass_transparency;
480  // When the global haptics_enabled is true the following vector contains
481  // per devceinformation about which devices have haptics enabled.
482  vector< bool > haptics_enabled;
483 
484  typedef std::map< X3DGeometryNode *, int > GeometryCountMap;
485  GeometryCountMap geometry_count;
486  std::map< string, void * > user_data;
487  };
488 
489 }
490 
491 #endif
#define H3D_FULL_LOCATION
Include this file to include the H3D specific types.
size_type size() const
Base class for all haptic devices.
Definition: H3DHapticsDevice.h:68
Base class for all surfaces.
Definition: H3DSurfaceNode.h:44
Thrown when trying to get hold of a H3DHapticsDevice using an invalid device index.
Definition: TraverseInfo.h:63
Class grouping X3DLightNode and its transformation matrix.
Definition: TraverseInfo.h:427
const Matrix4f & getLightTransform() const
Get the transformation matrix for the light.
Definition: TraverseInfo.h:434
TraverseInfo is a structure that is passed along when traversing the scene graph.
Definition: TraverseInfo.h:57
void enableGraphics()
Indicate that this node will be part of the graphics rendering traversal.
Definition: TraverseInfo.h:268
const vector< H3DHapticsDevice * > & getHapticsDevices()
Get the haptics devices that are available.
Definition: TraverseInfo.h:227
int getUserData(const string &key, void **data)
Get the user data value with the given key.
Definition: TraverseInfo.h:404
H3DSurfaceNode * getCurrentSurface()
Get the surface that is active in the current traversal.
Definition: TraverseInfo.h:218
const Matrix4f & getAccInverseMatrix()
Get the accumulated inverse transformation matrix, i.e.
Definition: TraverseInfo.h:370
bool hapticsEnabledForAll()
Returns true if haptics is enabled for all haptics devices.
Definition: TraverseInfo.h:302
void setCurrentSurface(H3DSurfaceNode *s)
Sets the surface that is active in the current traversal.
Definition: TraverseInfo.h:213
void setMultiPassTransparency(bool b)
If multi pass transparency is set to true the scene will be rendered three times graphically,...
Definition: TraverseInfo.h:277
vector< LightInfo > LightVector
Used to get all the X3DLightNodes that are global.
Definition: TraverseInfo.h:441
bool haveUserData(const string &key)
Returns true of user data with the given key exists.
Definition: TraverseInfo.h:387
const HapticShapeVector & getHapticShapes(H3DHapticsDevice *hd)
Get the HapticShapes that has been added for the given H3DHapticsDevice.
Definition: TraverseInfo.h:158
void enableHaptics(int i=-1)
When called, the addHapticShapes() and addForceEffects() functions calls will add HapticShapes to ren...
Definition: TraverseInfo.h:245
bool hapticsDisabledForAll()
Returns true if haptics is disabled for all haptics devices.
Definition: TraverseInfo.h:310
unsigned int nrLayers()
Returns the number of haptics layers that are available and initialized.
Definition: TraverseInfo.h:73
const HapticEffectVector & getForceEffects(H3DHapticsDevice *hd)
Get the HAPIForceEffects that has been added for the given H3DHapticsDevice.
Definition: TraverseInfo.h:207
void addHapticShape(H3DHapticsDevice *hd, HAPI::HAPIHapticShape *shape)
Adds a HapticShape to be rendered by a H3DHapticsDevice.
Definition: TraverseInfo.h:128
const vector< bool > & getHapticsEnabled()
Get a vector the same size as the haptics device vector indicating if haptics is disabled for that de...
Definition: TraverseInfo.h:319
int getHapticsDeviceIndex(H3DHapticsDevice *hd)
Returns the index of the given H3DHapticsDevice.
Definition: TraverseInfo.h:104
const HapticEffectVector & getForceEffects(int device_index)
Get the HAPIForceEffects that has been added for the H3DHapticsDevice with the given device_index.
Definition: TraverseInfo.h:192
bool getMultiPassTransparency()
See if multi pass transparency objects have been encountered.
Definition: TraverseInfo.h:282
int setHapticsEnabled(const vector< bool > &enabled)
Set a vector indicating for each haptics device if haptics is disabled or enabled.
Definition: TraverseInfo.h:327
bool graphicsEnabled()
Check whether graphics is enabled or not.
Definition: TraverseInfo.h:336
void setUserData(const string &key, void *data)
Set user data in the traversinfo object.
Definition: TraverseInfo.h:382
int deleteUserData(const string &key)
Delete user data with the given key. Returns 0 on success.
Definition: TraverseInfo.h:392
const Matrix4f & getAccForwardMatrix()
Get the accumulated forward transformation matrix, i.e.
Definition: TraverseInfo.h:363
void disableHaptics(int i=-1)
When called, future calls to addHapticShapes() and addForceEffects() functions will not add any shape...
Definition: TraverseInfo.h:233
const HapticShapeVector & getHapticShapes(int device_index)
Get the HapticShapes that has been added for the H3DHapticsDevice with the given device_index.
Definition: TraverseInfo.h:143
void addForceEffect(H3DHapticsDevice *hd, HAPI::HAPIForceEffect *effect)
Adds a HAPIForceEffect to be rendered by a H3DHapticsDevice.
Definition: TraverseInfo.h:176
void setCurrentLayer(unsigned int layer)
Set the currently active haptic layer.
Definition: TraverseInfo.h:93
void initializeLayers(unsigned int nr_layers)
Make sure that the TraverseInfo object have enough resources allocated for the given amount of haptic...
Definition: TraverseInfo.h:83
bool hapticsEnabled(unsigned int i=0)
Check whether haptics is enabled for a device index or not.
Definition: TraverseInfo.h:293
void pushMatrices(const Matrix4f &forward, const Matrix4f &inverse)
This function should be called when we move into a nested coordinate space with the transformation ma...
Definition: TraverseInfo.h:347
void disableGraphics()
Indicate that this node will not be part of the graphics rendering traversal.
Definition: TraverseInfo.h:260
void addPostTraverseCallback(CallbackFunc func, void *data)
Adds a callback function that will be called after the entire scene has been traversed
Definition: TraverseInfo.h:419
int getGeometryCount(X3DGeometryNode *geom)
Returns the number of times the given geometry has been added as the source of a haptic shape so far ...
Definition: TraverseInfo.h:376
unsigned int getCurrentLayer()
Get the currently active haptic layer.
Definition: TraverseInfo.h:98
void popMatrices()
This function should be called when we move out from a nested coordinate space.
Definition: TraverseInfo.h:356
This is the base node type for all geometry in X3D.
Definition: X3DGeometryNode.h:61
The X3DLightNode abstract node type is the base type from which all node types that serve as light so...
Definition: X3DLightNode.h:70
H3D_VALUE_EXCEPTION(string, InvalidType)
An exception thrown when a field is of the wrong type when it is checked.
H3D API namespace.
Definition: Anchor.h:38