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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use crate::{
    bvh_structure::BVHStructure, convex_mesh::ConvexMesh, foundation::Foundation,
    height_field::HeightField, owner::Owner, physics::Physics, traits::Class,
    triangle_mesh::TriangleMesh,
};

use physx_sys::{
    phys_PxCreateCooking,
    PxBVHStructureDesc_isValid,
    PxBVHStructureDesc_new,
    PxBVHStructureDesc_setToDefault_mut,
    PxConvexMeshDesc_isValid,
    PxConvexMeshDesc_new,
    PxConvexMeshDesc_setToDefault_mut,
    PxCookingParams_new,
    //PxCooking_getParams,
    //PxCooking_setParams_mut,
    //PxCooking_platformMismatch,
    //PxCooking_computeHullPolygons,
    //PxCooking_cookBVHStructure,
    //PxCooking_cookConvexMesh,
    //PxCooking_cookHeightField,
    //PxCooking_cookTriangleMesh,
    PxCooking_createBVHStructure,
    PxCooking_createConvexMesh,
    PxCooking_createHeightField,
    PxCooking_createTriangleMesh,
    PxCooking_release_mut,
    PxCooking_validateConvexMesh,
    PxCooking_validateTriangleMesh,
    PxHeightFieldDesc_isValid,
    PxHeightFieldDesc_new,
    PxHeightFieldDesc_setToDefault_mut,
    PxTriangleMeshDesc_isValid,
    PxTriangleMeshDesc_new,
    PxTriangleMeshDesc_setToDefault_mut,
};

/// A new-type wrapper around `physx_sys::PxCooking`.
pub struct PxCooking {
    obj: physx_sys::PxCooking,
}

unsafe impl Class<physx_sys::PxCooking> for PxCooking {
    fn as_ptr(&self) -> *const physx_sys::PxCooking {
        &self.obj
    }

    fn as_mut_ptr(&mut self) -> *mut physx_sys::PxCooking {
        &mut self.obj
    }
}

impl Drop for PxCooking {
    fn drop(&mut self) {
        unsafe { PxCooking_release_mut(self.as_mut_ptr()) }
    }
}

unsafe impl Send for PxCooking {}
unsafe impl Sync for PxCooking {}

impl PxCooking {
    /// Create a new cooking instance.
    pub fn new(foundation: &mut impl Foundation, params: &PxCookingParams) -> Option<Owner<Self>> {
        unsafe {
            Owner::from_raw(phys_PxCreateCooking(
                crate::physics::PX_PHYSICS_VERSION,
                foundation.as_mut_ptr(),
                params.as_ptr(),
            ) as *mut _)
        }
    }

    /// Cook a new BVH structure.
    pub fn create_bvh_structure(
        &self,
        physics: &mut impl Physics,
        desc: &PxBVHStructureDesc,
    ) -> Option<Owner<BVHStructure>> {
        unsafe {
            BVHStructure::from_raw(PxCooking_createBVHStructure(
                self.as_ptr(),
                desc.as_ptr(),
                physics.get_physics_insertion_callback()?,
            ))
        }
    }

    /// Cook a new convex mesh using a mesh descriptor.
    /// The data provided in the mesh descriptor should be validated by [`PxCooking::validate_convex_mesh`] before being
    /// passed into this method.
    pub fn create_convex_mesh(
        &self,
        physics: &mut impl Physics,
        desc: &PxConvexMeshDesc,
    ) -> ConvexMeshCookingResult {
        if !desc.is_valid() {
            return ConvexMeshCookingResult::InvalidDescriptor;
        };
        if let Some(callback) = physics.get_physics_insertion_callback() {
            let mut result = physx_sys::PxConvexMeshCookingResult::eFAILURE;
            let ptr = unsafe {
                ConvexMesh::from_raw(PxCooking_createConvexMesh(
                    self.as_ptr(),
                    desc.as_ptr(),
                    callback,
                    &mut result,
                ))
            };
            ConvexMeshCookingResult::from_raw(result, ptr)
        } else {
            ConvexMeshCookingResult::Failure
        }
    }

    /// Cook a new height field.
    pub fn create_height_field(
        &self,
        physics: &mut impl Physics,
        desc: &PxHeightFieldDesc,
    ) -> Option<Owner<HeightField>> {
        unsafe {
            HeightField::from_raw(PxCooking_createHeightField(
                self.as_ptr(),
                desc.as_ptr(),
                physics.get_physics_insertion_callback()?,
            ))
        }
    }

    /// Cook a new triangle mesh using a mesh descriptor.
    /// The data provided in the mesh descriptor should be validated by [`PxCooking::validate_triangle_mesh`] before being
    /// passed into this method.
    pub fn create_triangle_mesh(
        &self,
        physics: &mut impl Physics,
        desc: &PxTriangleMeshDesc,
    ) -> TriangleMeshCookingResult {
        if !desc.is_valid() {
            return TriangleMeshCookingResult::InvalidDescriptor;
        };
        if let Some(callback) = physics.get_physics_insertion_callback() {
            let mut result = physx_sys::PxTriangleMeshCookingResult::eFAILURE;
            let ptr = unsafe {
                TriangleMesh::from_raw(PxCooking_createTriangleMesh(
                    self.as_ptr(),
                    desc.as_ptr(),
                    callback,
                    &mut result,
                ))
            };
            TriangleMeshCookingResult::from_raw(result, ptr)
        } else {
            TriangleMeshCookingResult::Failure
        }
    }

    /// Validate a convex mesh descriptor.
    pub fn validate_convex_mesh(&self, desc: &PxConvexMeshDesc) -> bool {
        unsafe { PxCooking_validateConvexMesh(self.as_ptr(), desc.as_ptr()) }
    }

    /// Validate a triangle mesh descriptor.
    pub fn validate_triangle_mesh(&self, desc: &PxTriangleMeshDesc) -> bool {
        unsafe { PxCooking_validateTriangleMesh(self.as_ptr(), desc.as_ptr()) }
    }
}

pub enum ConvexMeshCookingResult {
    Success(Owner<ConvexMesh>),
    ZeroAreaTestFailed,
    PolygonsLimitReached,
    Failure,
    InvalidDescriptor,
}

impl ConvexMeshCookingResult {
    fn from_raw(
        px_result: physx_sys::PxConvexMeshCookingResult::Enum,
        ptr: Option<Owner<ConvexMesh>>,
    ) -> Self {
        match px_result {
            physx_sys::PxConvexMeshCookingResult::eSUCCESS => {
                if let Some(ptr) = ptr {
                    ConvexMeshCookingResult::Success(ptr)
                } else {
                    ConvexMeshCookingResult::Failure
                }
            }
            physx_sys::PxConvexMeshCookingResult::eZERO_AREA_TEST_FAILED => {
                ConvexMeshCookingResult::ZeroAreaTestFailed
            }
            physx_sys::PxConvexMeshCookingResult::ePOLYGONS_LIMIT_REACHED => {
                ConvexMeshCookingResult::PolygonsLimitReached
            }
            physx_sys::PxConvexMeshCookingResult::eFAILURE => ConvexMeshCookingResult::Failure,
            _ => unreachable!(
                "invalid PxConvexMeshCookingResult enum variant: {:?}",
                px_result
            ),
        }
    }
}

pub enum TriangleMeshCookingResult {
    Success(Owner<TriangleMesh>),
    LargeTriangle,
    Failure,
    InvalidDescriptor,
}

impl TriangleMeshCookingResult {
    fn from_raw(
        px_result: physx_sys::PxTriangleMeshCookingResult::Enum,
        ptr: Option<Owner<TriangleMesh>>,
    ) -> Self {
        match px_result {
            physx_sys::PxTriangleMeshCookingResult::eSUCCESS => {
                if let Some(ptr) = ptr {
                    TriangleMeshCookingResult::Success(ptr)
                } else {
                    TriangleMeshCookingResult::Failure
                }
            }
            physx_sys::PxTriangleMeshCookingResult::eLARGE_TRIANGLE => {
                TriangleMeshCookingResult::LargeTriangle
            }
            physx_sys::PxTriangleMeshCookingResult::eFAILURE => TriangleMeshCookingResult::Failure,
            _ => unreachable!(
                "invalid PxTriangleMeshCookingResult enum variant: {:?}",
                px_result
            ),
        }
    }
}

pub struct PxConvexMeshDesc {
    pub obj: physx_sys::PxConvexMeshDesc,
}

unsafe impl Class<physx_sys::PxConvexMeshDesc> for PxConvexMeshDesc {
    fn as_ptr(&self) -> *const physx_sys::PxConvexMeshDesc {
        &self.obj
    }

    fn as_mut_ptr(&mut self) -> *mut physx_sys::PxConvexMeshDesc {
        &mut self.obj
    }
}

impl Default for PxConvexMeshDesc {
    fn default() -> Self {
        Self::new()
    }
}

impl PxConvexMeshDesc {
    /// Create a new convex mesh descriptor.
    pub fn new() -> Self {
        unsafe {
            Self {
                obj: PxConvexMeshDesc_new(),
            }
        }
    }

    /// Check if the descriptor is valid.
    pub fn is_valid(&self) -> bool {
        unsafe { PxConvexMeshDesc_isValid(self.as_ptr()) }
    }

    /// Set the descriptor to its default values.
    pub fn set_to_default(&mut self) -> &mut Self {
        unsafe {
            PxConvexMeshDesc_setToDefault_mut(self.as_mut_ptr());
        }
        self
    }
}

pub struct PxTriangleMeshDesc {
    pub obj: physx_sys::PxTriangleMeshDesc,
}

unsafe impl Class<physx_sys::PxTriangleMeshDesc> for PxTriangleMeshDesc {
    fn as_ptr(&self) -> *const physx_sys::PxTriangleMeshDesc {
        &self.obj
    }

    fn as_mut_ptr(&mut self) -> *mut physx_sys::PxTriangleMeshDesc {
        &mut self.obj
    }
}

impl Default for PxTriangleMeshDesc {
    fn default() -> Self {
        Self::new()
    }
}

impl PxTriangleMeshDesc {
    /// Create a new triangle mesh descriptor.
    pub fn new() -> Self {
        unsafe {
            Self {
                obj: PxTriangleMeshDesc_new(),
            }
        }
    }

    /// Check if the descriptor is valid.
    pub fn is_valid(&self) -> bool {
        unsafe { PxTriangleMeshDesc_isValid(self.as_ptr()) }
    }

    /// Set the descriptor to its default values.
    pub fn set_to_default(&mut self) -> &mut Self {
        unsafe {
            PxTriangleMeshDesc_setToDefault_mut(self.as_mut_ptr());
        }
        self
    }
}

pub struct PxHeightFieldDesc {
    pub obj: physx_sys::PxHeightFieldDesc,
}

unsafe impl Class<physx_sys::PxHeightFieldDesc> for PxHeightFieldDesc {
    fn as_ptr(&self) -> *const physx_sys::PxHeightFieldDesc {
        &self.obj
    }

    fn as_mut_ptr(&mut self) -> *mut physx_sys::PxHeightFieldDesc {
        &mut self.obj
    }
}

impl Default for PxHeightFieldDesc {
    fn default() -> Self {
        Self::new()
    }
}

impl PxHeightFieldDesc {
    /// Create a new height field descriptor.
    pub fn new() -> Self {
        unsafe {
            Self {
                obj: PxHeightFieldDesc_new(),
            }
        }
    }

    /// Check if the descriptor is valid.
    pub fn is_valid(&self) -> bool {
        unsafe { PxHeightFieldDesc_isValid(self.as_ptr()) }
    }

    /// Set the descriptor to its default values.
    pub fn set_to_default(&mut self) -> &mut Self {
        unsafe {
            PxHeightFieldDesc_setToDefault_mut(self.as_mut_ptr());
        }
        self
    }
}

pub struct PxBVHStructureDesc {
    pub obj: physx_sys::PxBVHStructureDesc,
}

unsafe impl Class<physx_sys::PxBVHStructureDesc> for PxBVHStructureDesc {
    fn as_ptr(&self) -> *const physx_sys::PxBVHStructureDesc {
        &self.obj
    }

    fn as_mut_ptr(&mut self) -> *mut physx_sys::PxBVHStructureDesc {
        &mut self.obj
    }
}

impl Default for PxBVHStructureDesc {
    fn default() -> Self {
        Self::new()
    }
}

impl PxBVHStructureDesc {
    /// Create a new BVH structure descriptor.
    pub fn new() -> Self {
        unsafe {
            Self {
                obj: PxBVHStructureDesc_new(),
            }
        }
    }

    /// Check if the descriptor is valid.
    pub fn is_valid(&self) -> bool {
        unsafe { PxBVHStructureDesc_isValid(self.as_ptr()) }
    }

    /// Set the descriptor to its default values.
    pub fn set_to_default(&mut self) -> &mut Self {
        unsafe {
            PxBVHStructureDesc_setToDefault_mut(self.as_mut_ptr());
        }
        self
    }
}

pub struct PxCookingParams {
    pub obj: physx_sys::PxCookingParams,
}

unsafe impl Class<physx_sys::PxCookingParams> for PxCookingParams {
    fn as_ptr(&self) -> *const physx_sys::PxCookingParams {
        &self.obj
    }

    fn as_mut_ptr(&mut self) -> *mut physx_sys::PxCookingParams {
        &mut self.obj
    }
}

impl PxCookingParams {
    /// Create a new cooking params.
    pub fn new(physics: &impl Physics) -> Option<Self> {
        unsafe {
            physics.get_tolerances_scale().map(|tolerances| Self {
                obj: PxCookingParams_new(tolerances),
            })
        }
    }
}