diff --git a/src/Magnum/Trade/MeshData.cpp b/src/Magnum/Trade/MeshData.cpp index f5296c472..319a1faa6 100644 --- a/src/Magnum/Trade/MeshData.cpp +++ b/src/Magnum/Trade/MeshData.cpp @@ -64,11 +64,11 @@ Containers::Array meshAttributeDataNonOwningArray(const Conta return Containers::Array{const_cast(view.data()), view.size(), reinterpret_cast(Trade::Implementation::nonOwnedArrayDeleter)}; } -MeshData::MeshData(const MeshPrimitive primitive, Containers::Array&& indexData, const MeshIndexData& indices, Containers::Array&& vertexData, Containers::Array&& 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(indices.data)} { +MeshData::MeshData(const MeshPrimitive primitive, Containers::Array&& indexData, const MeshIndexData& indices, Containers::Array&& vertexData, Containers::Array&& 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(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; diff --git a/src/Magnum/Trade/MeshData.h b/src/Magnum/Trade/MeshData.h index fbb5be122..ba83cd7eb 100644 --- a/src/Magnum/Trade/MeshData.h +++ b/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 data) noexcept: MeshIndexData{MeshIndexType::UnsignedInt, data, nullptr} {} + /** @brief Index type */ + constexpr MeshIndexType type() const { return _type; } + + /** @brief Type-erased index data */ + constexpr Containers::ArrayView 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 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 data; + Containers::ArrayView _data; }; /** @@ -1083,8 +1088,8 @@ namespace Implementation { } #endif -constexpr MeshIndexData::MeshIndexData(MeshIndexType type, Containers::ArrayView 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 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& data, std::nullptr_t) noexcept: name{name}, format{format}, data{(CORRADE_CONSTEXPR_ASSERT( diff --git a/src/Magnum/Trade/Test/MeshDataTest.cpp b/src/Magnum/Trade/Test/MeshDataTest.cpp index 37f2900d7..5d20e46f5 100644 --- a/src/Magnum/Trade/Test/MeshDataTest.cpp +++ b/src/Magnum/Trade/Test/MeshDataTest.cpp @@ -264,50 +264,38 @@ constexpr UnsignedInt IndexInts[]{2110122, 132257, 3}; void MeshDataTest::constructIndex() { { - Containers::Array indexData{3*1}; - auto indexView = Containers::arrayCast(indexData); - - MeshIndexData indices{indexView}; - MeshData data{MeshPrimitive::Points, std::move(indexData), indices}; - CORRADE_COMPARE(data.indexType(), MeshIndexType::UnsignedByte); - CORRADE_COMPARE(static_cast(data.indices().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(cdata.indices().data()), IndexBytes); - CORRADE_COMPARE(data.indexCount(), 3); + constexpr MeshIndexType type = cindices.type(); + constexpr Containers::ArrayView data = cindices.data(); + CORRADE_COMPARE(type, MeshIndexType::UnsignedByte); + CORRADE_COMPARE(data.data(), IndexBytes); } { - Containers::Array indexData{3*2}; - auto indexView = Containers::arrayCast(indexData); - - MeshIndexData indices{indexView}; - MeshData data{MeshPrimitive::Points, std::move(indexData), indices}; - CORRADE_COMPARE(data.indexType(), MeshIndexType::UnsignedShort); - CORRADE_COMPARE(static_cast(data.indices().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(cdata.indices().data()), IndexShorts); - CORRADE_COMPARE(data.indexCount(), 3); + constexpr MeshIndexType type = cindices.type(); + constexpr Containers::ArrayView data = cindices.data(); + CORRADE_COMPARE(type, MeshIndexType::UnsignedShort); + CORRADE_COMPARE(data.data(), IndexShorts); } { - Containers::Array indexData{3*4}; - auto indexView = Containers::arrayCast(indexData); - - MeshIndexData indices{indexView}; - MeshData data{MeshPrimitive::Points, std::move(indexData), indices}; - CORRADE_COMPARE(data.indexType(), MeshIndexType::UnsignedInt); - CORRADE_COMPARE(static_cast(data.indices().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(cdata.indices().data()), IndexInts); - CORRADE_COMPARE(data.indexCount(), 3); + constexpr MeshIndexType type = cindices.type(); + constexpr Containers::ArrayView 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 indexData{3*2}; - auto indexView = Containers::arrayCast(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(data.indices().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 indexData{3*2}; + const char indexData[3*2]{}; std::ostringstream out; Error redirectError{&out};