collision-geometries.cc 13.4 KB
Newer Older
Joseph Mirabel's avatar
Joseph Mirabel committed
1
2
3
//
// Software License Agreement (BSD License)
//
4
//  Copyright (c) 2019-2020 CNRS-LAAS INRIA
Joseph Mirabel's avatar
Joseph Mirabel committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//  Author: Joseph Mirabel
//  All rights reserved.
//
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions
//  are met:
//
//   * Redistributions of source code must retain the above copyright
//     notice, this list of conditions and the following disclaimer.
//   * Redistributions in binary form must reproduce the above
//     copyright notice, this list of conditions and the following
//     disclaimer in the documentation and/or other materials provided
//     with the distribution.
//   * Neither the name of CNRS-LAAS. nor the names of its
//     contributors may be used to endorse or promote products derived
//     from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
//  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
//  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
//  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
//  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
//  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
//  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
//  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
//  POSSIBILITY OF SUCH DAMAGE.

#include <boost/python.hpp>

37
38
#include <eigenpy/registration.hpp>

Joseph Mirabel's avatar
Joseph Mirabel committed
39
40
41
42
43
44
45
#include "fcl.hh"

#include <hpp/fcl/fwd.hh>
#include <hpp/fcl/shape/geometric_shapes.h>
#include <hpp/fcl/shape/convex.h>
#include <hpp/fcl/BVH/BVH_model.h>

46
#ifdef HAS_DOXYGEN_AUTODOC
Joseph Mirabel's avatar
Joseph Mirabel committed
47
48
#include "doxygen_autodoc/hpp/fcl/BVH/BVH_model.h"
#include "doxygen_autodoc/hpp/fcl/shape/geometric_shapes.h"
49
50
51
#endif

#include "../doc/python/doxygen.hh"
Joseph Mirabel's avatar
Joseph Mirabel committed
52

Joseph Mirabel's avatar
Joseph Mirabel committed
53
54
55
56
57
58
using namespace boost::python;

using namespace hpp::fcl;
using boost::shared_ptr;
using boost::noncopyable;

Joseph Mirabel's avatar
Joseph Mirabel committed
59
struct BVHModelBaseWrapper
Joseph Mirabel's avatar
Joseph Mirabel committed
60
{
Joseph Mirabel's avatar
Joseph Mirabel committed
61
  static Vec3f vertices (const BVHModelBase& bvh, int i)
Joseph Mirabel's avatar
Joseph Mirabel committed
62
63
64
65
66
  {
    if (i >= bvh.num_vertices) throw std::out_of_range("index is out of range");
    return bvh.vertices[i];
  }

Joseph Mirabel's avatar
Joseph Mirabel committed
67
  static Triangle tri_indices (const BVHModelBase& bvh, int i)
Joseph Mirabel's avatar
Joseph Mirabel committed
68
69
70
71
72
73
74
75
76
77
78
79
  {
    if (i >= bvh.num_tris) throw std::out_of_range("index is out of range");
    return bvh.tri_indices[i];
  }
};

template <typename BV>
void exposeBVHModel (const std::string& bvname)
{
  typedef BVHModel<BV> BVHModel_t;

  std::string type = "BVHModel" + bvname;
Joseph Mirabel's avatar
Joseph Mirabel committed
80
  class_ <BVHModel_t, bases<BVHModelBase>, shared_ptr<BVHModel_t> >
Joseph Mirabel's avatar
Joseph Mirabel committed
81
    (type.c_str(), doxygen::class_doc<BVHModel_t>(), init<>(doxygen::constructor_doc<BVHModel_t>()))
Joseph Mirabel's avatar
Joseph Mirabel committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    ;
}

struct ConvexBaseWrapper
{
  static Vec3f points (const ConvexBase& convex, int i)
  {
    if (i >= convex.num_points) throw std::out_of_range("index is out of range");
    return convex.points[i];
  }

  static list neighbors (const ConvexBase& convex, int i)
  {
    if (i >= convex.num_points) throw std::out_of_range("index is out of range");
    list n;
    for (unsigned char j = 0; j < convex.neighbors[i].count(); ++j)
      n.append (convex.neighbors[i][j]);
    return n;
  }
};

template <typename PolygonT>
struct ConvexWrapper
{
  typedef Convex<PolygonT> Convex_t;

  static PolygonT polygons (const Convex_t& convex, int i)
  {
    if (i >= convex.num_polygons) throw std::out_of_range("index is out of range");
    return convex.polygons[i];
  }
};

void exposeShapes ()
{
  class_ <ShapeBase, bases<CollisionGeometry>, shared_ptr<ShapeBase>, noncopyable>
Joseph Mirabel's avatar
Joseph Mirabel committed
118
    ("ShapeBase", doxygen::class_doc<ShapeBase>(), no_init)
Joseph Mirabel's avatar
Joseph Mirabel committed
119
120
121
122
    //.def ("getObjectType", &CollisionGeometry::getObjectType)
    ;

  class_ <Box, bases<ShapeBase>, shared_ptr<Box> >
Joseph Mirabel's avatar
Joseph Mirabel committed
123
    ("Box", doxygen::class_doc<ShapeBase>(), init<>())
Joseph Mirabel's avatar
Joseph Mirabel committed
124
125
    .def (init<FCL_REAL,FCL_REAL,FCL_REAL>())
    .def (init<Vec3f>())
126
127
128
    .add_property("halfSide",
                  make_getter(&Box::halfSide, return_value_policy<return_by_value>()),
                  make_setter(&Box::halfSide, return_value_policy<return_by_value>()));
Joseph Mirabel's avatar
Joseph Mirabel committed
129
130
131
    ;

  class_ <Capsule, bases<ShapeBase>, shared_ptr<Capsule> >
Joseph Mirabel's avatar
Joseph Mirabel committed
132
    ("Capsule", doxygen::class_doc<Capsule>(), init<FCL_REAL, FCL_REAL>())
Joseph Mirabel's avatar
Joseph Mirabel committed
133
    .def_readwrite ("radius", &Capsule::radius)
134
    .def_readwrite ("halfLength", &Capsule::halfLength)
Joseph Mirabel's avatar
Joseph Mirabel committed
135
136
137
    ;

  class_ <Cone, bases<ShapeBase>, shared_ptr<Cone> >
Joseph Mirabel's avatar
Joseph Mirabel committed
138
    ("Cone", doxygen::class_doc<Cone>(), init<FCL_REAL, FCL_REAL>())
Joseph Mirabel's avatar
Joseph Mirabel committed
139
    .def_readwrite ("radius", &Cone::radius)
140
    .def_readwrite ("halfLength", &Cone::halfLength)
Joseph Mirabel's avatar
Joseph Mirabel committed
141
142
143
    ;

  class_ <ConvexBase, bases<ShapeBase>, shared_ptr<ConvexBase >, noncopyable>
Joseph Mirabel's avatar
Joseph Mirabel committed
144
    ("ConvexBase", doxygen::class_doc<ConvexBase>(), no_init)
Joseph Mirabel's avatar
Joseph Mirabel committed
145
146
147
148
149
150
151
    .def_readonly ("center", &ConvexBase::center)
    .def_readonly ("num_points", &ConvexBase::num_points)
    .def ("points", &ConvexBaseWrapper::points)
    .def ("neighbors", &ConvexBaseWrapper::neighbors)
    ;

  class_ <Convex<Triangle>, bases<ConvexBase>, shared_ptr<Convex<Triangle> >, noncopyable>
Joseph Mirabel's avatar
Joseph Mirabel committed
152
    ("Convex", doxygen::class_doc< Convex<Triangle> >(), no_init)
Joseph Mirabel's avatar
Joseph Mirabel committed
153
154
155
156
157
    .def_readonly ("num_polygons", &Convex<Triangle>::num_polygons)
    .def ("polygons", &ConvexWrapper<Triangle>::polygons)
    ;

  class_ <Cylinder, bases<ShapeBase>, shared_ptr<Cylinder> >
Joseph Mirabel's avatar
Joseph Mirabel committed
158
    ("Cylinder", doxygen::class_doc<Cylinder>(), init<FCL_REAL, FCL_REAL>())
Joseph Mirabel's avatar
Joseph Mirabel committed
159
    .def_readwrite ("radius", &Cylinder::radius)
160
    .def_readwrite ("halfLength", &Cylinder::halfLength)
Joseph Mirabel's avatar
Joseph Mirabel committed
161
162
163
    ;

  class_ <Halfspace, bases<ShapeBase>, shared_ptr<Halfspace> >
Joseph Mirabel's avatar
Joseph Mirabel committed
164
    ("Halfspace", doxygen::class_doc<Halfspace>(), init<const Vec3f&, FCL_REAL>())
Joseph Mirabel's avatar
Joseph Mirabel committed
165
166
167
168
169
170
171
    .def (init<FCL_REAL,FCL_REAL,FCL_REAL,FCL_REAL>())
    .def (init<>())
    .def_readwrite ("n", &Halfspace::n)
    .def_readwrite ("d", &Halfspace::d)
    ;

  class_ <Plane, bases<ShapeBase>, shared_ptr<Plane> >
Joseph Mirabel's avatar
Joseph Mirabel committed
172
    ("Plane", doxygen::class_doc<Plane>(), init<const Vec3f&, FCL_REAL>())
Joseph Mirabel's avatar
Joseph Mirabel committed
173
174
175
176
177
178
179
    .def (init<FCL_REAL,FCL_REAL,FCL_REAL,FCL_REAL>())
    .def (init<>())
    .def_readwrite ("n", &Plane::n)
    .def_readwrite ("d", &Plane::d)
    ;

  class_ <Sphere, bases<ShapeBase>, shared_ptr<Sphere> >
180
181
    ("Sphere", doxygen::class_doc<Sphere>(), init<FCL_REAL>(doxygen::constructor_doc<Sphere>()))
    .def_readwrite ("radius", &Sphere::radius, doxygen::class_attrib_doc<Sphere>("radius"))
Joseph Mirabel's avatar
Joseph Mirabel committed
182
183
184
    ;

  class_ <TriangleP, bases<ShapeBase>, shared_ptr<TriangleP> >
Joseph Mirabel's avatar
Joseph Mirabel committed
185
    ("TriangleP", doxygen::class_doc<TriangleP>(), init<const Vec3f&, const Vec3f&, const Vec3f&>())
Joseph Mirabel's avatar
Joseph Mirabel committed
186
187
188
189
190
191
192
    .def_readwrite ("a", &TriangleP::a)
    .def_readwrite ("b", &TriangleP::b)
    .def_readwrite ("c", &TriangleP::c)
    ;

}

193
194
195
196
boost::python::tuple AABB_distance_proxy(const AABB & self, const AABB & other)
{
  Vec3f P,Q;
  FCL_REAL distance = self.distance(other,&P,&Q);
197
  return boost::python::make_tuple(distance,P,Q);
198
199
}

Joseph Mirabel's avatar
Joseph Mirabel committed
200
201
void exposeCollisionGeometries ()
{
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
  
  if(!eigenpy::register_symbolic_link_to_registered_type<OBJECT_TYPE>())
  {
    enum_<OBJECT_TYPE>("OBJECT_TYPE")
      .value ("OT_UNKNOWN", OT_UNKNOWN)
      .value ("OT_BVH"    , OT_BVH)
      .value ("OT_GEOM"   , OT_GEOM)
      .value ("OT_OCTREE" , OT_OCTREE)
      ;
  }
  
  if(!eigenpy::register_symbolic_link_to_registered_type<NODE_TYPE>())
  {
    enum_<NODE_TYPE>("NODE_TYPE")
      .value ("BV_UNKNOWN", BV_UNKNOWN)
      .value ("BV_AABB"  , BV_AABB)
      .value ("BV_OBB"   , BV_OBB)
      .value ("BV_RSS"   , BV_RSS)
      .value ("BV_kIOS"  , BV_kIOS)
      .value ("BV_OBBRSS", BV_OBBRSS)
      .value ("BV_KDOP16", BV_KDOP16)
      .value ("BV_KDOP18", BV_KDOP18)
      .value ("BV_KDOP24", BV_KDOP24)
      .value ("GEOM_BOX"      , GEOM_BOX)
      .value ("GEOM_SPHERE"   , GEOM_SPHERE)
      .value ("GEOM_CAPSULE"  , GEOM_CAPSULE)
      .value ("GEOM_CONE"     , GEOM_CONE)
      .value ("GEOM_CYLINDER" , GEOM_CYLINDER)
      .value ("GEOM_CONVEX"   , GEOM_CONVEX)
      .value ("GEOM_PLANE"    , GEOM_PLANE)
      .value ("GEOM_HALFSPACE", GEOM_HALFSPACE)
      .value ("GEOM_TRIANGLE" , GEOM_TRIANGLE)
      .value ("GEOM_OCTREE"   , GEOM_OCTREE)
      ;
  }
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
  
  namespace bp = boost::python;
  
  class_<AABB>("AABB",
               "A class describing the AABB collision structure, which is a box in 3D space determined by two diagonal points",
               no_init)
    .def(init<>("Default constructor"))
    .def(init<Vec3f>(bp::arg("v"),"Creating an AABB at position v with zero size."))
    .def(init<Vec3f,Vec3f>(bp::args("a","b"),"Creating an AABB with two endpoints a and b."))
    .def(init<AABB,Vec3f>(bp::args("core","delta"),"Creating an AABB centered as core and is of half-dimension delta."))
    .def(init<Vec3f,Vec3f,Vec3f>(bp::args("a","b","c"),"Creating an AABB contains three points."))
  
    .def("contain",
         (bool (AABB::*)(const Vec3f &) const)&AABB::contain,
         bp::args("self","p"),
         "Check whether the AABB contains a point p.")
    .def("contain",
         (bool (AABB::*)(const AABB &) const)&AABB::contain,
         bp::args("self","other"),
         "Check whether the AABB contains another AABB.")
  
    .def("overlap",
         (bool (AABB::*)(const AABB &) const)&AABB::overlap,
         bp::args("self","other"),
         "Check whether two AABB are overlap.")
    .def("overlap",
         (bool (AABB::*)(const AABB&, AABB&) const)&AABB::overlap,
         bp::args("self","other","overlapping_part"),
         "Check whether two AABB are overlaping and return the overloaping part if true.")
  
    .def("distance",
         (FCL_REAL (AABB::*)(const AABB &) const)&AABB::distance,
         bp::args("self","other"),
         "Distance between two AABBs.")
//    .def("distance",
//         AABB_distance_proxy,
//         bp::args("self","other"),
//         "Distance between two AABBs.")
  
    .def(bp::self + bp::self)
    .def(bp::self += bp::self)
    .def(bp::self += Vec3f())
  
    .def("size",&AABB::volume,bp::arg("self"),"Size of the AABB.")
    .def("center",&AABB::center,bp::arg("self"),"Center of the AABB.")
    .def("width",&AABB::width,bp::arg("self"),"Width of the AABB.")
    .def("height",&AABB::height,bp::arg("self"),"Height of the AABB.")
    .def("depth",&AABB::depth,bp::arg("self"),"Depth of the AABB.")
    .def("volume",&AABB::volume,bp::arg("self"),"Volume of the AABB.")
  
    .def("expand",
         (AABB& (AABB::*)(const AABB &, FCL_REAL))&AABB::expand,
         bp::args("self","core","ratio"),
         "Expand the AABB by increase the thickness of the plate by a ratio.",
         bp::return_internal_reference<>())
    .def("expand",
         (AABB& (AABB::*)(const Vec3f &))&AABB::expand,
         bp::args("self","delta"),
         "Expand the half size of the AABB by delta, and keep the center unchanged.",
         bp::return_internal_reference<>());
  
  def("translate",
      (AABB (*)(const AABB&, const Vec3f&))&translate,
      bp::args("aabb","t"),
      "Translate the center of AABB by t");
  
  def("rotate",
      (AABB (*)(const AABB&, const Matrix3f&))&rotate,
      bp::args("aabb","R"),
      "Rotate the AABB object by R");
Joseph Mirabel's avatar
Joseph Mirabel committed
307

308
309
310
311
312
313
  if(!eigenpy::register_symbolic_link_to_registered_type<CollisionGeometry>())
  {
    class_ <CollisionGeometry, CollisionGeometryPtr_t, noncopyable>
      ("CollisionGeometry", no_init)
      .def ("getObjectType", &CollisionGeometry::getObjectType)
      .def ("getNodeType", &CollisionGeometry::getNodeType)
Joseph Mirabel's avatar
Joseph Mirabel committed
314

315
      .def ("computeLocalAABB", &CollisionGeometry::computeLocalAABB)
Joseph Mirabel's avatar
Joseph Mirabel committed
316

317
318
319
320
      .def ("computeCOM", &CollisionGeometry::computeCOM)
      .def ("computeMomentofInertia", &CollisionGeometry::computeMomentofInertia)
      .def ("computeVolume", &CollisionGeometry::computeVolume)
      .def ("computeMomentofInertiaRelatedToCOM", &CollisionGeometry::computeMomentofInertiaRelatedToCOM)
321

322
323
324
325
326
327
328
329
330
331
332
      .def_readwrite("aabb_radius",&CollisionGeometry::aabb_radius,"AABB radius.")
      .def_readwrite("aabb_center",&CollisionGeometry::aabb_center,"AABB center in local coordinate.")
      .def_readwrite("aabb_local",&CollisionGeometry::aabb_local,"AABB in local coordinate, used for tight AABB when only translation transform.")
    
      .def("isOccupied",&CollisionGeometry::isOccupied,bp::arg("self"),"Whether the object is completely occupied.")
      .def("isFree",&CollisionGeometry::isFree,bp::arg("self"),"Whether the object is completely free.")
      .def("isUncertain",&CollisionGeometry::isUncertain,bp::arg("self"),"Whether the object has some uncertainty.")

      .def_readwrite("cost_density",&CollisionGeometry::cost_density,"Collision cost for unit volume.")
      .def_readwrite("threshold_occupied",&CollisionGeometry::threshold_occupied,"Threshold for occupied ( >= is occupied).")
      .def_readwrite("threshold_free",&CollisionGeometry::threshold_free,"Threshold for free (<= is free).")
333
334
      ;
  }
Joseph Mirabel's avatar
Joseph Mirabel committed
335
336
337

  exposeShapes();

Joseph Mirabel's avatar
Joseph Mirabel committed
338
339
  class_ <BVHModelBase, bases<CollisionGeometry>, BVHModelPtr_t, noncopyable>
    ("BVHModelBase", no_init)
340
341
342
343
    .def ("vertices", &BVHModelBaseWrapper::vertices,
          bp::args("self","index"),"Retrieve the vertex given by its index.")
    .def ("tri_indices", &BVHModelBaseWrapper::tri_indices,
          bp::args("self","index"),"Retrieve the triangle given by its index.")
Joseph Mirabel's avatar
Joseph Mirabel committed
344
345
346
347
348
349
350
    .def_readonly ("num_vertices", &BVHModelBase::num_vertices)
    .def_readonly ("num_tris", &BVHModelBase::num_tris)

    .def_readonly ("convex", &BVHModelBase::convex)

    .def ("buildConvexRepresentation", &BVHModelBase::buildConvexRepresentation)
    ;
Joseph Mirabel's avatar
Joseph Mirabel committed
351
352
353
  exposeBVHModel<OBB    >("OBB"    );
  exposeBVHModel<OBBRSS >("OBBRSS" );
}