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