H3D API  2.4.1
H3DFakeHapticsDevice.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 __H3DFAKEHAPTICSDEVICE_H__
30 #define __H3DFAKEHAPTICSDEVICE_H__
31 
32 #include "H3D/H3DHapticsDevice.h"
33 #include "H3D/SFFloat.h"
34 #include "H3D/PeriodicUpdate.h"
35 #include <H3DUtil/Threads.h>
36 
37 #include <HAPI/HAPIHapticsDevice.h>
38 
39 namespace H3D {
47  class H3DAPI_API H3DFakeHapticsDevice: public H3DHapticsDevice {
48  public:
49  class H3DAPI_API FakeHapticsDevice: public HAPI::HAPIHapticsDevice {
50  public:
52  FakeHapticsDevice() : owner( NULL ) {}
53 
56  virtual void updateDeviceValues( DeviceValues &dv, HAPI::HAPITime dt );
57 
59  virtual void sendOutput( DeviceOutput &/*dv*/,
60  HAPI::HAPITime /*dt*/ ) {}
61 
63  virtual bool initHapticsDevice( int /*_thread_frequency*/ = 1000 ) {
64  return true;
65  }
66 
68  virtual bool releaseHapticsDevice() { return true; }
69  protected:
70  H3DFakeHapticsDevice *owner;
71 
72  friend class H3DFakeHapticsDevice;
73  };
82  template< class BaseField >
83  class GetValueSafeField: public PeriodicUpdate< BaseField > {
84  public:
88  value_for_haptic = PeriodicUpdate< BaseField >::value;
89  }
90 
92  inline virtual const typename BaseField::value_type &getValue( int _id = 0 ) {
94  // if in haptic thread only get the rt_value and return
95  value_lock.readLock();
96  value_for_haptic = rt_value;
97  value_lock.unlock();
98  return value_for_haptic;
99  } else {
102  }
103  }
105  inline virtual void update() {
106  assert( H3DUtil::ThreadBase::inMainThread() ); // terminate if update in non-main thread
108  value_lock.writeLock();
109  rt_value = this->value;
110  value_lock.unlock();
111  }
113  virtual void upToDate() {
116  }
118  inline virtual void setValue( const typename BaseField::value_type &v,
119  int _id = 0 ) {
122  value_lock.writeLock();
123  rt_value = this->value;
124  value_lock.unlock();
125  }
126  protected:
127  // value used to exchange value between haptic and main thread
128  typename BaseField::value_type rt_value;
130  typename BaseField::value_type value_for_haptic;
131  H3DUtil::ReadWriteLock value_lock;
132  };
133 
135  H3DFakeHapticsDevice( Inst< SFVec3f > _devicePosition = 0,
136  Inst< SFRotation > _deviceOrientation = 0,
137  Inst< TrackerPosition > _trackerPosition = 0,
138  Inst< TrackerOrientation > _trackerOrientation = 0,
139  Inst< SFMatrix4f > _positionCalibration = 0,
140  Inst< SFRotation > _orientationCalibration = 0,
141  Inst< SFVec3f > _proxyPosition = 0,
142  Inst< WeightedProxy > _weightedProxyPosition = 0,
143  Inst< SFFloat > _proxyWeighting = 0,
144  Inst< SFBool > _mainButton = 0,
145  Inst< SFBool > _secondaryButton = 0,
146  Inst< SFInt32 > _buttons = 0,
147  Inst< SFVec3f > _force = 0,
148  Inst< SFVec3f > _torque = 0,
149  Inst< SFInt32 > _inputDOF = 0,
150  Inst< SFInt32 > _outputDOF = 0,
151  Inst< SFInt32 > _hapticsRate = 0,
152  Inst< SFInt32 > _desiredHapticsRate = 0,
153  Inst< SFNode > _stylus = 0,
154  Inst< SFHapticsRendererNode > _hapticsRenderer = 0,
155  Inst< MFVec3f > _proxyPositions = 0,
156  Inst< SFBool > _followViewpoint = 0,
157  Inst< GetValueSafeField< SFVec3f > > _set_devicePosition = 0,
158  Inst< GetValueSafeField< SFRotation > > _set_deviceOrientation = 0,
159  Inst< GetValueSafeField< SFBool > > _set_mainButton = 0,
160  Inst< GetValueSafeField< SFBool > > _set_secondaryButton = 0,
161  Inst< GetValueSafeField< SFBool > > _set_pauseDeviceTransform = 0,
162  Inst< SFFloat > _gripperAngle = 0,
163  Inst< GetValueSafeField< SFFloat > > _set_gripperAngle = 0,
164  Inst< SFFloat > _gripperForce = 0 );
165 
168  releaseDevice();
169  hapi_device.reset( NULL );
170  }
171 
174 
179  H3DUniquePtr< GetValueSafeField<SFVec3f> > set_devicePosition;
180 
185  H3DUniquePtr< GetValueSafeField<SFRotation> > set_deviceOrientation;
186 
191  H3DUniquePtr< GetValueSafeField<SFBool> > set_mainButton;
192 
197  H3DUniquePtr< GetValueSafeField<SFBool> > set_secondaryButton;
198 
203  H3DUniquePtr< GetValueSafeField<SFBool> > set_pauseDeviceTransform;
204 
205  bool pauseDeviceTransform;
206 
207  HAPI::HAPIHapticsDevice::DeviceValues pauseDeviceTransformDV;
208  public:
209  virtual void initialize() {
210  // This makes it easier to subclass FakeHapticsDevice, or H3DFakeHapticsDevice and use a subclass
211  // H3DFakeHapticsDevice::FakeHapticsDevice internally.
212  dynamic_cast< H3DFakeHapticsDevice::FakeHapticsDevice * >(hapi_device.get())->owner = this;
213  }
214 
218  H3DUniquePtr< SFFloat > gripperAngle;
219 
223  H3DUniquePtr< GetValueSafeField<SFFloat> > set_gripperAngle;
224 
228  H3DUniquePtr< SFFloat > gripperForce;
229 
233  virtual void updateDeviceValues();
234  };
235 }
236 
237 #endif
Header file for H3DHapticsDevice.
Header file for PeriodicUpdate, template field modifier.
Contains the SFFloat field class.
static bool inHapticThread()
static bool inMainThread()
A field which ensure that getValue and only getValue can be called within haptic thread with thread s...
Definition: H3DFakeHapticsDevice.h:83
virtual void upToDate()
Make sure that the field is up-to-date, but never in non-main thread.
Definition: H3DFakeHapticsDevice.h:113
GetValueSafeField()
Constructor.
Definition: H3DFakeHapticsDevice.h:86
virtual void update()
update can not happens in non-main thread
Definition: H3DFakeHapticsDevice.h:105
BaseField::value_type value_for_haptic
value to be used for haptic thread
Definition: H3DFakeHapticsDevice.h:130
virtual void setValue(const typename BaseField::value_type &v, int _id=0)
Set the value of the field, also only allowed in main thread.
Definition: H3DFakeHapticsDevice.h:118
virtual const BaseField::value_type & getValue(int _id=0)
Get the value of the field.
Definition: H3DFakeHapticsDevice.h:92
<>
Definition: H3DFakeHapticsDevice.h:47
static H3DNodeDatabase database
Node database entry.
Definition: H3DFakeHapticsDevice.h:173
H3DUniquePtr< GetValueSafeField< SFBool > > set_secondaryButton
The set_secondaryButton field can be used to set the secondaryButton field of the haptics device.
Definition: H3DFakeHapticsDevice.h:197
H3DUniquePtr< GetValueSafeField< SFRotation > > set_deviceOrientation
The set_deviceOrientation field can be used to set the deviceOrientation field of the haptics device.
Definition: H3DFakeHapticsDevice.h:185
H3DUniquePtr< GetValueSafeField< SFFloat > > set_gripperAngle
The set_gripperAngle field can be used to set the gripperAngle field.
Definition: H3DFakeHapticsDevice.h:223
H3DUniquePtr< GetValueSafeField< SFVec3f > > set_devicePosition
The set_devicePosition field can be used to set the devicePosition field of the haptics device.
Definition: H3DFakeHapticsDevice.h:179
H3DUniquePtr< SFFloat > gripperForce
Output field for the gripper force.
Definition: H3DFakeHapticsDevice.h:228
H3DUniquePtr< GetValueSafeField< SFBool > > set_pauseDeviceTransform
The set_pauseDeviceTransform field can be used to pause the transform of the haptics device.
Definition: H3DFakeHapticsDevice.h:203
H3DUniquePtr< SFFloat > gripperAngle
Output field for the gripper angle.
Definition: H3DFakeHapticsDevice.h:218
H3DUniquePtr< GetValueSafeField< SFBool > > set_mainButton
The set_mainButton field can be used to set the mainButton field of the haptics device.
Definition: H3DFakeHapticsDevice.h:191
~H3DFakeHapticsDevice()
Destructor.
Definition: H3DFakeHapticsDevice.h:167
Base class for all haptic devices.
Definition: H3DHapticsDevice.h:68
The PeriodicUpdate is a template modifier that changes the way the field is updated.
Definition: PeriodicUpdate.h:74
virtual void upToDate()
upToDate is specialized to record the time of the call to the function in the last_up_to_date member.
Definition: PeriodicUpdate.h:125
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 H3DNodeDatabase contains a mapping between a name of a Node and the constructor for the Node with...
Definition: H3DNodeDatabase.h:194