-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
BSplinePose.cpp
447 lines (344 loc) · 14 KB
/
BSplinePose.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#include <bsplines/BSplinePose.hpp>
#include <sm/assert_macros.hpp>
// boost::tie
#include <boost/tuple/tuple.hpp>
#include <sm/kinematics/transformations.hpp>
namespace bsplines {
using namespace sm::kinematics;
BSplinePose::BSplinePose(int splineOrder, const RotationalKinematics::Ptr & rotationalKinematics)
: BSpline(splineOrder), rotation_(rotationalKinematics)
{
}
BSplinePose::~BSplinePose()
{
}
Eigen::Matrix4d BSplinePose::transformation(double tk) const
{
return curveValueToTransformation(eval(tk));
}
Eigen::Matrix4d BSplinePose::transformationAndJacobian(double tk, Eigen::MatrixXd * J, Eigen::VectorXi * coefficientIndices) const
{
Eigen::MatrixXd JS;
Eigen::VectorXd p;
p = evalDAndJacobian(tk,0,&JS, coefficientIndices);
Eigen::MatrixXd JT;
Eigen::Matrix4d T = curveValueToTransformationAndJacobian( p, &JT );
if(J)
{
*J = JT * JS;
}
return T;
}
Eigen::Matrix3d BSplinePose::orientationAndJacobian(double tk, Eigen::MatrixXd * J, Eigen::VectorXi * coefficientIndices) const
{
Eigen::Matrix3d C;
Eigen::MatrixXd JS;
Eigen::VectorXd p;
p = evalDAndJacobian(tk,0,&JS, coefficientIndices);
Eigen::Matrix3d S;
C = rotation_->parametersToRotationMatrix(p.tail<3>(), &S);
Eigen::MatrixXd JO = Eigen::MatrixXd::Zero(3,6);
JO.block(0,3,3,3) = S;
if(J)
{
*J = JO * JS;
}
return C;
}
Eigen::Matrix3d BSplinePose::inverseOrientationAndJacobian(double tk, Eigen::MatrixXd * J, Eigen::VectorXi * coefficientIndices) const
{
Eigen::Matrix3d C;
Eigen::MatrixXd JS;
Eigen::VectorXd p;
p = evalDAndJacobian(tk,0,&JS, coefficientIndices);
Eigen::Matrix3d S;
C = rotation_->parametersToRotationMatrix(p.tail<3>(), &S).transpose();
Eigen::MatrixXd JO = Eigen::MatrixXd::Zero(3,6);
JO.block(0,3,3,3) = S;
if(J)
{
*J = -C * JO * JS;
}
return C;
}
Eigen::Matrix4d BSplinePose::inverseTransformationAndJacobian(double tk, Eigen::MatrixXd * J, Eigen::VectorXi * coefficientIndices) const
{
//std::cout << __FUNCTION__ << "()\n";
//ASRL_THROW(std::runtime_error,"Not Implemented");
Eigen::MatrixXd JS;
Eigen::VectorXd p;
p = evalDAndJacobian(tk,0,&JS, coefficientIndices);
Eigen::MatrixXd JT;
Eigen::Matrix4d T = curveValueToTransformationAndJacobian( p, &JT );
// Invert the transformation.
T.topLeftCorner<3,3>().transposeInPlace();
T.topRightCorner<3,1>() = (-T.topLeftCorner<3,3>() * T.topRightCorner<3,1>()).eval();
if(J)
{
// The "box times" is the linearized transformation way of inverting the jacobian.
*J = -sm::kinematics::boxTimes(T) * JT * JS;
}
if(coefficientIndices)
{
*coefficientIndices = localCoefficientVectorIndices(tk);
}
return T;
}
Eigen::Matrix4d BSplinePose::inverseTransformation(double tk) const
{
Eigen::Matrix4d T = curveValueToTransformation(eval(tk));
T.topLeftCorner<3,3>().transposeInPlace();
T.topRightCorner<3,1>() = (-T.topLeftCorner<3,3>() * T.topRightCorner<3,1>()).eval();
return T;
}
Eigen::Vector4d BSplinePose::transformVectorAndJacobian(double tk, const Eigen::Vector4d & v_tk, Eigen::MatrixXd * J, Eigen::VectorXi * coefficientIndices) const
{
Eigen::MatrixXd JT;
Eigen::Matrix4d T_n_vk = transformationAndJacobian(tk, &JT, coefficientIndices);
Eigen::Vector4d v_n = T_n_vk * v_tk;
if(J)
{
*J = sm::kinematics::boxMinus(v_n) * JT;
}
return v_n;
}
Eigen::Vector3d BSplinePose::position(double tk) const
{
return eval(tk).head<3>();
}
Eigen::Matrix3d BSplinePose::orientation(double tk) const
{
return rotation_->parametersToRotationMatrix(eval(tk).tail<3>());
}
Eigen::Matrix3d BSplinePose::inverseOrientation(double tk) const
{
return rotation_->parametersToRotationMatrix(eval(tk).tail<3>()).transpose();
}
Eigen::Vector3d BSplinePose::linearVelocity(double tk) const
{
return evalD(tk,1).head<3>();
}
Eigen::Vector3d BSplinePose::linearVelocityBodyFrame(double tk) const
{
Eigen::VectorXd r = evalD(tk, 0);
Eigen::Matrix3d C_wb = rotation_->parametersToRotationMatrix(r.tail<3>());
return C_wb.transpose() * evalD(tk, 1).head<3>();
}
Eigen::Vector3d BSplinePose::linearAcceleration(double tk) const
{
return evalD(tk,2).head<3>();
}
Eigen::Vector3d BSplinePose::linearAccelerationBodyFrame(double tk) const
{
Eigen::VectorXd r = evalD(tk, 0);
Eigen::Matrix3d C_wb = rotation_->parametersToRotationMatrix(r.tail<3>());
return C_wb.transpose() * evalD(tk, 2).head<3>();
}
Eigen::Vector3d BSplinePose::linearAccelerationAndJacobian(double tk, Eigen::MatrixXd * J, Eigen::VectorXi * coefficientIndices) const
{
Eigen::Vector3d a = evalDAndJacobian(tk,2,J,coefficientIndices).head<3>();
if(J)
{
J->conservativeResize(3,J->cols());
}
return a;
}
// \omega_w_{b,w} (angular velocity of the body frame as seen from the world frame, expressed in the world frame)
Eigen::Vector3d BSplinePose::angularVelocity(double tk) const
{
Eigen::Vector3d omega;
Eigen::VectorXd r = evalD(tk,0);
Eigen::VectorXd v = evalD(tk,1);
// \omega = S(\bar \theta) \dot \theta
omega = -rotation_->parametersToSMatrix(r.tail<3>()) * v.tail<3>();
return omega;
}
// \omega_b_{w,b} (angular velocity of the world frame as seen from the body frame, expressed in the body frame)
Eigen::Vector3d BSplinePose::angularVelocityBodyFrame(double tk) const
{
Eigen::Vector3d omega;
Eigen::VectorXd r = evalD(tk,0);
Eigen::VectorXd v = evalD(tk,1);
Eigen::Matrix3d S;
Eigen::Matrix3d C_w_b = rotation_->parametersToRotationMatrix(r.tail<3>(), &S);
// \omega = S(\bar \theta) \dot \theta
omega = -C_w_b.transpose() * S * v.tail<3>();
return omega;
}
// \omega_b_{w,b} (angular velocity of the world frame as seen from the body frame, expressed in the body frame)
Eigen::Vector3d BSplinePose::angularVelocityBodyFrameAndJacobian(double tk, Eigen::MatrixXd * J, Eigen::VectorXi * coefficientIndices) const
{
Eigen::Vector3d omega;
Eigen::Vector3d p;
Eigen::Vector3d pdot;
Eigen::MatrixXd Jp;
Eigen::MatrixXd Jpdot;
p = evalDAndJacobian(tk,0,&Jp,NULL).tail<3>();
pdot = evalDAndJacobian(tk,1,&Jpdot,coefficientIndices).tail<3>();
Eigen::MatrixXd Jr;
Eigen::Matrix3d C_w_b = inverseOrientationAndJacobian(tk,&Jr,NULL);
// Rearrange the spline jacobian matrices. Now Jpdot is the
// jacobian of p wrt the spline coefficients stacked on top
// of the jacobian of pdot wrt the spline coefficients.
Jpdot.block(0,0,3,Jpdot.cols()) = Jp.block(3,0,3,Jp.cols());
//std::cout << "Jpdot\n" << Jpdot << std::endl;
Eigen::Matrix<double,3,6> Jo;
omega = -C_w_b * rotation_->angularVelocityAndJacobian(p,pdot,&Jo);
Jo = (-C_w_b * Jo).eval();
//std::cout << "Jo:\n" << Jo << std::endl;
if(J)
{
*J = Jo * Jpdot + sm::kinematics::crossMx(omega) * Jr;
}
return omega;
}
// \omega_w_{b,w} (angular velocity of the body frame as seen from the world frame, expressed in the world frame)
Eigen::Vector3d BSplinePose::angularVelocityAndJacobian(double tk, Eigen::MatrixXd * J, Eigen::VectorXi * coefficientIndices) const
{
Eigen::Vector3d omega;
Eigen::Vector3d p;
Eigen::Vector3d pdot;
Eigen::MatrixXd Jp;
Eigen::MatrixXd Jpdot;
p = evalDAndJacobian(tk,0,&Jp,NULL).tail<3>();
pdot = evalDAndJacobian(tk,1,&Jpdot,coefficientIndices).tail<3>();
// Rearrange the spline jacobian matrices. Now Jpdot is the
// jacobian of p wrt the spline coefficients stacked on top
// of the jacobian of pdot wrt the spline coefficients.
Jpdot.block(0,0,3,Jpdot.cols()) = Jp.block(3,0,3,Jp.cols());
//std::cout << "Jpdot\n" << Jpdot << std::endl;
Eigen::Matrix<double,3,6> Jo;
omega = rotation_->angularVelocityAndJacobian(p,pdot,&Jo);
//std::cout << "Jo:\n" << Jo << std::endl;
if(J)
{
*J = Jo * Jpdot;
}
return omega;
}
// \omega_dot_b_{w,b} (angular acceleration of the world frame as seen from the body frame, expressed in the body frame)
Eigen::Vector3d BSplinePose::angularAccelerationBodyFrame(double tk) const
{
Eigen::Vector3d omega;
Eigen::VectorXd r = evalD(tk,0);
Eigen::VectorXd v = evalD(tk,2);
Eigen::Matrix3d S;
Eigen::Matrix3d C_w_b = rotation_->parametersToRotationMatrix(r.tail<3>(), &S);
// \omega = S(\bar \theta) \dot \theta
omega = -C_w_b.transpose() * S * v.tail<3>();
return omega;
}
// \omega_dot_b_{w,b} (angular acceleration of the world frame as seen from the body frame, expressed in the body frame)
Eigen::Vector3d BSplinePose::angularAccelerationBodyFrameAndJacobian(double tk, Eigen::MatrixXd * J, Eigen::VectorXi * coefficientIndices) const
{
Eigen::Vector3d omega;
Eigen::Vector3d p;
Eigen::Vector3d pdot;
Eigen::MatrixXd Jp;
Eigen::MatrixXd Jpdot;
p = evalDAndJacobian(tk,0,&Jp,NULL).tail<3>();
pdot = evalDAndJacobian(tk,2,&Jpdot,coefficientIndices).tail<3>();
Eigen::MatrixXd Jr;
Eigen::Matrix3d C_w_b = inverseOrientationAndJacobian(tk,&Jr,NULL);
// Rearrange the spline jacobian matrices. Now Jpdot is the
// jacobian of p wrt the spline coefficients stacked on top
// of the jacobian of pdot wrt the spline coefficients.
Jpdot.block(0,0,3,Jpdot.cols()) = Jp.block(3,0,3,Jp.cols());
Eigen::Matrix<double,3,6> Jo;
omega = -C_w_b * rotation_->angularVelocityAndJacobian(p,pdot,&Jo);
Jo = (-C_w_b * Jo).eval();
if(J)
{
*J = Jo * Jpdot + sm::kinematics::crossMx(omega) * Jr;
}
return omega;
}
// \omega_dot_w_{b,w} (angular acceleration of the body frame as seen from the world frame, expressed in the world frame)
Eigen::Vector3d BSplinePose::angularAccelerationAndJacobian(double tk, Eigen::MatrixXd * J, Eigen::VectorXi * coefficientIndices) const
{
Eigen::Vector3d omega;
Eigen::Vector3d p;
Eigen::Vector3d pdot;
Eigen::MatrixXd Jp;
Eigen::MatrixXd Jpdot;
p = evalDAndJacobian(tk,0,&Jp,NULL).tail<3>();
pdot = evalDAndJacobian(tk,2,&Jpdot,coefficientIndices).tail<3>();
// Rearrange the spline jacobian matrices. Now Jpdot is the
// jacobian of p wrt the spline coefficients stacked on top
// of the jacobian of pdot wrt the spline coefficients.
Jpdot.block(0,0,3,Jpdot.cols()) = Jp.block(3,0,3,Jp.cols());
Eigen::Matrix<double,3,6> Jo;
omega = rotation_->angularVelocityAndJacobian(p,pdot,&Jo);
if(J)
{
*J = Jo * Jpdot;
}
return omega;
}
void BSplinePose::initPoseSpline(double t0, double t1, const Eigen::Matrix4d & T_n_t0, const Eigen::Matrix4d & T_n_t1)
{
Eigen::VectorXd v0 = transformationToCurveValue(T_n_t0);
Eigen::VectorXd v1 = transformationToCurveValue(T_n_t1);
initSpline(t0,t1,v0,v1);
}
void BSplinePose::addPoseSegment(double tk, const Eigen::Matrix4d & T_n_tk)
{
Eigen::VectorXd vk = transformationToCurveValue(T_n_tk);
addCurveSegment(tk, vk);
}
void BSplinePose::addPoseSegment2(double tk, const Eigen::Matrix4d & T_n_tk, double lambda)
{
Eigen::VectorXd vk = transformationToCurveValue(T_n_tk);
addCurveSegment2(tk, vk, lambda);
}
Eigen::Matrix4d BSplinePose::curveValueToTransformation( const Eigen::VectorXd & c ) const
{
SM_ASSERT_EQ_DBG(Exception, c.size(), 6, "The curve value is an unexpected size!");
Eigen::Matrix4d T = Eigen::Matrix4d::Identity();
T.topLeftCorner<3,3>() = rotation_->parametersToRotationMatrix(c.tail<3>());
T.topRightCorner<3,1>() = c.head<3>();
return T;
}
Eigen::Matrix4d BSplinePose::curveValueToTransformationAndJacobian( const Eigen::VectorXd & p, Eigen::MatrixXd * J ) const
{
SM_ASSERT_EQ_DBG(Exception, p.size(), 6, "The curve value is an unexpected size!");
Eigen::Matrix4d T = Eigen::Matrix4d::Identity();
Eigen::Matrix3d S;
T.topLeftCorner<3,3>() = rotation_->parametersToRotationMatrix(p.tail<3>(), &S);
T.topRightCorner<3,1>() = p.head<3>();
if(J)
{
*J = Eigen::MatrixXd::Identity(6,6);
J->topRightCorner<3,3>() = -crossMx(p.head<3>()) * S;
J->bottomRightCorner<3,3>() = S;
}
return T;
}
Eigen::VectorXd BSplinePose::transformationToCurveValue( const Eigen::Matrix4d & T ) const
{
Eigen::VectorXd c(6);
c.head<3>() = T.topRightCorner<3,1>();
c.tail<3>() = rotation_->rotationMatrixToParameters(T.topLeftCorner<3,3>());
return c;
}
void BSplinePose::initPoseSpline2(const Eigen::VectorXd & times, const Eigen::Matrix<double,6,Eigen::Dynamic> & poses, int numSegments, double lambda)
{
initSpline2(times, poses, numSegments, lambda);
}
void BSplinePose::initPoseSpline3(const Eigen::VectorXd & times, const Eigen::Matrix<double,6,Eigen::Dynamic> & poses, int numSegments, double lambda)
{
initSpline3(times, poses, numSegments, lambda);
}
void BSplinePose::initPoseSplineSparse(const Eigen::VectorXd & times, const Eigen::Matrix<double,6,Eigen::Dynamic> & poses, int numSegments, double lambda)
{
initSplineSparse(times, poses, numSegments, lambda);
}
void BSplinePose::initPoseSplineSparseKnots(const Eigen::VectorXd ×, const Eigen::MatrixXd &interpolationPoints, const Eigen::VectorXd knots, double lambda)
{
initSplineSparseKnots(times, interpolationPoints, knots, lambda);
}
RotationalKinematics::Ptr BSplinePose::rotation() const
{
return rotation_;
}
} // namespace bsplines