pinocchio 4.1.0
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
Loading...
Searching...
No Matches
se3.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#include <boost/python/tuple.hpp>
13
14#include "pinocchio/spatial.hpp"
15
16#include "pinocchio/utils/string.hpp"
17
18#include "pinocchio/bindings/python/utils/cast.hpp"
19#include "pinocchio/bindings/python/utils/printable.hpp"
20#include "pinocchio/bindings/python/utils/namespace.hpp"
21
22namespace pinocchio
23{
24 namespace python
25 {
26 namespace bp = boost::python;
27
28 template<typename SE3>
29 struct SE3PythonVisitor : public boost::python::def_visitor<SE3PythonVisitor<SE3>>
30 {
31 typedef typename SE3::Scalar Scalar;
32 static constexpr int Options = SE3::Options;
33 typedef typename SE3::Matrix3 Matrix3;
34 typedef typename SE3::Vector3 Vector3;
35 typedef typename SE3::Matrix4 Matrix4;
36 typedef typename SE3::Quaternion Quaternion;
37 typedef typename SE3::ActionMatrixType ActionMatrixType;
38
39 typedef MotionTpl<Scalar, Options> Motion;
40 typedef ForceTpl<Scalar, Options> Force;
41 typedef InertiaTpl<Scalar, Options> Inertia;
42
43 public:
44 template<class PyClass>
45 void visit(PyClass & cl) const
46 {
47 static const Scalar dummy_precision = Eigen::NumTraits<Scalar>::dummy_precision();
48
49 cl.def(
50 bp::init<const Matrix3 &, const Vector3 &>(
51 (bp::arg("self"), bp::arg("rotation"), bp::arg("translation")),
52 "Initialize from a rotation matrix and a translation vector."))
53 .def(
54 bp::init<const Quaternion &, const Vector3 &>(
55 (bp::arg("self"), bp::arg("quat"), bp::arg("translation")),
56 "Initialize from a quaternion and a translation vector."))
57 .def(bp::init<int>((bp::arg("self"), bp::arg("int")), "Init to identity."))
58 .def(
59 bp::init<const Matrix4 &>(
60 (bp::arg("self"), bp::arg("array")), "Initialize from an homogeneous matrix."))
61 .def(bp::init<const SE3 &>((bp::arg("self"), bp::arg("clone")), "Copy constructor"))
62
63 .add_property(
64 "rotation",
65 bp::make_function(
66 (typename SE3::AngularRef (SE3::*)())&SE3::rotation,
67 bp::return_internal_reference<>()),
68 (void (SE3::*)(const Matrix3 &))&SE3::rotation,
69 "The rotation part of the transformation.")
70 .add_property(
71 "translation",
72 bp::make_function(
73 (typename SE3::LinearRef (SE3::*)())&SE3::translation,
74 bp::return_internal_reference<>()),
75 (void (SE3::*)(const Vector3 &))&SE3::translation,
76 "The translation part of the transformation.")
77
78 .add_property(
79 "homogeneous", &SE3::toHomogeneousMatrix,
80 "Returns the equivalent homegeneous matrix (acting on SE3).")
81 .add_property(
82 "action", (ActionMatrixType (SE3::*)() const) & SE3::toActionMatrix,
83 "Returns the related action matrix (acting on Motion).")
84 .def(
85 "toActionMatrix", (ActionMatrixType (SE3::*)() const) & SE3::toActionMatrix,
86 bp::arg("self"), "Returns the related action matrix (acting on Motion).")
87 .add_property(
88 "actionInverse", (ActionMatrixType (SE3::*)() const) & SE3::toActionMatrixInverse,
89 "Returns the inverse of the action matrix (acting on Motion).\n"
90 "This is equivalent to do m.inverse().action")
91 .def(
92 "toActionMatrixInverse",
93 (ActionMatrixType (SE3::*)() const) & SE3::toActionMatrixInverse, bp::arg("self"),
94 "Returns the inverse of the action matrix (acting on Motion).\n"
95 "This is equivalent to do m.inverse().toActionMatrix()")
96 .add_property(
97 "dualAction", (ActionMatrixType (SE3::*)() const) & SE3::toDualActionMatrix,
98 "Returns the related dual action matrix (acting on Force).")
99 .def(
100 "toDualActionMatrix", (ActionMatrixType (SE3::*)() const) & SE3::toDualActionMatrix,
101 bp::arg("self"), "Returns the related dual action matrix (acting on Force).")
102
103 .def(
104 "setIdentity", &SE3PythonVisitor::setIdentity, bp::arg("self"),
105 "Set *this to the identity placement.")
106 .def(
107 "setRandom", &SE3PythonVisitor::setRandom, bp::arg("self"),
108 "Set *this to a random placement.")
109
110 .def("inverse", &SE3::inverse, bp::arg("self"), "Returns the inverse transform")
111
112 .def(
113 "act", (Vector3 (SE3::*)(const Vector3 &) const) & SE3::act, bp::args("self", "point"),
114 "Returns a point which is the result of the entry point transforms by *this.")
115 .def(
116 "actInv", (Vector3 (SE3::*)(const Vector3 &) const) & SE3::actInv,
117 bp::args("self", "point"),
118 "Returns a point which is the result of the entry point by the inverse of *this.")
119
120 .def(
121 "act", (SE3 (SE3::*)(const SE3 & other) const) & SE3::act, bp::args("self", "M"),
122 "Returns the result of *this * M.")
123 .def(
124 "actInv", (SE3 (SE3::*)(const SE3 & other) const) & SE3::actInv, bp::args("self", "M"),
125 "Returns the result of the inverse of *this times M.")
126
127 .def(
128 "act", (Motion (SE3::*)(const Motion &) const) & SE3::act, bp::args("self", "motion"),
129 "Returns the result action of *this onto a Motion.")
130 .def(
131 "actInv", (Motion (SE3::*)(const Motion &) const) & SE3::actInv,
132 bp::args("self", "motion"), "Returns the result of the inverse of *this onto a Motion.")
133
134 .def(
135 "act", (Force (SE3::*)(const Force &) const) & SE3::act, bp::args("self", "force"),
136 "Returns the result of *this onto a Force.")
137 .def(
138 "actInv", (Force (SE3::*)(const Force &) const) & SE3::actInv,
139 bp::args("self", "force"),
140 "Returns the result of the inverse of *this onto an Inertia.")
141
142 .def(
143 "act", (Inertia (SE3::*)(const Inertia &) const) & SE3::act,
144 bp::args("self", "inertia"), "Returns the result of *this onto a Force.")
145 .def(
146 "actInv", (Inertia (SE3::*)(const Inertia &) const) & SE3::actInv,
147 bp::args("self", "inertia"),
148 "Returns the result of the inverse of *this onto an Inertia.")
149
150#ifndef PINOCCHIO_PYTHON_SKIP_COMPARISON_OPERATIONS
151 .def(
152 "isApprox", &SE3::isApprox,
153 (bp::arg("self"), bp::arg("other"), bp::arg("prec") = dummy_precision),
154 "Returns true if *this is approximately equal to other, within the precision given by "
155 "prec.")
156
157 .def(
158 "isIdentity", &SE3::isIdentity, (bp::arg("self"), bp::arg("prec") = dummy_precision),
159 "Returns true if *this is approximately equal to the identity placement, within the "
160 "precision given by prec.")
161#endif
162
163 .def("__invert__", &SE3::inverse, "Returns the inverse of *this.")
164 .def(bp::self * bp::self)
165 .def("__mul__", &__mul__<Motion>)
166 .def("__mul__", &__mul__<Force>)
167 .def("__mul__", &__mul__<Inertia>)
168 .def("__mul__", &__mul__<Vector3>)
169 .add_property("np", &SE3::toHomogeneousMatrix)
170
171#ifndef PINOCCHIO_PYTHON_SKIP_COMPARISON_OPERATIONS
172 .def(bp::self == bp::self)
173 .def(bp::self != bp::self)
174#endif
175
176 .def("Identity", &SE3::Identity, "Returns the identity transformation.")
177 .staticmethod("Identity")
178 .def("Random", &SE3::Random, "Returns a random transformation.")
179 .staticmethod("Random")
180 .def(
181 "Interpolate", &SE3::template Interpolate<Scalar>, bp::args("A", "B", "alpha"),
182 "Linear interpolation on the SE3 manifold.\n\n"
183 "This method computes the linear interpolation between A and B, such that the result C "
184 "= A + (B-A)*t if it would be applied on classic Euclidian space.\n"
185 "This operation is very similar to the SLERP operation on Rotations.\n"
186 "Parameters:\n"
187 "\tA: Initial transformation\n"
188 "\tB: Target transformation\n"
189 "\talpha: Interpolation factor")
190 .staticmethod("Interpolate")
191
192 .def("__array__", &SE3::toHomogeneousMatrix)
193 .def(
194 "__array__", &__array__,
195 (bp::arg("self"), bp::arg("dtype") = bp::object(), bp::arg("copy") = bp::object()))
196
197#ifndef PINOCCHIO_PYTHON_NO_SERIALIZATION
198 .def_pickle(Pickle())
199#endif
200 ;
201 }
202
203 static std::string scopeName()
204 {
205 static std::string scope_name;
206 return scope_name;
207 }
208
209 static void expose()
210 {
211 typedef ::boost::python::detail::not_specified HolderType;
212 bp::class_<SE3, HolderType>(
213 "SE3", "SE3 transformation defined by a 3d vector and a rotation matrix.",
214 bp::init<>(bp::arg("self"), "Default constructor."))
216 .def(CastVisitor<SE3>())
218 .def(::eigenpy::CopyableVisitor<SE3>())
219 .def(bp::self_ns::str(bp::self_ns::self))
220 .def("__repr__", &repr);
221 }
222
223 private:
224 static Matrix4 __array__(const SE3 & self, bp::object, bp::object)
225 {
226 return self.toHomogeneousMatrix();
227 }
228
229 struct Pickle : bp::pickle_suite
230 {
231 static boost::python::tuple getinitargs(const SE3 & M)
232 {
233 return bp::make_tuple((Matrix3)M.rotation(), (Vector3)M.translation());
234 }
235
236 static bool getstate_manages_dict()
237 {
238 return true;
239 }
240 };
241
242 static void setIdentity(SE3 & self)
243 {
244 self.setIdentity();
245 }
246 static void setRandom(SE3 & self)
247 {
248 self.setRandom();
249 }
250
251 template<typename Spatial>
252 static Spatial __mul__(const SE3 & self, const Spatial & other)
253 {
254 return self.act(other);
255 }
256
257 static std::string repr(const SE3 & self)
258 {
259 // bp::object
260 // py_rotation(bp::handle<>(eigenpy::EigenToPy<Matrix3,Scalar>::convert(self.rotation())));
261 // std::string rotation_repr =
262 // bp::extract<std::string>(py_rotation.attr("__repr__")());
263 //
264 // bp::object
265 // py_translation(bp::handle<>(eigenpy::EigenToPy<Vector3,Scalar>::convert(self.translation())));
266 // std::string translation_repr =
267 // bp::extract<std::string>(py_translation.attr("__repr__")());
268
269 bp::object py_homogeneous(
270 bp::handle<>(eigenpy::EigenToPy<Matrix4, Scalar>::convert(self.toHomogeneousMatrix())));
271 std::string homegeneous_repr = bp::extract<std::string>(py_homogeneous.attr("__repr__")());
272 replace(homegeneous_repr, "\n", "");
273 replace(homegeneous_repr, " ", "");
274
275 std::stringstream ss_repr;
276 ss_repr << "SE3(";
277 ss_repr << homegeneous_repr;
278 ss_repr << ")";
279
280 return ss_repr.str();
281 }
282 };
283
284 } // namespace python
285} // namespace pinocchio
Main pinocchio namespace.
Definition treeview.dox:11
Add the Python method cast.
Definition cast.hpp:23