cesium-native 0.64.0
Loading...
Searching...
No Matches
AccessorView.h
1#pragma once
2
3#include <CesiumGltf/Accessor.h>
4#include <CesiumGltf/Model.h>
5
6#include <cstddef>
7#include <stdexcept>
8
9namespace CesiumGltf {
10
74
87template <class T> class AccessorView final {
88private:
89 const std::byte* _pData;
90 int64_t _stride;
91 int64_t _offset;
92 int64_t _size;
93 bool _normalized;
94 AccessorViewStatus _status;
95
96public:
100 typedef T value_type;
101
113 : _pData(nullptr),
114 _stride(0),
115 _offset(0),
116 _size(0),
117 _normalized(false),
118 _status(status) {}
119
133 const std::byte* pData,
134 int64_t stride,
135 int64_t offset,
136 int64_t size,
137 bool normalized)
138 : _pData(pData),
139 _stride(stride),
140 _offset(offset),
141 _size(size),
142 _normalized(normalized),
143 _status(AccessorViewStatus::Valid) {}
144
156 AccessorView(const Model& model, const Accessor& accessor) noexcept
157 : AccessorView() {
158 this->create(model, accessor);
159 }
160
173 AccessorView(const Model& model, int32_t accessorIndex) noexcept
174 : AccessorView() {
175 const Accessor* pAccessor = Model::getSafe(&model.accessors, accessorIndex);
176 if (!pAccessor) {
178 return;
179 }
180
181 this->create(model, *pAccessor);
182 }
183
192 const T& operator[](int64_t i) const {
193 if (i < 0 || i >= this->_size) {
194 throw std::range_error("index out of range");
195 }
196
197 return *reinterpret_cast<const T*>(
198 this->_pData + i * this->_stride + this->_offset);
199 }
200
208 int64_t size() const noexcept { return this->_size; }
209
216 AccessorViewStatus status() const noexcept { return this->_status; }
217
224 int64_t stride() const noexcept { return this->_stride; }
225
232 int64_t offset() const noexcept { return this->_offset; }
233
239 bool normalized() const noexcept { return this->_normalized; }
240
248 const std::byte* data() const noexcept {
249 return this->_pData + this->_offset;
250 }
251
252private:
253 void create(const Model& model, const Accessor& accessor) noexcept {
254 const CesiumGltf::BufferView* pBufferView =
255 Model::getSafe(&model.bufferViews, accessor.bufferView);
256 if (!pBufferView) {
258 return;
259 }
260
261 const CesiumGltf::Buffer* pBuffer =
262 Model::getSafe(&model.buffers, pBufferView->buffer);
263 if (!pBuffer) {
265 return;
266 }
267
268 const std::vector<std::byte>& data = pBuffer->cesium.data;
269 const int64_t bufferBytes = int64_t(data.size());
270 if (pBufferView->byteOffset + pBufferView->byteLength > bufferBytes) {
272 return;
273 }
274
275 const int64_t accessorByteStride = accessor.computeByteStride(model);
276 if (accessorByteStride < 0) {
278 return;
279 }
280
281 const int64_t accessorComponentElements =
282 accessor.computeNumberOfComponents();
283 const int64_t accessorComponentBytes =
284 accessor.computeByteSizeOfComponent();
285 const int64_t accessorBytesPerStride =
286 accessorComponentElements * accessorComponentBytes;
287
288 if (sizeof(T) != accessorBytesPerStride) {
289 this->_status = AccessorViewStatus::WrongSizeT;
290 return;
291 }
292
293 const int64_t accessorBytes = accessorByteStride * accessor.count;
294 const int64_t bytesRemainingInBufferView =
295 pBufferView->byteLength -
296 (accessor.byteOffset + accessorByteStride * (accessor.count - 1) +
297 accessorBytesPerStride);
298 if (accessorBytes > pBufferView->byteLength ||
299 bytesRemainingInBufferView < 0) {
301 return;
302 }
303
304 this->_pData = pBuffer->cesium.data.data();
305 this->_stride = accessorByteStride;
306 this->_offset = accessor.byteOffset + pBufferView->byteOffset;
307 this->_size = accessor.count;
308 this->_normalized = accessor.normalized;
309 this->_status = AccessorViewStatus::Valid;
310 }
311};
312
318#pragma pack(push, 1)
319
325 template <typename T> struct SCALAR {
329 T value[1];
330 };
331
337 template <typename T> struct VEC2 {
341 T value[2];
342 };
343
349 template <typename T> struct VEC3 {
353 T value[3];
354 };
355
361 template <typename T> struct VEC4 {
365 T value[4];
366 };
367
373 template <typename T> struct MAT2 {
377 T value[4];
378 };
379
385 template <typename T> struct MAT3 {
389 T value[9];
390 };
391
397 template <typename T> struct MAT4 {
401 T value[16];
402 };
403
404#pragma pack(pop)
405};
406
407namespace CesiumImpl {
408template <typename TCallback, typename TElement>
409std::invoke_result_t<TCallback, AccessorView<AccessorTypes::SCALAR<TElement>>>
411 const Model& model,
412 const Accessor& accessor,
413 TCallback&& callback) {
414 if (accessor.type == Accessor::Type::SCALAR) {
415 return callback(
417 }
418 if (accessor.type == Accessor::Type::VEC2) {
419 return callback(
420 AccessorView<AccessorTypes::VEC2<TElement>>(model, accessor));
421 }
422 if (accessor.type == Accessor::Type::VEC3) {
423 return callback(
424 AccessorView<AccessorTypes::VEC3<TElement>>(model, accessor));
425 }
426 if (accessor.type == Accessor::Type::VEC4) {
427 return callback(
428 AccessorView<AccessorTypes::VEC4<TElement>>(model, accessor));
429 }
430 if (accessor.type == Accessor::Type::MAT2) {
431 return callback(
432 AccessorView<AccessorTypes::MAT2<TElement>>(model, accessor));
433 }
434 if (accessor.type == Accessor::Type::MAT3) {
435 return callback(
436 AccessorView<AccessorTypes::MAT3<TElement>>(model, accessor));
437 }
438 if (accessor.type == Accessor::Type::MAT4) {
439 return callback(
440 AccessorView<AccessorTypes::MAT4<TElement>>(model, accessor));
441 }
442 // TODO Print a warning here???
443 return callback(AccessorView<AccessorTypes::SCALAR<TElement>>(
445}
446} // namespace CesiumImpl
447
463template <typename TCallback>
464std::invoke_result_t<TCallback, AccessorView<AccessorTypes::SCALAR<float>>>
466 const Model& model,
467 const Accessor& accessor,
468 TCallback&& callback) {
469 switch (accessor.componentType) {
470 case Accessor::ComponentType::BYTE:
471 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, int8_t>(
472 model,
473 accessor,
474 std::forward<TCallback>(callback));
475 case Accessor::ComponentType::UNSIGNED_BYTE:
476 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, uint8_t>(
477 model,
478 accessor,
479 std::forward<TCallback>(callback));
480 case Accessor::ComponentType::SHORT:
481 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, int16_t>(
482 model,
483 accessor,
484 std::forward<TCallback>(callback));
485 case Accessor::ComponentType::UNSIGNED_SHORT:
486 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, uint16_t>(
487 model,
488 accessor,
489 std::forward<TCallback>(callback));
490 case Accessor::ComponentType::INT:
491 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, int32_t>(
492 model,
493 accessor,
494 std::forward<TCallback>(callback));
495 case Accessor::ComponentType::UNSIGNED_INT:
496 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, uint32_t>(
497 model,
498 accessor,
499 std::forward<TCallback>(callback));
500 case Accessor::ComponentType::INT64:
501 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, int64_t>(
502 model,
503 accessor,
504 std::forward<TCallback>(callback));
505 case Accessor::ComponentType::UNSIGNED_INT64:
506 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, uint64_t>(
507 model,
508 accessor,
509 std::forward<TCallback>(callback));
510 case Accessor::ComponentType::FLOAT:
511 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, float>(
512 model,
513 accessor,
514 std::forward<TCallback>(callback));
515 case Accessor::ComponentType::DOUBLE:
516 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, double>(
517 model,
518 accessor,
519 std::forward<TCallback>(callback));
520 default:
523 }
524}
525
542template <typename TCallback>
543std::invoke_result_t<TCallback, AccessorView<AccessorTypes::SCALAR<float>>>
545 const Model& model,
546 int32_t accessorIndex,
547 TCallback&& callback) {
548 const Accessor* pAccessor = Model::getSafe(&model.accessors, accessorIndex);
549 if (!pAccessor) {
552 }
553
554 return createAccessorView(model, *pAccessor, callback);
555}
556} // namespace CesiumGltf
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.
Definition Accessor.h:12
std::vector< std::byte > data
The buffer's data.
A view into a buffer generally representing a subset of the buffer.
Definition BufferView.h:15
int64_t byteLength
The length of the bufferView in bytes.
Definition BufferView.h:46
int32_t buffer
The index of the buffer.
Definition BufferView.h:36
int64_t byteOffset
The offset into the buffer in bytes.
Definition BufferView.h:41
This class is not meant to be instantiated directly. Use Buffer instead.
Definition Buffer.h:9
BufferCesium cesium
Holds properties that are specific to the glTF loader rather than part of the glTF spec.
Definition Buffer.h:16
std::vector< CesiumGltf::Accessor > accessors
An array of accessors.
Definition ModelSpec.h:51
This class is not meant to be instantiated directly. Use Model instead.
Definition Model.h:14
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 ...
Definition Model.h:181