pinocchio  UNKNOWN
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  template<class Mat>
178  struct RowsReturn
179  {
180  typedef typename Mat::template NRowsBlockXpr<NV>::Type Type;
181  typedef typename Mat::template ConstNRowsBlockXpr<NV>::Type ConstType;
182  };
183  };
184  template<>
185  struct SizeDepType<Eigen::Dynamic>
186  {
187  template<class Mat>
188  struct SegmentReturn
189  {
190  typedef typename Mat::SegmentReturnType Type;
191  typedef typename Mat::ConstSegmentReturnType ConstType;
192  };
193  template<class Mat>
194  struct ColsReturn
195  {
196  typedef typename Mat::ColsBlockXpr Type;
197  typedef typename Mat::ConstColsBlockXpr ConstType;
198  };
199  template<class Mat>
200  struct RowsReturn
201  {
202  typedef typename Mat::RowsBlockXpr Type;
203  typedef typename Mat::ConstRowsBlockXpr ConstType;
204  };
205  };
206 
207  template<typename Derived>
209  {
210  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
211 
212  typedef typename traits<Derived>::JointDerived JointDerived;
213  SE3_JOINT_TYPEDEF_TEMPLATE;
214 
215 
216  JointModelDerived& derived() { return *static_cast<Derived*>(this); }
217  const JointModelDerived& derived() const { return *static_cast<const Derived*>(this); }
218 
219  JointDataDerived createData() const { return derived().createData(); }
220 
221  void calc(JointDataDerived& data,
222  const Eigen::VectorXd & qs) const
223  { derived().calc(data,qs); }
224 
225  void calc(JointDataDerived& data,
226  const Eigen::VectorXd & qs,
227  const Eigen::VectorXd & vs) const
228  { derived().calc(data,qs,vs); }
229 
230  void calc_aba(JointDataDerived & data,
231  Inertia::Matrix6 & I,
232  const bool update_I = false) const
233  { derived().calc_aba(data, I, update_I); }
234 
241  typename ConfigVector_t::Scalar finiteDifferenceIncrement() const
242  { return derived().finiteDifferenceIncrement(); }
243 
244  JointIndex i_id; // ID of the joint in the multibody list.
245  int i_q; // Index of the joint configuration in the joint configuration vector.
246  int i_v; // Index of the joint velocity in the joint velocity vector.
247 
248  int nv() const { return derived().nv_impl(); }
249  int nq() const { return derived().nq_impl(); }
250  // Both _impl methods are reimplemented by dynamic-size joints.
251  int nv_impl() const { return NV; }
252  int nq_impl() const { return NQ; }
253 
254  int idx_q() const { return i_q; }
255  int idx_v() const { return i_v; }
256  JointIndex id() const { return i_id; }
257 
258  void setIndexes(JointIndex id, int q, int v) { derived().setIndexes_impl(id, q, v); }
259 
260  void setIndexes_impl(JointIndex id,int q,int v) { i_id = id, i_q = q; i_v = v; }
261 
262  void disp(std::ostream & os) const
263  {
264  using namespace std;
265  os
266  << shortname() << endl
267  << " index: " << i_id << endl
268  << " index q: " << i_q << endl
269  << " index v: " << i_v << endl
270  << " nq: " << nq() << endl
271  << " nv: " << nv() << endl
272  ;
273  }
274 
275  friend std::ostream & operator << (std::ostream & os, const JointModelBase<Derived> & joint)
276  {
277  joint.disp(os);
278  return os;
279  }
280 
281  std::string shortname() const { return derived().shortname(); }
282  static std::string classname() { return Derived::classname(); }
283 
284  template <class OtherDerived>
285  bool operator==(const JointModelBase<OtherDerived> & other) const { return derived().isEqual(other); }
286 
287  template <class OtherDerived>
288  bool isEqual(const JointModelBase<OtherDerived> &) const { return false; }
289 
290  bool isEqual(const JointModelBase<Derived> & other) const
291  {
292  return other.id() == id() && other.idx_q() == idx_q() && other.idx_v() == idx_v();
293  }
294 
295  /* Acces to dedicated segment in robot config space. */
296  // Const access
297  template<typename D>
298  typename SizeDepType<NQ>::template SegmentReturn<D>::ConstType
299  jointConfigSelector(const Eigen::MatrixBase<D>& a) const { return derived().jointConfigSelector_impl(a); }
300  template<typename D>
301  typename SizeDepType<NQ>::template SegmentReturn<D>::ConstType
302  jointConfigSelector_impl(const Eigen::MatrixBase<D>& a) const { return a.template segment<NQ>(i_q); }
303  // Non-const access
304  template<typename D>
305  typename SizeDepType<NQ>::template SegmentReturn<D>::Type
306  jointConfigSelector( Eigen::MatrixBase<D>& a) const { return derived().jointConfigSelector_impl(a); }
307  template<typename D>
308  typename SizeDepType<NQ>::template SegmentReturn<D>::Type
309  jointConfigSelector_impl( Eigen::MatrixBase<D>& a) const { return a.template segment<NQ>(i_q); }
310 
311  /* Acces to dedicated segment in robot config velocity space. */
312  // Const access
313  template<typename D>
314  typename SizeDepType<NV>::template SegmentReturn<D>::ConstType
315  jointVelocitySelector(const Eigen::MatrixBase<D>& a) const { return derived().jointVelocitySelector_impl(a); }
316  template<typename D>
317  typename SizeDepType<NV>::template SegmentReturn<D>::ConstType
318  jointVelocitySelector_impl(const Eigen::MatrixBase<D>& a) const { return a.template segment<NV>(i_v); }
319  // Non-const access
320  template<typename D>
321  typename SizeDepType<NV>::template SegmentReturn<D>::Type
322  jointVelocitySelector( Eigen::MatrixBase<D>& a) const { return derived().jointVelocitySelector_impl(a); }
323  template<typename D>
324  typename SizeDepType<NV>::template SegmentReturn<D>::Type
325  jointVelocitySelector_impl( Eigen::MatrixBase<D>& a) const { return a.template segment<NV>(i_v); }
326 
327  template<typename D>
328  typename SizeDepType<NV>::template ColsReturn<D>::ConstType
329  jointCols(const Eigen::MatrixBase<D>& A) const { return derived().jointCols_impl(A); }
330  template<typename D>
331  typename SizeDepType<NV>::template ColsReturn<D>::ConstType
332  jointCols_impl(const Eigen::MatrixBase<D>& A) const { return A.template middleCols<NV>(i_v); }
333  template<typename D>
334  typename SizeDepType<NV>::template ColsReturn<D>::Type
335  jointCols(Eigen::MatrixBase<D>& A) const { return derived().jointCols_impl(A); }
336  template<typename D>
337  typename SizeDepType<NV>::template ColsReturn<D>::Type
338  jointCols_impl(Eigen::MatrixBase<D>& A) const { return A.template middleCols<NV>(i_v); }
339 
340  protected:
341 
345  inline JointModelBase() : i_id(std::numeric_limits<JointIndex>::max()), i_q(-1), i_v(-1) {}
346 
351  inline JointModelBase( const JointModelBase& clone) { *this = clone; }
352 
357  inline JointModelBase& operator= (const JointModelBase& clone)
358  {
359 // setIndexes(clone.id(),clone.idx_q(),clone.idx_v());
360  i_id = clone.i_id;
361  i_q = clone.i_q;
362  i_v = clone.i_v;
363  return *this;
364  }
365 
366  }; // struct JointModelBase
367 
368 } // namespace se3
369 
370 #endif // ifndef __se3_joint_base_hpp__
JointDataVariant createData(const JointModelVariant &jmodel)
Visit a JointModelVariant through CreateData visitor to create a JointDataVariant.
STL namespace.
JointModelBase(const JointModelBase &clone)
Definition: joint-base.hpp:351
JointDataBase()
Default constructor: protected.
Definition: joint-base.hpp:158
void calc_aba(const JointModelVariant &jmodel, JointDataVariant &jdata, Inertia::Matrix6 &I, const bool update_I)
Visit a JointDataVariant and the corresponding JointModelVariant through JointCalcAbaVisitor to...
int nq(const JointModelVariant &jmodel)
Visit a JointModelVariant through JointNqVisitor to get the dimension of the joint configuration spac...
ConfigVector_t::Scalar finiteDifferenceIncrement() const
Return the resolution of the finite differerence increment according to the Scalar type...
Definition: joint-base.hpp:241
void setIndexes(JointModelVariant &jmodel, JointIndex id, int q, int v)
Visit a JointModelVariant through JointSetIndexesVisitor to set the indexes of the joint in the kinem...
std::string shortname(const JointModelVariant &jmodel)
Visit a JointModelVariant through JointShortnameVisitor to get the shortname of the derived joint mod...
int nv(const JointModelVariant &jmodel)
Visit a JointModelVariant through JointNvVisitor to get the dimension of the joint tangent space...
int idx_q(const JointModelVariant &jmodel)
Visit a JointModelVariant through JointIdxQVisitor to get the index in the full model configuration s...
JointIndex id(const JointModelVariant &jmodel)
Visit a JointModelVariant through JointIdVisitor to get the index of the joint in the kinematic chain...
int idx_v(const JointModelVariant &jmodel)
Visit a JointModelVariant through JointIdxVVisitor to get the index in the full model tangent space c...