reda_geometry/shape/
edge.rs1use num_traits::NumCast;
2
3use crate::shape::Point;
4use crate::base::Vec2;
5use crate::traits::{BoundingBox, Cast, CoordType, MapPoint, TryBoundingBox};
6pub use crate::base::Angle;
7pub use crate::base::{Contains, Side};
8
9use super::Rect;
10
11#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
13pub struct Edge<T> {
14 pub start: Point<T>,
15 pub end: Point<T>,
16}
17
18impl<T: Copy> Edge<T> {
19 pub fn new(start: impl Into<Point<T>>, end: impl Into<Point<T>>) -> Self {
20 Self {
21 start: start.into(),
22 end: end.into(),
23 }
24 }
25
26 pub fn reversed(&self) -> Self {
28 Self {
29 start: self.end,
30 end: self.start,
31 }
32 }
33}
34
35impl<T: PartialEq> Edge<T> {
36 pub fn is_degenerate(&self) -> bool {
39 self.start == self.end
40 }
41
42 pub fn is_rectilinear(&self) -> bool {
43 !self.is_degenerate() && (self.start.x == self.end.x || self.start.y == self.end.y)
44 }
45
46 pub fn is_horizontal(&self) -> bool {
47 !self.is_degenerate() && self.start.y == self.end.y
48 }
49
50 pub fn is_vertical(&self) -> bool {
51 !self.is_degenerate() && self.start.x == self.end.x
52 }
53}
54
55impl<T: CoordType> Edge<T> {
56 pub fn vec2(&self) -> Vec2<T> {
58 self.end - self.start
59 }
60
61 pub fn side_of(&self, point: Point<T>) -> Side {
70 assert!(!self.is_degenerate());
71
72 let a = self.vec2();
73 let b = point - self.start;
74
75 if a.x.is_zero() {
77 let side = if point.x < self.start.x {
78 Side::Left
79 } else if point.x > self.start.x {
80 Side::Right
81 } else {
82 Side::Center
83 };
84
85 let side = if self.start.y < self.end.y {
87 side
88 } else {
89 side.other()
90 };
91
92 return side;
93 } else if a.y.is_zero() {
94 let side = if point.y < self.start.y {
95 Side::Right
96 } else if point.y > self.start.y {
97 Side::Left
98 } else {
99 Side::Center
100 };
101
102 let side = if self.start.x < self.end.x {
104 side
105 } else {
106 side.other()
107 };
108
109 return side;
110 }
111
112 let area = b.cross_prod(a);
113
114 if area.is_zero() {
115 Side::Center
116 } else if area < T::zero() {
117 Side::Left
118 } else {
119 Side::Right
120 }
121 }
122
123 pub fn contains_point(&self, point: Point<T>) -> Contains {
126 if self.start == point || self.end == point {
127 Contains::OnBounds
128 } else if self.is_degenerate() {
129 Contains::No
130 } else {
131 let a = self.start - point;
132 let b = self.end - point;
133 if a.cross_prod(b).is_zero() && a.dot(b) <= T::zero() {
136 Contains::WithinBounds
137 } else {
138 Contains::No
139 }
140 }
141 }
142
143 pub fn line_contains_point(&self, point: Point<T>) -> bool {
145 if self.is_degenerate() {
146 self.start == point
147 } else {
148 let l = self.vec2();
149 let b = point - self.start;
150
151 l.cross_prod(b).is_zero()
152 }
153 }
154
155 pub fn is_parallel(&self, other: &Self) -> bool {
157 if self.is_degenerate() || other.is_degenerate() {
158 false
159 } else {
160 let a = self.vec2();
161 let b = other.vec2();
162
163 if a.x.is_zero() {
164 b.x.is_zero()
166 } else if a.y.is_zero() {
167 b.y.is_zero()
169 } else if b.x.is_zero() || b.y.is_zero() {
170 false
171 } else {
172 a.cross_prod(b).is_zero()
173 }
174 }
175 }
176
177 pub fn is_collinear(&self, other: &Self) -> bool {
179 if self.is_degenerate() || other.is_degenerate() {
180 false
181 } else {
182 let v = self.vec2();
183 let a = other.start - self.start;
184 let b = other.end - self.start;
185
186 if self.start.x == self.end.x {
187 other.start.x == self.start.x && other.end.x == self.start.x
190 } else if self.start.y == self.end.y {
191 other.start.y == self.start.y && other.end.y == self.start.y
193 } else if other.start.x == other.end.x {
194 self.start.x == other.start.x && self.end.x == other.start.x
195 } else if other.start.y == other.end.y {
196 self.start.y == other.start.y && self.end.y == other.start.y
198 } else {
199 v.cross_prod(a).is_zero() && v.cross_prod(b).is_zero()
200 }
201 }
202 }
203
204 pub fn is_coincident(&self, other: &Self) -> bool {
208 let self_start_in_other = other.contains_point(self.start).inclusive_bounds();
212 let self_end_in_other = other.contains_point(self.end).inclusive_bounds();
213 let other_start_in_self = self.contains_point(other.start).inclusive_bounds();
214 let other_end_in_self = self.contains_point(other.end).inclusive_bounds();
215
216 let share_more_than_one_point = self.end != other.start
217 && self.start != other.end
218 && (self_start_in_other || other_start_in_self)
219 && (other_end_in_self || self_end_in_other);
220
221 debug_assert!(if share_more_than_one_point {
223 self.is_parallel(other)
224 } else {
225 true
226 });
227
228 let oriented_the_same_way = self.vec2().dot(other.vec2()) > T::zero();
229
230 share_more_than_one_point && oriented_the_same_way
231 }
232
233 pub fn is_parallel_approx(&self, other: &Self, epsilon_squared: T) -> bool {
237 if self.is_degenerate() || other.is_degenerate() {
238 false
239 } else {
240 let d1 = self.vec2();
241 let d2 = other.vec2();
242
243 let len1_sqr = d1.norm2_squared();
244 let len2_sqr = d2.norm2_squared();
245
246 let cross = d1.cross_prod(d2);
247 let cross_sqr = cross * cross;
248
249 cross_sqr <= len1_sqr * len2_sqr * epsilon_squared
251 }
252 }
253
254 pub fn is_collinear_approx(&self, other: &Self, epsilon_squared: T) -> bool {
257 if self.is_degenerate() || other.is_degenerate() {
258 false
259 } else {
260 let d1 = self.vec2();
261 let d2 = other.vec2();
262
263 let len1_sqr = d1.norm2_squared();
264 let len2_sqr = d2.norm2_squared();
265
266 let cross = d1.cross_prod(d2);
267 let cross_sqr = cross * cross;
268
269 let approx_parallel = cross_sqr <= len1_sqr * len2_sqr * epsilon_squared;
270
271 if approx_parallel {
272 let e = other.start - self.start;
273 let len_e_sqrt = e.norm2_squared();
274 let cross = e.cross_prod(d1);
275 let cross_sqr = cross * cross;
276 cross_sqr <= len1_sqr * len_e_sqrt * epsilon_squared
277 } else {
278 false
279 }
280 }
281 }
282
283 pub fn lines_intersect_approx(&self, other: &Self, epsilon_squared: T) -> bool {
286 !self.is_parallel_approx(other, epsilon_squared)
287 || self.is_collinear_approx(other, epsilon_squared)
288 }
289
290 pub fn crossed_by_line(&self, other: &Self) -> Contains {
296 let side1 = other.side_of(self.start);
298
299 if side1 == Side::Center {
300 Contains::OnBounds
301 } else {
302 let side2 = other.side_of(self.end);
303
304 if side2 == Side::Center {
305 Contains::OnBounds
306 } else if side1 == side2 {
307 Contains::No
308 } else {
309 Contains::WithinBounds
310 }
311 }
312 }
313
314 pub fn lines_intersect(&self, other: &Self) -> bool {
317 !self.is_parallel(other) || self.is_collinear(other)
318 }
319}
320
321impl<T: Copy> From<Edge<T>> for (Point<T>, Point<T>) {
322 fn from(e: Edge<T>) -> Self {
323 (e.start, e.end)
324 }
325}
326
327impl<T: Copy> From<&Edge<T>> for (Point<T>, Point<T>) {
328 fn from(e: &Edge<T>) -> Self {
329 (e.start, e.end)
330 }
331}
332
333impl<T: CoordType> From<(Point<T>, Point<T>)> for Edge<T> {
334 fn from(points: (Point<T>, Point<T>)) -> Self {
335 Edge::new(points.0, points.1)
336 }
337}
338
339impl<T: CoordType> From<[Point<T>; 2]> for Edge<T> {
340 fn from(points: [Point<T>; 2]) -> Self {
341 Edge::new(points[0], points[1])
342 }
343}
344
345impl<T: Copy> MapPoint<T> for Edge<T> {
346 fn transform<F>(&self, transformate: F) -> Self
347 where
348 F: Fn(Point<T>) -> Point<T>
349 {
350 Self {
351 start: transformate(self.start),
352 end: transformate(self.end)
353 }
354 }
355}
356
357impl<T: Copy + PartialOrd> BoundingBox<T> for Edge<T> {
358 fn bounding_box(&self) -> Rect<T> {
359 Rect::new(self.start, self.end)
360 }
361}
362
363impl<T: Copy + PartialOrd> TryBoundingBox<T> for Edge<T> {
364 fn try_bounding_box(&self) -> Option<Rect<T>> {
365 Some(self.bounding_box())
366 }
367}
368
369impl<Src: Copy + NumCast, Dst: Copy + NumCast> Cast<Src, Dst> for Edge<Src> {
370 type Output = Edge<Dst>;
371
372 fn try_cast(&self) -> Option<Self::Output> {
373 match (self.start.try_cast(), self.end.try_cast()) {
374 (Some(s), Some(e)) => Some(Edge::new(s, e)),
375 _ => None,
376 }
377 }
378}
379