pinocchio  2.1.3
motion-dense.hpp
1 //
2 // Copyright (c) 2017-2018 CNRS
3 //
4 
5 #ifndef __pinocchio_motion_dense_hpp__
6 #define __pinocchio_motion_dense_hpp__
7 
8 #include "pinocchio/spatial/skew.hpp"
9 
10 namespace pinocchio
11 {
12 
13  namespace internal
14  {
15  template<typename Derived>
16  struct SE3GroupAction< MotionDense<Derived> >
17  {
18  typedef typename SE3GroupAction< Derived >::ReturnType ReturnType;
19  };
20 
21  template<typename Derived, typename MotionDerived>
22  struct MotionAlgebraAction< MotionDense<Derived>, MotionDerived >
23  {
24  typedef typename MotionAlgebraAction< Derived, MotionDerived >::ReturnType ReturnType;
25  };
26  }
27 
28  template<typename Derived>
29  class MotionDense : public MotionBase<Derived>
30  {
31  public:
32  typedef MotionBase<Derived> Base;
33  MOTION_TYPEDEF_TPL(Derived);
34  typedef typename traits<Derived>::MotionRefType MotionRefType;
35 
36  using Base::linear;
37  using Base::angular;
38  using Base::derived;
39  using Base::isApprox;
40 
41  Derived & setZero() { linear().setZero(); angular().setZero(); return derived(); }
42  Derived & setRandom() { linear().setRandom(); angular().setRandom(); return derived(); }
43 
44  ActionMatrixType toActionMatrix_impl() const
45  {
46  ActionMatrixType X;
47  X.template block <3,3> (ANGULAR, ANGULAR) = X.template block <3,3> (LINEAR, LINEAR) = skew(angular());
48  X.template block <3,3> (LINEAR, ANGULAR) = skew(linear());
49  X.template block <3,3> (ANGULAR, LINEAR).setZero();
50 
51  return X;
52  }
53 
54  ActionMatrixType toDualActionMatrix_impl() const
55  {
56  ActionMatrixType X;
57  X.template block <3,3> (ANGULAR, ANGULAR) = X.template block <3,3> (LINEAR, LINEAR) = skew(angular());
58  X.template block <3,3> (ANGULAR, LINEAR) = skew(linear());
59  X.template block <3,3> (LINEAR, ANGULAR).setZero();
60 
61  return X;
62  }
63 
64  template<typename D2>
65  bool isEqual_impl(const MotionDense<D2> & other) const
66  { return linear() == other.linear() && angular() == other.angular(); }
67 
68  template<typename D2>
69  bool isEqual_impl(const MotionBase<D2> & other) const
70  { return other.derived() == derived(); }
71 
72  // Arithmetic operators
73  template<typename D2>
74  Derived & operator=(const MotionDense<D2> & other)
75  {
76  linear() = other.linear();
77  angular() = other.angular();
78  return derived();
79  }
80 
81  template<typename D2>
82  Derived & operator=(const MotionBase<D2> & other)
83  {
84  other.derived().setTo(derived());
85  return derived();
86  }
87 
88  template<typename V6>
89  Derived & operator=(const Eigen::MatrixBase<V6> & v)
90  {
91  EIGEN_STATIC_ASSERT_VECTOR_ONLY(V6); assert(v.size() == 6);
92  linear() = v.template segment<3>(LINEAR);
93  angular() = v.template segment<3>(ANGULAR);
94  return derived();
95  }
96 
97  MotionPlain operator-() const { return derived().__opposite__(); }
98  template<typename M1>
99  MotionPlain operator+(const MotionDense<M1> & v) const { return derived().__plus__(v.derived()); }
100  template<typename M1>
101  MotionPlain operator-(const MotionDense<M1> & v) const { return derived().__minus__(v.derived()); }
102 
103  template<typename M1>
104  Derived & operator+=(const MotionDense<M1> & v) { return derived().__pequ__(v.derived()); }
105  template<typename M1>
106  Derived & operator+=(const MotionBase<M1> & v)
107  { v.derived().addTo(derived()); return derived(); }
108 
109  template<typename M1>
110  Derived & operator-=(const MotionDense<M1> & v) { return derived().__mequ__(v.derived()); }
111 
112  MotionPlain __opposite__() const { return MotionPlain(-linear(),-angular()); }
113 
114  template<typename M1>
115  MotionPlain __plus__(const MotionDense<M1> & v) const
116  { return MotionPlain(linear()+v.linear(), angular()+v.angular()); }
117 
118  template<typename M1>
119  MotionPlain __minus__(const MotionDense<M1> & v) const
120  { return MotionPlain(linear()-v.linear(), angular()-v.angular()); }
121 
122  template<typename M1>
123  Derived & __pequ__(const MotionDense<M1> & v)
124  { linear() += v.linear(); angular() += v.angular(); return derived(); }
125 
126  template<typename M1>
127  Derived & __mequ__(const MotionDense<M1> & v)
128  { linear() -= v.linear(); angular() -= v.angular(); return derived(); }
129 
130  template<typename OtherScalar>
131  MotionPlain __mult__(const OtherScalar & alpha) const
132  { return MotionPlain(alpha*linear(),alpha*angular()); }
133 
134  template<typename OtherScalar>
135  MotionPlain __div__(const OtherScalar & alpha) const
136  { return derived().__mult__((OtherScalar)(1)/alpha); }
137 
138  template<typename F1>
139  Scalar dot(const ForceBase<F1> & phi) const
140  { return phi.linear().dot(linear()) + phi.angular().dot(angular()); }
141 
142  template<typename D>
143  typename internal::MotionAlgebraAction<D,Derived>::ReturnType cross_impl(const D & d) const
144  {
145  return d.motionAction(derived());
146  }
147 
148  template<typename M1, typename M2>
149  void motionAction(const MotionDense<M1> & v, MotionDense<M2> & mout) const
150  {
151  mout.linear() = v.linear().cross(angular())+v.angular().cross(linear());
152  mout.angular() = v.angular().cross(angular());
153  }
154 
155  template<typename M1>
156  MotionPlain motionAction(const MotionDense<M1> & v) const
157  {
158  MotionPlain res;
159  motionAction(v,res);
160  return res;
161  }
162 
163  template<typename M2>
164  bool isApprox(const MotionDense<M2> & m2, const Scalar & prec = Eigen::NumTraits<Scalar>::dummy_precision()) const
165  { return derived().isApprox_impl(m2, prec);}
166 
167  template<typename D2>
168  bool isApprox_impl(const MotionDense<D2> & m2, const Scalar & prec = Eigen::NumTraits<Scalar>::dummy_precision()) const
169  {
170  return linear().isApprox(m2.linear(), prec) && angular().isApprox(m2.angular(), prec);
171  }
172 
173  template<typename S2, int O2, typename D2>
174  void se3Action_impl(const SE3Tpl<S2,O2> & m, MotionDense<D2> & v) const
175  {
176  v.angular().noalias() = m.rotation()*angular();
177  v.linear().noalias() = m.rotation()*linear() + m.translation().cross(v.angular());
178  }
179 
180  template<typename S2, int O2>
181  MotionPlain se3Action_impl(const SE3Tpl<S2,O2> & m) const
182  {
183  MotionPlain res;
184  se3Action_impl(m,res);
185  return res;
186  }
187 
188  template<typename S2, int O2, typename D2>
189  EIGEN_STRONG_INLINE
190  void se3ActionInverse_impl(const SE3Tpl<S2,O2> & m, MotionDense<D2> & v) const
191  {
192  v.linear().noalias() = m.rotation().transpose()*(linear()-m.translation().cross(angular()));
193  v.angular().noalias() = m.rotation().transpose()*angular();
194  }
195 
196  template<typename S2, int O2>
197  MotionPlain se3ActionInverse_impl(const SE3Tpl<S2,O2> & m) const
198  {
199  MotionPlain res;
200  se3ActionInverse_impl(m,res);
201  return res;
202  }
203 
204  void disp_impl(std::ostream & os) const
205  {
206  os
207  << " v = " << linear().transpose () << std::endl
208  << " w = " << angular().transpose () << std::endl;
209  }
210 
212  MotionRefType ref() { return derived().ref(); }
213 
214  }; // class MotionDense
215 
217  template<typename M1, typename M2>
218  typename traits<M1>::MotionPlain operator^(const MotionDense<M1> & v1,
219  const MotionDense<M2> & v2)
220  { return v1.derived().cross(v2.derived()); }
221 
222  template<typename M1, typename F1>
223  typename traits<F1>::ForcePlain operator^(const MotionDense<M1> & v,
224  const ForceBase<F1> & f)
225  { return v.derived().cross(f.derived()); }
226 
227  template<typename M1>
228  typename traits<M1>::MotionPlain operator*(const typename traits<M1>::Scalar alpha,
229  const MotionDense<M1> & v)
230  { return v*alpha; }
231 
232 } // namespace pinocchio
233 
234 #endif // ifndef __pinocchio_motion_dense_hpp__
Base interface for forces representation.
Definition: force-base.hpp:27
Main pinocchio namespace.
Definition: treeview.dox:24
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