cesium-native  0.41.0
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 <variant>
10 
11 namespace CesiumGltf {
17  int64_t operator()(std::monostate) { return 0; }
18 
19  template <typename T> int64_t operator()(const AccessorView<T>& value) {
20  return value.size();
21  }
22 };
23 
29  AccessorViewStatus operator()(std::monostate) {
31  }
32 
33  template <typename T>
34  AccessorViewStatus operator()(const AccessorView<T>& value) {
35  return value.status();
36  }
37 };
38 
43 
50 getPositionAccessorView(const Model& model, const MeshPrimitive& primitive);
51 
56 
63 getNormalAccessorView(const Model& model, const MeshPrimitive& primitive);
64 
68 typedef std::variant<
76 
83  const Model& model,
84  const MeshPrimitive& primitive,
85  int32_t featureIdAttributeIndex);
86 
94  const Model& model,
95  const Node& node,
96  int32_t featureIdAttributeIndex);
97 
107  int64_t operator()(const AccessorView<float>& value) {
108  if (index < 0 || index >= value.size()) {
109  return -1;
110  }
111  return static_cast<int64_t>(glm::round(value[index]));
112  }
113 
114  template <typename T> int64_t operator()(const AccessorView<T>& value) {
115  if (index < 0 || index >= value.size()) {
116  return -1;
117  }
118  return static_cast<int64_t>(value[index]);
119  }
120 
121  int64_t index;
122 };
123 
129 typedef std::variant<
130  std::monostate,
135 
142 getIndexAccessorView(const Model& model, const MeshPrimitive& primitive);
143 
155  std::array<int64_t, 3> operator()(std::monostate) {
156  int64_t firstVertex = faceIndex;
157  int64_t numFaces = 0;
158 
159  switch (primitiveMode) {
160  case MeshPrimitive::Mode::TRIANGLE_STRIP:
161  numFaces = vertexCount - 2;
162  break;
163  case MeshPrimitive::Mode::TRIANGLE_FAN:
164  numFaces = vertexCount - 2;
165  firstVertex++;
166  break;
167  case MeshPrimitive::Mode::TRIANGLES:
168  numFaces = vertexCount / 3;
169  firstVertex *= 3;
170  break;
171  default:
172  // Unsupported primitive mode.
173  return {-1, -1, -1};
174  }
175 
176  if (faceIndex < 0 || faceIndex >= numFaces) {
177  return {-1, -1, -1};
178  }
179 
180  std::array<int64_t, 3> result;
181 
182  if (primitiveMode == MeshPrimitive::Mode::TRIANGLE_FAN) {
183  result[0] = 0;
184  result[1] = firstVertex < vertexCount ? firstVertex : -1;
185  result[2] = firstVertex + 1 < vertexCount ? firstVertex + 1 : -1;
186  } else {
187  for (int64_t i = 0; i < 3; i++) {
188  int64_t vertexIndex = firstVertex + i;
189  result[i] = vertexIndex < vertexCount ? vertexIndex : -1;
190  }
191  }
192 
193  return result;
194  }
195 
196  template <typename T>
197  std::array<int64_t, 3> operator()(const AccessorView<T>& value) {
198  int64_t firstIndex = faceIndex;
199  int64_t numFaces = 0;
200 
201  switch (primitiveMode) {
202  case MeshPrimitive::Mode::TRIANGLE_STRIP:
203  numFaces = value.size() - 2;
204  break;
205  case MeshPrimitive::Mode::TRIANGLE_FAN:
206  numFaces = value.size() - 2;
207  firstIndex++;
208  break;
209  case MeshPrimitive::Mode::TRIANGLES:
210  numFaces = value.size() / 3;
211  firstIndex *= 3;
212  break;
213  default:
214  // Unsupported primitive mode.
215  return {-1, -1, -1};
216  }
217 
218  if (faceIndex < 0 || faceIndex >= numFaces) {
219  return {-1, -1, -1};
220  }
221 
222  std::array<int64_t, 3> result;
223 
224  if (primitiveMode == MeshPrimitive::Mode::TRIANGLE_FAN) {
225  result[0] = value[0];
226  result[1] = firstIndex < value.size() ? value[firstIndex] : -1;
227  result[2] = firstIndex + 1 < value.size() ? value[firstIndex + 1] : -1;
228  } else {
229  for (int64_t i = 0; i < 3; i++) {
230  int64_t index = firstIndex + i;
231  result[i] = index < value.size() ? value[index] : -1;
232  }
233  }
234 
235  return result;
236  }
237 
238  int64_t faceIndex;
239  int64_t vertexCount;
240  int32_t primitiveMode;
241 }; // namespace CesiumGltf
242 
252  int64_t operator()(std::monostate) { return -1; }
253 
254  template <typename T>
255  int64_t operator()(const CesiumGltf::AccessorView<T>& value) {
256  if (index < 0 || index >= value.size()) {
257  return -1;
258  }
259 
260  return value[index];
261  }
262 
263  int64_t index;
264 };
265 
269 typedef std::variant<
274 
281  const Model& model,
282  const MeshPrimitive& primitive,
283  int32_t textureCoordinateSetIndex);
284 
293  std::optional<glm::dvec2>
294  operator()(const AccessorView<AccessorTypes::VEC2<float>>& value) {
295  if (index < 0 || index >= value.size()) {
296  return std::nullopt;
297  }
298 
299  return glm::dvec2(value[index].value[0], value[index].value[1]);
300  }
301 
302  template <typename T>
303  std::optional<glm::dvec2>
304  operator()(const AccessorView<AccessorTypes::VEC2<T>>& value) {
305  if (index < 0 || index >= value.size()) {
306  return std::nullopt;
307  }
308 
309  double u = static_cast<double>(value[index].value[0]);
310  double v = static_cast<double>(value[index].value[1]);
311 
312  // TODO: do normalization logic in accessor view?
313  u /= std::numeric_limits<T>::max();
314  v /= std::numeric_limits<T>::max();
315 
316  return glm::dvec2(u, v);
317  }
318 
319  int64_t index;
320 };
321 
326 typedef std::variant<
333 
335 getQuaternionAccessorView(const Model& model, const Accessor* accessor);
336 
338 getQuaternionAccessorView(const Model& model, int32_t accessorIndex);
339 } // namespace CesiumGltf
A view on the data of one accessor of a glTF asset.
Definition: AccessorView.h:80
int64_t size() const noexcept
Returns the size (number of elements) of this accessor.
Definition: AccessorView.h:192
AccessorViewStatus status() const noexcept
Gets the status of this accessor view.
Definition: AccessorView.h:200
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
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.
Definition: AccessorView.h:18
@ 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
PositionAccessorType getPositionAccessorView(const Model &model, const MeshPrimitive &primitive)
A 2D vector element for an AccessorView.
Definition: AccessorView.h:299
A typed view into a buffer view that contains raw binary data.
Definition: Accessor.h:12
Visitor that retrieves the count of elements in the given accessor type as an int64_t.
Geometry to be rendered with the given material.
Definition: MeshPrimitive.h:18
The root object for a glTF asset.
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...