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; }
247 const std::byte*
data() const noexcept {
248 return this->_pData + this->_offset;
252 void create(
const Model& model,
const Accessor& accessor)
noexcept {
268 const int64_t bufferBytes = int64_t(
data.size());
274 const int64_t accessorByteStride = accessor.computeByteStride(model);
275 if (accessorByteStride < 0) {
280 const int64_t accessorComponentElements =
281 accessor.computeNumberOfComponents();
282 const int64_t accessorComponentBytes =
283 accessor.computeByteSizeOfComponent();
284 const int64_t accessorBytesPerStride =
285 accessorComponentElements * accessorComponentBytes;
287 if (
sizeof(T) != accessorBytesPerStride) {
292 const int64_t accessorBytes = accessorByteStride * accessor.count;
293 const int64_t bytesRemainingInBufferView =
295 (accessor.byteOffset + accessorByteStride * (accessor.count - 1) +
296 accessorBytesPerStride);
297 if (accessorBytes > pBufferView->
byteLength ||
298 bytesRemainingInBufferView < 0) {
304 this->_stride = accessorByteStride;
305 this->_offset = accessor.byteOffset + pBufferView->
byteOffset;
306 this->_size = accessor.count;
307 this->_normalized = accessor.normalized;
336 template <
typename T>
struct VEC2 {
348 template <
typename T>
struct VEC3 {
360 template <
typename T>
struct VEC4 {
372 template <
typename T>
struct MAT2 {
384 template <
typename T>
struct MAT3 {
396 template <
typename T>
struct MAT4 {
406namespace CesiumImpl {
407template <
typename TCallback,
typename TElement>
408std::invoke_result_t<TCallback, AccessorView<AccessorTypes::SCALAR<TElement>>>
412 TCallback&& callback) {
413 if (accessor.
type == Accessor::Type::SCALAR) {
417 if (accessor.
type == Accessor::Type::VEC2) {
419 AccessorView<AccessorTypes::VEC2<TElement>>(model, accessor));
421 if (accessor.
type == Accessor::Type::VEC3) {
423 AccessorView<AccessorTypes::VEC3<TElement>>(model, accessor));
425 if (accessor.
type == Accessor::Type::VEC4) {
427 AccessorView<AccessorTypes::VEC4<TElement>>(model, accessor));
429 if (accessor.
type == Accessor::Type::MAT2) {
431 AccessorView<AccessorTypes::MAT2<TElement>>(model, accessor));
433 if (accessor.
type == Accessor::Type::MAT3) {
435 AccessorView<AccessorTypes::MAT3<TElement>>(model, accessor));
437 if (accessor.
type == Accessor::Type::MAT4) {
439 AccessorView<AccessorTypes::MAT4<TElement>>(model, accessor));
442 return callback(AccessorView<AccessorTypes::SCALAR<TElement>>(
462template <
typename TCallback>
463std::invoke_result_t<TCallback, AccessorView<AccessorTypes::SCALAR<float>>>
467 TCallback&& callback) {
469 case Accessor::ComponentType::BYTE:
470 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, int8_t>(
473 std::forward<TCallback>(callback));
474 case Accessor::ComponentType::UNSIGNED_BYTE:
475 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, uint8_t>(
478 std::forward<TCallback>(callback));
479 case Accessor::ComponentType::SHORT:
480 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, int16_t>(
483 std::forward<TCallback>(callback));
484 case Accessor::ComponentType::UNSIGNED_SHORT:
485 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, uint16_t>(
488 std::forward<TCallback>(callback));
489 case Accessor::ComponentType::INT:
490 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, int32_t>(
493 std::forward<TCallback>(callback));
494 case Accessor::ComponentType::UNSIGNED_INT:
495 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, uint32_t>(
498 std::forward<TCallback>(callback));
499 case Accessor::ComponentType::INT64:
500 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, int64_t>(
503 std::forward<TCallback>(callback));
504 case Accessor::ComponentType::UNSIGNED_INT64:
505 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, uint64_t>(
508 std::forward<TCallback>(callback));
509 case Accessor::ComponentType::FLOAT:
510 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, float>(
513 std::forward<TCallback>(callback));
514 case Accessor::ComponentType::DOUBLE:
515 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, double>(
518 std::forward<TCallback>(callback));
541template <
typename TCallback>
542std::invoke_result_t<TCallback, AccessorView<AccessorTypes::SCALAR<float>>>
545 int32_t accessorIndex,
546 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 ...