cesium-native 0.43.0
Loading...
Searching...
No Matches
AccessorUtility.h
1#pragma once
2
3#include "AccessorView.h"
4
5#include <CesiumGltf/MeshPrimitive.h>
6
7#include <glm/common.hpp>
8
9#include <array>
10#include <variant>
11
12namespace CesiumGltf {
20 int64_t operator()(std::monostate) { return 0; }
21
23 template <typename T> int64_t operator()(const AccessorView<T>& value) {
24 return value.size();
25 }
26};
27
39
42 template <typename T>
44 return value.status();
45 }
46};
47
52
59getPositionAccessorView(const Model& model, const MeshPrimitive& primitive);
60
65
72getNormalAccessorView(const Model& model, const MeshPrimitive& primitive);
73
77typedef std::variant<
85
92 const Model& model,
93 const MeshPrimitive& primitive,
94 int32_t featureIdAttributeIndex);
95
103 const Model& model,
104 const Node& node,
105 int32_t featureIdAttributeIndex);
106
119 int64_t operator()(const AccessorView<float>& value) {
120 if (index < 0 || index >= value.size()) {
121 return -1;
122 }
123 return static_cast<int64_t>(glm::round(value[index]));
124 }
125
127 template <typename T> int64_t operator()(const AccessorView<T>& value) {
128 if (index < 0 || index >= value.size()) {
129 return -1;
130 }
131 return static_cast<int64_t>(value[index]);
132 }
133
135 int64_t index;
136};
137
143typedef std::variant<
144 std::monostate,
149
156getIndexAccessorView(const Model& model, const MeshPrimitive& primitive);
157
171 std::array<int64_t, 3> operator()(std::monostate) {
172 int64_t firstVertex = faceIndex;
173 int64_t numFaces = 0;
174
175 switch (primitiveMode) {
177 numFaces = vertexCount - 2;
178 break;
180 numFaces = vertexCount - 2;
181 firstVertex++;
182 break;
184 numFaces = vertexCount / 3;
185 firstVertex *= 3;
186 break;
187 default:
188 // Unsupported primitive mode.
189 return {-1, -1, -1};
190 }
191
192 if (faceIndex < 0 || faceIndex >= numFaces) {
193 return {-1, -1, -1};
194 }
195
196 std::array<int64_t, 3> result;
197
199 result[0] = 0;
200 result[1] = firstVertex < vertexCount ? firstVertex : -1;
201 result[2] = firstVertex + 1 < vertexCount ? firstVertex + 1 : -1;
202 } else {
203 for (int64_t i = 0; i < 3; i++) {
204 int64_t vertexIndex = firstVertex + i;
205 result[i] = vertexIndex < vertexCount ? vertexIndex : -1;
206 }
207 }
208
209 return result;
210 }
211
215 template <typename T>
216 std::array<int64_t, 3> operator()(const AccessorView<T>& value) {
217 int64_t firstIndex = faceIndex;
218 int64_t numFaces = 0;
219
220 switch (primitiveMode) {
222 numFaces = value.size() - 2;
223 break;
225 numFaces = value.size() - 2;
226 firstIndex++;
227 break;
229 numFaces = value.size() / 3;
230 firstIndex *= 3;
231 break;
232 default:
233 // Unsupported primitive mode.
234 return {-1, -1, -1};
235 }
236
237 if (faceIndex < 0 || faceIndex >= numFaces) {
238 return {-1, -1, -1};
239 }
240
241 std::array<int64_t, 3> result;
242
244 result[0] = value[0];
245 result[1] = firstIndex < value.size() ? value[firstIndex] : -1;
246 result[2] = firstIndex + 1 < value.size() ? value[firstIndex + 1] : -1;
247 } else {
248 for (int64_t i = 0; i < 3; i++) {
249 int64_t index = firstIndex + i;
250 result[i] = index < value.size() ? value[index] : -1;
251 }
252 }
253
254 return result;
255 }
256
258 int64_t faceIndex;
260 int64_t vertexCount;
263}; // namespace CesiumGltf
264
276 int64_t operator()(std::monostate) { return -1; }
277
280 template <typename T>
282 if (index < 0 || index >= value.size()) {
283 return -1;
284 }
285
286 return value[index];
287 }
288
290 int64_t index;
291};
292
296typedef std::variant<
301
308 const Model& model,
309 const MeshPrimitive& primitive,
310 int32_t textureCoordinateSetIndex);
311
325 std::optional<glm::dvec2>
327 if (index < 0 || index >= value.size()) {
328 return std::nullopt;
329 }
330
331 return glm::dvec2(value[index].value[0], value[index].value[1]);
332 }
333
340 template <typename T>
341 std::optional<glm::dvec2>
343 if (index < 0 || index >= value.size()) {
344 return std::nullopt;
345 }
346
347 double u = static_cast<double>(value[index].value[0]);
348 double v = static_cast<double>(value[index].value[1]);
349
350 // TODO: do normalization logic in accessor view?
351 u /= std::numeric_limits<T>::max();
352 v /= std::numeric_limits<T>::max();
353
354 return glm::dvec2(u, v);
355 }
356
358 int64_t index;
359};
360
365typedef std::variant<
372
384getQuaternionAccessorView(const Model& model, const Accessor* accessor);
385
398getQuaternionAccessorView(const Model& model, int32_t accessorIndex);
399} // 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.
AccessorView< AccessorTypes::VEC3< float > > NormalAccessorType
std::variant< AccessorView< int8_t >, AccessorView< uint8_t >, AccessorView< int16_t >, AccessorView< uint16_t >, AccessorView< uint32_t >, AccessorView< float > > FeatureIdAccessorType
std::variant< std::monostate, AccessorView< uint8_t >, AccessorView< uint16_t >, AccessorView< uint32_t > > IndexAccessorType
std::variant< AccessorView< AccessorTypes::VEC4< uint8_t > >, AccessorView< AccessorTypes::VEC4< int8_t > >, AccessorView< AccessorTypes::VEC4< uint16_t > >, AccessorView< AccessorTypes::VEC4< int16_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)
FeatureIdAccessorType getFeatureIdAccessorView(const Model &model, const MeshPrimitive &primitive, int32_t featureIdAttributeIndex)
TexCoordAccessorType getTexCoordAccessorView(const Model &model, const MeshPrimitive &primitive, int32_t textureCoordinateSetIndex)
NormalAccessorType getNormalAccessorView(const Model &model, const MeshPrimitive &primitive)
AccessorViewStatus
Indicates the status of an accessor view.
@ InvalidAccessorIndex
The accessor index does not refer to a valid accessor.
std::variant< AccessorView< AccessorTypes::VEC2< uint8_t > >, AccessorView< AccessorTypes::VEC2< uint16_t > >, AccessorView< AccessorTypes::VEC2< float > > > TexCoordAccessorType
AccessorView< AccessorTypes::VEC3< float > > PositionAccessorType
QuaternionAccessorType getQuaternionAccessorView(const Model &model, const Accessor *accessor)
Obtains a QuaternionAccessorType from the given Accessor on the given Model.
PositionAccessorType getPositionAccessorView(const Model &model, const MeshPrimitive &primitive)
A 2D 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 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.
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.
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.
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.
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 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.
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...
std::optional< glm::dvec2 > operator()(const AccessorView< AccessorTypes::VEC2< float > > &value)
Attempts to obtain a glm::dvec2 at the given index from an accessor over a vec2 of floats....