mirror of https://github.com/mosra/magnum.git
Browse Source
Took me a while to realize that tying this to a certain hardcoded field isn't a good idea. The new variant is useful also for example for getting absolute light positions or just whatever else. Besides taking a SceneField there's now also an overload taking a field ID to avoid double lookups. The only behavioral difference compared to the old API is that the field is now required to exist, instead of the API being a silent no-op if not present. Eventually these APIs may get further extended to take a BitArrayView of objects for which to calculate the transforms, for example to take only meshes that are a part of the hierarchy, or meshes that satisfy an arbitrary other condition. Which will also resolve the remaining concerns with the API. I'm still keeping it marked as experimental tho, the usefulness isn't set in stone yet. The old APIs are marked as deprecated and implemented using the new ones.pull/617/head
9 changed files with 1120 additions and 226 deletions
@ -0,0 +1,214 @@
|
||||
/*
|
||||
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 "FlattenTransformationHierarchy.h" |
||||
|
||||
#include <Corrade/Containers/Array.h> |
||||
#include <Corrade/Containers/ArrayTuple.h> |
||||
#include <Corrade/Containers/GrowableArray.h> |
||||
#include <Corrade/Containers/Optional.h> |
||||
#include <Corrade/Containers/Pair.h> |
||||
|
||||
#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<UnsignedInt> 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<UnsignedInt>& mappingDestination, const Containers::StridedArrayView1D<Matrix3>& 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<UnsignedInt>& mappingDestination, const Containers::StridedArrayView1D<Matrix4>& transformationDestination) { |
||||
return scene.transformations3DInto(mappingDestination, transformationDestination); |
||||
} |
||||
}; |
||||
|
||||
template<UnsignedInt dimensions> void flattenTransformationHierarchyIntoImplementation(const Trade::SceneData& scene, const UnsignedInt fieldId, const Containers::StridedArrayView1D<MatrixTypeFor<dimensions, Float>>& outputTransformations, const MatrixTypeFor<dimensions, Float>& globalTransformation) { |
||||
CORRADE_ASSERT(SceneDataDimensionTraits<dimensions>::isDimensions(scene), |
||||
"SceneTools::flattenTransformationHierarchy(): the scene is not" << dimensions << Debug::nospace << "D", ); |
||||
CORRADE_ASSERT(fieldId < scene.fieldCount(), |
||||
"SceneTools::flattenTransformationHierarchy(): index" << fieldId << "out of range for" << scene.fieldCount() << "fields", ); |
||||
const Containers::Optional<UnsignedInt> parentFieldId = scene.findFieldId(Trade::SceneField::Parent); |
||||
CORRADE_ASSERT(parentFieldId, |
||||
"SceneTools::flattenTransformationHierarchy(): the scene has no hierarchy", ); |
||||
CORRADE_ASSERT(outputTransformations.size() == scene.fieldSize(fieldId), |
||||
"SceneTools::flattenTransformationHierarchyInto(): bad output size, expected" << scene.fieldSize(fieldId) << "but got" << outputTransformations.size(), ); |
||||
|
||||
/* Allocate a single storage for all temporary data */ |
||||
Containers::ArrayView<Containers::Pair<UnsignedInt, Int>> orderedClusteredParents; |
||||
Containers::ArrayView<Containers::Pair<UnsignedInt, MatrixTypeFor<dimensions, Float>>> transformations; |
||||
Containers::ArrayView<MatrixTypeFor<dimensions, Float>> 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<dimensions>::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<UnsignedInt, MatrixTypeFor<dimensions, Float>>& transformation: transformations) { |
||||
CORRADE_INTERNAL_ASSERT(transformation.first() < scene.mappingBound()); |
||||
absoluteTransformations[transformation.first() + 1] = transformation.second(); |
||||
} |
||||
|
||||
/* Turn the transformations into absolute */ |
||||
for(const Containers::Pair<UnsignedInt, Int>& 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<UnsignedInt>(outputTransformations); |
||||
scene.mappingInto(fieldId, 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<UnsignedInt dimensions> void flattenTransformationHierarchyIntoImplementation(const Trade::SceneData& scene, const Trade::SceneField field, const Containers::StridedArrayView1D<MatrixTypeFor<dimensions, Float>>& outputTransformations, const MatrixTypeFor<dimensions, Float>& globalTransformation) { |
||||
const Containers::Optional<UnsignedInt> fieldId = scene.findFieldId(field); |
||||
CORRADE_ASSERT(fieldId, |
||||
"SceneTools::flattenTransformationHierarchyInto(): field" << field << "not found", ); |
||||
|
||||
flattenTransformationHierarchyIntoImplementation<dimensions>(scene, *fieldId, outputTransformations, globalTransformation); |
||||
} |
||||
|
||||
template<UnsignedInt dimensions> Containers::Array<MatrixTypeFor<dimensions, Float>> flattenTransformationHierarchyImplementation(const Trade::SceneData& scene, const UnsignedInt fieldId, const MatrixTypeFor<dimensions, Float>& globalTransformation) { |
||||
CORRADE_ASSERT(fieldId < scene.fieldCount(), |
||||
"SceneTools::flattenTransformationHierarchy(): index" << fieldId << "out of range for" << scene.fieldCount() << "fields", {}); |
||||
|
||||
Containers::Array<MatrixTypeFor<dimensions, Float>> out{NoInit, scene.fieldSize(fieldId)}; |
||||
flattenTransformationHierarchyIntoImplementation<dimensions>(scene, fieldId, out, globalTransformation); |
||||
return out; |
||||
} |
||||
|
||||
template<UnsignedInt dimensions> Containers::Array<MatrixTypeFor<dimensions, Float>> flattenTransformationHierarchyImplementation(const Trade::SceneData& scene, const Trade::SceneField field, const MatrixTypeFor<dimensions, Float>& globalTransformation) { |
||||
const Containers::Optional<UnsignedInt> fieldId = scene.findFieldId(field); |
||||
CORRADE_ASSERT(fieldId, |
||||
"SceneTools::flattenTransformationHierarchy(): field" << field << "not found", {}); |
||||
|
||||
Containers::Array<MatrixTypeFor<dimensions, Float>> out{NoInit, scene.fieldSize(*fieldId)}; |
||||
flattenTransformationHierarchyIntoImplementation<dimensions>(scene, *fieldId, out, globalTransformation); |
||||
return out; |
||||
} |
||||
|
||||
} |
||||
|
||||
Containers::Array<Matrix3> flattenTransformationHierarchy2D(const Trade::SceneData& scene, const Trade::SceneField field, const Matrix3& globalTransformation) { |
||||
return flattenTransformationHierarchyImplementation<2>(scene, field, globalTransformation); |
||||
} |
||||
|
||||
Containers::Array<Matrix3> flattenTransformationHierarchy2D(const Trade::SceneData& scene, const Trade::SceneField field) { |
||||
return flattenTransformationHierarchyImplementation<2>(scene, field, {}); |
||||
} |
||||
|
||||
Containers::Array<Matrix3> flattenTransformationHierarchy2D(const Trade::SceneData& scene, const UnsignedInt fieldId, const Matrix3& globalTransformation) { |
||||
return flattenTransformationHierarchyImplementation<2>(scene, fieldId, globalTransformation); |
||||
} |
||||
|
||||
Containers::Array<Matrix3> flattenTransformationHierarchy2D(const Trade::SceneData& scene, const UnsignedInt fieldId) { |
||||
return flattenTransformationHierarchyImplementation<2>(scene, fieldId, {}); |
||||
} |
||||
|
||||
void flattenTransformationHierarchy2DInto(const Trade::SceneData& scene, const Trade::SceneField field, const Containers::StridedArrayView1D<Matrix3>& transformations, const Matrix3& globalTransformation) { |
||||
return flattenTransformationHierarchyIntoImplementation<2>(scene, field, transformations, globalTransformation); |
||||
} |
||||
|
||||
void flattenTransformationHierarchy2DInto(const Trade::SceneData& scene, const Trade::SceneField field, const Containers::StridedArrayView1D<Matrix3>& transformations) { |
||||
return flattenTransformationHierarchyIntoImplementation<2>(scene, field, transformations, {}); |
||||
} |
||||
|
||||
void flattenTransformationHierarchy2DInto(const Trade::SceneData& scene, const UnsignedInt fieldId, const Containers::StridedArrayView1D<Matrix3>& transformations, const Matrix3& globalTransformation) { |
||||
return flattenTransformationHierarchyIntoImplementation<2>(scene, fieldId, transformations, globalTransformation); |
||||
} |
||||
|
||||
void flattenTransformationHierarchy2DInto(const Trade::SceneData& scene, const UnsignedInt fieldId, const Containers::StridedArrayView1D<Matrix3>& transformations) { |
||||
return flattenTransformationHierarchyIntoImplementation<2>(scene, fieldId, transformations, {}); |
||||
} |
||||
|
||||
Containers::Array<Matrix4> flattenTransformationHierarchy3D(const Trade::SceneData& scene, const Trade::SceneField field, const Matrix4& globalTransformation) { |
||||
return flattenTransformationHierarchyImplementation<3>(scene, field, globalTransformation); |
||||
} |
||||
|
||||
Containers::Array<Matrix4> flattenTransformationHierarchy3D(const Trade::SceneData& scene, const Trade::SceneField field) { |
||||
return flattenTransformationHierarchyImplementation<3>(scene, field, {}); |
||||
} |
||||
|
||||
Containers::Array<Matrix4> flattenTransformationHierarchy3D(const Trade::SceneData& scene, const UnsignedInt fieldId, const Matrix4& globalTransformation) { |
||||
return flattenTransformationHierarchyImplementation<3>(scene, fieldId, globalTransformation); |
||||
} |
||||
|
||||
Containers::Array<Matrix4> flattenTransformationHierarchy3D(const Trade::SceneData& scene, const UnsignedInt fieldId) { |
||||
return flattenTransformationHierarchyImplementation<3>(scene, fieldId, {}); |
||||
} |
||||
|
||||
void flattenTransformationHierarchy3DInto(const Trade::SceneData& scene, const Trade::SceneField field, const Containers::StridedArrayView1D<Matrix4>& transformations, const Matrix4& globalTransformation) { |
||||
return flattenTransformationHierarchyIntoImplementation<3>(scene, field, transformations, globalTransformation); |
||||
} |
||||
|
||||
void flattenTransformationHierarchy3DInto(const Trade::SceneData& scene, const Trade::SceneField field, const Containers::StridedArrayView1D<Matrix4>& transformations) { |
||||
return flattenTransformationHierarchyIntoImplementation<3>(scene, field, transformations, {}); |
||||
} |
||||
|
||||
void flattenTransformationHierarchy3DInto(const Trade::SceneData& scene, const UnsignedInt fieldId, const Containers::StridedArrayView1D<Matrix4>& transformations, const Matrix4& globalTransformation) { |
||||
return flattenTransformationHierarchyIntoImplementation<3>(scene, fieldId, transformations, globalTransformation); |
||||
} |
||||
|
||||
void flattenTransformationHierarchy3DInto(const Trade::SceneData& scene, const UnsignedInt fieldId, const Containers::StridedArrayView1D<Matrix4>& transformations) { |
||||
return flattenTransformationHierarchyIntoImplementation<3>(scene, fieldId, transformations, {}); |
||||
} |
||||
|
||||
}} |
||||
@ -0,0 +1,243 @@
|
||||
#ifndef Magnum_SceneTools_FlattenTransformationHierarchy_h |
||||
#define Magnum_SceneTools_FlattenTransformationHierarchy_h |
||||
/*
|
||||
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. |
||||
*/ |
||||
|
||||
/** @file
|
||||
* @brief Function @ref Magnum::SceneTools::flattenTransformationHierarchy2D(), @ref Magnum::SceneTools::flattenTransformationHierarchy2DInto(), @ref Magnum::SceneTools::flattenTransformationHierarchy3D(), @ref Magnum::SceneTools::flattenTransformationHierarchy3DInto() |
||||
* @m_since_latest |
||||
*/ |
||||
|
||||
#include "Magnum/Magnum.h" |
||||
#include "Magnum/SceneTools/visibility.h" |
||||
#include "Magnum/Trade/Trade.h" |
||||
|
||||
namespace Magnum { namespace SceneTools { |
||||
|
||||
/**
|
||||
@brief Flatten a 2D transformation hierarchy for given field |
||||
@m_since_latest |
||||
|
||||
For all entries of given @p field in @p scene returns an absolute |
||||
transformation of the object they're attached to in the scene with |
||||
@p globalTransformation prepended. The @ref Trade::SceneField::Parent field is |
||||
expected to be contained in the scene, having no cycles or duplicates, and the |
||||
scene is expected to be 2D. The @p field is expected to be present, if it's |
||||
empty, the function returns an empty array. |
||||
|
||||
The operation is done in an @f$ \mathcal{O}(m + n) @f$ execution time and |
||||
memory complexity, with @f$ m @f$ being size of @p field and @f$ n @f$ being |
||||
@ref Trade::SceneData::mappingBound(). The function calls |
||||
@ref orderClusterParents() internally. |
||||
|
||||
The returned data are in the same order as object mapping entries in @p field. |
||||
Fields attached to objects without a @ref Trade::SceneField::Parent or to |
||||
objects in loose hierarchy subtrees will have their transformation set to an |
||||
unspecified value. |
||||
|
||||
This function can be used for example to flatten a mesh hierarchy, bake |
||||
the transformations to actual meshes and then concatenate them together into a |
||||
single mesh: |
||||
|
||||
@snippet MagnumSceneTools.cpp flattenTransformationHierarchy2D-mesh-concatenate |
||||
|
||||
@experimental |
||||
|
||||
@see @ref flattenTransformationHierarchy2D(const Trade::SceneData&, UnsignedInt, const Matrix3&), |
||||
@ref flattenTransformationHierarchy2DInto(), |
||||
@ref flattenTransformationHierarchy3D(), @ref Trade::SceneData::hasField(), |
||||
@ref Trade::SceneData::is2D() |
||||
*/ |
||||
#ifdef DOXYGEN_GENERATING_OUTPUT |
||||
MAGNUM_SCENETOOLS_EXPORT Containers::Array<Matrix3> flattenTransformationHierarchy2D(const Trade::SceneData& scene, Trade::SceneField field, const Matrix3& globalTransformation = {}); |
||||
#else |
||||
/* To avoid including Matrix3 */ |
||||
MAGNUM_SCENETOOLS_EXPORT Containers::Array<Matrix3> flattenTransformationHierarchy2D(const Trade::SceneData& scene, Trade::SceneField field, const Matrix3& globalTransformation); |
||||
MAGNUM_SCENETOOLS_EXPORT Containers::Array<Matrix3> flattenTransformationHierarchy2D(const Trade::SceneData& scene, Trade::SceneField field); |
||||
#endif |
||||
|
||||
/**
|
||||
@brief Flatten a 2D transformation hierarchy for given field ID |
||||
@m_since_latest |
||||
|
||||
A variant of @ref flattenTransformationHierarchy2D(const Trade::SceneData&, Trade::SceneField, const Matrix3&) |
||||
that takes a field ID instead of name. Useful for example in combination with |
||||
@ref Trade::SceneData::findFieldId() to avoid a double lookup if it isn't clear |
||||
if a field exists at all. The @p fieldId is expected to be smaller than |
||||
@ref Trade::SceneData::fieldCount(). |
||||
@experimental |
||||
*/ |
||||
#ifdef DOXYGEN_GENERATING_OUTPUT |
||||
MAGNUM_SCENETOOLS_EXPORT Containers::Array<Matrix3> flattenTransformationHierarchy2D(const Trade::SceneData& scene, UnsignedInt fieldId, const Matrix3& globalTransformation = {}); |
||||
#else |
||||
/* To avoid including Matrix3 */ |
||||
MAGNUM_SCENETOOLS_EXPORT Containers::Array<Matrix3> flattenTransformationHierarchy2D(const Trade::SceneData& scene, UnsignedInt fieldId, const Matrix3& globalTransformation); |
||||
MAGNUM_SCENETOOLS_EXPORT Containers::Array<Matrix3> flattenTransformationHierarchy2D(const Trade::SceneData& scene, UnsignedInt fieldId); |
||||
#endif |
||||
|
||||
/**
|
||||
@brief Flatten a 2D transformation hierarchy for given field into an existing array |
||||
@param[in] scene Input scene |
||||
@param[in] field Field to calculate the transformations for |
||||
@param[out] transformations Where to put the calculated transformations |
||||
@param[in] globalTransformation Global transformation to prepend |
||||
@m_since_latest |
||||
|
||||
A variant of @ref flattenTransformationHierarchy2D(const Trade::SceneData&, Trade::SceneField, const Matrix3&) |
||||
that fills existing memory instead of allocating a new array. The |
||||
@p transformations array is expected to have the same size as the @p field. |
||||
@experimental |
||||
*/ |
||||
#ifdef DOXYGEN_GENERATING_OUTPUT |
||||
MAGNUM_SCENETOOLS_EXPORT void flattenTransformationHierarchy2DInto(const Trade::SceneData& scene, Trade::SceneField field, const Containers::StridedArrayView1D<Matrix3>& transformations, const Matrix3& globalTransformation = {}); |
||||
#else |
||||
/* To avoid including Matrix3 */ |
||||
MAGNUM_SCENETOOLS_EXPORT void flattenTransformationHierarchy2DInto(const Trade::SceneData& scene, Trade::SceneField field, const Containers::StridedArrayView1D<Matrix3>& transformations, const Matrix3& globalTransformation); |
||||
MAGNUM_SCENETOOLS_EXPORT void flattenTransformationHierarchy2DInto(const Trade::SceneData& scene, Trade::SceneField field, const Containers::StridedArrayView1D<Matrix3>& transformations); |
||||
#endif |
||||
|
||||
/**
|
||||
@brief Flatten a 2D transformation hierarchy for given field ID into an existing array |
||||
@m_since_latest |
||||
|
||||
A variant of @ref flattenTransformationHierarchy2DInto(const Trade::SceneData&, Trade::SceneField, const Containers::StridedArrayView1D<Matrix3>&, const Matrix3&) |
||||
that takes a field ID instead of name. Useful for example in combination with |
||||
@ref Trade::SceneData::findFieldId() to avoid a double lookup if it isn't clear |
||||
if a field exists at all. The @p fieldId is expected to be smaller than |
||||
@ref Trade::SceneData::fieldCount(). |
||||
@experimental |
||||
*/ |
||||
#ifdef DOXYGEN_GENERATING_OUTPUT |
||||
MAGNUM_SCENETOOLS_EXPORT void flattenTransformationHierarchy2DInto(const Trade::SceneData& scene, UnsignedInt fieldId, const Containers::StridedArrayView1D<Matrix3>& transformations, const Matrix3& globalTransformation = {}); |
||||
#else |
||||
/* To avoid including Matrix3 */ |
||||
MAGNUM_SCENETOOLS_EXPORT void flattenTransformationHierarchy2DInto(const Trade::SceneData& scene, UnsignedInt fieldId, const Containers::StridedArrayView1D<Matrix3>& transformations, const Matrix3& globalTransformation); |
||||
MAGNUM_SCENETOOLS_EXPORT void flattenTransformationHierarchy2DInto(const Trade::SceneData& scene, UnsignedInt fieldId, const Containers::StridedArrayView1D<Matrix3>& transformations); |
||||
#endif |
||||
|
||||
/**
|
||||
@brief Flatten a 3D transformation hierarchy for given field |
||||
@m_since_latest |
||||
|
||||
For all entries of given @p field in @p scene returns an absolute |
||||
transformation of the object they're attached to in the scene with |
||||
@p globalTransformation prepended. The @ref Trade::SceneField::Parent field is |
||||
expected to be contained in the scene, having no cycles or duplicates, and the |
||||
scene is expected to be 3D. The @p field is expected to be present, if it's |
||||
empty, the function returns an empty array. |
||||
|
||||
The operation is done in an @f$ \mathcal{O}(m + n) @f$ execution time and |
||||
memory complexity, with @f$ m @f$ being size of @p field and @f$ n @f$ being |
||||
@ref Trade::SceneData::mappingBound(). The function calls |
||||
@ref orderClusterParents() internally. |
||||
|
||||
The returned data are in the same order as object mapping entries in @p field. |
||||
Fields attached to objects without a @ref Trade::SceneField::Parent or to |
||||
objects in loose hierarchy subtrees will have their transformation set to an |
||||
unspecified value. |
||||
|
||||
This function can be used for example to flatten a mesh hierarchy, bake |
||||
the transformations to actual meshes and then concatenate them together into a |
||||
single mesh: |
||||
|
||||
@snippet MagnumSceneTools.cpp flattenTransformationHierarchy3D-mesh-concatenate |
||||
|
||||
@experimental |
||||
|
||||
@see @ref flattenTransformationHierarchy3D(const Trade::SceneData&, UnsignedInt, const Matrix4&), |
||||
@ref flattenTransformationHierarchy3DInto(), |
||||
@ref flattenTransformationHierarchy2D(), @ref Trade::SceneData::hasField(), |
||||
@ref Trade::SceneData::is3D() |
||||
*/ |
||||
#ifdef DOXYGEN_GENERATING_OUTPUT |
||||
MAGNUM_SCENETOOLS_EXPORT Containers::Array<Matrix4> flattenTransformationHierarchy3D(const Trade::SceneData& scene, Trade::SceneField field, const Matrix4& globalTransformation = {}); |
||||
#else |
||||
/* To avoid including Matrix4 */ |
||||
MAGNUM_SCENETOOLS_EXPORT Containers::Array<Matrix4> flattenTransformationHierarchy3D(const Trade::SceneData& scene, Trade::SceneField field, const Matrix4& globalTransformation); |
||||
MAGNUM_SCENETOOLS_EXPORT Containers::Array<Matrix4> flattenTransformationHierarchy3D(const Trade::SceneData& scene, Trade::SceneField field); |
||||
#endif |
||||
|
||||
/**
|
||||
@brief Flatten a 3D transformation hierarchy for given field ID |
||||
@m_since_latest |
||||
|
||||
A variant of @ref flattenTransformationHierarchy3D(const Trade::SceneData&, Trade::SceneField, const Matrix4&) |
||||
that takes a field ID instead of name. Useful for example in combination with |
||||
@ref Trade::SceneData::findFieldId() to avoid a double lookup if it isn't clear |
||||
if a field exists at all. The @p fieldId is expected to be smaller than |
||||
@ref Trade::SceneData::fieldCount(). |
||||
@experimental |
||||
*/ |
||||
#ifdef DOXYGEN_GENERATING_OUTPUT |
||||
MAGNUM_SCENETOOLS_EXPORT Containers::Array<Matrix4> flattenTransformationHierarchy3D(const Trade::SceneData& scene, UnsignedInt fieldId, const Matrix4& globalTransformation = {}); |
||||
#else |
||||
/* To avoid including Matrix34 */ |
||||
MAGNUM_SCENETOOLS_EXPORT Containers::Array<Matrix4> flattenTransformationHierarchy3D(const Trade::SceneData& scene, UnsignedInt fieldId, const Matrix4& globalTransformation); |
||||
MAGNUM_SCENETOOLS_EXPORT Containers::Array<Matrix4> flattenTransformationHierarchy3D(const Trade::SceneData& scene, UnsignedInt fieldId); |
||||
#endif |
||||
|
||||
/**
|
||||
@brief Flatten a 3D transformation hierarchy for given field into an existing array |
||||
@param[in] scene Input scene |
||||
@param[in] field Field to calculate the transformations for |
||||
@param[out] transformations Where to put the calculated transformations |
||||
@param[in] globalTransformation Global transformation to prepend |
||||
@m_since_latest |
||||
|
||||
A variant of @ref flattenTransformationHierarchy3D(const Trade::SceneData&, Trade::SceneField, const Matrix4&) |
||||
that fills existing memory instead of allocating a new array. The |
||||
@p transformations array is expected to have the same size as the @p field. |
||||
@experimental |
||||
*/ |
||||
#ifdef DOXYGEN_GENERATING_OUTPUT |
||||
MAGNUM_SCENETOOLS_EXPORT void flattenTransformationHierarchy3DInto(const Trade::SceneData& scene, Trade::SceneField field, const Containers::StridedArrayView1D<Matrix4>& transformations, const Matrix4& globalTransformation = {}); |
||||
#else |
||||
/* To avoid including Matrix4 */ |
||||
MAGNUM_SCENETOOLS_EXPORT void flattenTransformationHierarchy3DInto(const Trade::SceneData& scene, Trade::SceneField field, const Containers::StridedArrayView1D<Matrix4>& transformations, const Matrix4& globalTransformation); |
||||
MAGNUM_SCENETOOLS_EXPORT void flattenTransformationHierarchy3DInto(const Trade::SceneData& scene, Trade::SceneField field, const Containers::StridedArrayView1D<Matrix4>& transformations); |
||||
#endif |
||||
|
||||
/**
|
||||
@brief Flatten a 3D transformation hierarchy for given field into an existing array |
||||
@m_since_latest |
||||
|
||||
A variant of @ref flattenTransformationHierarchy3DInto(const Trade::SceneData&, Trade::SceneField, const Containers::StridedArrayView1D<Matrix4>&, const Matrix4&) |
||||
that takes a field ID instead of name. Useful for example in combination with |
||||
@ref Trade::SceneData::findFieldId() to avoid a double lookup if it isn't clear |
||||
if a field exists at all. The @p fieldId is expected to be smaller than |
||||
@ref Trade::SceneData::fieldCount(). |
||||
@experimental |
||||
*/ |
||||
#ifdef DOXYGEN_GENERATING_OUTPUT |
||||
MAGNUM_SCENETOOLS_EXPORT void flattenTransformationHierarchy3DInto(const Trade::SceneData& scene, UnsignedInt fieldId, const Containers::StridedArrayView1D<Matrix4>& transformations, const Matrix4& globalTransformation = {}); |
||||
#else |
||||
/* To avoid including Matrix4 */ |
||||
MAGNUM_SCENETOOLS_EXPORT void flattenTransformationHierarchy3DInto(const Trade::SceneData& scene, UnsignedInt fieldId, const Containers::StridedArrayView1D<Matrix4>& transformations, const Matrix4& globalTransformation); |
||||
MAGNUM_SCENETOOLS_EXPORT void flattenTransformationHierarchy3DInto(const Trade::SceneData& scene, UnsignedInt fieldId, const Containers::StridedArrayView1D<Matrix4>& transformations); |
||||
#endif |
||||
|
||||
}} |
||||
|
||||
#endif |
||||
@ -0,0 +1,531 @@
|
||||
/*
|
||||
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 <sstream> |
||||
#include <Corrade/Containers/Triple.h> |
||||
#include <Corrade/TestSuite/Tester.h> |
||||
#include <Corrade/TestSuite/Compare/Container.h> |
||||
#include <Corrade/Utility/DebugStl.h> |
||||
|
||||
#include "Magnum/Math/Matrix3.h" |
||||
#include "Magnum/Math/Matrix4.h" |
||||
#include "Magnum/SceneTools/FlattenTransformationHierarchy.h" |
||||
#include "Magnum/Trade/SceneData.h" |
||||
|
||||
namespace Magnum { namespace SceneTools { namespace Test { namespace { |
||||
|
||||
struct FlattenTransformationHierarchyTest: TestSuite::Tester { |
||||
explicit FlattenTransformationHierarchyTest(); |
||||
|
||||
void test2D(); |
||||
void test3D(); |
||||
|
||||
void fieldNotFound(); |
||||
void not2DNot3D(); |
||||
void noParentField(); |
||||
|
||||
void into2D(); |
||||
void into3D(); |
||||
void intoInvalidSize(); |
||||
}; |
||||
|
||||
using namespace Math::Literals; |
||||
|
||||
const struct { |
||||
const char* name; |
||||
Matrix3 globalTransformation2D; |
||||
Matrix4 globalTransformation3D; |
||||
bool fieldIdInsteadOfName; |
||||
std::size_t transformationsToExclude, meshesToExclude; |
||||
std::size_t expectedOutputSize; |
||||
} TestData[]{ |
||||
{"", {}, {}, false, |
||||
2, 0, |
||||
5}, |
||||
{"field ID", {}, {}, true, |
||||
2, 0, |
||||
5}, |
||||
{"global transformation", |
||||
Matrix3::scaling(Vector2{0.5f}), Matrix4::scaling(Vector3{0.5f}), false, |
||||
2, 0, |
||||
5}, |
||||
{"global transformation, field ID", |
||||
Matrix3::scaling(Vector2{0.5f}), Matrix4::scaling(Vector3{0.5f}), true, |
||||
2, 0, |
||||
5}, |
||||
{"transformations not part of the hierarchy", {}, {}, false, |
||||
0, 0, |
||||
5}, |
||||
{"empty field", {}, {}, false, |
||||
2, 5, |
||||
0}, |
||||
}; |
||||
|
||||
const struct { |
||||
const char* name; |
||||
Matrix3 globalTransformation2D; |
||||
Matrix4 globalTransformation3D; |
||||
bool fieldIdInsteadOfName; |
||||
std::size_t expectedOutputSize; |
||||
} IntoData[]{ |
||||
{"", {}, {}, false, |
||||
5}, |
||||
{"field ID", {}, {}, true, |
||||
5}, |
||||
{"global transformation", |
||||
Matrix3::scaling(Vector2{0.5f}), Matrix4::scaling(Vector3{0.5f}), false, |
||||
5}, |
||||
{"global transformation, field ID", |
||||
Matrix3::scaling(Vector2{0.5f}), Matrix4::scaling(Vector3{0.5f}), true, |
||||
5}, |
||||
}; |
||||
|
||||
FlattenTransformationHierarchyTest::FlattenTransformationHierarchyTest() { |
||||
addInstancedTests({&FlattenTransformationHierarchyTest::test2D, |
||||
&FlattenTransformationHierarchyTest::test3D}, |
||||
Containers::arraySize(TestData)); |
||||
|
||||
addTests({&FlattenTransformationHierarchyTest::fieldNotFound, |
||||
&FlattenTransformationHierarchyTest::not2DNot3D, |
||||
&FlattenTransformationHierarchyTest::noParentField}); |
||||
|
||||
addInstancedTests({&FlattenTransformationHierarchyTest::into2D, |
||||
&FlattenTransformationHierarchyTest::into3D}, |
||||
Containers::arraySize(IntoData)); |
||||
|
||||
addTests({&FlattenTransformationHierarchyTest::intoInvalidSize}); |
||||
} |
||||
|
||||
const struct Scene { |
||||
/* Using smaller types to verify we don't have unnecessarily hardcoded
|
||||
32-bit types */ |
||||
struct Parent { |
||||
UnsignedShort object; |
||||
Byte parent; |
||||
} parents[9]; |
||||
|
||||
struct Transformation { |
||||
UnsignedShort object; |
||||
Matrix3 transformation2D; |
||||
Matrix4 transformation3D; |
||||
} transforms[7]; |
||||
|
||||
struct Mesh { |
||||
UnsignedShort object; |
||||
UnsignedShort mesh; |
||||
} meshes[5]; |
||||
} Data[]{{ |
||||
/*
|
||||
Cases to test: |
||||
|
||||
- leaf paths with no attachments which don't contribute to the |
||||
output in any way |
||||
- nodes with transforms but no meshes |
||||
- nodes with meshes but no transforms |
||||
- nodes with multiple meshes |
||||
- nodes with neither transforms nor meshes |
||||
- object 4 has a mesh with identity transform (or, rather, no |
||||
transformation entry at all) |
||||
- objects 2 and 16 have the same mesh attached with the exact |
||||
same transform -- this is a nonsense (they would overlap) and |
||||
as such isn't deduplicated in any way |
||||
- objects 0, 32 and 17 have transformations/meshes, but not part |
||||
of the hierarchy; these are cut away from the views in the |
||||
first test case to keep it simple |
||||
|
||||
1T 4M |
||||
/ \ | 32M 0MM |
||||
5T 2TM 11 |
||||
/ \ \ | 32T 17T |
||||
3MM 7T 6 16TM |
||||
*/ |
||||
{{3, 5}, |
||||
{11, 4}, |
||||
{5, 1}, |
||||
{1, -1}, |
||||
{7, 5}, |
||||
{6, 2}, |
||||
{2, 1}, |
||||
{4, -1}, |
||||
{16, 11}}, |
||||
{{2, Matrix3::scaling({3.0f, 5.0f}), |
||||
Matrix4::scaling({3.0f, 5.0f, 2.0f})}, |
||||
{1, Matrix3::translation({1.0f, -1.5f}), |
||||
Matrix4::translation({1.0f, -1.5f, 0.5f})}, |
||||
/* Same absolute transform as node 2 */ |
||||
{16, Matrix3::translation({1.0f, -1.5f})* |
||||
Matrix3::scaling({3.0f, 5.0f}), |
||||
Matrix4::translation({1.0f, -1.5f, 0.5f})* |
||||
Matrix4::scaling({3.0f, 5.0f, 2.0f})}, |
||||
{7, Matrix3::scaling({2.0f, 1.0f}), |
||||
Matrix4::scaling({2.0f, 1.0f, 0.5f})}, |
||||
{5, Matrix3::rotation(35.0_degf), |
||||
Matrix4::rotationZ(35.0_degf)}, |
||||
/* These are not part of the hierarchy */ |
||||
{32, Matrix3::translation({1.0f, 0.5f}), |
||||
Matrix4::translation({1.0f, 0.5f, 2.0f})}, |
||||
{17, Matrix3::translation({2.0f, 1.0f}), |
||||
Matrix4::translation({2.0f, 1.0f, 4.0f})}, |
||||
}, |
||||
/* The mesh IDs aren't used for anything, just setting them to something
|
||||
random (and high) to avoid their misuses as some offsets / IDs */ |
||||
{{2, 113}, |
||||
{3, 266}, |
||||
{4, 525}, |
||||
{3, 422}, |
||||
{16, 113}} |
||||
}}; |
||||
|
||||
void FlattenTransformationHierarchyTest::test2D() { |
||||
auto&& data = TestData[testCaseInstanceId()]; |
||||
setTestCaseDescription(data.name); |
||||
|
||||
Trade::SceneData scene{Trade::SceneMappingType::UnsignedShort, 33, {}, Data, { |
||||
/* To verify it doesn't just pick the first field ever */ |
||||
Trade::SceneFieldData{Trade::SceneField::Camera, Trade::SceneMappingType::UnsignedShort, nullptr, Trade::SceneFieldType::UnsignedInt, nullptr}, |
||||
Trade::SceneFieldData{Trade::SceneField::Parent, |
||||
Containers::stridedArrayView(Data->parents) |
||||
.slice(&Scene::Parent::object), |
||||
Containers::stridedArrayView(Data->parents) |
||||
.slice(&Scene::Parent::parent)}, |
||||
Trade::SceneFieldData{Trade::SceneField::Mesh, |
||||
Containers::stridedArrayView(Data->meshes) |
||||
.slice(&Scene::Mesh::object) |
||||
.exceptSuffix(data.meshesToExclude), |
||||
Containers::stridedArrayView(Data->meshes) |
||||
.slice(&Scene::Mesh::mesh) |
||||
.exceptSuffix(data.meshesToExclude)}, |
||||
Trade::SceneFieldData{Trade::SceneField::Transformation, |
||||
Containers::stridedArrayView(Data->transforms) |
||||
.slice(&Scene::Transformation::object) |
||||
.exceptSuffix(data.transformationsToExclude), |
||||
Containers::stridedArrayView(Data->transforms) |
||||
.slice(&Scene::Transformation::transformation2D) |
||||
.exceptSuffix(data.transformationsToExclude)}, |
||||
}}; |
||||
|
||||
Containers::Array<Matrix3> out; |
||||
/* To test all overloads */ |
||||
if(data.globalTransformation2D != Matrix3{}) { |
||||
if(data.fieldIdInsteadOfName) |
||||
out = flattenTransformationHierarchy2D(scene, 2, data.globalTransformation2D); |
||||
else |
||||
out = flattenTransformationHierarchy2D(scene, Trade::SceneField::Mesh, data.globalTransformation2D); |
||||
} else { |
||||
if(data.fieldIdInsteadOfName) |
||||
out = flattenTransformationHierarchy2D(scene, 2); |
||||
else |
||||
out = flattenTransformationHierarchy2D(scene, Trade::SceneField::Mesh); |
||||
} |
||||
|
||||
CORRADE_COMPARE_AS(out, Containers::arrayView({ |
||||
data.globalTransformation2D* |
||||
Matrix3::translation({1.0f, -1.5f})* |
||||
Matrix3::scaling({3.0f, 5.0f}), |
||||
data.globalTransformation2D* |
||||
Matrix3::translation({1.0f, -1.5f})* |
||||
Matrix3::rotation(35.0_degf), |
||||
data.globalTransformation2D, |
||||
data.globalTransformation2D* |
||||
Matrix3::translation({1.0f, -1.5f})* |
||||
Matrix3::rotation(35.0_degf), |
||||
data.globalTransformation2D* |
||||
Matrix3::translation({1.0f, -1.5f})* |
||||
Matrix3::scaling({3.0f, 5.0f}) |
||||
}).prefix(data.expectedOutputSize), TestSuite::Compare::Container); |
||||
} |
||||
|
||||
void FlattenTransformationHierarchyTest::test3D() { |
||||
auto&& data = TestData[testCaseInstanceId()]; |
||||
setTestCaseDescription(data.name); |
||||
|
||||
Trade::SceneData scene{Trade::SceneMappingType::UnsignedShort, 33, {}, Data, { |
||||
/* To verify it doesn't just pick the first field ever */ |
||||
Trade::SceneFieldData{Trade::SceneField::Camera, Trade::SceneMappingType::UnsignedShort, nullptr, Trade::SceneFieldType::UnsignedInt, nullptr}, |
||||
Trade::SceneFieldData{Trade::SceneField::Parent, |
||||
Containers::stridedArrayView(Data->parents) |
||||
.slice(&Scene::Parent::object), |
||||
Containers::stridedArrayView(Data->parents) |
||||
.slice(&Scene::Parent::parent)}, |
||||
Trade::SceneFieldData{Trade::SceneField::Mesh, |
||||
Containers::stridedArrayView(Data->meshes) |
||||
.slice(&Scene::Mesh::object) |
||||
.exceptSuffix(data.meshesToExclude), |
||||
Containers::stridedArrayView(Data->meshes) |
||||
.slice(&Scene::Mesh::mesh) |
||||
.exceptSuffix(data.meshesToExclude)}, |
||||
Trade::SceneFieldData{Trade::SceneField::Transformation, |
||||
Containers::stridedArrayView(Data->transforms) |
||||
.slice(&Scene::Transformation::object) |
||||
.exceptSuffix(data.transformationsToExclude), |
||||
Containers::stridedArrayView(Data->transforms) |
||||
.slice(&Scene::Transformation::transformation3D) |
||||
.exceptSuffix(data.transformationsToExclude)}, |
||||
}}; |
||||
|
||||
Containers::Array<Matrix4> out; |
||||
/* To test all overloads */ |
||||
if(data.globalTransformation3D != Matrix4{}) { |
||||
if(data.fieldIdInsteadOfName) |
||||
out = flattenTransformationHierarchy3D(scene, 2, data.globalTransformation3D); |
||||
else |
||||
out = flattenTransformationHierarchy3D(scene, Trade::SceneField::Mesh, data.globalTransformation3D); |
||||
} else { |
||||
if(data.fieldIdInsteadOfName) |
||||
out = flattenTransformationHierarchy3D(scene, 2); |
||||
else |
||||
out = flattenTransformationHierarchy3D(scene, Trade::SceneField::Mesh); |
||||
} |
||||
|
||||
CORRADE_COMPARE_AS(out, Containers::arrayView({ |
||||
data.globalTransformation3D* |
||||
Matrix4::translation({1.0f, -1.5f, 0.5f})* |
||||
Matrix4::scaling({3.0f, 5.0f, 2.0f}), |
||||
data.globalTransformation3D* |
||||
Matrix4::translation({1.0f, -1.5f, 0.5f})* |
||||
Matrix4::rotationZ(35.0_degf), |
||||
data.globalTransformation3D, |
||||
data.globalTransformation3D* |
||||
Matrix4::translation({1.0f, -1.5f, 0.5f})* |
||||
Matrix4::rotationZ(35.0_degf), |
||||
data.globalTransformation3D* |
||||
Matrix4::translation({1.0f, -1.5f, 0.5f})* |
||||
Matrix4::scaling({3.0f, 5.0f, 2.0f}) |
||||
}).prefix(data.expectedOutputSize), TestSuite::Compare::Container); |
||||
} |
||||
|
||||
void FlattenTransformationHierarchyTest::fieldNotFound() { |
||||
CORRADE_SKIP_IF_NO_ASSERT(); |
||||
|
||||
Trade::SceneData scene{Trade::SceneMappingType::UnsignedInt, 0, nullptr, { |
||||
Trade::SceneFieldData{Trade::SceneField::Parent, Trade::SceneMappingType::UnsignedInt, nullptr, Trade::SceneFieldType::Int, nullptr}, |
||||
Trade::SceneFieldData{Trade::SceneField::Transformation, Trade::SceneMappingType::UnsignedInt, nullptr, Trade::SceneFieldType::Matrix3x3, nullptr} |
||||
}}; |
||||
|
||||
std::ostringstream out; |
||||
Error redirectError{&out}; |
||||
flattenTransformationHierarchy2D(scene, Trade::SceneField::Mesh); |
||||
flattenTransformationHierarchy3D(scene, Trade::SceneField::Mesh); |
||||
flattenTransformationHierarchy2D(scene, 2); |
||||
flattenTransformationHierarchy3D(scene, 2); |
||||
CORRADE_COMPARE(out.str(), |
||||
"SceneTools::flattenTransformationHierarchy(): field Trade::SceneField::Mesh not found\n" |
||||
"SceneTools::flattenTransformationHierarchy(): field Trade::SceneField::Mesh not found\n" |
||||
"SceneTools::flattenTransformationHierarchy(): index 2 out of range for 2 fields\n" |
||||
"SceneTools::flattenTransformationHierarchy(): index 2 out of range for 2 fields\n"); |
||||
} |
||||
|
||||
void FlattenTransformationHierarchyTest::not2DNot3D() { |
||||
CORRADE_SKIP_IF_NO_ASSERT(); |
||||
|
||||
Trade::SceneData scene{Trade::SceneMappingType::UnsignedInt, 0, nullptr, { |
||||
Trade::SceneFieldData{Trade::SceneField::Parent, Trade::SceneMappingType::UnsignedInt, nullptr, Trade::SceneFieldType::Int, nullptr} |
||||
}}; |
||||
|
||||
std::ostringstream out; |
||||
Error redirectError{&out}; |
||||
flattenTransformationHierarchy2D(scene, Trade::SceneField::Parent); |
||||
flattenTransformationHierarchy2D(scene, 0); |
||||
flattenTransformationHierarchy3D(scene, Trade::SceneField::Parent); |
||||
flattenTransformationHierarchy3D(scene, 0); |
||||
CORRADE_COMPARE(out.str(), |
||||
"SceneTools::flattenTransformationHierarchy(): the scene is not 2D\n" |
||||
"SceneTools::flattenTransformationHierarchy(): the scene is not 2D\n" |
||||
"SceneTools::flattenTransformationHierarchy(): the scene is not 3D\n" |
||||
"SceneTools::flattenTransformationHierarchy(): the scene is not 3D\n"); |
||||
} |
||||
|
||||
void FlattenTransformationHierarchyTest::noParentField() { |
||||
CORRADE_SKIP_IF_NO_ASSERT(); |
||||
|
||||
Trade::SceneData scene{Trade::SceneMappingType::UnsignedInt, 0, nullptr, { |
||||
Trade::SceneFieldData{Trade::SceneField::Transformation, Trade::SceneMappingType::UnsignedInt, nullptr, Trade::SceneFieldType::Matrix3x3, nullptr} |
||||
}}; |
||||
|
||||
std::ostringstream out; |
||||
Error redirectError{&out}; |
||||
flattenTransformationHierarchy2D(scene, Trade::SceneField::Transformation); |
||||
flattenTransformationHierarchy2D(scene, 0); |
||||
CORRADE_COMPARE(out.str(), |
||||
"SceneTools::flattenTransformationHierarchy(): the scene has no hierarchy\n" |
||||
"SceneTools::flattenTransformationHierarchy(): the scene has no hierarchy\n"); |
||||
} |
||||
|
||||
void FlattenTransformationHierarchyTest::into2D() { |
||||
auto&& data = IntoData[testCaseInstanceId()]; |
||||
setTestCaseDescription(data.name); |
||||
|
||||
/* The *Into() variant is the actual base implementation, so just verify
|
||||
that the data get correctly propagated through. Everything else is |
||||
tested above already. */ |
||||
|
||||
Trade::SceneData scene{Trade::SceneMappingType::UnsignedShort, 33, {}, Data, { |
||||
Trade::SceneFieldData{Trade::SceneField::Parent, |
||||
Containers::stridedArrayView(Data->parents) |
||||
.slice(&Scene::Parent::object), |
||||
Containers::stridedArrayView(Data->parents) |
||||
.slice(&Scene::Parent::parent)}, |
||||
Trade::SceneFieldData{Trade::SceneField::Transformation, |
||||
Containers::stridedArrayView(Data->transforms) |
||||
.slice(&Scene::Transformation::object), |
||||
Containers::stridedArrayView(Data->transforms) |
||||
.slice(&Scene::Transformation::transformation2D)}, |
||||
Trade::SceneFieldData{Trade::SceneField::Mesh, |
||||
Containers::stridedArrayView(Data->meshes) |
||||
.slice(&Scene::Mesh::object), |
||||
Containers::stridedArrayView(Data->meshes) |
||||
.slice(&Scene::Mesh::mesh)} |
||||
}}; |
||||
|
||||
Containers::Array<Matrix3> out{NoInit, scene.fieldSize(Trade::SceneField::Mesh)}; |
||||
/* To test all overloads */ |
||||
if(data.globalTransformation2D != Matrix3{}) { |
||||
if(data.fieldIdInsteadOfName) |
||||
flattenTransformationHierarchy2DInto(scene, 2, out, data.globalTransformation2D); |
||||
else |
||||
flattenTransformationHierarchy2DInto(scene, Trade::SceneField::Mesh, out, data.globalTransformation2D); |
||||
} else { |
||||
if(data.fieldIdInsteadOfName) |
||||
flattenTransformationHierarchy2DInto(scene, 2, out); |
||||
else |
||||
flattenTransformationHierarchy2DInto(scene, Trade::SceneField::Mesh, out); |
||||
} |
||||
|
||||
CORRADE_COMPARE_AS(out, Containers::arrayView<Matrix3>({ |
||||
data.globalTransformation2D* |
||||
Matrix3::translation({1.0f, -1.5f})* |
||||
Matrix3::scaling({3.0f, 5.0f}), |
||||
data.globalTransformation2D* |
||||
Matrix3::translation({1.0f, -1.5f})* |
||||
Matrix3::rotation(35.0_degf), |
||||
data.globalTransformation2D, |
||||
data.globalTransformation2D* |
||||
Matrix3::translation({1.0f, -1.5f})* |
||||
Matrix3::rotation(35.0_degf), |
||||
data.globalTransformation2D* |
||||
Matrix3::translation({1.0f, -1.5f})* |
||||
Matrix3::scaling({3.0f, 5.0f}) |
||||
}), TestSuite::Compare::Container); |
||||
} |
||||
|
||||
void FlattenTransformationHierarchyTest::into3D() { |
||||
auto&& data = IntoData[testCaseInstanceId()]; |
||||
setTestCaseDescription(data.name); |
||||
|
||||
/* The *Into() variant is the actual base implementation, so just verify
|
||||
that the data get correctly propagated through. Everything else is |
||||
tested above already. */ |
||||
|
||||
Trade::SceneData scene{Trade::SceneMappingType::UnsignedShort, 33, {}, Data, { |
||||
Trade::SceneFieldData{Trade::SceneField::Parent, |
||||
Containers::stridedArrayView(Data->parents) |
||||
.slice(&Scene::Parent::object), |
||||
Containers::stridedArrayView(Data->parents) |
||||
.slice(&Scene::Parent::parent)}, |
||||
Trade::SceneFieldData{Trade::SceneField::Transformation, |
||||
Containers::stridedArrayView(Data->transforms) |
||||
.slice(&Scene::Transformation::object), |
||||
Containers::stridedArrayView(Data->transforms) |
||||
.slice(&Scene::Transformation::transformation3D)}, |
||||
Trade::SceneFieldData{Trade::SceneField::Mesh, |
||||
Containers::stridedArrayView(Data->meshes) |
||||
.slice(&Scene::Mesh::object), |
||||
Containers::stridedArrayView(Data->meshes) |
||||
.slice(&Scene::Mesh::mesh)} |
||||
}}; |
||||
|
||||
Containers::Array<Matrix4> out{NoInit, scene.fieldSize(Trade::SceneField::Mesh)}; |
||||
/* To test all overloads */ |
||||
if(data.globalTransformation3D != Matrix4{}) { |
||||
if(data.fieldIdInsteadOfName) |
||||
flattenTransformationHierarchy3DInto(scene, 2, out, data.globalTransformation3D); |
||||
else |
||||
flattenTransformationHierarchy3DInto(scene, Trade::SceneField::Mesh, out, data.globalTransformation3D); |
||||
} else { |
||||
if(data.fieldIdInsteadOfName) |
||||
flattenTransformationHierarchy3DInto(scene, 2, out); |
||||
else |
||||
flattenTransformationHierarchy3DInto(scene, Trade::SceneField::Mesh, out); |
||||
} |
||||
|
||||
CORRADE_COMPARE_AS(out, Containers::arrayView<Matrix4>({ |
||||
data.globalTransformation3D* |
||||
Matrix4::translation({1.0f, -1.5f, 0.5f})* |
||||
Matrix4::scaling({3.0f, 5.0f, 2.0f}), |
||||
data.globalTransformation3D* |
||||
Matrix4::translation({1.0f, -1.5f, 0.5f})* |
||||
Matrix4::rotationZ(35.0_degf), |
||||
data.globalTransformation3D, |
||||
data.globalTransformation3D* |
||||
Matrix4::translation({1.0f, -1.5f, 0.5f})* |
||||
Matrix4::rotationZ(35.0_degf), |
||||
data.globalTransformation3D* |
||||
Matrix4::translation({1.0f, -1.5f, 0.5f})* |
||||
Matrix4::scaling({3.0f, 5.0f, 2.0f}) |
||||
}), TestSuite::Compare::Container); |
||||
} |
||||
|
||||
void FlattenTransformationHierarchyTest::intoInvalidSize() { |
||||
CORRADE_SKIP_IF_NO_ASSERT(); |
||||
|
||||
struct Data { |
||||
UnsignedInt mapping; |
||||
UnsignedInt mesh; |
||||
} data[5]{}; |
||||
|
||||
Trade::SceneData scene2D{Trade::SceneMappingType::UnsignedInt, 1, {}, data, { |
||||
Trade::SceneFieldData{Trade::SceneField::Parent, Trade::SceneMappingType::UnsignedInt, nullptr, Trade::SceneFieldType::Int, nullptr}, |
||||
Trade::SceneFieldData{Trade::SceneField::Mesh, |
||||
Containers::stridedArrayView(data).slice(&Data::mapping), |
||||
Containers::stridedArrayView(data).slice(&Data::mesh)}, |
||||
Trade::SceneFieldData{Trade::SceneField::Transformation, Trade::SceneMappingType::UnsignedInt, nullptr, Trade::SceneFieldType::Matrix3x3, nullptr} |
||||
}}; |
||||
Trade::SceneData scene3D{Trade::SceneMappingType::UnsignedInt, 1, {}, data, { |
||||
Trade::SceneFieldData{Trade::SceneField::Parent, Trade::SceneMappingType::UnsignedInt, nullptr, Trade::SceneFieldType::Int, nullptr}, |
||||
Trade::SceneFieldData{Trade::SceneField::Mesh, |
||||
Containers::stridedArrayView(data).slice(&Data::mapping), |
||||
Containers::stridedArrayView(data).slice(&Data::mesh)}, |
||||
Trade::SceneFieldData{Trade::SceneField::Transformation, Trade::SceneMappingType::UnsignedInt, nullptr, Trade::SceneFieldType::Matrix4x4, nullptr} |
||||
}}; |
||||
|
||||
Matrix3 transformations2D[6]; |
||||
Matrix4 transformations3D[4]; |
||||
|
||||
std::ostringstream out; |
||||
Error redirectError{&out}; |
||||
flattenTransformationHierarchy2DInto(scene2D, Trade::SceneField::Mesh, transformations2D); |
||||
flattenTransformationHierarchy2DInto(scene2D, 1, transformations2D); |
||||
flattenTransformationHierarchy3DInto(scene3D, Trade::SceneField::Mesh, transformations3D); |
||||
flattenTransformationHierarchy3DInto(scene3D, 1, transformations3D); |
||||
CORRADE_COMPARE(out.str(), |
||||
"SceneTools::flattenTransformationHierarchyInto(): bad output size, expected 5 but got 6\n" |
||||
"SceneTools::flattenTransformationHierarchyInto(): bad output size, expected 5 but got 6\n" |
||||
"SceneTools::flattenTransformationHierarchyInto(): bad output size, expected 5 but got 4\n" |
||||
"SceneTools::flattenTransformationHierarchyInto(): bad output size, expected 5 but got 4\n"); |
||||
} |
||||
|
||||
}}}} |
||||
|
||||
CORRADE_TEST_MAIN(Magnum::SceneTools::Test::FlattenTransformationHierarchyTest) |
||||
Loading…
Reference in new issue