Drake
rotation_matrix.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cmath>
4 
5 #include <Eigen/Dense>
6 
8 
9 namespace drake {
10 namespace math {
11 
12 template <typename Derived>
14  const Eigen::MatrixBase<Derived>& R) {
15  using std::acos;
16  using std::sin;
17  EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(Eigen::MatrixBase<Derived>, 3, 3);
18 
19  typename Derived::Scalar theta = acos((R.trace() - 1.0) / 2.0);
20  Eigen::Vector4d a;
21  if (theta > std::numeric_limits<typename Derived::Scalar>::epsilon()) {
22  a << R(2, 1) - R(1, 2), R(0, 2) - R(2, 0), R(1, 0) - R(0, 1), theta;
23  a.head<3>() *= 1.0 / (2.0 * sin(theta));
24  } else {
25  a << 1.0, 0.0, 0.0, 0.0;
26  }
27  return a;
28 }
29 
30 template <typename Derived>
32  const Eigen::MatrixBase<Derived>& M) {
33  EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(Eigen::MatrixBase<Derived>, 3, 3);
34 
35  typedef typename Derived::Scalar Scalar;
36  Eigen::Matrix<Scalar, 4, 3> A;
37  A.row(0) << 1.0, 1.0, 1.0;
38  A.row(1) << 1.0, -1.0, -1.0;
39  A.row(2) << -1.0, 1.0, -1.0;
40  A.row(3) << -1.0, -1.0, 1.0;
41  Vector4<Scalar> B = A * M.diagonal();
42  Eigen::Index ind, max_col;
43  Scalar val = B.maxCoeff(&ind, &max_col);
44 
45  Scalar w, x, y, z;
46  switch (ind) {
47  case 0: {
48  // val = trace(M)
49  w = sqrt(1.0 + val) / 2.0;
50  Scalar w4 = w * 4.0;
51  x = (M(2, 1) - M(1, 2)) / w4;
52  y = (M(0, 2) - M(2, 0)) / w4;
53  z = (M(1, 0) - M(0, 1)) / w4;
54  break;
55  }
56  case 1: {
57  // val = M(1, 1) - M(2, 2) - M(3, 3)
58  Scalar s = 2.0 * sqrt(1.0 + val);
59  w = (M(2, 1) - M(1, 2)) / s;
60  x = 0.25 * s;
61  y = (M(0, 1) + M(1, 0)) / s;
62  z = (M(0, 2) + M(2, 0)) / s;
63  break;
64  }
65  case 2: {
66  // % val = M(2, 2) - M(1, 1) - M(3, 3)
67  Scalar s = 2.0 * (sqrt(1.0 + val));
68  w = (M(0, 2) - M(2, 0)) / s;
69  x = (M(0, 1) + M(1, 0)) / s;
70  y = 0.25 * s;
71  z = (M(1, 2) + M(2, 1)) / s;
72  break;
73  }
74  default: {
75  // val = M(3, 3) - M(2, 2) - M(1, 1)
76  Scalar s = 2.0 * (sqrt(1.0 + val));
77  w = (M(1, 0) - M(0, 1)) / s;
78  x = (M(0, 2) + M(2, 0)) / s;
79  y = (M(1, 2) + M(2, 1)) / s;
80  z = 0.25 * s;
81  break;
82  }
83  }
84 
86  q << w, x, y, z;
87  return q;
88 }
89 
90 template <typename Derived>
92  const Eigen::MatrixBase<Derived>& R) {
93  using std::atan2;
94  using std::pow;
95  using std::sqrt;
96  EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(Eigen::MatrixBase<Derived>, 3, 3);
97 
99  rpy << atan2(R(2, 1), R(2, 2)),
100  atan2(-R(2, 0), sqrt(pow(R(2, 1), 2.0) + pow(R(2, 2), 2.0))),
101  atan2(R(1, 0), R(0, 0));
102  return rpy;
103 }
104 
105 template <typename Derived>
107  const Eigen::MatrixBase<Derived>& R, int rotation_type) {
108  typedef typename Derived::Scalar Scalar;
109  switch (rotation_type) {
110  case 0:
111  return Eigen::Matrix<Scalar, Eigen::Dynamic, 1>(0, 1);
112  case 1:
113  return rotmat2rpy(R);
114  case 2:
115  return rotmat2quat(R);
116  default:
117  throw std::runtime_error("rotation representation type not recognized");
118  }
119 }
120 
121 } // namespace math
122 } // namespace drake
std::vector< Number > x
Definition: IpoptSolver.cpp:169
This file contains abbreviated definitions for certain specializations of Eigen::Matrix that are comm...
Eigen::Matrix< Scalar, 4, 1 > Vector4
A column vector of size 4, templated on scalar type.
Definition: eigen_types.h:28
Definition: constants.h:3
VectorX< typename Derived::Scalar > rotmat2Representation(const Eigen::MatrixBase< Derived > &R, int rotation_type)
Definition: rotation_matrix.h:106
Vector4< typename Derived::Scalar > rotmat2quat(const Eigen::MatrixBase< Derived > &M)
Definition: rotation_matrix.h:31
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > VectorX
A column vector of any size, templated on scalar type.
Definition: eigen_types.h:36
Eigen::Matrix< FunctionalForm, 3, 2 > A
Definition: functional_form_test.cc:619
FunctionalForm sqrt(FunctionalForm const &x)
Definition: functional_form.cc:208
Vector4< typename Derived::Scalar > rotmat2axis(const Eigen::MatrixBase< Derived > &R)
Definition: rotation_matrix.h:13
Eigen::Matrix< Scalar, 3, 1 > Vector3
A column vector of size 3, templated on scalar type.
Definition: eigen_types.h:24
Vector3< typename Derived::Scalar > rotmat2rpy(const Eigen::MatrixBase< Derived > &R)
Definition: rotation_matrix.h:91
Eigen::Matrix< FunctionalForm, 2, 3 > B
Definition: functional_form_test.cc:620
FunctionalForm sin(FunctionalForm const &x)
Definition: functional_form.cc:203