Browse Source

MeshTools: added a type-erased compressIndices().

Useful when consuming the indices straight from MeshData.
pull/371/head
Vladimír Vondruš 6 years ago
parent
commit
1974da2635
  1. 6
      doc/changelog.dox
  2. 12
      src/Magnum/MeshTools/CompressIndices.cpp
  3. 11
      src/Magnum/MeshTools/CompressIndices.h
  4. 49
      src/Magnum/MeshTools/Test/CompressIndicesTest.cpp

6
doc/changelog.dox

@ -211,9 +211,9 @@ See also:
@subsubsection changelog-latest-changes-meshtools MeshTools library
- Added @ref MeshTools::compressIndices() that takes a
@ref Containers::StridedArrayView instead of a @ref std::vector and
additionally allows you to specify the smallest allowed type and is working
with 8- and 16-byte index types as well
@ref Corrade::Containers::StridedArrayView instead of a @ref std::vector
and additionally allows you to specify the smallest allowed type and is
working with 8- and 16-byte index types as well
- Added @ref MeshTools::subdivide() that operates on a (growable)
@ref Corrade::Containers::Array instead of a @ref std::vector
- Added @ref MeshTools::subdivideInPlace() that operates on a partially

12
src/Magnum/MeshTools/CompressIndices.cpp

@ -87,6 +87,18 @@ std::pair<Containers::Array<char>, MeshIndexType> compressIndices(const Containe
return compressIndicesImplementation(indices, atLeast);
}
std::pair<Containers::Array<char>, MeshIndexType> compressIndices(const Containers::StridedArrayView2D<const char>& indices, const MeshIndexType atLeast) {
CORRADE_ASSERT(indices.isContiguous<1>(), "MeshTools::compressIndices(): second view dimension is not contiguous", {});
if(indices.size()[1] == 4)
return compressIndicesImplementation(Containers::arrayCast<1, const UnsignedInt>(indices), atLeast);
else if(indices.size()[1] == 2)
return compressIndicesImplementation(Containers::arrayCast<1, const UnsignedShort>(indices), atLeast);
else {
CORRADE_ASSERT(indices.size()[1] == 1, "MeshTools::compressIndices(): expected index type size 1, 2 or 4 but got" << indices.size()[1], {});
return compressIndicesImplementation(Containers::arrayCast<1, const UnsignedByte>(indices), atLeast);
}
}
#ifdef MAGNUM_BUILD_DEPRECATED
std::tuple<Containers::Array<char>, MeshIndexType, UnsignedInt, UnsignedInt> compressIndices(const std::vector<UnsignedInt>& indices) {
/** @todo Performance hint when range can be represented by smaller value? */

11
src/Magnum/MeshTools/CompressIndices.h

@ -77,6 +77,17 @@ MAGNUM_MESHTOOLS_EXPORT std::pair<Containers::Array<char>, MeshIndexType> compre
*/
MAGNUM_MESHTOOLS_EXPORT std::pair<Containers::Array<char>, MeshIndexType> compressIndices(const Containers::StridedArrayView1D<const UnsignedByte>& indices, MeshIndexType atLeast = MeshIndexType::UnsignedShort);
/**
@brief Compress a type-erased index array
@m_since_latest
Expects that the second dimension of @p indices is contiguous and represents
the actual 1/2/4-byte index type. Based on its size then calls one of the
@ref compressIndices(const Containers::StridedArrayView1D<const UnsignedInt>&, MeshIndexType)
etc. overloads.
*/
MAGNUM_MESHTOOLS_EXPORT std::pair<Containers::Array<char>, MeshIndexType> compressIndices(const Containers::StridedArrayView2D<const char>& indices, MeshIndexType atLeast = MeshIndexType::UnsignedShort);
#ifdef MAGNUM_BUILD_DEPRECATED
/**
@brief Compress vertex indices

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

@ -44,6 +44,9 @@ struct CompressIndicesTest: TestSuite::Tester {
template<class T> void compressUnsignedShort();
template<class T> void compressUnsignedInt();
void compressUnsignedByteInflateToShort();
/* No compressErased(), as that's tested in the templates above */
void compressErasedNonContiguous();
void compressErasedWrongIndexSize();
#ifdef MAGNUM_BUILD_DEPRECATED
void compressDeprecated();
#endif
@ -59,6 +62,8 @@ CompressIndicesTest::CompressIndicesTest() {
&CompressIndicesTest::compressUnsignedShort<UnsignedInt>,
&CompressIndicesTest::compressUnsignedInt<UnsignedInt>,
&CompressIndicesTest::compressUnsignedByteInflateToShort,
&CompressIndicesTest::compressErasedNonContiguous,
&CompressIndicesTest::compressErasedWrongIndexSize,
#ifdef MAGNUM_BUILD_DEPRECATED
&CompressIndicesTest::compressDeprecated,
@ -79,6 +84,14 @@ template<class T> void CompressIndicesTest::compressUnsignedByte() {
CORRADE_COMPARE_AS(Containers::arrayCast<UnsignedByte>(out.first),
Containers::arrayView<UnsignedByte>({1, 2, 3, 0, 4}),
TestSuite::Compare::Container);
/* Test the type-erased variant as well */
out = compressIndices(Containers::arrayCast<2, const char>(Containers::stridedArrayView(indices)), MeshIndexType::UnsignedByte);
CORRADE_COMPARE(out.second, MeshIndexType::UnsignedByte);
CORRADE_COMPARE_AS(Containers::arrayCast<UnsignedByte>(out.first),
Containers::arrayView<UnsignedByte>({1, 2, 3, 0, 4}),
TestSuite::Compare::Container);
}
template<class T> void CompressIndicesTest::compressUnsignedShort() {
@ -87,6 +100,14 @@ template<class T> void CompressIndicesTest::compressUnsignedShort() {
const T indices[]{1, 256, 0, 5};
std::pair<Containers::Array<char>, MeshIndexType> out = compressIndices(indices);
CORRADE_COMPARE(out.second, MeshIndexType::UnsignedShort);
CORRADE_COMPARE_AS(Containers::arrayCast<UnsignedShort>(out.first),
Containers::arrayView<UnsignedShort>({1, 256, 0, 5}),
TestSuite::Compare::Container);
/* Test the type-erased variant as well */
out = compressIndices(Containers::arrayCast<2, const char>(Containers::stridedArrayView(indices)));
CORRADE_COMPARE(out.second, MeshIndexType::UnsignedShort);
CORRADE_COMPARE_AS(Containers::arrayCast<UnsignedShort>(out.first),
Containers::arrayView<UnsignedShort>({1, 256, 0, 5}),
@ -99,6 +120,14 @@ template<class T> void CompressIndicesTest::compressUnsignedInt() {
const T indices[]{65536, 3, 2};
std::pair<Containers::Array<char>, MeshIndexType> out = compressIndices(indices);
CORRADE_COMPARE(out.second, MeshIndexType::UnsignedInt);
CORRADE_COMPARE_AS(Containers::arrayCast<UnsignedInt>(out.first),
Containers::arrayView<UnsignedInt>({65536, 3, 2}),
TestSuite::Compare::Container);
/* Test the type-erased variant as well */
out = compressIndices(Containers::arrayCast<2, const char>(Containers::stridedArrayView(indices)));
CORRADE_COMPARE(out.second, MeshIndexType::UnsignedInt);
CORRADE_COMPARE_AS(Containers::arrayCast<UnsignedInt>(out.first),
Containers::arrayView<UnsignedInt>({65536, 3, 2}),
@ -116,6 +145,26 @@ void CompressIndicesTest::compressUnsignedByteInflateToShort() {
TestSuite::Compare::Container);
}
void CompressIndicesTest::compressErasedNonContiguous() {
const char indices[6*4]{};
std::stringstream out;
Error redirectError{&out};
compressIndices(Containers::StridedArrayView2D<const char>{indices, {6, 2}, {4, 2}});
CORRADE_COMPARE(out.str(),
"MeshTools::compressIndices(): second view dimension is not contiguous\n");
}
void CompressIndicesTest::compressErasedWrongIndexSize() {
const char indices[6*3]{};
std::stringstream out;
Error redirectError{&out};
compressIndices(Containers::StridedArrayView2D<const char>{indices, {6, 3}}.every(2));
CORRADE_COMPARE(out.str(),
"MeshTools::compressIndices(): expected index type size 1, 2 or 4 but got 3\n");
}
#ifdef MAGNUM_BUILD_DEPRECATED
void CompressIndicesTest::compressDeprecated() {
Containers::Array<char> data;

Loading…
Cancel
Save