Rapier C — 3D / f64 0.36.0+c.0
Loading...
Searching...
No Matches
rapier.hpp
Go to the documentation of this file.
1
5#ifndef RAPIER_HPP
6#define RAPIER_HPP
7#include "rapier_helpers.h"
8#include <memory>
9#include <stdexcept>
10#include <string>
11
13namespace rapier {
15inline void check(RAPIER_TYPE(Status) status) {
16 if (status != RAPIER_CONST(OK)) {
17 throw std::runtime_error(std::string(RAPIER_FN(LastError)()));
18 }
19}
20
21// Use Owner only for owned pointers, never for a borrowed callback context.
23template <class T, RAPIER_TYPE(Status) (*Free)(T *)> struct Deleter {
25 void operator()(T *value) const noexcept {
26 (void)Free(value);
27 }
28};
29
30template <class T, RAPIER_TYPE(Status) (*Free)(T *)>
31using Owner = std::unique_ptr<T, Deleter<T, Free>>;
33using World = Owner<RAPIER_TYPE(World), RAPIER_FN(FreeWorld)>;
35using Shape = Owner<RAPIER_TYPE(SharedShape), RAPIER_FN(FreeSharedShape)>;
39using Snapshot = Owner<RAPIER_TYPE(Bytes), RAPIER_FN(FreeBytes)>;
40
44using ShapeMesh = Owner<RAPIER_TYPE(ShapeMesh), RAPIER_FN(FreeShapeMesh)>;
47 Owner<RAPIER_TYPE(KinematicCharacterController), RAPIER_FN(FreeKinematicCharacterController)>;
50#if defined(RAPIER_DIM3)
54 RAPIER_FN(FreeDynamicRayCastVehicleController)>;
57#endif
58#if defined(RAPIER_ROBOTICS) && defined(RAPIER_DIM3) && defined(RAPIER_F32)
60using UrdfRobot = Owner<RAPIER_TYPE(UrdfRobot), RAPIER_FN(FreeUrdfRobot)>;
62using UrdfRobotHandles = Owner<RAPIER_TYPE(UrdfRobotHandles), RAPIER_FN(FreeUrdfRobotHandles)>;
64using MjcfRobot = Owner<RAPIER_TYPE(MjcfRobot), RAPIER_FN(FreeMjcfRobot)>;
66using MjcfRobotHandles = Owner<RAPIER_TYPE(MjcfRobotHandles), RAPIER_FN(FreeMjcfRobotHandles)>;
67#endif
68
69// These factories return ordinary values. No allocation or deleter is needed.
71inline RAPIER_TYPE(RigidBodyDesc) rigid_body(uint32_t kind = RAPIER_CONST(DYNAMIC)) {
72 RAPIER_TYPE(RigidBodyDesc) value = RAPIER_FN(DynamicRigidBodyDesc)();
73 value.bodyType = kind;
74 return value;
75}
76
78inline RAPIER_TYPE(ColliderDesc) ball(RAPIER_TYPE(Real) radius) {
79 return RAPIER_FN(BallColliderDesc)(radius);
80}
81
83inline RAPIER_TYPE(ColliderDesc) cuboid(RAPIER_TYPE(Vector) half_extents) {
84 return RAPIER_FN(CuboidColliderDesc)(half_extents);
85}
86
88inline RAPIER_TYPE(SoftBodyDesc) soft_body() {
89 return RAPIER_FN(DefaultSoftBodyDesc)();
90}
91
93inline RAPIER_TYPE(JointDesc) joint(uint8_t locked_axes = 0) {
94 RAPIER_TYPE(JointDesc) value = RAPIER_FN(DefaultJointDesc)();
95 value.lockedAxes = locked_axes;
96 return value;
97}
98
100inline RAPIER_TYPE(QueryOptions) queryOptions() {
101 return RAPIER_FN(DefaultQueryOptions)();
102}
103
106 check(RAPIER_FN(CheckAbi)(RAPIER_CONST(ABI_VERSION), RAPIER_CONST(DIMENSION),
107 sizeof(RAPIER_TYPE(Real)), sizeof(RAPIER_TYPE(Vector)),
108 sizeof(RAPIER_TYPE(Pose)), RAPIER_CONST(ABI_FEATURES)));
109 RAPIER_TYPE(World) *value = RAPIER_FN(NewWorld)();
110 check(RAPIER_FN(LastStatus)());
111 return World(value);
112}
113} // namespace rapier
114#endif
Optional C++ ownership helpers.
Definition rapier.hpp:13
Owner< R3SoftBodyTearEvent, r3FreeSoftBodyTearEvent > SoftBodyTearEvent
Own a native SoftBodyTearEvent using its matching Free function.
Definition rapier.hpp:42
R3JointDesc joint(uint8_t locked_axes=0)
Return a joint description with the selected locked-axis mask.
Definition rapier.hpp:93
World make_world()
Check the ABI and create an owned world; throw on failure.
Definition rapier.hpp:105
R3ColliderDesc ball(R3Real radius)
Return a POD ball collider description.
Definition rapier.hpp:78
R3ColliderDesc cuboid(R3Vector half_extents)
Return a POD cuboid collider description.
Definition rapier.hpp:83
R3SoftBodyDesc soft_body()
Return native soft-body description defaults.
Definition rapier.hpp:88
Owner< R3SharedShape, r3FreeSharedShape > Shape
Own a native Shape using its matching Free function.
Definition rapier.hpp:35
R3QueryOptions queryOptions()
Return query defaults with no callback or exclusions.
Definition rapier.hpp:100
Owner< R3TriMeshData, r3FreeTriMeshData > TriMeshData
Own a native TriMeshData using its matching Free function.
Definition rapier.hpp:56
std::unique_ptr< T, Deleter< T, Free > > Owner
Unique owner of a native pointer; never wrap a borrowed pointer.
Definition rapier.hpp:31
Owner< R3World, r3FreeWorld > World
Own a native World using its matching Free function.
Definition rapier.hpp:33
Owner< R3ShapeMesh, r3FreeShapeMesh > ShapeMesh
Own a native ShapeMesh using its matching Free function.
Definition rapier.hpp:44
R3RigidBodyDesc rigid_body(uint32_t kind=R3_DYNAMIC)
Return a POD rigid-body description with the selected body type.
Definition rapier.hpp:71
Owner< R3EventCollector, r3FreeEventCollector > EventCollector
Own a native EventCollector using its matching Free function.
Definition rapier.hpp:37
Owner< R3DynamicRayCastVehicleController, r3FreeDynamicRayCastVehicleController > DynamicRayCastVehicleController
Own a native DynamicRayCastVehicleController using its matching Free function.
Definition rapier.hpp:52
Owner< R3KinematicCharacterController, r3FreeKinematicCharacterController > KinematicCharacterController
Own a native KinematicCharacterController using its matching Free function.
Definition rapier.hpp:46
Owner< R3PidController, r3FreePidController > PidController
Own a native PidController using its matching Free function.
Definition rapier.hpp:49
Owner< R3Bytes, r3FreeBytes > Snapshot
Own a native Snapshot using its matching Free function.
Definition rapier.hpp:39
void check(R3Status status)
Throw std::runtime_error with LastError when status is not OK.
Definition rapier.hpp:15
#define RAPIER_CONST(name)
Select an R2/R3 public constant name.
Definition rapier.h:32
#define RAPIER_FN(name)
Select an r2/r3 public function name.
Definition rapier.h:34
#define RAPIER_TYPE(name)
Select an R2/R3 public type name.
Definition rapier.h:30
Explicit invalid handle values; no owned resources.
Deleter for an owned pointer; Free must succeed at destruction time.
Definition rapier.hpp:23
void operator()(T *value) const noexcept
Release the pointer through its matching C API function.
Definition rapier.hpp:25