pinocchio  2.4.4
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
vector-space.hpp
1 //
2 // Copyright (c) 2016-2020 CNRS INRIA
3 //
4 
5 #ifndef __pinocchio_multibody_liegroup_vector_space_operation_hpp__
6 #define __pinocchio_multibody_liegroup_vector_space_operation_hpp__
7 
8 #include "pinocchio/multibody/liegroup/liegroup-base.hpp"
9 
10 #include <stdexcept>
11 #include <boost/integer/static_min_max.hpp>
12 
13 namespace pinocchio
14 {
15  template<int Dim, typename Scalar, int Options = 0> struct VectorSpaceOperationTpl;
16 
17  template<int Dim, typename _Scalar, int _Options>
18  struct traits< VectorSpaceOperationTpl<Dim,_Scalar,_Options> >
19  {
20  typedef _Scalar Scalar;
21  enum {
22  Options = _Options,
23  NQ = Dim,
24  NV = Dim
25  };
26  };
27 
28  template<int Dim, typename _Scalar, int _Options>
30  : public LieGroupBase< VectorSpaceOperationTpl<Dim,_Scalar,_Options> >
31  {
32  PINOCCHIO_LIE_GROUP_TPL_PUBLIC_INTERFACE(VectorSpaceOperationTpl);
33 
37  VectorSpaceOperationTpl(int size = boost::static_signed_max<0,Dim>::value)
38  : size_(size)
39  {
40  assert(size_.value() >= 0);
41  }
42 
46  : Base(), size_(other.size_.value())
47  {
48  assert(size_.value() >= 0);
49  }
50 
51  Index nq () const
52  {
53  return size_.value();
54  }
55  Index nv () const
56  {
57  return size_.value();
58  }
59 
60  ConfigVector_t neutral () const
61  {
62  return ConfigVector_t::Zero(size_.value());
63  }
64 
65  std::string name () const
66  {
67  std::ostringstream oss; oss << "R^" << nq();
68  return oss.str ();
69  }
70 
71  template <class ConfigL_t, class ConfigR_t, class Tangent_t>
72  static void difference_impl(const Eigen::MatrixBase<ConfigL_t> & q0,
73  const Eigen::MatrixBase<ConfigR_t> & q1,
74  const Eigen::MatrixBase<Tangent_t> & d)
75  {
76  PINOCCHIO_EIGEN_CONST_CAST(Tangent_t,d) = q1 - q0;
77  }
78 
79  template <ArgumentPosition arg, class ConfigL_t, class ConfigR_t, class JacobianOut_t>
80  void dDifference_impl (const Eigen::MatrixBase<ConfigL_t> &,
81  const Eigen::MatrixBase<ConfigR_t> &,
82  const Eigen::MatrixBase<JacobianOut_t> & J) const
83  {
84  if (arg == ARG0)
85  PINOCCHIO_EIGEN_CONST_CAST(JacobianOut_t,J) = -JacobianMatrix_t::Identity();
86  else if (arg == ARG1)
87  PINOCCHIO_EIGEN_CONST_CAST(JacobianOut_t,J).setIdentity();
88  }
89 
90  template <class ConfigIn_t, class Velocity_t, class ConfigOut_t>
91  static void integrate_impl(const Eigen::MatrixBase<ConfigIn_t> & q,
92  const Eigen::MatrixBase<Velocity_t> & v,
93  const Eigen::MatrixBase<ConfigOut_t> & qout)
94  {
95  PINOCCHIO_EIGEN_CONST_CAST(ConfigOut_t,qout) = q + v;
96  }
97 
98  template <class Config_t, class Jacobian_t>
99  static void integrateCoeffWiseJacobian_impl(const Eigen::MatrixBase<Config_t> &,
100  const Eigen::MatrixBase<Jacobian_t> & J)
101  {
102  PINOCCHIO_EIGEN_CONST_CAST(Jacobian_t,J).setIdentity();
103  }
104 
105  template <class Config_t, class Tangent_t, class JacobianOut_t>
106  static void dIntegrate_dq_impl(const Eigen::MatrixBase<Config_t > & /*q*/,
107  const Eigen::MatrixBase<Tangent_t> & /*v*/,
108  const Eigen::MatrixBase<JacobianOut_t> & J,
109  const AssignmentOperatorType op=SETTO)
110  {
111  Eigen::MatrixBase<JacobianOut_t>& Jout = PINOCCHIO_EIGEN_CONST_CAST(JacobianOut_t,J);
112  switch(op)
113  {
114  case SETTO:
115  Jout.setIdentity();
116  break;
117  case ADDTO:
118  Jout.diagonal().array() += Scalar(1);
119  break;
120  case RMTO:
121  Jout.diagonal().array() -= Scalar(1);
122  break;
123  default:
124  assert(false && "Wrong Op requesed value");
125  break;
126  }
127  }
128 
129  template <class Config_t, class Tangent_t, class JacobianOut_t>
130  static void dIntegrate_dv_impl(const Eigen::MatrixBase<Config_t > & /*q*/,
131  const Eigen::MatrixBase<Tangent_t> & /*v*/,
132  const Eigen::MatrixBase<JacobianOut_t> & J,
133  const AssignmentOperatorType op=SETTO)
134  {
135  Eigen::MatrixBase<JacobianOut_t>& Jout = PINOCCHIO_EIGEN_CONST_CAST(JacobianOut_t,J);
136  switch(op)
137  {
138  case SETTO:
139  Jout.setIdentity();
140  break;
141  case ADDTO:
142  Jout.diagonal().array() += Scalar(1);
143  break;
144  case RMTO:
145  Jout.diagonal().array() -= Scalar(1);
146  break;
147  default:
148  assert(false && "Wrong Op requesed value");
149  break;
150  }
151  }
152 
153 
154  template <class Config_t, class Tangent_t, class JacobianIn_t, class JacobianOut_t>
155  void dIntegrateTransport_dq_impl(const Eigen::MatrixBase<Config_t > & /*q*/,
156  const Eigen::MatrixBase<Tangent_t> & /*v*/,
157  const Eigen::MatrixBase<JacobianIn_t> & Jin,
158  const Eigen::MatrixBase<JacobianOut_t> & Jout) const
159  {
160  PINOCCHIO_EIGEN_CONST_CAST(JacobianOut_t,Jout) = Jin;
161  }
162 
163  template <class Config_t, class Tangent_t, class JacobianIn_t, class JacobianOut_t>
164  void dIntegrateTransport_dv_impl(const Eigen::MatrixBase<Config_t > & /*q*/,
165  const Eigen::MatrixBase<Tangent_t> & /*v*/,
166  const Eigen::MatrixBase<JacobianIn_t> & Jin,
167  const Eigen::MatrixBase<JacobianOut_t> & Jout) const
168  {
169  PINOCCHIO_EIGEN_CONST_CAST(JacobianOut_t,Jout) = Jin;
170  }
171 
172  template <class Config_t, class Tangent_t, class Jacobian_t>
173  void dIntegrateTransport_dq_impl(const Eigen::MatrixBase<Config_t > & /*q*/,
174  const Eigen::MatrixBase<Tangent_t> & /*v*/,
175  const Eigen::MatrixBase<Jacobian_t> & /*J*/) const {}
176 
177  template <class Config_t, class Tangent_t, class Jacobian_t>
178  void dIntegrateTransport_dv_impl(const Eigen::MatrixBase<Config_t > & /*q*/,
179  const Eigen::MatrixBase<Tangent_t> & /*v*/,
180  const Eigen::MatrixBase<Jacobian_t> & /*J*/) const {}
181 
182 
183 
184  // template <class ConfigL_t, class ConfigR_t>
185  // static double squaredDistance_impl(const Eigen::MatrixBase<ConfigL_t> & q0,
186  // const Eigen::MatrixBase<ConfigR_t> & q1)
187 
188  template <class Config_t>
189  static void normalize_impl (const Eigen::MatrixBase<Config_t>& /*qout*/)
190  {}
191 
192  template <class Config_t>
193  void random_impl (const Eigen::MatrixBase<Config_t>& qout) const
194  {
195  PINOCCHIO_EIGEN_CONST_CAST(Config_t,qout).setRandom();
196  }
197 
198  template <class ConfigL_t, class ConfigR_t, class ConfigOut_t>
199  void randomConfiguration_impl
200  (const Eigen::MatrixBase<ConfigL_t> & lower_pos_limit,
201  const Eigen::MatrixBase<ConfigR_t> & upper_pos_limit,
202  const Eigen::MatrixBase<ConfigOut_t> & qout) const
203  {
204  ConfigOut_t & res = PINOCCHIO_EIGEN_CONST_CAST(ConfigOut_t,qout).derived();
205  for (int i = 0; i < nq (); ++i)
206  {
207  if(lower_pos_limit[i] == -std::numeric_limits<typename ConfigL_t::Scalar>::infinity() ||
208  upper_pos_limit[i] == std::numeric_limits<typename ConfigR_t::Scalar>::infinity() )
209  {
210  std::ostringstream error;
211  error << "non bounded limit. Cannot uniformly sample joint at rank " << i;
212  // assert(false && "non bounded limit. Cannot uniformly sample joint revolute");
213  throw std::range_error(error.str());
214  }
215  res[i] = lower_pos_limit[i] + (( upper_pos_limit[i] - lower_pos_limit[i]) * rand())/RAND_MAX;
216  }
217  }
218 
219  private:
220 
221  Eigen::internal::variable_if_dynamic<Index, Dim> size_;
222  }; // struct VectorSpaceOperationTpl
223 
224 } // namespace pinocchio
225 
226 #endif // ifndef __pinocchio_multibody_liegroup_vector_space_operation_hpp__
int nv(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel)
Visit a JointModelTpl through JointNvVisitor to get the dimension of the joint tangent space...
int nq(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel)
Visit a JointModelTpl through JointNqVisitor to get the dimension of the joint configuration space...
VectorSpaceOperationTpl(int size=boost::static_signed_max< 0, Dim >::value)
std::string name(const LieGroupGenericTpl< LieGroupCollection > &lg)
Visit a LieGroupVariant to get the name of it.
VectorSpaceOperationTpl(const VectorSpaceOperationTpl &other)
Main pinocchio namespace.
Definition: treeview.dox:24
void neutral(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, const Eigen::MatrixBase< ReturnType > &qout)
Return the neutral configuration element related to the model configuration space.
Common traits structure to fully define base classes for CRTP.
Definition: fwd.hpp:35