3#include <CesiumGltf/Enum.h>
4#include <CesiumGltf/PropertyArrayView.h>
5#include <CesiumGltf/PropertyTransformations.h>
6#include <CesiumGltf/PropertyTypeTraits.h>
7#include <CesiumGltf/PropertyView.h>
8#include <CesiumUtility/Assert.h>
144template <
typename ElementType,
bool Normalized = false>
162template <
typename ElementType>
175 _arrayOffsetTypeSize{0},
178 _stringOffsetTypeSize{0} {}
192 _arrayOffsetTypeSize{0},
195 _stringOffsetTypeSize{0} {
198 "An empty property view should not be constructed with a valid status");
218 _arrayOffsetTypeSize{0},
221 _stringOffsetTypeSize{0} {
232 this->_status = PropertyTablePropertyViewStatus::ErrorNonexistentProperty;
255 std::span<const std::byte> values) noexcept
282 std::span<const std::byte> values) noexcept
289 _arrayOffsetTypeSize{0},
292 _stringOffsetTypeSize{0} {}
317 std::span<const std::byte> values,
318 std::span<const std::byte> arrayOffsets,
319 std::span<const std::byte> stringOffsets,
359 std::span<const std::byte> values,
360 std::span<const std::byte> arrayOffsets,
361 std::span<const std::byte> stringOffsets,
368 _arrayOffsets{arrayOffsets},
369 _arrayOffsetType{arrayOffsetType},
371 _stringOffsets{stringOffsets},
372 _stringOffsetType{stringOffsetType},
390 std::optional<PropertyValueViewToCopy<ElementType>>
391 get(int64_t index)
const noexcept {
394 CESIUM_ASSERT(index >= 0 &&
"index must be non-negative");
395 CESIUM_ASSERT(index <
size() &&
"index must be less than size");
400 ElementType value =
getRaw(index);
402 if (value == this->
noData()) {
423 ElementType
getRaw(int64_t index)
const noexcept {
426 "Check the status() first to make sure view is valid");
429 "Check the size() of the view to make sure it's not empty");
430 CESIUM_ASSERT(index >= 0 &&
"index must be non-negative");
431 CESIUM_ASSERT(index <
size() &&
"index must be less than size");
434 return getNumericValue(index);
438 return getBooleanValue(index);
442 return getStringValue(index);
446 return getNumericArrayValues<
451 return getBooleanArrayValues(index);
455 return getStringArrayValues(index);
466 int64_t
size() const noexcept {
return _size; }
469 ElementType getNumericValue(int64_t index)
const noexcept {
470 return reinterpret_cast<const ElementType*
>(_values.data())[index];
473 bool getBooleanValue(int64_t index)
const noexcept {
474 const int64_t byteIndex = index / 8;
475 const int64_t bitIndex = index % 8;
477 static_cast<int>(_values[
static_cast<size_t>(byteIndex)] >> bitIndex) &
479 return bitValue == 1;
482 std::string_view getStringValue(int64_t index)
const noexcept {
483 const size_t currentOffset = getOffsetFromOffsetsBuffer(
484 static_cast<size_t>(index),
487 const size_t nextOffset = getOffsetFromOffsetsBuffer(
488 static_cast<size_t>(index + 1),
491 return std::string_view(
492 reinterpret_cast<const char*
>(_values.data() + currentOffset),
493 nextOffset - currentOffset);
496 template <
typename T>
497 PropertyArrayView<T> getNumericArrayValues(int64_t index)
const noexcept {
498 size_t count =
static_cast<size_t>(this->arrayCount());
501 size_t arraySize = count *
sizeof(T);
502 const std::span<const std::byte> values(
503 _values.data() +
static_cast<size_t>(index) * arraySize,
505 return PropertyArrayView<T>{values};
510 const size_t currentOffset = getOffsetFromOffsetsBuffer(
511 static_cast<size_t>(index),
515 const size_t nextOffset = getOffsetFromOffsetsBuffer(
516 static_cast<size_t>(index + 1),
520 const std::span<const std::byte> values(
521 _values.data() + currentOffset,
522 nextOffset - currentOffset);
523 return PropertyArrayView<T>{values};
526 PropertyArrayView<std::string_view>
527 getStringArrayValues(int64_t index)
const noexcept {
528 size_t count =
static_cast<size_t>(this->arrayCount());
532 const size_t arraySize =
533 count *
static_cast<size_t>(_stringOffsetTypeSize);
534 const std::span<const std::byte> stringOffsetValues(
535 _stringOffsets.data() +
static_cast<size_t>(index) * arraySize,
536 arraySize +
static_cast<size_t>(_stringOffsetTypeSize));
537 return PropertyArrayView<std::string_view>(
541 static_cast<int64_t
>(count));
545 const size_t currentArrayOffset =
546 getOffsetFromOffsetsBuffer(
547 static_cast<size_t>(index),
550 static_cast<size_t>(_stringOffsetTypeSize);
551 const size_t nextArrayOffset = getOffsetFromOffsetsBuffer(
552 static_cast<size_t>(index + 1),
555 static_cast<size_t>(_stringOffsetTypeSize);
556 const size_t arraySize = nextArrayOffset - currentArrayOffset;
557 const std::span<const std::byte> stringOffsetValues(
558 _stringOffsets.data() + currentArrayOffset,
559 arraySize +
static_cast<size_t>(_stringOffsetTypeSize));
560 return PropertyArrayView<std::string_view>(
564 static_cast<int64_t
>(arraySize) / _stringOffsetTypeSize);
567 PropertyArrayView<bool> getBooleanArrayValues(int64_t index)
const noexcept {
568 size_t count =
static_cast<size_t>(this->arrayCount());
571 const size_t offsetBits = count *
static_cast<size_t>(index);
572 const size_t nextOffsetBits = count *
static_cast<size_t>(index + 1);
573 const std::span<const std::byte> buffer(
574 _values.data() + offsetBits / 8,
575 (nextOffsetBits / 8 - offsetBits / 8 + 1));
576 return PropertyArrayView<bool>(
579 static_cast<int64_t
>(count));
583 const size_t currentOffset = getOffsetFromOffsetsBuffer(
584 static_cast<size_t>(index),
587 const size_t nextOffset = getOffsetFromOffsetsBuffer(
588 static_cast<size_t>(index + 1),
591 const size_t totalBits = nextOffset - currentOffset;
592 const std::span<const std::byte> buffer(
593 _values.data() + currentOffset / 8,
594 (nextOffset / 8 - currentOffset / 8 + 1));
595 return PropertyArrayView<bool>(
598 static_cast<int64_t
>(totalBits));
601 std::span<const std::byte> _values;
604 std::span<const std::byte> _arrayOffsets;
606 int64_t _arrayOffsetTypeSize;
608 std::span<const std::byte> _stringOffsets;
610 int64_t _stringOffsetTypeSize;
627template <
typename ElementType>
643 _arrayOffsetTypeSize{0} {}
657 _arrayOffsetTypeSize{0} {
660 "An empty property view should not be constructed with a valid status");
680 _arrayOffsetTypeSize{0} {
691 this->_status = PropertyTablePropertyViewStatus::ErrorNonexistentProperty;
714 std::span<const std::byte> values) noexcept
721 _arrayOffsetTypeSize{0} {}
743 std::span<const std::byte> values,
744 std::span<const std::byte> arrayOffsets,
750 _arrayOffsets{arrayOffsets},
751 _arrayOffsetType{arrayOffsetType},
769 std::optional<PropertyValueViewToCopy<NormalizedType>>
770 get(int64_t index)
const noexcept {
773 CESIUM_ASSERT(index >= 0 &&
"index must be non-negative");
774 CESIUM_ASSERT(index <
size() &&
"index must be less than size");
779 ElementType value =
getRaw(index);
788 constexpr glm::length_t N = ElementType::length();
789 using T =
typename ElementType::value_type;
790 using NormalizedT =
typename NormalizedType::value_type;
796 constexpr glm::length_t N = ElementType::length();
797 using T =
typename ElementType::value_type;
798 using NormalizedT =
typename NormalizedType::value_type;
811 constexpr glm::length_t N = ArrayElementType::length();
812 using T =
typename ArrayElementType::value_type;
818 constexpr glm::length_t N = ArrayElementType::length();
819 using T =
typename ArrayElementType::value_type;
838 ElementType
getRaw(int64_t index)
const noexcept {
841 "Check the status() first to make sure view is valid");
844 "Check the size() of the view to make sure it's not empty");
845 CESIUM_ASSERT(index >= 0 &&
"index must be non-negative");
846 CESIUM_ASSERT(index <
size() &&
"index must be less than size");
849 return getValue(index);
853 return getArrayValues<typename MetadataArrayType<ElementType>::type>(
865 int64_t
size() const noexcept {
870 ElementType getValue(int64_t index)
const noexcept {
871 return reinterpret_cast<const ElementType*
>(_values.data())[index];
874 template <
typename T>
875 PropertyArrayView<T> getArrayValues(int64_t index)
const noexcept {
876 size_t count =
static_cast<size_t>(this->arrayCount());
879 size_t arraySize = count *
sizeof(T);
880 const std::span<const std::byte> values(
881 _values.data() +
static_cast<size_t>(index) * arraySize,
883 return PropertyArrayView<T>{values};
888 const size_t currentOffset = getOffsetFromOffsetsBuffer(
889 static_cast<size_t>(index),
893 const size_t nextOffset = getOffsetFromOffsetsBuffer(
894 static_cast<size_t>(index + 1),
898 const std::span<const std::byte> values(
899 _values.data() + currentOffset,
900 nextOffset - currentOffset);
901 return PropertyArrayView<T>{values};
904 std::span<const std::byte> _values;
907 std::span<const std::byte> _arrayOffsets;
909 int64_t _arrayOffsetTypeSize;
Indicates the status of a property table property view.
static const PropertyViewStatusType ErrorBufferViewSizeDoesNotMatchPropertyTableCount
This property view has an invalid buffer view; its length does not match the size of the property tab...
static const PropertyViewStatusType ErrorInvalidArrayOffsetBufferView
This array property view does not have a valid array offset buffer view index.
static const PropertyViewStatusType ErrorBufferViewOutOfBounds
This property view has a buffer view that points outside the bounds of its target buffer.
static const PropertyViewStatusType ErrorArrayCountAndOffsetBufferCoexist
This array property view has both a fixed length and an offset buffer view defined.
static const PropertyViewStatusType ErrorInvalidValueBufferView
This property view does not have a valid value buffer view index.
static const PropertyViewStatusType ErrorInvalidStringOffsetBuffer
This property view has a valid string offset buffer view, but the buffer view specifies an invalid bu...
static const PropertyViewStatusType ErrorInvalidStringOffsetType
This property view has an unknown string offset type.
static const PropertyViewStatusType ErrorArrayCountAndOffsetBufferDontExist
This array property view has neither a fixed length nor an offset buffer view defined.
static const PropertyViewStatusType ErrorArrayOffsetsNotSorted
This property view's array offset values are not sorted in ascending order.
static const PropertyViewStatusType ErrorInvalidArrayOffsetBuffer
This property view has a valid array string buffer view, but the buffer view specifies an invalid buf...
static const PropertyViewStatusType ErrorArrayOffsetOutOfBounds
This property view has an array offset that is out of bounds.
static const PropertyViewStatusType ErrorInvalidPropertyTable
This property view was initialized from an invalid PropertyTable.
static const PropertyViewStatusType ErrorInvalidValueBuffer
This property view has a valid value buffer view, but the buffer view specifies an invalid buffer ind...
static const PropertyViewStatusType ErrorInvalidStringOffsetBufferView
This string property view does not have a valid string offset buffer view index.
static const PropertyViewStatusType ErrorInvalidArrayOffsetType
This property view has an unknown array offset type.
static const PropertyViewStatusType ErrorStringOffsetsNotSorted
This property view's string offset values are not sorted in ascending order.
static const PropertyViewStatusType ErrorStringOffsetOutOfBounds
This property view has a string offset that is out of bounds.
static const PropertyViewStatusType ErrorBufferViewSizeNotDivisibleByTypeSize
This property view has an invalid buffer view; its length is not a multiple of the size of its type /...
PropertyTablePropertyView(const ClassProperty &classProperty, int64_t size)
Constructs an instance of an empty property that specifies a default value. Although this property ha...
PropertyTablePropertyView(const PropertyTableProperty &property, const ClassProperty &classProperty, int64_t size, std::span< const std::byte > values, std::span< const std::byte > arrayOffsets, std::span< const std::byte > stringOffsets, PropertyComponentType arrayOffsetType, PropertyComponentType stringOffsetType) noexcept
Construct an instance pointing to the data specified by a PropertyTableProperty.
PropertyTablePropertyView()
Constructs an invalid instance for a non-existent property.
std::optional< PropertyValueViewToCopy< ElementType > > get(int64_t index) const noexcept
Get the value of an element in the PropertyTable, with all value transforms applied....
PropertyTablePropertyView(const PropertyTableProperty &property, const ClassProperty &classProperty, const CesiumGltf::Enum *pEnumDefinition, int64_t size, std::span< const std::byte > values, std::span< const std::byte > arrayOffsets, std::span< const std::byte > stringOffsets, PropertyComponentType arrayOffsetType, PropertyComponentType stringOffsetType) noexcept
Construct an instance pointing to the data specified by a PropertyTableProperty, with an enum definit...
ElementType getRaw(int64_t index) const noexcept
Get the raw value of an element of the PropertyTable, without offset or scale applied.
PropertyTablePropertyView(PropertyViewStatusType status)
Constructs an invalid instance for an erroneous property.
int64_t size() const noexcept
Get the number of elements in this PropertyTablePropertyView. If the view is valid,...
PropertyTablePropertyView(const PropertyTableProperty &property, const ClassProperty &classProperty, int64_t size, std::span< const std::byte > values) noexcept
Construct an instance pointing to data specified by a PropertyTableProperty. Used for non-array or fi...
PropertyTablePropertyView(const PropertyTableProperty &property, const ClassProperty &classProperty, const CesiumGltf::Enum *pEnumDefinition, int64_t size, std::span< const std::byte > values) noexcept
Construct an instance pointing to data specified by a PropertyTableProperty, with an enum definition ...
PropertyTablePropertyView()
Constructs an invalid instance for a non-existent property.
std::optional< PropertyValueViewToCopy< NormalizedType > > get(int64_t index) const noexcept
Get the value of an element of the PropertyTable, with normalization and other value transforms appli...
ElementType getRaw(int64_t index) const noexcept
Get the raw value of an element of the PropertyTable, without offset, scale, or normalization applied...
PropertyTablePropertyView(const PropertyTableProperty &property, const ClassProperty &classProperty, int64_t size, std::span< const std::byte > values) noexcept
Construct an instance pointing to data specified by a PropertyTableProperty. Used for non-array or fi...
PropertyTablePropertyView(PropertyViewStatusType status)
Constructs an invalid instance for an erroneous property.
PropertyTablePropertyView(const PropertyTableProperty &property, const ClassProperty &classProperty, int64_t size, std::span< const std::byte > values, std::span< const std::byte > arrayOffsets, PropertyComponentType arrayOffsetType) noexcept
Construct an instance pointing to the data specified by a PropertyTableProperty.
int64_t size() const noexcept
Get the number of elements in this PropertyTablePropertyView. If the view is valid,...
PropertyTablePropertyView(const ClassProperty &classProperty, int64_t size)
Constructs an instance of an empty property that specifies a default value. Although this property ha...
A view on the data of the PropertyTableProperty that is created by a PropertyTableView.
Indicates the status of a property view.
static const PropertyViewStatusType Valid
This property view is valid and ready to use.
static const PropertyViewStatusType EmptyPropertyWithDefault
This property view does not contain any data, but specifies a default value. This happens when a clas...
std::optional< ElementType > offset() const noexcept
Gets the offset to apply to property values. Only applicable to SCALAR, VECN, and MATN types when the...
std::optional< ElementType > scale() const noexcept
Gets the scale to apply to property values. Only applicable to SCALAR, VECN, and MATN types when the ...
std::optional< ElementType > defaultValue() const noexcept
Gets the default value to use when encountering a "no data" value or an omitted property....
PropertyViewStatusType _status
Indicates the status of a property view.
PropertyViewStatusType status() const noexcept
Gets the status of this property view, indicating whether an error occurred.
std::optional< ElementType > noData() const noexcept
Gets the "no data" value, i.e., the value representing missing data in the property wherever it appea...
PropertyView()
Constructs an empty property instance.
std::optional< ElementType > noData() const noexcept
Constructs an empty property instance. false>noData
PropertyViewStatusType _status
Indicates the status of a property view.
std::optional< NormalizedType > scale() const noexcept
Constructs an empty property instance. false>scale
PropertyView()
Constructs an empty property instance.
std::optional< NormalizedType > defaultValue() const noexcept
Constructs an empty property instance. false>defaultValue
PropertyViewStatusType status() const noexcept
Constructs an empty property instance. false>status
std::optional< NormalizedType > offset() const noexcept
Constructs an empty property instance. false>offset
Classes for working with glTF models.
PropertyComponentType
The possible types of a property component.
PropertyArrayCopy< glm::mat< N, N, double > > transformNormalizedMatNArray(const PropertyArrayView< glm::mat< N, N, T > > &value, const std::optional< PropertyArrayView< glm::mat< N, N, double > > > &offset, const std::optional< PropertyArrayView< glm::mat< N, N, double > > > &scale)
Normalizes each element of an array of matrices and transforms them by optional offset and scale fact...
PropertyArrayCopy< glm::vec< N, double > > transformNormalizedVecNArray(const PropertyArrayView< glm::vec< N, T > > &value, const std::optional< PropertyArrayView< glm::vec< N, double > > > &offset, const std::optional< PropertyArrayView< glm::vec< N, double > > > &scale)
Normalizes each element of an array of vectors and transforms them by optional offset and scale facto...
PropertyArrayCopy< NormalizedType > transformNormalizedArray(const PropertyArrayView< T > &value, const std::optional< PropertyArrayView< NormalizedType > > &offset, const std::optional< PropertyArrayView< NormalizedType > > &scale)
Normalizes each element of an array of values and transforms them by optional offset and scale factor...
T transformValue(const T &value, const std::optional< T > &offset, const std::optional< T > &scale)
Transforms the value by optional offset and scale factors.
double normalize(T value)
Normalizes the given value between [0, 1] if unsigned or [-1, 1] if signed, based on the type's maxim...
int64_t getOffsetTypeSize(PropertyComponentType offsetType) noexcept
Returns the size in bytes of a PropertyComponentType used as the arrayOffsetType in the constructor o...
PropertyArrayCopy< T > transformArray(const PropertyArrayView< T > &value, const std::optional< PropertyArrayView< T > > &offset, const std::optional< PropertyArrayView< T > > &scale)
Transforms each element of an array of values by optional offset and scale factors....
int32_t PropertyViewStatusType
The type used for fields of PropertyViewStatus.
std::optional< CesiumUtility::JsonValue > defaultProperty
A default value to use when encountering a noData value or an omitted property. The value is given in...
This class is not meant to be instantiated directly. Use Enum instead.
An array of binary property values.
Convert an integer numeric type to the corresponding representation as a double type....