Skip to content

Commit

Permalink
Add pybind11 wrappings for Imath::Frustum (#456)
Browse files Browse the repository at this point in the history
* PyBindImath.h Frustum

Signed-off-by: Todica Ionut <[email protected]>

* pybindimathmodule Frustum

Signed-off-by: Todica Ionut <[email protected]>

* CMakeLists Frustum

Signed-off-by: Todica Ionut <[email protected]>

* Add PyBindImath Frustum

Signed-off-by: Todica Ionut <[email protected]>

---------

Signed-off-by: Todica Ionut <[email protected]>
  • Loading branch information
TodicaIonut authored Dec 16, 2024
1 parent 1bebbac commit 2e78172
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/pybind11/PyBindImath/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ set(PYBINDIMATH_SOURCES
PyBindImathPlane.cpp
PyBindImathLine.cpp
PyBindImathEuler.cpp
PyBindImathFrustum.cpp
)

set(PYBINDIMATH_HEADERS
Expand Down
1 change: 1 addition & 0 deletions src/pybind11/PyBindImath/PyBindImath.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ 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);
PYBINDIMATH_EXPORT void register_imath_euler(pybind11::module& m);
PYBINDIMATH_EXPORT void register_imath_frustum(pybind11::module& m);
}

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

#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");
}

}
1 change: 1 addition & 0 deletions src/pybind11/PyBindImath/pybindimathmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ PYBIND11_MODULE(pybindimath, m)
PyBindImath::register_imath_plane(m);
PyBindImath::register_imath_line(m);
PyBindImath::register_imath_euler(m);
PyBindImath::register_imath_frustum(m);


//
Expand Down

0 comments on commit 2e78172

Please sign in to comment.