pinocchio  1.2.6-7-g6de3e-dirty
constraint.hpp
1 //
2 // Copyright (c) 2015-2017 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 
27 
28 // S : v \in M^6 -> v_J \in lie(Q) ~= R^nv
29 // S^T : f_J \in lie(Q)^* ~= R^nv -> f \in F^6
30 
31 
32 namespace se3
33 {
34  template<int _Dim, typename _Scalar, int _Options=0> class ConstraintTpl;
35 
36  template<class Derived>
38  {
39  protected:
40  typedef typename traits<Derived>::Scalar Scalar;
41  typedef typename traits<Derived>::JointMotion JointMotion;
42  typedef typename traits<Derived>::JointForce JointForce;
43  typedef typename traits<Derived>::DenseBase DenseBase;
44 
45  public:
46  Derived & derived() { return *static_cast<Derived*>(this); }
47  const Derived& derived() const { return *static_cast<const Derived*>(this); }
48 
49  Motion operator* (const JointMotion& vj) const { return derived().__mult__(vj); }
50 
51  DenseBase & matrix() { return derived().matrix_impl(); }
52  const DenseBase & matrix() const { return derived().matrix_impl(); }
53  int nv() const { return derived().nv_impl(); }
54 
55  template<class OtherDerived>
56  bool isApprox(const ConstraintBase<OtherDerived> & other,
57  const Scalar & prec = Eigen::NumTraits<Scalar>::dummy_precision()) const
58  { return matrix().isApprox(other.matrix(),prec); }
59 
60  void disp(std::ostream & os) const { static_cast<const Derived*>(this)->disp_impl(os); }
61  friend std::ostream & operator << (std::ostream & os,const ConstraintBase<Derived> & X)
62  {
63  X.disp(os);
64  return os;
65  }
66 
67  }; // class ConstraintBase
68 
69  template<int D, typename T, int U>
70  struct traits< ConstraintTpl<D, T, U> >
71  {
72  typedef T Scalar;
73  typedef Eigen::Matrix<T,3,1,U> Vector3;
74  typedef Eigen::Matrix<T,4,1,U> Vector4;
75  typedef Eigen::Matrix<T,6,1,U> Vector6;
76  typedef Eigen::Matrix<T,3,3,U> Matrix3;
77  typedef Eigen::Matrix<T,4,4,U> Matrix4;
78  typedef Eigen::Matrix<T,6,6,U> Matrix6;
79  typedef Matrix3 Angular_t;
80  typedef Vector3 Linear_t;
81  typedef const Matrix3 ConstAngular_t;
82  typedef const Vector3 ConstLinear_t;
83  typedef Matrix6 ActionMatrix_t;
84  typedef Eigen::Quaternion<T,U> Quaternion_t;
85  typedef SE3Tpl<T,U> SE3;
86  typedef ForceTpl<T,U> Force;
87  typedef MotionTpl<T,U> Motion;
89  enum {
90  LINEAR = 0,
91  ANGULAR = 3
92  };
93  typedef Eigen::Matrix<Scalar,D,1,U> JointMotion;
94  typedef Eigen::Matrix<Scalar,D,1,U> JointForce;
95  typedef Eigen::Matrix<Scalar,6,D> DenseBase;
96 
97  }; // traits ConstraintTpl
98 
99  namespace internal
100  {
101  template<int Dim, typename Scalar, int Options>
102  struct ActionReturn<ConstraintTpl<Dim,Scalar,Options> >
103  { typedef Eigen::Matrix<Scalar,6,Dim> Type; };
104  }
105 
106  template<int _Dim, typename _Scalar, int _Options>
107  class ConstraintTpl : public ConstraintBase<ConstraintTpl < _Dim, _Scalar, _Options > >
108  {
109  public:
110  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
111 
113 
114  friend class ConstraintBase< ConstraintTpl< _Dim, _Scalar, _Options > >;
115  SPATIAL_TYPEDEF_TEMPLATE(ConstraintTpl);
116 
117  typedef typename Base::JointMotion JointMotion;
118  typedef typename Base::JointForce JointForce;
119  typedef typename Base::DenseBase DenseBase;
120 
121  enum { NV = _Dim, Options = _Options };
122 
123  public:
124  template<typename D>
125  ConstraintTpl(const Eigen::MatrixBase<D> & _S) : S(_S)
126  {
127  // There is currently a bug in Eigen/Core/util/StaticAssert.h in the use of the full namespace path
128  // TODO
129  EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(DenseBase, D);
130  }
131 
132  ConstraintTpl() : S()
133  {
134 #ifndef NDEBUG
135  S.fill( NAN );
136 #endif
137  }
138 
139  // It is only valid for dynamics size
140  ConstraintTpl(const int dim) : S(6,dim)
141  {
142  EIGEN_STATIC_ASSERT(_Dim==Eigen::Dynamic,YOU_CALLED_A_FIXED_SIZE_METHOD_ON_A_DYNAMIC_SIZE_MATRIX_OR_VECTOR)
143 #ifndef NDEBUG
144  S.fill( NAN );
145 #endif
146  }
147 
148  Motion __mult__(const JointMotion& vj) const
149  {
150  return Motion(S*vj);
151  }
152 
153 
154  struct Transpose
155  {
156  const ConstraintTpl & ref;
157  Transpose( const ConstraintTpl & ref ) : ref(ref) {}
158 
159  JointForce operator* (const Force& f) const
160  { return (ref.S.transpose()*f.toVector()).eval(); }
161 
162  template<typename D>
163  typename Eigen::Matrix<_Scalar,NV,Eigen::Dynamic>
164  operator*( const Eigen::MatrixBase<D> & F )
165  {
166  return (ref.S.transpose()*F).eval();
167  }
168 
169  };
170  Transpose transpose() const { return Transpose(*this); }
171 
172  DenseBase & matrix_impl() { return S; }
173  const DenseBase & matrix_impl() const { return S; }
174 
175  int nv_impl() const { return NV; }
176 
177  //template<int Dim,typename Scalar,int Options>
178  friend Eigen::Matrix<_Scalar,6,_Dim>
180  { return (Y.matrix()*S.S).eval(); }
181 
182 
183  DenseBase se3Action(const SE3 & m) const
184  {
185  return (m.toActionMatrix()*S).eval();
186  }
187 
188  DenseBase se3ActionInverse(const SE3 & m) const
189  {
190  return (m.inverse().toActionMatrix()*S).eval();
191  }
192 
193  DenseBase variation(const Motion & v) const
194  {
195  DenseBase res(v.toActionMatrix() * S);
196 
197  return res;
198  }
199 
200  void disp_impl(std::ostream & os) const { os << "S =\n" << S << std::endl;}
201 
202  protected:
203  DenseBase S;
204  }; // class ConstraintTpl
205 
210 
211 } // namespace se3
212 
213 #endif // ifndef __se3_constraint_hpp__
Concreate Class representing a force.
Definition: force.hpp:243
SE3Tpl inverse() const
aXb = bXa.inverse()
Definition: se3.hpp:212
const Vector6 & toVector() const
Return the force.
Definition: force.hpp:102