From 4389a58cce3a68fd6df3d04da56e6b730ff05d92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 23 Jun 2015 21:57:14 +0200 Subject: [PATCH] MeshTools: added compressIndicesAs(). Much more convenient and "good enough" alternative to compressIndices(). --- src/Magnum/MeshTools/CMakeLists.txt | 2 +- src/Magnum/MeshTools/CompressIndices.cpp | 17 +++++++++++++++++ src/Magnum/MeshTools/CompressIndices.h | 19 +++++++++++++++++++ src/Magnum/MeshTools/Test/CMakeLists.txt | 2 +- .../MeshTools/Test/CompressIndicesTest.cpp | 19 ++++++++++++++++++- 5 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/Magnum/MeshTools/CMakeLists.txt b/src/Magnum/MeshTools/CMakeLists.txt index b2ce35e5f..1b80869c0 100644 --- a/src/Magnum/MeshTools/CMakeLists.txt +++ b/src/Magnum/MeshTools/CMakeLists.txt @@ -26,13 +26,13 @@ # Files shared between main library and unit test library set(MagnumMeshTools_SRCS Compile.cpp - CompressIndices.cpp FullScreenTriangle.cpp Tipsify.cpp) # Files compiled with different flags for main library and unit test library set(MagnumMeshTools_GracefulAssert_SRCS CombineIndexedArrays.cpp + CompressIndices.cpp FlipNormals.cpp GenerateFlatNormals.cpp) diff --git a/src/Magnum/MeshTools/CompressIndices.cpp b/src/Magnum/MeshTools/CompressIndices.cpp index 1635830de..5d70679e7 100644 --- a/src/Magnum/MeshTools/CompressIndices.cpp +++ b/src/Magnum/MeshTools/CompressIndices.cpp @@ -74,4 +74,21 @@ std::tuple, Mesh::IndexType, UnsignedInt, UnsignedInt> c return std::make_tuple(std::move(data), type, *minmax.first, *minmax.second); } +template Containers::Array compressIndicesAs(const std::vector& indices) { + #if !defined(CORRADE_NO_ASSERT) || defined(CORRADE_GRACEFUL_ASSERT) + const auto max = std::max_element(indices.begin(), indices.end()); + CORRADE_ASSERT(Math::log(256, *max) < sizeof(T), "MeshTools::compressIndicesAs(): type too small to represent value" << *max, {}); + #endif + + Containers::Array buffer(indices.size()); + for(std::size_t i = 0; i != indices.size(); ++i) + buffer[i] = indices[i]; + + return buffer; +} + +template Containers::Array compressIndicesAs(const std::vector& indices); +template Containers::Array compressIndicesAs(const std::vector& indices); +template Containers::Array compressIndicesAs(const std::vector& indices); + }} diff --git a/src/Magnum/MeshTools/CompressIndices.h b/src/Magnum/MeshTools/CompressIndices.h index c066c88bb..29a94e177 100644 --- a/src/Magnum/MeshTools/CompressIndices.h +++ b/src/Magnum/MeshTools/CompressIndices.h @@ -62,10 +62,29 @@ Mesh mesh; mesh.setCount(indices.size()) .setIndexBuffer(indexBuffer, 0, indexType, indexStart, indexEnd); @endcode + +@see @ref compressIndicesAs() @todo Extract IndexType out of Mesh class */ std::tuple, Mesh::IndexType, UnsignedInt, UnsignedInt> MAGNUM_MESHTOOLS_EXPORT compressIndices(const std::vector& indices); +/** +@brief Compress vertex indices as given type + +The type can be either @ref Magnum::UnsignedByte "UnsignedByte", +@ref Magnum::UnsignedShort "UnsignedShort" or @ref Magnum::UnsignedInt "UnsignedInt". +Values in the index array are expected to be representable with given type. + +Example usage: +@code +std::vector indices; +Containers::Array indexData = MeshTools::compressIndicesAs(indices); +@endcode + +@see @ref compressIndices() +*/ +template MAGNUM_MESHTOOLS_EXPORT Containers::Array compressIndicesAs(const std::vector& indices); + }} #endif diff --git a/src/Magnum/MeshTools/Test/CMakeLists.txt b/src/Magnum/MeshTools/Test/CMakeLists.txt index 5d6e653a9..c356e075d 100644 --- a/src/Magnum/MeshTools/Test/CMakeLists.txt +++ b/src/Magnum/MeshTools/Test/CMakeLists.txt @@ -24,7 +24,7 @@ # corrade_add_test(MeshToolsCombineIndexedArraysTest CombineIndexedArraysTest.cpp LIBRARIES MagnumMeshToolsTestLib) -corrade_add_test(MeshToolsCompressIndicesTest CompressIndicesTest.cpp LIBRARIES MagnumMeshTools) +corrade_add_test(MeshToolsCompressIndicesTest CompressIndicesTest.cpp LIBRARIES MagnumMeshToolsTestLib) corrade_add_test(MeshToolsDuplicateTest DuplicateTest.cpp) corrade_add_test(MeshToolsFlipNormalsTest FlipNormalsTest.cpp LIBRARIES MagnumMeshToolsTestLib) corrade_add_test(MeshToolsGenerateFlatNormalsTest GenerateFlatNormalsTest.cpp LIBRARIES MagnumMeshToolsTestLib) diff --git a/src/Magnum/MeshTools/Test/CompressIndicesTest.cpp b/src/Magnum/MeshTools/Test/CompressIndicesTest.cpp index db392fe4a..3a6125287 100644 --- a/src/Magnum/MeshTools/Test/CompressIndicesTest.cpp +++ b/src/Magnum/MeshTools/Test/CompressIndicesTest.cpp @@ -23,8 +23,10 @@ DEALINGS IN THE SOFTWARE. */ +#include #include #include +#include #include #include "Magnum/MeshTools/CompressIndices.h" @@ -37,12 +39,16 @@ struct CompressIndicesTest: TestSuite::Tester { void compressChar(); void compressShort(); void compressInt(); + + void compressAsShort(); }; CompressIndicesTest::CompressIndicesTest() { addTests({&CompressIndicesTest::compressChar, &CompressIndicesTest::compressShort, - &CompressIndicesTest::compressInt}); + &CompressIndicesTest::compressInt, + + &CompressIndicesTest::compressAsShort}); } void CompressIndicesTest::compressChar() { @@ -108,6 +114,17 @@ void CompressIndicesTest::compressInt() { } } +void CompressIndicesTest::compressAsShort() { + CORRADE_COMPARE_AS(MeshTools::compressIndicesAs({123, 456}), + Containers::Array::from(123, 456), + TestSuite::Compare::Container); + + std::ostringstream out; + Error::setOutput(&out); + MeshTools::compressIndicesAs({65536}); + CORRADE_COMPARE(out.str(), "MeshTools::compressIndicesAs(): type too small to represent value 65536\n"); +} + }}} CORRADE_TEST_MAIN(Magnum::MeshTools::Test::CompressIndicesTest)