pinocchio  2.1.3
joint-translation.hpp
1 //
2 // Copyright (c) 2015-2018 CNRS
3 // Copyright (c) 2015-2016 Wandercraft, 86 rue de Paris 91400 Orsay, France.
4 //
5 
6 #ifndef __pinocchio_joint_translation_hpp__
7 #define __pinocchio_joint_translation_hpp__
8 
9 #include "pinocchio/macros.hpp"
10 #include "pinocchio/multibody/joint/joint-base.hpp"
11 #include "pinocchio/multibody/constraint.hpp"
12 #include "pinocchio/spatial/inertia.hpp"
13 #include "pinocchio/spatial/skew.hpp"
14 
15 namespace pinocchio
16 {
17 
18  template<typename Scalar, int Options=0> struct MotionTranslationTpl;
20 
21  namespace internal
22  {
23  template<typename Scalar, int Options>
24  struct SE3GroupAction< MotionTranslationTpl<Scalar,Options> >
25  {
26  typedef MotionTpl<Scalar,Options> ReturnType;
27  };
28 
29  template<typename Scalar, int Options, typename MotionDerived>
30  struct MotionAlgebraAction< MotionTranslationTpl<Scalar,Options>, MotionDerived>
31  {
32  typedef MotionTpl<Scalar,Options> ReturnType;
33  };
34  }
35 
36  template<typename _Scalar, int _Options>
37  struct traits< MotionTranslationTpl<_Scalar,_Options> >
38  {
39  typedef _Scalar Scalar;
40  enum { Options = _Options };
41  typedef Eigen::Matrix<Scalar,3,1,Options> Vector3;
42  typedef Eigen::Matrix<Scalar,6,1,Options> Vector6;
43  typedef Eigen::Matrix<Scalar,6,6,Options> Matrix6;
44  typedef typename PINOCCHIO_EIGEN_REF_CONST_TYPE(Vector6) ToVectorConstReturnType;
45  typedef typename PINOCCHIO_EIGEN_REF_TYPE(Vector6) ToVectorReturnType;
46  typedef Vector3 AngularType;
47  typedef Vector3 LinearType;
48  typedef const Vector3 ConstAngularType;
49  typedef const Vector3 ConstLinearType;
50  typedef Matrix6 ActionMatrixType;
52  enum {
53  LINEAR = 0,
54  ANGULAR = 3
55  };
56  }; // traits MotionTranslationTpl
57 
58  template<typename _Scalar, int _Options>
59  struct MotionTranslationTpl : MotionBase< MotionTranslationTpl<_Scalar,_Options> >
60  {
61  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
62 
63  MOTION_TYPEDEF_TPL(MotionTranslationTpl);
64 
66 
67  template<typename Vector3Like>
68  MotionTranslationTpl(const Eigen::MatrixBase<Vector3Like> & v)
69  : rate(v)
70  {}
71 
73  : rate(other.rate)
74  {}
75 
76  Vector3 & operator()() { return rate; }
77  const Vector3 & operator()() const { return rate; }
78 
79 // operator MotionPlain() const
80 // {
81 // return MotionPlain(rate,MotionPlain::Vector3::Zero());
82 // }
83 
84  MotionTranslationTpl & operator=(const MotionTranslationTpl & other)
85  {
86  rate = other.rate;
87  return *this;
88  }
89 
90  template<typename Derived>
91  void addTo(MotionDense<Derived> & other) const
92  {
93  other.linear() += rate;
94  }
95 
96  template<typename Derived>
97  void setTo(MotionDense<Derived> & other) const
98  {
99  other.linear() = rate;
100  other.angular().setZero();
101  }
102 
103  template<typename S2, int O2, typename D2>
104  void se3Action_impl(const SE3Tpl<S2,O2> & m, MotionDense<D2> & v) const
105  {
106  v.angular().setZero();
107  v.linear().noalias() = m.rotation() * rate; // TODO: check efficiency
108  }
109 
110  template<typename S2, int O2>
111  MotionPlain se3Action_impl(const SE3Tpl<S2,O2> & m) const
112  {
113  MotionPlain res;
114  se3Action_impl(m,res);
115  return res;
116  }
117 
118  template<typename S2, int O2, typename D2>
119  void se3ActionInverse_impl(const SE3Tpl<S2,O2> & m, MotionDense<D2> & v) const
120  {
121  // Linear
122  v.linear().noalias() = m.rotation().transpose() * rate;
123 
124  // Angular
125  v.angular().setZero();
126  }
127 
128  template<typename S2, int O2>
129  MotionPlain se3ActionInverse_impl(const SE3Tpl<S2,O2> & m) const
130  {
131  MotionPlain res;
132  se3ActionInverse_impl(m,res);
133  return res;
134  }
135 
136  template<typename M1, typename M2>
137  void motionAction(const MotionDense<M1> & v, MotionDense<M2> & mout) const
138  {
139  // Linear
140  mout.linear().noalias() = v.angular().cross(rate);
141 
142  // Angular
143  mout.angular().setZero();
144  }
145 
146  template<typename M1>
147  MotionPlain motionAction(const MotionDense<M1> & v) const
148  {
149  MotionPlain res;
150  motionAction(v,res);
151  return res;
152  }
153 
154  // data
155  Vector3 rate;
156 
157  }; // struct MotionTranslationTpl
158 
159  template<typename S1, int O1, typename MotionDerived>
160  inline typename MotionDerived::MotionPlain
161  operator+(const MotionTranslationTpl<S1,O1> & m1,
162  const MotionDense<MotionDerived> & m2)
163  {
164  return typename MotionDerived::MotionPlain(m2.linear() + m1.rate, m2.angular());
165  }
166 
167  template<typename Scalar, int Options> struct TransformTranslationTpl;
168 
169  template<typename _Scalar, int _Options>
170  struct traits< TransformTranslationTpl<_Scalar,_Options> >
171  {
172  enum {
173  Options = _Options,
174  LINEAR = 0,
175  ANGULAR = 3
176  };
177  typedef _Scalar Scalar;
179  typedef Eigen::Matrix<Scalar,3,1,Options> Vector3;
180  typedef Eigen::Matrix<Scalar,3,3,Options> Matrix3;
181  typedef typename Matrix3::IdentityReturnType AngularType;
182  typedef AngularType AngularRef;
183  typedef AngularType ConstAngularRef;
184  typedef Vector3 LinearType;
185  typedef LinearType & LinearRef;
186  typedef const LinearType & ConstLinearRef;
187  typedef typename traits<PlainType>::ActionMatrixType ActionMatrixType;
188  typedef typename traits<PlainType>::HomogeneousMatrixType HomogeneousMatrixType;
189  }; // traits TransformTranslationTpl
190 
191  namespace internal
192  {
193  template<typename Scalar, int Options>
194  struct SE3GroupAction< TransformTranslationTpl<Scalar,Options> >
195  { typedef typename traits <TransformTranslationTpl<Scalar,Options> >::PlainType ReturnType; };
196  }
197 
198  template<typename _Scalar, int _Options>
200  : SE3Base< TransformTranslationTpl<_Scalar,_Options> >
201  {
202  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
203  PINOCCHIO_SE3_TYPEDEF_TPL(TransformTranslationTpl);
205 
207 
208  template<typename Vector3Like>
209  TransformTranslationTpl(const Eigen::MatrixBase<Vector3Like> & translation)
210  : m_translation(translation)
211  {}
212 
213  PlainType plain() const
214  {
215  PlainType res(PlainType::Identity());
216  res.rotation().setIdentity();
217  res.translation() = translation();
218 
219  return res;
220  }
221 
222  operator PlainType() const { return plain(); }
223 
224  template<typename S2, int O2>
226  se3action(const SE3Tpl<S2,O2> & m) const
227  {
229  ReturnType res(m);
230  res.translation() += translation();
231 
232  return res;
233  }
234 
235  ConstLinearRef translation() const { return m_translation; }
236  LinearRef translation() { return m_translation; }
237 
238  AngularType rotation() const { return AngularType(3,3); }
239 
240  protected:
241 
242  LinearType m_translation;
243  };
244 
245  template<typename Scalar, int Options> struct ConstraintTranslationTpl;
246 
247  template<typename _Scalar, int _Options>
248  struct traits< ConstraintTranslationTpl<_Scalar,_Options> >
249  {
250  typedef _Scalar Scalar;
251  enum { Options = _Options };
252  typedef Eigen::Matrix<Scalar,3,1,Options> Vector3;
253  typedef Eigen::Matrix<Scalar,4,1,Options> Vector4;
254  typedef Eigen::Matrix<Scalar,6,1,Options> Vector6;
255  typedef Eigen::Matrix<Scalar,3,3,Options> Matrix3;
256  typedef Eigen::Matrix<Scalar,4,4,Options> Matrix4;
257  typedef Eigen::Matrix<Scalar,6,6,Options> Matrix6;
258  typedef Matrix3 Angular_t;
259  typedef Vector3 Linear_t;
260  typedef const Matrix3 ConstAngular_t;
261  typedef const Vector3 ConstLinear_t;
262  typedef Matrix6 ActionMatrix_t;
263  typedef Eigen::Quaternion<Scalar,Options> Quaternion_t;
264  typedef SE3Tpl<Scalar,Options> SE3;
268  enum {
269  LINEAR = 0,
270  ANGULAR = 3
271  };
273  typedef Eigen::Matrix<Scalar,3,1,Options> JointForce;
274  typedef Eigen::Matrix<Scalar,6,3,Options> DenseBase;
275 
276  typedef DenseBase MatrixReturnType;
277  typedef const DenseBase ConstMatrixReturnType;
278  }; // traits ConstraintTranslationTpl
279 
280  template<typename _Scalar, int _Options>
282  : ConstraintBase< ConstraintTranslationTpl<_Scalar,_Options> >
283  {
284  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
285 
286  SPATIAL_TYPEDEF_TEMPLATE(ConstraintTranslationTpl);
287 
288  enum { NV = 3, Options = _Options };
290  typedef typename traits<ConstraintTranslationTpl>::JointForce JointForce;
291  typedef typename traits<ConstraintTranslationTpl>::DenseBase DenseBase;
292 
294 
295 // template<typename S1, int O1>
296 // Motion operator*(const MotionTranslationTpl<S1,O1> & vj) const
297 // { return Motion(vj(), Motion::Vector3::Zero()); }
298 
299  template<typename Vector3Like>
300  JointMotion __mult__(const Eigen::MatrixBase<Vector3Like> & v) const
301  {
302  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Vector3Like,3);
303  return JointMotion(v);
304  }
305 
306  int nv_impl() const { return NV; }
307 
309  {
310  const ConstraintTranslationTpl & ref;
311  ConstraintTranspose(const ConstraintTranslationTpl & ref) : ref(ref) {}
312 
313  template<typename Derived>
315  operator* (const ForceDense<Derived> & phi)
316  {
317  return phi.linear();
318  }
319 
320  /* [CRBA] MatrixBase operator* (Constraint::Transpose S, ForceSet::Block) */
321  template<typename MatrixDerived>
322  const typename SizeDepType<3>::RowsReturn<MatrixDerived>::ConstType
323  operator*(const Eigen::MatrixBase<MatrixDerived> & F) const
324  {
325  assert(F.rows()==6);
326  return F.derived().template middleRows<3>(LINEAR);
327  }
328 
329  }; // struct ConstraintTranspose
330 
331  ConstraintTranspose transpose () const { return ConstraintTranspose(*this); }
332 
333  DenseBase matrix_impl() const
334  {
335  DenseBase S;
336  S.template middleRows<3>(LINEAR).setIdentity();
337  S.template middleRows<3>(ANGULAR).setZero();
338  return S;
339  }
340 
341  template<typename S1, int O1>
342  Eigen::Matrix<S1,6,3,O1> se3Action(const SE3Tpl<S1,O1> & m) const
343  {
344  Eigen::Matrix<S1,6,3,O1> M;
345  M.template middleRows<3>(LINEAR) = m.rotation ();
346  M.template middleRows<3>(ANGULAR).setZero ();
347 
348  return M;
349  }
350 
351  template<typename MotionDerived>
352  DenseBase motionAction(const MotionDense<MotionDerived> & m) const
353  {
354  const typename MotionDerived::ConstAngularType w = m.angular();
355 
356  DenseBase res;
357  skew(w,res.template middleRows<3>(LINEAR));
358  res.template middleRows<3>(ANGULAR).setZero();
359 
360  return res;
361  }
362 
363  }; // struct ConstraintTranslationTpl
364 
365  template<typename MotionDerived, typename S2, int O2>
366  inline typename MotionDerived::MotionPlain
367  operator^(const MotionDense<MotionDerived> & m1,
368  const MotionTranslationTpl<S2,O2> & m2)
369  {
370  return m2.motionAction(m1);
371  }
372 
373  /* [CRBA] ForceSet operator* (Inertia Y,Constraint S) */
374  template<typename S1, int O1, typename S2, int O2>
375  inline Eigen::Matrix<S2,6,3,O2>
376  operator*(const InertiaTpl<S1,O1> & Y,
378  {
379  typedef ConstraintTranslationTpl<S2,O2> Constraint;
380  Eigen::Matrix<S2,6,3,O2> M;
381  alphaSkew(Y.mass(),Y.lever(),M.template middleRows<3>(Constraint::ANGULAR));
382  M.template middleRows<3>(Constraint::LINEAR).setZero();
383  M.template middleRows<3>(Constraint::LINEAR).diagonal().fill(Y.mass ());
384 
385  return M;
386  }
387 
388  /* [ABA] Y*S operator*/
389  template<typename M6Like, typename S2, int O2>
390  inline const typename SizeDepType<3>::ColsReturn<M6Like>::ConstType
391  operator*(const Eigen::MatrixBase<M6Like> & Y,
393  {
394  typedef ConstraintTranslationTpl<S2,O2> Constraint;
395  return Y.derived().template middleCols<3>(Constraint::LINEAR);
396  }
397 
398  namespace internal
399  {
400  template<typename S1, int O1>
401  struct SE3GroupAction< ConstraintTranslationTpl<S1,O1> >
402  { typedef Eigen::Matrix<S1,6,3,O1> ReturnType; };
403 
404  template<typename S1, int O1, typename MotionDerived>
405  struct MotionAlgebraAction< ConstraintTranslationTpl<S1,O1>,MotionDerived >
406  { typedef Eigen::Matrix<S1,6,3,O1> ReturnType; };
407  }
408 
409 
410  template<typename Scalar, int Options> struct JointTranslationTpl;
411 
412  template<typename _Scalar, int _Options>
413  struct traits< JointTranslationTpl<_Scalar,_Options> >
414  {
415  enum {
416  NQ = 3,
417  NV = 3
418  };
419  typedef _Scalar Scalar;
420  enum { Options = _Options };
427  typedef Eigen::Matrix<Scalar,6,NV,Options> F_t;
428 
429  // [ABA]
430  typedef Eigen::Matrix<Scalar,6,NV,Options> U_t;
431  typedef Eigen::Matrix<Scalar,NV,NV,Options> D_t;
432  typedef Eigen::Matrix<Scalar,6,NV,Options> UD_t;
433 
434  PINOCCHIO_JOINT_DATA_BASE_ACCESSOR_DEFAULT_RETURN_TYPE
435 
436  typedef Eigen::Matrix<Scalar,NQ,1,Options> ConfigVector_t;
437  typedef Eigen::Matrix<Scalar,NV,1,Options> TangentVector_t;
438  }; // traits JointTranslationTpl
439 
440  template<typename Scalar, int Options>
441  struct traits< JointDataTranslationTpl<Scalar,Options> >
443 
444  template<typename Scalar, int Options>
445  struct traits< JointModelTranslationTpl<Scalar,Options> >
447 
448  template<typename _Scalar, int _Options>
450  : public JointDataBase< JointDataTranslationTpl<_Scalar,_Options> >
451  {
452  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
453 
455  PINOCCHIO_JOINT_DATA_TYPEDEF_TEMPLATE;
456  PINOCCHIO_JOINT_DATA_BASE_DEFAULT_ACCESSOR
457 
458  Constraint_t S;
459  Transformation_t M;
460  Motion_t v;
461  Bias_t c;
462 
463  F_t F; // TODO if not used anymore, clean F_t
464 
465  // [ABA] specific data
466  U_t U;
467  D_t Dinv;
468  UD_t UDinv;
469 
471 
472  static std::string classname() { return std::string("JointDataTranslation"); }
473  std::string shortname() const { return classname(); }
474  }; // struct JointDataTranslationTpl
475 
476  PINOCCHIO_JOINT_CAST_TYPE_SPECIALIZATION(JointModelTranslationTpl);
477  template<typename _Scalar, int _Options>
479  : public JointModelBase< JointModelTranslationTpl<_Scalar,_Options> >
480  {
481  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
482 
484  PINOCCHIO_JOINT_TYPEDEF_TEMPLATE;
485 
487  using Base::id;
488  using Base::idx_q;
489  using Base::idx_v;
490  using Base::setIndexes;
491 
492  JointDataDerived createData() const { return JointDataDerived(); }
493 
494  template<typename ConfigVector>
495  void calc(JointDataDerived & data,
496  const typename Eigen::MatrixBase<ConfigVector> & qs) const
497  {
498  data.M.translation() = this->jointConfigSelector(qs);
499  }
500 
501  template<typename ConfigVector, typename TangentVector>
502  void calc(JointDataDerived & data,
503  const typename Eigen::MatrixBase<ConfigVector> & qs,
504  const typename Eigen::MatrixBase<TangentVector> & vs) const
505  {
506  calc(data,qs.derived());
507 
508  data.v() = this->jointVelocitySelector(vs);
509  }
510 
511  template<typename Matrix6Like>
512  void calc_aba(JointDataDerived & data, const Eigen::MatrixBase<Matrix6Like> & I, const bool update_I) const
513  {
514  data.U = I.template middleCols<3>(Inertia::LINEAR);
515 
516  // compute inverse
517  data.Dinv.setIdentity();
518  data.U.template middleRows<3>(Inertia::LINEAR).llt().solveInPlace(data.Dinv);
519 
520  data.UDinv.template middleRows<3>(Inertia::LINEAR).setIdentity(); // can be put in data constructor
521  data.UDinv.template middleRows<3>(Inertia::ANGULAR).noalias() = data.U.template middleRows<3>(Inertia::ANGULAR) * data.Dinv;
522 
523  if (update_I)
524  {
525  Matrix6Like & I_ = PINOCCHIO_EIGEN_CONST_CAST(Matrix6Like,I);
526  I_.template block<3,3>(Inertia::ANGULAR,Inertia::ANGULAR)
527  -= data.UDinv.template middleRows<3>(Inertia::ANGULAR) * I_.template block<3,3>(Inertia::LINEAR, Inertia::ANGULAR);
528  I_.template middleCols<3>(Inertia::LINEAR).setZero();
529  I_.template block<3,3>(Inertia::LINEAR,Inertia::ANGULAR).setZero();
530  }
531  }
532 
533  Scalar finiteDifferenceIncrement() const
534  {
535  using math::sqrt;
536  return sqrt(Eigen::NumTraits<Scalar>::epsilon());
537  }
538 
539  static std::string classname() { return std::string("JointModelTranslation"); }
540  std::string shortname() const { return classname(); }
541 
543  template<typename NewScalar>
545  {
547  ReturnType res;
548  res.setIndexes(id(),idx_q(),idx_v());
549  return res;
550  }
551 
552  }; // struct JointModelTranslationTpl
553 
554 } // namespace pinocchio
555 
556 #include <boost/type_traits.hpp>
557 
558 namespace boost
559 {
560  template<typename Scalar, int Options>
561  struct has_nothrow_constructor< ::pinocchio::JointModelTranslationTpl<Scalar,Options> >
562  : public integral_constant<bool,true> {};
563 
564  template<typename Scalar, int Options>
565  struct has_nothrow_copy< ::pinocchio::JointModelTranslationTpl<Scalar,Options> >
566  : public integral_constant<bool,true> {};
567 
568  template<typename Scalar, int Options>
569  struct has_nothrow_constructor< ::pinocchio::JointDataTranslationTpl<Scalar,Options> >
570  : public integral_constant<bool,true> {};
571 
572  template<typename Scalar, int Options>
573  struct has_nothrow_copy< ::pinocchio::JointDataTranslationTpl<Scalar,Options> >
574  : public integral_constant<bool,true> {};
575 }
576 
577 #endif // ifndef __pinocchio_joint_translation_hpp__
int idx_q(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel)
Visit a JointModelTpl through JointIdxQVisitor to get the index in the full model configuration space...
int idx_v(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel)
Visit a JointModelTpl through JointIdxVVisitor to get the index in the full model tangent space corre...
JointDataTpl< Scalar, Options, JointCollectionTpl > createData(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel)
Visit a JointModelTpl through CreateData visitor to create a JointDataTpl.
ModelTpl< Scalar, Options, JointCollectionTpl >::TangentVectorType finiteDifferenceIncrement(const ModelTpl< Scalar, Options, JointCollectionTpl > &model)
Computes the finite difference increments for each degree of freedom according to the current joint c...
std::string shortname(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel)
Visit a JointModelTpl through JointShortnameVisitor to get the shortname of the derived joint model...
Main pinocchio namespace.
Definition: treeview.dox:24
void alphaSkew(const Scalar alpha, const Eigen::MatrixBase< Vector3 > &v, const Eigen::MatrixBase< Matrix3 > &M)
Computes the skew representation of a given 3d vector multiplied by a given scalar. i.e. the antisymmetric matrix representation of the cross product operator ( )
Definition: skew.hpp:124
Common traits structure to fully define base classes for CRTP.
Definition: spatial/fwd.hpp:29
void skew(const Eigen::MatrixBase< Vector3 > &v, const Eigen::MatrixBase< Matrix3 > &M)
Computes the skew representation of a given 3d vector, i.e. the antisymmetric matrix representation o...
Definition: skew.hpp:21
ConstLinearType linear() const
Return the linear part of the force vector.
Definition: force-base.hpp:47
JointModelTranslationTpl< NewScalar, Options > cast() const
void calc_aba(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel, JointDataTpl< Scalar, Options, JointCollectionTpl > &jdata, const Eigen::MatrixBase< Matrix6Type > &I, const bool update_I)
Visit a JointModelTpl and the corresponding JointDataTpl through JointCalcAbaVisitor to...