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::Frustum #442

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
55 changes: 55 additions & 0 deletions src/pybind11/PyBindImath/PyBindImathFrustum
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "PyBindImath.h"
#include <ImathFrustum.h>
#include <ImathVec.h>
#include <ImathMatrix.h>

namespace PyBindImath {

template <class T, class V, class M>
void register_frustum(pybind11::module& m, const char *name)
{
pybind11::class_<T> c(m, name);
c.def(pybind11::init<>(), "Uninitialized by default")
.def(pybind11::init<T>(), pybind11::arg("frustum"), "Copy constructor")
.def(pybind11::init<S>(), pybind11::arg("nearPlane"), pybind11::arg("farPlane"), pybind11::arg("fovx"), pybind11::arg("aspect"), "Initialize with basic frustum properties")

.def_readwrite("nearPlane", &T::nearPlane, "The near clipping plane")
.def_readwrite("farPlane", &T::farPlane, "The far clipping plane")
.def_readwrite("fovx", &T::fovx, "The field of view in x direction")
.def_readwrite("aspect", &T::aspect, "The aspect ratio")

.def("set", pybind11::overload_cast<S, S, S, S>(&T::set), pybind11::arg("nearPlane"), pybind11::arg("farPlane"), pybind11::arg("fovx"), pybind11::arg("aspect"), "Set frustum properties")
.def("projectionMatrix", &T::projectionMatrix, "Returns the projection matrix of the frustum")
.def("transform", &T::transform, pybind11::arg("matrix"), "Applies a transformation matrix to the frustum")
.def("intersects", [](T& self, const V& point) {
bool result = self.intersects(point);
return result;
}, pybind11::arg("point"), "Determines if the point is inside the frustum")

.def("screenToWorld", [](T& self, const V& screenPoint) {
V worldPoint;
self.screenToWorld(screenPoint, worldPoint);
return worldPoint;
}, pybind11::arg("screenPoint"), "Convert a screen space point to world space")

.def("worldToScreen", [](T& self, const V& worldPoint) {
V screenPoint;
self.worldToScreen(worldPoint, screenPoint);
return screenPoint;
}, pybind11::arg("worldPoint"), "Convert a world space point to screen space")

.def("__str__", [](const T &obj) {
std::stringstream ss;
ss << obj;
return ss.str();
});
}

void register_imath_frustum(pybind11::module &m)
{
register_frustum<Imath::Frustumf, Imath::V3f, Imath::M44f>(m, "Frustumf");
register_frustum<Imath::Frustumd, Imath::V3d, Imath::M44d>(m, "Frustumd");
}

}

Loading