HandModeliCub.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #include "HandModeliCub.h"
25 
27 
28 #include <iostream>
29 #include <fstream>
30 
31 
32 
33 namespace visionx
34 {
35  HandModeliCub::HandModeliCub(std::string sConfigFileName, CStereoCalibration* pStereoCalibration)
36  {
37  m_pStereoCalibration = pStereoCalibration;
38 
39  //****************************************
40  // read hand config file
41  //****************************************
42 
43  std::ifstream sFileStream(sConfigFileName.c_str(), std::ifstream::in);
44  if (!sFileStream.good())
45  {
46  ARMARX_WARNING_S << "CHandModelV2 constructor: file " << sConfigFileName << " could not be opened";
47  return;
48  }
49 
50  std::string sLine;
51  Vec3d vTemp;
52  float fTemp;
53  std::vector<std::vector<float> > aJointOffsets;
54  aJointOffsets.resize(5);
55 
56  // offset to thumb base
57  sLine = GetNextNonCommentLine(sFileStream);
58  sscanf(sLine.c_str(), "%f %f %f", &vTemp.x, &vTemp.y, &vTemp.z);
59  m_aOffsetsToFingers.push_back(vTemp);
60 
61  // distance from thumb base joint to second joint
62  sLine = GetNextNonCommentLine(sFileStream);
63  sscanf(sLine.c_str(), "%f", &fTemp);
64  aJointOffsets.at(0).push_back(fTemp);
65 
66  // distance from second to third thumb joint
67  sLine = GetNextNonCommentLine(sFileStream);
68  sscanf(sLine.c_str(), "%f", &fTemp);
69  aJointOffsets.at(0).push_back(fTemp);
70 
71  // distance from third to fourth thumb joint
72  sLine = GetNextNonCommentLine(sFileStream);
73  sscanf(sLine.c_str(), "%f", &fTemp);
74  aJointOffsets.at(0).push_back(fTemp);
75 
76  // distance from fourth thumb joint to fingertip
77  sLine = GetNextNonCommentLine(sFileStream);
78  sscanf(sLine.c_str(), "%f", &fTemp);
79  aJointOffsets.at(0).push_back(fTemp);
80 
81 
82 
83  // offset from palm to index base
84  sLine = GetNextNonCommentLine(sFileStream);
85  sscanf(sLine.c_str(), "%f %f %f", &vTemp.x, &vTemp.y, &vTemp.z);
86  m_aOffsetsToFingers.push_back(vTemp);
87 
88  // distance from index base joint to second joint
89  sLine = GetNextNonCommentLine(sFileStream);
90  sscanf(sLine.c_str(), "%f", &fTemp);
91  aJointOffsets.at(1).push_back(fTemp);
92 
93  // distance from second to third index joint
94  sLine = GetNextNonCommentLine(sFileStream);
95  sscanf(sLine.c_str(), "%f", &fTemp);
96  aJointOffsets.at(1).push_back(fTemp);
97 
98  // distance from third index joint to fingertip
99  sLine = GetNextNonCommentLine(sFileStream);
100  sscanf(sLine.c_str(), "%f", &fTemp);
101  aJointOffsets.at(1).push_back(fTemp);
102 
103 
104  // offset from palm to middle base
105  sLine = GetNextNonCommentLine(sFileStream);
106  sscanf(sLine.c_str(), "%f %f %f", &vTemp.x, &vTemp.y, &vTemp.z);
107  m_aOffsetsToFingers.push_back(vTemp);
108 
109  // distance from middle base joint to second joint
110  sLine = GetNextNonCommentLine(sFileStream);
111  sscanf(sLine.c_str(), "%f", &fTemp);
112  aJointOffsets.at(2).push_back(fTemp);
113 
114  // distance from second to third middle joint
115  sLine = GetNextNonCommentLine(sFileStream);
116  sscanf(sLine.c_str(), "%f", &fTemp);
117  aJointOffsets.at(2).push_back(fTemp);
118 
119  // distance from third middle joint to fingertip
120  sLine = GetNextNonCommentLine(sFileStream);
121  sscanf(sLine.c_str(), "%f", &fTemp);
122  aJointOffsets.at(2).push_back(fTemp);
123 
124 
125  // offset from palm to ring base
126  sLine = GetNextNonCommentLine(sFileStream);
127  sscanf(sLine.c_str(), "%f %f %f", &vTemp.x, &vTemp.y, &vTemp.z);
128  m_aOffsetsToFingers.push_back(vTemp);
129 
130  // distance from ring base joint to second joint
131  sLine = GetNextNonCommentLine(sFileStream);
132  sscanf(sLine.c_str(), "%f", &fTemp);
133  aJointOffsets.at(3).push_back(fTemp);
134 
135  // distance from second to third ring joint
136  sLine = GetNextNonCommentLine(sFileStream);
137  sscanf(sLine.c_str(), "%f", &fTemp);
138  aJointOffsets.at(3).push_back(fTemp);
139 
140  // distance from third ring joint to fingertip
141  sLine = GetNextNonCommentLine(sFileStream);
142  sscanf(sLine.c_str(), "%f", &fTemp);
143  aJointOffsets.at(3).push_back(fTemp);
144 
145 
146  // offset from palm to pinky base
147  sLine = GetNextNonCommentLine(sFileStream);
148  sscanf(sLine.c_str(), "%f %f %f", &vTemp.x, &vTemp.y, &vTemp.z);
149  m_aOffsetsToFingers.push_back(vTemp);
150 
151  // distance from pinky base joint to second joint
152  sLine = GetNextNonCommentLine(sFileStream);
153  sscanf(sLine.c_str(), "%f", &fTemp);
154  aJointOffsets.at(4).push_back(fTemp);
155 
156  // distance from second to third pinky joint
157  sLine = GetNextNonCommentLine(sFileStream);
158  sscanf(sLine.c_str(), "%f", &fTemp);
159  aJointOffsets.at(4).push_back(fTemp);
160 
161  // distance from third pinky joint to fingertip
162  sLine = GetNextNonCommentLine(sFileStream);
163  sscanf(sLine.c_str(), "%f", &fTemp);
164  aJointOffsets.at(4).push_back(fTemp);
165 
166 
167  // read fingertip points
168 
169  int nNumFingertipPoints;
170  sLine = GetNextNonCommentLine(sFileStream);
171  sscanf(sLine.c_str(), "%d", &nNumFingertipPoints);
172  ARMARX_VERBOSE_S << "Number of fingertip points: " << nNumFingertipPoints;
173 
174  float fFingerTipOffsetZ;
175  sLine = GetNextNonCommentLine(sFileStream);
176  sscanf(sLine.c_str(), "%f", &fFingerTipOffsetZ);
177 
178 
179  std::vector<Vec3d> aFingertipPoints;
180  sLine = GetNextNonCommentLine(sFileStream);
181  for (int i = 0; i < nNumFingertipPoints; i++)
182  {
183  sscanf(sLine.c_str(), "%f %f %f", &vTemp.x, &vTemp.y, &vTemp.z);
184  vTemp.z += fFingerTipOffsetZ;
185  aFingertipPoints.push_back(vTemp);
186  std::getline(sFileStream, sLine);
187  }
188 
189 
190  // tracking ball
191  sLine = GetNextNonCommentLine(sFileStream);
192  sscanf(sLine.c_str(), "%f %f %f", &m_vTrackingBallOffset.x, &m_vTrackingBallOffset.y, &m_vTrackingBallOffset.z);
193  sLine = GetNextNonCommentLine(sFileStream);
194  sscanf(sLine.c_str(), "%f", &m_fTrackingBallRadius);
195 
196 
197 
198  //****************************************
199  // construct fingers
200  //****************************************
201 
202  // thumb
203  std::vector<Vec3d> aFingertipPointsThumb;
204  aFingertipPointsThumb.resize(nNumFingertipPoints);
205  for (int i = 0; i < nNumFingertipPoints; i++)
206  {
207  Math3d::SetVec(aFingertipPointsThumb.at(i), aFingertipPoints.at(i).x, aFingertipPoints.at(i).y, aFingertipPoints.at(i).z);
208  }
209  CFinger* pNewFinger = new CFinger(aJointOffsets.at(0), aFingertipPointsThumb);
210  m_aFingers.push_back(pNewFinger);
211 
212  // other fingers
213  for (int i = 1; i <= 4; i++)
214  {
215  pNewFinger = new CFinger(aJointOffsets.at(i), aFingertipPoints);
216  m_aFingers.push_back(pNewFinger);
217  }
218 
219 
220 
221  //****************************************
222  // init variables
223  //****************************************
224 
225  m_aFingerJointsInWorldCS.resize(5);
226  for (int i = 0; i < 5; i++)
227  {
228  m_aFingerJointsInWorldCS.at(i).resize(m_aFingers.at(i)->m_aFingerJointsInFingerBaseCS.size());
229  for (size_t j = 0; j < m_aFingerJointsInWorldCS.at(i).size(); j++)
230  {
231  Math3d::SetVec(m_aFingerJointsInWorldCS.at(i).at(j), Math3d::zero_vec);
232  }
233  }
234 
236  for (int i = 0; i < 5; i++)
237  {
238  m_aFingerTipCornersInWorldCS.at(i).resize(m_aFingers.at(i)->m_aFingerTipCornersInFingerBaseCS.size());
239  for (size_t j = 0; j < m_aFingerTipCornersInWorldCS.at(i).size(); j++)
240  {
241  Math3d::SetVec(m_aFingerTipCornersInWorldCS.at(i).at(j), Math3d::zero_vec);
242  }
243  }
244 
245  m_aFingerTipPolygonsLeftCam.resize(5);
247 
248  m_fPalmJointAngle = 0;
249  Math3d::SetVec(m_vHandPosition, Math3d::zero_vec);
250  Math3d::SetMat(m_mHandOrientation, Math3d::unit_mat);
251  }
252 
253 
254 
255  void HandModeliCub::UpdateHand(double* pConfig)
256  {
257  Vec3d vHandPosition = {(float)pConfig[0], (float)pConfig[1], (float) pConfig[2]};
258  Mat3d mHandOrientation;
259  Math3d::SetRotationMat(mHandOrientation, pConfig[3], pConfig[4], pConfig[5]);
260 
261  float fPalmJointAngle = pConfig[6];
262 
263  std::vector<std::vector<float> > aFingerJointAngles;
264  aFingerJointAngles.resize(5);
265  aFingerJointAngles.at(0).push_back(pConfig[7]);
266  aFingerJointAngles.at(0).push_back(pConfig[8]);
267  aFingerJointAngles.at(0).push_back(pConfig[8]);
268  aFingerJointAngles.at(0).push_back(pConfig[8]);
269  aFingerJointAngles.at(1).push_back(-pConfig[9]);
270  aFingerJointAngles.at(1).push_back(-pConfig[9]);
271  aFingerJointAngles.at(1).push_back(-pConfig[9]);
272  aFingerJointAngles.at(2).push_back(-pConfig[10]);
273  aFingerJointAngles.at(2).push_back(-pConfig[10]);
274  aFingerJointAngles.at(2).push_back(-pConfig[10]);
275  aFingerJointAngles.at(3).push_back(-pConfig[11]);
276  aFingerJointAngles.at(3).push_back(-pConfig[11]);
277  aFingerJointAngles.at(3).push_back(-pConfig[11]);
278  aFingerJointAngles.at(4).push_back(-pConfig[11]);
279  aFingerJointAngles.at(4).push_back(-pConfig[11]);
280  aFingerJointAngles.at(4).push_back(-pConfig[11]);
281 
282  CHandModelV2::UpdateHand(vHandPosition, mHandOrientation, fPalmJointAngle, aFingerJointAngles);
283  }
284 }
visionx::CHandModelV2::CFinger
Definition: HandModelV2.h:43
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::HandModeliCub::UpdateHand
void UpdateHand(double *pConfig) override
Definition: HandModeliCub.cpp:255
visionx::CHandModelV2::m_fPalmJointAngle
float m_fPalmJointAngle
Definition: HandModelV2.h:87
HandModeliCub.h
visionx::CHandModelV2::m_pStereoCalibration
CStereoCalibration * m_pStereoCalibration
Definition: HandModelV2.h:83
visionx::CHandModelV2::m_aFingerTipPolygonsRightCam
std::vector< ConvexPolygonCalculations::Polygon > m_aFingerTipPolygonsRightCam
Definition: HandModelV2.h:71
visionx::CHandModelV2::m_aFingers
std::vector< CFinger * > m_aFingers
Definition: HandModelV2.h:85
visionx::CHandModelV2::m_aFingerJointsInWorldCS
std::vector< std::vector< Vec3d > > m_aFingerJointsInWorldCS
Definition: HandModelV2.h:65
visionx::CHandModelV2::m_vTrackingBallOffset
Vec3d m_vTrackingBallOffset
Definition: HandModelV2.h:89
GfxTL::Vec3d
VectorXD< 3, double > Vec3d
Definition: VectorXD.h:695
visionx::CHandModelV2::m_vHandPosition
Vec3d m_vHandPosition
Definition: HandModelV2.h:68
visionx::CHandModelV2::GetNextNonCommentLine
std::string GetNextNonCommentLine(std::ifstream &sFileStream)
Definition: HandModelV2.cpp:274
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:206
visionx::CHandModelV2::m_aFingerTipCornersInWorldCS
std::vector< std::vector< Vec3d > > m_aFingerTipCornersInWorldCS
Definition: HandModelV2.h:66
visionx::CHandModelV2::m_aOffsetsToFingers
std::vector< Vec3d > m_aOffsetsToFingers
Definition: HandModelV2.h:86
visionx::CHandModelV2::m_mHandOrientation
Mat3d m_mHandOrientation
Definition: HandModelV2.h:69
float
#define float
Definition: 16_Level.h:22
visionx::CHandModelV2::m_fTrackingBallRadius
float m_fTrackingBallRadius
Definition: HandModelV2.h:90
visionx::HandModeliCub::HandModeliCub
HandModeliCub(std::string sConfigFileName, CStereoCalibration *pStereoCalibration)
Definition: HandModeliCub.cpp:35
ARMARX_VERBOSE_S
#define ARMARX_VERBOSE_S
Definition: Logging.h:200
Logging.h
visionx::CHandModelV2::UpdateHand
virtual void UpdateHand(double *pConfig)
Definition: HandModelV2.cpp:358
visionx::CHandModelV2::m_aFingerTipPolygonsLeftCam
std::vector< ConvexPolygonCalculations::Polygon > m_aFingerTipPolygonsLeftCam
Definition: HandModelV2.h:71