Browse Source

MeshTools: added compressIndicesAs().

Much more convenient and "good enough" alternative to compressIndices().
pull/107/head
Vladimír Vondruš 11 years ago
parent
commit
4389a58cce
  1. 2
      src/Magnum/MeshTools/CMakeLists.txt
  2. 17
      src/Magnum/MeshTools/CompressIndices.cpp
  3. 19
      src/Magnum/MeshTools/CompressIndices.h
  4. 2
      src/Magnum/MeshTools/Test/CMakeLists.txt
  5. 19
      src/Magnum/MeshTools/Test/CompressIndicesTest.cpp

2
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)

17
src/Magnum/MeshTools/CompressIndices.cpp

@ -74,4 +74,21 @@ std::tuple<Containers::Array<char>, Mesh::IndexType, UnsignedInt, UnsignedInt> c
return std::make_tuple(std::move(data), type, *minmax.first, *minmax.second);
}
template<class T> Containers::Array<T> compressIndicesAs(const std::vector<UnsignedInt>& 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<T> buffer(indices.size());
for(std::size_t i = 0; i != indices.size(); ++i)
buffer[i] = indices[i];
return buffer;
}
template Containers::Array<UnsignedByte> compressIndicesAs(const std::vector<UnsignedInt>& indices);
template Containers::Array<UnsignedShort> compressIndicesAs(const std::vector<UnsignedInt>& indices);
template Containers::Array<UnsignedInt> compressIndicesAs(const std::vector<UnsignedInt>& indices);
}}

19
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<Containers::Array<char>, Mesh::IndexType, UnsignedInt, UnsignedInt> MAGNUM_MESHTOOLS_EXPORT compressIndices(const std::vector<UnsignedInt>& 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<UnsignedInt> indices;
Containers::Array<UnsignedShort> indexData = MeshTools::compressIndicesAs<UnsignedShort>(indices);
@endcode
@see @ref compressIndices()
*/
template<class T> MAGNUM_MESHTOOLS_EXPORT Containers::Array<T> compressIndicesAs(const std::vector<UnsignedInt>& indices);
}}
#endif

2
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)

19
src/Magnum/MeshTools/Test/CompressIndicesTest.cpp

@ -23,8 +23,10 @@
DEALINGS IN THE SOFTWARE.
*/
#include <sstream>
#include <Corrade/Containers/Array.h>
#include <Corrade/TestSuite/Tester.h>
#include <Corrade/TestSuite/Compare/Container.h>
#include <Corrade/Utility/Endianness.h>
#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<UnsignedShort>({123, 456}),
Containers::Array<UnsignedShort>::from(123, 456),
TestSuite::Compare::Container);
std::ostringstream out;
Error::setOutput(&out);
MeshTools::compressIndicesAs<UnsignedShort>({65536});
CORRADE_COMPARE(out.str(), "MeshTools::compressIndicesAs(): type too small to represent value 65536\n");
}
}}}
CORRADE_TEST_MAIN(Magnum::MeshTools::Test::CompressIndicesTest)

Loading…
Cancel
Save