From baea66127b2c51bc2bd6bdbd5b1b1981c434fc95 Mon Sep 17 00:00:00 2001 From: Sean Lilley Date: Mon, 9 Dec 2024 18:13:28 -0500 Subject: [PATCH] Add getters for AccessorView and AccessorWriter --- CHANGES.md | 6 +++++ CesiumGltf/include/CesiumGltf/AccessorView.h | 8 ++++++ .../include/CesiumGltf/AccessorWriter.h | 16 ++++++++++++ CesiumGltf/test/TestAccessorView.cpp | 26 ++++++++++++------- 4 files changed, 46 insertions(+), 10 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 26d28e96c..64def8584 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,12 @@ ### ? - ? +##### Additions :tada: + +- Added `offset` getter to `AccessorView`. +- Added `stride`, `offset`, and `data` getters to `AccessorWriter`. +- Added `value_type` typedef to `AccessorWriter`. + ##### Fixes :wrench: - Fixed a bug in `thenPassThrough` that caused a compiler error when given a value by r-value refrence. diff --git a/CesiumGltf/include/CesiumGltf/AccessorView.h b/CesiumGltf/include/CesiumGltf/AccessorView.h index bcd051c5b..367bbeb5b 100644 --- a/CesiumGltf/include/CesiumGltf/AccessorView.h +++ b/CesiumGltf/include/CesiumGltf/AccessorView.h @@ -214,6 +214,14 @@ template class AccessorView final { */ int64_t stride() const noexcept { return this->_stride; } + /** + * @brief Returns the offset of this accessor, which is the number of bytes + * from the start of the buffer to the first element. + * + * @returns The offset. + */ + int64_t offset() const noexcept { return this->_offset; } + /** * @brief Returns a pointer to the first byte of this accessor view's data. * The elements are stored contiguously, so the next one starts {@link stride} bytes later. diff --git a/CesiumGltf/include/CesiumGltf/AccessorWriter.h b/CesiumGltf/include/CesiumGltf/AccessorWriter.h index e38c50591..6c55b43e6 100644 --- a/CesiumGltf/include/CesiumGltf/AccessorWriter.h +++ b/CesiumGltf/include/CesiumGltf/AccessorWriter.h @@ -12,6 +12,11 @@ template class AccessorWriter final { AccessorView _accessor; public: + /** + * @brief The type of the elements in the accessor. + */ + typedef T value_type; + AccessorWriter() : _accessor() {} /** @@ -51,6 +56,17 @@ template class AccessorWriter final { AccessorViewStatus status() const noexcept { return this->_accessor.status(); } + + /** @copydoc AccessorView::stride */ + int64_t stride() const noexcept { return this->_accessor.stride(); } + + /** @copydoc AccessorView::offset */ + int64_t offset() const noexcept { return this->_accessor.offset(); } + + /** @copydoc AccessorView::data */ + std::byte* data() noexcept { + return const_cast(this->_accessor.data()); + } }; } // namespace CesiumGltf diff --git a/CesiumGltf/test/TestAccessorView.cpp b/CesiumGltf/test/TestAccessorView.cpp index 6e17db9cf..1f756fa85 100644 --- a/CesiumGltf/test/TestAccessorView.cpp +++ b/CesiumGltf/test/TestAccessorView.cpp @@ -5,21 +5,23 @@ #include #include +using namespace CesiumGltf; + TEST_CASE("AccessorView construct and read example") { auto anyOldFunctionToGetAModel = []() { - CesiumGltf::Model model; + Model model; - CesiumGltf::Accessor& accessor = model.accessors.emplace_back(); + Accessor& accessor = model.accessors.emplace_back(); accessor.bufferView = 0; - accessor.componentType = CesiumGltf::Accessor::ComponentType::FLOAT; - accessor.type = CesiumGltf::Accessor::Type::VEC3; + accessor.componentType = Accessor::ComponentType::FLOAT; + accessor.type = Accessor::Type::VEC3; accessor.count = 1; - CesiumGltf::BufferView& bufferView = model.bufferViews.emplace_back(); + BufferView& bufferView = model.bufferViews.emplace_back(); bufferView.buffer = 0; bufferView.byteLength = accessor.count * int64_t(sizeof(float)) * 3; - CesiumGltf::Buffer& buffer = model.buffers.emplace_back(); + Buffer& buffer = model.buffers.emplace_back(); buffer.byteLength = bufferView.byteLength; buffer.cesium.data.resize(size_t(buffer.byteLength)); @@ -32,17 +34,21 @@ TEST_CASE("AccessorView construct and read example") { }; //! [createFromAccessorAndRead] - CesiumGltf::Model model = anyOldFunctionToGetAModel(); - CesiumGltf::AccessorView positions(model, 0); + Model model = anyOldFunctionToGetAModel(); + AccessorView positions(model, 0); glm::vec3 firstPosition = positions[0]; //! [createFromAccessorAndRead] CHECK(firstPosition == glm::vec3(1.0f, 2.0f, 3.0f)); + + CHECK(positions.size() == 1); + CHECK(positions.status() == AccessorViewStatus::Valid); + CHECK(positions.stride() == 12); + CHECK(positions.offset() == 0); + CHECK(positions.data() == model.buffers[0].cesium.data.data()); } TEST_CASE("Create AccessorView of unknown type with lambda") { - using namespace CesiumGltf; - Model model; Buffer& buffer = model.buffers.emplace_back();