Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pybind11 wrappings for Imath::Plane #440

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/pybind11/PyBindImath/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ find_package(pybind11 REQUIRED)
set(PYBINDIMATH_SOURCES
PyBindImathBox.cpp
PyBindImathVec.cpp
PyBindImathPlane.cpp
PyBindImathLine.cpp
)

set(PYBINDIMATH_HEADERS
Expand Down
3 changes: 2 additions & 1 deletion src/pybind11/PyBindImath/PyBindImath.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 30 additions & 0 deletions src/pybind11/PyBindImath/PyBindImathLine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// SPDX-License-Identifier: BSD-3-Clause
// Copyright Contributors to the OpenEXR Project.
//

#include "PyBindImath.h"
#include <ImathLine.h>


namespace PyBindImath {
template <class T, class Q, class S>
void register_line(pybind11::module& m, const char *name)
{
pybind11::class_<T> c(m, name);
c.def(pybind11::init<>(), "Uninitialized by default")
.def(pybind11::init<const Q &, Q &>(), 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<Imath::Line3f, Imath::V3f, float>(m, "Line3f");
register_line<Imath::Line3d, Imath::V3d, double>(m, "Line3d");
}

}
53 changes: 53 additions & 0 deletions src/pybind11/PyBindImath/PyBindImathPlane.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// SPDX-License-Identifier: BSD-3-Clause
// Copyright Contributors to the OpenEXR Project.
//

#include "PyBindImath.h"
#include <ImathPlane.h>
#include <ImathVec.h>
#include <ImathLine.h>

namespace PyBindImath {

template <class T, class L, class Q, class S>
void register_plane(pybind11::module& m, const char *name)
{
pybind11::class_<T> c(m, name);
c.def(pybind11::init<>(), "Uninitialized by default")
.def(pybind11::init<const Q &, S>(), pybind11::arg("normal"), pybind11::arg("distance"), "Initialize with a normal and distance")
.def(pybind11::init<const Q &, const Q &>(), pybind11::arg("point"), pybind11::arg("normal"), "Initialize with a point and a normal")
.def(pybind11::init<const Q &, const Q &, const Q &>(), 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<const Q &, S>(&T::set), pybind11::arg("normal"), pybind11::arg("distance"), "Set via a given normal and distance")
.def("set", pybind11::overload_cast<const Q &, const Q &>(&T::set), pybind11::arg("point"), pybind11::arg("normal"), "Set via a given point and normal")
.def("set", pybind11::overload_cast<const Q &, const Q &, const Q &>(&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<Imath::Plane3f, Imath::Line3f, Imath::V3f, float>(m, "Plane3f");
register_plane<Imath::Plane3d, Imath::Line3d, Imath::V3d, double>(m, "Plane3d");
}

}

3 changes: 3 additions & 0 deletions src/pybind11/PyBindImath/pybindimathmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading