/* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026 Vladimír Vondruš Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include "magnum/scenegraph.h" namespace magnum { namespace { template struct PyDrawable: SceneGraph::PyFeature> { explicit PyDrawable(SceneGraph::AbstractObject& object, SceneGraph::DrawableGroup* drawables): SceneGraph::PyFeature>{object, drawables} {} void draw(const MatrixTypeFor& transformationMatrix, SceneGraph::Camera& camera) override { PYBIND11_OVERLOAD_PURE_NAME( void, PyDrawable, "draw", draw, transformationMatrix, camera ); } }; template void abstractObject(py::class_, SceneGraph::PyObjectHolder>>& c) { c /* Matrix transformation APIs */ .def("transformation_matrix", &SceneGraph::AbstractObject::transformationMatrix, "Transformation matrix") .def("absolute_transformation_matrix", &SceneGraph::AbstractObject::absoluteTransformationMatrix, "Transformation matrix relative to the root object"); } template void featureGroup(py::class_>& c) { c .def(py::init(), "Constructor") .def("__len__", &SceneGraph::FeatureGroup::size, "Count of features in the group") /* Get item. Fetching the already registered instance and returning that instead of wrapping the pointer again. Need to raise IndexError in order to allow iteration: https://docs.python.org/3/reference/datamodel.html#object.__getitem__ */ .def("__getitem__", [](SceneGraph::FeatureGroup& self, std::size_t index) -> PyFeature& { if(index >= self.size()) { PyErr_SetNone(PyExc_IndexError); throw py::error_already_set{}; } return static_cast(self[index]); }, "Feature at given index") .def("add", [](SceneGraph::FeatureGroup& self, PyFeature& feature) { self.add(feature); }, "Add a feature to the group") .def("remove", [](SceneGraph::FeatureGroup& self, PyFeature& feature) { self.add(feature); }, "Remove a feature from the group"); } template void feature(py::class_, SceneGraph::PyFeature>, SceneGraph::PyFeatureHolder>>& c) { c .def(py::init_alias&>(), "Constructor", py::arg("object")) .def_property_readonly("object", [](SceneGraph::AbstractFeature& self) -> SceneGraph::AbstractObject& { return self.object(); }, "Object holding this feature"); } template void drawable(py::class_, SceneGraph::AbstractFeature, PyDrawable, SceneGraph::PyFeatureHolder>>& c) { c .def(py::init_alias&, SceneGraph::DrawableGroup*>(), "Constructor", py::arg("object"), py::arg("drawables") = nullptr) .def_property_readonly("drawables", [](PyDrawable& self) { return self.drawables(); }, "Group containing this drawable") .def("draw", [](PyDrawable& self, const MatrixTypeFor& transformationMatrix, SceneGraph::Camera& camera) { self.draw(transformationMatrix, camera); }, "Draw the object using given camera", py::arg("transformation_matrix"), py::arg("camera")); } template void camera(py::class_, SceneGraph::AbstractFeature, SceneGraph::PyFeature>, SceneGraph::PyFeatureHolder>>& c) { c .def(py::init_alias&>(), "Constructor", py::arg("object")) .def_property("aspect_ratio_policy", &SceneGraph::Camera::aspectRatioPolicy, /* Using a lambda because the setter has method chaining */ [](SceneGraph::Camera& self, SceneGraph::AspectRatioPolicy policy) { self.setAspectRatioPolicy(policy); }, "Aspect ratio policy") .def_property_readonly("camera_matrix", &SceneGraph::Camera::cameraMatrix, "Camera matrix") .def_property("projection_matrix", &SceneGraph::Camera::projectionMatrix, /* Using a lambda because the setter has method chaining */ [](SceneGraph::Camera& self, const MatrixTypeFor& matrix) { self.setProjectionMatrix(matrix); }, "Projection matrix") .def("projection_size", &SceneGraph::Camera::projectionSize, "Size of (near) XY plane in current projection") .def_property("viewport", &SceneGraph::Camera::viewport, &SceneGraph::Camera::setViewport, "Viewport size") .def("draw", static_cast::*)(SceneGraph::DrawableGroup&)>(&SceneGraph::Camera::draw), "Draw"); } } void scenegraph(py::module_& m) { m.doc() = "Scene graph library"; /* Abstract objects. Returned from feature.object, so need to be registered as well. */ { py::class_> abstractObject2D{m, "AbstractObject2D", "Base object for two-dimensional scenes"}; py::class_> abstractObject3D{m, "AbstractObject3D", "Base object for three-dimensional scenes"}; abstractObject(abstractObject2D); abstractObject(abstractObject3D); } /* Drawables, camera */ { py::enum_{m, "AspectRatioPolicy", "Camera aspect ratio policy"} .value("NOT_PRESERVED", SceneGraph::AspectRatioPolicy::NotPreserved) .value("EXTEND", SceneGraph::AspectRatioPolicy::Extend) .value("CLIP", SceneGraph::AspectRatioPolicy::Clip); py::class_ drawableGroup2D{m, "DrawableGroup2D", "Group of drawables for two-dimensional float scenes"}; py::class_ drawableGroup3D{m, "DrawableGroup3D", "Group of drawables for three-dimensional float scenes"}; py::class_, SceneGraph::PyFeatureHolder> feature2D{m, "AbstractFeature2D", "Base for two-dimensional float features"}; py::class_, SceneGraph::PyFeatureHolder> feature3D{m, "AbstractFeature3D", "Base for three-dimensional float features"}; feature(feature2D); feature(feature3D); py::class_, SceneGraph::PyFeatureHolder> drawable2D{m, "Drawable2D", "Drawable for two-dimensional float scenes"}; py::class_, SceneGraph::PyFeatureHolder> drawable3D{m, "Drawable3D", "Drawable for three-dimensional float scenes"}; py::class_, SceneGraph::PyFeatureHolder> camera2D{m, "Camera2D", "Camera for two-dimensional float scenes"}; py::class_, SceneGraph::PyFeatureHolder> camera3D{m, "Camera3D", "Camera for three-dimensional float scenes"}; featureGroup>(drawableGroup2D); featureGroup>(drawableGroup3D); drawable(drawable2D); drawable(drawable3D); camera(camera2D); camera(camera3D); } /* Concrete transformation implementations */ magnum::scenegraphMatrix(m); magnum::scenegraphTrs(m); } } #ifndef MAGNUM_BUILD_STATIC /* TODO: remove declaration when https://github.com/pybind/pybind11/pull/1863 is released */ extern "C" PYBIND11_EXPORT PyObject* PyInit_scenegraph(); PYBIND11_MODULE(scenegraph, m) { magnum::scenegraph(m); } #endif