Skip to main content

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