16 #include "drake/drakeGeometryUtil_export.h" 32 DRAKEGEOMETRYUTIL_EXPORT
double angleDiff(
double phi1,
double phi2);
37 std::default_random_engine& generator);
39 std::default_random_engine& generator);
41 std::default_random_engine& generator);
44 template <typename Derived>
46 const
Eigen::MatrixBase<Derived>&
x, typename Derived::PlainObject& x_norm,
47 typename
drake::math::Gradient<Derived, Derived::RowsAtCompileTime,
48 1>::type* dx_norm =
nullptr,
49 typename
drake::math::Gradient<Derived, Derived::RowsAtCompileTime,
50 2>::type* ddx_norm =
nullptr) {
51 typename Derived::Scalar xdotx = x.squaredNorm();
52 typename Derived::Scalar norm_x =
sqrt(xdotx);
56 dx_norm->setIdentity(x.rows(), x.rows());
57 (*dx_norm) -= x * x.transpose() / xdotx;
62 auto ddx_norm_times_norm = -
matGradMultMat(x_norm, x_norm.transpose(),
63 (*dx_norm), dx_norm_transpose);
64 auto dnorm_inv = -x.transpose() / (xdotx * norm_x);
65 (*ddx_norm) = ddx_norm_times_norm / norm_x;
66 auto temp = (*dx_norm) * norm_x;
67 typename Derived::Index n = x.rows();
68 for (
int col = 0; col < n; col++) {
69 auto column_as_matrix = (dnorm_inv(0, col) * temp);
70 for (
int row_block = 0; row_block < n; row_block++) {
71 ddx_norm->block(row_block * n, col, n, 1) +=
72 column_as_matrix.col(row_block);
84 template <
typename Derived>
88 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Eigen::MatrixBase<Derived>,
93 typename Eigen::MatrixBase<Derived>::PlainObject qtilde;
97 typedef typename Derived::Scalar Scalar;
100 Scalar y = qtilde(2);
101 Scalar z = qtilde(3);
103 ret << w,
x, -y, -z, z, y,
x, w, -y, z, -w,
x, -z, y,
x, -w, w, -
x, y, -z,
x,
104 w, z, y, y, z, w,
x, -
x, -w, z, y, w, -
x, -y, z;
110 template <
typename DerivedR,
typename DerivedDR>
112 Eigen::Matrix<typename DerivedR::Scalar, drake::kRpySize, 1>,
113 DerivedDR::ColsAtCompileTime>::type
115 const Eigen::MatrixBase<DerivedDR>& dR) {
116 EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(Eigen::MatrixBase<DerivedR>,
120 Eigen::MatrixBase<DerivedDR>::RowsAtCompileTime ==
RotmatSize,
121 THIS_METHOD_IS_ONLY_FOR_MATRICES_OF_A_SPECIFIC_SIZE);
123 typename DerivedDR::Index
nq = dR.cols();
124 typedef typename DerivedR::Scalar Scalar;
126 Eigen::Matrix<Scalar, drake::kRpySize, 1>,
127 DerivedDR::ColsAtCompileTime>::type ReturnType;
131 getSubMatrixGradient<DerivedDR::ColsAtCompileTime>(dR, 0, 0, R.rows());
133 getSubMatrixGradient<DerivedDR::ColsAtCompileTime>(dR, 1, 0, R.rows());
135 getSubMatrixGradient<DerivedDR::ColsAtCompileTime>(dR, 2, 0, R.rows());
137 getSubMatrixGradient<DerivedDR::ColsAtCompileTime>(dR, 2, 1, R.rows());
139 getSubMatrixGradient<DerivedDR::ColsAtCompileTime>(dR, 2, 2, R.rows());
141 Scalar sqterm = R(2, 1) * R(2, 1) + R(2, 2) * R(2, 2);
145 drpy.row(0) = (R(2, 2) * dR32_dq - R(2, 1) * dR33_dq) / sqterm;
148 Scalar sqrt_sqterm =
sqrt(sqterm);
150 (-sqrt_sqterm * dR31_dq +
151 R(2, 0) / sqrt_sqterm * (R(2, 1) * dR32_dq + R(2, 2) * dR33_dq)) /
152 (R(2, 0) * R(2, 0) + R(2, 1) * R(2, 1) + R(2, 2) * R(2, 2));
155 sqterm = R(0, 0) * R(0, 0) + R(1, 0) * R(1, 0);
156 drpy.row(2) = (R(0, 0) * dR21_dq - R(1, 0) * dR11_dq) / sqterm;
160 template <
typename DerivedR,
typename DerivedDR>
162 Eigen::Matrix<typename DerivedR::Scalar, drake::kQuaternionSize, 1>,
163 DerivedDR::ColsAtCompileTime>::type
165 const Eigen::MatrixBase<DerivedDR>& dR) {
166 EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(Eigen::MatrixBase<DerivedR>,
170 Eigen::MatrixBase<DerivedDR>::RowsAtCompileTime ==
RotmatSize,
171 THIS_METHOD_IS_ONLY_FOR_MATRICES_OF_A_SPECIFIC_SIZE);
173 typedef typename DerivedR::Scalar Scalar;
175 Eigen::Matrix<Scalar, drake::kQuaternionSize, 1>,
176 DerivedDR::ColsAtCompileTime>::type ReturnType;
177 typename DerivedDR::Index
nq = dR.cols();
180 getSubMatrixGradient<DerivedDR::ColsAtCompileTime>(dR, 0, 0, R.rows());
182 getSubMatrixGradient<DerivedDR::ColsAtCompileTime>(dR, 0, 1, R.rows());
184 getSubMatrixGradient<DerivedDR::ColsAtCompileTime>(dR, 0, 2, R.rows());
186 getSubMatrixGradient<DerivedDR::ColsAtCompileTime>(dR, 1, 0, R.rows());
188 getSubMatrixGradient<DerivedDR::ColsAtCompileTime>(dR, 1, 1, R.rows());
190 getSubMatrixGradient<DerivedDR::ColsAtCompileTime>(dR, 1, 2, R.rows());
192 getSubMatrixGradient<DerivedDR::ColsAtCompileTime>(dR, 2, 0, R.rows());
194 getSubMatrixGradient<DerivedDR::ColsAtCompileTime>(dR, 2, 1, R.rows());
196 getSubMatrixGradient<DerivedDR::ColsAtCompileTime>(dR, 2, 2, R.rows());
198 Eigen::Matrix<Scalar, 4, 3>
A;
199 A.row(0) << 1.0, 1.0, 1.0;
200 A.row(1) << 1.0, -1.0, -1.0;
201 A.row(2) << -1.0, 1.0, -1.0;
202 A.row(3) << -1.0, -1.0, 1.0;
203 Eigen::Matrix<Scalar, 4, 1>
B = A * R.diagonal();
204 typename Eigen::Matrix<Scalar, 4, 1>::Index ind, max_col;
205 Scalar val = B.maxCoeff(&ind, &max_col);
212 auto dvaldq = dR11_dq + dR22_dq + dR33_dq;
213 auto dwdq = dvaldq / (4.0 *
sqrt(1.0 + val));
214 auto w =
sqrt(1.0 + val) / 2.0;
215 auto wsquare4 = 4.0 * w * w;
218 ((dR32_dq - dR23_dq) * w - (R(2, 1) - R(1, 2)) * dwdq) / wsquare4;
220 ((dR13_dq - dR31_dq) * w - (R(0, 2) - R(2, 0)) * dwdq) / wsquare4;
222 ((dR21_dq - dR12_dq) * w - (R(1, 0) - R(0, 1)) * dwdq) / wsquare4;
227 auto dvaldq = dR11_dq - dR22_dq - dR33_dq;
228 auto s = 2.0 *
sqrt(1.0 + val);
229 auto ssquare = s * s;
230 auto dsdq = dvaldq /
sqrt(1.0 + val);
232 ((dR32_dq - dR23_dq) * s - (R(2, 1) - R(1, 2)) * dsdq) / ssquare;
233 dq.row(1) = .25 * dsdq;
235 ((dR12_dq + dR21_dq) * s - (R(0, 1) + R(1, 0)) * dsdq) / ssquare;
237 ((dR13_dq + dR31_dq) * s - (R(0, 2) + R(2, 0)) * dsdq) / ssquare;
242 auto dvaldq = -dR11_dq + dR22_dq - dR33_dq;
243 auto s = 2.0 * (
sqrt(1.0 + val));
244 auto ssquare = s * s;
245 auto dsdq = dvaldq /
sqrt(1.0 + val);
247 ((dR13_dq - dR31_dq) * s - (R(0, 2) - R(2, 0)) * dsdq) / ssquare;
249 ((dR12_dq + dR21_dq) * s - (R(0, 1) + R(1, 0)) * dsdq) / ssquare;
250 dq.row(2) = .25 * dsdq;
252 ((dR23_dq + dR32_dq) * s - (R(1, 2) + R(2, 1)) * dsdq) / ssquare;
257 auto dvaldq = -dR11_dq - dR22_dq + dR33_dq;
258 auto s = 2.0 * (
sqrt(1.0 + val));
259 auto ssquare = s * s;
260 auto dsdq = dvaldq /
sqrt(1.0 + val);
262 ((dR21_dq - dR12_dq) * s - (R(1, 0) - R(0, 1)) * dsdq) / ssquare;
264 ((dR13_dq + dR31_dq) * s - (R(0, 2) + R(2, 0)) * dsdq) / ssquare;
266 ((dR23_dq + dR32_dq) * s - (R(1, 2) + R(2, 1)) * dsdq) / ssquare;
267 dq.row(3) = .25 * dsdq;
277 template <
typename Derived>
279 const Eigen::MatrixBase<Derived>& p) {
280 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Eigen::MatrixBase<Derived>,
282 Eigen::Matrix<typename Derived::Scalar, 3, 3> ret;
283 ret << 0.0, -p(2), p(1), p(2), 0.0, -p(0), -p(1), p(0), 0.0;
287 template <
typename DerivedA,
typename DerivedB>
289 const Eigen::MatrixBase<DerivedA>& a,
const Eigen::MatrixBase<DerivedB>& b,
292 Eigen::Matrix<typename DerivedA::Scalar, 3, Eigen::Dynamic> ret(3, da.cols());
293 ret.noalias() = da.colwise().cross(b);
294 ret.noalias() -= db.colwise().cross(a);
301 template <
typename DerivedQ,
typename DerivedM,
typename DerivedDM>
303 Eigen::MatrixBase<DerivedM>& M,
304 Eigen::MatrixBase<DerivedDM>* dM =
nullptr) {
306 using Scalar =
typename DerivedQ::Scalar;
308 M.row(0) << -q(1), -q(2), -q(3);
309 M.row(1) << q(0), q(3), -q(2);
310 M.row(2) << -q(3), q(0), q(1);
311 M.row(3) << q(2), -q(1), q(0);
315 (*dM) << Scalar(0), Scalar(-0.5), Scalar(0), Scalar(0), Scalar(0.5),
316 Scalar(0), Scalar(0), Scalar(0), Scalar(0), Scalar(0), Scalar(0),
317 Scalar(-0.5), Scalar(0), Scalar(0), Scalar(0.5), Scalar(0), Scalar(0),
318 Scalar(0), Scalar(-0.5), Scalar(0), Scalar(0), Scalar(0), Scalar(0),
319 Scalar(0.5), Scalar(0.5), Scalar(0), Scalar(0), Scalar(0), Scalar(0),
320 Scalar(-0.5), Scalar(0), Scalar(0), Scalar(0), Scalar(0), Scalar(0),
321 Scalar(-0.5), Scalar(0), Scalar(0), Scalar(-0.5), Scalar(0), Scalar(0),
322 Scalar(0.5), Scalar(0), Scalar(0), Scalar(0.5), Scalar(0), Scalar(0),
327 template <
typename DerivedRPY,
typename DerivedPhi,
typename DerivedDPhi,
328 typename DerivedDDPhi>
330 const Eigen::MatrixBase<DerivedRPY>& rpy,
331 typename Eigen::MatrixBase<DerivedPhi>& phi,
332 typename Eigen::MatrixBase<DerivedDPhi>* dphi =
nullptr,
333 typename Eigen::MatrixBase<DerivedDDPhi>* ddphi =
nullptr) {
336 typedef typename DerivedRPY::Scalar Scalar;
347 phi << cy / cp, sy / cp, Scalar(0), -sy, cy, Scalar(0), cy * tp, tp * sy,
351 Scalar sp2 = sp * sp;
352 Scalar cp2 = cp * cp;
353 (*dphi) << Scalar(0), (cy * sp) / cp2, -sy / cp, Scalar(0), Scalar(0), -cy,
354 Scalar(0), cy + (cy * sp2) / cp2, -(sp * sy) / cp, Scalar(0),
355 (sp * sy) / cp2, cy / cp, Scalar(0), Scalar(0), -sy, Scalar(0),
356 sy + (sp2 * sy) / cp2, (cy * sp) / cp, Scalar(0), Scalar(0), Scalar(0),
357 Scalar(0), Scalar(0), Scalar(0), Scalar(0), Scalar(0), Scalar(0);
361 Scalar cp3 = cp2 * cp;
362 (*ddphi) << Scalar(0), Scalar(0), Scalar(0), Scalar(0), Scalar(0),
363 Scalar(0), Scalar(0), Scalar(0), Scalar(0), Scalar(0), Scalar(0),
364 Scalar(0), Scalar(0), Scalar(0), Scalar(0), Scalar(0), Scalar(0),
365 Scalar(0), Scalar(0), Scalar(0), Scalar(0), Scalar(0), Scalar(0),
366 Scalar(0), Scalar(0), Scalar(0), Scalar(0), Scalar(0),
367 -(cy * (cp2 - Scalar(2))) / cp3, (sp * sy) / (sp2 - Scalar(1)),
368 Scalar(0), Scalar(0), Scalar(0), Scalar(0),
369 (Scalar(2) * cy * sp) / cp3, sy / (sp2 - Scalar(1)), Scalar(0),
370 (Scalar(2) * sy - cp2 * sy) / cp3, (cy * sp) / cp2, Scalar(0),
371 Scalar(0), Scalar(0), Scalar(0), (Scalar(2) * sp * sy) / cp3,
372 cy / cp2, Scalar(0), Scalar(0), Scalar(0), Scalar(0), Scalar(0),
373 Scalar(0), Scalar(0), Scalar(0), Scalar(0), Scalar(0),
374 (sp * sy) / (sp2 - Scalar(1)), -cy / cp, Scalar(0), Scalar(0), sy,
375 Scalar(0), sy / (sp2 - Scalar(1)), -(cy * sp) / cp, Scalar(0),
376 (cy * sp) / cp2, -sy / cp, Scalar(0), Scalar(0), -cy, Scalar(0),
377 cy / cp2, -(sp * sy) / cp, Scalar(0), Scalar(0), Scalar(0), Scalar(0),
378 Scalar(0), Scalar(0), Scalar(0), Scalar(0), Scalar(0);
383 template <
typename DerivedRPY,
typename DerivedE>
385 const Eigen::MatrixBase<DerivedRPY>& rpy, Eigen::MatrixBase<DerivedE>& E,
388 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Eigen::MatrixBase<DerivedRPY>,
390 EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(Eigen::MatrixBase<DerivedE>,
393 typedef typename DerivedRPY::Scalar Scalar;
401 E << cp * cy, -sy, 0.0, cp * sy, cy, 0.0, -sp, 0.0, 1.0;
403 (*dE) << 0.0, -sp * cy, -cp * sy, 0.0, -sp * sy, cp * cy, 0.0, -cp, 0.0,
404 0.0, 0.0, -cy, 0.0, 0.0, -sy, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
409 template <
typename Derived>
411 const Eigen::MatrixBase<Derived>& q) {
412 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Eigen::MatrixBase<Derived>,
414 typedef typename Derived::Scalar Scalar;
415 auto qtilde = q.normalized();
416 Eigen::Matrix<Scalar, 3, 4> ret;
417 ret << -qtilde(1), qtilde(0), -qtilde(3), qtilde(2), -qtilde(2), qtilde(3),
418 qtilde(0), -qtilde(1), -qtilde(3), -qtilde(2), qtilde(1), qtilde(0);
423 template <
typename DerivedRPY,
typename DerivedRPYdot,
typename DerivedOMEGA>
425 const Eigen::MatrixBase<DerivedRPY>& rpy,
426 const Eigen::MatrixBase<DerivedRPYdot>& rpydot,
427 Eigen::MatrixBase<DerivedOMEGA>& omega,
429 1>::type* domega =
nullptr) {
430 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Eigen::MatrixBase<DerivedRPY>,
432 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Eigen::MatrixBase<DerivedRPYdot>,
434 EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(Eigen::MatrixBase<DerivedOMEGA>,
437 Eigen::Matrix<typename DerivedOMEGA::Scalar, 3, 3> E;
439 Eigen::Matrix<typename DerivedOMEGA::Scalar, 9, 3> dE;
451 template <
typename Derived>
454 Derived::ColsAtCompileTime>
type;
457 template <
typename DerivedM>
459 const Eigen::Transform<typename DerivedM::Scalar, 3, Eigen::Isometry>& T,
460 const Eigen::MatrixBase<DerivedM>& M) {
462 DerivedM::ColsAtCompileTime> ret(drake::kTwistSize, M.cols());
463 ret.template topRows<3>().noalias() = T.linear() * M.template topRows<3>();
464 ret.template bottomRows<3>().noalias() =
465 -ret.template topRows<3>().colwise().cross(T.translation());
466 ret.template bottomRows<3>().noalias() +=
467 T.linear() * M.template bottomRows<3>();
471 template <
typename Scalar,
typename DerivedX,
typename DerivedDT,
475 const Eigen::MatrixBase<DerivedX>& X,
476 const Eigen::MatrixBase<DerivedDT>& dT,
477 const Eigen::MatrixBase<DerivedDX>& dX) {
479 typename DerivedDT::Index
nq = dT.cols();
481 const auto& R = T.linear();
482 const auto& p = T.translation();
484 std::array<int, 3> rows = {{0, 1, 2}};
485 std::array<int, 3> R_cols = {{0, 1, 2}};
486 std::array<int, 1> p_cols = {{3}};
488 auto dR = getSubMatrixGradient<Eigen::Dynamic>(dT, rows, R_cols, T.Rows);
489 auto dp = getSubMatrixGradient<Eigen::Dynamic>(dT, rows, p_cols, T.Rows);
492 1>::type ret(X.size(),
nq);
493 std::array<int, 3> Xomega_rows = {{0, 1, 2}};
494 std::array<int, 3> Xv_rows = {{3, 4, 5}};
495 for (
int col = 0; col < X.cols(); col++) {
496 auto Xomega_col = X.template block<3, 1>(0, col);
497 auto Xv_col = X.template block<3, 1>(3, col);
499 auto RXomega_col = (R * Xomega_col).eval();
501 std::array<int, 1> col_array = {{col}};
502 auto dXomega_col = getSubMatrixGradient<Eigen::Dynamic>(
503 dX, Xomega_rows, col_array, X.rows());
505 getSubMatrixGradient<Eigen::Dynamic>(dX, Xv_rows, col_array, X.rows());
507 auto domega_part_col =
508 (R * dXomega_col +
matGradMult(dR, Xomega_col)).eval();
509 auto dv_part_col = (R * dXv_col +
matGradMult(dR, Xv_col)).eval();
510 dv_part_col += dp.colwise().cross(RXomega_col);
511 dv_part_col -= domega_part_col.colwise().cross(p);
513 setSubMatrixGradient<Eigen::Dynamic>(ret, domega_part_col, Xomega_rows,
514 col_array, X.rows());
515 setSubMatrixGradient<Eigen::Dynamic>(ret, dv_part_col, Xv_rows, col_array,
521 template <
typename DerivedF>
523 const Eigen::Transform<typename DerivedF::Scalar, 3, Eigen::Isometry>& T,
524 const Eigen::MatrixBase<DerivedF>&
F) {
526 DerivedF::ColsAtCompileTime> ret(drake::kTwistSize, F.cols());
527 ret.template bottomRows<3>().noalias() =
528 T.linear() * F.template bottomRows<3>().eval();
529 ret.template topRows<3>() =
530 -ret.template bottomRows<3>().colwise().cross(T.translation());
531 ret.template topRows<3>().noalias() += T.linear() * F.template topRows<3>();
535 template <
typename Scalar,
typename DerivedX,
typename DerivedDT,
539 const Eigen::MatrixBase<DerivedX>& X,
540 const Eigen::MatrixBase<DerivedDT>& dT,
541 const Eigen::MatrixBase<DerivedDX>& dX) {
543 typename DerivedDT::Index
nq = dT.cols();
545 const auto& R = T.linear();
546 const auto& p = T.translation();
548 std::array<int, 3> rows = {{0, 1, 2}};
549 std::array<int, 3> R_cols = {{0, 1, 2}};
550 std::array<int, 1> p_cols = {{3}};
552 auto dR = getSubMatrixGradient<Eigen::Dynamic>(dT, rows, R_cols, T.Rows);
553 auto dp = getSubMatrixGradient<Eigen::Dynamic>(dT, rows, p_cols, T.Rows);
557 std::array<int, 3> Xomega_rows = {{0, 1, 2}};
558 std::array<int, 3> Xv_rows = {{3, 4, 5}};
559 for (
int col = 0; col < X.cols(); col++) {
560 auto Xomega_col = X.template block<3, 1>(0, col);
561 auto Xv_col = X.template block<3, 1>(3, col);
563 auto RXv_col = (R * Xv_col).eval();
565 std::array<int, 1> col_array = {{col}};
566 auto dXomega_col = getSubMatrixGradient<Eigen::Dynamic>(
567 dX, Xomega_rows, col_array, X.rows());
569 getSubMatrixGradient<Eigen::Dynamic>(dX, Xv_rows, col_array, X.rows());
571 auto domega_part_col = (R * dXomega_col).eval();
573 auto dv_part_col = (R * dXv_col).eval();
575 domega_part_col += dp.colwise().cross(RXv_col);
576 domega_part_col -= dv_part_col.colwise().cross(p);
578 setSubMatrixGradient<Eigen::Dynamic>(ret, domega_part_col, Xomega_rows,
579 col_array, X.rows());
580 setSubMatrixGradient<Eigen::Dynamic>(ret, dv_part_col, Xv_rows, col_array,
586 template <
typename DerivedI>
588 using namespace Eigen;
589 using Scalar =
typename DerivedI::Scalar;
592 auto J = I.template topLeftCorner<3, 3>();
593 auto cross_part_1 = I.template topRightCorner<3, 3>();
594 auto cross_part_2 = I.template bottomLeftCorner<3, 3>();
595 const auto& m = I(3, 3);
596 ret = ret && (J - J.transpose()).isZero();
598 (m * Matrix<Scalar, 3, 3>::Identity() -
599 I.template bottomRightCorner<3, 3>())
602 (cross_part_1 - cross_part_2)
605 (cross_part_1 + cross_part_1.transpose())
611 template <
typename DerivedI>
614 Eigen::Isometry>& T_current_to_new,
615 const Eigen::MatrixBase<DerivedI>& I) {
616 using namespace Eigen;
617 using Scalar =
typename DerivedI::Scalar;
625 const auto& R = T_current_to_new.linear();
626 const auto& p = T_current_to_new.translation();
628 auto J = I.template topLeftCorner<3, 3>();
629 Matrix<Scalar, 3, 1> c;
630 c << I(2, 4), I(0, 5), I(1, 3);
631 const auto& m = I(3, 3);
633 auto vectorToSkewSymmetricSquared = [](
const Matrix<Scalar, 3, 1>& a) {
634 Matrix<Scalar, 3, 3> ret;
635 auto a0_2 = a(0) * a(0);
636 auto a1_2 = a(1) * a(1);
637 auto a2_2 = a(2) * a(2);
639 ret(0, 0) = -a1_2 - a2_2;
640 ret(0, 1) = a(0) * a(1);
641 ret(0, 2) = a(0) * a(2);
643 ret(1, 0) = ret(0, 1);
644 ret(1, 1) = -a0_2 - a2_2;
645 ret(1, 2) = a(1) * a(2);
647 ret(2, 0) = ret(0, 2);
648 ret(2, 1) = ret(1, 2);
649 ret(2, 2) = -a0_2 - a1_2;
654 auto c_new = (R * c).eval();
655 auto J_new = I_new.template topLeftCorner<3, 3>();
657 if (m > NumTraits<Scalar>::epsilon()) {
658 J_new = vectorToSkewSymmetricSquared(c_new);
659 c_new.noalias() += m * p;
660 J_new -= vectorToSkewSymmetricSquared(c_new);
665 J_new.noalias() += R * J.template selfadjointView<Lower>() * R.transpose();
668 I_new.template bottomLeftCorner<3, 3>() =
669 -I_new.template topRightCorner<3, 3>();
670 I_new.template bottomRightCorner<3, 3>() =
671 I.template bottomRightCorner<3, 3>();
677 I_half_transformed.transpose());
681 template <
typename DerivedA,
typename DerivedB>
683 const Eigen::MatrixBase<DerivedA>& a,
684 const Eigen::MatrixBase<DerivedB>& b) {
686 ret.template topRows<3>() =
687 -b.template topRows<3>().colwise().cross(a.template topRows<3>());
688 ret.template bottomRows<3>() =
689 -b.template topRows<3>().colwise().cross(a.template bottomRows<3>());
690 ret.template bottomRows<3>() -=
691 b.template bottomRows<3>().colwise().cross(a.template topRows<3>());
695 template <
typename DerivedA,
typename DerivedB>
697 const Eigen::MatrixBase<DerivedA>& a,
698 const Eigen::MatrixBase<DerivedB>& b) {
700 ret.template topRows<3>() =
701 -b.template topRows<3>().colwise().cross(a.template topRows<3>());
702 ret.template topRows<3>() -=
703 b.template bottomRows<3>().colwise().cross(a.template bottomRows<3>());
704 ret.template bottomRows<3>() =
705 -b.template bottomRows<3>().colwise().cross(a.template topRows<3>());
709 template <
typename DerivedA,
typename DerivedB>
711 const Eigen::MatrixBase<DerivedA>& a,
const Eigen::MatrixBase<DerivedB>& b,
716 ret.row(0) = -da.row(2) * b[1] + da.row(1) * b[2] - a[2] * db.row(1) +
719 da.row(2) * b[0] - da.row(0) * b[2] + a[2] * db.row(0) - a[0] * db.row(2);
720 ret.row(2) = -da.row(1) * b[0] + da.row(0) * b[1] - a[1] * db.row(0) +
722 ret.row(3) = -da.row(5) * b[1] + da.row(4) * b[2] - da.row(2) * b[4] +
723 da.row(1) * b[5] - a[5] * db.row(1) + a[4] * db.row(2) -
724 a[2] * db.row(4) + a[1] * db.row(5);
725 ret.row(4) = da.row(5) * b[0] - da.row(3) * b[2] + da.row(2) * b[3] -
726 da.row(0) * b[5] + a[5] * db.row(0) - a[3] * db.row(2) +
727 a[2] * db.row(3) - a[0] * db.row(5);
728 ret.row(5) = -da.row(4) * b[0] + da.row(3) * b[1] - da.row(1) * b[3] +
729 da.row(0) * b[4] - a[4] * db.row(0) + a[3] * db.row(1) -
730 a[1] * db.row(3) + a[0] * db.row(4);
734 template <
typename DerivedA,
typename DerivedB>
736 const Eigen::MatrixBase<DerivedA>& a,
const Eigen::MatrixBase<DerivedB>& b,
741 ret.row(0) = da.row(2) * b[1] - da.row(1) * b[2] + da.row(5) * b[4] -
742 da.row(4) * b[5] + a[2] * db.row(1) - a[1] * db.row(2) +
743 a[5] * db.row(4) - a[4] * db.row(5);
744 ret.row(1) = -da.row(2) * b[0] + da.row(0) * b[2] - da.row(5) * b[3] +
745 da.row(3) * b[5] - a[2] * db.row(0) + a[0] * db.row(2) -
746 a[5] * db.row(3) + a[3] * db.row(5);
747 ret.row(2) = da.row(1) * b[0] - da.row(0) * b[1] + da.row(4) * b[3] -
748 da.row(3) * b[4] + a[1] * db.row(0) - a[0] * db.row(1) +
749 a[4] * db.row(3) - a[3] * db.row(4);
751 da.row(2) * b[4] - da.row(1) * b[5] + a[2] * db.row(4) - a[1] * db.row(5);
752 ret.row(4) = -da.row(2) * b[3] + da.row(0) * b[5] - a[2] * db.row(3) +
755 da.row(1) * b[3] - da.row(0) * b[4] + a[1] * db.row(3) - a[0] * db.row(4);
763 template <
typename DerivedQdotToV>
765 typedef typename Eigen::Matrix<
typename DerivedQdotToV::Scalar,
767 DerivedQdotToV::ColsAtCompileTime>
type;
770 template <
typename DerivedS,
typename DerivedQdotToV>
772 const Eigen::Transform<typename DerivedQdotToV::Scalar, 3, Eigen::Isometry>&
774 const Eigen::MatrixBase<DerivedS>& S,
775 const Eigen::MatrixBase<DerivedQdotToV>& qdot_to_v) {
776 const int nq_at_compile_time = DerivedQdotToV::ColsAtCompileTime;
777 typename DerivedQdotToV::Index
nq = qdot_to_v.cols();
778 auto qdot_to_twist = (S * qdot_to_v).eval();
781 Eigen::Matrix<typename DerivedQdotToV::Scalar, numel, nq_at_compile_time> ret(
784 const auto& Rx = T.linear().col(0);
785 const auto& Ry = T.linear().col(1);
786 const auto& Rz = T.linear().col(2);
788 const auto& qdot_to_omega_x = qdot_to_twist.row(0);
789 const auto& qdot_to_omega_y = qdot_to_twist.row(1);
790 const auto& qdot_to_omega_z = qdot_to_twist.row(2);
792 ret.template middleRows<3>(0) = -Rz * qdot_to_omega_y + Ry * qdot_to_omega_z;
793 ret.row(3).setZero();
795 ret.template middleRows<3>(4) = Rz * qdot_to_omega_x - Rx * qdot_to_omega_z;
796 ret.row(7).setZero();
798 ret.template middleRows<3>(8) = -Ry * qdot_to_omega_x + Rx * qdot_to_omega_y;
799 ret.row(11).setZero();
801 ret.template middleRows<3>(12) = T.linear() * qdot_to_twist.bottomRows(3);
802 ret.row(15).setZero();
807 template <
typename DerivedDT>
809 const Eigen::Transform<typename DerivedDT::Scalar, 3, Eigen::Isometry>& T,
810 const Eigen::MatrixBase<DerivedDT>& dT) {
811 typename DerivedDT::Index
nq = dT.cols();
813 const auto& R = T.linear();
814 const auto& p = T.translation();
816 std::array<int, 3> rows = {{0, 1, 2}};
817 std::array<int, 3> R_cols = {{0, 1, 2}};
818 std::array<int, 1> p_cols = {{3}};
820 auto dR = getSubMatrixGradient<Eigen::Dynamic>(dT, rows, R_cols, T.Rows);
821 auto dp = getSubMatrixGradient<Eigen::Dynamic>(dT, rows, p_cols, T.Rows);
824 auto dinvT_p = (-R.transpose() * dp -
matGradMult(dinvT_R, p)).eval();
827 Eigen::Matrix<typename DerivedDT::Scalar, numel, DerivedDT::ColsAtCompileTime>
829 setSubMatrixGradient<Eigen::Dynamic>(ret, dinvT_R, rows, R_cols, T.Rows);
830 setSubMatrixGradient<Eigen::Dynamic>(ret, dinvT_p, rows, p_cols, T.Rows);
833 const int last_row = 3;
834 for (
int col = 0; col < T.HDim; col++) {
835 ret.row(last_row + col * T.Rows).setZero();
841 template <
typename Derived>
843 const Eigen::MatrixBase<Derived>& expmap) {
844 using namespace Eigen;
845 typedef typename Derived::Scalar Scalar;
847 Derived::RowsAtCompileTime == 3 && Derived::ColsAtCompileTime == 1,
850 Scalar expmap_norm = expmap.norm();
851 bool is_degenerate = (expmap_norm < std::numeric_limits<double>::epsilon());
852 Eigen::Matrix<Scalar, 3, 1> ret = expmap;
853 if (!is_degenerate) ret -= expmap / expmap_norm * 2 * M_PI;
858 template <
typename Derived1,
typename Derived2>
860 const Eigen::MatrixBase<Derived1>& expmap1,
861 const Eigen::MatrixBase<Derived2>& expmap2) {
862 using namespace Eigen;
864 Derived1::RowsAtCompileTime == 3 && Derived1::ColsAtCompileTime == 1,
867 Derived2::RowsAtCompileTime == 3 && Derived2::ColsAtCompileTime == 1,
870 std::is_same<typename Derived1::Scalar, typename Derived2::Scalar>::value,
871 "Scalar types don't match.");
872 typedef typename Derived1::Scalar Scalar;
873 typedef typename NumTraits<Scalar>::Real Real;
876 Real distance1 = (expmap1 - expmap2).squaredNorm();
877 Real distance2 = (expmap1 - expmap2_flip).squaredNorm();
878 if (distance1 > distance2) {
const int SPACE_DIMENSION
Definition: drakeGeometryUtil.h:28
const int RotmatSize
Definition: drakeGeometryUtil.h:30
std::vector< Number > x
Definition: IpoptSolver.cpp:169
This file contains abbreviated definitions for certain specializations of Eigen::Matrix that are comm...
DHomogTrans< DerivedDT >::type dHomogTransInv(const Eigen::Transform< typename DerivedDT::Scalar, 3, Eigen::Isometry > &T, const Eigen::MatrixBase< DerivedDT > &dT)
Definition: drakeGeometryUtil.h:808
DRAKEGEOMETRYUTIL_EXPORT Eigen::Vector4d uniformlyRandomAxisAngle(std::default_random_engine &generator)
Definition: drakeGeometryUtil.cpp:20
Provides a portable macro for use in generating compile-time warnings for use of code that is permitt...
void normalizeVec(const Eigen::MatrixBase< Derived > &x, typename Derived::PlainObject &x_norm, typename drake::math::Gradient< Derived, Derived::RowsAtCompileTime, 1 >::type *dx_norm=nullptr, typename drake::math::Gradient< Derived, Derived::RowsAtCompileTime, 2 >::type *ddx_norm=nullptr)
Definition: drakeGeometryUtil.h:45
Definition: constants.h:3
MatGradMult< DerivedDA, DerivedB >::type matGradMult(const Eigen::MatrixBase< DerivedDA > &dA, const Eigen::MatrixBase< DerivedB > &B)
Definition: drakeGradientUtil.h:143
drake::math::Gradient< DerivedX, DerivedDX::ColsAtCompileTime, 1 >::type dTransformSpatialMotion(const Eigen::Transform< Scalar, 3, Eigen::Isometry > &T, const Eigen::MatrixBase< DerivedX > &X, const Eigen::MatrixBase< DerivedDT > &dT, const Eigen::MatrixBase< DerivedDX > &dX)
Definition: drakeGeometryUtil.h:474
drake::math::Gradient< DerivedX, DerivedDX::ColsAtCompileTime >::type dTransformSpatialForce(const Eigen::Transform< Scalar, 3, Eigen::Isometry > &T, const Eigen::MatrixBase< DerivedX > &X, const Eigen::MatrixBase< DerivedDT > &dT, const Eigen::MatrixBase< DerivedDX > &dX)
Definition: drakeGeometryUtil.h:538
Eigen::Matrix< Scalar, kTwistSize, Eigen::Dynamic > TwistMatrix
A matrix with one twist per column, and dynamically many columns.
Definition: eigen_types.h:73
DRAKEGEOMETRYUTIL_EXPORT Eigen::Matrix3d uniformlyRandomRotmat(std::default_random_engine &generator)
Definition: drakeGeometryUtil.cpp:36
DRAKEGEOMETRYUTIL_EXPORT Eigen::Vector4d uniformlyRandomQuat(std::default_random_engine &generator)
Definition: drakeGeometryUtil.cpp:32
Eigen::Matrix< typename DerivedA::Scalar, 3, Eigen::Dynamic > dcrossProduct(const Eigen::MatrixBase< DerivedA > &a, const Eigen::MatrixBase< DerivedB > &b, const typename drake::math::Gradient< DerivedA, Eigen::Dynamic >::type &da, const typename drake::math::Gradient< DerivedB, Eigen::Dynamic >::type &db)
Definition: drakeGeometryUtil.h:288
FunctionalForm cos(FunctionalForm const &x)
Definition: functional_form.cc:188
default_random_engine generator
Definition: testSplineGeneration.cpp:11
drake::math::Gradient< Eigen::Matrix< typename Derived::Scalar, 3, 3 >, drake::kQuaternionSize >::type dquat2rotmat(const Eigen::MatrixBase< Derived > &q)
Definition: drakeGeometryUtil.h:87
Definition: drakeGeometryUtil.h:764
FunctionalForm sqrt(FunctionalForm const &x)
Definition: functional_form.cc:208
#define DRAKE_ASSERT(condition)
DRAKE_ASSERT(condition) is similar to the built-in assert(condition) from the C++ system header <cas...
Definition: drake_assert.h:35
void rpydot2angularvelMatrix(const Eigen::MatrixBase< DerivedRPY > &rpy, Eigen::MatrixBase< DerivedE > &E, typename drake::math::Gradient< DerivedE, drake::kRpySize, 1 >::type *dE=nullptr)
Definition: drakeGeometryUtil.h:384
void rpydot2angularvel(const Eigen::MatrixBase< DerivedRPY > &rpy, const Eigen::MatrixBase< DerivedRPYdot > &rpydot, Eigen::MatrixBase< DerivedOMEGA > &omega, typename drake::math::Gradient< DerivedOMEGA, drake::kRpySize, 1 >::type *domega=nullptr)
Definition: drakeGeometryUtil.h:424
constexpr int kTwistSize
https://en.wikipedia.org/wiki/Screw_theory#Twist
Definition: constants.h:12
Provides Drake's assertion implementation.
DRAKEGEOMETRYUTIL_EXPORT Eigen::Vector3d uniformlyRandomRPY(std::default_random_engine &generator)
Definition: drakeGeometryUtil.cpp:40
MatGradMultMat< DerivedA, DerivedB, DerivedDA >::type matGradMultMat(const Eigen::MatrixBase< DerivedA > &A, const Eigen::MatrixBase< DerivedB > &B, const Eigen::MatrixBase< DerivedDA > &dA, const Eigen::MatrixBase< DerivedDB > &dB)
Definition: drakeGradientUtil.h:106
DRAKEGEOMETRYUTIL_EXPORT double angleDiff(double phi1, double phi2)
Definition: drakeGeometryUtil.cpp:10
drake::SquareTwistMatrix< typename DerivedI::Scalar > transformSpatialInertia(const Eigen::Transform< typename DerivedI::Scalar, drake::kSpaceDimension, Eigen::Isometry > &T_current_to_new, const Eigen::MatrixBase< DerivedI > &I)
Definition: drakeGeometryUtil.h:612
static int nq
Definition: inverseKinSnoptBackend.cpp:60
drake::math::Gradient< Eigen::Matrix< typename DerivedR::Scalar, drake::kRpySize, 1 >, DerivedDR::ColsAtCompileTime >::type drotmat2rpy(const Eigen::MatrixBase< DerivedR > &R, const Eigen::MatrixBase< DerivedDR > &dR)
Definition: drakeGeometryUtil.h:114
drake::math::Gradient< Eigen::Matrix< typename DerivedR::Scalar, drake::kQuaternionSize, 1 >, DerivedDR::ColsAtCompileTime >::type drotmat2quat(const Eigen::MatrixBase< DerivedR > &R, const Eigen::MatrixBase< DerivedDR > &dR)
Definition: drakeGeometryUtil.h:164
const int QUAT_SIZE
Definition: drakeGeometryUtil.h:24
DRAKEGEOMETRYUTIL_EXPORT int rotationRepresentationSize(int rotation_type)
Definition: drakeGeometryUtil.cpp:44
Eigen::Matrix< typename Derived::Scalar, 3, 3 > vectorToSkewSymmetric(const Eigen::MatrixBase< Derived > &p)
Definition: drakeGeometryUtil.h:278
const int HOMOGENEOUS_TRANSFORM_SIZE
Definition: drakeGeometryUtil.h:21
Eigen::Matrix< typename Derived::Scalar, 3, 1 > flipExpmap(const Eigen::MatrixBase< Derived > &expmap)
Definition: drakeGeometryUtil.h:842
drake::TwistMatrix< typename DerivedA::Scalar > dCrossSpatialMotion(const Eigen::MatrixBase< DerivedA > &a, const Eigen::MatrixBase< DerivedB > &b, const typename drake::math::Gradient< DerivedA, Eigen::Dynamic >::type &da, const typename drake::math::Gradient< DerivedB, Eigen::Dynamic >::type &db)
Definition: drakeGeometryUtil.h:710
Eigen::Matrix< typename DerivedQdotToV::Scalar, HOMOGENEOUS_TRANSFORM_SIZE, DerivedQdotToV::ColsAtCompileTime > type
Definition: drakeGeometryUtil.h:767
DHomogTrans< DerivedQdotToV >::type dHomogTrans(const Eigen::Transform< typename DerivedQdotToV::Scalar, 3, Eigen::Isometry > &T, const Eigen::MatrixBase< DerivedS > &S, const Eigen::MatrixBase< DerivedQdotToV > &qdot_to_v)
Definition: drakeGeometryUtil.h:771
constexpr int kSpaceDimension
Definition: constants.h:7
#define DRAKE_DEPRECATED(message)
Use DRAKE_DEPRECATED("message") to discourage use of particular classes, typedefs, variables, non-static data members, functions, arguments, enumerations, and template specializations.
Definition: drake_deprecated.h:33
Eigen::Matrix< typename Derived1::Scalar, 3, 1 > unwrapExpmap(const Eigen::MatrixBase< Derived1 > &expmap1, const Eigen::MatrixBase< Derived2 > &expmap2)
Definition: drakeGeometryUtil.h:859
Utilities for arithmetic on quaternions.
std::vector< snopt::doublereal > F
Definition: SnoptSolver.cpp:47
TransformSpatial< DerivedF >::type transformSpatialForce(const Eigen::Transform< typename DerivedF::Scalar, 3, Eigen::Isometry > &T, const Eigen::MatrixBase< DerivedF > &F)
Definition: drakeGeometryUtil.h:522
constexpr int kQuaternionSize
Definition: constants.h:5
const int RPY_SIZE
Definition: drakeGeometryUtil.h:26
Eigen::Matrix< typename Derived::Scalar,((Derived::SizeAtCompileTime==Eigen::Dynamic||Nq==Eigen::Dynamic)?Eigen::Dynamic:Gradient< Derived, Nq, DerivativeOrder-1 >::type::SizeAtCompileTime), Nq > type
Definition: gradient.h:20
Definition: gradient.h:13
constexpr int kRpySize
Definition: constants.h:9
void angularvel2rpydotMatrix(const Eigen::MatrixBase< DerivedRPY > &rpy, typename Eigen::MatrixBase< DerivedPhi > &phi, typename Eigen::MatrixBase< DerivedDPhi > *dphi=nullptr, typename Eigen::MatrixBase< DerivedDDPhi > *ddphi=nullptr)
Definition: drakeGeometryUtil.h:329
TransformSpatial< DerivedB >::type crossSpatialMotion(const Eigen::MatrixBase< DerivedA > &a, const Eigen::MatrixBase< DerivedB > &b)
Definition: drakeGeometryUtil.h:682
Eigen::Matrix< Scalar, kTwistSize, kTwistSize > SquareTwistMatrix
A six-by-six matrix.
Definition: eigen_types.h:77
TransformSpatial< DerivedM >::type transformSpatialMotion(const Eigen::Transform< typename DerivedM::Scalar, 3, Eigen::Isometry > &T, const Eigen::MatrixBase< DerivedM > &M)
Definition: drakeGeometryUtil.h:458
Eigen::Matrix< typename Derived::Scalar, 3, 4 > quatdot2angularvelMatrix(const Eigen::MatrixBase< Derived > &q)
Definition: drakeGeometryUtil.h:410
void angularvel2quatdotMatrix(const Eigen::MatrixBase< DerivedQ > &q, Eigen::MatrixBase< DerivedM > &M, Eigen::MatrixBase< DerivedDM > *dM=nullptr)
Definition: drakeGeometryUtil.h:302
TransformSpatial< DerivedB >::type crossSpatialForce(const Eigen::MatrixBase< DerivedA > &a, const Eigen::MatrixBase< DerivedB > &b)
Definition: drakeGeometryUtil.h:696
FunctionalForm sin(FunctionalForm const &x)
Definition: functional_form.cc:203
Derived::PlainObject transposeGrad(const Eigen::MatrixBase< Derived > &dX, typename Derived::Index rows_X)
Definition: drakeGradientUtil.h:89
drake::TwistMatrix< typename DerivedA::Scalar > dCrossSpatialForce(const Eigen::MatrixBase< DerivedA > &a, const Eigen::MatrixBase< DerivedB > &b, const typename drake::math::Gradient< DerivedA, Eigen::Dynamic >::type &da, const typename drake::math::Gradient< DerivedB, Eigen::Dynamic >::type &db)
Definition: drakeGeometryUtil.h:735
bool isRegularInertiaMatrix(const Eigen::MatrixBase< DerivedI > &I)
Definition: drakeGeometryUtil.h:587