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 
13 #include "pinocchio/spatial/se3.hpp"
14 #include "pinocchio/spatial/force.hpp"
18 
20 
21 namespace pinocchio
22 {
23  namespace python
24  {
25  namespace bp = boost::python;
26 
27  template<typename T> struct call;
28 
29  template<typename Scalar, int Options>
31  {
33 
34  static bool isApprox(const Force & self, const Force & other,
35  const Scalar & prec = Eigen::NumTraits<Scalar>::dummy_precision())
36  {
37  return self.isApprox(other,prec);
38  }
39 
40  static bool isZero(const Force & self,
41  const Scalar & prec = Eigen::NumTraits<Scalar>::dummy_precision())
42  {
43  return self.isZero(prec);
44  }
45  };
46 
47  BOOST_PYTHON_FUNCTION_OVERLOADS(isApproxForce_overload,call<Force>::isApprox,2,3)
49 
50  template<typename Force>
52  : public boost::python::def_visitor< ForcePythonVisitor<Force> >
53  {
55 
56  typedef typename Force::Vector6 Vector6;
57  typedef typename Force::Vector3 Vector3;
58  typedef typename Force::Scalar Scalar;
59 
60  typedef typename Eigen::Map<Vector3> MapVector3;
61  typedef typename Eigen::Ref<Vector3> RefVector3;
62 
63  template<class PyClass>
64  void visit(PyClass& cl) const
65  {
66  cl
67  .def(bp::init<>(bp::arg("self"),"Default constructor"))
68  .def(bp::init<Vector3,Vector3>
69  (bp::args("self","linear","angular"),
70  "Initialize from linear and angular components of a Wrench vector (don't mix the order)."))
71  .def(bp::init<Vector6>((bp::args("self","array")),"Init from a vector 6 [force,torque]"))
72  .def(bp::init<Force>((bp::args("self","other")),"Copy constructor."))
73 
74  .add_property("linear",
75  bp::make_function(&ForcePythonVisitor::getLinear,
76  bp::with_custodian_and_ward_postcall<0,1>()),
78  "Linear part of a *this, corresponding to the linear velocity in case of a Spatial velocity.")
79  .add_property("angular",
80  bp::make_function(&ForcePythonVisitor::getAngular,
81  bp::with_custodian_and_ward_postcall<0,1>()),
83  "Angular part of a *this, corresponding to the angular velocity in case of a Spatial velocity.")
84  .add_property("vector",
85  bp::make_function((typename Force::ToVectorReturnType (Force::*)())&Force::toVector,
86  bp::return_internal_reference<>()),
88  "Returns the components of *this as a 6d vector.")
89  .add_property("np",
90  bp::make_function((typename Force::ToVectorReturnType (Force::*)())&Force::toVector,
91  bp::return_internal_reference<>()))
92 
93  .def("se3Action",&Force::template se3Action<Scalar,Options>,
94  bp::args("self","M"),"Returns the result of the dual action of M on *this.")
95  .def("se3ActionInverse",&Force::template se3ActionInverse<Scalar,Options>,
96  bp::args("self","M"),"Returns the result of the dual action of the inverse of M on *this.")
97 
98  .def("setZero",&ForcePythonVisitor::setZero,bp::arg("self"),
99  "Set the linear and angular components of *this to zero.")
100  .def("setRandom",&ForcePythonVisitor::setRandom,bp::arg("self"),
101  "Set the linear and angular components of *this to random values.")
102 
103  .def(bp::self + bp::self)
104  .def(bp::self += bp::self)
105  .def(bp::self - bp::self)
106  .def(bp::self -= bp::self)
107  .def(-bp::self)
108 
109  .def(bp::self == bp::self)
110  .def(bp::self != bp::self)
111 
112  .def(bp::self * Scalar())
113  .def(Scalar() * bp::self)
114  .def(bp::self / Scalar())
115 
116  .def("isApprox",
118  isApproxForce_overload(bp::args("self","other","prec"),
119  "Returns true if *this is approximately equal to other, within the precision given by prec."))
120 
121  .def("isZero",
123  isZero_overload(bp::args("self","prec"),
124  "Returns true if *this is approximately equal to the zero Force, within the precision given by prec."))
125 
126  .def("Random",&Force::Random,"Returns a random Force.")
127  .staticmethod("Random")
128  .def("Zero",&Force::Zero,"Returns a zero Force.")
129  .staticmethod("Zero")
130 
131  .def("__array__",bp::make_function((typename Force::ToVectorReturnType (Force::*)())&Force::toVector,
132  bp::return_internal_reference<>()))
133 
134  .def_pickle(Pickle())
135  ;
136  }
137 
138  static void expose()
139  {
140 #if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION == 6 && EIGENPY_VERSION_AT_LEAST(2,9,0)
141  typedef PINOCCHIO_SHARED_PTR_HOLDER_TYPE(Force) HolderType;
142 #else
143  typedef ::boost::python::detail::not_specified HolderType;
144 #endif
145  bp::class_<Force,HolderType>("Force",
146  "Force vectors, in se3* == F^6.\n\n"
147  "Supported operations ...",
148  bp::no_init)
152  ;
153 
154  }
155 
156  private:
157 
158  struct Pickle : bp::pickle_suite
159  {
160  static
161  boost::python::tuple
162  getinitargs(const Force & f)
163  { return bp::make_tuple((Vector3)f.linear(),(Vector3)f.angular()); }
164 
165  static bool getstate_manages_dict() { return true; }
166  };
167 
168  static RefVector3 getLinear(Force & self ) { return self.linear(); }
169  static void setLinear(Force & self, const Vector3 & f) { self.linear(f); }
170  static RefVector3 getAngular(Force & self) { return self.angular(); }
171  static void setAngular(Force & self, const Vector3 & n) { self.angular(n); }
172 
173  static void setZero(Force & self) { self.setZero(); }
174  static void setRandom(Force & self) { self.setRandom(); }
175 
176  static void setVector(Force & self, const Vector6 & f) { self = f; }
177 
178  };
179 
180  } // namespace python
181 } // namespace pinocchio
182 
183 #endif // ifndef __pinocchio_python_spatial_force_hpp__
static ForceTpl Random()
Definition: force-tpl.hpp:88
static bool isZero(const Force &self, const Scalar &prec=Eigen::NumTraits< Scalar >::dummy_precision())
Vec3f n
ToVectorConstReturnType toVector() const
Return the force as an Eigen vector.
Definition: force-base.hpp:81
Set the Python method str and repr to use the overloading operator<<.
Definition: printable.hpp:21
void def(const char *name, Func func)
BOOST_PYTHON_FUNCTION_OVERLOADS(computeKKTContactDynamicMatrixInverse_overload, computeKKTContactDynamicMatrixInverse_proxy, 4, 5) static const Eigen
ConstAngularType angular() const
Return the angular part of the force vector.
Definition: force-base.hpp:35
SE3::Scalar Scalar
Definition: conversions.cpp:13
static void setLinear(Force &self, const Vector3 &f)
static ForceTpl Zero()
Definition: force-tpl.hpp:87
static void setVector(Force &self, const Vector6 &f)
#define PINOCCHIO_SHARED_PTR_HOLDER_TYPE(T)
Add the Python method copy to allow a copy of this by calling the copy constructor.
Definition: copyable.hpp:21
static bool isApprox(const Force &self, const Force &other, const Scalar &prec=Eigen::NumTraits< Scalar >::dummy_precision())
ConstLinearType linear() const
Return the linear part of the force vector.
Definition: force-base.hpp:42
Main pinocchio namespace.
Definition: timings.cpp:28
static boost::python::tuple getinitargs(const Force &f)
Common traits structure to fully define base classes for CRTP.
Definition: src/fwd.hpp:44
#define EIGENPY_DEFINE_STRUCT_ALLOCATOR_SPECIALIZATION(...)
static void setAngular(Force &self, const Vector3 &n)


pinocchio
Author(s):
autogenerated on Fri Jun 23 2023 02:38:30