geo-polygonize-core 0.51.2

A native Rust port of the JTS/GEOS polygonization algorithm. Reconstruct valid polygons from a set of lines.
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
#[cfg(test)]
mod tests {
    use crate::Polygonizer;
    use geo::Area;
    use geo_types::LineString;

    #[test]
    fn test_polygonize_simple_triangle() {
        let mut poly = Polygonizer::new();
        poly.add_geometry(LineString::from(vec![(0.0, 0.0), (10.0, 0.0)]).into());
        poly.add_geometry(LineString::from(vec![(10.0, 0.0), (0.0, 10.0)]).into());
        poly.add_geometry(LineString::from(vec![(0.0, 10.0), (0.0, 0.0)]).into());

        let polygons = poly.polygonize().unwrap().polygons;
        assert!(!polygons.is_empty());
        let triangle = polygons.iter().find(|p| {
            let p2d = p.to_polygon_2d();
            p2d.unsigned_area() > 49.0 && p2d.unsigned_area() < 51.0
        });
        assert!(triangle.is_some());
    }

    #[test]
    fn test_polygonize_hole() {
        let mut poly = Polygonizer::new();
        // Outer square
        poly.add_geometry(
            LineString::from(vec![
                (0.0, 0.0),
                (10.0, 0.0),
                (10.0, 10.0),
                (0.0, 10.0),
                (0.0, 0.0),
            ])
            .into(),
        );

        // Inner square
        poly.add_geometry(
            LineString::from(vec![
                (2.0, 2.0),
                (2.0, 8.0),
                (8.0, 8.0),
                (8.0, 2.0),
                (2.0, 2.0),
            ])
            .into(),
        );

        let polygons = poly.polygonize().unwrap().polygons;
        assert_eq!(
            polygons.len(),
            2,
            "Expected 2 polygons, found {}",
            polygons.len()
        );

        let donut = polygons
            .iter()
            .find(|p| (p.unsigned_area_2d() - 64.0).abs() < 1.0);
        assert!(donut.is_some(), "Donut polygon not found");
        assert_eq!(donut.unwrap().interiors.len(), 1);

        let island = polygons
            .iter()
            .find(|p| (p.unsigned_area_2d() - 36.0).abs() < 1.0);
        assert!(island.is_some(), "Island polygon not found");
    }

    #[test]
    fn test_noding_crossing_lines() {
        let mut poly = Polygonizer::new();
        poly.options_mut().node_input = true;

        // Frame
        poly.add_geometry(
            LineString::from(vec![
                (0.0, 0.0),
                (10.0, 0.0),
                (10.0, 10.0),
                (0.0, 10.0),
                (0.0, 0.0),
            ])
            .into(),
        );

        // Diagonals
        poly.add_geometry(LineString::from(vec![(0.0, 0.0), (10.0, 10.0)]).into());
        poly.add_geometry(LineString::from(vec![(0.0, 10.0), (10.0, 0.0)]).into());

        let polygons = poly.polygonize().expect("Polygonization failed").polygons;
        // Frame (empty because triangles are holes) + 4 Triangles
        // Wait, the logic assigns holes to shells.
        // Frame is OuterCCW (100) and OuterCW (-100).
        // Triangles are InnerCCW (25) and InnerCW (-25).
        // 4 Triangles (CW) are holes of Frame (OuterCCW).
        // Area = 100 - 4*25 = 0.
        // The Frame has Area 0 and is filtered out by the Polygonizer (tolerance 1e-6).
        // 4 Triangles (CCW) are shells. Area 25.
        // So we get:
        // 1. Triangle 1 (Area 25)
        // 2. Triangle 2 (Area 25)
        // 3. Triangle 3 (Area 25)
        // 4. Triangle 4 (Area 25)

        assert_eq!(
            polygons.len(),
            4,
            "Expected 4 polygons, found {}",
            polygons.len()
        );
        let triangles_count = polygons
            .iter()
            .filter(|p| (p.unsigned_area_2d() - 25.0).abs() < 1e-6)
            .count();
        assert_eq!(triangles_count, 4, "Expected 4 triangles of area 25");
    }

    #[test]
    fn test_noding_collinear_lines() {
        let mut poly = Polygonizer::new();
        poly.options_mut().node_input = true;

        // 1. Line (0,0)->(10,0)
        // 2. Line (5,0)->(15,0) (Overlap 5..10)
        // 3. Line (10,0)->(10,10)->(5,10)->(5,0) (To close the rectangle with the overlap)

        // The overlap is on (5,0) to (10,0).
        // If handled correctly, we should get:
        // - Segment (0,0)-(5,0)
        // - Segment (5,0)-(10,0) (Double covered but graph should unique-ify edges or handle overlap?)
        // - Segment (10,0)-(15,0)
        // - And the rest of the box.

        // We expect a rectangle (5,0)-(10,0)-(10,10)-(5,10)-(5,0). Area 50.

        poly.add_geometry(LineString::from(vec![(0.0, 0.0), (10.0, 0.0)]).into());
        poly.add_geometry(LineString::from(vec![(5.0, 0.0), (15.0, 0.0)]).into());
        poly.add_geometry(
            LineString::from(vec![(10.0, 0.0), (10.0, 10.0), (5.0, 10.0), (5.0, 0.0)]).into(),
        );

        let polygons = poly.polygonize().expect("Polygonization failed").polygons;

        // Should find the rectangle of area 50.
        let rect = polygons
            .iter()
            .find(|p| (p.unsigned_area_2d() - 50.0).abs() < 1e-6);
        assert!(
            rect.is_some(),
            "Expected rectangle of area 50 from collinear overlap"
        );
    }

    #[test]
    fn test_figure_8_pinching_bowtie() {
        let mut poly = Polygonizer::new();
        // Self-intersecting bowtie that forms exactly two valid cycles.
        // Node_input = true is needed to create the intersection node at (5, 5).
        poly.options_mut().node_input = true;
        poly.add_geometry(
            LineString::from(vec![
                (0.0, 0.0),
                (10.0, 10.0),
                (10.0, 0.0),
                (0.0, 10.0),
                (0.0, 0.0),
            ])
            .into(),
        );

        let polygons = poly.polygonize().expect("Polygonization failed").polygons;
        // The intersection is at (5,5).
        // Cycle 1: (0,0)-(5,5)-(0,10)-(0,0). Area = 25.
        // Cycle 2: (10,10)-(5,5)-(10,0)-(10,10). Area = 25.
        // Both are valid polygons.
        assert_eq!(
            polygons.len(),
            2,
            "Expected 2 polygons from figure-8 pinching, found {}",
            polygons.len()
        );

        let area1 = polygons[0].to_polygon_2d().unsigned_area();
        let area2 = polygons[1].to_polygon_2d().unsigned_area();
        assert!((area1 - 25.0).abs() < 1e-6);
        assert!((area2 - 25.0).abs() < 1e-6);
    }

    #[test]
    fn test_polygonize_empty_input() {
        let mut poly = Polygonizer::new();
        let polygons = poly
            .polygonize()
            .expect("Polygonization should not fail on empty input")
            .polygons;
        assert_eq!(polygons.len(), 0);
    }

    #[test]
    fn test_polygonize_empty_linestring() {
        let mut poly = Polygonizer::new();
        // Add an empty LineString
        poly.add_geometry(geo_types::Geometry::LineString(geo_types::LineString::new(
            vec![],
        )));
        let polygons = poly
            .polygonize()
            .expect("Polygonization should not fail on empty LineString")
            .polygons;
        assert_eq!(polygons.len(), 0);
    }

    #[test]
    fn test_polygonize_point() {
        let mut poly = Polygonizer::new();
        // Add a single point (should be ignored by extract_lines)
        poly.add_geometry(geo_types::Geometry::Point(geo_types::Point::new(0.0, 0.0)));
        let polygons = poly
            .polygonize()
            .expect("Polygonization should not fail on Point input")
            .polygons;
        assert_eq!(polygons.len(), 0);
    }

    #[test]
    fn test_polygonize_empty_input_with_noding() {
        let mut poly = Polygonizer::new();
        poly.options_mut().node_input = true;
        let polygons = poly
            .polygonize()
            .expect("Polygonization should not fail on empty input with noding")
            .polygons;
        assert_eq!(polygons.len(), 0);
    }

    #[test]
    fn test_polygonize_empty_linestring_with_noding() {
        let mut poly = Polygonizer::new();
        poly.options_mut().node_input = true;
        poly.add_geometry(geo_types::Geometry::LineString(geo_types::LineString::new(
            vec![],
        )));
        let polygons = poly
            .polygonize()
            .expect("Polygonization should not fail on empty LineString with noding")
            .polygons;
        assert_eq!(polygons.len(), 0);
    }

    #[test]
    fn test_polygonize_point_with_noding() {
        let mut poly = Polygonizer::new();
        poly.options_mut().node_input = true;
        poly.add_geometry(geo_types::Geometry::Point(geo_types::Point::new(0.0, 0.0)));
        let polygons = poly
            .polygonize()
            .expect("Polygonization should not fail on Point input with noding")
            .polygons;
        assert_eq!(polygons.len(), 0);
    }

    #[test]
    fn test_concave_hole_uses_interior_probe_not_centroid() {
        let mut poly = Polygonizer::new();

        // Outer shell (CCW)
        poly.add_geometry(
            LineString::from(vec![
                (0.0, 0.0),
                (10.0, 0.0),
                (10.0, 10.0),
                (0.0, 10.0),
                (0.0, 0.0),
            ])
            .into(),
        );

        // Concave C-shaped hole (CW). Its centroid lies outside of the ring.
        poly.add_geometry(
            LineString::from(vec![
                (3.0, 3.0),
                (3.0, 9.0),
                (9.0, 9.0),
                (9.0, 7.0),
                (5.0, 7.0),
                (5.0, 5.0),
                (9.0, 5.0),
                (9.0, 3.0),
                (3.0, 3.0),
            ])
            .into(),
        );

        let polygons = poly.polygonize().expect("Polygonization failed").polygons;

        let shell_with_hole = polygons
            .iter()
            .find(|p| (p.unsigned_area_2d() - 72.0).abs() < 1.0);
        assert!(
            shell_with_hole.is_some(),
            "Expected outer shell area near 72 with assigned concave hole"
        );
        assert_eq!(shell_with_hole.unwrap().interiors.len(), 1);
    }

    #[test]
    fn test_point_touch_hole_is_kept() {
        let mut poly = Polygonizer::new();

        poly.add_geometry(
            LineString::from(vec![
                (0.0, 0.0),
                (10.0, 0.0),
                (10.0, 10.0),
                (0.0, 10.0),
                (0.0, 0.0),
            ])
            .into(),
        );

        // CW hole touching shell at one vertex (5,0)
        poly.add_geometry(
            LineString::from(vec![(5.0, 0.0), (4.0, 2.0), (6.0, 2.0), (5.0, 0.0)]).into(),
        );

        let polygons = poly.polygonize().expect("Polygonization failed").polygons;

        let shell_with_hole = polygons
            .iter()
            .find(|p| (p.unsigned_area_2d() - 98.0).abs() < 1.0);
        assert!(
            shell_with_hole.is_some(),
            "Point-touch hole should be retained on parent shell"
        );
        assert_eq!(shell_with_hole.unwrap().interiors.len(), 1);
    }

    #[test]
    fn test_edge_touch_hole_is_dropped() {
        let mut poly = Polygonizer::new();

        poly.add_geometry(
            LineString::from(vec![
                (0.0, 0.0),
                (10.0, 0.0),
                (10.0, 10.0),
                (0.0, 10.0),
                (0.0, 0.0),
            ])
            .into(),
        );

        // CW hole sharing an entire edge segment with the shell boundary.
        poly.add_geometry(
            LineString::from(vec![
                (2.0, 0.0),
                (2.0, 2.0),
                (4.0, 2.0),
                (4.0, 0.0),
                (2.0, 0.0),
            ])
            .into(),
        );

        let polygons = poly.polygonize().expect("Polygonization failed").polygons;

        let outer = polygons
            .iter()
            .find(|p| (p.unsigned_area_2d() - 100.0).abs() < 1.0);
        assert!(
            outer.is_some(),
            "Edge-touch hole should not be assigned to the parent shell"
        );
        assert_eq!(outer.unwrap().interiors.len(), 0);
    }

    #[test]
    fn test_extract_only_polygonal_nested() {
        let mut poly = Polygonizer::new();
        poly.options_mut().extract_only_polygonal = true;

        // Outer square (0,0)-(10,0)-(10,10)-(0,10)-(0,0) (Area 100)
        poly.add_geometry(
            LineString::from(vec![
                (0.0, 0.0),
                (10.0, 0.0),
                (10.0, 10.0),
                (0.0, 10.0),
                (0.0, 0.0),
            ])
            .into(),
        );

        // Inner square (2,2)-(8,2)-(8,8)-(2,8)-(2,2) (Area 36)
        poly.add_geometry(
            LineString::from(vec![
                (2.0, 2.0),
                (8.0, 2.0),
                (8.0, 8.0),
                (2.0, 8.0),
                (2.0, 2.0),
            ])
            .into(),
        );

        let result = poly.polygonize().expect("Polygonization failed");
        let polygons = result.polygons;

        // Default behavior would return 2 polygons.
        // With extract_only_polygonal=true, the inner shell should be discarded.
        assert_eq!(polygons.len(), 1, "Expected 1 polygon (outer)");
        assert!((polygons[0].to_polygon_2d().unsigned_area() - 64.0).abs() < 1e-6);
        assert_eq!(polygons[0].interiors.len(), 1);
    }

    #[test]
    fn test_extract_only_polygonal_disjoint() {
        let mut poly = Polygonizer::new();
        poly.options_mut().extract_only_polygonal = true;

        // Poly 1
        poly.add_geometry(
            LineString::from(vec![
                (0.0, 0.0),
                (10.0, 0.0),
                (10.0, 10.0),
                (0.0, 10.0),
                (0.0, 0.0),
            ])
            .into(),
        );

        // Poly 2 (Disjoint)
        poly.add_geometry(
            LineString::from(vec![
                (20.0, 0.0),
                (30.0, 0.0),
                (30.0, 10.0),
                (20.0, 10.0),
                (20.0, 0.0),
            ])
            .into(),
        );

        let result = poly.polygonize().expect("Polygonization failed");
        assert_eq!(result.polygons.len(), 2);
    }

    #[test]
    fn test_extract_only_polygonal_concentric_squares() {
        let mut poly = Polygonizer::new();
        poly.options_mut().extract_only_polygonal = true;

        // Square 1: Outer, area 100
        poly.add_geometry(
            LineString::from(vec![
                (0.0, 0.0),
                (10.0, 0.0),
                (10.0, 10.0),
                (0.0, 10.0),
                (0.0, 0.0),
            ])
            .into(),
        );

        // Square 2: Middle (hole of Square 1), area 64
        // Hole winding should be CW
        poly.add_geometry(
            LineString::from(vec![
                (1.0, 1.0),
                (1.0, 9.0),
                (9.0, 9.0),
                (9.0, 1.0),
                (1.0, 1.0),
            ])
            .into(),
        );

        // Square 3: Inner (shell inside Square 2), area 36
        // Shell winding should be CCW
        poly.add_geometry(
            LineString::from(vec![
                (2.0, 2.0),
                (8.0, 2.0),
                (8.0, 8.0),
                (2.0, 8.0),
                (2.0, 2.0),
            ])
            .into(),
        );

        let result = poly.polygonize().expect("Polygonization failed");
        let polygons = result.polygons;

        // With extract_only_polygonal=true:
        // Square 1 is an outer shell (depth 0) -> kept.
        // Square 2 is a hole.
        // Square 3 is a shell inside Square 2 (depth 1) -> dropped.
        assert_eq!(polygons.len(), 1, "Expected 1 polygon (outer)");
        assert!((polygons[0].to_polygon_2d().unsigned_area() - 36.0).abs() < 1e-6);
        assert_eq!(polygons[0].interiors.len(), 0);

        // Now, let's add a fourth square
        let mut poly2 = Polygonizer::new();
        poly2.options_mut().extract_only_polygonal = true;

        poly2.add_geometry(
            LineString::from(vec![
                (0.0, 0.0),
                (10.0, 0.0),
                (10.0, 10.0),
                (0.0, 10.0),
                (0.0, 0.0),
            ])
            .into(),
        );
        poly2.add_geometry(
            LineString::from(vec![
                (1.0, 1.0),
                (1.0, 9.0),
                (9.0, 9.0),
                (9.0, 1.0),
                (1.0, 1.0),
            ])
            .into(),
        );
        poly2.add_geometry(
            LineString::from(vec![
                (2.0, 2.0),
                (8.0, 2.0),
                (8.0, 8.0),
                (2.0, 8.0),
                (2.0, 2.0),
            ])
            .into(),
        );
        // Square 4: inner-most hole, area 16
        poly2.add_geometry(
            LineString::from(vec![
                (3.0, 3.0),
                (3.0, 7.0),
                (7.0, 7.0),
                (7.0, 3.0),
                (3.0, 3.0),
            ])
            .into(),
        );
        // Square 5: innermost shell, area 4
        poly2.add_geometry(
            LineString::from(vec![
                (4.0, 4.0),
                (6.0, 4.0),
                (6.0, 6.0),
                (4.0, 6.0),
                (4.0, 4.0),
            ])
            .into(),
        );

        let result2 = poly2.polygonize().expect("Polygonization failed");
        let polygons2 = result2.polygons;

        // With extract_only_polygonal=true:
        // Square 1 (depth 0) -> kept (has Square 2 as hole)
        // Square 3 (depth 1) -> dropped
        // Square 5 (depth 2) -> kept (has no holes)
        assert_eq!(
            polygons2.len(),
            2,
            "Expected 2 polygons (outermost and innermost)"
        );
        let areas: std::collections::HashSet<_> = polygons2
            .iter()
            .map(|p| p.to_polygon_2d().unsigned_area() as i64)
            .collect();
        assert!(areas.contains(&16), "Areas: {:?}", areas);
        assert!(areas.contains(&4), "Areas: {:?}", areas);
    }

    #[test]
    fn test_small_rings_are_preserved() {
        let mut poly = Polygonizer::new();

        // 1. Valid Square (10x10)
        poly.add_geometry(
            LineString::from(vec![
                (0.0, 0.0),
                (10.0, 0.0),
                (10.0, 10.0),
                (0.0, 10.0),
                (0.0, 0.0),
            ])
            .into(),
        );

        // 2. Tiny Ring 1 (formerly discarded by the absolute area cutoff)
        // A triangle with base 1e-5 and height 1e-5 has area 0.5e-10.
        poly.add_geometry(
            LineString::from(vec![
                (20.0, 0.0),
                (20.00001, 0.0),
                (20.0, 0.00001),
                (20.0, 0.0),
            ])
            .into(),
        );

        // 3. Tiny Ring 2 (Inside Tiny Ring 1? No, let's make them disjoint first to test capture)
        // Another tiny one.
        poly.add_geometry(
            LineString::from(vec![
                (30.0, 0.0),
                (30.00001, 0.0),
                (30.0, 0.00001),
                (30.0, 0.0),
            ])
            .into(),
        );

        let result = poly.polygonize().expect("Polygonization failed");
        assert_eq!(result.polygons.len(), 3);
        assert!(result.invalid_rings.is_empty());
    }

    #[test]
    fn test_repeated_polygonize_preserves_input_segments_when_node_input_disabled() {
        let mut poly = Polygonizer::new();
        poly.options_mut().node_input = false;
        poly.options_mut().diagnostics.enabled = true;

        poly.add_geometry(
            LineString::from(vec![
                (0.0, 0.0),
                (10.0, 0.0),
                (10.0, 10.0),
                (0.0, 10.0),
                (0.0, 0.0),
            ])
            .into(),
        );

        let first = poly.polygonize().expect("First polygonization failed");
        let first_diagnostics = first.diagnostics.unwrap();
        assert_eq!(first_diagnostics.input_segment_count, 4);
        assert_eq!(first_diagnostics.noded_segment_count, 4);

        let second = poly.polygonize().expect("Second polygonization failed");
        let second_diagnostics = second.diagnostics.unwrap();
        assert_eq!(second_diagnostics.input_segment_count, 4);
        assert_eq!(second_diagnostics.noded_segment_count, 4);
    }

    #[test]
    fn diagnostics_report_noding_iterations() {
        let mut poly = Polygonizer::new();
        poly.options_mut().node_input = true;
        poly.options_mut().diagnostics.enabled = true;
        poly.add_geometry(LineString::from(vec![(0.0, 0.0), (10.0, 10.0)]).into());
        poly.add_geometry(LineString::from(vec![(0.0, 10.0), (10.0, 0.0)]).into());

        let diagnostics = poly.polygonize().unwrap().diagnostics.unwrap();

        assert_eq!(diagnostics.noding_iterations.len(), 1);
        assert_eq!(diagnostics.noding_iterations[0].intersections_found, 2);
        assert_eq!(diagnostics.noding_iterations[0].nodes_added, 2);
        assert_eq!(diagnostics.intersection_stats.interpolated_intersections, 2);
        assert_eq!(diagnostics.intersection_stats.exact_intersections, 1);
        assert_eq!(diagnostics.noding_work_stats.candidate_pairs, 1);
        assert_eq!(diagnostics.noding_work_stats.aabb_rejections, 0);
        assert_eq!(diagnostics.noding_work_stats.exact_intersection_calls, 1);
        assert_eq!(diagnostics.noding_work_stats.split_events, 2);
    }

    #[test]
    fn timing_only_diagnostics_skip_work_counters() {
        let mut poly = Polygonizer::new();
        poly.options_mut().node_input = true;
        poly.options_mut().diagnostics.timings = true;
        poly.add_geometry(LineString::from(vec![(0.0, 0.0), (10.0, 10.0)]).into());
        poly.add_geometry(LineString::from(vec![(0.0, 10.0), (10.0, 0.0)]).into());

        let diagnostics = poly.polygonize().unwrap().diagnostics.unwrap();

        assert_eq!(diagnostics.input_segment_count, 2);
        assert!(diagnostics.noding_iterations.is_empty());
        assert_eq!(diagnostics.noding_work_stats.candidate_pairs, 0);
    }

    #[test]
    fn diagnostics_report_pre_snap_candidates() {
        let mut poly = Polygonizer::new();
        poly.options_mut().node_input = true;
        poly.options_mut().pre_snap_tolerance = 0.5;
        poly.options_mut().diagnostics.enabled = true;
        poly.add_geometry(LineString::from(vec![(0.0, 0.0), (10.0, 0.0)]).into());
        poly.add_geometry(LineString::from(vec![(5.0, 0.4), (5.0, 1.0)]).into());

        let diagnostics = poly.polygonize().unwrap().diagnostics.unwrap();

        assert!(diagnostics.noding_work_stats.pre_snap_vertex_candidates > 0);
    }

    #[test]
    fn diagnostics_report_containment_work() {
        let mut poly = Polygonizer::new();
        poly.options_mut().diagnostics.enabled = true;
        poly.add_geometry(
            LineString::from(vec![
                (0.0, 0.0),
                (10.0, 0.0),
                (10.0, 10.0),
                (0.0, 10.0),
                (0.0, 0.0),
            ])
            .into(),
        );
        poly.add_geometry(
            LineString::from(vec![
                (2.0, 2.0),
                (2.0, 8.0),
                (8.0, 8.0),
                (8.0, 2.0),
                (2.0, 2.0),
            ])
            .into(),
        );

        let stats = poly
            .polygonize()
            .unwrap()
            .diagnostics
            .unwrap()
            .containment_stats;

        assert!(stats.prepared_shells > 0);
        assert!(stats.envelope_candidates > 0);
        assert!(stats.point_in_ring_calls > 0);
        assert!(stats.point_in_ring_calls <= stats.envelope_candidates);
        assert_eq!(stats.max_point_in_ring_calls_per_shell, 1);
        assert_eq!(stats.shells_with_64_plus_point_in_ring_calls, 0);
    }

    #[test]
    fn test_nested_small_rings_are_preserved() {
        let mut poly = Polygonizer::new();

        // Ring A: Tiny but "outer" relative to B.
        // Area = 1e-10.
        let ring_a = LineString::from(vec![
            (0.0, 0.0),
            (1e-5, 0.0),
            (1e-5, 1e-5),
            (0.0, 1e-5),
            (0.0, 0.0),
        ]);

        // Ring B (Inner): Area is smaller and is contained in A.
        let ring_b = LineString::from(vec![
            (0.2e-5, 0.2e-5),
            (0.8e-5, 0.2e-5),
            (0.8e-5, 0.8e-5),
            (0.2e-5, 0.8e-5),
            (0.2e-5, 0.2e-5),
        ]);

        poly.add_geometry(ring_a.into());
        poly.add_geometry(ring_b.into());

        let result = poly.polygonize().expect("Polygonization failed");
        assert_eq!(result.polygons.len(), 2);
        assert!(result.invalid_rings.is_empty());
    }
}