boost_geometry 0.0.8

Rust port of Boost.Geometry — same design philosophy (concepts, tags, strategy-based dispatch), works with your own point/geometry types, re-exported as a single API surface.
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
//! Public-facade DE-9IM tests across pointlike, linear, and areal pairs.

use boost_geometry::cs::Cartesian;
use boost_geometry::model::{
    Box as ModelBox, DynGeometry, DynGeometryCollection, Linestring, MultiLinestring, MultiPoint,
    MultiPolygon, Point2D, Polygon, Ring, Segment, polygon,
};
use boost_geometry::overlay::{
    De9im, Dimension, OverlayError, RelateError, crosses, overlaps, relate, relation, touches,
};
use boost_geometry::trait_::Polygon as _;

type P = Point2D<f64, Cartesian>;

fn square() -> Polygon<P> {
    polygon![[(0.0, 0.0), (4.0, 0.0), (4.0, 4.0), (0.0, 4.0), (0.0, 0.0)]]
}

/// `test/algorithms/relate/relate_pointlike_geometry.cpp:20-24` — equal and
/// disjoint point pairs.
#[test]
fn point_point_relations_use_point_dimension() {
    let equal = relation(&P::new(1.0, 1.0), &P::new(1.0, 1.0)).unwrap();
    assert_eq!(equal.interior_interior(), Dimension::Point);

    let disjoint = relation(&P::new(1.0, 1.0), &P::new(2.0, 2.0)).unwrap();
    assert_eq!(disjoint.interior_interior(), Dimension::Empty);
    assert_eq!(disjoint.interior_exterior(), Dimension::Point);
    assert_eq!(disjoint.exterior_interior(), Dimension::Point);
}

/// `test/algorithms/relate/relate_pointlike_geometry.cpp:166-174` — interior,
/// boundary, and exterior points against an areal geometry.
#[test]
fn point_polygon_distinguishes_interior_and_boundary() {
    let polygon = square();
    let inside = relation(&P::new(2.0, 2.0), &polygon).unwrap();
    assert_eq!(inside.interior_interior(), Dimension::Point);

    let boundary = relation(&P::new(0.0, 2.0), &polygon).unwrap();
    assert_eq!(boundary.interior_interior(), Dimension::Empty);
    assert_eq!(boundary.m[0][1], Dimension::Point);
    assert!(touches(&P::new(0.0, 2.0), &polygon).unwrap());
    assert!(relate(&P::new(2.0, 2.0), &polygon, "T********").unwrap());

    let reversed = relation(&polygon, &P::new(0.0, 2.0)).unwrap();
    for row in 0..3 {
        for column in 0..3 {
            assert_eq!(boundary.m[row][column], reversed.m[column][row]);
        }
    }
}

/// `test/algorithms/relate/relate_linear_linear.cpp:104-105` — the interiors
/// of two crossing lines meet at a point.
#[test]
fn crossing_linestrings_have_point_interior_intersection() {
    let first = Linestring::from_vec(vec![P::new(0.0, 0.0), P::new(4.0, 4.0)]);
    let second = Linestring::from_vec(vec![P::new(0.0, 4.0), P::new(4.0, 0.0)]);
    let matrix = relation(&first, &second).unwrap();
    assert_eq!(matrix.interior_interior(), Dimension::Point);
    assert_eq!(matrix.interior_exterior(), Dimension::Curve);
    assert_eq!(matrix.exterior_interior(), Dimension::Curve);
}

/// `test/algorithms/relate/relate_linear_areal.cpp:44-64` — a line crossing a
/// polygon has a curve in its interior and point boundary intersections.
#[test]
fn linestring_polygon_reports_curve_and_boundary_crossings() {
    let line = Linestring::from_vec(vec![P::new(-1.0, 2.0), P::new(5.0, 2.0)]);
    let matrix = relation(&line, &square()).unwrap();
    assert_eq!(matrix.interior_interior(), Dimension::Curve);
    assert_eq!(matrix.m[0][1], Dimension::Point);
    assert_eq!(matrix.interior_exterior(), Dimension::Curve);
    assert!(crosses(&line, &square()).unwrap());
}

fn box_at(x0: f64, y0: f64, x1: f64, y1: f64) -> Polygon<P> {
    polygon![[(x0, y0), (x1, y0), (x1, y1), (x0, y1), (x0, y0)]]
}

/// `test/algorithms/relate/relate_areal_areal.cpp:221-239` — colocated edge
/// and vertex clusters carry exact boundary-intersection dimensions.
#[test]
fn areal_touches_distinguish_shared_edges_and_vertices() {
    let first = box_at(0.0, 0.0, 4.0, 4.0);
    let edge_touch = box_at(4.0, 0.0, 8.0, 4.0);
    let edge_matrix = relation(&first, &edge_touch).unwrap();
    assert_eq!(edge_matrix.interior_interior(), Dimension::Empty);
    assert_eq!(edge_matrix.boundary_boundary(), Dimension::Curve);
    assert!(touches(&first, &edge_touch).unwrap());
    assert!(!overlaps(&first, &edge_touch).unwrap());

    let vertex_touch = box_at(4.0, 4.0, 8.0, 8.0);
    let vertex_matrix = relation(&first, &vertex_touch).unwrap();
    assert_eq!(vertex_matrix.interior_interior(), Dimension::Empty);
    assert_eq!(vertex_matrix.boundary_boundary(), Dimension::Point);
    assert!(touches(&first, &vertex_touch).unwrap());
}

/// `test/algorithms/relate/relate_areal_areal.cpp:222-239` — edge-aligned area
/// overlap is distinct from a pure boundary touch.
#[test]
fn collinear_boundaries_can_still_overlap_in_area() {
    let first = box_at(0.0, 0.0, 3.0, 1.0);
    let second = box_at(2.0, 0.0, 5.0, 1.0);
    let matrix = relation(&first, &second).unwrap();
    assert_eq!(matrix.interior_interior(), Dimension::Area);
    assert_eq!(matrix.boundary_boundary(), Dimension::Curve);
    assert!(overlaps(&first, &second).unwrap());
    assert!(!touches(&first, &second).unwrap());
}

/// `test/algorithms/relate/relate_areal_areal.cpp:196-219` — interior rings
/// participate in point-set topology.
#[test]
fn areal_relation_respects_polygon_holes() {
    let donut: Polygon<P> = polygon![
        [
            (0.0, 0.0),
            (10.0, 0.0),
            (10.0, 10.0),
            (0.0, 10.0),
            (0.0, 0.0)
        ],
        [(3.0, 3.0), (7.0, 3.0), (7.0, 7.0), (3.0, 7.0), (3.0, 3.0)]
    ];
    let island = box_at(4.0, 4.0, 6.0, 6.0);
    let matrix = relation(&donut, &island).unwrap();
    assert_eq!(matrix.interior_interior(), Dimension::Empty);
    assert_eq!(matrix.boundary_boundary(), Dimension::Empty);
    assert!(!touches(&donut, &island).unwrap());
    assert!(!overlaps(&donut, &island).unwrap());

    let point_in_hole = relation(&P::new(5.0, 5.0), &donut).unwrap();
    assert_eq!(point_in_hole.interior_interior(), Dimension::Empty);
    assert_eq!(point_in_hole.interior_exterior(), Dimension::Point);
}

/// `test/algorithms/relate/relate_pointlike_geometry.cpp:120-148` — endpoint,
/// interior, exterior, closed, empty, and degenerate linear locations.
#[test]
fn point_linestring_relations_cover_all_linear_locations() {
    let open = Linestring::from_vec(vec![P::new(0.0, 0.0), P::new(4.0, 0.0)]);
    let endpoint = relation(&P::new(0.0, 0.0), &open).unwrap();
    assert_eq!(endpoint.m[0][1], Dimension::Point);
    assert_eq!(endpoint.m[2][1], Dimension::Point);

    let interior = relation(&P::new(2.0, 0.0), &open).unwrap();
    assert_eq!(interior.interior_interior(), Dimension::Point);
    let exterior = relation(&P::new(2.0, 1.0), &open).unwrap();
    assert_eq!(exterior.interior_exterior(), Dimension::Point);

    let reversed = relation(&open, &P::new(2.0, 0.0)).unwrap();
    assert_eq!(reversed, interior.transposed());

    let closed = Linestring::from_vec(vec![P::new(0.0, 0.0), P::new(2.0, 0.0), P::new(0.0, 0.0)]);
    assert_eq!(
        relation(&P::new(0.0, 0.0), &closed)
            .unwrap()
            .interior_interior(),
        Dimension::Point
    );

    let empty = Linestring::<P>::new();
    assert_eq!(
        relation(&P::new(0.0, 0.0), &empty)
            .unwrap()
            .interior_exterior(),
        Dimension::Point
    );
    let degenerate = Linestring::from_vec(vec![P::new(1.0, 1.0), P::new(1.0, 1.0)]);
    assert_eq!(
        relation(&P::new(1.0, 1.0), &degenerate)
            .unwrap()
            .interior_interior(),
        Dimension::Point
    );
}

/// `test/algorithms/relate/relate_linear_linear.cpp:104-139` — disjoint,
/// endpoint-touching, overlapping, and crossing segment pairs.
#[test]
fn linear_relations_cover_disjoint_touch_and_overlap_kernels() {
    let horizontal = Linestring::from_vec(vec![P::new(0.0, 0.0), P::new(4.0, 0.0)]);
    let disjoint = Linestring::from_vec(vec![P::new(0.0, 2.0), P::new(4.0, 2.0)]);
    assert_eq!(
        relation(&horizontal, &disjoint)
            .unwrap()
            .interior_interior(),
        Dimension::Empty
    );

    let touching = Linestring::from_vec(vec![P::new(4.0, 0.0), P::new(5.0, 1.0)]);
    assert_eq!(
        relation(&horizontal, &touching)
            .unwrap()
            .boundary_boundary(),
        Dimension::Point
    );

    let overlapping = Linestring::from_vec(vec![P::new(2.0, 0.0), P::new(6.0, 0.0)]);
    assert_eq!(
        relation(&horizontal, &overlapping)
            .unwrap()
            .interior_interior(),
        Dimension::Curve
    );

    let diagonal = Linestring::from_vec(vec![P::new(0.0, -1.0), P::new(4.0, 1.0)]);
    assert!(crosses(&horizontal, &diagonal).unwrap());
}

/// `test/algorithms/relate/relate_linear_linear.cpp:155-164` — consecutive
/// duplicate vertices contribute no segment, while a closed linestring has no
/// mod-2 boundary. These are exact Boost DE-9IM fixtures exercised through the
/// public relation facade.
#[test]
fn duplicated_and_closed_linestrings_match_boost_matrices() {
    let reference =
        Linestring::from_vec(vec![P::new(0.0, 0.0), P::new(2.0, 2.0), P::new(4.0, 2.0)]);
    for duplicated in [
        Linestring::from_vec(vec![P::new(1.0, 1.0), P::new(2.0, 2.0), P::new(2.0, 2.0)]),
        Linestring::from_vec(vec![P::new(1.0, 1.0), P::new(1.0, 1.0), P::new(2.0, 2.0)]),
    ] {
        assert!(
            relation(&duplicated, &reference)
                .unwrap()
                .matches("1FF0FF102")
                .unwrap()
        );
    }

    let open = Linestring::from_vec(vec![P::new(0.0, 0.0), P::new(10.0, 0.0)]);
    let closed = Linestring::from_vec(vec![
        P::new(5.0, 0.0),
        P::new(9.0, 0.0),
        P::new(5.0, 5.0),
        P::new(1.0, 0.0),
        P::new(5.0, 0.0),
    ]);
    let observed = relation(&open, &closed).unwrap();
    assert!(observed.matches("1F1FF01F2").unwrap());

    // `relate_linear_linear.cpp:170-174` — a two-coordinate point-size
    // linestring is topologically a point in either ordered position.
    let point_size = Linestring::from_vec(vec![P::new(1.0, 0.0), P::new(1.0, 0.0)]);
    let horizontal = Linestring::from_vec(vec![P::new(0.0, 0.0), P::new(5.0, 0.0)]);
    let point_line = relation(&point_size, &horizontal).unwrap();
    assert!(point_line.matches("0FFFFF102").unwrap());
    assert_eq!(
        relation(&horizontal, &point_size).unwrap(),
        point_line.transposed()
    );
}

/// `test/algorithms/relate/relate_linear_areal.cpp:44-87` — a line on an
/// areal boundary and the reversed ordered pair exercise the boundary mask.
#[test]
fn line_on_polygon_boundary_and_reversed_pair_are_related() {
    let polygon = square();
    let boundary = Linestring::from_vec(vec![P::new(0.0, 0.0), P::new(4.0, 0.0)]);
    let matrix = relation(&boundary, &polygon).unwrap();
    assert_eq!(matrix.m[0][1], Dimension::Point);
    assert_eq!(relation(&polygon, &boundary).unwrap(), matrix.transposed());

    let empty_polygon = Polygon::new(Ring::<P>::new());
    assert_eq!(
        relation(&P::new(1.0, 1.0), &empty_polygon)
            .unwrap()
            .interior_exterior(),
        Dimension::Point
    );

    let open_polygon: Polygon<P> = Polygon::new(Ring::from_vec(vec![
        P::new(0.0, 0.0),
        P::new(4.0, 0.0),
        P::new(4.0, 4.0),
        P::new(0.0, 4.0),
    ]));
    assert_eq!(
        relation(&P::new(0.0, 2.0), &open_polygon).unwrap().m[0][1],
        Dimension::Point
    );
}

/// `test/algorithms/relate/relate_areal_areal.cpp:76-96` — containment in
/// either order distinguishes which polygon has interior outside the other.
#[test]
fn areal_containment_covers_both_ordered_matrix_arms() {
    let outer = box_at(0.0, 0.0, 10.0, 10.0);
    let inner = box_at(2.0, 2.0, 4.0, 4.0);
    let outer_inner = relation(&outer, &inner).unwrap();
    let inner_outer = relation(&inner, &outer).unwrap();
    assert_eq!(outer_inner, inner_outer.transposed());
    assert_eq!(outer_inner.interior_interior(), Dimension::Area);
    assert_eq!(outer_inner.interior_exterior(), Dimension::Area);
    assert_eq!(inner_outer.exterior_interior(), Dimension::Area);

    let identical = relation(&outer, &outer).unwrap();
    assert_eq!(identical.interior_interior(), Dimension::Area);

    let open_outer: Polygon<P> = Polygon::new(Ring::<P>::from_vec(vec![
        P::new(0.0, 0.0),
        P::new(0.0, 10.0),
        P::new(10.0, 10.0),
        P::new(10.0, 0.0),
    ]));
    assert_eq!(
        relation(&open_outer, &open_outer)
            .unwrap()
            .boundary_boundary(),
        Dimension::Curve
    );
}

#[test]
fn public_matrix_masks_cover_every_symbol_and_overlay_errors() {
    let matrix = De9im {
        m: [
            [Dimension::Empty, Dimension::Point, Dimension::Curve],
            [Dimension::Area, Dimension::Empty, Dimension::Point],
            [Dimension::Curve, Dimension::Area, Dimension::Empty],
        ],
    };
    assert!(matrix.matches("F012F012F").unwrap());
    assert!(matrix.matches("*T*******").unwrap());
    assert_eq!(matrix.matches("********"), Err(RelateError::InvalidMask));
    assert_eq!(matrix.matches("F012X012F"), Err(RelateError::InvalidMask));

    let huge = box_at(0.0, 0.0, 200_000_000.0, 200_000_000.0);
    assert_eq!(
        relate(&huge, &square(), "*********"),
        Err(RelateError::Overlay(OverlayError::Unsupported))
    );
}

/// `test/algorithms/overlaps/overlaps_box.cpp:21-35` and
/// `test/algorithms/relate/relate_pointlike_geometry.cpp:166-174` — boxes
/// participate in the same areal matrix dispatch as polygons.
#[test]
fn boxes_and_rings_use_public_areal_relate_dispatch() {
    let first = ModelBox::from_corners(P::new(1.0, 1.0), P::new(3.0, 3.0));
    let second = ModelBox::from_corners(P::new(0.0, 0.0), P::new(2.0, 2.0));
    assert!(
        relation(&first, &second)
            .unwrap()
            .matches("212101212")
            .unwrap()
    );
    assert!(overlaps(&first, &second).unwrap());

    let ring = square().exterior().clone();
    assert!(
        relation(&P::new(2.0, 2.0), &ring)
            .unwrap()
            .matches("0FFFFF212")
            .unwrap()
    );
    assert!(touches(&P::new(0.0, 2.0), &ring).unwrap());
}

/// `test/algorithms/relate/relate_pointlike_geometry.cpp:31-45,87-93` —
/// multi-point membership and the mod-2 boundary of a multi-linestring.
#[test]
fn pointlike_and_linear_multis_preserve_union_boundary_rules() {
    let first = MultiPoint::from_vec(vec![P::new(0.0, 0.0), P::new(1.0, 1.0)]);
    let second = MultiPoint::from_vec(vec![P::new(0.0, 0.0), P::new(1.0, 0.0)]);
    assert!(
        relation(&first, &second)
            .unwrap()
            .matches("0F0FFF0F2")
            .unwrap()
    );

    let lines = MultiLinestring::from_vec(vec![
        Linestring::from_vec(vec![P::new(0.0, 0.0), P::new(2.0, 0.0), P::new(2.0, 2.0)]),
        Linestring::from_vec(vec![P::new(0.0, 0.0), P::new(0.0, 2.0)]),
    ]);
    assert!(
        relation(&P::new(0.0, 0.0), &lines)
            .unwrap()
            .matches("0FFFFF102")
            .unwrap()
    );
}

/// `test/algorithms/relate/relate_pointlike_geometry.cpp:181-219` — polygon
/// members are related as one multi-polygon point set, including a shared
/// boundary vertex.
#[test]
fn multipolygon_relate_uses_the_public_union_topology() {
    let polygons =
        MultiPolygon::from_vec(vec![box_at(0.0, 0.0, 5.0, 5.0), box_at(5.0, 5.0, 9.0, 9.0)]);
    assert!(
        relation(&P::new(5.0, 5.0), &polygons)
            .unwrap()
            .matches("F0FFFF212")
            .unwrap()
    );
    assert!(
        relation(&P::new(6.0, 6.0), &polygons)
            .unwrap()
            .matches("0FFFFF212")
            .unwrap()
    );
}

/// `test/algorithms/relate/relate_gc.cpp:55-65` — heterogeneous collections
/// use the OGC union topology rather than treating members as independent
/// matrices.
#[test]
fn geometry_collections_relate_through_runtime_public_dispatch() {
    let joined_lines = DynGeometryCollection(vec![
        DynGeometry::LineString(Linestring::from_vec(vec![
            P::new(0.0, 0.0),
            P::new(1.0, 1.0),
        ])),
        DynGeometry::LineString(Linestring::from_vec(vec![
            P::new(1.0, 1.0),
            P::new(2.0, 2.0),
        ])),
    ]);
    let point = DynGeometryCollection(vec![DynGeometry::Point(P::new(1.0, 1.0))]);
    assert!(
        relation(&point, &joined_lines)
            .unwrap()
            .matches("0FFFFF102")
            .unwrap()
    );

    let closed_line =
        DynGeometryCollection(vec![DynGeometry::LineString(Linestring::from_vec(vec![
            P::new(0.0, 0.0),
            P::new(2.0, 0.0),
            P::new(0.0, 0.0),
        ]))]);
    let closed_endpoint = DynGeometryCollection(vec![DynGeometry::Point(P::new(0.0, 0.0))]);
    assert!(
        relation(&closed_endpoint, &closed_line)
            .unwrap()
            .matches("0FFFFF1F2")
            .unwrap()
    );

    let first = DynGeometryCollection(vec![
        DynGeometry::Polygon(box_at(0.0, 0.0, 5.0, 5.0)),
        DynGeometry::LineString(Linestring::from_vec(vec![
            P::new(1.0, 1.0),
            P::new(6.0, 6.0),
        ])),
    ]);
    let second = DynGeometryCollection(vec![
        DynGeometry::Polygon(box_at(0.0, 0.0, 5.0, 5.0)),
        DynGeometry::LineString(Linestring::from_vec(vec![
            P::new(5.0, 5.0),
            P::new(6.0, 6.0),
        ])),
    ]);
    let matrix = relation(&first, &second).unwrap();
    assert!(matrix.matches("2FFF1FFF2").unwrap());
}

/// `test/algorithms/relate/relate_linear_areal.cpp:44-64` and
/// `relate_gc.cpp:107-111` — segment, runtime-variant, and static-to-collection
/// ordered pairs all use the same public matrix contract.
#[test]
fn segment_dynamic_and_collection_reverse_pairs_are_public() {
    let segment = Segment::new(P::new(-1.0, 2.0), P::new(5.0, 2.0));
    let bounds = ModelBox::from_corners(P::new(0.0, 0.0), P::new(4.0, 4.0));
    let segment_box = relation(&segment, &bounds).unwrap();
    assert!(segment_box.matches("101FF0212").unwrap());
    assert_eq!(
        relation(&bounds, &segment).unwrap(),
        segment_box.transposed()
    );
    assert!(crosses(&segment, &bounds).unwrap());

    let dynamic_point = DynGeometry::Point(P::new(2.0, 2.0));
    let dynamic_polygon = DynGeometry::Polygon(square());
    let dynamic_matrix = relation(&dynamic_point, &dynamic_polygon).unwrap();
    assert!(dynamic_matrix.matches("0FFFFF212").unwrap());
    assert_eq!(
        relation(&dynamic_polygon, &dynamic_point).unwrap(),
        dynamic_matrix.transposed()
    );

    let adjacent = DynGeometryCollection(vec![
        DynGeometry::Polygon(box_at(10.0, 0.0, 20.0, 10.0)),
        DynGeometry::Point(P::new(15.0, 5.0)),
    ]);
    assert!(
        relation(&box_at(0.0, 0.0, 10.0, 10.0), &adjacent)
            .unwrap()
            .matches("FF2F11212")
            .unwrap()
    );
}

/// `test/algorithms/relate/relate_linear_linear.cpp:104-139` and
/// `relate_gc.cpp:55-111` — generic topology dispatch promotes degenerate
/// linear/areal inputs to their actual dimension and recursively expands every
/// runtime multi/collection variant.
#[test]
fn generic_topology_dispatch_covers_degenerate_and_dynamic_variants() {
    let segment = Segment::new(P::new(0.0, 0.0), P::new(4.0, 0.0));
    let line = Linestring::from_vec(vec![P::new(2.0, -1.0), P::new(2.0, 1.0)]);
    assert_eq!(
        relation(&line, &segment).unwrap().interior_interior(),
        Dimension::Point
    );

    let degenerate_line = Linestring::from_vec(vec![P::new(1.0, 0.0), P::new(1.0, 0.0)]);
    assert_eq!(
        relation(&degenerate_line, &segment)
            .unwrap()
            .interior_interior(),
        Dimension::Point
    );
    assert_eq!(
        relation(&segment, &degenerate_line).unwrap(),
        relation(&degenerate_line, &segment).unwrap().transposed()
    );

    let degenerate_polygon: Polygon<P> =
        Polygon::new(Ring::from_vec(vec![P::new(1.0, 0.0), P::new(3.0, 0.0)]));
    assert_eq!(
        relation(&degenerate_polygon, &segment)
            .unwrap()
            .interior_interior(),
        Dimension::Curve
    );

    let dynamic_multi_point = DynGeometry::MultiPoint(MultiPoint::from_vec(vec![
        P::new(0.0, 0.0),
        P::new(2.0, 2.0),
    ]));
    assert_eq!(
        relation(&dynamic_multi_point, &P::new(2.0, 2.0))
            .unwrap()
            .interior_interior(),
        Dimension::Point
    );

    let dynamic_multi_line = DynGeometry::MultiLineString(MultiLinestring::from_vec(vec![
        Linestring::from_vec(vec![P::new(0.0, 0.0), P::new(2.0, 2.0)]),
    ]));
    assert_eq!(
        relation(&dynamic_multi_line, &P::new(1.0, 1.0))
            .unwrap()
            .interior_interior(),
        Dimension::Point
    );

    let dynamic_multi_polygon = DynGeometry::MultiPolygon(MultiPolygon::from_vec(vec![square()]));
    assert_eq!(
        relation(&dynamic_multi_polygon, &P::new(2.0, 2.0))
            .unwrap()
            .interior_interior(),
        Dimension::Point
    );

    let dynamic_empty_line = DynGeometry::<f64, Cartesian>::LineString(Linestring::new());
    assert_eq!(
        relation(&dynamic_empty_line, &P::new(2.0, 2.0))
            .unwrap()
            .exterior_interior(),
        Dimension::Point
    );

    let redundant_polygon: Polygon<P> = Polygon::new(Ring::from_vec(vec![
        P::new(0.0, 0.0),
        P::new(0.0, 4.0),
        P::new(0.0, 4.0),
        P::new(4.0, 4.0),
        P::new(4.0, 0.0),
        P::new(0.0, 0.0),
    ]));
    assert_eq!(
        relation(&redundant_polygon, &P::new(2.0, 2.0))
            .unwrap()
            .interior_interior(),
        Dimension::Point
    );

    let nested = DynGeometry::GeometryCollection(vec![DynGeometry::GeometryCollection(vec![
        dynamic_multi_point,
        dynamic_multi_line,
        dynamic_multi_polygon,
    ])]);
    assert!(
        relation(&nested, &P::new(1.0, 1.0))
            .unwrap()
            .interior_interior()
            .is_set()
    );
}

/// Generic topology range checks cover point/line storage and polygon holes.
/// The reversed linear–areal order exercises the second public `crosses`
/// alternative rather than relying only on matrix transposition.
#[test]
fn generic_topology_rejects_out_of_range_members_and_crosses_both_orders() {
    let segment = Segment::new(P::new(-1.0, 2.0), P::new(5.0, 2.0));
    assert!(matches!(
        relation(&P::new(f64::MAX, 0.0), &segment),
        Err(OverlayError::Unsupported)
    ));

    let polygon_with_bad_hole = Polygon::with_inners(
        square().outer,
        vec![Ring::from_vec(vec![
            P::new(1.0, 1.0),
            P::new(f64::MAX, 1.0),
            P::new(2.0, 2.0),
            P::new(1.0, 1.0),
        ])],
    );
    assert!(matches!(
        relation(&polygon_with_bad_hole, &segment),
        Err(OverlayError::Unsupported)
    ));

    let donut = Polygon::with_inners(
        square().outer,
        vec![Ring::from_vec(vec![
            P::new(1.0, 1.0),
            P::new(3.0, 1.0),
            P::new(3.0, 3.0),
            P::new(1.0, 3.0),
            P::new(1.0, 1.0),
        ])],
    );
    assert!(relation(&donut, &segment).is_ok());

    let bounds = ModelBox::from_corners(P::new(0.0, 0.0), P::new(4.0, 4.0));
    assert!(crosses(&segment, &bounds).unwrap());
    assert!(crosses(&bounds, &segment).unwrap());
}