Cheetah Software  1.0
SpatialInertia.h
Go to the documentation of this file.
1 
6 #ifndef LIBBIOMIMETICS_SPATIALINERTIA_H
7 #define LIBBIOMIMETICS_SPATIALINERTIA_H
8 
10 #include "spatial.h"
11 
12 #include <cmath>
13 #include <iostream>
14 #include <type_traits>
15 
16 #include <eigen3/Eigen/Dense>
17 
18 using namespace ori;
19 using namespace spatial;
20 
24 template <typename T>
26  public:
27  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
32  SpatialInertia(T mass, const Vec3<T>& com, const Mat3<T>& inertia) {
33  Mat3<T> cSkew = vectorToSkewMat(com);
34  _inertia.template topLeftCorner<3, 3>() =
35  inertia + mass * cSkew * cSkew.transpose();
36  _inertia.template topRightCorner<3, 3>() = mass * cSkew;
37  _inertia.template bottomLeftCorner<3, 3>() = mass * cSkew.transpose();
38  _inertia.template bottomRightCorner<3, 3>() = mass * Mat3<T>::Identity();
39  }
40 
44  explicit SpatialInertia(const Mat6<T>& inertia) { _inertia = inertia; }
45 
49  SpatialInertia() { _inertia = Mat6<T>::Zero(); }
50 
54  explicit SpatialInertia(const MassProperties<T>& a) {
55  _inertia(0, 0) = a(4);
56  _inertia(0, 1) = a(9);
57  _inertia(0, 2) = a(8);
58  _inertia(1, 0) = a(9);
59  _inertia(1, 1) = a(5);
60  _inertia(1, 2) = a(7);
61  _inertia(2, 0) = a(8);
62  _inertia(2, 1) = a(7);
63  _inertia(2, 2) = a(6);
64  Mat3<T> cSkew = vectorToSkewMat(Vec3<T>(a(1), a(2), a(3)));
65  _inertia.template topRightCorner<3, 3>() = cSkew;
66  _inertia.template bottomLeftCorner<3, 3>() = cSkew.transpose();
67  _inertia.template bottomRightCorner<3, 3>() = a(0) * Mat3<T>::Identity();
68  }
69 
77  explicit SpatialInertia(const Mat4<T>& P) {
78  Mat6<T> I;
79  T m = P(3, 3);
80  Vec3<T> h = P.template topRightCorner<3, 1>();
81  Mat3<T> E = P.template topLeftCorner<3, 3>();
82  Mat3<T> Ibar = E.trace() * Mat3<T>::Identity() - E;
83  I.template topLeftCorner<3, 3>() = Ibar;
84  I.template topRightCorner<3, 3>() = vectorToSkewMat(h);
85  I.template bottomLeftCorner<3, 3>() = vectorToSkewMat(h).transpose();
86  I.template bottomRightCorner<3, 3>() = m * Mat3<T>::Identity();
87  _inertia = I;
88  }
89 
95  Vec3<T> h = matToSkewVec(_inertia.template topRightCorner<3, 3>());
96  a << _inertia(5, 5), h(0), h(1), h(2), _inertia(0, 0), _inertia(1, 1),
97  _inertia(2, 2), _inertia(2, 1), _inertia(2, 0), _inertia(1, 0);
98  return a;
99  }
100 
104  const Mat6<T>& getMatrix() const { return _inertia; }
105 
106  void setMatrix(const Mat6<T>& mat) { _inertia = mat; }
107 
108  void addMatrix(const Mat6<T>& mat) { _inertia += mat; }
109 
113  T getMass() { return _inertia(5, 5); }
114 
119  T m = getMass();
120  Mat3<T> mcSkew = _inertia.template topRightCorner<3, 3>();
121  Vec3<T> com = matToSkewVec(mcSkew) / m;
122  return com;
123  }
124 
129  T m = getMass();
130  Mat3<T> mcSkew = _inertia.template topRightCorner<3, 3>();
131  Mat3<T> I_rot = _inertia.template topLeftCorner<3, 3>() -
132  mcSkew * mcSkew.transpose() / m;
133  return I_rot;
134  }
135 
143  Vec3<T> h = matToSkewVec(_inertia.template topRightCorner<3, 3>());
144  Mat3<T> Ibar = _inertia.template topLeftCorner<3, 3>();
145  T m = _inertia(5, 5);
146  Mat4<T> P;
147  P.template topLeftCorner<3, 3>() =
148  0.5 * Ibar.trace() * Mat3<T>::Identity() - Ibar;
149  P.template topRightCorner<3, 1>() = h;
150  P.template bottomLeftCorner<1, 3>() = h.transpose();
151  P(3, 3) = m;
152  return P;
153  }
154 
159  Mat4<T> P = getPseudoInertia();
161  if (axis == CoordinateAxis::X)
162  X(0, 0) = -1;
163  else if (axis == CoordinateAxis::Y)
164  X(1, 1) = -1;
165  else if (axis == CoordinateAxis::Z)
166  X(2, 2) = -1;
167  P = X * P * X;
168  return SpatialInertia(P);
169  }
170 
171  private:
173 };
174 
175 #endif // LIBBIOMIMETICS_SPATIALINERTIA_H
typename Eigen::Matrix< T, 6, 6 > Mat6
Definition: cppTypes.h:70
typename Eigen::Matrix< T, 3, 3 > Mat3
Definition: cppTypes.h:54
const Mat6< T > & getMatrix() const
SpatialInertia(const Mat4< T > &P)
typename Eigen::Matrix< T, 4, 4 > Mat4
Definition: cppTypes.h:94
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
Mat3< T > getInertiaTensor()
SpatialInertia flipAlongAxis(CoordinateAxis axis)
MassProperties< T > asMassPropertyVector()
void addMatrix(const Mat6< T > &mat)
Mat4< T > getPseudoInertia()
Mat6< T > _inertia
Vec3< T > getCOM()
EIGEN_MAKE_ALIGNED_OPERATOR_NEW SpatialInertia(T mass, const Vec3< T > &com, const Mat3< T > &inertia)
SpatialInertia(const MassProperties< T > &a)
Mat3< typename T::Scalar > vectorToSkewMat(const Eigen::MatrixBase< T > &v)
Utility functions for 3D rotations.
Vec3< typename T::Scalar > matToSkewVec(const Eigen::MatrixBase< T > &m)
void setMatrix(const Mat6< T > &mat)
typename Eigen::Matrix< T, 10, 1 > MassProperties
Definition: cppTypes.h:98
SpatialInertia(const Mat6< T > &inertia)
Utility functions for manipulating spatial quantities.