Skip to main content

bevy_rapier3d/geometry/shape_views/
polyline.rs

1use crate::math::Vect;
2use rapier::parry::shape::{Polyline, PolylineFlags};
3
4/// Read-only access to the properties of a polyline.
5#[derive(Copy, Clone)]
6pub struct PolylineView<'a> {
7    /// The raw shape from Rapier.
8    pub raw: &'a Polyline,
9}
10
11macro_rules! impl_ref_methods(
12    ($View: ident) => {
13        impl<'a> $View<'a> {
14            /// The number of segments forming this polyline.
15            pub fn num_segments(&self) -> usize {
16                self.raw.num_segments()
17            }
18
19            /// An iterator through all the segments of this mesh.
20            pub fn segments(&self) -> impl ExactSizeIterator<Item = (Vect, Vect)> + '_ {
21                self.raw.segments().map(|seg| (seg.a.into(), seg.b.into()))
22            }
23
24            /// Get the `i`-th segment of this mesh.
25            pub fn segment(&self, i: u32) -> (Vect, Vect) {
26                let seg = self.raw.segment(i);
27                (seg.a.into(), seg.b.into())
28            }
29
30            /// The vertex buffer, containing all the vertices of this polyline.
31            pub fn vertices(&self) -> impl ExactSizeIterator<Item = Vect> + '_ {
32                self.raw.vertices().iter().map(|v| (*v).into())
33            }
34
35            /// The index buffer, describing all the segments of this polyline.
36            pub fn indices(&self) -> &[[u32; 2]] {
37                self.raw.indices()
38            }
39
40            /// The flags controlling the optional data associated to this polyline.
41            pub fn flags(&self) -> PolylineFlags {
42                self.raw.flags()
43            }
44
45            /// The outward pseudo-normal of each vertex, if this polyline is oriented.
46            ///
47            /// The pseudo-normals are only computed with the [`PolylineFlags::ORIENTED`] flag.
48            /// The returned slice is indexed like [`Self::vertices`].
49            #[cfg(feature = "dim2")]
50            pub fn pseudo_normals(&self) -> Option<&[Vect]> {
51                self.raw.pseudo_normals()
52            }
53        }
54    }
55);
56
57impl_ref_methods!(PolylineView);
58
59/// Read-write access to the properties of a polyline.
60pub struct PolylineViewMut<'a> {
61    /// The raw shape from Rapier.
62    pub raw: &'a mut Polyline,
63}
64
65impl_ref_methods!(PolylineViewMut);
66
67impl PolylineViewMut<'_> {
68    /// Reverse the orientation of this polyline by swapping the indices of all
69    /// its segments and reverting its index buffer.
70    pub fn reverse(&mut self) {
71        self.raw.reverse()
72    }
73
74    /// Sets the flags of this polyline, computing or discarding its optional associated data.
75    pub fn set_flags(&mut self, flags: PolylineFlags) {
76        self.raw.set_flags(flags)
77    }
78
79    /// Replaces the vertex positions, keeping the index buffer.
80    ///
81    /// The acceleration structure is refitted and the pseudo-normals (if any) are recomputed.
82    /// This is intended for polylines built with [`PolylineFlags::DEFORMABLE`].
83    ///
84    /// # Panics
85    /// Panics if `vertices.len()` differs from the current number of vertices.
86    pub fn set_vertices(&mut self, vertices: &[Vect]) {
87        self.raw.set_vertices(vertices)
88    }
89
90    /// Modifies the vertex positions in place through `f`, then refits the acceleration
91    /// structure and recomputes the pseudo-normals (if any).
92    ///
93    /// This is intended for polylines built with [`PolylineFlags::DEFORMABLE`].
94    pub fn update_vertices(&mut self, f: impl FnOnce(&mut [Vect])) {
95        self.raw.update_vertices(f)
96    }
97}