GCC Code Coverage Report


Directory: ./
File: include/pinocchio/multibody/joint/joint-model-base.hpp
Date: 2025-02-12 21:03:38
Exec Total Coverage
Lines: 128 128 100.0%
Branches: 35 68 51.5%

Line Branch Exec Source
1 //
2 // Copyright (c) 2015-2019 CNRS INRIA
3 // Copyright (c) 2015 Wandercraft, 86 rue de Paris 91400 Orsay, France.
4 //
5
6 #ifndef __pinocchio_multibody_joint_model_base_hpp__
7 #define __pinocchio_multibody_joint_model_base_hpp__
8
9 #include "pinocchio/multibody/joint/joint-base.hpp"
10 #include "pinocchio/multibody/joint/joint-common-operations.hpp"
11
12 #include "pinocchio/math/matrix-block.hpp"
13
14 #include <limits>
15
16 #define PINOCCHIO_JOINT_MODEL_TYPEDEF_GENERIC(Joint, TYPENAME) \
17 typedef Eigen::DenseIndex Index; \
18 typedef TYPENAME traits<Joint>::Scalar Scalar; \
19 typedef TYPENAME traits<Joint>::JointDataDerived JointDataDerived; \
20 typedef TYPENAME traits<Joint>::JointModelDerived JointModelDerived; \
21 typedef TYPENAME traits<Joint>::Constraint_t Constraint_t; \
22 typedef TYPENAME traits<Joint>::Transformation_t Transformation_t; \
23 typedef TYPENAME traits<Joint>::Motion_t Motion_t; \
24 typedef TYPENAME traits<Joint>::Bias_t Bias_t; \
25 typedef TYPENAME traits<Joint>::U_t U_t; \
26 typedef TYPENAME traits<Joint>::D_t D_t; \
27 typedef TYPENAME traits<Joint>::UD_t UD_t; \
28 enum \
29 { \
30 Options = traits<Joint>::Options, \
31 NQ = traits<Joint>::NQ, \
32 NV = traits<Joint>::NV \
33 }; \
34 typedef TYPENAME traits<Joint>::ConfigVector_t ConfigVector_t; \
35 typedef TYPENAME traits<Joint>::TangentVector_t TangentVector_t
36
37 #ifdef __clang__
38
39 #define PINOCCHIO_JOINT_TYPEDEF(Joint) \
40 PINOCCHIO_JOINT_MODEL_TYPEDEF_GENERIC(Joint, PINOCCHIO_EMPTY_ARG)
41 #define PINOCCHIO_JOINT_TYPEDEF_TEMPLATE(Joint) \
42 PINOCCHIO_JOINT_MODEL_TYPEDEF_GENERIC(Joint, typename)
43
44 #elif (__GNUC__ == 4) && (__GNUC_MINOR__ == 4) && (__GNUC_PATCHLEVEL__ == 2)
45
46 #define PINOCCHIO_JOINT_TYPEDEF(Joint) \
47 PINOCCHIO_JOINT_MODEL_TYPEDEF_GENERIC(Joint, PINOCCHIO_EMPTY_ARG)
48 #define PINOCCHIO_JOINT_TYPEDEF_TEMPLATE(Joint) \
49 PINOCCHIO_JOINT_MODEL_TYPEDEF_GENERIC(Joint, typename)
50
51 #else
52
53 #define PINOCCHIO_JOINT_TYPEDEF(Joint) PINOCCHIO_JOINT_MODEL_TYPEDEF_GENERIC(Joint, typename)
54 #define PINOCCHIO_JOINT_TYPEDEF_TEMPLATE(Joint) \
55 PINOCCHIO_JOINT_MODEL_TYPEDEF_GENERIC(Joint, typename)
56
57 #endif
58
59 #define PINOCCHIO_JOINT_USE_INDEXES(Joint) \
60 typedef JointModelBase<Joint> Base; \
61 using Base::idx_q; \
62 using Base::idx_v
63
64 #define PINOCCHIO_JOINT_CAST_TYPE_SPECIALIZATION(JointModelTpl) \
65 template<typename Scalar, int Options, typename NewScalar> \
66 struct CastType<NewScalar, JointModelTpl<Scalar, Options>> \
67 { \
68 typedef JointModelTpl<NewScalar, Options> type; \
69 }
70
71 namespace pinocchio
72 {
73
74 template<typename Derived>
75 struct JointModelBase : NumericalBase<Derived>
76 {
77 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
78
79 typedef typename traits<Derived>::JointDerived JointDerived;
80 PINOCCHIO_JOINT_TYPEDEF_TEMPLATE(JointDerived);
81
82 34135 JointModelDerived & derived()
83 {
84 34135 return *static_cast<Derived *>(this);
85 }
86 85645380 const JointModelDerived & derived() const
87 {
88 86077749 return *static_cast<const Derived *>(this);
89 }
90
91 44350 JointDataDerived createData() const
92 {
93 44350 return derived().createData();
94 }
95
96 6 const std::vector<bool> hasConfigurationLimit() const
97 {
98 6 return derived().hasConfigurationLimit();
99 }
100
101 6 const std::vector<bool> hasConfigurationLimitInTangent() const
102 {
103 6 return derived().hasConfigurationLimitInTangent();
104 }
105
106 template<typename ConfigVectorType>
107 1084500 void calc(JointDataDerived & data, const Eigen::MatrixBase<ConfigVectorType> & qs) const
108 {
109 1084500 derived().calc(data, qs.derived());
110 1084500 }
111
112 template<typename TangentVectorType>
113 50 void calc(
114 JointDataDerived & data,
115 const Blank not_used,
116 const Eigen::MatrixBase<TangentVectorType> & vs) const
117 {
118 50 derived().calc(data, not_used, vs.derived());
119 50 }
120
121 template<typename ConfigVectorType, typename TangentVectorType>
122 216056 void calc(
123 JointDataDerived & data,
124 const Eigen::MatrixBase<ConfigVectorType> & qs,
125 const Eigen::MatrixBase<TangentVectorType> & vs) const
126 {
127 216056 derived().calc(data, qs.derived(), vs.derived());
128 216056 }
129
130 template<typename VectorLike, typename Matrix6Like>
131 12266 void calc_aba(
132 JointDataDerived & data,
133 const Eigen::MatrixBase<VectorLike> & armature,
134 const Eigen::MatrixBase<Matrix6Like> & I,
135 const bool update_I = false) const
136 {
137 24532 derived().calc_aba(
138 12266 data, armature.derived(), PINOCCHIO_EIGEN_CONST_CAST(Matrix6Like, I), update_I);
139 12266 }
140
141 43522758 int nv() const
142 {
143 43522758 return derived().nv_impl();
144 }
145 3651253 int nq() const
146 {
147 7301254 return derived().nq_impl();
148 }
149
150 // Default _impl methods are reimplemented by dynamic-size joints.
151 22633472 int nv_impl() const
152 {
153 22633472 return NV;
154 }
155 2103449 int nq_impl() const
156 {
157 4205954 return NQ;
158 }
159
160 8248844 int idx_q() const
161 {
162 8248844 return derived().idx_q_impl();
163 }
164 10004959 int idx_v() const
165 {
166 20009918 return derived().idx_v_impl();
167 }
168 782344 JointIndex id() const
169 {
170 1564688 return derived().id_impl();
171 }
172
173 5265444 int idx_q_impl() const
174 {
175 5265444 return i_q;
176 }
177 333105 int idx_v_impl() const
178 {
179 5847421 return i_v;
180 }
181 779958 JointIndex id_impl() const
182 {
183 1559916 return i_id;
184 }
185
186 34133 void setIndexes(JointIndex id, int q, int v)
187 {
188 34133 derived().setIndexes_impl(id, q, v);
189 34133 }
190
191 33985 void setIndexes_impl(JointIndex id, int q, int v)
192 {
193 33985 i_id = id, i_q = q;
194 33985 i_v = v;
195 33985 }
196
197 82 void disp(std::ostream & os) const
198 {
199 using namespace std;
200
2/4
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 41 times.
✗ Branch 6 not taken.
164 os << shortname() << endl
201
4/8
✓ Branch 1 taken 41 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 41 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 41 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 41 times.
✗ Branch 11 not taken.
82 << " index: " << id() << endl
202
4/8
✓ Branch 1 taken 41 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 41 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 41 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 41 times.
✗ Branch 11 not taken.
82 << " index q: " << idx_q() << endl
203
4/8
✓ Branch 1 taken 41 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 41 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 41 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 41 times.
✗ Branch 11 not taken.
82 << " index v: " << idx_v() << endl
204
4/8
✓ Branch 1 taken 41 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 41 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 41 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 41 times.
✗ Branch 11 not taken.
82 << " nq: " << nq() << endl
205
4/8
✓ Branch 1 taken 41 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 41 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 41 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 41 times.
✗ Branch 11 not taken.
82 << " nv: " << nv() << endl;
206 82 }
207
208 82 friend std::ostream & operator<<(std::ostream & os, const JointModelBase<Derived> & joint)
209 {
210 82 joint.disp(os);
211 82 return os;
212 }
213
214 1526 std::string shortname() const
215 {
216 1526 return derived().shortname();
217 }
218 27 static std::string classname()
219 {
220 54 return Derived::classname();
221 }
222
223 template<typename NewScalar>
224 1498 typename CastType<NewScalar, Derived>::type cast() const
225 {
226 1498 return derived().template cast<NewScalar>();
227 }
228
229 template<class OtherDerived>
230 4684 bool operator==(const JointModelBase<OtherDerived> & other) const
231 {
232 4684 return derived().isEqual(other.derived());
233 }
234
235 template<class OtherDerived>
236 55 bool operator!=(const JointModelBase<OtherDerived> & other) const
237 {
238 55 return !(internal::comparison_eq(derived(), other.derived()));
239 }
240
241 template<class OtherDerived>
242 4 bool isEqual(const JointModelBase<OtherDerived> &) const
243 {
244 4 return false;
245 }
246
247 8494 bool isEqual(const JointModelBase<Derived> & other) const
248 {
249 8494 return derived().hasSameIndexes(other.derived());
250 }
251
252 template<class OtherDerived>
253 8544 bool hasSameIndexes(const JointModelBase<OtherDerived> & other) const
254 {
255
3/6
✓ Branch 1 taken 4272 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4272 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 4272 times.
✗ Branch 8 not taken.
8544 return internal::comparison_eq(other.id(), id())
256
4/8
✓ Branch 1 taken 4216 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4216 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 4216 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 4216 times.
✗ Branch 10 not taken.
8432 && internal::comparison_eq(other.idx_q(), idx_q())
257
6/10
✓ Branch 0 taken 4216 times.
✓ Branch 1 taken 56 times.
✓ Branch 3 taken 4216 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 4216 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 4216 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 4216 times.
✗ Branch 12 not taken.
16976 && internal::comparison_eq(other.idx_v(), idx_v());
258 }
259
260 /* Acces to dedicated segment in robot config space. */
261 // Const access
262 template<typename D>
263 typename SizeDepType<NQ>::template SegmentReturn<D>::ConstType
264 473666 jointConfigSelector(const Eigen::MatrixBase<D> & a) const
265 {
266 473666 return derived().jointConfigSelector_impl(a);
267 }
268
269 template<typename D>
270 typename SizeDepType<NQ>::template SegmentReturn<D>::ConstType
271 473654 jointConfigSelector_impl(const Eigen::MatrixBase<D> & a) const
272 {
273 473654 return SizeDepType<NQ>::segment(a.derived(), idx_q(), nq());
274 }
275
276 // Non-const access
277 template<typename D>
278 typename SizeDepType<NQ>::template SegmentReturn<D>::Type
279 334522 jointConfigSelector(Eigen::MatrixBase<D> & a) const
280 {
281 334522 return derived().jointConfigSelector_impl(a);
282 }
283
284 template<typename D>
285 typename SizeDepType<NQ>::template SegmentReturn<D>::Type
286 334516 jointConfigSelector_impl(Eigen::MatrixBase<D> & a) const
287 {
288 334516 return SizeDepType<NQ>::segment(a, idx_q(), nq());
289 }
290
291 /* Acces to dedicated segment in robot config velocity space. */
292 // Const access
293 template<typename D>
294 typename SizeDepType<NV>::template SegmentReturn<D>::ConstType
295 278033 jointVelocitySelector(const Eigen::MatrixBase<D> & a) const
296 {
297 278033 return derived().jointVelocitySelector_impl(a.derived());
298 }
299
300 template<typename D>
301 typename SizeDepType<NV>::template SegmentReturn<D>::ConstType
302 278019 jointVelocitySelector_impl(const Eigen::MatrixBase<D> & a) const
303 {
304 278019 return SizeDepType<NV>::segment(a.derived(), idx_v(), nv());
305 }
306
307 // Non-const access
308 template<typename D>
309 typename SizeDepType<NV>::template SegmentReturn<D>::Type
310 308452 jointVelocitySelector(Eigen::MatrixBase<D> & a) const
311 {
312 308452 return derived().jointVelocitySelector_impl(a.derived());
313 }
314
315 template<typename D>
316 typename SizeDepType<NV>::template SegmentReturn<D>::Type
317 308428 jointVelocitySelector_impl(Eigen::MatrixBase<D> & a) const
318 {
319 308428 return SizeDepType<NV>::segment(a.derived(), idx_v(), nv());
320 }
321
322 /* Acces to dedicated columns in a ForceSet or MotionSet matrix.*/
323 // Const access
324 template<typename D>
325 typename SizeDepType<NV>::template ColsReturn<D>::ConstType
326 2422 jointCols(const Eigen::MatrixBase<D> & A) const
327 {
328 2422 return derived().jointCols_impl(A.derived());
329 }
330
331 template<typename D>
332 typename SizeDepType<NV>::template ColsReturn<D>::ConstType
333 2422 jointCols_impl(const Eigen::MatrixBase<D> & A) const
334 {
335 2422 return SizeDepType<NV>::middleCols(A.derived(), idx_v(), nv());
336 }
337
338 // Non-const access
339 template<typename D>
340 607810 typename SizeDepType<NV>::template ColsReturn<D>::Type jointCols(Eigen::MatrixBase<D> & A) const
341 {
342 607810 return derived().jointCols_impl(A.derived());
343 }
344
345 template<typename D>
346 typename SizeDepType<NV>::template ColsReturn<D>::Type
347 315529 jointCols_impl(Eigen::MatrixBase<D> & A) const
348 {
349 315529 return SizeDepType<NV>::middleCols(A.derived(), idx_v(), nv());
350 }
351
352 /* Acces to dedicated rows in a matrix.*/
353 // Const access
354 template<typename D>
355 typename SizeDepType<NV>::template RowsReturn<D>::ConstType
356 152 jointRows(const Eigen::MatrixBase<D> & A) const
357 {
358 152 return derived().jointRows_impl(A.derived());
359 }
360
361 template<typename D>
362 typename SizeDepType<NV>::template RowsReturn<D>::ConstType
363 152 jointRows_impl(const Eigen::MatrixBase<D> & A) const
364 {
365 152 return SizeDepType<NV>::middleRows(A.derived(), idx_v(), nv());
366 }
367
368 // Non-const access
369 template<typename D>
370 196 typename SizeDepType<NV>::template RowsReturn<D>::Type jointRows(Eigen::MatrixBase<D> & A) const
371 {
372 196 return derived().jointRows_impl(A.derived());
373 }
374
375 template<typename D>
376 typename SizeDepType<NV>::template RowsReturn<D>::Type
377 196 jointRows_impl(Eigen::MatrixBase<D> & A) const
378 {
379 196 return SizeDepType<NV>::middleRows(A.derived(), idx_v(), nv());
380 }
381
382 /// \brief Returns a block of dimension nv()xnv() located at position idx_v(),idx_v() in the
383 /// matrix Mat
384 // Const access
385 template<typename D>
386 typename SizeDepType<NV>::template BlockReturn<D>::ConstType
387 jointBlock(const Eigen::MatrixBase<D> & Mat) const
388 {
389 return derived().jointBlock_impl(Mat.derived());
390 }
391
392 template<typename D>
393 typename SizeDepType<NV>::template BlockReturn<D>::ConstType
394 jointBlock_impl(const Eigen::MatrixBase<D> & Mat) const
395 {
396 return SizeDepType<NV>::block(Mat.derived(), idx_v(), idx_v(), nv(), nv());
397 }
398
399 // Non-const access
400 template<typename D>
401 typename SizeDepType<NV>::template BlockReturn<D>::Type
402 1588 jointBlock(Eigen::MatrixBase<D> & Mat) const
403 {
404 1588 return derived().jointBlock_impl(Mat.derived());
405 }
406
407 template<typename D>
408 typename SizeDepType<NV>::template BlockReturn<D>::Type
409 794 jointBlock_impl(Eigen::MatrixBase<D> & Mat) const
410 {
411 794 return SizeDepType<NV>::block(Mat.derived(), idx_v(), idx_v(), nv(), nv());
412 }
413
414 protected:
415 /// Default constructor: protected.
416 ///
417 /// Prevent the construction of stand-alone JointModelBase.
418 74526 inline JointModelBase()
419 74526 : i_id(std::numeric_limits<JointIndex>::max())
420 74526 , i_q(-1)
421 74526 , i_v(-1)
422 {
423 74526 }
424
425 /// Copy constructor: protected.
426 ///
427 /// Copy of stand-alone JointModelBase are prevented, but can be used from inhereting
428 /// objects. Copy is done by calling copy operator.
429 259338 inline JointModelBase(const JointModelBase & clone)
430 {
431 259338 *this = clone;
432 259338 }
433
434 /// Copy operator: protected.
435 ///
436 /// Copy of stand-alone JointModelBase are prevented, but can be used from inhereting
437 /// objects.
438 130654 inline JointModelBase & operator=(const JointModelBase & clone)
439 {
440 130654 i_id = clone.i_id;
441 130654 i_q = clone.i_q;
442 130654 i_v = clone.i_v;
443 130654 return *this;
444 }
445
446 // data
447 JointIndex i_id; // ID of the joint in the multibody list.
448 int i_q; // Index of the joint configuration in the joint configuration vector.
449 int i_v; // Index of the joint velocity in the joint velocity vector.
450
451 }; // struct JointModelBase
452
453 } // namespace pinocchio
454
455 #endif // ifndef __pinocchio_multibody_joint_model_base_hpp__
456