reda_geometry/base/
types.rs

1use std::ops::{Add, Neg, Sub};
2use num_traits::Signed;
3/// Precision for distance related decisions.
4pub const PREC_DISTANCE: DistanceType = 1e-5;
5pub type FloatType = f64;
6pub type DistanceType = FloatType;
7
8/// Angle expressed as a multiple of 90 degrees.
9#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug, Default)]
10#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11pub enum Angle {
12    /// 0 Degrees.
13    #[default]
14    R0,
15    /// 90 Degrees.
16    R90,
17    /// 180 Degrees.
18    R180,
19    /// 270 Degrees.
20    R270,
21}
22
23impl Angle {
24    pub fn to_int(&self) -> u32 {
25        match self {
26            Angle::R0 => 0,
27            Angle::R90 => 1,
28            Angle::R180 => 2,
29            Angle::R270 => 3,
30        }
31    }
32
33    pub fn from_int(a: u32) -> Self {
34        match a % 4 {
35            0 => Angle::R0,
36            1 => Angle::R90,
37            2 => Angle::R180,
38            3 => Angle::R270,
39            _ => unreachable!(),
40        }
41    }
42}
43
44impl Add for Angle {
45    type Output = Self;
46
47    fn add(self, rhs: Self) -> Self::Output {
48        Self::from_int(self.to_int() + rhs.to_int())
49    }
50}
51
52impl Sub for Angle {
53    type Output = Self;
54
55    fn sub(self, rhs: Self) -> Self::Output {
56        Self::from_int(self.to_int() + 4 - rhs.to_int())
57    }
58}
59
60impl Neg for Angle {
61    type Output = Self;
62
63    fn neg(self) -> Self::Output {
64        Self::from_int(4 - self.to_int())
65    }
66}
67
68/// Location relative to a directed line or edge.
69/// Something can be on the left of the line, right of the line or on top of the line (center).
70#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
71pub enum Side {
72    Left,
73    Center,
74    Right,
75}
76
77impl Side {
78    pub fn is_left(&self) -> bool {
79        *self == Side::Left
80    }
81
82    pub fn is_right(&self) -> bool {
83        *self == Side::Right
84    }
85
86    pub fn is_center(&self) -> bool {
87        *self == Side::Center
88    }
89
90    pub fn other(&self) -> Self {
91        match self {
92            Side::Left => Side::Right,
93            Side::Center => Side::Center,
94            Side::Right => Side::Left,
95        }
96    }
97}
98
99/// Relative orientation of two geometrical objects such as vectors.
100#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
101pub enum Orientation {
102    ClockWise,
103    CounterClockWise,
104    Straight,
105}
106
107impl Orientation {
108    pub fn is_clock_wise(self) -> bool {
109        self == Self::ClockWise
110    }
111
112    pub fn is_counter_clock_wise(self) -> bool {
113        self == Self::CounterClockWise
114    }
115
116    pub fn is_straight(self) -> bool {
117        self == Self::Straight
118    }
119}
120
121/// This is a result type for containment checks.
122/// * `No` Not inside.
123/// * `OnBounds` Lies on the boundaries.
124/// * `WithinBounds` Fully inside.
125#[derive(Clone, Copy, PartialEq, Eq, Debug)]
126pub enum Contains {
127    No,
128    OnBounds,
129    WithinBounds,
130}
131
132impl Contains {
133    pub fn is_within_bounds(&self) -> bool {
134        matches!(self, Contains::WithinBounds)
135    }
136
137    /// Tells if the point is contained or lies on the bounds.
138    pub fn inclusive_bounds(&self) -> bool {
139        matches!(
140            self,
141            Contains::WithinBounds | Contains::OnBounds
142        )
143    }
144
145    pub fn on_bounds(&self) -> bool {
146        matches!(self, Contains::OnBounds)
147    }
148
149    pub fn is_no(&self) -> bool {
150        matches!(self, Contains::No)
151    }
152
153    /// Returns the stronger result of the both.
154    /// Ordering from weak to strong is `No`, `OnBounds`, `WithinBounds`
155    pub fn max(self, other: Self) -> Self {
156        match (self, other) {
157            (Contains::WithinBounds, _) => Contains::WithinBounds,
158            (_, Contains::WithinBounds) => Contains::WithinBounds,
159            (Contains::OnBounds, _) => Contains::OnBounds,
160            (_, Contains::OnBounds) => Contains::OnBounds,
161            (Contains::No, _) => Contains::No,
162        }
163    }
164
165    /// Returns the weaker result of the both.
166    /// Ordering from weak to strong is `No`, `OnBounds`, `WithinBounds`
167    pub fn min(self, other: Self) -> Self {
168        match (self, other) {
169            (Contains::No, _) => Contains::No,
170            (_, Contains::No) => Contains::No,
171            (Contains::OnBounds, _) => Contains::OnBounds,
172            (_, Contains::OnBounds) => Contains::OnBounds,
173            (Contains::WithinBounds, _) => Contains::WithinBounds,
174        }
175    }
176}
177
178/// Direction in one dimension.
179#[derive(Copy, Clone, Debug, Hash, Ord, PartialOrd, Eq, PartialEq)]
180#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
181pub enum Direction1D {
182    /// Towards negative coordinates.
183    Low,
184    /// Towards positive coordinates.
185    High,
186}
187
188impl Direction1D {
189    /// Return the opposite direction.
190    pub fn reversed(self) -> Self {
191        match self {
192            Direction1D::Low => Direction1D::High,
193            Direction1D::High => Direction1D::Low,
194        }
195    }
196
197    /// Map `Low` to `-1` and `High` to `+1`.
198    pub fn sign<N>(&self) -> N
199    where
200        N: Signed,
201    {
202        match self {
203            Direction1D::Low => N::one().neg(),
204            Direction1D::High => N::one(),
205        }
206    }
207}
208
209impl From<Direction2D> for Direction1D {
210    /// Downcast 2D direction to 1D direction by preserving the sign.
211    fn from(d2: Direction2D) -> Self {
212        match d2 {
213            Direction2D::Right | Direction2D::Up => Direction1D::High,
214            Direction2D::Left | Direction2D::Down => Direction1D::Low,
215        }
216    }
217}
218
219/// Orientation in two dimensions along one of the axes.
220#[derive(Copy, Clone, Debug, Hash, Ord, PartialOrd, Eq, PartialEq)]
221#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
222pub enum Orientation2D {
223    /// Oriented parallel to the x axis.
224    Horizontal,
225    /// Oriented parallel to the y axis.
226    Vertical,
227}
228
229impl Orientation2D {
230    /// Get the orthogonal orientation.
231    pub fn other(self) -> Self {
232        match self {
233            Orientation2D::Horizontal => Self::Vertical,
234            Orientation2D::Vertical => Self::Horizontal,
235        }
236    }
237
238    /// Get the positive or negative direction aligned with this orientation.
239    pub fn direction(self, d1: Direction1D) -> Direction2D {
240        use Direction1D::*;
241        use Direction2D::*;
242        use Orientation2D::*;
243        match (self, d1) {
244            (Horizontal, Low) => Right,
245            (Horizontal, High) => Left,
246            (Vertical, Low) => Down,
247            (Vertical, High) => Up,
248        }
249    }
250
251    /// Check if orientation is vertical.
252    pub fn is_vertical(&self) -> bool {
253        self == &Self::Vertical
254    }
255
256    /// Check if orientation is horizontal.
257    pub fn is_horizontal(&self) -> bool {
258        self == &Self::Horizontal
259    }
260}
261
262/// Directions along the coordinate axes in two dimensions.
263#[derive(Copy, Clone, Debug, Hash, Ord, PartialOrd, Eq, PartialEq)]
264#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
265pub enum Direction2D {
266    /// Positive x direction.
267    Left,
268    /// Positive y direction.
269    Up,
270    /// Negative x direction.
271    Right,
272    /// Negative y direction.
273    Down,
274}
275
276impl Direction2D {
277    /// Get the opposite direction.
278    pub fn reversed(self) -> Self {
279        match self {
280            Direction2D::Left => Self::Right,
281            Direction2D::Up => Self::Down,
282            Direction2D::Right => Self::Left,
283            Direction2D::Down => Self::Up,
284        }
285    }
286
287    /// Get the orientation of this direction.
288    pub fn orientation(&self) -> Orientation2D {
289        match self {
290            Direction2D::Left | Direction2D::Right => Orientation2D::Horizontal,
291            Direction2D::Up | Direction2D::Down => Orientation2D::Vertical,
292        }
293    }
294
295    /// Get the sign of the direction.
296    pub fn sign<N>(self) -> N
297    where
298        N: Signed,
299    {
300        let d1: Direction1D = self.into();
301        d1.sign()
302    }
303}