pinocchio  3.3.1
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
joint-translation.hpp
1 //
2 // Copyright (c) 2015-2020 CNRS INRIA
3 // Copyright (c) 2015-2016 Wandercraft, 86 rue de Paris 91400 Orsay, France.
4 //
5 
6 #ifndef __pinocchio_multibody_joint_translation_hpp__
7 #define __pinocchio_multibody_joint_translation_hpp__
8 
9 #include "pinocchio/macros.hpp"
10 #include "pinocchio/multibody/joint/joint-base.hpp"
11 #include "pinocchio/multibody/joint-motion-subspace.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 = context::Options>
19  struct MotionTranslationTpl;
20  typedef MotionTranslationTpl<context::Scalar> MotionTranslation;
21 
22  template<typename Scalar, int Options>
23  struct SE3GroupAction<MotionTranslationTpl<Scalar, Options>>
24  {
26  };
27 
28  template<typename Scalar, int Options, typename MotionDerived>
29  struct MotionAlgebraAction<MotionTranslationTpl<Scalar, Options>, MotionDerived>
30  {
32  };
33 
34  template<typename _Scalar, int _Options>
35  struct traits<MotionTranslationTpl<_Scalar, _Options>>
36  {
37  typedef _Scalar Scalar;
38  enum
39  {
40  Options = _Options
41  };
42  typedef Eigen::Matrix<Scalar, 3, 1, Options> Vector3;
43  typedef Eigen::Matrix<Scalar, 6, 1, Options> Vector6;
44  typedef Eigen::Matrix<Scalar, 4, 4, Options> Matrix4;
45  typedef Eigen::Matrix<Scalar, 6, 6, Options> Matrix6;
46  typedef typename PINOCCHIO_EIGEN_REF_CONST_TYPE(Vector6) ToVectorConstReturnType;
47  typedef typename PINOCCHIO_EIGEN_REF_TYPE(Vector6) ToVectorReturnType;
48  typedef Vector3 AngularType;
49  typedef Vector3 LinearType;
50  typedef const Vector3 ConstAngularType;
51  typedef const Vector3 ConstLinearType;
52  typedef Matrix6 ActionMatrixType;
53  typedef Matrix4 HomogeneousMatrixType;
56  enum
57  {
58  LINEAR = 0,
59  ANGULAR = 3
60  };
61  }; // traits MotionTranslationTpl
62 
63  template<typename _Scalar, int _Options>
64  struct MotionTranslationTpl : MotionBase<MotionTranslationTpl<_Scalar, _Options>>
65  {
66  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
67 
68  MOTION_TYPEDEF_TPL(MotionTranslationTpl);
69 
71  {
72  }
73 
74  template<typename Vector3Like>
75  MotionTranslationTpl(const Eigen::MatrixBase<Vector3Like> & v)
76  : m_v(v)
77  {
78  }
79 
81  : m_v(other.m_v)
82  {
83  }
84 
85  Vector3 & operator()()
86  {
87  return m_v;
88  }
89  const Vector3 & operator()() const
90  {
91  return m_v;
92  }
93 
94  inline PlainReturnType plain() const
95  {
96  return PlainReturnType(m_v, PlainReturnType::Vector3::Zero());
97  }
98 
99  bool isEqual_impl(const MotionTranslationTpl & other) const
100  {
101  return internal::comparison_eq(m_v, other.m_v);
102  }
103 
104  MotionTranslationTpl & operator=(const MotionTranslationTpl & other)
105  {
106  m_v = other.m_v;
107  return *this;
108  }
109 
110  template<typename Derived>
111  void addTo(MotionDense<Derived> & other) const
112  {
113  other.linear() += m_v;
114  }
115 
116  template<typename Derived>
117  void setTo(MotionDense<Derived> & other) const
118  {
119  other.linear() = m_v;
120  other.angular().setZero();
121  }
122 
123  template<typename S2, int O2, typename D2>
124  void se3Action_impl(const SE3Tpl<S2, O2> & m, MotionDense<D2> & v) const
125  {
126  v.angular().setZero();
127  v.linear().noalias() = m.rotation() * m_v; // TODO: check efficiency
128  }
129 
130  template<typename S2, int O2>
131  MotionPlain se3Action_impl(const SE3Tpl<S2, O2> & m) const
132  {
133  MotionPlain res;
134  se3Action_impl(m, res);
135  return res;
136  }
137 
138  template<typename S2, int O2, typename D2>
139  void se3ActionInverse_impl(const SE3Tpl<S2, O2> & m, MotionDense<D2> & v) const
140  {
141  // Linear
142  v.linear().noalias() = m.rotation().transpose() * m_v;
143 
144  // Angular
145  v.angular().setZero();
146  }
147 
148  template<typename S2, int O2>
149  MotionPlain se3ActionInverse_impl(const SE3Tpl<S2, O2> & m) const
150  {
151  MotionPlain res;
152  se3ActionInverse_impl(m, res);
153  return res;
154  }
155 
156  template<typename M1, typename M2>
157  void motionAction(const MotionDense<M1> & v, MotionDense<M2> & mout) const
158  {
159  // Linear
160  mout.linear().noalias() = v.angular().cross(m_v);
161 
162  // Angular
163  mout.angular().setZero();
164  }
165 
166  template<typename M1>
167  MotionPlain motionAction(const MotionDense<M1> & v) const
168  {
169  MotionPlain res;
170  motionAction(v, res);
171  return res;
172  }
173 
174  const Vector3 & linear() const
175  {
176  return m_v;
177  }
178  Vector3 & linear()
179  {
180  return m_v;
181  }
182 
183  protected:
184  Vector3 m_v;
185 
186  }; // struct MotionTranslationTpl
187 
188  template<typename S1, int O1, typename MotionDerived>
189  inline typename MotionDerived::MotionPlain
190  operator+(const MotionTranslationTpl<S1, O1> & m1, const MotionDense<MotionDerived> & m2)
191  {
192  return typename MotionDerived::MotionPlain(m2.linear() + m1.linear(), m2.angular());
193  }
194 
195  template<typename Scalar, int Options>
196  struct TransformTranslationTpl;
197 
198  template<typename _Scalar, int _Options>
199  struct traits<TransformTranslationTpl<_Scalar, _Options>>
200  {
201  enum
202  {
203  Options = _Options,
204  LINEAR = 0,
205  ANGULAR = 3
206  };
207  typedef _Scalar Scalar;
209  typedef Eigen::Matrix<Scalar, 3, 1, Options> Vector3;
210  typedef Eigen::Matrix<Scalar, 3, 3, Options> Matrix3;
211  typedef typename Matrix3::IdentityReturnType AngularType;
212  typedef AngularType AngularRef;
213  typedef AngularType ConstAngularRef;
214  typedef Vector3 LinearType;
215  typedef LinearType & LinearRef;
216  typedef const LinearType & ConstLinearRef;
217  typedef typename traits<PlainType>::ActionMatrixType ActionMatrixType;
218  typedef typename traits<PlainType>::HomogeneousMatrixType HomogeneousMatrixType;
219  }; // traits TransformTranslationTpl
220 
221  template<typename Scalar, int Options>
222  struct SE3GroupAction<TransformTranslationTpl<Scalar, Options>>
223  {
224  typedef typename traits<TransformTranslationTpl<Scalar, Options>>::PlainType ReturnType;
225  };
226 
227  template<typename _Scalar, int _Options>
228  struct TransformTranslationTpl : SE3Base<TransformTranslationTpl<_Scalar, _Options>>
229  {
230  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
231  PINOCCHIO_SE3_TYPEDEF_TPL(TransformTranslationTpl);
232  typedef typename traits<TransformTranslationTpl>::Vector3 Vector3;
233 
235  {
236  }
237 
238  template<typename Vector3Like>
239  TransformTranslationTpl(const Eigen::MatrixBase<Vector3Like> & translation)
240  : m_translation(translation)
241  {
242  }
243 
244  PlainType plain() const
245  {
246  PlainType res(PlainType::Identity());
247  res.rotation().setIdentity();
248  res.translation() = translation();
249 
250  return res;
251  }
252 
253  operator PlainType() const
254  {
255  return plain();
256  }
257 
258  template<typename S2, int O2>
259  typename SE3GroupAction<TransformTranslationTpl>::ReturnType
260  se3action(const SE3Tpl<S2, O2> & m) const
261  {
262  typedef typename SE3GroupAction<TransformTranslationTpl>::ReturnType ReturnType;
263  ReturnType res(m);
264  res.translation() += translation();
265 
266  return res;
267  }
268 
269  ConstLinearRef translation() const
270  {
271  return m_translation;
272  }
273  LinearRef translation()
274  {
275  return m_translation;
276  }
277 
278  AngularType rotation() const
279  {
280  return AngularType(3, 3);
281  }
282 
283  bool isEqual(const TransformTranslationTpl & other) const
284  {
285  return internal::comparison_eq(m_translation, other.m_translation);
286  }
287 
288  protected:
289  LinearType m_translation;
290  };
291 
292  template<typename Scalar, int Options>
294 
295  template<typename _Scalar, int _Options>
296  struct traits<JointMotionSubspaceTranslationTpl<_Scalar, _Options>>
297  {
298  typedef _Scalar Scalar;
299 
300  enum
301  {
302  Options = _Options
303  };
304  enum
305  {
306  LINEAR = 0,
307  ANGULAR = 3
308  };
309 
311  typedef Eigen::Matrix<Scalar, 3, 1, Options> JointForce;
312  typedef Eigen::Matrix<Scalar, 6, 3, Options> DenseBase;
313  typedef Eigen::Matrix<Scalar, 3, 3, Options> ReducedSquaredMatrix;
314 
315  typedef DenseBase MatrixReturnType;
316  typedef const DenseBase ConstMatrixReturnType;
317 
318  typedef typename ReducedSquaredMatrix::IdentityReturnType StDiagonalMatrixSOperationReturnType;
319  }; // traits JointMotionSubspaceTranslationTpl
320 
321  template<typename _Scalar, int _Options>
323  : JointMotionSubspaceBase<JointMotionSubspaceTranslationTpl<_Scalar, _Options>>
324  {
325  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
326 
327  PINOCCHIO_CONSTRAINT_TYPEDEF_TPL(JointMotionSubspaceTranslationTpl)
328 
329  enum
330  {
331  NV = 3
332  };
333 
335  {
336  }
337 
338  // template<typename S1, int O1>
339  // Motion operator*(const MotionTranslationTpl<S1,O1> & vj) const
340  // { return Motion(vj(), Motion::Vector3::Zero()); }
341 
342  template<typename Vector3Like>
343  JointMotion __mult__(const Eigen::MatrixBase<Vector3Like> & v) const
344  {
345  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Vector3Like, 3);
346  return JointMotion(v);
347  }
348 
349  int nv_impl() const
350  {
351  return NV;
352  }
353 
354  struct ConstraintTranspose : JointMotionSubspaceTransposeBase<JointMotionSubspaceTranslationTpl>
355  {
358  : ref(ref)
359  {
360  }
361 
362  template<typename Derived>
363  typename ForceDense<Derived>::ConstLinearType operator*(const ForceDense<Derived> & phi)
364  {
365  return phi.linear();
366  }
367 
368  /* [CRBA] MatrixBase operator* (Constraint::Transpose S, ForceSet::Block) */
369  template<typename MatrixDerived>
370  const typename SizeDepType<3>::RowsReturn<MatrixDerived>::ConstType
371  operator*(const Eigen::MatrixBase<MatrixDerived> & F) const
372  {
373  assert(F.rows() == 6);
374  return F.derived().template middleRows<3>(LINEAR);
375  }
376 
377  }; // struct ConstraintTranspose
378 
379  ConstraintTranspose transpose() const
380  {
381  return ConstraintTranspose(*this);
382  }
383 
384  DenseBase matrix_impl() const
385  {
386  DenseBase S;
387  S.template middleRows<3>(LINEAR).setIdentity();
388  S.template middleRows<3>(ANGULAR).setZero();
389  return S;
390  }
391 
392  template<typename S1, int O1>
393  Eigen::Matrix<S1, 6, 3, O1> se3Action(const SE3Tpl<S1, O1> & m) const
394  {
395  Eigen::Matrix<S1, 6, 3, O1> M;
396  M.template middleRows<3>(LINEAR) = m.rotation();
397  M.template middleRows<3>(ANGULAR).setZero();
398 
399  return M;
400  }
401 
402  template<typename S1, int O1>
403  Eigen::Matrix<S1, 6, 3, O1> se3ActionInverse(const SE3Tpl<S1, O1> & m) const
404  {
405  Eigen::Matrix<S1, 6, 3, O1> M;
406  M.template middleRows<3>(LINEAR) = m.rotation().transpose();
407  M.template middleRows<3>(ANGULAR).setZero();
408 
409  return M;
410  }
411 
412  template<typename MotionDerived>
413  DenseBase motionAction(const MotionDense<MotionDerived> & m) const
414  {
415  const typename MotionDerived::ConstAngularType w = m.angular();
416 
417  DenseBase res;
418  skew(w, res.template middleRows<3>(LINEAR));
419  res.template middleRows<3>(ANGULAR).setZero();
420 
421  return res;
422  }
423 
424  bool isEqual(const JointMotionSubspaceTranslationTpl &) const
425  {
426  return true;
427  }
428 
429  }; // struct JointMotionSubspaceTranslationTpl
430 
431  template<typename MotionDerived, typename S2, int O2>
432  inline typename MotionDerived::MotionPlain
433  operator^(const MotionDense<MotionDerived> & m1, const MotionTranslationTpl<S2, O2> & m2)
434  {
435  return m2.motionAction(m1);
436  }
437 
438  /* [CRBA] ForceSet operator* (Inertia Y,Constraint S) */
439  template<typename S1, int O1, typename S2, int O2>
440  inline Eigen::Matrix<S2, 6, 3, O2>
441  operator*(const InertiaTpl<S1, O1> & Y, const JointMotionSubspaceTranslationTpl<S2, O2> &)
442  {
443  typedef JointMotionSubspaceTranslationTpl<S2, O2> Constraint;
444  Eigen::Matrix<S2, 6, 3, O2> M;
445  alphaSkew(Y.mass(), Y.lever(), M.template middleRows<3>(Constraint::ANGULAR));
446  M.template middleRows<3>(Constraint::LINEAR).setZero();
447  M.template middleRows<3>(Constraint::LINEAR).diagonal().fill(Y.mass());
448 
449  return M;
450  }
451 
452  /* [ABA] Y*S operator*/
453  template<typename M6Like, typename S2, int O2>
454  inline const typename SizeDepType<3>::ColsReturn<M6Like>::ConstType
455  operator*(const Eigen::MatrixBase<M6Like> & Y, const JointMotionSubspaceTranslationTpl<S2, O2> &)
456  {
457  typedef JointMotionSubspaceTranslationTpl<S2, O2> Constraint;
458  return Y.derived().template middleCols<3>(Constraint::LINEAR);
459  }
460 
461  template<typename S1, int O1>
463  {
464  typedef Eigen::Matrix<S1, 6, 3, O1> ReturnType;
465  };
466 
467  template<typename S1, int O1, typename MotionDerived>
469  {
470  typedef Eigen::Matrix<S1, 6, 3, O1> ReturnType;
471  };
472 
473  template<typename Scalar, int Options>
475 
476  template<typename _Scalar, int _Options>
477  struct traits<JointTranslationTpl<_Scalar, _Options>>
478  {
479  enum
480  {
481  NQ = 3,
482  NV = 3
483  };
484  typedef _Scalar Scalar;
485  enum
486  {
487  Options = _Options
488  };
495 
496  // [ABA]
497  typedef Eigen::Matrix<Scalar, 6, NV, Options> U_t;
498  typedef Eigen::Matrix<Scalar, NV, NV, Options> D_t;
499  typedef Eigen::Matrix<Scalar, 6, NV, Options> UD_t;
500 
501  typedef Eigen::Matrix<Scalar, NQ, 1, Options> ConfigVector_t;
502  typedef Eigen::Matrix<Scalar, NV, 1, Options> TangentVector_t;
503 
504  PINOCCHIO_JOINT_DATA_BASE_ACCESSOR_DEFAULT_RETURN_TYPE
505  }; // traits JointTranslationTpl
506 
507  template<typename _Scalar, int _Options>
508  struct traits<JointDataTranslationTpl<_Scalar, _Options>>
509  {
511  typedef _Scalar Scalar;
512  };
513 
514  template<typename _Scalar, int _Options>
515  struct traits<JointModelTranslationTpl<_Scalar, _Options>>
516  {
518  typedef _Scalar Scalar;
519  };
520 
521  template<typename _Scalar, int _Options>
522  struct JointDataTranslationTpl : public JointDataBase<JointDataTranslationTpl<_Scalar, _Options>>
523  {
524  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
525 
527  PINOCCHIO_JOINT_DATA_TYPEDEF_TEMPLATE(JointDerived);
528  PINOCCHIO_JOINT_DATA_BASE_DEFAULT_ACCESSOR
529 
530  ConfigVector_t joint_q;
531  TangentVector_t joint_v;
532 
533  Constraint_t S;
534  Transformation_t M;
535  Motion_t v;
536  Bias_t c;
537 
538  // [ABA] specific data
539  U_t U;
540  D_t Dinv;
541  UD_t UDinv;
542  D_t StU;
543 
545  : joint_q(ConfigVector_t::Zero())
546  , joint_v(TangentVector_t::Zero())
547  , M(Transformation_t::Vector3::Zero())
548  , v(Motion_t::Vector3::Zero())
549  , U(U_t::Zero())
550  , Dinv(D_t::Zero())
551  , UDinv(UD_t::Zero())
552  , StU(D_t::Zero())
553  {
554  }
555 
556  static std::string classname()
557  {
558  return std::string("JointDataTranslation");
559  }
560  std::string shortname() const
561  {
562  return classname();
563  }
564  }; // struct JointDataTranslationTpl
565 
566  PINOCCHIO_JOINT_CAST_TYPE_SPECIALIZATION(JointModelTranslationTpl);
567  template<typename _Scalar, int _Options>
569  : public JointModelBase<JointModelTranslationTpl<_Scalar, _Options>>
570  {
571  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
572 
574  PINOCCHIO_JOINT_TYPEDEF_TEMPLATE(JointDerived);
575 
577  using Base::id;
578  using Base::idx_q;
579  using Base::idx_v;
580  using Base::setIndexes;
581 
582  JointDataDerived createData() const
583  {
584  return JointDataDerived();
585  }
586 
587  const std::vector<bool> hasConfigurationLimit() const
588  {
589  return {true, true, true};
590  }
591 
592  const std::vector<bool> hasConfigurationLimitInTangent() const
593  {
594  return {true, true, true};
595  }
596 
597  template<typename ConfigVector>
598  void calc(JointDataDerived & data, const typename Eigen::MatrixBase<ConfigVector> & qs) const
599  {
600  data.joint_q = this->jointConfigSelector(qs);
601  data.M.translation() = data.joint_q;
602  }
603 
604  template<typename TangentVector>
605  void
606  calc(JointDataDerived & data, const Blank, const typename Eigen::MatrixBase<TangentVector> & vs)
607  const
608  {
609  data.joint_v = this->jointVelocitySelector(vs);
610  data.v.linear() = data.joint_v;
611  }
612 
613  template<typename ConfigVector, typename TangentVector>
614  void calc(
615  JointDataDerived & data,
616  const typename Eigen::MatrixBase<ConfigVector> & qs,
617  const typename Eigen::MatrixBase<TangentVector> & vs) const
618  {
619  calc(data, qs.derived());
620 
621  data.joint_v = this->jointVelocitySelector(vs);
622  data.v.linear() = data.joint_v;
623  }
624 
625  template<typename VectorLike, typename Matrix6Like>
626  void calc_aba(
627  JointDataDerived & data,
628  const Eigen::MatrixBase<VectorLike> & armature,
629  const Eigen::MatrixBase<Matrix6Like> & I,
630  const bool update_I) const
631  {
632  data.U = I.template middleCols<3>(Inertia::LINEAR);
633 
634  data.StU = data.U.template middleRows<3>(Inertia::LINEAR);
635  data.StU.diagonal() += armature;
636 
637  internal::PerformStYSInversion<Scalar>::run(data.StU, data.Dinv);
638 
639  data.UDinv.noalias() = data.U * data.Dinv;
640 
641  if (update_I)
642  PINOCCHIO_EIGEN_CONST_CAST(Matrix6Like, I).noalias() -= data.UDinv * data.U.transpose();
643  }
644 
645  static std::string classname()
646  {
647  return std::string("JointModelTranslation");
648  }
649  std::string shortname() const
650  {
651  return classname();
652  }
653 
655  template<typename NewScalar>
657  {
659  ReturnType res;
660  res.setIndexes(id(), idx_q(), idx_v());
661  return res;
662  }
663 
664  }; // struct JointModelTranslationTpl
665 
666 } // namespace pinocchio
667 
668 #include <boost/type_traits.hpp>
669 
670 namespace boost
671 {
672  template<typename Scalar, int Options>
673  struct has_nothrow_constructor<::pinocchio::JointModelTranslationTpl<Scalar, Options>>
674  : public integral_constant<bool, true>
675  {
676  };
677 
678  template<typename Scalar, int Options>
679  struct has_nothrow_copy<::pinocchio::JointModelTranslationTpl<Scalar, Options>>
680  : public integral_constant<bool, true>
681  {
682  };
683 
684  template<typename Scalar, int Options>
685  struct has_nothrow_constructor<::pinocchio::JointDataTranslationTpl<Scalar, Options>>
686  : public integral_constant<bool, true>
687  {
688  };
689 
690  template<typename Scalar, int Options>
691  struct has_nothrow_copy<::pinocchio::JointDataTranslationTpl<Scalar, Options>>
692  : public integral_constant<bool, true>
693  {
694  };
695 } // namespace boost
696 
697 #endif // ifndef __pinocchio_multibody_joint_translation_hpp__
ConstLinearType linear() const
Return the linear part of the force vector.
Definition: force-base.hpp:57
Main pinocchio namespace.
Definition: treeview.dox:11
JointDataTpl< Scalar, Options, JointCollectionTpl >::TangentVector_t joint_v(const JointDataTpl< Scalar, Options, JointCollectionTpl > &jdata)
Visit a JointDataVariant through JointConfigVisitor to get the joint velocity vector.
JointDataTpl< Scalar, Options, JointCollectionTpl >::ConfigVector_t joint_q(const JointDataTpl< Scalar, Options, JointCollectionTpl > &jdata)
Visit a JointDataVariant through JointConfigVisitor to get the joint configuration vector.
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....
Definition: skew.hpp:134
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:22
Blank type.
Definition: fwd.hpp:77
JointModelTranslationTpl< NewScalar, Options > cast() const
Return type of the ation of a Motion onto an object of type D.
Definition: motion.hpp:46
Base class for rigid transformation.
Definition: se3-base.hpp:31
Common traits structure to fully define base classes for CRTP.
Definition: fwd.hpp:72