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 /**
199 * @brief Optional triangular mesh exposed through the contact-skin queries and, when selected as
200 * a deformable actor's contact geometry, for contact quadrature.
201 *
202 * @details Skinning data is required. For triangular and tetrahedral primary meshes, it is a
203 * node-based linear embedding whose indices reference primary-mesh nodes. For polylines, the
204 * indices reference primary-mesh elements and define the rod's element-based embedding. Every
205 * contact-skin node must be referenced by at least one triangle.
206 */
207 std::optional<MeshData> contactSkinMesh;
208 std::optional<DynamicArray<BlendingData>> blending;
209
210 /** @brief Indices of mesh nodes that are constrained. */
211 std::optional<DynamicArray<int>> constrainedNodes;
212
213 /**
214 * @brief Per-element reference frame axes for polyline meshes.
215 *
216 * @details Flat array of unit vectors (3 reals per element), each orthogonal to its
217 * element's tangent. Only valid when the mesh is a polyline (@ref MeshData::nodesPerElement ==
218 * 2).
219 */
220 std::optional<DynamicArray<real>> elementFrameAxes;
221
222 // Implicit Geometry
223 std::optional<Box> box;
224 std::optional<Plane> plane;
225 std::optional<Sphere> sphere;
226
227 // SDF Grid
228 std::optional<GridSdfData> sdf;
229
230 // Soft Material Data (per element)
231 std::optional<PerElementSoftMaterialData> material;
232
233 /**
234 * @brief True if the loaded model contains unsupported experimental data.
235 *
236 * @details Some model files contain additional data for experimental features (e.g. ROMs),
237 * which cannot be represented by this struct. This flag is set during loading when such data is
238 * detected.
239 */
241
242#if MOCHI_LANGUAGE_CPP20
243 bool operator==(ModelData const& other) const = default;
244 bool operator!=(ModelData const& other) const = default;
245#endif
246
261};
262
263/**
264 * @brief A non-owning view of the contents of a Mochi model file.
265 *
266 * @see ModelData
267 */
269 ModelDataView() = default;
270
271 /**
272 * @brief Implicit conversion from @ref ModelData.
273 *
274 * @param[in] src Source data.
275 */
276 ModelDataView(ModelData const& src);
277
278 // Mesh
279 std::optional<MeshDataView> mesh;
280 std::optional<MeshDataView> visualMesh;
281 /** @copydoc ModelData::contactSkinMesh */
282 std::optional<MeshDataView> contactSkinMesh;
283 // DynamicArray needed here because blending is an array-of-structures.
284 std::optional<DynamicArray<BlendingDataView>> blending;
285
286 /** @brief Indices of mesh nodes that are constrained. */
287 std::optional<Span<int const>> constrainedNodes;
288
289 /**
290 * @brief Per-element reference frame axes for polyline meshes.
291 *
292 * @details Flat array of unit vectors (3 reals per element), each orthogonal to its
293 * element's tangent. Only valid when the mesh is a polyline (@ref MeshDataView::nodesPerElement
294 * == 2).
295 */
296 std::optional<Span<real const>> elementFrameAxes;
297
298 // Implicit Geometry
299 std::optional<Box> box;
300 std::optional<Plane> plane;
301 std::optional<Sphere> sphere;
302
303 // SDF Grid
304 std::optional<GridSdfDataView> sdf;
305
306 // Soft Material Data (per element)
307 std::optional<PerElementSoftMaterialDataView> material;
308
309 /**
310 * @brief True if the loaded model contains unsupported experimental data.
311 *
312 * @details Some model files contain additional data for experimental features (e.g. ROMs),
313 * which cannot be represented by this struct. This flag is set during loading when such data is
314 * detected.
315 */
317
318#if MOCHI_LANGUAGE_CPP20
319 bool operator==(ModelDataView const& other) const = default;
320 bool operator!=(ModelDataView const& other) const = default;
321#endif
322};
323
324// File format options used when saving model data.
325enum class FileFormat {
326 JSON, ///< JSON format (text)
327 H5, ///< HDF5 format (binary). Requires MOCHI_USE_HDF5.
329};
330
331/// Mesh file format hint for @ref Context::LoadShapeFromBytes and model-loading APIs that read
332/// from byte buffers.
333enum class MeshFileType {
334 Legacy, ///< Auto-detect between HDF5 and JSON (default behavior).
335 PLY, ///< PLY format (Stanford Polygon).
336 OFF, ///< OFF format (Object File Format).
337 STL, ///< STL format (Stereolithography).
338 OBJ, ///< OBJ format (Wavefront).
340};
341
342} // namespace superdex
343
345MOCHI_ENUM_ITEM(JSON)
348
349MOCHI_ENUM_BEGIN(superdex::MeshFileType)
350MOCHI_ENUM_ITEM(Legacy)
356
357#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:326
@ H5
HDF5 format (binary). Requires MOCHI_USE_HDF5.
Definition model_data.h:327
MeshFileType
Mesh file format hint for Context::LoadShapeFromBytes and model-loading APIs that read from byte buff...
Definition model_data.h:333
@ Legacy
Auto-detect between HDF5 and JSON (default behavior).
Definition model_data.h:334
@ STL
STL format (Stereolithography).
Definition model_data.h:337
@ PLY
PLY format (Stanford Polygon).
Definition model_data.h:335
@ OFF
OFF format (Object File Format).
Definition model_data.h:336
@ OBJ
OBJ format (Wavefront).
Definition model_data.h:338
NdArray< int, 3 > Int3
Definition nd_array.h:147
@ Count
Number of actor type enum values.
Definition mochi_enums.h:38
#define MOCHI_ENUM_END()
Definition reflection.h:330
#define MOCHI_STRUCT_END()
Definition reflection.h:333
#define MOCHI_ENUM_BEGIN(name)
Definition reflection.h:327
#define MOCHI_FIELD(name)
Definition reflection.h:351
#define MOCHI_STRUCT_BEGIN(name)
Definition reflection.h:332
#define MOCHI_ATTRIBUTE(...)
Definition reflection.h:357
#define MOCHI_ENUM_ITEM(name)
Definition reflection.h:328
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:268
std::optional< MeshDataView > contactSkinMesh
Optional triangular mesh exposed through the contact-skin queries and, when selected as a deformable ...
Definition model_data.h:282
std::optional< GridSdfDataView > sdf
Definition model_data.h:304
std::optional< Box > box
Definition model_data.h:299
std::optional< MeshDataView > visualMesh
Definition model_data.h:280
bool operator!=(ModelDataView const &other) const =default
std::optional< MeshDataView > mesh
Definition model_data.h:279
std::optional< Sphere > sphere
Definition model_data.h:301
std::optional< Plane > plane
Definition model_data.h:300
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:296
std::optional< DynamicArray< BlendingDataView > > blending
Definition model_data.h:284
std::optional< PerElementSoftMaterialDataView > material
Definition model_data.h:307
std::optional< Span< int const > > constrainedNodes
Indices of mesh nodes that are constrained.
Definition model_data.h:287
bool experimentalDataDetected
True if the loaded model contains unsupported experimental data.
Definition model_data.h:316
The contents of a Mochi model file.
Definition model_data.h:185
std::optional< DynamicArray< BlendingData > > blending
Definition model_data.h:208
bool operator==(ModelData const &other) const =default
std::optional< Sphere > sphere
Definition model_data.h:225
bool experimentalDataDetected
True if the loaded model contains unsupported experimental data.
Definition model_data.h:240
std::optional< DynamicArray< int > > constrainedNodes
Indices of mesh nodes that are constrained.
Definition model_data.h:211
std::optional< MeshData > mesh
Definition model_data.h:196
std::optional< Plane > plane
Definition model_data.h:224
std::optional< DynamicArray< real > > elementFrameAxes
Per-element reference frame axes for polyline meshes.
Definition model_data.h:220
std::optional< Box > box
Definition model_data.h:223
std::optional< MeshData > contactSkinMesh
Optional triangular mesh exposed through the contact-skin queries and, when selected as a deformable ...
Definition model_data.h:207
bool operator!=(ModelData const &other) const =default
std::optional< PerElementSoftMaterialData > material
Definition model_data.h:231
std::optional< MeshData > visualMesh
Definition model_data.h:197
std::optional< GridSdfData > sdf
Definition model_data.h:228