bindings/python/spatial/inertia.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2015-2024 CNRS INRIA
3 // Copyright (c) 2016 Wandercraft, 86 rue de Paris 91400 Orsay, France.
4 //
5 
6 #ifndef __pinocchio_python_spatial_inertia_hpp__
7 #define __pinocchio_python_spatial_inertia_hpp__
8 
9 #include <eigenpy/exception.hpp>
10 #include <eigenpy/eigenpy.hpp>
11 #include <eigenpy/memory.hpp>
12 #include <boost/python/tuple.hpp>
13 
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 Inertia>
31  struct InertiaPythonVisitor : public boost::python::def_visitor<InertiaPythonVisitor<Inertia>>
32  {
33  enum
34  {
36  };
37  typedef typename Inertia::Scalar Scalar;
38  typedef typename Inertia::Vector3 Vector3;
39  typedef typename Inertia::Matrix3 Matrix3;
40  typedef typename Inertia::Vector6 Vector6;
41  typedef typename Inertia::Matrix6 Matrix6;
42 
43  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1, Options> VectorXs;
46 
47  public:
48  template<class PyClass>
49  void visit(PyClass & cl) const
50  {
51  static const Scalar dummy_precision = Eigen::NumTraits<Scalar>::dummy_precision();
54  cl.def(
55  "__init__",
56  bp::make_constructor(
57  &InertiaPythonVisitor::makeFromMCI, bp::default_call_policies(),
58  bp::args("mass", "lever", "inertia")),
59  "Initialize from mass, lever and 3d inertia.")
60 
61  .def(bp::init<>(bp::arg("self"), "Default constructor."))
62  .def(bp::init<const Inertia &>((bp::arg("self"), bp::arg("clone")), "Copy constructor"))
63 
64  .add_property(
66  "Mass of the Spatial Inertia.")
67  .add_property(
68  "lever",
69  bp::make_function(
70  (typename Inertia::Vector3 & (Inertia::*)()) & Inertia::lever,
71  bp::return_internal_reference<>()),
73  "Center of mass location of the Spatial Inertia. It corresponds to the location of the "
74  "center of mass regarding to the frame where the Spatial Inertia is expressed.")
75  .add_property(
77  "Rotational part of the Spatial Inertia, i.e. a symmetric matrix "
78  "representing the rotational inertia around the center of mass.")
79 
80  .def("matrix", (Matrix6(Inertia::*)() const) & Inertia::matrix, bp::arg("self"))
81  .def("inverse", (Matrix6(Inertia::*)() const) & Inertia::inverse, bp::arg("self"))
82  .def(
83  "se3Action", &Inertia::template se3Action<Scalar, Options>, bp::args("self", "M"),
84  "Returns the result of the action of M on *this.")
85  .def(
86  "se3ActionInverse", &Inertia::template se3ActionInverse<Scalar, Options>,
87  bp::args("self", "M"), "Returns the result of the action of the inverse of M on *this.")
88 
89  .def(
90  "setIdentity", &Inertia::setIdentity, bp::arg("self"),
91  "Set *this to be the Identity inertia.")
92  .def(
93  "setZero", &Inertia::setZero, bp::arg("self"),
94  "Set all the components of *this to zero.")
95  .def(
96  "setRandom", &Inertia::setRandom, bp::arg("self"),
97  "Set all the components of *this to random values.")
98 
99  .def(bp::self + bp::self)
100  .def(bp::self += bp::self)
101  .def(bp::self - bp::self)
102  .def(bp::self -= bp::self)
103  .def(bp::self * bp::other<Motion>())
104 
105  .add_property("np", (Matrix6(Inertia::*)() const) & Inertia::matrix)
106  .def(
107  "vxiv", &Inertia::template vxiv<Motion>, bp::args("self", "v"),
108  "Returns the result of v x Iv.")
109  .def(
110  "vtiv", &Inertia::template vtiv<Motion>, bp::args("self", "v"),
111  "Returns the result of v.T * Iv.")
112  .def(
113  "vxi",
114  (Matrix6(Inertia::*)(const MotionDense<Motion> &) const)
115  & Inertia::template vxi<Motion>,
116  bp::args("self", "v"), "Returns the result of v x* I, a 6x6 matrix.")
117  .def(
118  "ivx",
119  (Matrix6(Inertia::*)(const MotionDense<Motion> &) const)
120  & Inertia::template ivx<Motion>,
121  bp::args("self", "v"), "Returns the result of I vx, a 6x6 matrix.")
122  .def(
123  "variation",
124  (Matrix6(Inertia::*)(const MotionDense<Motion> &) const)
125  & Inertia::template variation<Motion>,
126  bp::args("self", "v"), "Returns the time derivative of the inertia.")
127 
128 #ifndef PINOCCHIO_PYTHON_SKIP_COMPARISON_OPERATIONS
129  .def(bp::self == bp::self)
130  .def(bp::self != bp::self)
131 #endif
132 
133 #ifndef PINOCCHIO_PYTHON_SKIP_COMPARISON_OPERATIONS
134  .def(
135  "isApprox", &Inertia::isApprox,
136  (bp::arg("self"), bp::arg("other"), bp::arg("prec") = dummy_precision),
137  "Returns true if *this is approximately equal to other, within the precision given "
138  "by prec.")
139 
140  .def(
141  "isZero", &Inertia::isZero, (bp::arg("self"), bp::arg("prec") = dummy_precision),
142  "Returns true if *this is approximately equal to the zero Inertia, within the "
143  "precision given by prec.")
144 #endif
145 
146  .def("Identity", &Inertia::Identity, "Returns the identity Inertia.")
147  .staticmethod("Identity")
148  .def("Zero", &Inertia::Zero, "Returns the zero Inertia.")
149  .staticmethod("Zero")
150  .def("Random", &Inertia::Random, "Returns a random Inertia.")
151  .staticmethod("Random")
152 
153  .def(
154  "toDynamicParameters", &InertiaPythonVisitor::toDynamicParameters_proxy,
155  bp::arg("self"),
156  "Returns the representation of the matrix as a vector of dynamic parameters."
157  "\nThe parameters are given as v = [m, mc_x, mc_y, mc_z, I_{xx}, I_{xy}, I_{yy}, "
158  "I_{xz}, I_{yz}, I_{zz}]^T "
159  "where I = I_C + mS^T(c)S(c) and I_C has its origin at the barycenter")
160  .def(
161  "FromDynamicParameters", &Inertia::template FromDynamicParameters<VectorXs>,
162  bp::args("dynamic_parameters"),
163  "Builds and inertia matrix from a vector of dynamic parameters."
164  "\nThe parameters are given as dynamic_parameters = [m, mc_x, mc_y, mc_z, I_{xx}, "
165  "I_{xy}, I_{yy}, I_{xz}, I_{yz}, I_{zz}]^T "
166  "where I = I_C + mS^T(c)S(c) and I_C has its origin at the barycenter.")
167  .staticmethod("FromDynamicParameters")
168 
169  .def(
170  "FromSphere", &Inertia::FromSphere, bp::args("mass", "radius"),
171  "Returns the Inertia of a sphere defined by a given mass and radius.")
172  .staticmethod("FromSphere")
173  .def(
174  "FromEllipsoid", &Inertia::FromEllipsoid,
175  bp::args("mass", "length_x", "length_y", "length_z"),
176  "Returns the Inertia of an ellipsoid shape defined by a mass and given dimensions "
177  "the semi-axis of values length_{x,y,z}.")
178  .staticmethod("FromEllipsoid")
179  .def(
180  "FromCylinder", &Inertia::FromCylinder, bp::args("mass", "radius", "length"),
181  "Returns the Inertia of a cylinder defined by its mass, radius and length along the "
182  "Z axis.")
183  .staticmethod("FromCylinder")
184  .def(
185  "FromBox", &Inertia::FromBox, bp::args("mass", "length_x", "length_y", "length_z"),
186  "Returns the Inertia of a box shape with a mass and of dimension the semi axis of "
187  "length_{x,y,z}.")
188  .staticmethod("FromBox")
189  .def(
190  "FromCapsule", &Inertia::FromCapsule, bp::args("mass", "radius", "height"),
191  "Computes the Inertia of a capsule defined by its mass, radius and length along the "
192  "Z axis. Assumes a uniform density.")
193  .staticmethod("FromCapsule")
194 
195  .def("__array__", (Matrix6(Inertia::*)() const) & Inertia::matrix)
196  .def("__array__", &__array__)
197 #ifndef PINOCCHIO_PYTHON_NO_SERIALIZATION
198  .def_pickle(Pickle())
199 #endif
200  ;
202  }
203 
204  static Scalar getMass(const Inertia & self)
205  {
206  return self.mass();
207  }
208  static void setMass(Inertia & self, Scalar mass)
209  {
210  self.mass() = mass;
211  }
212 
213  static void setLever(Inertia & self, const Vector3 & lever)
214  {
215  self.lever() = lever;
216  }
217 
218  static Matrix3 getInertia(const Inertia & self)
219  {
220  return self.inertia().matrix();
221  }
222  // static void setInertia(Inertia & self, const Vector6 & minimal_inertia) {
223  // self.inertia().data() = minimal_inertia; }
224  static void setInertia(Inertia & self, const Matrix3 & symmetric_inertia)
225  {
226  if (!check_expression_if_real<Scalar>(
227  isZero(symmetric_inertia - symmetric_inertia.transpose())))
228  throw eigenpy::Exception("The 3d inertia should be symmetric.");
229  self.inertia().data() << symmetric_inertia(0, 0), symmetric_inertia(1, 0),
230  symmetric_inertia(1, 1), symmetric_inertia(0, 2), symmetric_inertia(1, 2),
231  symmetric_inertia(2, 2);
232  }
233 
235  {
236  return self.toDynamicParameters();
237  }
238 
239  static Inertia *
240  makeFromMCI(const Scalar & mass, const Vector3 & lever, const Matrix3 & inertia)
241  {
242 #ifndef PINOCCHIO_PYTHON_SKIP_COMPARISON_OPERATIONS
243  if (!inertia.isApprox(inertia.transpose()))
244  throw eigenpy::Exception("The 3d inertia should be symmetric.");
245  if (
246  (Vector3::UnitX().transpose() * inertia * Vector3::UnitX() < 0)
247  || (Vector3::UnitY().transpose() * inertia * Vector3::UnitY() < 0)
248  || (Vector3::UnitZ().transpose() * inertia * Vector3::UnitZ() < 0))
249  throw eigenpy::Exception("The 3d inertia should be positive.");
250 #endif
251  return new Inertia(mass, lever, inertia);
252  }
253 
254  static void expose()
255  {
256 #if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION == 6 && EIGENPY_VERSION_AT_LEAST(2, 9, 0)
257  typedef PINOCCHIO_SHARED_PTR_HOLDER_TYPE(Inertia) HolderType;
258 #else
259  typedef ::boost::python::detail::not_specified HolderType;
260 #endif
261  bp::class_<Inertia, HolderType>(
262  "Inertia",
263  "This class represenses a sparse version of a Spatial Inertia and its is defined by its "
264  "mass, its center of mass location and the rotational inertia expressed around this "
265  "center of mass.\n\n"
266  "Supported operations ...",
267  bp::no_init)
269  .def(CastVisitor<Inertia>())
273  }
274 
275  private:
276  static Matrix6 __array__(const Inertia & self, bp::object)
277  {
278  return self.matrix();
279  }
280 
281  struct Pickle : bp::pickle_suite
282  {
283  static boost::python::tuple getinitargs(const Inertia & I)
284  {
285  return bp::make_tuple(I.mass(), (Vector3)I.lever(), I.inertia().matrix());
286  }
287 
288  static bool getstate_manages_dict()
289  {
290  return true;
291  }
292  };
293 
294  }; // struct InertiaPythonVisitor
295 
296  } // namespace python
297 } // namespace pinocchio
298 
299 #endif // ifndef __pinocchio_python_spatial_inertia_hpp__
pinocchio::python::InertiaPythonVisitor::Scalar
Inertia::Scalar Scalar
Definition: bindings/python/spatial/inertia.hpp:37
pinocchio::InertiaTpl< context::Scalar, context::Options >
pinocchio::python::InertiaPythonVisitor::makeFromMCI
static Inertia * makeFromMCI(const Scalar &mass, const Vector3 &lever, const Matrix3 &inertia)
Definition: bindings/python/spatial/inertia.hpp:240
pinocchio::python::InertiaPythonVisitor::VectorXs
Eigen::Matrix< Scalar, Eigen::Dynamic, 1, Options > VectorXs
Definition: bindings/python/spatial/inertia.hpp:43
pinocchio::python::InertiaPythonVisitor::setInertia
static void setInertia(Inertia &self, const Matrix3 &symmetric_inertia)
Definition: bindings/python/spatial/inertia.hpp:224
boost::python
pinocchio::InertiaTpl< context::Scalar, context::Options >::FromEllipsoid
static InertiaTpl FromEllipsoid(const Scalar mass, const Scalar x, const Scalar y, const Scalar z)
Computes the Inertia of an ellipsoid defined by its mass and main semi-axis dimensions (x,...
Definition: spatial/inertia.hpp:384
PINOCCHIO_SHARED_PTR_HOLDER_TYPE
#define PINOCCHIO_SHARED_PTR_HOLDER_TYPE(T)
Definition: bindings/python/fwd.hpp:17
pinocchio::InertiaTpl< context::Scalar, context::Options >::setZero
void setZero()
Definition: spatial/inertia.hpp:337
pinocchio::InertiaTpl< context::Scalar, context::Options >::lever
const Vector3 & lever() const
Definition: spatial/inertia.hpp:806
pinocchio::python::InertiaPythonVisitor::Force
ForceTpl< Scalar, Options > Force
Definition: bindings/python/spatial/inertia.hpp:45
pinocchio::python::InertiaPythonVisitor::visit
void visit(PyClass &cl) const
Definition: bindings/python/spatial/inertia.hpp:49
pinocchio::python::InertiaPythonVisitor::getInertia
static Matrix3 getInertia(const Inertia &self)
Definition: bindings/python/spatial/inertia.hpp:218
eigenpy.hpp
pinocchio::isZero
bool isZero(const Eigen::MatrixBase< MatrixLike > &m, const typename MatrixLike::RealScalar &prec=Eigen::NumTraits< typename MatrixLike::Scalar >::dummy_precision())
Definition: math/matrix.hpp:59
pinocchio::python::InertiaPythonVisitor::expose
static void expose()
Definition: bindings/python/spatial/inertia.hpp:254
pinocchio::Inertia
InertiaTpl< context::Scalar, context::Options > Inertia
Definition: spatial/fwd.hpp:65
pinocchio::python::InertiaPythonVisitor::__array__
static Matrix6 __array__(const Inertia &self, bp::object)
Definition: bindings/python/spatial/inertia.hpp:276
PINOCCHIO_COMPILER_DIAGNOSTIC_IGNORED_SELF_ASSIGN_OVERLOADED
#define PINOCCHIO_COMPILER_DIAGNOSTIC_IGNORED_SELF_ASSIGN_OVERLOADED
Definition: include/pinocchio/macros.hpp:128
inertia.hpp
pinocchio::python::ExposeConstructorByCastVisitor
Definition: bindings/python/utils/cast.hpp:33
PINOCCHIO_COMPILER_DIAGNOSTIC_POP
#define PINOCCHIO_COMPILER_DIAGNOSTIC_POP
Definition: include/pinocchio/macros.hpp:125
pinocchio::MotionDense
Definition: context/casadi.hpp:36
pinocchio::python::Scalar
context::Scalar Scalar
Definition: admm-solver.cpp:29
exception.hpp
pinocchio::python::InertiaPythonVisitor::Pickle::getinitargs
static boost::python::tuple getinitargs(const Inertia &I)
Definition: bindings/python/spatial/inertia.hpp:283
pinocchio::InertiaTpl< context::Scalar, context::Options >::Zero
static InertiaTpl Zero()
Definition: spatial/inertia.hpp:332
pinocchio::python::InertiaPythonVisitor::Vector6
Inertia::Vector6 Vector6
Definition: bindings/python/spatial/inertia.hpp:40
cast.hpp
pinocchio::InertiaTpl::inertia
const Symmetric3 & inertia() const
Definition: spatial/inertia.hpp:810
pinocchio::InertiaTpl< context::Scalar, context::Options >::Options
@ Options
Definition: spatial/inertia.hpp:269
EIGENPY_DEFINE_STRUCT_ALLOCATOR_SPECIALIZATION
#define EIGENPY_DEFINE_STRUCT_ALLOCATOR_SPECIALIZATION(...)
pinocchio::python::PrintableVisitor
Set the Python method str and repr to use the overloading operator<<.
Definition: printable.hpp:21
pinocchio::InertiaTpl::mass
Scalar mass() const
Definition: spatial/inertia.hpp:802
pinocchio::python::InertiaPythonVisitor::Motion
MotionTpl< Scalar, Options > Motion
Definition: bindings/python/spatial/inertia.hpp:44
pinocchio::InertiaTpl< context::Scalar, context::Options >::setRandom
void setRandom()
Definition: spatial/inertia.hpp:467
pinocchio::python::InertiaPythonVisitor::setMass
static void setMass(Inertia &self, Scalar mass)
Definition: bindings/python/spatial/inertia.hpp:208
python
pinocchio::ForceTpl< Scalar, Options >
pinocchio::python::CopyableVisitor
Add the Python method copy to allow a copy of this by calling the copy constructor.
Definition: copyable.hpp:21
pinocchio::InertiaTpl< context::Scalar, context::Options >::FromCapsule
static InertiaTpl FromCapsule(const Scalar mass, const Scalar radius, const Scalar height)
Computes the Inertia of a capsule defined by its mass, radius and length along the Z axis....
Definition: spatial/inertia.hpp:434
pinocchio::python::InertiaPythonVisitor::toDynamicParameters_proxy
static VectorXs toDynamicParameters_proxy(const Inertia &self)
Definition: bindings/python/spatial/inertia.hpp:234
pinocchio::inverse
void inverse(const Eigen::MatrixBase< MatrixIn > &m_in, const Eigen::MatrixBase< MatrixOut > &dest)
Definition: math/matrix.hpp:273
pinocchio::InertiaTpl< context::Scalar, context::Options >::Random
static InertiaTpl Random()
Definition: spatial/inertia.hpp:356
PINOCCHIO_COMPILER_DIAGNOSTIC_PUSH
#define PINOCCHIO_COMPILER_DIAGNOSTIC_PUSH
macros for pragma push/pop/ignore deprecated warnings
Definition: include/pinocchio/macros.hpp:124
eigenpy::Exception
pinocchio::context::Vector3
Eigen::Matrix< Scalar, 3, 1, Options > Vector3
Definition: context/generic.hpp:53
pinocchio::python::InertiaPythonVisitor::setLever
static void setLever(Inertia &self, const Vector3 &lever)
Definition: bindings/python/spatial/inertia.hpp:213
pinocchio::python::CastVisitor
Add the Python method cast.
Definition: bindings/python/utils/cast.hpp:23
copyable.hpp
anymal-simulation.mass
mass
Definition: anymal-simulation.py:77
pinocchio::Symmetric3Tpl::matrix
Matrix3 matrix() const
Definition: spatial/symmetric3.hpp:304
pinocchio::python::InertiaPythonVisitor::getMass
static Scalar getMass(const Inertia &self)
Definition: bindings/python/spatial/inertia.hpp:204
pinocchio::python::InertiaPythonVisitor::Vector3
Inertia::Vector3 Vector3
Definition: bindings/python/spatial/inertia.hpp:38
pinocchio::InertiaTpl< context::Scalar, context::Options >::Identity
static InertiaTpl Identity()
Definition: spatial/inertia.hpp:344
pinocchio::python::InertiaPythonVisitor::Options
@ Options
Definition: bindings/python/spatial/inertia.hpp:35
cl
cl
memory.hpp
pinocchio::python::InertiaPythonVisitor::Pickle::getstate_manages_dict
static bool getstate_manages_dict()
Definition: bindings/python/spatial/inertia.hpp:288
printable.hpp
pinocchio::InertiaTpl< context::Scalar, context::Options >::FromBox
static InertiaTpl FromBox(const Scalar mass, const Scalar x, const Scalar y, const Scalar z)
Computes the Inertia of a box defined by its mass and main dimensions (x,y,z).
Definition: spatial/inertia.hpp:418
pinocchio::InertiaTpl< context::Scalar, context::Options >::FromSphere
static InertiaTpl FromSphere(const Scalar mass, const Scalar radius)
Computes the Inertia of a sphere defined by its mass and its radius.
Definition: spatial/inertia.hpp:369
pinocchio::MotionTpl< Scalar, Options >
pinocchio::python::InertiaPythonVisitor::Pickle
Definition: bindings/python/spatial/inertia.hpp:281
pinocchio::InertiaTpl< context::Scalar, context::Options >::FromCylinder
static InertiaTpl FromCylinder(const Scalar mass, const Scalar radius, const Scalar length)
Computes the Inertia of a cylinder defined by its mass, radius and length along the Z axis.
Definition: spatial/inertia.hpp:401
pinocchio::InertiaTpl< context::Scalar, context::Options >::setIdentity
void setIdentity()
Definition: spatial/inertia.hpp:349
pinocchio::python::InertiaPythonVisitor
Definition: bindings/python/spatial/inertia.hpp:31
pinocchio::python::InertiaPythonVisitor::Matrix6
Inertia::Matrix6 Matrix6
Definition: bindings/python/spatial/inertia.hpp:41
pinocchio::python::InertiaPythonVisitor::Matrix3
Inertia::Matrix3 Matrix3
Definition: bindings/python/spatial/inertia.hpp:39
pinocchio
Main pinocchio namespace.
Definition: timings.cpp:27


pinocchio
Author(s):
autogenerated on Wed Jun 19 2024 02:41:13