Browse Source

Trade: expose getters in MeshIndexData.

It made testing and everything harder than strictly necessary. OTOH
still keeping MeshData as a friend and accessing members directly since
those are heavily interconnected anyway.
pull/371/head
Vladimír Vondruš 6 years ago
parent
commit
aecec186be
  1. 4
      src/Magnum/Trade/MeshData.cpp
  2. 23
      src/Magnum/Trade/MeshData.h
  3. 72
      src/Magnum/Trade/Test/MeshDataTest.cpp

4
src/Magnum/Trade/MeshData.cpp

@ -64,11 +64,11 @@ Containers::Array<MeshAttributeData> meshAttributeDataNonOwningArray(const Conta
return Containers::Array<Trade::MeshAttributeData>{const_cast<Trade::MeshAttributeData*>(view.data()), view.size(), reinterpret_cast<void(*)(Trade::MeshAttributeData*, std::size_t)>(Trade::Implementation::nonOwnedArrayDeleter)};
}
MeshData::MeshData(const MeshPrimitive primitive, Containers::Array<char>&& indexData, const MeshIndexData& indices, Containers::Array<char>&& vertexData, Containers::Array<MeshAttributeData>&& attributes, const void* const importerState) noexcept: _indexType{indices.type}, _primitive{primitive}, _indexDataFlags{DataFlag::Owned|DataFlag::Mutable}, _vertexDataFlags{DataFlag::Owned|DataFlag::Mutable}, _importerState{importerState}, _indexData{std::move(indexData)}, _vertexData{std::move(vertexData)}, _attributes{std::move(attributes)}, _indices{Containers::arrayCast<const char>(indices.data)} {
MeshData::MeshData(const MeshPrimitive primitive, Containers::Array<char>&& indexData, const MeshIndexData& indices, Containers::Array<char>&& vertexData, Containers::Array<MeshAttributeData>&& attributes, const void* const importerState) noexcept: _indexType{indices._type}, _primitive{primitive}, _indexDataFlags{DataFlag::Owned|DataFlag::Mutable}, _vertexDataFlags{DataFlag::Owned|DataFlag::Mutable}, _importerState{importerState}, _indexData{std::move(indexData)}, _vertexData{std::move(vertexData)}, _attributes{std::move(attributes)}, _indices{Containers::arrayCast<const char>(indices._data)} {
/* Save vertex count. It's a strided array view, so the size is not
depending on type. */
if(_attributes.empty()) {
CORRADE_ASSERT(indices.type != MeshIndexType{},
CORRADE_ASSERT(indices._type != MeshIndexType{},
"Trade::MeshData: indices are expected to be valid if there are no attributes and vertex count isn't passed explicitly", );
/** @todo some better value? attributeless indexed with defined vertex count? */
_vertexCount = 0;

23
src/Magnum/Trade/MeshData.h

@ -152,18 +152,18 @@ constexpr UnsignedShort meshAttributeCustom(MeshAttribute name) {
@brief Mesh index data
@m_since_latest
Convenience type for populating @ref MeshData. Has no accessors, as the data
are then accessible through @ref MeshData APIs.
Convenience type for populating @ref MeshData, see its documentation for an
introduction.
@see @ref MeshAttributeData
*/
class MAGNUM_TRADE_EXPORT MeshIndexData {
public:
/** @brief Construct for a non-indexed mesh */
explicit MeshIndexData() noexcept: type{} {}
explicit MeshIndexData() noexcept: _type{} {}
/**
* @brief Construct with a runtime-specified index type
* @param type Mesh index type
* @param type Index type
* @param data Index data
*
* The @p data size is expected to correspond to given @p type (e.g.,
@ -185,6 +185,12 @@ class MAGNUM_TRADE_EXPORT MeshIndexData {
/** @brief Construct with unsigned int indices */
constexpr explicit MeshIndexData(Containers::ArrayView<const UnsignedInt> data) noexcept: MeshIndexData{MeshIndexType::UnsignedInt, data, nullptr} {}
/** @brief Index type */
constexpr MeshIndexType type() const { return _type; }
/** @brief Type-erased index data */
constexpr Containers::ArrayView<const void> data() const { return _data; }
private:
/* Contains an assert common for all constexpr constructor, nullptr_t
to disambiguate from the public constructor of the same signature --
@ -193,11 +199,10 @@ class MAGNUM_TRADE_EXPORT MeshIndexData {
template, we don't need that check anyway */
constexpr explicit MeshIndexData(MeshIndexType type, Containers::ArrayView<const void> data, std::nullptr_t);
/* Not prefixed with _ because we use them like public in MeshData */
friend MeshData;
MeshIndexType type;
MeshIndexType _type;
/* Void so the constructors can be constexpr */
Containers::ArrayView<const void> data;
Containers::ArrayView<const void> _data;
};
/**
@ -1083,8 +1088,8 @@ namespace Implementation {
}
#endif
constexpr MeshIndexData::MeshIndexData(MeshIndexType type, Containers::ArrayView<const void> data, std::nullptr_t):
type{type}, data{(CORRADE_CONSTEXPR_ASSERT(!data.empty(), "Trade::MeshIndexData: index array can't be empty, create a non-indexed mesh instead"), data)} {}
constexpr MeshIndexData::MeshIndexData(const MeshIndexType type, const Containers::ArrayView<const void> data, std::nullptr_t):
_type{type}, _data{(CORRADE_CONSTEXPR_ASSERT(!data.empty(), "Trade::MeshIndexData: index array can't be empty, create a non-indexed mesh instead"), data)} {}
constexpr MeshAttributeData::MeshAttributeData(const MeshAttribute name, const VertexFormat format, const Containers::StridedArrayView1D<const void>& data, std::nullptr_t) noexcept:
name{name}, format{format}, data{(CORRADE_CONSTEXPR_ASSERT(

72
src/Magnum/Trade/Test/MeshDataTest.cpp

@ -264,50 +264,38 @@ constexpr UnsignedInt IndexInts[]{2110122, 132257, 3};
void MeshDataTest::constructIndex() {
{
Containers::Array<char> indexData{3*1};
auto indexView = Containers::arrayCast<UnsignedByte>(indexData);
MeshIndexData indices{indexView};
MeshData data{MeshPrimitive::Points, std::move(indexData), indices};
CORRADE_COMPARE(data.indexType(), MeshIndexType::UnsignedByte);
CORRADE_COMPARE(static_cast<const void*>(data.indices<UnsignedByte>().data()), indexView.data());
CORRADE_COMPARE(data.indexCount(), 3);
const UnsignedByte indexData[]{25, 132, 3};
MeshIndexData indices{indexData};
CORRADE_COMPARE(indices.type(), MeshIndexType::UnsignedByte);
CORRADE_COMPARE(indices.data().data(), indexData);
constexpr MeshIndexData cindices{IndexBytes};
MeshData cdata{MeshPrimitive::Points, {}, IndexBytes, cindices};
CORRADE_COMPARE(cdata.indexType(), MeshIndexType::UnsignedByte);
CORRADE_COMPARE(static_cast<const void*>(cdata.indices<UnsignedByte>().data()), IndexBytes);
CORRADE_COMPARE(data.indexCount(), 3);
constexpr MeshIndexType type = cindices.type();
constexpr Containers::ArrayView<const void> data = cindices.data();
CORRADE_COMPARE(type, MeshIndexType::UnsignedByte);
CORRADE_COMPARE(data.data(), IndexBytes);
} {
Containers::Array<char> indexData{3*2};
auto indexView = Containers::arrayCast<UnsignedShort>(indexData);
MeshIndexData indices{indexView};
MeshData data{MeshPrimitive::Points, std::move(indexData), indices};
CORRADE_COMPARE(data.indexType(), MeshIndexType::UnsignedShort);
CORRADE_COMPARE(static_cast<const void*>(data.indices<UnsignedShort>().data()), indexView.data());
CORRADE_COMPARE(data.indexCount(), 3);
const UnsignedShort indexData[]{2575, 13224, 3};
MeshIndexData indices{indexData};
CORRADE_COMPARE(indices.type(), MeshIndexType::UnsignedShort);
CORRADE_COMPARE(indices.data().data(), indexData);
constexpr MeshIndexData cindices{IndexShorts};
MeshData cdata{MeshPrimitive::Points, {}, IndexShorts, cindices};
CORRADE_COMPARE(cdata.indexType(), MeshIndexType::UnsignedShort);
CORRADE_COMPARE(static_cast<const void*>(cdata.indices<UnsignedShort>().data()), IndexShorts);
CORRADE_COMPARE(data.indexCount(), 3);
constexpr MeshIndexType type = cindices.type();
constexpr Containers::ArrayView<const void> data = cindices.data();
CORRADE_COMPARE(type, MeshIndexType::UnsignedShort);
CORRADE_COMPARE(data.data(), IndexShorts);
} {
Containers::Array<char> indexData{3*4};
auto indexView = Containers::arrayCast<UnsignedInt>(indexData);
MeshIndexData indices{indexView};
MeshData data{MeshPrimitive::Points, std::move(indexData), indices};
CORRADE_COMPARE(data.indexType(), MeshIndexType::UnsignedInt);
CORRADE_COMPARE(static_cast<const void*>(data.indices<UnsignedInt>().data()), indexView.data());
CORRADE_COMPARE(data.indexCount(), 3);
const UnsignedInt indexData[]{2110122, 132257, 3};
MeshIndexData indices{indexData};
CORRADE_COMPARE(indices.type(), MeshIndexType::UnsignedInt);
CORRADE_COMPARE(indices.data().data(), indexData);
constexpr MeshIndexData cindices{IndexInts};
MeshData cdata{MeshPrimitive::Points, {}, IndexInts, cindices};
CORRADE_COMPARE(cdata.indexType(), MeshIndexType::UnsignedInt);
CORRADE_COMPARE(static_cast<const void*>(cdata.indices<UnsignedInt>().data()), IndexInts);
CORRADE_COMPARE(data.indexCount(), 3);
constexpr MeshIndexType type = cindices.type();
constexpr Containers::ArrayView<const void> data = cindices.data();
CORRADE_COMPARE(type, MeshIndexType::UnsignedInt);
CORRADE_COMPARE(data.data(), IndexInts);
}
}
@ -319,18 +307,14 @@ void MeshDataTest::constructIndexZeroCount() {
}
void MeshDataTest::constructIndexTypeErased() {
Containers::Array<char> indexData{3*2};
auto indexView = Containers::arrayCast<UnsignedShort>(indexData);
const char indexData[3*2]{};
MeshIndexData indices{MeshIndexType::UnsignedShort, indexData};
MeshData data{MeshPrimitive::Points, std::move(indexData), indices};
CORRADE_COMPARE(data.indexType(), MeshIndexType::UnsignedShort);
CORRADE_COMPARE(static_cast<const void*>(data.indices<UnsignedShort>().data()), indexView.data());
CORRADE_COMPARE(data.indexCount(), 3);
CORRADE_COMPARE(indices.type(), MeshIndexType::UnsignedShort);
CORRADE_VERIFY(indices.data().data() == indexData);
}
void MeshDataTest::constructIndexTypeErasedWrongSize() {
Containers::Array<char> indexData{3*2};
const char indexData[3*2]{};
std::ostringstream out;
Error redirectError{&out};

Loading…
Cancel
Save