Skip to content

Commit ce06333

Browse files
committed
python-nb : Expose BVH collision geoms
1 parent d31b16d commit ce06333

File tree

4 files changed

+99
-5
lines changed

4 files changed

+99
-5
lines changed

python-nb/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ find_package(Python REQUIRED COMPONENTS Interpreter Development)
22
find_package(nanobind CONFIG REQUIRED)
33

44
set(PYTHON_LIB_NAME_V2 coal_pywrap_v2)
5-
set(coal_pywrap_v2_SOURCES collision-geometries.cc math.cc module.cc)
5+
set(coal_pywrap_v2_SOURCES bvh.cc collision-geometries.cc math.cc module.cc)
66

77
nanobind_add_module(${PYTHON_LIB_NAME_V2} NB_STATIC LTO NB_SUPPRESS_WARNINGS ${coal_pywrap_v2_SOURCES})
88
target_link_libraries(

python-nb/bvh.cc

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "fwd.h"
2+
#include "coal/BVH/BVH_model.h"
3+
4+
#include "pickle.hh"
5+
#include "serializable.hh"
6+
7+
#include "coal/serialization/BVH_model.h"
8+
9+
using namespace coal;
10+
using namespace nb::literals;
11+
12+
template <typename BV>
13+
void exposeBVHModel(nb::module_& m, const char* name) {
14+
using BVHType = BVHModel<BV>;
15+
nb::class_<BVHType, BVHModelBase>(m, name)
16+
.def(nb::init<>())
17+
.def(nb::init<const BVHType&>(), "other"_a)
18+
.DEF_CLASS_FUNC(BVHType, getNumBVs)
19+
.DEF_CLASS_FUNC(BVHType, makeParentRelative)
20+
.DEF_CLASS_FUNC(BVHType, memUsage)
21+
.def("clone", &BVHType::clone, nb::rv_policy::take_ownership)
22+
.def(python::v2::PickleVisitor<BVHType>())
23+
.def(python::v2::SerializableVisitor<BVHType>());
24+
}
25+
26+
void exposeBVHModels(nb::module_& m) {
27+
using RowMatrixX3 = Eigen::Matrix<double, Eigen::Dynamic, 3, Eigen::RowMajor>;
28+
using MapRowMatrixX3 = Eigen::Map<RowMatrixX3>;
29+
nb::class_<BVHModelBase, CollisionGeometry>(m, "BVHModelBase")
30+
.def("vertex",
31+
[](BVHModelBase& bvh, size_t i) -> Vec3s& {
32+
if (i >= bvh.num_vertices) throw nb::index_error();
33+
return (*bvh.vertices)[i];
34+
})
35+
.def(
36+
"vertices",
37+
[](BVHModelBase& bvh) {
38+
if (bvh.num_vertices > 0) {
39+
return MapRowMatrixX3{bvh.vertices->data()->data(),
40+
bvh.num_vertices, 3};
41+
} else {
42+
return MapRowMatrixX3{nullptr, 0, 3};
43+
}
44+
},
45+
"Retrieve all vertices")
46+
.def(
47+
"tri_indices",
48+
[](const BVHModelBase& bvh, size_t i) {
49+
if (i >= bvh.num_tris) {
50+
nb::index_error("Triangle index out of range");
51+
}
52+
return (*bvh.tri_indices)[i];
53+
},
54+
"index"_a, "Retrieve the triangle given by its index.")
55+
.def_ro("num_vertices", &BVHModelBase::num_vertices)
56+
.def_ro("num_tris", &BVHModelBase::num_tris)
57+
.def_ro("build_state", &BVHModelBase::build_state)
58+
59+
.def_ro("convex", &BVHModelBase::convex)
60+
61+
.def("buildConvexRepresentation",
62+
&BVHModelBase::buildConvexRepresentation, "share_memory"_a)
63+
.def("buildConvexHull", &BVHModelBase::buildConvexHull, "keepTriangle"_a,
64+
"qhullCommand"_a = NULL)
65+
66+
.DEF_CLASS_FUNC(BVHModelBase, beginModel)
67+
.DEF_CLASS_FUNC(BVHModelBase, addVertex)
68+
.DEF_CLASS_FUNC(BVHModelBase, addVertices)
69+
.DEF_CLASS_FUNC(BVHModelBase, addTriangle)
70+
.DEF_CLASS_FUNC(BVHModelBase, addTriangles);
71+
72+
exposeBVHModel<OBB>(m, "OBB");
73+
exposeBVHModel<OBBRSS>(m, "OBBRSS");
74+
}

python-nb/collision-geometries.cc

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#include "fwd.h"
22
#include "serializable.hh"
3+
#include <nanobind/eigen/dense.h>
34
#include <nanobind/operators.h>
45

56
#include "coal/fwd.hh"
67
#include "coal/shape/geometric_shapes.h"
78
#include "coal/shape/convex.h"
8-
#include "coal/BVH/BVH_model.h"
99
#include "coal/hfield.h"
1010

1111
#include "coal/serialization/memory.h"
@@ -14,6 +14,8 @@ using namespace coal;
1414
using namespace nb::literals;
1515
using Triangles = std::vector<Triangle>;
1616

17+
void exposeBVHModels(nb::module_& m);
18+
1719
void exposeCollisionGeometries(nb::module_& m) {
1820
nb::enum_<BVHModelType>(m, "BVHModelType")
1921
.value("BVH_MODEL_UNKNOWN", BVH_MODEL_UNKNOWN)
@@ -62,6 +64,21 @@ void exposeCollisionGeometries(nb::module_& m) {
6264
.value("HF_AABB", HF_AABB)
6365
.value("HF_OBBRSS", HF_OBBRSS)
6466
.export_values();
67+
68+
nb::class_<CollisionGeometry>(m, "CollisionGeometry")
69+
.DEF_CLASS_FUNC(CollisionGeometry, getObjectType)
70+
.DEF_CLASS_FUNC(CollisionGeometry, getNodeType)
71+
72+
.DEF_CLASS_FUNC(CollisionGeometry, computeLocalAABB)
73+
.DEF_CLASS_FUNC(CollisionGeometry, computeCOM)
74+
.DEF_CLASS_FUNC(CollisionGeometry, computeMomentofInertia)
75+
.DEF_CLASS_FUNC(CollisionGeometry, computeVolume)
76+
.DEF_CLASS_FUNC(CollisionGeometry, computeMomentofInertiaRelatedToCOM)
77+
78+
.def(nb::self == nb::self)
79+
.def(nb::self != nb::self);
80+
81+
exposeBVHModels(m);
6582
}
6683

6784
void exposeCollisionObject(nb::module_& m) {
@@ -75,8 +92,9 @@ void exposeCollisionObject(nb::module_& m) {
7592
"cgeom"_a, "R"_a, "t"_a, "compute_local_aabb"_a = true)
7693
.def(nb::self == nb::self)
7794
.def(nb::self != nb::self)
78-
.def("getObjectType", &CollisionObject::getObjectType)
79-
.def("getNodeType", &CollisionObject::getNodeType)
95+
96+
.DEF_CLASS_FUNC(CollisionObject, getObjectType)
97+
.DEF_CLASS_FUNC(CollisionObject, getNodeType)
8098

8199
// properties
82100
.def_prop_rw("translation", &CollisionObject::getTranslation,
@@ -95,7 +113,7 @@ void exposeCollisionObject(nb::module_& m) {
95113
.def(
96114
"getAABB", [](CollisionObject& o) -> AABB& { return o.getAABB(); },
97115
nb::rv_policy::automatic_reference)
98-
.def("computeAABB", &CollisionObject::computeAABB)
116+
.DEF_CLASS_FUNC(CollisionObject, computeAABB)
99117

100118
.def("setCollisionGeometry", &CollisionObject::setCollisionGeometry,
101119
"cgeom"_a, "compute_local_aabb"_a = true)

python-nb/fwd.h

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#include <nanobind/nanobind.h>
22

33
namespace nb = nanobind;
4+
5+
#define DEF_CLASS_FUNC(cls_name, name) def(#name, &cls_name::name)

0 commit comments

Comments
 (0)