cesium-native 0.62.0
Loading...
Searching...
No Matches
AccessorUtility.h
1#pragma once
2
3#include <CesiumGltf/AccessorView.h>
4#include <CesiumGltf/MeshPrimitive.h>
5
6#include <glm/common.hpp>
7#include <glm/ext/quaternion_double.hpp>
8
9#include <array>
10#include <type_traits>
11#include <variant>
12
13namespace CesiumGltf {
14
15namespace CesiumImpl {
16template <typename T> double denormalize(T value) {
17 if constexpr (std::is_floating_point_v<T>) {
18 return double(value);
19 } else if constexpr (std::is_signed_v<T>) {
20 return glm::max(double(value) / std::numeric_limits<T>::max(), -1.0);
21 } else {
22 return double(value) / std::numeric_limits<T>::max();
23 }
24}
25} // namespace CesiumImpl
26
34 int64_t operator()(std::monostate) { return 0; }
35
37 template <typename T> int64_t operator()(const AccessorView<T>& value) {
38 return value.size();
39 }
40};
41
53
56 template <typename T>
58 return value.status();
59 }
60};
61
65typedef std::variant<
66 AccessorView<AccessorTypes::VEC3<int8_t>>,
67 AccessorView<AccessorTypes::VEC3<uint8_t>>,
68 AccessorView<AccessorTypes::VEC3<int16_t>>,
69 AccessorView<AccessorTypes::VEC3<uint16_t>>,
70 AccessorView<AccessorTypes::VEC3<float>>>
72
84getPositionAccessorView(const Model& model, const MeshPrimitive& primitive);
85
100 template <typename T>
101 std::optional<glm::dvec3>
103 if (index < 0 || index >= value.size()) {
104 return std::nullopt;
105 }
106
107 if (value.normalized()) {
108 return glm::dvec3(
109 CesiumImpl::denormalize(value[index].value[0]),
110 CesiumImpl::denormalize(value[index].value[1]),
111 CesiumImpl::denormalize(value[index].value[2]));
112 } else {
113 return glm::dvec3(
114 value[index].value[0],
115 value[index].value[1],
116 value[index].value[2]);
117 }
118 }
119
121 int64_t index;
122};
123
127typedef std::variant<
132
144getNormalAccessorView(const Model& model, const MeshPrimitive& primitive);
145
160 template <typename T>
161 std::optional<glm::dvec3>
163 if (index < 0 || index >= value.size()) {
164 return std::nullopt;
165 }
166
167 if (value.normalized()) {
168 return glm::dvec3(
169 CesiumImpl::denormalize(value[index].value[0]),
170 CesiumImpl::denormalize(value[index].value[1]),
171 CesiumImpl::denormalize(value[index].value[2]));
172 } else {
173 return glm::dvec3(
174 value[index].value[0],
175 value[index].value[1],
176 value[index].value[2]);
177 }
178 }
179
181 int64_t index;
182};
183
188typedef std::variant<
196
210 const Model& model,
211 const MeshPrimitive& primitive,
212 int32_t featureIdSetIndex);
213
228 const Model& model,
229 const Node& node,
230 int32_t featureIdSetIndex);
231
244 int64_t operator()(const AccessorView<float>& value) {
245 if (index < 0 || index >= value.size()) {
246 return -1;
247 }
248 return static_cast<int64_t>(glm::round(value[index]));
249 }
250
252 template <typename T> int64_t operator()(const AccessorView<T>& value) {
253 if (index < 0 || index >= value.size()) {
254 return -1;
255 }
256 return static_cast<int64_t>(value[index]);
257 }
258
260 int64_t index;
261};
262
268typedef std::variant<
269 std::monostate,
274
286getIndexAccessorView(const Model& model, const MeshPrimitive& primitive);
287
296IndexAccessorType getIndexAccessorView(const Model& model, int32_t index);
297
302getIndexAccessorView(const Model& model, const Accessor& accessor);
303
313 int64_t operator()(std::monostate) { return 0; }
314
318 template <typename T> int64_t operator()(const AccessorView<T>& value) {
319 return value.size();
320 }
321};
322
332 int64_t operator()(std::monostate) { return -1; }
333
337 template <typename T> int64_t operator()(const AccessorView<T>& /*value*/) {
338 return static_cast<int64_t>(std::numeric_limits<T>::max());
339 }
340};
341
355 std::array<int64_t, 3> operator()(std::monostate) {
356 int64_t firstVertex = faceIndex;
357 int64_t numFaces = 0;
358
359 switch (primitiveMode) {
361 numFaces = vertexCount - 2;
362 break;
364 numFaces = vertexCount - 2;
365 firstVertex++;
366 break;
368 numFaces = vertexCount / 3;
369 firstVertex *= 3;
370 break;
371 default:
372 // Unsupported primitive mode.
373 return {-1, -1, -1};
374 }
375
376 if (faceIndex < 0 || faceIndex >= numFaces) {
377 return {-1, -1, -1};
378 }
379
380 std::array<int64_t, 3> result;
381
383 result[0] = 0;
384 result[1] = firstVertex < vertexCount ? firstVertex : -1;
385 result[2] = firstVertex + 1 < vertexCount ? firstVertex + 1 : -1;
386 } else {
387 for (int64_t i = 0; i < 3; i++) {
388 int64_t vertexIndex = firstVertex + i;
389 result[static_cast<size_t>(i)] =
390 vertexIndex < vertexCount ? vertexIndex : -1;
391 }
392 }
393
394 return result;
395 }
396
400 template <typename T>
401 std::array<int64_t, 3> operator()(const AccessorView<T>& value) {
402 int64_t firstIndex = faceIndex;
403 int64_t numFaces = 0;
404
405 switch (primitiveMode) {
407 numFaces = value.size() - 2;
408 break;
410 numFaces = value.size() - 2;
411 firstIndex++;
412 break;
414 numFaces = value.size() / 3;
415 firstIndex *= 3;
416 break;
417 default:
418 // Unsupported primitive mode.
419 return {-1, -1, -1};
420 }
421
422 if (faceIndex < 0 || faceIndex >= numFaces) {
423 return {-1, -1, -1};
424 }
425
426 std::array<int64_t, 3> result;
427
429 result[0] = value[0];
430 result[1] = firstIndex < value.size()
431 ? static_cast<int64_t>(value[firstIndex])
432 : -1;
433 result[2] = firstIndex + 1 < value.size()
434 ? static_cast<int64_t>(value[firstIndex + 1])
435 : -1;
436 } else {
437 for (int64_t i = 0; i < 3; i++) {
438 int64_t index = firstIndex + i;
439 result[static_cast<size_t>(i)] =
440 index < value.size() ? static_cast<int64_t>(value[index]) : -1;
441 }
442 }
443
444 return result;
445 }
446
448 int64_t faceIndex;
450 int64_t vertexCount;
453}; // namespace CesiumGltf
454
466 int64_t operator()(std::monostate) { return -1; }
467
470 template <typename T>
472 if (index < 0 || index >= value.size()) {
473 return -1;
474 }
475
476 return value[index];
477 }
478
480 int64_t index;
481};
482
487typedef std::variant<
494
508 const Model& model,
509 const MeshPrimitive& primitive,
510 int32_t textureCoordinateSetIndex);
511
526 template <typename T>
527 std::optional<glm::dvec2>
529 if (index < 0 || index >= value.size()) {
530 return std::nullopt;
531 }
532
533 if (value.normalized()) {
534 return glm::dvec2(
535 CesiumImpl::denormalize(value[index].value[0]),
536 CesiumImpl::denormalize(value[index].value[1]));
537 } else {
538 return glm::dvec2(value[index].value[0], value[index].value[1]);
539 }
540 }
541
543 int64_t index;
544};
545
550typedef std::variant<
557
569getQuaternionAccessorView(const Model& model, const Accessor& accessor);
570
583getQuaternionAccessorView(const Model& model, int32_t accessorIndex);
584
596 template <typename T>
597 std::optional<glm::dquat>
599 if (index < 0 || index >= value.size()) {
600 return std::nullopt;
601 }
602
603 if (value.normalized()) {
604 // glm quat constructor is w,x,y,z
605 return glm::dquat(
606 CesiumImpl::denormalize(value[index].value[3]),
607 CesiumImpl::denormalize(value[index].value[0]),
608 CesiumImpl::denormalize(value[index].value[1]),
609 CesiumImpl::denormalize(value[index].value[2]));
610 } else {
611 // glm quat constructor is w,x,y,z
612 return glm::dquat(
613 value[index].value[3],
614 value[index].value[0],
615 value[index].value[1],
616 value[index].value[2]);
617 }
618 }
619
621 int64_t index;
622};
623
627typedef std::variant<
635
648 const Model& model,
649 const MeshPrimitive& primitive,
650 int32_t colorSetIndex);
651
664 template <typename T>
665 std::optional<glm::dvec4>
667 if (index < 0 || index >= value.size()) {
668 return std::nullopt;
669 }
670
671 if (value.normalized()) {
672 return glm::dvec4(
673 CesiumImpl::denormalize(value[index].value[0]),
674 CesiumImpl::denormalize(value[index].value[1]),
675 CesiumImpl::denormalize(value[index].value[2]),
676 1.0);
677 } else {
678 return glm::dvec4(
679 value[index].value[0],
680 value[index].value[1],
681 value[index].value[2],
682 1.0);
683 }
684 }
685
692 template <typename T>
693 std::optional<glm::dvec4>
695 if (index < 0 || index >= value.size()) {
696 return std::nullopt;
697 }
698
699 if (value.normalized()) {
700 return glm::dvec4(
701 CesiumImpl::denormalize(value[index].value[0]),
702 CesiumImpl::denormalize(value[index].value[1]),
703 CesiumImpl::denormalize(value[index].value[2]),
704 CesiumImpl::denormalize(value[index].value[3]));
705 } else {
706 return glm::dvec4(
707 value[index].value[0],
708 value[index].value[1],
709 value[index].value[2],
710 value[index].value[3]);
711 }
712 }
713
715 int64_t index;
716};
717
718} // namespace CesiumGltf
A view on the data of one accessor of a glTF asset.
int64_t size() const noexcept
Returns the size (number of elements) of this accessor.
AccessorViewStatus status() const noexcept
Gets the status of this accessor view.
Classes for working with glTF models.
std::variant< AccessorView< AccessorTypes::VEC3< int8_t > >, AccessorView< AccessorTypes::VEC3< uint8_t > >, AccessorView< AccessorTypes::VEC3< int16_t > >, AccessorView< AccessorTypes::VEC3< uint16_t > >, AccessorView< AccessorTypes::VEC3< float > > > PositionAccessorType
Type definition for all kinds of position (POSITION) accessors.
ColorAccessorType getColorAccessorView(const Model &model, const MeshPrimitive &primitive, int32_t colorSetIndex)
Retrieves an accessor view for the specified color attribute from the given glTF primitive and model....
std::variant< AccessorView< int8_t >, AccessorView< uint8_t >, AccessorView< int16_t >, AccessorView< uint16_t >, AccessorView< uint32_t >, AccessorView< float > > FeatureIdAccessorType
Type definition for all kinds of feature ID (_FEATURE_ID_n) attribute accessors.
std::variant< std::monostate, AccessorView< uint8_t >, AccessorView< uint16_t >, AccessorView< uint32_t > > IndexAccessorType
Type definition for all kinds of index accessors. std::monostate indicates a nonexistent accessor,...
std::variant< AccessorView< AccessorTypes::VEC4< int8_t > >, AccessorView< AccessorTypes::VEC4< uint8_t > >, AccessorView< AccessorTypes::VEC4< int16_t > >, AccessorView< AccessorTypes::VEC4< uint16_t > >, AccessorView< AccessorTypes::VEC4< float > > > QuaternionAccessorType
Type definition for quaternion accessors, as used in ExtMeshGpuInstancing rotations and animation sam...
IndexAccessorType getIndexAccessorView(const Model &model, const MeshPrimitive &primitive)
Retrieves an accessor view for the indices of the given glTF primitive from the model....
QuaternionAccessorType getQuaternionAccessorView(const Model &model, const Accessor &accessor)
Obtains a QuaternionAccessorType from the given Accessor on the given Model.
FeatureIdAccessorType getFeatureIdAccessorView(const Model &model, const MeshPrimitive &primitive, int32_t featureIdSetIndex)
Retrieves an accessor view for the specified feature ID attribute from the given glTF primitive and m...
TexCoordAccessorType getTexCoordAccessorView(const Model &model, const MeshPrimitive &primitive, int32_t textureCoordinateSetIndex)
Retrieves an accessor view for the specified texture coordinate set from the given glTF primitive and...
std::variant< AccessorView< AccessorTypes::VEC3< uint8_t > >, AccessorView< AccessorTypes::VEC3< uint16_t > >, AccessorView< AccessorTypes::VEC3< float > >, AccessorView< AccessorTypes::VEC4< uint8_t > >, AccessorView< AccessorTypes::VEC4< uint16_t > >, AccessorView< AccessorTypes::VEC4< float > > > ColorAccessorType
Type definition for all kinds of color (COLOR_n) accessors.
NormalAccessorType getNormalAccessorView(const Model &model, const MeshPrimitive &primitive)
Retrieves an accessor view for the normal attribute from the given glTF primitive and model....
AccessorViewStatus
Indicates the status of an accessor view.
@ InvalidAccessorIndex
The accessor index does not refer to a valid accessor.
std::variant< AccessorView< AccessorTypes::VEC3< int8_t > >, AccessorView< AccessorTypes::VEC3< int16_t > >, AccessorView< AccessorTypes::VEC3< float > > > NormalAccessorType
Type definition for all kinds of normal (NORMAL) accessors.
std::variant< AccessorView< AccessorTypes::VEC2< int8_t > >, AccessorView< AccessorTypes::VEC2< uint8_t > >, AccessorView< AccessorTypes::VEC2< int16_t > >, AccessorView< AccessorTypes::VEC2< uint16_t > >, AccessorView< AccessorTypes::VEC2< float > > > TexCoordAccessorType
Type definition for all kinds of texture coordinate (TEXCOORD_n) accessors.
PositionAccessorType getPositionAccessorView(const Model &model, const MeshPrimitive &primitive)
Retrieves an accessor view for the position attribute from the given glTF primitive and model....
A 2D vector element for an AccessorView.
A 3D vector element for an AccessorView.
A 4D vector element for an AccessorView.
This class is not meant to be instantiated directly. Use Accessor instead.
Definition Accessor.h:12
Visitor that retrieves the color from the given accessor type as a glm::dvec4.
std::optional< glm::dvec4 > operator()(const AccessorView< AccessorTypes::VEC3< T > > &value)
Attempts to obtain a glm::dvec4 at the given index from an accessor over a vec3. The values will be c...
std::optional< glm::dvec4 > operator()(const AccessorView< AccessorTypes::VEC4< T > > &value)
Attempts to obtain a glm::dvec4 at the given index from an accessor over a vec4. The values will be c...
int64_t index
The index of the color to obtain.
Visitor that retrieves the count of elements in the given accessor type as an int64_t.
int64_t operator()(const AccessorView< T > &value)
Attempts to obtain an element count from an AccessorView.
int64_t operator()(std::monostate)
Attempts to obtain an element count from an empty accessor variant, resulting in 0.
Visitor that retrieves the feature ID from the given accessor type as an int64_t. This should be init...
int64_t operator()(const AccessorView< float > &value)
Attempts to obtain a feature ID from an AccessorView over float values, returning the float value rou...
int64_t operator()(const AccessorView< T > &value)
Attempts to obtain a feature ID from an AccessorView.
int64_t index
The index of the vertex whose feature ID is being queried.
Visitor that retrieves the vertex index from the given accessor type as an int64_t....
int64_t operator()(const CesiumGltf::AccessorView< T > &value)
Attempts to obtain a vertex index from an CesiumGltf::AccessorView.
int64_t index
The index of the vertex index within the accessor itself.
int64_t operator()(std::monostate)
Attempts to obtain a vertex index from an empty IndexAccessorType, resulting in -1.
Visitor that retrieves the vertex indices from the given accessor type corresponding to a given face ...
std::array< int64_t, 3 > operator()(const AccessorView< T > &value)
Attempts to obtain the indices for the given face from an AccessorView, using the view's size and con...
int64_t faceIndex
The index of the face to obtain indices for.
std::array< int64_t, 3 > operator()(std::monostate)
Attempts to obtain the indices for the given face from an empty accessor variant, using the vertexCou...
int64_t vertexCount
The total number of vertices in the data being accessed.
int32_t primitiveMode
The MeshPrimitive::Mode of the data being accessed.
Returns the maximum possible index value for the given IndexAccessorType.
int64_t operator()(const AccessorView< T > &)
Attempts to obtain a maximum index value from an AccessorView.
int64_t operator()(std::monostate)
Attempts to obtain a maximum index value from an empty IndexAccessorType, resulting in -1.
static constexpr int32_t TRIANGLES
TRIANGLES (4)
static constexpr int32_t TRIANGLE_FAN
TRIANGLE_FAN (6)
static constexpr int32_t TRIANGLE_STRIP
TRIANGLE_STRIP (5)
Geometry to be rendered with the given material.
This class is not meant to be instantiated directly. Use Model instead.
Definition Model.h:14
A node in the node hierarchy. When the node contains skin, all mesh.primitives MUST contain JOINTS_0 ...
Definition Node.h:23
Visitor that retrieves the normal from the given accessor type as a glm::dvec3.
std::optional< glm::dvec3 > operator()(const AccessorView< AccessorTypes::VEC3< T > > &value)
Attempts to obtain a glm::dvec3 at the given index from an accessor over a vec3. The values will be c...
int64_t index
The index of the normal to obtain.
Visitor that returns the number of indices contained in an IndexAccessorType variant.
int64_t operator()(std::monostate)
Attempts to obtain a number of indices from an empty IndexAccessorType, resulting in 0.
int64_t operator()(const AccessorView< T > &value)
Attempts to obtain a number of indices from an AccessorView.
Visitor that retrieves the position from the given accessor type as a glm::dvec3.
int64_t index
The index of the position to obtain.
std::optional< glm::dvec3 > operator()(const AccessorView< AccessorTypes::VEC3< T > > &value)
Attempts to obtain a glm::dvec3 at the given index from an accessor over a vec3. The values will be c...
Visitor that retrieves the quaternion from the given accessor type as a glm::dquat.
std::optional< glm::dquat > operator()(const AccessorView< AccessorTypes::VEC4< T > > &value)
Attempts to obtain a glm::dquat at the given index from an accessor over a vec4. The values will be c...
int64_t index
The index of the quaternion to obtain.
Visitor that retrieves the status from the given accessor. Returns an invalid status for a std::monos...
AccessorViewStatus operator()(std::monostate)
Attempts to obtain an AccessorViewStatus from an empty accessor variant, resulting in AccessorViewSta...
AccessorViewStatus operator()(const AccessorView< T > &value)
Attempts to obtain an AccessorViewStatus from an AccessorView.
Visitor that retrieves the texture coordinates from the given accessor type as a glm::dvec2....
int64_t index
The index of texcoords to obtain.
std::optional< glm::dvec2 > operator()(const AccessorView< AccessorTypes::VEC2< T > > &value)
Attempts to obtain a glm::dvec2 at the given index from an accessor over a vec2. The values will be c...