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)]
10pub enum Angle {
11 #[default]
13 R0,
14 R90,
16 R180,
18 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#[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#[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#[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 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 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 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#[derive(Copy, Clone, Debug, Hash, Ord, PartialOrd, Eq, PartialEq)]
179pub enum Direction1D {
180 Low,
182 High,
184}
185
186impl Direction1D {
187 pub fn reversed(self) -> Self {
189 match self {
190 Direction1D::Low => Direction1D::High,
191 Direction1D::High => Direction1D::Low,
192 }
193 }
194
195 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 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#[derive(Copy, Clone, Debug, Hash, Ord, PartialOrd, Eq, PartialEq)]
219pub enum Orientation2D {
220 Horizontal,
222 Vertical,
224}
225
226impl Orientation2D {
227 pub fn other(self) -> Self {
229 match self {
230 Orientation2D::Horizontal => Self::Vertical,
231 Orientation2D::Vertical => Self::Horizontal,
232 }
233 }
234
235 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 pub fn is_vertical(&self) -> bool {
250 self == &Self::Vertical
251 }
252
253 pub fn is_horizontal(&self) -> bool {
255 self == &Self::Horizontal
256 }
257}
258
259#[derive(Copy, Clone, Debug, Hash, Ord, PartialOrd, Eq, PartialEq)]
261pub enum Direction2D {
262 Left,
264 Up,
266 Right,
268 Down,
270}
271
272impl Direction2D {
273 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 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 pub fn sign<N>(self) -> N
293 where
294 N: Signed,
295 {
296 let d1: Direction1D = self.into();
297 d1.sign()
298 }
299}