1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// PxBase hierarchy
#[rustfmt::skip]
use physx_sys::{
    PxActor,
    PxAggregate,
    PxArticulationJointReducedCoordinate,
    PxArticulationLink,
    PxArticulationReducedCoordinate,
    PxBVH,
    PxBase,
    PxConstraint,
    PxContactJoint,
    PxConvexMesh,
    PxD6Joint,
    PxDistanceJoint,
    PxFixedJoint,
    PxHeightField,
    PxJoint,
    PxMaterial,
    PxPrismaticJoint,
    PxPruningStructure,
    PxRefCounted,
    PxRevoluteJoint,
    PxRigidActor,
    PxRigidBody,
    PxRigidDynamic,
    PxRigidStatic,
    PxShape,
    PxSphericalJoint,
    PxTriangleMesh,
};

// Main classes
#[rustfmt::skip]
use physx_sys::{
    PxCooking,
    PxFoundation,
    PxPhysics,
    PxPvd,
    PxPvdSceneClient,
    PxPvdTransport,
    PxScene,
};

// Controller stuff
#[rustfmt::skip]
use physx_sys::{
    PxBoxController,
    PxBoxControllerDesc,
    PxCapsuleController,
    PxCapsuleControllerDesc,
    PxController,
    PxControllerDesc,
    PxControllerManager,
};

// Math
#[rustfmt::skip]
use physx_sys::{
    PxBounds3,
    PxExtendedVec3,
    PxMeshScale,
    PxQuat,
    PxTransform,
    PxVec3,
};

// Geometries
#[rustfmt::skip]
use physx_sys::{
    PxBoxGeometry,
    PxCapsuleGeometry,
    PxConvexMeshGeometry,
    PxGeometry,
    PxHeightFieldGeometry,
    PxPlaneGeometry,
    PxSphereGeometry,
    PxTriangleMeshGeometry,
};

// Misc
use physx_sys::PxArticulationCache;

////////////////////////////////////////////////
// Class
/// Trait for getting raw pointers for FFI calls, used to provide default implementations
/// of traits that "re-object" a C wrapper of a C++ library.
///
/// # Safety
/// **Implementing `Class<S>` for `T` where `S` is not a superclass of `T` will cause Undefined Behaviour.**
///
/// This trait may hide a raw pointer cast from `*Self` to `*T`.  It is intended for use in
/// default implementations for traits wrapping C++ classes.  In C++-land this is just how
/// things are done, but From Rust's perspective, this is madness.
/// The relations defined between types using this trait must align with the C++ class hierarchy.
/// The [Inherit] trait can be used to simplify implementing `Class<S> for T where T: Class<T>`.
pub unsafe trait Class<S> {
    /// Returns a raw const pointer to the wrapped type.
    /// Retrieving a raw pointer is safe.  However, pretty much any use of a raw pointer is unsafe.
    /// In particular: this pointer should not be used to construct a second owning wrapper around the pointer.
    fn as_ptr(&self) -> *const S;

    /// Returns a raw mut pointer to the wrapped type.
    /// Retrieving a raw pointer is safe.  However, pretty much any use of a raw pointer is unsafe.
    /// In particular: this pointer should not be used to construct a second owning wrapper around the pointer.
    fn as_mut_ptr(&mut self) -> *mut S;
}

#[macro_export(crate)]
/// Macro for quickly defining Class<...> impls for new type wrappers.
/// The type must be repr(transparent), and have the Px object in a field
/// named obj.  Will not work if the type parameters have trait bounds.
macro_rules! DeriveClassForNewType {
    ($PxWrap:ident : $($PxClass:ident),+) => {
        $(unsafe impl Class<::physx_sys::$PxClass> for $PxWrap {
            #[inline]
            fn as_ptr(&self) -> *const ::physx_sys::$PxClass {
                &self.obj as *const _ as *const _
            }

            #[inline]
            fn as_mut_ptr(&mut self) -> *mut ::physx_sys::$PxClass {
                &mut self.obj as *mut _ as *mut _
            }
        })+
    }
}

/// Derive Class<T> for the raw Px* types.
macro_rules! DeriveClass {
    ($PxType:ty $(: $($PxClass:ty),*)?) => {
        unsafe impl Class<$PxType> for $PxType {
            #[inline]
            fn as_ptr(&self) -> *const $PxType {
                self
            }

            #[inline]
            fn as_mut_ptr(&mut self) -> *mut $PxType {
                self
            }
        }

        $($(unsafe impl Class<$PxClass> for $PxType {
            #[inline]
            fn as_ptr(&self) -> *const $PxClass {
                self as *const _ as *const _
            }

            #[inline]
            fn as_mut_ptr(&mut self) -> *mut $PxClass {
                self as *mut _ as *mut _
            }
        })*)?
    }
}

DeriveClass!(PxBase);
DeriveClass!(PxActor: PxBase);
DeriveClass!(PxRigidActor: PxActor, PxBase);
DeriveClass!(PxRigidStatic: PxRigidActor, PxActor, PxBase);
DeriveClass!(PxRigidBody: PxRigidActor, PxActor, PxBase);
DeriveClass!(PxRigidDynamic: PxRigidBody, PxRigidActor, PxActor, PxBase);
DeriveClass!(
    PxArticulationLink: PxRigidBody,
    PxRigidActor,
    PxActor,
    PxBase
);
DeriveClass!(PxAggregate: PxBase);
DeriveClass!(PxArticulationReducedCoordinate: PxBase);
DeriveClass!(PxArticulationJointReducedCoordinate: PxBase);
DeriveClass!(PxBVH: PxBase);
DeriveClass!(PxConstraint: PxBase);
DeriveClass!(PxConvexMesh: PxBase);
DeriveClass!(PxHeightField: PxBase);
DeriveClass!(PxJoint: PxBase);
DeriveClass!(PxContactJoint: PxJoint, PxBase);
DeriveClass!(PxD6Joint: PxJoint, PxBase);
DeriveClass!(PxDistanceJoint: PxJoint, PxBase);
DeriveClass!(PxFixedJoint: PxJoint, PxBase);
DeriveClass!(PxPrismaticJoint: PxJoint, PxBase);
DeriveClass!(PxRevoluteJoint: PxJoint, PxBase);
DeriveClass!(PxSphericalJoint: PxJoint, PxBase);
DeriveClass!(PxMaterial: PxRefCounted, PxBase);
DeriveClass!(PxPruningStructure: PxBase);
DeriveClass!(PxShape: PxRefCounted, PxBase);
DeriveClass!(PxTriangleMesh: PxBase);

DeriveClass!(PxFoundation);
DeriveClass!(PxPhysics);
DeriveClass!(PxScene);
DeriveClass!(PxPvd);
DeriveClass!(PxPvdSceneClient);
DeriveClass!(PxPvdTransport);
DeriveClass!(PxCooking);

DeriveClass!(PxControllerManager);
DeriveClass!(PxController);
DeriveClass!(PxCapsuleController: PxController);
DeriveClass!(PxBoxController: PxController);
DeriveClass!(PxControllerDesc);
DeriveClass!(PxCapsuleControllerDesc: PxControllerDesc);
DeriveClass!(PxBoxControllerDesc: PxControllerDesc);

DeriveClass!(PxTransform);
DeriveClass!(PxQuat);
DeriveClass!(PxVec3);
DeriveClass!(PxExtendedVec3);
DeriveClass!(PxMeshScale);
DeriveClass!(PxBounds3);

DeriveClass!(PxArticulationCache);

macro_rules! DeriveClassForGeometries {
    ($($Px: ty,)*) => {
        $(
            unsafe impl Class<$Px> for $Px {
                #[inline]
                fn as_ptr(&self) -> *const $Px {
                    self
                }

                #[inline]
                fn as_mut_ptr(&mut self) -> *mut $Px {
                    self
                }
            }
            unsafe impl Class<PxGeometry> for $Px {
                #[inline]
                fn as_ptr(&self) -> *const PxGeometry {
                    self as *const _ as *const _
                }

                #[inline]
                fn as_mut_ptr(&mut self) -> *mut PxGeometry {
                    self as *mut _ as *mut _
                }
            }
        )*
    }
}

DeriveClassForGeometries!(
    PxSphereGeometry,
    PxBoxGeometry,
    PxCapsuleGeometry,
    PxHeightFieldGeometry,
    PxTriangleMeshGeometry,
    PxConvexMeshGeometry,
    PxPlaneGeometry,
);