pinocchio  1.2.6-7-g6de3e-dirty
motion.hpp
1 //
2 // Copyright (c) 2015-2017 CNRS
3 // Copyright (c) 2015-2016 Wandercraft, 86 rue de Paris 91400 Orsay, France.
4 //
5 // This file is part of Pinocchio
6 // Pinocchio is free software: you can redistribute it
7 // and/or modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation, either version
9 // 3 of the License, or (at your option) any later version.
10 //
11 // Pinocchio is distributed in the hope that it will be
12 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Lesser Public License for more details. You should have
15 // received a copy of the GNU Lesser General Public License along with
16 // Pinocchio If not, see
17 // <http://www.gnu.org/licenses/>.
18 
19 #ifndef __se3_motion_hpp__
20 #define __se3_motion_hpp__
21 
22 #include <Eigen/Core>
23 #include "pinocchio/spatial/fwd.hpp"
24 #include "pinocchio/macros.hpp"
25 #include "pinocchio/spatial/se3.hpp"
26 #include "pinocchio/spatial/force.hpp"
27 
28 namespace se3
29 {
30 
31 
32  template< class Derived>
33  class MotionBase
34  {
35  protected:
36  typedef Derived Derived_t;
37  SPATIAL_TYPEDEF_TEMPLATE(Derived_t);
38 
39  public:
40  Derived_t & derived() { return *static_cast<Derived_t*>(this); }
41  const Derived_t& derived() const { return *static_cast<const Derived_t*>(this); }
42 
43  ConstAngular_t angular() const { return static_cast<const Derived_t*>(this)->angular_impl(); }
44  ConstLinear_t linear() const { return static_cast<const Derived_t*>(this)->linear_impl(); }
45  Angular_t angular() { return static_cast<Derived_t*>(this)->angular_impl(); }
46  Linear_t linear() { return static_cast<Derived_t*>(this)->linear_impl(); }
47 
48  template<typename D>
49  void angular(const Eigen::MatrixBase<D> & w) { static_cast< Derived_t*>(this)->angular_impl(w); }
50  template<typename D>
51  void linear(const Eigen::MatrixBase<D> & v) { static_cast< Derived_t*>(this)->linear_impl(v); }
52 
53  const Vector6 & toVector() const { return derived().toVector_impl(); }
54  Vector6 & toVector() { return derived().toVector_impl(); }
55  operator Vector6 () const { return toVector(); }
56 
57  ActionMatrix_t toActionMatrix() const { return derived().toActionMatrix_impl(); }
58  ActionMatrix_t toDualActionMatrix() const { return derived().toDualActionMatrix_impl(); }
59  operator Matrix6 () const { return toActionMatrix(); }
60 
61  bool operator==(const Derived_t & other) const {return derived().isEqual(other);}
62  bool operator!=(const Derived_t & other) const { return !(*this == other); }
63  Derived_t operator-() const { return derived().__minus__(); }
64  Derived_t operator+(const Derived_t & v2) const { return derived().__plus__(v2); }
65  Derived_t operator-(const Derived_t & v2) const { return derived().__minus__(v2); }
66  Derived_t & operator+=(const Derived_t & v2) { return derived().__pequ__(v2); }
67  Derived_t & operator-=(const Derived_t & v2) { return derived().__mequ__(v2); }
68  Derived_t operator*(const Scalar alpha) const { return derived().__mult__(alpha); }
69 
70  bool isApprox (const Derived_t & other, const Scalar & prec = Eigen::NumTraits<Scalar>::dummy_precision()) const
71  { return derived().isApprox_impl(other, prec);}
72 
73  Derived_t se3Action(const SE3 & m) const { return derived().se3Action_impl(m); }
74  Derived_t se3ActionInverse(const SE3 & m) const { return derived().se3ActionInverse_impl(m); }
75 
76  Scalar dot(const Force & f) const { return static_cast<Derived_t*>(this)->dot(f); }
77 
78  void disp(std::ostream & os) const { derived().disp_impl(os); }
79  friend std::ostream & operator << (std::ostream & os, const MotionBase<Derived_t> & mv)
80  {
81  mv.disp(os);
82  return os;
83  }
84 
85  }; // class MotionBase
86 
87 
88  template<typename T, int U>
89  struct traits< MotionTpl<T, U> >
90  {
91  typedef T Scalar;
92  typedef Eigen::Matrix<T,3,1,U> Vector3;
93  typedef Eigen::Matrix<T,4,1,U> Vector4;
94  typedef Eigen::Matrix<T,6,1,U> Vector6;
95  typedef Eigen::Matrix<T,3,3,U> Matrix3;
96  typedef Eigen::Matrix<T,4,4,U> Matrix4;
97  typedef Eigen::Matrix<T,6,6,U> Matrix6;
98  typedef Matrix6 ActionMatrix_t;
99  typedef typename Vector6::template FixedSegmentReturnType<3>::Type Linear_t;
100  typedef typename Vector6::template FixedSegmentReturnType<3>::Type Angular_t;
101  typedef typename Vector6::template ConstFixedSegmentReturnType<3>::Type ConstLinear_t;
102  typedef typename Vector6::template ConstFixedSegmentReturnType<3>::Type ConstAngular_t;
103  typedef Eigen::Quaternion<T,U> Quaternion_t;
104  typedef SE3Tpl<T,U> SE3;
105  typedef ForceTpl<T,U> Force;
106  typedef MotionTpl<T,U> Motion;
108  enum {
109  LINEAR = 0,
110  ANGULAR = 3
111  };
112  }; // traits MotionTpl
113 
114 
115  template<typename _Scalar, int _Options>
116  class MotionTpl : public MotionBase< MotionTpl< _Scalar, _Options > >
117  {
118  public:
119  SPATIAL_TYPEDEF_TEMPLATE(MotionTpl);
120  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
121 
122  public:
123  // Constructors
124  MotionTpl() : data() {}
125 
126  template<typename v1,typename v2>
127  MotionTpl(const Eigen::MatrixBase<v1> & v, const Eigen::MatrixBase<v2> & w)
128  {
129  data << v, w;
130  }
131 
132  template<typename v6>
133  explicit MotionTpl(const Eigen::MatrixBase<v6> & v)
134  : data(v)
135  {
136  EIGEN_STATIC_ASSERT_VECTOR_ONLY(v6);
137  assert( v.size() == 6 );
138  }
139 
140 
141  template<typename S2,int O2>
142  explicit MotionTpl(const MotionTpl<S2,O2> & clone)
143  : data(clone.toVector())
144  {}
145 
146  // initializers
147  static MotionTpl Zero() { return MotionTpl(Vector6::Zero()); }
148  static MotionTpl Random() { return MotionTpl(Vector6::Random()); }
149 
150  MotionTpl & setZero () { data.setZero (); return *this; }
151  MotionTpl & setRandom () { data.setRandom (); return *this; }
152 
153  const Vector6 & toVector_impl() const { return data; }
154  Vector6 & toVector_impl() { return data; }
155 
156  ActionMatrix_t toActionMatrix_impl () const
157  {
158  ActionMatrix_t X;
159  X.template block <3,3> (ANGULAR, ANGULAR) = X.template block <3,3> (LINEAR, LINEAR) = skew (angular_impl());
160  X.template block <3,3> (LINEAR, ANGULAR) = skew (linear_impl());
161  X.template block <3,3> (ANGULAR, LINEAR).setZero ();
162 
163  return X;
164  }
165 
166  ActionMatrix_t toDualActionMatrix_impl () const
167  {
168  ActionMatrix_t X;
169  X.template block <3,3> (ANGULAR, ANGULAR) = X.template block <3,3> (LINEAR, LINEAR) = skew (angular_impl());
170  X.template block <3,3> (ANGULAR, LINEAR) = skew (linear_impl());
171  X.template block <3,3> (LINEAR, ANGULAR).setZero ();
172 
173  return X;
174  }
175 
176  // Getters
177  ConstAngular_t angular_impl() const { return data.template segment<3> (ANGULAR); }
178  ConstLinear_t linear_impl() const { return data.template segment<3> (LINEAR); }
179  Angular_t angular_impl() { return data.template segment<3> (ANGULAR); }
180  Linear_t linear_impl() { return data.template segment<3> (LINEAR); }
181 
182  template<typename D>
183  void angular_impl(const Eigen::MatrixBase<D> & w) { data.template segment<3> (ANGULAR)=w; }
184  template<typename D>
185  void linear_impl(const Eigen::MatrixBase<D> & v) { data.template segment<3> (LINEAR)=v; }
186 
187  bool isEqual (const MotionTpl & other) const { return data == other.data; }
188 
189  // Arithmetic operators
190  template<typename S2, int O2>
191  MotionTpl & operator= (const MotionTpl<S2,O2> & other)
192  {
193  data = other.toVector();
194  return *this;
195  }
196 
197  template<typename V6>
198  MotionTpl & operator=(const Eigen::MatrixBase<V6> & v)
199  {
200  EIGEN_STATIC_ASSERT_VECTOR_ONLY(V6); assert(v.size() == 6);
201  data = v;
202  return *this;
203  }
204 
205  MotionTpl __minus__() const { return MotionTpl(-data); }
206  MotionTpl __plus__(const MotionTpl & v2) const { return MotionTpl(data + v2.data); }
207  MotionTpl __minus__(const MotionTpl & v2) const { return MotionTpl(data - v2.data); }
208  MotionTpl& __pequ__(const MotionTpl & v2) { data += v2.data; return *this; }
209  MotionTpl& __mequ__(const MotionTpl & v2) { data -= v2.data; return *this; }
210  MotionTpl __mult__(const Scalar alpha) const { return MotionTpl(alpha*data); }
211 
212  Scalar dot(const Force & f) const { return data.dot(f.toVector()); }
213 
214  MotionTpl cross(const MotionTpl& v2) const
215  {
216  return MotionTpl( linear_impl().cross(v2.angular_impl())+angular_impl().cross(v2.linear_impl()),
217  angular_impl().cross(v2.angular_impl()) );
218  }
219 
220  Force cross(const Force& phi) const
221  {
222  return Force( angular_impl().cross(phi.linear_impl()),
223  angular_impl().cross(phi.angular_impl())+linear_impl().cross(phi.linear_impl()) );
224  }
225 
226  bool isApprox_impl (const MotionTpl & m2, const Scalar & prec = Eigen::NumTraits<Scalar>::dummy_precision()) const
227  {
228  return data.isApprox(m2.data, prec);
229  }
230 
231  MotionTpl se3Action_impl(const SE3 & m) const
232  {
233  Vector3 Rw (m.rotation()*angular_impl());
234  return MotionTpl( m.rotation()*linear_impl() + m.translation().cross(Rw),
235  Rw);
236  }
238  MotionTpl se3ActionInverse_impl(const SE3 & m) const
239  {
240  return MotionTpl( m.rotation().transpose()*(linear_impl()-m.translation().cross(angular_impl())),
241  m.rotation().transpose()*angular_impl());
242  }
243 
244  void disp_impl(std::ostream & os) const
245  {
246  os << " v = " << linear_impl().transpose () << std::endl
247  << " w = " << angular_impl().transpose () << std::endl;
248  }
249 
250 // /** \brief Compute the classical acceleration of point according to the spatial velocity and spatial acceleration of the frame centered on this point
251 // */
252 // static inline Vector3 computeLinearClassicalAcceleration (const MotionTpl & spatial_velocity, const MotionTpl & spatial_acceleration)
253 // {
254 // return spatial_acceleration.linear () + spatial_velocity.angular ().cross (spatial_velocity.linear ());
255 // }
256 //
257 // /**
258 // \brief Compute the spatial motion quantity of the parallel frame translated by translation_vector
259 // */
260 // MotionTpl translate (const Vector3 & translation_vector) const
261 // {
262 // return MotionTpl (linear() + angular().cross (translation_vector), angular());
263 // }
264 
265  protected:
266  Vector6 data;
267 
268  }; // class MotionTpl
269 
270  template<typename S,int O>
271  MotionTpl<S,O> operator^( const MotionTpl<S,O> &m1, const MotionTpl<S,O> &m2 ) { return m1.cross(m2); }
272  template<typename S,int O>
273  ForceTpl<S,O> operator^( const MotionTpl<S,O> &m, const ForceTpl<S,O> &f ) { return m.cross(f); }
274 
275  template<typename S,int O>
276  MotionTpl<S,O> operator*(const S alpha, const MotionTpl<S,O> & m )
277  { return m*alpha; }
278 
279 
281  struct BiasZero;
282 
283  template<>
284  struct traits< BiasZero >
285  {
286  typedef double Scalar;
287  typedef Eigen::Matrix<double,3,1,0> Vector3;
288  typedef Eigen::Matrix<double,4,1,0> Vector4;
289  typedef Eigen::Matrix<double,6,1,0> Vector6;
290  typedef Eigen::Matrix<double,3,3,0> Matrix3;
291  typedef Eigen::Matrix<double,4,4,0> Matrix4;
292  typedef Eigen::Matrix<double,6,6,0> Matrix6;
293  typedef Matrix6 ActionMatrix_t;
294  typedef Vector3 Angular_t;
295  typedef const Vector3 ConstAngular_t;
296  typedef Vector3 Linear_t;
297  typedef const Vector3 ConstLinear_t;
298  typedef Eigen::Quaternion<double,0> Quaternion_t;
299  typedef SE3Tpl<double,0> SE3;
300  typedef ForceTpl<double,0> Force;
301  typedef MotionTpl<double,0> Motion;
303  enum {
304  LINEAR = 0,
305  ANGULAR = 3
306  };
307  }; // traits BiasZero
308 
309  struct BiasZero : public MotionBase< BiasZero >
310  {
311  SPATIAL_TYPEDEF_NO_TEMPLATE(BiasZero);
312  operator Motion () const { return Motion::Zero(); }
313  }; // struct BiasZero
314 
315 inline const Motion & operator+( const Motion& v, const BiasZero&) { return v; }
316 inline const Motion & operator+ ( const BiasZero&,const Motion& v) { return v; }
317 
318 } // namespace se3
319 
320 #endif // ifndef __se3_motion_hpp__
Concreate Class representing a force.
Definition: force.hpp:243
ConstLinear_t linear_impl() const
Return the linear part of the force vector.
Definition: force.hpp:342
const Vector6 & toVector() const
Return the force.
Definition: force.hpp:102
MotionTpl se3ActionInverse_impl(const SE3 &m) const
bv = aXb.actInv(av)
Definition: motion.hpp:238
Vector6 data
Compute the classical acceleration of point according to the spatial velocity and spatial acceleratio...
Definition: motion.hpp:266
ConstAngular_t angular_impl() const
Return the angular part of the force vector.
Definition: force.hpp:336