From 0138a46b1998b7185d8af01e9571e7cd8976a162 Mon Sep 17 00:00:00 2001 From: Milana Tratsevska <147627981+mtrats@users.noreply.github.com> Date: Fri, 4 Oct 2024 17:35:47 -0400 Subject: [PATCH] Implemented pybind11 wrapper for plane (#440) Signed-off-by: Milana Tratsevska --- src/pybind11/PyBindImath/CMakeLists.txt | 2 + src/pybind11/PyBindImath/PyBindImath.h | 3 +- src/pybind11/PyBindImath/PyBindImathLine.cpp | 30 +++++++++++ src/pybind11/PyBindImath/PyBindImathPlane.cpp | 53 +++++++++++++++++++ .../PyBindImath/pybindimathmodule.cpp | 3 ++ 5 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 src/pybind11/PyBindImath/PyBindImathLine.cpp create mode 100644 src/pybind11/PyBindImath/PyBindImathPlane.cpp diff --git a/src/pybind11/PyBindImath/CMakeLists.txt b/src/pybind11/PyBindImath/CMakeLists.txt index 9c148cda..f66ea4b5 100644 --- a/src/pybind11/PyBindImath/CMakeLists.txt +++ b/src/pybind11/PyBindImath/CMakeLists.txt @@ -11,6 +11,8 @@ find_package(pybind11 REQUIRED) set(PYBINDIMATH_SOURCES PyBindImathBox.cpp PyBindImathVec.cpp + PyBindImathPlane.cpp + PyBindImathLine.cpp ) set(PYBINDIMATH_HEADERS diff --git a/src/pybind11/PyBindImath/PyBindImath.h b/src/pybind11/PyBindImath/PyBindImath.h index 346f4b63..8ccebf6c 100644 --- a/src/pybind11/PyBindImath/PyBindImath.h +++ b/src/pybind11/PyBindImath/PyBindImath.h @@ -19,7 +19,8 @@ namespace PyBindImath { PYBINDIMATH_EXPORT void register_imath_vec(pybind11::module& m); PYBINDIMATH_EXPORT void register_imath_box(pybind11::module& m); - +PYBINDIMATH_EXPORT void register_imath_plane(pybind11::module& m); +PYBINDIMATH_EXPORT void register_imath_line(pybind11::module& m); } #endif diff --git a/src/pybind11/PyBindImath/PyBindImathLine.cpp b/src/pybind11/PyBindImath/PyBindImathLine.cpp new file mode 100644 index 00000000..15fe40d7 --- /dev/null +++ b/src/pybind11/PyBindImath/PyBindImathLine.cpp @@ -0,0 +1,30 @@ +// +// SPDX-License-Identifier: BSD-3-Clause +// Copyright Contributors to the OpenEXR Project. +// + +#include "PyBindImath.h" +#include + + +namespace PyBindImath { +template +void register_line(pybind11::module& m, const char *name) +{ + pybind11::class_ c(m, name); + c.def(pybind11::init<>(), "Uninitialized by default") + .def(pybind11::init(), pybind11::arg("point1"), pybind11::arg("point2"), "Initialize with two points. The direction is the difference between the points.") + .def("__str__", [](const T &obj) { + std::stringstream ss; + ss << obj; + return ss.str(); + }); +} + +void register_imath_line(pybind11::module &m) +{ + register_line(m, "Line3f"); + register_line(m, "Line3d"); +} + +} diff --git a/src/pybind11/PyBindImath/PyBindImathPlane.cpp b/src/pybind11/PyBindImath/PyBindImathPlane.cpp new file mode 100644 index 00000000..2423135f --- /dev/null +++ b/src/pybind11/PyBindImath/PyBindImathPlane.cpp @@ -0,0 +1,53 @@ +// +// SPDX-License-Identifier: BSD-3-Clause +// Copyright Contributors to the OpenEXR Project. +// + +#include "PyBindImath.h" +#include +#include +#include + +namespace PyBindImath { + +template +void register_plane(pybind11::module& m, const char *name) +{ + pybind11::class_ c(m, name); + c.def(pybind11::init<>(), "Uninitialized by default") + .def(pybind11::init(), pybind11::arg("normal"), pybind11::arg("distance"), "Initialize with a normal and distance") + .def(pybind11::init(), pybind11::arg("point"), pybind11::arg("normal"), "Initialize with a point and a normal") + .def(pybind11::init(), pybind11::arg("point1"), pybind11::arg("point2"), pybind11::arg("point3"), "Initialize with three points") + .def_readwrite("normal", &T::normal, "The normal to the plane") + .def_readwrite("distance", &T::distance, "The distance from the origin to the plane") + .def("set", pybind11::overload_cast(&T::set), pybind11::arg("normal"), pybind11::arg("distance"), "Set via a given normal and distance") + .def("set", pybind11::overload_cast(&T::set), pybind11::arg("point"), pybind11::arg("normal"), "Set via a given point and normal") + .def("set", pybind11::overload_cast(&T::set), pybind11::arg("point1"), pybind11::arg("point2"), pybind11::arg("point3"), "Set via three points") + .def("intersect", [](T& self, const L& line) { + Q intersection; + bool result = self.intersect(line, intersection); + return pybind11::make_tuple(result, intersection); + }, pybind11::arg("line"), "Determine if a line intersects the plane. Returns a tuple (bool, Vec3). True if the line intersects the plane. Second element is the point of intersection") + .def("intersectT", [](T& self, const L& line) { + S intersection; + bool result = self.intersectT(line, intersection); + return pybind11::make_tuple(result, intersection); + }, pybind11::arg("line"), "Determine if a line intersects the plane. Returns a tuple (bool, T). True if the line intersects the plane. Second element is the parametric value of the point of intersection") + .def("distanceTo", &T::distanceTo, pybind11::arg("point"), "Returns the distance from a point to the plane") + .def("reflectPoint", &T::reflectPoint, pybind11::arg("point"), "Reflects the given point around the plane") + .def("reflectVector", &T::reflectVector, pybind11::arg("v"), "Reflects the direction vector around the plane") + .def("__str__", [](const T &obj) { + std::stringstream ss; + ss << obj; + return ss.str(); + }); +} + + void register_imath_plane(pybind11::module &m) +{ + register_plane(m, "Plane3f"); + register_plane(m, "Plane3d"); +} + +} + diff --git a/src/pybind11/PyBindImath/pybindimathmodule.cpp b/src/pybind11/PyBindImath/pybindimathmodule.cpp index ea4ca8cf..01d6734f 100644 --- a/src/pybind11/PyBindImath/pybindimathmodule.cpp +++ b/src/pybind11/PyBindImath/pybindimathmodule.cpp @@ -13,6 +13,9 @@ PYBIND11_MODULE(pybindimath, m) PyBindImath::register_imath_vec(m); PyBindImath::register_imath_box(m); + PyBindImath::register_imath_plane(m); + PyBindImath::register_imath_line(m); + // // Initialize constants