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