rdl_eigenmath.h
Go to the documentation of this file.
1 /*
2  * Original Copyright (c) 2011-2016 Martin Felis <martin.felis@iwr.uni-heidelberg.de>
3  *
4  *
5  * RDL - Robot Dynamics Library
6  * Modifications Copyright (c) 2017 Jordan Lack <jlack1987@gmail.com>
7  *
8  * Licensed under the zlib license. See LICENSE for more details.
9  */
10 
11 #ifndef __RDL_EIGENMATH_H__
12 #define __RDL_EIGENMATH_H__
13 
14 #include <Eigen/Dense>
15 #include <Eigen/StdVector>
16 #include <Eigen/QR>
17 #include <eigen3/Eigen/Eigen>
18 
19 namespace RobotDynamics
20 {
22 namespace Math
23 {
24 typedef Eigen::Matrix<double, 6, 3> Matrix63;
25 typedef Eigen::VectorXd VectorNd;
26 typedef Eigen::MatrixXd MatrixNd;
27 typedef Eigen::AngleAxisd AxisAngle;
28 } // namespace Math
29 } // namespace RobotDynamics
30 
31 namespace RobotDynamics
32 {
33 namespace Math
34 {
35 class SpatialTransform;
36 
44 {
45  public:
51  virtual void transform(const RobotDynamics::Math::SpatialTransform& X) = 0;
52 };
53 
54 class Vector3d : public Eigen::Vector3d, public TransformableGeometricObject
55 {
56  public:
57  typedef Eigen::Vector3d Base;
58 
59  template <typename OtherDerived>
60  // cppcheck-suppress noExplicitConstructor
61  Vector3d(const Eigen::MatrixBase<OtherDerived>& other) : Eigen::Vector3d(other)
62  {
63  }
64 
65  template <typename OtherDerived>
66  Vector3d& operator=(const Eigen::MatrixBase<OtherDerived>& other)
67  {
68  this->Base::operator=(other);
69  return *this;
70  }
71 
72  EIGEN_STRONG_INLINE Vector3d() : Vector3d(0., 0., 0.)
73  {
74  }
75 
76  EIGEN_STRONG_INLINE Vector3d(const double& v0, const double& v1, const double& v2)
77  {
78  Base::_check_template_params();
79 
80  (*this) << v0, v1, v2;
81  }
82 
83  void set(const Eigen::Vector3d& v)
84  {
85  Base::_check_template_params();
86 
87  set(v[0], v[1], v[2]);
88  }
89 
90  void set(const double& v0, const double& v1, const double& v2)
91  {
92  Base::_check_template_params();
93 
94  (*this) << v0, v1, v2;
95  }
96 
97  inline void transform(const RobotDynamics::Math::SpatialTransform& X);
98 
99  inline Vector3d transform_copy(const RobotDynamics::Math::SpatialTransform& X) const;
100 };
101 
102 class Matrix3d : public Eigen::Matrix3d
103 {
104  public:
105  typedef Eigen::Matrix3d Base;
106 
107  template <typename OtherDerived>
108  // cppcheck-suppress noExplicitConstructor
109  Matrix3d(const Eigen::MatrixBase<OtherDerived>& other) : Eigen::Matrix3d(other)
110  {
111  }
112 
113  template <typename OtherDerived>
114  Matrix3d& operator=(const Eigen::MatrixBase<OtherDerived>& other)
115  {
116  this->Base::operator=(other);
117  return *this;
118  }
119 
120  EIGEN_STRONG_INLINE Matrix3d()
121  {
122  }
123 
124  EIGEN_STRONG_INLINE Matrix3d(const double& m00, const double& m01, const double& m02, const double& m10, const double& m11, const double& m12, const double& m20,
125  const double& m21, const double& m22)
126  {
127  Base::_check_template_params();
128 
129  (*this) << m00, m01, m02, m10, m11, m12, m20, m21, m22;
130  }
131 };
132 
133 class Vector4d : public Eigen::Vector4d
134 {
135  public:
136  typedef Eigen::Vector4d Base;
137 
138  template <typename OtherDerived>
139  // cppcheck-suppress noExplicitConstructor
140  Vector4d(const Eigen::MatrixBase<OtherDerived>& other) : Eigen::Vector4d(other)
141  {
142  }
143 
144  template <typename OtherDerived>
145  Vector4d& operator=(const Eigen::MatrixBase<OtherDerived>& other)
146  {
147  this->Base::operator=(other);
148  return *this;
149  }
150 
151  EIGEN_STRONG_INLINE Vector4d()
152  {
153  }
154 
155  EIGEN_STRONG_INLINE Vector4d(const double& v0, const double& v1, const double& v2, const double& v3)
156  {
157  Base::_check_template_params();
158 
159  (*this) << v0, v1, v2, v3;
160  }
161 
162  void set(const double& v0, const double& v1, const double& v2, const double& v3)
163  {
164  Base::_check_template_params();
165 
166  (*this) << v0, v1, v2, v3;
167  }
168 };
169 
170 class SpatialVector : public Eigen::Matrix<double, 6, 1>
171 {
172  public:
173  typedef Eigen::Matrix<double, 6, 1> Base;
174 
175  template <typename OtherDerived>
176  // cppcheck-suppress noExplicitConstructor
177  SpatialVector(const Eigen::MatrixBase<OtherDerived>& other) : Eigen::Matrix<double, 6, 1>(other)
178  {
179  }
180 
181  template <typename OtherDerived>
182  SpatialVector& operator=(const Eigen::MatrixBase<OtherDerived>& other)
183  {
184  this->Base::operator=(other);
185  return *this;
186  }
187 
188  EIGEN_STRONG_INLINE SpatialVector()
189  {
190  (*this) << 0., 0., 0., 0., 0., 0.;
191  }
192 
193  EIGEN_STRONG_INLINE SpatialVector(const double& v0, const double& v1, const double& v2, const double& v3, const double& v4, const double& v5)
194  {
195  Base::_check_template_params();
196 
197  (*this) << v0, v1, v2, v3, v4, v5;
198  }
199 
200  EIGEN_STRONG_INLINE void set(const double& v0, const double& v1, const double& v2, const double& v3, const double& v4, const double& v5)
201  {
202  Base::_check_template_params();
203 
204  (*this) << v0, v1, v2, v3, v4, v5;
205  }
206 
207  EIGEN_STRONG_INLINE Vector3d getAngularPart() const
208  {
209  return Vector3d(this->data()[0], this->data()[1], this->data()[2]);
210  }
211 
212  EIGEN_STRONG_INLINE Vector3d getLinearPart() const
213  {
214  return Vector3d(this->data()[3], this->data()[4], this->data()[5]);
215  }
216 
217  inline void setAngularPart(const Vector3d& v)
218  {
219  this->data()[0] = v(0);
220  this->data()[1] = v(1);
221  this->data()[2] = v(2);
222  }
223 
224  inline void setLinearPart(const Vector3d& v)
225  {
226  this->data()[3] = v(0);
227  this->data()[4] = v(1);
228  this->data()[5] = v(2);
229  }
230 
231  EIGEN_STRONG_INLINE void set(const Vector3d& angularPart, const Vector3d& linearPart)
232  {
233  Base::_check_template_params();
234 
235  (*this) << angularPart[0], angularPart[1], angularPart[2], linearPart[0], linearPart[1], linearPart[2];
236  }
237 };
238 
239 class Matrix4d : public Eigen::Matrix<double, 4, 4>
240 {
241  public:
242  typedef Eigen::Matrix<double, 4, 4> Base;
243 
244  template <typename OtherDerived>
245  // cppcheck-suppress noExplicitConstructor
246  Matrix4d(const Eigen::MatrixBase<OtherDerived>& other) : Eigen::Matrix<double, 4, 4>(other)
247  {
248  }
249 
250  template <typename OtherDerived>
251  Matrix4d& operator=(const Eigen::MatrixBase<OtherDerived>& other)
252  {
253  this->Base::operator=(other);
254  return *this;
255  }
256 
257  EIGEN_STRONG_INLINE Matrix4d()
258  {
259  }
260 
261  EIGEN_STRONG_INLINE Matrix4d(const Scalar& m00, const Scalar& m01, const Scalar& m02, const Scalar& m03, const Scalar& m10, const Scalar& m11, const Scalar& m12,
262  const Scalar& m13, const Scalar& m20, const Scalar& m21, const Scalar& m22, const Scalar& m23, const Scalar& m30, const Scalar& m31,
263  const Scalar& m32, const Scalar& m33)
264  {
265  Base::_check_template_params();
266 
267  (*this) << m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33;
268  }
269 
270  void set(const Scalar& m00, const Scalar& m01, const Scalar& m02, const Scalar& m03, const Scalar& m10, const Scalar& m11, const Scalar& m12, const Scalar& m13,
271  const Scalar& m20, const Scalar& m21, const Scalar& m22, const Scalar& m23, const Scalar& m30, const Scalar& m31, const Scalar& m32, const Scalar& m33)
272  {
273  Base::_check_template_params();
274 
275  (*this) << m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33;
276  }
277 };
278 
279 class SpatialMatrix : public Eigen::Matrix<double, 6, 6>
280 {
281  public:
282  typedef Eigen::Matrix<double, 6, 6> Base;
283 
284  template <typename OtherDerived>
285  // cppcheck-suppress noExplicitConstructor
286  SpatialMatrix(const Eigen::MatrixBase<OtherDerived>& other) : Eigen::Matrix<double, 6, 6>(other)
287  {
288  }
289 
290  template <typename OtherDerived>
291  SpatialMatrix& operator=(const Eigen::MatrixBase<OtherDerived>& other)
292  {
293  this->Base::operator=(other);
294  return *this;
295  }
296 
297  EIGEN_STRONG_INLINE SpatialMatrix()
298  {
299  Base::_check_template_params();
300 
301  (*this) << 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.;
302  }
303 
304  EIGEN_STRONG_INLINE SpatialMatrix(const Scalar& m00, const Scalar& m01, const Scalar& m02, const Scalar& m03, const Scalar& m04, const Scalar& m05, const Scalar& m10,
305  const Scalar& m11, const Scalar& m12, const Scalar& m13, const Scalar& m14, const Scalar& m15, const Scalar& m20, const Scalar& m21,
306  const Scalar& m22, const Scalar& m23, const Scalar& m24, const Scalar& m25, const Scalar& m30, const Scalar& m31, const Scalar& m32,
307  const Scalar& m33, const Scalar& m34, const Scalar& m35, const Scalar& m40, const Scalar& m41, const Scalar& m42, const Scalar& m43,
308  const Scalar& m44, const Scalar& m45, const Scalar& m50, const Scalar& m51, const Scalar& m52, const Scalar& m53, const Scalar& m54,
309  const Scalar& m55)
310  {
311  Base::_check_template_params();
312 
313  (*this) << m00, m01, m02, m03, m04, m05, m10, m11, m12, m13, m14, m15, m20, m21, m22, m23, m24, m25, m30, m31, m32, m33, m34, m35, m40, m41, m42, m43, m44, m45,
314  m50, m51, m52, m53, m54, m55;
315  }
316 
317  void set(const Scalar& m00, const Scalar& m01, const Scalar& m02, const Scalar& m03, const Scalar& m04, const Scalar& m05, const Scalar& m10, const Scalar& m11,
318  const Scalar& m12, const Scalar& m13, const Scalar& m14, const Scalar& m15, const Scalar& m20, const Scalar& m21, const Scalar& m22, const Scalar& m23,
319  const Scalar& m24, const Scalar& m25, const Scalar& m30, const Scalar& m31, const Scalar& m32, const Scalar& m33, const Scalar& m34, const Scalar& m35,
320  const Scalar& m40, const Scalar& m41, const Scalar& m42, const Scalar& m43, const Scalar& m44, const Scalar& m45, const Scalar& m50, const Scalar& m51,
321  const Scalar& m52, const Scalar& m53, const Scalar& m54, const Scalar& m55)
322  {
323  Base::_check_template_params();
324 
325  (*this) << m00, m01, m02, m03, m04, m05, m10, m11, m12, m13, m14, m15, m20, m21, m22, m23, m24, m25, m30, m31, m32, m33, m34, m35, m40, m41, m42, m43, m44, m45,
326  m50, m51, m52, m53, m54, m55;
327  }
328 };
329 
338 {
342  SpatialTransform() : E(Matrix3d::Identity(3, 3)), r(Vector3d::Zero(3, 1))
343  {
344  }
345 
353  SpatialTransform(const Matrix3d& rotation, const double x, const double y, const double z) : E(rotation), r(x, y, z)
354  {
355  }
356 
362  SpatialTransform(const Matrix3d& rotation, const Vector3d& translation) : E(rotation), r(translation)
363  {
364  }
365 
371  explicit SpatialTransform(const Matrix3d& rotation) : E(rotation), r(0., 0., 0.)
372  {
373  }
374 
380  explicit SpatialTransform(const Vector3d& translation) : E(1., 0., 0., 0., 1., 0., 0., 0., 1.), r(translation)
381  {
382  }
383 
390  SpatialVector apply(const SpatialVector& v_sp) const
391  {
392  Vector3d v_rxw(v_sp[3] - r[1] * v_sp[2] + r[2] * v_sp[1], v_sp[4] - r[2] * v_sp[0] + r[0] * v_sp[2], v_sp[5] - r[0] * v_sp[1] + r[1] * v_sp[0]);
393 
394  return SpatialVector(E(0, 0) * v_sp[0] + E(0, 1) * v_sp[1] + E(0, 2) * v_sp[2], E(1, 0) * v_sp[0] + E(1, 1) * v_sp[1] + E(1, 2) * v_sp[2],
395  E(2, 0) * v_sp[0] + E(2, 1) * v_sp[1] + E(2, 2) * v_sp[2], E(0, 0) * v_rxw[0] + E(0, 1) * v_rxw[1] + E(0, 2) * v_rxw[2],
396  E(1, 0) * v_rxw[0] + E(1, 1) * v_rxw[1] + E(1, 2) * v_rxw[2], E(2, 0) * v_rxw[0] + E(2, 1) * v_rxw[1] + E(2, 2) * v_rxw[2]);
397  }
398 
406  {
407  Vector3d E_T_f(E(0, 0) * f_sp[3] + E(1, 0) * f_sp[4] + E(2, 0) * f_sp[5], E(0, 1) * f_sp[3] + E(1, 1) * f_sp[4] + E(2, 1) * f_sp[5],
408  E(0, 2) * f_sp[3] + E(1, 2) * f_sp[4] + E(2, 2) * f_sp[5]);
409 
410  return SpatialVector(E(0, 0) * f_sp[0] + E(1, 0) * f_sp[1] + E(2, 0) * f_sp[2] - r[2] * E_T_f[1] + r[1] * E_T_f[2],
411  E(0, 1) * f_sp[0] + E(1, 1) * f_sp[1] + E(2, 1) * f_sp[2] + r[2] * E_T_f[0] - r[0] * E_T_f[2],
412  E(0, 2) * f_sp[0] + E(1, 2) * f_sp[1] + E(2, 2) * f_sp[2] - r[1] * E_T_f[0] + r[0] * E_T_f[1], E_T_f[0], E_T_f[1], E_T_f[2]);
413  }
414 
422  {
423  Vector3d En_rxf = E * (Vector3d(f_sp[0], f_sp[1], f_sp[2]) - r.cross(Vector3d(f_sp[3], f_sp[4], f_sp[5])));
424 
425  return SpatialVector(En_rxf[0], En_rxf[1], En_rxf[2], E(0, 0) * f_sp[3] + E(0, 1) * f_sp[4] + E(0, 2) * f_sp[5],
426  E(1, 0) * f_sp[3] + E(1, 1) * f_sp[4] + E(1, 2) * f_sp[5], E(2, 0) * f_sp[3] + E(2, 1) * f_sp[4] + E(2, 2) * f_sp[5]);
427  }
428 
435  {
436  Matrix3d _Erx = E * Matrix3d(0., -r[2], r[1], r[2], 0., -r[0], -r[1], r[0], 0.);
437  SpatialMatrix result;
438 
439  result.block<3, 3>(0, 0) = E;
440  result.block<3, 3>(0, 3) = Matrix3d::Zero(3, 3);
441  result.block<3, 3>(3, 0) = -_Erx;
442  result.block<3, 3>(3, 3) = E;
443 
444  return result;
445  }
446 
453  {
454  Matrix3d _Erx = E * Matrix3d(0., -r[2], r[1], r[2], 0., -r[0], -r[1], r[0], 0.);
455  SpatialMatrix result;
456 
457  result.block<3, 3>(0, 0) = E;
458  result.block<3, 3>(0, 3) = -_Erx;
459  result.block<3, 3>(3, 0) = Matrix3d::Zero(3, 3);
460  result.block<3, 3>(3, 3) = E;
461 
462  return result;
463  }
464 
471  {
472  Matrix3d _Erx = E * Matrix3d(0., -r[2], r[1], r[2], 0., -r[0], -r[1], r[0], 0.);
473  SpatialMatrix result;
474 
475  result.block<3, 3>(0, 0) = E.transpose();
476  result.block<3, 3>(0, 3) = -_Erx.transpose();
477  result.block<3, 3>(3, 0) = Matrix3d::Zero(3, 3);
478  result.block<3, 3>(3, 3) = E.transpose();
479 
480  return result;
481  }
482 
489  {
490  return SpatialTransform(E.transpose(), -E * r);
491  }
492 
496  void invert()
497  {
498  r = -E * r;
499  E.transposeInPlace();
500  }
501 
508  {
509  return SpatialTransform(E * XT.E, XT.r + XT.E.transpose() * r);
510  }
511 
512  void operator*=(const SpatialTransform& XT)
513  {
514  r = XT.r + XT.E.transpose() * r;
515  E *= XT.E;
516  }
517 
520 };
521 
523 {
524  return X.E * (*this);
525 }
526 
528 {
529  *this = X.E * (*this);
530 }
531 } // namespace Math
532 } // namespace RobotDynamics
533 
538 
539 /* ___RDL_EIGENMATH_H__ */
540 #endif // ifndef __RDL_EIGENMATH_H__
SpatialTransform operator*(const SpatialTransform &XT) const
Overloaded * operator for combining transforms.
EIGEN_STRONG_INLINE Vector3d(const double &v0, const double &v1, const double &v2)
Definition: rdl_eigenmath.h:76
SpatialMatrix toMatrixTranspose() const
Returns spatial force transform transposed.
Eigen::Matrix< double, 6, 6 > Base
EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(RobotDynamics::Joint)
void setAngularPart(const Vector3d &v)
Vector4d & operator=(const Eigen::MatrixBase< OtherDerived > &other)
SpatialVector applyTranspose(const SpatialVector &f_sp) const
Applies .
void setLinearPart(const Vector3d &v)
EIGEN_STRONG_INLINE SpatialMatrix(const Scalar &m00, const Scalar &m01, const Scalar &m02, const Scalar &m03, const Scalar &m04, const Scalar &m05, const Scalar &m10, const Scalar &m11, const Scalar &m12, const Scalar &m13, const Scalar &m14, const Scalar &m15, const Scalar &m20, const Scalar &m21, const Scalar &m22, const Scalar &m23, const Scalar &m24, const Scalar &m25, const Scalar &m30, const Scalar &m31, const Scalar &m32, const Scalar &m33, const Scalar &m34, const Scalar &m35, const Scalar &m40, const Scalar &m41, const Scalar &m42, const Scalar &m43, const Scalar &m44, const Scalar &m45, const Scalar &m50, const Scalar &m51, const Scalar &m52, const Scalar &m53, const Scalar &m54, const Scalar &m55)
SpatialMatrix toMatrix() const
Return transform as 6x6 spatial matrix.
EIGEN_STRONG_INLINE SpatialMatrix()
EIGEN_STRONG_INLINE Matrix3d(const double &m00, const double &m01, const double &m02, const double &m10, const double &m11, const double &m12, const double &m20, const double &m21, const double &m22)
Vector3d transform_copy(const RobotDynamics::Math::SpatialTransform &X) const
EIGEN_STRONG_INLINE Matrix4d(const Scalar &m00, const Scalar &m01, const Scalar &m02, const Scalar &m03, const Scalar &m10, const Scalar &m11, const Scalar &m12, const Scalar &m13, const Scalar &m20, const Scalar &m21, const Scalar &m22, const Scalar &m23, const Scalar &m30, const Scalar &m31, const Scalar &m32, const Scalar &m33)
Matrix4d(const Eigen::MatrixBase< OtherDerived > &other)
Eigen::Matrix< double, 6, 1 > Base
Matrix3d & operator=(const Eigen::MatrixBase< OtherDerived > &other)
The TransformableGeometricObject class is an essential interface because it forces all geometric obje...
Definition: rdl_eigenmath.h:43
EIGEN_STRONG_INLINE Matrix4d()
void operator*=(const SpatialTransform &XT)
Compact representation of spatial transformations.
EIGEN_STRONG_INLINE SpatialVector(const double &v0, const double &v1, const double &v2, const double &v3, const double &v4, const double &v5)
SpatialVector apply(const SpatialVector &v_sp) const
Transform a spatial vector. Same as .
SpatialTransform inverse() const
Returns inverse of transform.
EIGEN_STRONG_INLINE Matrix3d()
SpatialVector & operator=(const Eigen::MatrixBase< OtherDerived > &other)
SpatialTransform(const Matrix3d &rotation, const Vector3d &translation)
Constructor.
void transform(const RobotDynamics::Math::SpatialTransform &X)
Pure virtual object. This object forces objects that inherit from it to have a method that tells how ...
Eigen::VectorXd VectorNd
Definition: rdl_eigenmath.h:25
EIGEN_STRONG_INLINE Vector4d(const double &v0, const double &v1, const double &v2, const double &v3)
SpatialMatrix toMatrixAdjoint() const
Returns Spatial transform that transforms spatial force vectors.
Vector3d & operator=(const Eigen::MatrixBase< OtherDerived > &other)
Definition: rdl_eigenmath.h:66
EIGEN_STRONG_INLINE Vector3d getAngularPart() const
SpatialMatrix(const Eigen::MatrixBase< OtherDerived > &other)
Vector4d(const Eigen::MatrixBase< OtherDerived > &other)
Eigen::MatrixXd MatrixNd
Definition: rdl_eigenmath.h:26
Eigen::Matrix< double, 6, 3 > Matrix63
Definition: rdl_eigenmath.h:24
void invert()
Inverts in place. .
EIGEN_STRONG_INLINE SpatialVector()
SpatialTransform(const Matrix3d &rotation)
Constructor.
EIGEN_STRONG_INLINE Vector4d()
SpatialVector applyAdjoint(const SpatialVector &f_sp) const
Applies where is a spatial force.
Matrix3d(const Eigen::MatrixBase< OtherDerived > &other)
EIGEN_STRONG_INLINE Vector3d getLinearPart() const
SpatialTransform(const Vector3d &translation)
Constructor.
SpatialVector(const Eigen::MatrixBase< OtherDerived > &other)
EIGEN_STRONG_INLINE Vector3d()
Definition: rdl_eigenmath.h:72
SpatialMatrix & operator=(const Eigen::MatrixBase< OtherDerived > &other)
Matrix4d & operator=(const Eigen::MatrixBase< OtherDerived > &other)
Eigen::AngleAxisd AxisAngle
Definition: rdl_eigenmath.h:27
SpatialTransform(const Matrix3d &rotation, const double x, const double y, const double z)
Constructor.
Namespace for all structures of the RobotDynamics library.
Definition: Body.h:21
Vector3d(const Eigen::MatrixBase< OtherDerived > &other)
Definition: rdl_eigenmath.h:61
Eigen::Matrix< double, 4, 4 > Base


rdl_dynamics
Author(s):
autogenerated on Tue Apr 20 2021 02:25:27