cesium-native 0.44.2
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
170 std::array<int64_t, 3> operator()(std::monostate) {
171 int64_t firstVertex = faceIndex;
172 int64_t numFaces = 0;
173
174 switch (primitiveMode) {
176 numFaces = vertexCount - 2;
177 break;
179 numFaces = vertexCount - 2;
180 firstVertex++;
181 break;
183 numFaces = vertexCount / 3;
184 firstVertex *= 3;
185 break;
186 default:
187 // Unsupported primitive mode.
188 return {-1, -1, -1};
189 }
190
191 if (faceIndex < 0 || faceIndex >= numFaces) {
192 return {-1, -1, -1};
193 }
194
195 std::array<int64_t, 3> result;
196
198 result[0] = 0;
199 result[1] = firstVertex < vertexCount ? firstVertex : -1;
200 result[2] = firstVertex + 1 < vertexCount ? firstVertex + 1 : -1;
201 } else {
202 for (int64_t i = 0; i < 3; i++) {
203 int64_t vertexIndex = firstVertex + i;
204 result[i] = vertexIndex < vertexCount ? vertexIndex : -1;
205 }
206 }
207
208 return result;
209 }
210
214 template <typename T>
215 std::array<int64_t, 3> operator()(const AccessorView<T>& value) {
216 int64_t firstIndex = faceIndex;
217 int64_t numFaces = 0;
218
219 switch (primitiveMode) {
221 numFaces = value.size() - 2;
222 break;
224 numFaces = value.size() - 2;
225 firstIndex++;
226 break;
228 numFaces = value.size() / 3;
229 firstIndex *= 3;
230 break;
231 default:
232 // Unsupported primitive mode.
233 return {-1, -1, -1};
234 }
235
236 if (faceIndex < 0 || faceIndex >= numFaces) {
237 return {-1, -1, -1};
238 }
239
240 std::array<int64_t, 3> result;
241
243 result[0] = value[0];
244 result[1] = firstIndex < value.size() ? value[firstIndex] : -1;
245 result[2] = firstIndex + 1 < value.size() ? value[firstIndex + 1] : -1;
246 } else {
247 for (int64_t i = 0; i < 3; i++) {
248 int64_t index = firstIndex + i;
249 result[i] = index < value.size() ? value[index] : -1;
250 }
251 }
252
253 return result;
254 }
255
257 int64_t faceIndex;
259 int64_t vertexCount;
262}; // namespace CesiumGltf
263
275 int64_t operator()(std::monostate) { return -1; }
276
279 template <typename T>
281 if (index < 0 || index >= value.size()) {
282 return -1;
283 }
284
285 return value[index];
286 }
287
289 int64_t index;
290};
291
295typedef std::variant<
300
307 const Model& model,
308 const MeshPrimitive& primitive,
309 int32_t textureCoordinateSetIndex);
310
324 std::optional<glm::dvec2>
326 if (index < 0 || index >= value.size()) {
327 return std::nullopt;
328 }
329
330 return glm::dvec2(value[index].value[0], value[index].value[1]);
331 }
332
339 template <typename T>
340 std::optional<glm::dvec2>
342 if (index < 0 || index >= value.size()) {
343 return std::nullopt;
344 }
345
346 double u = static_cast<double>(value[index].value[0]);
347 double v = static_cast<double>(value[index].value[1]);
348
349 // TODO: do normalization logic in accessor view?
350 u /= std::numeric_limits<T>::max();
351 v /= std::numeric_limits<T>::max();
352
353 return glm::dvec2(u, v);
354 }
355
357 int64_t index;
358};
359
364typedef std::variant<
371
383getQuaternionAccessorView(const Model& model, const Accessor* accessor);
384
397getQuaternionAccessorView(const Model& model, int32_t accessorIndex);
398} // 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....