s2rst 0.3.2

A Rust port of Google's S2 spherical geometry library — points, regions, shapes, and a hierarchical cell index on the sphere.
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
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2026 Torgeir Børresen <tb@starkad.no>
// Rust port of Google's S2 Geometry library — a derivative work, modified from
// the upstream Apache-2.0 source(s) below (Copyright Google Inc.). See LICENSE.
//   - C++:  google/s2geometry
//   - Java: google/s2-geometry-library-java

//! Geometry validation for S2 shape indexes.
//!
//! Provides two validation queries with different strictness levels:
//!
//! - [`S2ValidQuery`] — least strict, compatible with `S2BooleanOperation`
//! - [`S2LegacyValidQuery`] — stricter, matching `S2Polygon::IsValid()` semantics
//!
//! Corresponds to C++ `s2validation_query.h`.

#![expect(
    clippy::cast_sign_loss,
    reason = "EdgeId/ShapeId (i32) used as Vec indices in validation"
)]
#![expect(
    clippy::cast_possible_truncation,
    reason = "ShapeId/EdgeId (usize<->i32) for shape index iteration"
)]
#![expect(
    clippy::cast_possible_wrap,
    reason = "usize -> i32 for ShapeId/EdgeId — always in range"
)]
use std::collections::{HashMap, HashSet};

use crate::s2::Point;
use crate::s2::builder::{S2Error, S2ErrorCode};
use crate::s2::contains_point_query::{ContainsPointQuery, VertexModel};
use crate::s2::contains_vertex_query::ContainsVertexQuery;
use crate::s2::edge_crosser::EdgeCrosser;
use crate::s2::edge_crossings::Crossing;
use crate::s2::shape::{Dimension, Edge, Shape, ShapeId};
use crate::s2::shape_index::{ClippedShape, ShapeIndex, ShapeIndexIterator};
use crate::s2::shape_util::{self, sort_edges_ccw};

// ─── Options ──────────────────────────────────────────────────────────────

/// Types of single-vertex touches allowed between shapes.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TouchType {
    /// Allow no touches between shapes.
    #[default]
    None = 0b00,
    /// Interior point may touch the other shape.
    Interior = 0b01,
    /// Boundary point may touch the other shape.
    Boundary = 0b10,
    /// Allow any touches between shapes.
    Any = 0b11,
}

/// Configuration for validation queries.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[expect(clippy::struct_excessive_bools, reason = "matches C++ structure")]
pub struct ValidationOptions {
    /// Whether degenerate edges {A,A} are allowed.
    pub allow_degenerate_edges: bool,
    /// Whether duplicate polyline edges are allowed.
    pub allow_duplicate_polyline_edges: bool,
    /// Whether reverse-duplicate edges {A,B},{B,A} are allowed.
    pub allow_reverse_duplicates: bool,
    /// Whether polyline edges can cross each other.
    pub allow_polyline_interior_crossings: bool,
    /// Whether to enforce legacy validation semantics.
    pub legacy_mode: bool,
    /// Touch matrix: `allowed_touches`[min(dima,dimb)][max(dima,dimb)]
    /// Each entry is (`TypeA`, `TypeB`).
    allowed_touches: [[(TouchType, TouchType); 3]; 3],
}

impl Default for ValidationOptions {
    fn default() -> Self {
        let any = (TouchType::Any, TouchType::Any);
        ValidationOptions {
            allow_degenerate_edges: true,
            allow_duplicate_polyline_edges: true,
            allow_reverse_duplicates: true,
            allow_polyline_interior_crossings: true,
            legacy_mode: false,
            allowed_touches: [[any; 3]; 3],
        }
    }
}

impl ValidationOptions {
    fn allowed_touches(&self, dima: Dimension, dimb: Dimension) -> (TouchType, TouchType) {
        let (a, b) = if dima > dimb {
            (dimb, dima)
        } else {
            (dima, dimb)
        };
        self.allowed_touches[a.as_usize()][b.as_usize()]
    }
}

// ─── Helper: point validity ──────────────────────────────────────────────

fn valid_point(p: Point) -> bool {
    p.0.x.is_finite() && p.0.y.is_finite() && p.0.z.is_finite()
}

fn is_unit_length(p: Point) -> bool {
    let n2 = p.0.norm2();
    // C++ S2::IsUnitLength uses 1e-15 tolerance.
    (n2 - 1.0).abs() <= 1e-15
}

// ─── Incident edge tracking ──────────────────────────────────────────────

/// Key for the incident edge map: (`shape_id`, vertex bit pattern).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
struct VertexKey {
    shape_id: ShapeId,
    x_bits: u64,
    y_bits: u64,
    z_bits: u64,
}

impl VertexKey {
    fn new(shape_id: ShapeId, vertex: Point) -> Self {
        VertexKey {
            shape_id,
            x_bits: vertex.0.x.to_bits(),
            y_bits: vertex.0.y.to_bits(),
            z_bits: vertex.0.z.to_bits(),
        }
    }

    fn vertex(&self) -> Point {
        Point(crate::r3::Vector::new(
            f64::from_bits(self.x_bits),
            f64::from_bits(self.y_bits),
            f64::from_bits(self.z_bits),
        ))
    }
}

/// Tracks which edge IDs are incident on each (`shape_id`, vertex) pair.
struct IncidentEdgeTracker {
    edges: HashMap<VertexKey, HashSet<i32>>,
}

impl IncidentEdgeTracker {
    fn new() -> Self {
        IncidentEdgeTracker {
            edges: HashMap::new(),
        }
    }

    fn add_edge(&mut self, shape_id: ShapeId, edge_id: i32, edge: Edge) {
        self.edges
            .entry(VertexKey::new(shape_id, edge.v0))
            .or_default()
            .insert(edge_id);
        self.edges
            .entry(VertexKey::new(shape_id, edge.v1))
            .or_default()
            .insert(edge_id);
    }
}

// ─── Edge info for per-cell processing ───────────────────────────────────

/// An edge with its metadata from a cell.
#[derive(Clone, Debug)]
struct CellEdge {
    v0: Point,
    v1: Point,
    edge_id: i32,
    shape_id: ShapeId,
    chain_id: usize,
    offset: usize,
    dim: Dimension,
}

/// Collects edges from a cell, sorted by dimension.
fn collect_cell_edges(
    index: &ShapeIndex,
    cell: &crate::s2::shape_index::ShapeIndexCell,
) -> Vec<CellEdge> {
    let mut edges = Vec::new();
    for clipped in &cell.shapes {
        let Some(shape) = index.shape(clipped.shape_id) else {
            continue;
        };
        let dim = shape.dimension();
        for &edge_id in &clipped.edges {
            let edge = shape.edge(edge_id as usize);
            let pos = shape.chain_position(edge_id as usize);
            edges.push(CellEdge {
                v0: edge.v0,
                v1: edge.v1,
                edge_id,
                shape_id: clipped.shape_id,
                chain_id: pos.chain_id,
                offset: pos.offset,
                dim,
            });
        }
    }
    // Sort by dimension so we can partition easily.
    edges.sort_by_key(|e| e.dim);
    edges
}

// ─── S2ValidQuery ─────────────────────────────────────────────────────────

/// Least-strict validation query, compatible with `S2BooleanOperation`.
///
/// Checks:
/// - Points are unit magnitude and finite
/// - No antipodal edges
/// - Polygon chains are closed, connected, and interior-on-left
/// - Polygon interiors are disjoint from all other geometry
/// - No duplicate polygon edges
#[derive(Debug)]
pub struct S2ValidQuery {
    options: ValidationOptions,
}

impl Default for S2ValidQuery {
    fn default() -> Self {
        Self::new()
    }
}

impl S2ValidQuery {
    /// Creates a new validation query with default options.
    pub fn new() -> Self {
        S2ValidQuery {
            options: ValidationOptions::default(),
        }
    }

    /// Returns a reference to the options.
    pub fn options(&self) -> &ValidationOptions {
        &self.options
    }

    /// Returns a mutable reference to the options.
    pub fn options_mut(&mut self) -> &mut ValidationOptions {
        &mut self.options
    }

    /// Validates an index. Returns `Ok(())` if valid, `Err(S2Error)` with
    /// details on failure.
    ///
    /// # Errors
    ///
    /// Returns an `S2Error` describing the first validation failure found
    /// (e.g., non-unit-length vertices, self-intersections, duplicate edges).
    pub fn validate(&self, index: &ShapeIndex) -> Result<(), S2Error> {
        let mut error = S2Error::ok();
        if self.validate_inner(index, &mut error) {
            Ok(())
        } else {
            Err(error)
        }
    }

    fn validate_inner(&self, index: &ShapeIndex, error: &mut S2Error) -> bool {
        let mut tracker = IncidentEdgeTracker::new();

        // Phase 1: Per-shape checks.
        let mut iter = index.iter();
        for shape_id in (0..index.num_shape_ids() as i32).map(ShapeId) {
            let Some(shape) = index.shape(shape_id) else {
                continue;
            };
            if !self.check_shape(&mut iter, shape, shape_id, error) {
                return false;
            }
        }

        // Phase 2: Per-cell checks.
        iter.begin();
        while !iter.done() {
            let Some(cell) = iter.index_cell() else {
                iter.next();
                continue;
            };

            let cell_edges = collect_cell_edges(index, cell);

            // Track 2D edges for vertex crossing checks.
            for e in &cell_edges {
                if e.dim == Dimension::Polygon {
                    tracker.add_edge(e.shape_id, e.edge_id, Edge::new(e.v0, e.v1));
                }
            }

            // Check for duplicate edges.
            if !self.check_for_duplicate_edges(index, &cell_edges, error) {
                return false;
            }

            // Check for interior crossings.
            if !self.check_for_interior_crossings(&cell_edges, error) {
                return false;
            }

            // Check touches.
            if !self.check_touches_are_valid(index, cell, &cell_edges, error) {
                return false;
            }

            // Legacy: check duplicate vertices within chains.
            if self.options.legacy_mode
                && !Self::check_duplicate_vertices_in_chain(&cell_edges, error)
            {
                return false;
            }

            // Check points for containment in polygons.
            let cell_center = iter.cell_id().to_point();
            for e in &cell_edges {
                if e.dim == Dimension::Point
                    && self.point_contained(index, cell, cell_center, e.shape_id, e.v0, error)
                {
                    return false;
                }
            }

            iter.next();
        }

        // Phase 3: Global checks.
        // Vertex crossing check for 2D shapes.
        for (key, edge_ids) in &tracker.edges {
            let Some(shape) = index.shape(key.shape_id) else {
                continue;
            };
            if shape.dimension() == Dimension::Polygon
                && !check_vertex_crossings(key.vertex(), shape, key.shape_id, edge_ids, error)
            {
                return false;
            }
        }

        // Containment check: no chain's first vertex should be inside another polygon.
        let mut query = ContainsPointQuery::new(index, VertexModel::Open);
        for shape_id in (0..index.num_shape_ids() as i32).map(ShapeId) {
            let Some(shape) = index.shape(shape_id) else {
                continue;
            };
            if shape.dimension() == Dimension::Point {
                continue;
            }
            for chain_id in 0..shape.num_chains() {
                let chain = shape.chain(chain_id);
                if chain.length < 1 {
                    continue;
                }
                let vertex = shape.chain_edge(chain_id, 0).v0;
                if query.contains(vertex) {
                    *error = S2Error::new(
                        S2ErrorCode::OverlappingGeometry,
                        format!("Shape {shape_id} has edges contained in another shape."),
                    );
                    return false;
                }
            }
        }

        true
    }

    /// Per-shape validation checks.
    fn check_shape(
        &self,
        iter: &mut ShapeIndexIterator<'_>,
        shape: &dyn Shape,
        shape_id: ShapeId,
        error: &mut S2Error,
    ) -> bool {
        let dim = shape.dimension();
        // With `Dimension` as an enum, invalid dimension values (> 2) are
        // impossible — the check is now enforced at compile time.

        let mut chains_to_check = Vec::new();

        for chain_id in 0..shape.num_chains() {
            let chain = shape.chain(chain_id);

            // Polygon chains must be closed.
            if dim == Dimension::Polygon && chain.length > 0 {
                let first_edge = shape.chain_edge(chain_id, 0);
                let last_edge = shape.chain_edge(chain_id, chain.length - 1);
                if last_edge.v1 != first_edge.v0 {
                    *error = S2Error::new(
                        S2ErrorCode::LoopNotEnoughVertices,
                        format!("Chain {chain_id} of shape {shape_id} isn't closed"),
                    );
                    return false;
                }
            }

            for offset in 0..chain.length {
                let edge = shape.chain_edge(chain_id, offset);

                // Check for inf/nan coordinates.
                if !valid_point(edge.v0) || !valid_point(edge.v1) {
                    *error = S2Error::new(
                        S2ErrorCode::InvalidVertex,
                        format!("Shape {shape_id} has invalid coordinates"),
                    );
                    return false;
                }

                // Check unit length.
                if !is_unit_length(edge.v0) || !is_unit_length(edge.v1) {
                    *error = S2Error::new(
                        S2ErrorCode::NotUnitLength,
                        format!("Shape {shape_id} has non-unit length vertices"),
                    );
                    return false;
                }

                // Check for degenerate edges.
                if dim > Dimension::Point
                    && !self.options.allow_degenerate_edges
                    && edge.v0 == edge.v1
                {
                    *error = S2Error::new(
                        S2ErrorCode::DuplicateVertices,
                        format!(
                            "Shape {}: chain {}, edge {} is degenerate",
                            shape_id,
                            chain_id,
                            chain.start + offset,
                        ),
                    );
                    return false;
                }

                // Check for antipodal vertices.
                if edge.v0 == Point(-edge.v1.0) {
                    *error = S2Error::new(
                        S2ErrorCode::AntipodalVertices,
                        format!("Shape {shape_id} has adjacent antipodal vertices"),
                    );
                    return false;
                }

                // Check chain connectivity.
                if dim > Dimension::Point && chain.length >= 2 && offset > 0 {
                    let prev = shape.chain_edge(chain_id, offset - 1);
                    if prev.v1 != edge.v0 {
                        *error = S2Error::new(
                            S2ErrorCode::NotContinuous,
                            format!(
                                "Chain {chain_id} of shape {shape_id} has neighboring edges that don't connect.",
                            ),
                        );
                        return false;
                    }
                }
            }

            // Polygon chain orientation check: need at least 2 distinct points.
            if dim != Dimension::Polygon || chain.length == 0 {
                continue;
            }

            let first_vertex = shape.chain_edge(chain_id, 0).v0;
            let mut has_distinct = false;
            for offset in 0..chain.length {
                let v = shape.chain_edge(chain_id, offset).v0;
                if v != first_vertex {
                    has_distinct = true;
                    break;
                }
            }
            if !has_distinct {
                continue;
            }
            chains_to_check.push(chain_id);
        }

        // Check chain orientation for selected chains.
        for chain_id in chains_to_check {
            if !self.check_chain_orientation(iter, shape, shape_id, chain_id, error) {
                return false;
            }
        }

        true
    }

    /// Checks that a polygon chain is oriented with interior on the left.
    #[expect(clippy::unused_self, reason = "matches C++ method signature")]
    fn check_chain_orientation(
        &self,
        iter: &mut ShapeIndexIterator<'_>,
        shape: &dyn Shape,
        shape_id: ShapeId,
        chain_id: usize,
        error: &mut S2Error,
    ) -> bool {
        let chain = shape.chain(chain_id);
        let mut query = ContainsVertexQuery::new(Point::default());

        for offset in 0..chain.length {
            let vertex = shape.chain_edge(chain_id, offset).v0;
            query.init(vertex);

            // Seek to the cell containing this vertex.
            if !iter.locate_point(vertex) {
                *error = S2Error::new(S2ErrorCode::DataLoss, "Shape vertex was not indexed");
                return false;
            }

            let center = iter.cell_id().to_point();
            let Some(cell) = iter.index_cell() else {
                *error = S2Error::new(S2ErrorCode::DataLoss, "Shape vertex was not indexed");
                return false;
            };

            let Some(clipped) = cell.find_by_shape_id(shape_id) else {
                *error = S2Error::new(S2ErrorCode::DataLoss, "Shape vertex was not indexed");
                return false;
            };

            // Compute winding number and vertex sign together.
            let mut winding = i32::from(clipped.contains_center);
            let mut crosser = EdgeCrosser::new(center, vertex);

            for &edge_id in &clipped.edges {
                let edge = shape.edge(edge_id as usize);
                winding += crosser.signed_edge_or_vertex_crossing(edge.v0, edge.v1);

                // Include edges incident on vertex.
                if edge.v0 == vertex {
                    query.add_edge(edge.v1, 1);
                } else if edge.v1 == vertex {
                    query.add_edge(edge.v0, -1);
                }
            }

            let duplicates = query.duplicate_edges();
            if !duplicates {
                let sign = query.contains_vertex();
                if sign == 0 {
                    continue;
                }
                let expected_winding = if sign < 0 { 0 } else { 1 };
                if winding != expected_winding {
                    *error = S2Error::new(
                        S2ErrorCode::PolygonInconsistentLoopOrientations,
                        format!(
                            "Shape {shape_id} has one or more edges with interior on the right."
                        ),
                    );
                    return false;
                }
                return true;
            }

            // Duplicate edges at this vertex; try the next one.
        }

        true
    }

    /// Checks for duplicate edges in a cell.
    fn check_for_duplicate_edges(
        &self,
        _index: &ShapeIndex,
        cell_edges: &[CellEdge],
        error: &mut S2Error,
    ) -> bool {
        let dim0 = if self.options.allow_duplicate_polyline_edges {
            Dimension::Polygon
        } else {
            Dimension::Polyline
        };

        // Get edges in the dim range [dim0, 2].
        let edges: Vec<&CellEdge> = cell_edges.iter().filter(|e| e.dim >= dim0).collect();

        for i in 0..edges.len() {
            for j in (i + 1)..edges.len() {
                let mut duplicate = edges[i].v0 == edges[j].v0 && edges[i].v1 == edges[j].v1;
                if !self.options.allow_reverse_duplicates {
                    duplicate |= edges[i].v0 == edges[j].v1 && edges[i].v1 == edges[j].v0;
                }
                if duplicate {
                    *error = S2Error::new(
                        S2ErrorCode::OverlappingGeometry,
                        "One or more duplicate polygon edges detected",
                    );
                    return false;
                }
            }
        }
        true
    }

    /// Checks for interior crossings between edges in a cell.
    fn check_for_interior_crossings(&self, cell_edges: &[CellEdge], error: &mut S2Error) -> bool {
        // Get polyline and polygon edges (dim >= 1).
        let edges: Vec<&CellEdge> = cell_edges
            .iter()
            .filter(|e| e.dim >= Dimension::Polyline)
            .collect();

        if edges.is_empty() {
            return true;
        }

        // Find where polygon edges start.
        let polyline_count = edges
            .iter()
            .filter(|e| e.dim == Dimension::Polyline)
            .count();
        let check_start = if self.options.allow_polyline_interior_crossings {
            polyline_count
        } else {
            0
        };

        if check_start >= edges.len() {
            return true;
        }

        for i in 0..edges.len().saturating_sub(1) {
            let mut j = if i + 1 > check_start {
                i + 1
            } else {
                check_start
            };

            // Skip adjacent edges.
            if j < edges.len() && edges[i].v1 == edges[j].v0 {
                j += 1;
            }

            if j >= edges.len() {
                continue;
            }

            let mut crosser = EdgeCrosser::new(edges[i].v0, edges[i].v1);
            for k in j..edges.len() {
                if crosser.crossing_sign(edges[k].v0, edges[k].v1) == Crossing::Cross {
                    *error = S2Error::new(
                        S2ErrorCode::OverlappingGeometry,
                        format!(
                            "Chain {} edge {} crosses chain {} edge {}",
                            edges[i].chain_id, edges[i].offset, edges[k].chain_id, edges[k].offset,
                        ),
                    );
                    return false;
                }
            }
        }
        true
    }

    /// Checks that vertex touches are valid under configured semantics.
    fn check_touches_are_valid(
        &self,
        index: &ShapeIndex,
        _cell: &crate::s2::shape_index::ShapeIndexCell,
        cell_edges: &[CellEdge],
        error: &mut S2Error,
    ) -> bool {
        // Check if all touches are allowed — if so, skip.
        let any = (TouchType::Any, TouchType::Any);
        let dims = [Dimension::Point, Dimension::Polyline, Dimension::Polygon];
        // need_check[d] is false when all dimension pairs involving d allow Any touches.
        let mut need_check = [true; 3];
        for (idx, &di) in dims.iter().enumerate() {
            let mut all_any = true;
            for &dj in &dims {
                if self.options.allowed_touches(di, dj) != any {
                    all_any = false;
                    break;
                }
            }
            need_check[idx] = !all_any;
        }

        if !need_check[0] && !need_check[1] && !need_check[2] {
            return true;
        }

        // Gather test vertices.
        struct TestVertex {
            vertex: Point,
            edge_id: i32,
            shape_id: ShapeId,
            dim: Dimension,
            on_boundary: bool,
        }

        let mut test_vertices = Vec::new();
        for e in cell_edges {
            if !need_check[e.dim.as_usize()] {
                continue;
            }
            let Some(shape) = index.shape(e.shape_id) else {
                continue;
            };
            if e.dim == Dimension::Polyline {
                let on_boundary = polyline_vertex_is_boundary(shape, e.edge_id as usize, 0);
                test_vertices.push(TestVertex {
                    vertex: e.v0,
                    edge_id: e.edge_id,
                    shape_id: e.shape_id,
                    dim: e.dim,
                    on_boundary,
                });
                let on_boundary = polyline_vertex_is_boundary(shape, e.edge_id as usize, 1);
                if on_boundary {
                    test_vertices.push(TestVertex {
                        vertex: e.v1,
                        edge_id: e.edge_id,
                        shape_id: e.shape_id,
                        dim: e.dim,
                        on_boundary: true,
                    });
                }
            } else {
                test_vertices.push(TestVertex {
                    vertex: e.v0,
                    edge_id: e.edge_id,
                    shape_id: e.shape_id,
                    dim: e.dim,
                    on_boundary: e.dim == Dimension::Polygon,
                });
            }
        }

        // Check each test vertex against all edges.
        for tv in &test_vertices {
            for e in cell_edges {
                // Don't compare an edge against itself.
                if tv.shape_id == e.shape_id && tv.edge_id == e.edge_id {
                    continue;
                }

                let vertidx = if tv.vertex == e.v0 {
                    0
                } else if tv.vertex == e.v1 {
                    1
                } else {
                    continue;
                };

                // Skip closed polyline self-touches.
                if tv.shape_id == e.shape_id
                    && e.dim == Dimension::Polyline
                    && let Some(shape) = index.shape(e.shape_id)
                {
                    if vertidx == 0
                        && let Some(prev) = shape_util::prev_edge_wrap(shape, e.edge_id as usize)
                        && prev == tv.edge_id as usize
                    {
                        continue;
                    }
                    if vertidx == 1
                        && let Some(next) = shape_util::next_edge_wrap(shape, e.edge_id as usize)
                        && next == tv.edge_id as usize
                    {
                        continue;
                    }
                }

                let on_boundary = if e.dim == Dimension::Polygon {
                    true
                } else if e.dim == Dimension::Polyline {
                    if let Some(shape) = index.shape(e.shape_id) {
                        polyline_vertex_is_boundary(shape, e.edge_id as usize, vertidx)
                    } else {
                        false
                    }
                } else {
                    false
                };

                let typea = if tv.on_boundary {
                    TouchType::Boundary
                } else {
                    TouchType::Interior
                };
                let typeb = if on_boundary {
                    TouchType::Boundary
                } else {
                    TouchType::Interior
                };

                let allowed = self.options.allowed_touches(tv.dim, e.dim);
                let permitted_ab =
                    (allowed.0 as u8 & typea as u8 != 0) && (allowed.1 as u8 & typeb as u8 != 0);
                let permitted_ba =
                    (allowed.0 as u8 & typeb as u8 != 0) && (allowed.1 as u8 & typea as u8 != 0);

                if !permitted_ab && !permitted_ba {
                    *error = S2Error::new(
                        S2ErrorCode::OverlappingGeometry,
                        "Index has geometry with invalid vertex touches.",
                    );
                    return false;
                }
            }
        }
        true
    }

    /// Checks if a point is contained in any polygon in the cell.
    #[expect(clippy::unused_self, reason = "matches C++ method signature")]
    fn point_contained(
        &self,
        index: &ShapeIndex,
        cell: &crate::s2::shape_index::ShapeIndexCell,
        cell_center: Point,
        point_shape_id: ShapeId,
        point: Point,
        error: &mut S2Error,
    ) -> bool {
        for clipped in &cell.shapes {
            if clipped.shape_id == point_shape_id {
                continue;
            }
            let Some(shape) = index.shape(clipped.shape_id) else {
                continue;
            };
            if shape.dimension() != Dimension::Polygon {
                continue;
            }
            if shape_contains_in_cell(clipped, shape, cell_center, point) {
                *error = S2Error::new(
                    S2ErrorCode::OverlappingGeometry,
                    format!(
                        "Shape {point_shape_id} has one or more edges contained in another shape.",
                    ),
                );
                return true;
            }
        }
        false
    }

    /// Legacy mode: checks for duplicate vertices within the same chain.
    fn check_duplicate_vertices_in_chain(cell_edges: &[CellEdge], error: &mut S2Error) -> bool {
        for i in 0..cell_edges.len() {
            for j in (i + 1)..cell_edges.len() {
                if cell_edges[j].chain_id != cell_edges[i].chain_id {
                    continue;
                }
                if cell_edges[j].shape_id != cell_edges[i].shape_id {
                    continue;
                }
                if cell_edges[j].v0 == cell_edges[i].v0 {
                    *error = S2Error::new(
                        S2ErrorCode::DuplicateVertices,
                        format!(
                            "Chain {} of shape {} has duplicate vertices",
                            cell_edges[i].chain_id, cell_edges[i].shape_id,
                        ),
                    );
                    return false;
                }
            }
        }
        true
    }
}

// ─── S2LegacyValidQuery ───────────────────────────────────────────────────

/// Stricter validation query matching `S2Polygon::IsValid()` semantics.
///
/// Additional constraints beyond `S2ValidQuery`:
/// - No degenerate edges
/// - All shapes must have the same dimension
/// - No duplicate vertices within a chain
/// - Polygon chains must have at least 3 edges
#[derive(Debug)]
pub struct S2LegacyValidQuery {
    inner: S2ValidQuery,
}

impl Default for S2LegacyValidQuery {
    fn default() -> Self {
        Self::new()
    }
}

impl S2LegacyValidQuery {
    /// Creates a new legacy validation query with stricter defaults.
    pub fn new() -> Self {
        let mut q = S2ValidQuery::new();
        q.options.allow_degenerate_edges = false;
        q.options.allow_reverse_duplicates = false;
        q.options.legacy_mode = true;
        S2LegacyValidQuery { inner: q }
    }

    /// Validates an index. Returns `Ok(())` if valid, `Err(S2Error)` on failure.
    ///
    /// # Errors
    ///
    /// Returns an `S2Error` describing the first validation failure found
    /// under legacy validation rules.
    pub fn validate(&self, index: &ShapeIndex) -> Result<(), S2Error> {
        let mut error = S2Error::ok();
        if self.validate_inner(index, &mut error) {
            Ok(())
        } else {
            Err(error)
        }
    }

    fn validate_inner(&self, index: &ShapeIndex, error: &mut S2Error) -> bool {
        // Check: all shapes must have the same dimension.
        let mut dim: Option<Dimension> = None;
        for shape_id in (0..index.num_shape_ids() as i32).map(ShapeId) {
            let Some(shape) = index.shape(shape_id) else {
                continue;
            };
            let d = shape.dimension();
            match dim {
                Some(prev) if prev != d => {
                    *error = S2Error::new(
                        S2ErrorCode::InvalidDimension,
                        "Mixed dimensional geometry is invalid for legacy semantics.",
                    );
                    return false;
                }
                _ => dim = Some(d),
            }
        }

        // Check: polygon chains must have >= 3 edges.
        for shape_id in (0..index.num_shape_ids() as i32).map(ShapeId) {
            let Some(shape) = index.shape(shape_id) else {
                continue;
            };
            if shape.dimension() == Dimension::Polygon {
                let mut has_empty = false;
                for chain_id in 0..shape.num_chains() {
                    let chain = shape.chain(chain_id);
                    if chain.length == 0 {
                        has_empty = true;
                    } else if chain.length < 3 {
                        *error = S2Error::new(
                            S2ErrorCode::LoopNotEnoughVertices,
                            format!(
                                "Shape {shape_id} has a non-empty chain with less than three edges.",
                            ),
                        );
                        return false;
                    }
                }
                if has_empty && shape.num_chains() > 1 {
                    *error = S2Error::new(
                        S2ErrorCode::PolygonEmptyLoop,
                        format!("Shape {shape_id} has too many empty chains"),
                    );
                    return false;
                }
            }
        }

        // Delegate to the inner S2ValidQuery.
        self.inner.validate_inner(index, error)
    }
}

// ─── Helper functions ─────────────────────────────────────────────────────

/// Returns true if a polyline vertex is a boundary point (start or end of open chain).
fn polyline_vertex_is_boundary(shape: &dyn Shape, edge_id: usize, vertex: usize) -> bool {
    debug_assert!(vertex == 0 || vertex == 1);
    let pos = shape.chain_position(edge_id);
    let chain = shape.chain(pos.chain_id);

    if pos.offset == 0 && vertex == 0 {
        return shape_util::prev_edge_wrap(shape, edge_id).is_none();
    }
    if pos.offset == chain.length - 1 && vertex == 1 {
        return shape_util::next_edge_wrap(shape, edge_id).is_none();
    }
    false
}

/// Checks if a shape contains a point within a cell, using edge crossing
/// from the cell center to the point.
fn shape_contains_in_cell(
    clipped: &ClippedShape,
    shape: &dyn Shape,
    cell_center: Point,
    point: Point,
) -> bool {
    let mut inside = clipped.contains_center;
    if clipped.edges.is_empty() {
        return inside;
    }

    let mut crosser = EdgeCrosser::new(cell_center, point);
    for &edge_id in &clipped.edges {
        let edge = shape.edge(edge_id as usize);
        if crosser.edge_or_vertex_crossing(edge.v0, edge.v1) {
            inside = !inside;
        }
    }
    inside
}

/// Checks that polygon chains don't cross at a vertex.
fn check_vertex_crossings(
    vertex: Point,
    shape: &dyn Shape,
    shape_id: ShapeId,
    edge_ids: &HashSet<i32>,
    error: &mut S2Error,
) -> bool {
    if edge_ids.len() < 2 {
        return true;
    }

    // Build edges with metadata and sort CCW.
    struct EdgeWithInfo {
        edge: Edge,
        edge_id: i32,
        chain_id: usize,
        prev_id: Option<usize>,
        sign: i32, // -1 for outgoing (v0 == vertex), +1 for incoming
    }

    let mut edges_info: Vec<EdgeWithInfo> = Vec::new();
    for &edge_id in edge_ids {
        let pos = shape.chain_position(edge_id as usize);
        let edge = shape.edge(edge_id as usize);
        let prev = shape_util::prev_edge_wrap(shape, edge_id as usize);
        let sign = if edge.v0 == vertex { -1 } else { 1 };
        edges_info.push(EdgeWithInfo {
            edge,
            edge_id,
            chain_id: pos.chain_id,
            prev_id: prev,
            sign,
        });
    }

    // Sort the edges CCW around the vertex.
    let mut raw_edges: Vec<Edge> = edges_info.iter().map(|e| e.edge).collect();
    if raw_edges.is_empty() {
        return true;
    }
    let first = raw_edges[0];
    sort_edges_ccw(vertex, first, &mut raw_edges);

    // Rebuild edges_info in sorted order.
    let mut sorted_info: Vec<&EdgeWithInfo> = Vec::with_capacity(edges_info.len());
    for sorted_edge in &raw_edges {
        // Find matching edge_info.
        if let Some(idx) = edges_info.iter().position(|e| e.edge == *sorted_edge) {
            sorted_info.push(&edges_info[idx]);
        }
    }

    // For each outgoing edge (sign == -1), scan CCW until we find its matching
    // incoming edge. All chain sums should be zero at that point.
    let n = sorted_info.len();
    let mut chain_sums: HashMap<usize, i32> = HashMap::new();

    for i in 0..n {
        let curr = sorted_info[i];
        if curr.sign > 0 {
            continue; // Skip incoming edges.
        }

        chain_sums.clear();
        let mut found = false;
        for j in 1..n {
            let edge = sorted_info[(i + j) % n];
            // Check if this is the matching incoming edge.
            // curr.prev_id is the edge before curr in the chain — that's the
            // incoming edge at this vertex.
            if curr.chain_id == edge.chain_id
                && let Some(curr_prev) = curr.prev_id
                && edge.edge_id as usize == curr_prev
            {
                // Found matching incoming edge. Check chain sums.
                for &sum in chain_sums.values() {
                    if sum != 0 {
                        *error = S2Error::new(
                            S2ErrorCode::OverlappingGeometry,
                            format!(
                                "Shape {shape_id} has one or more chains that cross at a vertex",
                            ),
                        );
                        return false;
                    }
                }
                found = true;
                break;
            }
            *chain_sums.entry(edge.chain_id).or_insert(0) += edge.sign;
        }

        if !found {
            *error = S2Error::new(
                S2ErrorCode::InvalidVertex,
                "Outgoing edge with no incoming edge",
            );
            return false;
        }
    }

    true
}

// ─── Module registration ──────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::s2::text_format;

    fn expect_valid(geometry: &str) {
        let index = text_format::make_index(geometry);
        let result = S2ValidQuery::new().validate(&index);
        assert!(result.is_ok(), "Expected valid but got: {:?}", result.err());
    }

    fn expect_invalid(geometry: &str, expected_code: S2ErrorCode) {
        let index = text_format::make_index(geometry);
        let err = S2ValidQuery::new().validate(&index).expect_err(&format!(
            "Expected invalid geometry to fail validation: {geometry}"
        ));
        assert_eq!(
            err.code, expected_code,
            "Expected {:?} but got {:?}: {}",
            expected_code, err.code, err.message
        );
    }

    fn expect_legacy_valid(geometry: &str) {
        let index = text_format::make_index(geometry);
        let result = S2LegacyValidQuery::new().validate(&index);
        assert!(result.is_ok(), "Expected valid but got: {:?}", result.err());
    }

    fn expect_legacy_invalid(geometry: &str, expected_code: S2ErrorCode) {
        let index = text_format::make_index(geometry);
        let err = S2LegacyValidQuery::new()
            .validate(&index)
            .expect_err(&format!(
                "Expected invalid geometry to fail validation: {geometry}"
            ));
        assert_eq!(
            err.code, expected_code,
            "Expected {:?} but got {:?}: {}",
            expected_code, err.code, err.message
        );
    }

    /// Checks a geometry is valid under both `S2ValidQuery` and `S2LegacyValidQuery`.
    fn expect_both_valid(geometry: &str) {
        expect_valid(geometry);
        expect_legacy_valid(geometry);
    }

    /// Checks a geometry is invalid under both queries with the same error code.
    fn expect_both_invalid(geometry: &str, expected_code: S2ErrorCode) {
        expect_invalid(geometry, expected_code);
        expect_legacy_invalid(geometry, expected_code);
    }

    // ─── Both queries: BasicGeometryOk ──────────────────────────────

    #[test]
    fn test_basic_geometry_ok() {
        // Basic polygon.
        expect_both_valid("## 1:0, 0:-1, -1:0, 0:1");
        // Polyline.
        expect_both_valid("# 0:0, 1:0, 0:-1, -1:0, 0:1 #");
        // Multi-point.
        expect_both_valid("0:0 | 1:0 | 0:-1 | -1:0 | 0:1 ##");
        // Polygon with properly oriented hole (CW: E→S→W→N).
        expect_both_valid("## 2:0, 0:-2, -2:0, 0:2; 0:1, -1:0, 0:-1, 1:0;");
        // Polygon with improperly oriented hole (CCW, same as shell) should fail.
        expect_both_invalid(
            "## 2:0, 0:-2, -2:0, 0:2; 1:0, 0:-1, -1:0, 0:1;",
            S2ErrorCode::PolygonInconsistentLoopOrientations,
        );
    }

    #[test]
    fn test_empty_geometry_ok() {
        expect_both_valid("##");
    }

    #[test]
    fn test_full_geometry_ok() {
        expect_both_valid("## full");
    }

    #[test]
    fn test_interior_on_right_regression() {
        // Regression: polygon that confused duplicate edge detection.
        expect_both_valid("## 0:4, 3:128, 4:2, 0:0");
    }

    #[test]
    fn test_tangent_polygons_ok() {
        // Two polygons touching at one vertex.
        expect_both_valid("## 1:0, 0:-1, -1:0, 0:1 | 0:1, -1:2, 0:3, 1:2");
    }

    #[test]
    fn test_antipodal_edge_fails() {
        let mut index = ShapeIndex::new();
        let v0 = Point::from_coords(1.0, 0.0, 0.0);
        let v1 = Point(-v0.0);
        index.add(Box::new(crate::s2::lax_polyline::LaxPolyline::new(vec![
            v0, v1,
        ])));
        index.build();

        let err = S2ValidQuery::new().validate(&index).unwrap_err();
        assert_eq!(err.code, S2ErrorCode::AntipodalVertices);
    }

    #[test]
    fn test_open_chain_fails() {
        let mut index = ShapeIndex::new();
        use crate::s2::LatLng;
        use crate::s2::shape::{Chain, ChainPosition, ReferencePoint};

        #[derive(Debug)]
        struct OpenShape {
            vertices: Vec<Point>,
        }
        impl Shape for OpenShape {
            fn num_edges(&self) -> usize {
                self.vertices.len() - 1
            }
            fn edge(&self, id: usize) -> Edge {
                Edge::new(self.vertices[id], self.vertices[id + 1])
            }
            fn reference_point(&self) -> ReferencePoint {
                ReferencePoint::default()
            }
            fn num_chains(&self) -> usize {
                1
            }
            fn chain(&self, _: usize) -> Chain {
                Chain::new(0, self.num_edges())
            }
            fn chain_edge(&self, _: usize, offset: usize) -> Edge {
                self.edge(offset)
            }
            fn chain_position(&self, edge_id: usize) -> ChainPosition {
                ChainPosition::new(0, edge_id)
            }
            fn dimension(&self) -> Dimension {
                Dimension::Polygon
            }
        }

        let p = |lat: f64, lng: f64| LatLng::from_degrees(lat, lng).to_point();
        index.add(Box::new(OpenShape {
            vertices: vec![p(0.0, 0.0), p(1.0, 0.0), p(0.0, 1.0)],
        }));
        index.build();

        let err = S2ValidQuery::new().validate(&index).unwrap_err();
        assert_eq!(err.code, S2ErrorCode::LoopNotEnoughVertices);
    }

    #[test]
    fn test_duplicate_polygon_edges_fail() {
        // Two separate polygon shapes sharing an edge.
        expect_both_invalid(
            "## 2:0, 0:-2, -2:0, 0:2 | 2:0, 0:-2, 0:0",
            S2ErrorCode::OverlappingGeometry,
        );
    }

    // ─── Chain orientation ───────────────────────────────────────────

    #[test]
    fn test_chains_touching_ok() {
        // Polygon with hole touching at vertex 0:2 (hole CW: E→S→W→N).
        expect_both_valid("## 2:0, 0:-2, -2:0, 0:2; 0:2, -1:0, 0:-1, 1:0;");
        // Touching at vertex -2:0.
        expect_both_valid("## 2:0, 0:-2, -2:0, 0:2; 0:1, -2:0, 0:-1, 1:0;");
        // Improperly oriented hole touching shell (should fail).
        expect_both_invalid(
            "## 2:0, 0:-2, -2:0, 0:2; 1:0, 0:-2, -1:0, 0:2;",
            S2ErrorCode::PolygonInconsistentLoopOrientations,
        );
    }

    #[test]
    fn test_nested_shells_fail() {
        // Various nested CCW shells sharing 0, 1, or 2 vertices with the outer shell.
        let cases = [
            "## 2:0, 0:-2, -2:0, 0:2; 1:0, 0:-1, -1:0, 0:1",
            "## 2:0, 0:-2, -2:0, 0:2; 2:0, 0:-1, -1:0, 0:1",
            "## 2:0, 0:-2, -2:0, 0:2; 2:0, 0:-1, -2:0, 0:1",
            "## 2:0, 0:-2, -2:0, 0:2; 1:0, 0:-2, -1:0, 0:1",
            "## 2:0, 0:-2, -2:0, 0:2; 1:0, 0:-1, -2:0, 0:1",
            "## 2:0, 0:-2, -2:0, 0:2; 1:0, 0:-1, -1:0, 0:2",
        ];
        for case in cases {
            expect_both_invalid(case, S2ErrorCode::PolygonInconsistentLoopOrientations);
        }
    }

    #[test]
    fn test_chains_cannot_cross() {
        // Two chains that cross each other.
        expect_both_invalid(
            "## 3:0, 0:-3, -3:0, 0:+3; 3:2, 0:-1, -3:2, 0:+5",
            S2ErrorCode::PolygonInconsistentLoopOrientations,
        );
        // More crossing cases (detected as overlapping geometry).
        expect_both_invalid(
            "## 0:3, 3:0, 0:-3, -3:0; 3:2, 0:+5, -3:2, 0:-1",
            S2ErrorCode::OverlappingGeometry,
        );
    }

    #[test]
    fn test_shell_in_hole_fails() {
        // A shell contained in a hole.
        expect_both_invalid(
            "## 0:0, 10:10, 10:0; 5:21, 8:21, 6:23",
            S2ErrorCode::PolygonInconsistentLoopOrientations,
        );
    }

    // ─── Multi-dimensional tests (S2ValidQuery only) ────────────────

    #[test]
    fn test_multi_dimensional_ok() {
        // Multi-dimensional geometry: points + polyline + polygon.
        expect_valid(" 3:0| 0:-3| -3:0| 0:3# 2:0, 0:-2, -2:0, 0:2# 1:0, 0:-1, -1:0, 0:1");
    }

    #[test]
    fn test_contained_geometry_fails() {
        // Point inside a polygon.
        expect_invalid(
            "0:0 ## 2:0, 0:-2, -2:0, 0:2",
            S2ErrorCode::OverlappingGeometry,
        );
        // Polyline inside a polygon.
        expect_invalid(
            "# 0:-1, 0:1 # 2:0, 0:-2, -2:0, 0:2",
            S2ErrorCode::OverlappingGeometry,
        );
        // Polygon inside a polygon.
        expect_invalid(
            "## 2:0, 0:-2, -2:0, 0:2 | 1:0, 0:-1, -1:0, 0:1",
            S2ErrorCode::OverlappingGeometry,
        );
    }

    // ─── Legacy-only tests ──────────────────────────────────────────

    #[test]
    fn test_legacy_multi_dimensional_fails() {
        expect_legacy_invalid(
            " 3:0| 0:-3| -3:0| 0:3# 2:0, 0:-2, -2:0, 0:2# 1:0, 0:-1, -1:0, 0:1",
            S2ErrorCode::InvalidDimension,
        );
    }

    #[test]
    fn test_legacy_degenerate_edges_fail() {
        // Degenerate polygon edge.
        expect_legacy_invalid(
            "## 2:0, 2:0, 0:-2, -2:0, 0:-2",
            S2ErrorCode::DuplicateVertices,
        );
        // Degenerate polyline edge.
        expect_legacy_invalid("# 0:0, 0:0, 1:1, 2:2 #", S2ErrorCode::DuplicateVertices);
    }

    #[test]
    fn test_legacy_short_chains_fail() {
        expect_legacy_invalid("## 0:0", S2ErrorCode::LoopNotEnoughVertices);
        expect_legacy_invalid("## 0:0, 1:1", S2ErrorCode::LoopNotEnoughVertices);
    }

    #[test]
    fn test_legacy_split_interiors_ok() {
        expect_legacy_valid("## 3:0, 0:-3, -3:0, 0:+3; 3:0, 0:+1, -3:0, 0:-1");
    }

    #[test]
    fn test_legacy_self_touching_loop_fails() {
        expect_legacy_invalid(
            "## 2:0, 0:-2, -2:0, -1:1, 0:-2, 1:1",
            S2ErrorCode::DuplicateVertices,
        );
    }

    // ─── S2ValidQuery-specific tests ─────────────────────────────────

    #[test]
    fn test_valid_degenerate_rings_allowed() {
        expect_valid("## 0:0");
        expect_valid("## 0:0, 1:1");
    }

    #[test]
    fn test_valid_split_interiors_ok() {
        expect_valid("## 3:0, 0:-3, -3:0, 0:+3; 3:0, 0:+1, -3:0, 0:-1");
    }

    #[test]
    fn test_valid_polyline_crossings_ok() {
        // Interior crossings between polylines.
        expect_valid("# 0:-1, 0:1 | -1:0, 1:0 #");
        // More complex polyline crossings.
        expect_valid("# 0:0, 1:1, 0:2, 1:3, 0:4 | 1:0, 0:1, 1:2, 0:3, 1:4 #");
        // Interior crossings within a polyline.
        expect_valid("# 0:0, 1:1, 0:2, 1:3, 0:4, 1:4, 0:3, 1:2, 0:1, 1:0 #");
    }

    #[test]
    fn test_valid_reverse_duplicate_on_center() {
        // Reverse duplicate pair touching cell center.
        expect_valid("## 2:0, 0:-2, -2:0, 0:2; 0:0, 1:1");
    }

    // test_badly_dimensioned_fails removed: `Dimension` is now an enum,
    // so invalid dimensions (> 2) are prevented at compile time.

    // ─── sort_edges_ccw tests ────────────────────────────────────────

    /// Returns `num` evenly spaced edges all sharing a common origin.
    /// C++: `CcwEdgesAbout`
    fn ccw_edges_about(center: Point, num: usize) -> Vec<Edge> {
        use crate::s2::LatLng;
        let mut edges = Vec::with_capacity(num);
        for i in 0..num {
            let angle = 2.0 * std::f64::consts::PI / num as f64 * i as f64;
            let other = LatLng::from_radians(angle.sin(), angle.cos()).to_point();
            edges.push(Edge::new(center, other));
        }
        edges
    }

    #[test]
    fn test_sort_edges_ccw_start_edge_first() {
        use crate::s2::point;

        let origin = Point::from_coords(0.0, 0.0, 1.0);
        let m = point::get_frame(origin);

        let num_edges = 10usize;
        let mut edges: Vec<Edge> = Vec::new();
        for i in 0..num_edges {
            let angle = (i as f64) * 2.0 * std::f64::consts::PI / num_edges as f64;
            let p = Point(
                crate::r3::Vector::new(0.01 * angle.cos(), 0.01 * angle.sin(), 1.0).normalize(),
            );
            let other = point::from_frame(&m, p);
            edges.push(Edge::new(origin, other));
        }

        for i in 0..num_edges {
            let mut shuffled = edges.clone();
            shuffled.swap(0, 3);
            shuffled.swap(2, 7);
            sort_edges_ccw(origin, edges[i], &mut shuffled);
            assert_eq!(shuffled[0], edges[i]);
        }
    }

    #[test]
    fn test_sort_edges_ccw_sorts_edges() {
        // C++: SortEdgesCcw::SortsEdges
        use rand::SeedableRng;
        use rand::seq::SliceRandom;

        let origin = crate::s2::LatLng::from_radians(0.0, 0.0).to_point();
        let num_edges = 10;
        let mut sorted = ccw_edges_about(origin, num_edges);

        let mut rng = rand::rngs::StdRng::seed_from_u64(42);
        for _ in 0..num_edges {
            sorted.rotate_left(1);
            let mut shuffled = sorted.clone();
            shuffled.shuffle(&mut rng);
            sort_edges_ccw(origin, sorted[0], &mut shuffled);
            assert_eq!(shuffled, sorted);
        }
    }

    #[test]
    fn test_sort_edges_ccw_sorts_edges_flipped() {
        // C++: SortEdgesCcw::SortsEdgesFlipped
        use rand::SeedableRng;
        use rand::seq::SliceRandom;

        let origin = crate::s2::LatLng::from_radians(0.0, 0.0).to_point();
        let num_edges = 10;
        let mut sorted = ccw_edges_about(origin, num_edges);

        // Flip the orientation of some edges.
        sorted[3] = sorted[3].reversed();
        sorted[8] = sorted[8].reversed();

        let mut rng = rand::rngs::StdRng::seed_from_u64(43);
        for _ in 0..num_edges {
            sorted.rotate_left(1);
            let mut shuffled = sorted.clone();
            shuffled.shuffle(&mut rng);
            sort_edges_ccw(origin, sorted[0], &mut shuffled);
            assert_eq!(shuffled, sorted);
        }
    }

    #[test]
    fn test_sort_edges_ccw_reverse_duplicates_ordered() {
        // C++: SortEdgesCcw::ReverseDuplicatesOrdered
        use rand::SeedableRng;
        use rand::seq::SliceRandom;

        let origin = crate::s2::LatLng::from_radians(0.0, 0.0).to_point();
        let num_edges = 10;
        let mut sorted = ccw_edges_about(origin, num_edges);

        // Insert reverse duplicates at positions 8 and 3 (after the original).
        // C++ inserts at begin+8 then begin+3, which shifts indices.
        let rev8 = sorted[8].reversed();
        let rev3 = sorted[3].reversed();
        sorted.insert(9, rev8); // after index 8
        sorted.insert(4, rev3); // after index 3 (was 3, now insert at 4 due to 0-indexed after)

        // Pick sorted[4] as the first edge (matching C++ which uses sorted[4]).
        // Actually C++ uses sorted[4] after both insertions.
        let first = sorted[5]; // adjust: C++ sorted[4] after insert at 3 then 8

        let mut rng = rand::rngs::StdRng::seed_from_u64(44);
        let mut shuffled = sorted.clone();
        shuffled.shuffle(&mut rng);

        sort_edges_ccw(origin, first, &mut shuffled);

        // After sorting, reverse duplicates should be adjacent, and the one
        // with v0 == origin should come first.
        // Find the reverse duplicate pairs.
        let mut found_pairs = 0;
        for i in 0..shuffled.len() - 1 {
            if shuffled[i] == shuffled[i + 1].reversed() {
                assert_eq!(
                    shuffled[i].v0, origin,
                    "reverse duplicate pair: first should have v0 == origin"
                );
                found_pairs += 1;
            }
        }
        assert_eq!(found_pairs, 2, "expected 2 reverse duplicate pairs");
    }

    // ─── Quilt tests ────────────────────────────────────────────────

    /// Builds a "quilt" test shape — a grid of diamond loops stretching from
    /// south to north pole, where every vertex has two chains incident on it.
    /// C++: `MakeQuilt`
    fn make_quilt() -> crate::s2::lax_polygon::LaxPolygon {
        use crate::s2::LatLng;

        let grid_point = |x: i32, y: i32| -> Point {
            debug_assert!((0..=12).contains(&y));
            let x = x.rem_euclid(24);
            if y == 0 {
                return Point::from_coords(0.0, 0.0, -1.0);
            }
            if y == 12 {
                return Point::from_coords(0.0, 0.0, 1.0);
            }
            let lat = -90.0 + 15.0 * f64::from(y);
            let lng = -180.0 + 15.0 * f64::from(x);
            LatLng::from_degrees(lat, lng).to_point()
        };

        let mut loops = Vec::new();
        for x in (0..24).step_by(2) {
            for y in (0..12).step_by(2) {
                let lp = vec![
                    grid_point(x, y + 1),
                    grid_point(x + 1, y + 2),
                    grid_point(x + 2, y + 1),
                    grid_point(x + 1, y),
                ];
                loops.push(lp);
            }
        }
        crate::s2::lax_polygon::LaxPolygon::from_loops_owned(loops)
    }

    #[test]
    fn test_quilt_is_valid() {
        // C++: S2ValidTest::QuiltIsValid
        let quilt = make_quilt();
        let mut index = ShapeIndex::new();
        index.add(Box::new(quilt));
        index.build();

        assert!(
            S2ValidQuery::new().validate(&index).is_ok(),
            "Expected quilt to be valid"
        );
    }

    #[test]
    fn test_quilt_is_not_valid_legacy() {
        // C++: S2LegacyValidTest::QuiltIsNotValid
        // The quilt has reverse duplicate edges near the poles.
        let quilt = make_quilt();
        let mut index = ShapeIndex::new();
        index.add(Box::new(quilt));
        index.build();

        let err = S2LegacyValidQuery::new()
            .validate(&index)
            .expect_err("Expected quilt to be invalid under legacy validation");
        assert_eq!(err.code, S2ErrorCode::OverlappingGeometry);
    }

    // ─── Polygon on cell centers ─────────────────────────────────────

    /// Helper to get cell center from a token string.
    fn cell_center(token: &str) -> Point {
        use crate::s2::cell::Cell;
        use crate::s2::cell_id::CellId;
        Cell::from(CellId::from_token(token)).center()
    }

    #[test]
    fn test_polygon_on_centers_works() {
        // C++: S2ValidTest::PolygonOnCentersWorks
        // Diamond polygon using cell centers straddling equator/prime meridian.
        let loops = vec![
            vec![
                cell_center("0ec"),
                cell_center("044"),
                cell_center("1bc"),
                cell_center("114"),
            ],
            vec![
                cell_center("104"),
                cell_center("1ac"),
                cell_center("054"),
                cell_center("0fc"),
            ],
        ];
        let poly = crate::s2::lax_polygon::LaxPolygon::from_loops_owned(loops);
        let mut index = ShapeIndex::new();
        index.add(Box::new(poly));
        index.build();

        assert!(
            S2ValidQuery::new().validate(&index).is_ok(),
            "Expected polygon on centers to be valid"
        );
    }

    #[test]
    fn test_degenerate_polygon_on_centers_works() {
        // C++: S2ValidTest::DegeneratePolygonOnCentersworks
        // Polygon with reverse-duplicate pairs between cell centers.
        let loops = vec![vec![
            cell_center("0ec"),
            cell_center("044"),
            cell_center("1bc"),
            cell_center("114"),
            cell_center("1bc"),
            cell_center("044"),
        ]];
        let poly = crate::s2::lax_polygon::LaxPolygon::from_loops_owned(loops);
        let mut index = ShapeIndex::new();
        index.add(Box::new(poly));
        index.build();

        assert!(
            S2ValidQuery::new().validate(&index).is_ok(),
            "Expected degenerate polygon on centers to be valid"
        );

        // Second case: diagonal out and back.
        let tokens = ["1004", "1014", "1044", "1054", "1104", "1114"];
        let mut loop_pts: Vec<Point> = tokens.iter().map(|t| cell_center(t)).collect();
        for i in (1..5).rev() {
            loop_pts.push(cell_center(tokens[i]));
        }
        let poly2 = crate::s2::lax_polygon::LaxPolygon::from_loops_owned(vec![loop_pts]);
        let mut index2 = ShapeIndex::new();
        index2.add(Box::new(poly2));
        index2.build();

        assert!(
            S2ValidQuery::new().validate(&index2).is_ok(),
            "Expected diagonal degenerate polygon to be valid"
        );
    }

    // ─── LoopsCrossing ───────────────────────────────────────────────

    #[test]
    fn test_loops_crossing() {
        // C++: AllValidationQueries::LoopsCrossing
        // Generate concentric loops and swap vertices to create crossings.
        use crate::s1;
        use crate::s2::testing::{make_regular_points, random_point};
        use rand::Rng;
        use rand::SeedableRng;

        let mut rng = rand::rngs::StdRng::seed_from_u64(0xDEAD_BEEF);
        let num_iters = 100;

        for _ in 0..num_iters {
            let center = random_point(&mut rng);
            let num_vertices = 4 + rng.gen_range(0..10);

            // Create two concentric loops with decreasing radii.
            let loop0 = make_regular_points(center, s1::Angle::from_degrees(80.0), num_vertices);
            let loop1 = make_regular_points(center, s1::Angle::from_degrees(8.0), num_vertices);

            let mut loop0 = loop0;
            let mut loop1 = loop1;

            // Swap one vertex to create a crossing.
            let i = rng.gen_range(0..num_vertices);
            std::mem::swap(&mut loop0[i], &mut loop1[i]);

            // Optionally also copy adjacent vertices for vertex-crossing.
            if rng.r#gen::<bool>() {
                let n = num_vertices;
                loop0[(i + 1) % n] = loop1[(i + 1) % n];
                loop0[(i + n - 1) % n] = loop1[(i + n - 1) % n];
            }

            // Build S2Polygon from these loops (disabling normal validation).
            let polygon = crate::s2::Polygon::from_loops(vec![
                crate::s2::Loop::new(loop0),
                crate::s2::Loop::new(loop1),
            ]);

            let mut index = ShapeIndex::new();
            index.add(Box::new(polygon));
            index.build();

            // Should be invalid under both queries.
            assert!(
                S2ValidQuery::new().validate(&index).is_err(),
                "iter: crossing polygon should be invalid under S2ValidQuery"
            );

            assert!(
                S2LegacyValidQuery::new().validate(&index).is_err(),
                "iter: crossing polygon should be invalid under S2LegacyValidQuery"
            );
        }
    }

    // ─── Decoder-dependent tests (manual equivalents) ────────────────

    #[test]
    fn test_outgoing_edge_no_incoming() {
        // C++: AllValidationQueries::OutgoingEdgeButNoIncomingEdge
        // Manually construct a polygon shape where one vertex has an outgoing
        // edge but no matching incoming edge (open chain pretending to be dim=2).
        use crate::s2::shape::{Chain, ChainPosition, ReferencePoint};

        #[derive(Debug)]
        struct BrokenPolygonShape {
            edges: Vec<(Point, Point)>,
        }

        impl Shape for BrokenPolygonShape {
            fn num_edges(&self) -> usize {
                self.edges.len()
            }
            fn edge(&self, id: usize) -> Edge {
                Edge::new(self.edges[id].0, self.edges[id].1)
            }
            fn reference_point(&self) -> ReferencePoint {
                ReferencePoint::new(self.edges[0].0, false)
            }
            fn num_chains(&self) -> usize {
                1
            }
            fn chain(&self, _: usize) -> Chain {
                Chain::new(0, self.edges.len())
            }
            fn chain_edge(&self, _: usize, offset: usize) -> Edge {
                self.edge(offset)
            }
            fn chain_position(&self, edge_id: usize) -> ChainPosition {
                ChainPosition::new(0, edge_id)
            }
            fn dimension(&self) -> Dimension {
                Dimension::Polygon
            }
        }

        // Create a "polygon" chain A→B→C→D that doesn't close (D != A).
        use crate::s2::LatLng;
        let a = LatLng::from_degrees(0.0, 0.0).to_point();
        let b = LatLng::from_degrees(1.0, 0.0).to_point();
        let c = LatLng::from_degrees(1.0, 1.0).to_point();
        let d = LatLng::from_degrees(0.5, 0.5).to_point(); // != A

        let shape = BrokenPolygonShape {
            edges: vec![(a, b), (b, c), (c, d)],
        };

        let mut index = ShapeIndex::new();
        index.add(Box::new(shape));
        index.build();

        // Should fail because the chain isn't closed.
        let err = S2ValidQuery::new().validate(&index).unwrap_err();
        assert_eq!(err.code, S2ErrorCode::LoopNotEnoughVertices);
    }

    #[test]
    fn test_invalid_chain_near_chain() {
        // C++: AllValidationQueries::InvalidChainNearChain
        // A shape with one valid chain and one chain with non-unit-length vertices.
        use crate::s2::shape::{Chain, ChainPosition, ReferencePoint};

        #[derive(Debug)]
        struct TwoChainsOneInvalid {
            /// Chain 0: valid polygon chain (3 edges, closed).
            /// Chain 1: chain with non-unit-length vertices (3 edges, closed).
            vertices_0: Vec<Point>,
            vertices_1: Vec<Point>,
        }

        impl Shape for TwoChainsOneInvalid {
            fn num_edges(&self) -> usize {
                self.vertices_0.len() + self.vertices_1.len()
            }
            fn edge(&self, id: usize) -> Edge {
                let n0 = self.vertices_0.len();
                if id < n0 {
                    let v0 = self.vertices_0[id];
                    let v1 = self.vertices_0[(id + 1) % n0];
                    Edge::new(v0, v1)
                } else {
                    let i = id - n0;
                    let n1 = self.vertices_1.len();
                    let v0 = self.vertices_1[i];
                    let v1 = self.vertices_1[(i + 1) % n1];
                    Edge::new(v0, v1)
                }
            }
            fn reference_point(&self) -> ReferencePoint {
                ReferencePoint::new(self.vertices_0[0], false)
            }
            fn num_chains(&self) -> usize {
                2
            }
            fn chain(&self, chain_id: usize) -> Chain {
                if chain_id == 0 {
                    Chain::new(0, self.vertices_0.len())
                } else {
                    Chain::new(self.vertices_0.len(), self.vertices_1.len())
                }
            }
            fn chain_edge(&self, chain_id: usize, offset: usize) -> Edge {
                self.edge(if chain_id == 0 {
                    offset
                } else {
                    self.vertices_0.len() + offset
                })
            }
            fn chain_position(&self, edge_id: usize) -> ChainPosition {
                let n0 = self.vertices_0.len();
                if edge_id < n0 {
                    ChainPosition::new(0, edge_id)
                } else {
                    ChainPosition::new(1, edge_id - n0)
                }
            }
            fn dimension(&self) -> Dimension {
                Dimension::Polygon
            }
        }

        use crate::s2::LatLng;
        // Chain 0: valid triangle.
        let v0 = vec![
            LatLng::from_degrees(10.0, 0.0).to_point(),
            LatLng::from_degrees(10.0, 10.0).to_point(),
            LatLng::from_degrees(20.0, 5.0).to_point(),
        ];
        // Chain 1: non-unit-length vertices (scaled by 2.0).
        let v1 = vec![
            Point(crate::r3::Vector::new(2.0, 0.0, 0.0)),
            Point(crate::r3::Vector::new(0.0, 2.0, 0.0)),
            Point(crate::r3::Vector::new(0.0, 0.0, 2.0)),
        ];

        let shape = TwoChainsOneInvalid {
            vertices_0: v0,
            vertices_1: v1,
        };

        let mut index = ShapeIndex::new();
        index.add(Box::new(shape));
        index.build();

        let err = S2ValidQuery::new().validate(&index).unwrap_err();
        assert_eq!(err.code, S2ErrorCode::NotUnitLength);
    }

    #[cfg(feature = "serde")]
    #[test]
    fn test_serde_touch_type_roundtrip() {
        for v in [
            TouchType::None,
            TouchType::Interior,
            TouchType::Boundary,
            TouchType::Any,
        ] {
            let j = serde_json::to_string(&v).unwrap();
            assert_eq!(v, serde_json::from_str::<TouchType>(&j).unwrap());
        }
    }

    #[cfg(feature = "serde")]
    #[test]
    fn test_serde_validation_options_roundtrip() {
        let opts = ValidationOptions::default();
        let json = serde_json::to_string(&opts).unwrap();
        let back: ValidationOptions = serde_json::from_str(&json).unwrap();
        assert_eq!(opts.allow_degenerate_edges, back.allow_degenerate_edges);
        assert_eq!(opts.legacy_mode, back.legacy_mode);
    }
}