pinocchio  2.1.3
visitor.hpp
1 //
2 // Copyright (c) 2015,2018 CNRS
3 // Copyright (c) 2015 Wandercraft, 86 rue de Paris 91400 Orsay, France.
4 //
5 
6 #ifndef __pinocchio_visitor_hpp__
7 #define __pinocchio_visitor_hpp__
8 
9 #define BOOST_FUSION_INVOKE_MAX_ARITY 10
10 
11 #include <boost/variant.hpp>
12 #include <boost/fusion/include/invoke.hpp>
13 #include <boost/fusion/container/generation/make_vector.hpp>
14 
15 #include "pinocchio/multibody/joint/joint-base.hpp"
16 
17 namespace boost
18 {
19  namespace fusion
20  {
21 
22  // Append the element T at the front of boost fusion vector V.
23  template<typename T,typename V>
24  typename result_of::push_front<V const, T>::type
25  append(T const& t,V const& v)
26  { return push_front(v,t); }
27 
28  // Append the elements T1 and T2 at the front of boost fusion vector V.
29  template<typename T1,typename T2,typename V>
30  typename result_of::push_front<typename result_of::push_front<V const, T2>::type const, T1>::type
31  append2(T1 const& t1,T2 const& t2,V const& v)
32  { return push_front(push_front(v,t2),t1); }
33 
34  }
35 }
36 
37 namespace pinocchio
38 {
39  namespace fusion
40  {
41  namespace bf = boost::fusion;
42 
43  typedef boost::blank NoArg;
44 
45  template<typename JointVisitorDerived, typename ReturnType = void>
47  {
48 
49  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl, typename ArgsTmp>
50  static ReturnType run(const JointModelTpl<Scalar,Options,JointCollectionTpl> & jmodel,
52  ArgsTmp args)
53  {
54  InternalVisitor<JointModelTpl<Scalar,Options,JointCollectionTpl>,ArgsTmp> visitor(jdata,args);
55  return boost::apply_visitor(visitor,jmodel);
56  }
57 
58  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
59  static ReturnType run(const JointModelTpl<Scalar,Options,JointCollectionTpl> & jmodel,
61  {
62  InternalVisitor<JointModelTpl<Scalar,Options,JointCollectionTpl>,NoArg> visitor(jdata);
63  return boost::apply_visitor(visitor,jmodel);
64  }
65 
66  template<typename JointModelDerived, typename ArgsTmp>
67  static ReturnType run(const JointModelBase<JointModelDerived> & jmodel,
69  ArgsTmp args)
70  {
71  InternalVisitor<JointModelDerived,ArgsTmp> visitor(jdata,args);
72  return visitor(jmodel.derived());
73  }
74 
75  template<typename JointModelDerived>
76  static ReturnType run(const JointModelBase<JointModelDerived> & jmodel,
78  {
79  InternalVisitor<JointModelDerived,NoArg> visitor(jdata);
80  return visitor(jmodel.derived());
81  }
82 
83  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl, typename ArgsTmp>
84  static ReturnType run(const JointModelTpl<Scalar,Options,JointCollectionTpl> & jmodel,
85  ArgsTmp args)
86  {
87  ModelOnlyInternalVisitor<ArgsTmp> visitor(args);
88  return boost::apply_visitor(visitor,jmodel);
89  }
90 
91  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
92  static ReturnType run(const JointModelTpl<Scalar,Options,JointCollectionTpl> & jmodel)
93  {
94  ModelOnlyInternalVisitor<NoArg> visitor;
95  return boost::apply_visitor(visitor,jmodel);
96  }
97 
98  template<typename JointModelDerived, typename ArgsTmp>
99  static ReturnType run(const JointModelBase<JointModelDerived> & jmodel,
100  ArgsTmp args)
101  {
102  ModelOnlyInternalVisitor<ArgsTmp> visitor(args);
103  return visitor(jmodel.derived());
104  }
105 
106  template<typename JointModelDerived>
107  static ReturnType run(const JointModelBase<JointModelDerived> & jmodel)
108  {
109  ModelOnlyInternalVisitor<NoArg> visitor;
110  return visitor(jmodel.derived());
111  }
112 
113  private:
114 
115  template<typename JointModel, typename ArgType>
116  struct InternalVisitor
117  : public boost::static_visitor<ReturnType>
118  {
119  typedef typename JointModel::JointDataDerived JointData;
120 
121  InternalVisitor(JointData & jdata, ArgType args)
122  : jdata(jdata), args(args)
123  {}
124 
125  template<typename JointModelDerived>
126  ReturnType operator()(const JointModelBase<JointModelDerived> & jmodel) const
127  {
128  return bf::invoke(&JointVisitorDerived::template algo<JointModelDerived>,
129  bf::append2(boost::ref(jmodel),
130  boost::ref(boost::get<typename JointModelBase<JointModelDerived>::JointDataDerived >(jdata)),
131  args));
132  }
133 
134  ReturnType operator()(const JointModelVoid) {return;}
135 
136  JointData & jdata;
137  ArgType args;
138  };
139 
140  template<typename JointModel>
141  struct InternalVisitor<JointModel,NoArg>
142  : public boost::static_visitor<ReturnType>
143  {
144  typedef typename JointModel::JointDataDerived JointData;
145 
146  InternalVisitor(JointData & jdata)
147  : jdata(jdata)
148  {}
149 
150  template<typename JointModelDerived>
151  ReturnType operator()(const JointModelBase<JointModelDerived> & jmodel) const
152  {
153  return bf::invoke(&JointVisitorDerived::template algo<JointModelDerived>,
154  bf::make_vector(boost::ref(jmodel),
155  boost::ref(boost::get<typename JointModelBase<JointModelDerived>::JointDataDerived >(jdata)))
156  );
157  }
158 
159  JointData & jdata;
160  };
161 
162  template<typename ArgType, typename Dummy = void>
163  struct ModelOnlyInternalVisitor : public boost::static_visitor<ReturnType>
164  {
165  ModelOnlyInternalVisitor(ArgType args)
166  : args(args)
167  {}
168 
169  template<typename JointModelDerived>
170  ReturnType operator()(const JointModelBase<JointModelDerived> & jmodel) const
171  {
172  return bf::invoke(&JointVisitorDerived::template algo<JointModelDerived>,
173  bf::append(boost::ref(jmodel),
174  args));
175  }
176 
177  ReturnType operator()(const JointModelVoid) {return;}
178 
179  ArgType args;
180  };
181 
182  template<typename Dummy>
183  struct ModelOnlyInternalVisitor<NoArg,Dummy>
184  : public boost::static_visitor<ReturnType>
185  {
186  ModelOnlyInternalVisitor() {}
187 
188  template<typename JointModelDerived>
189  ReturnType operator()(const JointModelBase<JointModelDerived> & jmodel) const
190  {
191  return JointVisitorDerived::template algo<JointModelDerived>(jmodel);
192  }
193  };
194 
195  }; // struct JointVisitorBase
196 
197  } // namespace fusion
198 } // namespace pinocchio
199 
200 #endif // ifndef __pinocchio_visitor_hpp__
Main pinocchio namespace.
Definition: treeview.dox:24