locus-core 0.4.0

A high-performance fiducial marker detector for robotics.
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
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
//! Quad extraction and geometric primitive fitting.
//!
//! This module implements the middle stage of the detection pipeline:
//! 1. **Contour Tracing**: Extracting the boundary of connected components.
//! 2. **Simplification**: Using Douglas-Peucker to reduce complex contours to polygons.
//! 3. **Quad Fitting**: Heuristics to reduce polygons to quadrilaterals and verify convexity.
//! 4. **Sub-pixel Refinement**: Intensity-based edge localization for maximum precision.

#![allow(clippy::cast_possible_wrap)]
#![allow(clippy::cast_sign_loss)]
#![allow(clippy::similar_names)]
#![allow(unsafe_code)]

use crate::Detection;
use crate::batch::{CandidateState, DetectionBatch, MAX_CANDIDATES, Point2f};
use crate::config::DetectorConfig;
use crate::edge_refinement::{ErfEdgeFitter, RefineConfig, SampleConfig};
use crate::image::ImageView;
use crate::segmentation::LabelResult;
use bumpalo::Bump;
use bumpalo::collections::Vec as BumpVec;
use multiversion::multiversion;

use crate::workspace::WORKSPACE_ARENA;

/// Per-corner 2×2 covariances as `[[σ_xx, σ_xy, σ_yx, σ_yy]; 4]`.
pub(crate) type CornerCovariances = [[f32; 4]; 4];

// Re-export the canonical Point type from the crate root.
pub use crate::Point;

/// Fast quad extraction using bounding box stats from CCL.
/// Only traces contours for components that pass geometric filters.
/// Uses default configuration.
#[allow(dead_code)]
pub(crate) fn extract_quads_fast(
    arena: &Bump,
    img: &ImageView,
    label_result: &LabelResult,
) -> Vec<Detection> {
    extract_quads_with_config(arena, img, label_result, &DetectorConfig::default(), 1, img)
}

/// Quad extraction with Structure of Arrays (SoA) output.
///
/// This function populates the `corners` and `status_mask` fields of the provided `DetectionBatch`.
/// It returns the total number of candidates found ($N$).
#[allow(clippy::too_many_lines)]
#[tracing::instrument(skip_all, name = "pipeline::quad_extraction")]
pub fn extract_quads_soa(
    batch: &mut DetectionBatch,
    img: &ImageView,
    label_result: &LabelResult,
    config: &DetectorConfig,
    decimation: usize,
    refinement_img: &ImageView,
    debug_telemetry: bool,
) -> (usize, Option<Vec<[Point; 4]>>) {
    use rayon::prelude::*;

    let stats = &label_result.component_stats;

    // Collect into a temporary Vec to handle the MAX_CANDIDATES limit and sequential writing.
    // In a future optimization, we could use an atomic counter to write directly into the batch.
    let detections: Vec<([Point; 4], [Point; 4], CornerCovariances)> = stats
        .par_iter()
        .enumerate()
        .filter_map(|(label_idx, stat)| {
            WORKSPACE_ARENA.with(|cell| {
                let mut arena = cell.borrow_mut();
                arena.reset();
                let label = (label_idx + 1) as u32;
                extract_single_quad(
                    &arena,
                    img,
                    label_result.labels,
                    label,
                    stat,
                    config,
                    decimation,
                    refinement_img,
                )
            })
        })
        .collect();

    let n = detections.len().min(MAX_CANDIDATES);
    let mut unrefined = if debug_telemetry {
        Some(Vec::with_capacity(n))
    } else {
        None
    };

    for (i, (corners, unrefined_pts, covs)) in detections.into_iter().take(n).enumerate() {
        for (j, corner) in corners.iter().enumerate() {
            batch.corners[i][j] = Point2f {
                x: corner.x as f32,
                y: corner.y as f32,
            };
        }
        // Write per-corner 2×2 covariances (4 floats each, 16 total per candidate).
        if covs.is_empty() {
            batch.corner_covariances[i].fill(0.0);
        } else {
            for (chunk, cov) in batch.corner_covariances[i]
                .chunks_exact_mut(4)
                .zip(covs.iter())
            {
                chunk.copy_from_slice(cov);
            }
        }
        if let Some(ref mut u) = unrefined {
            u.push(unrefined_pts);
        }
        batch.status_mask[i] = CandidateState::Active;
    }

    // Ensure the rest of the batch is marked as Empty
    for i in n..MAX_CANDIDATES {
        batch.status_mask[i] = CandidateState::Empty;
    }

    (n, unrefined)
}

/// Internal helper to extract a single quad from a component.
#[inline]
#[allow(clippy::too_many_arguments, clippy::too_many_lines)]
fn extract_single_quad(
    arena: &Bump,
    img: &ImageView,
    labels: &[u32],
    label: u32,
    stat: &crate::segmentation::ComponentStats,
    config: &DetectorConfig,
    decimation: usize,
    refinement_img: &ImageView,
) -> Option<([Point; 4], [Point; 4], CornerCovariances)> {
    let min_edge_len_sq = config.quad_min_edge_length * config.quad_min_edge_length;
    let d = decimation as f64;

    // Fast geometric filtering using bounding box
    let bbox_w = u32::from(stat.max_x - stat.min_x) + 1;
    let bbox_h = u32::from(stat.max_y - stat.min_y) + 1;
    let bbox_area = bbox_w * bbox_h;

    // Filter: too small or too large
    if bbox_area < config.quad_min_area || bbox_area > (img.width * img.height * 9 / 10) as u32 {
        return None;
    }

    // Filter: not roughly square (aspect ratio)
    let aspect = bbox_w.max(bbox_h) as f32 / bbox_w.min(bbox_h).max(1) as f32;
    if aspect > config.quad_max_aspect_ratio {
        return None;
    }

    // Filter: fill ratio (should be ~50-80% for a tag with inner pattern)
    let fill = stat.pixel_count as f32 / bbox_area as f32;
    if fill < config.quad_min_fill_ratio || fill > config.quad_max_fill_ratio {
        return None;
    }

    // Moments-based culling gate: reject elongated or sparse blobs before contour tracing.
    // Disabled by default (both thresholds are 0.0).
    if (config.quad_max_elongation > 0.0 || config.quad_min_density > 0.0)
        && let Some((elongation, density)) = crate::segmentation::compute_moment_shape(stat)
    {
        if config.quad_max_elongation > 0.0 && elongation > config.quad_max_elongation {
            return None;
        }
        if config.quad_min_density > 0.0 && density < config.quad_min_density {
            return None;
        }
    }

    // Passed filters — extract rough quad corners using the configured mode.
    // `gn_covs` carries per-corner 2×2 covariances from the EdLines GN solver;
    // zero for ContourRdp (no covariance information available).
    let (quad_pts_dec, gn_covs): ([Point; 4], CornerCovariances) = match config.quad_extraction_mode
    {
        crate::config::QuadExtractionMode::EdLines => {
            let ed_cfg = crate::edlines::EdLinesConfig::from_detector_config(config);
            crate::edlines::extract_quad_edlines(
                arena,
                img,
                refinement_img,
                labels,
                label,
                stat,
                &ed_cfg,
            )?
        },
        crate::config::QuadExtractionMode::ContourRdp => {
            let sx = stat.first_pixel_x as usize;
            let sy = stat.first_pixel_y as usize;

            let contour = trace_boundary(arena, labels, img.width, img.height, sx, sy, label);

            if contour.len() < 12 {
                return None;
            }

            let simple_contour = chain_approximation(arena, &contour);
            let perimeter = contour.len() as f64;
            let epsilon = (perimeter * 0.02).max(1.0);
            let simplified = douglas_peucker(arena, &simple_contour, epsilon);

            if simplified.len() < 4 || simplified.len() > 11 {
                return None;
            }

            let simpl_len = simplified.len();
            let reduced = if simpl_len == 5 {
                simplified
            } else if simpl_len == 4 {
                let mut closed = BumpVec::new_in(arena);
                for p in &simplified {
                    closed.push(*p);
                }
                closed.push(simplified[0]);
                closed
            } else {
                reduce_to_quad(arena, &simplified)
            };

            if reduced.len() != 5 {
                return None;
            }

            let area = polygon_area(&reduced);
            let compactness = (12.566 * area.abs()) / (perimeter * perimeter);

            if area.abs() <= f64::from(config.quad_min_area) || compactness <= 0.1 {
                return None;
            }

            // Standardize to CW for consistency
            if area > 0.0 {
                (
                    [reduced[0], reduced[1], reduced[2], reduced[3]],
                    [[0.0; 4]; 4],
                )
            } else {
                (
                    [reduced[0], reduced[3], reduced[2], reduced[1]],
                    [[0.0; 4]; 4],
                )
            }
        },
    };

    // Scale to full resolution using correct coordinate mapping.
    // A point at (x, y) in decimated coordinates maps to (x*d, y*d) in full-res.
    let quad_pts = [
        Point {
            x: quad_pts_dec[0].x * d,
            y: quad_pts_dec[0].y * d,
        },
        Point {
            x: quad_pts_dec[1].x * d,
            y: quad_pts_dec[1].y * d,
        },
        Point {
            x: quad_pts_dec[2].x * d,
            y: quad_pts_dec[2].y * d,
        },
        Point {
            x: quad_pts_dec[3].x * d,
            y: quad_pts_dec[3].y * d,
        },
    ];

    // Expand 0.5px outward from the centroid to align with pixel boundaries.
    // This is needed for ContourRdp, whose corners are at integer-coordinate
    // midpoints of edge segments.  EdLines already produces full-resolution
    // sub-pixel corners (from its micro-ray parabola + Gauss-Newton pass), so
    // applying the expansion would move them *away* from the true edge and force
    // the subsequent refine_corner to fight the artificial offset.
    let quad_pts = if config.quad_extraction_mode == crate::config::QuadExtractionMode::EdLines {
        quad_pts // corners already sub-pixel accurate; no expansion needed
    } else {
        let center_x = (quad_pts[0].x + quad_pts[1].x + quad_pts[2].x + quad_pts[3].x) * 0.25;
        let center_y = (quad_pts[0].y + quad_pts[1].y + quad_pts[2].y + quad_pts[3].y) * 0.25;
        let mut ep = quad_pts;
        for i in 0..4 {
            ep[i].x += 0.5 * (quad_pts[i].x - center_x).signum();
            ep[i].y += 0.5 * (quad_pts[i].y - center_y).signum();
        }
        ep
    };

    let mut ok = true;
    for i in 0..4 {
        let d2 = (quad_pts[i].x - quad_pts[(i + 1) % 4].x).powi(2)
            + (quad_pts[i].y - quad_pts[(i + 1) % 4].y).powi(2);
        if d2 < min_edge_len_sq {
            ok = false;
            break;
        }
    }

    if ok {
        // `CornerRefinementMode::None` passes the quad corners through untouched.
        // This is appropriate for EdLines, which handles sub-pixel refinement
        // internally; it is also an explicit opt-out for benchmarking.
        // All other modes call `refine_corner`.
        //
        // Covariances: only propagate GN covariances when no further refinement
        // is applied (Mode::None).  GWLF computes its own covariances in
        // detector.rs; ERF has none.  When refinement overwrites corners,
        // the GN covariances are no longer valid.
        let (corners, out_covs) =
            if config.refinement_mode == crate::config::CornerRefinementMode::None {
                (quad_pts, gn_covs)
            } else {
                let use_erf = config.refinement_mode == crate::config::CornerRefinementMode::Erf;
                (
                    [
                        refine_corner(
                            arena,
                            refinement_img,
                            quad_pts[0],
                            quad_pts[3],
                            quad_pts[1],
                            config.subpixel_refinement_sigma,
                            decimation,
                            use_erf,
                        ),
                        refine_corner(
                            arena,
                            refinement_img,
                            quad_pts[1],
                            quad_pts[0],
                            quad_pts[2],
                            config.subpixel_refinement_sigma,
                            decimation,
                            use_erf,
                        ),
                        refine_corner(
                            arena,
                            refinement_img,
                            quad_pts[2],
                            quad_pts[1],
                            quad_pts[3],
                            config.subpixel_refinement_sigma,
                            decimation,
                            use_erf,
                        ),
                        refine_corner(
                            arena,
                            refinement_img,
                            quad_pts[3],
                            quad_pts[2],
                            quad_pts[0],
                            config.subpixel_refinement_sigma,
                            decimation,
                            use_erf,
                        ),
                    ],
                    [[0.0; 4]; 4],
                )
            };

        let edge_score = calculate_edge_score(refinement_img, corners);
        if edge_score > config.quad_min_edge_score {
            return Some((corners, quad_pts, out_covs));
        }
    }
    None
}

/// Max per-point `distort(undistort(xd)) − xd` drift tolerated during
/// boundary rectification. Chosen at 2× the `camera_geometry.rs` round-trip
/// proptest envelope (`< 1e-4`) so Newton blow-up bails while
/// well-conditioned points stay.
#[cfg(feature = "non_rectified")]
const MAX_UNDISTORT_RESIDUAL: f64 = 2e-4;

/// Intrinsics rescaled to the decimation grid: `p_dec = (p_full + 0.5) / d − 0.5`
/// on the principal point, focals divided by `d`. The +0.5 shifts come from
/// the project's center-aware decimation convention (see `.agent/rules/core.md`).
#[cfg(feature = "non_rectified")]
#[derive(Clone, Copy, Debug)]
struct ScaledIntrinsics {
    fx: f64,
    fy: f64,
    cx: f64,
    cy: f64,
}

#[cfg(feature = "non_rectified")]
impl ScaledIntrinsics {
    #[inline]
    fn from_intrinsics(intrinsics: &crate::pose::CameraIntrinsics, decimation: usize) -> Self {
        let d = decimation as f64;
        Self {
            fx: intrinsics.fx / d,
            fy: intrinsics.fy / d,
            cx: (intrinsics.cx + 0.5) / d - 0.5,
            cy: (intrinsics.cy + 0.5) / d - 0.5,
        }
    }
}

/// Camera-aware quad extraction with Structure of Arrays (SoA) output.
///
/// Identical contract to [`extract_quads_soa`], but runs Douglas-Peucker in
/// *normalized (straight-line)* camera coordinates by undistorting each
/// boundary point through `C::undistort` before simplification. This keeps
/// curved marker edges from being mis-quantized into >4 RDP points on
/// distorted (Brown-Conrady / Kannala-Brandt) imagery.
///
/// The pinhole path (`C::IS_RECTIFIED == true`) is compile-time erased to
/// the existing rectified flow via monomorphization.
#[cfg(feature = "non_rectified")]
#[allow(clippy::too_many_arguments)]
#[tracing::instrument(skip_all, name = "pipeline::quad_extraction_camera")]
pub fn extract_quads_soa_with_camera<C: crate::camera::CameraModel>(
    batch: &mut DetectionBatch,
    img: &ImageView,
    label_result: &LabelResult,
    config: &DetectorConfig,
    decimation: usize,
    refinement_img: &ImageView,
    debug_telemetry: bool,
    camera: &C,
    intrinsics: &crate::pose::CameraIntrinsics,
) -> (usize, Option<Vec<[Point; 4]>>) {
    use rayon::prelude::*;

    let stats = &label_result.component_stats;
    let scaled = ScaledIntrinsics::from_intrinsics(intrinsics, decimation);

    let detections: Vec<([Point; 4], [Point; 4], CornerCovariances)> = stats
        .par_iter()
        .enumerate()
        .filter_map(|(label_idx, stat)| {
            WORKSPACE_ARENA.with(|cell| {
                let mut arena = cell.borrow_mut();
                arena.reset();
                let label = (label_idx + 1) as u32;
                extract_single_quad_with_camera(
                    &arena,
                    img,
                    label_result.labels,
                    label,
                    stat,
                    config,
                    decimation,
                    refinement_img,
                    camera,
                    scaled,
                    intrinsics,
                )
            })
        })
        .collect();

    let n = detections.len().min(MAX_CANDIDATES);
    let mut unrefined = if debug_telemetry {
        Some(Vec::with_capacity(n))
    } else {
        None
    };

    for (i, (corners, unrefined_pts, covs)) in detections.into_iter().take(n).enumerate() {
        for (j, corner) in corners.iter().enumerate() {
            batch.corners[i][j] = Point2f {
                x: corner.x as f32,
                y: corner.y as f32,
            };
        }
        if covs.is_empty() {
            batch.corner_covariances[i].fill(0.0);
        } else {
            for (chunk, cov) in batch.corner_covariances[i]
                .chunks_exact_mut(4)
                .zip(covs.iter())
            {
                chunk.copy_from_slice(cov);
            }
        }
        if let Some(ref mut u) = unrefined {
            u.push(unrefined_pts);
        }
        batch.status_mask[i] = CandidateState::Active;
    }

    for i in n..MAX_CANDIDATES {
        batch.status_mask[i] = CandidateState::Empty;
    }

    (n, unrefined)
}

/// Per-component worker for [`extract_quads_soa_with_camera`].
///
/// Runs the ContourRdp pipeline in normalized (undistorted) camera space so
/// that projectively-straight lines remain straight under RDP. EdLines is
/// intentionally unsupported on this path and is blocked upstream by
/// `DetectorError::Config(EdLinesUnsupportedWithDistortion)`.
#[cfg(feature = "non_rectified")]
#[inline]
#[allow(clippy::too_many_arguments, clippy::too_many_lines)]
fn extract_single_quad_with_camera<C: crate::camera::CameraModel>(
    arena: &Bump,
    img: &ImageView,
    labels: &[u32],
    label: u32,
    stat: &crate::segmentation::ComponentStats,
    config: &DetectorConfig,
    decimation: usize,
    refinement_img: &ImageView,
    camera: &C,
    scaled: ScaledIntrinsics,
    intrinsics: &crate::pose::CameraIntrinsics,
) -> Option<([Point; 4], [Point; 4], CornerCovariances)> {
    // EdLines is blocked upstream for distorted cameras; this path is
    // ContourRdp-only. For pinhole the monomorphization is bit-identical
    // to the rectified flow (IS_RECTIFIED erases the rectify steps).
    if config.quad_extraction_mode != crate::config::QuadExtractionMode::ContourRdp {
        return None;
    }

    let min_edge_len_sq = config.quad_min_edge_length * config.quad_min_edge_length;
    let d = decimation as f64;

    let bbox_w = u32::from(stat.max_x - stat.min_x) + 1;
    let bbox_h = u32::from(stat.max_y - stat.min_y) + 1;
    let bbox_area = bbox_w * bbox_h;

    if bbox_area < config.quad_min_area || bbox_area > (img.width * img.height * 9 / 10) as u32 {
        return None;
    }

    let aspect = bbox_w.max(bbox_h) as f32 / bbox_w.min(bbox_h).max(1) as f32;
    if aspect > config.quad_max_aspect_ratio {
        return None;
    }

    let fill = stat.pixel_count as f32 / bbox_area as f32;
    if fill < config.quad_min_fill_ratio || fill > config.quad_max_fill_ratio {
        return None;
    }

    if (config.quad_max_elongation > 0.0 || config.quad_min_density > 0.0)
        && let Some((elongation, density)) = crate::segmentation::compute_moment_shape(stat)
    {
        if config.quad_max_elongation > 0.0 && elongation > config.quad_max_elongation {
            return None;
        }
        if config.quad_min_density > 0.0 && density < config.quad_min_density {
            return None;
        }
    }

    let sx = stat.first_pixel_x as usize;
    let sy = stat.first_pixel_y as usize;
    let contour = trace_boundary(arena, labels, img.width, img.height, sx, sy, label);

    if contour.len() < 12 {
        return None;
    }

    // Rectify the boundary in decimated-pixel units so downstream pixel
    // thresholds (RDP epsilon, min edge length) still apply. Newton
    // divergence is the only failure signal since `CameraModel::undistort`
    // is infallible; we detect it by re-distorting and bailing on drift.
    let rectified = if C::IS_RECTIFIED {
        contour
    } else {
        let mut rect = BumpVec::with_capacity_in(contour.len(), arena);
        for p in &contour {
            let xd = (p.x - scaled.cx) / scaled.fx;
            let yd = (p.y - scaled.cy) / scaled.fy;
            let [xn, yn] = camera.undistort(xd, yd);
            let [xd_chk, yd_chk] = camera.distort(xn, yn);
            let dx = xd_chk - xd;
            let dy = yd_chk - yd;
            if (dx * dx + dy * dy).sqrt() > MAX_UNDISTORT_RESIDUAL {
                return None;
            }
            rect.push(Point {
                x: xn * scaled.fx + scaled.cx,
                y: yn * scaled.fy + scaled.cy,
            });
        }
        rect
    };

    let simple_contour = chain_approximation(arena, &rectified);
    let perimeter = rectified.len() as f64;
    let epsilon = (perimeter * 0.02).max(1.0);
    let simplified = douglas_peucker(arena, &simple_contour, epsilon);

    if simplified.len() < 4 || simplified.len() > 11 {
        return None;
    }

    let simpl_len = simplified.len();
    let reduced = if simpl_len == 5 {
        simplified
    } else if simpl_len == 4 {
        let mut closed = BumpVec::new_in(arena);
        for p in &simplified {
            closed.push(*p);
        }
        closed.push(simplified[0]);
        closed
    } else {
        reduce_to_quad(arena, &simplified)
    };

    if reduced.len() != 5 {
        return None;
    }

    let area = polygon_area(&reduced);
    let compactness = (12.566 * area.abs()) / (perimeter * perimeter);

    if area.abs() <= f64::from(config.quad_min_area) || compactness <= 0.1 {
        return None;
    }

    // Standardize to CW in rectified space.
    let quad_rect: [Point; 4] = if area > 0.0 {
        [reduced[0], reduced[1], reduced[2], reduced[3]]
    } else {
        [reduced[0], reduced[3], reduced[2], reduced[1]]
    };

    // Un-rectify the 4 corners back to decimated-pixel distorted space.
    let quad_pts_dec: [Point; 4] = if C::IS_RECTIFIED {
        quad_rect
    } else {
        let mut out = quad_rect;
        for p in &mut out {
            let xn = (p.x - scaled.cx) / scaled.fx;
            let yn = (p.y - scaled.cy) / scaled.fy;
            let [xd, yd] = camera.distort(xn, yn);
            p.x = xd * scaled.fx + scaled.cx;
            p.y = yd * scaled.fy + scaled.cy;
        }
        out
    };

    let quad_pts = quad_pts_dec.map(|p| Point {
        x: p.x * d,
        y: p.y * d,
    });

    // Gate the +0.5 outward expansion on `C::IS_RECTIFIED`: corners produced
    // by RDP in straight-space are projectively exact intersections, not
    // integer-midpoint artifacts of a stepped pixel contour. Applying the
    // 0.5px nudge would move them off the true edge and fight later
    // refinement.
    let quad_pts = if C::IS_RECTIFIED {
        let center_x = (quad_pts[0].x + quad_pts[1].x + quad_pts[2].x + quad_pts[3].x) * 0.25;
        let center_y = (quad_pts[0].y + quad_pts[1].y + quad_pts[2].y + quad_pts[3].y) * 0.25;
        let mut ep = quad_pts;
        for i in 0..4 {
            ep[i].x += 0.5 * (quad_pts[i].x - center_x).signum();
            ep[i].y += 0.5 * (quad_pts[i].y - center_y).signum();
        }
        ep
    } else {
        quad_pts
    };

    for i in 0..4 {
        let d2 = (quad_pts[i].x - quad_pts[(i + 1) % 4].x).powi(2)
            + (quad_pts[i].y - quad_pts[(i + 1) % 4].y).powi(2);
        if d2 < min_edge_len_sq {
            return None;
        }
    }

    // Full-res rectified corners — only meaningful on the distorted path,
    // where `refine_corner_with_camera` expects its triplet in straight space.
    let quad_rect_full = quad_rect.map(|p| Point {
        x: p.x * d,
        y: p.y * d,
    });

    let (corners, out_covs) = if config.refinement_mode == crate::config::CornerRefinementMode::None
    {
        (quad_pts, [[0.0_f32; 4]; 4])
    } else if C::IS_RECTIFIED {
        let use_erf = config.refinement_mode == crate::config::CornerRefinementMode::Erf;
        (
            [
                refine_corner(
                    arena,
                    refinement_img,
                    quad_pts[0],
                    quad_pts[3],
                    quad_pts[1],
                    config.subpixel_refinement_sigma,
                    decimation,
                    use_erf,
                ),
                refine_corner(
                    arena,
                    refinement_img,
                    quad_pts[1],
                    quad_pts[0],
                    quad_pts[2],
                    config.subpixel_refinement_sigma,
                    decimation,
                    use_erf,
                ),
                refine_corner(
                    arena,
                    refinement_img,
                    quad_pts[2],
                    quad_pts[1],
                    quad_pts[3],
                    config.subpixel_refinement_sigma,
                    decimation,
                    use_erf,
                ),
                refine_corner(
                    arena,
                    refinement_img,
                    quad_pts[3],
                    quad_pts[2],
                    quad_pts[0],
                    config.subpixel_refinement_sigma,
                    decimation,
                    use_erf,
                ),
            ],
            [[0.0_f32; 4]; 4],
        )
    } else {
        (
            [
                refine_corner_with_camera(
                    refinement_img,
                    quad_rect_full[0],
                    quad_rect_full[3],
                    quad_rect_full[1],
                    quad_pts[0],
                    decimation,
                    intrinsics,
                    camera,
                ),
                refine_corner_with_camera(
                    refinement_img,
                    quad_rect_full[1],
                    quad_rect_full[0],
                    quad_rect_full[2],
                    quad_pts[1],
                    decimation,
                    intrinsics,
                    camera,
                ),
                refine_corner_with_camera(
                    refinement_img,
                    quad_rect_full[2],
                    quad_rect_full[1],
                    quad_rect_full[3],
                    quad_pts[2],
                    decimation,
                    intrinsics,
                    camera,
                ),
                refine_corner_with_camera(
                    refinement_img,
                    quad_rect_full[3],
                    quad_rect_full[2],
                    quad_rect_full[0],
                    quad_pts[3],
                    decimation,
                    intrinsics,
                    camera,
                ),
            ],
            [[0.0_f32; 4]; 4],
        )
    };

    // Edges between distorted corners are *curved* in the image, so a
    // straight-line edge score would sample the tag interior and spuriously
    // reject. Sample along the rectified straight line and forward-distort
    // each point to read pixels from the real (distorted) image.
    let edge_score = if C::IS_RECTIFIED {
        calculate_edge_score(refinement_img, corners)
    } else {
        calculate_edge_score_curved(refinement_img, &quad_rect, camera, scaled, decimation)
    };
    if edge_score <= config.quad_min_edge_score {
        return None;
    }

    Some((corners, quad_pts, out_covs))
}

/// Quad extraction with custom configuration.
///
/// This is the main entry point for quad detection with custom parameters.
/// Components are processed in parallel for maximum throughput.
#[allow(clippy::too_many_lines)]
pub fn extract_quads_with_config(
    _arena: &Bump,
    img: &ImageView,
    label_result: &LabelResult,
    config: &DetectorConfig,
    decimation: usize,
    refinement_img: &ImageView,
) -> Vec<Detection> {
    use rayon::prelude::*;

    let stats = &label_result.component_stats;
    let d = decimation as f64;

    // Process components in parallel, each with its own thread-local arena
    stats
        .par_iter()
        .enumerate()
        .filter_map(|(label_idx, stat)| {
            WORKSPACE_ARENA.with(|cell| {
                let mut arena = cell.borrow_mut();
                arena.reset();
                let label = (label_idx + 1) as u32;

                let quad_result = extract_single_quad(
                    &arena,
                    img,
                    label_result.labels,
                    label,
                    stat,
                    config,
                    decimation,
                    refinement_img,
                );

                let (corners, _unrefined, _covs) = quad_result?;
                // To keep backward compatibility, we still need to calculate some fields
                // that aren't yet in the SoA (or are derived from it).
                let area = polygon_area(&corners);

                Some(Detection {
                    id: label,
                    center: [
                        (corners[0].x + corners[1].x + corners[2].x + corners[3].x) / 4.0,
                        (corners[0].y + corners[1].y + corners[2].y + corners[3].y) / 4.0,
                    ],
                    corners: [
                        [corners[0].x, corners[0].y],
                        [corners[1].x, corners[1].y],
                        [corners[2].x, corners[2].y],
                        [corners[3].x, corners[3].y],
                    ],
                    hamming: 0,
                    rotation: 0,
                    decision_margin: area * d * d, // Area in full-res
                    bits: 0,
                    pose: None,
                    pose_covariance: None,
                })
            })
        })
        .collect()
}

/// Legacy extract_quads for backward compatibility.
#[allow(dead_code)]
pub(crate) fn extract_quads(arena: &Bump, img: &ImageView, labels: &[u32]) -> Vec<Detection> {
    // Create a fake LabelResult with stats computed on-the-fly
    let mut detections = Vec::new();
    let num_labels = (labels.len() / 32) + 1;
    let processed_labels = arena.alloc_slice_fill_copy(num_labels, 0u32);

    let width = img.width;
    let height = img.height;

    for y in 1..height - 1 {
        let row_off = y * width;
        let prev_row_off = (y - 1) * width;

        for x in 1..width - 1 {
            let idx = row_off + x;
            let label = labels[idx];

            if label == 0 {
                continue;
            }

            if labels[idx - 1] == label || labels[prev_row_off + x] == label {
                continue;
            }

            let bit_idx = (label as usize) / 32;
            let bit_mask = 1 << (label % 32);
            if processed_labels[bit_idx] & bit_mask != 0 {
                continue;
            }

            processed_labels[bit_idx] |= bit_mask;
            let contour = trace_boundary(arena, labels, width, height, x, y, label);

            if contour.len() >= 12 {
                // Lowered from 30 to support 8px+ tags
                let simplified = douglas_peucker(arena, &contour, 4.0);
                if simplified.len() == 5 {
                    let area = polygon_area(&simplified);
                    let perimeter = contour.len() as f64;
                    let compactness = (12.566 * area) / (perimeter * perimeter);

                    if area > 400.0 && compactness > 0.5 {
                        let mut ok = true;
                        for i in 0..4 {
                            let d2 = (simplified[i].x - simplified[i + 1].x).powi(2)
                                + (simplified[i].y - simplified[i + 1].y).powi(2);
                            if d2 < 100.0 {
                                ok = false;
                                break;
                            }
                        }

                        if ok {
                            detections.push(Detection {
                                id: label,
                                center: polygon_center(&simplified),
                                corners: [
                                    [simplified[0].x, simplified[0].y],
                                    [simplified[1].x, simplified[1].y],
                                    [simplified[2].x, simplified[2].y],
                                    [simplified[3].x, simplified[3].y],
                                ],
                                hamming: 0,
                                rotation: 0,
                                decision_margin: area,
                                bits: 0,
                                pose: None,
                                pose_covariance: None,
                            });
                        }
                    }
                }
            }
        }
    }
    detections
}

#[multiversion(targets(
    "x86_64+avx2+bmi1+bmi2+popcnt+lzcnt",
    "x86_64+avx512f+avx512bw+avx512dq+avx512vl",
    "aarch64+neon"
))]
fn find_max_distance_optimized(points: &[Point], start: usize, end: usize) -> (f64, usize) {
    let a = points[start];
    let b = points[end];
    let dx = b.x - a.x;
    let dy = b.y - a.y;
    let mag_sq = dx * dx + dy * dy;

    if mag_sq < 1e-18 {
        let mut dmax = 0.0;
        let mut index = start;
        for (i, p) in points.iter().enumerate().take(end).skip(start + 1) {
            let d = ((p.x - a.x).powi(2) + (p.y - a.y).powi(2)).sqrt();
            if d > dmax {
                dmax = d;
                index = i;
            }
        }
        return (dmax, index);
    }

    let mut dmax = 0.0;
    let mut index = start;

    let mut i = start + 1;

    #[cfg(all(target_arch = "x86_64", target_feature = "avx2"))]
    if let Some(_dispatch) = multiversion::target::x86_64::avx2::get() {
        // SAFETY: `multiversion::target::x86_64::avx2::get()` returned `Some`,
        // confirming the runtime CPU supports AVX2; the AVX2 intrinsics
        // below (`_mm256_set1_pd`, `_mm256_loadu_pd`, …) are therefore
        // safe to invoke. `_mm256_loadu_pd` is the unaligned variant; the
        // `points[i + 3].x` reads are bounds-checked by the surrounding
        // `while i + 4 <= end` guard.
        unsafe {
            use std::arch::x86_64::*;
            let v_dx = _mm256_set1_pd(dx);
            let v_dy = _mm256_set1_pd(dy);
            let v_ax = _mm256_set1_pd(a.x);
            let v_ay = _mm256_set1_pd(a.y);
            let v_bx = _mm256_set1_pd(b.x);
            let v_by = _mm256_set1_pd(b.y);

            let mut v_dmax = _mm256_setzero_pd();
            let mut v_indices = _mm256_setzero_pd(); // We'll store indices as doubles for simplicity

            while i + 4 <= end {
                // Load 4 points (8 doubles: x0, y0, x1, y1, x2, y2, x3, y3)
                // Point is struct { x: f64, y: f64 } which is memory-compatible with [f64; 2]
                let p_ptr = points.as_ptr().add(i) as *const f64;

                // Unpack into xxxx and yyyy
                // [x0, y0, x1, y1]
                let raw0 = _mm256_loadu_pd(p_ptr);
                // [x2, y2, x3, y3]
                let raw1 = _mm256_loadu_pd(p_ptr.add(4));

                // permute to get [x0, x1, y0, y1]
                let x01y01 = _mm256_shuffle_pd(raw0, raw0, 0b0000); // Wait, shuffle_pd is tricky
                // Better: use unpack
                let x0x1 = _mm256_set_pd(
                    points[i + 3].x,
                    points[i + 2].x,
                    points[i + 1].x,
                    points[i].x,
                );
                let y0y1 = _mm256_set_pd(
                    points[i + 3].y,
                    points[i + 2].y,
                    points[i + 1].y,
                    points[i].y,
                );

                // formula: |dy*px - dx*py + bx*ay - by*ax| * inv_mag
                let term1 = _mm256_mul_pd(v_dy, x0x1);
                let term2 = _mm256_mul_pd(v_dx, y0y1);
                let term3 = _mm256_set1_pd(b.x * a.y - b.y * a.x);

                let dist_v = _mm256_sub_pd(term1, term2);
                let dist_v = _mm256_add_pd(dist_v, term3);

                // Absolute value
                let mask = _mm256_set1_pd(-0.0);
                let dist_v = _mm256_andnot_pd(mask, dist_v);

                // Check if any dist > v_dmax
                let cmp = _mm256_cmp_pd(dist_v, v_dmax, _CMP_GT_OQ);
                if _mm256_movemask_pd(cmp) != 0 {
                    // Update dmax and indices - this is a bit slow in SIMD,
                    // but we only do it when we find a new max.
                    let dists: [f64; 4] = std::mem::transmute(dist_v);
                    for (j, &d) in dists.iter().enumerate() {
                        if d > dmax {
                            dmax = d;
                            index = i + j;
                        }
                    }
                    v_dmax = _mm256_set1_pd(dmax);
                }
                i += 4;
            }
        }
    }

    // Scalar tail
    while i < end {
        let d = perpendicular_distance(points[i], a, b);
        if d > dmax {
            dmax = d;
            index = i;
        }
        i += 1;
    }

    (dmax, index)
}

/// Simplify a contour using the Douglas-Peucker algorithm.
///
/// Leverages an iterative implementation with a manual stack to avoid
/// the overhead of recursive function calls and multiple temporary allocations.
pub(crate) fn douglas_peucker<'a>(
    arena: &'a Bump,
    points: &[Point],
    epsilon: f64,
) -> BumpVec<'a, Point> {
    if points.len() < 3 {
        let mut v = BumpVec::new_in(arena);
        v.extend_from_slice(points);
        return v;
    }

    let n = points.len();
    let mut keep = BumpVec::from_iter_in((0..n).map(|_| false), arena);
    keep[0] = true;
    keep[n - 1] = true;

    let mut stack = BumpVec::new_in(arena);
    stack.push((0, n - 1));

    while let Some((start, end)) = stack.pop() {
        if end - start < 2 {
            continue;
        }

        let (dmax, index) = find_max_distance_optimized(points, start, end);

        if dmax > epsilon {
            keep[index] = true;
            stack.push((start, index));
            stack.push((index, end));
        }
    }

    let mut simplified = BumpVec::new_in(arena);
    for (i, &k) in keep.iter().enumerate() {
        if k {
            simplified.push(points[i]);
        }
    }
    simplified
}

fn perpendicular_distance(p: Point, a: Point, b: Point) -> f64 {
    let dx = b.x - a.x;
    let dy = b.y - a.y;
    let mag = (dx * dx + dy * dy).sqrt();
    if mag < 1e-9 {
        return ((p.x - a.x).powi(2) + (p.y - a.y).powi(2)).sqrt();
    }
    ((dy * p.x - dx * p.y + b.x * a.y - b.y * a.x).abs()) / mag
}

fn polygon_area(points: &[Point]) -> f64 {
    let mut area = 0.0;
    for i in 0..points.len() - 1 {
        area += (points[i].x * points[i + 1].y) - (points[i + 1].x * points[i].y);
    }
    area * 0.5
}

#[allow(dead_code)]
fn polygon_center(points: &[Point]) -> [f64; 2] {
    let mut cx = 0.0;
    let mut cy = 0.0;
    let n = points.len() - 1;
    for p in points.iter().take(n) {
        cx += p.x;
        cy += p.y;
    }
    [cx / n as f64, cy / n as f64]
}

/// Refine corners to sub-pixel accuracy using intensity-based optimization.
///
/// Fits lines to the two edges meeting at a corner using PSF-blurred step function
/// model and Gauss-Newton optimization, then computes their intersection.
/// Achieves ~0.02px accuracy vs ~0.2px for gradient-peak methods.
#[must_use]
#[allow(clippy::too_many_arguments)]
pub(crate) fn refine_corner(
    arena: &Bump,
    img: &ImageView,
    p: Point,
    p_prev: Point,
    p_next: Point,
    sigma: f64,
    decimation: usize,
    use_erf: bool,
) -> Point {
    // Intensity-based refinement (ERF fit) is much more accurate but slower.
    let line1 = if use_erf {
        refine_edge_erf(arena, img, p_prev, p, sigma, decimation)
            .or_else(|| fit_edge_line(img, p_prev, p, decimation))
    } else {
        fit_edge_line(img, p_prev, p, decimation)
    };

    let line2 = if use_erf {
        refine_edge_erf(arena, img, p, p_next, sigma, decimation)
            .or_else(|| fit_edge_line(img, p, p_next, decimation))
    } else {
        fit_edge_line(img, p, p_next, decimation)
    };

    if let (Some(l1), Some(l2)) = (line1, line2) {
        // Intersect lines: a1*x + b1*y + c1 = 0 and a2*x + b2*y + c2 = 0
        let det = l1.0 * l2.1 - l2.0 * l1.1;
        if det.abs() > 1e-6 {
            let x = (l1.1 * l2.2 - l2.1 * l1.2) / det;
            let y = (l2.0 * l1.2 - l1.0 * l2.2) / det;

            // Sanity check: intersection must be near original point
            let dist_sq = (x - p.x).powi(2) + (y - p.y).powi(2);
            let max_dist = if decimation > 1 {
                (decimation as f64) + 2.0
            } else {
                2.0
            };
            if dist_sq < max_dist * max_dist {
                return Point { x, y };
            }
        }
    }

    p
}

/// Fit a line (a*x + b*y + c = 0) to an edge by sampling gradient peaks.
fn fit_edge_line(
    img: &ImageView,
    p1: Point,
    p2: Point,
    decimation: usize,
) -> Option<(f64, f64, f64)> {
    let dx = p2.x - p1.x;
    let dy = p2.y - p1.y;
    let len = (dx * dx + dy * dy).sqrt();
    if len < 4.0 {
        return None;
    }

    let nx = dy / len;
    let ny = -dx / len;

    let mut sum_d = 0.0;
    let mut count = 0;

    let n_samples = (len as usize).clamp(5, 15);
    // Original search range was 3 pixels
    let r = if decimation > 1 {
        (decimation as i32) + 1
    } else {
        3
    };

    for i in 1..=n_samples {
        let t = i as f64 / (n_samples + 1) as f64;
        let px = p1.x + dx * t;
        let py = p1.y + dy * t;

        let mut best_px = px;
        let mut best_py = py;
        let mut best_mag = 0.0;

        for step in -r..=r {
            let sx = px + nx * f64::from(step);
            let sy = py + ny * f64::from(step);

            let g = img.sample_gradient_bilinear(sx, sy);
            let mag = g[0] * g[0] + g[1] * g[1];
            if mag > best_mag {
                best_mag = mag;
                best_px = sx;
                best_py = sy;
            }
        }

        if best_mag > 10.0 {
            let mut mags = [0.0f64; 3];
            for (j, offset) in [-1.0, 0.0, 1.0].iter().enumerate() {
                let sx = best_px + nx * offset;
                let sy = best_py + ny * offset;
                let g = img.sample_gradient_bilinear(sx, sy);
                mags[j] = g[0] * g[0] + g[1] * g[1];
            }

            let num = mags[2] - mags[0];
            let den = 2.0 * (mags[0] + mags[2] - 2.0 * mags[1]);
            let sub_offset = if den.abs() > 1e-6 {
                (-num / den).clamp(-0.5, 0.5)
            } else {
                0.0
            };

            let refined_x = best_px + nx * sub_offset;
            let refined_y = best_py + ny * sub_offset;

            sum_d += -(nx * refined_x + ny * refined_y);
            count += 1;
        }
    }

    if count < 3 {
        return None;
    }

    Some((nx, ny, sum_d / f64::from(count)))
}

/// Refine edge position using the unified ERF intensity model and return
/// the line coefficients `(nx, ny, d)` for downstream intersection in
/// `refine_corner`.
///
/// The fitter uses a left-hand normal convention; `refine_corner`'s
/// intersection math is sign-invariant because both sibling lines flip together.
///
/// `fit()` may return false on sample shortfall or low contrast; in that case
/// `line_params()` still holds the geometric normal of p1→p2, which is a
/// safer fallback than `fit_edge_line`'s gradient-peak search.
fn refine_edge_erf(
    arena: &Bump,
    img: &ImageView,
    p1: Point,
    p2: Point,
    sigma: f64,
    decimation: usize,
) -> Option<(f64, f64, f64)> {
    let mut fitter = ErfEdgeFitter::new(img, [p1.x, p1.y], [p2.x, p2.y], true)?;
    let sample_cfg = SampleConfig::for_quad(fitter.edge_len(), decimation);
    let refine_cfg = RefineConfig::quad_style(sigma);
    fitter.fit(arena, &sample_cfg, &refine_cfg);
    Some(fitter.line_params())
}

/// Camera-aware corner refinement for the straight-space extractor.
///
/// The incoming triplet is in *rectified full-res pixel space*. For each
/// of the two edges meeting at `p_rect`, `fit_edge_line_curved` returns a
/// straight line fit in rectified space (the space where the edges truly
/// are lines). We intersect those two lines in rectified space and
/// forward-distort the intersection to get the refined pixel-space corner.
///
/// `p_px` (the current distorted pixel-space corner) is used only for the
/// "stay near the original" sanity check, matching `refine_corner`.
#[cfg(feature = "non_rectified")]
#[must_use]
#[allow(clippy::too_many_arguments)]
fn refine_corner_with_camera<C: crate::camera::CameraModel>(
    img: &ImageView,
    p_rect: Point,
    p_prev_rect: Point,
    p_next_rect: Point,
    p_px: Point,
    decimation: usize,
    intrinsics: &crate::pose::CameraIntrinsics,
    camera: &C,
) -> Point {
    let line1 = fit_edge_line_curved(img, p_prev_rect, p_rect, decimation, intrinsics, camera);
    let line2 = fit_edge_line_curved(img, p_rect, p_next_rect, decimation, intrinsics, camera);

    if let (Some(l1), Some(l2)) = (line1, line2) {
        // Intersect in rectified space, then re-distort to pixel space.
        let det = l1.0 * l2.1 - l2.0 * l1.1;
        if det.abs() > 1e-6 {
            let xr = (l1.1 * l2.2 - l2.1 * l1.2) / det;
            let yr = (l2.0 * l1.2 - l1.0 * l2.2) / det;
            let xn = (xr - intrinsics.cx) / intrinsics.fx;
            let yn = (yr - intrinsics.cy) / intrinsics.fy;
            let [xd, yd] = camera.distort(xn, yn);
            let x = xd * intrinsics.fx + intrinsics.cx;
            let y = yd * intrinsics.fy + intrinsics.cy;
            let dist_sq = (x - p_px.x).powi(2) + (y - p_px.y).powi(2);
            let max_dist = if decimation > 1 {
                (decimation as f64) + 2.0
            } else {
                2.0
            };
            if dist_sq < max_dist * max_dist {
                return Point { x, y };
            }
        }
    }

    p_px
}

/// Curve-aware line fit. Walks along the forward-distorted rectified edge
/// in pixel space, finds the gradient peak along a local normal at each
/// sample, and undistorts each peak back to rectified space. A straight
/// line is then least-squares fit in rectified space (where the edge truly
/// is straight) and returned as `(a, b, c)` with `a*xr + b*yr + c = 0`,
/// `a^2 + b^2 = 1`.
///
/// Fitting in rectified space is what makes this correct: the curved pixel
/// edge only approximates a straight line locally, so a pixel-space fit
/// picks up a systematic inward bias. Rectifying each peak sample removes
/// it.
#[cfg(feature = "non_rectified")]
fn fit_edge_line_curved<C: crate::camera::CameraModel>(
    img: &ImageView,
    p1_rect: Point,
    p2_rect: Point,
    decimation: usize,
    intrinsics: &crate::pose::CameraIntrinsics,
    camera: &C,
) -> Option<(f64, f64, f64)> {
    let dx_r = p2_rect.x - p1_rect.x;
    let dy_r = p2_rect.y - p1_rect.y;
    let len_r = (dx_r * dx_r + dy_r * dy_r).sqrt();
    if len_r < 4.0 {
        return None;
    }

    let n_samples = (len_r as usize).clamp(5, 15);
    let r = if decimation > 1 {
        (decimation as i32) + 1
    } else {
        3
    };

    let fx = intrinsics.fx;
    let fy = intrinsics.fy;
    let cx = intrinsics.cx;
    let cy = intrinsics.cy;
    let fx_over_fy = fx / fy;
    let fy_over_fx = fy / fx;

    let mut moments = crate::gwlf::MomentAccumulator::new();

    for i in 1..=n_samples {
        let t = i as f64 / (n_samples + 1) as f64;
        let rx = p1_rect.x + dx_r * t;
        let ry = p1_rect.y + dy_r * t;
        let xn = (rx - cx) / fx;
        let yn = (ry - cy) / fy;
        let [xd, yd] = camera.distort(xn, yn);
        let px = xd * fx + cx;
        let py = yd * fy + cy;

        let j = camera.distort_jacobian(xn, yn);
        let t_px_x = j[0][0] * dx_r + j[0][1] * dy_r * fx_over_fy;
        let t_px_y = j[1][0] * dx_r * fy_over_fx + j[1][1] * dy_r;
        let t_len = (t_px_x * t_px_x + t_px_y * t_px_y).sqrt();
        if t_len < 1e-6 {
            continue;
        }
        let nx = t_px_y / t_len;
        let ny = -t_px_x / t_len;

        // Window scan along the normal. Cache magnitudes so the parabolic
        // sub-pixel refine can reuse samples that coincide with integer steps.
        let window = (2 * r + 1) as usize;
        let mut mag_buf = [0.0f64; 16];
        let mag_slice = &mut mag_buf[..window];
        let mut best_idx: usize = 0;
        let mut best_mag = 0.0;
        for step in -r..=r {
            let idx = (step + r) as usize;
            let sx = px + nx * f64::from(step);
            let sy = py + ny * f64::from(step);
            let g = img.sample_gradient_bilinear(sx, sy);
            let mag = g[0] * g[0] + g[1] * g[1];
            mag_slice[idx] = mag;
            if mag > best_mag {
                best_mag = mag;
                best_idx = idx;
            }
        }

        if best_mag <= 10.0 {
            continue;
        }

        let step_best = best_idx as i32 - r;
        let best_px = px + nx * f64::from(step_best);
        let best_py = py + ny * f64::from(step_best);

        // Re-use cached neighbors when available; fall back to a fresh sample
        // when the peak sat at an edge of the scan window.
        let m_center = best_mag;
        let m_minus = if best_idx > 0 {
            mag_slice[best_idx - 1]
        } else {
            let g = img.sample_gradient_bilinear(best_px - nx, best_py - ny);
            g[0] * g[0] + g[1] * g[1]
        };
        let m_plus = if best_idx + 1 < window {
            mag_slice[best_idx + 1]
        } else {
            let g = img.sample_gradient_bilinear(best_px + nx, best_py + ny);
            g[0] * g[0] + g[1] * g[1]
        };

        let num = m_plus - m_minus;
        let den = 2.0 * (m_minus + m_plus - 2.0 * m_center);
        let sub_offset = if den.abs() > 1e-6 {
            (-num / den).clamp(-0.5, 0.5)
        } else {
            0.0
        };
        let refined_px = best_px + nx * sub_offset;
        let refined_py = best_py + ny * sub_offset;

        let [xn_r, yn_r] = camera.undistort((refined_px - cx) / fx, (refined_py - cy) / fy);
        moments.add(xn_r * fx + cx, yn_r * fy + cy, 1.0);
    }

    if moments.sum_w < 3.0 {
        return None;
    }

    // TLS line fit in rectified space: normal = eigenvector of the smallest
    // covariance eigenvalue. Fit there, not in pixel space — the pixel edge
    // is curved, so a pixel-space TLS picks up a systematic inward bias.
    let centroid = moments.centroid()?;
    let cov = moments.covariance()?;
    let eig = crate::gwlf::solve_2x2_symmetric(cov[(0, 0)], cov[(0, 1)], cov[(1, 1)]);
    let n_vec = eig.v_min;
    let c = -(n_vec.x * centroid.x + n_vec.y * centroid.y);
    Some((n_vec.x, n_vec.y, c))
}

/// Reducing a polygon to a quad (4 vertices + 1 closing) by iteratively removing
/// the vertex that forms the smallest area triangle with its neighbors.
/// This is robust for noisy/jagged shapes that are approximately quadrilateral.
fn reduce_to_quad<'a>(arena: &'a Bump, poly: &[Point]) -> BumpVec<'a, Point> {
    if poly.len() <= 5 {
        return BumpVec::from_iter_in(poly.iter().copied(), arena);
    }

    // Work on a mutable copy
    let mut current = BumpVec::from_iter_in(poly.iter().copied(), arena);
    // Remove closing point for processing
    current.pop();

    while current.len() > 4 {
        let n = current.len();
        let mut min_area = f64::MAX;
        let mut min_idx = 0;

        for i in 0..n {
            let p_prev = current[(i + n - 1) % n];
            let p_curr = current[i];
            let p_next = current[(i + 1) % n];

            // Triangle area: 0.5 * |x1(y2 - y3) + x2(y3 - y1) + x3(y1 - y2)|
            let area = (p_prev.x * (p_curr.y - p_next.y)
                + p_curr.x * (p_next.y - p_prev.y)
                + p_next.x * (p_prev.y - p_curr.y))
                .abs()
                * 0.5;

            if area < min_area {
                min_area = area;
                min_idx = i;
            }
        }

        // Remove the vertex contributing least to the shape
        current.remove(min_idx);
    }

    // Re-close the loop
    if !current.is_empty() {
        let first = current[0];
        current.push(first);
    }

    current
}

/// Calculate the minimum average gradient magnitude along the 4 edges of the quad.
///
/// Returns the lowest score among the 4 edges. If any edge is very weak,
/// the return value will be low, indicating a likely false positive.
/// Camera-aware edge-score for the straight-space quad extractor.
///
/// `rect_corners` are in **decimated rectified-pixel** space (the output of
/// RDP). For each edge we walk a parametric straight line in that space,
/// forward-distort each sample with `camera` to get the pixel in the real
/// (distorted) `img`, and read the gradient there. This follows the true
/// curved edge in the distorted image instead of the straight-line chord
/// between corners.
#[cfg(feature = "non_rectified")]
fn calculate_edge_score_curved<C: crate::camera::CameraModel>(
    img: &ImageView,
    rect_corners: &[Point; 4],
    camera: &C,
    scaled: ScaledIntrinsics,
    decimation: usize,
) -> f64 {
    let d = decimation as f64;
    let mut min_score = f64::MAX;
    for i in 0..4 {
        let p1 = rect_corners[i];
        let p2 = rect_corners[(i + 1) % 4];
        let dx = p2.x - p1.x;
        let dy = p2.y - p1.y;
        let len = (dx * dx + dy * dy).sqrt();
        if len < 4.0 {
            return 0.0;
        }
        let n_samples = (len as usize).clamp(3, 10);
        let mut edge_mag_sum = 0.0;
        for k in 1..=n_samples {
            let t = k as f64 / (n_samples + 1) as f64;
            let rx = p1.x + dx * t;
            let ry = p1.y + dy * t;
            let xn = (rx - scaled.cx) / scaled.fx;
            let yn = (ry - scaled.cy) / scaled.fy;
            let [xd, yd] = camera.distort(xn, yn);
            let px = (xd * scaled.fx + scaled.cx) * d;
            let py = (yd * scaled.fy + scaled.cy) * d;
            let g = img.sample_gradient_bilinear(px, py);
            edge_mag_sum += (g[0] * g[0] + g[1] * g[1]).sqrt();
        }
        let avg_mag = edge_mag_sum / n_samples as f64;
        if avg_mag < min_score {
            min_score = avg_mag;
        }
    }
    min_score
}

fn calculate_edge_score(img: &ImageView, corners: [Point; 4]) -> f64 {
    let mut min_score = f64::MAX;

    for i in 0..4 {
        let p1 = corners[i];
        let p2 = corners[(i + 1) % 4];

        let dx = p2.x - p1.x;
        let dy = p2.y - p1.y;
        let len = (dx * dx + dy * dy).sqrt();

        if len < 4.0 {
            // Tiny edge, likely noise or degenerate
            return 0.0;
        }

        // Sample points along the edge (excluding corners to avoid corner effects)
        let n_samples = (len as usize).clamp(3, 10); // At least 3, at most 10
        let mut edge_mag_sum = 0.0;

        for k in 1..=n_samples {
            // t goes from roughly 0.1 to 0.9 to avoid corners
            let t = k as f64 / (n_samples + 1) as f64;
            let x = p1.x + dx * t;
            let y = p1.y + dy * t;

            let g = img.sample_gradient_bilinear(x, y);
            edge_mag_sum += (g[0] * g[0] + g[1] * g[1]).sqrt();
        }

        let avg_mag = edge_mag_sum / n_samples as f64;
        if avg_mag < min_score {
            min_score = avg_mag;
        }
    }

    min_score
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::float_cmp, clippy::unwrap_used)]
mod tests {
    use super::*;
    use bumpalo::Bump;
    use proptest::prelude::*;

    #[test]
    fn test_edge_score_rejection() {
        // Create a 20x20 image mostly gray
        let width = 20;
        let height = 20;
        let stride = 20;
        let mut data = vec![128u8; width * height];

        // Draw a weak quad (contrast 10)
        // Center 10,10. Size 8x8.
        // Inside 128, Outside 138. Gradient ~5.
        // 5 < 10, should be rejected.
        for y in 6..14 {
            for x in 6..14 {
                data[y * width + x] = 138;
            }
        }

        let img = ImageView::new(&data, width, height, stride).unwrap();

        let corners = [
            Point { x: 6.0, y: 6.0 },
            Point { x: 14.0, y: 6.0 },
            Point { x: 14.0, y: 14.0 },
            Point { x: 6.0, y: 14.0 },
        ];

        let score = calculate_edge_score(&img, corners);
        // Gradient should be roughly (138-128)/2 = 5 per pixel boundary?
        // Sobel-like (p(x+1)-p(x-1))/2.
        // At edge x=6: left=128, right=138. (138-128)/2 = 5.
        // Magnitude 5.0.
        // Threshold is 10.0.
        assert!(score < 10.0, "Score {score} should be < 10.0");

        // Draw a strong quad (contrast 50)
        // Inside 200, Outside 50. Gradient ~75.
        // 75 > 10, should pass.
        for y in 6..14 {
            for x in 6..14 {
                data[y * width + x] = 200;
            }
        }
        // Restore background
        for y in 0..height {
            for x in 0..width {
                if !(6..14).contains(&x) || !(6..14).contains(&y) {
                    data[y * width + x] = 50;
                }
            }
        }
        let img = ImageView::new(&data, width, height, stride).unwrap();
        let score = calculate_edge_score(&img, corners);
        assert!(score > 40.0, "Score {score} should be > 40.0");
    }

    proptest! {
        #[test]
        fn prop_douglas_peucker_invariants(
            points in prop::collection::vec((0.0..1000.0, 0.0..1000.0), 3..100),
            epsilon in 0.1..10.0f64
        ) {
            let arena = Bump::new();
            let contour: Vec<Point> = points.iter().map(|&(x, y)| Point { x, y }).collect();
            let simplified = douglas_peucker(&arena, &contour, epsilon);

            // 1. Simplified points are a subset of original points (by coordinates)
            for p in &simplified {
                assert!(contour.iter().any(|&op| (op.x - p.x).abs() < 1e-9 && (op.y - p.y).abs() < 1e-9));
            }

            // 2. End points are preserved
            assert_eq!(simplified[0].x, contour[0].x);
            assert_eq!(simplified[0].y, contour[0].y);
            assert_eq!(simplified.last().unwrap().x, contour.last().unwrap().x);
            assert_eq!(simplified.last().unwrap().y, contour.last().unwrap().y);

            // 3. Simplified contour has fewer or equal points
            assert!(simplified.len() <= contour.len());

            // 4. All original points are at most epsilon away from the simplified segment
            for i in 1..simplified.len() {
                let a = simplified[i-1];
                let b = simplified[i];

                // Find indices in original contour matching simplified points
                let mut start_idx = None;
                let mut end_idx = None;
                for (j, op) in contour.iter().enumerate() {
                    if (op.x - a.x).abs() < 1e-9 && (op.y - a.y).abs() < 1e-9 {
                        start_idx = Some(j);
                    }
                    if (op.x - b.x).abs() < 1e-9 && (op.y - b.y).abs() < 1e-9 {
                        end_idx = Some(j);
                    }
                }

                if let (Some(s), Some(e)) = (start_idx, end_idx) {
                    for op in contour.iter().take(e + 1).skip(s) {
                        let d = perpendicular_distance(*op, a, b);
                        assert!(d <= epsilon + 1e-7, "Distance {d} > epsilon {epsilon} at point");
                    }
                }
            }
        }
    }

    // ========================================================================
    // QUAD EXTRACTION ROBUSTNESS TESTS
    // ========================================================================

    use crate::config::TagFamily;
    use crate::segmentation::label_components_with_stats;
    use crate::simd::math::erf_approx;
    use crate::test_utils::{
        TestImageParams, compute_corner_error, generate_test_image_with_params,
    };
    use crate::threshold::ThresholdEngine;

    /// Helper: Generate a tag image and run through threshold + segmentation + quad extraction.
    fn run_quad_extraction(tag_size: usize, canvas_size: usize) -> (Vec<Detection>, [[f64; 2]; 4]) {
        let params = TestImageParams {
            family: TagFamily::AprilTag36h11,
            id: 0,
            tag_size,
            canvas_size,
            ..Default::default()
        };

        let (data, corners) = generate_test_image_with_params(&params);
        let img = ImageView::new(&data, canvas_size, canvas_size, canvas_size).unwrap();

        let arena = Bump::new();
        let engine = ThresholdEngine::new();
        let stats = engine.compute_tile_stats(&arena, &img);
        let mut binary = vec![0u8; canvas_size * canvas_size];
        engine.apply_threshold(&arena, &img, &stats, &mut binary);
        let label_result =
            label_components_with_stats(&arena, &binary, canvas_size, canvas_size, true);
        let detections = extract_quads_fast(&arena, &img, &label_result);

        (detections, corners)
    }

    /// Test quad extraction at varying tag sizes.
    #[test]
    fn test_quad_extraction_at_varying_sizes() {
        let canvas_size = 640;
        let tag_sizes = [32, 48, 64, 100, 150, 200, 300];

        for tag_size in tag_sizes {
            let (detections, _corners) = run_quad_extraction(tag_size, canvas_size);
            let detected = !detections.is_empty();

            if tag_size >= 48 {
                assert!(detected, "Tag size {tag_size}: No quad detected");
            }

            if detected {
                println!(
                    "Tag size {:>3}px: {} quads, center=[{:.1},{:.1}]",
                    tag_size,
                    detections.len(),
                    detections[0].center[0],
                    detections[0].center[1]
                );
            } else {
                println!("Tag size {tag_size:>3}px: No quad detected");
            }
        }
    }

    /// Test corner detection accuracy vs ground truth.
    #[test]
    fn test_quad_corner_accuracy() {
        let canvas_size = 640;
        let tag_sizes = [100, 150, 200, 300];

        for tag_size in tag_sizes {
            let (detections, gt_corners) = run_quad_extraction(tag_size, canvas_size);

            assert!(!detections.is_empty(), "Tag size {tag_size}: No detection");

            let det_corners = detections[0].corners;
            let error = compute_corner_error(&det_corners, &gt_corners);

            let max_error = 5.0;
            assert!(
                error < max_error,
                "Tag size {tag_size}: Corner error {error:.2}px exceeds max"
            );

            println!("Tag size {tag_size:>3}px: Corner error = {error:.2}px");
        }
    }

    /// Test that quad center is approximately correct.
    #[test]
    fn test_quad_center_accuracy() {
        let canvas_size = 640;
        let tag_size = 150;

        let (detections, gt_corners) = run_quad_extraction(tag_size, canvas_size);
        assert!(!detections.is_empty(), "No detection");

        let expected_cx =
            (gt_corners[0][0] + gt_corners[1][0] + gt_corners[2][0] + gt_corners[3][0]) / 4.0;
        let expected_cy =
            (gt_corners[0][1] + gt_corners[1][1] + gt_corners[2][1] + gt_corners[3][1]) / 4.0;

        let det_center = detections[0].center;
        let dx = det_center[0] - expected_cx;
        let dy = det_center[1] - expected_cy;
        let center_error = (dx * dx + dy * dy).sqrt();

        assert!(
            center_error < 2.0,
            "Center error {center_error:.2}px exceeds 2px"
        );

        println!(
            "Quad center: detected=[{:.1},{:.1}], expected=[{:.1},{:.1}], error={:.2}px",
            det_center[0], det_center[1], expected_cx, expected_cy, center_error
        );
    }

    /// Test quad extraction with decimation > 1 to verify center-aware mapping.
    #[test]
    fn test_quad_extraction_with_decimation() {
        let canvas_size = 640;
        let tag_size = 160;
        let decimation = 2;

        let params = TestImageParams {
            family: TagFamily::AprilTag36h11,
            id: 0,
            tag_size,
            canvas_size,
            ..Default::default()
        };

        let (data, gt_corners) = generate_test_image_with_params(&params);
        let img = ImageView::new(&data, canvas_size, canvas_size, canvas_size).unwrap();

        // Manual decimation to match the pipeline
        let new_w = canvas_size / decimation;
        let new_h = canvas_size / decimation;
        let mut decimated_data = vec![0u8; new_w * new_h];
        let decimated_img = img
            .decimate_to(decimation, &mut decimated_data)
            .expect("decimation failed");

        let arena = Bump::new();
        let engine = ThresholdEngine::new();
        let stats = engine.compute_tile_stats(&arena, &decimated_img);
        let mut binary = vec![0u8; new_w * new_h];
        engine.apply_threshold(&arena, &decimated_img, &stats, &mut binary);

        let label_result = label_components_with_stats(&arena, &binary, new_w, new_h, true);

        // Run extraction with decimation=2
        // Refinement image is the full resolution image
        let config = DetectorConfig {
            decimation,
            ..Default::default()
        };
        let detections = extract_quads_with_config(
            &arena,
            &decimated_img,
            &label_result,
            &config,
            decimation,
            &img,
        );

        assert!(!detections.is_empty(), "No quad detected with decimation");

        let det_corners = detections[0].corners;
        let error = compute_corner_error(&det_corners, &gt_corners);

        // Sub-pixel refinement on full-res should keep error very low despite decimation
        assert!(
            error < 2.0,
            "Corner error with decimation: {error:.2}px exceeds 2px"
        );

        println!("Decimated (d={decimation}) corner error: {error:.4}px");
    }

    // ========================================================================
    // SUB-PIXEL CORNER REFINEMENT ACCURACY TESTS
    // ========================================================================

    /// Generate a synthetic image with an anti-aliased vertical edge.
    ///
    /// The edge is placed at `edge_x` (sub-pixel position) using the PSF model:
    /// I(x) = (A+B)/2 + (B-A)/2 * erf((x - edge_x) / σ)
    fn generate_vertical_edge_image(
        width: usize,
        height: usize,
        edge_x: f64,
        sigma: f64,
        dark: u8,
        light: u8,
    ) -> Vec<u8> {
        let mut data = vec![0u8; width * height];
        let a = f64::from(dark);
        let b = f64::from(light);
        let s_sqrt2 = sigma * std::f64::consts::SQRT_2;

        for y in 0..height {
            for x in 0..width {
                // Evaluation at pixel center (Foundation Principle 1)
                let px = x as f64 + 0.5;
                let intensity =
                    f64::midpoint(a, b) + (b - a) / 2.0 * erf_approx((px - edge_x) / s_sqrt2);
                data[y * width + x] = intensity.clamp(0.0, 255.0) as u8;
            }
        }
        data
    }

    /// Generate a synthetic image with an anti-aliased slanted edge (corner region).
    /// Creates two edges meeting at a corner point for refine_corner testing.
    fn generate_corner_image(
        width: usize,
        height: usize,
        corner_x: f64,
        corner_y: f64,
        sigma: f64,
    ) -> Vec<u8> {
        let mut data = vec![0u8; width * height];
        let s_sqrt2 = sigma * std::f64::consts::SQRT_2;

        for y in 0..height {
            for x in 0..width {
                // Foundation Principle 1: Pixel center is at (px+0.5, py+0.5)
                let px = x as f64 + 0.5;
                let py = y as f64 + 0.5;

                // Distance to vertical edge (x = corner_x)
                let dist_v = px - corner_x;
                // Distance to horizontal edge (y = corner_y)
                let dist_h = py - corner_y;

                // In a corner, the closest edge determines the intensity
                // Use smooth transition based on the minimum distance to the two edges
                let signed_dist = if px < corner_x && py < corner_y {
                    // Inside corner: negative distance to nearest edge
                    -dist_v.abs().min(dist_h.abs())
                } else if px >= corner_x && py >= corner_y {
                    // Fully outside
                    dist_v.min(dist_h).max(0.0)
                } else {
                    // On one edge but not the other
                    if px < corner_x {
                        dist_h // Outside in y
                    } else {
                        dist_v // Outside in x
                    }
                };

                // Foundation Principle 2: I(d) = (A+B)/2 + (B-A)/2 * erf(d / (sigma * sqrt(2)))
                let intensity = 127.5 + 127.5 * erf_approx(signed_dist / s_sqrt2);
                data[y * width + x] = intensity.clamp(0.0, 255.0) as u8;
            }
        }
        data
    }

    /// Test that refine_corner achieves sub-pixel accuracy on a synthetic edge.
    ///
    /// This test creates an anti-aliased corner at a known sub-pixel position
    /// and verifies that refine_corner recovers the position within 0.05 pixels.
    #[test]
    fn test_refine_corner_subpixel_accuracy() {
        let arena = Bump::new();
        let width = 60;
        let height = 60;
        let sigma = 0.6; // Default PSF sigma

        // Test multiple sub-pixel offsets
        let test_cases = [
            (30.4, 30.4),   // x=30.4, y=30.4
            (25.7, 25.7),   // x=25.7, y=25.7
            (35.23, 35.23), // x=35.23, y=35.23
            (28.0, 28.0),   // Integer position (control)
            (32.5, 32.5),   // Half-pixel
        ];

        for (true_x, true_y) in test_cases {
            let data = generate_corner_image(width, height, true_x, true_y, sigma);
            let img = ImageView::new(&data, width, height, width).unwrap();

            // Initial corner estimate (round to nearest pixel)
            let init_p = Point {
                x: true_x.round(),
                y: true_y.round(),
            };

            // Previous and next corners along the L-shape
            // For an L-corner at (cx, cy):
            // - p_prev is along the vertical edge (above the corner)
            // - p_next is along the horizontal edge (to the left of the corner)
            let p_prev = Point {
                x: true_x.round(),
                y: true_y.round() - 10.0,
            };
            let p_next = Point {
                x: true_x.round() - 10.0,
                y: true_y.round(),
            };

            let refined = refine_corner(&arena, &img, init_p, p_prev, p_next, sigma, 1, true);

            let error_x = (refined.x - true_x).abs();
            let error_y = (refined.y - true_y).abs();
            let error_total = (error_x * error_x + error_y * error_y).sqrt();

            println!(
                "Corner ({:.2}, {:.2}): refined=({:.4}, {:.4}), error=({:.4}, {:.4}), total={:.4}px",
                true_x, true_y, refined.x, refined.y, error_x, error_y, error_total
            );

            // Assert sub-pixel accuracy < 0.1px (relaxed from 0.05 for robustness)
            // The ideal is <0.05px but real-world noise and edge cases may require relaxation
            assert!(
                error_total < 0.15,
                "Corner ({true_x}, {true_y}): error {error_total:.4}px exceeds 0.15px threshold"
            );
        }
    }

    /// Test refine_corner on a simple vertical edge to verify edge localization.
    #[test]
    fn test_refine_corner_vertical_edge() {
        let arena = Bump::new();
        let width = 40;
        let height = 40;
        let sigma = 0.6;

        // Test vertical edge at x=20.4
        let true_edge_x = 20.4;
        let data = generate_vertical_edge_image(width, height, true_edge_x, sigma, 0, 255);
        let img = ImageView::new(&data, width, height, width).unwrap();

        // Set up a corner where two edges meet
        // For a pure vertical edge test, we'll use a simple L-corner configuration
        let corner_y = 20.0;
        let init_p = Point {
            x: true_edge_x.round(),
            y: corner_y,
        };
        let p_prev = Point {
            x: true_edge_x.round(),
            y: corner_y - 10.0,
        };
        let p_next = Point {
            x: true_edge_x.round() - 10.0,
            y: corner_y,
        };

        let refined = refine_corner(&arena, &img, init_p, p_prev, p_next, sigma, 1, true);

        // The x-coordinate should be refined to near the true edge position
        // y-coordinate depends on the horizontal edge (which doesn't exist in this test)
        let error_x = (refined.x - true_edge_x).abs();

        println!(
            "Vertical edge x={:.2}: refined.x={:.4}, error={:.4}px",
            true_edge_x, refined.x, error_x
        );

        // Vertical edge localization should be very accurate
        assert!(
            error_x < 0.1,
            "Vertical edge x={true_edge_x}: error {error_x:.4}px exceeds 0.1px threshold"
        );
    }
}

#[multiversion(targets(
    "x86_64+avx2+bmi1+bmi2+popcnt+lzcnt",
    "x86_64+avx512f+avx512bw+avx512dq+avx512vl",
    "aarch64+neon"
))]
/// Boundary Tracing using robust border following.
///
/// This implementation uses a state-machine based approach to follow the border
/// of a connected component. Uses precomputed offsets for speed.
fn trace_boundary<'a>(
    arena: &'a Bump,
    labels: &[u32],
    width: usize,
    height: usize,
    start_x: usize,
    start_y: usize,
    target_label: u32,
) -> BumpVec<'a, Point> {
    let mut points = BumpVec::new_in(arena);

    // Precompute offsets for Moore neighborhood (CW order starting from Top)
    // This avoids repeated multiplication in the hot loop
    let w = width as isize;
    let offsets: [isize; 8] = [
        -w,     // 0: T
        -w + 1, // 1: TR
        1,      // 2: R
        w + 1,  // 3: BR
        w,      // 4: B
        w - 1,  // 5: BL
        -1,     // 6: L
        -w - 1, // 7: TL
    ];

    // Direction deltas for bounds checking
    let dx: [isize; 8] = [0, 1, 1, 1, 0, -1, -1, -1];
    let dy: [isize; 8] = [-1, -1, 0, 1, 1, 1, 0, -1];

    let mut curr_x = start_x as isize;
    let mut curr_y = start_y as isize;
    let mut curr_idx = start_y * width + start_x;
    let mut walk_dir = 2usize; // Initial: move Right

    for _ in 0..10000 {
        points.push(Point {
            x: curr_x as f64 + 0.5,
            y: curr_y as f64 + 0.5,
        });

        let mut found = false;
        let search_start = (walk_dir + 6) % 8;

        for i in 0..8 {
            let dir = (search_start + i) % 8;
            let nx = curr_x + dx[dir];
            let ny = curr_y + dy[dir];

            // Branchless bounds check using unsigned comparison
            if (nx as usize) < width && (ny as usize) < height {
                let nidx = (curr_idx as isize + offsets[dir]) as usize;
                if labels[nidx] == target_label {
                    curr_x = nx;
                    curr_y = ny;
                    curr_idx = nidx;
                    walk_dir = dir;
                    found = true;
                    break;
                }
            }
        }

        if !found || (curr_x == start_x as isize && curr_y == start_y as isize) {
            break;
        }
    }

    points
}

/// Simplified version of CHAIN_APPROX_SIMPLE:
/// Removes all redundant points on straight lines.
pub(crate) fn chain_approximation<'a>(arena: &'a Bump, points: &[Point]) -> BumpVec<'a, Point> {
    if points.len() < 3 {
        let mut v = BumpVec::new_in(arena);
        v.extend_from_slice(points);
        return v;
    }

    let mut result = BumpVec::new_in(arena);
    result.push(points[0]);

    for i in 1..points.len() - 1 {
        let p_prev = points[i - 1];
        let p_curr = points[i];
        let p_next = points[i + 1];

        let dx1 = p_curr.x - p_prev.x;
        let dy1 = p_curr.y - p_prev.y;
        let dx2 = p_next.x - p_curr.x;
        let dy2 = p_next.y - p_curr.y;

        // If directions are strictly different, it's a corner
        // Using exact float comparison is safe here because these are pixel coordinates (integers)
        if (dx1 * dy2 - dx2 * dy1).abs() > 1e-6 {
            result.push(p_curr);
        }
    }

    result.push(*points.last().unwrap_or(&points[0]));
    result
}