cesium-native 0.64.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
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
153 const ClassProperty& classProperty,
154 int64_t size) noexcept
155 : PropertyView<ElementType, false>(classProperty), _accessor{}, _size{0} {
156 if (this->_status != PropertyAttributePropertyViewStatus::Valid) {
157 // Don't override the status / size if something is wrong with the class
158 // property's definition.
159 return;
160 }
161
162 if (!classProperty.defaultProperty) {
163 // This constructor should only be called if the class property *has* a
164 // default value. But in the case that it does not, this property view
165 // becomes invalid.
166 this->_status =
168 return;
169 }
170
171 this->_status =
173 this->_size = size;
174 }
175
186 const PropertyAttributeProperty& property,
187 const ClassProperty& classProperty,
189 : PropertyView<ElementType, false>(classProperty, property),
190 _accessor{accessorView},
191 _size{
193 ? accessorView.size()
194 : 0} {}
195
211 std::optional<ElementType> get(int64_t index) const noexcept {
212 if (this->_status ==
214 return this->defaultValue();
215 }
216
217 ElementType value = getRaw(index);
218
219 if (value == this->noData()) {
220 return this->defaultValue();
221 }
222
223 return transformValue(value, this->offset(), this->scale());
224 }
225
236 ElementType getRaw(int64_t index) const noexcept {
237 CESIUM_ASSERT(
239 "Check the status() first to make sure view is valid");
240 CESIUM_ASSERT(
241 size() > 0 &&
242 "Check the size() of the view to make sure it's not empty");
243 CESIUM_ASSERT(index >= 0 && "index must be non-negative");
244 CESIUM_ASSERT(index < size() && "index must be less than size");
245
246 return _accessor[index];
247 }
248
256 int64_t size() const noexcept { return _size; }
257
262 const AccessorView<ElementType>& accessorView() const { return _accessor; }
263
264private:
266 int64_t _size;
267};
268
280template <typename ElementType>
281class PropertyAttributePropertyView<ElementType, true>
282 : public PropertyView<ElementType, true> {
283private:
284 using NormalizedType = typename TypeToNormalizedType<ElementType>::type;
285
286public:
291 : PropertyView<ElementType, true>(), _accessor{}, _size{0} {}
292
300 : PropertyView<ElementType, true>(status), _accessor{}, _size{0} {
301 CESIUM_ASSERT(
303 "An empty property view should not be constructed with a valid status");
304 }
305
316 const ClassProperty& classProperty,
317 int64_t size) noexcept
318 : PropertyView<ElementType, true>(classProperty), _accessor{}, _size{0} {
319 if (this->_status != PropertyAttributePropertyViewStatus::Valid) {
320 // Don't override the status / size if something is wrong with the class
321 // property's definition.
322 return;
323 }
324
325 if (!classProperty.defaultProperty) {
326 // This constructor should only be called if the class property *has* a
327 // default value. But in the case that it does not, this property view
328 // becomes invalid.
329 this->_status =
331 return;
332 }
333
334 this->_status =
336 this->_size = size;
337 }
338
349 const PropertyAttributeProperty& property,
350 const ClassProperty& classProperty,
352 : PropertyView<ElementType, true>(classProperty, property),
353 _accessor{accessorView},
354 _size{
356 ? accessorView.size()
357 : 0} {}
358
374 std::optional<NormalizedType> get(int64_t index) const noexcept {
375 if (this->_status ==
377 return this->defaultValue();
378 }
379
380 ElementType value = getRaw(index);
381
382 if (value == this->noData()) {
383 return this->defaultValue();
384 }
385
389 this->offset(),
390 this->scale());
391 }
392
394 constexpr glm::length_t N = ElementType::length();
395 using T = typename ElementType::value_type;
396 using NormalizedT = typename NormalizedType::value_type;
398 normalize<N, T>(value),
399 this->offset(),
400 this->scale());
401 }
402
404 constexpr glm::length_t N = ElementType::length();
405 using T = typename ElementType::value_type;
406 using NormalizedT = typename NormalizedType::value_type;
408 normalize<N, T>(value),
409 this->offset(),
410 this->scale());
411 }
412 }
413
424 ElementType getRaw(int64_t index) const noexcept {
425 CESIUM_ASSERT(
427 "Check the status() first to make sure view is valid");
428 CESIUM_ASSERT(
429 size() > 0 &&
430 "Check the size() of the view to make sure it's not empty");
431 CESIUM_ASSERT(index >= 0 && "index must be non-negative");
432 CESIUM_ASSERT(index < size() && "index must be less than size");
433
434 return _accessor[index];
435 }
436
444 int64_t size() const noexcept { return _size; }
445
450 const AccessorView<ElementType>& accessorView() const { return _accessor; }
451
452private:
454 int64_t _size;
455};
456
457} // 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....