bevy_rapier3d/geometry/shape_views/convex_polyhedron.rs
1use crate::math::Vect;
2use rapier::parry::shape::ConvexPolyhedron;
3
4/// Read-only access to the properties of a convex polyhedron.
5#[derive(Copy, Clone)]
6pub struct ConvexPolyhedronView<'a> {
7 /// The raw shape from Rapier.
8 pub raw: &'a ConvexPolyhedron,
9}
10
11macro_rules! impl_ref_methods(
12 ($View: ident) => {
13 impl<'a> $View<'a> {
14 /// The vertices of this convex polyhedron.
15 pub fn points(&self) -> impl ExactSizeIterator<Item = Vect> + '_ {
16 self.raw.points().iter().copied()
17 }
18
19 /// The number of vertices of this convex polyhedron.
20 pub fn num_vertices(&self) -> usize {
21 self.raw.vertices().len()
22 }
23
24 /// The indices of the edges of this convex polyhedron.
25 ///
26 /// The internal storage also contains edges between coplanar triangles merged into
27 /// the same polygonal face; those are skipped here.
28 pub fn edges(&self) -> impl Iterator<Item = u32> + '_ {
29 self.raw
30 .edges()
31 .iter()
32 .enumerate()
33 .filter(|(_, edge)| edge.faces[0] != edge.faces[1])
34 .map(|(i, _)| i as u32)
35 }
36
37 /// The number of (polygonal) faces of this convex polyhedron.
38 pub fn num_faces(&self) -> usize {
39 self.raw.faces().len()
40 }
41
42 /// The indices of the faces adjacent to the `i`-th vertex.
43 pub fn vertex_adjacent_faces(&self, i: usize) -> &[u32] {
44 let vertex = &self.raw.vertices()[i];
45 let first = vertex.first_adj_face_or_edge as usize;
46 let num = vertex.num_adj_faces_or_edge as usize;
47 &self.raw.faces_adj_to_vertex()[first..first + num]
48 }
49
50 /// The indices of the two endpoints of the `i`-th edge (indexing [`Self::points`]).
51 pub fn edge_vertices(&self, i: usize) -> [u32; 2] {
52 self.raw.edges()[i].vertices
53 }
54
55 /// The indices of the two faces adjacent to the `i`-th edge.
56 pub fn edge_faces(&self, i: usize) -> [u32; 2] {
57 self.raw.edges()[i].faces
58 }
59
60 /// The unit direction of the `i`-th edge, from its first to its second vertex.
61 pub fn edge_direction(&self, i: usize) -> Vect {
62 self.raw.edges()[i].dir
63 }
64
65 /// The outward unit normal of the `i`-th face.
66 pub fn face_normal(&self, i: usize) -> Vect {
67 self.raw.faces()[i].normal
68 }
69
70 /// The indices of the vertices of the `i`-th face (indexing [`Self::points`]).
71 pub fn face_vertices(&self, i: usize) -> &[u32] {
72 let face = &self.raw.faces()[i];
73 let first = face.first_vertex_or_edge as usize;
74 let num = face.num_vertices_or_edges as usize;
75 &self.raw.vertices_adj_to_face()[first..first + num]
76 }
77
78 /// The indices of the edges of the `i`-th face.
79 pub fn face_edges(&self, i: usize) -> &[u32] {
80 let face = &self.raw.faces()[i];
81 let first = face.first_vertex_or_edge as usize;
82 let num = face.num_vertices_or_edges as usize;
83 &self.raw.edges_adj_to_face()[first..first + num]
84 }
85
86 /// Triangulates the boundary of this convex polyhedron, returning its vertex and
87 /// index buffers.
88 pub fn to_trimesh(&self) -> (Vec<Vect>, Vec<[u32; 3]>) {
89 self.raw.to_trimesh()
90 }
91 }
92 }
93);
94
95impl_ref_methods!(ConvexPolyhedronView);
96
97/// Read-write access to the properties of a convex polyhedron.
98///
99/// Parry does not offer any in-place modification of convex polyhedra, so this view is
100/// read-only in practice.
101pub struct ConvexPolyhedronViewMut<'a> {
102 /// The raw shape from Rapier.
103 pub raw: &'a mut ConvexPolyhedron,
104}
105
106impl_ref_methods!(ConvexPolyhedronViewMut);