pinocchio  1.2.6-7-g6de3e-dirty
force.hpp
1 //
2 // Copyright (c) 2015 CNRS
3 // Copyright (c) 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_force_hpp__
20 #define __se3_force_hpp__
21 
22 #include <Eigen/Core>
23 #include "pinocchio/spatial/fwd.hpp"
24 #include "pinocchio/macros.hpp"
25 #include "pinocchio/spatial/se3.hpp"
26 
35 namespace se3
36 {
37 
38 
46  template< class Derived>
47  class ForceBase
48  {
49  protected:
50 
51  typedef Derived Derived_t;
52  SPATIAL_TYPEDEF_TEMPLATE(Derived_t);
53 
54  public:
55  Derived_t & derived() { return *static_cast<Derived_t*>(this); }
56  const Derived_t& derived() const { return *static_cast<const Derived_t*>(this); }
57 
58 
64  ConstAngular_t angular() const { return derived().angular_impl(); }
65 
71  ConstLinear_t linear() const { return derived().linear_impl(); }
72 
74  Angular_t angular() { return derived().angular_impl(); }
75 
77  Linear_t linear() { return derived().linear_impl(); }
78 
79 
85  void angular(const Vector3 & n) { derived().angular_impl(n); }
86 
92  void linear(const Vector3 & f) { derived().linear_impl(f); }
93 
102  const Vector6 & toVector() const { return derived().toVector_impl(); }
103 
105  Vector6 & toVector() { return derived().toVector_impl(); }
106 
107  /*
108  * @brief C-style cast operator
109  * \copydoc ForceBase::toVector
110  */
111  operator Vector6 () const { return toVector(); }
112 
113 // void disp(std::ostream & os) const
114 // {
115 // static_cast<const Derived_t*>(this)->disp_impl(os);
116 // }
117 
118 
123  bool operator== (const Derived_t & other) const {return derived().isEqual(other);}
124 
127  bool operator!=(const Derived_t & other) const { return !(*this == other); }
128 
131  bool isApprox(const Derived & other, const Scalar & prec = Eigen::NumTraits<Scalar>::dummy_precision()) const
132  { return derived().isApprox_impl(other, prec); }
133 
137  Derived_t & operator= (const Derived_t & other) { return derived().__equl__(other); }
138 
143  Derived_t & operator+= (const Derived_t & phi) { return derived().__pequ__(phi); }
144 
149  Derived_t & operator-= (const Derived_t & phi) { return derived().__mequ__(phi); }
150 
153  Derived_t operator+(const Derived_t & phi) const { return derived().__plus__(phi); }
154 
157  Derived_t operator*(double a) const { return derived().__mult__(a); }
158 
161  Derived_t operator-() const { return derived().__minus__(); }
162 
165  Derived_t operator-(const Derived_t & phi) const { return derived().__minus__(phi); }
166 
169  Scalar dot(const Motion & m) const { return static_cast<Derived_t*>(this)->dot(m); }
170 
171 
183  Derived_t se3Action(const SE3 & m) const { return derived().se3Action_impl(m); }
184 
196  Derived_t se3ActionInverse(const SE3 & m) const { return derived().se3ActionInverse_impl(m); }
197 
198  void disp(std::ostream & os) const { derived().disp_impl(os); }
199  friend std::ostream & operator << (std::ostream & os, const ForceBase<Derived_t> & X)
200  {
201  X.disp(os);
202  return os;
203  }
204 
205  }; // class ForceBase
206 
207 
208  template<typename T, int U>
209  struct traits< ForceTpl<T, U> >
210  {
211  typedef T Scalar;
212  typedef Eigen::Matrix<T,3,1,U> Vector3;
213  typedef Eigen::Matrix<T,4,1,U> Vector4;
214  typedef Eigen::Matrix<T,6,1,U> Vector6;
215  typedef Eigen::Matrix<T,3,3,U> Matrix3;
216  typedef Eigen::Matrix<T,4,4,U> Matrix4;
217  typedef Eigen::Matrix<T,6,6,U> Matrix6;
218  typedef typename Vector6::template FixedSegmentReturnType<3>::Type Linear_t;
219  typedef typename Vector6::template FixedSegmentReturnType<3>::Type Angular_t;
220  typedef typename Vector6::template ConstFixedSegmentReturnType<3>::Type ConstLinear_t;
221  typedef typename Vector6::template ConstFixedSegmentReturnType<3>::Type ConstAngular_t;
222  typedef Matrix6 ActionMatrix_t;
223  typedef Eigen::Quaternion<T,U> Quaternion_t;
224  typedef SE3Tpl<T,U> SE3;
225  typedef ForceTpl<T,U> Force;
226  typedef MotionTpl<T,U> Motion;
228  enum {
229  LINEAR = 0,
230  ANGULAR = 3
231  };
232  }; // traits ForceTpl
233 
234 
242  template<typename _Scalar, int _Options>
243  class ForceTpl : public ForceBase< ForceTpl< _Scalar, _Options > >
244  {
245 
246  public:
247  friend class ForceBase< ForceTpl< _Scalar, _Options > >;
248  SPATIAL_TYPEDEF_TEMPLATE(ForceTpl);
249  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
250 
251  ForceTpl() : data() {}
252 
253  template<typename F3,typename N3>
254  ForceTpl(const Eigen::MatrixBase<F3> & f,const Eigen::MatrixBase<N3> & n)
255  {
256  EIGEN_STATIC_ASSERT_VECTOR_ONLY(F3);
257  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(F3,3);
258  EIGEN_STATIC_ASSERT_VECTOR_ONLY(N3);
259  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(N3,3);
260  data << f, n;
261  }
262 
263  template<typename F6>
264  explicit ForceTpl(const Eigen::MatrixBase<F6> & f)
265  : data(f)
266  {
267  EIGEN_STATIC_ASSERT_VECTOR_ONLY(F6);
268  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(F6,6);
269  }
270 
271  template<typename S2,int O2>
272  explicit ForceTpl(const ForceTpl<S2,O2> & clone)
273  : data(clone.toVector())
274  {}
275 
276  static ForceTpl Zero() { return ForceTpl(Linear_t::Zero(), Angular_t::Zero()); }
277  static ForceTpl Random() { return ForceTpl(Linear_t::Random(), Angular_t::Random()); }
278 
279  ForceTpl & setZero () { data.setZero (); return *this; }
280  ForceTpl & setRandom () { data.setRandom (); return *this; }
281 
282  const Vector6 & toVector_impl() const { return data; }
283  Vector6 & toVector_impl() { return data; }
284 
285  void disp_impl(std::ostream & os) const
286  {
287  os << " f = " << linear_impl().transpose() << std::endl
288  << " tau = " << angular_impl().transpose() << std::endl;
289  }
290 
292  ForceTpl se3Action_impl(const SE3 & m) const
293  {
294  Vector3 Rf (m.rotation()*linear_impl());
295  return ForceTpl(Rf,m.translation().cross(Rf)+m.rotation()*angular_impl());
296  }
297 
298 
299  ForceTpl se3ActionInverse_impl(const SE3 & m) const
300  {
301  return ForceTpl(m.rotation().transpose()*linear_impl(),
302  m.rotation().transpose()*(angular_impl() - m.translation().cross(linear_impl())) );
303  }
304 
305  bool isEqual (const ForceTpl & other) const { return data == other.data; }
306 
307  // Arithmetic operators
308  template<typename S2, int O2>
309  ForceTpl & operator= (const ForceTpl<S2,O2> & other)
310  {
311  data = other.toVector();
312  return *this;
313  }
314 
315  template<typename F6>
316  ForceTpl & operator= (const Eigen::MatrixBase<F6> & phi)
317  {
318  EIGEN_STATIC_ASSERT_VECTOR_ONLY(F6);
319  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(F6,6);
320  data = phi;
321  return *this;
322  }
323 
324  ForceTpl & __equl__(const ForceTpl & other) { data = other.data; return *this; }
325  ForceTpl & __pequ__ (const ForceTpl & phi) { data += phi.data; return *this; }
326  ForceTpl & __mequ__ (const ForceTpl & phi) { data -= phi.data; return *this; }
327  ForceTpl __plus__(const ForceTpl & phi) const { return ForceTpl(data + phi.data); }
328  ForceTpl __mult__(const double a) const { return ForceTpl(a*data); }
329  ForceTpl __minus__() const { return ForceTpl(-data); }
330  ForceTpl __minus__(const ForceTpl & phi) const { return ForceTpl(data - phi.data); }
331 
332  bool isApprox_impl(const ForceTpl & other, const Scalar & prec = Eigen::NumTraits<Scalar>::dummy_precision()) const
333  { return data.isApprox(other.data, prec); }
334 
336  ConstAngular_t angular_impl() const { return data.template segment<3> (ANGULAR); }
338  Angular_t angular_impl() { return data.template segment<3> (ANGULAR); }
340  void angular_impl(const Vector3 & n) { data.template segment<3> (ANGULAR) = n; }
342  ConstLinear_t linear_impl() const { return data.template segment<3> (LINEAR);}
344  Linear_t linear_impl() { return data.template segment<3> (LINEAR);}
346  void linear_impl(const Vector3 & f) { data.template segment<3> (LINEAR) = f; }
347 
348  Scalar dot(const Motion & m) const { return data.dot(m.toVector()); }
349 
350  protected:
351  Vector6 data;
352 
353  }; // class ForceTpl
354 
355 } // namespace se3
356 
357 #endif // ifndef __se3_force_hpp__
358 
Derived_t se3Action(const SE3 &m) const
Transform from A to B coordinates the Force represented by *this such that .
Definition: force.hpp:183
Linear_t linear()
Return the linear part of the force vector.
Definition: force.hpp:77
Derived_t operator-() const
Definition: force.hpp:161
Vector6 & toVector()
Return the force.
Definition: force.hpp:105
Derived_t operator*(double a) const
Definition: force.hpp:157
void linear_impl(const Vector3 &f)
Set the linear part of the force vector.
Definition: force.hpp:346
Angular_t angular()
Return the angular part of the force vector.
Definition: force.hpp:74
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
void angular_impl(const Vector3 &n)
Set the angular part of the force vector.
Definition: force.hpp:340
Derived_t & operator+=(const Derived_t &phi)
Replaces *this by *this + other.
Definition: force.hpp:143
Derived_t & operator=(const Derived_t &other)
Copies the Derived Force into *this.
Definition: force.hpp:137
ConstAngular_t angular() const
Return the angular part of the force vector.
Definition: force.hpp:64
Derived_t operator-(const Derived_t &phi) const
Definition: force.hpp:165
ForceTpl se3Action_impl(const SE3 &m) const
af = aXb.act(bf)
Definition: force.hpp:292
ConstAngular_t angular_impl() const
Return the angular part of the force vector.
Definition: force.hpp:336
void linear(const Vector3 &f)
Set the linear part of the force vector.
Definition: force.hpp:92
Derived_t operator+(const Derived_t &phi) const
Definition: force.hpp:153
Linear_t linear_impl()
Return the linear part of the force vector.
Definition: force.hpp:344
bool operator==(const Derived_t &other) const
Definition: force.hpp:123
bool isApprox(const Derived &other, const Scalar &prec=Eigen::NumTraits< Scalar >::dummy_precision()) const
Definition: force.hpp:131
Base interface for forces representation.
Definition: force.hpp:47
Derived_t se3ActionInverse(const SE3 &m) const
Transform from B to A coordinates the Force represented by *this such that .
Definition: force.hpp:196
Derived_t & operator-=(const Derived_t &phi)
Replaces *this by *this - other.
Definition: force.hpp:149
Scalar dot(const Motion &m) const
Definition: force.hpp:169
ConstLinear_t linear() const
Return the linear part of the force vector.
Definition: force.hpp:71
void angular(const Vector3 &n)
Set the angular part of the force vector.
Definition: force.hpp:85
Angular_t angular_impl()
Return the angular part of the force vector.
Definition: force.hpp:338
bool operator!=(const Derived_t &other) const
Definition: force.hpp:127