3#include <CesiumGltf/Accessor.h>
4#include <CesiumGltf/Model.h>
89 const std::byte* _pData;
133 const std::byte* pData,
158 this->create(model, accessor);
181 this->create(model, *pAccessor);
193 if (i < 0 || i >= this->_size) {
194 throw std::range_error(
"index out of range");
197 return *
reinterpret_cast<const T*
>(
198 this->_pData + i * this->_stride + this->_offset);
208 int64_t
size() const noexcept {
return this->_size; }
224 int64_t
stride() const noexcept {
return this->_stride; }
232 int64_t
offset() const noexcept {
return this->_offset; }
239 bool normalized() const noexcept {
return this->_normalized; }
248 const std::byte*
data() const noexcept {
249 return this->_pData + this->_offset;
253 void create(
const Model& model,
const Accessor& accessor)
noexcept {
269 const int64_t bufferBytes = int64_t(
data.size());
275 const int64_t accessorByteStride = accessor.computeByteStride(model);
276 if (accessorByteStride < 0) {
281 const int64_t accessorComponentElements =
282 accessor.computeNumberOfComponents();
283 const int64_t accessorComponentBytes =
284 accessor.computeByteSizeOfComponent();
285 const int64_t accessorBytesPerStride =
286 accessorComponentElements * accessorComponentBytes;
288 if (
sizeof(T) != accessorBytesPerStride) {
293 const int64_t accessorBytes = accessorByteStride * accessor.count;
294 const int64_t bytesRemainingInBufferView =
296 (accessor.byteOffset + accessorByteStride * (accessor.count - 1) +
297 accessorBytesPerStride);
298 if (accessorBytes > pBufferView->
byteLength ||
299 bytesRemainingInBufferView < 0) {
305 this->_stride = accessorByteStride;
306 this->_offset = accessor.byteOffset + pBufferView->
byteOffset;
307 this->_size = accessor.count;
308 this->_normalized = accessor.normalized;
337 template <
typename T>
struct VEC2 {
349 template <
typename T>
struct VEC3 {
361 template <
typename T>
struct VEC4 {
373 template <
typename T>
struct MAT2 {
385 template <
typename T>
struct MAT3 {
397 template <
typename T>
struct MAT4 {
407namespace CesiumImpl {
408template <
typename TCallback,
typename TElement>
409std::invoke_result_t<TCallback, AccessorView<AccessorTypes::SCALAR<TElement>>>
413 TCallback&& callback) {
414 if (accessor.
type == Accessor::Type::SCALAR) {
418 if (accessor.
type == Accessor::Type::VEC2) {
420 AccessorView<AccessorTypes::VEC2<TElement>>(model, accessor));
422 if (accessor.
type == Accessor::Type::VEC3) {
424 AccessorView<AccessorTypes::VEC3<TElement>>(model, accessor));
426 if (accessor.
type == Accessor::Type::VEC4) {
428 AccessorView<AccessorTypes::VEC4<TElement>>(model, accessor));
430 if (accessor.
type == Accessor::Type::MAT2) {
432 AccessorView<AccessorTypes::MAT2<TElement>>(model, accessor));
434 if (accessor.
type == Accessor::Type::MAT3) {
436 AccessorView<AccessorTypes::MAT3<TElement>>(model, accessor));
438 if (accessor.
type == Accessor::Type::MAT4) {
440 AccessorView<AccessorTypes::MAT4<TElement>>(model, accessor));
443 return callback(AccessorView<AccessorTypes::SCALAR<TElement>>(
463template <
typename TCallback>
464std::invoke_result_t<TCallback, AccessorView<AccessorTypes::SCALAR<float>>>
468 TCallback&& callback) {
470 case Accessor::ComponentType::BYTE:
471 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, int8_t>(
474 std::forward<TCallback>(callback));
475 case Accessor::ComponentType::UNSIGNED_BYTE:
476 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, uint8_t>(
479 std::forward<TCallback>(callback));
480 case Accessor::ComponentType::SHORT:
481 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, int16_t>(
484 std::forward<TCallback>(callback));
485 case Accessor::ComponentType::UNSIGNED_SHORT:
486 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, uint16_t>(
489 std::forward<TCallback>(callback));
490 case Accessor::ComponentType::INT:
491 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, int32_t>(
494 std::forward<TCallback>(callback));
495 case Accessor::ComponentType::UNSIGNED_INT:
496 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, uint32_t>(
499 std::forward<TCallback>(callback));
500 case Accessor::ComponentType::INT64:
501 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, int64_t>(
504 std::forward<TCallback>(callback));
505 case Accessor::ComponentType::UNSIGNED_INT64:
506 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, uint64_t>(
509 std::forward<TCallback>(callback));
510 case Accessor::ComponentType::FLOAT:
511 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, float>(
514 std::forward<TCallback>(callback));
515 case Accessor::ComponentType::DOUBLE:
516 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, double>(
519 std::forward<TCallback>(callback));
542template <
typename TCallback>
543std::invoke_result_t<TCallback, AccessorView<AccessorTypes::SCALAR<float>>>
546 int32_t accessorIndex,
547 TCallback&& callback) {
A view on the data of one accessor of a glTF asset.
bool normalized() const noexcept
Returns whether the accessor values are normalized.
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...
AccessorView(const std::byte *pData, int64_t stride, int64_t offset, int64_t size, bool normalized)
Creates a new instance from low-level parameters.
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.
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.
T value_type
The type of the elements in the accessor.
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 ...