Skip to content

Commit bbfa160

Browse files
rlidwkatgolsson
andauthored
high-level API for ConvexMesh, TriangleMesh, HeightField (#195)
* high-level API for ConvexMesh, TriangleMesh, HeightField --------- Co-authored-by: Tom Solberg <[email protected]>
1 parent 4cbf2c2 commit bbfa160

File tree

3 files changed

+543
-6
lines changed

3 files changed

+543
-6
lines changed

physx/src/convex_mesh.rs

Lines changed: 137 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
1-
use crate::{owner::Owner, traits::Class};
1+
use std::mem::MaybeUninit;
22

3-
use physx_sys::PxConvexMesh_release_mut;
3+
use crate::{
4+
math::{PxBounds3, PxVec3},
5+
owner::Owner,
6+
traits::Class,
7+
};
8+
9+
use physx_sys::{
10+
PxConvexMesh_getIndexBuffer,
11+
PxConvexMesh_getLocalBounds,
12+
PxConvexMesh_getMassInformation,
13+
PxConvexMesh_getNbPolygons,
14+
PxConvexMesh_getNbVertices,
15+
PxConvexMesh_getPolygonData,
16+
PxConvexMesh_getVertices,
17+
// TODO: SDF getters
18+
//PxConvexMesh_getSDF,
19+
//PxConvexMesh_getConcreteTypeName,
20+
PxConvexMesh_isGpuCompatible,
21+
PxConvexMesh_release_mut,
22+
// TODO: high level wrapper for PxMassProperties
23+
PxMassProperties,
24+
};
425

526
#[repr(transparent)]
627
pub struct ConvexMesh {
@@ -18,6 +39,90 @@ impl ConvexMesh {
1839
pub unsafe fn from_raw(ptr: *mut physx_sys::PxConvexMesh) -> Option<Owner<ConvexMesh>> {
1940
unsafe { Owner::from_raw(ptr as *mut Self) }
2041
}
42+
43+
/// Returns the number of vertices.
44+
pub fn get_nb_vertices(&self) -> u32 {
45+
unsafe { PxConvexMesh_getNbVertices(self.as_ptr()) }
46+
}
47+
48+
/// Returns the vertices.
49+
pub fn get_vertices(&self) -> &[PxVec3] {
50+
unsafe {
51+
std::slice::from_raw_parts(
52+
PxConvexMesh_getVertices(self.as_ptr()) as *const PxVec3,
53+
self.get_nb_vertices() as usize,
54+
)
55+
}
56+
}
57+
58+
/// Returns the index buffer.
59+
pub fn get_index_buffer(&self) -> &[u8] {
60+
let polygon_count = self.get_nb_polygons();
61+
62+
// for each polygon index buffer contains its points,
63+
// so we take last polygon's index offset plus its length to calculate total size
64+
let index_buffer_length = if polygon_count > 0 {
65+
let last_polygon = self.get_polygon_data(polygon_count - 1).unwrap();
66+
last_polygon.index_base as usize + last_polygon.nb_verts as usize
67+
} else {
68+
0
69+
};
70+
71+
unsafe {
72+
std::slice::from_raw_parts(
73+
PxConvexMesh_getIndexBuffer(self.as_ptr()),
74+
index_buffer_length,
75+
)
76+
}
77+
}
78+
79+
/// Returns the number of polygons.
80+
pub fn get_nb_polygons(&self) -> u32 {
81+
unsafe { PxConvexMesh_getNbPolygons(self.as_ptr()) }
82+
}
83+
84+
/// Returns the polygon data.
85+
pub fn get_polygon_data(&self, index: u32) -> Option<HullPolygon> {
86+
let mut polygon = MaybeUninit::uninit();
87+
88+
if unsafe { PxConvexMesh_getPolygonData(self.as_ptr(), index, polygon.as_mut_ptr()) } {
89+
Some(unsafe { polygon.assume_init() }.into())
90+
} else {
91+
None
92+
}
93+
}
94+
95+
/// Returns the mass properties of the mesh assuming unit density.
96+
pub fn get_mass_information(&self) -> PxMassProperties {
97+
let mut mass = MaybeUninit::uninit();
98+
let mut local_inertia = MaybeUninit::uninit();
99+
let mut local_center_of_mass = MaybeUninit::uninit();
100+
101+
unsafe {
102+
PxConvexMesh_getMassInformation(
103+
self.as_ptr(),
104+
mass.as_mut_ptr(),
105+
local_inertia.as_mut_ptr(),
106+
local_center_of_mass.as_mut_ptr(),
107+
);
108+
109+
PxMassProperties {
110+
inertiaTensor: local_inertia.assume_init(),
111+
centerOfMass: local_center_of_mass.assume_init(),
112+
mass: mass.assume_init(),
113+
}
114+
}
115+
}
116+
117+
/// Returns the local-space (vertex space) AABB from the convex mesh.
118+
pub fn get_local_bounds(&self) -> PxBounds3 {
119+
unsafe { PxConvexMesh_getLocalBounds(self.as_ptr()) }.into()
120+
}
121+
122+
/// This method decides whether a convex mesh is gpu compatible.
123+
pub fn is_gpu_compatible(&self) -> bool {
124+
unsafe { PxConvexMesh_isGpuCompatible(self.as_ptr()) }
125+
}
21126
}
22127

23128
unsafe impl Send for ConvexMesh {}
@@ -28,3 +133,33 @@ impl Drop for ConvexMesh {
28133
unsafe { PxConvexMesh_release_mut(self.as_mut_ptr()) }
29134
}
30135
}
136+
137+
#[derive(Debug, Clone)]
138+
pub struct HullPolygon {
139+
/// Plane equation for this polygon.
140+
pub plane: [f32; 4],
141+
/// Number of vertices/edges in the polygon.
142+
pub nb_verts: u16,
143+
/// Offset in index buffer.
144+
pub index_base: u16,
145+
}
146+
147+
impl From<physx_sys::PxHullPolygon> for HullPolygon {
148+
fn from(value: physx_sys::PxHullPolygon) -> Self {
149+
Self {
150+
plane: value.mPlane,
151+
nb_verts: value.mNbVerts,
152+
index_base: value.mIndexBase,
153+
}
154+
}
155+
}
156+
157+
impl From<HullPolygon> for physx_sys::PxHullPolygon {
158+
fn from(value: HullPolygon) -> Self {
159+
Self {
160+
mPlane: value.plane,
161+
mNbVerts: value.nb_verts,
162+
mIndexBase: value.index_base,
163+
}
164+
}
165+
}

0 commit comments

Comments
 (0)