SuperDex Physics C++ API
Loading...
Searching...
No Matches
model_data.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19// PLEASE DO NOT ADD OTHER INCLUDES HERE. This header is included in the mochi_physics public API.
29
30#include <optional>
31
32namespace superdex {
33
34// Forwards:
35struct GridSdfDataView;
36struct ModelDataView;
37
38/**
39 * @brief Data for a pre-computed SDF grid.
40 *
41 * @see GridSdfDataView
42 */
44 GridSdfData() = default;
45
46 /**
47 * @brief Copy from @ref GridSdfDataView.
48 *
49 * @param[in] src Source data.
50 */
51 explicit GridSdfData(GridSdfDataView const& src);
52
53 /** @brief Dimensions of the SDF grid in X, Y, and Z. */
55
56 /**
57 * @brief Signed distance values [m].
58 *
59 * @note Size must be (dims[0] * dims[1] * dims[2])
60 */
62
63 /** @brief Spatial bounds of the SDF grid. Values are distributed uniformly within this volume. */
65
66 /**
67 * @brief Spatial bounds of the portion of the SDF grid with negative values.
68 *
69 * @note This is generally the bounds of the mesh for which the SDF grid was computed, while the
70 * overall grid bounds may be larger due to padding for penalty fall-off distance.
71 */
73
74 /**
75 * @brief Optional parent-from-grid per-axis scale to apply at runtime.
76 *
77 * @note Applied order is scale, then rotation, then translation.
78 * @note Typically set when a transform is baked into the containing model.
79 */
80 std::optional<Real3> scale;
81
82 /**
83 * @brief Optional parent-from-grid rotation to apply at runtime.
84 *
85 * @note Applied order is scale, then rotation, then translation.
86 * @note Typically set when a transform is baked into the containing model.
87 */
88 std::optional<Quaternion> rotation;
89
90 /**
91 * @brief Optional parent-from-grid translation [m] to apply at runtime.
92 *
93 * @note Applied order is scale, then rotation, then translation.
94 * @note Typically set when a transform is baked into the containing model.
95 */
96 std::optional<Real3> translation;
97
98#if MOCHI_LANGUAGE_CPP20
99 bool operator==(GridSdfData const& other) const = default;
100 bool operator!=(GridSdfData const& other) const = default;
101#endif
102
112};
113
114/**
115 * @brief A non-owning view of precomputed grid-based signed-distance-field data.
116 *
117 * @see GridSdfData
118 */
120 GridSdfDataView() = default;
121
122 /**
123 * @brief Implicit conversion from @ref GridSdfData.
124 *
125 * @param[in] src Source data.
126 */
127 GridSdfDataView(GridSdfData const& src);
128
129 /** @brief Dimensions of the SDF grid in X, Y, and Z. */
131
132 /**
133 * @brief Signed distance values [m].
134 *
135 * @note Size must be (dims[0] * dims[1] * dims[2])
136 */
138
139 /** @brief Spatial bounds of the SDF grid. Values are distributed uniformly within this volume. */
141
142 /**
143 * @brief Spatial bounds of the portion of the SDF grid with negative values.
144 *
145 * @note This is generally the bounds of the mesh for which the SDF grid was computed, while the
146 * overall grid bounds may be larger due to padding for penalty fall-off distance.
147 */
149
150 /**
151 * @brief Optional parent-from-grid per-axis scale to apply at runtime.
152 *
153 * @note Applied order is scale, then rotation, then translation.
154 * @note Typically set when a transform is baked into the containing model.
155 */
156 std::optional<Real3> scale;
157
158 /**
159 * @brief Optional parent-from-grid rotation to apply at runtime.
160 *
161 * @note Applied order is scale, then rotation, then translation.
162 * @note Typically set when a transform is baked into the containing model.
163 */
164 std::optional<Quaternion> rotation;
165
166 /**
167 * @brief Optional parent-from-grid translation [m] to apply at runtime.
168 *
169 * @note Applied order is scale, then rotation, then translation.
170 * @note Typically set when a transform is baked into the containing model.
171 */
172 std::optional<Real3> translation;
173
174#if MOCHI_LANGUAGE_CPP20
175 bool operator==(GridSdfDataView const& other) const = default;
176 bool operator!=(GridSdfDataView const& other) const = default;
177#endif
178};
179
180/**
181 * @brief The contents of a Mochi model file.
182 *
183 * @see ModelDataView
184 */
185struct ModelData {
186 ModelData() = default;
187
188 /**
189 * @brief Copy from @ref ModelDataView.
190 *
191 * @param[in] src Source data.
192 */
193 explicit ModelData(ModelDataView const& src);
194
195 // Mesh
196 std::optional<MeshData> mesh;
197 std::optional<MeshData> visualMesh;
198 std::optional<DynamicArray<BlendingData>> blending;
199
200 /** @brief Indices of mesh nodes that are constrained. */
201 std::optional<DynamicArray<int>> constrainedNodes;
202
203 /**
204 * @brief Per-element reference frame axes for polyline meshes.
205 *
206 * @details Flat array of unit vectors (3 reals per element), each orthogonal to its
207 * element's tangent. Only valid when the mesh is a polyline (@ref MeshData::nodesPerElement ==
208 * 2).
209 */
210 std::optional<DynamicArray<real>> elementFrameAxes;
211
212 // Implicit Geometry
213 std::optional<Box> box;
214 std::optional<Plane> plane;
215 std::optional<Sphere> sphere;
216
217 // SDF Grid
218 std::optional<GridSdfData> sdf;
219
220 // Soft Material Data (per element)
221 std::optional<PerElementSoftMaterialData> material;
222
223 /**
224 * @brief True if the loaded model contains unsupported experimental data.
225 *
226 * @details Some model files contain additional data for experimental features (e.g. ROMs),
227 * which cannot be represented by this struct. This flag is set during loading when such data is
228 * detected.
229 */
231
232#if MOCHI_LANGUAGE_CPP20
233 bool operator==(ModelData const& other) const = default;
234 bool operator!=(ModelData const& other) const = default;
235#endif
236
250};
251
252/**
253 * @brief A non-owning view of the contents of a Mochi model file.
254 *
255 * @see ModelData
256 */
258 ModelDataView() = default;
259
260 /**
261 * @brief Implicit conversion from @ref ModelData.
262 *
263 * @param[in] src Source data.
264 */
265 ModelDataView(ModelData const& src);
266
267 // Mesh
268 std::optional<MeshDataView> mesh;
269 std::optional<MeshDataView> visualMesh;
270 // DynamicArray needed here because blending is an array-of-structures.
271 std::optional<DynamicArray<BlendingDataView>> blending;
272
273 /** @brief Indices of mesh nodes that are constrained. */
274 std::optional<Span<int const>> constrainedNodes;
275
276 /**
277 * @brief Per-element reference frame axes for polyline meshes.
278 *
279 * @details Flat array of unit vectors (3 reals per element), each orthogonal to its
280 * element's tangent. Only valid when the mesh is a polyline (@ref MeshDataView::nodesPerElement
281 * == 2).
282 */
283 std::optional<Span<real const>> elementFrameAxes;
284
285 // Implicit Geometry
286 std::optional<Box> box;
287 std::optional<Plane> plane;
288 std::optional<Sphere> sphere;
289
290 // SDF Grid
291 std::optional<GridSdfDataView> sdf;
292
293 // Soft Material Data (per element)
294 std::optional<PerElementSoftMaterialDataView> material;
295
296 /**
297 * @brief True if the loaded model contains unsupported experimental data.
298 *
299 * @details Some model files contain additional data for experimental features (e.g. ROMs),
300 * which cannot be represented by this struct. This flag is set during loading when such data is
301 * detected.
302 */
304
305#if MOCHI_LANGUAGE_CPP20
306 bool operator==(ModelDataView const& other) const = default;
307 bool operator!=(ModelDataView const& other) const = default;
308#endif
309};
310
311// File format options used when saving model data.
312enum class FileFormat {
313 JSON, ///< JSON format (text)
314 H5, ///< HDF5 format (binary). Requires MOCHI_USE_HDF5.
316};
317
318/// Mesh file format hint for @ref Context::LoadShapeFromBytes and model-loading APIs that read
319/// from byte buffers.
320enum class MeshFileType {
321 Legacy, ///< Auto-detect between HDF5 and JSON (default behavior).
322 PLY, ///< PLY format (Stanford Polygon).
323 OFF, ///< OFF format (Object File Format).
324 STL, ///< STL format (Stereolithography).
325 OBJ, ///< OBJ format (Wavefront).
327};
328
329} // namespace superdex
330
332MOCHI_ENUM_ITEM(JSON)
335
336MOCHI_ENUM_BEGIN(superdex::MeshFileType)
337MOCHI_ENUM_ITEM(Legacy)
343
344#include "model_data_inl.h"
A dynamically resizable array with syntax and behavior similar to std::pmr::vector.
@ JSON
JSON format (text).
Definition model_data.h:313
@ H5
HDF5 format (binary). Requires MOCHI_USE_HDF5.
Definition model_data.h:314
MeshFileType
Mesh file format hint for Context::LoadShapeFromBytes and model-loading APIs that read from byte buff...
Definition model_data.h:320
@ Legacy
Auto-detect between HDF5 and JSON (default behavior).
Definition model_data.h:321
@ STL
STL format (Stereolithography).
Definition model_data.h:324
@ PLY
PLY format (Stanford Polygon).
Definition model_data.h:322
@ OFF
OFF format (Object File Format).
Definition model_data.h:323
@ OBJ
OBJ format (Wavefront).
Definition model_data.h:325
NdArray< int, 3 > Int3
Definition nd_array.h:138
@ Count
Number of actor type enum values.
Definition mochi_enums.h:38
#define MOCHI_ENUM_END()
Definition reflection.h:276
#define MOCHI_STRUCT_END()
Definition reflection.h:279
#define MOCHI_ENUM_BEGIN(name)
Definition reflection.h:273
#define MOCHI_FIELD(name)
Definition reflection.h:293
#define MOCHI_STRUCT_BEGIN(name)
Definition reflection.h:278
#define MOCHI_ATTRIBUTE(...)
Definition reflection.h:298
#define MOCHI_ENUM_ITEM(name)
Definition reflection.h:274
A non-owning view of precomputed grid-based signed-distance-field data.
Definition model_data.h:119
Aabb negativeValueBounds
Spatial bounds of the portion of the SDF grid with negative values.
Definition model_data.h:148
Aabb bounds
Spatial bounds of the SDF grid.
Definition model_data.h:140
std::optional< Real3 > translation
Optional parent-from-grid translation [m] to apply at runtime.
Definition model_data.h:172
Span< real const > values
Signed distance values [m].
Definition model_data.h:137
std::optional< Quaternion > rotation
Optional parent-from-grid rotation to apply at runtime.
Definition model_data.h:164
std::optional< Real3 > scale
Optional parent-from-grid per-axis scale to apply at runtime.
Definition model_data.h:156
Int3 dims
Dimensions of the SDF grid in X, Y, and Z.
Definition model_data.h:130
bool operator==(GridSdfDataView const &other) const =default
bool operator!=(GridSdfDataView const &other) const =default
Data for a pre-computed SDF grid.
Definition model_data.h:43
DynamicArray< real > values
Signed distance values [m].
Definition model_data.h:61
Aabb bounds
Spatial bounds of the SDF grid.
Definition model_data.h:64
std::optional< Quaternion > rotation
Optional parent-from-grid rotation to apply at runtime.
Definition model_data.h:88
Aabb negativeValueBounds
Spatial bounds of the portion of the SDF grid with negative values.
Definition model_data.h:72
Int3 dims
Dimensions of the SDF grid in X, Y, and Z.
Definition model_data.h:54
bool operator!=(GridSdfData const &other) const =default
std::optional< Real3 > translation
Optional parent-from-grid translation [m] to apply at runtime.
Definition model_data.h:96
bool operator==(GridSdfData const &other) const =default
std::optional< Real3 > scale
Optional parent-from-grid per-axis scale to apply at runtime.
Definition model_data.h:80
A non-owning view of the contents of a Mochi model file.
Definition model_data.h:257
std::optional< GridSdfDataView > sdf
Definition model_data.h:291
std::optional< Box > box
Definition model_data.h:286
std::optional< MeshDataView > visualMesh
Definition model_data.h:269
bool operator!=(ModelDataView const &other) const =default
std::optional< MeshDataView > mesh
Definition model_data.h:268
std::optional< Sphere > sphere
Definition model_data.h:288
std::optional< Plane > plane
Definition model_data.h:287
bool operator==(ModelDataView const &other) const =default
std::optional< Span< real const > > elementFrameAxes
Per-element reference frame axes for polyline meshes.
Definition model_data.h:283
std::optional< DynamicArray< BlendingDataView > > blending
Definition model_data.h:271
std::optional< PerElementSoftMaterialDataView > material
Definition model_data.h:294
std::optional< Span< int const > > constrainedNodes
Indices of mesh nodes that are constrained.
Definition model_data.h:274
bool experimentalDataDetected
True if the loaded model contains unsupported experimental data.
Definition model_data.h:303
The contents of a Mochi model file.
Definition model_data.h:185
std::optional< DynamicArray< BlendingData > > blending
Definition model_data.h:198
bool operator==(ModelData const &other) const =default
std::optional< Sphere > sphere
Definition model_data.h:215
bool experimentalDataDetected
True if the loaded model contains unsupported experimental data.
Definition model_data.h:230
std::optional< DynamicArray< int > > constrainedNodes
Indices of mesh nodes that are constrained.
Definition model_data.h:201
std::optional< MeshData > mesh
Definition model_data.h:196
std::optional< Plane > plane
Definition model_data.h:214
std::optional< DynamicArray< real > > elementFrameAxes
Per-element reference frame axes for polyline meshes.
Definition model_data.h:210
std::optional< Box > box
Definition model_data.h:213
bool operator!=(ModelData const &other) const =default
std::optional< PerElementSoftMaterialData > material
Definition model_data.h:221
std::optional< MeshData > visualMesh
Definition model_data.h:197
std::optional< GridSdfData > sdf
Definition model_data.h:218