vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
VirtualCouplingEffectorFilter.h
Go to the documentation of this file.
1 #ifndef VIRTUAL_COUPLING_EFFECTOR_FILTER_H
2 #define VIRTUAL_COUPLING_EFFECTOR_FILTER_H
3 
5 
6 #ifdef COMPILE_PHANTOM_DEVICE
7 
8 namespace vreckoDP_PHANToM {
9 
10 
11 //#define PHANTOM_VCEF_MAX_DERIVATIVE 2
12  // 0: position
13  // 1st: velocity
14  // 2nd: acceleration
15 
16 
17 /* PHANToM filter to perform the virtual coupling -> drag the phantom handle to the object */
18 class VirtualCouplingEffectorFilter : public PHANToMFilter {
19 public:
20  VirtualCouplingEffectorFilter();
21 
22  virtual bool loadXMLParameters(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *parameters);
23  virtual void apply(PHANToMSTATE &pState, osg::Vec3 &force, osg::Vec3 &torque);
24 protected:
25 
26  template<class _Type, int _MaxData> class OldDataStorage {
27  public:
28  _Type data[_MaxData];
29  // 0 = current
30  // 1 = 1-frame old data
31  // 2 = 2-frames old data
32  // etc.
33 
34  inline void getMaxData() { return _MaxData; }
35 
36  inline _Type &getValue() { return data[0]; }
37  inline const _Type &getValue() const { return data[0]; }
38 
39  inline _Type getVelocity() { return (data[0] - data[1]); }
40  inline _Type getAccel() { return (data[0] - 2 * data[1] + data[2]); }
41 
42  inline void fillWithSingleValue(_Type& newData) {
43  for (int i = 0; i < _MaxData; ++i)
44  data[i] = newData;
45  }
46 
47  inline void addNew(_Type& newData) {
48  for (int i = _MaxData - 1; i > 1; --i)
49  data[i] = data[i - 1];
50  data[0] = newData;
51  }
52 
53 
54  };
55 
56  template<class _Type, int _MaxData> class StorageWithDamping : public OldDataStorage<_Type, _MaxData> {
57  public:
58  StorageWithDamping(float _springDamping, float _velocityDamping) : OldDataStorage() {
59  springDamping = _springDamping;
60  velocityDamping = _velocityDamping;
61  }
62 
63  void dampAndStoreValue(_Type *value) {
64  *value = (*value * springDamping + getVelocity() * velocityDamping);
65  addNew(*value);
66  }
67 
68  void setDamping(float newSpringDamping, float newVelocityDamping) {
69  springDamping = newSpringDamping;
70  velocityDamping = newVelocityDamping;
71  }
72 
73  float getSpringDamping() { return springDamping; }
74  float getVelocityDamping() { return velocityDamping; }
75 
76  protected:
77  float springDamping;
78  float velocityDamping;
79  };
80 
81  unsigned int realPhantomPosObjectID; // secondary (optional) object, that shows the real phantom position
82 
83  DynamicArray<osg::Matrix> originalScales; // original scaling of the held objects (TODO - probably not necessary, remove)
84 // osg::Matrix cachedPreTransform; // scaling and center shifting at once - TODO
85 
86  osg::Vec3 offsetFromCenter; // Offset of the phantom reference point
87 
88  OldDataStorage<osg::Vec3, 4> lastRealPos;
89  OldDataStorage<osg::Vec3, 4> lastVirtualPos;
90  // RealPos and VirtualPos are shifted from the "Phantom".pPosition by [offsetFromCenter] * "rotation"
91 
92  OldDataStorage<osg::Quat, 4> lastRealRot;
93  OldDataStorage<osg::Quat, 4> lastVirtualRot;
94 
95  void inverseQuadraticScale(osg::Vec3 *valueToRescale, float inputMaxLength, float outputMaxLength);
96  // Recalculates linear interpolation to inverse quadratic
97  // If the input vector is longer than [inputMaxLength], it will be shortened.
98  // If it is smaller, it will be rescaled.
99 
100  void linearScale(osg::Vec3 *valueToRescale, float inputMaxLength, float outputMaxLength);
101  // If the input vector is longer than [inputMaxLength], it will be shortened.
102  // If it is smaller, it will be rescaled.
103 
104  void forceScale(osg::Vec3 *valueToRescale, float inputMaxLength, float outputMaxLength);
105 
106  float virtToolWeight;
107 
108  // dynamic parameters
109  osg::Vec3 virtToolLinMomentum; // (kg?) . (distance unit) / SECOND (not millisecond)
110  osg::Vec3 virtToolAngMomentum;
111 
112  // fixed parameters, can be changed in the config file
113  float fSpringLinDamping;
114  float fSpringLinFriction;
115  float fSpringAngDamping;
116  float fSpringAngFriction;
117 
118  float fDragLinForceTool; // Translational force that drags to tool to the user pos
119  float fDragLinDistTool; // ... and distance
120  float fDragLinForceUser; // Translational force that drags user to the tool pos
121  float fDragLinDistUser; // ... and distance
122 
123  float fDragAngForceTool; // Rotational force force that drags to tool to the user pos
124  float fDragAngForceUser; // Translational force that drags user to the tool pos
125 
126  float fMaxLinDistTool; // maximum distance, that the virtual tool is allowed to move in one haptic frame
127  // (1/1000 s OR LONGER if the processing takes too long)
128  float fMaxAngDistTool; // maximum distance, that the virtual tool is allowed to rotate in one haptic frame
129 
130  float fDistanceToReset; // Distance between two (haptic) frames that will cause the forces to be reseted
131 
132  float fNoMoveTime; // [in s] Time after Reset in which the moved object remain motionless and the user is dragged to its position
133 
134  bool bPauseIfColStructsAreNotReady;
135  // If the virtual coupling detects that the held object(s) contain structures that are still being created (in different thread)
136  // the simulation will fix the held object(s) and disable any movement.
137  // NOTE: It only supports certain collision structures, support for other structures have to be added into this Ability.
138 
139  // Used for sticky materials
140  int computeCollisionThroughtLastXFrame;
141  int collisionsFrontPosition;
142  bool collisionsFront[5000];
143  bool collisionsOccuredLastXFrame(int X);
144  void pushCollisionToFront(bool value);
145 
146  bool bStickyObjectsEnabled;
147  float fStickyObjectsDistance;
148 
149  // Used for damping
150  struct dampingValues {
151  osg::Vec3f force;
152  float fRotAngle;
153  };
154 
155  int iDampingValuesX;
156  int iDampingEndValuesX;
157  dampingValues dampingField[5000];
158  int iDampingValuesFieldPosition;
159  void getAvarageLastXDampingValues(osg::Vec3f *force, float *angle);
160  float getAvarageLastXDampingValuesRotAngle(int count);
161  void pushValuesToDamping(osg::Vec3f force, float fRotAngle);
162 
163 };
164 
165 }
166 
167 #endif
168 
169 #endif