cesium-native  0.41.0
PropertyAttributePropertyView.h
1 #pragma once
2 
3 #include "CesiumGltf/AccessorView.h"
4 #include "CesiumGltf/PropertyAttributeProperty.h"
5 #include "CesiumGltf/PropertyTransformations.h"
6 #include "CesiumGltf/PropertyTypeTraits.h"
7 #include "CesiumGltf/PropertyView.h"
8 
9 #include <CesiumUtility/Assert.h>
10 
11 #include <cmath>
12 #include <cstdint>
13 
14 namespace CesiumGltf {
24 public:
29  static const int ErrorInvalidPropertyAttribute = 14;
30 
35  static const int ErrorUnsupportedProperty = 15;
36 
41  static const int ErrorMissingAttribute = 16;
42 
46  static const int ErrorInvalidAccessor = 17;
47 
52  static const int ErrorAccessorTypeMismatch = 18;
53 
58  static const int ErrorAccessorComponentTypeMismatch = 19;
59 
64  static const int ErrorAccessorNormalizationMismatch = 20;
65 
70  static const int ErrorInvalidBufferView = 21;
71 
76  static const int ErrorInvalidBuffer = 22;
77 
82  static const PropertyViewStatusType ErrorAccessorOutOfBounds = 23;
83 
88  static const PropertyViewStatusType ErrorBufferViewOutOfBounds = 24;
89 };
90 
104 template <typename ElementType, bool Normalized = false>
106 
118 template <typename ElementType>
119 class PropertyAttributePropertyView<ElementType, false>
120  : public PropertyView<ElementType, false> {
121 public:
126  : PropertyView<ElementType, false>(), _accessor{}, _size{0} {}
127 
133  PropertyAttributePropertyView(PropertyViewStatusType status) noexcept
134  : PropertyView<ElementType, false>(status), _accessor{}, _size{0} {
135  CESIUM_ASSERT(
137  "An empty property view should not be constructed with a valid status");
138  }
139 
151  const ClassProperty& classProperty,
152  int64_t size) noexcept
153  : PropertyView<ElementType, false>(classProperty), _accessor{}, _size{0} {
154  if (this->_status != PropertyAttributePropertyViewStatus::Valid) {
155  // Don't override the status / size if something is wrong with the class
156  // property's definition.
157  return;
158  }
159 
160  if (!classProperty.defaultProperty) {
161  // This constructor should only be called if the class property *has* a
162  // default value. But in the case that it does not, this property view
163  // becomes invalid.
164  this->_status =
166  return;
167  }
168 
169  this->_status =
171  this->_size = size;
172  }
173 
183  const PropertyAttributeProperty& property,
184  const ClassProperty& classProperty,
185  const AccessorView<ElementType>& accessorView) noexcept
186  : PropertyView<ElementType, false>(classProperty, property),
187  _accessor{accessorView},
188  _size{
190  ? accessorView.size()
191  : 0} {}
192 
208  std::optional<ElementType> get(int64_t index) const noexcept {
209  if (this->_status ==
211  return this->defaultValue();
212  }
213 
214  ElementType value = getRaw(index);
215 
216  if (value == this->noData()) {
217  return this->defaultValue();
218  }
219 
220  return transformValue(value, this->offset(), this->scale());
221  }
222 
233  ElementType getRaw(int64_t index) const noexcept {
234  CESIUM_ASSERT(
236  "Check the status() first to make sure view is valid");
237  CESIUM_ASSERT(
238  size() > 0 &&
239  "Check the size() of the view to make sure it's not empty");
240  CESIUM_ASSERT(index >= 0 && "index must be non-negative");
241  CESIUM_ASSERT(index < size() && "index must be less than size");
242 
243  return _accessor[index];
244  }
245 
253  int64_t size() const noexcept { return _size; }
254 
255 private:
256  AccessorView<ElementType> _accessor;
257  int64_t _size;
258 };
259 
271 template <typename ElementType>
272 class PropertyAttributePropertyView<ElementType, true>
273  : public PropertyView<ElementType, true> {
274 private:
275  using NormalizedType = typename TypeToNormalizedType<ElementType>::type;
276 
277 public:
282  : PropertyView<ElementType, true>(), _accessor{}, _size{0} {}
283 
289  PropertyAttributePropertyView(PropertyViewStatusType status) noexcept
290  : PropertyView<ElementType, true>(status), _accessor{}, _size{0} {
291  CESIUM_ASSERT(
293  "An empty property view should not be constructed with a valid status");
294  }
295 
307  const ClassProperty& classProperty,
308  int64_t size) noexcept
309  : PropertyView<ElementType, true>(classProperty), _accessor{}, _size{0} {
310  if (this->_status != PropertyAttributePropertyViewStatus::Valid) {
311  // Don't override the status / size if something is wrong with the class
312  // property's definition.
313  return;
314  }
315 
316  if (!classProperty.defaultProperty) {
317  // This constructor should only be called if the class property *has* a
318  // default value. But in the case that it does not, this property view
319  // becomes invalid.
320  this->_status =
322  return;
323  }
324 
325  this->_status =
327  this->_size = size;
328  }
329 
339  const PropertyAttributeProperty& property,
340  const ClassProperty& classProperty,
341  const AccessorView<ElementType>& accessorView) noexcept
342  : PropertyView<ElementType, true>(classProperty, property),
343  _accessor{accessorView},
344  _size{
346  ? accessorView.size()
347  : 0} {}
348 
364  std::optional<NormalizedType> get(int64_t index) const noexcept {
365  if (this->_status ==
367  return this->defaultValue();
368  }
369 
370  ElementType value = getRaw(index);
371 
372  if (value == this->noData()) {
373  return this->defaultValue();
374  }
375 
376  if constexpr (IsMetadataScalar<ElementType>::value) {
377  return transformValue<NormalizedType>(
378  normalize<ElementType>(value),
379  this->offset(),
380  this->scale());
381  }
382 
383  if constexpr (IsMetadataVecN<ElementType>::value) {
384  constexpr glm::length_t N = ElementType::length();
385  using T = typename ElementType::value_type;
386  using NormalizedT = typename NormalizedType::value_type;
387  return transformValue<glm::vec<N, NormalizedT>>(
388  normalize<N, T>(value),
389  this->offset(),
390  this->scale());
391  }
392 
393  if constexpr (IsMetadataMatN<ElementType>::value) {
394  constexpr glm::length_t N = ElementType::length();
395  using T = typename ElementType::value_type;
396  using NormalizedT = typename NormalizedType::value_type;
397  return transformValue<glm::mat<N, N, NormalizedT>>(
398  normalize<N, T>(value),
399  this->offset(),
400  this->scale());
401  }
402  }
403 
414  ElementType getRaw(int64_t index) const noexcept {
415  CESIUM_ASSERT(
417  "Check the status() first to make sure view is valid");
418  CESIUM_ASSERT(
419  size() > 0 &&
420  "Check the size() of the view to make sure it's not empty");
421  CESIUM_ASSERT(index >= 0 && "index must be non-negative");
422  CESIUM_ASSERT(index < size() && "index must be less than size");
423 
424  return _accessor[index];
425  }
426 
434  int64_t size() const noexcept { return _size; }
435 
436 private:
437  AccessorView<ElementType> _accessor;
438  int64_t _size;
439 };
440 
441 } // namespace CesiumGltf
Indicates the status of a property attribute property view.
static const int ErrorInvalidBufferView
This property view uses an accessor that does not have a valid buffer view index.
static const int ErrorInvalidBuffer
This property view uses a buffer view that does not have a valid buffer index.
static const int ErrorAccessorNormalizationMismatch
This property view's normalization does not match the normalization of the accessor it uses.
static const int ErrorInvalidAccessor
This property view's attribute does not have a valid accessor index.
static const int ErrorMissingAttribute
This property view was initialized with a primitive that does not contain the specified attribute.
static const int ErrorAccessorComponentTypeMismatch
This property view's component type does not match the type of the accessor it uses.
static const int ErrorUnsupportedProperty
This property view is associated with a ClassProperty of an unsupported type.
static const int ErrorAccessorTypeMismatch
This property view's type does not match the type of the accessor it uses.
static const int ErrorInvalidPropertyAttribute
This property view was initialized from an invalid PropertyAttribute.
static const PropertyViewStatusType ErrorAccessorOutOfBounds
This property view uses an accessor that points outside the bounds of its target buffer view.
static const PropertyViewStatusType ErrorBufferViewOutOfBounds
This property view uses a buffer view that points outside the bounds of its target buffer.
std::optional< ElementType > get(int64_t index) const noexcept
Gets the value of the property for the given vertex index with all value transforms applied....
PropertyAttributePropertyView(PropertyViewStatusType status) noexcept
Constructs an invalid instance for an erroneous property.
ElementType getRaw(int64_t index) const noexcept
Gets the raw value of the property for the given vertex index.
PropertyAttributePropertyView() noexcept
Constructs an invalid instance for a non-existent property.
PropertyAttributePropertyView(const ClassProperty &classProperty, int64_t size) noexcept
Constructs an instance of an empty property that specifies a default value. Although this property ha...
int64_t size() const noexcept
Get the number of elements in this PropertyAttributePropertyView. If the view is valid,...
PropertyAttributePropertyView(const PropertyAttributeProperty &property, const ClassProperty &classProperty, const AccessorView< ElementType > &accessorView) noexcept
Construct a view of the data specified by a PropertyAttributeProperty.
ElementType getRaw(int64_t index) const noexcept
Gets the raw value of the property for the given vertex index.
PropertyAttributePropertyView(const PropertyAttributeProperty &property, const ClassProperty &classProperty, const AccessorView< ElementType > &accessorView) noexcept
Construct a view of the data specified by a PropertyAttributeProperty.
int64_t size() const noexcept
Get the number of elements in this PropertyAttributePropertyView. If the view is valid,...
PropertyAttributePropertyView(PropertyViewStatusType status) noexcept
Constructs an invalid instance for an erroneous property.
PropertyAttributePropertyView() noexcept
Constructs an invalid instance for a non-existent property.
PropertyAttributePropertyView(const ClassProperty &classProperty, int64_t size) noexcept
Constructs an instance of an empty property that specifies a default value. Although this property ha...
std::optional< NormalizedType > get(int64_t index) const noexcept
Gets the value of the property for the given vertex index with all value transforms applied....
A view of the data specified by a PropertyAttributeProperty.
Indicates the status of a property view.
Definition: PropertyView.h:27
static const PropertyViewStatusType Valid
This property view is valid and ready to use.
Definition: PropertyView.h:32
static const PropertyViewStatusType ErrorNonexistentProperty
This property view is trying to view a property that does not exist.
Definition: PropertyView.h:47
static const PropertyViewStatusType EmptyPropertyWithDefault
This property view does not contain any data, but specifies a default value. This happens when a clas...
Definition: PropertyView.h:41
Represents a non-normalized metadata property in EXT_structural_metadata.
Definition: PropertyView.h:265
Represents a normalized metadata property in EXT_structural_metadata.
Definition: PropertyView.h:647
Represents a metadata property in EXT_structural_metadata.
Definition: PropertyView.h:251
Classes for working with glTF models.
A class property.
Definition: ClassProperty.h:19
Check if a C++ type can be represented as a matN type.
Check if a C++ type can be represented as a scalar property type.
Check if a C++ type can be represented as a vecN type.
An attribute containing property values.
Convert an integer numeric type to the corresponding representation as a double type....