/* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022 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 "FlattenMeshHierarchy.h" #include #include #include #include #include #include #include "Magnum/DimensionTraits.h" #include "Magnum/Math/Matrix3.h" #include "Magnum/Math/Matrix4.h" #include "Magnum/Trade/SceneData.h" #include "Magnum/SceneTools/OrderClusterParents.h" namespace Magnum { namespace SceneTools { namespace { template struct SceneDataDimensionTraits; template<> struct SceneDataDimensionTraits<2> { static bool isDimensions(const Trade::SceneData& scene) { return scene.is2D(); } static void transformationsInto(const Trade::SceneData& scene, const Containers::StridedArrayView1D& mappingDestination, const Containers::StridedArrayView1D& transformationDestination) { return scene.transformations2DInto(mappingDestination, transformationDestination); } }; template<> struct SceneDataDimensionTraits<3> { static bool isDimensions(const Trade::SceneData& scene) { return scene.is3D(); } static void transformationsInto(const Trade::SceneData& scene, const Containers::StridedArrayView1D& mappingDestination, const Containers::StridedArrayView1D& transformationDestination) { return scene.transformations3DInto(mappingDestination, transformationDestination); } }; template void flattenMeshHierarchyIntoImplementation(const Trade::SceneData& scene, const Containers::StridedArrayView1D>& outputTransformations, const MatrixTypeFor& globalTransformation) { CORRADE_ASSERT(SceneDataDimensionTraits::isDimensions(scene), "SceneTools::flattenMeshHierarchy(): the scene is not" << dimensions << Debug::nospace << "D", ); const Containers::Optional parentFieldId = scene.findFieldId(Trade::SceneField::Parent); CORRADE_ASSERT(parentFieldId, "SceneTools::flattenMeshHierarchy(): the scene has no hierarchy", ); Containers::Optional meshFieldId = scene.findFieldId(Trade::SceneField::Mesh); CORRADE_ASSERT(outputTransformations.size() == (meshFieldId ? scene.fieldSize(*meshFieldId) : 0), "SceneTools::flattenMeshHierarchyInto(): bad output size, expected" << scene.fieldSize(*meshFieldId) << "but got" << outputTransformations.size(), ); /* If there's no mesh field in the file, nothing to do. Another case is that there is a mesh field but it's empty, then for simplicity we still go through everything. */ if(!meshFieldId) return; /* Allocate a single storage for all temporary data */ Containers::ArrayView> orderedClusteredParents; Containers::ArrayView>> transformations; Containers::ArrayView> absoluteTransformations; Containers::ArrayTuple storage{ /* Output of orderClusterParentsInto() */ {NoInit, scene.fieldSize(*parentFieldId), orderedClusteredParents}, /* Output of scene.transformationsXDInto() */ {NoInit, scene.transformationFieldSize(), transformations}, /* Above transformations but indexed by object ID */ {ValueInit, std::size_t(scene.mappingBound() + 1), absoluteTransformations} }; orderClusterParentsInto(scene, stridedArrayView(orderedClusteredParents).slice(&decltype(orderedClusteredParents)::Type::first), stridedArrayView(orderedClusteredParents).slice(&decltype(orderedClusteredParents)::Type::second)); SceneDataDimensionTraits::transformationsInto(scene, stridedArrayView(transformations).slice(&decltype(transformations)::Type::first), stridedArrayView(transformations).slice(&decltype(transformations)::Type::second)); /* Retrieve transformations of all objects, indexed by object ID. Since not all nodes in the hierarchy may have a transformation assigned, the whole array got initialized to identity first. */ /** @todo switch to a hashmap eventually? */ absoluteTransformations[0] = globalTransformation; for(const Containers::Pair>& transformation: transformations) { CORRADE_INTERNAL_ASSERT(transformation.first() < scene.mappingBound()); absoluteTransformations[transformation.first() + 1] = transformation.second(); } /* Turn the transformations into absolute */ for(const Containers::Pair& parentOffset: orderedClusteredParents) { absoluteTransformations[parentOffset.first() + 1] = absoluteTransformations[parentOffset.second() + 1]* absoluteTransformations[parentOffset.first() + 1]; } /* Allocate the output array, retrieve mesh & material IDs and assign absolute transformations to each. The matrix location is abused for object mapping, which is subsequently replaced by the absolute object transformation for given mesh. */ const auto mapping = Containers::arrayCast(outputTransformations); scene.mappingInto(*meshFieldId, mapping); for(std::size_t i = 0; i != mapping.size(); ++i) { CORRADE_INTERNAL_ASSERT(mapping[i] < scene.mappingBound()); outputTransformations[i] = absoluteTransformations[mapping[i] + 1]; } } template Containers::Array>> flattenMeshHierarchyImplementation(const Trade::SceneData& scene, const MatrixTypeFor& globalTransformation) { const Containers::Optional meshFieldId = scene.findFieldId(Trade::SceneField::Mesh); /* Get the transformations. This will be a no-op if the mesh field isn't present, but will go through other assertions that may still be rather valuable */ Containers::Array>> out{NoInit, meshFieldId ? scene.fieldSize(*meshFieldId) : 0}; flattenMeshHierarchyIntoImplementation(scene, stridedArrayView(out).slice(&decltype(out)::Type::third), globalTransformation); /* Fetch the additional mesh and material ID as well, which are in the same order */ if(meshFieldId) scene.meshesMaterialsInto(nullptr, stridedArrayView(out).slice(&decltype(out)::Type::first), stridedArrayView(out).slice(&decltype(out)::Type::second)); return out; } } Containers::Array> flattenMeshHierarchy2D(const Trade::SceneData& scene, const Matrix3& globalTransformation) { return flattenMeshHierarchyImplementation<2>(scene, globalTransformation); } Containers::Array> flattenMeshHierarchy2D(const Trade::SceneData& scene) { return flattenMeshHierarchyImplementation<2>(scene, {}); } void flattenMeshHierarchy2DInto(const Trade::SceneData& scene, const Containers::StridedArrayView1D& transformations, const Matrix3& globalTransformation) { return flattenMeshHierarchyIntoImplementation<2>(scene, transformations, globalTransformation); } void flattenMeshHierarchy2DInto(const Trade::SceneData& scene, const Containers::StridedArrayView1D& transformations) { return flattenMeshHierarchyIntoImplementation<2>(scene, transformations, {}); } Containers::Array> flattenMeshHierarchy3D(const Trade::SceneData& scene, const Matrix4& globalTransformation) { return flattenMeshHierarchyImplementation<3>(scene, globalTransformation); } Containers::Array> flattenMeshHierarchy3D(const Trade::SceneData& scene) { return flattenMeshHierarchyImplementation<3>(scene, {}); } void flattenMeshHierarchy3DInto(const Trade::SceneData& scene, const Containers::StridedArrayView1D& transformations, const Matrix4& globalTransformation) { return flattenMeshHierarchyIntoImplementation<3>(scene, transformations, globalTransformation); } void flattenMeshHierarchy3DInto(const Trade::SceneData& scene, const Containers::StridedArrayView1D& transformations) { return flattenMeshHierarchyIntoImplementation<3>(scene, transformations, {}); } }}