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

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

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

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

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

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

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

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

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

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

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