joint-spherical-ZYX.hpp 14.6 KB
Newer Older
1
//
2
// Copyright (c) 2015-2019 CNRS INRIA
3
// Copyright (c) 2015-2016 Wandercraft, 86 rue de Paris 91400 Orsay, France.
4
5
//

6
7
#ifndef __pinocchio_joint_spherical_ZYX_hpp__
#define __pinocchio_joint_spherical_ZYX_hpp__
8

9
#include "pinocchio/macros.hpp"
10
#include "pinocchio/multibody/joint/joint-base.hpp"
11
#include "pinocchio/multibody/joint/joint-spherical.hpp"
12
13
#include "pinocchio/multibody/constraint.hpp"
#include "pinocchio/math/sincos.hpp"
14
#include "pinocchio/math/matrix.hpp"
15
16
17
#include "pinocchio/spatial/inertia.hpp"
#include "pinocchio/spatial/skew.hpp"

18
namespace pinocchio
19
{
20
21
  template<typename Scalar, int Options> struct ConstraintSphericalZYXTpl;
  
22
  template <typename _Scalar, int _Options>
23
  struct traits< ConstraintSphericalZYXTpl<_Scalar,_Options> >
24
25
26
  {
    typedef _Scalar Scalar;
    enum { Options = _Options };
27
    
28
29
30
31
    enum {
      LINEAR = 0,
      ANGULAR = 3
    };
32
    
33
    typedef MotionSphericalTpl<Scalar,Options> JointMotion;
34
35
    typedef Eigen::Matrix<Scalar,3,1,Options> JointForce;
    typedef Eigen::Matrix<Scalar,6,3,Options> DenseBase;
36
    
37
38
39
40
    typedef DenseBase MatrixReturnType;
    typedef const DenseBase ConstMatrixReturnType;
  }; // struct traits struct ConstraintRotationalSubspace
  
41
  template<typename _Scalar, int _Options>
42
43
  struct ConstraintSphericalZYXTpl
  : public ConstraintBase< ConstraintSphericalZYXTpl<_Scalar,_Options> >
44
  {
45
46
    EIGEN_MAKE_ALIGNED_OPERATOR_NEW
    
47
    PINOCCHIO_CONSTRAINT_TYPEDEF_TPL(ConstraintSphericalZYXTpl)
48
    
49
    enum { NV = 3 };
50
    typedef Eigen::Matrix<Scalar,3,3,Options> Matrix3;
51
    
52
    ConstraintSphericalZYXTpl() {}
53
54
55
    
    template<typename Matrix3Like>
    ConstraintSphericalZYXTpl(const Eigen::MatrixBase<Matrix3Like> & subspace)
56
    : m_S(subspace)
57
58
59
    {  EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(Matrix3Like,3,3); }
    
    template<typename Vector3Like>
60
    JointMotion __mult__(const Eigen::MatrixBase<Vector3Like> & v) const
61
62
    {
      EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Vector3Like,3);
63
      return JointMotion(m_S * v);
64
    }
65
    
66
67
    Matrix3 & operator() () { return m_S; }
    const Matrix3 & operator() () const { return m_S; }
68
69
70
71
    
    int nv_impl() const { return NV; }
    
    struct ConstraintTranspose
72
    {
73
74
      const ConstraintSphericalZYXTpl & ref;
      ConstraintTranspose(const ConstraintSphericalZYXTpl & ref) : ref(ref) {}
75
      
76
      template<typename Derived>
77
      const typename MatrixMatrixProduct<
78
      Eigen::Transpose<const Matrix3>,
79
      Eigen::Block<const typename ForceDense<Derived>::Vector6,3,1>
80
      >::type
81
      operator* (const ForceDense<Derived> & phi) const
82
      {
83
        return ref.m_S.transpose() * phi.angular();
84
85
86
87
      }
      
      /* [CRBA]  MatrixBase operator* (Constraint::Transpose S, ForceSet::Block) */
      template<typename D>
88
      const typename MatrixMatrixProduct<
89
90
      typename Eigen::Transpose<const Matrix3>,
      typename Eigen::MatrixBase<const D>::template NRowsBlockXpr<3>::Type
91
      >::type
92
      operator* (const Eigen::MatrixBase<D> & F) const
93
      {
94
        EIGEN_STATIC_ASSERT(D::RowsAtCompileTime==6,THIS_METHOD_IS_ONLY_FOR_MATRICES_OF_A_SPECIFIC_SIZE)
95
        return ref.m_S.transpose () * F.template middleRows<3>(ANGULAR);
96
      }
97
98
99
100
    }; // struct ConstraintTranspose
    
    ConstraintTranspose transpose () const { return ConstraintTranspose(*this); }
    
101
    DenseBase matrix_impl() const
102
    {
103
104
      DenseBase S;
      S.template middleRows<3>(LINEAR).setZero();
105
      S.template middleRows<3>(ANGULAR) = m_S;
106
      return S;
107
108
109
110
111
112
    }
    
    //      const typename Eigen::ProductReturnType<
    //      const ConstraintDense,
    //      const Matrix3
    //      >::Type
113
114
115
    template<typename S1, int O1>
    Eigen::Matrix<Scalar,6,3,Options>
    se3Action(const SE3Tpl<S1,O1> & m) const
116
117
118
119
120
    {
      //        Eigen::Matrix <Scalar,6,3,Options> X_subspace;
      //        X_subspace.template block <3,3> (Motion::LINEAR, 0) = skew (m.translation ()) * m.rotation ();
      //        X_subspace.template block <3,3> (Motion::ANGULAR, 0) = m.rotation ();
      //
121
      //        return (X_subspace * m_S).eval();
122
      
123
      Eigen::Matrix<Scalar,6,3,Options> result;
124
125
      
      // ANGULAR
126
      result.template middleRows<3>(ANGULAR).noalias() = m.rotation () * m_S;
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
      
      // LINEAR
      cross(m.translation(),
            result.template middleRows<3>(Motion::ANGULAR),
            result.template middleRows<3>(LINEAR));
   
      return result;
    }
    
    template<typename S1, int O1>
    Eigen::Matrix<Scalar,6,3,Options>
    se3ActionInverse(const SE3Tpl<S1,O1> & m) const
    {
      Eigen::Matrix<Scalar,6,3,Options> result;
      
      // LINEAR
      cross(m.translation(),
144
            m_S,
145
146
147
148
            result.template middleRows<3>(ANGULAR));
      result.template middleRows<3>(LINEAR).noalias() = -m.rotation().transpose() * result.template middleRows<3>(ANGULAR);
      
      // ANGULAR
149
      result.template middleRows<3>(ANGULAR).noalias() = m.rotation().transpose() * m_S;
150
151
152
153
      
      return result;
    }
    
154
155
    template<typename MotionDerived>
    DenseBase motionAction(const MotionDense<MotionDerived> & m) const
156
    {
157
158
      const typename MotionDerived::ConstLinearType v = m.linear();
      const typename MotionDerived::ConstAngularType w = m.angular();
159
160
      
      DenseBase res;
161
162
      cross(v,m_S,res.template middleRows<3>(LINEAR));
      cross(w,m_S,res.template middleRows<3>(ANGULAR));
163
164
      
      return res;
165
    }
166
    
167
168
169
    Matrix3 & angularSubspace() { return m_S; }
    const Matrix3 & angularSubspace() const { return m_S; }
    
170
171
172
173
    bool isEqual(const ConstraintSphericalZYXTpl & other) const
    {
      return m_S == other.m_S;
    }
174
    
175
176
177
  protected:
    Matrix3 m_S;
    
178
  }; // struct ConstraintSphericalZYXTpl
179

180
  /* [CRBA] ForceSet operator* (Inertia Y,Constraint S) */
181
182
183
184
  template <typename S1, int O1, typename S2, int O2>
  Eigen::Matrix<S1,6,3,O1>
  operator*(const InertiaTpl<S1,O1> & Y,
            const ConstraintSphericalZYXTpl<S2,O2> & S)
185
  {
186
187
188
189
190
191
    typedef typename InertiaTpl<S1,O1>::Symmetric3 Symmetric3;
    typedef ConstraintSphericalZYXTpl<S2,O2> Constraint;
    Eigen::Matrix<S1,6,3,O1> M;
    alphaSkew (-Y.mass(),Y.lever(),M.template middleRows<3>(Constraint::LINEAR));
    M.template middleRows<3>(Constraint::ANGULAR) =  (Y.inertia () -
    typename Symmetric3::AlphaSkewSquare(Y.mass (), Y.lever ())).matrix();
192

193
    return (M * S.angularSubspace()).eval();
194
  }
195
196
  
  /* [ABA] Y*S operator (Inertia Y,Constraint S) */
197
  //  inline Eigen::Matrix<double,6,3>
198
  template<typename Matrix6Like, typename S2, int O2>
199
  const typename MatrixMatrixProduct<
200
201
  typename Eigen::internal::remove_const<typename SizeDepType<3>::ColsReturn<Matrix6Like>::ConstType>::type,
  typename ConstraintSphericalZYXTpl<S2,O2>::Matrix3
202
  >::type
203
204
  operator*(const Eigen::MatrixBase<Matrix6Like> & Y,
            const ConstraintSphericalZYXTpl<S2,O2> & S)
205
  {
206
    EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(Matrix6Like,6,6);
207
    return Y.derived().template middleCols<3>(Inertia::ANGULAR) * S.angularSubspace();
208
  }
209

210
211
  template<typename S1, int O1>
  struct SE3GroupAction< ConstraintSphericalZYXTpl<S1,O1> >
212
  {
213
214
215
216
217
218
219
220
221
222
223
224
    //      typedef const typename Eigen::ProductReturnType<
    //      Eigen::Matrix <double,6,3,0>,
    //      Eigen::Matrix <double,3,3,0>
    //      >::Type Type;
    typedef Eigen::Matrix<S1,6,3,O1> ReturnType;
  };
  
  template<typename S1, int O1, typename MotionDerived>
  struct MotionAlgebraAction< ConstraintSphericalZYXTpl<S1,O1>, MotionDerived >
  {
    typedef Eigen::Matrix<S1,6,3,O1> ReturnType;
  };
225

226
227
228
229
  template<typename Scalar, int Options> struct JointSphericalZYXTpl;
  
  template<typename _Scalar, int _Options>
  struct traits< JointSphericalZYXTpl<_Scalar,_Options> >
230
  {
231
232
233
234
    enum {
      NQ = 3,
      NV = 3
    };
235
236
237
238
239
240
    typedef _Scalar Scalar;
    enum { Options = _Options };
    typedef JointDataSphericalZYXTpl<Scalar,Options> JointDataDerived;
    typedef JointModelSphericalZYXTpl<Scalar,Options> JointModelDerived;
    typedef ConstraintSphericalZYXTpl<Scalar,Options> Constraint_t;
    typedef SE3Tpl<Scalar,Options> Transformation_t;
241
242
    typedef MotionSphericalTpl<Scalar,Options> Motion_t;
    typedef MotionSphericalTpl<Scalar,Options> Bias_t;
Justin Carpentier's avatar
Justin Carpentier committed
243

244
    // [ABA]
245
246
247
    typedef Eigen::Matrix<Scalar,6,NV,Options> U_t;
    typedef Eigen::Matrix<Scalar,NV,NV,Options> D_t;
    typedef Eigen::Matrix<Scalar,6,NV,Options> UD_t;
248
    
Gabriele Buondonno's avatar
Gabriele Buondonno committed
249
    PINOCCHIO_JOINT_DATA_BASE_ACCESSOR_DEFAULT_RETURN_TYPE
250

251
252
    typedef Eigen::Matrix<Scalar,NQ,1,Options> ConfigVector_t;
    typedef Eigen::Matrix<Scalar,NV,1,Options> TangentVector_t;
253
  };
254
255
256
257
258
259
260
261
  
  template<typename Scalar, int Options>
  struct traits< JointDataSphericalZYXTpl<Scalar,Options> >
  { typedef JointSphericalZYXTpl<Scalar,Options> JointDerived; };
  
  template<typename Scalar, int Options>
  struct traits< JointModelSphericalZYXTpl<Scalar,Options> >
  { typedef JointSphericalZYXTpl<Scalar,Options> JointDerived; };
262

263
264
  template<typename _Scalar, int _Options>
  struct JointDataSphericalZYXTpl : public JointDataBase< JointDataSphericalZYXTpl<_Scalar,_Options> >
265
  {
266
    EIGEN_MAKE_ALIGNED_OPERATOR_NEW
267
    typedef JointSphericalZYXTpl<_Scalar,_Options> JointDerived;
268
    PINOCCHIO_JOINT_DATA_TYPEDEF_TEMPLATE(JointDerived);
Gabriele Buondonno's avatar
Gabriele Buondonno committed
269
    PINOCCHIO_JOINT_DATA_BASE_DEFAULT_ACCESSOR
270
271
272
273
274
275
    
    Constraint_t S;
    Transformation_t M;
    Motion_t v;
    Bias_t c;

276
277
278
279
    // [ABA] specific data
    U_t U;
    D_t Dinv;
    UD_t UDinv;
280
    D_t StU;
281

282
    JointDataSphericalZYXTpl ()
283
284
285
286
    : S(Constraint_t::Matrix3::Zero())
    , M(Transformation_t::Identity())
    , v(Motion_t::Vector3::Zero())
    , c(Bias_t::Vector3::Zero())
287
288
289
290
    , U(U_t::Zero())
    , Dinv(D_t::Zero())
    , UDinv(UD_t::Zero())
    {}
291
292
293

    static std::string classname() { return std::string("JointDataSphericalZYX"); }
    std::string shortname() const { return classname(); }
294
    
295
  }; // strcut JointDataSphericalZYXTpl
296

Gabriele Buondonno's avatar
Gabriele Buondonno committed
297
  PINOCCHIO_JOINT_CAST_TYPE_SPECIALIZATION(JointModelSphericalZYXTpl);
298
  template<typename _Scalar, int _Options>
jcarpent's avatar
jcarpent committed
299
300
  struct JointModelSphericalZYXTpl
  : public JointModelBase< JointModelSphericalZYXTpl<_Scalar,_Options> >
301
  {
302
    EIGEN_MAKE_ALIGNED_OPERATOR_NEW
303
    typedef JointSphericalZYXTpl<_Scalar,_Options> JointDerived;
304
    PINOCCHIO_JOINT_TYPEDEF_TEMPLATE(JointDerived);
jcarpent's avatar
jcarpent committed
305
306
307
308
309
310
    
    typedef JointModelBase<JointModelSphericalZYXTpl> Base;
    using Base::id;
    using Base::idx_q;
    using Base::idx_v;
    using Base::setIndexes;
311

312
    JointDataDerived createData() const { return JointDataDerived(); }
313

314
315
316
    template<typename ConfigVector>
    void calc(JointDataDerived & data,
              const typename Eigen::MatrixBase<ConfigVector> & qs) const
317
    {
318
319
320
      typename ConfigVector::template ConstFixedSegmentReturnType<NQ>::Type & q = qs.template segment<NQ>(idx_q());
      
      typedef typename ConfigVector::Scalar S2;
321

322
323
324
      S2 c0,s0; SINCOS(q(0), &s0, &c0);
      S2 c1,s1; SINCOS(q(1), &s1, &c1);
      S2 c2,s2; SINCOS(q(2), &s2, &c2);
325
326
327
328
329
330
331
332
333
334
335

      data.M.rotation () << c0 * c1,
                c0 * s1 * s2 - s0 * c2,
                c0 * s1 * c2 + s0 * s2,
                s0 * c1,
                s0 * s1 * s2 + c0 * c2,
                s0 * s1 * c2 - c0 * s2,
                -s1,
                c1 * s2,
                c1 * c2;

336
      data.S.angularSubspace()
337
338
339
      <<  -s1, Scalar(0), Scalar(1),
      c1 * s2, c2, Scalar(0),
      c1 * c2, -s2, Scalar(0);
340
341
    }

342
343
344
345
    template<typename ConfigVector, typename TangentVector>
    void calc(JointDataDerived & data,
              const typename Eigen::MatrixBase<ConfigVector> & qs,
              const typename Eigen::MatrixBase<TangentVector> & vs) const
346
    {
347
348
349
350
351
352
353
354
      typename ConfigVector::template ConstFixedSegmentReturnType<NQ>::Type & q = qs.template segment<NQ>(idx_q());
      
      typedef typename ConfigVector::Scalar S2;
      
      S2 c0,s0; SINCOS(q(0), &s0, &c0);
      S2 c1,s1; SINCOS(q(1), &s1, &c1);
      S2 c2,s2; SINCOS(q(2), &s2, &c2);
      
355
      data.M.rotation () << c0 * c1,
356
357
358
359
360
361
362
363
364
          c0 * s1 * s2 - s0 * c2,
          c0 * s1 * c2 + s0 * s2,
          s0 * c1,
          s0 * s1 * s2 + c0 * c2,
          s0 * s1 * c2 - c0 * s2,
          -s1,
          c1 * s2,
          c1 * c2;
      
365
      data.S.angularSubspace()
366
367
368
369
      <<  -s1, Scalar(0), Scalar(1),
      c1 * s2, c2, Scalar(0),
      c1 * c2, -s2, Scalar(0);
    
370
371
      typename TangentVector::template ConstFixedSegmentReturnType<NV>::Type
      & q_dot = vs.template segment<NV>(idx_v());
372

373
      data.v().noalias() = data.S.angularSubspace() * q_dot;
374

375
376
377
      data.c()(0) = -c1 * q_dot(0) * q_dot(1);
      data.c()(1) = -s1 * s2 * q_dot(0) * q_dot(1) + c1 * c2 * q_dot(0) * q_dot(2) - s2 * q_dot(1) * q_dot(2);
      data.c()(2) = -s1 * c2 * q_dot(0) * q_dot(1) - c1 * s2 * q_dot(0) * q_dot(2) - c2 * q_dot(1) * q_dot(2);
378
    }
379
    
380
    template<typename Matrix6Like>
381
382
383
    void calc_aba(JointDataDerived & data,
                  const Eigen::MatrixBase<Matrix6Like> & I,
                  const bool update_I) const
384
    {
385
386
      data.U.noalias() = I.template middleCols<3>(Motion::ANGULAR) * data.S.angularSubspace();
      data.StU.noalias() = data.S.angularSubspace().transpose() * data.U.template middleRows<3>(Motion::ANGULAR);
387
388
      
      // compute inverse
389
390
391
//      data.Dinv.setIdentity();
//      data.StU.llt().solveInPlace(data.Dinv);
      internal::PerformStYSInversion<Scalar>::run(data.StU,data.Dinv);
392
      
393
      data.UDinv.noalias() = data.U * data.Dinv;
394
395
      
      if (update_I)
gabrielebndn's avatar
gabrielebndn committed
396
        PINOCCHIO_EIGEN_CONST_CAST(Matrix6Like,I) -= data.UDinv * data.U.transpose();
397
    }
398
    
399
400
    static std::string classname() { return std::string("JointModelSphericalZYX"); }
    std::string shortname() const { return classname(); }
401
402
403
404
405
406
407
408
409
410
    
    /// \returns An expression of *this with the Scalar type casted to NewScalar.
    template<typename NewScalar>
    JointModelSphericalZYXTpl<NewScalar,Options> cast() const
    {
      typedef JointModelSphericalZYXTpl<NewScalar,Options> ReturnType;
      ReturnType res;
      res.setIndexes(id(),idx_q(),idx_v());
      return res;
    }
411

412
  }; // struct JointModelSphericalZYXTpl
413

414
} // namespace pinocchio
415

416
417
418
419
420
#include <boost/type_traits.hpp>

namespace boost
{
  template<typename Scalar, int Options>
421
  struct has_nothrow_constructor< ::pinocchio::JointModelSphericalZYXTpl<Scalar,Options> >
422
423
424
  : public integral_constant<bool,true> {};
  
  template<typename Scalar, int Options>
425
  struct has_nothrow_copy< ::pinocchio::JointModelSphericalZYXTpl<Scalar,Options> >
426
427
428
  : public integral_constant<bool,true> {};
  
  template<typename Scalar, int Options>
429
  struct has_nothrow_constructor< ::pinocchio::JointDataSphericalZYXTpl<Scalar,Options> >
430
431
432
  : public integral_constant<bool,true> {};
  
  template<typename Scalar, int Options>
433
  struct has_nothrow_copy< ::pinocchio::JointDataSphericalZYXTpl<Scalar,Options> >
434
435
436
  : public integral_constant<bool,true> {};
}

437
#endif // ifndef __pinocchio_joint_spherical_ZYX_hpp__