BREP_kernel 0.16.0

A boundary representation (BREP) geometry kernel for building CAD applications.
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
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
//! Is a constructed result SOUND enough to return — and if not, can it be
//! repaired rather than refused?
//!
//! `validate()` is an incidence test and says nothing about a body passing
//! through itself. [`crate::solid_self_intersections`] does, in two forms: two
//! faces CROSSING, and one face FOLDING through itself. Neither changes the
//! volume in a way a closed-form oracle can see — a shape can enclose exactly
//! the right amount of space while passing through itself — so a lane that
//! returns one reports success for a different result. That is the failure
//! mode this module exists to stop.
//!
//! ## Repair first, refuse second
//!
//! A self-intersection is not automatically a dead end. Where two faces of one
//! body cross along a curve and the material beyond that curve is a sliver,
//! the answer is to SPLIT both faces along their carriers' exact intersection
//! and drop the pieces that should not be there —
//! [`resolve_face_crossing`]. Only when that cannot be bounded does the lane
//! refuse.
//!
//! The drop is closed under one rule — a face left holding a boundary no other
//! face answers for bordered the dropped material — and that rule has two
//! answers. DROPPING the face is right when the unanswered boundary is the
//! whole of what it contributed: a tail beside a tail. RE-TRIMMING it — taking
//! the unanswered coedges off and re-linking what is left into closed loops —
//! is right when the body keeps the face and only part of its boundary went,
//! which is the only way a BREAKOUT closes. A bore carried out through a side
//! wall leaves the mouth flat holding the stretch of a corner edge that now
//! runs INSIDE the bore and the arc of its own mouth ring that now runs
//! OUTSIDE the body; dropping the flat is absurd, and taking those two coedges
//! off it merges its mouth loop into its outer loop, which is the topology a
//! breakout has. The enumeration runs under the dropping closure first and
//! reaches the re-trimming one only where that resolved nothing at all, so the
//! second can turn a refusal into a repair and can never change a repair this
//! module already made.
//!
//! A FOLD is different and is refused outright here. A face folded through
//! itself is a TRIM that was never constructed: the answer is to carve the
//! folded part of the trim away at the point the carrier is built
//! (`offset/carve.rs`), not to repair the finished body. Refusing it by name
//! is what sends the caller to the lane that can fix it.
//!
//! ## A hole that left its face
//!
//! The scan reads the body as a SURFACE: it convicts two faces that cross and
//! one face that folds. Neither describes a face whose HOLE loop has wandered
//! off the face it is a hole in — the trim is nonsense but nothing crosses,
//! because the wall the hole opens into has gone with it. A direct edit that
//! carries a bore clean off the body builds exactly that: `validate()` is an
//! incidence test and passes, the scan finds no pair to convict, and the
//! divergence-theorem volume of a cylinder is blind to where its axis sits, so
//! the wrong body measures right to the last bit. [`refuse_holes_leaving_their_faces`]
//! is the floor for it, and it runs LAST — after the repair, on whatever body
//! the repair left — so a crossing the repair can take is still taken.
//!
//! ## And a hole that left its face ONTO A NEIGHBOUR
//!
//! That floor convicts two different things, and only one of them has no right
//! answer. A bore dragged CLEAN OFF the body has none: the carrier misses the
//! solid, and there is nothing to cut at any extent. A bore dragged so that its
//! exit mouth crosses the edge between the face it opened through and the face
//! next door has one, and it is the body the crossing repair already builds for
//! a bore carried out through a side wall — the moved carrier and the
//! neighbour split along their exact section, the overshoot dropped, and the
//! faces left holding an unanswered boundary RE-TRIMMED, which merges the mouth
//! into the outer loops of the two faces it now opens through.
//!
//! [`accept_or_break_out`] tries that before refusing, and the two cases are
//! told apart by GEOMETRY rather than by a threshold: the convicted hole loop
//! is traced in its own face's parameter plane and asked which of that face's
//! outer coedges it crosses ([`breakout_pair`]). A hole that has broken out
//! onto a neighbour crosses one, and the face across that edge is the one to
//! re-trim against. A hole that has left the body altogether crosses none, and
//! there is no second face for the repair to be about — so the runaway refuses
//! before a single face is split, and it refuses with the floor's own words
//! plus the repair's reason for declining.
//!
//! The extent is DERIVED and not invented, which is what makes this the same
//! repair rather than a new operation with a new input. The tool is the moved
//! carrier trimmed against the body, and where the carrier meets the body is
//! what the imprint computes. Measured on the 2026-09-21 exit-hole report: the
//! body this builds removes 4195.389267765 from the filled wedge, against
//! 4195.389268713 for the same wedge with the INFINITE moved carrier
//! subtracted through the boolean and 4195.389267095 for a slice integration
//! that never enters the kernel — three roads, 1.6e-9 apart.
//!
//! ## The order
//!
//! Three separate slices built [`accept_sound`]'s sequence on 2026-09-21 and
//! none of them saw the whole of it, so here it is end to end:
//!
//! 1. SCAN once. One tessellation, one BVH sweep; every verdict below is read
//!    off that single reading.
//! 2. NOT FLAGGED — [`accept_or_break_out`]: the hole-containment floor runs on
//!    the body as built, and the body is returned when it says nothing. When it
//!    convicts, the BREAKOUT is tried before the refusal — the same split and
//!    re-trim step 4 runs, on a pair the floor names instead of one the scan
//!    confirmed — and only a breakout that cannot be built refuses. This is the
//!    ONLY path a bore carried clear of its faces reaches, because a bore that
//!    has left the solid crosses nothing.
//! 3. FLAGGED — the body is dumped under `BREP_HEAL_DUMP`, and a FOLD refuses
//!    here and now: a fold is a trim that was never constructed, not material
//!    to drop.
//! 4. A CROSSING goes to [`resolve_face_crossing`], which returns the body
//!    UNCHANGED when nothing is confirmed, refuses a fold and refuses more than
//!    one crossing pair, and otherwise imprints the two carriers' section onto
//!    both faces and enumerates every way of dropping the resulting pieces —
//!    closed under `Drop` first, and under `Retrim` only where that resolved
//!    nothing at all.
//! 5. The repaired body is SCANNED AGAIN, and a repair that still
//!    self-intersects refuses, naming the original crossing.
//! 6. [`accept_or_break_out`] AGAIN, on the repaired body, so a re-trim that
//!    left a hole off its face is broken out in turn or refused, rather than
//!    shipped.
//!
//! Steps 4 and 6 bound each other, and the boundary is not a gap. The crossing
//! repair needs a section to split along, and a section exists exactly while
//! the moved carrier still MEETS the body; the containment floor decides what
//! is left. Measured on the 2026-09-21 runaway-bore report, the crossing
//! regime ends and the carrier's clearance turns positive in the SAME step of
//! a swept drag, and 13.90 mm past that the repair is handed a body with
//! nothing to imprint and hands it straight back.
//!
//! What step 2's breakout adds is the OTHER side of that boundary: the window
//! where the mouth has crossed its face's edge but the wall's overshoot is
//! still a sliver the tessellation cannot resolve, so the scan confirms
//! nothing and step 4 is never entered. On the reported document that window
//! is `s = 0.99 … 1.03` of the drag, and the scan takes over at `s = 1.04`.
//! The two routes reach the same [`split_and_drop`] and the repair is one
//! repair.
//!
//! ## Cost
//!
//! One tessellation and one BVH sweep per accepted result, and only on the
//! success path. Both verdicts come off that one scan. The containment floor
//! traces the loops of PLANAR faces that have more than one, in their own
//! parameters; a face with a single loop costs nothing.
//!
//! That note used to stop at that sentence, and stopping there was
//! misleading: the floor's cost was never in how MANY faces qualify but in
//! how LARGE one qualifying face's loops can be. A loop is traced at 32
//! stations per KNOT SPAN, so a box's face gives four points and an imported
//! 100-tooth gear's flat gives 1.1 MILLION; testing every hole point against
//! every one of those is a PRODUCT, and three such faces out of 1004 cost 24
//! s of a 30 s replay. The trace is unchanged — decimating it would weaken
//! the predicate — and the product is what went instead: the outer loop is
//! indexed once into bands of its own `v` ([`BoundaryBands`]) and each hole
//! point reads only the band it falls in, for the same answer at `hole
//! points × segments per band`.

use crate::{KernelRefusal, KernelStage, OrRefuse, SoundnessDefect};
use crate::topology::{BrepSolid, LoopRecord};
use crate::{solid_self_intersections, SelfIntersectionOptions, SelfIntersectionReport};

/// Scan a finished result and either accept it, repair it, or refuse it by
/// name.
///
/// `entry` is the lane's own name, so the refusal reads as that lane's
/// refusal rather than as a soundness module's.
pub fn accept_sound(solid: BrepSolid, entry: &str) -> Result<BrepSolid, KernelRefusal> {
    let report = scan(&solid, entry)?;
    if !report.is_flagged() {
        return accept_or_break_out(solid, entry, &report);
    }
    dump_flagged(&solid, entry);
    if let Some(refusal) = fold_refusal(&report, entry) {
        return Err(refusal);
    }
    // Two faces crossing: try to split and trim before refusing.
    let repaired = match resolve_face_crossing(&solid) {
        Ok(repaired) => repaired,
        // The crossing lane declined. Where the containment floor ALSO convicts
        // a hole loop that has left its face, this body may be a BREAKOUT the
        // other route can take — a mouth that has migrated onto a neighbouring
        // face crosses that neighbour, so the scan flags it, and the pair the
        // scan picked is not always the pair the re-trim needs.
        //
        // Guarded three ways, because a second chance is exactly how a body
        // that should refuse gets talked into building. The floor's conviction
        // is the ticket — a body that merely self-intersects never reaches
        // here. The success path above is untouched, so a body that repairs
        // today repairs identically. And the result goes through the same
        // re-scan and the same floor below as any other repair.
        Err(reason) if first_hole_breach(&solid).is_some() => {
            match break_out_until_contained(solid.clone(), entry) {
                Ok(repaired) => repaired,
                Err(second) => {
                    return Err(crossing_refusal(
                        &report,
                        entry,
                        &format!(
                            "{reason}. The breakout route was offered it too and declined: \
                             {second}"
                        ),
                    ))
                }
            }
        }
        Err(reason) => return Err(crossing_refusal(&report, entry, &reason.message)),
    };
    let after = scan(&repaired, entry)?;
    if after.is_flagged() {
        return Err(crossing_refusal(
            &report,
            entry,
            &format!(
                "the repair ran and the result still self-intersects — {}",
                after.summary().unwrap_or_else(|| "no summary".into())
            ),
        ));
    }
    accept_or_break_out(repaired, entry, &after)
}

/// How many hole-containment breaches one acceptance may break out, before it
/// stops and refuses.
///
/// A bore has TWO mouths, and a motion big enough to walk one onto the next
/// face can walk the other too — so one is not enough, and the whole point of a
/// bound is that a repair which does not actually reduce the breaches cannot
/// spin. Four leaves room for a two-mouth migration and a re-trim that opens
/// one more, and stops well short of a body being talked into building by
/// attrition.
const MAX_BREAKOUT_ROUNDS: usize = 4;

/// The containment floor, with the BREAKOUT tried before the refusal.
///
/// A hole loop that has left its face is not one case but two, and they are
/// told apart by where it went. A hole that has left ONTO ANOTHER FACE has a
/// right answer the kernel can build — the moved carrier re-trimmed against the
/// face it now opens through — and this builds it when the mouth STRADDLES its
/// face's boundary, which is the reported case. A hole that has left the body
/// has no such answer, and this refuses exactly as the floor always did, with
/// the repair's own reason for declining carried after the floor's so the
/// refusal says which of the two it was.
///
/// **A mouth that has migrated WHOLLY off its face is built by the CROSSING
/// LANE, not by this route.** Until 2026-09-22 it was built by neither: the
/// departed face kept a hole loop the imprint never cut and the drop
/// enumeration had no move for retiring one, so every candidate face this
/// route offered the carrier to declined. The drop closures have that move now
/// — [`fragment_is_detached`] and [`holds_only_stale_loops`] — and a turned
/// bore whose exit migrates onto the next face is repaired to its closed form
/// through [`resolve_face_crossing`], which `accept_sound` reaches first.
///
/// What is left for this route to decide is the mouth that has arrived on
/// NOTHING: a carrier clear of the body cuts no face of it, every candidate
/// declines, and the refusal names each face it tried and the distance it
/// tried it at. That is measured, not assumed, by
/// `a_mouth_that_has_left_the_body_reaches_the_migration_route_and_is_refused_by_name`.
fn accept_or_break_out(solid: BrepSolid, entry: &str, report: &SelfIntersectionReport) -> Result<BrepSolid, KernelRefusal> {
    if refuse_holes_leaving_their_faces(&solid, entry).is_ok() {
        // accept_refuses_undecided_crossing_20261016: contained, and a scan
        // that ended undecided is still not a clean one. A hole that left its
        // face is named by the floor below first: that is the more specific
        // diagnosis, and its breakout re-scans what it builds.
        if let Some(refusal) = undecided_refusal(report, entry) {
            return Err(refusal);
        }
        return Ok(solid);
    }
    break_out_until_contained(solid, entry)
}

/// Break out every hole the containment floor convicts, one at a time, and hand
/// back a body that BOTH scans clean and passes the floor.
///
/// The rounds are what a two-mouth migration needs: repairing one mouth leaves
/// the other still off its face, and — while it is — the body legitimately
/// still passes through itself, because the second mouth's wall is still
/// standing where the material is not. So an intermediate round is allowed to
/// be flagged; the END of it is not. Nothing is returned that would not have
/// been accepted had it been built that way in the first place.
fn break_out_until_contained(solid: BrepSolid, entry: &str) -> Result<BrepSolid, KernelRefusal> {
    let mut current = solid;
    for _ in 0..MAX_BREAKOUT_ROUNDS {
        let Some(breach) = first_hole_breach(&current) else {
            // Contained. It still has to be SOUND to be returned.
            let report = scan_output(&current, entry)?;
            if report.is_flagged() {
                return Err(KernelRefusal::unsound(
                    KernelStage::Validate,
                    SoundnessDefect::Crossing,
                    crossing_faces(&report),
                    format!(
                        "{entry}: the breakout repair built a body and it SELF-INTERSECTS — {}",
                        report.summary().unwrap_or_else(|| "no summary".into()),
                    ),
                ));
            }
            return Ok(current);
        };
        // The floor's refusal is the primary one; the breakout's decline is
        // context appended to it, and the class stays the floor's.
        let floor = hole_breach_refusal(&current, &breach, entry);
        current = repair_hole_breakout(&current, &breach).map_err(|reason| {
            floor.with_message(|floor| {
                format!("{floor}. The breakout repair was offered this body and declined it: {reason}")
            })
        })?;
    }
    let breach = first_hole_breach(&current).expect("the loop only falls through on a breach");
    Err(
        hole_breach_refusal(&current, &breach, entry).with_message(|floor| {
            format!(
                "{floor}. {MAX_BREAKOUT_ROUNDS} breakouts were built and a hole is still off its face, so \
                 this stops rather than repairing a body into one nobody asked for"
            )
        }),
    )
}

/// [`accept_sound`] for a lane whose INPUTS may themselves self-intersect.
///
/// A boolean of two bodies that already pass through themselves is entitled to
/// return a result that does: the defect came in with the operand and refusing
/// it here would blame the wrong operation. What must never pass is a crossing
/// the operation ITSELF introduced, so the inputs are scanned only when the
/// output is flagged — the common case pays for one scan, not three.
pub fn accept_sound_against(
    inputs: &[&BrepSolid],
    solid: BrepSolid,
    entry: &str,
) -> Result<BrepSolid, KernelRefusal> {
    let report = scan_output(&solid, entry)?;
    if !report.is_flagged() {
        return Ok(solid);
    }
    for input in inputs {
        let before = scan(input, entry)?;
        if before.is_flagged() {
            // Said plainly rather than silently: the operand arrived broken.
            return Ok(solid);
        }
    }
    accept_sound(solid, entry)
}

/// [`accept_sound_against`] reading only the faces in `candidates`: the faces
/// the operation built or changed, their neighbours, and every face whose box
/// reaches the other operand. A face outside that set is an operand's own,
/// untouched, and lies clear of everything the operation added, so a crossing
/// on it was the operand's and passes through exactly as the full scan passes
/// it (the inputs are scanned, flagged, and the result returned). An empty
/// set is a result with nothing to scan. A flagged subset hands the whole
/// result to the full acceptance, so every repair and refusal reads as
/// before. (`BooleanOptions::untouched_passthrough`)
pub fn accept_sound_against_scoped(
    inputs: &[&BrepSolid],
    solid: BrepSolid,
    entry: &str,
    candidates: &rustc_hash::FxHashSet<u64>,
) -> Result<BrepSolid, KernelRefusal> {
    if candidates.is_empty() {
        return Ok(solid);
    }
    let faces: Vec<crate::topology::FaceRecord> = solid
        .shells
        .iter()
        .flat_map(|shell| &shell.faces)
        .filter(|face| candidates.contains(&face.id))
        .cloned()
        .collect();
    if faces.len() == solid.shells.iter().map(|shell| shell.faces.len()).sum::<usize>() {
        return accept_sound_against(inputs, solid, entry);
    }
    let edge_ids: rustc_hash::FxHashSet<u64> = faces
        .iter()
        .flat_map(|face| &face.loops)
        .flat_map(|loop_record| &loop_record.coedges)
        .map(|coedge| coedge.edge_id)
        .collect();
    let edges: Vec<crate::topology::EdgeRecord> =
        solid.edges.iter().filter(|edge| edge_ids.contains(&edge.id)).cloned().collect();
    let vertex_ids: rustc_hash::FxHashSet<u64> =
        edges.iter().flat_map(|edge| [edge.start_vertex_id, edge.end_vertex_id]).collect();
    let subset = BrepSolid {
        mass_properties_cache: Default::default(),
        cells: Default::default(),
        id: solid.id,
        vertices: solid.vertices.iter().filter(|vertex| vertex_ids.contains(&vertex.id)).cloned().collect(),
        edges,
        shells: vec![crate::topology::ShellRecord { id: 1, faces }],
        genus: 0,
        kind: crate::BodyKind::Solid,
    };
    // The bands come from the whole result, as the full scan derives them.
    let options = SelfIntersectionOptions::for_solid(&solid);
    let report = scan_with(&subset, options, entry)?;
    if let Some(refusal) = undecided_refusal(&report, entry) {
        return Err(refusal);
    }
    if !report.is_flagged() {
        return Ok(solid);
    }
    accept_sound_against(inputs, solid, entry)
}

/// The scan, with a read failure reported as a refusal rather than swallowed:
/// a result that could not be scanned is not a result that scanned clean.
fn scan(solid: &BrepSolid, entry: &str) -> Result<SelfIntersectionReport, KernelRefusal> {
    scan_with(solid, SelfIntersectionOptions::for_solid(solid), entry)
}

// BREP named hunk: begin accept_refuses_undecided_crossing_20261016
/// Opt-in (`BREP_SOUNDNESS_REFUSE_UNDECIDED=1`), see `resolve::refuse_enabled`.
/// A RESULT whose scan ends undecided is not a result that scanned clean: the
/// mesh confirmation could not decide some candidate crossings and the carriers
/// did not settle them either (`brep/soundness/resolve.rs`). It is refused,
/// naming the face pairs and why each stayed open. A report that is flagged is
/// left to the crossing and fold refusals, which say what is wrong.
fn undecided_refusal(report: &SelfIntersectionReport, entry: &str) -> Option<KernelRefusal> {
    if !crate::soundness::resolve::enabled() || !crate::soundness::resolve::refuse_enabled() {
        return None;
    }
    undecided_refusal_message(report, entry)
}

/// The refusal itself, whatever the switch: `None` for a flagged or a clean report.
fn undecided_refusal_message(report: &SelfIntersectionReport, entry: &str) -> Option<KernelRefusal> {
    if report.is_flagged() || report.undecided == 0 {
        return None;
    }
    let pairs: Vec<String> = report
        .undecided_pairs
        .iter()
        .map(|pair| {
            let cause = match pair.cause {
                crate::CrossingUndecidedCause::Witness => "the mesh crossing's witness is not a point both faces own",
                crate::CrossingUndecidedCause::Pierce => "a projection or a normal could not be evaluated",
                crate::CrossingUndecidedCause::MissingFace => "a triangle's face is not in the body",
            };
            format!("faces {} and {} near ({:.4}, {:.4}, {:.4}): {cause}", pair.face_a, pair.face_b, pair.point.x, pair.point.y, pair.point.z)
        })
        .collect();
    let more = report.undecided.saturating_sub(pairs.len());
    Some(KernelRefusal::non_convergence(
        KernelStage::Validate,
        "self_intersection_undecided",
        format!(
            "{entry}: the soundness scan could not decide {} candidate crossing(s), and the exact carriers \
             did not settle them either, so the result is not known to be free of self-intersection: {}{}",
            report.undecided,
            pairs.join("; "),
            if more > 0 { format!(" (and {more} more)") } else { String::new() },
        ),
    ))
}

/// [`scan`] for a RESULT about to be returned: an undecided scan refuses.
fn scan_output(solid: &BrepSolid, entry: &str) -> Result<SelfIntersectionReport, KernelRefusal> {
    let report = scan(solid, entry)?;
    match undecided_refusal(&report, entry) {
        Some(refusal) => Err(refusal),
        None => Ok(report),
    }
}
// BREP named hunk: end accept_refuses_undecided_crossing_20261016

fn scan_with(
    solid: &BrepSolid,
    options: SelfIntersectionOptions,
    entry: &str,
) -> Result<SelfIntersectionReport, KernelRefusal> {
    solid_self_intersections(solid, options).map_err(|message| {
        KernelRefusal::internal(
            KernelStage::Validate,
            "soundness_scan",
            format!("{entry}: the soundness scan could not run: {message}"),
        )
    })
}

/// The faces the scan's confirmed crossings name, each once, in report order.
fn crossing_faces(report: &SelfIntersectionReport) -> Vec<u64> {
    let mut faces = Vec::new();
    for crossing in &report.confirmed {
        for face in [crossing.face_a, crossing.face_b] {
            if !faces.contains(&face) {
                faces.push(face);
            }
        }
    }
    faces
}

/// The refusal for a face folded through itself — named, with both feet, the
/// point and the margins, and pointing at the lane that owns the fix.
fn fold_refusal(report: &SelfIntersectionReport, entry: &str) -> Option<KernelRefusal> {
    let fold = report.folds.first()?;
    let faces = report.folds.iter().map(|fold| fold.face).collect();
    let message = format!(
        "{entry}: the result's face {}{} FOLDS THROUGH ITSELF in 3D — its surface carries \
         ({:.6}, {:.6}) and ({:.6}, {:.6}), {:.3e} apart in its own domain and both inside \
         its trim, to the same point ({:.4}, {:.4}, {:.4}) (residual {:.3e}, branches {:.1} \
         degrees apart){}. The face is wrong in AREA where its volume is not, so nothing that \
         measures volume notices and `validate()` cannot see it at all. A fold is a trim that \
         was never constructed: the folded part of the parameter region has to be carved away \
         where the carrier is built, not repaired out of the finished body.",
        fold.face,
        fold.face_name
            .as_ref()
            .map(|name| format!(" '{name}'"))
            .unwrap_or_default(),
        fold.uv_a[0],
        fold.uv_a[1],
        fold.uv_b[0],
        fold.uv_b[1],
        fold.uv_separation,
        fold.point.x,
        fold.point.y,
        fold.point.z,
        fold.residual,
        fold.normal_angle.to_degrees(),
        if report.folds.len() > 1 {
            format!(" ({} folded faces in all)", report.folds.len())
        } else {
            String::new()
        },
    );
    Some(KernelRefusal::unsound(
        KernelStage::Validate,
        SoundnessDefect::Fold,
        faces,
        message,
    ))
}

/// The refusal for two faces crossing, naming every pair and why the repair
/// did not take it.
fn crossing_refusal(report: &SelfIntersectionReport, entry: &str, reason: &str) -> KernelRefusal {
    let pairs = report
        .confirmed
        .iter()
        .map(|crossing| {
            format!(
                "faces {} and {} near ({:.4}, {:.4}, {:.4}) (straddle {:.3e} against a band of {:.1e})",
                crossing.face_a,
                crossing.face_b,
                crossing.point.x,
                crossing.point.y,
                crossing.point.z,
                crossing.straddle,
                report.options.straddle_band
            )
        })
        .collect::<Vec<_>>()
        .join("; ");
    KernelRefusal::unsound(
        KernelStage::Validate,
        SoundnessDefect::Crossing,
        crossing_faces(report),
        format!(
            "{entry}: the result SELF-INTERSECTS — {pairs}. The enclosed space can still measure \
             right, so neither a volume oracle nor `validate()` would have caught this. The repair \
             that splits both faces along their carriers' intersection and drops the enclosed \
             material did not take it: {reason}"
        ),
    )
}

// ---------------------------------------------------------------------------
// The hole-containment floor
// ---------------------------------------------------------------------------

/// Stations per curved pcurve span when a loop is traced in its face's own
/// parameters. A straight pcurve contributes its start point only — the next
/// coedge begins where it ends.
const LOOP_SAMPLES_PER_SPAN: usize = 32;

/// A loop of a face as a closed polyline in that face's own parameter plane,
/// traced coedge by coedge along each pcurve (already oriented to its coedge).
fn loop_parameter_polyline(loop_record: &LoopRecord) -> Result<Vec<(f64, f64)>, KernelRefusal> {
    pcurve_polyline(loop_record.coedges.iter().map(|coedge| &coedge.pcurve))
}

/// The same walk for a FRAGMENT's loop, which carries the same pcurves under a
/// different record. One sampler, so a loop traced during the repair and the
/// same loop traced by the containment floor are the same polyline.
fn fragment_loop_polyline(
    fragment_loop: &crate::fragment::FragmentLoop,
) -> Result<Vec<(f64, f64)>, KernelRefusal> {
    pcurve_polyline(fragment_loop.coedges.iter().map(|coedge| &coedge.pcurve))
}

fn pcurve_polyline<'a>(
    pcurves: impl Iterator<Item = &'a crate::NurbsCurve>,
) -> Result<Vec<(f64, f64)>, KernelRefusal> {
    let mut points = Vec::new();
    for pcurve in pcurves {
        let [d0, d1] = pcurve.domain().or_refuse(KernelStage::Validate, "domain")?;
        let steps = if pcurve.degree <= 1 && pcurve.control_points.len() == 2 {
            1
        } else {
            let mut spans = 0usize;
            for window in pcurve.knots.windows(2) {
                if window[1] > window[0] {
                    spans += 1;
                }
            }
            spans.max(1) * LOOP_SAMPLES_PER_SPAN
        };
        for step in 0..steps {
            let uv = pcurve.evaluate(d0 + (d1 - d0) * (step as f64 / steps as f64)).or_refuse(KernelStage::Validate, "evaluate")?;
            points.push((uv.x, uv.y));
        }
    }
    Ok(points)
}

fn polyline_area(points: &[(f64, f64)]) -> f64 {
    let mut twice = 0.0;
    for index in 0..points.len() {
        let (x0, y0) = points[index];
        let (x1, y1) = points[(index + 1) % points.len()];
        twice += x0 * y1 - x1 * y0;
    }
    0.5 * twice
}

/// The outer loop's segments, bucketed by the band of `v` each one spans.
///
/// The floor asks a hole point two questions — even-odd parity against the
/// outer loop, and whether the point sits within `tolerance` of it — and both
/// read ONLY the segments whose own `v` range reaches the point's `v`. A
/// segment that does not reach it has both endpoints on the same side of the
/// horizontal ray, so it cannot toggle the parity; and every point of it is
/// further away than `tolerance` in `v` alone, so it cannot be the witness
/// that the point is on the boundary. Answering out of the band is therefore
/// the SAME answer as walking the whole loop, not an approximation of it: the
/// set of segments that can toggle is identical, parity is an XOR and does not
/// care in what order they come, and the nearest segment — if there is one
/// within `tolerance` at all — is among those searched.
///
/// What changes is the COST. The brute-force walk is `hole points × outer
/// points`, which on an imported 100-tooth gear's flat is 1.1 million outer
/// stations against 11 904 hole stations — 1.3e10 pairs on ONE face, 24 s of a
/// 30 s replay. Through the bands it is `hole points × segments per band`,
/// plus one linear pass to build.
///
/// Laid out CSR — one offsets array and one flat segment array — because a
/// loop of a million points would otherwise want a million `Vec` headers.
struct BoundaryBands<'a> {
    points: &'a [(f64, f64)],
    /// The bottom of the first band: the loop's lowest `v`.
    v_lo: f64,
    /// Bands per unit of `v`. Zero when the loop has no `v` extent to divide,
    /// in which case there is one band and everything lands in it.
    bands_per_v: f64,
    /// `starts[band] .. starts[band + 1]` indexes `segments`.
    starts: Vec<u32>,
    /// Segment `i` is `points[i] -> points[(i + 1) % len]`, the closing
    /// segment included.
    segments: Vec<u32>,
}

impl<'a> BoundaryBands<'a> {
    fn build(points: &'a [(f64, f64)]) -> Self {
        let count = points.len();
        debug_assert!(count < u32::MAX as usize, "segment indices are u32");
        let (mut v_lo, mut v_hi) = (f64::INFINITY, f64::NEG_INFINITY);
        let mut variation = 0.0;
        for index in 0..count {
            let (_, y0) = points[index];
            let (_, y1) = points[(index + 1) % count];
            v_lo = v_lo.min(y0);
            v_hi = v_hi.max(y0);
            variation += (y1 - y0).abs();
        }
        // One band per `variation / count` of `v`. That width is what makes the
        // index linear in the loop whatever the loop's shape: a segment lands
        // in `its own v extent / band width + 2` bands, and those sum to about
        // `3 × count` however the vertical variation is distributed — a gear
        // tooth's thousands of short segments and a box's four long ones alike.
        let span = v_hi - v_lo;
        let bands = if span.is_finite() && span > 0.0 && variation > 0.0 {
            let wanted = span * count as f64 / variation;
            if wanted.is_finite() && wanted >= 1.0 {
                (wanted as usize).min(count.max(1))
            } else {
                1
            }
        } else {
            1
        };
        let bands_per_v = if bands > 1 { bands as f64 / span } else { 0.0 };
        let band_of = |v: f64| -> usize {
            if bands_per_v == 0.0 {
                return 0;
            }
            let raw = (v - v_lo) * bands_per_v;
            // Written so a NaN takes the first branch rather than falling
            // through to an `as usize` cast of it.
            if !(raw > 0.0) {
                0
            } else if raw >= (bands - 1) as f64 {
                bands - 1
            } else {
                raw as usize
            }
        };
        let mut starts = vec![0u32; bands + 1];
        for index in 0..count {
            let (_, y0) = points[index];
            let (_, y1) = points[(index + 1) % count];
            let (lo, hi) = (band_of(y0.min(y1)), band_of(y0.max(y1)));
            for start in starts[lo + 1..=hi + 1].iter_mut() {
                *start += 1;
            }
        }
        for band in 1..=bands {
            starts[band] += starts[band - 1];
        }
        let mut cursor = starts.clone();
        let mut segments = vec![0u32; starts[bands] as usize];
        for index in 0..count {
            let (_, y0) = points[index];
            let (_, y1) = points[(index + 1) % count];
            let (lo, hi) = (band_of(y0.min(y1)), band_of(y0.max(y1)));
            for band in lo..=hi {
                segments[cursor[band] as usize] = index as u32;
                cursor[band] += 1;
            }
        }
        Self {
            points,
            v_lo,
            bands_per_v,
            starts,
            segments,
        }
    }

    fn band_count(&self) -> usize {
        self.starts.len() - 1
    }

    /// The band `v` falls in, clamped. A `v` outside the loop's own extent
    /// clamps to an end band, whose segments then fail the tests on their own
    /// terms — nothing straddles a ray the whole loop sits above or below.
    fn band_of(&self, v: f64) -> usize {
        if self.bands_per_v == 0.0 {
            return 0;
        }
        let bands = self.band_count();
        let raw = (v - self.v_lo) * self.bands_per_v;
        if !(raw > 0.0) {
            0
        } else if raw >= (bands - 1) as f64 {
            bands - 1
        } else {
            raw as usize
        }
    }

    fn segment(&self, index: u32) -> ((f64, f64), (f64, f64)) {
        let index = index as usize;
        (
            self.points[index],
            self.points[(index + 1) % self.points.len()],
        )
    }

    /// Even-odd parity: is `(u, v)` inside the closed loop?
    fn contains(&self, point: (f64, f64)) -> bool {
        let (u, v) = point;
        let band = self.band_of(v);
        let mut inside = false;
        for &index in &self.segments[self.starts[band] as usize..self.starts[band + 1] as usize] {
            let ((x0, y0), (x1, y1)) = self.segment(index);
            if (y0 > v) != (y1 > v) && u < x0 + (v - y0) * (x1 - x0) / (y1 - y0) {
                inside = !inside;
            }
        }
        inside
    }

    /// Does `(u, v)` sit within `tolerance` of the loop — is it ON the
    /// boundary rather than off the face?
    ///
    /// The bands searched are widened by one on each side of the `tolerance`
    /// band, so the margin against a segment being excluded by a rounding of
    /// its own `v` extent is a whole band rather than an ulp.
    fn within(&self, point: (f64, f64), tolerance: f64) -> bool {
        let (u, v) = point;
        let lo = self.band_of(v - tolerance).saturating_sub(1);
        let hi = (self.band_of(v + tolerance) + 1).min(self.band_count() - 1);
        for band in lo..=hi {
            for &index in &self.segments[self.starts[band] as usize..self.starts[band + 1] as usize]
            {
                let ((x0, y0), (x1, y1)) = self.segment(index);
                let (dx, dy) = (x1 - x0, y1 - y0);
                let length_squared = dx * dx + dy * dy;
                let t = if length_squared > 0.0 {
                    (((u - x0) * dx + (v - y0) * dy) / length_squared).clamp(0.0, 1.0)
                } else {
                    0.0
                };
                let (px, py) = (x0 + t * dx, y0 + t * dy);
                if ((u - px).powi(2) + (v - py).powi(2)).sqrt() <= tolerance {
                    return true;
                }
            }
        }
        false
    }

    /// The point of the loop nearest to `(u, v)` in the parameter plane, and
    /// how far it is.
    ///
    /// Searched outward from the point's own band a ring at a time, stopping
    /// when the next ring's `v` alone is further than the best found: every
    /// segment in band `b` has every point of it within that band's `v`
    /// range, so a band `r` away from the query's cannot hold a closer point
    /// than `(r - 1)` band heights. A query beyond the loop's `v` extent
    /// clamps to an end band and the same bound holds, larger. Called only
    /// for a station that has already FAILED parity and the parameter band,
    /// so its cost is paid once per conviction, not once per station.
    fn nearest(&self, point: (f64, f64)) -> ((f64, f64), f64) {
        let (u, v) = point;
        let bands = self.band_count();
        let centre = self.band_of(v);
        let band_height = if self.bands_per_v > 0.0 { 1.0 / self.bands_per_v } else { f64::INFINITY };
        let mut best = f64::INFINITY;
        let mut best_point = self.points[0];
        let search = |band: usize, best: &mut f64, best_point: &mut (f64, f64)| {
            for &index in &self.segments[self.starts[band] as usize..self.starts[band + 1] as usize] {
                let ((x0, y0), (x1, y1)) = self.segment(index);
                let (dx, dy) = (x1 - x0, y1 - y0);
                let length_squared = dx * dx + dy * dy;
                let t = if length_squared > 0.0 {
                    (((u - x0) * dx + (v - y0) * dy) / length_squared).clamp(0.0, 1.0)
                } else {
                    0.0
                };
                let candidate = (x0 + t * dx, y0 + t * dy);
                let distance = ((u - candidate.0).powi(2) + (v - candidate.1).powi(2)).sqrt();
                if distance < *best {
                    *best = distance;
                    *best_point = candidate;
                }
            }
        };
        for ring in 0..bands {
            if ring > 1 && (ring - 1) as f64 * band_height > best {
                break;
            }
            let below = centre.checked_sub(ring);
            let above = if centre + ring < bands { Some(centre + ring) } else { None };
            if below.is_none() && above.is_none() {
                break;
            }
            if let Some(band) = below {
                search(band, &mut best, &mut best_point);
            }
            if let Some(band) = above {
                if ring > 0 {
                    search(band, &mut best, &mut best_point);
                }
            }
        }
        (best_point, best)
    }
}

/// THE HOLE-CONTAINMENT FLOOR: a face whose hole loop has left that face's
/// own outer loop is refused, naming the hole — on EVERY carrier, planar or
/// not.
///
/// A hole is a hole IN something. Carry a bore's mouth off the flat it opens
/// through — a direct edit dragging the wall clear of the body — and what the
/// fixed-topology road emits is the mouth loop hanging in the plane beside the
/// face it is supposed to perforate, with the wall it bounds somewhere else
/// entirely. Every other floor is blind to it: `validate()` tests INCIDENCE and
/// the incidences are all intact; the scan needs two faces to CROSS and the
/// runaway bore crosses nothing; and the divergence-theorem volume of a
/// cylinder does not depend on where its axis sits, so the body can measure to
/// the last bit of the right answer while being the wrong body. The correct
/// result needs the hole to break through a neighbouring face — a topology
/// change the direct edits do not make — so this refuses instead of shipping it.
///
/// Decided in the face's own parameter plane by parity against the outer loop
/// (the one enclosing the largest parameter area). A point that fails parity
/// but sits within `tolerance` of the outer loop is treated as ON it, so a
/// mouth tangent to its face's own boundary is not convicted by sampling
/// noise. The predicate is only as fine as the sampling: a curved hole poking
/// out by less than its sampled chord sag can still pass.
///
/// On a PLANE the parameter plane is affine to space and the pcurves are exact
/// images, so the band is the parameter band alone. On every other carrier the
/// pcurves are FITTED, to [`crate::PCURVE_REFINEMENT_TOLERANCE`] in space, and
/// a fit's noise can put a station of a legitimately tangent mouth a few
/// parameter ulps outside its face. So a station that fails both tests on a
/// non-affine face is convicted only if it is also further than
/// [`CURVED_BREACH_ACQUITTAL`] IN SPACE from the nearest point of the outer
/// loop — the same trace read back through the surface. That distance is the
/// smallest curved breach the floor can see; it is a floor on the fit, not on
/// the sampling. Until 2026-09-30 every non-planar face was skipped outright,
/// so a mouth carried off a cylinder wall or a torus by a motion that pierced
/// nothing the scan could resolve shipped as a wrong body.
///
/// A closed carrier is read in two shapes. Where NO loop winds the period
/// (the nested case — a bored post's wall, whose outer loop holds the seam
/// twice), the outer loop is the one enclosing the largest parameter area and
/// the holes must lie inside it; a hole station is folded by whole periods
/// into the outer loop's window only where that loop carries no seam coedge,
/// so a hole traced across a seam that IS an edge of the outer loop (a hole
/// with an edge of its own face running through it — the builders split the
/// seam where a mouth reaches it) is a wrong body and is convicted. Where some
/// loop DOES wind (the banded case — a refitted full cylinder is bounded by
/// two rings and no seam, a dome cut off a sphere by one), the face has no
/// outer loop and a hole must lie on every ring's material side, decided
/// along the open direction with the ring's winding sign and the face's own
/// orientation convention ([`banded_hole_breach`]). A band whose rings wind a
/// carrier closed BOTH ways selects its intended region by the same orientation
/// convention, using the next transverse boundary modulo that period. A loop
/// winding both ways remains outside this arm. A breach the banded arm convicts is refused, not
/// broken out: the breakout route reads a nested outer loop.
///
/// Both questions go through [`BoundaryBands`], which is an INDEX over the
/// outer loop and not a coarser reading of it: the answers are the ones a walk
/// of the whole traced polyline gives, off the whole traced polyline, at a
/// cost that is no longer the product of the two loops' lengths.
///
/// This runs at the END of [`accept_sound`], on the body the repair left, so a
/// crossing the repair can take is still taken — the reason an earlier
/// primitive-level copy of this floor was removed.
fn refuse_holes_leaving_their_faces(solid: &BrepSolid, entry: &str) -> Result<(), KernelRefusal> {
    match first_hole_breach(solid) {
        Some(breach) => Err(hole_breach_refusal(solid, &breach, entry)),
        None => Ok(()),
    }
}

/// A hole loop the floor has convicted: WHICH face, which loop of it, and the
/// sample that fell outside, in that face's own parameters.
///
/// The floor's verdict is a yes or no; this is the same verdict with the
/// witness kept, so a lane that can BUILD the right body has something to
/// build it from. Nothing about the predicate changes by recording it.
struct HoleBreach {
    face: u64,
    loop_index: usize,
    loop_id: u64,
    at: (f64, f64),
    /// Convicted by the banded arm — the face has rings and no outer loop, so
    /// the breakout route, which reads a nested outer loop, has nothing to
    /// read and declines by name.
    banded: bool,
}

/// THE FLOOR'S PREDICATE, verbatim — the first hole loop on any face that has
/// left that face's own outer loop, or `None`.
///
/// This is the whole of the decision [`refuse_holes_leaving_their_faces`] used
/// to make inline; the refusal is now written by [`hole_breach_refusal`] off
/// what this returns. Same loops, same trace, same bands, same tolerance, same
/// first-hit order.
fn first_hole_breach(solid: &BrepSolid) -> Option<HoleBreach> {
    for shell in &solid.shells {
        for face in &shell.faces {
            if face.loops.len() < 2 {
                continue;
            }
            // Exact pcurves on an affine carrier; fitted ones everywhere else,
            // which earn the acquittal in space.
            let fitted = !face.surface.is_affine().unwrap_or(false);
            let closed = face.surface.closed_directions().unwrap_or((false, false));
            let period = (
                face.surface.domain_u().map(|[a, b]| b - a).unwrap_or(0.0),
                face.surface.domain_v().map(|[a, b]| b - a).unwrap_or(0.0),
            );
            let windings: Vec<(i32, i32)> = face
                .loops
                .iter()
                .map(|record| loop_winding(record, closed, period))
                .collect();
            let polylines = face
                .loops
                .iter()
                .map(loop_parameter_polyline)
                .collect::<Result<Vec<_>, KernelRefusal>>()
                // A loop this cannot be traced from is not a loop this can
                // convict: the acceptance's own floors already ran.
                .unwrap_or_default();
            if polylines.len() != face.loops.len() || polylines.iter().any(Vec::is_empty) {
                continue;
            }
            let breach = if windings.iter().all(|&winding| winding == (0, 0)) {
                let spherical = matches!(face.surface.analytic(), Some(crate::AnalyticSurface::Sphere { .. }));
                if spherical && !sphere_has_parameter_outer(solid, face, &polylines) {
                    spherical_hole_breach(solid, face)
                } else {
                    nested_hole_breach(face, &polylines, fitted, closed, period)
                }
            } else {
                banded_hole_breach(face, &polylines, &windings, fitted, closed, period)
            };
            if breach.is_some() {
                return breach;
            }
        }
    }
    None
}

/// A bounded spherical patch can use the existing indexed nested floor.
/// Its largest UV loop must be a real, correctly oriented material outer
/// boundary, with the other real loops oriented as holes. A full-domain
/// rectangle made only of seams and collapsed poles is not such a boundary.
fn sphere_has_parameter_outer(
    solid: &BrepSolid,
    face: &crate::FaceRecord,
    polylines: &[Vec<(f64, f64)>],
) -> bool {
    let areas: Vec<_> = polylines.iter().map(|points| polyline_area(points)).collect();
    let Some(outer) = (0..areas.len()).max_by(|&a, &b| areas[a].abs().total_cmp(&areas[b].abs())) else {
        return false;
    };
    let orientation = if face.same_sense { 1.0 } else { -1.0 };
    if areas[outer] * orientation <= 0.0 { return false; }
    let real = |index: usize| face.loops[index].coedges.iter().any(|coedge| {
        face.loops.iter().flat_map(|rim| &rim.coedges)
            .filter(|other| other.edge_id == coedge.edge_id).count() == 1
            && !solid.edges.iter().any(|edge| edge.id == coedge.edge_id && edge.degenerate)
    });
    real(outer) && (0..areas.len()).all(|i| i == outer || !real(i) || areas[i] * orientation <= 0.0)
}

/// A sphere has no distinguished outer boundary: two disjoint negatively
/// oriented trims can both be holes in the remaining sphere. Choosing the
/// largest UV polygon as an outer loop falsely rejects that topology, and
/// even mistakes an imported retraced pole cut for a runaway hole. Ask the
/// same spherical material-side classifier used by containment and meshing:
/// each real boundary must lie in the region left by the other loops.
fn spherical_hole_breach(solid: &BrepSolid, face: &crate::FaceRecord) -> Option<HoleBreach> {
    let mut uses = std::collections::HashMap::new();
    for coedge in face.loops.iter().flat_map(|rim| &rim.coedges) {
        *uses.entry(coedge.edge_id).or_insert(0usize) += 1;
    }
    for (index, rim) in face.loops.iter().enumerate() {
        let mut other = face.clone();
        other.loops.remove(index);
        // A loop containing only domain seams and collapsed poles carries
        // no material boundary. Keep mixed loops intact: the sampler reads
        // each coedge's endpoint from the next coedge's start. Removing a
        // paired seam joining two rims would bridge those rims with fake
        // chords; the spherical classifier cancels the seam itself.
        other.loops.retain(|boundary| {
            boundary.coedges.iter().any(|coedge| {
                uses[&coedge.edge_id] == 1
                    && !solid.edges.iter().any(|edge| {
                        edge.id == coedge.edge_id && edge.degenerate
                    })
            })
        });
        if other.loops.is_empty() {
            continue;
        }
        let [u0, u1] = face.surface.domain_u().ok()?;
        for coedge in &rim.coedges {
            // A seam traversed twice and a collapsed pole have no spherical
            // area. Their UV trace is not a boundary of the retained region.
            if uses[&coedge.edge_id] > 1
                || solid
                    .edges
                    .iter()
                    .any(|edge| edge.id == coedge.edge_id && edge.degenerate)
            {
                continue;
            }
            let boundary = LoopRecord {
                id: rim.id,
                coedges: vec![coedge.clone()],
            };
            let Ok(mut points) = loop_parameter_polyline(&boundary) else {
                continue;
            };
            // Straight UV edges contribute only their start in the ordinary
            // polygon sampler. Include an interior witness here: two holes
            // can share horizontal boundary levels while their vertical sides
            // run through the other hole's interior.
            if coedge.pcurve.degree == 1 && coedge.pcurve.control_points.len() == 2 {
                let [a, b] = coedge.pcurve.domain().ok()?;
                let midpoint = coedge.pcurve.evaluate((a + b) * 0.5).ok()?;
                points.push((midpoint.x, midpoint.y));
            }
            for (u, v) in points {
                if crate::parameter_point_in_face(
                    &other,
                    crate::Vec2 {
                        x: u0 + (u - u0).rem_euclid(u1 - u0),
                        y: v,
                    },
                    1e-6,
                )
                .ok()
                    != Some(crate::PolygonClass::Outside)
                {
                    continue;
                }
                return Some(HoleBreach {
                    face: face.id,
                    loop_index: index,
                    loop_id: rim.id,
                    at: (u, v),
                    banded: true,
                });
            }
        }
    }
    None
}

/// How many times a loop goes round each CLOSED direction of its carrier, read
/// off its coedges' pcurve ENDPOINTS rather than its traced stations: a ring
/// the refit writes as one straight pcurve across the whole period traces to a
/// single station, and the trace cannot see it go round. An open direction
/// winds nothing.
fn loop_winding(loop_record: &LoopRecord, closed: (bool, bool), period: (f64, f64)) -> (i32, i32) {
    let (mut du, mut dv) = (0.0, 0.0);
    for coedge in &loop_record.coedges {
        let Ok([d0, d1]) = coedge.pcurve.domain() else { return (0, 0) };
        let (Ok(start), Ok(end)) = (coedge.pcurve.evaluate(d0), coedge.pcurve.evaluate(d1)) else {
            return (0, 0);
        };
        du += end.x - start.x;
        dv += end.y - start.y;
    }
    let wind = |closed: bool, net: f64, period: f64| {
        if closed && period > 0.0 && net.is_finite() {
            (net / period).round() as i32
        } else {
            0
        }
    };
    (wind(closed.0, du, period.0), wind(closed.1, dv, period.1))
}

/// Does this loop hold the same edge twice — a SEAM of a closed carrier, both
/// sides of it on this one face?
fn has_seam_pair(loop_record: &LoopRecord) -> bool {
    loop_record.coedges.iter().enumerate().any(|(index, coedge)| {
        loop_record.coedges[index + 1..]
            .iter()
            .any(|other| other.edge_id == coedge.edge_id)
    })
}

/// THE NESTED CASE: no loop winds the carrier, so the loop enclosing the
/// largest parameter area is the face's outer boundary and every other loop is
/// a hole that must lie inside it. This is the floor exactly as the planar arm
/// always ran it, with two additions for a closed carrier: a hole station is
/// FOLDED by whole periods into the outer loop's own window where the outer
/// loop carries no seam coedge (a fitted pcurve unwraps across the seam on its
/// own terms, and a partial patch's outer loop and its hole need not have
/// unwrapped the same way), and never folded where it does — then the seam is
/// an edge of the outer loop and a hole across it is a wrong body.
fn nested_hole_breach(
    face: &crate::FaceRecord,
    polylines: &[Vec<(f64, f64)>],
    fitted: bool,
    closed: (bool, bool),
    period: (f64, f64),
) -> Option<HoleBreach> {
    let outer_index = (0..polylines.len())
        .max_by(|&a, &b| {
            polyline_area(&polylines[a])
                .abs()
                .total_cmp(&polyline_area(&polylines[b]).abs())
        })
        .unwrap_or(0);
    let outer = &polylines[outer_index];
    // Every hole point is asked the same two questions against this one
    // loop, so the loop is indexed ONCE and each question then reads the band
    // it falls in rather than the whole polyline. The answers are the ones
    // the whole-polyline walk gives; see [`BoundaryBands`].
    let bands = BoundaryBands::build(outer);
    let tolerance = boundary_tolerance(outer);
    let seamed = has_seam_pair(&face.loops[outer_index]);
    let (mut lo, mut hi) = ((f64::MAX, f64::MAX), (f64::MIN, f64::MIN));
    for &(u, v) in outer {
        lo = (lo.0.min(u), lo.1.min(v));
        hi = (hi.0.max(u), hi.1.max(v));
    }
    let centre = (0.5 * (lo.0 + hi.0), 0.5 * (lo.1 + hi.1));
    let fold_u = closed.0 && !seamed && period.0 > 0.0;
    let fold_v = closed.1 && !seamed && period.1 > 0.0;
    let fold = |(u, v): (f64, f64)| {
        (
            if fold_u { u + period.0 * ((centre.0 - u) / period.0).round() } else { u },
            if fold_v { v + period.1 * ((centre.1 - v) / period.1).round() } else { v },
        )
    };
    for (index, hole) in polylines.iter().enumerate() {
        if index == outer_index {
            continue;
        }
        let Some(at) = hole.iter().map(|&point| fold(point)).find(|&point| {
            !bands.contains(point)
                && !bands.within(point, tolerance)
                && !(fitted && acquitted_in_space(&face.surface, point, bands.nearest(point).0))
        }) else {
            continue;
        };
        return Some(HoleBreach {
            face: face.id,
            loop_index: index,
            loop_id: face.loops[index].id,
            at,
            banded: false,
        });
    }
    None
}

/// A ring's segments in the parameter plane, one coedge after another with
/// every end point included, closed by the whole periods the ring winds — so
/// the segment that closes it is the short one back to the start's image, not
/// a jump across the domain.
struct Ring {
    segments: Vec<((f64, f64), (f64, f64))>,
    /// Is the face's material on the ring's POSITIVE side along the transverse
    /// direction?
    material_positive: bool,
    positive_winding: bool,
}

/// THE BANDED CASE: some loop winds the carrier's closed direction, so the
/// face has no outer loop in the parameter plane — it is a band between RINGS
/// (a refitted full cylinder is bounded by two, a dome cut off a sphere by
/// one), and its holes are the loops that do not wind. A hole is in the face
/// when it is on every ring's MATERIAL side, decided by a ray along the open
/// direction: the ring's winding sign says which side its material is on once
/// the face's own orientation convention is read off a hole (a hole loop runs
/// with the material on its left when its parameter area is negative — the
/// mirror of the outer loop it is a hole in — and on its right otherwise).
/// Stations and rings are compared modulo the period, so a ring or a hole
/// unwrapped across the seam reads the same as one that was not.
///
/// Every winding loop must go round exactly once along the same closed axis.
/// If the transverse direction is also closed, oriented crossings select the
/// material region (including the complementary band); ring windings must
/// cancel. Loops winding both axes remain outside this arm.
fn banded_hole_breach(
    face: &crate::FaceRecord,
    polylines: &[Vec<(f64, f64)>],
    windings: &[(i32, i32)],
    fitted: bool,
    closed: (bool, bool),
    period: (f64, f64),
) -> Option<HoleBreach> {
    let rings_wind = |axis: usize| {
        windings
            .iter()
            .filter(|&&winding| winding != (0, 0))
            .all(|&(u, v)| {
                if axis == 0 {
                    u.abs() == 1 && v == 0
                } else {
                    v.abs() == 1 && u == 0
                }
            })
    };
    // `along` is the closed direction the rings wind; `open` names the
    // transverse axis (which can itself be periodic).
    let along = if rings_wind(0) && closed.0 && period.0 > 0.0 {
        0
    } else if rings_wind(1) && closed.1 && period.1 > 0.0 {
        1
    } else {
        return None;
    };
    let open = 1 - along;
    let axis = |point: (f64, f64), which: usize| if which == 0 { point.0 } else { point.1 };
    let period_along = axis(period, along);
    let transverse_period = if if open == 0 { closed.0 } else { closed.1 } {
        let p = axis(period, open);
        // Boundaries of a region on a torus must cancel in homology. A
        // nonseparating single ring does not define a material band.
        if p <= 0.0
            || windings
                .iter()
                .map(|&w| if along == 0 { w.0 } else { w.1 })
                .sum::<i32>()
                != 0
        {
            return None;
        }
        Some(p)
    } else {
        None
    };
    let domain_start = if along == 0 {
        face.surface.domain_u().ok()?[0]
    } else {
        face.surface.domain_v().ok()?[0]
    };

    // The orientation convention, read off the first hole with an area.
    let left = polylines
        .iter()
        .zip(windings)
        .filter(|(_, &winding)| winding == (0, 0))
        .map(|(hole, _)| {
            // Area is translation invariant. Subtract a local origin before
            // multiplying: period translations must not cancel a small
            // hole's area to zero or invert its orientation convention.
            let origin = hole[0];
            let local: Vec<_> = hole.iter()
                .map(|&(u, v)| (u - origin.0, v - origin.1))
                .collect();
            polyline_area(&local)
        })
        .find(|area| *area != 0.0)?
        < 0.0;

    let mut rings: Vec<Ring> = Vec::new();
    let mut extent = 0.0f64;
    for (index, record) in face.loops.iter().enumerate() {
        let winding = windings[index];
        if winding == (0, 0) {
            continue;
        }
        let mut points: Vec<(f64, f64)> = Vec::new();
        for coedge in &record.coedges {
            points.extend(coedge_parameter_polyline(coedge).ok()?);
        }
        let first = *points.first()?;
        points.push((
            first.0 + winding.0 as f64 * period.0,
            first.1 + winding.1 as f64 * period.1,
        ));
        for window in points.windows(2) {
            extent = extent
                .max(axis(window[0], open) - axis(window[1], open))
                .max(axis(window[1], open) - axis(window[0], open));
        }
        let segments = points
            .windows(2)
            .map(|window| (window[0], window[1]))
            .collect();
        // Material on the LEFT of a ring winding +u lies at +v; on the left of
        // one winding +v it lies at −u.
        let positive_winding = axis((winding.0 as f64, winding.1 as f64), along) > 0.0;
        let material_positive = if along == 0 {
            positive_winding == left
        } else {
            positive_winding != left
        };
        rings.push(Ring {
            segments,
            material_positive,
            positive_winding,
        });
    }
    let tolerance = (extent.max(period_along) * 1e-9).max(1e-12);

    for (index, hole) in polylines.iter().enumerate() {
        if windings[index] != (0, 0) {
            continue;
        }
        for &station in hole {
            // Reduced into one period along the rings.
            let station = {
                let s = axis(station, along);
                let reduced = domain_start + (s - domain_start).rem_euclid(period_along);
                if along == 0 {
                    (reduced, station.1)
                } else {
                    (station.0, reduced)
                }
            };
            let held = if let Some(transverse) = transverse_period {
                periodic_band_holds(&rings, station, along, period_along, transverse)?
            } else {
                rings
                    .iter()
                    .all(|ring| ring_holds(ring, station, along, period_along))
            };
            if held {
                continue;
            }
            let (nearest, distance) =
                rings_nearest(&rings, station, along, period_along, transverse_period);
            if distance <= tolerance {
                continue;
            }
            if fitted && acquitted_in_space(&face.surface, station, nearest) {
                continue;
            }
            return Some(HoleBreach {
                face: face.id,
                loop_index: index,
                loop_id: face.loops[index].id,
                at: station,
                banded: true,
            });
        }
    }
    None
}

/// Images relative to the segment midpoint nearest the query, rather than
/// the domain origin. Ordinary traced ring segments span at most a period
/// and a bit; two images either way cover those even after arbitrary whole
/// period translations. This is not a bound for multiply traversed segments.
const RING_IMAGES: [i32; 5] = [-2, -1, 0, 1, 2];

/// Is `station` on this ring's material side? A ray from the station along
/// the OPEN direction, positive way, crosses the ring an odd number of times
/// exactly when the ring lies on the positive side of the station — and then
/// the station is on the ring's NEGATIVE side, which is the material side
/// exactly when the ring's material is not on its positive side.
fn ring_holds(ring: &Ring, station: (f64, f64), along: usize, period: f64) -> bool {
    let (s_along, s_open) = if along == 0 {
        (station.0, station.1)
    } else {
        (station.1, station.0)
    };
    let mut crossings = 0usize;
    for &(a, b) in &ring.segments {
        let (a_along, a_open) = if along == 0 { (a.0, a.1) } else { (a.1, a.0) };
        let (b_along, b_open) = if along == 0 { (b.0, b.1) } else { (b.1, b.0) };
        for image in RING_IMAGES {
            let shift =
                (((s_along - 0.5 * (a_along + b_along)) / period).round() + image as f64) * period;
            let (a_s, b_s) = (a_along + shift, b_along + shift);
            if (a_s > s_along) != (b_s > s_along) {
                let at = a_open + (s_along - a_s) * (b_open - a_open) / (b_s - a_s);
                if at > s_open {
                    crossings += 1;
                }
            }
        }
    }
    let ring_is_positive = crossings % 2 == 1;
    ring_is_positive != ring.material_positive
}

/// On a doubly periodic chart there is no outside at infinity. The first
/// transverse boundary ahead of a station decides its material side. Its
/// orientation comes from the same hole-area convention as the open band;
/// reversing all loops therefore preserves the region, while reversing just
/// the rings chooses its complement. Signed events cancel tangent contacts.
fn periodic_band_holds(
    rings: &[Ring],
    station: (f64, f64),
    along: usize,
    period: f64,
    transverse: f64,
) -> Option<bool> {
    let (s, t) = if along == 0 {
        station
    } else {
        (station.1, station.0)
    };
    let mut events = Vec::new();
    for ring in rings {
        for &(a, b) in &ring.segments {
            let (a_s, a_t) = if along == 0 { a } else { (a.1, a.0) };
            let (b_s, b_t) = if along == 0 { b } else { (b.1, b.0) };
            let centre = ((s - 0.5 * (a_s + b_s)) / period).round();
            for image in RING_IMAGES {
                let shift = (centre + image as f64) * period;
                let (a_image, b_image) = (a_s + shift, b_s + shift);
                if (a_image > s) != (b_image > s) {
                    // Both segments incident to a tangent vertex must emit
                    // the stored vertex value. Re-interpolating an endpoint
                    // can move one event by ulps, splitting an exact signed
                    // cancellation and falsely rejecting a remote station.
                    // Compare in the same shifted chart as the crossing test;
                    // no event-distance tolerance or physical band changes.
                    let at = if s == a_image {
                        a_t
                    } else if s == b_image {
                        b_t
                    } else {
                        a_t + (s - a_s - shift) * (b_t - a_t) / (b_s - a_s)
                    };
                    let positive = if (b_s > a_s) == ring.positive_winding {
                        ring.material_positive
                    } else {
                        !ring.material_positive
                    };
                    events.push((
                        (at - t).rem_euclid(transverse),
                        if positive { 1i32 } else { -1 },
                    ));
                }
            }
        }
    }
    events.sort_by(|a, b| a.0.total_cmp(&b.0));
    let mut i = 0;
    while i < events.len() {
        let distance = events[i].0;
        let mut sign = 0;
        while i < events.len() && events[i].0 == distance {
            sign += events[i].1;
            i += 1;
        }
        if sign != 0 {
            return Some(sign < 0);
        }
    }
    None
}

/// The nearest point of any ring to `station`, over the rings' period images,
/// and its distance in the parameter plane.
fn rings_nearest(
    rings: &[Ring],
    station: (f64, f64),
    along: usize,
    period: f64,
    transverse: Option<f64>,
) -> ((f64, f64), f64) {
    let (u, v) = station;
    let mut best = (station, f64::INFINITY);
    for ring in rings {
        for &(a, b) in &ring.segments {
            for image in RING_IMAGES {
                let midpoint = (0.5 * (a.0 + b.0), 0.5 * (a.1 + b.1));
                let (s, m) = if along == 0 {
                    (u, midpoint.0)
                } else {
                    (v, midpoint.1)
                };
                let along_shift = (((s - m) / period).round() + image as f64) * period;
                for across_image in if transverse.is_some() {
                    &RING_IMAGES[..]
                } else {
                    &RING_IMAGES[2..3]
                } {
                    let across_shift = transverse
                        .map(|p| {
                            let (s, m) = if along == 0 {
                                (v, midpoint.1)
                            } else {
                                (u, midpoint.0)
                            };
                            (((s - m) / p).round() + *across_image as f64) * p
                        })
                        .unwrap_or(0.0);
                    let shift = if along == 0 {
                        (along_shift, across_shift)
                    } else {
                        (across_shift, along_shift)
                    };
                    let (x0, y0) = (a.0 + shift.0, a.1 + shift.1);
                    let (x1, y1) = (b.0 + shift.0, b.1 + shift.1);
                    let (dx, dy) = (x1 - x0, y1 - y0);
                    let length_squared = dx * dx + dy * dy;
                    let t = if length_squared > 0.0 {
                        (((u - x0) * dx + (v - y0) * dy) / length_squared).clamp(0.0, 1.0)
                    } else {
                        0.0
                    };
                    let candidate = (x0 + t * dx, y0 + t * dy);
                    let distance = ((u - candidate.0).powi(2) + (v - candidate.1).powi(2)).sqrt();
                    if distance < best.1 {
                        best = (candidate, distance);
                    }
                }
            }
        }
    }
    best
}

/// The "on the boundary" band, measured against the outer loop's own parameter
/// extent so it means the same thing on a 1 mm face and on a 1 m one.
fn boundary_tolerance(outer: &[(f64, f64)]) -> f64 {
    let (mut lo, mut hi) = ((f64::MAX, f64::MAX), (f64::MIN, f64::MIN));
    for &(u, v) in outer {
        lo = (lo.0.min(u), lo.1.min(v));
        hi = (hi.0.max(u), hi.1.max(v));
    }
    let extent = (hi.0 - lo.0).max(hi.1 - lo.1);
    (extent * 1e-9).max(1e-12)
}

/// How far IN SPACE a station of a fitted hole loop may sit outside its face's
/// outer loop and still be read as ON it: ten times the floor the pcurve fit
/// refines to ([`crate::PCURVE_REFINEMENT_TOLERANCE`], 1e-7). A tangent mouth's
/// fit noise is under the floor; a breach the floor is for is a mouth carried
/// out by a motion, orders above it.
const CURVED_BREACH_ACQUITTAL: f64 = 10.0 * crate::PCURVE_REFINEMENT_TOLERANCE;

/// Is a station that failed parity and the parameter band still within
/// [`CURVED_BREACH_ACQUITTAL`] of the face's boundary IN SPACE? Read through
/// the face's own surface at the station and at the boundary's nearest
/// parameter point (`nearest`), so a fitted pcurve's parameter noise is
/// judged by what it amounts to on the carrier. A surface that cannot be
/// evaluated acquits nothing.
fn acquitted_in_space(
    surface: &crate::NurbsSurface,
    point: (f64, f64),
    nearest: (f64, f64),
) -> bool {
    // EXTENDED, not clamped: a station past the end of an open direction is
    // read where the carrier's own ruled extension puts it, and one past a
    // seam where the period wraps it. `evaluate` clamps a parameter into the
    // domain, which would put every overshoot back ON the rim and acquit it.
    match (
        surface.evaluate_extended(point.0, point.1),
        surface.evaluate_extended(nearest.0, nearest.1),
    ) {
        (Ok(here), Ok(there)) => here.sub(there).length() <= CURVED_BREACH_ACQUITTAL,
        _ => false,
    }
}

/// The floor's refusal, written off the breach it convicted.
fn hole_breach_refusal(solid: &BrepSolid, breach: &HoleBreach, entry: &str) -> KernelRefusal {
    let face = solid
        .shells
        .iter()
        .flat_map(|shell| &shell.faces)
        .find(|face| face.id == breach.face);
    let label = match face.and_then(|face| face.name.as_deref()) {
        Some(name) => format!("`{name}` (face {})", breach.face),
        None => format!("face {}", breach.face),
    };
    let (u, v) = breach.at;
    let where_in_space = face
        .and_then(|face| face.surface.evaluate_extended(u, v).ok())
        .map(|point| format!(" — ({:.4}, {:.4}, {:.4}) in space", point.x, point.y, point.z))
        .unwrap_or_default();
    KernelRefusal::unsound(
        KernelStage::Validate,
        SoundnessDefect::HoleBreach,
        vec![breach.face],
        format!(
            "{entry}: hole loop {} of {label} has LEFT that face — it runs outside the \
             face's own outer boundary, at ({u:.6}, {v:.6}) in the face's parameters{}. \
             A hole is a hole IN a face; this one bounds a wall that no longer opens \
             through it, so the right body needs the hole to break through a \
             neighbouring face, a topology change this operation does not make. \
             `validate()` cannot see it (every incidence is intact), the \
             self-intersection scan cannot (nothing crosses), and the volume need not \
             change at all — refusing rather than returning the wrong body",
            breach.loop_id, where_in_space,
        ),
    )
}

// ---------------------------------------------------------------------------
// The breakout route: a hole that has left its face onto a NAMED neighbour
// ---------------------------------------------------------------------------

/// The faces a breakout is between: the WALL the convicted hole loop bounds,
/// and the ONE neighbour of the breached face the hole has run out onto.
struct BreakoutPair {
    wall: u64,
    neighbour: u64,
}

/// WHICH neighbour the runaway hole has gone onto — read in the breached
/// face's own parameter plane, where the question is a segment crossing and
/// not a guess.
///
/// The hole loop bounds a wall (for a bore, the bore's own cylindrical face):
/// exactly one other face answers for its edges, and that is the face whose
/// carrier has to be re-trimmed. The hole has left through some stretch of the
/// breached face's OUTER loop, and each outer coedge is shared with exactly one
/// neighbouring face, so the coedges the hole polyline crosses name the faces
/// it has gone out onto.
///
/// This refuses rather than choosing whenever the answer is not a single pair:
/// a hole bounding more than one face, a hole that crosses no outer coedge at
/// all (it has left the face's own parameter patch entirely — the runaway), and
/// a hole crossing into MORE THAN ONE neighbour, which is a breakout over a
/// corner and needs both re-trims resolved together rather than one at a time.
fn breakout_pair(solid: &BrepSolid, breach: &HoleBreach) -> Result<BreakoutPair, KernelRefusal> {
    let wall = breakout_wall(solid, breach)?;
    match breakout_neighbours_crossed(solid, breach, wall)?[..] {
        [neighbour] => Ok(BreakoutPair { wall, neighbour }),
        [] => Err(KernelRefusal::unsupported(KernelStage::Validate, NOT_A_STRADDLE_WHAT, NOT_A_STRADDLE)),
        ref many => Err(KernelRefusal::unsupported(KernelStage::Validate, "breakout.pair", format!(
            "the runaway hole crosses into {} neighbouring faces at once ({many:?}) — a \
             breakout over a corner, where the two re-trims bound each other and resolving one \
             at a time is a different problem from resolving both",
            many.len(),
        ))),
    }
}

/// The marker [`breakout_pair`] returns when the hole does not STRADDLE its
/// face's boundary. It has either migrated wholly onto another face or left the
/// body; [`repair_hole_breakout`] tells those apart, by trying.
const NOT_A_STRADDLE: &str = "the hole does not straddle its own face's boundary";

/// The slug [`breakout_pair`] mints [`NOT_A_STRADDLE`] under
/// (`UnsupportedGeometry`), shared with [`is_not_a_straddle`].
const NOT_A_STRADDLE_WHAT: &str = "breakout.not_a_straddle";

/// Is this [`breakout_pair`]'s non-straddle answer? Read off the class and
/// slug, so the route's decision does not follow the sentence.
fn is_not_a_straddle(refusal: &KernelRefusal) -> bool {
    matches!(
        &refusal.class,
        crate::RefusalClass::UnsupportedGeometry { what } if what == NOT_A_STRADDLE_WHAT
    )
}

/// The face the convicted hole loop BOUNDS — the moved carrier, for a bore the
/// bore's own wall. Exactly one other face may answer for its edges.
fn breakout_wall(solid: &BrepSolid, breach: &HoleBreach) -> Result<u64, KernelRefusal> {
    let face = face_by_id(solid, breach.face)?;
    let mut walls: Vec<u64> = Vec::new();
    for coedge in &face.loops[breach.loop_index].coedges {
        for other in solid.shells.iter().flat_map(|shell| &shell.faces) {
            if other.id == face.id {
                continue;
            }
            if other
                .loops
                .iter()
                .any(|wire| wire.coedges.iter().any(|use_| use_.edge_id == coedge.edge_id))
                && !walls.contains(&other.id)
            {
                walls.push(other.id);
            }
        }
    }
    match walls[..] {
        [wall] => Ok(wall),
        _ => Err(KernelRefusal::unsupported(KernelStage::Validate, "breakout.wall_count", format!(
            "the runaway hole loop bounds {} face(s) ({walls:?}); this route re-trims ONE moved \
             carrier against the body and cannot decide which of several is the one that moved",
            walls.len(),
        ))),
    }
}

fn face_by_id<'a>(
    solid: &'a BrepSolid,
    id: u64,
) -> Result<&'a crate::FaceRecord, KernelRefusal> {
    solid
        .shells
        .iter()
        .flat_map(|shell| &shell.faces)
        .find(|face| face.id == id)
        .ok_or_else(|| {
            KernelRefusal::internal(KernelStage::Validate, "face_by_id", format!("face {id} is not on the body"))
        })
}

/// The neighbours the hole has run out ONTO, read as segment crossings in the
/// breached face's own parameter plane.
///
/// This answers the STRADDLE: part of the mouth still inside the face it was
/// recorded on, part outside. It returns nothing for a mouth that has migrated
/// wholly onto another face — such a loop is entirely outside the outer loop
/// and crosses none of its coedges — and nothing for one that has left the body
/// altogether. Those two are told apart downstream, by geometry, in
/// [`repair_hole_breakout`].
fn breakout_neighbours_crossed(
    solid: &BrepSolid,
    breach: &HoleBreach,
    wall: u64,
) -> Result<Vec<u64>, KernelRefusal> {
    let face = face_by_id(solid, breach.face)?;
    let polylines = face
        .loops
        .iter()
        .map(loop_parameter_polyline)
        .collect::<Result<Vec<_>, KernelRefusal>>()
        .map_err(|error| {
            error.with_message(|error| format!("the breached face's loops could not be traced: {error}"))
        })?;
    let outer_index = (0..polylines.len())
        .max_by(|&a, &b| {
            polyline_area(&polylines[a])
                .abs()
                .total_cmp(&polyline_area(&polylines[b]).abs())
        })
        .unwrap_or(0);
    if outer_index == breach.loop_index {
        return Err(KernelRefusal::internal(KernelStage::Validate, "breakout.outer_loop", "the convicted loop is the face's own outer loop"));
    }
    let hole = &polylines[breach.loop_index];

    let mut neighbours: Vec<u64> = Vec::new();
    for coedge in &face.loops[outer_index].coedges {
        let side = coedge_parameter_polyline(coedge)
            .map_err(|error| error.with_message(|error| format!("an outer coedge could not be traced: {error}")))?;
        if !polylines_cross(hole, &side) {
            continue;
        }
        for other in solid.shells.iter().flat_map(|shell| &shell.faces) {
            if other.id == face.id || other.id == wall {
                continue;
            }
            if other
                .loops
                .iter()
                .any(|wire| wire.coedges.iter().any(|use_| use_.edge_id == coedge.edge_id))
                && !neighbours.contains(&other.id)
            {
                neighbours.push(other.id);
            }
        }
    }
    Ok(neighbours)
}

/// Every face across the breached face's OUTER boundary — the faces a mouth
/// leaving that face can have arrived on.
///
/// A migration is a mouth that has walked off one face and onto the next. The
/// next one is over an edge, so this is the candidate set, and it is small: the
/// outer loop's own neighbours, the wall excepted. A carrier that has reached
/// something further away than that is not a case this route claims, and it
/// says so rather than widening until it finds a face it can cut.
fn faces_over_the_outer_boundary(
    solid: &BrepSolid,
    breach: &HoleBreach,
    wall: u64,
) -> Result<Vec<u64>, KernelRefusal> {
    let face = face_by_id(solid, breach.face)?;
    let polylines = face
        .loops
        .iter()
        .map(loop_parameter_polyline)
        .collect::<Result<Vec<_>, KernelRefusal>>()
        .unwrap_or_default();
    let outer_index = (0..polylines.len())
        .max_by(|&a, &b| {
            polyline_area(&polylines[a])
                .abs()
                .total_cmp(&polyline_area(&polylines[b]).abs())
        })
        .unwrap_or(0);
    let mut over: Vec<u64> = Vec::new();
    for coedge in &face.loops[outer_index].coedges {
        for other in solid.shells.iter().flat_map(|shell| &shell.faces) {
            if other.id == face.id || other.id == wall {
                continue;
            }
            if other
                .loops
                .iter()
                .any(|wire| wire.coedges.iter().any(|use_| use_.edge_id == coedge.edge_id))
                && !over.contains(&other.id)
            {
                over.push(other.id);
            }
        }
    }
    Ok(over)
}

/// One coedge as a polyline in its face's parameters, END POINT INCLUDED, so
/// the segments it yields are the whole of that coedge.
///
/// [`loop_parameter_polyline`] deliberately leaves each coedge's end point to
/// the next coedge's start; a single coedge asked about on its own has no next.
fn coedge_parameter_polyline(
    coedge: &crate::topology::CoedgeRecord,
) -> Result<Vec<(f64, f64)>, KernelRefusal> {
    let pcurve = &coedge.pcurve;
    let [d0, d1] = pcurve.domain().or_refuse(KernelStage::Validate, "domain")?;
    let steps = if pcurve.degree <= 1 && pcurve.control_points.len() == 2 {
        1
    } else {
        let mut spans = 0usize;
        for window in pcurve.knots.windows(2) {
            if window[1] > window[0] {
                spans += 1;
            }
        }
        spans.max(1) * LOOP_SAMPLES_PER_SPAN
    };
    let mut points = Vec::with_capacity(steps + 1);
    for step in 0..=steps {
        let uv = pcurve.evaluate(d0 + (d1 - d0) * (step as f64 / steps as f64)).or_refuse(KernelStage::Validate, "evaluate")?;
        points.push((uv.x, uv.y));
    }
    Ok(points)
}

/// Does the CLOSED polyline `loop_points` cross the OPEN polyline `side`?
/// Proper segment intersection, in the face's own parameters.
fn polylines_cross(loop_points: &[(f64, f64)], side: &[(f64, f64)]) -> bool {
    for index in 0..loop_points.len() {
        let a = loop_points[index];
        let b = loop_points[(index + 1) % loop_points.len()];
        for window in side.windows(2) {
            if segments_cross(a, b, window[0], window[1]) {
                return true;
            }
        }
    }
    false
}

fn segments_cross(a: (f64, f64), b: (f64, f64), c: (f64, f64), d: (f64, f64)) -> bool {
    let orient = |p: (f64, f64), q: (f64, f64), r: (f64, f64)| {
        (q.0 - p.0) * (r.1 - p.1) - (q.1 - p.1) * (r.0 - p.0)
    };
    let (d1, d2) = (orient(c, d, a), orient(c, d, b));
    let (d3, d4) = (orient(a, b, c), orient(a, b, d));
    ((d1 > 0.0) != (d2 > 0.0)) && ((d3 > 0.0) != (d4 > 0.0))
}

/// THE BREAKOUT, reached from the containment floor instead of from the scan.
///
/// The floor convicts a hole loop that has left the face it is recorded on,
/// planar or curved. Where that hole has left ONTO a named neighbour — the case the operator
/// reported, an exit hole dragged across the edge between two faces — the right
/// body is the one the self-crossing repair already builds for a bore carried
/// out through a side wall: the moved carrier and the neighbour split along
/// their exact section, the pieces that ended up on the wrong side dropped, and
/// the faces left holding an unanswered boundary RE-TRIMMED, which merges the
/// mouth into the outer loops of the faces it now opens through.
///
/// What makes it the SAME repair and not a second one is that the extent is
/// still derived and not invented: the tool is the moved carrier trimmed
/// against the body, and where it meets the body is what the imprint computes.
/// The route in is what changes — the scan needs a mesh crossing it can
/// confirm, and at the near edge of a breakout the crossing is a sliver the
/// tessellation does not resolve, so the floor sees it first.
///
/// The genuine runaway is excluded by GEOMETRY and not by a threshold. A bore
/// carried clean off the body crosses no boundary of the face it left, so
/// [`breakout_pair`] finds no neighbour and this refuses before a single face
/// is split; and even given a pair, the imprint of a carrier that misses the
/// body cuts neither trim and [`split_and_drop`] declines on its own terms.
fn repair_hole_breakout(solid: &BrepSolid, breach: &HoleBreach) -> Result<BrepSolid, KernelRefusal> {
    let origin = || RepairOrigin::Breakout {
        face: breach.face,
        loop_id: breach.loop_id,
    };
    if breach.banded {
        return Err(KernelRefusal::unsupported(
            KernelStage::Validate,
            "breakout.band_face",
            format!(
                "face {} is a band bounded by rings that wind its carrier's period, with no outer \
                 loop in its parameter plane; this route re-trims a hole against the outer loop \
                 it straddles and does not re-trim a band",
                breach.face,
            ),
        ));
    }
    // THE STRADDLE: part of the mouth still inside the face it was recorded on,
    // part outside. Which neighbour is read off the crossing, exactly.
    match breakout_pair(solid, breach) {
        Ok(BreakoutPair { wall, neighbour }) => {
            return split_and_drop(solid, wall, neighbour, origin())
        }
        // Every refusal but the non-straddle is this route's own, classed as it
        // always was here; the non-straddle is read off its slug, not its text.
        Err(reason) if !is_not_a_straddle(&reason) => {
            return Err(KernelRefusal::unsupported(
                KernelStage::Validate,
                "breakout.pair",
                reason.message,
            ))
        }
        Err(_) => {}
    }

    // THE MIGRATION, or the RUNAWAY. The mouth is wholly outside the face it
    // was recorded on, so nothing in that face's own parameter plane can say
    // where it went — a mouth that has walked onto the next face and a mouth
    // that has left the solid look the same from there.
    //
    // They do not look the same to the IMPRINT. The question that separates
    // them is whether the moved carrier still meets a face of the body, and
    // `split_and_drop` answers exactly that: handed a face the carrier misses
    // it declines with "the carriers' intersection reaches ... neither", before
    // anything is dropped. So the candidates are tried, and the answer is
    // decided BY THE RESULT — the same principle the drop enumeration inside
    // already runs on, and a discriminator that cannot drift, because a carrier
    // clear of the body cuts nothing anywhere.
    //
    // More than one candidate building is not an answer: the mouth would be on
    // two faces at once with no reason to prefer either, and choosing would be
    // the guess this module exists not to make.
    let wall = breakout_wall(solid, breach).map_err(|refusal| {
        KernelRefusal::internal(KernelStage::Validate, "breakout.wall", refusal.message)
    })?;
    let mut candidates = faces_over_the_outer_boundary(solid, breach, wall)
        .map_err(|refusal| {
            KernelRefusal::internal(KernelStage::Validate, "breakout.candidates", refusal.message)
        })?;
    if candidates.is_empty() {
        return Err(KernelRefusal::unsupported(
            KernelStage::Validate,
            "breakout.runaway",
            RUNAWAY,
        ));
    }

    // WHICH face the mouth moved onto, when its own face's parameter plane can
    // no longer say. The carrier is a whole surface and generally meets the
    // body in more than one place — a through bore leaves by one face and
    // enters by another, and both are faces this carrier can be re-trimmed
    // against — so "some face it cuts" is not the answer. The answer is the
    // face the MIGRATED MOUTH IS AT: the candidates are ordered by the least
    // 3D distance from the convicted hole loop to the face's own trim, and the
    // nearest one that the repair can actually build is taken.
    //
    // A TIE is refused rather than broken. Two faces the same distance from the
    // mouth means the mouth sits on the edge between them, which is a corner
    // breakout — the case whose two re-trims bound each other.
    let mouth = hole_loop_in_space(solid, breach).map_err(|refusal| {
        KernelRefusal::internal(KernelStage::Validate, "breakout.mouth", refusal.message)
    })?;
    let scale = crate::solid_scale(solid);
    let mut ranked: Vec<(f64, u64)> = Vec::new();
    for candidate in candidates.drain(..) {
        ranked.push((
            distance_to_face(solid, candidate, &mouth)
                .map_err(|refusal| {
                    KernelRefusal::internal(KernelStage::Validate, "breakout.distance", refusal.message)
                })?,
            candidate,
        ));
    }
    ranked.sort_by(|a, b| a.0.total_cmp(&b.0));
    if ranked.len() > 1 && (ranked[1].0 - ranked[0].0).abs() <= scale * 1e-9 {
        return Err(KernelRefusal::ill_posed(
            KernelStage::Validate,
            "breakout.corner_tie",
            format!(
                "the migrated mouth is the same distance ({:.6}) from faces {} and {}, so the body \
                 does not say which of them it moved onto — a breakout over the corner between \
                 them, where the two re-trims bound each other",
                ranked[0].0, ranked[0].1, ranked[1].1,
            ),
        ));
    }

    let carried = take_crossing_repairs();
    let mut declined: Vec<String> = Vec::new();
    for (distance, candidate) in ranked {
        match split_and_drop(solid, wall, candidate, origin()) {
            Ok(body) => {
                // The trial that built pushed a record; the ones that declined
                // pushed none. Keep that one and put the caller's ledger back.
                let mine = take_crossing_repairs();
                restore_repairs(carried.into_iter().chain(mine).collect());
                return Ok(body);
            }
            Err(reason) => {
                declined.push(format!("face {candidate} at {distance:.6}: {reason}"))
            }
        }
    }
    restore_repairs(carried);
    Err(KernelRefusal::unsupported(
        KernelStage::Validate,
        "breakout.runaway",
        format!(
            "{RUNAWAY} — the moved carrier was offered every face across that boundary, nearest \
             first, and cut none of them ({})",
            declined.join(" | ")
        ),
    ))
}

/// The convicted hole loop as 3D points: its pcurves are in the breached face's
/// own parameters, so they are read back through that face's surface — the same
/// road the floor takes, and the reason the floor was never wrong about where a
/// loop is.
fn hole_loop_in_space(solid: &BrepSolid, breach: &HoleBreach) -> Result<Vec<crate::Vec3>, KernelRefusal> {
    let face = face_by_id(solid, breach.face)?;
    let uv = loop_parameter_polyline(&face.loops[breach.loop_index])?;
    uv.into_iter()
        .map(|(u, v)| face.surface.evaluate(u, v))
        .collect::<Result<Vec<_>, _>>()
        .map_err(|error| format!("the convicted hole loop could not be read in space: {error}"))
        .or_refuse(KernelStage::Validate, "evaluate")
}

/// The least distance from a set of points to a face's own TRIM — sampled, in
/// the face's parameters, so a point beside a face's carrier but off the end of
/// the face does not read as being on it.
fn distance_to_face(
    solid: &BrepSolid,
    face_id: u64,
    points: &[crate::Vec3],
) -> Result<f64, KernelRefusal> {
    const STATIONS: usize = 32;
    let face = face_by_id(solid, face_id)?;
    let polylines = face
        .loops
        .iter()
        .map(loop_parameter_polyline)
        .collect::<Result<Vec<_>, KernelRefusal>>()?;
    let outer_index = (0..polylines.len())
        .max_by(|&a, &b| {
            polyline_area(&polylines[a])
                .abs()
                .total_cmp(&polyline_area(&polylines[b]).abs())
        })
        .unwrap_or(0);
    let bands = BoundaryBands::build(&polylines[outer_index]);
    let (mut lo, mut hi) = ((f64::MAX, f64::MAX), (f64::MIN, f64::MIN));
    for &(u, v) in &polylines[outer_index] {
        lo = (lo.0.min(u), lo.1.min(v));
        hi = (hi.0.max(u), hi.1.max(v));
    }
    let mut best = f64::INFINITY;
    for iu in 0..=STATIONS {
        for iv in 0..=STATIONS {
            let u = lo.0 + (hi.0 - lo.0) * iu as f64 / STATIONS as f64;
            let v = lo.1 + (hi.1 - lo.1) * iv as f64 / STATIONS as f64;
            if !bands.contains((u, v)) {
                continue;
            }
            if polylines
                .iter()
                .enumerate()
                .any(|(index, hole)| index != outer_index && inside_polyline((u, v), hole))
            {
                continue;
            }
            let Ok(point) = face.surface.evaluate(u, v) else {
                continue;
            };
            for probe in points {
                best = best.min(probe.sub(point).length());
            }
        }
    }
    Ok(best)
}

/// Even-odd parity against a closed polyline, for the hole loops the band index
/// is not built over.
fn inside_polyline(point: (f64, f64), polygon: &[(f64, f64)]) -> bool {
    let (u, v) = point;
    let mut parity = false;
    for index in 0..polygon.len() {
        let (ax, ay) = polygon[index];
        let (bx, by) = polygon[(index + 1) % polygon.len()];
        if (ay > v) != (by > v) {
            let x = ax + (v - ay) / (by - ay) * (bx - ax);
            if x > u {
                parity = !parity;
            }
        }
    }
    parity
}

/// The sentence a hole that has left the BODY earns, as opposed to one that has
/// moved onto another face. It is the one thing standing between the operator
/// and a silently ruined model, so it is a constant and not a phrasing.
const RUNAWAY: &str = "the runaway hole has left the face it was recorded on without arriving \
     on another: the moved carrier meets no face of the body, so there is nothing for it to be \
     re-trimmed against and no cut to perform at any extent";

/// [`resolve_face_crossing`] for a pair this module has named itself rather
/// than read off the scan.
///
/// The scan is one way to find two faces of one body that have to be split
/// along their section, and the containment floor is another. Both hand the
/// same pair to the same repair.
pub fn resolve_face_crossing_between(
    solid: &BrepSolid,
    face_a: u64,
    face_b: u64,
) -> Result<BrepSolid, KernelRefusal> {
    let report = scan(solid, "resolve_face_crossing_between")?;
    split_and_drop(solid, face_a, face_b, RepairOrigin::Scan(&report))
}

// ---------------------------------------------------------------------------
// The repair
// ---------------------------------------------------------------------------

/// WHAT FOUND the pair this repair is about to split.
///
/// Two floors reach the same repair. The scan finds two faces whose meshes
/// cross and can say where and by how much; the hole-containment floor finds a
/// hole loop that has left its face and can say which loop and which face. The
/// repair does the same work either way, and the ledger says which asked for
/// it rather than printing a crossing point that was never measured.
enum RepairOrigin<'a> {
    /// The self-intersection scan confirmed a crossing pair.
    Scan(&'a SelfIntersectionReport),
    /// The containment floor convicted a hole loop that has broken out onto a
    /// neighbouring face.
    Breakout { face: u64, loop_id: u64 },
}

/// The operand tag this repair works under. The solid is its own and only
/// operand: what is being resolved is a body crossing ITSELF, so there is no
/// second operand to be the other side of.
const SELF_OPERAND: u8 = 0;

/// How many pieces the crossing may cut the two faces into before this repair
/// declines. Every way of dropping some of them is REBUILT and re-checked, so
/// the enumeration has to stay small enough to run in full: at eight pieces
/// there are at most 225 candidate drops, nearly all of which the re-trim
/// closure ends before anything is assembled. The variable-radius fillet's
/// crossing cuts four pieces and the two-bore fixture's cuts six.
const MAXIMUM_CROSSING_PIECES: usize = 8;

/// Split two faces of ONE body that cross each other along their carriers'
/// exact intersection, drop the material the crossing encloses, and re-sew.
///
/// This is the bounded half of the self-intersection answer. Where two walls of
/// one body overshoot past each other instead of meeting at an edge, the
/// crossing curve IS the edge they should have met at: imprinting it onto both
/// faces cuts each into the part that belongs and the tail that overshot, and
/// dropping both tails leaves a body sewn along that curve.
///
/// **What it refuses, and why each refusal is a bound rather than a shrug:**
///
/// * A FOLD inside one face. There is no second face to imprint against, and
///   the answer is a trim that was never constructed rather than material to
///   drop — `offset/carve.rs` owns it.
/// * More than one crossing pair. Each repair changes the faces the next one
///   would be measured against, so resolving them together is a different
///   problem from resolving one.
/// * A crossing whose carriers' intersection does not cut BOTH trims. Then the
///   mesh crossing is not a curve the faces share and there is nothing to split
///   along.
/// * More pieces than `MAXIMUM_CROSSING_PIECES`, because every candidate drop
///   is rebuilt in full and the enumeration has to run to the end rather than
///   be sampled.
/// * Any split whose reassembly does not come back SOUND. Every way of dropping
///   some pieces of each face — keeping at least one of each — is enumerated,
///   closed under the re-trim, rebuilt and re-checked in full (`validate()`
///   clean, connected, and scanning clean), so the piece choice is decided by
///   the result rather than guessed from a normal. If none verifies, the solid
///   is returned untouched and the caller refuses: a repair that cannot prove
///   its own answer must not ship one.
/// * A split that MORE THAN ONE drop resolves. Two sound answers is not an
///   answer, and choosing between them would be the guess this repair exists
///   not to make.
///
/// **The re-trim is the other half of the split.** Dropping a tail also removes
/// that tail's boundary uses on the NEIGHBOURING faces it bordered, and a body
/// cannot close over an edge only one face answers for. So the drop is closed
/// under "a face left holding an unmatched boundary goes too" — which, since
/// the imprint already cut those neighbours where the crossing curve met their
/// edges, drops the part of each that the crossing cut off. The closure is
/// bounded by the faces the imprint TOUCHED: reaching any other face ends that
/// candidate. Nothing here edits a loop by hand; every piece it chooses between
/// came out of the imprint/fragment machinery.
///
/// **And the drop RETIRES what the old configuration left.** The touched bound
/// is about material the crossing encloses, and it does not reach the third
/// thing a moved carrier leaves on a body: a piece, or a whole loop, that the
/// NEW configuration has no room for — a bore's exit ring still recorded on the
/// top face after the exit has migrated onto the side. Neither is material this
/// drop ran past; both are held by nothing the drop keeps, and keeping either
/// cannot close. `fragment_is_detached` retires the one the fragmenter
/// returns as its own island and `holds_only_stale_loops` the one it returns
/// as a hole loop that has left its face, and the ledger says a repair retired
/// something rather than leaving it to be read off the cell counts.
///
/// **A census has two directions and only one was enforced.** An edge used ONCE
/// is a face left holding an unanswered boundary, above. An edge used THREE
/// times is three faces along one curve, which is not a body either, and the
/// assembly does not refuse it — it puts the third face on a shell of its own
/// and every floor in this repository then passes the result.
/// `over_used_edge` refuses that candidate where the third use still exists,
/// which is here. It is what stopped the turned-bore migration returning a body
/// 23% out on area.
pub fn resolve_face_crossing(solid: &BrepSolid) -> Result<BrepSolid, KernelRefusal> {
    let options = SelfIntersectionOptions::for_solid(solid);
    let report = solid_self_intersections(solid, options).map_err(|message| {
        KernelRefusal::internal(
            KernelStage::Validate,
            "soundness_scan",
            format!("the soundness scan could not run: {message}"),
        )
    })?;
    if let Some(fold) = report.folds.first() {
        return Err(KernelRefusal::unsound(
            KernelStage::Validate,
            SoundnessDefect::Fold,
            report.folds.iter().map(|fold| fold.face).collect(),
            format!(
                "face {} folds through ITSELF; a fold is a trim that was never constructed and \
                 belongs to the carrier carve, not to this repair",
                fold.face
            ),
        ));
    }
    if report.confirmed.is_empty() {
        return Ok(solid.clone());
    }
    if report.confirmed.len() > 1 {
        return Err(KernelRefusal::unsupported(
            KernelStage::Validate,
            "crossing.multiple_pairs",
            format!(
                "{} crossing face pairs; each repair changes the faces the next would be measured \
                 against, so this repair takes one pair at a time",
                report.confirmed.len()
            ),
        ));
    }
    let crossing = &report.confirmed[0];
    split_and_drop(solid, crossing.face_a, crossing.face_b, RepairOrigin::Scan(&report))
}

/// One crossing pair, split and resolved. Separated from the scan so the
/// refusals above read as one list.
fn split_and_drop(
    solid: &BrepSolid,
    face_a: u64,
    face_b: u64,
    origin: RepairOrigin<'_>,
) -> Result<BrepSolid, KernelRefusal> {
    use crate::fragment::fragment_solid;
    use crate::imprint::{FaceKey, ImprintOptions};
    use crate::{apply_edge_splits, build_imprints};

    let scale = crate::solid_scale(solid);
    let tolerance = (scale * 1e-7).max(1e-9);
    if trace_on() {
        eprintln!("HEAL split_and_drop: faces {face_a} | {face_b}, scale {scale:.6}");
        trace_solid("entry solid", solid);
    }

    // Each face as its own one-face open body, which is what the imprint
    // driver pairs: it walks operand A's faces against operand B's, and a
    // solid paired with itself would pair every face with itself first.
    let open_a = one_face_body(solid, face_a)?;
    let open_b = one_face_body(solid, face_b)?;
    let mut request = ImprintOptions {
        tolerance,
        // The GLOBAL interpolation lane: the section the repair cuts along is
        // the faithful one. On the only row this repair takes
        // (`FilletFailureWith2RasiusValues`) the global lane's section lies
        // 1.6e-6 from its two carriers where the local lane's lies 7.8e-4, and
        // it moves every face the section bounds onto its closed form (`Pin_S`
        // 1.6e-4 -> 4.4e-7). The local lane was asked for until
        // `process_curve` split an edge a section ENDS on whatever the angle:
        // that section is tangent to the bore's mouth ring at both ends, the
        // global fit reproduces the tangency, and the old gates left the ring
        // whole, so the repair succeeded only on the local fit's 1.4-degree
        // end-tangent error. `BREP_HEAL_FIT_LANE` re-measures it.
        local_fit: false,
        // The station budget is the imprint's own (the ladder climbs it to
        // twice that). `96` was `offset_shell`'s: every section this repair
        // reaches is a sparse march (68 points on the fillet row, 7 and 8 on
        // `BadBoolean`) that saturates either budget, bit-identically.
        ..ImprintOptions::default()
    };
    measured_fit_request(&mut request);
    let mut imprint = build_imprints(&open_a, &open_b, &request).map_err(|error| {
        error.with_message(|error| format!("the two carriers could not be imprinted: {error}"))
    })?;

    // Both faces belong to ONE body, so both sides of the imprint are rewritten
    // onto that body's operand. The face ids are the solid's own and cannot
    // collide, so this is a relabel and not a merge.
    for face in &mut imprint.by_face {
        face.operand = SELF_OPERAND;
    }
    for split in &mut imprint.edge_splits {
        split.operand = SELF_OPERAND;
    }
    for piece in &mut imprint.pieces {
        for support in &mut piece.support_faces {
            *support = FaceKey {
                operand: SELF_OPERAND,
                face_id: support.face_id,
            };
        }
        for pcurve in &mut piece.pcurves {
            pcurve.operand = SELF_OPERAND;
        }
    }
    for barrier in &mut imprint.barrier_edges {
        barrier.0 = SELF_OPERAND;
    }

    if trace_on() {
        trace_imprint(&imprint);
    }

    let cut_a = imprint
        .by_face
        .iter()
        .any(|face| face.face_id == face_a && !face.piece_ids.is_empty());
    let cut_b = imprint
        .by_face
        .iter()
        .any(|face| face.face_id == face_b && !face.piece_ids.is_empty());
    if !cut_a || !cut_b {
        return Err(KernelRefusal::unsupported(
            KernelStage::Fragment,
            "crossing.no_shared_curve",
            format!(
                "the carriers' intersection reaches {} of the two trims, so there is no shared \
                 curve to split along",
                match (cut_a, cut_b) {
                    (true, false) => format!("only face {face_a}"),
                    (false, true) => format!("only face {face_b}"),
                    _ => "neither".into(),
                }
            ),
        ));
    }

    let split = apply_edge_splits(solid, SELF_OPERAND, &imprint).map_err(|error| {
        error.with_message(|error| format!("the imprinted edge splits could not be applied: {error}"))
    })?;
    let fragments = fragment_solid(&split, SELF_OPERAND, &imprint).map_err(|error| {
        error.with_message(|error| format!("the split faces could not be fragmented: {error}"))
    })?;

    if trace_on() {
        trace_solid("split solid", &split);
        trace_fragments(&fragments);
    }

    // The faces the imprint TOUCHED: the two that cross, plus every face whose
    // own loop carries an edge the imprint split. Those are exactly the faces
    // the dropped material can border — the crossing curve ends ON a boundary
    // edge of each crossing face, and that edge is shared with a neighbour — so
    // they are the only faces this repair may re-trim. Anything beyond them is
    // material this repair has not been shown to own, and reaching one ends the
    // candidate rather than widening the drop.
    let touched = touched_faces(solid, &imprint, face_a, face_b);
    // A POLE edge is legitimately used once — it is a collapsed boundary, not a
    // shared one — so it never means the face beside it bordered dropped
    // material. Left in the census it would drop the fragment holding it, and
    // the cascade would refuse a body the repair could have resolved.
    let degenerate: rustc_hash::FxHashSet<EdgeKey> = split
        .edges
        .iter()
        .filter(|edge| edge.degenerate)
        .map(|edge| EdgeKey::Boundary(SELF_OPERAND, edge.id))
        .collect();

    let pieces_of = |face: u64| -> Vec<usize> {
        fragments
            .iter()
            .enumerate()
            .filter_map(|(index, fragment)| (fragment.source_face_id == face).then_some(index))
            .collect()
    };
    let pieces_a = pieces_of(face_a);
    let pieces_b = pieces_of(face_b);
    // How many pieces the section has to cut, and it is not the same question
    // on both roads.
    //
    // From the SCAN, two faces overshoot PAST each other and the crossing curve
    // is the edge they should have met at, so it runs across both trims and
    // each has a tail: both sides must come out in two or more pieces, and one
    // piece is a section that grazed a trim rather than cut it.
    //
    // From the containment floor, the shape is different and the difference is
    // the whole of a COMPLETED MIGRATION. When a bore's mouth has walked
    // entirely onto the next face, the moved carrier meets that face in a
    // CLOSED loop inside its trim — the new mouth — which cuts nothing off it;
    // the face comes back as one piece carrying a new hole, and the only tail
    // is the wall's. Demanding two pieces of both would refuse exactly the case
    // the operator asked for, so this road asks for two pieces on ONE side and
    // at least one on the other. Nothing about the enumeration changes: a face
    // with one piece has one drop set, the empty one.
    let (need_a, need_b) = match &origin {
        RepairOrigin::Scan(_) => (2, 2),
        RepairOrigin::Breakout { .. } => {
            if pieces_a.len().max(pieces_b.len()) >= 2 {
                (1, 1)
            } else {
                (2, 2)
            }
        }
    };
    if pieces_a.len() < need_a || pieces_b.len() < need_b {
        return Err(KernelRefusal::unsupported(
            KernelStage::Fragment,
            "crossing.one_sided_cut",
            format!(
                "the crossing cut face {face_a} into {} piece(s) and face {face_b} into {} — this \
                 repair resolves the case where the curve cuts BOTH trims, so one part of each is \
                 the tail that overshot",
                pieces_a.len(),
                pieces_b.len()
            ),
        ));
    }
    if pieces_a.len() + pieces_b.len() > MAXIMUM_CROSSING_PIECES {
        return Err(KernelRefusal::non_convergence(
            KernelStage::Fragment,
            "crossing.pieces",
            format!(
                "the crossing cut face {face_a} into {} piece(s) and face {face_b} into {}, which is \
                 {} pieces against this repair's budget of {MAXIMUM_CROSSING_PIECES} — every way to \
                 drop some of them is rebuilt and re-checked, so the enumeration is bounded rather \
                 than sampled",
                pieces_a.len(),
                pieces_b.len(),
                pieces_a.len() + pieces_b.len(),
            ),
        ));
    }

    // WHICH pieces are the tails is decided by the RESULT, not by a normal.
    // Every way to drop some pieces of each crossing face (keeping at least one
    // of each) is closed under the re-trim below, rebuilt in full and
    // re-checked; the repair is the answer only when exactly ONE of them
    // verifies.
    //
    // The enumeration runs under TWO closures, and the second only where the
    // first found nothing at all. [`DropClosure::Drop`] drops a neighbour the
    // drop leaves holding an unmatched boundary — the rule this repair has
    // always used, and the one every body it already resolves is decided by.
    // [`DropClosure::Retrim`] RE-TRIMS that neighbour instead. Ordering them
    // this way makes the second closure strictly additive: a body the first
    // resolves never reaches it, so no answer this repair already returns can
    // change. The price is the enumeration twice over on a body that refuses.
    let plan = DropPlan {
        split: &split,
        fragments: &fragments,
        imprint: &imprint,
        touched: &touched,
        degenerate: &degenerate,
        pieces_a: &pieces_a,
        pieces_b: &pieces_b,
        face_a,
        face_b,
        scale,
    };
    let (mut verified, refusals) = plan.enumerate(DropClosure::Drop);
    if verified.is_empty() {
        verified = plan.enumerate(DropClosure::Retrim).0;
    }

    // Several drops verified: the rebuilt bodies cannot say which material the
    // crossing enclosed, but the body that crossed itself can. Its winding
    // number decides (`winding_choice`); `BREP_HEAL_WINDING_DECIDES=0` restores
    // the refusal.
    let winding_decides = switch!("BREP_HEAL_WINDING_DECIDES", true);
    if verified.len() > 1 && winding_decides {
        let pieces: Vec<usize> = pieces_a.iter().chain(pieces_b.iter()).copied().collect();
        if let Some(index) = winding_choice(solid, &fragments, &pieces, &verified) {
            if trace_on() {
                eprintln!("HEAL winding decides: candidate {index} of {}", verified.len());
            }
            let chosen = verified.swap_remove(index);
            verified = vec![chosen];
        }
    }
    if verified.len() > 1 {
        return Err(KernelRefusal::ill_posed(
            KernelStage::Select,
            "crossing.ambiguous_drop",
            format!(
                "{} different drops of the crossing pieces all rebuilt into a sound body, so the \
                 result does not decide which material the crossing encloses; this repair answers \
                 only where one does",
                verified.len()
            ),
        ));
    }
    let Some(Candidate {
        dropped,
        retired,
        retrimmed,
        solid: repaired,
    }) = verified.pop()
    else {
        // The first few reasons, not all of them: eight pieces is up to 225
        // candidates and a refusal nobody can read is a refusal nobody uses.
        let listed = refusals
            .iter()
            .take(4)
            .cloned()
            .collect::<Vec<_>>()
            .join(" | ");
        // The candidate reasons (an over-used edge, a closure that consumed
        // every face, a rebuild that declined) are CONTEXT inside this one
        // refusal, whose class is minted here: the body still crosses itself.
        return Err(KernelRefusal::unsound(
            KernelStage::Sew,
            SoundnessDefect::Crossing,
            vec![face_a, face_b],
            format!(
                "none of the {} ways of dropping the crossing pieces rebuilt into a sound body \
                 ({listed}{}); the worst crossing was {:.3e} past its band, so the overshoot is \
                 real and it is the SPLIT that did not resolve it",
                refusals.len(),
                if refusals.len() > 4 {
                    format!(" | and {} more", refusals.len() - 4)
                } else {
                    String::new()
                },
                match &origin {
                    RepairOrigin::Scan(report) => report
                        .confirmed
                        .first()
                        .map(|crossing| crossing.straddle)
                        .unwrap_or_default(),
                    RepairOrigin::Breakout { .. } => 0.0,
                }
            ),
        ));
    };

    record_repair(
        &fragments,
        &dropped,
        &retired,
        &retrimmed,
        face_a,
        face_b,
        &origin,
    );
    Ok(repaired)
}

/// One candidate answer: the fragments the drop removed, the ones it RETIRED
/// as detached, the faces it RE-TRIMMED rather than removed, and the body
/// those choices rebuilt into.
struct Candidate {
    dropped: Vec<usize>,
    retired: Vec<u64>,
    retrimmed: Vec<u64>,
    solid: BrepSolid,
}


/// Which of several verified drops is the body that crossed itself meant to
/// be? Its material is where its WINDING NUMBER is at least one: two holes
/// widened into each other remove their overlap twice, so the plate winds once
/// round a point of the plate, not at all round a point of one hole, and -1
/// round a point of both, which is void like the rest of the opening. A piece
/// of a crossing face is boundary of that material exactly when the side its
/// outward normal leaves winds below one and the side it enters winds one or
/// more; every other piece is a tail to drop.
///
/// The winding is read off the unrepaired body's watertight mesh, the sum of
/// its triangles' solid angles (exact for any closed mesh, crossing or not),
/// at two points a little either side of each piece's interior test point.
/// The offset is many chord tolerances, so the mesh's chord error cannot put
/// a point on the wrong side of its own face. Any winding that does not come
/// out within a quarter of an integer, a piece whose material lies on the
/// wrong side of it (a drop never turns a face over), or anything other than
/// exactly one candidate keeping exactly the predicted pieces, answers
/// nothing, and the caller's ambiguity refusal stands.
fn winding_choice(
    solid: &BrepSolid,
    fragments: &[crate::fragment::FaceFragmentRecord],
    pieces: &[usize],
    verified: &[Candidate],
) -> Option<usize> {
    let scale = crate::solid_scale(solid);
    let offset = scale * 1e-3;
    let mesh = crate::tessellate_brep_watertight(solid, offset / 20.0).ok()?;
    let point = |index: u32| {
        let at = 3 * index as usize;
        crate::Vec3::new(mesh.positions[at], mesh.positions[at + 1], mesh.positions[at + 2])
    };
    let winding = |q: crate::Vec3| -> Option<i64> {
        let mut total = 0.0;
        for triangle in mesh.indices.chunks_exact(3) {
            let a = point(triangle[0]).sub(q);
            let b = point(triangle[1]).sub(q);
            let c = point(triangle[2]).sub(q);
            let (la, lb, lc) = (a.length(), b.length(), c.length());
            let numerator = a.dot(b.cross(c));
            let denominator = la * lb * lc + a.dot(b) * lc + a.dot(c) * lb + b.dot(c) * la;
            total += 2.0 * numerator.atan2(denominator);
        }
        let turns = total / (4.0 * std::f64::consts::PI);
        ((turns - turns.round()).abs() < 0.25).then_some(turns.round() as i64)
    };
    let mut keep: Vec<(usize, bool)> = Vec::with_capacity(pieces.len());
    for &index in pieces {
        let fragment = fragments.get(index)?;
        let normal = fragment.surface.normal(fragment.test_uv.x, fragment.test_uv.y).ok()?;
        let normal = normal.normalized().ok()?;
        let outward = if fragment.same_sense { normal } else { normal.scale(-1.0) };
        let outside = winding(fragment.test_point.add(outward.scale(offset)))?;
        let inside = winding(fragment.test_point.sub(outward.scale(offset)))?;
        match (inside >= 1, outside >= 1) {
            (true, false) => keep.push((index, true)),
            (false, true) => return None,
            _ => keep.push((index, false)),
        }
        if trace_on() {
            eprintln!(
                "HEAL winding: piece {index} of face {} inside {inside} outside {outside}",
                fragment.source_face_id
            );
        }
    }
    let mut matching = verified.iter().enumerate().filter(|(_, candidate)| {
        keep.iter().all(|&(index, kept)| candidate.dropped.contains(&index) != kept)
    });
    let first = matching.next()?.0;
    matching.next().is_none().then_some(first)
}

/// How a candidate drop closes over a neighbour it leaves holding a boundary
/// no other face answers for.
#[derive(Clone, Copy, Eq, PartialEq)]
enum DropClosure {
    /// DROP that neighbour too, iterated to a fixed point — [`close_drop_set`].
    Drop,
    /// RE-TRIM it — [`retrim_drop_set`]: take the unmatched coedges off it and
    /// re-link what remains into closed loops on the same carrier, with the
    /// same pcurves. The bound is the same one (a face the imprint never cut
    /// still ends the candidate); what changes is that a face it DID cut is
    /// re-trimmed rather than consumed.
    Retrim,
}

/// One crossing pair's split and everything a candidate drop is measured
/// against. Bundled so the enumeration can run once per closure.
struct DropPlan<'a> {
    split: &'a BrepSolid,
    fragments: &'a [crate::fragment::FaceFragmentRecord],
    imprint: &'a crate::imprint::ImprintResultRecord,
    touched: &'a rustc_hash::FxHashSet<u64>,
    degenerate: &'a rustc_hash::FxHashSet<EdgeKey>,
    pieces_a: &'a [usize],
    pieces_b: &'a [usize],
    face_a: u64,
    face_b: u64,
    scale: f64,
}

impl DropPlan<'_> {
    /// Every way of dropping some pieces of each crossing face, closed under
    /// `closure`, rebuilt in full and re-checked. The candidates that verified
    /// come back first, the reasons the rest did not second.
    fn enumerate(&self, closure: DropClosure) -> (Vec<Candidate>, Vec<String>) {
        let (face_a, face_b) = (self.face_a, self.face_b);
        let mut refusals: Vec<String> = Vec::new();
        let mut verified: Vec<Candidate> = Vec::new();
        let mut seen: Vec<(Vec<usize>, Vec<Vec<usize>>)> = Vec::new();
        for drop_a in proper_subsets(self.pieces_a) {
            for drop_b in proper_subsets(self.pieces_b) {
                let mut dropped: Vec<usize> =
                    drop_a.iter().chain(drop_b.iter()).copied().collect();
                if dropped.is_empty() {
                    continue;
                }
                let label = format!(
                    "dropping piece(s) {:?} of face {face_a} and {:?} of face {face_b}",
                    drop_a
                        .iter()
                        .map(|index| self
                            .pieces_a
                            .iter()
                            .position(|piece| piece == index)
                            .unwrap_or(0))
                        .collect::<Vec<_>>(),
                    drop_b
                        .iter()
                        .map(|index| self
                            .pieces_b
                            .iter()
                            .position(|piece| piece == index)
                            .unwrap_or(0))
                        .collect::<Vec<_>>(),
                );
                // The re-trim: a face that bordered dropped material is left
                // with a boundary use no other face answers, and a body cannot
                // close over one. Closing the drop under that rule is what
                // turns a two-face split into a re-trim of every face the
                // dropped material bordered.
                // The dropping closure leaves every fragment as it was, so it
                // borrows the list rather than copying it per candidate.
                let mut retired: Vec<u64> = Vec::new();
                let closed = match closure {
                    DropClosure::Drop => close_drop_set(
                        self.fragments,
                        &mut dropped,
                        &mut retired,
                        self.touched,
                        self.degenerate,
                    )
                    .map(|()| (None, Vec::new())),
                    DropClosure::Retrim => retrim_drop_set(
                        self.fragments,
                        &mut dropped,
                        &mut retired,
                        self.touched,
                        self.degenerate,
                    )
                    .map(|(working, retrimmed)| (Some(working), retrimmed)),
                };
                let (working, retrimmed) = match closed {
                    Ok(closed) => closed,
                    Err(reason) => {
                        if trace_on() {
                            println!("    candidate died at closure {label}: {reason}");
                        }
                        refusals.push(format!("{label}: {reason}"));
                        continue;
                    }
                };
                let working: &[crate::fragment::FaceFragmentRecord] =
                    working.as_deref().unwrap_or(self.fragments);
                dropped.sort_unstable();
                // Two seeds can close onto the same answer. The signature is
                // the drop AND what the re-trim left of each kept fragment, so
                // a second closure that re-trimmed differently is a different
                // candidate rather than a hidden duplicate.
                let signature = (
                    dropped.clone(),
                    working
                        .iter()
                        .enumerate()
                        .filter(|(index, _)| !dropped.contains(index))
                        .map(|(_, fragment)| {
                            fragment
                                .loops
                                .iter()
                                .map(|fragment_loop| fragment_loop.coedges.len())
                                .collect()
                        })
                        .collect(),
                );
                if seen.contains(&signature) {
                    continue;
                }
                seen.push(signature);
                if dropped.len() == working.len() {
                    refusals.push(format!("{label}: the re-trim consumed every face"));
                    continue;
                }
                // The census's OTHER direction — see [`over_used_edge`].
                if let Some((key, count)) = over_used_edge(working, &dropped, self.degenerate) {
                    refusals.push(format!(
                        "{label}: {} is held {count} times by the pieces this drop keeps, and a \
                         closed body holds every edge exactly twice",
                        match key {
                            EdgeKey::Boundary(_, edge) => format!("edge {edge}"),
                            EdgeKey::Imprint(piece) => format!("imprint piece {piece}"),
                        }
                    ));
                    if trace_on() {
                        println!(
                            "    candidate refused   {label}: {key:?} held {count} times"
                        );
                    }
                    continue;
                }
                let selected: Vec<_> = working
                    .iter()
                    .enumerate()
                    .filter(|(index, _)| !dropped.contains(index))
                    .map(|(_, fragment)| fragment.clone())
                    .collect();
                match rebuild(self.split, selected, self.imprint, self.scale) {
                    // `BREP_HEAL_TRACE=1` prints every candidate the
                    // enumeration considered, whether it verified, and what
                    // volume it produced. This is the instrument that found the
                    // 2026-09-22 stale-fragment defect: the `touched` bound
                    // kills most candidates before `verified.len() == 1` picks
                    // a survivor, and without the list there is no way to see
                    // that the survivor was not the right one.
                    Ok(solid) => {
                        if trace_on() {
                            println!(
                                "    CANDIDATE VERIFIED  {label}  volume {:.6}  dropped {:?}",
                                crate::solid_mass_properties(&solid)
                                    .map(|mass| mass.volume)
                                    .unwrap_or(f64::NAN),
                                dropped
                            );
                        }
                        verified.push(Candidate {
                            dropped,
                            retired,
                            retrimmed,
                            solid,
                        })
                    }
                    Err(reason) => {
                        if trace_on() {
                            println!("    candidate refused   {label}: {reason}");
                        }
                        refusals.push(format!("{label}: {reason}"))
                    }
                }
            }
        }
        (verified, refusals)
    }
}

/// The faces this repair may re-trim: the two that cross, and every face whose
/// own loops use an edge the imprint split.
fn touched_faces(
    solid: &BrepSolid,
    imprint: &crate::imprint::ImprintResultRecord,
    face_a: u64,
    face_b: u64,
) -> rustc_hash::FxHashSet<u64> {
    use rustc_hash::FxHashSet as HashSet;
    let split_edges: HashSet<u64> = imprint
        .edge_splits
        .iter()
        .filter(|split| split.operand == SELF_OPERAND)
        .map(|split| split.edge_id)
        .collect();
    let mut touched: HashSet<u64> = [face_a, face_b].into_iter().collect();
    for face in solid.shells.iter().flat_map(|shell| &shell.faces) {
        let uses_split = face
            .loops
            .iter()
            .flat_map(|loop_record| &loop_record.coedges)
            .any(|coedge| split_edges.contains(&coedge.edge_id));
        if uses_split {
            touched.insert(face.id);
        }
    }
    touched
}

/// Close a drop set under the rule a closed body cannot break: every edge is
/// used exactly twice. Dropping a tail takes its uses with it, so any face left
/// holding a boundary no other face answers bordered the dropped material and
/// has to be re-trimmed — which, since the imprint already cut it, means
/// dropping the part of it that the crossing cut off. Iterated to a fixed
/// point, and bounded by the touched set: reaching a face the imprint never cut
/// means the drop has left the material the crossing encloses, and the
/// candidate is refused rather than grown.
///
/// The one thing that bound does not own is a fragment joined to NOTHING the
/// drop keeps — see [`fragment_is_detached`]. That is not material the drop ran
/// past; it is what the old configuration left, and it is retired wherever it
/// came from.
fn close_drop_set(
    fragments: &[crate::fragment::FaceFragmentRecord],
    dropped: &mut Vec<usize>,
    retired: &mut Vec<u64>,
    touched: &rustc_hash::FxHashSet<u64>,
    degenerate: &rustc_hash::FxHashSet<EdgeKey>,
) -> Result<(), KernelRefusal> {
    use rustc_hash::FxHashMap as HashMap;
    loop {
        let mut uses: HashMap<EdgeKey, usize> = HashMap::default();
        for (index, fragment) in fragments.iter().enumerate() {
            if dropped.contains(&index) {
                continue;
            }
            for key in fragment
                .loops
                .iter()
                .flat_map(|fragment_loop| &fragment_loop.coedges)
                .filter_map(|coedge| edge_key(&coedge.source))
            {
                *uses.entry(key).or_default() += 1;
            }
        }
        let mut grew = false;
        for (index, fragment) in fragments.iter().enumerate() {
            if dropped.contains(&index) {
                continue;
            }
            // RETIRED, not re-trimmed: a fragment holding no edge that any
            // other kept fragment holds is attached to nothing this drop keeps.
            // The [`touched`] bound does not apply to it, because that bound
            // asks whether the drop has run past the material the crossing
            // encloses — and a piece joined to the body by nothing is not
            // material this drop reached, it is material the OLD configuration
            // left behind. Retiring it cannot cascade: the only uses it removes
            // are uses nothing else holds. See [`fragment_is_detached`].
            if fragment_is_detached(fragment, &uses, degenerate) {
                dropped.push(index);
                retired.push(fragment.source_face_id);
                grew = true;
                continue;
            }
            let unmatched = fragment
                .loops
                .iter()
                .flat_map(|fragment_loop| &fragment_loop.coedges)
                .filter_map(|coedge| edge_key(&coedge.source))
                .filter(|key| !degenerate.contains(key))
                .any(|key| uses.get(&key) == Some(&1));
            if !unmatched {
                continue;
            }
            if !touched.contains(&fragment.source_face_id) {
                return Err(KernelRefusal::internal(KernelStage::Validate, "retrim.uncut_face", format!(
                    "the re-trim reached face {}, which the imprint never cut — the drop has run \
                     past the material the crossing encloses",
                    fragment.source_face_id
                )));
            }
            dropped.push(index);
            grew = true;
        }
        if !grew {
            return Ok(());
        }
    }
}

/// [`close_drop_set`]'s other closure: a face left holding a boundary no other
/// face answers for is RE-TRIMMED rather than dropped.
///
/// Dropping it is the right answer when the unanswered boundary is the whole
/// of what that face contributed to the dropped material — a tail beside a
/// tail. It is the WRONG answer when the face is one the body keeps and only
/// part of its boundary went: a bore carried out through a side wall leaves
/// the mouth flat holding the stretch of its own corner edge that now runs
/// inside the bore, and the arc of the mouth ring that now runs outside the
/// body, and neither is material to drop — they are two coedges to take OFF a
/// face that otherwise stays exactly as it is. Taking them off merges that
/// face's mouth loop into its outer one, which is precisely the topology a
/// breakout has and a drop-only closure cannot reach.
///
/// The bound is [`close_drop_set`]'s: a face the imprint never cut still ends
/// the candidate rather than widening the re-trim, with the two exceptions that
/// are not re-trims at all — a fragment attached to nothing
/// ([`fragment_is_detached`]) and a whole loop that has left its face
/// ([`holds_only_stale_loops`]), both of which are retired. What comes back
/// is the fragment list to rebuild from — the untouched fragments as they were,
/// the re-trimmed ones with their loops re-linked — and the ids of the faces
/// that were re-trimmed, for the ledger. A fragment left with no coedge at all
/// joins `dropped`.
fn retrim_drop_set(
    fragments: &[crate::fragment::FaceFragmentRecord],
    dropped: &mut Vec<usize>,
    retired: &mut Vec<u64>,
    touched: &rustc_hash::FxHashSet<u64>,
    degenerate: &rustc_hash::FxHashSet<EdgeKey>,
) -> Result<(Vec<crate::fragment::FaceFragmentRecord>, Vec<u64>), KernelRefusal> {
    use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};

    let mut working = fragments.to_vec();
    let mut retrimmed: Vec<u64> = Vec::new();
    loop {
        let mut uses: HashMap<EdgeKey, usize> = HashMap::default();
        for (index, fragment) in working.iter().enumerate() {
            if dropped.contains(&index) {
                continue;
            }
            for key in fragment
                .loops
                .iter()
                .flat_map(|fragment_loop| &fragment_loop.coedges)
                .filter_map(|coedge| edge_key(&coedge.source))
            {
                *uses.entry(key).or_default() += 1;
            }
        }
        // RETIRED FIRST, and before the early return below, because a
        // detached fragment does not have to hold an UNMATCHED edge to be
        // detached — one whose loop walks its own seam twice holds that seam
        // twice itself, and the census reads two. See [`fragment_is_detached`].
        let mut grew = false;
        for index in 0..working.len() {
            if dropped.contains(&index) {
                continue;
            }
            if fragment_is_detached(&working[index], &uses, degenerate) {
                dropped.push(index);
                retired.push(working[index].source_face_id);
                grew = true;
            }
        }
        if grew {
            // The census is stale the moment anything is retired.
            continue;
        }
        // A POLE edge is legitimately used once, as in [`close_drop_set`].
        let unmatched: HashSet<EdgeKey> = uses
            .iter()
            .filter(|(key, count)| **count == 1 && !degenerate.contains(key))
            .map(|(key, _)| *key)
            .collect();
        if unmatched.is_empty() {
            return Ok((working, retrimmed));
        }
        for index in 0..working.len() {
            if dropped.contains(&index) {
                continue;
            }
            let holds = working[index]
                .loops
                .iter()
                .flat_map(|fragment_loop| &fragment_loop.coedges)
                .filter_map(|coedge| edge_key(&coedge.source))
                .any(|key| unmatched.contains(&key));
            if !holds {
                continue;
            }
            // A STALE LOOP is the other way the old configuration leaves
            // something behind, and the [`touched`] bound does not own it —
            // see [`holds_only_stale_loops`].
            let stale = holds_only_stale_loops(&working[index], &unmatched, degenerate);
            if !touched.contains(&working[index].source_face_id) && !stale {
                return Err(KernelRefusal::internal(KernelStage::Validate, "retrim.uncut_face", format!(
                    "the re-trim reached face {}, which the imprint never cut — the drop has run \
                     past the material the crossing encloses",
                    working[index].source_face_id
                )));
            }
            let face = working[index].source_face_id;
            match retrim_fragment(&working[index], &unmatched)? {
                Some(next) => {
                    working[index] = next;
                    if stale {
                        retired.push(face);
                    } else if !retrimmed.contains(&face) {
                        retrimmed.push(face);
                    }
                }
                None => dropped.push(index),
            }
            grew = true;
        }
        if !grew {
            return Ok((working, retrimmed));
        }
    }
}

/// Re-trim ONE fragment: take off every coedge in `remove` and re-link what is
/// left into closed loops.
///
/// The link is made in the face's OWN parameter domain, which is where a trim
/// loop is closed and the only frame that tells the two sides of a seam apart —
/// a cylinder's seam is one curve in 3D and two coedges a period apart here.
/// Nothing is fitted or re-parameterised: each surviving coedge keeps its
/// source, its sense and its pcurve, and only the ORDER and the grouping into
/// loops are rebuilt. A chain that does not close is a re-trim this repair
/// cannot prove, and it refuses rather than emitting an open face.
///
/// The loops come back in the order of their first surviving coedge, so a face
/// whose outer loop has just absorbed an inner one is still outer-loop-first.
/// `Ok(None)` when nothing at all is left to keep.
fn retrim_fragment(
    fragment: &crate::fragment::FaceFragmentRecord,
    remove: &rustc_hash::FxHashSet<EdgeKey>,
) -> Result<Option<crate::fragment::FaceFragmentRecord>, KernelRefusal> {
    use crate::fragment::{FaceFragmentRecord, FragmentCoedge, FragmentLoop};

    let kept: Vec<FragmentCoedge> = fragment
        .loops
        .iter()
        .flat_map(|fragment_loop| &fragment_loop.coedges)
        .filter(|coedge| !edge_key(&coedge.source).is_some_and(|key| remove.contains(&key)))
        .cloned()
        .collect();
    if kept.is_empty() {
        return Ok(None);
    }
    let ends = kept
        .iter()
        .map(|coedge| {
            let [t0, t1] = coedge.pcurve.domain()?;
            Ok((coedge.pcurve.evaluate(t0)?, coedge.pcurve.evaluate(t1)?))
        })
        .collect::<Result<Vec<_>, String>>().or_refuse(KernelStage::Validate, "collect")?;
    // The domain's own diagonal sets the band a junction is matched in: a
    // plane's parameters are millimetres and a cylinder's are radians against
    // millimetres, so one fixed number would mean two different things on the
    // two carriers. The ends being matched were cut from ONE curve by the
    // imprint's own split, so they agree to the fit's precision and the band
    // only has to be small against the face.
    let [u0, u1] = fragment.surface.domain_u().or_refuse(KernelStage::Validate, "domain_u")?;
    let [v0, v1] = fragment.surface.domain_v().or_refuse(KernelStage::Validate, "domain_v")?;
    let tolerance = 1e-4 * (u1 - u0).hypot(v1 - v0);
    let mut used = vec![false; kept.len()];
    let mut loops: Vec<FragmentLoop> = Vec::new();
    for seed in 0..kept.len() {
        if used[seed] {
            continue;
        }
        used[seed] = true;
        let mut chain = vec![seed];
        let start = ends[seed].0;
        let mut cursor = ends[seed].1;
        while cursor.sub(start).length() > tolerance {
            let next = (0..kept.len())
                .filter(|index| !used[*index])
                .map(|index| (index, ends[index].0.sub(cursor).length()))
                .filter(|(_, gap)| *gap <= tolerance)
                .min_by(|left, right| left.1.total_cmp(&right.1))
                .map(|(index, _)| index);
            let Some(next) = next else {
                return Err(KernelRefusal::internal(KernelStage::Validate, "retrim.open_chain", format!(
                    "face {}'s re-trim left a chain open at ({:.6}, {:.6}) in its own parameter \
                     domain — what the drop took off it does not close into a loop",
                    fragment.source_face_id, cursor.x, cursor.y
                )));
            };
            used[next] = true;
            chain.push(next);
            cursor = ends[next].1;
        }
        loops.push(FragmentLoop {
            coedges: chain.into_iter().map(|index| kept[index].clone()).collect(),
        });
    }
    Ok(Some(FaceFragmentRecord {
        loops,
        ..fragment.clone()
    }))
}

/// Is this fragment attached to NOTHING else the drop keeps?
///
/// The census `uses` counts every non-dropped fragment's edge keys, this one's
/// included. So a fragment is detached exactly when, for every non-degenerate
/// key it holds, the census count equals ITS OWN count of that key: nobody else
/// is holding any edge of it.
///
/// It is stated that way rather than as "every key it holds is used once"
/// because a fragment may legitimately hold one key TWICE on its own — a
/// cylinder band walks its seam up one side and down the other — and such a
/// fragment is still detached when the seam's only two uses are both its.
///
/// **What a detached fragment IS.** It is a piece of the old configuration that
/// the new one has no room for: on the turned-bore migration, the top face's
/// hole loop is the exit ring the bore no longer opens through, sitting out at
/// x = 44.6 where the top face's own square ends at 20. The fragmenter returns
/// it as its own island because the ring is disjoint from the square, and the
/// only other face holding that ring is the wall's overshoot tail. Drop the
/// tail and the island is joined to nothing.
///
/// **Why retiring it is bounded.** Keeping it can never produce a closed body:
/// every edge it holds is answered for only by itself, so the assembly is open
/// there however the rest of the drop goes. And retiring it removes only uses
/// nothing else holds, so it cannot leave a third face unmatched — it does not
/// cascade, and no candidate the enumeration would otherwise verify is lost.
fn fragment_is_detached(
    fragment: &crate::fragment::FaceFragmentRecord,
    uses: &rustc_hash::FxHashMap<EdgeKey, usize>,
    degenerate: &rustc_hash::FxHashSet<EdgeKey>,
) -> bool {
    use rustc_hash::FxHashMap as HashMap;
    let mut own: HashMap<EdgeKey, usize> = HashMap::default();
    for key in fragment
        .loops
        .iter()
        .flat_map(|fragment_loop| &fragment_loop.coedges)
        .filter_map(|coedge| edge_key(&coedge.source))
        .filter(|key| !degenerate.contains(key))
    {
        *own.entry(key).or_default() += 1;
    }
    // A fragment carrying no identified edge at all is NOT called detached:
    // its boundary is all DERIVED, which `edge_key` deliberately does not
    // count, and the rebuild's own closure is what answers for those.
    !own.is_empty()
        && own
            .iter()
            .all(|(key, count)| uses.get(key).copied().unwrap_or(0) == *count)
}

/// The OTHER way the old configuration leaves something behind: a WHOLE LOOP
/// of a face that nothing else answers for AND THAT HAS LEFT THAT FACE.
///
/// [`fragment_is_detached`] catches the leftover when the fragmenter returns it
/// as its own island — which it does when the stale ring is far enough from the
/// face's own trim to be plainly disjoint. When it is nearer, the same ring
/// comes back as a HOLE LOOP of the face it left: the turned bore's exit ring
/// at 40 degrees sits at `x = 22.9 - 30.7` against a top face that ends at 20,
/// and the top face is ONE fragment carrying it. There is no island to retire,
/// and taking the loop off is a RE-TRIM of a face the imprint never cut.
///
/// That is the move the earlier records named as missing — *"the departed face
/// keeps a hole loop the imprint never cut, and the drop enumeration has no
/// move for retiring one"*. This is the move, and it is bounded FOUR ways:
///
/// * **Unanswered for.** Every non-degenerate edge of the loop is used once,
///   by this fragment alone, so no other face the drop keeps is standing on it
///   and removing it takes nothing from anyone. A loop only PART of which is
///   unanswered for is a genuine re-trim of a neighbour that bordered the
///   dropped material, and the [`touched`] bound still owns that case.
/// * **It has LEFT the face**, by the containment floor's own test: every
///   sample of it lies outside the face's outer loop in that face's own
///   parameter plane. This is the condition that matters, and it was added
///   after measuring what happens without it. Unanswered-for alone is
///   SYMMETRIC — at 40 degrees it licensed retiring the bore's ENTRY mouth
///   just as readily as its departed exit ring, so the enumeration verified
///   two bodies and refused the pair as ambiguous. A loop still inside its own
///   face is trimming material and is not a leftover, whoever is holding it.
/// * **A loop survives.** This never empties a face; a fragment with nothing
///   left at all is the detached case above.
/// * **A PLANE only**, as the containment floor restricts itself, because on a
///   periodic surface "outside the outer loop" in the parameter plane is a
///   statement about the seam rather than about the trim.
///
/// And the body still has to rebuild, `validate()`, connect, scan clean and
/// pass the containment floor afterwards, exactly as before.
fn holds_only_stale_loops(
    fragment: &crate::fragment::FaceFragmentRecord,
    unmatched: &rustc_hash::FxHashSet<EdgeKey>,
    degenerate: &rustc_hash::FxHashSet<EdgeKey>,
) -> bool {
    if !matches!(
        fragment.surface.analytic(),
        Some(crate::AnalyticSurface::Plane { .. })
    ) {
        return false;
    }
    let mut stale: Vec<usize> = Vec::new();
    let mut surviving = 0usize;
    for (index, fragment_loop) in fragment.loops.iter().enumerate() {
        let keys: Vec<EdgeKey> = fragment_loop
            .coedges
            .iter()
            .filter_map(|coedge| edge_key(&coedge.source))
            .filter(|key| !degenerate.contains(key))
            .collect();
        if !keys.is_empty() && keys.iter().all(|key| unmatched.contains(key)) {
            stale.push(index);
            continue;
        }
        if keys.iter().any(|key| unmatched.contains(key)) {
            return false;
        }
        surviving += 1;
    }
    if stale.is_empty() || surviving == 0 {
        return false;
    }
    let Ok(polylines) = fragment
        .loops
        .iter()
        .map(fragment_loop_polyline)
        .collect::<Result<Vec<_>, KernelRefusal>>()
    else {
        return false;
    };
    if polylines.iter().any(Vec::is_empty) {
        return false;
    }
    let outer_index = (0..polylines.len())
        .max_by(|&a, &b| {
            polyline_area(&polylines[a])
                .abs()
                .total_cmp(&polyline_area(&polylines[b]).abs())
        })
        .unwrap_or(0);
    // The face's own outer loop being the stale one is not a leftover to
    // retire; it is the whole face going, which is the detached case.
    if stale.contains(&outer_index) {
        return false;
    }
    let outer = &polylines[outer_index];
    let bands = BoundaryBands::build(outer);
    let tolerance = boundary_tolerance(outer);
    stale.iter().all(|index| {
        polylines[*index]
            .iter()
            .all(|&point| !bands.contains(point) && !bands.within(point, tolerance))
    })
}

/// The other half of the census rule, and the one that had no enforcement.
///
/// A closed body uses every non-degenerate edge EXACTLY TWICE. The drop
/// closures above are the "fewer than twice" half — a face left holding a
/// boundary nobody answers is dropped or re-trimmed. Nothing checked the other
/// direction, and the direction matters here: an imprint piece is cut into BOTH
/// crossing faces, so before any drop it is held four times, once by each of
/// the four pieces. A drop that takes one piece away leaves it held three
/// times — three faces along one curve, which is not a body.
///
/// That is the candidate the turned-bore migration used to return: dropping the
/// exit mouth's disc alone leaves the wall's kept part, the wall's overshoot
/// tail and the mouth flat's remainder all on the mouth ring. Refusing it is
/// what leaves the one drop that is right.
///
/// **And nothing downstream catches it**, which is why the check belongs here
/// and not there. `assemble_open_fragments` resolves the third use by putting
/// the overshoot on a shell of its OWN: the body comes back with every edge
/// used exactly twice, `validate()` clean, `solid_connectivity` calling it
/// connected (the sheets share edges with the main shell), the scan clean and
/// the containment floor acquitting it — a three-shell solid whose first shell
/// is the right answer and whose other two are sheets stapled to it. Measured
/// on the turned bore at 60 degrees: shell 1 is the 7 correct faces at
/// 7673.516114379 against a closed form of 7673.516114438, shell 2 is the
/// overshoot alone at -536.326313 and shell 3 the departed ring at -6.7e-14.
///
/// Returns the offending key and its count, smallest key first so the refusal
/// reads the same on every run.
fn over_used_edge(
    fragments: &[crate::fragment::FaceFragmentRecord],
    dropped: &[usize],
    degenerate: &rustc_hash::FxHashSet<EdgeKey>,
) -> Option<(EdgeKey, usize)> {
    use rustc_hash::FxHashMap as HashMap;
    let mut uses: HashMap<EdgeKey, usize> = HashMap::default();
    for (index, fragment) in fragments.iter().enumerate() {
        if dropped.contains(&index) {
            continue;
        }
        for key in fragment
            .loops
            .iter()
            .flat_map(|fragment_loop| &fragment_loop.coedges)
            .filter_map(|coedge| edge_key(&coedge.source))
            .filter(|key| !degenerate.contains(key))
        {
            *uses.entry(key).or_default() += 1;
        }
    }
    let mut over: Vec<(EdgeKey, usize)> = uses
        .into_iter()
        .filter(|(_, count)| *count > 2)
        .collect();
    over.sort_unstable();
    over.into_iter().next()
}

/// The identity of a fragment coedge's edge, for the use census. A DERIVED
/// coedge carries a freshly minted curve rather than an identity, so it is not
/// counted — the rebuild's own closure check is what answers for those.
fn edge_key(source: &crate::fragment::FragmentEdgeSource) -> Option<EdgeKey> {
    use crate::fragment::FragmentEdgeSource;
    match source {
        FragmentEdgeSource::Boundary { operand, edge_id }
        | FragmentEdgeSource::SharedBoundary { operand, edge_id } => {
            Some(EdgeKey::Boundary(*operand, *edge_id))
        }
        FragmentEdgeSource::Imprint { piece_id } => Some(EdgeKey::Imprint(*piece_id)),
        FragmentEdgeSource::Derived { .. } => None,
    }
}

#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
enum EdgeKey {
    Boundary(u8, u64),
    Imprint(u64),
}

/// Every subset of `pieces` that leaves at least one behind, the empty one
/// included — the ways one face's pieces can be dropped.
fn proper_subsets(pieces: &[usize]) -> Vec<Vec<usize>> {
    let mut subsets = Vec::new();
    for mask in 0..(1u32 << pieces.len()) {
        if mask == (1u32 << pieces.len()) - 1 {
            // Dropping every piece of a crossing face is not a re-trim of it.
            continue;
        }
        subsets.push(
            pieces
                .iter()
                .enumerate()
                .filter(|(bit, _)| mask & (1 << bit) != 0)
                .map(|(_, piece)| *piece)
                .collect(),
        );
    }
    subsets
}

/// Reassemble one choice of kept fragments and prove it sound, or say why not.
fn rebuild(
    split: &BrepSolid,
    selected: Vec<crate::fragment::FaceFragmentRecord>,
    imprint: &crate::imprint::ImprintResultRecord,
    scale: f64,
) -> Result<BrepSolid, KernelRefusal> {
    use crate::boolean::{assemble_open_fragments, finalize_assembled_solid};
    use rustc_hash::FxHashMap as HashMap;

    let mut sources: HashMap<u8, &BrepSolid> = HashMap::default();
    sources.insert(SELF_OPERAND, split);
    let assembly_tolerance = 2e-3f64.max(scale * 5e-5);
    let solid = assemble_open_fragments(selected, &sources, imprint, assembly_tolerance)
        .map_err(|error| error.with_message(|error| format!("the kept pieces did not assemble: {error}")))?;
    let solid = finalize_assembled_solid(solid, assembly_tolerance)
        .map_err(|error| error.with_message(|error| format!("the assembly did not finalize: {error}")))?;

    let issues = solid.validate();
    if !issues.is_empty() {
        return Err(KernelRefusal::internal(KernelStage::Validate, "rebuild.validate", format!(
            "the rebuilt body fails validate(): {}",
            issues[0].message
        )));
    }
    if crate::solid_connectivity(&solid).is_disconnected() {
        return Err(KernelRefusal::internal(KernelStage::Validate, "rebuild.connected", "the rebuilt body is not connected"));
    }
    let options = SelfIntersectionOptions::for_solid(&solid);
    let report = solid_self_intersections(&solid, options)
        .map_err(|message| format!("the rebuilt body could not be scanned: {message}"))
        .or_refuse(KernelStage::Validate, "solid_self_intersections")?;
    if report.is_flagged() {
        return Err(KernelRefusal::internal(KernelStage::Validate, "rebuild.self_intersects", format!(
            "the rebuilt body still self-intersects: {}",
            report.summary().unwrap_or_else(|| "no summary".into())
        )));
    }
    Ok(solid)
}

/// One face of a body as its own open body, carrying the edges and vertices
/// that face uses and nothing else.
fn one_face_body(solid: &BrepSolid, face_id: u64) -> Result<BrepSolid, KernelRefusal> {
    use crate::topology::ShellRecord;
    use rustc_hash::FxHashSet as HashSet;

    let face = solid
        .shells
        .iter()
        .flat_map(|shell| &shell.faces)
        .find(|face| face.id == face_id)
        .ok_or_else(|| {
            // The class its one caller gave it before this helper was typed.
            KernelRefusal::internal(KernelStage::Fragment, "one_face_body", format!("face {face_id} is not in this body"))
        })?
        .clone();
    let used: HashSet<u64> = face
        .loops
        .iter()
        .flat_map(|loop_record| &loop_record.coedges)
        .map(|coedge| coedge.edge_id)
        .collect();
    let edges: Vec<_> = solid
        .edges
        .iter()
        .filter(|edge| used.contains(&edge.id))
        .cloned()
        .collect();
    let vertices: HashSet<u64> = edges
        .iter()
        .flat_map(|edge| [edge.start_vertex_id, edge.end_vertex_id])
        .collect();
    Ok(BrepSolid {
        mass_properties_cache: Default::default(),
        cells: Default::default(),
        id: solid.id,
        genus: 0,
        kind: crate::BodyKind::Solid,
        vertices: solid
            .vertices
            .iter()
            .filter(|vertex| vertices.contains(&vertex.id))
            .cloned()
            .collect(),
        edges,
        shells: vec![ShellRecord {
            id: 1,
            faces: vec![face],
        }],
    })
}

// ---------------------------------------------------------------------------
// The ledger: a repaired result is reported as repaired
// ---------------------------------------------------------------------------

/// One self-crossing this module REPAIRED rather than refused.
///
/// A repair changes the answer a lane returns, so it is never silent: the
/// feature that ran it carries this in its own result
/// (`FeatureResult::notes`), and the case gate records it per case. The
/// ledger is the carrier between the two — [`accept_sound`] is called from
/// lanes with no diagnostics record to write into, and threading one through
/// every lane exit would be a wider change than the repair itself. Same shape
/// as the boolean's `CONFORMANCE_REPAIRS` counter and the pcurve fit ledger.
#[derive(Clone, Debug)]
pub struct CrossingRepairRecord {
    /// The two faces that crossed.
    pub faces: (u64, u64),
    /// The crossing point the scan confirmed.
    pub point: crate::Vec3,
    /// How far past its band the crossing straddled.
    pub straddle: f64,
    /// Source faces whose pieces were dropped, with how many pieces of each.
    pub dropped: Vec<(u64, usize)>,
    /// Source faces the repair RETIRED a leftover from, with how many: a
    /// piece the drop left joined to nothing, or a whole loop of a face that
    /// nothing else answers for. Either way it is what the OLD configuration
    /// left on the body, and the ledger is the only place that says a repair
    /// took one off — "a piece was dropped" and "a leftover was retired" are
    /// different events.
    pub retired: Vec<(u64, usize)>,
    /// The faces the drop RE-TRIMMED: neighbours of the dropped material,
    /// which are neither of the crossing pair.
    pub retrimmed: Vec<u64>,
    /// Set when the repair was reached from the HOLE-CONTAINMENT FLOOR rather
    /// than from the scan: the face whose hole loop had left it, and that
    /// loop. `point` and `straddle` are then unmeasured and read zero, because
    /// no crossing was confirmed — the floor convicts a containment, not a
    /// crossing.
    pub breakout: Option<(u64, u64)>,
}

impl std::fmt::Display for CrossingRepairRecord {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some((face, loop_id)) = self.breakout {
            return write!(
                formatter,
                "repaired a BREAKOUT: hole loop {loop_id} of face {face} had left that face \
                 onto face {}, so face {} — the wall it bounds — and face {} were split along \
                 their carriers' intersection; dropped {} and re-trimmed {}{}",
                self.faces.1,
                self.faces.0,
                self.faces.1,
                self.pieces_dropped(),
                self.faces_retrimmed(),
                self.pieces_retired(),
            );
        }
        write!(
            formatter,
            "repaired a self-crossing: faces {} and {} crossed near ({:.4}, {:.4}, {:.4}) \
             (straddle {:.3e}); split along their carriers' intersection, dropped {} \
             and re-trimmed {}{}",
            self.faces.0,
            self.faces.1,
            self.point.x,
            self.point.y,
            self.point.z,
            self.straddle,
            self.pieces_dropped(),
            self.faces_retrimmed(),
            self.pieces_retired(),
        )
    }
}

impl CrossingRepairRecord {
    fn pieces_dropped(&self) -> String {
        self.dropped
            .iter()
            .map(|(face, count)| format!("{count} piece(s) of face {face}"))
            .collect::<Vec<_>>()
            .join(", ")
    }

    fn faces_retrimmed(&self) -> String {
        if self.retrimmed.is_empty() {
            "no neighbouring face".to_string()
        } else {
            format!(
                "face(s) {}",
                self.retrimmed
                    .iter()
                    .map(|face| face.to_string())
                    .collect::<Vec<_>>()
                    .join(", ")
            )
        }
    }

    /// The clause that says the OLD configuration's leftovers were taken off
    /// the body. Empty when nothing was retired, so every message this ledger
    /// already printed is unchanged; a repair that retires something says so
    /// and names the face, which is the only way the case gate's result line
    /// tells a repaired migration from the one it used to return.
    fn pieces_retired(&self) -> String {
        if self.retired.is_empty() {
            return String::new();
        }
        format!(
            ", and RETIRED {} the old configuration left, held by nothing else this repair keeps",
            self.retired
                .iter()
                .map(|(face, count)| format!("{count} leftover(s) of face {face}"))
                .collect::<Vec<_>>()
                .join(", ")
        )
    }
}

thread_local! {
    static CROSSING_REPAIRS: std::cell::RefCell<Vec<CrossingRepairRecord>> =
        const { std::cell::RefCell::new(Vec::new()) };
}

/// Take every repair made on this thread since the last call, leaving the
/// ledger empty. The history loop drains it after each feature so the repair
/// is reported against the feature that made it.
pub fn take_crossing_repairs() -> Vec<CrossingRepairRecord> {
    CROSSING_REPAIRS.with(|repairs| std::mem::take(&mut *repairs.borrow_mut()))
}

/// Put a ledger back, replacing whatever is there.
///
/// The migration route TRIES candidate faces, and a trial that builds records
/// itself on the way past. Only the candidate whose body is returned may keep
/// its record, and a caller's own entries must survive the trials, so the
/// ledger is taken before and written back after. Nothing else may use this:
/// a repair reports itself once, where it happened.
fn restore_repairs(records: Vec<CrossingRepairRecord>) {
    CROSSING_REPAIRS.with(|repairs| *repairs.borrow_mut() = records);
}

fn record_repair(
    fragments: &[crate::fragment::FaceFragmentRecord],
    dropped: &[usize],
    retired: &[u64],
    retrimmed_faces: &[u64],
    face_a: u64,
    face_b: u64,
    origin: &RepairOrigin<'_>,
) {
    let tally = |faces: &[u64]| {
        let mut by_face: Vec<(u64, usize)> = Vec::new();
        for face in faces {
            match by_face.iter_mut().find(|(existing, _)| existing == face) {
                Some((_, count)) => *count += 1,
                None => by_face.push((*face, 1)),
            }
        }
        by_face.sort_unstable();
        by_face
    };
    let by_face = tally(
        &dropped
            .iter()
            .map(|index| fragments[*index].source_face_id)
            .collect::<Vec<_>>(),
    );
    let retired = tally(retired);
    // A face that lost ONLY a detached piece was not re-trimmed — nothing of
    // it was re-cut, a leftover of the old configuration was taken off the
    // body — so it is reported under `retired` and not counted here twice.
    let mut retrimmed: Vec<u64> = by_face
        .iter()
        .filter(|(face, count)| {
            retired
                .iter()
                .find(|(retired_face, _)| retired_face == face)
                .map(|(_, retired_count)| retired_count < count)
                .unwrap_or(true)
        })
        .map(|(face, _)| *face)
        .chain(retrimmed_faces.iter().copied())
        .filter(|face| *face != face_a && *face != face_b)
        .collect();
    retrimmed.sort_unstable();
    retrimmed.dedup();
    let crossing = match origin {
        RepairOrigin::Scan(report) => report.confirmed.first(),
        RepairOrigin::Breakout { .. } => None,
    };
    let record = CrossingRepairRecord {
        faces: (face_a, face_b),
        point: crossing.map(|crossing| crossing.point).unwrap_or_default(),
        straddle: crossing.map(|crossing| crossing.straddle).unwrap_or_default(),
        dropped: by_face,
        retired,
        retrimmed,
        breakout: match origin {
            RepairOrigin::Scan(_) => None,
            RepairOrigin::Breakout { face, loop_id } => Some((*face, *loop_id)),
        },
    };
    if trace_on() {
        eprintln!("HEAL {record}");
    }
    CROSSING_REPAIRS.with(|repairs| repairs.borrow_mut().push(record));
}

/// Write the flagged body out before the repair touches it, when
/// `BREP_HEAL_DUMP` names a directory. A crossing is measured off the body that
/// carries it — per-face areas, a mesh-arbitrated genus, the volume the repair
/// changes — and replaying a whole document to reach it again costs more than
/// the file does. Diagnostics only: the lane still repairs or refuses exactly
/// as it would have.
fn dump_flagged(solid: &BrepSolid, entry: &str) {
    let Ok(directory) = std::env::var("BREP_HEAL_DUMP") else {
        return;
    };
    if directory.is_empty() {
        return;
    }
    let name: String = entry
        .chars()
        .map(|character| if character.is_alphanumeric() { character } else { '_' })
        .collect();
    let path = std::path::Path::new(&directory).join(format!("flagged-{name}.json"));
    match serde_json::to_string(solid) {
        Ok(text) => {
            if let Err(error) = std::fs::write(&path, text) {
                eprintln!("HEAL dump {}: {error}", path.display());
            } else {
                eprintln!("HEAL dumped the flagged body to {}", path.display());
            }
        }
        Err(error) => eprintln!("HEAL dump: {error}"),
    }
}

// ---------------------------------------------------------------------------
// Trace instrument (BREP_HEAL_TRACE=1)
// ---------------------------------------------------------------------------

fn trace_on() -> bool {
    debug_flag!(live "BREP_HEAL_TRACE")
}

/// Measurement hatch for the section fit the repair's imprint asks for:
/// `BREP_HEAL_FIT_LANE=local|global` picks the lane its ladder starts in and
/// `BREP_HEAL_FIT_POINTS=<n>` its station budget. Both unset is the request as
/// built, so every lane and budget can be read off ONE binary against the
/// shipped one (`crossing_repair_probe body` with `BREP_DEBUG_POLYLINE_FIT=1`).
fn measured_fit_request(request: &mut crate::imprint::ImprintOptions) {
    match std::env::var("BREP_HEAL_FIT_LANE").as_deref() {
        Ok("local") => request.local_fit = true,
        Ok("global") => request.local_fit = false,
        _ => {}
    }
    if let Some(points) = std::env::var("BREP_HEAL_FIT_POINTS")
        .ok()
        .and_then(|value| value.parse::<usize>().ok())
        .filter(|points| *points >= 2)
    {
        request.maximum_fit_points = points;
    }
    if trace_on() {
        eprintln!(
            "HEAL imprint request: {} lane, {} stations, tolerance {:.3e}",
            if request.local_fit { "local" } else { "global" },
            request.maximum_fit_points,
            request.tolerance
        );
    }
}

fn trace_solid(label: &str, solid: &BrepSolid) {
    use rustc_hash::FxHashMap as HashMap;
    let point: HashMap<u64, crate::Vec3> = solid
        .vertices
        .iter()
        .map(|vertex| (vertex.id, vertex.point))
        .collect();
    eprintln!(
        "HEAL {label}: {}F {}E {}V shells={}",
        solid.shells.iter().map(|shell| shell.faces.len()).sum::<usize>(),
        solid.edges.len(),
        solid.vertices.len(),
        solid.shells.len()
    );
    let mut uses: HashMap<u64, usize> = HashMap::default();
    for shell in &solid.shells {
        for face in &shell.faces {
            for loop_record in &face.loops {
                for coedge in &loop_record.coedges {
                    *uses.entry(coedge.edge_id).or_default() += 1;
                }
            }
        }
    }
    for edge in &solid.edges {
        let start = point.get(&edge.start_vertex_id).copied().unwrap_or_default();
        let end = point.get(&edge.end_vertex_id).copied().unwrap_or_default();
        eprintln!(
            "HEAL   edge {:>3} uses={} v{}->v{} ({:.4},{:.4},{:.4})->({:.4},{:.4},{:.4}) deg={} '{}'",
            edge.id,
            uses.get(&edge.id).copied().unwrap_or(0),
            edge.start_vertex_id,
            edge.end_vertex_id,
            start.x, start.y, start.z, end.x, end.y, end.z,
            edge.degenerate,
            edge.name.as_deref().unwrap_or("")
        );
    }
    for shell in &solid.shells {
        for face in &shell.faces {
            eprintln!(
                "HEAL   face {:>3} loops={} '{}'",
                face.id,
                face.loops.len(),
                face.name.as_deref().unwrap_or("")
            );
            for (index, loop_record) in face.loops.iter().enumerate() {
                let text = loop_record
                    .coedges
                    .iter()
                    .map(|coedge| format!("{}{}", if coedge.forward { '+' } else { '-' }, coedge.edge_id))
                    .collect::<Vec<_>>()
                    .join(" ");
                eprintln!("HEAL     loop {index}: {text}");
            }
        }
    }
}

fn trace_imprint(imprint: &crate::imprint::ImprintResultRecord) {
    eprintln!(
        "HEAL imprint: {} piece(s) {} edge_split(s) by_face={:?}",
        imprint.pieces.len(),
        imprint.edge_splits.len(),
        imprint
            .by_face
            .iter()
            .map(|face| (face.operand, face.face_id, face.piece_ids.clone()))
            .collect::<Vec<_>>()
    );
    for piece in &imprint.pieces {
        let start = piece.curve.evaluate(piece.t0).unwrap_or_default();
        let end = piece.curve.evaluate(piece.t1).unwrap_or_default();
        let middle = piece.curve.evaluate((piece.t0 + piece.t1) / 2.0).unwrap_or_default();
        eprintln!(
            "HEAL   piece {} v{}->v{} ({:.4},{:.4},{:.4}) .. ({:.4},{:.4},{:.4}) .. ({:.4},{:.4},{:.4}) supports={:?} shared={:?}",
            piece.id,
            piece.start_vertex_id,
            piece.end_vertex_id,
            start.x, start.y, start.z,
            middle.x, middle.y, middle.z,
            end.x, end.y, end.z,
            piece.support_faces.iter().map(|key| (key.operand, key.face_id)).collect::<Vec<_>>(),
            piece.shared_edge,
        );
    }
    for split in &imprint.edge_splits {
        eprintln!(
            "HEAL   edge_split operand={} edge={} at {:?}",
            split.operand, split.edge_id, split.parameters
        );
    }
    for vertex in &imprint.vertices {
        eprintln!(
            "HEAL   imprint vertex {} ({:.4},{:.4},{:.4})",
            vertex.id, vertex.point.x, vertex.point.y, vertex.point.z
        );
    }
}

fn trace_fragments(fragments: &[crate::fragment::FaceFragmentRecord]) {
    use crate::fragment::FragmentEdgeSource;
    eprintln!("HEAL fragments: {}", fragments.len());
    for (index, fragment) in fragments.iter().enumerate() {
        eprintln!(
            "HEAL   fragment {index} face {} loops={} test=({:.4},{:.4},{:.4})",
            fragment.source_face_id,
            fragment.loops.len(),
            fragment.test_point.x,
            fragment.test_point.y,
            fragment.test_point.z
        );
        for (loop_index, fragment_loop) in fragment.loops.iter().enumerate() {
            let text = fragment_loop
                .coedges
                .iter()
                .map(|coedge| {
                    let tag = match &coedge.source {
                        FragmentEdgeSource::Boundary { operand, edge_id } => format!("b{operand}:{edge_id}"),
                        FragmentEdgeSource::SharedBoundary { operand, edge_id } => format!("s{operand}:{edge_id}"),
                        FragmentEdgeSource::Imprint { piece_id } => format!("p{piece_id}"),
                        FragmentEdgeSource::Derived { start, end, .. } => format!(
                            "d({:.3},{:.3},{:.3})->({:.3},{:.3},{:.3})",
                            start.x, start.y, start.z, end.x, end.y, end.z
                        ),
                    };
                    format!("{}{tag}", if coedge.forward { '+' } else { '-' })
                })
                .collect::<Vec<_>>()
                .join(" ");
            eprintln!("HEAL     loop {loop_index}: {text}");
        }
    }
}