Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
accessor_utils.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#pragma once
9
12
13#include <fx/gltf.h>
14#include <gsl/span_ext>
15
16namespace momentum {
17
18inline int32_t getComponentSize(const fx::gltf::Accessor::ComponentType& componentType) {
19 switch (componentType) {
20 case fx::gltf::Accessor::ComponentType::Byte:
21 case fx::gltf::Accessor::ComponentType::UnsignedByte:
22 return 1;
23 case fx::gltf::Accessor::ComponentType::Short:
24 case fx::gltf::Accessor::ComponentType::UnsignedShort:
25 return 2;
26 // case fx::gltf::Accessor::ComponentType::Int:
27 case fx::gltf::Accessor::ComponentType::UnsignedInt:
28 case fx::gltf::Accessor::ComponentType::Float:
29 return 4;
30 default:
31 return -1;
32 }
33}
34
35inline int32_t getTypeSize(const fx::gltf::Accessor::Type& ty) {
36 switch (ty) {
37 case fx::gltf::Accessor::Type::Scalar:
38 return 1;
39 case fx::gltf::Accessor::Type::Vec2:
40 return 2;
41 case fx::gltf::Accessor::Type::Vec3:
42 return 3;
43 case fx::gltf::Accessor::Type::Vec4:
44 case fx::gltf::Accessor::Type::Mat2:
45 return 4;
46 case fx::gltf::Accessor::Type::Mat3:
47 return 9;
48 case fx::gltf::Accessor::Type::Mat4:
49 return 16;
50 default:
51 return -1;
52 }
53}
54
55template <typename T>
56std::vector<T> copyAccessorBuffer(const fx::gltf::Document& model, int32_t id) {
57 const auto& accessor = model.accessors[id];
58
59 const auto tsize = getTypeSize(accessor.type);
60 const auto csize = getComponentSize(accessor.componentType);
61 const auto elsize = tsize * csize;
62
63 if (elsize != sizeof(T)) {
64 MT_LOGW(
65 "[{}] Accessor type mismatch. Expect to read {} bytes, but got type of {} bytes with {} components",
66 __func__,
67 sizeof(T),
68 csize,
69 tsize);
70 return {};
71 }
72
73 const auto& view = model.bufferViews[accessor.bufferView];
74 const auto stride = view.byteStride == 0 ? elsize : view.byteStride;
75
76 const auto& buf = model.buffers[view.buffer];
77 auto bytes = std::as_bytes(std::span<const uint8_t>(buf.data));
78 bytes = bytes.subspan(view.byteOffset, view.byteLength);
79 bytes = bytes.subspan(accessor.byteOffset);
80
81 std::vector<T> r(accessor.count);
82 for (size_t i = 0; i < accessor.count; i++) {
83 std::memcpy(static_cast<void*>(&r[i]), &bytes[i * stride], elsize);
84 }
85
86 return r;
87}
88
89template <typename T>
90std::vector<T> copyAlignedAccessorBuffer(const fx::gltf::Document& model, int32_t id) {
91 const auto& accessor = model.accessors[id];
92 if (accessor.count == 0) {
93 MT_LOGW("Accessor count zero.");
94 return {};
95 }
96
97 const auto tsize = getTypeSize(accessor.type);
98 const auto csize = getComponentSize(accessor.componentType);
99 const auto elsize = tsize * csize;
100
101 if (elsize != sizeof(T)) {
102 MT_LOGW(
103 "[{}] Accessor type mismatch. Expect to read {} bytes, but got type of {} bytes with {} components",
104 __func__,
105 sizeof(T),
106 csize,
107 tsize);
108 return {};
109 }
110
111 const auto& view = model.bufferViews[accessor.bufferView];
112 const auto stride = view.byteStride == 0 ? elsize : view.byteStride;
113
114 const auto& buf = model.buffers[view.buffer];
115 auto bytes = std::as_bytes(std::span<const uint8_t>(buf.data));
116 bytes = bytes.subspan(view.byteOffset, view.byteLength);
117 bytes = bytes.subspan(accessor.byteOffset);
118
119 std::vector<T> r(accessor.count);
120 for (size_t i = 0; i < accessor.count; i++) {
121 std::memcpy(static_cast<void*>(&r[i]), &bytes[i * stride], elsize);
122 }
123
124 return r;
125}
126
127// template to set accessor
128template <typename T>
129void setAccessorType(fx::gltf::Accessor& /* accessor */) {
130 MT_THROW("Unsupported data type {}", typeid(T).name());
131}
132
133// create accessor buffer
134template <typename T>
136 fx::gltf::Document& model,
137 std::span<T> data,
138 const bool align = false,
139 const bool normalized = false) {
140 // gltf 2.0 allows multiple buffers, but only the first buffer will be stored in
141 // the binary file, all other ones need to be external files. Because of this we will
142 // only ever store data in the first buffer
143
144 // check if we have a buffer
145 if (model.buffers.empty()) {
146 model.buffers.resize(1);
147 }
148
149 // create buffer for data
150 const size_t bufferIdx = 0;
151 auto& buffer = model.buffers.front();
152
153 // copy data from input buffer to output buffer
154 const size_t bufferDataStart = buffer.data.size();
155 const auto elementSize = sizeof(T);
156 auto alignedElementSize = elementSize;
157 auto dataSize = data.size() * alignedElementSize;
158
159 // check for necessary alignment
160 if (align && alignedElementSize % 4 != 0) {
161 // element is not aligned, need to get bigger element size
162 alignedElementSize = ((elementSize / 4) + 1) * 4;
163 dataSize = data.size() * alignedElementSize;
164 buffer.data.resize(bufferDataStart + dataSize);
165 // need to go over each element one by one to copy
166 for (size_t e = 0; e < data.size(); e++) {
167 std::memcpy(&buffer.data[bufferDataStart + alignedElementSize * e], &data[e], elementSize);
168 }
169 buffer.byteLength = static_cast<uint32_t>(buffer.data.size());
170 } else {
171 // no alignment needed or already aligned, just copy data in
172 buffer.data.resize(bufferDataStart + dataSize);
173 std::memcpy(&buffer.data[bufferDataStart], data.data(), dataSize);
174 buffer.byteLength = static_cast<uint32_t>(buffer.data.size());
175 }
176
177 // create bufferview
178 const size_t bufferViewIdx = model.bufferViews.size();
179 model.bufferViews.emplace_back();
180 auto& bufferView = model.bufferViews.back();
181
182 bufferView.buffer = static_cast<int32_t>(bufferIdx);
183 bufferView.byteLength = static_cast<uint32_t>(dataSize);
184 bufferView.byteOffset = static_cast<uint32_t>(bufferDataStart);
185 if (align && elementSize % 4 != 0) {
186 bufferView.byteStride = static_cast<uint32_t>(alignedElementSize);
187 } else {
188 bufferView.byteStride = 0;
189 }
190
191 // create accessor
192 const size_t accessorIdx = model.accessors.size();
193 model.accessors.emplace_back();
194 auto& accessor = model.accessors.back();
195
196 // set component type and accessor size depending on the type
197 setAccessorType<T>(accessor);
198 accessor.bufferView = static_cast<int32_t>(bufferViewIdx);
199 accessor.byteOffset = 0;
200 accessor.count = static_cast<uint32_t>(data.size());
201 accessor.normalized = normalized;
202
203 return static_cast<int32_t>(accessorIdx);
204}
205
206template <>
207inline void setAccessorType<const int32_t>(fx::gltf::Accessor& accessor) {
208 accessor.componentType = fx::gltf::Accessor::ComponentType::UnsignedInt;
209 accessor.type = fx::gltf::Accessor::Type::Scalar;
210}
211
212template <>
213inline void setAccessorType<const float>(fx::gltf::Accessor& accessor) {
214 accessor.componentType = fx::gltf::Accessor::ComponentType::Float;
215 accessor.type = fx::gltf::Accessor::Type::Scalar;
216}
217
218template <>
219inline void setAccessorType<const Vector2f>(fx::gltf::Accessor& accessor) {
220 accessor.componentType = fx::gltf::Accessor::ComponentType::Float;
221 accessor.type = fx::gltf::Accessor::Type::Vec2;
222}
223
224template <>
225inline void setAccessorType<const Vector3f>(fx::gltf::Accessor& accessor) {
226 accessor.componentType = fx::gltf::Accessor::ComponentType::Float;
227 accessor.type = fx::gltf::Accessor::Type::Vec3;
228}
229
230template <>
231inline void setAccessorType<const Vector4f>(fx::gltf::Accessor& accessor) {
232 accessor.componentType = fx::gltf::Accessor::ComponentType::Float;
233 accessor.type = fx::gltf::Accessor::Type::Vec4;
234}
235
236template <>
237inline void setAccessorType<const Vector3b>(fx::gltf::Accessor& accessor) {
238 accessor.componentType = fx::gltf::Accessor::ComponentType::UnsignedByte;
239 accessor.type = fx::gltf::Accessor::Type::Vec3;
240}
241
242template <>
243inline void setAccessorType<const Vector3i>(fx::gltf::Accessor& accessor) {
244 accessor.componentType = fx::gltf::Accessor::ComponentType::UnsignedInt;
245 accessor.type = fx::gltf::Accessor::Type::Vec3;
246}
247
248template <>
249inline void setAccessorType<const Vector4s>(fx::gltf::Accessor& accessor) {
250 accessor.componentType = fx::gltf::Accessor::ComponentType::UnsignedShort;
251 accessor.type = fx::gltf::Accessor::Type::Vec4;
252}
253
254template <>
255inline void setAccessorType<const Vector4b>(fx::gltf::Accessor& accessor) {
256 accessor.componentType = fx::gltf::Accessor::ComponentType::UnsignedByte;
257 accessor.type = fx::gltf::Accessor::Type::Vec4;
258}
259
260template <>
261inline void setAccessorType<const Matrix4f>(fx::gltf::Accessor& accessor) {
262 accessor.componentType = fx::gltf::Accessor::ComponentType::Float;
263 accessor.type = fx::gltf::Accessor::Type::Mat4;
264}
265
266// add animation
268inline fx::gltf::Animation::Sampler::Type toGltfSamplerType(
269 GltfAnimationInterpolation interpolation) {
270 switch (interpolation) {
272 return fx::gltf::Animation::Sampler::Type::Step;
274 return fx::gltf::Animation::Sampler::Type::Linear;
275 }
276 return fx::gltf::Animation::Sampler::Type::Step;
277}
278
279template <typename T>
281 fx::gltf::Document& model,
282 fx::gltf::Animation& anim,
283 std::span<T> data,
284 const std::span<float>& timestamps,
285 GltfAnimationInterpolation interpolation) {
286 MT_CHECK(
287 data.size() == timestamps.size(), "data: {}, timestmaps: {}", data.size(), timestamps.size());
288
289 // create new sampler
290 const auto index = static_cast<int32_t>(anim.samplers.size());
291 anim.samplers.emplace_back();
292 auto& sampler = anim.samplers.back();
293 sampler.interpolation = toGltfSamplerType(interpolation);
294 sampler.input = createAccessorBuffer(model, timestamps);
295 sampler.output = createAccessorBuffer(model, data);
296
297 // return sampler index
298 return index;
299}
300
301template <typename T>
303 fx::gltf::Document& model,
304 fx::gltf::Animation& anim,
305 std::span<T> data,
306 const int32_t timestampAccessor,
307 GltfAnimationInterpolation interpolation) {
308 // create new sampler
309 const auto index = static_cast<int32_t>(anim.samplers.size());
310 anim.samplers.emplace_back();
311 auto& sampler = anim.samplers.back();
312 sampler.interpolation = toGltfSamplerType(interpolation);
313 sampler.input = timestampAccessor;
314 sampler.output = createAccessorBuffer(model, data);
315
316 // return sampler index
317 return index;
318}
319
320} // namespace momentum
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Definition checks.h:36
#define MT_THROW(...)
Throws a std::runtime_error with a formatted message.
Definition exception.h:31
#define MT_LOGW(...)
Definition log.h:180
void setAccessorType< const Vector4b >(fx::gltf::Accessor &accessor)
void setAccessorType< const Vector4f >(fx::gltf::Accessor &accessor)
void setAccessorType< const Vector3i >(fx::gltf::Accessor &accessor)
int32_t getTypeSize(const fx::gltf::Accessor::Type &ty)
int32_t createAccessorBuffer(fx::gltf::Document &model, std::span< T > data, const bool align=false, const bool normalized=false)
std::vector< T > copyAlignedAccessorBuffer(const fx::gltf::Document &model, int32_t id)
GltfAnimationInterpolation
How animation samplers written to a GLTF file interpolate between their keys.
@ Linear
Interpolate between keys (GLTF "LINEAR"; slerp for rotations).
@ Step
Hold each key until the next one (GLTF "STEP").
std::vector< T > copyAccessorBuffer(const fx::gltf::Document &model, int32_t id)
void setAccessorType< const Matrix4f >(fx::gltf::Accessor &accessor)
void setAccessorType< const int32_t >(fx::gltf::Accessor &accessor)
void setAccessorType< const Vector2f >(fx::gltf::Accessor &accessor)
int32_t getComponentSize(const fx::gltf::Accessor::ComponentType &componentType)
void setAccessorType(fx::gltf::Accessor &)
void setAccessorType< const Vector4s >(fx::gltf::Accessor &accessor)
void setAccessorType< const Vector3b >(fx::gltf::Accessor &accessor)
fx::gltf::Animation::Sampler::Type toGltfSamplerType(GltfAnimationInterpolation interpolation)
Converts the save option to the GLTF sampler interpolation type.
void setAccessorType< const float >(fx::gltf::Accessor &accessor)
void setAccessorType< const Vector3f >(fx::gltf::Accessor &accessor)
int32_t createSampler(fx::gltf::Document &model, fx::gltf::Animation &anim, std::span< T > data, const std::span< float > &timestamps, GltfAnimationInterpolation interpolation)