SuperDex Physics C++ API
Loading...
Searching...
No Matches
mochi_context.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
20#include "mochi_handle.h"
21#include "mochi_structs.h"
22
23/********************************************************************************
24 IMPORTANT: PLEASE KEEP HEADER INCLUDES TO A MINIMUM.
25 If you must include a mochi_core header, then please make sure that it only
26 declares the data types (not containing other implementation details).
27*********************************************************************************/
36
37#include <string_view>
38
39namespace superdex {
40
41class Actor;
42class AsyncScene;
43class DebugServer;
44class IKSolver;
45class Scene;
46
47class Context {
48 public:
49 //----------------------------------------------------------------------------------------------
50 // Concurrency APIs
51 //----------------------------------------------------------------------------------------------
52
53 virtual void BindThisThread() = 0;
54
55 virtual void UnbindThisThread() = 0;
56
57 virtual void SetIsSingleThreaded(bool isSingleThreaded) = 0;
58
59 [[nodiscard]] virtual bool IsSingleThreaded() const = 0;
60
61 [[nodiscard]] virtual int GetNumThreads() const = 0;
62
63 //----------------------------------------------------------------------------------------------
64 // Debugging APIs
65 //----------------------------------------------------------------------------------------------
66
67 static void EnableLogChannel(LogChannel channel, bool enable);
68
69 [[nodiscard]] MOCHI_API static bool IsLogChannelEnabled(LogChannel channel);
70
71 static void SetLogCallback(LogFn callback);
72
73 [[nodiscard]] MOCHI_API static LogFn GetLogCallback();
74
75 static void SetAssertionFailureCallback(OnAssertFn callback);
76
78
79 //----------------------------------------------------------------------------------------------
80 // Shape APIs
81 //----------------------------------------------------------------------------------------------
82
83 [[nodiscard]] virtual ShapeHandle LoadShapeFromFile(std::string_view filePath, Error& error) = 0;
84
85 [[nodiscard]] ShapeHandle
86 LoadShapeFromFile(std::string_view filePath, Real3 const& bakeScale, Error& error);
87
88 [[nodiscard]] ShapeHandle
89 LoadShapeFromFile(std::string_view filePath, TransformRT const& bakeTransform, Error& error);
90
91 [[nodiscard]] virtual ShapeHandle LoadShapeFromFile(
92 std::string_view filePath,
93 Real3 const& bakeScale,
94 TransformRT const& bakeTransform,
95 Error& error) = 0;
96
97 [[nodiscard]] virtual ShapeHandle
99
100 [[nodiscard]] ShapeHandle LoadShapeFromBytes(
101 Span<char const> fileData,
102 MeshFileType format,
103 Real3 const& bakeScale,
104 Error& error);
105
106 [[nodiscard]] ShapeHandle LoadShapeFromBytes(
107 Span<char const> fileData,
108 MeshFileType format,
109 TransformRT const& bakeTransform,
110 Error& error);
111
112 [[nodiscard]] virtual ShapeHandle LoadShapeFromBytes(
113 Span<char const> fileData,
114 MeshFileType format,
115 Real3 const& bakeScale,
116 TransformRT const& bakeTransform,
117 Error& error) = 0;
118
119 [[nodiscard]] ShapeHandle LoadShapeFromBytes(Span<char const> fileData, Error& error);
120
121 [[nodiscard]] ShapeHandle
122 LoadShapeFromBytes(Span<char const> fileData, Real3 const& bakeScale, Error& error);
123
124 [[nodiscard]] ShapeHandle
125 LoadShapeFromBytes(Span<char const> fileData, TransformRT const& bakeTransform, Error& error);
126
127 [[nodiscard]] ShapeHandle LoadShapeFromBytes(
128 Span<char const> fileData,
129 Real3 const& bakeScale,
130 TransformRT const& bakeTransform,
131 Error& error);
132
133 [[nodiscard]] virtual ShapeHandle CreateMeshShape(MeshData const& mesh, Error& error) = 0;
134
135 [[nodiscard]] virtual ShapeHandle CreateMeshShape(MeshDataView const& mesh, Error& error) = 0;
136
137 [[nodiscard]] virtual ShapeHandle CreateModelShape(ModelData const& model, Error& error) = 0;
138
139 [[nodiscard]] virtual ShapeHandle CreateModelShape(ModelDataView const& model, Error& error) = 0;
140
141 [[nodiscard]] virtual ShapeHandle
142 CreateTetMeshShape(Span<real const> coordinates, Span<int const> connectivity, Error& error) = 0;
143
144 [[nodiscard]] virtual ShapeHandle
145 CreateTriMeshShape(Span<real const> coordinates, Span<int const> connectivity, Error& error) = 0;
146
147 [[nodiscard]] virtual ShapeHandle
148 CreateSphereShape(Real3 const& center, real radius, Error& error) = 0;
149
150 [[nodiscard]] virtual ShapeHandle
151 CreatePlaneShape(Real3 const& normal, real distance, Error& error) = 0;
152
153 [[nodiscard]] virtual MeshDataView GetShapeMesh(ShapeHandle shape, Error& error) const = 0;
154 [[nodiscard]] virtual MeshDataView GetShapeSurfaceMesh(ShapeHandle shape, Error& error) const = 0;
155 [[nodiscard]] virtual MeshDataView GetShapeContactSkinMesh(ShapeHandle shape, Error& error)
156 const = 0;
157 [[nodiscard]] virtual MeshDataView GetShapeVisualMesh(ShapeHandle shape, Error& error) const = 0;
158 [[nodiscard]] virtual Span<real const> GetShapeElementFrameAxes(ShapeHandle shape, Error& error)
159 const = 0;
160
161 [[nodiscard]] virtual Aabb GetShapeAabb(ShapeHandle shape, Error& error) const = 0;
162
164 ShapeHandle shape,
165 Error& error) const = 0;
166
167 virtual void ReleaseShape(ShapeHandle shape) = 0;
168
169 [[nodiscard]] virtual int GetNumShapes() const = 0;
170
171 virtual void EnableFileCache(bool enable) = 0;
172
173 [[nodiscard]] virtual bool IsFileCacheEnabled() const = 0;
174
175 virtual void ClearFileCache() = 0;
176
177 virtual void ClearFileFromCache(std::string_view filePath) = 0;
178
179 //----------------------------------------------------------------------------------------------
180 // Scene APIs
181 //----------------------------------------------------------------------------------------------
182
183 [[nodiscard]] virtual Scene* CreateScene(std::string_view name) = 0;
184
185 virtual void DestroyScene(Scene* scene) = 0;
186
187 virtual Scene* GetScene(SceneHandle handle) = 0;
188
189 virtual Scene const* GetScene(SceneHandle handle) const = 0;
190
191 [[nodiscard]] virtual bool IsValidScene(Scene const* scene) const = 0;
192
193 //----------------------------------------------------------------------------------------------
194 // AsyncScene APIs
195 //----------------------------------------------------------------------------------------------
196
197 [[nodiscard]] virtual AsyncScene* CreateAsyncScene(std::string_view name, Error& error) = 0;
198
199 [[nodiscard]] virtual AsyncScene* CreateAsyncScenePaused(std::string_view name, Error& error) = 0;
200
201 virtual void DestroyAsyncScene(AsyncScene* scene) = 0;
202
203 [[nodiscard]] virtual bool IsValidAsyncScene(AsyncScene const* scene) const = 0;
204
205 //----------------------------------------------------------------------------------------------
206 // DebugServer APIs
207 //----------------------------------------------------------------------------------------------
208
210 virtual DebugServer const& GetDebugServer() const = 0;
211
212 protected:
213 MOCHI_API static void EnableLogChannelInternal(LogChannel channel, bool enable);
215 MOCHI_API static void SetAssertionFailureCallbackInternal(OnAssertFn callback);
216
217 // Please use DestroyContext. Do not delete the pointer directly.
218 virtual ~Context() = default;
219};
220
221[[nodiscard]] MOCHI_API Context* CreateContext(int numWorkerThreads = -1);
222
224
225} // namespace superdex
226
227#include "mochi_context_inl.h"
Represents a simulated body (actor) in a scene.
Definition mochi_actor.h:49
Manages a simulation scene that steps asynchronously using the SuperDex Physics worker pool.
Main entry point for SuperDex Physics simulation.
virtual ~Context()=default
static bool IsLogChannelEnabled(LogChannel channel)
Return true if messages on the specified log channel are enabled.
virtual MeshDataView GetShapeSurfaceMesh(ShapeHandle shape, Error &error) const =0
Get a view of the shape's surface mesh data.
virtual void ClearFileCache()=0
Release all shapes that are being preserved by the file cache (if any).
virtual MeshDataView GetShapeMesh(ShapeHandle shape, Error &error) const =0
Get a view of the shape's main mesh data.
virtual ShapeHandle CreateMeshShape(MeshData const &mesh, Error &error)=0
Create a shape using in-memory mesh data, so that it can be used to create actors.
virtual AsyncScene * CreateAsyncScenePaused(std::string_view name, Error &error)=0
Create a new AsyncScene that will initially be in the paused state.
static void EnableLogChannel(LogChannel channel, bool enable)
Enable or disable log messages on the specified channel.
static void EnableLogChannelInternal(LogChannel channel, bool enable)
virtual ShapeHandle LoadShapeFromBytes(Span< char const > fileData, MeshFileType format, Error &error)=0
Load a shape from a file of supported format, where the file has already been loaded into memory,...
virtual bool IsValidScene(Scene const *scene) const =0
Check if a scene is valid (not destroyed), and belongs to this Context.
virtual AsyncScene * CreateAsyncScene(std::string_view name, Error &error)=0
Create a new AsyncScene.
virtual DebugServer & GetDebugServer()=0
Get the debug server for this context.
static OnAssertFn GetAssertionFailureCallback()
Get the function currently being used to report assertion failures.
virtual bool IsValidAsyncScene(AsyncScene const *scene) const =0
Check if an AsyncScene is valid (not destroyed), and belongs to this Context.
virtual void ClearFileFromCache(std::string_view filePath)=0
Release all cached entries for a given shape file.
virtual bool IsSingleThreaded() const =0
Return true if running in single-threaded mode.
virtual void ReleaseShape(ShapeHandle shape)=0
Release a shape handle.
virtual ShapeHandle CreateSphereShape(Real3 const &center, real radius, Error &error)=0
Create an implicit sphere shape.
virtual ShapeHandle CreatePlaneShape(Real3 const &normal, real distance, Error &error)=0
Create an implicit plane with infinite extents.
virtual void DestroyAsyncScene(AsyncScene *scene)=0
Immediately stop and destroy an AsyncScene and all its actors and constraints.
virtual ShapeHandle CreateMeshShape(MeshDataView const &mesh, Error &error)=0
Create a shape using in-memory mesh data, so that it can be used to create actors.
virtual Scene * GetScene(SceneHandle handle)=0
Get a pointer to a scene by its handle.
virtual void SetIsSingleThreaded(bool isSingleThreaded)=0
Enable or disable single-threaded mode.
virtual ShapeHandle LoadShapeFromFile(std::string_view filePath, Real3 const &bakeScale, TransformRT const &bakeTransform, Error &error)=0
Load a shape from a file of supported format, and bake in some scale, rotation, and translation.
virtual int GetNumShapes() const =0
Get the number of shape handles that have been created/loaded and have not yet been released.
virtual Aabb GetShapeAabb(ShapeHandle shape, Error &error) const =0
Get an axis-aligned bounding box (AABB) that contains the shape, in its local coordinate frame.
virtual bool IsFileCacheEnabled() const =0
Return true if the file cache feature is enabled.
virtual void UnbindThisThread()=0
Unbind the calling thread from this Context.
static void SetLogCallbackInternal(LogFn callback)
virtual Scene const * GetScene(SceneHandle handle) const =0
Get a const pointer to a scene by its handle.
virtual void BindThisThread()=0
Bind the calling thread to this Context.
virtual void DestroyScene(Scene *scene)=0
Immediately destroy a scene and all its actors and constraints.
virtual ArticulatedShapeInfo GetArticulatedShapeInfo(ShapeHandle shape, Error &error) const =0
Get information about an articulated shape.
virtual Span< real const > GetShapeElementFrameAxes(ShapeHandle shape, Error &error) const =0
Get the per-element frame axes of a polyline shape.
static void SetLogCallback(LogFn callback)
Optionally provide a callback function to redirect SuperDex Physics logging.
virtual ShapeHandle LoadShapeFromBytes(Span< char const > fileData, MeshFileType format, Real3 const &bakeScale, TransformRT const &bakeTransform, Error &error)=0
Load a shape from a file of supported format, where the file has already been loaded into memory,...
virtual ShapeHandle CreateModelShape(ModelDataView const &model, Error &error)=0
Create a shape using in-memory model data, so that it can be used to create actors.
virtual ShapeHandle CreateTetMeshShape(Span< real const > coordinates, Span< int const > connectivity, Error &error)=0
Create a tetrahedral mesh shape using in-memory data.
static void SetAssertionFailureCallback(OnAssertFn callback)
Optionally provide a callback function to customize the way in which assertion failures are reported.
virtual int GetNumThreads() const =0
Return the number of worker threads available to execute asynchronous tasks.
virtual ShapeHandle CreateTriMeshShape(Span< real const > coordinates, Span< int const > connectivity, Error &error)=0
Create a triangle mesh shape using in-memory data.
virtual Scene * CreateScene(std::string_view name)=0
Create a new Scene.
virtual DebugServer const & GetDebugServer() const =0
Get a const reference to the debug server for this context.
virtual void EnableFileCache(bool enable)=0
Enable or disable the file cache.
virtual MeshDataView GetShapeVisualMesh(ShapeHandle shape, Error &error) const =0
Get a view of the shape's visual mesh data, including skinning data if available.
static LogFn GetLogCallback()
Get the function currently being used to output logging.
virtual ShapeHandle CreateModelShape(ModelData const &model, Error &error)=0
Create a shape using in-memory model data, so that it can be used to create actors.
virtual MeshDataView GetShapeContactSkinMesh(ShapeHandle shape, Error &error) const =0
Get a view of the shape's contact-skin mesh data, including its skinning data.
virtual ShapeHandle LoadShapeFromFile(std::string_view filePath, Error &error)=0
Load a shape from a file of supported format.
Server API to enable remote debugging and visualization.
Represents a simulation scene.
Definition mochi_scene.h:45
#define MOCHI_API
Context * CreateContext(int numWorkerThreads=-1)
Initialize SuperDex Physics by creating a Context object.
void DestroyContext(Context *context)
Shut down SuperDex Physics by destroying the Context object.
std::function< void(LogChannel, char const *message, char const *file, int line)> LogFn
Signature of a logging callback function.
Definition log.h:65
LogChannel
Definition log.h:37
MeshFileType
Mesh file format hint for Context::LoadShapeFromBytes and model-loading APIs that read from byte buff...
Definition model_data.h:333
NdArray< real, 3 > Real3
Definition nd_array.h:115
std::function< bool(char const *condition, char const *message, char const *file, int line)> OnAssertFn
Callback invoked when an assertion fails.
Definition debug.h:140
Information describing an articulated shape's kinematic structure.
A non-owning view of the mesh data for a Mochi actor or model file.
Definition mesh_data.h:234
Mesh data for a Mochi actor or model file.
Definition mesh_data.h:173
A non-owning view of the contents of a Mochi model file.
Definition model_data.h:268
The contents of a Mochi model file.
Definition model_data.h:185
Unique identifier for a Scene within a Context.
Handle to a shape (mesh, sphere, plane, etc.) within a Context.