GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: include/pinocchio/bindings/python/spatial/se3.hpp Lines: 54 64 84.4 %
Date: 2024-04-26 13:14:21 Branches: 83 176 47.2 %

Line Branch Exec Source
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_se3_hpp__
7
#define __pinocchio_python_spatial_se3_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/motion.hpp"
15
#include "pinocchio/spatial/force.hpp"
16
#include "pinocchio/spatial/inertia.hpp"
17
#include "pinocchio/spatial/explog.hpp"
18
19
#include "pinocchio/bindings/python/fwd.hpp"
20
#include "pinocchio/bindings/python/utils/copyable.hpp"
21
#include "pinocchio/bindings/python/utils/printable.hpp"
22
23
#if EIGENPY_VERSION_AT_MOST(2,8,1)
24
EIGENPY_DEFINE_STRUCT_ALLOCATOR_SPECIALIZATION(pinocchio::SE3)
25
#endif
26
27
namespace pinocchio
28
{
29
  namespace python
30
  {
31
    namespace bp = boost::python;
32
33
38
    BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(isIdentity_overload,SE3::isIdentity,0,1)
34
35
    template<typename T> struct call;
36
37
    template<typename Scalar, int Options>
38
    struct call< SE3Tpl<Scalar,Options> >
39
    {
40
      typedef SE3Tpl<Scalar,Options> SE3;
41
42
      static bool isApprox(const SE3 & self, const SE3 & other,
43
                           const Scalar & prec = Eigen::NumTraits<Scalar>::dummy_precision())
44
      {
45
        return self.isApprox(other,prec);
46
      }
47
    };
48
49
38
    BOOST_PYTHON_FUNCTION_OVERLOADS(isApproxSE3_overload,call<SE3>::isApprox,2,3)
50
51
    template<typename SE3>
52
    struct SE3PythonVisitor
53
    : public boost::python::def_visitor< SE3PythonVisitor<SE3> >
54
    {
55
      typedef typename SE3::Scalar Scalar;
56
      typedef typename SE3::Matrix3 Matrix3;
57
      typedef typename SE3::Vector3 Vector3;
58
      typedef typename SE3::Matrix4 Matrix4;
59
      typedef typename SE3::Quaternion Quaternion;
60
61
    public:
62
63
      template<class PyClass>
64
19
      void visit(PyClass& cl) const
65
      {
66
        cl
67
        .def(bp::init<Matrix3,Vector3>
68
             (bp::args("self","rotation","translation"),
69
              "Initialize from a rotation matrix and a translation vector."))
70

19
        .def(bp::init<Quaternion,Vector3>
71
             (bp::args("self","quat","translation"),
72
              "Initialize from a quaternion and a translation vector."))
73

38
        .def(bp::init<int>(bp::args("self","int"),"Init to identity."))
74

38
        .def(bp::init<SE3>(bp::args("self","other"), "Copy constructor."))
75

38
        .def(bp::init<Matrix4>
76
             (bp::args("self","array"),
77
              "Initialize from an homogeneous matrix."))
78
79

38
        .add_property("rotation",
80
                      bp::make_function((typename SE3::AngularRef (SE3::*)()) &SE3::rotation,bp::return_internal_reference<>()),
81
                      (void (SE3::*)(const Matrix3 &)) &SE3::rotation,
82
                      "The rotation part of the transformation.")
83
38
        .add_property("translation",
84
                      bp::make_function((typename SE3::LinearRef (SE3::*)()) &SE3::translation,bp::return_internal_reference<>()),
85
                      (void (SE3::*)(const Vector3 &)) &SE3::translation,
86
                      "The translation part of the transformation.")
87
88

57
        .add_property("homogeneous",&SE3::toHomogeneousMatrix,
89
                      "Returns the equivalent homegeneous matrix (acting on SE3).")
90
19
        .add_property("action",&SE3::toActionMatrix,
91
                      "Returns the related action matrix (acting on Motion).")
92
19
        .def("toActionMatrix",&SE3::toActionMatrix,bp::arg("self"),
93
             "Returns the related action matrix (acting on Motion).")
94

38
        .add_property("actionInverse",&SE3::toActionMatrixInverse,
95
                      "Returns the inverse of the action matrix (acting on Motion).\n"
96
                      "This is equivalent to do m.inverse().action")
97
19
        .def("toActionMatrixInverse",&SE3::toActionMatrixInverse,bp::arg("self"),
98
             "Returns the inverse of the action matrix (acting on Motion).\n"
99
             "This is equivalent to do m.inverse().toActionMatrix()")
100

38
        .add_property("dualAction",&SE3::toDualActionMatrix,
101
                      "Returns the related dual action matrix (acting on Force).")
102
19
        .def("toDualActionMatrix",&SE3::toDualActionMatrix,bp::arg("self"),
103
             "Returns the related dual action matrix (acting on Force).")
104
105

38
        .def("setIdentity",&SE3PythonVisitor::setIdentity,bp::arg("self"),
106
             "Set *this to the identity placement.")
107

38
        .def("setRandom",&SE3PythonVisitor::setRandom,bp::arg("self"),
108
             "Set *this to a random placement.")
109
110

38
        .def("inverse", &SE3::inverse, bp::arg("self"),
111
             "Returns the inverse transform")
112
113
38
        .def("act", (Vector3 (SE3::*)(const Vector3 &) const) &SE3::act,
114
             bp::args("self","point"),
115
             "Returns a point which is the result of the entry point transforms by *this.")
116

38
        .def("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

38
        .def("act", (SE3 (SE3::*)(const SE3 & other) const) &SE3::act,
121
             bp::args("self","M"), "Returns the result of *this * M.")
122

38
        .def("actInv", (SE3 (SE3::*)(const SE3 & other) const) &SE3::actInv,
123
             bp::args("self","M"), "Returns the result of the inverse of *this times M.")
124
125

38
        .def("act", (Motion (SE3::*)(const Motion &) const) &SE3::act,
126
             bp::args("self","motion"), "Returns the result action of *this onto a Motion.")
127

38
        .def("actInv", (Motion (SE3::*)(const Motion &) const) &SE3::actInv,
128
             bp::args("self","motion"), "Returns the result of the inverse of *this onto a Motion.")
129
130

38
        .def("act", (Force (SE3::*)(const Force &) const) &SE3::act,
131
             bp::args("self","force"), "Returns the result of *this onto a Force.")
132

38
        .def("actInv", (Force (SE3::*)(const Force &) const) &SE3::actInv,
133
             bp::args("self","force"), "Returns the result of the inverse of *this onto an Inertia.")
134
135

38
        .def("act", (Inertia (SE3::*)(const Inertia &) const) &SE3::act,
136
             bp::args("self","inertia"), "Returns the result of *this onto a Force.")
137

38
        .def("actInv", (Inertia (SE3::*)(const Inertia &) const) &SE3::actInv,
138
             bp::args("self","inertia"), "Returns the result of the inverse of *this onto an Inertia.")
139
140

38
        .def("isApprox",
141
             call<SE3>::isApprox,
142
             isApproxSE3_overload(bp::args("self","other","prec"),
143
                                  "Returns true if *this is approximately equal to other, within the precision given by prec."))
144
145


38
        .def("isIdentity",
146
             &SE3::isIdentity,
147
             isIdentity_overload(bp::args("self","prec"),
148
                                 "Returns true if *this is approximately equal to the identity placement, within the precision given by prec."))
149
150

38
        .def("__invert__",&SE3::inverse,"Returns the inverse of *this.")
151
38
        .def(bp::self * bp::self)
152

38
        .def("__mul__",&__mul__<Motion>)
153
19
        .def("__mul__",&__mul__<Force>)
154
19
        .def("__mul__",&__mul__<Inertia>)
155
19
        .def("__mul__",&__mul__<Vector3>)
156
19
        .add_property("np",&SE3::toHomogeneousMatrix)
157
158
19
        .def(bp::self == bp::self)
159

38
        .def(bp::self != bp::self)
160
161
19
        .def("Identity",&SE3::Identity,"Returns the identity transformation.")
162
19
        .staticmethod("Identity")
163
19
        .def("Random",&SE3::Random,"Returns a random transformation.")
164
19
        .staticmethod("Random")
165
19
        .def("Interpolate",&SE3::template Interpolate<double>,
166
             bp::args("A","B","alpha"),
167
             "Linear interpolation on the SE3 manifold.\n\n"
168
             "This method computes the linear interpolation between A and B, such that the result C = A + (B-A)*t if it would be applied on classic Euclidian space.\n"
169
             "This operation is very similar to the SLERP operation on Rotations.\n"
170
             "Parameters:\n"
171
             "\tA: Initial transformation\n"
172
             "\tB: Target transformation\n"
173
             "\talpha: Interpolation factor")
174

38
        .staticmethod("Interpolate")
175
176
19
        .def("__array__",&SE3::toHomogeneousMatrix)
177
178

19
        .def_pickle(Pickle())
179
        ;
180
19
      }
181
182
19
      static void expose()
183
      {
184
#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION == 6 && EIGENPY_VERSION_AT_LEAST(2,9,0)
185
    typedef PINOCCHIO_SHARED_PTR_HOLDER_TYPE(SE3) HolderType;
186
#else
187
    typedef ::boost::python::detail::not_specified HolderType;
188
#endif
189
        bp::class_<SE3,HolderType>("SE3",
190
                        "SE3 transformation defined by a 3d vector and a rotation matrix.",
191
                        bp::init<>(bp::arg("self"),"Default constructor."))
192
        .def(SE3PythonVisitor<SE3>())
193

38
        .def(CopyableVisitor<SE3>())
194

19
        .def(PrintableVisitor<SE3>())
195
        ;
196
197
19
      }
198
    private:
199
200
      struct Pickle : bp::pickle_suite
201
      {
202
        static
203
        boost::python::tuple
204
        getinitargs(const SE3 & M)
205
        { return bp::make_tuple((Matrix3)M.rotation(),(Vector3)M.translation()); }
206
207
19
        static bool getstate_manages_dict() { return true; }
208
      };
209
210
      static void setIdentity(SE3 & self) { self.setIdentity(); }
211
      static void setRandom(SE3 & self) { self.setRandom(); }
212
213
      template<typename Spatial>
214
      static Spatial __mul__(const SE3 & self, const Spatial & other)
215
      { return self.act(other); }
216
    };
217
218
  } // namespace python
219
} // namespace pinocchio
220
221
#endif // ifndef __pinocchio_python_spatial_se3_hpp__