GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: include/pinocchio/bindings/python/multibody/geometry-object.hpp Lines: 48 66 72.7 %
Date: 2024-04-26 13:14:21 Branches: 51 134 38.1 %

Line Branch Exec Source
1
//
2
// Copyright (c) 2017-2021 CNRS INRIA
3
//
4
5
#ifndef __pinocchio_python_geometry_object_hpp__
6
#define __pinocchio_python_geometry_object_hpp__
7
8
#include <boost/python.hpp>
9
#include <eigenpy/memory.hpp>
10
#include <eigenpy/eigen-to-python.hpp>
11
12
#include "pinocchio/multibody/geometry.hpp"
13
14
#if EIGENPY_VERSION_AT_MOST(2,8,1)
15
EIGENPY_DEFINE_STRUCT_ALLOCATOR_SPECIALIZATION(pinocchio::GeometryObject)
16
#endif
17
18
namespace pinocchio
19
{
20
  namespace python
21
  {
22
    namespace bp = boost::python;
23
24
    namespace
25
    {
26
      /// Convert GeometryMaterial boost variant to a Python object.
27
      /// This converter copy the GeometryMaterial content.
28
      struct GeometryMaterialValueToObject : boost::static_visitor<PyObject*>
29
      {
30
        static result_type convert(GeometryMaterial const & gm)
31
        {
32
          return apply_visitor(GeometryMaterialValueToObject(), gm);
33
        }
34
35
        template<typename T>
36
        result_type operator()(T & t) const
37
        {
38
          return bp::incref(bp::object(t).ptr());
39
        }
40
      };
41
42
      /// Convert GeometryMaterial boost variant to a Python object.
43
      /// This converter return the GeometryMaterial reference.
44
      /// The code the create the reference holder is taken from \see boost::python::to_python_indirect.
45
      struct GeometryMaterialRefToObject : boost::static_visitor<PyObject*>
46
      {
47
        static result_type convert(GeometryMaterial const & gm)
48
        {
49
          return apply_visitor(GeometryMaterialRefToObject(), gm);
50
        }
51
52
        template<typename T>
53
        result_type operator()(T & t) const
54
        {
55
          return bp::detail::make_reference_holder::execute(&t);
56
        }
57
      };
58
59
      /// Converter used in \see ReturnInternalVariant.
60
      /// This is inspired by \see boost::python::reference_existing_object.
61
      ///  It will call GeometryMaterialRefToObject to extract the variant reference.
62
      struct GeometryMaterialConverter
63
      {
64
        template <class T>
65
        struct apply
66
        {
67
          struct type
68
          {
69
            inline PyObject* operator()(const GeometryMaterial& gm) const
70
            {
71
              return GeometryMaterialRefToObject::convert(gm);
72
            }
73
74
#ifndef BOOST_PYTHON_NO_PY_SIGNATURES
75
            inline PyTypeObject const* get_pytype()const
76
            {
77
              return bp::converter::registered_pytype<GeometryMaterial>::get_pytype();
78
            }
79
#endif
80
          };
81
        };
82
      };
83
84
      /// Variant of \see boost::python::return_internal_reference that
85
      /// extract GeometryMaterial variant before converting it into a PyObject*
86
      struct GeometryMaterialReturnInternalVariant : bp::return_internal_reference<> {
87
       public:
88
        typedef GeometryMaterialConverter result_converter;
89
      };
90
    }
91
92
93
94
    struct GeometryObjectPythonVisitor
95
    : public boost::python::def_visitor< GeometryObjectPythonVisitor >
96
    {
97
98
      typedef GeometryObject::CollisionGeometryPtr CollisionGeometryPtr;
99
100
      template<class PyClass>
101
19
      void visit(PyClass& cl) const
102
      {
103
19
        cl
104
        .def(bp::init<std::string,FrameIndex,JointIndex,CollisionGeometryPtr,SE3,
105
                      bp::optional<std::string,Eigen::Vector3d,bool,Eigen::Vector4d,std::string,GeometryMaterial> >
106
             (
107
             bp::args("self","name","parent_frame","parent_joint","collision_geometry",
108
                      "placement", "mesh_path", "mesh_scale", "override_material", "mesh_color", "mesh_texture_path",
109
                      "mesh_material"),
110
             "Full constructor of a GeometryObject."))
111

19
        .def(bp::init<std::string,JointIndex,CollisionGeometryPtr,SE3,
112
                      bp::optional<std::string,Eigen::Vector3d,bool,Eigen::Vector4d,std::string,GeometryMaterial> >
113
             (
114
              bp::args("self","name","parent_joint","collision_geometry",
115
                       "placement", "mesh_path", "mesh_scale", "override_material", "mesh_color", "mesh_texture_path",
116
                       "mesh_material"),
117
              "Reduced constructor of a GeometryObject. This constructor does not require to specify the parent frame index."
118
              ))
119

38
        .def(bp::init<const GeometryObject&>
120
             (
121
              bp::args("self","otherGeometryObject"),
122
              "Copy constructor"
123
              ))
124

38
        .add_property("meshScale",
125
                      bp::make_getter(&GeometryObject::meshScale,
126
                                      bp::return_internal_reference<>()),
127
                      bp::make_setter(&GeometryObject::meshScale),
128
                       "Scaling parameter of the mesh.")
129

38
        .add_property("meshColor",
130
                      bp::make_getter(&GeometryObject::meshColor,
131
                                      bp::return_internal_reference<>()),
132
                      bp::make_setter(&GeometryObject::meshColor),
133
                      "Color rgba of the mesh.")
134

38
        .def_readwrite("geometry", &GeometryObject::geometry,
135
                       "The FCL CollisionGeometry associated to the given GeometryObject.")
136
19
        .def_readwrite("name", &GeometryObject::name,
137
                       "Name associated to the given GeometryObject.")
138
19
        .def_readwrite("parentJoint", &GeometryObject::parentJoint,
139
                       "Index of the parent joint.")
140
19
        .def_readwrite("parentFrame", &GeometryObject::parentFrame,
141
                       "Index of the parent frame.")
142
19
        .def_readwrite("placement",&GeometryObject::placement,
143
                       "Position of geometry object in parent joint's frame.")
144
19
        .def_readwrite("meshPath", &GeometryObject::meshPath,
145
                       "Path to the mesh file.")
146
19
        .def_readwrite("overrideMaterial", &GeometryObject::overrideMaterial,
147
                       "Boolean that tells whether material information is stored inside the given GeometryObject.")
148
19
        .def_readwrite("meshTexturePath", &GeometryObject::meshTexturePath,
149
                       "Path to the mesh texture file.")
150
19
        .def_readwrite("disableCollision", &GeometryObject::disableCollision,
151
                       "If true, no collision or distance check will be done between the Geometry and any other geometry.")
152
19
        .add_property("meshMaterial",
153
                      bp::make_getter(&GeometryObject::meshMaterial,
154
                                      GeometryMaterialReturnInternalVariant()),
155
                      bp::make_setter(&GeometryObject::meshMaterial),
156
                      "Material associated to the mesh (applied only if overrideMaterial is True)")
157
158

38
        .def(bp::self == bp::self)
159
38
        .def(bp::self != bp::self)
160
161
#ifdef PINOCCHIO_WITH_HPP_FCL
162
19
          .def("CreateCapsule", &GeometryObjectPythonVisitor::maker_capsule)
163

19
          .staticmethod("CreateCapsule")
164
#endif // PINOCCHIO_WITH_HPP_FCL
165
        ;
166
167
        // Check registration
168
        {
169
19
          const bp::type_info info = bp::type_id<CollisionGeometryPtr>();
170
19
          const bp::converter::registration* reg = bp::converter::registry::query(info);
171
          // We just need to check if the type shared_ptr<CollisionGeometry> exist in the registry
172
19
          if(!reg)
173
            bp::register_ptr_to_python<CollisionGeometryPtr>();
174
        }
175
19
      }
176
177
#ifdef PINOCCHIO_WITH_HPP_FCL
178
      static GeometryObject maker_capsule(const double radius, const double length)
179
      {
180
        return GeometryObject("",FrameIndex(0),JointIndex(0),
181
                              pinocchio::shared_ptr<fcl::CollisionGeometry>(new fcl::Capsule(radius, length)),
182
                              SE3::Identity());
183
184
      }
185
#endif // PINOCCHIO_WITH_HPP_FCL
186
187
      static GeometryMaterial get_content(const GeometryMaterial& gm)
188
      {
189
        return gm;
190
      }
191
192
19
      static void expose()
193
      {
194
        /// Define material types
195
19
        bp::class_<GeometryNoMaterial>("GeometryNoMaterial", bp::init<>())
196

19
        .def(bp::init<GeometryNoMaterial>());
197
19
        bp::class_<GeometryPhongMaterial>("GeometryPhongMaterial", bp::init<>())
198

19
        .def(bp::init<GeometryPhongMaterial>())
199

19
        .def(bp::init<Eigen::Vector4d, Eigen::Vector4d, double>())
200
        .add_property("meshEmissionColor",
201
19
                      bp::make_getter(&GeometryPhongMaterial::meshEmissionColor,
202
                                      bp::return_internal_reference<>()),
203
38
                      bp::make_setter(&GeometryPhongMaterial::meshEmissionColor),
204
19
                      "RGBA emission (ambient) color value of the mesh")
205
        .add_property("meshSpecularColor",
206
19
                      bp::make_getter(&GeometryPhongMaterial::meshSpecularColor,
207
                                      bp::return_internal_reference<>()),
208
38
                      bp::make_setter(&GeometryPhongMaterial::meshSpecularColor),
209
19
                      "RGBA specular value of the mesh")
210
19
        .def_readwrite("meshShininess", &GeometryPhongMaterial::meshShininess,
211
19
                       "Shininess associated to the specular lighting model (between 0 and 1)");
212
213
        /// Define material conversion from C++ variant to python object
214
19
        bp::to_python_converter<GeometryMaterial, GeometryMaterialValueToObject>();
215
216
        /// Define material conversion from python object to C++ object
217
19
        bp::implicitly_convertible<GeometryNoMaterial, GeometryMaterial>();
218
19
        bp::implicitly_convertible<GeometryPhongMaterial, GeometryMaterial>();
219
220
19
        bp::class_<GeometryObject>("GeometryObject",
221
                                   "A wrapper on a collision geometry including its parent joint, parent frame, placement in parent joint's frame.\n\n",
222
                                   bp::no_init
223
                                   )
224
19
        .def(GeometryObjectPythonVisitor())
225
        ;
226
227
38
        bp::enum_<GeometryType>("GeometryType")
228
19
        .value("VISUAL",VISUAL)
229
19
        .value("COLLISION",COLLISION)
230
19
        .export_values()
231
        ;
232
19
      }
233
234
    };
235
236
237
  } // namespace python
238
} // namespace pinocchio
239
240
#endif // ifndef __pinocchio_python_geometry_object_hpp__