bindings/python/spatial/force.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2015-2023 CNRS INRIA
3 // Copyright (c) 2016 Wandercraft, 86 rue de Paris 91400 Orsay, France.
4 //
5 
6 #ifndef __pinocchio_python_spatial_force_hpp__
7 #define __pinocchio_python_spatial_force_hpp__
8 
9 #include <eigenpy/eigenpy.hpp>
10 #include <eigenpy/memory.hpp>
11 #include <boost/python/tuple.hpp>
12 
15 
19 
20 #if EIGENPY_VERSION_AT_MOST(2, 8, 1)
22 #endif
23 
24 namespace pinocchio
25 {
26  namespace python
27  {
28  namespace bp = boost::python;
29 
30  template<typename T>
31  struct call;
32 
33  template<typename Scalar, int Options>
35  {
37 
38  static bool isApprox(
39  const Force & self,
40  const Force & other,
41  const Scalar & prec = Eigen::NumTraits<Scalar>::dummy_precision())
42  {
43  return self.isApprox(other, prec);
44  }
45 
46  static bool
47  isZero(const Force & self, const Scalar & prec = Eigen::NumTraits<Scalar>::dummy_precision())
48  {
49  return self.isZero(prec);
50  }
51  };
52 
53  template<typename Force>
54  struct ForcePythonVisitor : public boost::python::def_visitor<ForcePythonVisitor<Force>>
55  {
56  enum
57  {
59  };
60 
61  typedef typename Force::Vector6 Vector6;
62  typedef typename Force::Vector3 Vector3;
63  typedef typename Force::Scalar Scalar;
64 
65  typedef typename Eigen::Map<Vector3> MapVector3;
66  typedef typename Eigen::Ref<Vector3> RefVector3;
67 
68  template<class PyClass>
69  void visit(PyClass & cl) const
70  {
71  static const Scalar dummy_precision = Eigen::NumTraits<Scalar>::dummy_precision();
74  cl.def(bp::init<>(bp::arg("self"), "Default constructor"))
75  .def(bp::init<const Vector3 &, const Vector3 &>(
76  (bp::arg("self"), bp::arg("linear"), bp::arg("angular")),
77  "Initialize from linear and angular components of a Wrench vector (don't mix the "
78  "order)."))
79  .def(bp::init<const Vector6 &>(
80  (bp::args("self", "array")), "Init from a vector 6 [force,torque]"))
81  .def(bp::init<const Force &>((bp::arg("self"), bp::arg("clone")), "Copy constructor"))
82 
83  .add_property(
84  "linear",
85  bp::make_function(
86  &ForcePythonVisitor::getLinear, bp::with_custodian_and_ward_postcall<0, 1>()),
88  "Linear part of a *this, corresponding to the linear velocity in case of a "
89  "Spatial velocity.")
90  .add_property(
91  "angular",
92  bp::make_function(
93  &ForcePythonVisitor::getAngular, bp::with_custodian_and_ward_postcall<0, 1>()),
95  "Angular part of a *this, corresponding to the angular velocity in case of "
96  "a Spatial velocity.")
97  .add_property(
98  "vector",
99  bp::make_function(
100  (typename Force::ToVectorReturnType(Force::*)()) & Force::toVector,
101  bp::return_internal_reference<>()),
102  &ForcePythonVisitor::setVector, "Returns the components of *this as a 6d vector.")
103  .add_property(
104  "np", bp::make_function(
105  (typename Force::ToVectorReturnType(Force::*)()) & Force::toVector,
106  bp::return_internal_reference<>()))
107 
108  .def(
109  "se3Action", &Force::template se3Action<Scalar, Options>, bp::args("self", "M"),
110  "Returns the result of the dual action of M on *this.")
111  .def(
112  "se3ActionInverse", &Force::template se3ActionInverse<Scalar, Options>,
113  bp::args("self", "M"),
114  "Returns the result of the dual action of the inverse of M on *this.")
115 
116  .def(
117  "setZero", &ForcePythonVisitor::setZero, bp::arg("self"),
118  "Set the linear and angular components of *this to zero.")
119  .def(
120  "setRandom", &ForcePythonVisitor::setRandom, bp::arg("self"),
121  "Set the linear and angular components of *this to random values.")
122 
123  .def(
124  "dot", (Scalar(Force::*)(const MotionDense<context::Motion> &) const)&Force::dot,
125  bp::args("self", "m"), "Dot product between *this and a Motion m.")
126 
127  .def(bp::self + bp::self)
128  .def(bp::self += bp::self)
129  .def(bp::self - bp::self)
130  .def(bp::self -= bp::self)
131  .def(-bp::self)
132 
133 #ifndef PINOCCHIO_PYTHON_SKIP_COMPARISON_OPERATIONS
134  .def(bp::self == bp::self)
135  .def(bp::self != bp::self)
136 #endif
137 
138  .def(bp::self * Scalar())
139  .def(Scalar() * bp::self)
140  .def(bp::self / Scalar())
141 
142 #ifndef PINOCCHIO_PYTHON_SKIP_COMPARISON_OPERATIONS
143  .def(
144  "isApprox", &call<Force>::isApprox,
145  (bp::arg("self"), bp::arg("other"), bp::arg("prec") = dummy_precision),
146  "Returns true if *this is approximately equal to other, within the precision given "
147  "by prec.")
148 
149  .def(
150  "isZero", &call<Force>::isZero, (bp::arg("self"), bp::arg("prec") = dummy_precision),
151  "Returns true if *this is approximately equal to the zero Force, within the "
152  "precision given by prec.")
153 #endif
154 
155  .def("Random", &Force::Random, "Returns a random Force.")
156  .staticmethod("Random")
157  .def("Zero", &Force::Zero, "Returns a zero Force.")
158  .staticmethod("Zero")
159 
160  .def(
161  "__array__", bp::make_function(
162  (typename Force::ToVectorReturnType(Force::*)()) & Force::toVector,
163  bp::return_internal_reference<>()))
164  .def(
165  "__array__", &__array__,
166  (bp::arg("self"), bp::arg("dtype") = bp::object(), bp::arg("copy") = bp::object()),
167  bp::return_internal_reference<>())
168 #ifndef PINOCCHIO_PYTHON_NO_SERIALIZATION
169  .def_pickle(Pickle())
170 #endif
171  ;
173  }
174 
175  static void expose()
176  {
178  bp::objects::register_dynamic_id<ForceBase>();
179  bp::objects::register_conversion<Force, ForceBase>(false);
180 
182  bp::objects::register_dynamic_id<ForceBase>();
183  bp::objects::register_conversion<Force, ForceDense>(false);
184 
185 #if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION == 6 && EIGENPY_VERSION_AT_LEAST(2, 9, 0)
186  typedef PINOCCHIO_SHARED_PTR_HOLDER_TYPE(Force) HolderType;
187 #else
188  typedef ::boost::python::detail::not_specified HolderType;
189 #endif
190  bp::class_<Force, HolderType>(
191  "Force",
192  "Force vectors, in se3* == F^6.\n\n"
193  "Supported operations ...",
194  bp::no_init)
196  .def(CastVisitor<Force>())
198  .def(CopyableVisitor<Force>())
199  .def(PrintableVisitor<Force>());
200  }
201 
202  private:
203  static typename Force::ToVectorConstReturnType
204  __array__(const Force & self, bp::object, bp::object)
205  {
206  return self.toVector();
207  }
208 
209  struct Pickle : bp::pickle_suite
210  {
211  static boost::python::tuple getinitargs(const Force & f)
212  {
213  return bp::make_tuple((Vector3)f.linear(), (Vector3)f.angular());
214  }
215 
216  static bool getstate_manages_dict()
217  {
218  return true;
219  }
220  };
221 
222  static RefVector3 getLinear(Force & self)
223  {
224  return self.linear();
225  }
226  static void setLinear(Force & self, const Vector3 & f)
227  {
228  self.linear(f);
229  }
230  static RefVector3 getAngular(Force & self)
231  {
232  return self.angular();
233  }
234  static void setAngular(Force & self, const Vector3 & n)
235  {
236  self.angular(n);
237  }
238 
239  static void setZero(Force & self)
240  {
241  self.setZero();
242  }
243  static void setRandom(Force & self)
244  {
245  self.setRandom();
246  }
247 
248  static void setVector(Force & self, const Vector6 & f)
249  {
250  self = f;
251  }
252  };
253 
254  } // namespace python
255 } // namespace pinocchio
256 
257 #endif // ifndef __pinocchio_python_spatial_force_hpp__
boost::python
PINOCCHIO_SHARED_PTR_HOLDER_TYPE
#define PINOCCHIO_SHARED_PTR_HOLDER_TYPE(T)
Definition: bindings/python/fwd.hpp:17
pinocchio::python::ForcePythonVisitor::RefVector3
Eigen::Ref< Vector3 > RefVector3
Definition: bindings/python/spatial/force.hpp:66
eigenpy.hpp
pinocchio::python::ForcePythonVisitor::setLinear
static void setLinear(Force &self, const Vector3 &f)
Definition: bindings/python/spatial/force.hpp:226
pinocchio::ForceBase
Base interface for forces representation.
Definition: context/casadi.hpp:32
pinocchio::python::ForcePythonVisitor::setAngular
static void setAngular(Force &self, const Vector3 &n)
Definition: bindings/python/spatial/force.hpp:234
pinocchio::python::ForcePythonVisitor::MapVector3
Eigen::Map< Vector3 > MapVector3
Definition: bindings/python/spatial/force.hpp:65
PINOCCHIO_COMPILER_DIAGNOSTIC_IGNORED_SELF_ASSIGN_OVERLOADED
#define PINOCCHIO_COMPILER_DIAGNOSTIC_IGNORED_SELF_ASSIGN_OVERLOADED
Definition: include/pinocchio/macros.hpp:133
pinocchio::python::ExposeConstructorByCastVisitor
Definition: bindings/python/utils/cast.hpp:33
PINOCCHIO_COMPILER_DIAGNOSTIC_POP
#define PINOCCHIO_COMPILER_DIAGNOSTIC_POP
Definition: include/pinocchio/macros.hpp:130
pinocchio::MotionDense
Definition: context/casadi.hpp:36
pinocchio::python::Scalar
context::Scalar Scalar
Definition: admm-solver.cpp:29
pinocchio::python::ForcePythonVisitor::getLinear
static RefVector3 getLinear(Force &self)
Definition: bindings/python/spatial/force.hpp:222
pinocchio::python::ForcePythonVisitor::Pickle::getstate_manages_dict
static bool getstate_manages_dict()
Definition: bindings/python/spatial/force.hpp:216
pinocchio::python::ForcePythonVisitor::Vector6
Force::Vector6 Vector6
Definition: bindings/python/spatial/force.hpp:61
pinocchio::python::ForcePythonVisitor::setVector
static void setVector(Force &self, const Vector6 &f)
Definition: bindings/python/spatial/force.hpp:248
pinocchio::python::ForcePythonVisitor::Scalar
Force::Scalar Scalar
Definition: bindings/python/spatial/force.hpp:63
autodiff-rnea.f
f
Definition: autodiff-rnea.py:24
pinocchio::python::ForcePythonVisitor::Vector3
Force::Vector3 Vector3
Definition: bindings/python/spatial/force.hpp:62
pinocchio::python::call
Definition: bindings/python/spatial/force.hpp:31
pinocchio::python::Options
@ Options
Definition: expose-contact-inverse-dynamics.cpp:22
cast.hpp
EIGENPY_DEFINE_STRUCT_ALLOCATOR_SPECIALIZATION
#define EIGENPY_DEFINE_STRUCT_ALLOCATOR_SPECIALIZATION(...)
pinocchio::python::ForcePythonVisitor::__array__
static Force::ToVectorConstReturnType __array__(const Force &self, bp::object, bp::object)
Definition: bindings/python/spatial/force.hpp:204
pinocchio::python::PrintableVisitor
Set the Python method str and repr to use the overloading operator<<.
Definition: printable.hpp:21
pinocchio::python::ForcePythonVisitor::setRandom
static void setRandom(Force &self)
Definition: bindings/python/spatial/force.hpp:243
pinocchio::python::ForcePythonVisitor::Pickle::getinitargs
static boost::python::tuple getinitargs(const Force &f)
Definition: bindings/python/spatial/force.hpp:211
pinocchio::python::call< ForceTpl< Scalar, Options > >::isApprox
static bool isApprox(const Force &self, const Force &other, const Scalar &prec=Eigen::NumTraits< Scalar >::dummy_precision())
Definition: bindings/python/spatial/force.hpp:38
python
se3.hpp
pinocchio::ForceTpl
Definition: context/casadi.hpp:25
pinocchio::python::ForcePythonVisitor
Definition: bindings/python/spatial/force.hpp:54
pinocchio::python::CopyableVisitor
Add the Python method copy to allow a copy of this by calling the copy constructor.
Definition: copyable.hpp:21
pinocchio::python::ForcePythonVisitor::Options
@ Options
Definition: bindings/python/spatial/force.hpp:58
PINOCCHIO_COMPILER_DIAGNOSTIC_PUSH
#define PINOCCHIO_COMPILER_DIAGNOSTIC_PUSH
macros for pragma push/pop/ignore deprecated warnings
Definition: include/pinocchio/macros.hpp:129
pinocchio::context::Vector3
Eigen::Matrix< Scalar, 3, 1, Options > Vector3
Definition: context/generic.hpp:53
pinocchio::python::ForcePythonVisitor::visit
void visit(PyClass &cl) const
Definition: bindings/python/spatial/force.hpp:69
pinocchio::python::CastVisitor
Add the Python method cast.
Definition: bindings/python/utils/cast.hpp:23
copyable.hpp
pinocchio::ForceTpl::Random
static ForceTpl Random()
Definition: force-tpl.hpp:114
pinocchio::python::ForcePythonVisitor::Pickle
Definition: bindings/python/spatial/force.hpp:209
pinocchio::python::call< ForceTpl< Scalar, Options > >::Force
ForceTpl< Scalar, Options > Force
Definition: bindings/python/spatial/force.hpp:36
pinocchio::python::ForcePythonVisitor::setZero
static void setZero(Force &self)
Definition: bindings/python/spatial/force.hpp:239
pinocchio::ForceDense
Definition: context/casadi.hpp:34
cl
cl
memory.hpp
printable.hpp
pinocchio::traits
Common traits structure to fully define base classes for CRTP.
Definition: fwd.hpp:71
pinocchio::ForceTpl::Zero
static ForceTpl Zero()
Definition: force-tpl.hpp:110
pinocchio::python::ForcePythonVisitor::getAngular
static RefVector3 getAngular(Force &self)
Definition: bindings/python/spatial/force.hpp:230
force.hpp
pinocchio::python::call< ForceTpl< Scalar, Options > >::isZero
static bool isZero(const Force &self, const Scalar &prec=Eigen::NumTraits< Scalar >::dummy_precision())
Definition: bindings/python/spatial/force.hpp:47
pinocchio::python::ForcePythonVisitor::expose
static void expose()
Definition: bindings/python/spatial/force.hpp:175
n
Vec3f n
pinocchio
Main pinocchio namespace.
Definition: timings.cpp:27


pinocchio
Author(s):
autogenerated on Thu Dec 19 2024 03:41:29