H3D API  2.4.1
SpaceWareSensor.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 //
27 //
29 #ifndef __SPACEWARESENSOR_H__
30 #define __SPACEWARESENSOR_H__
31 
32 
33 #include <H3D/X3DSensorNode.h>
34 #include <list>
35 #include <H3D/SFVec3f.h>
36 #include <H3D/SFFloat.h>
37 #include <H3D/SFInt32.h>
38 #include <H3D/SFRotation.h>
39 #include <H3DUtil/Threads.h>
40 
41 namespace H3D {
42 
55  class H3DAPI_API SpaceWareSensor : public X3DSensorNode {
56  public:
57 
64  struct H3DAPI_API ScaleRotation:
65  public TypedField< SFRotation,
66  Types< SFRotation, SFFloat > > {
67  virtual void update() {
68  Rotation r =
69  static_cast< SFRotation * >( routes_in[0] )->getValue();
70  H3DFloat s =
71  static_cast< SFFloat * >( routes_in[1] )->getValue();
72 
73  value = r * s;
74  }
75  };
76 
82  struct H3DAPI_API ScaleTranslation:
83  public TypedField< SFVec3f,
84  Types< SFVec3f, SFFloat > > {
85  virtual void update() {
86  const Vec3f &r = static_cast< SFVec3f * >( routes_in[0] )->getValue();
87  H3DFloat s = static_cast< SFFloat * >( routes_in[1] )->getValue();
88  value = r * s;
89  }
90  };
91 
92 
97  struct H3DAPI_API AccumulateRotation:
98  public AutoUpdate< SFRotation > {
99  AccumulateRotation() { last_time = TimeStamp(); }
100 
101  virtual void setValue( const Rotation &r, int _id = 0 ) {
102  SFRotation::setValue( r, _id );
103  last_time = TimeStamp();
104  }
105 
106  virtual void update() {
107  H3DTime current_time = TimeStamp();
108  H3DTime time_scale = 1;
109  if( static_cast< SpaceWareSensor * >(owner)->accumulateTimeDependent->getValue() )
110  time_scale = current_time - last_time;
111  last_time = current_time;
112  Rotation r = static_cast< SFRotation * >( event.ptr )->getValue();
113  value = ( r * time_scale ) * value;
114  }
115  protected:
116  H3DTime last_time;
117  };
118 
124  class H3DAPI_API ResetAccumulatedRotation:
125  public AutoUpdate< SFRotation > {
126 
127  public:
128  virtual void setValue( const Rotation &r, int _id = 0 ) {
129  SFRotation::setValue( r, _id );
130  SpaceWareSensor * sws = static_cast< SpaceWareSensor * >(owner);
131  sws->accumulatedRotation->setValue( r, sws->id );
132  Rotation temp_r = r;
133  Vec3f euler_angles = temp_r.toEulerAngles();
134  sws->accumulatedYaw->setValue(
135  Rotation( 1, 0, 0, euler_angles.x ), sws->id );
136  sws->accumulatedPitch->setValue(
137  Rotation( 0, 1, 0, euler_angles.y ), sws->id );
138  sws->accumulatedRoll->setValue(
139  Rotation( 0, 0, 1, euler_angles.z ), sws->id );
140  }
141 
142  protected:
143  virtual void update() {
145  SpaceWareSensor * sws = static_cast< SpaceWareSensor * >(owner);
146  sws->accumulatedRotation->setValue( value, sws->id );
147  Rotation temp_r = value;
148  Vec3f euler_angles = temp_r.toEulerAngles();
149  sws->accumulatedYaw->setValue(
150  Rotation( 1, 0, 0, euler_angles.x ), sws->id );
151  sws->accumulatedPitch->setValue(
152  Rotation( 0, 1, 0, euler_angles.y ), sws->id );
153  sws->accumulatedRoll->setValue(
154  Rotation( 0, 0, 1, euler_angles.z ), sws->id );
155  }
156  };
157 #ifdef __BORLANDC__
158  friend class ResetAccumulatedRotation;
159 #endif
160 
165  struct H3DAPI_API AccumulateTranslation:
166  public AutoUpdate< SFVec3f > {
167  AccumulateTranslation() { last_time = TimeStamp(); }
168 
169  virtual void setValue( const Vec3f &v, int _id = 0 ) {
170  SFVec3f::setValue( v, _id );
171  last_time = TimeStamp();
172  }
173 
174  virtual void update() {
175  H3DTime current_time = TimeStamp();
176  H3DTime time_scale = 1;
177  if( static_cast< SpaceWareSensor * >(owner)->accumulateTimeDependent->getValue() )
178  time_scale = ( current_time - last_time ) * 0.025f;
179  last_time = current_time;
180  const Vec3f &v = static_cast< SFVec3f * >( event.ptr )->getValue();
181  value = value + v * time_scale;
182  }
183  protected:
184  H3DTime last_time;
185  };
186 
190  class H3DAPI_API ResetAccumulatedTranslation:
191  public AutoUpdate< SFVec3f > {
192  public:
193  virtual void setValue( const Vec3f &v, int _id = 0 ) {
194  SFVec3f::setValue( v, _id );
195  SpaceWareSensor * sws = static_cast< SpaceWareSensor * >(owner);
196  sws->accumulatedTranslation->setValue( v, sws->id );
197  }
198  protected:
199  virtual void update() {
200  SFVec3f::update();
201  SpaceWareSensor * sws = static_cast< SpaceWareSensor * >(owner);
202  sws->accumulatedTranslation->setValue( value, sws->id );
203  }
204  };
205 #ifdef __BORLANDC__
206  friend class ResetAccumulatedTranslation;
207 #endif
208 
211  Inst< SFBool > _enabled = 0,
212  Inst< SFNode > _metadata = 0,
213  Inst< SFBool > _isActive = 0,
214  Inst< SFVec3f > _rawTranslation = 0,
215  Inst< SFRotation > _rawYaw = 0,
216  Inst< SFRotation > _rawPitch = 0,
217  Inst< SFRotation > _rawRoll = 0,
218  Inst< SFRotation > _rawRotation = 0,
219  Inst< ScaleTranslation > _instantTranslation = 0,
220  Inst< ScaleRotation > _instantYaw = 0,
221  Inst< ScaleRotation > _instantPitch = 0,
222  Inst< ScaleRotation > _instantRoll = 0,
223  Inst< ScaleRotation > _instantRotation = 0,
224  Inst< AccumulateTranslation > _accumulatedTranslation = 0,
225  Inst< AccumulateRotation > _accumulatedYaw = 0,
226  Inst< AccumulateRotation > _accumulatedPitch = 0,
227  Inst< AccumulateRotation > _accumulatedRoll = 0,
228  Inst< AccumulateRotation > _accumulatedRotation = 0,
229  Inst< SFFloat > _translationScale = 0,
230  Inst< SFFloat > _rotationScale = 0,
231  Inst< SFInt32 > _buttons = 0,
232  Inst< SFInt32 > _latestButtonPress = 0,
233  Inst< SFInt32 > _latestButtonRelease = 0,
234  Inst< SFBool > _accumulateTimeDependent = 0,
235  Inst< ResetAccumulatedTranslation > _resetAccumulatedTranslation = 0,
236  Inst< ResetAccumulatedRotation > _resetAccumulatedRotation = 0
237  );
238 
239  ~SpaceWareSensor() {
240  if( this == sws_instance )
241  sws_instance = 0;
242  }
243 
249  H3DUniquePtr< SFVec3f> rawTranslation;
250 
257  H3DUniquePtr< SFRotation> rawYaw;
258 
265  H3DUniquePtr< SFRotation> rawPitch;
266 
273  H3DUniquePtr< SFRotation> rawRoll;
274 
281  H3DUniquePtr< SFRotation> rawRotation;
282 
288  H3DUniquePtr< ScaleTranslation> instantTranslation;
289 
295  H3DUniquePtr< ScaleRotation> instantYaw;
296 
302  H3DUniquePtr< ScaleRotation> instantPitch;
303 
309  H3DUniquePtr< ScaleRotation> instantRoll;
310 
316  H3DUniquePtr< ScaleRotation> instantRotation;
317 
323  H3DUniquePtr< AccumulateTranslation> accumulatedTranslation;
324 
330  H3DUniquePtr< AccumulateRotation> accumulatedYaw;
331 
337  H3DUniquePtr< AccumulateRotation> accumulatedPitch;
338 
344  H3DUniquePtr< AccumulateRotation> accumulatedRoll;
345 
351  H3DUniquePtr< AccumulateRotation> accumulatedRotation;
352 
359  H3DUniquePtr< SFFloat> translationScale;
360 
368  H3DUniquePtr< SFFloat> rotationScale;
369 
380  H3DUniquePtr< SFInt32 > buttons;
381 
387  H3DUniquePtr< SFInt32 > latestButtonPress;
388 
394  H3DUniquePtr< SFInt32 > latestButtonRelease;
395 
402  H3DUniquePtr< SFBool > accumulateTimeDependent;
403 
409  H3DUniquePtr< ResetAccumulatedTranslation > resetAccumulatedTranslation;
410 
417  H3DUniquePtr< ResetAccumulatedRotation > resetAccumulatedRotation;
418 
419  // This data structure is used to transfer button data from the
420  // device communication thread to the scene graph thread.
421  struct ButtonData {
422  ButtonData( int _pressed,
423  int _released,
424  int _current_state ) :
425  pressed( _pressed ),
426  released( _released ),
427  current_state( _current_state ) {}
428  unsigned int pressed, released, current_state;
429  };
430  public:
431  // has a motion event occured since last scenegraph loop.
432  bool thread_motion_event;
433  // button data since last scenegraph loop.
434  vector< ButtonData > thread_buttons;
435  // rotation values from the last motion event.
436  Vec3f thread_rotation;
437  // translation values from the last motion event.
438  Vec3f thread_translation;
439  // is_active in thread. Will be false if thread main loop
440  // can not run due to some error.
441  bool thread_is_active;
442 
445 
446  // instance of the first SpaceWareSensor created
447  // used if the spacewaresensor should be used to navigate the scene
448  static SpaceWareSensor *sws_instance;
449 
450  private:
451 #ifdef HAVE_3DXWARE
452  // the handle of the communication thread.
453  H3DUniquePtr< H3DUtil::SimpleThread > thread_handle;
454 #endif
455 
458  void updateValues();
459 
460  struct H3DAPI_API Update
461  : AutoUpdate<Field> {
462  void update(){
463  static_cast<SpaceWareSensor*>
464  (owner)->updateValues();
465  }
466  };
467 
468  H3DUniquePtr< Update > update;
469  };
470 }
471 
472 #endif
Contains the SFFloat field class.
Contains the SFInt32 field class.
Contains the SFRotation field class.
Contains the SFVec3f field class.
Header file for X3DSensorNode, X3D scene-graph node.
The SFFloat field contains one single-precision floating point number.
Definition: SFFloat.h:41
The SFRotation field contains one arbitrary Rotation.
Definition: SFRotation.h:41
The SFVec3f field contains a Vec3f.
Definition: SFVec3f.h:41
virtual void setValue(const Rotation &v, int id=0)
Set the value of the field.
Definition: SField.h:215
virtual void update()
Make the field up to date given that an event has occured.
Definition: SField.h:181
This class is an AutoUpdate class that sets the field accumulatedRotation to its input value and also...
Definition: SpaceWareSensor.h:125
virtual void update()
Make the field up to date given that an event has occured.
Definition: SpaceWareSensor.h:143
This class is an AutoUpdate class that sets the field accumulatedTranslation to its input value.
Definition: SpaceWareSensor.h:191
virtual void update()
Make the field up to date given that an event has occured.
Definition: SpaceWareSensor.h:199
This is a X3DSensorNode for reading values from a 3DConnexion motion controller such as the SpaceTrav...
Definition: SpaceWareSensor.h:55
H3DUniquePtr< AccumulateRotation > accumulatedYaw
The sum of all instantYaw values.
Definition: SpaceWareSensor.h:330
H3DUniquePtr< AccumulateRotation > accumulatedRotation
The sum of all instantRotation values.
Definition: SpaceWareSensor.h:351
H3DUniquePtr< ScaleTranslation > instantTranslation
rawTranslation scaled by rotationScale
Definition: SpaceWareSensor.h:288
H3DUniquePtr< SFRotation > rawYaw
Contains the current rotation around the x-axis as reported by the device.
Definition: SpaceWareSensor.h:257
H3DUniquePtr< ScaleRotation > instantPitch
rawPitch scaled by rotationScale
Definition: SpaceWareSensor.h:302
H3DUniquePtr< AccumulateRotation > accumulatedRoll
The sum of all instantRoll values.
Definition: SpaceWareSensor.h:344
H3DUniquePtr< SFInt32 > latestButtonPress
The button number of the last button that was pressed.
Definition: SpaceWareSensor.h:387
H3DUniquePtr< SFInt32 > buttons
Integer representing the current status of the buttons.
Definition: SpaceWareSensor.h:380
H3DUniquePtr< SFVec3f > rawTranslation
Contains the current translation as reported by the device.
Definition: SpaceWareSensor.h:249
H3DUniquePtr< SFBool > accumulateTimeDependent
If true the Accumulated-fields depends on time passed since last time accumulated was updated instead...
Definition: SpaceWareSensor.h:402
H3DUniquePtr< ResetAccumulatedTranslation > resetAccumulatedTranslation
Resets the value of accumulatedTranslation to its own value.
Definition: SpaceWareSensor.h:409
static H3DNodeDatabase database
The H3DNodeDatabase for this node.
Definition: SpaceWareSensor.h:444
H3DUniquePtr< ResetAccumulatedRotation > resetAccumulatedRotation
Resets the value of accumulatedRotation to its own value.
Definition: SpaceWareSensor.h:417
H3DUniquePtr< ScaleRotation > instantRotation
Composite rotation of instantYaw, instantPitch and instantRoll.
Definition: SpaceWareSensor.h:316
H3DUniquePtr< SFRotation > rawRoll
Contains the current rotation around the z-axis as reported by the device.
Definition: SpaceWareSensor.h:273
H3DUniquePtr< SFFloat > translationScale
The scale factor for the instantTranslation field.
Definition: SpaceWareSensor.h:359
H3DUniquePtr< ScaleRotation > instantRoll
rawRoll scaled by rotationScale
Definition: SpaceWareSensor.h:309
H3DUniquePtr< SFFloat > rotationScale
The scale factor for the instant version of the different rotation fields.
Definition: SpaceWareSensor.h:368
H3DUniquePtr< ScaleRotation > instantYaw
rawYaw scaled by rotationScale
Definition: SpaceWareSensor.h:295
H3DUniquePtr< AccumulateRotation > accumulatedPitch
The sum of all instantPitch values.
Definition: SpaceWareSensor.h:337
H3DUniquePtr< SFRotation > rawPitch
Contains the current rotation around the y-axis as reported by the device.
Definition: SpaceWareSensor.h:265
H3DUniquePtr< AccumulateTranslation > accumulatedTranslation
The sum of all instantTranslation values.
Definition: SpaceWareSensor.h:323
H3DUniquePtr< SFInt32 > latestButtonRelease
The button number of the last button that was released.
Definition: SpaceWareSensor.h:394
H3DUniquePtr< SFRotation > rawRotation
Contains the current total rotation as reported by the device.
Definition: SpaceWareSensor.h:281
A template modifier class for adding type checking on the routes to any Field class.
Definition: TypedField.h:84
This abstract node type is the base type for all sensors.
Definition: X3DSensorNode.h:40
Rotation()
float H3DFloat
double H3DTime
Type getValue(const char *s, const char *&rest)
Function that reads characters from a char * and converts them to a given type.
Definition: X3DFieldConversion.h:134
H3D API namespace.
Definition: Anchor.h:38
The AutoUpdate field is a template to force the BaseField to update itself as soon as an event is rec...
Definition: FieldTemplates.h:130
The H3DNodeDatabase contains a mapping between a name of a Node and the constructor for the Node with...
Definition: H3DNodeDatabase.h:194
This class is an AutoUpdate class that accumulates the rotation values routed to it,...
Definition: SpaceWareSensor.h:98
virtual void update()
Make the field up to date given that an event has occured.
Definition: SpaceWareSensor.h:106
This class is an AutoUpdate class that accumulates the Vec3f values routed to it, i....
Definition: SpaceWareSensor.h:166
virtual void update()
Make the field up to date given that an event has occured.
Definition: SpaceWareSensor.h:174
Updates itself to the Rotation routed to it where the angle is scaled by the scale factor.
Definition: SpaceWareSensor.h:66
virtual void update()
Make the field up to date given that an event has occured.
Definition: SpaceWareSensor.h:67
Updates itself to the SFVec3f routed to it scaled by the scale factor.
Definition: SpaceWareSensor.h:84
virtual void update()
Make the field up to date given that an event has occured.
Definition: SpaceWareSensor.h:85