nodedb_spatial/predicates/
intersection.rs1use nodedb_types::geometry::Geometry;
19
20pub fn st_intersection(a: &Geometry, b: &Geometry) -> Geometry {
25 match (a, b) {
26 (
28 Geometry::Polygon {
29 coordinates: rings_a,
30 },
31 Geometry::Polygon {
32 coordinates: rings_b,
33 },
34 ) => {
35 let Some(ext_a) = rings_a.first() else {
36 return empty_geometry();
37 };
38 let Some(ext_b) = rings_b.first() else {
39 return empty_geometry();
40 };
41 let clipped = sutherland_hodgman(ext_a, ext_b);
42 if clipped.len() < 3 {
43 return empty_geometry();
44 }
45 let mut ring = clipped;
47 if ring.first() != ring.last()
48 && let Some(&first) = ring.first()
49 {
50 ring.push(first);
51 }
52 Geometry::Polygon {
53 coordinates: vec![ring],
54 }
55 }
56
57 (Geometry::Point { coordinates: pt }, Geometry::Polygon { coordinates: rings })
59 | (Geometry::Polygon { coordinates: rings }, Geometry::Point { coordinates: pt }) => {
60 if let Some(ext) = rings.first()
61 && (nodedb_types::geometry::point_in_polygon(pt[0], pt[1], ext)
62 || crate::predicates::edge::point_on_ring_boundary(*pt, ext))
63 {
64 return Geometry::Point { coordinates: *pt };
65 }
66 empty_geometry()
67 }
68
69 (Geometry::Point { coordinates: ca }, Geometry::Point { coordinates: cb }) => {
71 if (ca[0] - cb[0]).abs() < 1e-12 && (ca[1] - cb[1]).abs() < 1e-12 {
72 Geometry::Point { coordinates: *ca }
73 } else {
74 empty_geometry()
75 }
76 }
77
78 (Geometry::LineString { coordinates: line }, Geometry::Polygon { coordinates: rings })
80 | (Geometry::Polygon { coordinates: rings }, Geometry::LineString { coordinates: line }) => {
81 clip_linestring_to_polygon(line, rings)
82 }
83
84 _ => {
87 if crate::predicates::st_intersects(a, b) {
88 Geometry::GeometryCollection {
91 geometries: vec![a.clone(), b.clone()],
92 }
93 } else {
94 empty_geometry()
95 }
96 }
97 }
98}
99
100fn empty_geometry() -> Geometry {
102 Geometry::GeometryCollection {
103 geometries: Vec::new(),
104 }
105}
106
107fn sutherland_hodgman(subject: &[[f64; 2]], clip: &[[f64; 2]]) -> Vec<[f64; 2]> {
117 if subject.is_empty() || clip.is_empty() {
118 return Vec::new();
119 }
120
121 let mut output = strip_closing(subject);
122 let clip_edges = strip_closing(clip);
123
124 let n = clip_edges.len();
125 for i in 0..n {
126 if output.is_empty() {
127 return Vec::new();
128 }
129
130 let edge_start = clip_edges[i];
131 let edge_end = clip_edges[(i + 1) % n];
132
133 let input = output;
134 output = Vec::with_capacity(input.len());
136
137 let m = input.len();
138 for j in 0..m {
139 let current = input[j];
140 let previous = input[(j + m - 1) % m];
141
142 let curr_inside = is_inside(current, edge_start, edge_end);
143 let prev_inside = is_inside(previous, edge_start, edge_end);
144
145 if curr_inside {
146 if !prev_inside {
147 if let Some(pt) = line_intersection(previous, current, edge_start, edge_end) {
149 output.push(pt);
150 }
151 }
152 output.push(current);
153 } else if prev_inside {
154 if let Some(pt) = line_intersection(previous, current, edge_start, edge_end) {
156 output.push(pt);
157 }
158 }
159 }
160 }
161
162 output
163}
164
165fn is_inside(point: [f64; 2], edge_start: [f64; 2], edge_end: [f64; 2]) -> bool {
167 let cross = (edge_end[0] - edge_start[0]) * (point[1] - edge_start[1])
170 - (edge_end[1] - edge_start[1]) * (point[0] - edge_start[0]);
171 cross >= 0.0
172}
173
174fn line_intersection(a1: [f64; 2], a2: [f64; 2], b1: [f64; 2], b2: [f64; 2]) -> Option<[f64; 2]> {
176 let dx_a = a2[0] - a1[0];
177 let dy_a = a2[1] - a1[1];
178 let dx_b = b2[0] - b1[0];
179 let dy_b = b2[1] - b1[1];
180
181 let denom = dx_a * dy_b - dy_a * dx_b;
182 if denom.abs() < 1e-15 {
183 return None; }
185
186 let t = ((b1[0] - a1[0]) * dy_b - (b1[1] - a1[1]) * dx_b) / denom;
187 Some([a1[0] + t * dx_a, a1[1] + t * dy_a])
188}
189
190fn strip_closing(ring: &[[f64; 2]]) -> Vec<[f64; 2]> {
192 if ring.len() >= 2 && ring.first() == ring.last() {
193 ring[..ring.len() - 1].to_vec()
194 } else {
195 ring.to_vec()
196 }
197}
198
199fn clip_linestring_to_polygon(line: &[[f64; 2]], rings: &[Vec<[f64; 2]>]) -> Geometry {
201 let Some(exterior) = rings.first() else {
202 return empty_geometry();
203 };
204
205 let mut inside_points: Vec<[f64; 2]> = Vec::new();
207 for pt in line {
208 if nodedb_types::geometry::point_in_polygon(pt[0], pt[1], exterior)
209 || crate::predicates::edge::point_on_ring_boundary(*pt, exterior)
210 {
211 inside_points.push(*pt);
212 }
213 }
214
215 if inside_points.len() < 2 {
216 return empty_geometry();
217 }
218
219 Geometry::LineString {
220 coordinates: inside_points,
221 }
222}
223
224#[cfg(test)]
225mod tests {
226 use super::*;
227
228 fn square(min: f64, max: f64) -> Geometry {
229 Geometry::polygon(vec![vec![
230 [min, min],
231 [max, min],
232 [max, max],
233 [min, max],
234 [min, min],
235 ]])
236 }
237
238 #[test]
239 fn overlapping_squares() {
240 let a = square(0.0, 10.0);
241 let b = square(5.0, 15.0);
242 let result = st_intersection(&a, &b);
243 if let Geometry::Polygon { coordinates } = &result {
244 assert!(!coordinates[0].is_empty());
245 let xs: Vec<f64> = coordinates[0].iter().map(|c| c[0]).collect();
247 let ys: Vec<f64> = coordinates[0].iter().map(|c| c[1]).collect();
248 let min_x = xs.iter().cloned().fold(f64::INFINITY, f64::min);
249 let max_x = xs.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
250 let min_y = ys.iter().cloned().fold(f64::INFINITY, f64::min);
251 let max_y = ys.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
252 assert!((min_x - 5.0).abs() < 0.01, "min_x={min_x}");
253 assert!((max_x - 10.0).abs() < 0.01, "max_x={max_x}");
254 assert!((min_y - 5.0).abs() < 0.01, "min_y={min_y}");
255 assert!((max_y - 10.0).abs() < 0.01, "max_y={max_y}");
256 } else {
257 panic!("expected Polygon, got {:?}", result.geometry_type());
258 }
259 }
260
261 #[test]
262 fn contained_square() {
263 let a = square(0.0, 10.0);
264 let b = square(2.0, 8.0);
265 let result = st_intersection(&a, &b);
266 if let Geometry::Polygon { coordinates } = &result {
267 let xs: Vec<f64> = coordinates[0].iter().map(|c| c[0]).collect();
269 let min_x = xs.iter().cloned().fold(f64::INFINITY, f64::min);
270 let max_x = xs.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
271 assert!((min_x - 2.0).abs() < 0.01);
272 assert!((max_x - 8.0).abs() < 0.01);
273 } else {
274 panic!("expected Polygon");
275 }
276 }
277
278 #[test]
279 fn disjoint_returns_empty() {
280 let a = square(0.0, 5.0);
281 let b = square(10.0, 15.0);
282 let result = st_intersection(&a, &b);
283 if let Geometry::GeometryCollection { geometries } = &result {
284 assert!(geometries.is_empty());
285 } else {
286 panic!("expected empty GeometryCollection");
287 }
288 }
289
290 #[test]
291 fn point_inside_polygon() {
292 let poly = square(0.0, 10.0);
293 let pt = Geometry::point(5.0, 5.0);
294 let result = st_intersection(&poly, &pt);
295 assert_eq!(result.geometry_type(), "Point");
296 }
297
298 #[test]
299 fn point_outside_polygon() {
300 let poly = square(0.0, 10.0);
301 let pt = Geometry::point(20.0, 20.0);
302 let result = st_intersection(&poly, &pt);
303 if let Geometry::GeometryCollection { geometries } = &result {
304 assert!(geometries.is_empty());
305 }
306 }
307
308 #[test]
309 fn identical_points() {
310 let a = Geometry::point(5.0, 5.0);
311 let b = Geometry::point(5.0, 5.0);
312 let result = st_intersection(&a, &b);
313 assert_eq!(result.geometry_type(), "Point");
314 }
315
316 #[test]
317 fn different_points() {
318 let a = Geometry::point(0.0, 0.0);
319 let b = Geometry::point(1.0, 1.0);
320 let result = st_intersection(&a, &b);
321 if let Geometry::GeometryCollection { geometries } = &result {
322 assert!(geometries.is_empty());
323 }
324 }
325
326 #[test]
327 fn linestring_clipped_to_polygon() {
328 let poly = square(0.0, 10.0);
329 let line = Geometry::line_string(vec![[2.0, 5.0], [5.0, 5.0], [8.0, 5.0]]);
331 let result = st_intersection(&poly, &line);
332 assert_eq!(result.geometry_type(), "LineString");
333 if let Geometry::LineString { coordinates } = &result {
334 assert_eq!(coordinates.len(), 3);
335 }
336 }
337}