cesium-native 0.61.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
8#include <array>
9#include <variant>
10
11namespace CesiumGltf {
19 int64_t operator()(std::monostate) { return 0; }
20
22 template <typename T> int64_t operator()(const AccessorView<T>& value) {
23 return value.size();
24 }
25};
26
38
41 template <typename T>
43 return value.status();
44 }
45};
46
51
58getPositionAccessorView(const Model& model, const MeshPrimitive& primitive);
59
64
71getNormalAccessorView(const Model& model, const MeshPrimitive& primitive);
72
76typedef std::variant<
84
91 const Model& model,
92 const MeshPrimitive& primitive,
93 int32_t featureIdAttributeIndex);
94
102 const Model& model,
103 const Node& node,
104 int32_t featureIdAttributeIndex);
105
118 int64_t operator()(const AccessorView<float>& value) {
119 if (index < 0 || index >= value.size()) {
120 return -1;
121 }
122 return static_cast<int64_t>(glm::round(value[index]));
123 }
124
126 template <typename T> int64_t operator()(const AccessorView<T>& value) {
127 if (index < 0 || index >= value.size()) {
128 return -1;
129 }
130 return static_cast<int64_t>(value[index]);
131 }
132
134 int64_t index;
135};
136
142typedef std::variant<
143 std::monostate,
148
155getIndexAccessorView(const Model& model, const MeshPrimitive& primitive);
156
160IndexAccessorType getIndexAccessorView(const Model& model, int32_t index);
161
171 int64_t operator()(std::monostate) { return 0; }
172
176 template <typename T> int64_t operator()(const AccessorView<T>& value) {
177 return value.size();
178 }
179};
180
190 int64_t operator()(std::monostate) { return -1; }
191
195 template <typename T> int64_t operator()(const AccessorView<T>& /*value*/) {
196 return static_cast<int64_t>(std::numeric_limits<T>::max());
197 }
198};
199
213 std::array<int64_t, 3> operator()(std::monostate) {
214 int64_t firstVertex = faceIndex;
215 int64_t numFaces = 0;
216
217 switch (primitiveMode) {
219 numFaces = vertexCount - 2;
220 break;
222 numFaces = vertexCount - 2;
223 firstVertex++;
224 break;
226 numFaces = vertexCount / 3;
227 firstVertex *= 3;
228 break;
229 default:
230 // Unsupported primitive mode.
231 return {-1, -1, -1};
232 }
233
234 if (faceIndex < 0 || faceIndex >= numFaces) {
235 return {-1, -1, -1};
236 }
237
238 std::array<int64_t, 3> result;
239
241 result[0] = 0;
242 result[1] = firstVertex < vertexCount ? firstVertex : -1;
243 result[2] = firstVertex + 1 < vertexCount ? firstVertex + 1 : -1;
244 } else {
245 for (int64_t i = 0; i < 3; i++) {
246 int64_t vertexIndex = firstVertex + i;
247 result[static_cast<size_t>(i)] =
248 vertexIndex < vertexCount ? vertexIndex : -1;
249 }
250 }
251
252 return result;
253 }
254
258 template <typename T>
259 std::array<int64_t, 3> operator()(const AccessorView<T>& value) {
260 int64_t firstIndex = faceIndex;
261 int64_t numFaces = 0;
262
263 switch (primitiveMode) {
265 numFaces = value.size() - 2;
266 break;
268 numFaces = value.size() - 2;
269 firstIndex++;
270 break;
272 numFaces = value.size() / 3;
273 firstIndex *= 3;
274 break;
275 default:
276 // Unsupported primitive mode.
277 return {-1, -1, -1};
278 }
279
280 if (faceIndex < 0 || faceIndex >= numFaces) {
281 return {-1, -1, -1};
282 }
283
284 std::array<int64_t, 3> result;
285
287 result[0] = value[0];
288 result[1] = firstIndex < value.size()
289 ? static_cast<int64_t>(value[firstIndex])
290 : -1;
291 result[2] = firstIndex + 1 < value.size()
292 ? static_cast<int64_t>(value[firstIndex + 1])
293 : -1;
294 } else {
295 for (int64_t i = 0; i < 3; i++) {
296 int64_t index = firstIndex + i;
297 result[static_cast<size_t>(i)] =
298 index < value.size() ? static_cast<int64_t>(value[index]) : -1;
299 }
300 }
301
302 return result;
303 }
304
306 int64_t faceIndex;
308 int64_t vertexCount;
311}; // namespace CesiumGltf
312
324 int64_t operator()(std::monostate) { return -1; }
325
328 template <typename T>
330 if (index < 0 || index >= value.size()) {
331 return -1;
332 }
333
334 return value[index];
335 }
336
338 int64_t index;
339};
340
344typedef std::variant<
349
356 const Model& model,
357 const MeshPrimitive& primitive,
358 int32_t textureCoordinateSetIndex);
359
373 std::optional<glm::dvec2>
375 if (index < 0 || index >= value.size()) {
376 return std::nullopt;
377 }
378
379 return glm::dvec2(value[index].value[0], value[index].value[1]);
380 }
381
388 template <typename T>
389 std::optional<glm::dvec2>
391 if (index < 0 || index >= value.size()) {
392 return std::nullopt;
393 }
394
395 double u = static_cast<double>(value[index].value[0]);
396 double v = static_cast<double>(value[index].value[1]);
397
398 // TODO: do normalization logic in accessor view?
399 u /= std::numeric_limits<T>::max();
400 v /= std::numeric_limits<T>::max();
401
402 return glm::dvec2(u, v);
403 }
404
406 int64_t index;
407};
408
413typedef std::variant<
420
432getQuaternionAccessorView(const Model& model, const Accessor* accessor);
433
446getQuaternionAccessorView(const Model& model, int32_t accessorIndex);
447} // 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.
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
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 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....