pinocchio  1.2.6-7-g6de3e-dirty
joint-base.hpp
1 //
2 // Copyright (c) 2015-2016 CNRS
3 // Copyright (c) 2015 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_joint_base_hpp__
20 #define __se3_joint_base_hpp__
21 
22 #include "pinocchio/multibody/fwd.hpp"
23 #include "pinocchio/multibody/joint/fwd.hpp"
24 
25 #include <Eigen/Core>
26 #include <limits>
27 
28 namespace se3
29 {
30 #ifdef __clang__
31 
32 #define SE3_JOINT_TYPEDEF_ARG(prefix) \
33  typedef int Index; \
34  typedef prefix traits<JointDerived>::Scalar Scalar; \
35  typedef prefix traits<JointDerived>::JointDataDerived JointDataDerived; \
36  typedef prefix traits<JointDerived>::JointModelDerived JointModelDerived; \
37  typedef prefix traits<JointDerived>::Constraint_t Constraint_t; \
38  typedef prefix traits<JointDerived>::Transformation_t Transformation_t; \
39  typedef prefix traits<JointDerived>::Motion_t Motion_t; \
40  typedef prefix traits<JointDerived>::Bias_t Bias_t; \
41  typedef prefix traits<JointDerived>::F_t F_t; \
42  typedef prefix traits<JointDerived>::U_t U_t; \
43  typedef prefix traits<JointDerived>::D_t D_t; \
44  typedef prefix traits<JointDerived>::UD_t UD_t; \
45  enum { \
46  NQ = traits<JointDerived>::NQ, \
47  NV = traits<JointDerived>::NV \
48  }; \
49  typedef prefix traits<JointDerived>::ConfigVector_t ConfigVector_t; \
50  typedef prefix traits<JointDerived>::TangentVector_t TangentVector_t
51 
52 #define SE3_JOINT_TYPEDEF SE3_JOINT_TYPEDEF_ARG()
53 #define SE3_JOINT_TYPEDEF_TEMPLATE SE3_JOINT_TYPEDEF_ARG(typename)
54 
55 #elif (__GNUC__ == 4) && (__GNUC_MINOR__ == 4) && (__GNUC_PATCHLEVEL__ == 2)
56 
57 #define SE3_JOINT_TYPEDEF_NOARG() \
58  typedef int Index; \
59  typedef traits<JointDerived>::Scalar Scalar; \
60  typedef traits<JointDerived>::JointDataDerived JointDataDerived; \
61  typedef traits<JointDerived>::JointModelDerived JointModelDerived; \
62  typedef traits<JointDerived>::Constraint_t Constraint_t; \
63  typedef traits<JointDerived>::Transformation_t Transformation_t; \
64  typedef traits<JointDerived>::Motion_t Motion_t; \
65  typedef traits<JointDerived>::Bias_t Bias_t; \
66  typedef traits<JointDerived>::F_t F_t; \
67  typedef traits<JointDerived>::U_t U_t; \
68  typedef traits<JointDerived>::D_t D_t; \
69  typedef traits<JointDerived>::UD_t UD_t; \
70  enum { \
71  NQ = traits<JointDerived>::NQ, \
72  NV = traits<JointDerived>::NV \
73  }; \
74  typedef traits<JointDerived>::ConfigVector_t ConfigVector_t; \
75  typedef traits<JointDerived>::TangentVector_t TangentVector_t
76 
77 #define SE3_JOINT_TYPEDEF_ARG(prefix) \
78  typedef int Index; \
79  typedef prefix traits<JointDerived>::Scalar Scalar;
80  typedef prefix traits<JointDerived>::JointDataDerived JointDataDerived; \
81  typedef prefix traits<JointDerived>::JointModelDerived JointModelDerived; \
82  typedef prefix traits<JointDerived>::Constraint_t Constraint_t; \
83  typedef prefix traits<JointDerived>::Transformation_t Transformation_t; \
84  typedef prefix traits<JointDerived>::Motion_t Motion_t; \
85  typedef prefix traits<JointDerived>::Bias_t Bias_t; \
86  typedef prefix traits<JointDerived>::F_t F_t; \
87  typedef prefix traits<JointDerived>::U_t U_t; \
88  typedef prefix traits<JointDerived>::D_t D_t; \
89  typedef prefix traits<JointDerived>::UD_t UD_t; \
90  enum { \
91  NQ = traits<JointDerived>::NQ, \
92  NV = traits<JointDerived>::NV \
93  }; \
94  typedef prefix traits<JointDerived>::ConfigVector_t ConfigVector_t; \
95  typedef prefix traits<JointDerived>::TangentVector_t TangentVector_t
96 
97 #define SE3_JOINT_TYPEDEF SE3_JOINT_TYPEDEF_NOARG()
98 #define SE3_JOINT_TYPEDEF_TEMPLATE SE3_JOINT_TYPEDEF_ARG(typename)
99 
100 #else
101 
102 #define SE3_JOINT_TYPEDEF_ARG() \
103  typedef int Index; \
104  typedef typename traits<JointDerived>::Scalar Scalar; \
105  typedef typename traits<JointDerived>::JointDataDerived JointDataDerived; \
106  typedef typename traits<JointDerived>::JointModelDerived JointModelDerived; \
107  typedef typename traits<JointDerived>::Constraint_t Constraint_t; \
108  typedef typename traits<JointDerived>::Transformation_t Transformation_t; \
109  typedef typename traits<JointDerived>::Motion_t Motion_t; \
110  typedef typename traits<JointDerived>::Bias_t Bias_t; \
111  typedef typename traits<JointDerived>::F_t F_t; \
112  typedef typename traits<JointDerived>::U_t U_t; \
113  typedef typename traits<JointDerived>::D_t D_t; \
114  typedef typename traits<JointDerived>::UD_t UD_t; \
115  enum { \
116  NQ = traits<JointDerived>::NQ, \
117  NV = traits<JointDerived>::NV \
118  }; \
119  typedef typename traits<JointDerived>::ConfigVector_t ConfigVector_t; \
120  typedef typename traits<JointDerived>::TangentVector_t TangentVector_t
121 
122 #define SE3_JOINT_TYPEDEF SE3_JOINT_TYPEDEF_ARG()
123 #define SE3_JOINT_TYPEDEF_TEMPLATE SE3_JOINT_TYPEDEF_ARG()
124 
125 #endif
126 
127 #define SE3_JOINT_USE_INDEXES \
128  typedef JointModelBase<JointModelDerived> Base; \
129  using Base::idx_q; \
130  using Base::idx_v
131 
132 
133  template<typename Derived>
135  {
136  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
137 
138  typedef typename traits<Derived>::JointDerived JointDerived;
139  SE3_JOINT_TYPEDEF_TEMPLATE;
140 
141  JointDataDerived& derived() { return *static_cast<Derived*>(this); }
142  const JointDataDerived& derived() const { return *static_cast<const Derived*>(this); }
143 
144  const Constraint_t & S() const { return derived().S; }
145  const Transformation_t & M() const { return derived().M; }
146  const Motion_t & v() const { return derived().v; }
147  const Bias_t & c() const { return derived().c; }
148  F_t & F() { return derived().F; }
149 
150  const U_t & U() const { return derived().U; }
151  U_t & U() { return derived().U; }
152  const D_t & Dinv() const { return derived().Dinv; }
153  const UD_t & UDinv() const { return derived().UDinv; }
154 
155  protected:
156 
158  inline JointDataBase() {}
159 
160  }; // struct JointDataBase
161 
162  template<int NV>
163  struct SizeDepType
164  {
165  template<class Mat>
167  {
168  typedef typename Mat::template FixedSegmentReturnType<NV>::Type Type;
169  typedef typename Mat::template ConstFixedSegmentReturnType<NV>::Type ConstType;
170  };
171  template<class Mat>
172  struct ColsReturn
173  {
174  typedef typename Mat::template NColsBlockXpr<NV>::Type Type;
175  typedef typename Mat::template ConstNColsBlockXpr<NV>::Type ConstType;
176  };
177  };
178  template<>
179  struct SizeDepType<Eigen::Dynamic>
180  {
181  template<class Mat>
182  struct SegmentReturn
183  {
184  typedef typename Mat::SegmentReturnType Type;
185  typedef typename Mat::ConstSegmentReturnType ConstType;
186  };
187  template<class Mat>
188  struct ColsReturn
189  {
190  typedef typename Mat::ColsBlockXpr Type;
191  typedef typename Mat::ConstColsBlockXpr ConstType;
192  };
193  };
194 
195  template<typename Derived>
197  {
198  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
199 
200  typedef typename traits<Derived>::JointDerived JointDerived;
201  SE3_JOINT_TYPEDEF_TEMPLATE;
202 
203 
204  JointModelDerived& derived() { return *static_cast<Derived*>(this); }
205  const JointModelDerived& derived() const { return *static_cast<const Derived*>(this); }
206 
207  JointDataDerived createData() const { return derived().createData(); }
208 
209  void calc(JointDataDerived& data,
210  const Eigen::VectorXd & qs) const
211  { derived().calc(data,qs); }
212 
213  void calc(JointDataDerived& data,
214  const Eigen::VectorXd & qs,
215  const Eigen::VectorXd & vs) const
216  { derived().calc(data,qs,vs); }
217 
218  void calc_aba(JointDataDerived & data,
219  Inertia::Matrix6 & I,
220  const bool update_I = false) const
221  { derived().calc_aba(data, I, update_I); }
222 
229  typename ConfigVector_t::Scalar finiteDifferenceIncrement() const
230  { return derived().finiteDifferenceIncrement(); }
231 
240  ConfigVector_t integrate(const Eigen::VectorXd & q,const Eigen::VectorXd & v) const
241  { return derived().integrate_impl(q, v); }
242 
243 
253  ConfigVector_t interpolate(const Eigen::VectorXd & q0,const Eigen::VectorXd & q1, double u) const
254  { return derived().interpolate_impl(q0, q1, u); }
255 
256 
265  ConfigVector_t random() const
266  { return derived().random_impl(); }
267 
277  ConfigVector_t randomConfiguration(const ConfigVector_t & lower_pos_limit,
278  const ConfigVector_t & upper_pos_limit) const
279  { return derived().randomConfiguration_impl(lower_pos_limit, upper_pos_limit); }
280 
281 
290  TangentVector_t difference(const Eigen::VectorXd & q0,const Eigen::VectorXd & q1) const
291  { return derived().difference_impl(q0, q1); }
292 
301  double distance(const Eigen::VectorXd & q0,const Eigen::VectorXd & q1) const
302  { return derived().distance_impl(q0, q1); }
303 
309  ConfigVector_t neutralConfiguration() const
310  { return derived().neutralConfiguration_impl(); }
311 
317  void normalize(Eigen::VectorXd & q) const
318  { return derived().normalize_impl(q); }
319 
323  void normalize_impl(Eigen::VectorXd &) const { }
324 
331  bool isSameConfiguration(const Eigen::VectorXd & q1, const Eigen::VectorXd & q2, const Scalar & prec = Eigen::NumTraits<Scalar>::dummy_precision()) const
332  { return derived().isSameConfiguration_impl(q1,q2, prec); }
333 
334  JointIndex i_id; // ID of the joint in the multibody list.
335  int i_q; // Index of the joint configuration in the joint configuration vector.
336  int i_v; // Index of the joint velocity in the joint velocity vector.
337 
338  int nv() const { return derived().nv_impl(); }
339  int nq() const { return derived().nq_impl(); }
340  // Both _impl methods are reimplemented by dynamic-size joints.
341  int nv_impl() const { return NV; }
342  int nq_impl() const { return NQ; }
343 
344  int idx_q() const { return i_q; }
345  int idx_v() const { return i_v; }
346  JointIndex id() const { return i_id; }
347 
348  void setIndexes(JointIndex id, int q, int v) { derived().setIndexes_impl(id, q, v); }
349 
350  void setIndexes_impl(JointIndex id,int q,int v) { i_id = id, i_q = q; i_v = v; }
351 
352  void disp(std::ostream & os) const
353  {
354  using namespace std;
355  os
356  << shortname() << endl
357  << " index: " << i_id << endl
358  << " index q: " << i_q << endl
359  << " index v: " << i_v << endl
360  << " nq: " << nq() << endl
361  << " nv: " << nv() << endl
362  ;
363  }
364 
365  friend std::ostream & operator << (std::ostream & os, const JointModelBase<Derived> & joint)
366  {
367  joint.disp(os);
368  return os;
369  }
370 
371  std::string shortname() const { return derived().shortname(); }
372  static std::string classname() { return Derived::classname(); }
373 
374  template <class OtherDerived>
375  bool operator==(const JointModelBase<OtherDerived> & other) const { return derived().isEqual(other); }
376 
377  template <class OtherDerived>
378  bool isEqual(const JointModelBase<OtherDerived> &) const { return false; }
379 
380  bool isEqual(const JointModelBase<Derived> & other) const
381  {
382  return other.id() == id() && other.idx_q() == idx_q() && other.idx_v() == idx_v();
383  }
384 
385  /* Acces to dedicated segment in robot config space. */
386  // Const access
387  template<typename D>
388  typename SizeDepType<NQ>::template SegmentReturn<D>::ConstType
389  jointConfigSelector(const Eigen::MatrixBase<D>& a) const { return derived().jointConfigSelector_impl(a); }
390  template<typename D>
391  typename SizeDepType<NQ>::template SegmentReturn<D>::ConstType
392  jointConfigSelector_impl(const Eigen::MatrixBase<D>& a) const { return a.template segment<NQ>(i_q); }
393  // Non-const access
394  template<typename D>
395  typename SizeDepType<NQ>::template SegmentReturn<D>::Type
396  jointConfigSelector( Eigen::MatrixBase<D>& a) const { return derived().jointConfigSelector_impl(a); }
397  template<typename D>
398  typename SizeDepType<NQ>::template SegmentReturn<D>::Type
399  jointConfigSelector_impl( Eigen::MatrixBase<D>& a) const { return a.template segment<NQ>(i_q); }
400 
401  /* Acces to dedicated segment in robot config velocity space. */
402  // Const access
403  template<typename D>
404  typename SizeDepType<NV>::template SegmentReturn<D>::ConstType
405  jointVelocitySelector(const Eigen::MatrixBase<D>& a) const { return derived().jointVelocitySelector_impl(a); }
406  template<typename D>
407  typename SizeDepType<NV>::template SegmentReturn<D>::ConstType
408  jointVelocitySelector_impl(const Eigen::MatrixBase<D>& a) const { return a.template segment<NV>(i_v); }
409  // Non-const access
410  template<typename D>
411  typename SizeDepType<NV>::template SegmentReturn<D>::Type
412  jointVelocitySelector( Eigen::MatrixBase<D>& a) const { return derived().jointVelocitySelector_impl(a); }
413  template<typename D>
414  typename SizeDepType<NV>::template SegmentReturn<D>::Type
415  jointVelocitySelector_impl( Eigen::MatrixBase<D>& a) const { return a.template segment<NV>(i_v); }
416 
417  template<typename D>
418  typename SizeDepType<NV>::template ColsReturn<D>::ConstType
419  jointCols(const Eigen::MatrixBase<D>& A) const { return derived().jointCols_impl(A); }
420  template<typename D>
421  typename SizeDepType<NV>::template ColsReturn<D>::ConstType
422  jointCols_impl(const Eigen::MatrixBase<D>& A) const { return A.template middleCols<NV>(i_v); }
423  template<typename D>
424  typename SizeDepType<NV>::template ColsReturn<D>::Type
425  jointCols(Eigen::MatrixBase<D>& A) const { return derived().jointCols_impl(A); }
426  template<typename D>
427  typename SizeDepType<NV>::template ColsReturn<D>::Type
428  jointCols_impl(Eigen::MatrixBase<D>& A) const { return A.template middleCols<NV>(i_v); }
429 
430  protected:
431 
435  inline JointModelBase() : i_id(std::numeric_limits<JointIndex>::max()), i_q(-1), i_v(-1) {}
436 
441  inline JointModelBase( const JointModelBase& clone) { *this = clone; }
442 
447  inline JointModelBase& operator= (const JointModelBase& clone)
448  {
449 // setIndexes(clone.id(),clone.idx_q(),clone.idx_v());
450  i_id = clone.i_id;
451  i_q = clone.i_q;
452  i_v = clone.i_v;
453  return *this;
454  }
455 
456  }; // struct JointModelBase
457 
458 } // namespace se3
459 
460 #endif // ifndef __se3_joint_base_hpp__
STL namespace.
JointModelBase(const JointModelBase &clone)
Definition: joint-base.hpp:441
JointDataBase()
Default constructor: protected.
Definition: joint-base.hpp:158
void normalize_impl(Eigen::VectorXd &) const
Default implementation of normalize.
Definition: joint-base.hpp:323
ConfigVector_t randomConfiguration(const ConfigVector_t &lower_pos_limit, const ConfigVector_t &upper_pos_limit) const
Generate a configuration vector uniformly sampled among provided limits.
Definition: joint-base.hpp:277
ConfigVector_t random() const
Generate a random joint configuration, normalizing quaternions when necessary.
Definition: joint-base.hpp:265
void normalize(Eigen::VectorXd &q) const
Normalize a configuration.
Definition: joint-base.hpp:317
bool isSameConfiguration(const Eigen::VectorXd &q1, const Eigen::VectorXd &q2, const Scalar &prec=Eigen::NumTraits< Scalar >::dummy_precision()) const
Check if two configurations are equivalent within the given precision.
Definition: joint-base.hpp:331
ConfigVector_t::Scalar finiteDifferenceIncrement() const
Return the resolution of the finite differerence increment according to the Scalar type...
Definition: joint-base.hpp:229
ConfigVector_t neutralConfiguration() const
Get neutral configuration of joint.
Definition: joint-base.hpp:309
TangentVector_t difference(const Eigen::VectorXd &q0, const Eigen::VectorXd &q1) const
the tangent vector that must be integrated during one unit time to go from q0 to q1 ...
Definition: joint-base.hpp:290
ConfigVector_t interpolate(const Eigen::VectorXd &q0, const Eigen::VectorXd &q1, double u) const
Interpolation between two joint&#39;s configurations.
Definition: joint-base.hpp:253
ConfigVector_t integrate(const Eigen::VectorXd &q, const Eigen::VectorXd &v) const
Integrate joint&#39;s configuration for a tangent vector during one unit time.
Definition: joint-base.hpp:240
double distance(const Eigen::VectorXd &q0, const Eigen::VectorXd &q1) const
Distance between two configurations of the joint.
Definition: joint-base.hpp:301