Skip to content

Commit

Permalink
(feat): adds python bindings for AABB type
Browse files Browse the repository at this point in the history
  • Loading branch information
wpumacay committed Oct 12, 2023
1 parent c270d21 commit 99f61d5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
19 changes: 13 additions & 6 deletions python/math3d/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Helper functions used for numpy convertions
from math3d_bindings import ( # type: ignore; math3d -> numpy; numpy -> math3d
# fmt: off
from math3d_bindings import (
AABB_d,
AABB_f,
Euler_d,
Euler_f,
Line_d,
Expand Down Expand Up @@ -50,6 +53,8 @@
vec4_to_nparray_f64,
)

# fmt: on

__all__ = [
# math3d types
"Vector2f",
Expand All @@ -72,6 +77,13 @@
"eConvention",
"Pose3d_f",
"Pose3d_d",
# utilities
"Line_f",
"Line_d",
"Plane_f",
"Plane_d",
"AABB_f",
"AABB_d",
# math3d -> numpy conversions
"vec2_to_nparray_f32",
"vec2_to_nparray_f64",
Expand All @@ -98,9 +110,4 @@
"nparray_to_mat3_f64",
"nparray_to_mat4_f32",
"nparray_to_mat4_f64",
# utilities
"Line_f",
"Line_d",
"Plane_f",
"Plane_d",
]
2 changes: 2 additions & 0 deletions python/math3d/bindings/bindings_py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ PYBIND11_MODULE(math3d_bindings, m) {
::math::bindings_utils_line<::math::float64_t>(m, "Line_d");
::math::bindings_utils_plane<::math::float32_t>(m, "Plane_f");
::math::bindings_utils_plane<::math::float64_t>(m, "Plane_d");
::math::bindings_utils_aabb<::math::float32_t>(m, "AABB_f");
::math::bindings_utils_aabb<::math::float64_t>(m, "AABB_d");

::math::bindings_conversions_functions(m);
}
27 changes: 27 additions & 0 deletions python/math3d/bindings/utils/geometry_helpers_py.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>

#include <math/utils/geometry_helpers.hpp>

Expand Down Expand Up @@ -96,4 +97,30 @@ auto bindings_utils_plane(py::module& m, const char* class_name) -> void {
});
}

template <typename T>
using SFINAE_AABB_BINDINGS = typename std::enable_if<IsScalar<T>::value>::type*;

template <typename T>
// NOLINTNEXTLINE
auto bindings_utils_aabb(py::module& m, const char* class_name) -> void {
using AABB = ::math::AABB<T>;
using Vec3 = ::math::Vector3<T>;
py::class_<AABB>(m, class_name)
.def(py::init<>())
.def(py::init<Vec3, Vec3>())
.def(py::init([](const py::array_t<T>& np_min,
const py::array_t<T>& np_max) -> AABB {
return AABB(::math::nparray_to_vec3<T>(np_min),
::math::nparray_to_vec3<T>(np_max));
}))
.def_readwrite("min", &AABB::p_min)
.def_readwrite("max", &AABB::p_max)
.def("computeCenter", &AABB::computeCenter)
.def("computeCorners", &AABB::computeCorners)
.def("intersects", &AABB::intersects)
.def("__repr__", [](const AABB& self) -> py::str {
return py::str(self.toString());
});
}

} // namespace math

0 comments on commit 99f61d5

Please sign in to comment.