pinocchio  2.1.3
se3-tpl.hpp
1 //
2 // Copyright (c) 2015-2018 CNRS
3 // Copyright (c) 2016 Wandercraft, 86 rue de Paris 91400 Orsay, France.
4 //
5 
6 #ifndef __pinocchio_se3_tpl_hpp__
7 #define __pinocchio_se3_tpl_hpp__
8 
9 #include <Eigen/Geometry>
10 #include "pinocchio/math/quaternion.hpp"
11 
12 namespace pinocchio
13 {
14  template<typename _Scalar, int _Options>
15  struct traits< SE3Tpl<_Scalar,_Options> >
16  {
17  enum {
18  Options = _Options,
19  LINEAR = 0,
20  ANGULAR = 3
21  };
22  typedef _Scalar Scalar;
23  typedef Eigen::Matrix<Scalar,3,1,Options> Vector3;
24  typedef Eigen::Matrix<Scalar,4,1,Options> Vector4;
25  typedef Eigen::Matrix<Scalar,6,1,Options> Vector6;
26  typedef Eigen::Matrix<Scalar,3,3,Options> Matrix3;
27  typedef Eigen::Matrix<Scalar,4,4,Options> Matrix4;
28  typedef Eigen::Matrix<Scalar,6,6,Options> Matrix6;
29  typedef Matrix3 AngularType;
30  typedef typename PINOCCHIO_EIGEN_REF_TYPE(Matrix3) AngularRef;
31  typedef typename PINOCCHIO_EIGEN_REF_CONST_TYPE(Matrix3) ConstAngularRef;
32  typedef Vector3 LinearType;
33  typedef typename PINOCCHIO_EIGEN_REF_TYPE(Vector3) LinearRef;
34  typedef typename PINOCCHIO_EIGEN_REF_CONST_TYPE(Vector3) ConstLinearRef;
35  typedef Matrix6 ActionMatrixType;
36  typedef Matrix4 HomogeneousMatrixType;
37  }; // traits SE3Tpl
38 
39  template<typename _Scalar, int _Options>
40  struct SE3Tpl : public SE3Base< SE3Tpl<_Scalar,_Options> >
41  {
42  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
43  PINOCCHIO_SE3_TYPEDEF_TPL(SE3Tpl);
44  typedef SE3Base< SE3Tpl<_Scalar,_Options> > Base;
45  typedef Eigen::Quaternion<Scalar,Options> Quaternion;
46  typedef typename traits<SE3Tpl>::Vector3 Vector3;
47  typedef typename traits<SE3Tpl>::Matrix3 Matrix3;
48  typedef typename traits<SE3Tpl>::Matrix4 Matrix4;
49  typedef typename traits<SE3Tpl>::Vector4 Vector4;
50  typedef typename traits<SE3Tpl>::Matrix6 Matrix6;
51 
52  using Base::rotation;
53  using Base::translation;
54 
55  SE3Tpl(): rot(), trans() {};
56 
57  template<typename Matrix3Like,typename Vector3Like>
58  SE3Tpl(const Eigen::MatrixBase<Matrix3Like> & R,
59  const Eigen::MatrixBase<Vector3Like> & p)
60  : rot(R), trans(p)
61  {
62  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Vector3Like,3)
63  EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(Matrix3Like,3,3)
64  }
65 
66  template<typename Matrix4Like>
67  explicit SE3Tpl(const Eigen::MatrixBase<Matrix4Like> & m)
68  : rot(m.template block<3,3>(LINEAR,LINEAR))
69  , trans(m.template block<3,1>(LINEAR,ANGULAR))
70  {
71  EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(Matrix4Like,4,4);
72  }
73 
74  SE3Tpl(int)
75  : rot(AngularType::Identity())
76  , trans(LinearType::Zero())
77  {}
78 
79  template<int O2>
80  SE3Tpl(const SE3Tpl<Scalar,O2> & clone)
81  : rot(clone.rotation()),trans(clone.translation()) {}
82 
83  template<int O2>
84  SE3Tpl & operator=(const SE3Tpl<Scalar,O2> & other)
85  {
86  rot = other.rotation();
87  trans = other.translation();
88  return *this;
89  }
90 
91  static SE3Tpl Identity()
92  {
93  return SE3Tpl(1);
94  }
95 
96  SE3Tpl & setIdentity()
97  { rot.setIdentity (); trans.setZero (); return *this;}
98 
100  SE3Tpl inverse() const
101  {
102  return SE3Tpl(rot.transpose(), -rot.transpose()*trans);
103  }
104 
105  static SE3Tpl Random()
106  {
107  return SE3Tpl().setRandom();
108  }
109 
110  SE3Tpl & setRandom()
111  {
112  Quaternion q; quaternion::uniformRandom(q);
113  rot = q.matrix();
114  trans.setRandom();
115 
116  return *this;
117  }
118 
119  HomogeneousMatrixType toHomogeneousMatrix_impl() const
120  {
121  HomogeneousMatrixType M;
122  M.template block<3,3>(LINEAR,LINEAR) = rot;
123  M.template block<3,1>(LINEAR,ANGULAR) = trans;
124  M.template block<1,3>(ANGULAR,LINEAR).setZero();
125  M(3,3) = 1;
126  return M;
127  }
128 
130  ActionMatrixType toActionMatrix_impl() const
131  {
132  typedef Eigen::Block<ActionMatrixType,3,3> Block3;
133  ActionMatrixType M;
134  M.template block<3,3>(ANGULAR,ANGULAR)
135  = M.template block<3,3>(LINEAR,LINEAR) = rot;
136  M.template block<3,3>(ANGULAR,LINEAR).setZero();
137  Block3 B = M.template block<3,3>(LINEAR,ANGULAR);
138 
139  B.col(0) = trans.cross(rot.col(0));
140  B.col(1) = trans.cross(rot.col(1));
141  B.col(2) = trans.cross(rot.col(2));
142  return M;
143  }
144 
145  ActionMatrixType toDualActionMatrix_impl() const
146  {
147  typedef Eigen::Block<ActionMatrixType,3,3> Block3;
148  ActionMatrixType M;
149  M.template block<3,3>(ANGULAR,ANGULAR)
150  = M.template block<3,3>(LINEAR,LINEAR) = rot;
151  M.template block<3,3>(LINEAR,ANGULAR).setZero();
152  Block3 B = M.template block<3,3>(ANGULAR,LINEAR);
153 
154  B.col(0) = trans.cross(rot.col(0));
155  B.col(1) = trans.cross(rot.col(1));
156  B.col(2) = trans.cross(rot.col(2));
157  return M;
158  }
159 
160  void disp_impl(std::ostream & os) const
161  {
162  os
163  << " R =\n" << rot << std::endl
164  << " p = " << trans.transpose() << std::endl;
165  }
166 
168 
170  template<typename D>
172  act_impl(const D & d) const
173  {
174  return d.se3Action(*this);
175  }
176 
178  template<typename D> typename internal::SE3GroupAction<D>::ReturnType
179  actInv_impl(const D & d) const
180  {
181  return d.se3ActionInverse(*this);
182  }
183 
184  template<typename EigenDerived>
185  typename EigenDerived::PlainObject
186  actOnEigenObject(const Eigen::MatrixBase<EigenDerived> & p) const
187  { return (rotation()*p+translation()).eval(); }
188 
189  template<typename MapDerived>
190  Vector3 actOnEigenObject(const Eigen::MapBase<MapDerived> & p) const
191  { return Vector3(rotation()*p+translation()); }
192 
193  template<typename EigenDerived>
194  typename EigenDerived::PlainObject
195  actInvOnEigenObject(const Eigen::MatrixBase<EigenDerived> & p) const
196  { return (rotation().transpose()*(p-translation())).eval(); }
197 
198  template<typename MapDerived>
199  Vector3 actInvOnEigenObject(const Eigen::MapBase<MapDerived> & p) const
200  { return Vector3(rotation().transpose()*(p-translation())); }
201 
202  Vector3 act_impl(const Vector3 & p) const
203  { return Vector3(rotation()*p+translation()); }
204 
205  Vector3 actInv_impl(const Vector3 & p) const
206  { return Vector3(rotation().transpose()*(p-translation())); }
207 
208  template<int O2>
209  SE3Tpl act_impl(const SE3Tpl<Scalar,O2> & m2) const
210  { return SE3Tpl(rot*m2.rotation()
211  ,translation()+rotation()*m2.translation());}
212 
213  template<int O2>
214  SE3Tpl actInv_impl(const SE3Tpl<Scalar,O2> & m2) const
215  { return SE3Tpl(rot.transpose()*m2.rotation(),
216  rot.transpose()*(m2.translation()-translation()));}
217 
218  template<int O2>
219  SE3Tpl __mult__(const SE3Tpl<Scalar,O2> & m2) const
220  { return this->act_impl(m2);}
221 
222  template<int O2>
223  bool __equal__(const SE3Tpl<Scalar,O2> & m2) const
224  {
225  return (rotation() == m2.rotation() && translation() == m2.translation());
226  }
227 
228  template<int O2>
229  bool isApprox_impl(const SE3Tpl<Scalar,O2> & m2,
230  const Scalar & prec = Eigen::NumTraits<Scalar>::dummy_precision()) const
231  {
232  return rotation().isApprox(m2.rotation(), prec)
233  && translation().isApprox(m2.translation(), prec);
234  }
235 
236  bool isIdentity(const Scalar & prec = Eigen::NumTraits<Scalar>::dummy_precision()) const
237  {
238  return rotation().isIdentity(prec) && translation().isZero(prec);
239  }
240 
241  ConstAngularRef rotation_impl() const { return rot; }
242  AngularRef rotation_impl() { return rot; }
243  void rotation_impl(const AngularType & R) { rot = R; }
244  ConstLinearRef translation_impl() const { return trans;}
245  LinearRef translation_impl() { return trans;}
246  void translation_impl(const LinearType & p) { trans = p; }
247 
249  template<typename NewScalar>
251  {
252  typedef SE3Tpl<NewScalar,Options> ReturnType;
253  ReturnType res(rot.template cast<NewScalar>(),
254  trans.template cast<NewScalar>());
255  return res;
256  }
257 
258  protected:
259  AngularType rot;
260  LinearType trans;
261 
262  }; // class SE3Tpl
263 
264 } // namespace pinocchio
265 
266 #endif // ifndef __pinocchio_se3_tpl_hpp__
267 
SE3Tpl inverse() const
aXb = bXa.inverse()
Definition: se3-tpl.hpp:100
internal::SE3GroupAction< D >::ReturnType act_impl(const D &d) const
— GROUP ACTIONS ON M6, F6 and I6 —
Definition: se3-tpl.hpp:172
internal::SE3GroupAction< D >::ReturnType actInv_impl(const D &d) const
by = aXb.actInv(ay)
Definition: se3-tpl.hpp:179
void uniformRandom(const Eigen::QuaternionBase< Derived > &q)
Uniformly random quaternion sphere.
Definition: quaternion.hpp:96
Main pinocchio namespace.
Definition: treeview.dox:24
SE3Tpl< NewScalar, Options > cast() const
Definition: se3-tpl.hpp:250
Common traits structure to fully define base classes for CRTP.
Definition: spatial/fwd.hpp:29
ActionMatrixType toActionMatrix_impl() const
Vb.toVector() = bXa.toMatrix() * Va.toVector()
Definition: se3-tpl.hpp:130