bevy_rapier3d/geometry/shape_views/segment.rs
1use crate::math::{Real, Vect};
2use rapier::parry::shape::Segment;
3
4/// Read-only access to the properties of a segment.
5#[derive(Copy, Clone)]
6pub struct SegmentView<'a> {
7 /// The raw shape from Rapier.
8 pub raw: &'a Segment,
9}
10
11macro_rules! impl_ref_methods(
12 ($View: ident) => {
13 impl<'a> $View<'a> {
14 /// The segment first point.
15 pub fn a(&self) -> Vect {
16 self.raw.a.into()
17 }
18
19 /// The segment second point.
20 pub fn b(&self) -> Vect {
21 self.raw.b.into()
22 }
23
24 /// The direction of this segment scaled by its length.
25 ///
26 /// Points from `self.a` toward `self.b`.
27 pub fn scaled_direction(&self) -> Vect {
28 self.raw.scaled_direction().into()
29 }
30
31 /// The length of this segment.
32 pub fn length(&self) -> Real {
33 self.raw.length()
34 }
35
36 /// The unit direction of this segment.
37 ///
38 /// Points from `self.a()` toward `self.b()`.
39 /// Returns `None` is both points are equal.
40 pub fn direction(&self) -> Option<Vect> {
41 self.raw.direction().map(|dir| (*dir).into())
42 }
43
44 /// In 2D, the not-normalized counterclockwise normal of this segment.
45 #[cfg(feature = "dim2")]
46 pub fn scaled_normal(&self) -> Vect {
47 self.raw.scaled_normal().into()
48 }
49
50 /// The not-normalized counterclockwise normal of this segment, assuming it lies on the plane
51 /// with the normal collinear to the given axis (0 = X, 1 = Y, 2 = Z).
52 #[cfg(feature = "dim3")]
53 pub fn scaled_planar_normal(&self, plane_axis: u8) -> Vect {
54 self.raw.scaled_planar_normal(plane_axis).into()
55 }
56
57 /// In 2D, the normalized counterclockwise normal of this segment.
58 #[cfg(feature = "dim2")]
59 pub fn normal(&self) -> Option<Vect> {
60 self.raw.normal().map(|n| (*n).into())
61 }
62
63 /// Returns `None`. Exists only for API similarity with the 2D parry.
64 #[cfg(feature = "dim3")]
65 pub fn normal(&self) -> Option<Vect> {
66 self.raw.normal().map(|n| (*n).into())
67 }
68
69 /// The normalized counterclockwise normal of this segment, assuming it lies on the plane
70 /// with the normal collinear to the given axis (0 = X, 1 = Y, 2 = Z).
71 #[cfg(feature = "dim3")]
72 pub fn planar_normal(&self, plane_axis: u8) -> Option<Vect> {
73 self.raw.planar_normal(plane_axis).map(|n| (*n).into())
74 }
75 }
76 }
77);
78
79impl_ref_methods!(SegmentView);
80
81/// Read-write access to the properties of a segment.
82pub struct SegmentViewMut<'a> {
83 /// The raw shape from Rapier.
84 pub raw: &'a mut Segment,
85}
86
87impl_ref_methods!(SegmentViewMut);
88
89impl SegmentViewMut<'_> {
90 /// Set the first point of the segment.
91 pub fn set_a(&mut self, a: Vect) {
92 self.raw.a = a.into();
93 }
94
95 /// Set the second point of the segment.
96 pub fn set_b(&mut self, b: Vect) {
97 self.raw.b = b.into();
98 }
99}