BREP_reconstruction 0.3.0

Kernel integration for neutral BREP_RANSAC recognition results
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
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
//! Conservative local BREP reconstruction from the kernel's public mesh segmentation.
//!
//! Plane and safely bounded cylinder/cone/sphere regions remain analytic.
//! Unsupported regions, and revolved regions whose interfaces cannot be shared
//! exactly, are represented by maximal edge-connected, provably coplanar planar
//! patches. A sphere is framed as a revolution about the common axis of the
//! rings that bound it; a cap around the pole carries the seam and the
//! degenerate pole edge of the kernel's own sphere solid. A sphere whose
//! plane rings lie on several axes, and two equal-radius cylinders whose axes
//! cross, share exact circles and ellipses whose pcurves are fitted; a
//! whole-body failure after such a lane demotes only its regions.

use brep_kernel::{
    make_sphere_surface_framed,
    build_pcurve_on_surface_range, circle_angle_to_parameter, intersect_analytic_pair, make_arc,
    make_line, make_revolution, mesh_to_faceted_brep, project_point_to_curve, segment_mesh_faces,
    solid_signed_volume, ArenaCoedge, ArenaEdge, ArenaFace, ArenaLoop, ArenaShell, ArenaVertex,
    BrepSolid, EdgeId, FaceId, MeshRegion, MeshSegmentation, NurbsCurve, NurbsSurface, RegionCarrier,
    SegmentOptions,
    TopologyArena, Vec3, Vec4, KNOT_IDENTITY_TOL,
};
use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque};
use std::f64::consts::TAU;

#[derive(Clone, Copy)]
struct PlaneInfo {
    origin: Vec3,
    normal: Vec3,
}

#[derive(Clone, Copy)]
struct CylinderCarrier {
    origin: Vec3,
    axis: Vec3,
    radius: f64,
    sense: i8,
    /// Largest radial deviation of a source vertex the segmenter accepted
    /// for this carrier: how far a ring vertex may sit off the exact circle.
    deviation: f64,
}

#[derive(Clone, Copy)]
struct SphereCarrier {
    center: Vec3,
    radius: f64,
    sense: i8,
    /// Largest radial deviation of a source vertex the segmenter accepted
    /// for this carrier.
    deviation: f64,
}

#[derive(Clone, Copy)]
struct ConeCarrier {
    apex: Vec3,
    axis: Vec3,
    half_angle: f64,
    sense: i8,
    /// Largest deviation of a source vertex the segmenter accepted for
    /// this carrier.
    deviation: f64,
}

/// How far a ring or arc vertex may sit off its exact circle: the exact
/// topology tolerance, or twice the deviation the segmenter already
/// accepted for the carrier when that is wider (twice, because the carrier
/// is snapped to its neighbors after the acceptance was measured). A vertex
/// the tessellator left on a rim chord is still on the ring the exact
/// circle replaces.
fn accepted_radial_bar(tolerance: f64, deviation: f64) -> f64 {
    tolerance.max(2.0 * deviation)
}

struct CylinderInfo {
    carrier: CylinderCarrier,
    base_station: f64,
    height: f64,
    x_axis: Vec3,
    y_axis: Vec3,
    surface: NurbsSurface,
}

struct ConeInfo {
    carrier: ConeCarrier,
    base_station: f64,
    height: f64,
    x_axis: Vec3,
    y_axis: Vec3,
    surface: NurbsSurface,
}

struct SphereInfo {
    carrier: SphereCarrier,
    /// Polar axis of the revolution parameterization: the common normal of
    /// the ring planes bounding the region, pointing into the region; or,
    /// when the rings are on several axes, one whose poles both lie in
    /// removed caps (see [`tilted_sphere_frame`]).
    axis: Vec3,
    x_axis: Vec3,
    y_axis: Vec3,
    surface: NurbsSurface,
    /// The region is a cap around the north pole (one ring), so its face
    /// carries the seam and the degenerate pole edge.
    north_pole_inside: bool,
}

#[derive(Clone)]
struct Chain {
    vertices: Vec<usize>,
    closed: bool,
    adjacent: (u32, u32),
}

#[derive(Clone, Copy)]
struct Traversal {
    chain: usize,
    forward: bool,
}

struct RegionBoundary {
    cycles: Vec<Vec<Traversal>>,
}

struct LocalRegions {
    triangle_regions: Vec<u32>,
    planes: HashMap<u32, PlaneInfo>,
    cylinders: HashMap<u32, CylinderCarrier>,
    cones: HashMap<u32, ConeCarrier>,
    spheres: HashMap<u32, SphereCarrier>,
    local_to_original: HashMap<u32, u32>,
    count: usize,
    stats: HybridBrepStats,
}

enum HybridBuildFailure {
    Region { original: u32, message: String },
    /// The assembled shell failed a whole-body check after regions were
    /// built on a lane whose edges carry fitted pcurves on a sphere or
    /// cylinder (a tilted sphere ring, a crossing-bore ellipse). Those
    /// regions are demoted together and the build retried, so a new lane
    /// can only ever cost its own regions, never the body.
    Speculative { originals: Vec<u32>, message: String },
    Global(String),
}

impl From<String> for HybridBuildFailure {
    fn from(message: String) -> Self {
        Self::Global(message)
    }
}

impl From<&str> for HybridBuildFailure {
    fn from(message: &str) -> Self {
        Self::Global(message.to_owned())
    }
}

/// Auditable accounting for an analytic/faceted reconstruction.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, serde::Serialize)]
pub(crate) struct HybridBrepStats {
    pub(crate) total_faces: usize,
    pub(crate) analytic_plane_faces: usize,
    pub(crate) analytic_plane_triangles: usize,
    pub(crate) analytic_cylinder_faces: usize,
    pub(crate) analytic_cylinder_triangles: usize,
    pub(crate) analytic_cone_faces: usize,
    pub(crate) analytic_cone_triangles: usize,
    pub(crate) analytic_sphere_faces: usize,
    pub(crate) analytic_sphere_triangles: usize,
    pub(crate) faceted_faces: usize,
    pub(crate) faceted_triangles: usize,
    pub(crate) demoted_regions: usize,
    pub(crate) demoted_triangles: usize,
    pub(crate) segmentation_plane_regions: usize,
    pub(crate) segmentation_cylinder_regions: usize,
    pub(crate) segmentation_cone_regions: usize,
    pub(crate) segmentation_sphere_regions: usize,
    pub(crate) segmentation_unsupported_regions: usize,
}

/// Validated reconstructed solid and the accounting used to select policy.
pub(crate) struct HybridBrepOutput {
    pub(crate) solid: BrepSolid,
    pub(crate) stats: HybridBrepStats,
}

#[derive(Clone, Copy)]
struct ChainUv {
    region: u32,
    start: [f64; 2],
    end: [f64; 2],
}

struct BuiltEdge {
    edge: EdgeId,
    curve_along_chain: bool,
    revolve_uvs: Vec<ChainUv>,
    custom_revolve_pcurves: HashMap<u32, NurbsCurve>,
}

struct Ids(u64);

impl Ids {
    fn next(&mut self) -> u64 {
        let value = self.0;
        self.0 += 1;
        value
    }
}

/// Rebuild a validated analytic/faceted shell from public segmentation.
///
/// This is intentionally narrower than a general mesh-to-BREP converter.
/// Plane and safely bounded cylinder/cone regions remain analytic. Unsupported
/// regions become maximal edge-connected coplanar planar patches. A cylinder touching
/// those facets is retained only when every shared edge is an exact axial
/// ruling; otherwise the complete cylinder region is recursively demoted to
/// planar triangle faces. Every shared boundary must still be provably a line,
/// circular arc, complete ring, or cylinder ruling.
pub(crate) fn hybrid_plane_cylinder_brep(
    positions: &[f64],
    indices: Option<&[u32]>,
    options: &SegmentOptions,
) -> Result<HybridBrepOutput, String> {
    hybrid_plane_cylinder_brep_retry(positions, indices, options, None)
}

/// Localized failures demote one region per attempt; past this many, the
/// mesh is not a case of a few unsafe regions but of a segmentation the
/// builder cannot use, and the faceted fallback is the honest answer.
const HYBRID_RETRY_LIMIT: usize = 512;

fn hybrid_plane_cylinder_brep_retry(
    positions: &[f64],
    indices: Option<&[u32]>,
    options: &SegmentOptions,
    mut injected_failure: Option<u32>,
) -> Result<HybridBrepOutput, String> {
    // The segmentation does not depend on the demotions; run it once.
    let owned_indices;
    let index_buffer: &[u32] = match indices {
        Some(value) => value,
        None => {
            owned_indices = (0..positions.len() as u32 / 3).collect::<Vec<_>>();
            &owned_indices
        }
    };
    let segmentation = segment_mesh_faces(positions, index_buffer, options)?;
    // A faceted seed gives us a public, type-safe TopologyArena without
    // depending directly on SlotMap.  The seed is also an independent closed-
    // manifold precondition.  Its topology is cleared before reconstruction;
    // built once, it is cloned per attempt.
    let seed = mesh_to_faceted_brep(positions, Some(index_buffer), options.weld_tolerance)?;
    let mut blank = TopologyArena::from_brep(&seed)?;
    blank.vertices.clear();
    blank.edges.clear();
    blank.coedges.clear();
    blank.loops.clear();
    blank.faces.clear();
    blank.shells.clear();
    blank.wire_solid_id = 1;
    blank.genus = 0;
    let mut forced_demotions = BTreeSet::new();
    loop {
        let attempt = if let Some(original) = injected_failure.take() {
            Err(HybridBuildFailure::Region {
                original,
                message: "injected localized carrier construction failure".into(),
            })
        } else {
            hybrid_plane_cylinder_brep_once(
                positions,
                index_buffer,
                options,
                &segmentation,
                blank.clone(),
                &forced_demotions,
            )
        };
        match attempt {
            Ok(output) => return Ok(output),
            Err(HybridBuildFailure::Region { original, message }) => {
                if std::env::var("BREP_DEBUG_HYBRID").is_ok() {
                    eprintln!("[hybrid] retry: region {original} failed: {message}");
                }
                if !forced_demotions.insert(original) {
                    return Err(format!(
                        "hybrid BREP: region {original} remained unconstructible after local demotion: {message}"
                    ));
                }
                if forced_demotions.len() > HYBRID_RETRY_LIMIT {
                    return Err(format!(
                        "hybrid BREP: more than {HYBRID_RETRY_LIMIT} regions needed local demotion; last: region {original}: {message}"
                    ));
                }
            }
            Err(HybridBuildFailure::Speculative { originals, message }) => {
                if std::env::var("BREP_DEBUG_HYBRID").is_ok() {
                    eprintln!("[hybrid] retry: fitted-pcurve regions {originals:?} failed: {message}");
                }
                let before = forced_demotions.len();
                forced_demotions.extend(originals);
                if forced_demotions.len() == before {
                    return Err(message);
                }
                if forced_demotions.len() > HYBRID_RETRY_LIMIT {
                    return Err(format!(
                        "hybrid BREP: more than {HYBRID_RETRY_LIMIT} regions needed local demotion; last: {message}"
                    ));
                }
            }
            Err(HybridBuildFailure::Global(message)) => return Err(message),
        }
    }
}

fn hybrid_plane_cylinder_brep_once(
    positions: &[f64],
    indices: &[u32],
    options: &SegmentOptions,
    segmentation: &MeshSegmentation,
    mut arena: TopologyArena,
    forced_demotions: &BTreeSet<u32>,
) -> Result<HybridBrepOutput, HybridBuildFailure> {
    if positions.is_empty()
        || !positions.len().is_multiple_of(3)
        || !indices.len().is_multiple_of(3)
    {
        return Err("hybrid BREP: invalid triangle buffers".into());
    }
    let vertices = positions
        .chunks_exact(3)
        .map(|p| Vec3::new(p[0], p[1], p[2]))
        .collect::<Vec<_>>();
    let triangles = indices
        .chunks_exact(3)
        .map(|t| [t[0] as usize, t[1] as usize, t[2] as usize])
        .collect::<Vec<_>>();
    if triangles
        .iter()
        .flatten()
        .any(|&vertex| vertex >= vertices.len())
    {
        return Err("hybrid BREP: triangle index outside vertex buffer".into());
    }

    if segmentation.welded_vertex_count != vertices.len() {
        return Err(format!(
            "hybrid BREP: segmentation welded {} input vertices to {}; pre-weld the mesh before exact reconstruction",
            vertices.len(), segmentation.welded_vertex_count
        ).into());
    }
    if segmentation.triangle_region_ids.len() != triangles.len() {
        return Err("hybrid BREP: segmentation triangle count drift".into());
    }

    let diagonal = mesh_diagonal(&vertices);
    // Exact shared topology must be substantially tighter than carrier
    // recognition. This still covers deterministic f32 STL quantization.
    let tolerance = (options.fit_tolerance * diagonal)
        .min(2.0e-6 * diagonal)
        .max(1.0e-10);
    let incidence = edge_incidence(&triangles)?;
    if incidence.values().any(|incident| incident.len() != 2) {
        return Err("hybrid BREP: input is not a closed two-manifold".into());
    }
    let local = local_regions(
        &segmentation.regions,
        &segmentation.triangle_region_ids,
        &triangles,
        &vertices,
        &incidence,
        tolerance,
        forced_demotions,
    )?;
    let stats = local.stats;
    let local_to_original = local.local_to_original;
    let planes = local.planes;
    let mut cylinder_carriers = local.cylinders;
    let mut cone_carriers = local.cones;
    let sphere_carriers = local.spheres;
    let mut region_centroids = HashMap::<u32, (Vec3, usize)>::new();
    let mut region_samples = HashMap::<u32, Vec<Vec3>>::new();
    for (triangle, &region) in local.triangle_regions.iter().enumerate() {
        if !sphere_carriers.contains_key(&region) {
            continue;
        }
        let [a, b, c] = triangles[triangle];
        let centroid = vertices[a].add(vertices[b]).add(vertices[c]).scale(1.0 / 3.0);
        region_samples.entry(region).or_default().push(centroid);
        let entry = region_centroids.entry(region).or_insert((Vec3::new(0.0, 0.0, 0.0), 0));
        entry.0 = entry.0.add(centroid);
        entry.1 += 1;
    }
    let region_centroids = region_centroids
        .into_iter()
        .map(|(region, (sum, count))| (region, sum.scale(1.0 / count as f64)))
        .collect::<HashMap<_, _>>();
    stage_trace("local regions done");
    let (chains, mut boundaries) =
        region_boundaries(&triangles, &local.triangle_regions, local.count, &incidence)?;
    let is_revolve = |region: u32| {
        cylinder_carriers.contains_key(&region)
            || cone_carriers.contains_key(&region)
            || sphere_carriers.contains_key(&region)
    };
    let chains = split_bent_planar_chains(chains, &mut boundaries, &vertices, tolerance, is_revolve);
    stage_trace("boundaries done");

    snap_cylinder_axes(
        &mut cylinder_carriers,
        &planes,
        &chains,
        &vertices,
        tolerance,
    )
    .map_err(|(region, message)| HybridBuildFailure::Region {
        original: local_to_original[&region],
        message,
    })?;
    snap_cone_carriers(
        &mut cone_carriers,
        &cylinder_carriers,
        &planes,
        &chains,
        &vertices,
        tolerance,
    )
    .map_err(|(region, message)| HybridBuildFailure::Region {
        original: local_to_original[&region],
        message,
    })?;
    for (&region, cone) in &cone_carriers {
        let mut low = f64::INFINITY;
        let mut high = f64::NEG_INFINITY;
        for chain in chains
            .iter()
            .filter(|chain| chain.adjacent.0 == region || chain.adjacent.1 == region)
        {
            for point in chain_points(chain, &vertices) {
                let station = cone_station(*cone, point);
                low = low.min(station);
                high = high.max(station);
            }
        }
        validate_cone_span(
            local_to_original[&region],
            low,
            high,
            cone.half_angle,
            tolerance,
        )?;
    }
    stage_trace("axes snapped");
    let cylinders = build_cylinder_surfaces(&cylinder_carriers, &chains, &vertices, tolerance)
        .map_err(|(region, message)| HybridBuildFailure::Region {
            original: local_to_original[&region],
            message,
        })?;
    let cones = build_cone_surfaces(&cone_carriers, &cylinders, &chains, &vertices, tolerance)
        .map_err(|(region, message)| HybridBuildFailure::Region {
            original: local_to_original[&region],
            message,
        })?;
    let spheres = build_sphere_surfaces(
        &sphere_carriers,
        &planes,
        &cylinders,
        &cones,
        &chains,
        &vertices,
        &region_centroids,
        &region_samples,
        tolerance,
    )
    .map_err(|(region, message)| HybridBuildFailure::Region {
        original: local_to_original[&region],
        message,
    })?;
    stage_trace("surfaces built");
    sort_region_cycles(
        &mut boundaries,
        &planes,
        &cylinders,
        &cones,
        &spheres,
        &chains,
        &vertices,
    );
    stage_trace("cycles sorted");

    let mut ids = Ids(2);
    let mut arena_vertices = HashMap::new();

    let mut built_edges = Vec::with_capacity(chains.len());
    let chain_trace = std::env::var("BREP_DEBUG_HYBRID").is_ok();
    for (chain_index, chain) in chains.iter().enumerate() {
        let involves_revolve = [chain.adjacent.0, chain.adjacent.1].iter().any(|region| {
            cylinders.contains_key(region) || cones.contains_key(region) || spheres.contains_key(region)
        });
        if chain_trace && involves_revolve {
            let kind = |region: u32| {
                if planes.contains_key(&region) {
                    "plane"
                } else if cylinders.contains_key(&region) {
                    "cylinder"
                } else if cones.contains_key(&region) {
                    "cone"
                } else if spheres.contains_key(&region) {
                    "sphere"
                } else {
                    "?"
                }
            };
            eprintln!(
                "[hybrid] chain {chain_index}: {} {} / {} {}, {} points, closed={}",
                chain.adjacent.0,
                kind(chain.adjacent.0),
                chain.adjacent.1,
                kind(chain.adjacent.1),
                chain.vertices.len(),
                chain.closed
            );
        }
        let built = match build_chain_edge(
            chain_index,
            chain,
            &vertices,
            &planes,
            &cylinders,
            &cones,
            &spheres,
            tolerance,
            &mut arena,
            &mut arena_vertices,
            &mut ids,
        ) {
            Ok(built) => built,
            Err(message) => {
                let local = [chain.adjacent.0, chain.adjacent.1]
                    .into_iter()
                    .find(|region| spheres.contains_key(region))
                    .or_else(|| {
                        [chain.adjacent.0, chain.adjacent.1]
                            .into_iter()
                            .find(|region| cones.contains_key(region))
                    })
                    .or_else(|| {
                        [chain.adjacent.0, chain.adjacent.1]
                            .into_iter()
                            .find(|region| cylinders.contains_key(region))
                    });
                if let Some(local) = local {
                    return Err(HybridBuildFailure::Region {
                        original: local_to_original[&local],
                        message,
                    });
                }
                return Err(HybridBuildFailure::Global(message));
            }
        };
        built_edges.push(built);
    }

    stage_trace("chain edges built");
    let mut faces = Vec::<FaceId>::new();
    for region in 0..local.count as u32 {
        let face = if let Some(plane) = planes.get(&region) {
            build_plane_face(
                region,
                *plane,
                &boundaries[region as usize],
                &chains,
                &built_edges,
                &vertices,
                &mut arena,
                &mut ids,
            )?
        } else if let Some(cylinder) = cylinders.get(&region) {
            build_cylinder_face(
                region,
                cylinder,
                &boundaries[region as usize],
                &built_edges,
                &mut arena,
                &mut ids,
            )
            .map_err(|message| HybridBuildFailure::Region {
                original: local_to_original[&region],
                message,
            })?
        } else if let Some(sphere) = spheres.get(&region) {
            build_sphere_face(
                region,
                sphere,
                &boundaries[region as usize],
                &built_edges,
                &mut arena,
                &mut ids,
            )
            .map_err(|message| HybridBuildFailure::Region {
                original: local_to_original[&region],
                message,
            })?
        } else {
            build_cone_face(
                region,
                &cones[&region],
                &boundaries[region as usize],
                &built_edges,
                &mut arena,
                &mut ids,
            )
            .map_err(|message| HybridBuildFailure::Region {
                original: local_to_original[&region],
                message,
            })?
        };
        faces.push(face);
    }
    if std::env::var("BREP_DEBUG_HYBRID").is_ok() {
        for (region, &face) in faces.iter().enumerate() {
            let record = &arena.faces[face];
            let kind = if planes.contains_key(&(region as u32)) {
                "plane"
            } else if cylinders.contains_key(&(region as u32)) {
                "cylinder"
            } else if cones.contains_key(&(region as u32)) {
                "cone"
            } else {
                "sphere"
            };
            let coedges = record
                .loops
                .iter()
                .flat_map(|&l| arena.loops[l].coedges.iter())
                .map(|&c| {
                    let coedge = &arena.coedges[c];
                    let edge = &arena.edges[coedge.edge];
                    format!("c{}:e{}{}", coedge.wire_id, edge.wire_id, if edge.degenerate { "*" } else { "" })
                })
                .collect::<Vec<_>>()
                .join(" ");
            eprintln!(
                "[hybrid] face {} = region {region} ({kind}, original {}): {coedges}",
                record.wire_id,
                local_to_original.get(&(region as u32)).copied().unwrap_or(u32::MAX)
            );
        }
    }
    arena.shells.insert(ArenaShell {
        wire_id: ids.next(),
        faces,
    });
    stage_trace("faces built");
    let speculative = built_edges
        .iter()
        .flat_map(|built| built.custom_revolve_pcurves.keys())
        .filter(|region| spheres.contains_key(region) || cylinders.contains_key(region))
        .map(|region| local_to_original[region])
        .collect::<BTreeSet<_>>();
    finish_shell(arena, stats).map_err(|message| {
        if speculative.is_empty() {
            HybridBuildFailure::Global(message)
        } else {
            HybridBuildFailure::Speculative {
                originals: speculative.into_iter().collect(),
                message,
            }
        }
    })
}

/// Convert the assembled arena and run the whole-body checks.
fn finish_shell(arena: TopologyArena, stats: HybridBrepStats) -> Result<HybridBrepOutput, String> {
    let mut solid = arena.to_brep()?;
    stage_trace("arena converted");
    let vertex_count = solid.vertices.len() as i64;
    // A degenerate pole edge is not a 1-cell of the shell.
    let edge_count = solid.edges.iter().filter(|edge| !edge.degenerate).count() as i64;
    let face_count = solid
        .shells
        .iter()
        .map(|shell| shell.faces.len())
        .sum::<usize>() as i64;
    let extra_loops = solid
        .shells
        .iter()
        .flat_map(|shell| &shell.faces)
        .map(|face| face.loops.len().saturating_sub(1) as i64)
        .sum::<i64>();
    let numerator = 2 - (vertex_count - edge_count + face_count - extra_loops);
    if numerator < 0 || numerator % 2 != 0 {
        return Err(format!(
            "hybrid BREP: non-manifold Euler accounting (2-(V-E+F-H)={numerator})"
        ));
    }
    solid.genus = numerator / 2;
    let issues = solid.validate();
    stage_trace("validated");
    if !issues.is_empty() {
        return Err(format!("hybrid BREP: topology validation failed: {issues:?}"));
    }
    let volume = solid_signed_volume(&solid)?;
    stage_trace("volume computed");
    if !volume.is_finite() || volume <= 0.0 {
        return Err(format!(
            "hybrid BREP: reconstructed shell is inverted or empty (signed volume {volume:.6e})"
        ));
    }
    Ok(HybridBrepOutput { solid, stats })
}

fn validate_cone_span(
    original: u32,
    low: f64,
    high: f64,
    half_angle: f64,
    tolerance: f64,
) -> Result<(), HybridBuildFailure> {
    if !low.is_finite() || high - low <= tolerance || low * half_angle.tan() <= tolerance {
        return Err(HybridBuildFailure::Region {
            original,
            message: "pointed or zero-span cone requires unsupported pole topology".into(),
        });
    }
    Ok(())
}

fn mesh_diagonal(vertices: &[Vec3]) -> f64 {
    let mut low = vertices[0];
    let mut high = vertices[0];
    for &point in &vertices[1..] {
        low.x = low.x.min(point.x);
        low.y = low.y.min(point.y);
        low.z = low.z.min(point.z);
        high.x = high.x.max(point.x);
        high.y = high.y.max(point.y);
        high.z = high.z.max(point.z);
    }
    high.sub(low).length()
}

type EdgeKey = (usize, usize);

fn edge_key(a: usize, b: usize) -> EdgeKey {
    if a < b {
        (a, b)
    } else {
        (b, a)
    }
}

fn edge_incidence(triangles: &[[usize; 3]]) -> Result<BTreeMap<EdgeKey, Vec<usize>>, String> {
    let mut incidence = BTreeMap::<EdgeKey, Vec<usize>>::new();
    for (triangle_id, triangle) in triangles.iter().enumerate() {
        if triangle[0] == triangle[1] || triangle[1] == triangle[2] || triangle[2] == triangle[0] {
            return Err("hybrid BREP: repeated vertex in triangle".into());
        }
        for corner in 0..3 {
            incidence
                .entry(edge_key(triangle[corner], triangle[(corner + 1) % 3]))
                .or_default()
                .push(triangle_id);
        }
    }
    Ok(incidence)
}

fn carrier_cylinder(region: &MeshRegion) -> Result<Option<CylinderCarrier>, String> {
    let RegionCarrier::Cylinder {
        axis_point,
        axis_dir,
        radius,
        sense,
    } = region.carrier
    else {
        return Ok(None);
    };
    if !radius.is_finite() || radius <= 0.0 || !matches!(sense, -1 | 1) {
        return Ok(None);
    }
    Ok(Some(CylinderCarrier {
        origin: axis_point,
        axis: axis_dir.normalized()?,
        radius,
        sense,
        deviation: region.max_deviation.max(0.0),
    }))
}

fn carrier_cone(region: &MeshRegion) -> Result<Option<ConeCarrier>, String> {
    let RegionCarrier::Cone {
        apex,
        axis_dir,
        half_angle_rad,
        sense,
    } = region.carrier
    else {
        return Ok(None);
    };
    if !half_angle_rad.is_finite()
        || !(1.0e-6..std::f64::consts::FRAC_PI_2 - 1.0e-6).contains(&half_angle_rad)
        || !matches!(sense, -1 | 1)
    {
        return Ok(None);
    }
    Ok(Some(ConeCarrier {
        apex,
        axis: axis_dir.normalized()?,
        half_angle: half_angle_rad,
        sense,
        deviation: region.max_deviation.max(0.0),
    }))
}

fn carrier_sphere(region: &MeshRegion) -> Result<Option<SphereCarrier>, String> {
    let RegionCarrier::Sphere {
        center,
        radius,
        sense,
    } = region.carrier
    else {
        return Ok(None);
    };
    if !radius.is_finite() || radius <= 0.0 || !matches!(sense, -1 | 1) {
        return Ok(None);
    }
    Ok(Some(SphereCarrier {
        center,
        radius,
        sense,
        deviation: region.max_deviation.max(0.0),
    }))
}

fn sphere_radial_error(sphere: SphereCarrier, point: Vec3) -> f64 {
    (point.sub(sphere.center).length() - sphere.radius).abs()
}

/// A sphere/plane edge is a circle whenever both ends lie in the plane and
/// on the sphere; whether that circle is a ring of the chosen polar axis is
/// settled when the sphere surface is framed.
fn sphere_plane_edge_supported(
    sphere: SphereCarrier,
    plane: PlaneInfo,
    a: Vec3,
    b: Vec3,
    tolerance: f64,
) -> bool {
    let Ok(normal) = plane.normal.normalized() else {
        return false;
    };
    let bar = accepted_radial_bar(tolerance, sphere.deviation);
    a.sub(plane.origin).dot(normal).abs() <= tolerance
        && b.sub(plane.origin).dot(normal).abs() <= tolerance
        && sphere_radial_error(sphere, a) <= bar
        && sphere_radial_error(sphere, b) <= bar
}

/// A sphere meets a coaxial revolve (its center on the axis) along a ring:
/// both ends at one station on the axis, on the sphere and on the revolve.
fn sphere_revolve_edge_supported(
    sphere: SphereCarrier,
    axis_origin: Vec3,
    axis: Vec3,
    on_revolve: impl Fn(Vec3) -> bool,
    a: Vec3,
    b: Vec3,
    tolerance: f64,
) -> bool {
    let offset = sphere.center.sub(axis_origin);
    if offset.sub(axis.scale(offset.dot(axis))).length() > tolerance * 4.0 {
        return false;
    }
    let bar = accepted_radial_bar(tolerance * 4.0, sphere.deviation);
    (a.sub(axis_origin).dot(axis) - b.sub(axis_origin).dot(axis)).abs() <= tolerance * 4.0
        && sphere_radial_error(sphere, a) <= bar
        && sphere_radial_error(sphere, b) <= bar
        && on_revolve(a)
        && on_revolve(b)
}

fn sphere_cylinder_edge_supported(
    sphere: SphereCarrier,
    cylinder: CylinderCarrier,
    a: Vec3,
    b: Vec3,
    tolerance: f64,
) -> bool {
    sphere_revolve_edge_supported(
        sphere,
        cylinder.origin,
        cylinder.axis,
        |point| {
            (radial(cylinder, point).length() - cylinder.radius).abs()
                <= accepted_radial_bar(tolerance * 4.0, cylinder.deviation)
        },
        a,
        b,
        tolerance,
    )
}

fn sphere_cone_edge_supported(
    sphere: SphereCarrier,
    cone: ConeCarrier,
    a: Vec3,
    b: Vec3,
    tolerance: f64,
) -> bool {
    sphere_revolve_edge_supported(
        sphere,
        cone.apex,
        cone.axis,
        |point| point_on_cone(cone, point, accepted_radial_bar(tolerance * 4.0, cone.deviation)),
        a,
        b,
        tolerance,
    )
}

fn edge_is_cylinder_ruling(cylinder: CylinderCarrier, a: Vec3, b: Vec3, tolerance: f64) -> bool {
    let ra = radial(cylinder, a);
    let rb = radial(cylinder, b);
    let Ok(ua) = ra.normalized() else {
        return false;
    };
    let Ok(ub) = rb.normalized() else {
        return false;
    };
    let chord = b.sub(a);
    let Ok(direction) = chord.normalized() else {
        return false;
    };
    (ra.length() - cylinder.radius).abs() <= tolerance
        && (rb.length() - cylinder.radius).abs() <= tolerance
        && ua.cross(ub).length() <= 0.01
        && direction.cross(cylinder.axis).length() <= 0.01
}

/// Where two cylinders of one radius whose axes cross meet: the two planes
/// through the crossing point normal to the sum and to the difference of the
/// axes (each cuts both walls in the same ellipse), or `None` when the pair
/// is not such a crossing within the exact-topology bar.
fn cylinder_pair_section(
    first: CylinderCarrier,
    second: CylinderCarrier,
    tolerance: f64,
) -> Option<(Vec3, [Vec3; 2])> {
    let bar = accepted_radial_bar(tolerance * 4.0, first.deviation.max(second.deviation));
    if (first.radius - second.radius).abs() > bar {
        return None;
    }
    let (a1, a2) = (first.axis.normalized().ok()?, second.axis.normalized().ok()?);
    let across = a1.cross(a2);
    if across.length() <= 0.1 {
        return None;
    }
    // Closest points of the two axis lines.
    let w0 = first.origin.sub(second.origin);
    let (b, d, e) = (a1.dot(a2), a1.dot(w0), a2.dot(w0));
    let denominator = 1.0 - b * b;
    let s = (b * e - d) / denominator;
    let t = (e - b * d) / denominator;
    let p1 = first.origin.add(a1.scale(s));
    let p2 = second.origin.add(a2.scale(t));
    if p1.sub(p2).length() > tolerance * 4.0 {
        return None;
    }
    let normals = [a1.add(a2).normalized().ok()?, a1.sub(a2).normalized().ok()?];
    Some((p1.add(p2).scale(0.5), normals))
}

/// The section plane normal a cylinder/cylinder edge lies in, when both
/// ends are on both walls and on one plane of [`cylinder_pair_section`].
fn cylinder_pair_section_normal(
    first: CylinderCarrier,
    second: CylinderCarrier,
    points: &[Vec3],
    tolerance: f64,
) -> Option<(Vec3, Vec3)> {
    let (center, normals) = cylinder_pair_section(first, second, tolerance)?;
    let bar = |cylinder: CylinderCarrier| accepted_radial_bar(tolerance * 4.0, cylinder.deviation);
    if points.iter().any(|&point| {
        (radial(first, point).length() - first.radius).abs() > bar(first)
            || (radial(second, point).length() - second.radius).abs() > bar(second)
    }) {
        return None;
    }
    normals
        .into_iter()
        .find(|normal| {
            points
                .iter()
                .all(|point| point.sub(center).dot(*normal).abs() <= tolerance * 4.0)
        })
        .map(|normal| (center, normal))
}

fn cylinder_plane_edge_supported(
    cylinder: CylinderCarrier,
    plane: PlaneInfo,
    a: Vec3,
    b: Vec3,
    tolerance: f64,
) -> bool {
    let Ok(normal) = plane.normal.normalized() else {
        return false;
    };
    if a.sub(plane.origin).dot(normal).abs() > tolerance
        || b.sub(plane.origin).dot(normal).abs() > tolerance
    {
        return false;
    }
    let ra = radial(cylinder, a);
    let rb = radial(cylinder, b);
    let looks_like_ruling = (station(cylinder, a) - station(cylinder, b)).abs() > tolerance
        && ra
            .normalized()
            .ok()
            .zip(rb.normalized().ok())
            .is_some_and(|(ua, ub)| ua.cross(ub).length() <= 0.05);
    if looks_like_ruling {
        // The exact axial line is contained by the plane only when its
        // direction is tangent to that plane.
        return normal.dot(cylinder.axis).abs() <= 0.01;
    }
    let on_ring = (station(cylinder, a) - station(cylinder, b)).abs() <= tolerance * 4.0;
    // A constant-station circle lies in the neighboring plane only when that
    // plane is perpendicular to the cylinder axis.
    on_ring && normal.cross(cylinder.axis).length() <= 0.01
}

fn cone_station(cone: ConeCarrier, point: Vec3) -> f64 {
    point.sub(cone.apex).dot(cone.axis)
}

fn cone_radial(cone: ConeCarrier, point: Vec3) -> Vec3 {
    let delta = point.sub(cone.apex);
    delta.sub(cone.axis.scale(delta.dot(cone.axis)))
}

fn point_on_cone(cone: ConeCarrier, point: Vec3, tolerance: f64) -> bool {
    let station = cone_station(cone, point);
    station >= -tolerance
        && (cone_radial(cone, point).length() - station * cone.half_angle.tan()).abs() <= tolerance
}

fn edge_is_cone_ruling(cone: ConeCarrier, a: Vec3, b: Vec3, tolerance: f64) -> bool {
    if !point_on_cone(cone, a, tolerance) || !point_on_cone(cone, b, tolerance) {
        return false;
    }
    let ra = cone_radial(cone, a);
    let rb = cone_radial(cone, b);
    let (Ok(ua), Ok(ub), Ok(direction)) = (ra.normalized(), rb.normalized(), b.sub(a).normalized())
    else {
        return false;
    };
    let expected = cone
        .axis
        .add(ua.scale(cone.half_angle.tan()))
        .normalized()
        .unwrap_or(cone.axis);
    ua.cross(ub).length() <= 0.01
        && direction
            .cross(expected)
            .length()
            .min(direction.cross(expected.scale(-1.0)).length())
            <= 0.01
}

fn cone_plane_edge_supported(
    cone: ConeCarrier,
    plane: PlaneInfo,
    a: Vec3,
    b: Vec3,
    tolerance: f64,
) -> bool {
    let Ok(normal) = plane.normal.normalized() else {
        return false;
    };
    let da = a.sub(plane.origin).dot(normal).abs();
    let db = b.sub(plane.origin).dot(normal).abs();
    if da > tolerance || db > tolerance {
        return false;
    }
    let ra = cone_radial(cone, a);
    let rb = cone_radial(cone, b);
    let looks_like_ruling = (cone_station(cone, a) - cone_station(cone, b)).abs() > tolerance
        && ra
            .normalized()
            .ok()
            .zip(rb.normalized().ok())
            .is_some_and(|(ua, ub)| ua.cross(ub).length() <= 0.05);
    if looks_like_ruling {
        let apex_in_plane = cone.apex.sub(plane.origin).dot(normal).abs() <= tolerance * 4.0;
        if apex_in_plane && normal.dot(b.sub(a)).abs() <= tolerance {
            return true;
        }
    }
    let on_ring = (cone_station(cone, a) - cone_station(cone, b)).abs() <= tolerance * 4.0;
    if on_ring && normal.cross(cone.axis).length() <= 0.01 {
        return true;
    }
    // A finite oblique trim is represented later as the exact projective map
    // of only its reference-circle arc. This also covers local hyperbolic and
    // parabolic branches without the mixed weights of a full-circle map.
    true
}

fn cylinder_cone_edge_supported(
    cylinder: CylinderCarrier,
    cone: ConeCarrier,
    a: Vec3,
    b: Vec3,
    tolerance: f64,
) -> bool {
    if cylinder.axis.cross(cone.axis).length() > 0.01 {
        return false;
    }
    let cylinder_station_spread = (station(cylinder, a) - station(cylinder, b)).abs();
    let cone_station_spread = (cone_station(cone, a) - cone_station(cone, b)).abs();
    cylinder_station_spread <= tolerance * 4.0
        && cone_station_spread <= tolerance * 4.0
        && (radial(cylinder, a).length() - cylinder.radius).abs() <= tolerance * 4.0
        && (radial(cylinder, b).length() - cylinder.radius).abs() <= tolerance * 4.0
        && point_on_cone(cone, a, tolerance * 4.0)
        && point_on_cone(cone, b, tolerance * 4.0)
}

/// Diagnostic stage trace, enabled by `BREP_DEBUG_HYBRID`.
fn stage_trace(stage: &str) {
    if std::env::var("BREP_DEBUG_HYBRID").is_ok() {
        eprintln!("[hybrid] stage: {stage}");
    }
}

/// Diagnostic trace of one demotion decision, enabled by `BREP_DEBUG_HYBRID`.
fn demote_trace(candidate: u32, neighbor: u32, why: &str, a: Vec3, b: Vec3) {
    if std::env::var("BREP_DEBUG_HYBRID").is_ok() {
        eprintln!(
            "[hybrid] demote region {candidate} (neighbour {neighbor}): {why}; edge ({:.4},{:.4},{:.4})-({:.4},{:.4},{:.4})",
            a.x, a.y, a.z, b.x, b.y, b.z
        );
    }
}

fn facet_plane(triangle: [usize; 3], vertices: &[Vec3]) -> Result<PlaneInfo, String> {
    let a = vertices[triangle[0]];
    let b = vertices[triangle[1]];
    let c = vertices[triangle[2]];
    let normal = b.sub(a).cross(c.sub(a)).normalized().map_err(|_| {
        "hybrid BREP: cannot create a planar face from a degenerate source triangle".to_owned()
    })?;
    Ok(PlaneInfo { origin: a, normal })
}

fn facets_are_coplanar(
    first: [usize; 3],
    second: [usize; 3],
    vertices: &[Vec3],
    tolerance: f64,
) -> Result<bool, String> {
    let first_plane = facet_plane(first, vertices)?;
    let second_plane = facet_plane(second, vertices)?;
    // Do not turn a shallow crease into a plane merely because a distance
    // tolerance happens to cover its short triangles. The distance checks
    // below cover f32 STL coordinate noise; this angular check remains
    // deliberately close to exact coplanarity.
    if first_plane.normal.dot(second_plane.normal) <= 0.0
        || first_plane.normal.cross(second_plane.normal).length() > 1.0e-8
    {
        return Ok(false);
    }
    Ok(first.iter().chain(second.iter()).all(|&vertex| {
        let point = vertices[vertex];
        point.sub(first_plane.origin).dot(first_plane.normal).abs() <= tolerance
            && point
                .sub(second_plane.origin)
                .dot(second_plane.normal)
                .abs()
                <= tolerance
    }))
}

/// Replace unsupported regions with safely merged coplanar patches and
/// recursively demote revolved regions that cannot share exact edges with them.
fn local_regions(
    regions: &[MeshRegion],
    triangle_regions: &[u32],
    triangles: &[[usize; 3]],
    vertices: &[Vec3],
    incidence: &BTreeMap<EdgeKey, Vec<usize>>,
    tolerance: f64,
    forced_demotions: &BTreeSet<u32>,
) -> Result<LocalRegions, String> {
    let mut original_cylinders = HashMap::<u32, CylinderCarrier>::new();
    let mut original_cones = HashMap::<u32, ConeCarrier>::new();
    let mut original_spheres = HashMap::<u32, SphereCarrier>::new();
    let mut original_planes = HashMap::<u32, PlaneInfo>::new();
    let mut retained = BTreeSet::<u32>::new();
    for region in regions {
        if let RegionCarrier::Plane { origin, normal } = region.carrier {
            original_planes.insert(region.id, PlaneInfo { origin, normal });
        }
        if let Some(cylinder) = carrier_cylinder(region)? {
            original_cylinders.insert(region.id, cylinder);
            if !forced_demotions.contains(&region.id) {
                retained.insert(region.id);
            }
        }
        if let Some(cone) = carrier_cone(region)? {
            original_cones.insert(region.id, cone);
            if !forced_demotions.contains(&region.id) {
                retained.insert(region.id);
            }
        }
        if let Some(sphere) = carrier_sphere(region)? {
            original_spheres.insert(region.id, sphere);
            if !forced_demotions.contains(&region.id) {
                retained.insert(region.id);
            }
        }
    }
    #[derive(Clone, Copy)]
    enum Revolve {
        Cylinder(CylinderCarrier),
        Cone(ConeCarrier),
        Sphere(SphereCarrier),
    }
    let revolve_of = |id: u32| -> Option<Revolve> {
        original_cylinders
            .get(&id)
            .map(|&cylinder| Revolve::Cylinder(cylinder))
            .or_else(|| original_cones.get(&id).map(|&cone| Revolve::Cone(cone)))
            .or_else(|| original_spheres.get(&id).map(|&sphere| Revolve::Sphere(sphere)))
    };
    // A sphere ring stays exact against a coaxial revolve even when that
    // revolve was itself demoted for one of its other trims.
    let sphere_ring_with = |sphere: SphereCarrier, other: Revolve, a: Vec3, b: Vec3| -> bool {
        match other {
            Revolve::Cylinder(cylinder) => {
                sphere_cylinder_edge_supported(sphere, cylinder, a, b, tolerance)
            }
            Revolve::Cone(cone) => sphere_cone_edge_supported(sphere, cone, a, b, tolerance),
            Revolve::Sphere(_) => false,
        }
    };

    loop {
        let mut demote = BTreeSet::new();
        for (&(a, b), incident) in incidence {
            let first = triangle_regions[incident[0]];
            let second = triangle_regions[incident[1]];
            if first == second {
                continue;
            }
            for (candidate, neighbor) in [(first, second), (second, first)] {
                if !retained.contains(&candidate) {
                    continue;
                }
                let Some(own) = revolve_of(candidate) else {
                    continue;
                };
                if retained.contains(&neighbor) {
                    let (supported, why) = match (own, revolve_of(neighbor)) {
                        (Revolve::Cylinder(cylinder), Some(Revolve::Cone(cone)))
                        | (Revolve::Cone(cone), Some(Revolve::Cylinder(cylinder))) => (
                            cylinder_cone_edge_supported(
                                cylinder,
                                cone,
                                vertices[a],
                                vertices[b],
                                tolerance,
                            ),
                            "cylinder/cone edge unsupported",
                        ),
                        // Two cylinders meet exactly along a shared axial
                        // ruling: a tangent chain of arcs on parallel axes
                        // (a rounded outline, a fillet running out into a
                        // boss). Any other cylinder/cylinder curve is not
                        // yet proven exact.
                        // Two cylinders of one radius whose axes cross meet
                        // in planar ellipses (crossing bores).
                        (Revolve::Cylinder(first), Some(Revolve::Cylinder(second))) => (
                            (edge_is_cylinder_ruling(first, vertices[a], vertices[b], tolerance)
                                && edge_is_cylinder_ruling(
                                    second,
                                    vertices[a],
                                    vertices[b],
                                    tolerance,
                                ))
                                || cylinder_pair_section_normal(
                                    first,
                                    second,
                                    &[vertices[a], vertices[b]],
                                    tolerance,
                                )
                                .is_some(),
                            "cylinder/cylinder edge is neither a shared ruling nor an equal-radius crossing",
                        ),
                        (Revolve::Sphere(sphere), Some(other))
                        | (other, Some(Revolve::Sphere(sphere))) => (
                            sphere_ring_with(sphere, other, vertices[a], vertices[b]),
                            "sphere edge is not a coaxial ring",
                        ),
                        // Cone/cone intersections are not yet proven exact.
                        _ => (false, "same-kind revolve adjacency"),
                    };
                    if !supported {
                        demote_trace(candidate, neighbor, why, vertices[a], vertices[b]);
                        demote.insert(candidate);
                        demote.insert(neighbor);
                    }
                    continue;
                }
                // A revolve may have been locally demoted because one of its
                // other trims is unsupported. Its shared ring with a coaxial
                // neighbor remains exact, so that neighbor need not be
                // demoted with it.
                match (own, revolve_of(neighbor)) {
                    (Revolve::Cylinder(cylinder), Some(Revolve::Cone(cone))) => {
                        if cylinder_cone_edge_supported(
                            cylinder,
                            cone,
                            vertices[a],
                            vertices[b],
                            tolerance,
                        ) {
                            continue;
                        }
                    }
                    (Revolve::Sphere(sphere), Some(other)) => {
                        if sphere_ring_with(sphere, other, vertices[a], vertices[b]) {
                            continue;
                        }
                    }
                    (other, Some(Revolve::Sphere(sphere))) => {
                        if sphere_ring_with(sphere, other, vertices[a], vertices[b]) {
                            continue;
                        }
                    }
                    _ => {}
                }
                if let Some(&plane) = original_planes.get(&neighbor) {
                    let supported = match own {
                        Revolve::Cylinder(cylinder) => cylinder_plane_edge_supported(
                            cylinder,
                            plane,
                            vertices[a],
                            vertices[b],
                            tolerance,
                        ),
                        Revolve::Cone(cone) => cone_plane_edge_supported(
                            cone,
                            plane,
                            vertices[a],
                            vertices[b],
                            tolerance,
                        ),
                        Revolve::Sphere(sphere) => sphere_plane_edge_supported(
                            sphere,
                            plane,
                            vertices[a],
                            vertices[b],
                            tolerance,
                        ),
                    };
                    if !supported {
                        demote_trace(candidate, neighbor, "edge with plane neighbour unsupported", vertices[a], vertices[b]);
                        demote.insert(candidate);
                    }
                    continue;
                }
                // Only a ruling can be shared exactly with a facet; a
                // sphere has none.
                let supported = match own {
                    Revolve::Cylinder(cylinder) => {
                        edge_is_cylinder_ruling(cylinder, vertices[a], vertices[b], tolerance)
                    }
                    Revolve::Cone(cone) => {
                        edge_is_cone_ruling(cone, vertices[a], vertices[b], tolerance)
                    }
                    Revolve::Sphere(_) => false,
                };
                if !supported {
                    demote_trace(candidate, neighbor, "edge with non-plane neighbour is not a ruling", vertices[a], vertices[b]);
                    demote.insert(candidate);
                }
            }
        }
        if demote.is_empty() {
            break;
        }
        for region in demote {
            retained.remove(&region);
        }
    }

    let demoted = original_cylinders
        .keys()
        .chain(original_cones.keys())
        .chain(original_spheres.keys())
        .copied()
        .filter(|id| !retained.contains(id))
        .collect::<BTreeSet<_>>();
    let mut planes = HashMap::new();
    let mut cylinders = HashMap::new();
    let mut cones = HashMap::new();
    let mut spheres = HashMap::new();
    let mut original_to_local = HashMap::<u32, u32>::new();
    let mut local_to_original = HashMap::<u32, u32>::new();
    let mut next = 0_u32;
    for region in regions {
        match region.carrier {
            RegionCarrier::Plane { origin, normal } => {
                original_to_local.insert(region.id, next);
                local_to_original.insert(next, region.id);
                planes.insert(next, PlaneInfo { origin, normal });
                next += 1;
            }
            RegionCarrier::Cylinder { .. } if retained.contains(&region.id) => {
                original_to_local.insert(region.id, next);
                local_to_original.insert(next, region.id);
                cylinders.insert(next, original_cylinders[&region.id]);
                next += 1;
            }
            RegionCarrier::Cone { .. } if retained.contains(&region.id) => {
                original_to_local.insert(region.id, next);
                local_to_original.insert(next, region.id);
                cones.insert(next, original_cones[&region.id]);
                next += 1;
            }
            RegionCarrier::Sphere { .. } if retained.contains(&region.id) => {
                original_to_local.insert(region.id, next);
                local_to_original.insert(next, region.id);
                spheres.insert(next, original_spheres[&region.id]);
                next += 1;
            }
            _ => {}
        }
    }

    if std::env::var("BREP_DEBUG_HYBRID").is_ok() {
        for (&local, cylinder) in &cylinders {
            eprintln!(
                "[hybrid] region {local} (original {}): cylinder r={:.4} axis=({:+.4},{:+.4},{:+.4}) origin=({:.3},{:.3},{:.3}) sense={}",
                local_to_original[&local],
                cylinder.radius,
                cylinder.axis.x, cylinder.axis.y, cylinder.axis.z,
                cylinder.origin.x, cylinder.origin.y, cylinder.origin.z,
                cylinder.sense
            );
        }
    }
    let fallback = triangle_regions
        .iter()
        .map(|original| !original_to_local.contains_key(original))
        .collect::<Vec<_>>();
    let mut neighbors = vec![Vec::<usize>::new(); triangles.len()];
    for incident in incidence.values() {
        let first = incident[0];
        let second = incident[1];
        if fallback[first] && fallback[second] {
            neighbors[first].push(second);
            neighbors[second].push(first);
        }
    }

    // Check every member against one fixed seed carrier. Pairwise-only
    // transitive merging could otherwise accumulate a long sequence of tiny
    // creases into a patch that is not actually planar.
    let mut fallback_component = vec![None; triangles.len()];
    let mut fallback_components = 0_usize;
    for seed in 0..triangles.len() {
        if !fallback[seed] || fallback_component[seed].is_some() {
            continue;
        }
        let component = fallback_components;
        fallback_components += 1;
        fallback_component[seed] = Some(component);
        let mut queue = VecDeque::from([seed]);
        while let Some(current) = queue.pop_front() {
            for &neighbor in &neighbors[current] {
                if fallback_component[neighbor].is_none()
                    && facets_are_coplanar(
                        triangles[seed],
                        triangles[neighbor],
                        vertices,
                        tolerance,
                    )?
                {
                    fallback_component[neighbor] = Some(component);
                    queue.push_back(neighbor);
                }
            }
        }
    }

    let mut fallback_to_local = HashMap::<usize, u32>::new();
    let mut local_triangle_regions = Vec::with_capacity(triangles.len());
    for (triangle_id, &original) in triangle_regions.iter().enumerate() {
        if let Some(&local) = original_to_local.get(&original) {
            local_triangle_regions.push(local);
        } else {
            let component = fallback_component[triangle_id]
                .ok_or_else(|| "hybrid BREP: fallback component was not assigned".to_owned())?;
            let local = if let Some(&local) = fallback_to_local.get(&component) {
                local
            } else {
                let local = next;
                next += 1;
                planes.insert(local, facet_plane(triangles[triangle_id], vertices)?);
                fallback_to_local.insert(component, local);
                local
            };
            local_triangle_regions.push(local);
        }
    }
    let analytic_plane_triangles = triangle_regions
        .iter()
        .filter(|region| original_planes.contains_key(region))
        .count();
    let analytic_cylinder_triangles = triangle_regions
        .iter()
        .filter(|region| retained.contains(region) && original_cylinders.contains_key(region))
        .count();
    let analytic_cone_triangles = triangle_regions
        .iter()
        .filter(|region| retained.contains(region) && original_cones.contains_key(region))
        .count();
    let analytic_sphere_triangles = triangle_regions
        .iter()
        .filter(|region| retained.contains(region) && original_spheres.contains_key(region))
        .count();
    let demoted_triangles = triangle_regions
        .iter()
        .filter(|region| demoted.contains(region))
        .count();
    let faceted_triangles = triangles.len()
        - analytic_plane_triangles
        - analytic_cylinder_triangles
        - analytic_cone_triangles
        - analytic_sphere_triangles;
    let stats = HybridBrepStats {
        total_faces: next as usize,
        analytic_plane_faces: original_planes.len(),
        analytic_plane_triangles,
        analytic_cylinder_faces: original_cylinders
            .keys()
            .filter(|id| retained.contains(id))
            .count(),
        analytic_cylinder_triangles,
        analytic_cone_faces: original_cones
            .keys()
            .filter(|id| retained.contains(id))
            .count(),
        analytic_cone_triangles,
        analytic_sphere_faces: original_spheres
            .keys()
            .filter(|id| retained.contains(id))
            .count(),
        analytic_sphere_triangles,
        faceted_faces: fallback_to_local.len(),
        faceted_triangles,
        demoted_regions: demoted.len(),
        demoted_triangles,
        segmentation_plane_regions: original_planes.len(),
        segmentation_cylinder_regions: original_cylinders.len(),
        segmentation_cone_regions: original_cones.len(),
        segmentation_sphere_regions: original_spheres.len(),
        segmentation_unsupported_regions: regions.len()
            - original_planes.len()
            - original_cylinders.len()
            - original_cones.len()
            - original_spheres.len(),
    };
    Ok(LocalRegions {
        triangle_regions: local_triangle_regions,
        planes,
        cylinders,
        cones,
        spheres,
        local_to_original,
        count: next as usize,
        stats,
    })
}

fn region_boundaries(
    triangles: &[[usize; 3]],
    regions: &[u32],
    region_count: usize,
    incidence: &BTreeMap<EdgeKey, Vec<usize>>,
) -> Result<(Vec<Chain>, Vec<RegionBoundary>), String> {
    let mut chains = Vec::<Chain>::new();
    let mut chain_by_edges = BTreeMap::<Vec<EdgeKey>, usize>::new();
    let mut boundaries = Vec::with_capacity(region_count);
    for region in 0..region_count as u32 {
        let mut outgoing = BTreeMap::<usize, (usize, u32)>::new();
        for (triangle_id, triangle) in triangles.iter().enumerate() {
            if regions[triangle_id] != region {
                continue;
            }
            for corner in 0..3 {
                let a = triangle[corner];
                let b = triangle[(corner + 1) % 3];
                let incident = &incidence[&edge_key(a, b)];
                let other = incident.iter().copied().find(|&id| id != triangle_id);
                let neighbor = other.map(|id| regions[id]).unwrap_or(u32::MAX);
                if neighbor != region && outgoing.insert(a, (b, neighbor)).is_some() {
                    return Err(format!(
                        "hybrid BREP: region {region} boundary pinches at vertex {a}"
                    ));
                }
            }
        }
        let starts = outgoing.keys().copied().collect::<Vec<_>>();
        let mut visited = BTreeSet::new();
        let mut cycles = Vec::new();
        for start in starts {
            if visited.contains(&start) {
                continue;
            }
            let mut steps = Vec::<(usize, usize, u32)>::new();
            let mut current = start;
            loop {
                if !visited.insert(current) {
                    return Err(format!(
                        "hybrid BREP: region {region} boundary self-crosses"
                    ));
                }
                let &(next, neighbor) = outgoing
                    .get(&current)
                    .ok_or_else(|| format!("hybrid BREP: region {region} boundary walk is open"))?;
                steps.push((current, next, neighbor));
                current = next;
                if current == start {
                    break;
                }
            }
            let all_one_neighbor = steps.iter().all(|step| step.2 == steps[0].2);
            let mut groups = Vec::<Vec<(usize, usize, u32)>>::new();
            if all_one_neighbor {
                groups.push(steps);
            } else {
                let cut = (0..steps.len())
                    .find(|&index| {
                        steps[index].2 != steps[(index + steps.len() - 1) % steps.len()].2
                    })
                    .unwrap();
                steps.rotate_left(cut);
                let mut begin = 0;
                while begin < steps.len() {
                    let neighbor = steps[begin].2;
                    let mut end = begin + 1;
                    while end < steps.len() && steps[end].2 == neighbor {
                        end += 1;
                    }
                    groups.push(steps[begin..end].to_vec());
                    begin = end;
                }
            }
            let mut traversals = Vec::new();
            for group in groups {
                // `steps` is one boundary cycle. It may be only one of several
                // cycles owned by this region, so comparing against the
                // region-wide outgoing-edge count would misclassify a hole ring.
                let closed = all_one_neighbor;
                let mut chain_vertices = vec![group[0].0];
                chain_vertices.extend(group.iter().map(|step| step.1));
                if closed {
                    chain_vertices.pop();
                }
                let mut key = group
                    .iter()
                    .map(|step| edge_key(step.0, step.1))
                    .collect::<Vec<_>>();
                key.sort_unstable();
                let chain_id = if let Some(&existing) = chain_by_edges.get(&key) {
                    existing
                } else {
                    let id = chains.len();
                    let adjacent = (region.min(group[0].2), region.max(group[0].2));
                    chains.push(Chain {
                        vertices: chain_vertices.clone(),
                        closed,
                        adjacent,
                    });
                    chain_by_edges.insert(key, id);
                    id
                };
                let stored = &chains[chain_id];
                let a = chain_vertices[0];
                let b = chain_vertices[1];
                let position = stored
                    .vertices
                    .iter()
                    .position(|&vertex| vertex == a)
                    .ok_or("hybrid BREP: shared chain start is missing")?;
                let forward = if stored.closed {
                    stored.vertices[(position + 1) % stored.vertices.len()] == b
                } else if position + 1 < stored.vertices.len() && stored.vertices[position + 1] == b
                {
                    true
                } else if position > 0 && stored.vertices[position - 1] == b {
                    false
                } else {
                    return Err("hybrid BREP: shared chain directions disagree".into());
                };
                traversals.push(Traversal {
                    chain: chain_id,
                    forward,
                });
            }
            cycles.push(traversals);
        }
        boundaries.push(RegionBoundary { cycles });
    }
    Ok((chains, boundaries))
}

/// Two planar regions meet along a line, so their chain is one straight
/// edge — unless one of them is a leftover facet whose two edges into the
/// plane bend at a shared vertex. Such a chain becomes one straight edge
/// per bend, and every traversal of it in the region cycles is rewritten in
/// order.
fn split_bent_planar_chains(
    chains: Vec<Chain>,
    boundaries: &mut [RegionBoundary],
    vertices: &[Vec3],
    tolerance: f64,
    is_revolve: impl Fn(u32) -> bool,
) -> Vec<Chain> {
    let mut result = Vec::with_capacity(chains.len());
    let mut replacement: Vec<Vec<usize>> = Vec::with_capacity(chains.len());
    for chain in chains {
        let planar = !is_revolve(chain.adjacent.0) && !is_revolve(chain.adjacent.1);
        let points = chain_points(&chain, vertices);
        if !planar || chain.closed || points.len() < 3 || max_line_deviation(&points) <= tolerance {
            replacement.push(vec![result.len()]);
            result.push(chain);
            continue;
        }
        // Greedy runs of collinear mesh edges.
        let mut pieces = Vec::new();
        let mut start = 0;
        for end in 2..chain.vertices.len() {
            if max_line_deviation(&points[start..=end]) > tolerance {
                pieces.push(result.len());
                result.push(Chain {
                    vertices: chain.vertices[start..end].to_vec(),
                    closed: false,
                    adjacent: chain.adjacent,
                });
                start = end - 1;
            }
        }
        pieces.push(result.len());
        result.push(Chain {
            vertices: chain.vertices[start..].to_vec(),
            closed: false,
            adjacent: chain.adjacent,
        });
        replacement.push(pieces);
    }
    for boundary in boundaries.iter_mut() {
        for cycle in &mut boundary.cycles {
            let mut rewritten = Vec::with_capacity(cycle.len());
            for traversal in cycle.iter() {
                let pieces = &replacement[traversal.chain];
                if traversal.forward {
                    rewritten.extend(pieces.iter().map(|&chain| Traversal {
                        chain,
                        forward: true,
                    }));
                } else {
                    rewritten.extend(pieces.iter().rev().map(|&chain| Traversal {
                        chain,
                        forward: false,
                    }));
                }
            }
            *cycle = rewritten;
        }
    }
    result
}

fn chain_points(chain: &Chain, vertices: &[Vec3]) -> Vec<Vec3> {
    chain.vertices.iter().map(|&id| vertices[id]).collect()
}

fn snap_cylinder_axes(
    cylinders: &mut HashMap<u32, CylinderCarrier>,
    planes: &HashMap<u32, PlaneInfo>,
    chains: &[Chain],
    vertices: &[Vec3],
    tolerance: f64,
) -> Result<(), (u32, String)> {
    for (&region, cylinder) in cylinders.iter_mut() {
        let mut candidates = Vec::new();
        for chain in chains.iter().filter(|chain| {
            chain.closed && (chain.adjacent.0 == region || chain.adjacent.1 == region)
        }) {
            let neighbor = if chain.adjacent.0 == region {
                chain.adjacent.1
            } else {
                chain.adjacent.0
            };
            let Some(plane) = planes.get(&neighbor) else {
                continue;
            };
            if plane.normal.cross(cylinder.axis).length() > 0.01 {
                continue;
            }
            let points = chain_points(chain, vertices);
            let spread = points
                .iter()
                .map(|point| point.sub(plane.origin).dot(plane.normal).abs())
                .fold(0.0_f64, f64::max);
            if spread <= tolerance {
                let axis = if plane.normal.dot(cylinder.axis) >= 0.0 {
                    plane.normal
                } else {
                    plane.normal.scale(-1.0)
                };
                candidates.push(axis.normalized().map_err(|error| (region, error))?);
            }
        }
        if let Some(&axis) = candidates.first() {
            // Independent plane fits to rounded STL rings need not have
            // bit-identical normals. Bound their disagreement by the
            // positional error it induces around this cylinder's rim,
            // using the same tolerance as the shared topology checks.
            if candidates
                .iter()
                .any(|other| cylinder.radius * other.cross(axis).length() > tolerance)
            {
                return Err((
                    region,
                    format!("hybrid BREP: cylinder {region} has inconsistent ring normals"),
                ));
            }
            cylinder.axis = axis;
        }
    }
    Ok(())
}

fn snap_cone_carriers(
    cones: &mut HashMap<u32, ConeCarrier>,
    cylinders: &HashMap<u32, CylinderCarrier>,
    planes: &HashMap<u32, PlaneInfo>,
    chains: &[Chain],
    vertices: &[Vec3],
    tolerance: f64,
) -> Result<(), (u32, String)> {
    for (&region, cone) in cones.iter_mut() {
        let relevant = chains
            .iter()
            .filter(|chain| chain.adjacent.0 == region || chain.adjacent.1 == region)
            .collect::<Vec<_>>();
        let mut snapped = None::<(Vec3, Vec3)>;

        // A cylinder/cone join is a shared circle. The cylinder fixes the
        // coaxial frame and the cone apex then follows from its opening angle.
        for chain in &relevant {
            let neighbor = if chain.adjacent.0 == region {
                chain.adjacent.1
            } else {
                chain.adjacent.0
            };
            let Some(cylinder) = cylinders.get(&neighbor).copied() else {
                continue;
            };
            let mut axis = cylinder.axis;
            if axis.dot(cone.axis) < 0.0 {
                axis = axis.scale(-1.0);
            }
            let points = chain_points(chain, vertices);
            let center_station = points
                .iter()
                .map(|&point| point.sub(cylinder.origin).dot(axis))
                .sum::<f64>()
                / points.len() as f64;
            let center = cylinder.origin.add(axis.scale(center_station));

            let mut rings = Vec::<(f64, f64)>::new();
            for boundary_chain in &relevant {
                let boundary_points = chain_points(boundary_chain, vertices);
                let stations = boundary_points
                    .iter()
                    .map(|&point| point.sub(cylinder.origin).dot(axis))
                    .collect::<Vec<_>>();
                let low_station = stations.iter().copied().fold(f64::INFINITY, f64::min);
                let high_station = stations.iter().copied().fold(f64::NEG_INFINITY, f64::max);
                if high_station - low_station > tolerance * 4.0 {
                    continue;
                }
                let station = stations.iter().sum::<f64>() / stations.len() as f64;
                let neighbor = if boundary_chain.adjacent.0 == region {
                    boundary_chain.adjacent.1
                } else {
                    boundary_chain.adjacent.0
                };
                let radius = if cylinders.contains_key(&neighbor) {
                    cylinder.radius
                } else {
                    boundary_points
                        .iter()
                        .map(|&point| {
                            let delta = point.sub(cylinder.origin);
                            delta.sub(axis.scale(delta.dot(axis))).length()
                        })
                        .sum::<f64>()
                        / boundary_points.len() as f64
                };
                rings.push((station, radius));
            }
            rings.sort_by(|a, b| a.0.total_cmp(&b.0));
            if let (Some(&(s0, r0)), Some(&(s1, r1))) = (rings.first(), rings.last()) {
                let slope = (r1 - r0) / (s1 - s0);
                if slope.is_finite() && slope > 1.0e-6 {
                    let apex_station = s0 - r0 / slope;
                    cone.apex = cylinder.origin.add(axis.scale(apex_station));
                    cone.axis = axis;
                    cone.half_angle = slope.atan();
                    snapped = Some((cone.apex, axis));
                    break;
                }
            }

            // Short cone patches fit their apex poorly from tessellated
            // normals. Their two side planes, however, contain the exact cone
            // apex. Intersect those planes with the coaxial line fixed by the
            // adjacent cylinder, then derive the half-angle from the common
            // cylinder/cone ring.
            let mut apex_stations = Vec::new();
            for side_chain in &relevant {
                let side_neighbor = if side_chain.adjacent.0 == region {
                    side_chain.adjacent.1
                } else {
                    side_chain.adjacent.0
                };
                let Some(side_plane) = planes.get(&side_neighbor) else {
                    continue;
                };
                let side_points = chain_points(side_chain, vertices);
                let side_spread = side_points
                    .iter()
                    .map(|&point| point.sub(cylinder.origin).dot(axis))
                    .fold((f64::INFINITY, f64::NEG_INFINITY), |(low, high), value| {
                        (low.min(value), high.max(value))
                    });
                if side_spread.1 - side_spread.0 <= tolerance * 4.0 {
                    continue;
                }
                let normal = side_plane
                    .normal
                    .normalized()
                    .map_err(|error| (region, error))?;
                let denominator = normal.dot(axis);
                if denominator.abs() <= 1.0e-8 {
                    continue;
                }
                apex_stations
                    .push(normal.dot(side_plane.origin.sub(cylinder.origin)) / denominator);
            }
            let side_solution = if !apex_stations.is_empty() {
                let apex_station = apex_stations.iter().sum::<f64>() / apex_stations.len() as f64;
                let axial_radius = center_station - apex_station;
                if axial_radius <= tolerance {
                    None
                } else {
                    Some((
                        cylinder.origin.add(axis.scale(apex_station)),
                        (cylinder.radius / axial_radius).atan(),
                    ))
                }
            } else {
                None
            };
            // Accept a side-plane apex only when it corroborates the fitted
            // opening angle. Otherwise those planes cut the cone in genuine
            // oblique conics and do not contain its apex.
            let (apex, half_angle) = match side_solution {
                Some((apex, angle)) if (angle - cone.half_angle).abs() <= 0.01 => (apex, angle),
                _ => (
                    center.sub(axis.scale(cylinder.radius / cone.half_angle.tan())),
                    cone.half_angle,
                ),
            };
            cone.half_angle = half_angle;
            snapped = Some((apex, axis));
            break;
        }

        // Otherwise a ring in an axis-normal plane supplies an equally exact
        // frame. This covers truncated cones without an adjacent cylinder.
        if snapped.is_none() {
            for chain in relevant.iter().filter(|chain| chain.closed) {
                let neighbor = if chain.adjacent.0 == region {
                    chain.adjacent.1
                } else {
                    chain.adjacent.0
                };
                let Some(plane) = planes.get(&neighbor).copied() else {
                    continue;
                };
                let mut axis = plane.normal.normalized().map_err(|error| (region, error))?;
                if axis.dot(cone.axis) < 0.0 {
                    axis = axis.scale(-1.0);
                }
                if axis.cross(cone.axis).length() > 0.01 {
                    continue;
                }
                let points = chain_points(chain, vertices);
                let mean = points
                    .iter()
                    .copied()
                    .fold(Vec3::new(0.0, 0.0, 0.0), Vec3::add)
                    .scale(1.0 / points.len() as f64);
                let center = mean.sub(axis.scale(mean.sub(plane.origin).dot(axis)));
                let radius = points
                    .iter()
                    .map(|&point| {
                        let delta = point.sub(center);
                        delta.sub(axis.scale(delta.dot(axis))).length()
                    })
                    .sum::<f64>()
                    / points.len() as f64;
                if radius > tolerance {
                    let apex = center.sub(axis.scale(radius / cone.half_angle.tan()));
                    snapped = Some((apex, axis));
                    break;
                }
            }
        }

        if let Some((apex, axis)) = snapped {
            cone.apex = apex;
            cone.axis = axis;
        }
    }
    Ok(())
}

fn station(carrier: CylinderCarrier, point: Vec3) -> f64 {
    point.sub(carrier.origin).dot(carrier.axis)
}

fn radial(carrier: CylinderCarrier, point: Vec3) -> Vec3 {
    let delta = point.sub(carrier.origin);
    delta.sub(carrier.axis.scale(delta.dot(carrier.axis)))
}

fn build_cylinder_surfaces(
    carriers: &HashMap<u32, CylinderCarrier>,
    chains: &[Chain],
    vertices: &[Vec3],
    tolerance: f64,
) -> Result<HashMap<u32, CylinderInfo>, (u32, String)> {
    let mut result = HashMap::new();
    for (&region, &carrier) in carriers {
        let relevant = chains
            .iter()
            .filter(|chain| chain.adjacent.0 == region || chain.adjacent.1 == region)
            .collect::<Vec<_>>();
        let mut low = f64::INFINITY;
        let mut high = f64::NEG_INFINITY;
        for chain in &relevant {
            for point in chain_points(chain, vertices) {
                let value = station(carrier, point);
                low = low.min(value);
                high = high.max(value);
            }
        }
        if !low.is_finite() || high - low <= tolerance {
            return Err((
                region,
                format!("hybrid BREP: cylinder {region} has no finite axial span"),
            ));
        }
        let mut seam = None;
        for chain in &relevant {
            if chain.closed {
                continue;
            }
            let points = chain_points(chain, vertices);
            let s0 = station(carrier, points[0]);
            let s1 = station(carrier, *points.last().unwrap());
            let r0 = radial(carrier, points[0]);
            let r1 = radial(carrier, *points.last().unwrap());
            if (s1 - s0).abs() > tolerance && r0.cross(r1).length() <= tolerance * carrier.radius {
                seam = Some(r0.normalized().map_err(|error| (region, error))?);
                break;
            }
        }
        // Without a ruling, a crossing-bore ellipse arc puts the seam at its
        // end, so the arcs that chain around the wall meet it only at vertices.
        if seam.is_none() {
            for chain in &relevant {
                let neighbor = if chain.adjacent.0 == region {
                    chain.adjacent.1
                } else {
                    chain.adjacent.0
                };
                let Some(&other) = carriers.get(&neighbor) else {
                    continue;
                };
                let points = chain_points(chain, vertices);
                if !chain.closed
                    && cylinder_pair_section_normal(carrier, other, &points, tolerance).is_some()
                {
                    seam = Some(
                        radial(carrier, points[0])
                            .normalized()
                            .map_err(|error| (region, error))?,
                    );
                    break;
                }
            }
        }
        let x_axis = match seam {
            Some(axis) => axis,
            None => carrier
                .axis
                .perpendicular()
                .map_err(|error| (region, error))?,
        };
        let y_axis = carrier
            .axis
            .cross(x_axis)
            .normalized()
            .map_err(|error| (region, error))?;
        let base = carrier.origin.add(carrier.axis.scale(low));
        let profile = make_line(
            base.add(x_axis.scale(carrier.radius)),
            base.add(carrier.axis.scale(high - low))
                .add(x_axis.scale(carrier.radius)),
        )
        .map_err(|error| (region, error))?;
        let surface =
            make_revolution(base, carrier.axis, &profile, TAU).map_err(|error| (region, error))?;
        result.insert(
            region,
            CylinderInfo {
                carrier,
                base_station: low,
                height: high - low,
                x_axis,
                y_axis,
                surface,
            },
        );
    }
    Ok(result)
}

fn build_cone_surfaces(
    carriers: &HashMap<u32, ConeCarrier>,
    cylinders: &HashMap<u32, CylinderInfo>,
    chains: &[Chain],
    vertices: &[Vec3],
    tolerance: f64,
) -> Result<HashMap<u32, ConeInfo>, (u32, String)> {
    let mut result = HashMap::new();
    for (&region, &carrier) in carriers {
        let relevant = chains
            .iter()
            .filter(|chain| chain.adjacent.0 == region || chain.adjacent.1 == region)
            .collect::<Vec<_>>();
        let mut low = f64::INFINITY;
        let mut high = f64::NEG_INFINITY;
        for chain in &relevant {
            for point in chain_points(chain, vertices) {
                let value = cone_station(carrier, point);
                low = low.min(value);
                high = high.max(value);
            }
        }
        // Pointed cones need a degenerate pole edge, which this conservative
        // mixed builder deliberately does not synthesize.
        if !low.is_finite()
            || low * carrier.half_angle.tan() <= tolerance
            || high - low <= tolerance
        {
            return Err((
                region,
                format!("hybrid BREP: cone {region} is pointed or has no finite axial span"),
            ));
        }
        // Keep fitted STL boundary samples away from the NURBS domain ends.
        // The trim loops still define the face; this small carrier extension
        // merely permits exact plane/cone intersections through quantized end
        // vertices without evaluating outside the surface domain.
        let margin = ((high - low) * 1.0e-4).max(tolerance * 2.0);
        low = (low - margin).max(tolerance / carrier.half_angle.tan());
        high += margin;
        let mut seam = relevant.iter().find_map(|chain| {
            let neighbor = if chain.adjacent.0 == region {
                chain.adjacent.1
            } else {
                chain.adjacent.0
            };
            cylinders.get(&neighbor).map(|cylinder| cylinder.x_axis)
        });
        for chain in &relevant {
            if chain.closed {
                continue;
            }
            let points = chain_points(chain, vertices);
            let s0 = cone_station(carrier, points[0]);
            let s1 = cone_station(carrier, *points.last().unwrap());
            let r0 = cone_radial(carrier, points[0]);
            let r1 = cone_radial(carrier, *points.last().unwrap());
            if (s1 - s0).abs() > tolerance
                && r0.cross(r1).length() <= tolerance * r0.length().max(r1.length())
            {
                seam = Some(r0.normalized().map_err(|error| (region, error))?);
                break;
            }
        }
        let x_axis = match seam {
            Some(axis) => axis,
            None => carrier
                .axis
                .perpendicular()
                .map_err(|error| (region, error))?,
        };
        let y_axis = carrier
            .axis
            .cross(x_axis)
            .normalized()
            .map_err(|error| (region, error))?;
        let radius_low = low * carrier.half_angle.tan();
        let radius_high = high * carrier.half_angle.tan();
        let base = carrier.apex.add(carrier.axis.scale(low));
        let profile = make_line(
            base.add(x_axis.scale(radius_low)),
            base.add(carrier.axis.scale(high - low))
                .add(x_axis.scale(radius_high)),
        )
        .map_err(|error| (region, error))?;
        let surface =
            make_revolution(base, carrier.axis, &profile, TAU).map_err(|error| (region, error))?;
        result.insert(
            region,
            ConeInfo {
                carrier,
                base_station: low,
                height: high - low,
                x_axis,
                y_axis,
                surface,
            },
        );
    }
    Ok(result)
}

/// Frame each sphere as a revolution about the common normal of the ring
/// planes that bound it, pointing into the region so a single-ring cap
/// holds the north pole; the seam passes through the first ring's first
/// source vertex.
///
/// A sphere whose rings are NOT all coaxial (a sphere clipped by a box: six
/// flats on three axes) is framed about one ring axis whose two poles both
/// fall in removed caps of coaxial rings, with its seam meridian turned clear
/// of every other ring. Those tilted rings are exact plane/sphere circles
/// whose sphere pcurves are fitted, not isoparametric; see
/// [`tilted_sphere_ring_edge`].
#[allow(clippy::too_many_arguments)]
fn build_sphere_surfaces(
    carriers: &HashMap<u32, SphereCarrier>,
    planes: &HashMap<u32, PlaneInfo>,
    cylinders: &HashMap<u32, CylinderInfo>,
    cones: &HashMap<u32, ConeInfo>,
    chains: &[Chain],
    vertices: &[Vec3],
    region_centroids: &HashMap<u32, Vec3>,
    region_samples: &HashMap<u32, Vec<Vec3>>,
    tolerance: f64,
) -> Result<HashMap<u32, SphereInfo>, (u32, String)> {
    let mut result = HashMap::new();
    for (&region, &carrier) in carriers {
        let relevant = chains
            .iter()
            .filter(|chain| chain.adjacent.0 == region || chain.adjacent.1 == region)
            .collect::<Vec<_>>();
        if relevant.is_empty() || relevant.iter().any(|chain| !chain.closed) {
            return Err((
                region,
                format!("hybrid BREP: sphere {region} is not bounded by closed rings"),
            ));
        }
        let mut axis: Option<Vec3> = None;
        // A ring shared with a coaxial revolve is one edge whose pcurve on
        // each face starts at that face's seam, so the seams must coincide.
        let mut seam: Option<Vec3> = None;
        let angular = tolerance / carrier.radius;
        let mut rings = Vec::with_capacity(relevant.len());
        let mut coaxial = true;
        for chain in &relevant {
            let neighbor = if chain.adjacent.0 == region {
                chain.adjacent.1
            } else {
                chain.adjacent.0
            };
            let (direction, seam_hint) = if let Some(plane) = planes.get(&neighbor) {
                (plane.normal.normalized().map_err(|error| (region, error))?, None)
            } else if let Some(cylinder) = cylinders.get(&neighbor) {
                (cylinder.carrier.axis, Some(cylinder.x_axis))
            } else if let Some(cone) = cones.get(&neighbor) {
                (cone.carrier.axis, Some(cone.x_axis))
            } else {
                return Err((
                    region,
                    format!("hybrid BREP: sphere {region} ring neighbor is not a plane or coaxial revolve"),
                ));
            };
            let points = chain_points(chain, vertices);
            let station = points
                .iter()
                .map(|point| point.sub(carrier.center).dot(direction))
                .sum::<f64>()
                / points.len() as f64;
            rings.push(SphereRing {
                direction,
                station,
                plane: planes.contains_key(&neighbor),
                seam_hint,
                first_point: points[0],
            });
            match axis {
                None => axis = Some(direction),
                Some(current) if current.cross(direction).length() <= angular => {}
                Some(_) => coaxial = false,
            }
        }
        let (mut axis, seam, north_pole_inside) = if coaxial {
            for ring in &rings {
                if let Some(hint) = ring.seam_hint {
                    seam.get_or_insert(hint);
                }
            }
            (axis.unwrap(), seam, relevant.len() == 1)
        } else {
            let samples = region_samples.get(&region).map(Vec::as_slice).unwrap_or(&[]);
            let (axis, seam) = tilted_sphere_frame(carrier, &mut rings, samples, tolerance)
                .map_err(|message| {
                    (
                        region,
                        format!("hybrid BREP: sphere {region} rings are not coaxial and {message}"),
                    )
                })?;
            (axis, Some(seam), false)
        };
        // Point the axis into the region: a cap then surrounds the north
        // pole. A tilted frame has both poles in removed caps, so either
        // sense serves and the one chosen stays.
        if coaxial {
            if let Some(&centroid) = region_centroids.get(&region) {
                if centroid.sub(carrier.center).dot(axis) < 0.0 {
                    axis = axis.scale(-1.0);
                }
            }
        }
        let seam = seam.unwrap_or_else(|| rings[0].first_point.sub(carrier.center));
        let surface = make_sphere_surface_framed(carrier.center, carrier.radius, axis, Some(seam))
            .map_err(|error| (region, error))?;
        let equator = surface.evaluate(0.0, 0.5).map_err(|error| (region, error))?;
        let x_axis = equator
            .sub(carrier.center)
            .normalized()
            .map_err(|error| (region, error))?;
        let y_axis = axis.cross(x_axis).normalized().map_err(|error| (region, error))?;
        result.insert(
            region,
            SphereInfo {
                carrier,
                axis,
                x_axis,
                y_axis,
                surface,
                north_pole_inside,
            },
        );
    }
    Ok(result)
}

/// One closed ring bounding a sphere region: the plane `(p - center) ·
/// direction = station` it lies in, and what the neighbor across it demands.
struct SphereRing {
    direction: Vec3,
    station: f64,
    plane: bool,
    seam_hint: Option<Vec3>,
    first_point: Vec3,
}

/// Polar axis and seam direction for a sphere region bounded by rings on
/// several axes, or why there is none.
///
/// The region must be the sphere less DISJOINT caps, one per ring: every
/// sample of the region lies on the kept side of every ring. The polar axis
/// is a ring axis whose two poles both lie strictly inside removed caps of
/// rings coaxial with it, so those rings stay latitudes and no pole touches
/// the face. Every other ring must be a plane circle, and the seam meridian
/// is turned to clear each such ring's removed cap by a margin, so its
/// fitted pcurve is a closed loop inside the parameter square.
fn tilted_sphere_frame(
    carrier: SphereCarrier,
    rings: &mut [SphereRing],
    samples: &[Vec3],
    tolerance: f64,
) -> Result<(Vec3, Vec3), String> {
    let radius = carrier.radius;
    let angular = tolerance / radius;
    // Orient each ring so `(p - center) · direction >= station` is the KEPT
    // side: the removed cap is then centred on `-direction`.
    for ring in rings.iter_mut() {
        let side = samples
            .iter()
            .map(|sample| sample.sub(carrier.center).dot(ring.direction) - ring.station)
            .max_by(|a, b| a.abs().total_cmp(&b.abs()))
            .ok_or_else(|| "the region has no samples to side its rings".to_owned())?;
        if side < 0.0 {
            ring.direction = ring.direction.scale(-1.0);
            ring.station = -ring.station;
        }
        if ring.station.abs() >= radius - tolerance {
            return Err("a ring is degenerate".to_owned());
        }
    }
    let slack = tolerance * 4.0;
    if samples.iter().any(|sample| {
        rings
            .iter()
            .any(|ring| sample.sub(carrier.center).dot(ring.direction) < ring.station - slack)
    }) {
        return Err("the region is not the sphere less one cap per ring".to_owned());
    }
    // Removed caps must be disjoint: the angle between two cap centres must
    // exceed the sum of their angular radii. A cap centred on `-direction`
    // holds the points with `(p - center) · -direction > -station`.
    let cap_radius = |ring: &SphereRing| (-ring.station / radius).clamp(-1.0, 1.0).acos();
    for (index, first) in rings.iter().enumerate() {
        for second in &rings[index + 1..] {
            let between = first.direction.dot(second.direction).clamp(-1.0, 1.0).acos();
            if between <= cap_radius(first) + cap_radius(second) + angular {
                return Err("two removed caps touch".to_owned());
            }
        }
    }
    // Strictly inside a removed cap, clear of its rim.
    let in_removed_cap = |ring: &SphereRing, pole: Vec3| {
        pole.dot(ring.direction) * radius < ring.station - slack
    };
    for candidate in rings.iter() {
        let axis = candidate.direction;
        let parallel = |ring: &SphereRing| axis.cross(ring.direction).length() <= angular;
        let north_capped = rings
            .iter()
            .any(|ring| parallel(ring) && in_removed_cap(ring, axis));
        let south_capped = rings
            .iter()
            .any(|ring| parallel(ring) && in_removed_cap(ring, axis.scale(-1.0)));
        if !north_capped || !south_capped {
            continue;
        }
        let tilted = rings.iter().filter(|ring| !parallel(ring)).collect::<Vec<_>>();
        if tilted.iter().any(|ring| !ring.plane) {
            continue;
        }
        let Ok(x0) = axis.perpendicular() else {
            continue;
        };
        let Ok(y0) = axis.cross(x0).normalized() else {
            continue;
        };
        // How far the meridian at azimuth `phi` stays outside every tilted
        // cap, as an angle. The meridian's closest approach to a direction
        // `w` has cosine `max(hypot(a, b), |b|)` for `a` the component of
        // `w` along the meridian's equator direction and `b` along the axis.
        let clearance = |phi: f64| {
            let toward = x0.scale(phi.cos()).add(y0.scale(phi.sin()));
            tilted
                .iter()
                .map(|ring| {
                    let cap = ring.direction.scale(-1.0);
                    let a = cap.dot(toward);
                    let b = cap.dot(axis);
                    let closest = if a >= 0.0 { a.hypot(b) } else { b.abs() };
                    closest.clamp(-1.0, 1.0).acos() - cap_radius(ring)
                })
                .fold(f64::INFINITY, f64::min)
        };
        let forced = rings
            .iter()
            .filter(|ring| parallel(ring))
            .find_map(|ring| ring.seam_hint);
        let phi = match forced {
            Some(hint) => hint.dot(y0).atan2(hint.dot(x0)),
            None => (0..720)
                .map(|step| step as f64 * TAU / 720.0)
                .max_by(|a, b| clearance(*a).total_cmp(&clearance(*b)))
                .unwrap(),
        };
        if clearance(phi) * radius <= 16.0 * tolerance {
            continue;
        }
        return Ok((axis, x0.scale(phi.cos()).add(y0.scale(phi.sin()))));
    }
    Err("no ring axis has both poles in removed coaxial caps with a seam clear of the other rings".to_owned())
}

fn sphere_station(info: &SphereInfo, point: Vec3) -> f64 {
    point.sub(info.carrier.center).dot(info.axis)
}

fn sphere_coordinates(info: &SphereInfo, point: Vec3) -> [f64; 2] {
    let d = point.sub(info.carrier.center);
    let angle = d.dot(info.y_axis).atan2(d.dot(info.x_axis)).rem_euclid(TAU);
    let u = circle_angle_to_parameter(4, TAU, angle);
    // The meridian is a half circle from the south pole: two rational
    // quadratic spans over the polar angle.
    let polar = (sphere_station(info, point) / info.carrier.radius)
        .clamp(-1.0, 1.0)
        .asin();
    let v = circle_angle_to_parameter(2, std::f64::consts::PI, polar + std::f64::consts::FRAC_PI_2);
    [u, v]
}

fn max_line_deviation(points: &[Vec3]) -> f64 {
    if points.len() < 3 {
        return 0.0;
    }
    let start = points[0];
    let chord = points[points.len() - 1].sub(start);
    let denominator = chord.dot(chord);
    points[1..points.len() - 1]
        .iter()
        .map(|point| {
            let delta = point.sub(start);
            let t = if denominator > 0.0 {
                (delta.dot(chord) / denominator).clamp(0.0, 1.0)
            } else {
                0.0
            };
            delta.sub(chord.scale(t)).length()
        })
        .fold(0.0, f64::max)
}

fn trim_curve(curve: &NurbsCurve, start: f64, end: f64) -> Result<NurbsCurve, String> {
    let domain = curve.domain()?;
    // `split` refuses a parameter within the kernel's knot identity
    // tolerance of either end, so a trim that close to an end is the whole
    // curve: a station a fraction of a nanometre above a region's own
    // minimum (the fitted axis is not exactly a coordinate direction, so
    // two vertices on one rim differ in the last bits) is that region's
    // base, not a cut.
    let mut trimmed = if end < domain[1] - KNOT_IDENTITY_TOL {
        curve.split(end)?.0
    } else {
        curve.clone()
    };
    if start > domain[0] + KNOT_IDENTITY_TOL {
        trimmed = trimmed.split(start)?.1;
    }
    Ok(trimmed)
}

fn cylinder_coordinates(info: &CylinderInfo, point: Vec3) -> [f64; 2] {
    let r = radial(info.carrier, point);
    let angle = r.dot(info.y_axis).atan2(r.dot(info.x_axis)).rem_euclid(TAU);
    // A rational quadratic circle is not angle-linear inside a knot span.
    // Use the public exact angle-to-NURBS mapping used by the kernel carrier.
    let u = circle_angle_to_parameter(4, TAU, angle);
    let v = (station(info.carrier, point) - info.base_station) / info.height;
    [u, v]
}

fn cone_coordinates(info: &ConeInfo, point: Vec3) -> [f64; 2] {
    let r = cone_radial(info.carrier, point);
    let angle = r.dot(info.y_axis).atan2(r.dot(info.x_axis)).rem_euclid(TAU);
    let u = circle_angle_to_parameter(4, TAU, angle);
    let v = (cone_station(info.carrier, point) - info.base_station) / info.height;
    [u, v]
}

/// The exact circle where a plane cuts a sphere, as the edge of a closed
/// chain that is NOT a latitude of the sphere's frame, with its fitted pcurve
/// on the sphere (oriented along the curve).
///
/// The circle starts at the source ring's first vertex, so the edge's one
/// vertex is where the chain begins. The pcurve is checked against the
/// circle at the exact-topology bar; the frame's seam was turned clear of
/// this ring, so the pcurve is a closed loop that never wraps.
fn tilted_sphere_ring_edge(
    chain_index: usize,
    sphere: &SphereInfo,
    plane: PlaneInfo,
    points: &[Vec3],
    tolerance: f64,
) -> Result<(NurbsCurve, f64, f64, bool, NurbsCurve), String> {
    let normal = plane.normal.normalized()?;
    let center = sphere.carrier.center;
    let offset = plane.origin.sub(center).dot(normal);
    let radius_sq = sphere.carrier.radius * sphere.carrier.radius - offset * offset;
    if radius_sq <= tolerance * tolerance {
        return Err(format!(
            "hybrid BREP: tilted sphere ring {chain_index} misses its sphere"
        ));
    }
    let ring_radius = radius_sq.sqrt();
    let ring_center = center.add(normal.scale(offset));
    let radial_bar = accepted_radial_bar(tolerance, sphere.carrier.deviation);
    for &point in points {
        let delta = point.sub(ring_center);
        let height = delta.dot(normal);
        let in_plane = delta.sub(normal.scale(height)).length();
        if height.abs() > tolerance || (in_plane - ring_radius).abs() > radial_bar {
            return Err(format!(
                "hybrid BREP: tilted sphere ring {chain_index} source leaves its circle (height {height:.3e}, radial {:.3e}, bar {radial_bar:.3e})",
                in_plane - ring_radius
            ));
        }
    }
    let first = points[0].sub(ring_center);
    let x = first.sub(normal.scale(first.dot(normal))).normalized()?;
    let y = normal.cross(x).normalized()?;
    // The chain winds one way about the normal; the circle runs the same way
    // when its second point turns positively.
    let angles = points
        .iter()
        .chain(std::iter::once(&points[0]))
        .map(|point| {
            let delta = point.sub(ring_center);
            delta.dot(y).atan2(delta.dot(x)) / TAU
        })
        .collect::<Vec<_>>();
    let mut unwrapped = angles.clone();
    unwrap(&mut unwrapped);
    let sweep = unwrapped.last().unwrap() - unwrapped[0];
    if (sweep.abs() - 1.0).abs() > 0.02 || !monotone_parameter_walk(&unwrapped, 1.0e-5) {
        return Err(format!(
            "hybrid BREP: tilted sphere ring {chain_index} does not wind once (sweep {sweep:.6})"
        ));
    }
    let curve = make_arc(ring_center, x, y, ring_radius, 0.0, TAU)?;
    let domain = curve.domain()?;
    let pcurve = build_pcurve_on_surface_range(
        &sphere.surface,
        &curve,
        domain[0],
        domain[1],
        true,
        (tolerance * 0.05).max(1.0e-9),
    )?;
    let pcurve_domain = pcurve.domain()?;
    let mut previous_u: Option<f64> = None;
    for sample in 0..=64 {
        let fraction = sample as f64 / 64.0;
        let t = domain[0] + (domain[1] - domain[0]) * fraction;
        let q = pcurve_domain[0] + (pcurve_domain[1] - pcurve_domain[0]) * fraction;
        let uv = pcurve.evaluate(q)?;
        let surface_point = sphere.surface.evaluate(uv.x, uv.y)?;
        if curve.evaluate(t)?.sub(surface_point).length() > tolerance * 0.1
            || previous_u.is_some_and(|u| (uv.x - u).abs() > 0.25)
        {
            return Err(format!(
                "hybrid BREP: tilted sphere ring {chain_index} pcurve failed agreement"
            ));
        }
        previous_u = Some(uv.x);
    }
    let start = pcurve.evaluate(pcurve_domain[0])?;
    let end = pcurve.evaluate(pcurve_domain[1])?;
    if (start.x - end.x).abs() > 1.0e-9 || (start.y - end.y).abs() > 1.0e-9 {
        return Err(format!(
            "hybrid BREP: tilted sphere ring {chain_index} pcurve does not close"
        ));
    }
    Ok((curve, domain[0], domain[1], sweep > 0.0, pcurve))
}

/// The exact ellipse arc two crossing cylinders of one radius share, with
/// its fitted pcurve on each (oriented along the curve).
///
/// In its section plane through the axes' crossing point the ellipse has
/// the common radius as its semi-minor axis, along the common normal of the
/// axes, and `r / sqrt(1 - (e · axis)^2)` along the in-plane direction `e`
/// normal to that, the same for both walls. Both pcurves are checked against
/// the arc, and each must stay inside its wall's parameter square without
/// wrapping: the wall's seam was put at the end of one of these arcs.
#[allow(clippy::type_complexity)]
fn cylinder_pair_section_edge(
    chain_index: usize,
    walls: [(u32, &CylinderInfo); 2],
    points: &[Vec3],
    tolerance: f64,
) -> Result<(NurbsCurve, f64, f64, bool, Vec<(u32, NurbsCurve)>), String> {
    let [(_, first), (_, second)] = walls;
    let (center, normal) =
        cylinder_pair_section_normal(first.carrier, second.carrier, points, tolerance)
            .ok_or_else(|| {
                format!(
                    "hybrid BREP: cylinder/cylinder chain {chain_index} is not an equal-radius crossing section"
                )
            })?;
    let radius = 0.5 * (first.carrier.radius + second.carrier.radius);
    let minor = first.carrier.axis.cross(second.carrier.axis).normalized()?;
    let major = normal.cross(minor).normalized()?;
    let along = major.dot(first.carrier.axis.normalized()?);
    let major_radius = radius / (1.0 - along * along).sqrt();
    let mut angles = points
        .iter()
        .map(|point| {
            let delta = point.sub(center);
            (delta.dot(minor) / radius).atan2(delta.dot(major) / major_radius)
        })
        .collect::<Vec<_>>();
    for index in 1..angles.len() {
        while angles[index] - angles[index - 1] > std::f64::consts::PI {
            angles[index] -= TAU;
        }
        while angles[index] - angles[index - 1] < -std::f64::consts::PI {
            angles[index] += TAU;
        }
    }
    if !monotone_parameter_walk(&angles, 1.0e-5) {
        return Err(format!(
            "hybrid BREP: cylinder/cylinder chain {chain_index} backtracks along its ellipse"
        ));
    }
    let (a0, a1) = (angles[0], *angles.last().unwrap());
    let (low, high) = (a0.min(a1), a0.max(a1));
    if high - low >= TAU - 1.0e-6 {
        return Err(format!(
            "hybrid BREP: cylinder/cylinder chain {chain_index} spans its whole ellipse"
        ));
    }
    // A unit arc in the xy plane, mapped affinely onto the ellipse: exact.
    let unit = make_arc(
        Vec3::new(0.0, 0.0, 0.0),
        Vec3::new(1.0, 0.0, 0.0),
        Vec3::new(0.0, 1.0, 0.0),
        1.0,
        low,
        high,
    )?;
    let controls = unit
        .control_points
        .iter()
        .map(|control| {
            let point = center
                .add(major.scale(major_radius * control.x / control.w))
                .add(minor.scale(radius * control.y / control.w));
            Vec4::from_point(point, control.w)
        })
        .collect::<Vec<_>>();
    let curve = NurbsCurve::new(unit.degree, unit.knots.clone(), controls)?;
    let domain = curve.domain()?;
    let bar = accepted_radial_bar(
        tolerance,
        first.carrier.deviation.max(second.carrier.deviation),
    );
    for &point in points {
        let distance = project_point_to_curve(&curve, point)?.distance;
        if distance > bar {
            return Err(format!(
                "hybrid BREP: cylinder/cylinder chain {chain_index} source leaves its ellipse by {distance:.3e} (bar {bar:.3e})"
            ));
        }
    }
    let mut pcurves = Vec::with_capacity(2);
    for (owner, wall) in walls {
        let pcurve = build_pcurve_on_surface_range(
            &wall.surface,
            &curve,
            domain[0],
            domain[1],
            true,
            (tolerance * 0.05).max(1.0e-9),
        )?;
        let pcurve_domain = pcurve.domain()?;
        let mut previous_u: Option<f64> = None;
        for sample in 0..=32 {
            let fraction = sample as f64 / 32.0;
            let t = domain[0] + (domain[1] - domain[0]) * fraction;
            let q = pcurve_domain[0] + (pcurve_domain[1] - pcurve_domain[0]) * fraction;
            let uv = pcurve.evaluate(q)?;
            let surface_point = wall.surface.evaluate(uv.x, uv.y)?;
            let miss = curve.evaluate(t)?.sub(surface_point).length();
            if miss > bar || previous_u.is_some_and(|u| (uv.x - u).abs() > 0.25) {
                return Err(format!(
                    "hybrid BREP: cylinder/cylinder chain {chain_index} pcurve on region {owner} failed agreement (miss {miss:.3e}, bar {bar:.3e})"
                ));
            }
            previous_u = Some(uv.x);
        }
        pcurves.push((owner, pcurve));
    }
    Ok((curve, domain[0], domain[1], a1 > a0, pcurves))
}

fn exact_local_cone_plane_conic(
    info: &ConeInfo,
    plane: PlaneInfo,
    points: &[Vec3],
    tolerance: f64,
) -> Result<NurbsCurve, String> {
    let mut angles = points
        .iter()
        .map(|&point| {
            let radial = cone_radial(info.carrier, point);
            radial.dot(info.y_axis).atan2(radial.dot(info.x_axis))
        })
        .collect::<Vec<_>>();
    for index in 1..angles.len() {
        while angles[index] - angles[index - 1] > std::f64::consts::PI {
            angles[index] -= TAU;
        }
        while angles[index] - angles[index - 1] < -std::f64::consts::PI {
            angles[index] += TAU;
        }
    }
    if !monotone_parameter_walk(&angles, 1.0e-5) {
        return Err("hybrid BREP: cone/plane source angles are not monotone".into());
    }
    let low = angles[0].min(*angles.last().unwrap());
    let high = angles[0].max(*angles.last().unwrap());
    if high - low <= 1.0e-10 || high - low > TAU + 1.0e-10 {
        return Err("hybrid BREP: cone/plane source angle span is invalid".into());
    }

    // Map a finite exact reference-circle arc projectively through the cone
    // apex onto the cutting plane. Unlike mapping a full circle, this keeps
    // hyperbolic/parabolic branches one-signed over the actual trim interval.
    let reference_station = info.base_station + 0.5 * info.height;
    let reference_radius = reference_station * info.carrier.half_angle.tan();
    let center = info
        .carrier
        .apex
        .add(info.carrier.axis.scale(reference_station));
    let circle = make_arc(
        center,
        info.x_axis,
        info.y_axis,
        reference_radius,
        low,
        high,
    )?;
    let normal = plane.normal.normalized()?;
    let apex = info.carrier.apex;
    let scale = plane.origin.sub(apex).dot(normal);
    if scale.abs() <= tolerance {
        return Err("hybrid BREP: cone/plane section passes through the apex".into());
    }
    let mut controls = circle
        .control_points
        .iter()
        .map(|point| {
            let relative = Vec3::new(
                point.x - point.w * apex.x,
                point.y - point.w * apex.y,
                point.z - point.w * apex.z,
            );
            let weight = relative.dot(normal);
            let scaled = relative.scale(scale);
            Vec4 {
                x: apex.x * weight + scaled.x,
                y: apex.y * weight + scaled.y,
                z: apex.z * weight + scaled.z,
                w: weight,
            }
        })
        .collect::<Vec<_>>();
    if controls.iter().any(|point| point.w.abs() <= 1.0e-12) {
        return Err("hybrid BREP: cone/plane local conic crosses an asymptote".into());
    }
    let sign = controls[0].w.signum();
    if controls.iter().any(|point| point.w.signum() != sign) {
        return Err("hybrid BREP: cone/plane local conic has mixed weight signs".into());
    }
    if sign < 0.0 {
        for point in &mut controls {
            point.x = -point.x;
            point.y = -point.y;
            point.z = -point.z;
            point.w = -point.w;
        }
    }
    NurbsCurve::new(circle.degree, circle.knots, controls)
}

fn unwrap(values: &mut [f64]) {
    for index in 1..values.len() {
        while values[index] - values[index - 1] > 0.5 {
            values[index] -= 1.0;
        }
        while values[index] - values[index - 1] < -0.5 {
            values[index] += 1.0;
        }
    }
}

fn monotone_parameter_walk(values: &[f64], tolerance: f64) -> bool {
    let net = values.last().unwrap() - values[0];
    if net.abs() <= tolerance {
        return false;
    }
    let sign = net.signum();
    let variation = values
        .windows(2)
        .map(|pair| pair[1] - pair[0])
        .try_fold(0.0, |sum, step| {
            (step * sign >= -tolerance).then_some(sum + step.abs())
        });
    variation.is_some_and(|total| (total - net.abs()).abs() <= tolerance * values.len() as f64)
}

fn exact_arena_vertex(
    mesh_vertex: usize,
    exact_point: Vec3,
    tolerance: f64,
    arena: &mut TopologyArena,
    arena_vertices: &mut HashMap<usize, brep_kernel::VertexId>,
    ids: &mut Ids,
) -> Result<brep_kernel::VertexId, String> {
    if let Some(&vertex) = arena_vertices.get(&mesh_vertex) {
        let existing = arena
            .vertices
            .get(vertex)
            .ok_or_else(|| "hybrid BREP: reused arena vertex disappeared".to_owned())?;
        let disagreement = existing.point.sub(exact_point).length();
        if !disagreement.is_finite() || disagreement > tolerance {
            return Err(format!(
                "hybrid BREP: exact curves disagree by {disagreement:.6e} (bar {tolerance:.6e}) at shared mesh vertex {mesh_vertex}: ({:.6},{:.6},{:.6}) vs ({:.6},{:.6},{:.6})",
                existing.point.x,
                existing.point.y,
                existing.point.z,
                exact_point.x,
                exact_point.y,
                exact_point.z,
            ));
        }
        return Ok(vertex);
    }
    let vertex = arena.vertices.insert(ArenaVertex {
        wire_id: ids.next(),
        point: exact_point,
    });
    arena_vertices.insert(mesh_vertex, vertex);
    Ok(vertex)
}

#[derive(Clone, Copy)]
enum RevolveInfoRef<'a> {
    Cylinder(&'a CylinderInfo),
    Cone(&'a ConeInfo),
    Sphere(&'a SphereInfo),
}

impl<'a> RevolveInfoRef<'a> {
    fn surface(self) -> &'a NurbsSurface {
        match self {
            Self::Cylinder(info) => &info.surface,
            Self::Cone(info) => &info.surface,
            Self::Sphere(info) => &info.surface,
        }
    }

    fn coordinates(self, point: Vec3) -> [f64; 2] {
        match self {
            Self::Cylinder(info) => cylinder_coordinates(info, point),
            Self::Cone(info) => cone_coordinates(info, point),
            Self::Sphere(info) => sphere_coordinates(info, point),
        }
    }

    fn station(self, point: Vec3) -> f64 {
        match self {
            Self::Cylinder(info) => station(info.carrier, point),
            Self::Cone(info) => cone_station(info.carrier, point),
            Self::Sphere(info) => sphere_station(info, point),
        }
    }

    /// The exact topology tolerance expressed along the angular parameter:
    /// an arc that starts at the seam ruling reaches it through a second
    /// chain whose fitted radial direction differs by the carrier noise.
    fn seam_tolerance(self, tolerance: f64) -> f64 {
        match self {
            Self::Cylinder(info) => (tolerance / (TAU * info.carrier.radius)).max(1.0e-8),
            Self::Cone(_) => 1.0e-8,
            Self::Sphere(info) => (tolerance / (TAU * info.carrier.radius)).max(1.0e-8),
        }
    }

    /// Radial bar for a source vertex on a ring or arc chain: the exact
    /// topology tolerance, or the deviation the segmenter already accepted
    /// for this carrier when that is wider. A vertex the tessellator left
    /// on a rim chord is still on the ring the exact circle replaces.
    fn radial_bar(self, tolerance: f64) -> f64 {
        match self {
            Self::Cylinder(info) => accepted_radial_bar(tolerance, info.carrier.deviation),
            Self::Cone(info) => accepted_radial_bar(tolerance, info.carrier.deviation),
            Self::Sphere(info) => accepted_radial_bar(tolerance, info.carrier.deviation),
        }
    }

    fn radial_error(self, point: Vec3) -> f64 {
        match self {
            Self::Cylinder(info) => {
                (radial(info.carrier, point).length() - info.carrier.radius).abs()
            }
            Self::Cone(info) => {
                let s = cone_station(info.carrier, point);
                (cone_radial(info.carrier, point).length() - s * info.carrier.half_angle.tan())
                    .abs()
                    * info.carrier.half_angle.cos()
            }
            Self::Sphere(info) => sphere_radial_error(info.carrier, point),
        }
    }
}

fn revolve_info<'a>(
    region: u32,
    cylinders: &'a HashMap<u32, CylinderInfo>,
    cones: &'a HashMap<u32, ConeInfo>,
    spheres: &'a HashMap<u32, SphereInfo>,
) -> Option<RevolveInfoRef<'a>> {
    cylinders
        .get(&region)
        .map(RevolveInfoRef::Cylinder)
        .or_else(|| cones.get(&region).map(RevolveInfoRef::Cone))
        .or_else(|| spheres.get(&region).map(RevolveInfoRef::Sphere))
}

#[allow(clippy::too_many_arguments)]
fn build_chain_edge(
    chain_index: usize,
    chain: &Chain,
    vertices: &[Vec3],
    planes: &HashMap<u32, PlaneInfo>,
    cylinders: &HashMap<u32, CylinderInfo>,
    cones: &HashMap<u32, ConeInfo>,
    spheres: &HashMap<u32, SphereInfo>,
    tolerance: f64,
    arena: &mut TopologyArena,
    arena_vertices: &mut HashMap<usize, brep_kernel::VertexId>,
    ids: &mut Ids,
) -> Result<BuiltEdge, String> {
    let points = chain_points(chain, vertices);
    let mut custom_revolve_pcurves = HashMap::new();
    let revolve_ids = [chain.adjacent.0, chain.adjacent.1]
        .into_iter()
        .filter(|id| {
            cylinders.contains_key(id) || cones.contains_key(id) || spheres.contains_key(id)
        })
        .collect::<Vec<_>>();
    if revolve_ids.len() > 2 {
        return Err(format!(
            "hybrid BREP: chain {chain_index} has too many revolved neighbors"
        ));
    }
    let (curve, t0, t1, curve_along_chain, primary_uv) = if let Some(&region) = revolve_ids.first()
    {
        let info = revolve_info(region, cylinders, cones, spheres).unwrap();
        let stations = points.iter().map(|&p| info.station(p)).collect::<Vec<_>>();
        let station_spread = stations.iter().copied().fold(f64::NEG_INFINITY, f64::max)
            - stations.iter().copied().fold(f64::INFINITY, f64::min);
        let radius_error = points
            .iter()
            .map(|&point| info.radial_error(point))
            .fold(0.0, f64::max);
        let mut coordinates = points
            .iter()
            .map(|&p| info.coordinates(p))
            .collect::<Vec<_>>();
        let mut us = coordinates.iter().map(|uv| uv[0]).collect::<Vec<_>>();
        unwrap(&mut us);
        for (uv, u) in coordinates.iter_mut().zip(&us) {
            uv[0] = *u;
        }
        let tilted_plane = match info {
            RevolveInfoRef::Sphere(sphere) if chain.closed && station_spread > tolerance => {
                [chain.adjacent.0, chain.adjacent.1]
                    .into_iter()
                    .find_map(|neighbor| planes.get(&neighbor).map(|plane| (sphere, *plane)))
            }
            _ => None,
        };
        if let Some((sphere, plane)) = tilted_plane {
            let (curve, t0, t1, curve_along_chain, pcurve) =
                tilted_sphere_ring_edge(chain_index, sphere, plane, &points, tolerance)?;
            let domain = pcurve.domain()?;
            let start = pcurve.evaluate(domain[0])?;
            let end = pcurve.evaluate(domain[1])?;
            custom_revolve_pcurves.insert(region, pcurve);
            (
                curve,
                t0,
                t1,
                curve_along_chain,
                Some(ChainUv {
                    region,
                    start: [start.x, start.y],
                    end: [end.x, end.y],
                }),
            )
        } else if chain.closed {
            let first = points[0];
            let mut ring_points = points.clone();
            ring_points.push(first);
            let mut ring_us = ring_points
                .iter()
                .map(|&p| info.coordinates(p)[0])
                .collect::<Vec<_>>();
            unwrap(&mut ring_us);
            let sweep = ring_us[ring_us.len() - 1] - ring_us[0];
            if station_spread > tolerance
                || radius_error > info.radial_bar(tolerance)
                || (sweep.abs() - 1.0).abs() > 0.02
                || !monotone_parameter_walk(&ring_us, 1.0e-5)
            {
                return Err(format!(
                    "hybrid BREP: closed chain {chain_index} (region {region}, adjacent {:?}) is not a revolve ring: station spread {station_spread:.3e}, radial error {radius_error:.3e} (bar {:.3e}), sweep {sweep:.6}, monotone {}",
                    chain.adjacent,
                    info.radial_bar(tolerance),
                    monotone_parameter_walk(&ring_us, 1.0e-5)
                ));
            }
            let v = coordinates.iter().map(|uv| uv[1]).sum::<f64>() / coordinates.len() as f64;
            let curve = info.surface().iso_curve_v(v)?;
            (
                curve,
                0.0,
                1.0,
                sweep > 0.0,
                Some(ChainUv {
                    region,
                    start: [0.0, v],
                    end: [if sweep > 0.0 { 1.0 } else { -1.0 }, v],
                }),
            )
        } else if station_spread <= tolerance && radius_error <= info.radial_bar(tolerance) {
            if !monotone_parameter_walk(&us, 1.0e-5) {
                return Err(format!(
                    "hybrid BREP: cylinder arc chain {chain_index} backtracks"
                ));
            }
            let mut a = us[0];
            let mut b = *us.last().unwrap();
            let seam_tolerance = info.seam_tolerance(tolerance);
            let shift = (-2..=2)
                .map(f64::from)
                .find(|shift| {
                    a + shift >= -seam_tolerance
                        && b + shift >= -seam_tolerance
                        && a + shift <= 1.0 + seam_tolerance
                        && b + shift <= 1.0 + seam_tolerance
                })
                .ok_or_else(|| {
                    format!(
                        "hybrid BREP: cylinder arc chain {chain_index} (region {region}, adjacent {:?}) crosses the chosen seam: u {a:.6} -> {b:.6} over {} points",
                        chain.adjacent,
                        points.len()
                    )
                })?;
            a += shift;
            b += shift;
            if a.abs() <= seam_tolerance {
                a = 0.0;
            } else if (a - 1.0).abs() <= seam_tolerance {
                a = 1.0;
            }
            if b.abs() <= seam_tolerance {
                b = 0.0;
            } else if (b - 1.0).abs() <= seam_tolerance {
                b = 1.0;
            }
            if (b - a).abs() <= 1.0e-9 || (b - a).abs() >= 1.0 - 1.0e-9 {
                return Err(format!(
                    "hybrid BREP: cylinder arc chain {chain_index} has invalid sweep"
                ));
            }
            let v = coordinates.iter().map(|uv| uv[1]).sum::<f64>() / coordinates.len() as f64;
            let low = a.min(b);
            let high = a.max(b);
            let curve = trim_curve(&info.surface().iso_curve_v(v)?, low, high)?;
            let domain = curve.domain()?;
            (
                curve,
                domain[0],
                domain[1],
                b > a,
                Some(ChainUv {
                    region,
                    start: [a, v],
                    end: [b, v],
                }),
            )
        } else if !matches!(info, RevolveInfoRef::Sphere(_))
            && (max_line_deviation(&points) <= tolerance
                || us.iter().copied().fold(f64::NEG_INFINITY, f64::max)
                    - us.iter().copied().fold(f64::INFINITY, f64::min)
                    <= 1.0e-4)
        {
            let a = coordinates[0];
            let mut b = *coordinates.last().unwrap();
            while b[0] - a[0] > 0.5 {
                b[0] -= 1.0;
            }
            while b[0] - a[0] < -0.5 {
                b[0] += 1.0;
            }
            if (b[0] - a[0]).abs() > 1.0e-4 {
                return Err(format!(
                    "hybrid BREP: chain {chain_index} is not an axial ruling"
                ));
            }
            let u = (a[0] + b[0]) * 0.5;
            let surface = info.surface();
            // An isoparametric ruling sits at the mean of its endpoint
            // angles, which moves each endpoint tangentially by half their
            // angular spread.  The arc chains meeting this ruling place
            // those corner vertices at their own angle, and
            // `exact_arena_vertex` compares the two placements at the
            // exact-topology bar; a tessellation whose ruling endpoints
            // differ by a fraction of a degree would lose its whole
            // cylinder to that disagreement.  Half the bar leaves the rest
            // of it to the offsets the arc chain itself carries (its mean
            // station, its seam snap).
            let parametric_offset = [a, b]
                .into_iter()
                .map(|uv| {
                    Ok(surface
                        .evaluate(u.rem_euclid(1.0), uv[1])?
                        .sub(surface.evaluate(uv[0].rem_euclid(1.0), uv[1])?)
                        .length())
                })
                .collect::<Result<Vec<f64>, String>>()?
                .into_iter()
                .fold(0.0, f64::max);
            if parametric_offset > tolerance * 0.5 {
                // Build the segment between the endpoints' own positions on
                // the carrier instead.  It is the chord of a helix spanning
                // at most 1.0e-4 of a turn, so it leaves the surface by
                // radius * (pi * 1.0e-4)^2 / 2 -- under a nanometre at any
                // scale a mesh import carries -- while both corners stay
                // exactly where the arcs that share them put them.
                if max_line_deviation(&points) > tolerance {
                    return Err(format!(
                        "hybrid BREP: chain {chain_index} is neither an axial ruling nor straight"
                    ));
                }
                (
                    make_line(
                        surface.evaluate(a[0].rem_euclid(1.0), a[1])?,
                        surface.evaluate(b[0].rem_euclid(1.0), b[1])?,
                    )?,
                    0.0,
                    1.0,
                    true,
                    Some(ChainUv {
                        region,
                        start: a,
                        end: b,
                    }),
                )
            } else {
                let low = a[1].min(b[1]);
                let high = a[1].max(b[1]);
                let curve = trim_curve(&surface.iso_curve_u(u.rem_euclid(1.0))?, low, high)?;
                let domain = curve.domain()?;
                (
                    curve,
                    domain[0],
                    domain[1],
                    b[1] > a[1],
                    Some(ChainUv {
                        region,
                        start: [u, a[1]],
                        end: [u, b[1]],
                    }),
                )
            }
        } else if let (Some(cone), Some(plane)) = (
            cones.get(&region),
            [chain.adjacent.0, chain.adjacent.1]
                .into_iter()
                .find_map(|neighbor| planes.get(&neighbor)),
        ) {
            let normal = plane.normal.normalized()?;
            let x = normal.perpendicular()?;
            let y = normal.cross(x).normalized()?;
            let extent = points
                .iter()
                .map(|point| point.sub(plane.origin).length())
                .fold(1.0_f64, f64::max)
                * 4.0;
            let plane_surface = brep_kernel::make_plane(
                plane.origin.sub(x.scale(extent)).sub(y.scale(extent)),
                x,
                y,
                extent * 2.0,
                extent * 2.0,
            )?;
            let branches = intersect_analytic_pair(&plane_surface, &cone.surface, tolerance)
                .filter(|branches| !branches.is_empty())
                .unwrap_or_else(|| {
                    exact_local_cone_plane_conic(cone, *plane, &points, tolerance)
                        .into_iter()
                        .collect()
                });
            let mut candidates = branches
                .into_iter()
                .map(|curve| {
                    let projections = points
                        .iter()
                        .map(|&point| project_point_to_curve(&curve, point))
                        .collect::<Result<Vec<_>, String>>()?;
                    let score: f64 = projections
                        .iter()
                        .map(|projection| projection.distance)
                        .sum();
                    Ok((score, curve, projections))
                })
                .collect::<Result<Vec<_>, String>>()?;
            candidates.sort_by(|a, b| a.0.total_cmp(&b.0));
            let (_, curve, projections) = candidates.into_iter().next().ok_or_else(|| {
                format!("hybrid BREP: exact cone/plane conic is empty for chain {chain_index}")
            })?;
            let source_error = projections
                .iter()
                .map(|projection| projection.distance)
                .fold(0.0_f64, f64::max);
            if source_error > tolerance * 2.0 {
                return Err(format!(
                    "hybrid BREP: exact cone/plane conic chain {chain_index} misses source by {source_error:.6e}"
                ));
            }
            let p0 = projections[0];
            let p1 = *projections.last().unwrap();
            let low = p0.u.min(p1.u);
            let high = p0.u.max(p1.u);
            let direction = (p1.u - p0.u).signum();
            let monotone = direction != 0.0
                && projections.windows(2).all(|pair| {
                    (pair[1].u - pair[0].u) * direction >= -1.0e-8
                        && pair[1].u >= low - 1.0e-8
                        && pair[1].u <= high + 1.0e-8
                });
            if !monotone || high - low <= 1.0e-10 {
                return Err(format!(
                    "hybrid BREP: exact cone/plane conic chain {chain_index} crosses its branch seam"
                ));
            }
            let pcurve = build_pcurve_on_surface_range(
                &cone.surface,
                &curve,
                low,
                high,
                true,
                (tolerance * 0.05).max(1.0e-9),
            )?;
            let pcurve_domain = pcurve.domain()?;
            for sample in 0..=16 {
                let fraction = sample as f64 / 16.0;
                let t = low + (high - low) * fraction;
                let q = pcurve_domain[0] + (pcurve_domain[1] - pcurve_domain[0]) * fraction;
                let edge_point = curve.evaluate(t)?;
                let uv = pcurve.evaluate(q)?;
                let surface_point = cone.surface.evaluate(uv.x, uv.y)?;
                if edge_point.sub(surface_point).length() > tolerance * 0.1
                    || edge_point.sub(plane.origin).dot(normal).abs() > tolerance * 0.1
                {
                    return Err(format!(
                        "hybrid BREP: cone/plane conic chain {chain_index} pcurve failed agreement"
                    ));
                }
            }
            let edge_uv_start = pcurve.evaluate(pcurve_domain[0])?;
            let edge_uv_end = pcurve.evaluate(pcurve_domain[1])?;
            let curve_along_chain = p1.u > p0.u;
            let (chain_uv_start, chain_uv_end) = if curve_along_chain {
                (edge_uv_start, edge_uv_end)
            } else {
                (edge_uv_end, edge_uv_start)
            };
            custom_revolve_pcurves.insert(region, pcurve);
            (
                curve,
                low,
                high,
                curve_along_chain,
                Some(ChainUv {
                    region,
                    start: [chain_uv_start.x, chain_uv_start.y],
                    end: [chain_uv_end.x, chain_uv_end.y],
                }),
            )
        } else if let (Some(first), Some(second)) = (
            cylinders.get(&chain.adjacent.0).filter(|_| !chain.closed),
            cylinders.get(&chain.adjacent.1),
        ) {
            let (curve, t0, t1, curve_along_chain, pcurves) = cylinder_pair_section_edge(
                chain_index,
                [(chain.adjacent.0, first), (chain.adjacent.1, second)],
                &points,
                tolerance,
            )?;
            let mut primary = None;
            for (owner, pcurve) in pcurves {
                let domain = pcurve.domain()?;
                let (start, end) = (pcurve.evaluate(domain[0])?, pcurve.evaluate(domain[1])?);
                let (start, end) = if curve_along_chain { (start, end) } else { (end, start) };
                if owner == region {
                    primary = Some(ChainUv {
                        region,
                        start: [start.x, start.y],
                        end: [end.x, end.y],
                    });
                }
                custom_revolve_pcurves.insert(owner, pcurve);
            }
            (curve, t0, t1, curve_along_chain, primary)
        } else {
            return Err(format!(
                "hybrid BREP: revolved chain {chain_index} (region {region}, adjacent {:?}, closed={}, station spread {station_spread:.6e}, radial error {radius_error:.6e}, line deviation {:.6e}, uv {:?}->{:?}, points {:?}) is neither arc, ring, nor ruling",
                chain.adjacent,
                chain.closed,
                max_line_deviation(&points),
                coordinates.first(),
                coordinates.last(),
                points,
                // Source samples are included to make conservative demotions
                // diagnosable in focused regression tests.
            ));
        }
    } else {
        if chain.closed || max_line_deviation(&points) > tolerance {
            return Err(format!(
                "hybrid BREP: non-cylinder chain {chain_index} is not a line"
            ));
        }
        (
            make_line(points[0], *points.last().unwrap())?,
            0.0,
            1.0,
            true,
            None,
        )
    };

    let first = chain.vertices[0];
    let last = if chain.closed {
        first
    } else {
        *chain.vertices.last().unwrap()
    };
    let (start_mesh, end_mesh) = if curve_along_chain {
        (first, last)
    } else {
        (last, first)
    };
    let exact_start = curve.evaluate(t0)?;
    let exact_end = curve.evaluate(t1)?;
    let start = exact_arena_vertex(
        start_mesh,
        exact_start,
        tolerance,
        arena,
        arena_vertices,
        ids,
    )?;
    let end = exact_arena_vertex(end_mesh, exact_end, tolerance, arena, arena_vertices, ids)?;
    let edge = arena.edges.insert(ArenaEdge {
        wire_id: ids.next(),
        curve,
        t0,
        t1,
        start,
        end,
        degenerate: false,
        name: None,
    });
    let mut revolve_uvs = primary_uv.into_iter().collect::<Vec<_>>();
    if let Some(pcurve) = revolve_ids
        .get(1)
        .and_then(|secondary| custom_revolve_pcurves.get(secondary))
    {
        let domain = pcurve.domain()?;
        let (start, end) = (pcurve.evaluate(domain[0])?, pcurve.evaluate(domain[1])?);
        let (start, end) = if curve_along_chain { (start, end) } else { (end, start) };
        revolve_uvs.push(ChainUv {
            region: revolve_ids[1],
            start: [start.x, start.y],
            end: [end.x, end.y],
        });
    } else if revolve_ids.len() == 2 {
        let secondary_region = revolve_ids[1];
        let secondary = revolve_info(secondary_region, cylinders, cones, spheres).unwrap();
        let mut coordinates = points
            .iter()
            .map(|&point| secondary.coordinates(point))
            .collect::<Vec<_>>();
        let mut us = coordinates.iter().map(|uv| uv[0]).collect::<Vec<_>>();
        unwrap(&mut us);
        for (uv, u) in coordinates.iter_mut().zip(&us) {
            uv[0] = *u;
        }
        let uv = if chain.closed {
            let mut ring_points = points.clone();
            ring_points.push(points[0]);
            let mut ring_us = ring_points
                .iter()
                .map(|&point| secondary.coordinates(point)[0])
                .collect::<Vec<_>>();
            unwrap(&mut ring_us);
            let sweep = ring_us.last().unwrap() - ring_us[0];
            let v = coordinates.iter().map(|uv| uv[1]).sum::<f64>() / coordinates.len() as f64;
            ChainUv {
                region: secondary_region,
                start: [0.0, v],
                end: [if sweep > 0.0 { 1.0 } else { -1.0 }, v],
            }
        } else {
            let start = coordinates[0];
            let mut end = *coordinates.last().unwrap();
            if (start[1] - end[1]).abs() <= 1.0e-5 {
                // A common ring arc: constant station on both revolves.
                ChainUv {
                    region: secondary_region,
                    start,
                    end,
                }
            } else {
                // A shared axial ruling: constant angle on the secondary
                // cylinder, which may sit on its parameter seam.
                while end[0] - start[0] > 0.5 {
                    end[0] -= 1.0;
                }
                while end[0] - start[0] < -0.5 {
                    end[0] += 1.0;
                }
                if (end[0] - start[0]).abs() > 1.0e-4 {
                    return Err(format!(
                        "hybrid BREP: shared revolve chain {chain_index} is neither a common ring arc nor a common ruling"
                    ));
                }
                let u = ((start[0] + end[0]) * 0.5).rem_euclid(1.0);
                ChainUv {
                    region: secondary_region,
                    start: [u, start[1]],
                    end: [u, end[1]],
                }
            }
        };
        revolve_uvs.push(uv);
    }
    Ok(BuiltEdge {
        edge,
        curve_along_chain,
        revolve_uvs,
        custom_revolve_pcurves,
    })
}

fn plane_frame(
    plane: PlaneInfo,
    boundary: &RegionBoundary,
    chains: &[Chain],
    vertices: &[Vec3],
) -> Result<(Vec3, Vec3, Vec3, f64, f64), String> {
    let normal = plane.normal.normalized()?;
    let x = normal.perpendicular()?;
    let y = normal.cross(x).normalized()?;
    let mut min_u = f64::INFINITY;
    let mut max_u = f64::NEG_INFINITY;
    let mut min_v = f64::INFINITY;
    let mut max_v = f64::NEG_INFINITY;
    for traversal in boundary.cycles.iter().flatten() {
        for &vertex in &chains[traversal.chain].vertices {
            let delta = vertices[vertex].sub(plane.origin);
            let u = delta.dot(x);
            let v = delta.dot(y);
            min_u = min_u.min(u);
            max_u = max_u.max(u);
            min_v = min_v.min(v);
            max_v = max_v.max(v);
        }
    }
    if !min_u.is_finite() || max_u - min_u <= 1.0e-12 || max_v - min_v <= 1.0e-12 {
        return Err("hybrid BREP: degenerate planar face bounds".into());
    }
    Ok((
        plane.origin.add(x.scale(min_u)).add(y.scale(min_v)),
        x,
        y,
        max_u - min_u,
        max_v - min_v,
    ))
}

fn affine_pcurve(curve: &NurbsCurve, origin: Vec3, x: Vec3, y: Vec3) -> Result<NurbsCurve, String> {
    let controls = curve
        .control_points
        .iter()
        .map(|control| {
            if !control.w.is_finite() || control.w.abs() <= f64::EPSILON {
                return Err("hybrid BREP: pcurve source has an invalid homogeneous weight".into());
            }
            let point = Vec3::new(
                control.x / control.w,
                control.y / control.w,
                control.z / control.w,
            );
            let delta = point.sub(origin);
            Ok(Vec4 {
                x: delta.dot(x) * control.w,
                y: delta.dot(y) * control.w,
                z: 0.0,
                w: control.w,
            })
        })
        .collect::<Result<Vec<_>, String>>()?;
    NurbsCurve::new(curve.degree, curve.knots.clone(), controls)
}

#[allow(clippy::too_many_arguments)]
fn build_plane_face(
    _region: u32,
    plane: PlaneInfo,
    boundary: &RegionBoundary,
    chains: &[Chain],
    built_edges: &[BuiltEdge],
    vertices: &[Vec3],
    arena: &mut TopologyArena,
    ids: &mut Ids,
) -> Result<FaceId, String> {
    let (origin, x, y, u_extent, v_extent) = plane_frame(plane, boundary, chains, vertices)?;
    let surface = brep_kernel::make_plane(origin, x, y, u_extent, v_extent)?;
    let mut loops = Vec::new();
    for cycle in &boundary.cycles {
        let mut coedges = Vec::new();
        for traversal in cycle {
            let built = &built_edges[traversal.chain];
            let edge = &arena.edges[built.edge];
            let mut pcurve = affine_pcurve(&edge.curve, origin, x, y)?;
            let forward = traversal.forward == built.curve_along_chain;
            if !forward {
                pcurve = pcurve.reversed()?;
            }
            coedges.push(arena.coedges.insert(ArenaCoedge {
                wire_id: ids.next(),
                edge: built.edge,
                forward,
                pcurve,
            }));
        }
        loops.push(arena.loops.insert(ArenaLoop {
            wire_id: ids.next(),
            coedges,
        }));
    }
    Ok(arena.faces.insert(ArenaFace {
        wire_id: ids.next(),
        surface,
        same_sense: true,
        loops,
        name: None,
    }))
}

fn parameter_line(start: [f64; 2], end: [f64; 2]) -> Result<NurbsCurve, String> {
    make_line(
        Vec3::new(start[0], start[1], 0.0),
        Vec3::new(end[0], end[1], 0.0),
    )
}

fn build_cylinder_face(
    region: u32,
    cylinder: &CylinderInfo,
    boundary: &RegionBoundary,
    built_edges: &[BuiltEdge],
    arena: &mut TopologyArena,
    ids: &mut Ids,
) -> Result<FaceId, String> {
    let mut loops = Vec::new();
    for cycle in &boundary.cycles {
        let mut coedges = Vec::new();
        let mut previous_end: Option<[f64; 2]> = None;
        for traversal in cycle {
            let built = &built_edges[traversal.chain];
            let uv = built
                .revolve_uvs
                .iter()
                .find(|uv| uv.region == region)
                .copied()
                .ok_or_else(|| format!("hybrid BREP: cylinder {region} has non-parametric edge"))?;
            let (mut start, mut end) = if traversal.forward {
                (uv.start, uv.end)
            } else {
                (uv.end, uv.start)
            };
            if let Some(previous) = previous_end {
                let shift = (previous[0] - start[0]).round();
                start[0] += shift;
                end[0] += shift;
            }
            previous_end = Some(end);
            let forward = traversal.forward == built.curve_along_chain;
            let mut pcurve = built
                .custom_revolve_pcurves
                .get(&region)
                .cloned()
                .unwrap_or(parameter_line(start, end)?);
            if !forward && built.custom_revolve_pcurves.contains_key(&region) {
                pcurve = pcurve.reversed()?;
            }
            coedges.push(arena.coedges.insert(ArenaCoedge {
                wire_id: ids.next(),
                edge: built.edge,
                forward,
                pcurve,
            }));
        }
        loops.push(arena.loops.insert(ArenaLoop {
            wire_id: ids.next(),
            coedges,
        }));
    }
    Ok(arena.faces.insert(ArenaFace {
        wire_id: ids.next(),
        surface: cylinder.surface.clone(),
        same_sense: cylinder.carrier.sense == 1,
        loops,
        name: None,
    }))
}

/// A sphere face from its ring loops.  A cap around the north pole also
/// carries the surface seam from the ring to the pole and the degenerate
/// pole edge, the way the kernel's own sphere solid is built, so its
/// parameter-space loop closes.
fn build_sphere_face(
    region: u32,
    sphere: &SphereInfo,
    boundary: &RegionBoundary,
    built_edges: &[BuiltEdge],
    arena: &mut TopologyArena,
    ids: &mut Ids,
) -> Result<FaceId, String> {
    let mut loops = Vec::new();
    for cycle in &boundary.cycles {
        let mut coedges = Vec::new();
        let mut previous_end: Option<[f64; 2]> = None;
        let mut ring: Option<(brep_kernel::VertexId, [f64; 2], [f64; 2])> = None;
        for traversal in cycle {
            let built = &built_edges[traversal.chain];
            let uv = built
                .revolve_uvs
                .iter()
                .find(|uv| uv.region == region)
                .copied()
                .ok_or_else(|| format!("hybrid BREP: sphere {region} has non-parametric edge"))?;
            let (mut start, mut end) = if traversal.forward {
                (uv.start, uv.end)
            } else {
                (uv.end, uv.start)
            };
            if let Some(previous) = previous_end {
                let shift = (previous[0] - start[0]).round();
                start[0] += shift;
                end[0] += shift;
            }
            previous_end = Some(end);
            let forward = traversal.forward == built.curve_along_chain;
            let edge = &arena.edges[built.edge];
            ring = Some((edge.start, start, end));
            // A tilted ring carries its fitted loop, oriented along its curve.
            let pcurve = match built.custom_revolve_pcurves.get(&region) {
                Some(pcurve) if forward => pcurve.clone(),
                Some(pcurve) => pcurve.reversed()?,
                None => parameter_line(start, end)?,
            };
            coedges.push(arena.coedges.insert(ArenaCoedge {
                wire_id: ids.next(),
                edge: built.edge,
                forward,
                pcurve,
            }));
        }
        if sphere.north_pole_inside {
            let Some((ring_vertex, ring_start, ring_end)) = ring.filter(|_| cycle.len() == 1) else {
                return Err(format!(
                    "hybrid BREP: sphere {region} cap boundary is not a single ring"
                ));
            };
            let v = ring_start[1];
            let seam = trim_curve(&sphere.surface.iso_curve_u(0.0)?, v, 1.0)?;
            let domain = seam.domain()?;
            let pole_point = sphere.surface.evaluate(0.0, 1.0)?;
            let pole = arena.vertices.insert(ArenaVertex {
                wire_id: ids.next(),
                point: pole_point,
            });
            let seam_edge = arena.edges.insert(ArenaEdge {
                wire_id: ids.next(),
                curve: seam,
                t0: domain[0],
                t1: domain[1],
                start: ring_vertex,
                end: pole,
                degenerate: false,
                name: None,
            });
            let pole_edge = arena.edges.insert(ArenaEdge {
                wire_id: ids.next(),
                curve: make_line(pole_point, pole_point)?,
                t0: 0.0,
                t1: 1.0,
                start: pole,
                end: pole,
                degenerate: true,
                name: None,
            });
            // Up the seam on the ring's far side, across the pole, down the
            // seam on the ring's near side: the loop closes in parameter
            // space with the cap on its left.
            coedges.push(arena.coedges.insert(ArenaCoedge {
                wire_id: ids.next(),
                edge: seam_edge,
                forward: true,
                pcurve: parameter_line([ring_end[0], v], [ring_end[0], 1.0])?,
            }));
            coedges.push(arena.coedges.insert(ArenaCoedge {
                wire_id: ids.next(),
                edge: pole_edge,
                forward: true,
                pcurve: parameter_line([ring_end[0], 1.0], [ring_start[0], 1.0])?,
            }));
            coedges.push(arena.coedges.insert(ArenaCoedge {
                wire_id: ids.next(),
                edge: seam_edge,
                forward: false,
                pcurve: parameter_line([ring_start[0], 1.0], [ring_start[0], v])?,
            }));
        }
        loops.push(arena.loops.insert(ArenaLoop {
            wire_id: ids.next(),
            coedges,
        }));
    }
    Ok(arena.faces.insert(ArenaFace {
        wire_id: ids.next(),
        surface: sphere.surface.clone(),
        same_sense: sphere.carrier.sense == 1,
        loops,
        name: None,
    }))
}

fn build_cone_face(
    region: u32,
    cone: &ConeInfo,
    boundary: &RegionBoundary,
    built_edges: &[BuiltEdge],
    arena: &mut TopologyArena,
    ids: &mut Ids,
) -> Result<FaceId, String> {
    let mut loops = Vec::new();
    for cycle in &boundary.cycles {
        let mut coedges = Vec::new();
        let mut previous_end: Option<[f64; 2]> = None;
        for traversal in cycle {
            let built = &built_edges[traversal.chain];
            let uv = built
                .revolve_uvs
                .iter()
                .find(|uv| uv.region == region)
                .copied()
                .ok_or_else(|| format!("hybrid BREP: cone {region} has non-parametric edge"))?;
            let (mut start, mut end) = if traversal.forward {
                (uv.start, uv.end)
            } else {
                (uv.end, uv.start)
            };
            if let Some(previous) = previous_end {
                let shift = (previous[0] - start[0]).round();
                start[0] += shift;
                end[0] += shift;
            }
            previous_end = Some(end);
            let forward = traversal.forward == built.curve_along_chain;
            let mut pcurve = built
                .custom_revolve_pcurves
                .get(&region)
                .cloned()
                .unwrap_or(parameter_line(start, end)?);
            if !forward && built.custom_revolve_pcurves.contains_key(&region) {
                pcurve = pcurve.reversed()?;
            }
            coedges.push(arena.coedges.insert(ArenaCoedge {
                wire_id: ids.next(),
                edge: built.edge,
                forward,
                pcurve,
            }));
        }
        loops.push(arena.loops.insert(ArenaLoop {
            wire_id: ids.next(),
            coedges,
        }));
    }
    Ok(arena.faces.insert(ArenaFace {
        wire_id: ids.next(),
        surface: cone.surface.clone(),
        same_sense: cone.carrier.sense == 1,
        loops,
        name: None,
    }))
}

fn sort_region_cycles(
    boundaries: &mut [RegionBoundary],
    planes: &HashMap<u32, PlaneInfo>,
    cylinders: &HashMap<u32, CylinderInfo>,
    cones: &HashMap<u32, ConeInfo>,
    spheres: &HashMap<u32, SphereInfo>,
    chains: &[Chain],
    vertices: &[Vec3],
) {
    for (region, boundary) in boundaries.iter_mut().enumerate() {
        let mut scored = boundary
            .cycles
            .drain(..)
            .map(|cycle| {
                let mut points = Vec::new();
                for traversal in &cycle {
                    let chain = &chains[traversal.chain];
                    let iter: Box<dyn Iterator<Item = &usize>> = if traversal.forward {
                        Box::new(chain.vertices.iter())
                    } else {
                        Box::new(chain.vertices.iter().rev())
                    };
                    for vertex in iter {
                        points.push(vertices[*vertex]);
                    }
                }
                let area = if let Some(plane) = planes.get(&(region as u32)) {
                    let normal = plane.normal.normalized().unwrap_or(plane.normal);
                    let x = normal.perpendicular().unwrap_or(Vec3::new(1.0, 0.0, 0.0));
                    let y = normal.cross(x);
                    polygon_area(
                        &points
                            .iter()
                            .map(|p| {
                                let d = p.sub(plane.origin);
                                [d.dot(x), d.dot(y)]
                            })
                            .collect::<Vec<_>>(),
                    )
                } else if let Some(cylinder) = cylinders.get(&(region as u32)) {
                    let mut uv = points
                        .iter()
                        .map(|&p| cylinder_coordinates(cylinder, p))
                        .collect::<Vec<_>>();
                    let mut us = uv.iter().map(|p| p[0]).collect::<Vec<_>>();
                    unwrap(&mut us);
                    for (p, u) in uv.iter_mut().zip(us) {
                        p[0] = u;
                    }
                    polygon_area(&uv)
                } else if let Some(cone) = cones.get(&(region as u32)) {
                    let mut uv = points
                        .iter()
                        .map(|&p| cone_coordinates(cone, p))
                        .collect::<Vec<_>>();
                    let mut us = uv.iter().map(|p| p[0]).collect::<Vec<_>>();
                    unwrap(&mut us);
                    for (p, u) in uv.iter_mut().zip(us) {
                        p[0] = u;
                    }
                    polygon_area(&uv)
                } else if let Some(sphere) = spheres.get(&(region as u32)) {
                    let mut uv = points
                        .iter()
                        .map(|&p| sphere_coordinates(sphere, p))
                        .collect::<Vec<_>>();
                    let mut us = uv.iter().map(|p| p[0]).collect::<Vec<_>>();
                    unwrap(&mut us);
                    for (p, u) in uv.iter_mut().zip(us) {
                        p[0] = u;
                    }
                    polygon_area(&uv)
                } else {
                    0.0
                };
                let same = planes.contains_key(&(region as u32))
                    || cylinders
                        .get(&(region as u32))
                        .is_some_and(|c| c.carrier.sense == 1)
                    || cones
                        .get(&(region as u32))
                        .is_some_and(|c| c.carrier.sense == 1)
                    || spheres
                        .get(&(region as u32))
                        .is_some_and(|c| c.carrier.sense == 1);
                let score = if same { area } else { -area };
                (score, cycle)
            })
            .collect::<Vec<_>>();
        scored.sort_by(|a, b| b.0.total_cmp(&a.0));
        boundary.cycles = scored.into_iter().map(|(_, cycle)| cycle).collect();
    }
}

fn polygon_area(points: &[[f64; 2]]) -> f64 {
    if points.len() < 3 {
        return 0.0;
    }
    (0..points.len())
        .map(|i| {
            let a = points[i];
            let b = points[(i + 1) % points.len()];
            a[0] * b[1] - b[0] * a[1]
        })
        .sum::<f64>()
        * 0.5
}