16 changed files with 340 additions and 7 deletions
@ -0,0 +1,40 @@
|
||||
.. |
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, |
||||
2020, 2021, 2022 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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. |
||||
.. |
||||
|
||||
.. py:function:: magnum.scenetools.flatten_transformation_hierarchy2d |
||||
:raise KeyError: If :p:`field` does not exist in :p:`scene` |
||||
:raise IndexError: If :p:`field_id` negative or not less than |
||||
:ref:`trade.SceneData.field_count` |
||||
:raise AssertionError: If :p:`scene` is not 2D |
||||
:raise AssertionError: If :p:`scene` does not have |
||||
:ref:`trade.SceneField.PARENT` |
||||
|
||||
.. py:function:: magnum.scenetools.flatten_transformation_hierarchy3d |
||||
:raise KeyError: If :p:`field` does not exist in :p:`scene` |
||||
:raise IndexError: If :p:`field_id` negative or not less than |
||||
:ref:`trade.SceneData.field_count` |
||||
:raise AssertionError: If :p:`scene` is not 2D |
||||
:raise AssertionError: If :p:`scene` does not have |
||||
:ref:`trade.SceneField.PARENT` |
||||
@ -0,0 +1,138 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, |
||||
2020, 2021, 2022 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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 <pybind11/pybind11.h> |
||||
#include <pybind11/stl.h> /* for std::vector */ |
||||
#include <Corrade/Containers/Optional.h> |
||||
#include <Corrade/Containers/ArrayViewStl.h> |
||||
#include <Magnum/Math/Matrix3.h> |
||||
#include <Magnum/Math/Matrix4.h> |
||||
#include <Magnum/SceneTools/FlattenTransformationHierarchy.h> |
||||
#include <Magnum/Trade/SceneData.h> |
||||
|
||||
#include "magnum/bootstrap.h" |
||||
|
||||
namespace magnum { |
||||
|
||||
void scenetools(py::module_& m) { |
||||
m.doc() = "Scene manipulation and optimization tools"; |
||||
|
||||
#ifndef MAGNUM_BUILD_STATIC |
||||
/* These are a part of the same module in the static build, no need to
|
||||
import (also can't import because there it's _magnum.*) */ |
||||
py::module_::import("magnum.trade"); |
||||
#endif |
||||
|
||||
m |
||||
.def("flatten_transformation_hierarchy2d", [](const Trade::SceneData& scene, Trade::SceneField field, const Matrix3& globalTransformation) { |
||||
const Containers::Optional<UnsignedInt> fieldId = scene.findFieldId(field); |
||||
if(!fieldId) { |
||||
PyErr_SetNone(PyExc_KeyError); |
||||
throw py::error_already_set{}; |
||||
} |
||||
if(!scene.is2D()) { |
||||
PyErr_SetString(PyExc_AssertionError, "the scene is not 2D"); |
||||
throw py::error_already_set{}; |
||||
} |
||||
if(!scene.hasField(Trade::SceneField::Parent)) { |
||||
PyErr_SetString(PyExc_AssertionError, "the scene has no hierarchy"); |
||||
throw py::error_already_set{}; |
||||
} |
||||
|
||||
/** @todo maybe do a caster for arrays, finally?! */ |
||||
std::vector<Matrix3> out(scene.fieldSize(*fieldId)); |
||||
SceneTools::flattenTransformationHierarchy2DInto(scene, *fieldId, out, globalTransformation); |
||||
return out; |
||||
}, "Flatten a 2D transformation hierarchy for given field", py::arg("scene"), py::arg("field"), py::arg("global_transformation") = Matrix3{}) |
||||
.def("flatten_transformation_hierarchy2d", [](const Trade::SceneData& scene, UnsignedInt fieldId, const Matrix3& globalTransformation) { |
||||
if(fieldId >= scene.fieldCount()) { |
||||
PyErr_SetNone(PyExc_IndexError); |
||||
throw py::error_already_set{}; |
||||
} |
||||
if(!scene.is2D()) { |
||||
PyErr_SetString(PyExc_AssertionError, "the scene is not 2D"); |
||||
throw py::error_already_set{}; |
||||
} |
||||
if(!scene.hasField(Trade::SceneField::Parent)) { |
||||
PyErr_SetString(PyExc_AssertionError, "the scene has no hierarchy"); |
||||
throw py::error_already_set{}; |
||||
} |
||||
|
||||
/** @todo maybe do a caster for arrays, finally?! */ |
||||
std::vector<Matrix3> out(scene.fieldSize(fieldId)); |
||||
SceneTools::flattenTransformationHierarchy2DInto(scene, fieldId, out, globalTransformation); |
||||
return out; |
||||
}, "Flatten a 2D transformation hierarchy for given field ID", py::arg("scene"), py::arg("field_id"), py::arg("global_transformation") = Matrix3{}) |
||||
.def("flatten_transformation_hierarchy3d", [](const Trade::SceneData& scene, Trade::SceneField field, const Matrix4& globalTransformation) { |
||||
const Containers::Optional<UnsignedInt> fieldId = scene.findFieldId(field); |
||||
if(!fieldId) { |
||||
PyErr_SetNone(PyExc_KeyError); |
||||
throw py::error_already_set{}; |
||||
} |
||||
if(!scene.is3D()) { |
||||
PyErr_SetString(PyExc_AssertionError, "the scene is not 3D"); |
||||
throw py::error_already_set{}; |
||||
} |
||||
if(!scene.hasField(Trade::SceneField::Parent)) { |
||||
PyErr_SetString(PyExc_AssertionError, "the scene has no hierarchy"); |
||||
throw py::error_already_set{}; |
||||
} |
||||
|
||||
/** @todo maybe do a caster for arrays, finally?! */ |
||||
std::vector<Matrix4> out(scene.fieldSize(*fieldId)); |
||||
SceneTools::flattenTransformationHierarchy3DInto(scene, *fieldId, out, globalTransformation); |
||||
return out; |
||||
}, "Flatten a 3D transformation hierarchy for given field", py::arg("scene"), py::arg("field"), py::arg("global_transformation") = Matrix4{}) |
||||
.def("flatten_transformation_hierarchy3d", [](const Trade::SceneData& scene, UnsignedInt fieldId, const Matrix4& globalTransformation) { |
||||
if(fieldId >= scene.fieldCount()) { |
||||
PyErr_SetNone(PyExc_IndexError); |
||||
throw py::error_already_set{}; |
||||
} |
||||
if(!scene.is3D()) { |
||||
PyErr_SetString(PyExc_AssertionError, "the scene is not 3D"); |
||||
throw py::error_already_set{}; |
||||
} |
||||
if(!scene.hasField(Trade::SceneField::Parent)) { |
||||
PyErr_SetString(PyExc_AssertionError, "the scene has no hierarchy"); |
||||
throw py::error_already_set{}; |
||||
} |
||||
|
||||
/** @todo maybe do a caster for arrays, finally?! */ |
||||
std::vector<Matrix4> out(scene.fieldSize(fieldId)); |
||||
SceneTools::flattenTransformationHierarchy3DInto(scene, fieldId, out, globalTransformation); |
||||
return out; |
||||
}, "Flatten a 3D transformation hierarchy for given field ID", py::arg("scene"), py::arg("field_id"), py::arg("global_transformation") = Matrix4{}); |
||||
} |
||||
|
||||
} |
||||
|
||||
#ifndef MAGNUM_BUILD_STATIC |
||||
/* TODO: remove declaration when https://github.com/pybind/pybind11/pull/1863
|
||||
is released */ |
||||
extern "C" PYBIND11_EXPORT PyObject* PyInit_scenetools(); |
||||
PYBIND11_MODULE(scenetools, m) { |
||||
magnum::scenetools(m); |
||||
} |
||||
#endif |
||||
@ -0,0 +1,119 @@
|
||||
# |
||||
# This file is part of Magnum. |
||||
# |
||||
# Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, |
||||
# 2020, 2021, 2022 Vladimír Vondruš <mosra@centrum.cz> |
||||
# |
||||
# 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 |
||||
|
||||
from corrade import containers |
||||
from magnum import * |
||||
from magnum import scenetools, trade |
||||
|
||||
class FlattenTransformationHierarchy(unittest.TestCase): |
||||
def test_2d(self): |
||||
# The only way to get a 2D scene is via PrimitiveImporter |
||||
importer = trade.ImporterManager().load_and_instantiate('PrimitiveImporter') |
||||
importer.open_data(containers.ArrayView()) |
||||
|
||||
scene = importer.scene(0) |
||||
self.assertTrue(scene.is_2d) |
||||
|
||||
transformations = scenetools.flatten_transformation_hierarchy2d(scene, trade.SceneField.MESH) |
||||
self.assertEqual(len(transformations), scene.field_size(trade.SceneField.MESH)) |
||||
self.assertEqual(transformations[0], Matrix3.translation((-4.5, -3.0))) |
||||
|
||||
def test_2d_field_id(self): |
||||
# The only way to get a 2D scene is via PrimitiveImporter |
||||
importer = trade.ImporterManager().load_and_instantiate('PrimitiveImporter') |
||||
importer.open_data(containers.ArrayView()) |
||||
|
||||
scene = importer.scene(0) |
||||
self.assertTrue(scene.is_2d) |
||||
|
||||
mesh_field = scene.field_id(trade.SceneField.MESH) |
||||
|
||||
transformations = scenetools.flatten_transformation_hierarchy2d(scene, mesh_field) |
||||
self.assertEqual(len(transformations), scene.field_size(mesh_field)) |
||||
self.assertEqual(transformations[0], Matrix3.translation((-4.5, -3.0))) |
||||
|
||||
def test_3d(self): |
||||
importer = trade.ImporterManager().load_and_instantiate('PrimitiveImporter') |
||||
importer.open_data(containers.ArrayView()) |
||||
|
||||
scene = importer.scene(1) |
||||
self.assertTrue(scene.is_3d) |
||||
|
||||
transformations = scenetools.flatten_transformation_hierarchy3d(scene, trade.SceneField.MESH) |
||||
self.assertEqual(len(transformations), scene.field_size(trade.SceneField.MESH)) |
||||
self.assertEqual(transformations[0], Matrix4.translation((-4.5, -3.0, 0.0))) |
||||
|
||||
def test_3d_field_id(self): |
||||
importer = trade.ImporterManager().load_and_instantiate('PrimitiveImporter') |
||||
importer.open_data(containers.ArrayView()) |
||||
|
||||
scene = importer.scene(1) |
||||
self.assertTrue(scene.is_3d) |
||||
|
||||
mesh_field = scene.field_id(trade.SceneField.MESH) |
||||
|
||||
transformations = scenetools.flatten_transformation_hierarchy3d(scene, mesh_field) |
||||
self.assertEqual(len(transformations), scene.field_size(mesh_field)) |
||||
self.assertEqual(transformations[0], Matrix4.translation((-4.5, -3.0, 0.0))) |
||||
|
||||
def test_field_not_found(self): |
||||
importer = trade.ImporterManager().load_and_instantiate('PrimitiveImporter') |
||||
importer.open_data(containers.ArrayView()) |
||||
|
||||
scene = importer.scene(0) |
||||
|
||||
with self.assertRaises(KeyError): |
||||
scenetools.flatten_transformation_hierarchy2d(scene, trade.SceneField.LIGHT) |
||||
with self.assertRaises(KeyError): |
||||
scenetools.flatten_transformation_hierarchy3d(scene, trade.SceneField.LIGHT) |
||||
with self.assertRaises(IndexError): |
||||
scenetools.flatten_transformation_hierarchy2d(scene, scene.field_count) |
||||
with self.assertRaises(IndexError): |
||||
scenetools.flatten_transformation_hierarchy3d(scene, scene.field_count) |
||||
|
||||
def test_not_2d_not_3d(self): |
||||
importer = trade.ImporterManager().load_and_instantiate('PrimitiveImporter') |
||||
importer.open_data(containers.ArrayView()) |
||||
|
||||
scene2d = importer.scene(0) |
||||
scene3d = importer.scene(1) |
||||
self.assertFalse(scene2d.is_3d) |
||||
self.assertFalse(scene3d.is_2d) |
||||
|
||||
with self.assertRaisesRegex(AssertionError, "the scene is not 2D"): |
||||
scenetools.flatten_transformation_hierarchy2d(scene3d, trade.SceneField.MESH) |
||||
with self.assertRaisesRegex(AssertionError, "the scene is not 2D"): |
||||
scenetools.flatten_transformation_hierarchy2d(scene3d, scene2d.field_id(trade.SceneField.MESH)) |
||||
with self.assertRaisesRegex(AssertionError, "the scene is not 3D"): |
||||
scenetools.flatten_transformation_hierarchy3d(scene2d, trade.SceneField.MESH) |
||||
with self.assertRaisesRegex(AssertionError, "the scene is not 3D"): |
||||
scenetools.flatten_transformation_hierarchy3d(scene2d, scene3d.field_id(trade.SceneField.MESH)) |
||||
|
||||
def test_no_hierarchy(self): |
||||
# TODO implement once it's possible to create / import a scene without |
||||
# the parent field |
||||
pass |
||||
Loading…
Reference in new issue