-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFK.cpp
executable file
·285 lines (254 loc) · 10 KB
/
FK.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "FK.h"
#include "transform4d.h"
#include "graphSearchAlgorithms.h"
#include "basicAlgorithms.h"
#include "containerHelper.h"
#include "listIO.h"
#include <cassert>
#include <cmath>
#include <iostream>
#include <fstream>
#include <functional>
#if defined(_WIN32) || defined(WIN32)
#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES
#endif
#endif
#include <math.h>
using namespace std;
namespace
{
inline double deg2rad(double deg) { return deg * M_PI / 180.0; }
// Convert Euler angles (in degrees) in the given RotateOrder, to the row-major 3x3 rotation.
void euler2Rotation(const double angle[3], double R[9], RotateOrder order)
{
Mat3d RX = getElementRotationMatrix(0, deg2rad(angle[0]));
Mat3d RY = getElementRotationMatrix(1, deg2rad(angle[1]));
Mat3d RZ = getElementRotationMatrix(2, deg2rad(angle[2]));
Mat3d & RMat = asMat3d(R);
switch(order)
{
case XYZ:
RMat = RZ * RY * RX;
return;
case YZX:
RMat = RX * RZ * RY;
return;
case ZXY:
RMat = RY * RX * RZ;
return;
case XZY:
RMat = RY * RZ * RX;
return;
case YXZ:
RMat = RZ * RX * RY;
return;
case ZYX:
RMat = RX * RY * RZ;
return;
}
assert(0);
}
} // anonymous namespace
FK::FK(const std::string & jointParentsFilename, const std::string & skeletonConfigFilename)
{
const int listOffset = 0;
const bool sortListAfterLoad = false;
int exitCode = ListIO::load(jointParentsFilename.c_str(), jointParents, listOffset, sortListAfterLoad);
assert(exitCode == 0);
numJoints = jointParents.size();
cout << "Load # joints " << numJoints << " : " << streamRange(jointParents) << endl;
ifstream fin(skeletonConfigFilename.c_str());
assert(fin);
// jointRestTransformConfig file format:
//
// <joint 0 translation vector> <joint 1 translation vector> ...
// <joint 0 rotation vector> <joint 1 rotation vector> ...
// <joint 0 jointOrientation vector> <joint 1 jointOrientation vector> ...
// (optional) <joint 0 rotateOrder ("xyz" or other orders) > <joint 0 rotateOrder> ...
// first, load translation, rotation and jointOrientation data
jointRestTranslations.resize(numJoints);
jointRestEulerAngles.resize(numJoints);
jointOrientations.resize(numJoints);
jointInvRestGlobalTransforms.resize(numJoints);
for(int i = 0; i < numJoints; i++)
fin >> jointRestTranslations[i][0] >> jointRestTranslations[i][1] >> jointRestTranslations[i][2];
for(int i = 0; i < numJoints; i++)
fin >> jointRestEulerAngles[i][0] >> jointRestEulerAngles[i][1] >> jointRestEulerAngles[i][2];
for(int i = 0; i < numJoints; i++)
fin >> jointOrientations[i][0] >> jointOrientations[i][1] >> jointOrientations[i][2];
assert(fin.fail() == false);
fin >> ws;
jointEulerAngles = jointRestEulerAngles;
jointRotateOrders.resize(numJoints, getDefaultRotateOrder());
if (fin.eof() == false) // load rotateOrder
{
// {0: "XYZ", 1: "YZX", 2: "ZXY", 3: "XZY", 4: "YXZ", 5: "ZYX"}
const map<string, RotateOrder> orderStrMap =
{
{"xyz", RotateOrder::XYZ},
{"yzx", RotateOrder::YZX},
{"zxy", RotateOrder::ZXY},
{"xzy", RotateOrder::XZY},
{"yxz", RotateOrder::YXZ},
{"zyx", RotateOrder::ZYX}
};
string orderStr(3, '\0');
for(int i = 0; i < numJoints; i++)
{
memset(&orderStr[0], 0, sizeof(char) * 3);
for(int d = 0; d < 3; d++)
fin.get(orderStr[d]);
for(int d = 0; d < 3; d++)
orderStr[d] = tolower(orderStr[d]);
// cout << "joint " << i << " " << orderStr << endl;
auto it = orderStrMap.find(orderStr);
assert(it != orderStrMap.end());
jointRotateOrders[i] = it->second;
fin >> ws;
}
assert(fin.fail() == false);
}
fin.close();
vector<bool> jointVisited(numJoints, false);
// Use recursion to create an order to update joint transforms,
// so that one joint's parent always gets updated before the joint.
std::function<void(int)> goToParent = [&](int jointID) -> void
{
if (jointVisited[jointID])
return;
if (jointParents[jointID] >= 0) // this joint is not the root
{
goToParent(jointParents[jointID]);
}
jointUpdateOrder.push_back(jointID);
jointVisited[jointID] = true;
};
jointUpdateOrder.clear();
for(int i = 0; i < numJoints; i++)
goToParent(i);
assert(jointUpdateOrder.size() == (size_t)numJoints);
for(int i = 0; i < numJoints; i++)
assert(jointParents[i] < 0 || jointUpdateOrder[jointParents[i]] < jointUpdateOrder[i]);
// Call computeLocalAndGlobalTransforms to compute jointRestGlobalTransforms given restTranslations/EulerAngles/JointOrientations.
// jointInvRestGlobalTransforms here is just a place-holder.
vector<RigidTransform4d> jointRestGlobalTransforms(numJoints);
computeLocalAndGlobalTransforms(jointRestTranslations, jointRestEulerAngles, jointOrientations, jointRotateOrders,
jointParents, jointUpdateOrder,
jointInvRestGlobalTransforms/*not used*/, jointRestGlobalTransforms);
for (int i = 0; i < numJoints; i++)
{
jointInvRestGlobalTransforms[i] = inv(jointRestGlobalTransforms[i]);
}
buildJointChildren();
jointLocalTransforms.resize(numJoints);
jointGlobalTransforms.resize(numJoints);
jointSkinTransforms.resize(numJoints);
computeJointTransforms();
}
void FK::buildJointChildren()
{
jointChildren.assign(numJoints, {});
for(int jointID = 0; jointID < numJoints; jointID++)
{
int parentID = jointParents[jointID];
if (parentID < 0)
continue;
jointChildren[parentID].push_back(jointID);
}
}
vector<int> FK::getJointDescendents(int jointID) const
{
vector<int> ret = jointChildren[jointID];
doBFSOnDirectedTree(convertArrayToFunction(jointChildren), ret);
sort(ret.begin(), ret.end());
return ret;
}
void printRotMat(double mat[9]) {
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
cout << mat[i*3+j] << " ";
cout << endl;
}
cout << endl;
}
// This is the main function that performs forward kinematics.
// Each joint has its local transformation relative to the parent joint.
// globalTransform of a joint is the transformation that converts a point expressed in the joint's local frame of reference, to the world coordinate frame.
// localTransform is the transformation that converts a point expressed in the joint's local frame of reference, to the coordinate frame of its parent joint.
// Specifically, if xLocal is the homogeneous coordinate of a point expressed in the joint's local frame, and xParent is the homogeneous coordinate of the
// same point expressed in the frame of the joint's parent, we have:
// xParent = localTransform * xLocal , and
// globalTransform = parentGlobalTransform * localTransform .
// Note that the globalTransform of the root joint equals its localTransform.
//
// Input: translations of each joint relative to its parent (in parent's coordinate system),
// current Euler angles, joint orientations, joint rotation order, joint parents, joint update order.
// All arrays are assumed to have the same length (= #joints).
// Joint orientations are always given in XYZ order (Maya convention).
// Output: localTransforms and globalTransforms.
void FK::computeLocalAndGlobalTransforms(
const vector<Vec3d> & translations, const vector<Vec3d> & eulerAngles,
const vector<Vec3d> & jointOrientationEulerAngles, const vector<RotateOrder> & rotateOrders,
const std::vector<int> jointParents, const vector<int> & jointUpdateOrder,
vector<RigidTransform4d> & localTransforms, vector<RigidTransform4d> & globalTransforms)
{
// Students should implement this.
// First, compute the localTransform for each joint, using eulerAngles and jointOrientationEulerAngles,
// and the "euler2Rotation" function.
Mat3d currLocalRotMat;
double currRotArr[9], currJointRotArr[9];
// printRotMat(currJointRotMat);
for(int i=0; i<localTransforms.size(); i++)
{
// convert rotation "offset" - joint orientation Euler angles to matrix
euler2Rotation(jointOrientationEulerAngles[i], currJointRotArr, XYZ);
// convert rotation Euler angles to matrix
euler2Rotation(eulerAngles[i], currRotArr, rotateOrders[i]);
// calculate complete 3x3 rotation matrix inside currrent local transformation
// convert to mat3d for calculation
currLocalRotMat = Mat3d(currJointRotArr) * Mat3d(currRotArr);
// set current localTransform (4x4) with rotation and translation
localTransforms[i] = RigidTransform4d(currLocalRotMat, translations[i]);
}
// Then, recursively compute the globalTransforms, from the root to the leaves of the hierarchy.
// Use the jointParents and jointUpdateOrder arrays to do so.
// Also useful are the Mat3d and RigidTransform4d classes defined in the Vega folder.
for(int i=0; i<localTransforms.size(); i++)
{
// get current joint
int currJoint = jointUpdateOrder[i];
// recursively compute global transformation for each joint
jointParents[currJoint] == -1
// M(g)root = M(l)root b/c root has no parent
? globalTransforms[currJoint] = localTransforms[currJoint]
// M(g)currJoint = M(g)currJoint'sParent * M(l)currJoint
: globalTransforms[currJoint] = globalTransforms[jointParents[currJoint]] * localTransforms[currJoint];
}
}
// Compute skinning transformations for all the joints, using the formula:
// skinTransform = globalTransform * invRestTransform
void FK::computeSkinningTransforms(
const vector<RigidTransform4d> & globalTransforms,
const vector<RigidTransform4d> & invRestGlobalTransforms,
vector<RigidTransform4d> & skinTransforms)
{
// Students should implement this.
for(int i=0; i<skinTransforms.size(); i++)
{
skinTransforms[i] = globalTransforms[i] * invRestGlobalTransforms[i];
}
}
void FK::computeJointTransforms()
{
computeLocalAndGlobalTransforms(jointRestTranslations, jointEulerAngles, jointOrientations, jointRotateOrders,
jointParents, jointUpdateOrder,
jointLocalTransforms, jointGlobalTransforms);
computeSkinningTransforms(jointGlobalTransforms, jointInvRestGlobalTransforms, jointSkinTransforms);
}
void FK::resetToRestPose()
{
jointEulerAngles = jointRestEulerAngles;
computeJointTransforms();
}