pinocchio  UNKNOWN
liegroup-algo.hxx
1 //
2 // Copyright (c) 2018 CNRS
3 //
4 // This file is part of Pinocchio
5 // Pinocchio is free software: you can redistribute it
6 // and/or modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation, either version
8 // 3 of the License, or (at your option) any later version.
9 //
10 // Pinocchio is distributed in the hope that it will be
11 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Lesser Public License for more details. You should have
14 // received a copy of the GNU Lesser General Public License along with
15 // Pinocchio If not, see
16 // <http://www.gnu.org/licenses/>.
17 
18 #ifndef __se3_lie_group_algo_hxx__
19 #define __se3_lie_group_algo_hxx__
20 
21 #include "pinocchio/multibody/visitor.hpp"
22 
23 namespace se3
24 {
25 
26  namespace details
27  {
28  template<typename Algo>
29  struct Dispatch
30  {
31  static void run (const JointModelComposite& jmodel,
32  typename Algo::ArgsType args)
33  {
34  for (size_t i = 0; i < jmodel.joints.size(); ++i)
35  Algo::run(jmodel.joints[i], args);
36  }
37  };
38 
39 #define SE3_DETAILS_WRITE_ARGS_0(JM) const JointModelBase<JM> & jmodel
40 #define SE3_DETAILS_WRITE_ARGS_1(JM) SE3_DETAILS_WRITE_ARGS_0(JM), typename boost::fusion::result_of::at_c<ArgsType, 0>::type a0
41 #define SE3_DETAILS_WRITE_ARGS_2(JM) SE3_DETAILS_WRITE_ARGS_1(JM), typename boost::fusion::result_of::at_c<ArgsType, 1>::type a1
42 #define SE3_DETAILS_WRITE_ARGS_3(JM) SE3_DETAILS_WRITE_ARGS_2(JM), typename boost::fusion::result_of::at_c<ArgsType, 2>::type a2
43 #define SE3_DETAILS_WRITE_ARGS_4(JM) SE3_DETAILS_WRITE_ARGS_3(JM), typename boost::fusion::result_of::at_c<ArgsType, 3>::type a3
44 
45 #define SE3_DETAILS_DISPATCH_JOINT_COMPOSITE_1(Visitor, Algo) \
46  template <typename LieGroup_t> struct Algo <LieGroup_t, JointModelComposite> { \
47  typedef typename Visitor<LieGroup_t>::ArgsType ArgsType; \
48  static void run (SE3_DETAILS_WRITE_ARGS_1(JointModelComposite)) \
49  { ::se3::details::Dispatch< Visitor<LieGroup_t> >::run(jmodel.derived(), ArgsType(a0)); } \
50  }
51 
52 #define SE3_DETAILS_DISPATCH_JOINT_COMPOSITE_2(Visitor, Algo) \
53  template <typename LieGroup_t> struct Algo <LieGroup_t, JointModelComposite> { \
54  typedef typename Visitor<LieGroup_t>::ArgsType ArgsType; \
55  static void run (SE3_DETAILS_WRITE_ARGS_2(JointModelComposite)) \
56  { ::se3::details::Dispatch< Visitor<LieGroup_t> >::run(jmodel.derived(), ArgsType(a0, a1)); } \
57  }
58 
59 #define SE3_DETAILS_DISPATCH_JOINT_COMPOSITE_3(Visitor, Algo) \
60  template <typename LieGroup_t> struct Algo <LieGroup_t, JointModelComposite> { \
61  typedef typename Visitor<LieGroup_t>::ArgsType ArgsType; \
62  static void run (SE3_DETAILS_WRITE_ARGS_3(JointModelComposite)) \
63  { ::se3::details::Dispatch< Visitor<LieGroup_t> >::run(jmodel.derived(), ArgsType(a0, a1, a2)); } \
64  }
65 
66 #define SE3_DETAILS_DISPATCH_JOINT_COMPOSITE_4(Visitor, Algo) \
67  template <typename LieGroup_t> struct Algo <LieGroup_t, JointModelComposite> { \
68  typedef typename Visitor<LieGroup_t>::ArgsType ArgsType; \
69  static void run (SE3_DETAILS_WRITE_ARGS_4(JointModelComposite)) \
70  { ::se3::details::Dispatch< Visitor<LieGroup_t> >::run(jmodel.derived(), ArgsType(a0, a1, a2, a3)); } \
71  }
72 
73 #define SE3_DETAILS_VISITOR_METHOD_ALGO_1(Algo, TplParam) \
74  template<typename JointModel> \
75  static void algo(SE3_DETAILS_WRITE_ARGS_1(JointModel)) \
76  { Algo<TplParam, JointModel>::run(jmodel, a0); }
77 
78 #define SE3_DETAILS_VISITOR_METHOD_ALGO_2(Algo, TplParam) \
79  template<typename JointModel> \
80  static void algo(SE3_DETAILS_WRITE_ARGS_2(JointModel)) \
81  { Algo<TplParam, JointModel>::run(jmodel, a0, a1); }
82 
83 #define SE3_DETAILS_VISITOR_METHOD_ALGO_3(Algo, TplParam) \
84  template<typename JointModel> \
85  static void algo(SE3_DETAILS_WRITE_ARGS_3(JointModel)) \
86  { Algo<TplParam, JointModel>::run(jmodel, a0, a1, a2); }
87 
88 #define SE3_DETAILS_VISITOR_METHOD_ALGO_4(Algo, TplParam) \
89  template<typename JointModel> \
90  static void algo(SE3_DETAILS_WRITE_ARGS_4(JointModel)) \
91  { Algo<TplParam, JointModel>::run(jmodel, a0, a1, a2, a3); }
92 
93  } // namespace details
94 
95  template<typename LieGroup_t, typename JointModel> struct IntegrateStepAlgo;
96 
97  template<typename LieGroup_t>
98  struct IntegrateStep : public fusion::JointModelVisitor<IntegrateStep<LieGroup_t> >
99  {
100  typedef boost::fusion::vector<const Eigen::VectorXd &,
101  const Eigen::VectorXd &,
102  Eigen::VectorXd &
103  > ArgsType;
104 
105  JOINT_MODEL_VISITOR_INIT(IntegrateStep);
106 
107  SE3_DETAILS_VISITOR_METHOD_ALGO_3(IntegrateStepAlgo, LieGroup_t)
108  };
109 
110  template<typename LieGroup_t, typename JointModel>
111  struct IntegrateStepAlgo {
112  static void run(const se3::JointModelBase<JointModel> & jmodel,
113  const Eigen::VectorXd & q,
114  const Eigen::VectorXd & v,
115  Eigen::VectorXd & result)
116  {
117  typename LieGroup_t::template operation<JointModel>::type lgo;
118  lgo.integrate(jmodel.jointConfigSelector (q),
119  jmodel.jointVelocitySelector(v),
120  jmodel.jointConfigSelector (result));
121  }
122  };
123 
124  SE3_DETAILS_DISPATCH_JOINT_COMPOSITE_3(IntegrateStep, IntegrateStepAlgo);
125 
126  template<typename LieGroup_t, typename JointModel> struct InterpolateStepAlgo;
127 
128  template<typename LieGroup_t>
129  struct InterpolateStep : public fusion::JointModelVisitor<InterpolateStep<LieGroup_t> >
130  {
131  typedef boost::fusion::vector<const Eigen::VectorXd &,
132  const Eigen::VectorXd &,
133  const double,
134  Eigen::VectorXd &
135  > ArgsType;
136 
137  JOINT_MODEL_VISITOR_INIT(InterpolateStep);
138 
139  SE3_DETAILS_VISITOR_METHOD_ALGO_4(InterpolateStepAlgo, LieGroup_t)
140  };
141 
142  template<typename LieGroup_t, typename JointModel>
143  struct InterpolateStepAlgo {
144  static void run(const se3::JointModelBase<JointModel> & jmodel,
145  const Eigen::VectorXd & q0,
146  const Eigen::VectorXd & q1,
147  const double u,
148  Eigen::VectorXd & result)
149  {
150  typename LieGroup_t::template operation<JointModel>::type lgo;
151  lgo.interpolate(jmodel.jointConfigSelector(q0),
152  jmodel.jointConfigSelector(q1),
153  u,
154  jmodel.jointConfigSelector(result));
155  }
156  };
157 
158  SE3_DETAILS_DISPATCH_JOINT_COMPOSITE_4(InterpolateStep, InterpolateStepAlgo);
159 
160  template<typename LieGroup_t, typename JointModel> struct DifferenceStepAlgo;
161 
162  template<typename LieGroup_t>
163  struct DifferenceStep : public fusion::JointModelVisitor<DifferenceStep<LieGroup_t> >
164  {
165  typedef boost::fusion::vector<const Eigen::VectorXd &,
166  const Eigen::VectorXd &,
167  Eigen::VectorXd &
168  > ArgsType;
169 
170  JOINT_MODEL_VISITOR_INIT(DifferenceStep);
171 
172  SE3_DETAILS_VISITOR_METHOD_ALGO_3(DifferenceStepAlgo, LieGroup_t)
173  };
174 
175  template<typename LieGroup_t, typename JointModel>
176  struct DifferenceStepAlgo {
177  static void run(const se3::JointModelBase<JointModel> & jmodel,
178  const Eigen::VectorXd & q0,
179  const Eigen::VectorXd & q1,
180  Eigen::VectorXd & result)
181  {
182  typename LieGroup_t::template operation<JointModel>::type lgo;
183  lgo.difference(jmodel.jointConfigSelector(q0),
184  jmodel.jointConfigSelector(q1),
185  jmodel.jointVelocitySelector(result));
186  }
187  };
188 
189  SE3_DETAILS_DISPATCH_JOINT_COMPOSITE_3(DifferenceStep, DifferenceStepAlgo);
190 
191  template<typename LieGroup_t, typename JointModel> struct SquaredDistanceStepAlgo;
192 
193  template<typename LieGroup_t>
194  struct SquaredDistanceStep : public fusion::JointModelVisitor<SquaredDistanceStep<LieGroup_t> >
195  {
196  typedef boost::fusion::vector<const JointIndex,
197  const Eigen::VectorXd &,
198  const Eigen::VectorXd &,
199  Eigen::VectorXd &
200  > ArgsType;
201 
202  JOINT_MODEL_VISITOR_INIT(SquaredDistanceStep);
203 
204  SE3_DETAILS_VISITOR_METHOD_ALGO_4(SquaredDistanceStepAlgo, LieGroup_t)
205  };
206 
207  template<typename LieGroup_t, typename JointModel>
208  struct SquaredDistanceStepAlgo {
209  static void run(const se3::JointModelBase<JointModel> & jmodel,
210  const JointIndex i,
211  const Eigen::VectorXd & q0,
212  const Eigen::VectorXd & q1,
213  Eigen::VectorXd & distances)
214  {
215  typename LieGroup_t::template operation<JointModel>::type lgo;
216  distances[(long)i] += lgo.squaredDistance(jmodel.jointConfigSelector(q0),
217  jmodel.jointConfigSelector(q1));
218  }
219  };
220 
221  SE3_DETAILS_DISPATCH_JOINT_COMPOSITE_4(SquaredDistanceStep, SquaredDistanceStepAlgo);
222 
223  template<typename LieGroup_t, typename JointModel> struct RandomConfigurationStepAlgo;
224 
225  template<typename LieGroup_t>
226  struct RandomConfigurationStep : public fusion::JointModelVisitor<RandomConfigurationStep<LieGroup_t> >
227  {
228  typedef boost::fusion::vector<Eigen::VectorXd &,
229  const Eigen::VectorXd &,
230  const Eigen::VectorXd &
231  > ArgsType;
232 
233  JOINT_MODEL_VISITOR_INIT(RandomConfigurationStep);
234 
235  SE3_DETAILS_VISITOR_METHOD_ALGO_3(RandomConfigurationStepAlgo, LieGroup_t)
236  };
237 
238  template<typename LieGroup_t, typename JointModel>
240  static void run(const se3::JointModelBase<JointModel> & jmodel,
241  Eigen::VectorXd & q,
242  const Eigen::VectorXd & lowerLimits,
243  const Eigen::VectorXd & upperLimits)
244  {
245  typename LieGroup_t::template operation<JointModel>::type lgo;
246  lgo.randomConfiguration(jmodel.jointConfigSelector(lowerLimits),
247  jmodel.jointConfigSelector(upperLimits),
248  jmodel.jointConfigSelector(q));
249  }
250  };
251 
252  SE3_DETAILS_DISPATCH_JOINT_COMPOSITE_3(RandomConfigurationStep, RandomConfigurationStepAlgo);
253 
254 
255 
256  template<typename LieGroup_t, typename JointModel> struct NormalizeStepAlgo;
257 
258  template<typename LieGroup_t>
259  struct NormalizeStep : public fusion::JointModelVisitor< NormalizeStep<LieGroup_t> >
260  {
261  typedef boost::fusion::vector<Eigen::VectorXd &> ArgsType;
262 
263  JOINT_MODEL_VISITOR_INIT(NormalizeStep);
264 
265  SE3_DETAILS_VISITOR_METHOD_ALGO_1(NormalizeStepAlgo, LieGroup_t)
266  };
267 
268  template<typename LieGroup_t, typename JointModel>
269  struct NormalizeStepAlgo
270  {
271  static void run(const se3::JointModelBase<JointModel> & jmodel,
272  Eigen::VectorXd & qout)
273  {
274  typename LieGroup_t::template operation<JointModel>::type lgo;
275  lgo.normalize(jmodel.jointConfigSelector(qout));
276  }
277  };
278 
279  SE3_DETAILS_DISPATCH_JOINT_COMPOSITE_1(NormalizeStep, NormalizeStepAlgo);
280 
281  template<typename LieGroup_t, typename JointModel> struct IsSameConfigurationStepAlgo;
282 
283  template<typename LieGroup_t>
284  struct IsSameConfigurationStep : public fusion::JointModelVisitor<IsSameConfigurationStep<LieGroup_t> >
285  {
286  typedef boost::fusion::vector<bool &,
287  const Eigen::VectorXd &,
288  const Eigen::VectorXd &,
289  const double&> ArgsType;
290 
291  JOINT_MODEL_VISITOR_INIT(IsSameConfigurationStep);
292 
293  SE3_DETAILS_VISITOR_METHOD_ALGO_4(IsSameConfigurationStepAlgo, LieGroup_t)
294  };
295 
296  template<typename LieGroup_t, typename JointModel>
298  static void run(const se3::JointModelBase<JointModel> & jmodel,
299  bool & isSame,
300  const Eigen::VectorXd & q1,
301  const Eigen::VectorXd & q2,
302  const double prec)
303  {
304  typename LieGroup_t::template operation<JointModel>::type lgo;
305  isSame &= lgo.isSameConfiguration(jmodel.jointConfigSelector(q1),
306  jmodel.jointConfigSelector(q2),
307  prec);
308  }
309  };
310 
311  SE3_DETAILS_DISPATCH_JOINT_COMPOSITE_4(IsSameConfigurationStep, IsSameConfigurationStepAlgo);
312 
313  template<typename LieGroup_t, typename JointModel> struct NeutralStepAlgo;
314 
315  template<typename LieGroup_t>
316  struct NeutralStep : public fusion::JointModelVisitor< NeutralStep<LieGroup_t> >
317  {
318  typedef boost::fusion::vector<Eigen::VectorXd &> ArgsType;
319 
320  JOINT_MODEL_VISITOR_INIT(NeutralStep);
321 
322  SE3_DETAILS_VISITOR_METHOD_ALGO_1(NeutralStepAlgo, LieGroup_t)
323  };
324 
325  template<typename LieGroup_t, typename JointModel>
326  struct NeutralStepAlgo
327  {
328  static void run(const se3::JointModelBase<JointModel> & jmodel,
329  Eigen::VectorXd & neutral_elt)
330  {
331  typename LieGroup_t::template operation<JointModel>::type lgo;
332  jmodel.jointConfigSelector(neutral_elt) = lgo.neutral();
333  }
334  };
335 
336  SE3_DETAILS_DISPATCH_JOINT_COMPOSITE_1(NeutralStep, NeutralStepAlgo);
337 
338 }
339 
340 #endif // ifndef __se3_lie_group_algo_hxx__
JointModelVector joints
Vector of joints contained in the joint composite.