pinocchio 4.1.0
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
Loading...
Searching...
No Matches
motion.hpp
1//
2// Copyright (c) 2015-2018 CNRS
3// Copyright (c) 2018-2025 INRIA
4// Copyright (c) 2016 Wandercraft, 86 rue de Paris 91400 Orsay, France.
5//
6
7#pragma once
8
9#include <eigenpy/eigenpy.hpp>
10#include <eigenpy/memory.hpp>
11#include <eigenpy/copyable.hpp>
12
13#include <boost/python/tuple.hpp>
14#include <boost/python/implicit.hpp>
15
16#include "pinocchio/spatial.hpp"
17
18#include "pinocchio/bindings/python/utils/cast.hpp"
19#include "pinocchio/bindings/python/utils/printable.hpp"
20
21namespace pinocchio
22{
23 namespace python
24 {
25 namespace bp = boost::python;
26
27 template<typename T>
28 struct call;
29
30 template<typename Scalar, int Options>
31 struct call<MotionTpl<Scalar, Options>>
32 {
33 typedef MotionTpl<Scalar, Options> Motion;
34
35 static bool isApprox(
36 const Motion & self,
37 const Motion & other,
38 const Scalar & prec = Eigen::NumTraits<Scalar>::dummy_precision())
39 {
40 return self.isApprox(other, prec);
41 }
42
43 static bool
44 isZero(const Motion & self, const Scalar & prec = Eigen::NumTraits<Scalar>::dummy_precision())
45 {
46 return self.isZero(prec);
47 }
48 };
49
50 template<typename Motion>
51 struct MotionPythonVisitor : public boost::python::def_visitor<MotionPythonVisitor<Motion>>
52 {
53 static constexpr int Options = traits<Motion>::Options;
54
55 typedef typename Motion::Scalar Scalar;
56 typedef ForceTpl<Scalar, Options> Force;
57 typedef typename Motion::Vector6 Vector6;
58 typedef typename Motion::Vector3 Vector3;
59
60 typedef typename Eigen::Map<Vector3> MapVector3;
61 typedef typename Eigen::Ref<Vector3> RefVector3;
62
63 public:
64 template<class PyClass>
65 void visit(PyClass & cl) const
66 {
67 static const Scalar dummy_precision = Eigen::NumTraits<Scalar>::dummy_precision();
68 PINOCCHIO_COMPILER_DIAGNOSTIC_PUSH
69 PINOCCHIO_COMPILER_DIAGNOSTIC_IGNORED_SELF_ASSIGN_OVERLOADED
70 cl.def(bp::init<>(bp::arg("self"), "Default constructor"))
71 .def(
72 bp::init<const Vector3 &, const Vector3 &>(
73 (bp::arg("self"), bp::arg("linear"), bp::arg("angular")),
74 "Initialize from linear and angular components of a Motion vector (don't mix the "
75 "order)."))
76 .def(
77 bp::init<const Vector6 &>(
78 (bp::arg("self"), bp::arg("array")),
79 "Init from a vector 6 [linear velocity, angular velocity]"))
80 .def(bp::init<const Motion &>((bp::arg("self"), bp::arg("clone")), "Copy constructor"))
81
82 .add_property(
83 "linear",
84 bp::make_function(
85 &MotionPythonVisitor::getLinear, bp::with_custodian_and_ward_postcall<0, 1>()),
86 &MotionPythonVisitor::setLinear,
87 "Linear part of a *this, corresponding to the linear velocity in case of a Spatial "
88 "velocity.")
89 .add_property(
90 "angular",
91 bp::make_function(
92 &MotionPythonVisitor::getAngular, bp::with_custodian_and_ward_postcall<0, 1>()),
93 &MotionPythonVisitor::setAngular,
94 "Angular part of a *this, corresponding to the angular velocity in case of a Spatial "
95 "velocity.")
96 .add_property(
97 "vector",
98 bp::make_function(
99 (typename Motion::ToVectorReturnType (Motion::*)())&Motion::toVector,
100 bp::return_internal_reference<>()),
101 &MotionPythonVisitor::setVector, "Returns the components of *this as a 6d vector.")
102 .add_property(
103 "np", bp::make_function(
104 (typename Motion::ToVectorReturnType (Motion::*)())&Motion::toVector,
105 bp::return_internal_reference<>()))
106
107 .def(
108 "se3Action", &Motion::template se3Action<Scalar, Options>, bp::args("self", "M"),
109 "Returns the result of the action of M on *this.")
110 .def(
111 "se3ActionInverse", &Motion::template se3ActionInverse<Scalar, Options>,
112 bp::args("self", "M"), "Returns the result of the action of the inverse of M on *this.")
113
114 .add_property(
115 "action", &Motion::toActionMatrix,
116 "Returns the action matrix of *this (acting on Motion).")
117 .add_property(
118 "dualAction", &Motion::toDualActionMatrix,
119 "Returns the dual action matrix of *this (acting on Force).")
120 .add_property(
121 "homogeneous", &Motion::toHomogeneousMatrix,
122 "Equivalent homogeneous representation of the Motion vector")
123
124 .def(
125 "setZero", &MotionPythonVisitor::setZero, bp::arg("self"),
126 "Set the linear and angular components of *this to zero.")
127 .def(
128 "setRandom", &MotionPythonVisitor::setRandom, bp::arg("self"),
129 "Set the linear and angular components of *this to random values.")
130
131 .def(
132 "dot", (Scalar (Motion::*)(const ForceBase<Force> &) const) & Motion::dot,
133 bp::args("self", "f"), "Dot product between *this and a Force f.")
134
135 .def(
136 "cross", (Motion (Motion::*)(const Motion &) const) & Motion::cross,
137 bp::args("self", "m"), "Action of *this onto another Motion m. Returns ยจ*this x m.")
138 .def(
139 "cross", (Force (Motion::*)(const Force &) const) & Motion::cross,
140 bp::args("self", "f"), "Dual action of *this onto a Force f. Returns *this x* f.")
141
142 .def(bp::self + bp::self)
143 .def(bp::self += bp::self)
144 .def(bp::self - bp::self)
145 .def(bp::self -= bp::self)
146 .def(-bp::self)
147 .def(bp::self ^ bp::self)
148 .def(bp::self ^ Force())
149
150#ifndef PINOCCHIO_PYTHON_SKIP_COMPARISON_OPERATIONS
151 .def(bp::self == bp::self)
152 .def(bp::self != bp::self)
153#endif
154
155 .def(bp::self * Scalar())
156 .def(Scalar() * bp::self)
157 .def(bp::self / Scalar())
158
159#ifndef PINOCCHIO_PYTHON_SKIP_COMPARISON_OPERATIONS
160 .def(
161 "isApprox", &call<Motion>::isApprox,
162 (bp::arg("self"), bp::arg("other"), bp::arg("prec") = dummy_precision),
163 "Returns true if *this is approximately equal to other, within the precision given by "
164 "prec.")
165
166 .def(
167 "isZero", &call<Motion>::isZero, (bp::arg("self"), bp::arg("prec") = dummy_precision),
168 "Returns true if *this is approximately equal to the zero Motion, within the precision "
169 "given by prec.")
170#endif
171
172 .def("Random", &Motion::Random, "Returns a random Motion.")
173 .staticmethod("Random")
174 .def("Zero", &Motion::Zero, "Returns a zero Motion.")
175 .staticmethod("Zero")
176
177 .def(
178 "__array__", bp::make_function(
179 (typename Motion::ToVectorReturnType (Motion::*)())&Motion::toVector,
180 bp::return_internal_reference<>()))
181 .def(
182 "__array__", &__array__,
183 (bp::arg("self"), bp::arg("dtype") = bp::object(), bp::arg("copy") = bp::object()),
184 bp::return_internal_reference<>())
185#ifndef PINOCCHIO_PYTHON_NO_SERIALIZATION
186 .def_pickle(Pickle())
187#endif
188 ;
189 PINOCCHIO_COMPILER_DIAGNOSTIC_POP
190 }
191
192 static void expose()
193 {
194 typedef pinocchio::MotionBase<Motion> MotionBase;
195 bp::objects::register_dynamic_id<MotionBase>();
196 bp::objects::register_conversion<Motion, MotionBase>(false);
197
198 typedef pinocchio::MotionDense<Motion> MotionDense;
199 bp::objects::register_dynamic_id<MotionDense>();
200 bp::objects::register_conversion<Motion, MotionDense>(false);
201
202 typedef ::boost::python::detail::not_specified HolderType;
203 bp::class_<Motion, HolderType>(
204 "Motion",
205 "Motion vectors, in se3 == M^6.\n\n"
206 "Supported operations ...",
207 bp::no_init)
211 .def(::eigenpy::CopyableVisitor<Motion>())
213 }
214
215 private:
216 static typename Motion::ToVectorConstReturnType
217 __array__(const Motion & self, bp::object, bp::object)
218 {
219 return self.toVector();
220 }
221
222 struct Pickle : bp::pickle_suite
223 {
224 static boost::python::tuple getinitargs(const Motion & m)
225 {
226 return bp::make_tuple((Vector3)m.linear(), (Vector3)m.angular());
227 }
228
229 static bool getstate_manages_dict()
230 {
231 return true;
232 }
233 };
234
235 static RefVector3 getLinear(Motion & self)
236 {
237 return self.linear();
238 }
239 static void setLinear(Motion & self, const Vector3 & v)
240 {
241 self.linear(v);
242 }
243 static RefVector3 getAngular(Motion & self)
244 {
245 return self.angular();
246 }
247 static void setAngular(Motion & self, const Vector3 & w)
248 {
249 self.angular(w);
250 }
251
252 static void setVector(Motion & self, const Vector6 & v)
253 {
254 self = v;
255 }
256
257 static void setZero(Motion & self)
258 {
259 self.setZero();
260 }
261 static void setRandom(Motion & self)
262 {
263 self.setRandom();
264 }
265 };
266
267 } // namespace python
268} // namespace pinocchio
Main pinocchio namespace.
Definition treeview.dox:11
Add the Python method cast.
Definition cast.hpp:23
Set the Python method str and repr to use the overloading operator<<.
Definition printable.hpp:21