pinocchio  2.1.3
constraint.hpp
1 //
2 // Copyright (c) 2015-2018 CNRS
3 // Copyright (c) 2016 Wandercraft, 86 rue de Paris 91400 Orsay, France.
4 //
5 
6 #ifndef __pinocchio_constraint_hpp__
7 #define __pinocchio_constraint_hpp__
8 
9 
10 #include "pinocchio/macros.hpp"
11 #include "pinocchio/spatial/fwd.hpp"
12 #include "pinocchio/spatial/motion.hpp"
13 #include "pinocchio/spatial/act-on-set.hpp"
14 
15 
16 // S : v \in M^6 -> v_J \in lie(Q) ~= R^nv
17 // S^T : f_J \in lie(Q)^* ~= R^nv -> f \in F^6
18 
19 
20 namespace pinocchio
21 {
22  template<int _Dim, typename _Scalar, int _Options=0> class ConstraintTpl;
23 
24  template<class Derived>
26  {
27  protected:
28  typedef typename traits<Derived>::Scalar Scalar;
29  typedef typename traits<Derived>::JointMotion JointMotion;
30  typedef typename traits<Derived>::JointForce JointForce;
31  typedef typename traits<Derived>::DenseBase DenseBase;
32  typedef typename traits<Derived>::MatrixReturnType MatrixReturnType;
33  typedef typename traits<Derived>::ConstMatrixReturnType ConstMatrixReturnType;
34 
35  public:
36  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
37 
38  Derived & derived() { return *static_cast<Derived*>(this); }
39  const Derived & derived() const { return *static_cast<const Derived*>(this); }
40 
41  template<typename VectorLike>
42  JointMotion operator*(const Eigen::MatrixBase<VectorLike> & vj) const
43  { return derived().__mult__(vj); }
44 
45  MatrixReturnType matrix() { return derived().matrix_impl(); }
46  ConstMatrixReturnType matrix() const { return derived().matrix_impl(); }
47 
48  int nv() const { return derived().nv_impl(); }
49 
50  template<class OtherDerived>
51  bool isApprox(const ConstraintBase<OtherDerived> & other,
52  const Scalar & prec = Eigen::NumTraits<Scalar>::dummy_precision()) const
53  { return matrix().isApprox(other.matrix(),prec); }
54 
55  void disp(std::ostream & os) const { static_cast<const Derived*>(this)->disp_impl(os); }
56  friend std::ostream & operator << (std::ostream & os,const ConstraintBase<Derived> & X)
57  {
58  X.disp(os);
59  return os;
60  }
61 
62  template<typename MotionDerived>
63  DenseBase motionAction(const MotionDense<MotionDerived> & v) const
64  { return derived().motionAction(v); }
65 
66  }; // class ConstraintBase
67 
68  template<int _Dim, typename _Scalar, int _Options>
69  struct traits< ConstraintTpl<_Dim, _Scalar, _Options> >
70  {
71  typedef _Scalar Scalar;
72  enum {
73  LINEAR = 0,
74  ANGULAR = 3,
75  Options = _Options,
76  Dim = _Dim
77  };
78  typedef Eigen::Matrix<Scalar,3,1,Options> Vector3;
79  typedef Eigen::Matrix<Scalar,4,1,Options> Vector4;
80  typedef Eigen::Matrix<Scalar,6,1,Options> Vector6;
81  typedef Eigen::Matrix<Scalar,3,3,Options> Matrix3;
82  typedef Eigen::Matrix<Scalar,4,4,Options> Matrix4;
83  typedef Eigen::Matrix<Scalar,6,6,Options> Matrix6;
84  typedef Matrix3 Angular_t;
85  typedef Vector3 Linear_t;
86  typedef const Matrix3 ConstAngular_t;
87  typedef const Vector3 ConstLinear_t;
88  typedef Matrix6 ActionMatrix_t;
89  typedef Eigen::Quaternion<Scalar,Options> Quaternion_t;
94 
96  typedef Eigen::Matrix<Scalar,Dim,1,Options> JointForce;
97  typedef Eigen::Matrix<Scalar,6,Dim,Options> DenseBase;
98 
99  typedef typename PINOCCHIO_EIGEN_REF_CONST_TYPE(DenseBase) ConstMatrixReturnType;
100  typedef typename PINOCCHIO_EIGEN_REF_TYPE(DenseBase) MatrixReturnType;
101 
102  }; // traits ConstraintTpl
103 
104  namespace internal
105  {
106  template<int Dim, typename Scalar, int Options>
107  struct SE3GroupAction< ConstraintTpl<Dim,Scalar,Options> >
108  { typedef Eigen::Matrix<Scalar,6,Dim> ReturnType; };
109 
110  template<int Dim, typename Scalar, int Options, typename MotionDerived>
111  struct MotionAlgebraAction< ConstraintTpl<Dim,Scalar,Options>, MotionDerived >
112  { typedef Eigen::Matrix<Scalar,6,Dim> ReturnType; };
113  }
114 
115  template<int _Dim, typename _Scalar, int _Options>
116  class ConstraintTpl
117  : public ConstraintBase< ConstraintTpl<_Dim,_Scalar,_Options> >
118  {
119  public:
120  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
121 
122  typedef ConstraintBase<ConstraintTpl> Base;
123 
124  friend class ConstraintBase<ConstraintTpl>;
125  SPATIAL_TYPEDEF_TEMPLATE(ConstraintTpl);
126 
127  typedef typename Base::JointMotion JointMotion;
128  typedef typename Base::JointForce JointForce;
129  typedef typename Base::DenseBase DenseBase;
130  typedef typename Base::ConstMatrixReturnType ConstMatrixReturnType;
131  typedef typename Base::MatrixReturnType MatrixReturnType;
132 
133  enum { NV = _Dim, Options = _Options };
134 
135  using Base::nv;
136 
137  public:
138  template<typename D>
139  explicit ConstraintTpl(const Eigen::MatrixBase<D> & _S) : S(_S)
140  {
141  // There is currently a bug in Eigen/Core/util/StaticAssert.h in the use of the full namespace path
142  // TODO
143  EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(DenseBase, D);
144  }
145 
146  ConstraintTpl() : S()
147  {
148  EIGEN_STATIC_ASSERT(_Dim!=Eigen::Dynamic,YOU_CALLED_A_DYNAMIC_SIZE_METHOD_ON_A_FIXED_SIZE_MATRIX_OR_VECTOR)
149  }
150 
151  // It is only valid for dynamics size
152  explicit ConstraintTpl(const int dim) : S(6,dim)
153  {
154  EIGEN_STATIC_ASSERT(_Dim==Eigen::Dynamic,YOU_CALLED_A_FIXED_SIZE_METHOD_ON_A_DYNAMIC_SIZE_MATRIX_OR_VECTOR)
155  }
156 
157  template<typename VectorLike>
158  JointMotion __mult__(const Eigen::MatrixBase<VectorLike> & vj) const
159  {
160  return JointMotion(S*vj);
161  }
162 
163  struct Transpose
164  {
165  const ConstraintTpl & ref;
166  Transpose( const ConstraintTpl & ref ) : ref(ref) {}
167 
168  template<typename Derived>
169  JointForce operator*(const ForceDense<Derived> & f) const
170  { return (ref.S.transpose()*f.toVector()).eval(); }
171 
172  template<typename D>
173  typename Eigen::Matrix<Scalar,NV,Eigen::Dynamic>
174  operator*(const Eigen::MatrixBase<D> & F)
175  {
176  return (ref.S.transpose()*F).eval();
177  }
178 
179  };
180 
181  Transpose transpose() const { return Transpose(*this); }
182 
183  MatrixReturnType matrix_impl() { return S; }
184  ConstMatrixReturnType matrix_impl() const { return S; }
185 
186  int nv_impl() const { return (int)S.cols(); }
187 
188  template<typename S2,int O2>
189  friend typename ConstraintTpl<_Dim,_Scalar,_Options>::DenseBase
190  operator*(const InertiaTpl<S2,O2> & Y, const ConstraintTpl & S)
191  {
192  typedef typename ConstraintTpl::DenseBase ReturnType;
193  ReturnType res(6,S.nv());
194  motionSet::inertiaAction(Y,S.S,res);
195  return res;
196  }
197 
198  template<typename S2,int O2>
199  friend Eigen::Matrix<_Scalar,6,_Dim>
200  operator*(const Eigen::Matrix<S2,6,6,O2> & Ymatrix, const ConstraintTpl & S)
201  {
202  typedef Eigen::Matrix<_Scalar,6,_Dim> ReturnType;
203  return ReturnType(Ymatrix*S.matrix());
204 
205  }
206 
207  DenseBase se3Action(const SE3 & m) const
208  {
209  DenseBase res(6,nv());
210  motionSet::se3Action(m,S,res);
211  return res;
212  }
213 
214  DenseBase se3ActionInverse(const SE3 & m) const
215  {
216  DenseBase res(6,nv());
218  return res;
219  }
220 
221  template<typename MotionDerived>
222  DenseBase motionAction(const MotionDense<MotionDerived> & v) const
223  {
224  DenseBase res(6,nv());
225  motionSet::motionAction(v,S,res);
226  return res;
227  }
228 
229  void disp_impl(std::ostream & os) const { os << "S =\n" << S << std::endl;}
230 
231  protected:
232  DenseBase S;
233  }; // class ConstraintTpl
234 
239 
240 } // namespace pinocchio
241 
242 #endif // ifndef __pinocchio_constraint_hpp__
int nv(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel)
Visit a JointModelTpl through JointNvVisitor to get the dimension of the joint tangent space...
static void motionAction(const MotionDense< MotionDerived > &v, const Eigen::MatrixBase< Mat > &iF, Eigen::MatrixBase< MatRet > const &jF)
Action of a motion on a set of motions, represented by a 6xN matrix whose columns represent a spatial...
static void se3ActionInverse(const SE3Tpl< Scalar, Options > &m, const Eigen::MatrixBase< Mat > &iV, Eigen::MatrixBase< MatRet > const &jV)
Inverse SE3 action on a set of motions, represented by a 6xN matrix whose column represent a spatial ...
ToVectorConstReturnType toVector() const
Return the force as an Eigen vector.
Definition: force-base.hpp:86
Main pinocchio namespace.
Definition: treeview.dox:24
Common traits structure to fully define base classes for CRTP.
Definition: spatial/fwd.hpp:29
static void inertiaAction(const InertiaTpl< Scalar, Options > &I, const Eigen::MatrixBase< Mat > &iF, Eigen::MatrixBase< MatRet > const &jF)
Action of an Inertia matrix on a set of motions, represented by a 6xN matrix whose columns represent ...
static void se3Action(const SE3Tpl< Scalar, Options > &m, const Eigen::MatrixBase< Mat > &iV, Eigen::MatrixBase< MatRet > const &jV)
SE3 action on a set of motions, represented by a 6xN matrix whose column represent a spatial motion...