bevy_rapier2d/geometry/shape_views/
polyline.rs

1use crate::math::Vect;
2use rapier::parry::shape::Polyline;
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    }
41);
42
43impl_ref_methods!(PolylineView);
44
45/// Read-write access to the properties of a polyline.
46pub struct PolylineViewMut<'a> {
47    /// The raw shape from Rapier.
48    pub raw: &'a mut Polyline,
49}
50
51impl_ref_methods!(PolylineViewMut);
52
53impl PolylineViewMut<'_> {
54    /// Reverse the orientation of this polyline by swapping the indices of all
55    /// its segments and reverting its index buffer.
56    pub fn reverse(&mut self) {
57        self.raw.reverse()
58    }
59}