geo 0.33.1

Geospatial primitives and algorithms
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
use super::*;
use crate::algorithm::line_intersection::line_intersection;
use crate::{GeoFloat, Line};

fn compute_brute_force_intersections<T: GeoFloat>(
    lines: &[Line<T>],
) -> Vec<(Line<T>, Line<T>, LineIntersection<T>)> {
    let mut result = Vec::new();
    for i in 0..lines.len() {
        for j in (i + 1)..lines.len() {
            if let Some(isect) = line_intersection(lines[i], lines[j]) {
                result.push((lines[i], lines[j], isect));
            }
        }
    }
    result
}

/// Helper function to verify that sweep line and brute force find the same intersections
fn verify_intersections(lines: &[Line<f64>]) {
    // Get intersections using both algorithms
    let sweep_intersections: Vec<_> = Intersections::from_iter(lines).collect();
    let brute_force_intersections = compute_brute_force_intersections(lines);

    // Check for same count
    assert_eq!(
        sweep_intersections.len(),
        brute_force_intersections.len(),
        "Sweep algorithm found {} intersections but brute force found {}",
        sweep_intersections.len(),
        brute_force_intersections.len()
    );

    // Check that all brute force intersections are found by sweep
    // and that their intersection details match
    for (bf_line1, bf_line2, bf_intersection) in &brute_force_intersections {
        let matching_intersection = sweep_intersections.iter().find(|(line1, line2, _)| {
            (*line1 == bf_line1 && *line2 == bf_line2) || (*line1 == bf_line2 && *line2 == bf_line1)
        });

        assert!(
            matching_intersection.is_some(),
            "Sweep algorithm missed an intersection between lines"
        );

        // Compare the actual intersection details
        #[allow(clippy::assertions_on_constants)]
        if let Some((_, _, sw_intersection)) = matching_intersection {
            match (bf_intersection, sw_intersection) {
                (
                    LineIntersection::SinglePoint {
                        intersection: bf_point,
                        is_proper: bf_proper,
                    },
                    LineIntersection::SinglePoint {
                        intersection: sw_point,
                        is_proper: sw_proper,
                    },
                ) => {
                    assert_eq!(
                        *bf_proper, *sw_proper,
                        "Proper intersection status doesn't match for the same line pairs"
                    );

                    assert_eq!(bf_point, sw_point,);
                }
                (
                    LineIntersection::Collinear {
                        intersection: bf_line,
                    },
                    LineIntersection::Collinear {
                        intersection: sw_line,
                    },
                ) => {
                    assert!(
                        (*bf_line == *sw_line)
                            || (*bf_line == Line::new(sw_line.end, sw_line.start)),
                        "Collinear intersection lines don't match: brute force: {bf_line:?}, sweep: {sw_line:?}"
                    );
                }
                _ => {
                    assert!(
                        false,
                        "Intersection types don't match: brute force: {bf_intersection:?}, sweep: {sw_intersection:?}",
                    );
                }
            }
        }
    }

    // Check that all sweep intersections are found by brute force
    for (sw_line1, sw_line2, _) in sweep_intersections {
        let found = brute_force_intersections.iter().any(|(line1, line2, _)| {
            (line1 == sw_line1 && line2 == sw_line2) || (line1 == sw_line2 && line2 == sw_line1)
        });

        assert!(
            found,
            "Sweep algorithm found an intersection not found by brute force"
        );
    }
}

#[test]
fn should_not_panic() {
    // from https://github.com/georust/geo/issues/1342
    // these segments cause a panic in the existing sweep line impl:
    // segment not found in active-vec-set: 4
    let segments = vec![
        Line::from([(10.0, 6.0), (10.0, 8.0)]),
        Line::from([(10.0, 6.0), (8.0, 10.0)]),
        Line::from([(10.0, 6.0), (8.0, 8.0)]),
        Line::from([(10.0, 6.0), (8.0, 4.0)]),
        Line::from([(10.0, 6.0), (2.0, 8.0)]),
        Line::from([(10.0, 4.0), (10.0, 8.0)]),
        Line::from([(10.0, 8.0), (8.0, 10.0)]),
        Line::from([(8.0, 10.0), (8.0, 8.0)]),
        Line::from([(8.0, 8.0), (4.0, 2.0)]),
        Line::from([(2.0, 2.0), (4.0, 10.0)]),
        Line::from([(2.0, 2.0), (6.0, 10.0)]),
        Line::from([(4.0, 8.0), (4.0, 10.0)]),
        Line::from([(2.0, 6.0), (6.0, 10.0)]),
        Line::from([(6.0, 10.0), (10.0, 2.0)]),
        Line::from([(10.0, 2.0), (10.0, 10.0)]),
        Line::from([(10.0, 10.0), (8.0, 2.0)]),
        Line::from([(8.0, 6.0), (6.0, 8.0)]),
        Line::from([(8.0, 6.0), (6.0, 2.0)]),
        Line::from([(8.0, 6.0), (6.0, 4.0)]),
        Line::from([(8.0, 6.0), (4.0, 6.0)]),
        Line::from([(8.0, 6.0), (2.0, 4.0)]),
        Line::from([(2.0, 4.0), (6.0, 8.0)]),
        Line::from([(6.0, 4.0), (4.0, 6.0)]),
        Line::from([(4.0, 4.0), (2.0, 4.0)]),
        Line::from([(10.0, 6.0), (2.0, 2.0)]),
        Line::from([(2.0, 2.0), (8.0, 6.0)]),
    ];
    verify_intersections(&segments);
}

#[test]
fn test_iterator_behavior() {
    let input = vec![
        Line::from([(1., 0.), (0., 1.)]),
        Line::from([(0., 0.), (1., 1.)]),
    ];

    // They intersect at (0.5, 0.5)
    let intersections: Vec<_> = Intersections::from_iter(&input).collect();

    // There should be one intersection
    assert_eq!(intersections.len(), 1);

    // Check intersection details
    let (_line1, _line2, intersection) = &intersections[0];

    assert!(matches!(
        intersection,
        LineIntersection::SinglePoint {
            intersection: _,
            is_proper: true
        }
    ));
}

#[test]
fn test_multiple_intersections() {
    let input = vec![
        Line::from([(1., 0.), (0., 1.)]),
        Line::from([(0., 0.75), (1., 0.25)]),
        Line::from([(0., 0.25), (1., 0.75)]),
        Line::from([(0., 0.), (1., 1.)]),
    ];

    verify_intersections(&input);
}

#[test]
fn test_brute_force_comparison() {
    // Create a set of lines with various intersections
    let input = vec![
        Line::from([(0., 0.), (10., 10.)]),
        Line::from([(0., 10.), (10., 0.)]),
        Line::from([(5., 0.), (5., 10.)]),
        Line::from([(0., 5.), (10., 5.)]),
    ];

    verify_intersections(&input);
}

/// Generate a grid explicitly with known intersections for easier debugging
fn generate_explicit_grid(size: usize) -> Vec<Line<f64>> {
    let mut lines = Vec::with_capacity(size * 2);

    // Create a grid of "size" horizontal and "size" vertical lines
    // crossing at exact integer coordinates
    for i in 0..size {
        // Convert to float for line coordinates
        let pos = i as f64;

        // Horizontal line at y = pos
        lines.push(Line::from([(-10.0, pos), (10.0, pos)]));

        // Vertical line at x = pos
        lines.push(Line::from([(pos, -10.0), (pos, 10.0)]));
    }

    // Should have exactly size² intersections
    lines
}

/// Test to diagnose and fix the Bentley-Ottmann algorithm for grid patterns
#[test]
fn test_debug_grid_algorithm() {
    // Create a small grid pattern to find the intersections missed by Bentley-Ottmann
    for size in [3, 5, 7, 10] {
        let lines = generate_explicit_grid(size);

        // Expected number of intersections in grid
        let expected_intersections = size * size;

        let sweep_results: Vec<_> = Intersections::from_iter(&lines).collect();
        assert_eq!(
            sweep_results.len(),
            expected_intersections,
            "Sweep algorithm should find {expected_intersections} grid intersections for {size}x{size} grid",
        );
    }
}

// Test to diagnose issues with line_intersection for horizontal-vertical pairs
#[test]
fn test_horizontal_vertical_intersection() {
    // Create a grid of 5 horizontal and 5 vertical lines
    let size = 5;
    let mut missing_count = 0;

    // Generate explicit horizontal and vertical lines
    let mut lines = Vec::with_capacity(size * 2);

    // Create horizontal lines
    let mut horizontal_lines = Vec::with_capacity(size);
    for i in 0..size {
        let y = i as f64;
        horizontal_lines.push(Line::from([(-10.0, y), (10.0, y)]));
    }

    // Create vertical lines
    let mut vertical_lines = Vec::with_capacity(size);
    for i in 0..size {
        let x = i as f64;
        vertical_lines.push(Line::from([(x, -10.0), (x, 10.0)]));
    }

    // Add all lines to the main list
    lines.extend(horizontal_lines.iter().cloned());
    lines.extend(vertical_lines.iter().cloned());

    for h_line in horizontal_lines.iter() {
        for v_line in vertical_lines.iter() {
            // Check if these lines should intersect
            let result = line_intersection(*h_line, *v_line);
            // Check if the intersection was found
            if result.is_none() {
                missing_count += 1;
            }
        }
    }
    // Make sure we found all intersections
    assert_eq!(
        missing_count, 0,
        "All horizontal-vertical pairs should intersect. We're missing {missing_count} intersections",
    );
}

/// Test with nearly parallel lines that have small differences
#[test]
fn test_nearly_parallel_lines() {
    // Set of nearly parallel lines with very small differences in angle
    let lines = vec![
        // Base line
        Line::from([(0.0, 0.0), (10.0, 1.0)]),
        // Nearly parallel lines with small differences
        Line::from([(0.0, 0.0), (10.0, 1.0000001)]), // Extremely small angle diff
        Line::from([(0.0, 0.0), (10.0, 1.00001)]),   // Very small angle diff
        Line::from([(0.0, 0.0), (10.0, 0.99999)]),   // Very small angle diff (opposite)
        Line::from([(0.0, 0.1), (10.0, 1.1)]),       // Parallel but offset
        // Intersecting lines at small angles
        Line::from([(0.1, 0.0), (10.0, 0.9)]), // Intersects baseline
        Line::from([(5.0, 0.0), (5.0, 10.0)]), // Vertical intersector
    ];

    verify_intersections(&lines);
}

/// Test with lines having coordinates near powers of 2
#[test]
fn test_power_of_two_boundaries() {
    // Lines with coordinates near powers of 2, where floating point precision changes
    let lines = vec![
        // Near 2^10 = 1024
        Line::from([(1023.0, 1023.0), (1025.0, 1025.0)]),
        Line::from([(1023.0, 1025.0), (1025.0, 1023.0)]),
        // Near 2^20 = 1,048,576
        Line::from([(1048575.0, 1048575.0), (1048577.0, 1048577.0)]),
        Line::from([(1048575.0, 1048577.0), (1048577.0, 1048575.0)]),
        // Mixed scales
        Line::from([(1024.0, 1024.0), (1048576.0, 1048576.0)]),
        Line::from([(1024.0, 1048576.0), (1048576.0, 1024.0)]),
    ];

    verify_intersections(&lines);
}

/// Test with extremely large coordinate values
#[test]
fn test_large_coordinates() {
    // Lines with very large coordinate values
    let large = 1e15; // 10^15
    let lines = vec![
        Line::from([(large, large), (large + 10.0, large + 10.0)]),
        Line::from([(large, large + 10.0), (large + 10.0, large)]),
        Line::from([(large, large), (large, large + 10.0)]),
        Line::from([(large, large), (large + 10.0, large)]),
    ];

    verify_intersections(&lines);
}

/// Test with extremely small coordinate values and differences
#[test]
fn test_small_coordinates() {
    // Lines with very small coordinate values and differences
    let small = 1e-10; // 10^-10
    let lines = vec![
        Line::from([(small, small), (small * 2.0, small * 2.0)]),
        Line::from([(small, small * 2.0), (small * 2.0, small)]),
        Line::from([(0.0, 0.0), (small, small)]),
        Line::from([(0.0, small), (small, 0.0)]),
        // Very small differences between endpoints
        Line::from([(small, 0.0), (small + 1e-15, 1e-15)]),
        Line::from([(0.0, small), (1e-15, small + 1e-15)]),
    ];

    verify_intersections(&lines);
}

/// Test with collinear or almost-collinear segments
#[test]
fn test_collinear_segments() {
    // Collinear and nearly-collinear segments
    let lines = vec![
        // Two collinear segments that don't overlap
        Line::from([(0.0, 0.0), (1.0, 1.0)]),
        Line::from([(2.0, 2.0), (3.0, 3.0)]),
        // Two collinear segments that overlap
        Line::from([(4.0, 4.0), (6.0, 6.0)]),
        Line::from([(5.0, 5.0), (7.0, 7.0)]),
        // Nearly collinear segments (tiny offset)
        Line::from([(8.0, 8.0), (10.0, 10.0)]),
        Line::from([(9.0, 9.0 + 1e-10), (11.0, 11.0 + 1e-10)]),
    ];

    verify_intersections(&lines);
}

/// Test with lines forming very shallow angles
#[test]
fn test_shallow_angle_intersections() {
    // Lines that intersect at very shallow angles
    let lines = vec![
        Line::from([(0.0, 0.0), (100.0, 1.0)]),
        Line::from([(0.0, 0.1), (100.0, 0.0)]),
        // Even shallower
        Line::from([(0.0, 0.0), (1000.0, 1.0)]),
        Line::from([(0.0, 0.1), (1000.0, 0.0)]),
        // Extremely shallow
        Line::from([(0.0, 0.0), (10000.0, 1.0)]),
        Line::from([(0.0, 0.001), (10000.0, 0.0)]),
    ];

    verify_intersections(&lines);
}

/// Test with a mix of all previous precision-challenging cases
#[test]
fn test_mixed_precision_challenges() {
    // Combining various challenging cases
    let lines = vec![
        // Nearly parallel with small y offset
        Line::from([(0.0, 0.0), (10.0, 1.0)]),
        Line::from([(0.0, 1e-10), (10.0, 1.0 + 1e-10)]),
        // Power of 2 boundary intersections
        Line::from([(1024.0, 0.0), (1024.0, 10.0)]),
        Line::from([(0.0, 5.0), (2048.0, 5.0)]),
        // Large coordinate values
        Line::from([(1e12, 1e12), (1e12 + 10.0, 1e12 + 10.0)]),
        Line::from([(1e12, 1e12 + 10.0), (1e12 + 10.0, 1e12)]),
        // Small differences and shallow angles
        Line::from([(0.0, 0.0), (10000.0, 1.0)]),
        Line::from([(0.0, 0.5), (10000.0, 0.4)]),
    ];

    verify_intersections(&lines);
}

#[test]
fn test_overlapping_segments() {
    let segments = vec![
        // First polygon set
        Line::from([(10.0, 10.0), (15.0, 10.0)]),
        Line::from([(15.0, 10.0), (15.0, 20.0)]),
        Line::from([(15.0, 20.0), (10.0, 10.0)]),
        Line::from([(10.0, -10.0), (15.0, -10.0)]),
        Line::from([(15.0, -10.0), (15.0, -20.0)]),
        Line::from([(15.0, -20.0), (10.0, -10.0)]),
        Line::from([(20.0, 10.0), (25.0, 10.0)]),
        Line::from([(25.0, 10.0), (25.0, 20.0)]),
        Line::from([(25.0, 20.0), (20.0, 10.0)]),
        Line::from([(20.0, -10.0), (25.0, -10.0)]),
        Line::from([(25.0, -10.0), (25.0, -20.0)]),
        Line::from([(25.0, -20.0), (20.0, -10.0)]),
        // Second polygon set
        Line::from([(10.0, 10.0), (15.0, 10.0)]),
        Line::from([(15.0, 10.0), (15.0, 15.0)]),
        Line::from([(15.0, 15.0), (10.0, 10.0)]),
        Line::from([(10.0, -10.0), (15.0, -10.0)]),
        Line::from([(15.0, -10.0), (15.0, -15.0)]),
        Line::from([(15.0, -15.0), (10.0, -10.0)]),
        Line::from([(20.0, 10.0), (25.0, 10.0)]),
        Line::from([(25.0, 10.0), (25.0, 5.0)]),
        Line::from([(25.0, 5.0), (20.0, 10.0)]),
        Line::from([(20.0, -10.0), (25.0, -10.0)]),
        Line::from([(25.0, -10.0), (25.0, -5.0)]),
        Line::from([(25.0, -5.0), (20.0, -10.0)]),
    ];

    verify_intersections(&segments);
}

#[test]
fn test_intersections_at_endpoints() {
    // This test creates line segments that specifically test endpoint intersections
    let segments = vec![
        // Star-like pattern with intersections at endpoints
        Line::from([(-9.0, 10.0), (9.0, 10.0)]),
        Line::from([(9.0, 10.0), (0.1, 0.1)]),
        Line::from([(0.1, 0.1), (10.0, 9.0)]),
        Line::from([(10.0, 9.0), (10.0, -9.0)]),
        Line::from([(10.0, -9.0), (0.1, -0.1)]),
        Line::from([(0.1, -0.1), (9.0, -10.0)]),
        Line::from([(9.0, -10.0), (-9.0, -10.0)]),
        Line::from([(-9.0, -10.0), (-0.1, -0.1)]),
        Line::from([(-0.1, -0.1), (-10.0, -9.0)]),
        Line::from([(-10.0, -9.0), (-10.0, 9.0)]),
        Line::from([(-10.0, 9.0), (-0.1, 0.1)]),
        Line::from([(-0.1, 0.1), (-9.0, 10.0)]),
        // Same pattern with slight offsets (to test nearly identical points)
        Line::from([(-8.99999, 10.0), (8.99999, 10.0)]),
        Line::from([(8.99999, 10.0), (0.1, 0.1)]),
        Line::from([(0.1, 0.1), (10.0, 8.99999)]),
        Line::from([(10.0, 8.99999), (10.0, -8.99999)]),
        Line::from([(10.0, -8.99999), (0.1, -0.1)]),
        Line::from([(0.1, -0.1), (8.99999, -10.0)]),
        Line::from([(8.99999, -10.0), (-8.99999, -10.0)]),
        Line::from([(-8.99999, -10.0), (-0.1, -0.1)]),
        Line::from([(-0.1, -0.1), (-10.0, -8.99999)]),
        Line::from([(-10.0, -8.99999), (-10.0, 8.99999)]),
        Line::from([(-10.0, 8.99999), (-0.1, 0.1)]),
        Line::from([(-0.1, 0.1), (-8.99999, 10.0)]),
    ];

    verify_intersections(&segments);
}

/// Test with very thin triangles that create nearly parallel lines
#[test]
fn test_thin_triangles() {
    // These thin triangles create segments that are nearly parallel
    // and test the algorithm's handling of precision issues
    let segments = vec![
        // First triangle (very thin)
        Line::from([(0.0, 0.0), (100.0, 0.0)]),
        Line::from([(100.0, 0.0), (50.0, 0.001)]),
        Line::from([(50.0, 0.001), (0.0, 0.0)]),
        // Second triangle (very thin, slightly offset)
        Line::from([(0.0, 0.1), (100.0, 0.1)]),
        Line::from([(100.0, 0.1), (50.0, 0.101)]),
        Line::from([(50.0, 0.101), (0.0, 0.1)]),
        // Intersector lines
        Line::from([(25.0, -1.0), (25.0, 2.0)]),
        Line::from([(75.0, -1.0), (75.0, 2.0)]),
    ];

    verify_intersections(&segments);
}

#[test]
fn test_checkerboard_pattern() {
    let mut segments = Vec::new();

    // Create a grid of squares (3x3 checkerboard)
    let size = 3;
    let square_size = 10.0;

    for row in 0..size {
        for col in 0..size {
            let x = col as f64 * square_size;
            let y = row as f64 * square_size;

            // Add the square
            segments.push(Line::from([(x, y), (x + square_size, y)]));
            segments.push(Line::from([
                (x + square_size, y),
                (x + square_size, y + square_size),
            ]));
            segments.push(Line::from([
                (x + square_size, y + square_size),
                (x, y + square_size),
            ]));
            segments.push(Line::from([(x, y + square_size), (x, y)]));

            // Add diagonal lines
            if (row + col) % 2 == 0 {
                segments.push(Line::from([(x, y), (x + square_size, y + square_size)]));
            } else {
                segments.push(Line::from([(x + square_size, y), (x, y + square_size)]));
            }
        }
    }

    verify_intersections(&segments);
}

/// Test inspired by the "hourglasses" shape
#[test]
fn test_hourglasses() {
    // Create hourglass shapes that test overlapping segments
    let segments = vec![
        // First hourglass
        Line::from([(0.0, 0.0), (10.0, 10.0)]),
        Line::from([(10.0, 10.0), (0.0, 20.0)]),
        Line::from([(0.0, 20.0), (10.0, 30.0)]),
        Line::from([(10.0, 30.0), (0.0, 40.0)]),
        Line::from([(0.0, 0.0), (0.0, 40.0)]),
        // Second hourglass (overlapping with the first)
        Line::from([(5.0, 5.0), (15.0, 15.0)]),
        Line::from([(15.0, 15.0), (5.0, 25.0)]),
        Line::from([(5.0, 25.0), (15.0, 35.0)]),
        Line::from([(15.0, 35.0), (5.0, 45.0)]),
        Line::from([(5.0, 5.0), (5.0, 45.0)]),
        // Connecting lines to create more intersections
        Line::from([(0.0, 10.0), (15.0, 10.0)]),
        Line::from([(0.0, 30.0), (15.0, 30.0)]),
    ];

    verify_intersections(&segments);
}