pinocchio  3.3.0
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
motion-tpl.hpp
1 //
2 // Copyright (c) 2015-2018 CNRS
3 // Copyright (c) 2015-2016 Wandercraft, 86 rue de Paris 91400 Orsay, France.
4 //
5 
6 #ifndef __pinocchio_spatial_motion_tpl_hpp__
7 #define __pinocchio_spatial_motion_tpl_hpp__
8 
9 namespace pinocchio
10 {
11  template<typename _Scalar, int _Options>
12  struct traits<MotionTpl<_Scalar, _Options>>
13  {
14  typedef _Scalar Scalar;
15  typedef Eigen::Matrix<Scalar, 3, 1, _Options> Vector3;
16  typedef Eigen::Matrix<Scalar, 6, 1, _Options> Vector6;
17  typedef Eigen::Matrix<Scalar, 4, 4, _Options> Matrix4;
18  typedef Eigen::Matrix<Scalar, 6, 6, _Options> Matrix6;
19  typedef Matrix6 ActionMatrixType;
20  typedef Matrix4 HomogeneousMatrixType;
21  typedef typename PINOCCHIO_EIGEN_REF_CONST_TYPE(Vector6) ToVectorConstReturnType;
22  typedef typename PINOCCHIO_EIGEN_REF_TYPE(Vector6) ToVectorReturnType;
23  typedef typename Vector6::template FixedSegmentReturnType<3>::Type LinearType;
24  typedef typename Vector6::template FixedSegmentReturnType<3>::Type AngularType;
25  typedef typename Vector6::template ConstFixedSegmentReturnType<3>::Type ConstLinearType;
26  typedef typename Vector6::template ConstFixedSegmentReturnType<3>::Type ConstAngularType;
28  typedef const MotionPlain & PlainReturnType;
29  enum
30  {
31  LINEAR = 0,
32  ANGULAR = 3,
33  Options = _Options
34  };
35 
37  }; // traits MotionTpl
38 
39  template<typename _Scalar, int _Options>
40  class MotionTpl : public MotionDense<MotionTpl<_Scalar, _Options>>
41  {
42  public:
43  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
45  MOTION_TYPEDEF_TPL(MotionTpl);
46  enum
47  {
48  Options = _Options
49  };
50 
51  using Base::operator=;
52  using Base::angular;
53  using Base::linear;
54 
55  using Base::__mequ__;
56  using Base::__minus__;
57  using Base::__mult__;
58  using Base::__opposite__;
59  using Base::__pequ__;
60  using Base::__plus__;
61 
62  // Constructors
63  MotionTpl()
64  {
65  }
66 
67  template<typename V1, typename V2>
68  MotionTpl(const Eigen::MatrixBase<V1> & v, const Eigen::MatrixBase<V2> & w)
69  {
70  EIGEN_STATIC_ASSERT_VECTOR_ONLY(V1);
71  EIGEN_STATIC_ASSERT_VECTOR_ONLY(V2);
72  linear() = v;
73  angular() = w;
74  }
75 
76  template<typename V6>
77  explicit MotionTpl(const Eigen::MatrixBase<V6> & v)
78  : m_data(v)
79  {
80  EIGEN_STATIC_ASSERT_VECTOR_ONLY(V6);
81  }
82 
83  MotionTpl(const MotionTpl & other)
84  {
85  *this = other;
86  }
87 
88  template<typename S2, int O2>
89  explicit MotionTpl(const MotionTpl<S2, O2> & other)
90  {
91  *this = other.template cast<Scalar>();
92  }
93 
94  template<int O2>
95  explicit MotionTpl(const MotionTpl<Scalar, O2> & clone)
96  : m_data(clone.toVector())
97  {
98  }
99 
100  // Same explanation as converting constructor from MotionBase
101  template<
102  typename M2,
103  typename std::enable_if<!std::is_convertible<MotionDense<M2>, MotionTpl>::value, bool>::type =
104  true>
105  explicit MotionTpl(const MotionDense<M2> & clone)
106  {
107  linear() = clone.linear();
108  angular() = clone.angular();
109  }
110 
111  // MotionBase implement a conversion function to PlainReturnType.
112  // Usually, PlainReturnType is defined as MotionTpl.
113  // In this case, this converting constructor is redundant and
114  // create a warning with -Wconversion
115  template<
116  typename M2,
117  typename std::enable_if<!std::is_convertible<MotionBase<M2>, MotionTpl>::value, bool>::type =
118  true>
119  explicit MotionTpl(const MotionBase<M2> & clone)
120  {
121  *this = clone;
122  }
123 
129  MotionTpl & operator=(const MotionTpl & clone) // Copy assignment operator
130  {
131  m_data = clone.toVector();
132  return *this;
133  }
134 
135  // initializers
136  static MotionTpl Zero()
137  {
138  return MotionTpl(Vector6::Zero());
139  }
140  static MotionTpl Random()
141  {
142  return MotionTpl(Vector6::Random());
143  }
144 
145  inline PlainReturnType plain() const
146  {
147  return *this;
148  }
149 
150  ToVectorConstReturnType toVector_impl() const
151  {
152  return m_data;
153  }
154  ToVectorReturnType toVector_impl()
155  {
156  return m_data;
157  }
158 
159  // Getters
160  ConstAngularType angular_impl() const
161  {
162  return m_data.template segment<3>(ANGULAR);
163  }
164  ConstLinearType linear_impl() const
165  {
166  return m_data.template segment<3>(LINEAR);
167  }
168  AngularType angular_impl()
169  {
170  return m_data.template segment<3>(ANGULAR);
171  }
172  LinearType linear_impl()
173  {
174  return m_data.template segment<3>(LINEAR);
175  }
176 
177  template<typename V3>
178  void angular_impl(const Eigen::MatrixBase<V3> & w)
179  {
180  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(V3, 3);
181  angular_impl() = w;
182  }
183  template<typename V3>
184  void linear_impl(const Eigen::MatrixBase<V3> & v)
185  {
186  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(V3, 3);
187  linear_impl() = v;
188  }
189 
190  // Specific operators for MotionTpl and MotionRef
191  template<int O2>
192  MotionPlain __plus__(const MotionTpl<Scalar, O2> & v) const
193  {
194  return MotionPlain(m_data + v.toVector());
195  }
196 
197  template<typename Vector6ArgType>
198  MotionPlain __plus__(const MotionRef<Vector6ArgType> & v) const
199  {
200  return MotionPlain(m_data + v.toVector());
201  }
202 
203  template<int O2>
204  MotionPlain __minus__(const MotionTpl<Scalar, O2> & v) const
205  {
206  return MotionPlain(m_data - v.toVector());
207  }
208 
209  template<typename Vector6ArgType>
210  MotionPlain __minus__(const MotionRef<Vector6ArgType> & v) const
211  {
212  return MotionPlain(m_data - v.toVector());
213  }
214 
215  template<int O2>
216  MotionTpl & __pequ__(const MotionTpl<Scalar, O2> & v)
217  {
218  m_data += v.toVector();
219  return *this;
220  }
221 
222  template<typename Vector6ArgType>
223  MotionTpl & __pequ__(const MotionRef<Vector6ArgType> & v)
224  {
225  m_data += v.toVector();
226  return *this;
227  }
228 
229  template<int O2>
230  MotionTpl & __mequ__(const MotionTpl<Scalar, O2> & v)
231  {
232  m_data -= v.toVector();
233  return *this;
234  }
235 
236  template<typename Vector6ArgType>
237  MotionTpl & __mequ__(const MotionRef<Vector6ArgType> & v)
238  {
239  m_data -= v.toVector();
240  return *this;
241  }
242 
243  template<typename OtherScalar>
244  MotionPlain __mult__(const OtherScalar & alpha) const
245  {
246  return MotionPlain(alpha * m_data);
247  }
248 
249  MotionRef<Vector6> ref()
250  {
251  return MotionRef<Vector6>(m_data);
252  }
253 
255  template<typename NewScalar>
257  {
258  typedef MotionTpl<NewScalar, Options> ReturnType;
259  ReturnType res(linear().template cast<NewScalar>(), angular().template cast<NewScalar>());
260  return res;
261  }
262 
263  protected:
264  Vector6 m_data;
265 
266  }; // class MotionTpl
267 
268 } // namespace pinocchio
269 
270 #endif // ifndef __pinocchio_spatial_motion_tpl_hpp__
MotionTpl & operator=(const MotionTpl &clone)
Copy assignment operator.
Definition: motion-tpl.hpp:129
MotionTpl< NewScalar, Options > cast() const
Definition: motion-tpl.hpp:256
Main pinocchio namespace.
Definition: treeview.dox:11
Common traits structure to fully define base classes for CRTP.
Definition: fwd.hpp:72