3#include "CesiumGltf/Accessor.h"
4#include "CesiumGltf/Model.h"
89 const std::byte* _pData;
112 : _pData(nullptr), _stride(0), _offset(0), _size(0), _status(
status) {}
126 const std::byte* pData,
149 this->create(model, accessor);
172 this->create(model, *pAccessor);
184 if (i < 0 || i >= this->_size) {
185 throw std::range_error(
"index out of range");
188 return *
reinterpret_cast<const T*
>(
189 this->_pData + i * this->_stride + this->_offset);
199 int64_t
size() const noexcept {
return this->_size; }
215 int64_t
stride() const noexcept {
return this->_stride; }
223 int64_t
offset() const noexcept {
return this->_offset; }
231 const std::byte*
data() const noexcept {
232 return this->_pData + this->_offset;
236 void create(
const Model& model,
const Accessor& accessor)
noexcept {
252 const int64_t bufferBytes = int64_t(
data.size());
258 const int64_t accessorByteStride = accessor.computeByteStride(model);
259 if (accessorByteStride < 0) {
264 const int64_t accessorComponentElements =
265 accessor.computeNumberOfComponents();
266 const int64_t accessorComponentBytes =
267 accessor.computeByteSizeOfComponent();
268 const int64_t accessorBytesPerStride =
269 accessorComponentElements * accessorComponentBytes;
271 if (
sizeof(T) != accessorBytesPerStride) {
276 const int64_t accessorBytes = accessorByteStride * accessor.count;
277 const int64_t bytesRemainingInBufferView =
279 (accessor.byteOffset + accessorByteStride * (accessor.count - 1) +
280 accessorBytesPerStride);
281 if (accessorBytes > pBufferView->
byteLength ||
282 bytesRemainingInBufferView < 0) {
288 this->_stride = accessorByteStride;
289 this->_offset = accessor.byteOffset + pBufferView->
byteOffset;
290 this->_size = accessor.count;
319 template <
typename T>
struct VEC2 {
331 template <
typename T>
struct VEC3 {
343 template <
typename T>
struct VEC4 {
355 template <
typename T>
struct MAT2 {
367 template <
typename T>
struct MAT3 {
379 template <
typename T>
struct MAT4 {
389namespace CesiumImpl {
390template <
typename TCallback,
typename TElement>
391std::invoke_result_t<TCallback, AccessorView<AccessorTypes::SCALAR<TElement>>>
395 TCallback&& callback) {
396 if (accessor.
type == Accessor::Type::SCALAR) {
400 if (accessor.
type == Accessor::Type::VEC2) {
402 AccessorView<AccessorTypes::VEC2<TElement>>(model, accessor));
404 if (accessor.
type == Accessor::Type::VEC3) {
406 AccessorView<AccessorTypes::VEC3<TElement>>(model, accessor));
408 if (accessor.
type == Accessor::Type::VEC4) {
410 AccessorView<AccessorTypes::VEC4<TElement>>(model, accessor));
412 if (accessor.
type == Accessor::Type::MAT2) {
414 AccessorView<AccessorTypes::MAT2<TElement>>(model, accessor));
416 if (accessor.
type == Accessor::Type::MAT3) {
418 AccessorView<AccessorTypes::MAT3<TElement>>(model, accessor));
420 if (accessor.
type == Accessor::Type::MAT4) {
422 AccessorView<AccessorTypes::MAT4<TElement>>(model, accessor));
425 return callback(AccessorView<AccessorTypes::SCALAR<TElement>>(
445template <
typename TCallback>
446std::invoke_result_t<TCallback, AccessorView<AccessorTypes::SCALAR<float>>>
450 TCallback&& callback) {
452 case Accessor::ComponentType::BYTE:
453 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, int8_t>(
456 std::forward<TCallback>(callback));
457 case Accessor::ComponentType::UNSIGNED_BYTE:
458 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, uint8_t>(
461 std::forward<TCallback>(callback));
462 case Accessor::ComponentType::SHORT:
463 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, int16_t>(
466 std::forward<TCallback>(callback));
467 case Accessor::ComponentType::UNSIGNED_SHORT:
468 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, uint16_t>(
471 std::forward<TCallback>(callback));
472 case Accessor::ComponentType::INT:
473 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, int32_t>(
476 std::forward<TCallback>(callback));
477 case Accessor::ComponentType::UNSIGNED_INT:
478 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, uint32_t>(
481 std::forward<TCallback>(callback));
482 case Accessor::ComponentType::INT64:
483 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, int64_t>(
486 std::forward<TCallback>(callback));
487 case Accessor::ComponentType::UNSIGNED_INT64:
488 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, uint64_t>(
491 std::forward<TCallback>(callback));
492 case Accessor::ComponentType::FLOAT:
493 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, float>(
496 std::forward<TCallback>(callback));
497 case Accessor::ComponentType::DOUBLE:
498 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, double>(
501 std::forward<TCallback>(callback));
524template <
typename TCallback>
525std::invoke_result_t<TCallback, AccessorView<AccessorTypes::SCALAR<float>>>
528 int32_t accessorIndex,
529 TCallback&& callback) {
A view on the data of one accessor of a glTF asset.
int64_t offset() const noexcept
Returns the offset of this accessor, which is the number of bytes from the start of the buffer to the...
int64_t stride() const noexcept
Returns the stride of this accessor, which is the number of bytes from the start of one element to th...
T value_type
The type of the elements in the accessor.
int64_t size() const noexcept
Returns the size (number of elements) of this accessor.
AccessorView(AccessorViewStatus status=AccessorViewStatus::InvalidAccessorIndex)
Construct a new instance not pointing to any data.
const std::byte * data() const noexcept
Returns a pointer to the first byte of this accessor view's data. The elements are stored contiguousl...
AccessorView(const Model &model, int32_t accessorIndex) noexcept
Creates a new instance from a given model and accessor index.
AccessorView(const std::byte *pData, int64_t stride, int64_t offset, int64_t size)
Creates a new instance from low-level parameters.
const T & operator[](int64_t i) const
Provides the specified accessor element.
AccessorView(const Model &model, const Accessor &accessor) noexcept
Creates a new instance from a given model and Accessor.
AccessorViewStatus status() const noexcept
Gets the status of this accessor view.
Classes for working with glTF models.
AccessorViewStatus
Indicates the status of an accessor view.
@ BufferViewTooSmall
The accessor is too large to fit in its bufferView.
@ InvalidByteStride
The BufferView::byteStride is negative, which is invalid.
@ Valid
This accessor is valid and ready to use.
@ InvalidBufferViewIndex
The accessor's bufferView index does not refer to a valid bufferView.
@ WrongSizeT
The sizeof(T) does not match the accessor's Accessor::computeBytesPerVertex.
@ InvalidBufferIndex
The accessor's bufferView's buffer index does not refer to a valid buffer.
@ InvalidAccessorIndex
The accessor index does not refer to a valid accessor.
@ BufferTooSmall
The accessor's bufferView is too large to fit in its buffer.
@ InvalidType
The AccessorSpec:type is invalid.
@ InvalidComponentType
The AccessorSpec::componentType is invalid.
std::invoke_result_t< TCallback, AccessorView< AccessorTypes::SCALAR< float > > > createAccessorView(const Model &model, const Accessor &accessor, TCallback &&callback)
Creates an appropriate AccessorView for a given accessor.
std::string type
Specifies if the accessor's elements are scalars, vectors, or matrices.
int32_t componentType
The datatype of the accessor's components.
A 2x2 matrix element for an AccessorView.
T value[4]
The component values of this element.
A 3x3 matrix element for an AccessorView.
T value[9]
The component values of this element.
A 4x4 matrix element for an AccessorView.
T value[16]
The component values of this element.
A scalar element for an AccessorView.
T value[1]
The component values of this element.
A 2D vector element for an AccessorView.
T value[2]
The component values of this element.
A 3D vector element for an AccessorView.
T value[3]
The component values of this element.
A 4D vector element for an AccessorView.
T value[4]
The component values of this element.
Contains types that may optionally be used with AccessorView for various Accessor::componentType valu...
This class is not meant to be instantiated directly. Use Accessor instead.
std::vector< std::byte > data
The buffer's data.
A view into a buffer generally representing a subset of the buffer.
int64_t byteLength
The length of the bufferView in bytes.
int32_t buffer
The index of the buffer.
int64_t byteOffset
The offset into the buffer in bytes.
This class is not meant to be instantiated directly. Use Buffer instead.
BufferCesium cesium
Holds properties that are specific to the glTF loader rather than part of the glTF spec.
std::vector< CesiumGltf::Accessor > accessors
An array of accessors.
This class is not meant to be instantiated directly. Use Model instead.
static const T & getSafe(const std::vector< T > &items, int32_t index)
Safely gets the element with a given index, returning a default instance if the index is outside the ...