cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mtsVF_RCM.h
Go to the documentation of this file.
1 #ifndef _mtsVF_RCM_h
2 #define _mtsVF_RCM_h
3 
11 #include <sawConstraintController/mtsVFDataRCM.h>
13 
15 {
17 
18 public:
19 
21  mtsVF_RCM(const std::string & name, mtsVFDataRCM * data) : mtsVFJointPosition(name,data){
22  H.SetSize(8,6);
23  h.SetSize(8);
24  H.Zeros();
25  h.Zeros();
26  }
27  void FillInTableauRefs(const mtsVFBase::CONTROLLERMODE mode, const double TickTime);
28 
29 private:
30 
31  vct3 ClosestPoint;
32  vctDoubleMat H;
33  vctDoubleVec h;
34  vct3 ur;
35 
36  void FillMoveConstraints3D(vctDoubleMat &H, vctDoubleVec &h,
37  vct3 &ur,
38  vct3 direction,
39  vct3 error,
40  double tolerance,
41  int tesselationNumber = 8)
42  {
43  H.SetSize(tesselationNumber,6);
44  h.SetSize(tesselationNumber);
45  H.Zeros();
46  h.Zeros();
47  vct3 unitV_wd;
48  vct3 unitV_pl;
50  ZAxisToRotation(direction, R);
51  for (int i = 1; i <= tesselationNumber; i++) {
52  unitV_pl.Assign(cos(i*2*3.14159/tesselationNumber), sin(i*2*3.14159/tesselationNumber), 0);
53  unitV_wd.ProductOf(R, unitV_pl);
54  H(i-1, 0) = -unitV_wd.X();
55  H(i-1, 1) = -unitV_wd.Y();
56  H(i-1, 2) = -unitV_wd.Z();
57  h(i-1) = -tolerance - vctDotProduct(unitV_wd, -error);
58  }
59  ur.ProductOf(R, error);
60  }
61 
62  void ZAxisToRotation(vct3 Zaxis, vctFixedSizeMatrix<double, 3, 3> &R)
63  {
64  vct3 Dx, Dy;
65  vct3 YVec3(0., 1., 0.);
66  vct3 ZVec3(0., 0., 1.);
67  Zaxis.Divide(Zaxis.Norm());
68  if ( fabs(vctDotProduct(Zaxis, ZVec3)) > 0.99 ) {
69  Dx = (YVec3 % Zaxis);
70  } else {
71  Dx = (ZVec3 % Zaxis);
72  }
73  Dx.Divide(Dx.Norm());
74  Dy = (Zaxis % Dx);
75  Dy.Divide(Dy.Norm());
76  R(0, 0) = Dx(0);
77  R(1, 0) = Dx(1);
78  R(2, 0) = Dx(2);
79  R(0, 1) = Dy(0);
80  R(1, 1) = Dy(1);
81  R(2, 1) = Dy(2);
82  R(0, 2) = Zaxis(0);
83  R(1, 2) = Zaxis(1);
84  R(2, 2) = Zaxis(2);
85  bool sanityCheckOkay = true;
86  for (int cc = 0; cc < 3; cc++) {
87  for (int rr = 0; rr < 3; rr++) {
88  if (!CMN_ISFINITE(R(rr, cc))) sanityCheckOkay &= false;
89  }
90  }
91  if (sanityCheckOkay == false) {
92  CMN_LOG_CLASS_RUN_ERROR << "#### Dx ####" << std::endl << Dx << "########" << std::endl;
93  CMN_LOG_CLASS_RUN_ERROR << "#### Dy ####" << std::endl << Dy << "########" << std::endl;
94  CMN_LOG_CLASS_RUN_ERROR << "#### Zaxis ####" << std::endl << Zaxis << "########" << std::endl;
95  }
96  }
97 
98  vctDoubleVec ClosestLinePoint(vctDoubleVec givenPt, vctDoubleVec linePoint, vctDoubleVec lineD)
99  {
100  vctDoubleVec xcl;
101  xcl.SetSize(3);
102  double normD = lineD(0) * lineD(0) + lineD(1) * lineD(1) + lineD(2) * lineD(2);
103  if (normD > 1e-6) {
104  double sqrtNorm = sqrt(normD);
105  lineD(0) /= sqrtNorm;
106  lineD(1) /= sqrtNorm;
107  lineD(2) /= sqrtNorm;
108  } else {
109  CMN_LOG_CLASS_RUN_ERROR << "FAILURE: " << lineD << std::endl;
110  }
111  double bx = givenPt(0) - linePoint(0);
112  double by = givenPt(1) - linePoint(1);
113  double bz = givenPt(2) - linePoint(2);
114  double d = bx*lineD(0) + by*lineD(1)+bz*lineD(2);
115  if (!CMN_ISFINITE(d)) {
116  CMN_LOG_CLASS_RUN_ERROR << "FAILURE: " << lineD << " " << d << std::endl;
117  }
118  xcl(0) = linePoint(0) + lineD(0)*d;
119  xcl(1) = linePoint(1) + lineD(1)*d;
120  xcl(2) = linePoint(2) + lineD(2)*d;
121  return xcl;
122  }
123 
124  double dist(vctDoubleVec tip, vctDoubleVec closestPoint)
125  {
126  double x = closestPoint[0] - tip[0];
127  double y = closestPoint[1] - tip[1];
128  double z = closestPoint[2] - tip[2];
129  if(tip*closestPoint > 0)
130  return sqrt(x*x + y*y + z*z);
131  else
132  return -sqrt(x*x + y*y + z*z);
133  }
134 
135 };
136 
138 
139 #endif
value_type & Y(void)
Definition: vctFixedSizeVectorBase.h:572
Typedef for dynamic vectors.
NormType Norm(void) const
Definition: vctFixedSizeConstVectorBase.h:453
bool Zeros(void)
Definition: vctDynamicMatrixBase.h:468
void FillInTableauRefs(const mtsVFBase::CONTROLLERMODE mode, const double TickTime)
Updates co with virtual fixture data.
Typedef for fixed size vectors.
mtsVF_RCM()
Definition: mtsVF_RCM.h:20
This is the base class for all virtual fixture objects.
Definition: mtsVFJointPos.h:30
mtsVF_RCM(const std::string &name, mtsVFDataRCM *data)
Definition: mtsVF_RCM.h:21
ThisType & Divide(const value_type scalar)
Definition: vctFixedSizeVectorBase.h:1159
ThisType & Assign(const vctFixedSizeConstVectorBase< _size, __stride, __elementType, __dataPtrType > &other)
Definition: vctFixedSizeVectorBase.h:274
void SetSize(size_type size)
Definition: vctDynamicVector.h:315
value_type & X(void)
Definition: vctFixedSizeVectorBase.h:559
Typedef for dynamic matrices.
#define CMN_LOG_LOD_RUN_VERBOSE
Definition: cmnLogLoD.h:95
Declaration of nmrLSqLin.
Defines cmnGenericObject.
_elementType vctDotProduct(const vctDynamicConstVectorBase< _vector1OwnerType, _elementType > &vector1, const vctDynamicConstVectorBase< _vector2OwnerType, _elementType > &vector2)
Definition: vctDynamicConstVectorBase.h:1067
bool Zeros(void)
Definition: vctDynamicVectorBase.h:225
void SetSize(size_type rows, size_type cols, bool storageOrder)
Definition: vctDynamicMatrix.h:364
#define CMN_DECLARE_SERVICES(hasDynamicCreation, lod)
Definition: cmnClassRegisterMacros.h:116
#define CMN_LOG_CLASS_RUN_ERROR
Definition: cmnLogger.h:117
CONTROLLERMODE
Definition: mtsVFBase.h:39
value_type & Z(void)
Definition: vctFixedSizeVectorBase.h:585
CMN_DECLARE_SERVICES_INSTANTIATION(mtsVF_RCM)
Definition: mtsVF_RCM.h:14
const int CMN_DYNAMIC_CREATION
Definition: cmnClassRegisterMacros.h:331
#define CMN_ISFINITE(x)
Definition: cmnPortability.h:415
ThisType & ProductOf(const vctFixedSizeConstVectorBase< _size, __stride, value_type, __dataPtrType > &vector, const value_type scalar)
Definition: vctFixedSizeVectorBase.h:1003