|
18 | 18 |
|
19 | 19 | // Copyright 2025- Niall Oswald and Kenneth Martin and Jo Wayne Tan |
20 | 20 |
|
21 | | -use crate::algorithms::visibility::visiblity_polygon; |
22 | | -use crate::extensions::segments::{FromSegments, HullSegments}; |
23 | | -use geo::{Coord, Distance, Euclidean, GeoFloat, Line, LineString, Polygon}; |
24 | | -use rayon::prelude::*; |
| 21 | +use crate::algorithms::visibility::visibility_intersection; |
| 22 | +use crate::extensions::conversions::IntoCoord; |
| 23 | +use crate::extensions::segments::FromSegments; |
| 24 | +use crate::extensions::triangulate::Triangulate; |
| 25 | +use geo::{Distance, Euclidean, GeoFloat, Line, LineString, Polygon}; |
| 26 | +use spade::handles::{FixedVertexHandle, VertexHandle}; |
| 27 | +use spade::{CdtEdge, ConstrainedDelaunayTriangulation, Point2, SpadeNum, Triangulation}; |
| 28 | + |
| 29 | +struct CircularIterator<'a, T: SpadeNum> { |
| 30 | + current: VertexHandle<'a, Point2<T>, (), CdtEdge<()>>, |
| 31 | + until: VertexHandle<'a, Point2<T>, (), CdtEdge<()>>, |
| 32 | + cdt: &'a ConstrainedDelaunayTriangulation<Point2<T>>, |
| 33 | +} |
| 34 | + |
| 35 | +impl<'a, T: SpadeNum> CircularIterator<'a, T> { |
| 36 | + fn new( |
| 37 | + from: VertexHandle<'a, Point2<T>, (), CdtEdge<()>>, |
| 38 | + until: VertexHandle<'a, Point2<T>, (), CdtEdge<()>>, |
| 39 | + cdt: &'a ConstrainedDelaunayTriangulation<Point2<T>>, |
| 40 | + ) -> Self { |
| 41 | + CircularIterator { |
| 42 | + current: from, |
| 43 | + until, |
| 44 | + cdt, |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl<'a, T: SpadeNum> Iterator for CircularIterator<'a, T> { |
| 50 | + type Item = VertexHandle<'a, Point2<T>, (), CdtEdge<()>>; |
| 51 | + |
| 52 | + fn next(&mut self) -> Option<Self::Item> { |
| 53 | + if self.current == self.until { |
| 54 | + return None; |
| 55 | + } |
| 56 | + |
| 57 | + let vertex = self.current; |
| 58 | + |
| 59 | + self.current = { |
| 60 | + let handle = |
| 61 | + FixedVertexHandle::from_index((self.current.index() + 1) % self.cdt.num_vertices()); |
| 62 | + self.cdt.get_vertex(handle).unwrap() |
| 63 | + }; |
25 | 64 |
|
26 | | -fn rdp_preserve<T>(ls: &[Coord<T>], eps: T) -> Vec<Coord<T>> |
| 65 | + Some(vertex) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +fn rdp_preserve<T>( |
| 70 | + from: VertexHandle<'_, Point2<T>, (), CdtEdge<()>>, |
| 71 | + to: VertexHandle<'_, Point2<T>, (), CdtEdge<()>>, |
| 72 | + cdt: &ConstrainedDelaunayTriangulation<Point2<T>>, |
| 73 | + eps: T, |
| 74 | +) -> Vec<Point2<T>> |
27 | 75 | where |
28 | | - T: GeoFloat + Send + Sync, |
| 76 | + T: SpadeNum + GeoFloat + Send + Sync, |
29 | 77 | { |
30 | | - let (first, last) = match ls { |
31 | | - [] => return vec![], |
32 | | - &[only] => return vec![only], |
33 | | - &[first, last] => return vec![first, last], |
34 | | - &[first, .., last] => (first, last), |
35 | | - }; |
36 | | - |
37 | | - let visible = visiblity_polygon(ls); |
38 | | - let chord = Line::new(first, last); |
39 | | - |
40 | | - let split_index = visible |
41 | | - .into_iter() |
42 | | - .skip(1) |
43 | | - .fold( |
44 | | - (0usize, T::zero()), |
45 | | - |(farthest_index, farthest_distance), (index, coord)| { |
46 | | - let distance = Euclidean.distance(coord, &chord); |
47 | | - if distance < farthest_distance { |
48 | | - (farthest_index, farthest_distance) |
49 | | - } else { |
50 | | - (index, distance) |
51 | | - } |
52 | | - }, |
53 | | - ) |
54 | | - .0; |
55 | | - |
56 | | - if split_index == 0 || split_index == ls.len() - 1 { |
57 | | - println!("Failed to reduce. Skipping."); |
58 | | - return vec![first, last]; |
| 78 | + if cdt.exists_constraint(from.fix(), to.fix()) { |
| 79 | + return vec![from.position(), to.position()]; |
59 | 80 | } |
60 | 81 |
|
61 | | - let farthest_distance = ls.iter().map(|&v| Euclidean.distance(v, &chord)).fold( |
62 | | - T::zero(), |
63 | | - |farthest_distance, distance| { |
| 82 | + let chord = { |
| 83 | + let [from, to] = [from, to].map(|v| v.position().into_coord()); |
| 84 | + Line::new(from, to) |
| 85 | + }; |
| 86 | + |
| 87 | + let farthest_distance = |
| 88 | + CircularIterator::new(from, to, cdt).fold(T::zero(), |farthest_distance, v| { |
| 89 | + let distance = Euclidean.distance(v.position().into_coord(), &chord); |
64 | 90 | if distance > farthest_distance { |
65 | 91 | distance |
66 | 92 | } else { |
67 | 93 | farthest_distance |
68 | 94 | } |
| 95 | + }); |
| 96 | + |
| 97 | + if farthest_distance <= eps { |
| 98 | + return vec![from.position(), to.position()]; |
| 99 | + } |
| 100 | + |
| 101 | + let (split_vertex, _) = visibility_intersection(from, to, cdt).into_iter().fold( |
| 102 | + (from, -T::one()), // Placeholder, should always be overwritten |
| 103 | + |(farthest_vertex, farthest_distance), v| { |
| 104 | + let distance = Euclidean.distance(v.position().into_coord(), &chord); |
| 105 | + if distance > farthest_distance { |
| 106 | + (v, distance) |
| 107 | + } else { |
| 108 | + (farthest_vertex, farthest_distance) |
| 109 | + } |
69 | 110 | }, |
70 | 111 | ); |
71 | 112 |
|
72 | | - if farthest_distance > eps { |
73 | | - let (mut left, right) = rayon::join( |
74 | | - || rdp_preserve(&ls[..=split_index], eps), |
75 | | - || rdp_preserve(&ls[split_index..], eps), |
76 | | - ); |
| 113 | + // This should never occur |
| 114 | + if split_vertex == from || split_vertex == to { |
| 115 | + panic!("Attempted to split at endpoint"); |
| 116 | + } |
77 | 117 |
|
78 | | - left.pop(); |
79 | | - left.extend_from_slice(&right); |
| 118 | + let (mut left, right) = rayon::join( |
| 119 | + || rdp_preserve(from, split_vertex, cdt, eps), |
| 120 | + || rdp_preserve(split_vertex, to, cdt, eps), |
| 121 | + ); |
80 | 122 |
|
81 | | - return left; |
82 | | - } |
| 123 | + left.pop(); |
| 124 | + left.extend_from_slice(&right); |
83 | 125 |
|
84 | | - vec![first, last] |
| 126 | + left |
85 | 127 | } |
86 | 128 |
|
87 | 129 | pub trait SimplifyRDP<T, Epsilon = T> { |
88 | 130 | fn simplify_rdp(&self, eps: Epsilon) -> Self; |
89 | 131 | } |
90 | 132 |
|
91 | | -impl<T> SimplifyRDP<T> for LineString<T> |
92 | | -where |
93 | | - T: GeoFloat + Send + Sync, |
94 | | -{ |
95 | | - fn simplify_rdp(&self, eps: T) -> Self { |
96 | | - LineString::new(rdp_preserve(&self.0, eps)) |
97 | | - } |
98 | | -} |
99 | | - |
100 | 133 | impl<T> SimplifyRDP<T> for Polygon<T> |
101 | 134 | where |
102 | | - T: GeoFloat + Send + Sync, |
| 135 | + T: SpadeNum + GeoFloat + Send + Sync, |
103 | 136 | { |
104 | 137 | fn simplify_rdp(&self, eps: T) -> Self { |
105 | | - let reduced_segments = self |
106 | | - .hull_segments() |
107 | | - .into_par_iter() // parallelize with rayon |
108 | | - .map(|ls| ls.simplify_rdp(eps)) |
109 | | - .collect::<Vec<_>>(); |
110 | | - |
111 | | - Polygon::from_segments(reduced_segments) |
| 138 | + if self.exterior().0.len() < 3 { |
| 139 | + return self.clone(); |
| 140 | + } |
| 141 | + |
| 142 | + let cdt = self.triangulate(); |
| 143 | + |
| 144 | + let segments = cdt |
| 145 | + .convex_hull() |
| 146 | + .map(|edge| { |
| 147 | + rdp_preserve(edge.from(), edge.to(), &cdt, eps) |
| 148 | + .into_iter() |
| 149 | + .map(|point| point.into_coord()) |
| 150 | + .collect::<LineString<_>>() |
| 151 | + }) |
| 152 | + .collect(); |
| 153 | + |
| 154 | + Polygon::from_segments(segments) |
112 | 155 | } |
113 | 156 | } |
0 commit comments