From 2b0dfaa22a52c52eb2b7b1eca6b5836692d9b397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 22 May 2019 23:23:35 +0200 Subject: [PATCH] python: expose MeshTools::compile(). --- doc/python/conf.py | 3 +- package/ci/travis-desktop-gles.sh | 2 +- package/ci/travis-desktop.sh | 2 +- src/python/CMakeLists.txt | 1 + src/python/magnum/CMakeLists.txt | 15 +++++- src/python/magnum/PyMesh.h | 42 ++++++++++++++++ src/python/magnum/gl.cpp | 8 +-- src/python/magnum/meshtools.cpp | 56 +++++++++++++++++++++ src/python/magnum/test/test_meshtools_gl.py | 44 ++++++++++++++++ src/python/setup.py.cmake | 1 + 10 files changed, 163 insertions(+), 11 deletions(-) create mode 100644 src/python/magnum/PyMesh.h create mode 100644 src/python/magnum/meshtools.cpp create mode 100644 src/python/magnum/test/test_meshtools_gl.py diff --git a/doc/python/conf.py b/doc/python/conf.py index 7c81499..27dec18 100644 --- a/doc/python/conf.py +++ b/doc/python/conf.py @@ -9,6 +9,7 @@ import corrade.containers import magnum import magnum.gl +import magnum.meshtools import magnum.platform import magnum.platform.egl import magnum.platform.glx @@ -21,7 +22,7 @@ import magnum.trade # So the doc see everything # TODO: use just +=, m.css should reorder this on its own -magnum.__all__ = ['math', 'gl', 'platform', 'primitives', 'shaders', 'scenegraph', 'trade'] + magnum.__all__ +magnum.__all__ = ['math', 'gl', 'meshtools', 'platform', 'primitives', 'shaders', 'scenegraph', 'trade'] + magnum.__all__ # TODO ugh... can this be expressed directly in pybind? magnum.gl.__annotations__ = {} diff --git a/package/ci/travis-desktop-gles.sh b/package/ci/travis-desktop-gles.sh index d2598ba..6a7562a 100755 --- a/package/ci/travis-desktop-gles.sh +++ b/package/ci/travis-desktop-gles.sh @@ -34,7 +34,7 @@ cmake .. \ -DWITH_AUDIO=OFF \ -DWITH_DEBUGTOOLS=OFF \ -DWITH_GL=ON \ - -DWITH_MESHTOOLS=OFF \ + -DWITH_MESHTOOLS=ON \ -DWITH_PRIMITIVES=ON \ -DWITH_SCENEGRAPH=ON \ -DWITH_SHADERS=ON \ diff --git a/package/ci/travis-desktop.sh b/package/ci/travis-desktop.sh index 8685af4..65686ae 100755 --- a/package/ci/travis-desktop.sh +++ b/package/ci/travis-desktop.sh @@ -28,7 +28,7 @@ cmake .. \ -DWITH_AUDIO=OFF \ -DWITH_DEBUGTOOLS=OFF \ -DWITH_GL=ON \ - -DWITH_MESHTOOLS=OFF \ + -DWITH_MESHTOOLS=ON \ -DWITH_PRIMITIVES=ON \ -DWITH_SCENEGRAPH=ON \ -DWITH_SHADERS=ON \ diff --git a/src/python/CMakeLists.txt b/src/python/CMakeLists.txt index fa51edc..d8d7b74 100644 --- a/src/python/CMakeLists.txt +++ b/src/python/CMakeLists.txt @@ -41,6 +41,7 @@ add_subdirectory(magnum) # configure_file() and then replacing generator expressions with file(GENERATE) foreach(target magnum_gl + magnum_meshtools magnum_primitives magnum_scenegraph magnum_shaders diff --git a/src/python/magnum/CMakeLists.txt b/src/python/magnum/CMakeLists.txt index 53a784a..35966f5 100644 --- a/src/python/magnum/CMakeLists.txt +++ b/src/python/magnum/CMakeLists.txt @@ -24,7 +24,7 @@ # # *Not* REQUIRED -find_package(Magnum COMPONENTS GL Shaders SceneGraph Primitives Trade) +find_package(Magnum COMPONENTS GL Shaders SceneGraph MeshTools Primitives Trade) set(magnum_SRCS magnum.cpp @@ -55,6 +55,19 @@ if(Magnum_GL_FOUND) LIBRARY_OUTPUT_DIRECTORY ${output_dir}/magnum) endif() +if(Magnum_MeshTools_FOUND) + set(magnum_meshtools_SRCS + meshtools.cpp) + + pybind11_add_module(magnum_meshtools ${magnum_meshtools_SRCS}) + target_include_directories(magnum_meshtools PRIVATE ${PROJECT_SOURCE_DIR}/src/python) + target_link_libraries(magnum_meshtools PRIVATE Magnum::MeshTools) + set_target_properties(magnum_meshtools PROPERTIES + FOLDER "python" + OUTPUT_NAME "meshtools" + LIBRARY_OUTPUT_DIRECTORY ${output_dir}/magnum) +endif() + if(Magnum_Primitives_FOUND) set(magnum_primitives_SRCS primitives.cpp) diff --git a/src/python/magnum/PyMesh.h b/src/python/magnum/PyMesh.h new file mode 100644 index 0000000..f71adad --- /dev/null +++ b/src/python/magnum/PyMesh.h @@ -0,0 +1,42 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 + 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 "magnum/bootstrap.h" + +namespace magnum { + +struct PyMesh: GL::Mesh { + explicit PyMesh(GL::MeshPrimitive primitive): GL::Mesh(primitive) {} + explicit PyMesh(MeshPrimitive primitive): GL::Mesh(primitive) {} + explicit PyMesh(GL::Mesh&& mesh): GL::Mesh(std::move(mesh)) {} + + std::vector buffers; +}; + +} diff --git a/src/python/magnum/gl.cpp b/src/python/magnum/gl.cpp index 3968b12..8be800a 100644 --- a/src/python/magnum/gl.cpp +++ b/src/python/magnum/gl.cpp @@ -36,16 +36,10 @@ #include "corrade/EnumOperators.h" #include "magnum/bootstrap.h" #include "magnum/NonDestructible.h" +#include "magnum/PyMesh.h" namespace magnum { namespace { -struct PyMesh: GL::Mesh { - explicit PyMesh(GL::MeshPrimitive primitive): GL::Mesh(primitive) {} - explicit PyMesh(MeshPrimitive primitive): GL::Mesh(primitive) {} - - std::vector buffers; -}; - void gl(py::module& m) { py::module::import("corrade.containers"); diff --git a/src/python/magnum/meshtools.cpp b/src/python/magnum/meshtools.cpp new file mode 100644 index 0000000..8bd2ab5 --- /dev/null +++ b/src/python/magnum/meshtools.cpp @@ -0,0 +1,56 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 + 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 + +#include "magnum/bootstrap.h" +#include "magnum/PyMesh.h" + +namespace magnum { namespace { + +void meshtools(py::module& m) { + m.import("magnum.gl"); + m.import("magnum.trade"); + + m + .def("compile", [](const Trade::MeshData2D& data) { + return PyMesh{MeshTools::compile(data)}; + }, "Compile 2D mesh data") + .def("compile", [](const Trade::MeshData3D& data) { + return PyMesh{MeshTools::compile(data)}; + }, "Compile 3D mesh data"); +} + +}} + +PYBIND11_MODULE(meshtools, m) { + m.doc() = "Mesh tools"; + + magnum::meshtools(m); +} diff --git a/src/python/magnum/test/test_meshtools_gl.py b/src/python/magnum/test/test_meshtools_gl.py new file mode 100644 index 0000000..1dcb178 --- /dev/null +++ b/src/python/magnum/test/test_meshtools_gl.py @@ -0,0 +1,44 @@ +# +# This file is part of Magnum. +# +# Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 +# 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. +# + +import unittest + +# setUpModule gets called before everything else, skipping if GL tests can't +# be run +from . import GLTestCase, setUpModule + +from magnum import * +from magnum import gl, meshtools, primitives + +class Compile(GLTestCase): + def test_2d(self): + a = meshtools.compile(primitives.square_wireframe()) + self.assertEqual(a.primitive, gl.MeshPrimitive.LINE_LOOP) + self.assertEqual(a.count, 4) + + def test_3d(self): + a = meshtools.compile(primitives.cube_solid()) + self.assertEqual(a.primitive, gl.MeshPrimitive.TRIANGLES) + self.assertEqual(a.count, 36) diff --git a/src/python/setup.py.cmake b/src/python/setup.py.cmake index f03fec2..8f90e64 100644 --- a/src/python/setup.py.cmake +++ b/src/python/setup.py.cmake @@ -34,6 +34,7 @@ extension_paths = { 'corrade.containers': '$', 'magnum._magnum': '$', 'magnum.gl': '${magnum_gl_file}', + 'magnum.meshtools': '${magnum_meshtools_file}', 'magnum.primitives': '${magnum_primitives_file}', 'magnum.scenegraph': '${magnum_scenegraph_file}', 'magnum.shaders': '${magnum_shaders_file}',