bevy_rapier3d/geometry/shape_views/
trimesh.rs

1use crate::math::Vect;
2use rapier::parry::shape::{TopologyError, TriMesh, TriMeshFlags};
3
4/// Read-only access to the properties of a triangle mesh.
5#[derive(Copy, Clone)]
6pub struct TriMeshView<'a> {
7    /// The raw shape from Rapier.
8    pub raw: &'a TriMesh,
9}
10
11macro_rules! impl_ref_methods(
12    ($View: ident) => {
13        impl<'a> $View<'a> {
14            /// The number of triangles forming this mesh.
15            pub fn num_triangles(&self) -> usize {
16                self.raw.num_triangles()
17            }
18
19            /// An iterator through all the triangles of this mesh.
20            pub fn triangles(&self) -> impl ExactSizeIterator<Item = (Vect, Vect, Vect)> + '_ {
21                self.raw
22                    .triangles()
23                    .map(|tri| (tri.a.into(), tri.b.into(), tri.c.into()))
24            }
25
26            /// Get the `i`-th triangle of this mesh.
27            pub fn triangle(&self, i: u32) -> (Vect, Vect, Vect) {
28                let tri = self.raw.triangle(i);
29                (tri.a.into(), tri.b.into(), tri.c.into())
30            }
31
32            /// The vertex buffer of this mesh.
33            pub fn vertices(&self) -> impl ExactSizeIterator<Item = Vect> + '_ {
34                self.raw.vertices().iter().map(|pt| (*pt).into())
35            }
36
37            /// The index buffer of this mesh.
38            pub fn indices(&self) -> &[[u32; 3]] {
39                self.raw.indices()
40            }
41
42            /// A flat view of the index buffer of this mesh.
43            pub fn flat_indices(&self) -> &[u32] {
44                self.raw.flat_indices()
45            }
46        }
47    }
48);
49
50impl_ref_methods!(TriMeshView);
51
52/// Read-write access to the properties of a triangle mesh.
53pub struct TriMeshViewMut<'a> {
54    /// The raw shape from Rapier.
55    pub raw: &'a mut TriMesh,
56}
57
58impl_ref_methods!(TriMeshViewMut);
59
60impl TriMeshViewMut<'_> {
61    /// Sets the flags of this triangle mesh, controlling its optional associated data.
62    pub fn set_flags(&mut self, flags: TriMeshFlags) -> Result<(), TopologyError> {
63        self.raw.set_flags(flags)
64    }
65}