1use std::ops::{Add, Neg, Sub};
2use num_traits::Signed;
3pub const PREC_DISTANCE: DistanceType = 1e-5;
5pub type FloatType = f64;
6pub type DistanceType = FloatType;
7
8#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug, Default)]
10#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11pub enum Angle {
12 #[default]
14 R0,
15 R90,
17 R180,
19 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#[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#[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#[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 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 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 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#[derive(Copy, Clone, Debug, Hash, Ord, PartialOrd, Eq, PartialEq)]
180#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
181pub enum Direction1D {
182 Low,
184 High,
186}
187
188impl Direction1D {
189 pub fn reversed(self) -> Self {
191 match self {
192 Direction1D::Low => Direction1D::High,
193 Direction1D::High => Direction1D::Low,
194 }
195 }
196
197 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 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#[derive(Copy, Clone, Debug, Hash, Ord, PartialOrd, Eq, PartialEq)]
221#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
222pub enum Orientation2D {
223 Horizontal,
225 Vertical,
227}
228
229impl Orientation2D {
230 pub fn other(self) -> Self {
232 match self {
233 Orientation2D::Horizontal => Self::Vertical,
234 Orientation2D::Vertical => Self::Horizontal,
235 }
236 }
237
238 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 pub fn is_vertical(&self) -> bool {
253 self == &Self::Vertical
254 }
255
256 pub fn is_horizontal(&self) -> bool {
258 self == &Self::Horizontal
259 }
260}
261
262#[derive(Copy, Clone, Debug, Hash, Ord, PartialOrd, Eq, PartialEq)]
264#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
265pub enum Direction2D {
266 Left,
268 Up,
270 Right,
272 Down,
274}
275
276impl Direction2D {
277 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 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 pub fn sign<N>(self) -> N
297 where
298 N: Signed,
299 {
300 let d1: Direction1D = self.into();
301 d1.sign()
302 }
303}