GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: include/pinocchio/bindings/python/spatial/force.hpp Lines: 38 53 71.7 %
Date: 2024-01-23 21:41:47 Branches: 53 122 43.4 %

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_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"
15
#include "pinocchio/bindings/python/fwd.hpp"
16
#include "pinocchio/bindings/python/utils/copyable.hpp"
17
#include "pinocchio/bindings/python/utils/printable.hpp"
18
19
EIGENPY_DEFINE_STRUCT_ALLOCATOR_SPECIALIZATION(pinocchio::Force)
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>
30
    struct call< ForceTpl<Scalar,Options> >
31
    {
32
      typedef ForceTpl<Scalar,Options> Force;
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
38
    BOOST_PYTHON_FUNCTION_OVERLOADS(isApproxForce_overload,call<Force>::isApprox,2,3)
48
57
    BOOST_PYTHON_FUNCTION_OVERLOADS(isZero_overload,call<Force>::isZero,1,2)
49
50
    template<typename Force>
51
    struct ForcePythonVisitor
52
    : public boost::python::def_visitor< ForcePythonVisitor<Force> >
53
    {
54
      enum { Options = traits<Motion>::Options };
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
19
      void visit(PyClass& cl) const
65
      {
66
        cl
67
        .def(bp::init<>(bp::arg("self"),"Default constructor"))
68

19
        .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

38
        .def(bp::init<Vector6>((bp::args("self","array")),"Init from a vector 6 [force,torque]"))
72

38
        .def(bp::init<Force>((bp::args("self","other")),"Copy constructor."))
73
74

38
        .add_property("linear",
75
                      bp::make_function(&ForcePythonVisitor::getLinear,
76
                                        bp::with_custodian_and_ward_postcall<0,1>()),
77
                      &ForcePythonVisitor::setLinear,
78
                      "Linear part of a *this, corresponding to the linear velocity in case of a Spatial velocity.")
79

38
        .add_property("angular",
80
                      bp::make_function(&ForcePythonVisitor::getAngular,
81
                                        bp::with_custodian_and_ward_postcall<0,1>()),
82
                      &ForcePythonVisitor::setAngular,
83
                      "Angular part of a *this, corresponding to the angular velocity in case of a Spatial velocity.")
84

38
        .add_property("vector",
85
                      bp::make_function((typename Force::ToVectorReturnType (Force::*)())&Force::toVector,
86
                                        bp::return_internal_reference<>()),
87
                      &ForcePythonVisitor::setVector,
88
                      "Returns the components of *this as a 6d vector.")
89

38
        .add_property("np",
90
                      bp::make_function((typename Force::ToVectorReturnType (Force::*)())&Force::toVector,
91
                                        bp::return_internal_reference<>()))
92
93

38
        .def("se3Action",&Force::template se3Action<Scalar,Options>,
94
             bp::args("self","M"),"Returns the result of the dual action of M on *this.")
95
38
        .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

38
        .def("setZero",&ForcePythonVisitor::setZero,bp::arg("self"),
99
             "Set the linear and angular components of *this to zero.")
100

38
        .def("setRandom",&ForcePythonVisitor::setRandom,bp::arg("self"),
101
             "Set the linear and angular components of *this to random values.")
102
103

38
        .def(bp::self + bp::self)
104
19
        .def(bp::self += bp::self)
105
19
        .def(bp::self - bp::self)
106
19
        .def(bp::self -= bp::self)
107
19
        .def(-bp::self)
108
109
19
        .def(bp::self == bp::self)
110
19
        .def(bp::self != bp::self)
111
112
19
        .def(bp::self * Scalar())
113
19
        .def(Scalar() * bp::self)
114
19
        .def(bp::self / Scalar())
115
116
19
        .def("isApprox",
117
             &call<Force>::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

38
        .def("isZero",
122
             &call<Force>::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

38
        .def("Random",&Force::Random,"Returns a random Force.")
127
19
        .staticmethod("Random")
128
19
        .def("Zero",&Force::Zero,"Returns a zero Force.")
129
19
        .staticmethod("Zero")
130
131
19
        .def("__array__",bp::make_function((typename Force::ToVectorReturnType (Force::*)())&Force::toVector,
132
                                           bp::return_internal_reference<>()))
133
134

19
        .def_pickle(Pickle())
135
        ;
136
19
      }
137
138
19
      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)
149
        .def(ForcePythonVisitor<Force>())
150
19
        .def(CopyableVisitor<Force>())
151

19
        .def(PrintableVisitor<Force>())
152
        ;
153
154
19
      }
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
19
        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__