cesium-native 0.51.0
Loading...
Searching...
No Matches
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#include <CesiumUtility/Assert.h>
9
10#include <cmath>
11#include <cstdint>
12
13namespace CesiumGltf {
23public:
28 static const int ErrorInvalidPropertyAttribute = 15;
29
34 static const int ErrorUnsupportedProperty = 16;
35
40 static const int ErrorMissingAttribute = 17;
41
45 static const int ErrorInvalidAccessor = 18;
46
51 static const int ErrorAccessorTypeMismatch = 19;
52
58
64
69 static const int ErrorInvalidBufferView = 22;
70
75 static const int ErrorInvalidBuffer = 23;
76
82
88};
89
103template <typename ElementType, bool Normalized = false>
105
117template <typename ElementType>
118class PropertyAttributePropertyView<ElementType, false>
119 : public PropertyView<ElementType, false> {
120public:
125 : PropertyView<ElementType, false>(), _accessor{}, _size{0} {}
126
133 : PropertyView<ElementType, false>(status), _accessor{}, _size{0} {
134 CESIUM_ASSERT(
136 "An empty property view should not be constructed with a valid status");
137 }
138
150 const ClassProperty& classProperty,
151 int64_t size) noexcept
152 : PropertyView<ElementType, false>(classProperty), _accessor{}, _size{0} {
153 if (this->_status != PropertyAttributePropertyViewStatus::Valid) {
154 // Don't override the status / size if something is wrong with the class
155 // property's definition.
156 return;
157 }
158
159 if (!classProperty.defaultProperty) {
160 // This constructor should only be called if the class property *has* a
161 // default value. But in the case that it does not, this property view
162 // becomes invalid.
163 this->_status =
165 return;
166 }
167
168 this->_status =
170 this->_size = size;
171 }
172
182 const PropertyAttributeProperty& property,
183 const ClassProperty& classProperty,
185 : PropertyView<ElementType, false>(classProperty, property),
186 _accessor{accessorView},
187 _size{
189 ? accessorView.size()
190 : 0} {}
191
207 std::optional<ElementType> get(int64_t index) const noexcept {
208 if (this->_status ==
210 return this->defaultValue();
211 }
212
213 ElementType value = getRaw(index);
214
215 if (value == this->noData()) {
216 return this->defaultValue();
217 }
218
219 return transformValue(value, this->offset(), this->scale());
220 }
221
232 ElementType getRaw(int64_t index) const noexcept {
233 CESIUM_ASSERT(
235 "Check the status() first to make sure view is valid");
236 CESIUM_ASSERT(
237 size() > 0 &&
238 "Check the size() of the view to make sure it's not empty");
239 CESIUM_ASSERT(index >= 0 && "index must be non-negative");
240 CESIUM_ASSERT(index < size() && "index must be less than size");
241
242 return _accessor[index];
243 }
244
252 int64_t size() const noexcept { return _size; }
253
258 const AccessorView<ElementType>& accessorView() const { return _accessor; }
259
260private:
262 int64_t _size;
263};
264
276template <typename ElementType>
277class PropertyAttributePropertyView<ElementType, true>
278 : public PropertyView<ElementType, true> {
279private:
280 using NormalizedType = typename TypeToNormalizedType<ElementType>::type;
281
282public:
287 : PropertyView<ElementType, true>(), _accessor{}, _size{0} {}
288
295 : PropertyView<ElementType, true>(status), _accessor{}, _size{0} {
296 CESIUM_ASSERT(
298 "An empty property view should not be constructed with a valid status");
299 }
300
311 const ClassProperty& classProperty,
312 int64_t size) noexcept
313 : PropertyView<ElementType, true>(classProperty), _accessor{}, _size{0} {
314 if (this->_status != PropertyAttributePropertyViewStatus::Valid) {
315 // Don't override the status / size if something is wrong with the class
316 // property's definition.
317 return;
318 }
319
320 if (!classProperty.defaultProperty) {
321 // This constructor should only be called if the class property *has* a
322 // default value. But in the case that it does not, this property view
323 // becomes invalid.
324 this->_status =
326 return;
327 }
328
329 this->_status =
331 this->_size = size;
332 }
333
343 const PropertyAttributeProperty& property,
344 const ClassProperty& classProperty,
346 : PropertyView<ElementType, true>(classProperty, property),
347 _accessor{accessorView},
348 _size{
350 ? accessorView.size()
351 : 0} {}
352
368 std::optional<NormalizedType> get(int64_t index) const noexcept {
369 if (this->_status ==
371 return this->defaultValue();
372 }
373
374 ElementType value = getRaw(index);
375
376 if (value == this->noData()) {
377 return this->defaultValue();
378 }
379
383 this->offset(),
384 this->scale());
385 }
386
388 constexpr glm::length_t N = ElementType::length();
389 using T = typename ElementType::value_type;
390 using NormalizedT = typename NormalizedType::value_type;
392 normalize<N, T>(value),
393 this->offset(),
394 this->scale());
395 }
396
398 constexpr glm::length_t N = ElementType::length();
399 using T = typename ElementType::value_type;
400 using NormalizedT = typename NormalizedType::value_type;
402 normalize<N, T>(value),
403 this->offset(),
404 this->scale());
405 }
406 }
407
418 ElementType getRaw(int64_t index) const noexcept {
419 CESIUM_ASSERT(
421 "Check the status() first to make sure view is valid");
422 CESIUM_ASSERT(
423 size() > 0 &&
424 "Check the size() of the view to make sure it's not empty");
425 CESIUM_ASSERT(index >= 0 && "index must be non-negative");
426 CESIUM_ASSERT(index < size() && "index must be less than size");
427
428 return _accessor[index];
429 }
430
438 int64_t size() const noexcept { return _size; }
439
444 const AccessorView<ElementType>& accessorView() const { return _accessor; }
445
446private:
448 int64_t _size;
449};
450
451} // namespace CesiumGltf
A view on the data of one accessor of a glTF asset.
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....
const AccessorView< ElementType > & accessorView() const
Gets the underlying AccessorView from the PropertyAttributePropertyView.
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.
const AccessorView< ElementType > & accessorView() const
Gets the underlying AccessorView from the PropertyAttributePropertyView.
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,...
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....
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...
A view of the data specified by a PropertyAttributeProperty.
Indicates the status of a property view.
static const PropertyViewStatusType Valid
This property view is valid and ready to use.
static const PropertyViewStatusType ErrorNonexistentProperty
This property view is trying to view a property that does not exist.
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.
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...
int32_t PropertyViewStatusType
The type used for fields of PropertyViewStatus.
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....