pinocchio  2.1.3
force-dense.hpp
1 //
2 // Copyright (c) 2017-2018 CNRS
3 //
4 
5 #ifndef __pinocchio_force_dense_hpp__
6 #define __pinocchio_force_dense_hpp__
7 
8 namespace pinocchio
9 {
10 
11  namespace internal
12  {
13  template<typename Derived>
14  struct SE3GroupAction< ForceDense<Derived> >
15  {
16  typedef typename SE3GroupAction< Derived >::ReturnType ReturnType;
17  };
18 
19  template<typename Derived, typename MotionDerived>
20  struct MotionAlgebraAction< ForceDense<Derived>, MotionDerived >
21  {
22  typedef typename MotionAlgebraAction< Derived, MotionDerived >::ReturnType ReturnType;
23  };
24  }
25 
26  template<typename Derived>
27  class ForceDense : public ForceBase<Derived>
28  {
29  public:
30  typedef ForceBase<Derived> Base;
31  FORCE_TYPEDEF_TPL(Derived);
32  typedef typename traits<Derived>::ForceRefType ForceRefType;
33 
34  using Base::linear;
35  using Base::angular;
36  using Base::derived;
37  using Base::isApprox;
38  using Base::operator=;
39 
40  Derived & setZero() { linear().setZero(); angular().setZero(); return derived(); }
41  Derived & setRandom() { linear().setRandom(); angular().setRandom(); return derived(); }
42 
43  template<typename D2>
44  bool isEqual_impl(const ForceDense<D2> & other) const
45  { return linear() == other.linear() && angular() == other.angular(); }
46 
47  template<typename D2>
48  bool isEqual_impl(const ForceBase<D2> & other) const
49  { return other.derived() == derived(); }
50 
51  // Arithmetic operators
52  template<typename D2>
53  Derived & setFrom(const ForceDense<D2> & other)
54  {
55  linear() = other.linear();
56  angular() = other.angular();
57  return derived();
58  }
59 
60  template<typename D2>
61  Derived & operator=(const ForceDense<D2> & other)
62  {
63  return derived().setFrom(other.derived());
64  }
65 
66  template<typename V6>
67  Derived & operator=(const Eigen::MatrixBase<V6> & v)
68  {
69  EIGEN_STATIC_ASSERT_VECTOR_ONLY(V6); assert(v.size() == 6);
70  linear() = v.template segment<3>(LINEAR);
71  angular() = v.template segment<3>(ANGULAR);
72  return derived();
73  }
74 
75  ForcePlain operator-() const { return derived().__opposite__(); }
76  template<typename F1>
77  ForcePlain operator+(const ForceDense<F1> & f) const { return derived().__plus__(f.derived()); }
78  template<typename F1>
79  ForcePlain operator-(const ForceDense<F1> & f) const { return derived().__minus__(f.derived()); }
80 
81  template<typename F1>
82  Derived & operator+=(const ForceDense<F1> & f) { return derived().__pequ__(f.derived()); }
83  template<typename F1>
84  Derived & operator+=(const ForceBase<F1> & f)
85  { f.derived().addTo(derived()); return derived(); }
86 
87  template<typename M1>
88  Derived & operator-=(const ForceDense<M1> & v) { return derived().__mequ__(v.derived()); }
89 
90  ForcePlain __opposite__() const { return ForcePlain(-linear(),-angular()); }
91 
92  template<typename M1>
93  ForcePlain __plus__(const ForceDense<M1> & v) const
94  { return ForcePlain(linear()+v.linear(), angular()+v.angular()); }
95 
96  template<typename M1>
97  ForcePlain __minus__(const ForceDense<M1> & v) const
98  { return ForcePlain(linear()-v.linear(), angular()-v.angular()); }
99 
100  template<typename M1>
101  Derived & __pequ__(const ForceDense<M1> & v)
102  { linear() += v.linear(); angular() += v.angular(); return derived(); }
103 
104  template<typename M1>
105  Derived & __mequ__(const ForceDense<M1> & v)
106  { linear() -= v.linear(); angular() -= v.angular(); return derived(); }
107 
108  template<typename OtherScalar>
109  ForcePlain __mult__(const OtherScalar & alpha) const
110  { return ForcePlain(alpha*linear(),alpha*angular()); }
111 
112  template<typename OtherScalar>
113  ForcePlain __div__(const OtherScalar & alpha) const
114  { return derived().__mult__((OtherScalar)(1)/alpha); }
115 
116  template<typename F1>
117  Scalar dot(const MotionDense<F1> & phi) const
118  { return phi.linear().dot(linear()) + phi.angular().dot(angular()); }
119 
120  template<typename M1, typename M2>
121  void motionAction(const MotionDense<M1> & v, ForceDense<M2> & fout) const
122  {
123  fout.linear().noalias() = v.angular().cross(linear());
124  fout.angular().noalias() = v.angular().cross(angular())+v.linear().cross(linear());
125  }
126 
127  template<typename M1>
128  ForcePlain motionAction(const MotionDense<M1> & v) const
129  {
130  ForcePlain res;
131  motionAction(v,res);
132  return res;
133  }
134 
135  template<typename M2>
136  bool isApprox(const ForceDense<M2> & f, const Scalar & prec = Eigen::NumTraits<Scalar>::dummy_precision()) const
137  { return derived().isApprox_impl(f, prec);}
138 
139  template<typename D2>
140  bool isApprox_impl(const ForceDense<D2> & f, const Scalar & prec = Eigen::NumTraits<Scalar>::dummy_precision()) const
141  {
142  return linear().isApprox(f.linear(), prec) && angular().isApprox(f.angular(), prec);
143  }
144 
145  template<typename S2, int O2, typename D2>
146  void se3Action_impl(const SE3Tpl<S2,O2> & m, ForceDense<D2> & f) const
147  {
148  f.linear().noalias() = m.rotation()*linear();
149  f.angular().noalias() = m.rotation()*angular();
150  f.angular() += m.translation().cross(f.linear());
151  }
152 
153  template<typename S2, int O2>
154  ForcePlain se3Action_impl(const SE3Tpl<S2,O2> & m) const
155  {
156  ForcePlain res;
157  se3Action_impl(m,res);
158  return res;
159  }
160 
161  template<typename S2, int O2, typename D2>
162  void se3ActionInverse_impl(const SE3Tpl<S2,O2> & m, ForceDense<D2> & f) const
163  {
164  f.linear().noalias() = m.rotation().transpose()*linear();
165  f.angular().noalias() = m.rotation().transpose()*(angular()-m.translation().cross(linear()));
166  }
167 
168  template<typename S2, int O2>
169  ForcePlain se3ActionInverse_impl(const SE3Tpl<S2,O2> & m) const
170  {
171  ForcePlain res;
172  se3ActionInverse_impl(m,res);
173  return res;
174  }
175 
176  void disp_impl(std::ostream & os) const
177  {
178  os
179  << " f = " << linear().transpose () << std::endl
180  << "tau = " << angular().transpose () << std::endl;
181  }
182 
184  ForceRefType ref() { return derived().ref(); }
185 
186  }; // class ForceDense
187 
189  template<typename F1>
190  typename traits<F1>::ForcePlain operator*(const typename traits<F1>::Scalar alpha,
191  const ForceDense<F1> & f)
192  { return f.derived()*alpha; }
193 
194 } // namespace pinocchio
195 
196 #endif // ifndef __pinocchio_force_dense_hpp__
ConstAngularType angular() const
Return the angular part of the force vector.
Definition: force-base.hpp:40
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
ConstLinearType linear() const
Return the linear part of the force vector.
Definition: force-base.hpp:47
ForceRefType ref()