- 3.0.2 rigid body dynamics module.
utils.h
Go to the documentation of this file.
1 /* CPYHDR { */
2 /*
3  * This file is part of the 'iit-rbd' library.
4  * Copyright © 2015 2016, Marco Frigerio (marco.frigerio@iit.it)
5  *
6  * See the LICENSE file for more information.
7  */
8 /* } CPYHDR */
9 #ifndef IIT_RBD_UTILS_H_
10 #define IIT_RBD_UTILS_H_
11 
12 #include <cmath>
13 #include <ctime>
14 #include <iostream>
15 
16 #include "rbd.h"
17 #include "types.h"
18 
19 namespace iit {
20 namespace rbd {
21 
22 class Utils {
23  public:
24  struct InertiaMoments {
25  double ixx; double iyy; double izz; double ixy; double ixz; double iyz;
26  };
27 
33  template <typename Scalar>
35  CwiseAlmostZeroOp(const Scalar& threshold) : thresh(threshold) {}
36  const Scalar operator()(const Scalar& x) const {
37  return std::abs(x) < thresh ? 0 : x;
38  }
39  private:
40  Scalar thresh;
41  };
42 public:
43  template <typename Scalar>
45  Scalar Ixx, Scalar Iyy, Scalar Izz,
46  Scalar Ixy, Scalar Ixz, Scalar Iyz)
47  {
48  Mat33<Scalar> I;
49  I << Ixx, -Ixy, -Ixz,
50  -Ixy, Iyy, -Iyz,
51  -Ixz, -Iyz, Izz;
52  return I;
53  }
54 
56  Matrix33d out;
57  out << 0 , -in(2), in(1),
58  in(2), 0 , -in(0),
59  -in(1), in(0), 0;
60  return out;
61  }
62 
63  // Put implementation here in header even if it is not inline, since
64  // this is a template function
69  template <typename Derived>
70  static void fillAsRotationMatrix(double rx, double ry, double rz,
71  const MatrixBase<Derived>& mx)
72  {
73  eigen_assert(mx.rows() == 3 && mx.cols() == 3);
74 
75  static double cx;
76  static double sx;
77  static double cy;
78  static double sy;
79  static double cz;
80  static double sz;
81  cx = std::cos(rx);
82  sx = std::sin(rx);
83  cy = std::cos(ry);
84  sy = std::sin(ry);
85  cz = std::cos(rz);
86  sz = std::sin(rz);
87 
88  const_cast<MatrixBase<Derived>&>(mx)
89  << cy*cz, cx*sz+sx*sy*cz, sx*sz-cx*sy*cz,
90  -cy*sz, cx*cz-sx*sy*sz, cx*sy*sz+sx*cz,
91  sy, -sx*cy, cx*cy;
92  }
93 
98  static Matrix33d buildRotationMatrix(double rx, double ry, double rz) {
99  double cx = std::cos(rx);
100  double sx = std::sin(rx);
101  double cy = std::cos(ry);
102  double sy = std::sin(ry);
103  double cz = std::cos(rz);
104  double sz = std::sin(rz);
105  Matrix33d ret;
106  ret << cy*cz, cx*sz+sx*sy*cz, sx*sz-cx*sy*cz,
107  -cy*sz, cx*cz-sx*sy*sz, cx*sy*sz+sx*cz,
108  sy, -sx*cy, cx*cy;
109  return ret;
110  }
111 
112 #define block31 template block<3,1>
113 #define block33 template block<3,3>
114 
115  template <typename Scalar>
117  fillAsCrossProductMatrix(v.block31(0,0), mx.block33(0,0));
118  mx.block33(0,3).setZero();
119  fillAsCrossProductMatrix(v.block31(3,0), mx.block33(3,0));
120  mx.block33(3,3) = mx.block33(0,0);
121  }
122  template <typename Scalar>
124  fillAsCrossProductMatrix(v.block31(0,0), mx.block33(0,0));
125  fillAsCrossProductMatrix(v.block31(3,0), mx.block33(0,3));
126  mx.block33(3,0).setZero();
127  mx.block33(3,3) = mx.block33(0,0);
128  }
129 
130  template <typename D1, typename D2>
131  static void fillAsCrossProductMatrix(const MatrixBase<D2>& in, const MatrixBase<D1>& mx)
132  {
133  eigen_assert(mx.rows() == 3 && mx.cols() == 3);
134  typedef typename D2::Scalar Scalar;
135 
136  const_cast< MatrixBase<D1>& >(mx)
137  << static_cast<Scalar>(0.0) , -in(2), in(1),
138  in(2), static_cast<Scalar>(0.0) , -in(0),
139  -in(1), in(0), static_cast<Scalar>(0.0);
140  }
141 
142  template <typename Derived>
144  eigen_assert(homT.rows() == 4 && homT.cols() == 4); // weak way to check if it is a homogeneous transform
145  return homT.block31(0,3);
146  }
147  template <typename Derived>
149  eigen_assert(homT.rows() == 4 && homT.cols() == 4); // weak way to check if it is a homogeneous transform
150  return homT.block33(0,0);
151  }
152  template <typename Derived>
154  eigen_assert(homT.rows() == 4 && homT.cols() == 4); // weak way to check if it is a homogeneous transform
155  return homT.block31(0,2);
156  }
157 
158 #undef block31
159 #undef block33
160 
173  template <typename Derived, typename Other>
175  const MatrixBase<Derived>& homT,
176  const MatrixBase<Other>& vect3d)
177  {
178  eigen_assert(homT.rows() == 4 && homT.cols() == 4); // weak way to check if it is a homogeneous transform
179  eigen_assert(vect3d.rows() == 3 && vect3d.cols() == 1); // check if it is a 3d column
180  return rotationMx(homT) * vect3d + positionVector(homT);
181  }
182 
183  static void randomVec(Vector6D& vec)
184  {
185  std::srand(std::time(NULL));
186  for(int i=0; i<6; i++) {
187  vec(i) = ((double)std::rand()) / RAND_MAX;
188  }
189  }
190 
192  {
193  angularPart(g).setZero();
194  std::srand(std::time(NULL));
195  g(LX) = ((double)std::rand()) / RAND_MAX;
196  g(LY) = ((double)std::rand()) / RAND_MAX;
197  g(LZ) = ((double)std::rand()) / RAND_MAX;
198  linearPart(g) = (linearPart(g) / linearPart(g).norm()) * iit::rbd::g;
199  }
200 
201  template <class RT>
202  static void cmdlineargs_spatialv(int argc, char** argv,
203  iit::rbd::Vector6D& vec)
204  {
205  if(argc < 6) {
206  std::cerr << "Not enough arguments" << std::endl;
207  exit(-1);
208  }
209  for(int i=0; i<6; i++) {
210  vec(i) = std::atof(argv[i]);
211  }
212  }
213 
214 };
215 
216 
217 }
218 }
219 
220 #endif /* IIT_RBD_UTILS_H_ */
static Mat33< Scalar > buildInertiaTensor(Scalar Ixx, Scalar Iyy, Scalar Izz, Scalar Ixy, Scalar Ixz, Scalar Iyz)
Definition: utils.h:44
double ixx
Definition: utils.h:25
static const MatrixBlock< const Derived, 3, 1 > zAxis(const MatrixBase< Derived > &homT)
Definition: utils.h:153
static void randomGravity(VelocityVector &g)
Definition: utils.h:191
double ixz
Definition: utils.h:25
Cored::Vector3 Vector3d
Definition: rbd.h:137
CwiseAlmostZeroOp(const Scalar &threshold)
Definition: utils.h:35
Definition: rbd.h:175
static void randomVec(Vector6D &vec)
Definition: utils.h:183
static void fillAsCrossProductMatrix(const MatrixBase< D2 > &in, const MatrixBase< D1 > &mx)
Definition: utils.h:131
static const MatrixBlock< const Derived, 3, 1 > positionVector(const MatrixBase< Derived > &homT)
Definition: utils.h:143
static void fillAsForceCrossProductMx(const Vec6< Scalar > &v, Mat66< Scalar > &mx)
Definition: utils.h:123
ct::core::ADCodegenLinearizer< state_dim, control_dim >::ADCGScalar Scalar
typename Core< S >::Matrix33 Mat33
Definition: types.h:23
static const MatrixBlock< const Derived, 3, 3 > rotationMx(const MatrixBase< Derived > &homT)
Definition: utils.h:148
static void fillAsRotationMatrix(double rx, double ry, double rz, const MatrixBase< Derived > &mx)
Definition: utils.h:70
Definition: rbd.h:175
Eigen::Block< XprType, R, C > MatrixBlock
Definition: rbd.h:57
Part3D angularPart(Vector6D &f)
Definition: rbd.h:154
for i
double ixy
Definition: utils.h:25
typename Core< S >::Vector6 Vec6
Definition: types.h:26
typename Core< S >::Matrix66 Mat66
Definition: types.h:24
static void fillAsMotionCrossProductMx(const Vec6< Scalar > &v, Mat66< Scalar > &mx)
Definition: utils.h:116
Cored::VelocityVector VelocityVector
Definition: rbd.h:142
Eigen::MatrixBase< Derived > MatrixBase
Definition: rbd.h:51
Cored::Matrix33 Matrix33d
Definition: rbd.h:136
static Vector3d transform(const MatrixBase< Derived > &homT, const MatrixBase< Other > &vect3d)
Definition: utils.h:174
Definition: utils.h:22
const Scalar operator()(const Scalar &x) const
Definition: utils.h:36
Definition: utils.h:24
Cored::Vector6D Vector6D
Definition: rbd.h:141
double iyz
Definition: utils.h:25
Part3D linearPart(Vector6D &f)
Definition: rbd.h:157
Definition: rbd.h:175
static Matrix33d buildCrossProductMatrix(const Vector3d &in)
Definition: utils.h:55
static void cmdlineargs_spatialv(int argc, char **argv, iit::rbd::Vector6D &vec)
Definition: utils.h:202
static Matrix33d buildRotationMatrix(double rx, double ry, double rz)
Definition: utils.h:98
double izz
Definition: utils.h:25
double iyy
Definition: utils.h:25