SuperDex Physics C++ API
Loading...
Searching...
No Matches
mochi_scene.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
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*********************************************************************************/
32
33#include <cstdint>
34#include <functional>
35#include <string_view>
36
37namespace superdex {
38
39class Constraint;
40class DebugDraw;
41class Context;
42
43inline constexpr Real3 kDefaultGravity = {0_r, -9.8_r, 0_r};
44
45class Scene {
46 public:
47 [[nodiscard]] virtual Context* GetContext() = 0;
48
49 [[nodiscard]] virtual Context const* GetContext() const = 0;
50
51 virtual SceneHandle GetHandle() const = 0;
52
53 [[nodiscard]] virtual char const* GetName() const = 0;
54
55 [[nodiscard]] virtual Real3 GetGravity() const = 0;
56
57 virtual void SetGravity(Real3 const& gravity) = 0;
58
59 [[nodiscard]] virtual SolverParams GetSolverParams() const = 0;
60
61 virtual void SetSolverParams(SolverParams const& params, Error& error) = 0;
62
63 virtual void Step(double timeStepSec) = 0;
64
65 [[nodiscard]] virtual double GetLastTimeStep() const = 0;
66
67 [[nodiscard]] virtual double GetTotalSimulationTime() const = 0;
68
69 [[nodiscard]] virtual SolverStats GetSolverStats() const = 0;
70
71 [[nodiscard]] virtual PerformanceStats GetPerformanceStats() const = 0;
72
73 virtual void SetForceSingleIsland(bool forceSingleIsland) = 0;
74
75 [[nodiscard]] virtual bool GetForceSingleIsland() const = 0;
76
77 // State Snapshots *******************************************************************************
78
79 [[nodiscard]] virtual StateHandle CaptureState(Error& error) = 0;
80
81 virtual void RestoreState(StateHandle handle, bool releaseImmediately, Error& error) = 0;
82
83 virtual void CaptureStateToBytes(DynamicArray<uint8_t>& outData, Error& error) = 0;
84
85 virtual void RestoreStateFromBytes(Span<uint8_t const> data, Error& error) = 0;
86
87 virtual void ReleaseState(StateHandle handle) = 0;
88
89 virtual void ReleaseAllStates() = 0;
90
91 [[nodiscard]] virtual bool IsEqualState(StateHandle stateA, StateHandle stateB) const = 0;
92
93 virtual void CaptureStateToFile(std::string_view filePath, Error& error) = 0;
94
95 // Actors ****************************************************************************************
96
97 virtual Actor* CreateRigidActor(RigidActorParams const& params, Error& error) = 0;
98
99 virtual Actor* CreateSoftActor(SoftActorParams const& params, Error& error) = 0;
100
101 virtual Actor* CreateArticulatedActor(ArticulatedActorParams const& params, Error& error) = 0;
102
103 virtual Actor* CreateSoftSkinnedActor(SoftSkinnedActorParams const& params, Error& error) = 0;
104
105 virtual void DestroyActor(ActorHandle actor) = 0;
106
107 void DestroyActor(Actor* actor);
108
109 [[nodiscard]] virtual Actor* GetActor(ActorHandle actor) = 0;
110
111 [[nodiscard]] virtual Actor const* GetActor(ActorHandle actor) const = 0;
112
113 [[nodiscard]] virtual int GetNumActors() const = 0;
114
115 virtual void GetActors(Span<Actor*> outActors, Error& error) = 0;
116
117 virtual void GetActors(Span<Actor const*> outActors, Error& error) const = 0;
118
119 virtual void ForEachActor(std::function<void(Actor*)> const& callback) = 0;
120
121 virtual void ForEachActor(std::function<void(Actor const*)> const& callback) const = 0;
122
123 // Constraints ***********************************************************************************
124
127 Error& error) = 0;
128
131 Error& error) = 0;
132
135 Error& error) = 0;
136
139 Error& error) = 0;
140
143 Error& error) = 0;
144
147 Error& error) = 0;
148
151 Error& error) = 0;
152
155 Error& error) = 0;
156
159 Error& error) = 0;
160
163 Error& error) = 0;
164
167 Error& error) = 0;
168
171 Error& error) = 0;
172
175 Error& error) = 0;
176
179 Error& error) = 0;
180
183 Error& error) = 0;
184
185 [[nodiscard]] virtual Constraint* GetConstraint(ConstraintHandle constraint) = 0;
186
187 [[nodiscard]] virtual Constraint const* GetConstraint(ConstraintHandle constraint) const = 0;
188
189 virtual void DestroyConstraint(Constraint* constraint) = 0;
190
191 virtual void DestroyConstraint(ConstraintHandle constraint) = 0;
192
193 [[nodiscard]] virtual int GetNumConstraints() const = 0;
194
195 virtual void ForEachConstraint(std::function<void(Constraint*)> const& callback) = 0;
196
197 virtual void ForEachConstraint(std::function<void(Constraint const*)> const& callback) const = 0;
198
199 // Debugging ***********************************************************************************
200
201 [[nodiscard]] virtual DebugDraw& GetDebugDraw() = 0;
202
203 [[nodiscard]] virtual DebugDraw const& GetDebugDraw() const = 0;
204
205 virtual void UpdateDebugger() = 0;
206
207 // Recording ***********************************************************************************
208
209 [[nodiscard]] virtual bool IsRecording() const = 0;
210
211 virtual void
212 StartRecording(std::string_view filePath, RecordingParams const& params, Error& error) = 0;
213
214 virtual void StopRecording() = 0;
215
216 // Contact Layers *****************************************************************************
217
219 std::string_view layerA,
220 std::string_view layerB,
221 bool enable,
222 Error& error) = 0;
223
225 std::string_view layerA,
226 std::string_view layerB,
227 bool enable,
228 Error& error) = 0;
229
230 [[nodiscard]] virtual bool IsLayerContactEnabled(std::string_view layerA, std::string_view layerB)
231 const = 0;
232
233 [[nodiscard]] virtual int GetNumContactLayers() const = 0;
234
236 std::function<void(std::string_view name)> const& callback) const = 0;
237
239 ActorHandle colliding,
240 ActorHandle collider,
241 bool enable,
242 IncludeNestedActors includeNestedActors,
243 Error& error) = 0;
244
246 ActorHandle actorA,
247 ActorHandle actorB,
248 bool enable,
249 IncludeNestedActors includeNestedActors,
250 Error& error) = 0;
251
252 // Callbacks **********************************************************************************
253
254 static constexpr int kDefaultCallbackPriority = 100;
255
257 std::string_view debugName,
258 std::function<void(StepInfo const&)> callback,
259 int priority = kDefaultCallbackPriority) = 0;
260
262 std::string_view debugName,
263 std::function<void(StepInfo const&)> callback,
264 int priority = kDefaultCallbackPriority) = 0;
265
266 virtual void CancelCallback(CallbackHandle handle) = 0;
267
268 protected:
269 /// Don't delete the Scene pointer. Call @ref Context::DestroyScene.
270 virtual ~Scene() = default;
271};
272
273} // namespace superdex
274
275#include "mochi_scene_inl.h"
Represents a simulated body (actor) in a scene.
Definition mochi_actor.h:51
Represents a constraint that restricts the degrees of freedom of one or more actors.
Main entry point for SuperDex Physics simulation.
Manages debug visualization features for a scene.
A dynamically resizable array with syntax and behavior similar to std::pmr::vector.
Represents a simulation scene.
Definition mochi_scene.h:45
virtual void ReleaseState(StateHandle handle)=0
Release a StateHandle when you are done with it.
virtual int GetNumContactLayers() const =0
Get the number of unique contact layer names in the scene.
virtual void EnumerateContactLayerNames(std::function< void(std::string_view name)> const &callback) const =0
Enumerate all unique contact layer names in the scene.
virtual Context const * GetContext() const =0
Get read-only access to the Context that owns this scene.
virtual Actor const * GetActor(ActorHandle actor) const =0
Get a const pointer to the Actor associated with an ActorHandle.
virtual Constraint * CreateRigidSphericalJointConstraint(RigidSphericalJointConstraintParams const &params, Error &error)=0
Create a spherical joint constraint between two rigid actors.
virtual double GetLastTimeStep() const =0
Returns the time step size [s] from the most recent positive call to Step.
virtual char const * GetName() const =0
Get the name of the scene.
virtual Constraint * CreateRodElementRotationToRigidConstraint(RodElementRotationToRigidConstraintParams const &params, Error &error)=0
Create a rotation constraint between a rigid actor and a rod element.
virtual Context * GetContext()=0
Get the Context that owns this scene.
virtual Constraint * CreateRigidPrismaticJointConstraint(RigidPrismaticJointConstraintParams const &params, Error &error)=0
Create a prismatic joint constraint between two rigid actors.
virtual Constraint * CreateRigidPivotToRigidTargetConstraint(RigidPivotToRigidTargetConstraintParams const &params, Error &error)=0
Create a position constraint between a rigid actor's pivot point and the world position of that pivot...
virtual void ForEachConstraint(std::function< void(Constraint const *)> const &callback) const =0
Iterate over all the constraints in the scene using a callback (const version).
virtual void ReleaseAllStates()=0
Release memory for all captured state and invalidate all prior StateHandle objects.
virtual void RestoreState(StateHandle handle, bool releaseImmediately, Error &error)=0
Restore captured simulation state.
virtual Constraint * CreateArticulated3dRotationRangeConstraint(Articulated3dRotationRangeConstraintParams const &params, Error &error)=0
Create a range constraint on the DoFs of a 3D rotation joint of an articulated actor.
virtual CallbackHandle RegisterPostStepCallback(std::string_view debugName, std::function< void(StepInfo const &)> callback, int priority=kDefaultCallbackPriority)=0
Register a callback to execute after each simulation step.
virtual void DestroyConstraint(ConstraintHandle constraint)=0
Destroy a constraint and remove it from the scene.
virtual StateHandle CaptureState(Error &error)=0
Capture the simulation state that changes from step to step for later restoration.
virtual bool GetForceSingleIsland() const =0
Get whether all actors are forced to be in a single simulation island.
virtual CallbackHandle RegisterPreStepCallback(std::string_view debugName, std::function< void(StepInfo const &)> callback, int priority=kDefaultCallbackPriority)=0
Register a callback to execute before each simulation step.
virtual void ForEachActor(std::function< void(Actor const *)> const &callback) const =0
Iterate over all the actors in the scene using a callback (const version).
virtual DebugDraw & GetDebugDraw()=0
Get the debug draw interface for this scene.
virtual ~Scene()=default
Don't delete the Scene pointer. Call Context::DestroyScene.
virtual SolverStats GetSolverStats() const =0
Get solver convergence metrics from the last simulation step.
virtual void EnableLayerContactAsymmetric(std::string_view layerA, std::string_view layerB, bool enable, Error &error)=0
Enable or disable collision detection for an ordered pair of contact layer names.
virtual PerformanceStats GetPerformanceStats() const =0
Get timing and profiling metrics from the last simulation step.
virtual bool IsRecording() const =0
Check if the scene is currently recording.
virtual bool IsEqualState(StateHandle stateA, StateHandle stateB) const =0
Return true if both StateHandles are valid and refer to identical state information (full precision).
virtual SceneHandle GetHandle() const =0
Get the scene handle.
virtual void ForEachConstraint(std::function< void(Constraint *)> const &callback)=0
Iterate over all the constraints in the scene using a callback.
virtual void RestoreStateFromBytes(Span< uint8_t const > data, Error &error)=0
Restore simulation state from a binary byte buffer produced by CaptureStateToBytes.
virtual int GetNumConstraints() const =0
Get the number of constraints in the scene.
virtual Actor * CreateSoftSkinnedActor(SoftSkinnedActorParams const &params, Error &error)=0
Create a soft-skinned actor with an articulated skeleton.
virtual double GetTotalSimulationTime() const =0
Returns the total simulation time [s] in the scene.
virtual void SetSolverParams(SolverParams const &params, Error &error)=0
Set the solver parameters.
virtual void EnableActorContactAsymmetric(ActorHandle colliding, ActorHandle collider, bool enable, IncludeNestedActors includeNestedActors, Error &error)=0
Enable or disable collision detection between two actors.
virtual Real3 GetGravity() const =0
Get the gravity vector in world frame.
virtual void GetActors(Span< Actor * > outActors, Error &error)=0
Get Actor pointers for all the actors in the scene.
virtual void GetActors(Span< Actor const * > outActors, Error &error) const =0
Get const Actor pointers for all the actors in the scene.
virtual Constraint * CreateJointRotationRangeConstraint(JointRotationRangeConstraintParams const &params, Error &error)=0
Create a range constraint on joint rotation.
virtual void DestroyConstraint(Constraint *constraint)=0
Destroy a constraint and remove it from the scene.
virtual void SetGravity(Real3 const &gravity)=0
Set the gravity vector in world frame.
virtual void UpdateDebugger()=0
This gives the debugger a chance to process messages that require reading or writing scene state.
virtual void CaptureStateToBytes(DynamicArray< uint8_t > &outData, Error &error)=0
Capture simulation state to a binary byte buffer.
virtual void ForEachActor(std::function< void(Actor *)> const &callback)=0
Iterate over all the actors in the scene using a callback.
virtual void EnableActorContactSymmetric(ActorHandle actorA, ActorHandle actorB, bool enable, IncludeNestedActors includeNestedActors, Error &error)=0
Equivalent to calling EnableActorContactAsymmetric with actorA and actorB in both orders using the sa...
virtual Constraint * CreateArticulated3dRotationTargetConstraint(Articulated3dRotationTargetConstraintParams const &params, Error &error)=0
Create a target constraint on an articulated 3D rotation.
virtual void CancelCallback(CallbackHandle handle)=0
Cancel a previously registered callback.
virtual Constraint * CreateJointRotationTrackingConstraint(JointRotationTrackingConstraintParams const &params, Error &error)=0
Create a tracking constraint on joint rotation.
virtual void Step(double timeStepSec)=0
Advance the simulation of the Scene one step.
virtual Constraint * CreateDeformableNodeToRigidConstraint(DeformableNodeToRigidConstraintParams const &params, Error &error)=0
Create a constraint connecting a deformable node to a rigid actor.
virtual Actor * CreateRigidActor(RigidActorParams const &params, Error &error)=0
Create a rigid body actor.
virtual Actor * GetActor(ActorHandle actor)=0
Get the Actor pointer associated with an ActorHandle.
virtual void StartRecording(std::string_view filePath, RecordingParams const &params, Error &error)=0
Create or replace the specified file and start recording the scene to an HDF5 file.
virtual bool IsLayerContactEnabled(std::string_view layerA, std::string_view layerB) const =0
Check if collision detection is enabled for an ordered pair of contact layer names.
static constexpr int kDefaultCallbackPriority
Default priority for callback ordering.
virtual Constraint * CreateArticulatedSingleDofTargetConstraint(ArticulatedSingleDofTargetConstraintParams const &params, Error &error)=0
Create a target constraint on a single articulated DoF.
virtual Constraint * CreateRigidPivotPositionConstraint(RigidPivotPositionConstraintParams const &params, Error &error)=0
Create a position constraint on a rigid actor's pivot point.
virtual Constraint * CreateDeformableNodeToDeformableNodeConstraint(DeformableNodeToDeformableNodeConstraintParams const &params, Error &error)=0
Create a constraint connecting two deformable actor nodes.
virtual Constraint * CreateArticulatedSingleDofRangeConstraint(ArticulatedSingleDofRangeConstraintParams const &params, Error &error)=0
Create a range constraint on a single articulated DoF.
virtual Constraint * CreateRigidPivotRotationConstraint(RigidPivotRotationConstraintParams const &params, Error &error)=0
Create a rotation constraint on a rigid actor's pivot frame.
virtual int GetNumActors() const =0
Get the number of actors in the scene, including nested link actors and nested soft actors.
virtual Constraint * CreateDeformableNodePositionConstraint(DeformableNodePositionConstraintParams const &params, Error &error)=0
Create a position constraint on a deformable actor node.
virtual Actor * CreateSoftActor(SoftActorParams const &params, Error &error)=0
Create a soft deformable actor.
virtual void CaptureStateToFile(std::string_view filePath, Error &error)=0
Captures a snapshot of the state of a Scene, similar to Scene::CaptureState, then writes the data to ...
virtual Constraint * GetConstraint(ConstraintHandle constraint)=0
Get the Constraint pointer associated with a ConstraintHandle.
virtual SolverParams GetSolverParams() const =0
Get the solver parameters.
virtual Actor * CreateArticulatedActor(ArticulatedActorParams const &params, Error &error)=0
Create an articulated actor.
virtual void EnableLayerContactSymmetric(std::string_view layerA, std::string_view layerB, bool enable, Error &error)=0
Equivalent to calling EnableLayerContactAsymmetric with layerA and layerB in both orders.
virtual DebugDraw const & GetDebugDraw() const =0
Get a const reference to the debug draw interface for this scene.
virtual void DestroyActor(ActorHandle actor)=0
Destroy an actor and remove it from the scene.
virtual Constraint const * GetConstraint(ConstraintHandle constraint) const =0
Get the const Constraint pointer associated with a ConstraintHandle.
virtual void StopRecording()=0
Stop recording the scene and close the recording file.
virtual void SetForceSingleIsland(bool forceSingleIsland)=0
Enables/disables forcing all actors in the scene to be in a single simulation island.
IncludeNestedActors
Specifies whether actor-contact filtering also applies to nested actors.
NdArray< real, 3 > Real3
Definition nd_array.h:106
constexpr Real3 kDefaultGravity
Default gravity acceleration in world frame [m/s²].
Definition mochi_scene.h:43
Unique identifier for an Actor within a Scene.
Parameters for creating a range constraint on a 3D-rotation articulated joint.
Parameters for creating a target constraint on 3D rotation DoFs of an articulated actor.
Parameters to create an articulated actor.
Parameters for creating a range constraint on a single articulated DoF.
Parameters for creating a target constraint on a single articulated DoF.
Unique identifier for a callback registration within a Scene.
Unique identifier for a Constraint within a Scene.
Parameters for creating a position constraint on a deformable actor node.
Parameters for creating a constraint coupling a node in a deformable actor to a node in another defor...
Parameters for creating a constraint coupling a deformable node to a rigid actor.
Parameters for creating a joint rotation range constraint between two rigid actors.
Parameters for creating a joint rotation tracking constraint between two rigid actors.
Timing and profiling metrics from the last simulation step.
Parameters controlling what data gets included in a scene recording.
Parameters to create a rigid body actor.
Parameters for creating a position constraint on a rigid actor's pivot point.
Parameters for creating a rotation constraint on a rigid actor's pivot frame.
Parameters for creating a constraint on a rigid actor's pivot point.
Parameters for creating a prismatic joint between two rigid actors.
Parameters for creating a spherical joint between two rigid actors.
Parameters for creating a tracking constraint on the relative rotation between a rigid actor and a ro...
Unique identifier for a Scene within a Context.
Parameters to create a soft deformable actor.
Parameters to create soft-skinned actors with a shared articulated skeleton.
Simulation solver configuration parameters.
Solver convergence metrics from the last simulation step.
Unique identifier for a captured simulation state within a Scene.
Information passed to per-step callbacks.