00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00027
00029 #ifndef __TRAVERSEINFO_H__
00030 #define __TRAVERSEINFO_H__
00031
00032 #include "H3DTypes.h"
00033 #include "HapticForceEffect.h"
00034 #include "HapticShape.h"
00035 #include "AutoRefVector.h"
00036 #include <sstream>
00037 #include <stack>
00038
00039 using namespace std;
00040
00041 namespace H3D {
00042
00043 class HapticShape;
00044 class H3DSurfaceNode;
00045 class H3DHapticsDevice;
00046
00047 typedef AutoRefVector< HapticShape > HapticShapeVector;
00048 typedef AutoRefVector< HapticForceEffect > HapticEffectVector;
00049
00056 class H3DAPI_API TraverseInfo {
00057 public:
00058
00062 H3D_VALUE_EXCEPTION( int, InvalidHapticsDeviceIndex );
00063
00068 TraverseInfo( const vector< H3DHapticsDevice * > &_haptics_devices ) :
00069 current_surface( NULL ),
00070 haptics_devices( _haptics_devices ),
00071 haptic_shapes( _haptics_devices.size() ),
00072 haptic_effects( _haptics_devices.size() ),
00073 haptics_enabled( true ) {
00074
00075 transform_stack.push( TransformInfo( Matrix4f(), Matrix4f () ) );
00076 }
00077
00080 inline int getHapticsDeviceIndex( H3DHapticsDevice *hd ) {
00081 int index = 0;
00082 for( vector< H3DHapticsDevice * >::iterator i = haptics_devices.begin();
00083 i != haptics_devices.end();
00084 i++, index++ ) {
00085 if( (*i) == hd ) return index;
00086 }
00087 return -1;
00088 }
00089
00096 void addHapticShape( int device_index, HapticShape *shape );
00097
00103 inline void addHapticShape( H3DHapticsDevice *hd, HapticShape *shape ) {
00104 int device_index = getHapticsDeviceIndex( hd );
00105 addHapticShape( device_index, shape );
00106 }
00107
00112 void addHapticShapeToAll( HapticShape *shape );
00113
00117 inline const HapticShapeVector &getHapticShapes(int device_index){
00118 if( device_index < 0 || device_index >= (int)haptics_devices.size() ) {
00119 stringstream s;
00120 s << "TraverseInfo only has " << (unsigned int) haptics_devices.size()
00121 << " haptics device available. ";
00122 throw InvalidHapticsDeviceIndex( device_index,
00123 s.str(),
00124 H3D_FULL_LOCATION );
00125 }
00126 return haptic_shapes[ device_index ];
00127 }
00128
00131 inline const HapticShapeVector
00132 &getHapticShapes( H3DHapticsDevice * hd ) {
00133 int device_index = getHapticsDeviceIndex( hd );
00134 return getHapticShapes( device_index );
00135 }
00136
00143 void addForceEffect( int device_index, HapticForceEffect *effect );
00144
00150 inline void addForceEffect( H3DHapticsDevice *hd,
00151 HapticForceEffect *effect ) {
00152 int device_index = getHapticsDeviceIndex( hd );
00153 addForceEffect( device_index, effect );
00154 }
00155
00160 void addForceEffectToAll( HapticForceEffect *effect );
00161
00165 inline const HapticEffectVector
00166 &getForceEffects( int device_index){
00167 if( device_index < 0 || device_index >= (int)haptics_devices.size() ) {
00168 stringstream s;
00169 s << "TraverseInfo only has " << (unsigned int)haptics_devices.size()
00170 << " haptics device available. ";
00171 throw InvalidHapticsDeviceIndex( device_index,
00172 s.str(),
00173 H3D_FULL_LOCATION );
00174 }
00175 return haptic_effects[ device_index ];
00176 }
00177
00180 inline const HapticEffectVector
00181 &getForceEffects( H3DHapticsDevice * hd ) {
00182 int device_index = getHapticsDeviceIndex( hd );
00183 return getForceEffects( device_index );
00184 }
00185
00187 inline void setCurrentSurface( H3DSurfaceNode *s ) {
00188 current_surface = s;
00189 }
00190
00192 inline H3DSurfaceNode * getCurrentSurface() {
00193 return current_surface;
00194 }
00195
00196 inline H3DHapticsDevice *getHapticsDevice( int device_index ) {
00197 return haptics_devices[ device_index ];
00198 }
00199
00201 inline const vector< H3DHapticsDevice * > &getHapticsDevices() {
00202 return haptics_devices;
00203 }
00204
00207 inline void disableHaptics() {
00208 haptics_enabled = false;
00209 }
00210
00213 inline void enableHaptics() {
00214 haptics_enabled = true;
00215 }
00216
00224 inline bool hapticsEnabled() {
00225 return haptics_enabled;
00226 }
00227
00235 inline void pushMatrices( const Matrix4f &forward,
00236 const Matrix4f &inverse ) {
00237 const TransformInfo &top = transform_stack.top();
00238 transform_stack.push( TransformInfo( top.acc_frw * forward,
00239 inverse * top.acc_inv ) );
00240 }
00241
00244 inline void popMatrices() {
00245 transform_stack.pop();
00246 }
00247
00251 inline const Matrix4f &getAccForwardMatrix() {
00252 return transform_stack.top().acc_frw;
00253 }
00254
00258 inline const Matrix4f &getAccInverseMatrix() {
00259 return transform_stack.top().acc_inv;
00260 }
00261
00262 protected:
00263 class TransformInfo {
00264 public:
00265 TransformInfo( const Matrix4f &_acc_frw,
00266 const Matrix4f &_acc_inv ):
00267 acc_frw( _acc_frw ),
00268 acc_inv( _acc_inv ) {}
00269 Matrix4f acc_frw, acc_inv;
00270 };
00271 stack< TransformInfo > transform_stack;
00272
00273 H3DSurfaceNode *current_surface;
00274 vector< H3DHapticsDevice * > haptics_devices;
00275 vector< HapticShapeVector > haptic_shapes;
00276 vector< HapticEffectVector > haptic_effects;
00277 bool haptics_enabled;
00278 };
00279
00280 };
00281
00282 #endif