threers 0.1.0

Drop-in three.js replacement for Rust and WebAssembly (wgpu) — THREE.* API, scene graph, PBR, post-fx, mesh BVH, CSG, an OpenSCAD front end with a watertight exact-CSG kernel, headless render, and native media codecs
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
//! The general fuse — stages C and D of
//! [`docs/brep-boolean-v2-plan.md`](../../docs/brep-boolean-v2-plan.md).
//!
//! # Why a second boolean
//!
//! [`Body::boolean`] splits one face at a time by the curves computed for that
//! face's pairs, and recovers afterwards which of the pieces' boundaries are
//! the same edge. Everything it gets wrong is that recovery: an edge of one
//! solid lying in a face of the other, an outline that two faces each cut
//! their own way, a split point one face found and its neighbour did not.
//!
//! Here nothing needs recovering, because nothing is computed twice:
//!
//! 1. **One vertex pool.** Every input vertex, every end of a cut, every
//!    crossing goes through one table welded at the tolerance, so a point two
//!    faces both find is one index.
//! 2. **Each pair once.** A face of A and a face of B are intersected once and
//!    the section is handed to both: for two planes the line they share,
//!    otherwise the curves [`ssi`] or [`march`] finds, clipped to the part
//!    lying in both faces. Faces on one surface hand each other their
//!    outlines instead.
//! 3. **An arrangement per face**, in its own parameters. The face's rings
//!    and the sections it was handed are split wherever they cross or touch,
//!    and the faces of that graph are walked. A face that wraps round its
//!    surface is a whole period wide with its seam on both sides, and every
//!    section is laid down once per period it reaches.
//! 4. **Classification** of one point per piece: on a face the other solid
//!    shares, by the rule for shared walls; otherwise by a ray against the
//!    other solid, exactly when its faces are all flat and against its
//!    tessellation when they are not.
//! 5. **Assembly.** Pieces of one wrapping face that meet across its seam are
//!    one face again, and one that meets itself keeps the seam as an edge it
//!    uses twice. Every side is split at every vertex lying on it, after
//!    which an edge is a pair of pool indices and the two faces along it name
//!    the same pair. Runs of those between the same two faces are chained
//!    back into one edge.
//!
//! Kept pieces are not merged otherwise, as OpenCASCADE's general fuse leaves
//! them. Anything the method cannot settle — a face it cannot lay out, a ray
//! that grazes in every direction, an edge with other than two faces — is a
//! [`Declined`], and [`Body::boolean`] falls back to its own path.

use std::collections::{HashMap, HashSet};

use crate::nurbs::v3;
use crate::nurbs::V3;

use super::body::{Body, Edge, Face, TrimLoop};
use super::boolean::{point_in_mesh, BooleanOp, Declined};
use super::curve3d::Curve3d;
use super::intersect::{march, ssi, SsiResult};
use super::Surface;

type P2 = [f64; 2];

/// A ring of a face: its points in space and in the surface's parameters.
type Ring = (Vec<V3>, Vec<(f64, f64)>);

/// A link across a seam: the piece beyond, the direction, which way, and
/// the two sides it joins.
type Link = (usize, usize, i64, SideAt, SideAt);

// -- the plane ---------------------------------------------------------------

fn sub2(a: P2, b: P2) -> P2 {
    [a[0] - b[0], a[1] - b[1]]
}

fn cross2(a: P2, b: P2) -> f64 {
    a[0] * b[1] - a[1] * b[0]
}

fn dot2(a: P2, b: P2) -> f64 {
    a[0] * b[0] + a[1] * b[1]
}

fn len2(a: P2) -> f64 {
    a[0].hypot(a[1])
}

/// Distance from `p` to the segment `ab`, and the parameter of the nearest
/// point, clamped to it.
fn to_segment(p: P2, a: P2, b: P2) -> (f64, f64) {
    let ab = sub2(b, a);
    let l = dot2(ab, ab);
    let t = if l > 0.0 {
        (dot2(sub2(p, a), ab) / l).clamp(0.0, 1.0)
    } else {
        0.0
    };
    let at = [a[0] + ab[0] * t, a[1] + ab[1] * t];
    (len2(sub2(p, at)), t)
}

/// Inside an odd number of the rings.
fn in_rings(rings: &[Vec<P2>], p: P2) -> bool {
    let mut inside = false;
    for r in rings {
        let n = r.len();
        for i in 0..n {
            let (a, b) = (r[i], r[(i + 1) % n]);
            if (a[1] > p[1]) != (b[1] > p[1]) {
                let x = a[0] + (p[1] - a[1]) / (b[1] - a[1]) * (b[0] - a[0]);
                if p[0] < x {
                    inside = !inside;
                }
            }
        }
    }
    inside
}

fn ring_distance(rings: &[Vec<P2>], p: P2) -> f64 {
    let mut best = f64::INFINITY;
    for r in rings {
        let n = r.len();
        for i in 0..n {
            best = best.min(to_segment(p, r[i], r[(i + 1) % n]).0);
        }
    }
    best
}

fn signed_area(ring: &[P2]) -> f64 {
    let n = ring.len();
    (0..n)
        .map(|i| cross2(ring[i], ring[(i + 1) % n]))
        .sum::<f64>()
        * 0.5
}

/// Distance from `p` to the polyline `line`.
fn to_polyline(p: V3, line: &[V3]) -> f64 {
    line.windows(2)
        .map(|w| {
            let ab = v3::sub(w[1], w[0]);
            let l = v3::dot(ab, ab);
            let t = if l > 0.0 {
                (v3::dot(v3::sub(p, w[0]), ab) / l).clamp(0.0, 1.0)
            } else {
                0.0
            };
            v3::dist(p, v3::add(w[0], v3::scale(ab, t)))
        })
        .fold(f64::INFINITY, f64::min)
}

fn lerp3(a: V3, b: V3, t: f64) -> V3 {
    v3::add(a, v3::scale(v3::sub(b, a), t))
}

// -- faces as parameter regions ----------------------------------------------

/// A face of an operand, laid out in its surface's parameters.
///
/// Parameters are *scaled* by the surface's speed at the face's middle, so a
/// distance in them is roughly a distance in space and one tolerance serves
/// for both.
struct Patch {
    surface: Surface,
    flipped: bool,
    /// Origin and normal, when the surface is a plane.
    plane: Option<(V3, V3)>,
    scale: P2,
    /// The middle of the face, in the surface's own parameters.
    anchor: (f64, f64),
    /// Periods, scaled.
    period: [Option<f64>; 2],
    /// The domain: each ring's parameters, and the points in space they are.
    rings2: Vec<Vec<P2>>,
    rings3: Vec<Vec<V3>>,
    /// Box of the domain in scaled parameters.
    dlo: P2,
    dhi: P2,
    /// Box in space.
    lo: V3,
    hi: V3,
    /// Whether the domain is a whole period across, in u and in v.
    wraps: [bool; 2],
}

impl Patch {
    fn uv(&self, p: V3) -> Option<P2> {
        let (u, v) = self.surface.invert_near(p, self.anchor)?;
        Some([u * self.scale[0], v * self.scale[1]])
    }

    fn point(&self, w: P2) -> V3 {
        self.surface
            .point(w[0] / self.scale[0], w[1] / self.scale[1])
    }

    /// `q` and the same point a period either way, where the surface has one.
    fn images(&self, q: P2) -> Vec<P2> {
        let shifts = |p: Option<f64>| match p {
            Some(t) => vec![0.0, -t, t],
            None => vec![0.0],
        };
        let mut out = Vec::with_capacity(9);
        for du in shifts(self.period[0]) {
            for dv in shifts(self.period[1]) {
                out.push([q[0] + du, q[1] + dv]);
            }
        }
        out
    }

    /// Off the surface by more than the tolerance?
    fn off_surface(&self, p: V3, tolerance: f64) -> bool {
        match self.plane {
            Some((o, n)) => v3::dot(n, v3::sub(p, o)).abs() > tolerance * 4.0,
            None => self
                .uv(p)
                .is_none_or(|q| v3::dist(self.point(q), p) > tolerance * 4.0),
        }
    }

    /// On the face or its boundary.
    fn holds(&self, p: V3, tolerance: f64) -> bool {
        self.holds_within(p, tolerance, tolerance)
    }

    /// On the face, within `tolerance` of its surface and `reach` of its
    /// boundary.
    fn holds_within(&self, p: V3, tolerance: f64, reach: f64) -> bool {
        if self.off_surface(p, tolerance) {
            return false;
        }
        let Some(q) = self.uv(p) else {
            return false;
        };
        self.images(q)
            .into_iter()
            .any(|s| ring_distance(&self.rings2, s) <= reach || in_rings(&self.rings2, s))
    }

    /// Inside the face, clear of its boundary.
    fn interior(&self, p: V3, tolerance: f64) -> bool {
        if self.off_surface(p, tolerance) {
            return false;
        }
        let Some(q) = self.uv(p) else {
            return false;
        };
        self.images(q)
            .into_iter()
            .any(|s| in_rings(&self.rings2, s) && ring_distance(&self.rings2, s) > tolerance)
    }

    /// A polyline's parameters, continued across the period rather than
    /// jumping at it.
    fn unwrapped(&self, pts: &[V3]) -> Option<Vec<P2>> {
        let mut out: Vec<P2> = Vec::with_capacity(pts.len());
        for p in pts {
            let mut q = self.uv(*p)?;
            if let Some(prev) = out.last() {
                for k in 0..2 {
                    if let Some(t) = self.period[k] {
                        q[k] -= t * ((q[k] - prev[k]) / t).round();
                    }
                }
            }
            out.push(q);
        }
        Some(out)
    }

    /// The shifts by whole periods that bring a polyline with this box over
    /// the domain.
    fn copies(&self, lo: P2, hi: P2, tolerance: f64) -> Vec<P2> {
        let range = |k: usize| -> Vec<f64> {
            match self.period[k] {
                None => vec![0.0],
                Some(t) => {
                    let first = ((self.dlo[k] - hi[k] - tolerance) / t).ceil() as i64;
                    let last = ((self.dhi[k] - lo[k] + tolerance) / t).floor() as i64;
                    (first..=last.max(first)).map(|j| j as f64 * t).collect()
                }
            }
        };
        let mut out = Vec::new();
        for du in range(0) {
            for dv in range(1) {
                out.push([du, dv]);
            }
        }
        out
    }
}

/// How fast the surface moves with each parameter, at `(u, v)`.
fn speed(s: &Surface, u: f64, v: f64) -> P2 {
    let h = 1e-6;
    let su = v3::dist(s.point(u + h, v), s.point(u - h, v)) / (2.0 * h);
    let sv = v3::dist(s.point(u, v + h), s.point(u, v - h)) / (2.0 * h);
    [su.max(1e-9), sv.max(1e-9)]
}

type Sample = ((f64, f64), V3);

/// Points along the iso-curve from `a` to `b` (parameters), close enough
/// that each chord is within `tolerance` of the surface.
fn iso_points(s: &Surface, a: (f64, f64), b: (f64, f64), tolerance: f64) -> Vec<Sample> {
    fn go(s: &Surface, a: Sample, b: Sample, tolerance: f64, depth: usize, out: &mut Vec<Sample>) {
        let m = ((a.0 .0 + b.0 .0) * 0.5, (a.0 .1 + b.0 .1) * 0.5);
        let pm = s.point(m.0, m.1);
        if depth < 12 && (depth < 3 || v3::dist(pm, lerp3(a.1, b.1, 0.5)) > tolerance) {
            go(s, a, (m, pm), tolerance, depth + 1, out);
            out.push((m, pm));
            go(s, (m, pm), b, tolerance, depth + 1, out);
        }
    }
    let (sa, sb) = ((a, s.point(a.0, a.1)), (b, s.point(b.0, b.1)));
    let mut out = vec![sa];
    // A straight iso-line needs no inner points; probe once to tell.
    let pm = s.point((a.0 + b.0) * 0.5, (a.1 + b.1) * 0.5);
    if v3::dist(pm, lerp3(sa.1, sb.1, 0.5)) > tolerance * 1e-3 {
        go(s, sa, sb, tolerance, 0, &mut out);
    }
    out.push(sb);
    out
}

/// One side of a wrapping face's domain along `u` at `v = at`: the rim lying
/// there, if there is one, its own vertices; otherwise the iso-curve.
fn side_along_u(
    surface: &Surface,
    rim: Option<&[(V3, (f64, f64))]>,
    at: f64,
    from: f64,
    to: f64,
    tolerance: f64,
) -> Vec<(V3, (f64, f64))> {
    let Some(rim) = rim else {
        return iso_points(surface, (from, at), (to, at), tolerance)
            .into_iter()
            .map(|(q, p)| (p, q))
            .collect();
    };
    let t = surface.period().0.unwrap_or(f64::INFINITY);
    let (lo, hi) = (from.min(to), from.max(to));
    // The rim's vertices in this period, in order of `u`.
    let mut pts: Vec<(f64, V3)> = rim
        .iter()
        .map(|(p, q)| {
            let x = if t.is_finite() {
                lo + (q.0 - lo).rem_euclid(t)
            } else {
                q.0
            };
            (x, *p)
        })
        .collect();
    pts.sort_by(|a, b| a.0.total_cmp(&b.0));
    let near = |a: f64, b: f64| (a - b).abs() <= 1e-9 * (1.0 + a.abs());
    // Where the rim is at `x`: a vertex, or on the chord between two.
    let on_rim = |x: f64| -> V3 {
        if let Some((_, p)) = pts.iter().find(|(y, _)| near(*y, x) || near(*y + t, x)) {
            return *p;
        }
        let n = pts.len();
        for i in 0..n {
            let (a, pa) = pts[i];
            let (mut b, pb) = pts[(i + 1) % n];
            if i + 1 == n {
                b += t;
            }
            let y = if x < a { x + t } else { x };
            if y >= a && y <= b && b > a {
                return lerp3(pa, pb, (y - a) / (b - a));
            }
        }
        surface.point(x, at)
    };
    let mut side = vec![(on_rim(lo), (lo, at))];
    for (x, p) in &pts {
        if *x > lo && !near(*x, lo) && *x < hi && !near(*x, hi) {
            side.push((*p, (*x, at)));
        }
    }
    side.push((on_rim(hi), (hi, at)));
    if from > to {
        side.reverse();
    }
    side
}

/// The faces of a body as patches, or `None` when one cannot be laid out.
fn patches(body: &Body, tolerance: f64) -> Option<Vec<Patch>> {
    let mut out = Vec::with_capacity(body.faces().len());
    for (fi, face) in body.faces().iter().enumerate() {
        let surface = body.surfaces()[face.surface].clone();
        let (pu, pv) = surface.period();
        let loops = body.face_loops(fi);
        let real: Vec<&TrimLoop> = loops
            .iter()
            .filter(|l| l.is_outer() || l.is_hole())
            .collect();
        let wraps = [face.u_wraps && pu.is_some(), face.v_wraps && pv.is_some()];
        let mut rings: Vec<Ring> = Vec::new();
        let anchor;
        let mut wraps = wraps;
        if !wraps[0] && !wraps[1] {
            if real.is_empty() || real.iter().any(|l| l.vertices.len() < 3) {
                return None;
            }
            let (mut lo, mut hi) = ([f64::INFINITY; 2], [f64::NEG_INFINITY; 2]);
            for q in real.iter().flat_map(|l| l.uv.iter()) {
                for k in 0..2 {
                    lo[k] = lo[k].min(q[k]);
                    hi[k] = hi[k].max(q[k]);
                }
            }
            // A face a whole period across, closed by its seam as an edge
            // it uses twice: the domain is its loops as they are, with the
            // seam on both sides.
            for (k, p) in [pu, pv].into_iter().enumerate() {
                if let Some(t) = p {
                    let span = hi[k] - lo[k];
                    if span > t * (1.0 + 1e-6) {
                        return None;
                    }
                    if span >= t * (1.0 - 1e-6) {
                        wraps[k] = true;
                    }
                }
            }
            anchor = ((lo[0] + hi[0]) * 0.5, (lo[1] + hi[1]) * 0.5);
            let mut sorted = real.clone();
            sorted.sort_by_key(|l| !l.is_outer());
            for l in sorted {
                let pts: Vec<V3> = l.vertices.iter().map(|&v| body.vertices()[v]).collect();
                rings.push((pts, l.uv.iter().map(|q| (q[0], q[1])).collect()));
            }
        } else {
            if real.iter().any(|l| l.is_outer()) || wraps[1] && !wraps[0] {
                return None;
            }
            let rims: Vec<Vec<(V3, (f64, f64))>> = loops
                .iter()
                .filter(|l| l.is_degenerate())
                .map(|l| {
                    l.vertices
                        .iter()
                        .filter_map(|&v| {
                            let p = body.vertices()[v];
                            surface.invert(p).map(|q| (p, q))
                        })
                        .collect::<Vec<_>>()
                })
                .filter(|r: &Vec<_>| !r.is_empty())
                .collect();
            // A period wide where the face wraps, starting at a vertex of a
            // rim so the seam's ends are vertices already there.
            let mut u = face.u_range;
            let mut v = face.v_range;
            if let Some(t) = pu {
                let start = rims.first().map(|r| r[0].1 .0).unwrap_or(u.0);
                u = (start, start + t);
            }
            if let (true, Some(t)) = (wraps[1], pv) {
                v = (v.0, v.0 + t);
            } else if !rims.is_empty() {
                // Where the rims are, not where the face says: the two can
                // part by rounding, and a side that misses its rim is sampled
                // afresh and shares no point with the face beyond it.
                let mean = |r: &Vec<(V3, (f64, f64))>| {
                    r.iter().map(|x| x.1 .1).sum::<f64>() / r.len() as f64
                };
                let heights: Vec<f64> = rims.iter().map(mean).collect();
                let (lo, hi) = heights
                    .iter()
                    .fold((f64::INFINITY, f64::NEG_INFINITY), |m, &h| {
                        (m.0.min(h), m.1.max(h))
                    });
                let span = (v.1 - v.0).abs().max(1e-12);
                if (lo - v.0).abs() <= span * 1e-3 {
                    v.0 = lo;
                }
                if (hi - v.1).abs() <= span * 1e-3 {
                    v.1 = hi;
                }
            }
            anchor = ((u.0 + u.1) * 0.5, (v.0 + v.1) * 0.5);
            // The rim at a height: every point of it within a thousandth of
            // the face's height of `at`.
            let reach = (v.1 - v.0).abs() * 1e-3 + 1e-9;
            let rim_at = |at: f64| {
                rims.iter()
                    .find(|r| r.iter().all(|(_, q)| (q.1 - at).abs() <= reach))
                    .map(|r| r.as_slice())
            };
            if std::env::var_os("THREERS_BREP_DEBUG").is_some() {
                eprintln!(
                    "  [fuse] wrapping {} face {fi}: u {u:?} v {v:?}, rims at {:?}, found {} {}",
                    surface.kind(),
                    rims.iter()
                        .map(|r| (r.len(), r[0].1 .1))
                        .collect::<Vec<_>>(),
                    rim_at(v.0).is_some(),
                    rim_at(v.1).is_some()
                );
            }
            let t4 = tolerance * 0.25;
            let mut ring: Vec<(V3, (f64, f64))> = Vec::new();
            for side in [
                side_along_u(&surface, rim_at(v.0), v.0, u.0, u.1, t4),
                iso_points(&surface, (u.1, v.0), (u.1, v.1), t4)
                    .into_iter()
                    .map(|(q, p)| (p, q))
                    .collect(),
                side_along_u(&surface, rim_at(v.1), v.1, u.1, u.0, t4),
                iso_points(&surface, (u.0, v.1), (u.0, v.0), t4)
                    .into_iter()
                    .map(|(q, p)| (p, q))
                    .collect(),
            ] {
                let n = side.len();
                ring.extend(side.into_iter().take(n - 1));
            }
            rings.push((
                ring.iter().map(|x| x.0).collect(),
                ring.iter().map(|x| x.1).collect(),
            ));
            for l in real {
                let pts: Vec<V3> = l.vertices.iter().map(|&v| body.vertices()[v]).collect();
                let mut uv: Vec<(f64, f64)> = Vec::with_capacity(pts.len());
                for p in &pts {
                    let near = uv.last().copied().unwrap_or(anchor);
                    uv.push(surface.invert_near(*p, near)?);
                }
                rings.push((pts, uv));
            }
        }
        let scale = speed(&surface, anchor.0, anchor.1);
        let mut rings2: Vec<Vec<P2>> = rings
            .iter()
            .map(|(_, uv)| {
                uv.iter()
                    .map(|q| [q.0 * scale[0], q.1 * scale[1]])
                    .collect()
            })
            .collect();
        let mut rings3: Vec<Vec<V3>> = rings.into_iter().map(|(p, _)| p).collect();
        let period = [pu.map(|t| t * scale[0]), pv.map(|t| t * scale[1])];
        let (mut dlo, mut dhi) = ([f64::INFINITY; 2], [f64::NEG_INFINITY; 2]);
        for q in rings2.first().into_iter().flatten() {
            for k in 0..2 {
                dlo[k] = dlo[k].min(q[k]);
                dhi[k] = dhi[k].max(q[k]);
            }
        }
        // A hole of a wrapping face that crosses its seam is also a hole a
        // period over.
        if wraps[0] || wraps[1] {
            let n = rings2.len();
            for i in 1..n {
                for k in 0..2 {
                    let Some(t) = period[k].filter(|_| wraps[k]) else {
                        continue;
                    };
                    for shift in [-t, t] {
                        let moved: Vec<P2> = rings2[i]
                            .iter()
                            .map(|q| {
                                let mut q = *q;
                                q[k] += shift;
                                q
                            })
                            .collect();
                        if moved.iter().any(|q| q[k] > dlo[k] && q[k] < dhi[k]) {
                            rings2.push(moved);
                            rings3.push(rings3[i].clone());
                        }
                    }
                }
            }
        }
        let (mut lo, mut hi) = ([f64::INFINITY; 3], [f64::NEG_INFINITY; 3]);
        let mut grow = |p: V3| {
            for k in 0..3 {
                lo[k] = lo[k].min(p[k]);
                hi[k] = hi[k].max(p[k]);
            }
        };
        for p in rings3.iter().flatten() {
            grow(*p);
        }
        // A curved face bulges past its outline — a sphere's outline is one
        // meridian — so its box is taken over the whole of its domain.
        if !matches!(surface, Surface::Plane { .. }) {
            const N: usize = 16;
            for i in 0..=N {
                for j in 0..=N {
                    let u = dlo[0] + (dhi[0] - dlo[0]) * i as f64 / N as f64;
                    let v = dlo[1] + (dhi[1] - dlo[1]) * j as f64 / N as f64;
                    grow(surface.point(u / scale[0], v / scale[1]));
                }
            }
        }
        let pad = tolerance + (0..3).map(|k| hi[k] - lo[k]).fold(0.0, f64::max) * 0.02;
        let plane = match &surface {
            Surface::Plane { origin, normal, .. } => Some((*origin, *normal)),
            _ => None,
        };
        out.push(Patch {
            surface,
            flipped: face.flipped,
            plane,
            scale,
            anchor,
            period,
            rings2,
            rings3,
            dlo,
            dhi,
            lo: lo.map(|x| x - pad),
            hi: hi.map(|x| x + pad),
            wraps,
        });
    }
    Some(out)
}

fn boxes_meet(a: (V3, V3), b: (V3, V3), tolerance: f64) -> bool {
    (0..3).all(|k| a.0[k] <= b.1[k] + tolerance && b.0[k] <= a.1[k] + tolerance)
}

// -- sections ----------------------------------------------------------------

enum Section {
    Apart,
    /// The two touch without crossing: along a curve or at a point.
    Graze,
    /// The two lie on one surface; whether they face the same way.
    Wall(bool),
    Cuts(Vec<Vec<V3>>),
}

/// Where the line `p0 + t·d` (lying in the plane of `f`, `d` unit) is on the
/// face, boundary included, as intervals of `t`.
fn intervals(f: &Patch, p0: V3, d: V3, tolerance: f64) -> Vec<(f64, f64)> {
    let q0 = f.uv(p0).unwrap_or([0.0, 0.0]);
    let q1 = f.uv(v3::add(p0, d)).unwrap_or([1.0, 0.0]);
    let dd = sub2(q1, q0);
    let mut ts: Vec<f64> = Vec::new();
    for ring in &f.rings2 {
        let n = ring.len();
        for i in 0..n {
            let (a, b) = (ring[i], ring[(i + 1) % n]);
            let (sa, sb) = (cross2(dd, sub2(a, q0)), cross2(dd, sub2(b, q0)));
            let (ta, tb) = (dot2(sub2(a, q0), dd), dot2(sub2(b, q0), dd));
            let (za, zb) = (sa.abs() <= tolerance, sb.abs() <= tolerance);
            if za {
                ts.push(ta);
            }
            if zb {
                ts.push(tb);
            }
            if !za && !zb && (sa > 0.0) != (sb > 0.0) {
                ts.push(ta + (tb - ta) * sa / (sa - sb));
            }
        }
    }
    ts.sort_by(f64::total_cmp);
    ts.dedup_by(|x, y| (*x - *y).abs() <= tolerance);
    let mut out: Vec<(f64, f64)> = Vec::new();
    for w in ts.windows(2) {
        let m = (w[0] + w[1]) * 0.5;
        let q = [q0[0] + dd[0] * m, q0[1] + dd[1] * m];
        if ring_distance(&f.rings2, q) > tolerance && !in_rings(&f.rings2, q) {
            continue;
        }
        match out.last_mut() {
            Some(last) if (last.1 - w[0]).abs() <= tolerance => last.1 = w[1],
            _ => out.push((w[0], w[1])),
        }
    }
    out
}

/// Two planes: the line they share, clipped to both faces.
fn plane_section(f: &Patch, g: &Patch, tolerance: f64) -> Section {
    let ((fo, fnorm), (go, gnorm)) = (f.plane.unwrap(), g.plane.unwrap());
    let u = v3::cross(fnorm, gnorm);
    let lu = v3::norm(u);
    if lu <= 1e-9 {
        if v3::dot(fnorm, v3::sub(go, fo)).abs() > tolerance {
            return Section::Apart;
        }
        let same = (v3::dot(fnorm, gnorm) > 0.0) == (f.flipped == g.flipped);
        return Section::Wall(same);
    }
    let (h1, h2) = (v3::dot(fnorm, fo), v3::dot(gnorm, go));
    let p0 = v3::scale(
        v3::add(
            v3::scale(v3::cross(gnorm, u), h1),
            v3::scale(v3::cross(u, fnorm), h2),
        ),
        1.0 / (lu * lu),
    );
    let d = v3::scale(u, 1.0 / lu);
    let (a, b) = (
        intervals(f, p0, d, tolerance),
        intervals(g, p0, d, tolerance),
    );
    let mut cuts = Vec::new();
    for &(a0, a1) in &a {
        for &(b0, b1) in &b {
            let (lo, hi) = (a0.max(b0), a1.min(b1));
            if hi - lo > tolerance {
                cuts.push(vec![
                    v3::add(p0, v3::scale(d, lo)),
                    v3::add(p0, v3::scale(d, hi)),
                ]);
            }
        }
    }
    Section::Cuts(cuts)
}

/// A curve as points, within a box: `None` for a point of contact.
fn polyline(curve: &Curve3d, lo: V3, hi: V3, tolerance: f64) -> Option<(Vec<V3>, bool)> {
    use std::f64::consts::TAU;
    fn count(c: &Curve3d) -> usize {
        match c {
            Curve3d::Sampled { points, .. } => points.len(),
            Curve3d::Spline { at, .. } => at.len(),
            Curve3d::OnSurfaces { param, .. } => count(param),
            _ => 0,
        }
    }
    fn is_closed(c: &Curve3d) -> bool {
        match c {
            Curve3d::Sampled { closed, .. } | Curve3d::Spline { closed, .. } => *closed,
            Curve3d::OnSurfaces { param, .. } => is_closed(param),
            _ => false,
        }
    }
    match curve {
        Curve3d::Point(_) => None,
        Curve3d::Line { origin, dir } => {
            let (mut t0, mut t1) = (f64::NEG_INFINITY, f64::INFINITY);
            for k in 0..3 {
                if dir[k].abs() < 1e-15 {
                    if origin[k] < lo[k] || origin[k] > hi[k] {
                        return None;
                    }
                    continue;
                }
                let (a, b) = ((lo[k] - origin[k]) / dir[k], (hi[k] - origin[k]) / dir[k]);
                t0 = t0.max(a.min(b));
                t1 = t1.min(a.max(b));
            }
            (t1 - t0 > tolerance).then(|| (vec![curve.point(t0), curve.point(t1)], false))
        }
        Curve3d::Circle { radius: r, .. } | Curve3d::Ellipse { a: r, .. } => {
            let per = 2.0
                * (1.0 - (tolerance * 0.25 / r.max(1e-12)).min(1.0))
                    .acos()
                    .max(1e-6);
            let n = ((TAU / per).ceil() as usize).clamp(12, 2048);
            Some((
                (0..n)
                    .map(|i| curve.point(TAU * i as f64 / n as f64))
                    .collect(),
                true,
            ))
        }
        _ => {
            let n = count(curve);
            (n >= 2).then(|| {
                (
                    (0..n).map(|i| curve.point(i as f64)).collect(),
                    is_closed(curve),
                )
            })
        }
    }
}

/// The stretches of a polyline lying on both patches.
fn clip(poly: &[V3], closed: bool, f: &Patch, g: &Patch, tolerance: f64) -> Vec<Vec<V3>> {
    let mut pts = poly.to_vec();
    if closed && pts.len() > 2 {
        pts.push(pts[0]);
    }
    if pts.len() < 2 {
        return Vec::new();
    }
    let on = |p: V3| f.holds(p, tolerance) && g.holds(p, tolerance);
    let extent = |p: &Patch| (0..3).map(|k| p.hi[k] - p.lo[k]).fold(0.0f64, f64::max);
    let size = extent(f).max(extent(g));
    let mut runs: Vec<Vec<V3>> = Vec::new();
    let mut cur: Vec<V3> = Vec::new();
    let first_on = on(pts[0]);
    let mut prev = (pts[0], first_on);
    if first_on {
        cur.push(pts[0]);
    }
    for w in pts.windows(2) {
        let (a, b) = (w[0], w[1]);
        let m = ((v3::dist(a, b) / (size * 0.02).max(tolerance)).ceil() as usize).clamp(1, 64);
        for j in 1..=m {
            let p = lerp3(a, b, j as f64 / m as f64);
            let s = on(p);
            if s != prev.1 {
                // Where it changes, by halving: against the boundary itself
                // where the two ends disagree about it, so the end lands on
                // the boundary rather than a tolerance beyond it.
                let (mut x, mut y) = (prev.0, p);
                // Close enough to the boundary that the end welds to the point on
                // it; far enough that a run along an edge, a rounding off it,
                // still reads as on.
                let tight = tolerance * 1e-3;
                let exact = |q: V3| {
                    f.holds_within(q, tolerance, tight) && g.holds_within(q, tolerance, tight)
                };
                // Where both ends of the bracket lie within the tolerance of
                // the boundary on the same side of it — a sample can fall
                // just short — the exact boundary is a little beyond one of
                // them: widen the bracket a few tolerances and look there.
                if exact(x) == exact(y) {
                    let dir = v3::sub(y, x);
                    let l = v3::norm(dir);
                    if l > 0.0 {
                        let reach = v3::scale(dir, tolerance * 4.0 / l);
                        let (wx, wy) = (v3::sub(x, reach), v3::add(y, reach));
                        if exact(wx) != exact(y) {
                            x = wx;
                        } else if exact(x) != exact(wy) {
                            y = wy;
                        }
                    }
                }
                let test: &dyn Fn(V3) -> bool = if exact(x) != exact(y) { &exact } else { &on };
                let side = test(x);
                for _ in 0..50 {
                    let mid = lerp3(x, y, 0.5);
                    if test(mid) == side {
                        x = mid;
                    } else {
                        y = mid;
                    }
                }
                if prev.1 {
                    cur.push(x);
                    runs.push(std::mem::take(&mut cur));
                } else {
                    cur = vec![y];
                }
            }
            if j == m && s {
                cur.push(b);
            }
            prev = (p, s);
        }
    }
    let whole = runs.is_empty() && cur.len() == pts.len();
    if cur.len() >= 2 {
        runs.push(cur);
    }
    if closed && !whole && first_on && runs.len() >= 2 {
        let last = runs.pop().unwrap();
        let mut joined = last;
        joined.extend(runs[0].iter().skip(1).copied());
        runs[0] = joined;
    }
    runs.retain(|r| r.windows(2).map(|w| v3::dist(w[0], w[1])).sum::<f64>() > tolerance);
    // A stretch along a ring of either face is that ring, with its points.
    for run in &mut runs {
        if std::env::var_os("THREERS_BREP_DEBUG").is_some() {
            let snaps: Vec<bool> = [f, g]
                .iter()
                .map(|p| {
                    p.rings3
                        .iter()
                        .any(|r| along_ring(run, r, tolerance).is_some())
                })
                .collect();
            eprintln!(
                "  [fuse] run of {} points, closes {}, along a ring of {} {:?} / {} {:?}",
                run.len(),
                v3::dist(run[0], run[run.len() - 1]) <= tolerance,
                f.surface.kind(),
                snaps[0],
                g.surface.kind(),
                snaps[1]
            );
        }
        for p in [f, g] {
            if let Some(along) = p
                .rings3
                .iter()
                .find_map(|ring| along_ring(run, ring, tolerance))
            {
                *run = along;
                break;
            }
        }
    }
    runs
}

/// When `run` lies along the closed polyline `ring`, the ring's own points
/// between the run's ends, in the run's direction.
fn along_ring(run: &[V3], ring: &[V3], tolerance: f64) -> Option<Vec<V3>> {
    let n = ring.len();
    if n < 2 || run.len() < 2 {
        return None;
    }
    let mut closed = ring.to_vec();
    closed.push(ring[0]);
    let off = |p: V3| to_polyline(p, &closed) > tolerance;
    // Every point and every middle: a chord across the ring's corner is not
    // along it.
    if run.iter().any(|p| off(*p)) || run.windows(2).any(|w| off(lerp3(w[0], w[1], 0.5))) {
        return None;
    }
    // Position along the ring, as segment index plus fraction.
    let place = |p: V3| -> f64 {
        let mut best = (f64::INFINITY, 0.0);
        for i in 0..n {
            let (a, b) = (ring[i], ring[(i + 1) % n]);
            let ab = v3::sub(b, a);
            let l = v3::dot(ab, ab);
            let t = if l > 0.0 {
                (v3::dot(v3::sub(p, a), ab) / l).clamp(0.0, 1.0)
            } else {
                0.0
            };
            let d = v3::dist(p, v3::add(a, v3::scale(ab, t)));
            if d < best.0 {
                best = (d, i as f64 + t);
            }
        }
        best.1
    };
    let nf = n as f64;
    let (s, e) = (place(run[0]), place(run[run.len() - 1]));
    // Which way the run goes round: by where its middle is.
    let mid = place(lerp3(
        run[run.len() / 2 - usize::from(run.len() == 2)],
        run[run.len() / 2],
        0.5,
    ));
    let closes = v3::dist(run[0], run[run.len() - 1]) <= tolerance;
    if closes {
        // A closed run along part of a ring — a band's rim, which in the
        // band's outline runs from one side of its seam to the other — is
        // the stretch of the ring that stays on it, each way from where
        // the run starts. Going round the whole ring would take the seam
        // and the other rim with it.
        let on = |p: V3| to_polyline(p, run) <= tolerance;
        let idx = |k: f64| (k.rem_euclid(nf) as usize) % n;
        let mut ahead = Vec::new();
        let mut k = s.floor() + 1.0;
        while ahead.len() < n && on(ring[idx(k)]) {
            ahead.push(ring[idx(k)]);
            k += 1.0;
        }
        if ahead.len() >= n {
            // The ring is the run: all of it, from where the run starts.
            let mut out = vec![run[0]];
            out.extend(ahead);
            out.push(run[run.len() - 1]);
            return Some(out);
        }
        let mut behind = Vec::new();
        let mut k = s.ceil() - 1.0;
        while behind.len() + ahead.len() < n && on(ring[idx(k)]) {
            behind.push(ring[idx(k)]);
            k -= 1.0;
        }
        behind.reverse();
        let mut out = behind;
        out.push(run[0]);
        out.extend(ahead);
        out.dedup_by(|a, b| v3::dist(*a, *b) <= tolerance * 1e-6);
        return (out.len() >= 2).then_some(out);
    }
    let forward = if closes {
        (mid - s).rem_euclid(nf) < nf * 0.5
    } else {
        (mid - s).rem_euclid(nf) <= (e - s).rem_euclid(nf)
    };
    let span = if closes {
        nf
    } else if forward {
        (e - s).rem_euclid(nf)
    } else {
        (s - e).rem_euclid(nf)
    };
    let mut out = vec![run[0]];
    let mut k = if forward {
        s.floor() + 1.0
    } else {
        s.ceil() - 1.0
    };
    loop {
        let d = if forward { k - s } else { s - k };
        if d >= span - 1e-9 {
            break;
        }
        if d > 1e-9 {
            out.push(ring[(k.rem_euclid(nf) as usize) % n]);
        }
        k += if forward { 1.0 } else { -1.0 };
    }
    out.push(run[run.len() - 1]);
    // The ring's points taken must be on the run: a closed run along one
    // side of a ring is not the whole ring.
    if out.iter().any(|p| to_polyline(*p, run) > tolerance) {
        return None;
    }
    Some(out)
}

enum Touch {
    Crosses,
    AllAlong,
    Somewhere,
}

/// Do the two surfaces cross along this run, or touch?
///
/// The sine between their normals is near zero where they touch, and the
/// way they lean across the run changes sign at a touch lying between two
/// samples, which no threshold on either sample sees: at twice the size, the
/// same graze of two equal cylinders fell between samples and was missed.
fn touch(run: &[V3], f: &Patch, g: &Patch) -> Touch {
    let normal = |p: &Patch, x: V3| {
        p.uv(x)
            .and_then(|q| p.surface.normal(q[0] / p.scale[0], q[1] / p.scale[1]))
    };
    let mut flat_all_along = run.len() >= 2;
    let mut before: Option<(f64, f64)> = None;
    for w in run.windows(2) {
        let (Some(a), Some(b)) = (normal(f, w[0]), normal(g, w[0])) else {
            before = None;
            flat_all_along = false;
            continue;
        };
        let x = v3::cross(a, b);
        let sine = v3::norm(x);
        let lean = v3::dot(x, v3::sub(w[1], w[0]));
        if sine >= 1e-6 {
            flat_all_along = false;
        }
        if sine < 1e-2 {
            before = Some((sine, lean));
            continue;
        }
        if let Some((was, dir)) = before
            && dir * lean < 0.0
            && was.min(sine) < 0.2
        {
            return Touch::Somewhere;
        }
        before = Some((sine, lean));
    }
    if flat_all_along {
        return Touch::AllAlong;
    }
    let touches = run.iter().any(|x| {
        matches!((normal(f, *x), normal(g, *x)), (Some(a), Some(b)) if v3::norm(v3::cross(a, b)) < 1e-2)
    });
    if touches {
        Touch::Somewhere
    } else {
        Touch::Crosses
    }
}

/// One face's ring handed to another on the same surface, as stretches,
/// each stretch that runs along a ring of the other taking that ring's own
/// points. The two solids sample one circle each their own way, and laid
/// side by side the two polygons leave slivers between them.
fn onto_rings(ring: &[V3], to: &Patch, tolerance: f64) -> Vec<Vec<V3>> {
    let n = ring.len();
    let near = |p: V3| {
        to.rings3.iter().any(|r| {
            let mut c = r.clone();
            c.push(r[0]);
            to_polyline(p, &c) <= tolerance
        })
    };
    // Whether each side lies along the other's rings.
    let along: Vec<bool> = (0..n)
        .map(|i| {
            let (a, b) = (ring[i], ring[(i + 1) % n]);
            near(a) && near(b) && near(lerp3(a, b, 0.5))
        })
        .collect();
    let mut closed = ring.to_vec();
    closed.push(ring[0]);
    if along.iter().all(|&x| !x) {
        return vec![closed];
    }
    if along.iter().all(|&x| x) {
        // Wholly along one of the other's rings: nothing new to lay down.
        return Vec::new();
    }
    // Stretches between changes, starting at one.
    let start = (0..n)
        .find(|&i| along[i] != along[(i + n - 1) % n])
        .unwrap_or(0);
    let mut out = Vec::new();
    let mut i = start;
    let mut run = vec![ring[i]];
    let mut kind = along[i];
    for _ in 0..n {
        let next = (i + 1) % n;
        run.push(ring[next]);
        i = next;
        if along[i] != kind || i == start {
            if !kind {
                out.push(std::mem::take(&mut run));
            } else {
                // Along the other's ring: its own points, between the ends.
                let snapped = to
                    .rings3
                    .iter()
                    .find_map(|r| along_ring(&run, r, tolerance));
                out.push(snapped.unwrap_or_else(|| run.clone()));
                run.clear();
            }
            run = vec![ring[i]];
            kind = along[i];
        }
    }
    out
}

fn section(f: &Patch, g: &Patch, tolerance: f64) -> Section {
    if !boxes_meet((f.lo, f.hi), (g.lo, g.hi), tolerance) {
        return Section::Apart;
    }
    if f.plane.is_some() && g.plane.is_some() {
        return plane_section(f, g, tolerance);
    }
    let lo = [0, 1, 2].map(|k| f.lo[k].max(g.lo[k]));
    let hi = [0, 1, 2].map(|k| f.hi[k].min(g.hi[k]));
    let curves = match ssi(&f.surface, &g.surface) {
        SsiResult::Disjoint => return Section::Apart,
        SsiResult::Coincident { opposite } => {
            return Section::Wall(!(opposite ^ (f.flipped != g.flipped)));
        }
        SsiResult::Curves(cs) => cs,
        SsiResult::Unknown => march(&f.surface, &g.surface, (lo, hi), tolerance * 0.25),
    };
    if std::env::var_os("THREERS_BREP_DEBUG").is_some() {
        for c in &curves {
            if let Some((pts, closed)) = polyline(c, lo, hi, tolerance) {
                eprintln!(
                    "  [fuse] {} x {}: {} curve of {} points, closed {closed}, {:?} .. {:?}",
                    f.surface.kind(),
                    g.surface.kind(),
                    c.kind(),
                    pts.len(),
                    pts.first(),
                    pts.last()
                );
            }
        }
    }
    let mut cuts = Vec::new();
    for c in &curves {
        if let Curve3d::Point(p) = c {
            if f.holds(*p, tolerance) && g.holds(*p, tolerance) {
                return Section::Graze;
            }
            continue;
        }
        if let Some((pts, closed)) = polyline(c, lo, hi, tolerance) {
            if std::env::var_os("THREERS_BREP_DEBUG").is_some() {
                let p = pts[0];
                let gap = |x: &Patch| x.uv(p).map(|q| ring_distance(&x.rings2, q));
                eprintln!(
                    "  [fuse] its first point: on {} {} ({:?} from its boundary), on {} {} ({:?})",
                    f.surface.kind(),
                    f.holds(p, tolerance),
                    gap(f),
                    g.surface.kind(),
                    g.holds(p, tolerance),
                    gap(g)
                );
            }
            let runs = clip(&pts, closed, f, g, tolerance);
            if std::env::var_os("THREERS_BREP_DEBUG").is_some() {
                eprintln!(
                    "  [fuse] clipped to {} runs of {:?} points",
                    runs.len(),
                    runs.iter().map(|r| r.len()).collect::<Vec<_>>()
                );
            }
            for run in runs {
                match touch(&run, f, g) {
                    Touch::Crosses => cuts.push(run),
                    // Resting along a ruling: neither face is cut.
                    Touch::AllAlong => {}
                    Touch::Somewhere => return Section::Graze,
                }
            }
        }
    }
    Section::Cuts(cuts)
}

// -- the arrangement of one face ---------------------------------------------

/// Points welded at a tolerance: the same place is the same index.
struct Pool {
    points: Vec<V3>,
    grid: HashMap<[i64; 3], Vec<usize>>,
    tolerance: f64,
}

impl Pool {
    fn new(tolerance: f64) -> Self {
        Self {
            points: Vec::new(),
            grid: HashMap::new(),
            tolerance,
        }
    }

    fn id(&mut self, p: V3) -> usize {
        let k = p.map(|x| (x / self.tolerance).floor() as i64);
        for dx in -1..=1 {
            for dy in -1..=1 {
                for dz in -1..=1 {
                    if let Some(list) = self.grid.get(&[k[0] + dx, k[1] + dy, k[2] + dz]) {
                        for &i in list {
                            if v3::dist(self.points[i], p) <= self.tolerance {
                                return i;
                            }
                        }
                    }
                }
            }
        }
        self.points.push(p);
        let i = self.points.len() - 1;
        self.grid.entry(k).or_default().push(i);
        i
    }
}

/// A vertex of a face's arrangement: which point in space, and where in the
/// face's parameters. One point can be two vertices — the two ends of a seam,
/// the corners at a pole.
#[derive(Clone, Copy)]
struct Node {
    point: usize,
    uv: P2,
}

/// One piece of a face: its outline and holes as nodes, and a point inside it.
struct Cell {
    rings: Vec<Vec<Node>>,
    probe: V3,
}

/// The point of a region furthest from its boundary, from a grid over it.
fn probe_of(rings: &[Vec<P2>], tolerance: f64) -> Option<P2> {
    let (mut lo, mut hi) = ([f64::INFINITY; 2], [f64::NEG_INFINITY; 2]);
    for q in &rings[0] {
        for k in 0..2 {
            lo[k] = lo[k].min(q[k]);
            hi[k] = hi[k].max(q[k]);
        }
    }
    let mut best: Option<(f64, P2)> = None;
    for n in [7usize, 23, 61] {
        for i in 0..n {
            for j in 0..n {
                let q = [
                    lo[0] + (hi[0] - lo[0]) * (i as f64 + 0.5) / n as f64,
                    lo[1] + (hi[1] - lo[1]) * (j as f64 + 0.5) / n as f64,
                ];
                if !in_rings(rings, q) {
                    continue;
                }
                let d = ring_distance(rings, q);
                if best.is_none_or(|b| d > b.0) {
                    best = Some((d, q));
                }
            }
        }
        if best.is_some_and(|b| b.0 > tolerance * 4.0) {
            break;
        }
    }
    best.filter(|b| b.0 > tolerance * 0.1).map(|b| b.1)
}

/// Nodes of one face's arrangement, one per point and place in parameters.
struct Nodes {
    list: Vec<Node>,
    by_point: HashMap<usize, Vec<usize>>,
    tolerance: f64,
}

impl Nodes {
    fn id(&mut self, point: usize, uv: P2) -> usize {
        let list = self.by_point.entry(point).or_default();
        for &n in list.iter() {
            if len2(sub2(self.list[n].uv, uv)) <= self.tolerance * 4.0 {
                return n;
            }
        }
        self.list.push(Node { point, uv });
        list.push(self.list.len() - 1);
        self.list.len() - 1
    }
}

/// Boxes in the plane, filed by the cells of a uniform grid they cover.
struct Grid2 {
    cell: f64,
    cells: HashMap<(i64, i64), Vec<usize>>,
    /// Boxes too big to file, offered to every query.
    big: Vec<usize>,
}

impl Grid2 {
    fn of(boxes: &[(P2, P2)], tolerance: f64) -> Grid2 {
        // Cells about the size of a typical box.
        let mut sizes: Vec<f64> = boxes
            .iter()
            .map(|b| (b.1[0] - b.0[0]).max(b.1[1] - b.0[1]))
            .collect();
        sizes.sort_by(f64::total_cmp);
        let cell = sizes
            .get(sizes.len() / 2)
            .copied()
            .unwrap_or(1.0)
            .max(tolerance * 4.0)
            .max(1e-12);
        let mut grid = Grid2 {
            cell,
            cells: HashMap::new(),
            big: Vec::new(),
        };
        for (i, b) in boxes.iter().enumerate() {
            let (lo, hi) = grid.span(*b, tolerance);
            if (hi.0 - lo.0 + 1) * (hi.1 - lo.1 + 1) > 256 {
                grid.big.push(i);
                continue;
            }
            for x in lo.0..=hi.0 {
                for y in lo.1..=hi.1 {
                    grid.cells.entry((x, y)).or_default().push(i);
                }
            }
        }
        grid
    }

    fn span(&self, b: (P2, P2), pad: f64) -> ((i64, i64), (i64, i64)) {
        let k = |x: f64| (x / self.cell).floor() as i64;
        (
            (k(b.0[0] - pad), k(b.0[1] - pad)),
            (k(b.1[0] + pad), k(b.1[1] + pad)),
        )
    }

    /// Every box filed near `b`, some more than once.
    fn near(&self, b: (P2, P2), pad: f64) -> Vec<usize> {
        let (lo, hi) = self.span(b, pad);
        let mut out = self.big.clone();
        if (hi.0 - lo.0 + 1) * (hi.1 - lo.1 + 1) > 4096 {
            // A box as big as the face: everything.
            let mut all: Vec<usize> = self.cells.values().flatten().copied().collect();
            all.extend(&self.big);
            all.sort_unstable();
            all.dedup();
            return all;
        }
        for x in lo.0..=hi.0 {
            for y in lo.1..=hi.1 {
                if let Some(list) = self.cells.get(&(x, y)) {
                    out.extend(list);
                }
            }
        }
        out
    }
}

/// Cut a patch by its sections and return its pieces.
fn cells(
    f: &Patch,
    index: usize,
    cuts: &[Vec<V3>],
    pool: &mut Pool,
    tolerance: f64,
) -> Result<Vec<Cell>, Declined> {
    let mut nodes = Nodes {
        list: Vec::new(),
        by_point: HashMap::new(),
        tolerance,
    };
    let mut segs: Vec<(usize, usize)> = Vec::new();
    for (pts, uv) in f.rings3.iter().zip(&f.rings2) {
        let ids: Vec<usize> = pts
            .iter()
            .zip(uv)
            .map(|(p, q)| nodes.id(pool.id(*p), *q))
            .collect();
        let n = ids.len();
        for i in 0..n {
            segs.push((ids[i], ids[(i + 1) % n]));
        }
    }
    let pad = tolerance * 2.0;
    for cut in cuts {
        let Some(uv) = f.unwrapped(cut) else { continue };
        let (mut lo, mut hi) = ([f64::INFINITY; 2], [f64::NEG_INFINITY; 2]);
        for q in &uv {
            for k in 0..2 {
                lo[k] = lo[k].min(q[k]);
                hi[k] = hi[k].max(q[k]);
            }
        }
        let ids: Vec<usize> = cut.iter().map(|p| pool.id(*p)).collect();
        for shift in f.copies(lo, hi, tolerance) {
            let moved: Vec<P2> = uv
                .iter()
                .map(|q| [q[0] + shift[0], q[1] + shift[1]])
                .collect();
            let ns: Vec<usize> = ids
                .iter()
                .zip(&moved)
                .map(|(&p, q)| nodes.id(p, *q))
                .collect();
            for (k, w) in ns.windows(2).enumerate() {
                let (a, b) = (moved[k], moved[k + 1]);
                let outside = (0..2).any(|d| {
                    (a[d] < f.dlo[d] - pad && b[d] < f.dlo[d] - pad)
                        || (a[d] > f.dhi[d] + pad && b[d] > f.dhi[d] + pad)
                });
                if !outside {
                    segs.push((w[0], w[1]));
                }
            }
        }
    }
    segs.retain(|(a, b)| a != b);

    // Where segments cross, each crossing a node of both.
    let bbox = |s: (usize, usize), nodes: &Nodes| {
        let (a, b) = (nodes.list[s.0].uv, nodes.list[s.1].uv);
        (
            [a[0].min(b[0]), a[1].min(b[1])],
            [a[0].max(b[0]), a[1].max(b[1])],
        )
    };
    let boxes: Vec<(P2, P2)> = segs.iter().map(|s| bbox(*s, &nodes)).collect();
    let mut splits: Vec<Vec<usize>> = vec![Vec::new(); segs.len()];
    // Pairs whose boxes meet, from a grid over them rather than every pair:
    // a fine tolerance samples a face's curves into thousands of segments.
    let grid = Grid2::of(&boxes, tolerance);
    let mut seen = vec![usize::MAX; segs.len()];
    for i in 0..segs.len() {
        for j in grid.near(boxes[i], tolerance) {
            if j <= i || seen[j] == i {
                continue;
            }
            seen[j] = i;
            let (bi, bj) = (boxes[i], boxes[j]);
            if bi.0[0] > bj.1[0] + tolerance
                || bj.0[0] > bi.1[0] + tolerance
                || bi.0[1] > bj.1[1] + tolerance
                || bj.0[1] > bi.1[1] + tolerance
            {
                continue;
            }
            let (a, b) = (nodes.list[segs[i].0].uv, nodes.list[segs[i].1].uv);
            let (c, d) = (nodes.list[segs[j].0].uv, nodes.list[segs[j].1].uv);
            let (r, s) = (sub2(b, a), sub2(d, c));
            let (lr, ls) = (len2(r), len2(s));
            let denom = cross2(r, s);
            if denom.abs() <= 1e-12 * lr * ls {
                continue;
            }
            let ac = sub2(c, a);
            let t = cross2(ac, s) / denom;
            let u = cross2(ac, r) / denom;
            let (et, eu) = (tolerance / lr, tolerance / ls);
            if t > et && t < 1.0 - et && u > eu && u < 1.0 - eu {
                let q = [a[0] + r[0] * t, a[1] + r[1] * t];
                let p = if f.plane.is_some() {
                    let (pa, pb) = (nodes.list[segs[i].0].point, nodes.list[segs[i].1].point);
                    lerp3(pool.points[pa], pool.points[pb], t)
                } else {
                    f.point(q)
                };
                let id = nodes.id(pool.id(p), q);
                splits[i].push(id);
                splits[j].push(id);
            }
        }
    }
    // And wherever a node lies on a segment: a touch, an overlap, an end.
    let points: Vec<(P2, P2)> = nodes.list.iter().map(|n| (n.uv, n.uv)).collect();
    let near_nodes = Grid2::of(&points, tolerance);
    for (i, &(a, b)) in segs.iter().enumerate() {
        let (pa, pb) = (nodes.list[a].uv, nodes.list[b].uv);
        let l = len2(sub2(pb, pa));
        let bi = boxes[i];
        for v in near_nodes.near(bi, tolerance) {
            if v == a || v == b {
                continue;
            }
            let q = nodes.list[v].uv;
            if q[0] < bi.0[0] - tolerance
                || q[0] > bi.1[0] + tolerance
                || q[1] < bi.0[1] - tolerance
                || q[1] > bi.1[1] + tolerance
            {
                continue;
            }
            let (d, t) = to_segment(q, pa, pb);
            if d <= tolerance && t * l > tolerance && (1.0 - t) * l > tolerance {
                splits[i].push(v);
            }
        }
    }
    let nodes = nodes.list;
    let mut edges: HashSet<(usize, usize)> = HashSet::new();
    for (i, &(a, b)) in segs.iter().enumerate() {
        let pa = nodes[a].uv;
        let dir = sub2(nodes[b].uv, pa);
        let mut along = splits[i].clone();
        along.sort_by(|&x, &y| {
            dot2(sub2(nodes[x].uv, pa), dir).total_cmp(&dot2(sub2(nodes[y].uv, pa), dir))
        });
        along.dedup();
        let chain: Vec<usize> = std::iter::once(a)
            .chain(along)
            .chain(std::iter::once(b))
            .collect();
        for w in chain.windows(2) {
            if w[0] != w[1] {
                edges.insert((w[0].min(w[1]), w[0].max(w[1])));
            }
        }
    }
    // A cut that ends off the face — a wall's outline reaching past it —
    // bounds nothing there. One that ends *inside* the face is a section
    // that stopped short, a trace that could not get through a pinch, and
    // the pieces either side of it are not pieces at all.
    loop {
        let mut degree: HashMap<usize, usize> = HashMap::new();
        for &(a, b) in &edges {
            *degree.entry(a).or_default() += 1;
            *degree.entry(b).or_default() += 1;
        }
        for (&v, &d) in &degree {
            let q = nodes[v].uv;
            if d == 1 && in_rings(&f.rings2, q) && ring_distance(&f.rings2, q) > tolerance * 4.0 {
                if std::env::var_os("THREERS_BREP_DEBUG").is_some() {
                    let nearest = nodes
                        .iter()
                        .enumerate()
                        .filter(|(w, _)| *w != v)
                        .map(|(_, n)| len2(sub2(n.uv, q)))
                        .fold(f64::INFINITY, f64::min);
                    eprintln!(
                        "  [fuse] {} face {index}: a cut ends inside at {:?} ({:.3e} from its boundary, {nearest:.3e} from the nearest other point, tolerance {tolerance:.3e})",
                        f.surface.kind(),
                        pool.points[nodes[v].point],
                        ring_distance(&f.rings2, q)
                    );
                }
                return Err(Declined::NeedsArrangement { face: index });
            }
        }
        let before = edges.len();
        edges.retain(|(a, b)| degree[a] > 1 && degree[b] > 1);
        if edges.len() == before {
            break;
        }
    }

    // The graph's faces, by the tightest turn at each vertex.
    let mut edges: Vec<(usize, usize)> = edges.into_iter().collect();
    edges.sort_unstable();
    let mut half: Vec<(usize, usize)> = Vec::with_capacity(edges.len() * 2);
    for &(a, b) in &edges {
        half.push((a, b));
        half.push((b, a));
    }
    let angle = |h: usize| {
        let d = sub2(nodes[half[h].1].uv, nodes[half[h].0].uv);
        d[1].atan2(d[0])
    };
    let mut out_of: HashMap<usize, Vec<usize>> = HashMap::new();
    for (h, &(from, _)) in half.iter().enumerate() {
        out_of.entry(from).or_default().push(h);
    }
    for list in out_of.values_mut() {
        list.sort_by(|&x, &y| angle(x).total_cmp(&angle(y)));
    }
    let mut seen = vec![false; half.len()];
    let mut cycles: Vec<Vec<usize>> = Vec::new();
    for start in 0..half.len() {
        if seen[start] {
            continue;
        }
        let mut cycle = Vec::new();
        let mut h = start;
        while !seen[h] {
            seen[h] = true;
            cycle.push(half[h].0);
            let back = h ^ 1;
            let list = &out_of[&half[back].0];
            let pos = list.iter().position(|&x| x == back).unwrap_or(0);
            h = list[(pos + list.len() - 1) % list.len()];
        }
        if cycle.len() >= 3 {
            cycles.push(cycle);
        }
    }

    let tiny = tolerance * tolerance;
    let mut faces: Vec<(Vec<usize>, Vec<P2>, f64)> = Vec::new();
    let mut outlines: Vec<Vec<usize>> = Vec::new();
    for c in cycles {
        let ring: Vec<P2> = c.iter().map(|&i| nodes[i].uv).collect();
        let area = signed_area(&ring);
        if area > tiny {
            faces.push((c, ring, area));
        } else if area < -tiny {
            outlines.push(c);
        }
    }
    // An outline is a hole of the smallest piece around it.
    let mut holes: Vec<Vec<Vec<usize>>> = vec![Vec::new(); faces.len()];
    for o in outlines {
        let p = nodes[o[0]].uv;
        let around = faces
            .iter()
            .enumerate()
            .filter(|(_, face)| {
                let r = std::slice::from_ref(&face.1);
                in_rings(r, p) && ring_distance(r, p) > tolerance
            })
            .min_by(|x, y| x.1 .2.total_cmp(&y.1 .2))
            .map(|(i, _)| i);
        if let Some(i) = around {
            holes[i].push(o);
        }
    }
    let mut out = Vec::new();
    for ((outer, ring, _), holes) in faces.into_iter().zip(holes) {
        let mut rings2 = vec![ring];
        rings2.extend(
            holes
                .iter()
                .map(|h| h.iter().map(|&i| nodes[i].uv).collect::<Vec<P2>>()),
        );
        let Some(q) = probe_of(&rings2, tolerance) else {
            continue;
        };
        if !in_rings(&f.rings2, q) || ring_distance(&f.rings2, q) <= tolerance * 0.5 {
            continue;
        }
        let rings = std::iter::once(&outer)
            .chain(&holes)
            .map(|r| r.iter().map(|&i| nodes[i]).collect())
            .collect();
        out.push(Cell {
            rings,
            probe: f.point(q),
        });
    }
    Ok(out)
}

// -- classification ----------------------------------------------------------

/// Is `p` inside the solid bounded by these flat patches? By the parity of a
/// ray, cast again in another direction when it grazes an edge.
fn inside_flat(p: V3, faces: &[Patch], tolerance: f64) -> Option<bool> {
    // Directions no model is likely to line an edge up with.
    const DIRECTIONS: [V3; 5] = [
        [0.531_9, 0.627_3, 0.569_1],
        [-0.419_7, 0.733_1, 0.534_2],
        [0.313_7, -0.681_9, 0.660_3],
        [-0.645_1, -0.359_3, -0.674_3],
        [0.152_9, 0.297_1, -0.942_5],
    ];
    'direction: for d in DIRECTIONS {
        let d = v3::normalize(d)?;
        let mut crossings = 0;
        for f in faces {
            let (o, n) = f.plane?;
            let dn = v3::dot(n, d);
            let dist = v3::dot(n, v3::sub(p, o));
            if dn.abs() <= 1e-9 {
                if dist.abs() <= tolerance && f.holds(p, tolerance) {
                    continue 'direction;
                }
                continue;
            }
            let t = -dist / dn;
            if t <= 0.0 {
                continue;
            }
            let Some(q) = f.uv(v3::add(p, v3::scale(d, t))) else {
                continue 'direction;
            };
            if ring_distance(&f.rings2, q) <= tolerance {
                continue 'direction;
            }
            if in_rings(&f.rings2, q) {
                crossings += 1;
            }
        }
        return Some(crossings % 2 == 1);
    }
    None
}

/// Where the ray `p + t·d` (`d` unit) meets a surface, as values of `t`;
/// `None` for a surface without a closed form for it.
fn ray_hits(surface: &Surface, p: V3, d: V3) -> Option<Vec<f64>> {
    // Roots of a·t² + b·t + c.
    let roots = |a: f64, b: f64, c: f64| -> Vec<f64> {
        if a.abs() <= 1e-15 {
            return if b.abs() <= 1e-15 {
                Vec::new()
            } else {
                vec![-c / b]
            };
        }
        let disc = b * b - 4.0 * a * c;
        if disc < 0.0 {
            return Vec::new();
        }
        let r = disc.sqrt();
        // The form that loses no digits to cancellation.
        let q = -0.5 * (b + b.signum() * r);
        let mut out = vec![q / a];
        if q != 0.0 {
            out.push(c / q);
        }
        out
    };
    // The part of `v` square to `axis`.
    let flat = |v: V3, axis: V3| v3::sub(v, v3::scale(axis, v3::dot(v, axis)));
    Some(match surface {
        Surface::Plane { origin, normal, .. } => {
            let dn = v3::dot(*normal, d);
            if dn.abs() <= 1e-15 {
                Vec::new()
            } else {
                vec![v3::dot(*normal, v3::sub(*origin, p)) / dn]
            }
        }
        Surface::Cylinder {
            origin,
            axis,
            radius,
            ..
        } => {
            let (w, dp) = (flat(v3::sub(p, *origin), *axis), flat(d, *axis));
            roots(
                v3::dot(dp, dp),
                2.0 * v3::dot(w, dp),
                v3::dot(w, w) - radius * radius,
            )
        }
        Surface::Sphere { center, radius, .. } => {
            let w = v3::sub(p, *center);
            roots(1.0, 2.0 * v3::dot(w, d), v3::dot(w, w) - radius * radius)
        }
        Surface::Cone {
            apex,
            axis,
            half_angle,
            ..
        } => {
            // |radial|² = (h·tan α)², both nappes; the face's region keeps
            // the one it is on.
            let w = v3::sub(p, *apex);
            let (h, dh) = (v3::dot(w, *axis), v3::dot(d, *axis));
            let (wr, dr) = (flat(w, *axis), flat(d, *axis));
            let k = half_angle.tan().powi(2);
            roots(
                v3::dot(dr, dr) - k * dh * dh,
                2.0 * (v3::dot(wr, dr) - k * h * dh),
                v3::dot(wr, wr) - k * h * h,
            )
        }
        _ => return None,
    })
}

/// Is `p` inside the solid bounded by these patches? By the parity of a ray
/// against the surfaces themselves, cast again in another direction when it
/// grazes a face or passes by the edge of one. `None` where a surface has no
/// closed form, or no direction is clean.
fn inside_exact(p: V3, faces: &[Patch], tolerance: f64) -> Option<bool> {
    const DIRECTIONS: [V3; 7] = [
        [0.531_9, 0.627_3, 0.569_1],
        [-0.419_7, 0.733_1, 0.534_2],
        [0.313_7, -0.681_9, 0.660_3],
        [-0.645_1, -0.359_3, -0.674_3],
        [0.152_9, 0.297_1, -0.942_5],
        [0.871_3, -0.211_7, -0.442_6],
        [-0.236_1, -0.918_4, 0.317_9],
    ];
    'direction: for d in DIRECTIONS {
        let d = v3::normalize(d)?;
        let mut crossings = 0;
        for f in faces {
            for t in ray_hits(&f.surface, p, d)? {
                if t <= -tolerance {
                    continue;
                }
                let h = v3::add(p, v3::scale(d, t));
                let q = f.uv(h)?;
                let images = f.images(q);
                let near = images
                    .iter()
                    .any(|s| ring_distance(&f.rings2, *s) <= tolerance);
                let inside = images.iter().any(|s| in_rings(&f.rings2, *s));
                if !inside && !near {
                    continue;
                }
                // On the surface where the ray starts, by an edge of a face,
                // or along a face: no count is to be trusted.
                if t <= tolerance || near {
                    continue 'direction;
                }
                let normal = f.surface.normal(q[0] / f.scale[0], q[1] / f.scale[1]);
                if normal.is_none_or(|n| v3::dot(n, d).abs() <= 1e-6) {
                    continue 'direction;
                }
                crossings += 1;
            }
        }
        return Some(crossings % 2 == 1);
    }
    None
}

enum Solid<'a> {
    Flat(&'a [Patch]),
    /// Bounded by surfaces a ray meets in closed form.
    Exact(&'a [Patch]),
    Mesh(Vec<[V3; 3]>),
}

impl Solid<'_> {
    fn contains(&self, p: V3, tolerance: f64) -> Option<bool> {
        match self {
            Solid::Flat(faces) => inside_flat(p, faces, tolerance),
            Solid::Exact(faces) => inside_exact(p, faces, tolerance),
            Solid::Mesh(tris) => Some(point_in_mesh(tris, p)),
        }
    }
}

// -- the operation -----------------------------------------------------------

fn is_flat(x: &Body) -> bool {
    x.faces()
        .iter()
        .all(|f| x.surfaces()[f.surface].kind() == "plane")
}

/// A boolean through the general fuse, or `None` when a face of either solid
/// cannot be laid out as a region of its surface.
pub fn general(
    a: &Body,
    b: &Body,
    op: BooleanOp,
    tolerance: f64,
) -> Option<Result<Body, Declined>> {
    // Between flat faces nothing is sampled, so what the tolerance is for —
    // a chord's sag — does not arise, and welding at it only merges what is
    // thinner than it: a wall's 4 mm membrane cut at a tolerance of 4.7 came
    // back a solid of the wrong volume. Points there are as exact as the
    // arithmetic, and as the inputs' own vertices agree.
    let size = {
        let (mut lo, mut hi) = ([f64::INFINITY; 3], [f64::NEG_INFINITY; 3]);
        for p in a.vertices().iter().chain(b.vertices()) {
            for k in 0..3 {
                lo[k] = lo[k].min(p[k]);
                hi[k] = hi[k].max(p[k]);
            }
        }
        (0..3)
            .map(|k| hi[k] - lo[k])
            .fold(0.0f64, f64::max)
            .max(tolerance)
    };
    let tolerance = if is_flat(a) && is_flat(b) {
        // A millionth of the model: fine enough to keep any feature, coarse
        // enough for inputs whose vertices agree only as far as single
        // precision carried them — a face at y = 2199.99949 against one at
        // 2200, five ten-thousandths apart on a model five metres across.
        tolerance.min((size * 1e-6).max(1e-9))
    } else {
        // Curved faces are sampled, and the tolerance is what sampling is
        // held to and what points are welded at alike: anything thinner is
        // merged. A caller wanting a building's millimetres asks for them.
        tolerance
    };
    // Curved faces are laid out from their edges, so the edges first need to
    // be as close to the curves as the tolerance asks.
    let refined = |x: &Body| {
        let mut x = x.clone();
        if !is_flat(&x) {
            x.refine_edges(tolerance * 0.25);
        }
        x
    };
    let (ra, rb) = (refined(a), refined(b));
    let pa = patches(&ra, tolerance)?;
    let pb = patches(&rb, tolerance)?;
    let origins = Origins::of(&[&ra, &rb], tolerance);
    fn solid<'a>(x: &Body, p: &'a [Patch], tolerance: f64, size: f64) -> Option<Solid<'a>> {
        if is_flat(x) {
            return Some(Solid::Flat(p));
        }
        if p.iter().all(|f| {
            matches!(
                f.surface,
                Surface::Plane { .. }
                    | Surface::Cylinder { .. }
                    | Surface::Sphere { .. }
                    | Surface::Cone { .. }
            )
        }) {
            return Some(Solid::Exact(p));
        }
        // A closed mesh to cast rays at, however coarse it takes: one that
        // does not close has no inside, and every point reads as outside it.
        // A body cut several times over can fill its faces at a few
        // ten-thousandths of its size and not at a millionth.
        [tolerance, tolerance * 10.0, tolerance * 100.0, size * 1e-3]
            .into_iter()
            .map(|t| x.solid_triangles(t))
            .find(|tris| !tris.is_empty())
            .map(Solid::Mesh)
    }
    let Some(sa) = solid(&ra, &pa, tolerance, size) else {
        return Some(Err(Declined::NotWatertight { open_edges: 0 }));
    };
    let Some(sb) = solid(&rb, &pb, tolerance, size) else {
        return Some(Err(Declined::NotWatertight { open_edges: 0 }));
    };
    Some(fuse(&pa, &pb, &sa, &sb, &origins, op, tolerance))
}

/// A boolean of two solids bounded by planes alone, or `None` when either
/// has a face that is not flat.
pub fn planar(a: &Body, b: &Body, op: BooleanOp, tolerance: f64) -> Option<Result<Body, Declined>> {
    if !is_flat(a) || !is_flat(b) {
        return None;
    }
    general(a, b, op, tolerance)
}

fn fuse(
    pa: &[Patch],
    pb: &[Patch],
    sa: &Solid,
    sb: &Solid,
    origins: &Origins,
    op: BooleanOp,
    tolerance: f64,
) -> Result<Body, Declined> {
    let na = pa.len();
    let all: Vec<&Patch> = pa.iter().chain(pb).collect();
    let mut cuts: Vec<Vec<Vec<V3>>> = vec![Vec::new(); all.len()];
    // Patches of the other solid each one shares its surface with, and
    // whether the two face the same way.
    let mut walls: Vec<Vec<(usize, bool)>> = vec![Vec::new(); all.len()];

    for i in 0..na {
        for j in na..all.len() {
            match section(all[i], all[j], tolerance) {
                Section::Apart => {}
                Section::Graze => {
                    return Err(Declined::TangentialContact {
                        face_a: i,
                        face_b: j - na,
                    });
                }
                Section::Wall(same) => {
                    if std::env::var_os("THREERS_BREP_DEBUG").is_some() {
                        eprintln!(
                            "  [fuse] faces {i} and {j} share a {} (same way {same})",
                            all[i].surface.kind()
                        );
                    }
                    walls[i].push((j, same));
                    walls[j].push((i, same));
                    for (from, to) in [(j, i), (i, j)] {
                        for ring in &all[from].rings3 {
                            cuts[to].extend(onto_rings(ring, all[to], tolerance));
                        }
                    }
                }
                Section::Cuts(cs) => {
                    for c in cs {
                        cuts[i].push(c.clone());
                        cuts[j].push(c);
                    }
                }
            }
        }
    }

    let mut pool = Pool::new(tolerance);
    // Input vertices first, so a point both solids have is theirs.
    for f in &all {
        for p in f.rings3.iter().flatten() {
            pool.id(*p);
        }
    }
    let mut kept: Vec<Kept> = Vec::new();
    for (i, f) in all.iter().enumerate() {
        let from_a = i < na;
        let other = if from_a { sb } else { sa };
        let pieces = cells(f, i, &cuts[i], &mut pool, tolerance)?;
        if std::env::var_os("THREERS_BREP_DEBUG").is_some() && !f.plane.is_some() {
            eprintln!(
                "  [fuse] {} face {i} of {}: {} pieces from {} cuts",
                f.surface.kind(),
                if from_a { "A" } else { "B" },
                pieces.len(),
                cuts[i].len()
            );
        }
        for cell in pieces {
            let on_wall = walls[i]
                .iter()
                .find_map(|&(j, same)| all[j].interior(cell.probe, tolerance).then_some(same));
            let (keep, flip) = match on_wall {
                Some(same) => match (op, from_a, same) {
                    (BooleanOp::Union, true, true) | (BooleanOp::Intersection, true, true) => {
                        (true, false)
                    }
                    (BooleanOp::Difference, true, false) => (true, false),
                    _ => (false, false),
                },
                None => {
                    let within = other
                        .contains(cell.probe, tolerance)
                        .ok_or(Declined::UnclassifiablePiece { face: i })?;
                    match (op, from_a) {
                        (BooleanOp::Union, _) => (!within, false),
                        (BooleanOp::Intersection, _) => (within, false),
                        (BooleanOp::Difference, true) => (!within, false),
                        (BooleanOp::Difference, false) => (within, true),
                    }
                }
            };
            if std::env::var_os("THREERS_BREP_DEBUG").is_some() && !from_a && f.plane.is_none() {
                eprintln!(
                    "  [fuse] piece of {} face {i}: probe {:?} on a wall {:?} keep {keep}",
                    f.surface.kind(),
                    cell.probe,
                    on_wall
                );
            }
            if keep {
                kept.push(Kept {
                    patch: i,
                    flip,
                    rings: cell.rings,
                });
            }
        }
    }
    let kept = rejoin_seams(&all, kept, tolerance);
    assemble(&all, kept, &pool, origins, tolerance)
}

struct Kept {
    patch: usize,
    flip: bool,
    rings: Vec<Vec<Node>>,
}

/// A side of a piece: piece, ring, index in the ring.
type SideAt = (usize, usize, usize);

/// Pieces of a wrapping face that meet across its seam are one face again.
///
/// Each piece is moved by whole periods so that it continues its neighbour
/// across the seam, and the seam sides the two then share are dropped. A
/// piece that reaches itself that way goes all the way round: it keeps the
/// seam, which it then uses twice, as OpenCASCADE has a cylinder's wall.
fn rejoin_seams(all: &[&Patch], kept: Vec<Kept>, tolerance: f64) -> Vec<Kept> {
    let mut out: Vec<Kept> = Vec::new();
    let mut by_patch: HashMap<usize, Vec<Kept>> = HashMap::new();
    for k in kept {
        if all[k.patch].wraps.iter().any(|&w| w) {
            by_patch.entry(k.patch).or_default().push(k);
        } else {
            out.push(k);
        }
    }
    let mut order: Vec<usize> = by_patch.keys().copied().collect();
    order.sort_unstable();
    for pi in order {
        let f = all[pi];
        let cells = by_patch.remove(&pi).unwrap();
        // Which seam a node is on: (direction, low side?).
        let side = |q: P2| -> Option<(usize, bool)> {
            (0..2).find_map(|k| {
                if !f.wraps[k] {
                    None
                } else if (q[k] - f.dlo[k]).abs() <= tolerance {
                    Some((k, true))
                } else if (q[k] - f.dhi[k]).abs() <= tolerance {
                    Some((k, false))
                } else {
                    None
                }
            })
        };
        // Seam sides, by the points they join.
        let mut low: HashMap<(usize, usize, usize), SideAt> = HashMap::new();
        let mut high: Vec<(usize, SideAt, (usize, usize))> = Vec::new();
        for (c, cell) in cells.iter().enumerate() {
            for (r, ring) in cell.rings.iter().enumerate() {
                let n = ring.len();
                for i in 0..n {
                    let (a, b) = (ring[i], ring[(i + 1) % n]);
                    match (side(a.uv), side(b.uv)) {
                        (Some((k, true)), Some((k2, true))) if k == k2 => {
                            low.insert((k, a.point, b.point), (c, r, i));
                        }
                        (Some((k, false)), Some((k2, false))) if k == k2 => {
                            high.push((k, (c, r, i), (a.point, b.point)));
                        }
                        _ => {}
                    }
                }
            }
        }
        // Offsets in whole periods, by a walk across matched seam sides. The
        // low side of the neighbour runs the other way.
        let mut links: Vec<Vec<Link>> = vec![Vec::new(); cells.len()];
        for &(k, mine, (pa, pb)) in &high {
            if let Some(&theirs) = low.get(&(k, pb, pa)) {
                links[mine.0].push((theirs.0, k, 1, mine, theirs));
                links[theirs.0].push((mine.0, k, -1, theirs, mine));
            }
        }
        let mut offset: Vec<Option<[i64; 2]>> = vec![None; cells.len()];
        let mut dropped: HashSet<SideAt> = HashSet::new();
        for start in 0..cells.len() {
            if offset[start].is_some() {
                continue;
            }
            offset[start] = Some([0, 0]);
            let mut queue = vec![start];
            while let Some(c) = queue.pop() {
                let oc = offset[c].unwrap();
                for &(d, k, step, mine, theirs) in &links[c] {
                    let mut od = oc;
                    od[k] += step;
                    match offset[d] {
                        None => {
                            offset[d] = Some(od);
                            queue.push(d);
                            dropped.insert(mine);
                            dropped.insert(theirs);
                        }
                        Some(x) if x == od => {
                            dropped.insert(mine);
                            dropped.insert(theirs);
                        }
                        Some(_) => {}
                    }
                }
            }
        }
        // Group the pieces that were joined.
        let mut group: Vec<usize> = (0..cells.len()).collect();
        fn find(g: &mut [usize], mut x: usize) -> usize {
            while g[x] != x {
                g[x] = g[g[x]];
                x = g[x];
            }
            x
        }
        for (c, list) in links.iter().enumerate() {
            for &(d, _, _, mine, _) in list {
                if dropped.contains(&mine) {
                    let (x, y) = (find(&mut group, c), find(&mut group, d));
                    group[x] = y;
                }
            }
        }
        let mut members: HashMap<usize, Vec<usize>> = HashMap::new();
        for c in 0..cells.len() {
            let g = find(&mut group, c);
            members.entry(g).or_default().push(c);
        }
        let mut roots: Vec<usize> = members.keys().copied().collect();
        roots.sort_unstable();
        for g in roots {
            let list = &members[&g];
            let touched = list.iter().any(|&c| {
                cells[c]
                    .rings
                    .iter()
                    .enumerate()
                    .any(|(r, ring)| (0..ring.len()).any(|i| dropped.contains(&(c, r, i))))
            });
            if !touched {
                let c = list[0];
                out.push(Kept {
                    patch: pi,
                    flip: cells[c].flip,
                    rings: cells[c].rings.clone(),
                });
                continue;
            }
            // Directed sides, moved by their piece's offset, walked into
            // rings again.
            let mut sides: Vec<(Node, Node)> = Vec::new();
            for &c in list {
                let o = offset[c].unwrap();
                let shift = [
                    o[0] as f64 * f.period[0].unwrap_or(0.0),
                    o[1] as f64 * f.period[1].unwrap_or(0.0),
                ];
                let mv = |x: Node| Node {
                    point: x.point,
                    uv: [x.uv[0] + shift[0], x.uv[1] + shift[1]],
                };
                for (r, ring) in cells[c].rings.iter().enumerate() {
                    let n = ring.len();
                    for i in 0..n {
                        if !dropped.contains(&(c, r, i)) {
                            sides.push((mv(ring[i]), mv(ring[(i + 1) % n])));
                        }
                    }
                }
            }
            let mut used = vec![false; sides.len()];
            let mut rings: Vec<Vec<Node>> = Vec::new();
            for s in 0..sides.len() {
                if used[s] {
                    continue;
                }
                let mut ring = Vec::new();
                let mut at = s;
                loop {
                    used[at] = true;
                    ring.push(sides[at].0);
                    let end = sides[at].1;
                    let next = (0..sides.len()).find(|&t| {
                        !used[t]
                            && sides[t].0.point == end.point
                            && len2(sub2(sides[t].0.uv, end.uv)) <= tolerance * 4.0
                    });
                    match next {
                        Some(t) => at = t,
                        None => break,
                    }
                }
                if ring.len() >= 3 {
                    rings.push(ring);
                }
            }
            // The outline first.
            let area = |r: &Vec<Node>| signed_area(&r.iter().map(|n| n.uv).collect::<Vec<_>>());
            rings.sort_by(|x, y| area(y).total_cmp(&area(x)));
            out.push(Kept {
                patch: pi,
                flip: cells[list[0]].flip,
                rings,
            });
        }
    }
    out
}

/// The kept pieces as a body.
fn assemble(
    all: &[&Patch],
    kept: Vec<Kept>,
    pool: &Pool,
    origins: &Origins,
    tolerance: f64,
) -> Result<Body, Declined> {
    let pts = &pool.points;
    let used: Vec<usize> = {
        let mut u: Vec<usize> = kept
            .iter()
            .flat_map(|k| k.rings.iter().flatten().map(|n| n.point))
            .collect();
        u.sort_unstable();
        u.dedup();
        u
    };
    // Every side split at every vertex lying on it — looked for in the cells
    // of a grid round the side, not among every vertex there is.
    let cell = {
        let mut lengths: Vec<f64> = kept
            .iter()
            .flat_map(|k| k.rings.iter())
            .flat_map(|r| {
                r.windows(2)
                    .map(|w| v3::dist(pts[w[0].point], pts[w[1].point]))
            })
            .collect();
        lengths.sort_by(f64::total_cmp);
        lengths
            .get(lengths.len() / 2)
            .copied()
            .unwrap_or(1.0)
            .max(tolerance * 4.0)
            .max(1e-12)
    };
    let key = |p: V3| p.map(|x| (x / cell).floor() as i64);
    let mut filed: HashMap<[i64; 3], Vec<usize>> = HashMap::new();
    for &v in &used {
        filed.entry(key(pts[v])).or_default().push(v);
    }
    let near = |pa: V3, pb: V3| -> Vec<usize> {
        let lo = key([0, 1, 2].map(|k| pa[k].min(pb[k]) - tolerance));
        let hi = key([0, 1, 2].map(|k| pa[k].max(pb[k]) + tolerance));
        let count: i64 = (0..3).map(|k| hi[k] - lo[k] + 1).product();
        if count > 4096 {
            return used.clone();
        }
        let mut out = Vec::new();
        for x in lo[0]..=hi[0] {
            for y in lo[1]..=hi[1] {
                for z in lo[2]..=hi[2] {
                    if let Some(list) = filed.get(&[x, y, z]) {
                        out.extend(list);
                    }
                }
            }
        }
        out
    };
    let split = |ring: &[Node]| -> Vec<Node> {
        let n = ring.len();
        let mut out: Vec<Node> = Vec::with_capacity(n);
        for i in 0..n {
            let (a, b) = (ring[i], ring[(i + 1) % n]);
            out.push(a);
            let (pa, pb) = (pts[a.point], pts[b.point]);
            let ab = v3::sub(pb, pa);
            let l = v3::norm(ab);
            if l <= tolerance {
                continue;
            }
            let mut on: Vec<(f64, usize)> = near(pa, pb)
                .iter()
                .filter(|&&v| v != a.point && v != b.point)
                .filter_map(|&v| {
                    let t = v3::dot(v3::sub(pts[v], pa), ab) / (l * l);
                    if t * l <= tolerance || (1.0 - t) * l <= tolerance {
                        return None;
                    }
                    (v3::dist(lerp3(pa, pb, t), pts[v]) <= tolerance).then_some((t, v))
                })
                .collect();
            on.sort_by(|x, y| x.0.total_cmp(&y.0));
            out.extend(on.into_iter().map(|(t, v)| Node {
                point: v,
                uv: [
                    a.uv[0] + (b.uv[0] - a.uv[0]) * t,
                    a.uv[1] + (b.uv[1] - a.uv[1]) * t,
                ],
            }));
        }
        out
    };

    struct Built {
        patch: usize,
        flipped: bool,
        rings: Vec<Vec<Node>>,
        /// A whole band round its surface with nothing cut out of it: its
        /// seam sides, which it does not keep. It is a face that wraps, as
        /// the primitives make one, bounded by its rims alone.
        band: Option<HashSet<usize>>,
    }
    let mut built: Vec<Built> = Vec::new();
    for k in kept {
        let rings: Vec<Vec<Node>> = k
            .rings
            .iter()
            .map(|r| split(r))
            .filter(|r| r.len() >= 3)
            .collect();
        if rings.is_empty() {
            continue;
        }
        let f = all[k.patch];
        let band = match (f.wraps, f.period[0]) {
            ([true, false], Some(t)) if rings.len() == 1 => {
                let ring = &rings[0];
                let n = ring.len();
                let on_seam = |q: P2| {
                    let k = (q[0] - f.dlo[0]) / t;
                    (k - k.round()).abs() * t <= tolerance
                };
                let seam: HashSet<usize> = (0..n)
                    .filter(|&i| {
                        let (a, b) = (ring[i].uv, ring[(i + 1) % n].uv);
                        on_seam(a) && on_seam(b) && (a[0] - b[0]).abs() <= tolerance
                    })
                    .collect();
                let (lo, hi) = ring
                    .iter()
                    .fold((f64::INFINITY, f64::NEG_INFINITY), |m, x| {
                        (m.0.min(x.uv[0]), m.1.max(x.uv[0]))
                    });
                // And the rest is rims: each at one height, the lowest or the
                // highest, since a face that wraps is read as the band
                // between them. A sphere with a square column through it
                // goes all the way round, and its edges are not rims.
                let (vlo, vhi) = ring
                    .iter()
                    .fold((f64::INFINITY, f64::NEG_INFINITY), |m, x| {
                        (m.0.min(x.uv[1]), m.1.max(x.uv[1]))
                    });
                let rims = ring.iter().all(|x| {
                    (x.uv[1] - vlo).abs() <= tolerance || (x.uv[1] - vhi).abs() <= tolerance
                });
                ((hi - lo - t).abs() <= tolerance && !seam.is_empty() && rims).then_some(seam)
            }
            _ => None,
        };
        built.push(Built {
            patch: k.patch,
            flipped: f.flipped ^ k.flip,
            rings,
            band,
        });
    }

    // A band whose rim the face beside it has cut in two — at the end of
    // that face's own seam — is not read back right as a face that wraps,
    // so it keeps its seam instead, and the edges are worked out again.
    let chains = loop {
        let chains = {
            // Sides by the points they join, with the faces along each.
            let mut uses: HashMap<(usize, usize), Vec<usize>> = HashMap::new();
            for (fi, b) in built.iter().enumerate() {
                for (r, ring) in b.rings.iter().enumerate() {
                    let n = ring.len();
                    for i in 0..n {
                        if r == 0 && b.band.as_ref().is_some_and(|s| s.contains(&i)) {
                            continue;
                        }
                        let (p, q) = (ring[i].point, ring[(i + 1) % n].point);
                        if p != q {
                            uses.entry((p.min(q), p.max(q))).or_default().push(fi);
                        }
                    }
                }
            }
            let open = uses.values().filter(|u| u.len() != 2).count();
            if open > 0 {
                if std::env::var_os("THREERS_BREP_DEBUG").is_some() {
                    for (fi, b) in built.iter().enumerate() {
                        let zs: Vec<f64> =
                            b.rings.iter().flatten().map(|n| pts[n.point][2]).collect();
                        let (lo, hi) =
                            zs.iter().fold((f64::INFINITY, f64::NEG_INFINITY), |a, &z| {
                                (a.0.min(z), a.1.max(z))
                            });
                        eprintln!(
                            "  [fuse] face {fi}: patch {} {} rings {} z {lo}..{hi}",
                            b.patch,
                            all[b.patch].surface.kind(),
                            b.rings.len()
                        );
                    }
                    for (k, u) in &uses {
                        if u.len() != 2 {
                            let kinds: Vec<&str> = u
                                .iter()
                                .map(|&f| all[built[f].patch].surface.kind())
                                .collect();
                            eprintln!(
                                "  [fuse] side {:?} .. {:?} faces {u:?} {kinds:?}",
                                pts[k.0], pts[k.1]
                            );
                        }
                    }
                }
                return Err(Declined::NotWatertight { open_edges: open });
            }

            // Chain sides into edges through points where nothing else meets.
            let mut at: HashMap<usize, Vec<(usize, usize)>> = HashMap::new();
            for &key in uses.keys() {
                at.entry(key.0).or_default().push(key);
                at.entry(key.1).or_default().push(key);
            }
            let pair = |key: &(usize, usize)| {
                let mut u = uses[key].clone();
                u.sort_unstable();
                u
            };
            // Through a point where nothing else meets and the line does
            // not turn a corner: the same two faces can meet along a line
            // and then an arc, and those are two edges with a vertex
            // between. A corner is a turn no sampled curve makes — a circle
            // has twelve points at the least, 30° apart — since a coarse rim
            // of 44 points turns 8° at every one.
            let through = |v: usize| -> bool {
                let list = &at[&v];
                if list.len() != 2 || pair(&list[0]) != pair(&list[1]) {
                    return false;
                }
                let other = |k: &(usize, usize)| if k.0 == v { k.1 } else { k.0 };
                let (a, b) = (
                    v3::sub(pts[v], pts[other(&list[0])]),
                    v3::sub(pts[other(&list[1])], pts[v]),
                );
                let (la, lb) = (v3::norm(a), v3::norm(b));
                la <= 0.0 || lb <= 0.0 || v3::dot(a, b) / (la * lb) >= (35.0f64).to_radians().cos()
            };
            let mut keys: Vec<(usize, usize)> = uses.keys().copied().collect();
            keys.sort_unstable();
            let mut chain_of: HashMap<(usize, usize), usize> = HashMap::new();
            let mut chains: Vec<(Vec<usize>, Vec<usize>)> = Vec::new(); // points, faces
            let walk = |from: usize,
                        mut via: (usize, usize),
                        id: usize,
                        chain_of: &mut HashMap<(usize, usize), usize>| {
                let mut out = Vec::new();
                let mut v = from;
                loop {
                    let next_v = if via.0 == v { via.1 } else { via.0 };
                    out.push(next_v);
                    if !through(next_v) {
                        break;
                    }
                    let Some(&next) = at[&next_v].iter().find(|&&k| k != via) else {
                        break;
                    };
                    if chain_of.contains_key(&next) {
                        break;
                    }
                    chain_of.insert(next, id);
                    via = next;
                    v = next_v;
                }
                out
            };
            for &key in &keys {
                if chain_of.contains_key(&key) {
                    continue;
                }
                let id = chains.len();
                chain_of.insert(key, id);
                let mut points = vec![key.0];
                points.extend(walk(key.0, key, id, &mut chain_of));
                let closed = points.last() == Some(&key.0);
                if !closed
                    && through(key.0)
                    && let Some(start) = at[&key.0].iter().copied().find(|&k| k != key)
                    && !chain_of.contains_key(&start)
                {
                    chain_of.insert(start, id);
                    let mut back = walk(key.0, start, id, &mut chain_of);
                    back.reverse();
                    back.extend(points);
                    points = back;
                }
                chains.push((points, pair(&key)));
            }
            chains
        };
        let mut changed = false;
        for (fi, b) in built.iter_mut().enumerate() {
            if b.band.is_some()
                && chains
                    .iter()
                    .any(|(pts, users)| users.contains(&fi) && pts.first() != pts.last())
            {
                b.band = None;
                changed = true;
            }
        }
        if !changed {
            break chains;
        }
    };
    let mut index: HashMap<usize, usize> = HashMap::new();
    let mut vertices: Vec<V3> = Vec::new();
    let mut name = |v: usize, vertices: &mut Vec<V3>| {
        *index.entry(v).or_insert_with(|| {
            vertices.push(pts[v]);
            vertices.len() - 1
        })
    };
    // One surface per face of the inputs, however many pieces it became, so
    // a point is on as many surfaces as there are and no more.
    let mut surfaces: Vec<Surface> = Vec::new();
    let mut surface_of: HashMap<usize, usize> = HashMap::new();
    let mut faces = Vec::with_capacity(built.len());
    for b in &built {
        let f = all[b.patch];
        let surface = *surface_of.entry(b.patch).or_insert_with(|| {
            surfaces.push(f.surface.clone());
            surfaces.len() - 1
        });
        let mut loops = Vec::with_capacity(b.rings.len());
        let (mut lo, mut hi) = ([f64::INFINITY; 2], [f64::NEG_INFINITY; 2]);
        for ring in &b.rings {
            let uv: Vec<P2> = ring
                .iter()
                .map(|n| [n.uv[0] / f.scale[0], n.uv[1] / f.scale[1]])
                .collect();
            for q in &uv {
                for k in 0..2 {
                    lo[k] = lo[k].min(q[k]);
                    hi[k] = hi[k].max(q[k]);
                }
            }
            loops.push(TrimLoop {
                area: signed_area(&uv),
                uv,
                vertices: ring.iter().map(|n| name(n.point, &mut vertices)).collect(),
            });
        }
        let band = b.band.is_some();
        faces.push(Face {
            surface,
            edges: Vec::new(),
            flipped: b.flipped,
            u_range: (lo[0], hi[0]),
            v_range: (lo[1], hi[1]),
            u_wraps: band,
            v_wraps: false,
            loops: (!band).then_some(loops),
        });
    }
    let mut edges: Vec<Edge> = Vec::with_capacity(chains.len());
    for (points, users) in &chains {
        let closed = points.len() > 2 && points.first() == points.last();
        let vs: Vec<usize> = points.iter().map(|&v| name(v, &mut vertices)).collect();
        edges.push(Edge {
            surfaces: (faces[users[0]].surface, faces[users[1]].surface),
            vertices: vs,
            closed,
        });
        let e = edges.len() - 1;
        for &fi in users {
            faces[fi].edges.push(e);
        }
    }
    // Every point on every surface it is a point of.
    settle(
        &surfaces,
        &mut vertices,
        &mut faces,
        &edges,
        origins,
        tolerance,
    );
    merge_coincident(&vertices, &mut faces, &mut edges, tolerance);
    bend(
        &surfaces,
        &mut vertices,
        &mut faces,
        &mut edges,
        origins,
        tolerance,
    );
    Ok(Body::from_parts(surfaces, vertices, edges, faces))
}

/// A middle point for every edge of two points that is not straight.
///
/// Where curves cross close together, a piece of an arc between two of the
/// crossings can come out with no sample in it: two points, which read back
/// as a straight line, and which piece does depends on where the sampling
/// fell. Its midpoint, put on the curve — the circle it was drawn from, or
/// where its two surfaces meet — says what it is.
fn bend(
    surfaces: &[Surface],
    vertices: &mut Vec<V3>,
    faces: &mut [Face],
    edges: &mut [Edge],
    origins: &Origins,
    tolerance: f64,
) {
    let tiny = tolerance * 1e-6;
    let mut added: Vec<(usize, usize, usize)> = Vec::new(); // ends and middle
    for e in edges.iter_mut() {
        if e.vertices.len() != 2 || e.closed {
            continue;
        }
        let (a, b) = (e.vertices[0], e.vertices[1]);
        let (pa, pb) = (vertices[a], vertices[b]);
        let m = lerp3(pa, pb, 0.5);
        // The circle of an input edge it runs along: both ends on it, and
        // its middle by that edge's own polyline — a straight cut across a
        // disc has both ends on the rim too.
        let circle = origins
            .at(pa, tolerance)
            .into_iter()
            .filter_map(|(_, _, c)| c)
            .find(|&c| {
                let reach = tolerance.max(origins.circles[c].3 * 2.0);
                origins.off_circle(c, pa) <= tiny
                    && origins.off_circle(c, pb) <= tiny
                    && origins.at(m, reach).iter().any(|x| x.2 == Some(c))
            });
        let q = match circle {
            Some(c) => {
                // The nearer of the two arcs between the ends: the point out
                // from the centre through the chord's middle.
                origins.onto_circle(c, m)
            }
            None if e.surfaces.0 != e.surfaces.1 => {
                match super::intersect::settle(
                    &surfaces[e.surfaces.0],
                    &surfaces[e.surfaces.1],
                    m,
                    tolerance,
                ) {
                    Some(q) if v3::dist(q, m) <= v3::dist(pa, pb) => q,
                    _ => continue,
                }
            }
            None => continue,
        };
        if v3::dist(q, m) <= tiny.max(1e-12) {
            continue;
        }
        vertices.push(q);
        let mid = vertices.len() - 1;
        e.vertices = vec![a, mid, b];
        added.push((a, b, mid));
    }
    if added.is_empty() {
        return;
    }
    // And into the loops that step from one end to the other.
    let between: HashMap<(usize, usize), usize> = added
        .iter()
        .flat_map(|&(a, b, m)| [((a, b), m), ((b, a), m)])
        .collect();
    for f in faces.iter_mut() {
        let s = &surfaces[f.surface];
        for l in f.loops.iter_mut().flatten() {
            let n = l.vertices.len();
            let mut vs = Vec::with_capacity(n + 2);
            let mut uv = Vec::with_capacity(n + 2);
            for k in 0..n {
                let (a, b) = (l.vertices[k], l.vertices[(k + 1) % n]);
                vs.push(a);
                uv.push(l.uv[k]);
                if let Some(&m) = between.get(&(a, b)) {
                    let near = [
                        (l.uv[k][0] + l.uv[(k + 1) % n][0]) * 0.5,
                        (l.uv[k][1] + l.uv[(k + 1) % n][1]) * 0.5,
                    ];
                    let q = s
                        .invert_near(vertices[m], (near[0], near[1]))
                        .map(|(u, w)| [u, w])
                        .unwrap_or(near);
                    vs.push(m);
                    uv.push(q);
                }
            }
            l.vertices = vs;
            l.uv = uv;
        }
    }
}

/// The edges of the inputs, as segments with the two surfaces each lies
/// between.
struct Origins {
    surfaces: Vec<Surface>,
    /// Ends, the two surfaces, and the circle the whole edge lies on if
    /// it lies on one.
    segments: Vec<(V3, V3, usize, usize, Option<usize>)>,
    /// Centre, normal, radius, and how far a chord of the edge sags.
    circles: Vec<(V3, V3, f64, f64)>,
    grid: HashMap<[i64; 3], Vec<usize>>,
    /// Segments too long to file by cell, checked every time.
    long: Vec<usize>,
    cell: f64,
}

impl Origins {
    fn of(bodies: &[&Body], tolerance: f64) -> Origins {
        let mut surfaces = Vec::new();
        let mut segments = Vec::new();
        let mut circles = Vec::new();
        for b in bodies {
            let base = surfaces.len();
            surfaces.extend(b.surfaces().iter().cloned());
            for e in b.edges() {
                // An edge between a surface and itself — a seam, or the
                // line between two pieces of one plane — adds no surface,
                // but its circle, if it is one, is still its curve.
                let pts: Vec<V3> = e.vertices.iter().map(|&v| b.vertices()[v]).collect();
                // An edge between two pieces of one plane is a polyline the
                // surfaces cannot place a point on; if it samples a circle,
                // the circle can.
                if std::env::var_os("THREERS_BREP_DEBUG").is_some()
                    && pts.len() >= 4
                    && circle_through(&pts).is_none()
                {
                    let (a, z) = (pts[0], pts[pts.len() - 1]);
                    let bent = pts
                        .iter()
                        .map(|p| to_polyline(*p, &[a, z]))
                        .fold(0.0, f64::max);
                    if bent > tolerance {
                        eprintln!(
                            "  [fuse] input edge of {} points bent {bent:.2e}, neither straight nor a circle, between {} and {}",
                            pts.len(),
                            b.surfaces()[e.surfaces.0].kind(),
                            b.surfaces()[e.surfaces.1].kind()
                        );
                    }
                }
                let circle = circle_through(&pts).map(|c| {
                    circles.push(c);
                    circles.len() - 1
                });
                for w in pts.windows(2) {
                    segments.push((w[0], w[1], base + e.surfaces.0, base + e.surfaces.1, circle));
                }
            }
        }
        // Cells about as big as the segments are long, mostly.
        let mut lengths: Vec<f64> = segments.iter().map(|s| v3::dist(s.0, s.1)).collect();
        lengths.sort_by(f64::total_cmp);
        let cell = lengths
            .get(lengths.len() / 2)
            .copied()
            .unwrap_or(1.0)
            .max(tolerance * 8.0);
        let mut grid: HashMap<[i64; 3], Vec<usize>> = HashMap::new();
        let mut long = Vec::new();
        for (i, (p, q, _, _, _)) in segments.iter().enumerate() {
            let k = |x: f64| (x / cell).floor() as i64;
            let lo = [0, 1, 2].map(|d| k(p[d].min(q[d]) - tolerance));
            let hi = [0, 1, 2].map(|d| k(p[d].max(q[d]) + tolerance));
            if (0..3)
                .map(|d| (hi[d] - lo[d] + 1) as i128)
                .product::<i128>()
                > 64
            {
                long.push(i);
                continue;
            }
            for x in lo[0]..=hi[0] {
                for y in lo[1]..=hi[1] {
                    for z in lo[2]..=hi[2] {
                        grid.entry([x, y, z]).or_default().push(i);
                    }
                }
            }
        }
        Origins {
            surfaces,
            segments,
            circles,
            grid,
            long,
            cell,
        }
    }

    /// The surface pairs, and circles, of the input edges passing within
    /// `tolerance` of `p`.
    fn at(&self, p: V3, reach: f64) -> Vec<(usize, usize, Option<usize>)> {
        let tolerance = reach;
        let k = p.map(|x| (x / self.cell).floor() as i64);
        let mut out = Vec::new();
        for &i in self.grid.get(&k).into_iter().flatten().chain(&self.long) {
            let (a, b, s, t, c) = self.segments[i];
            if to_polyline(p, &[a, b]) <= tolerance && !out.contains(&(s, t, c)) {
                out.push((s, t, c));
            }
        }
        out
    }
}

/// The circle a polyline's points all lie on, to rounding: centre, normal,
/// radius, and the sag of its longest chord. `None` for a straight one, or
/// one that is not a circle.
fn circle_through(pts: &[V3]) -> Option<(V3, V3, f64, f64)> {
    let n = pts.len();
    if n < 4 {
        return None;
    }
    let (a, m, b) = (pts[0], pts[n / 3], pts[2 * n / 3]);
    let (u, w) = (v3::sub(m, a), v3::sub(b, a));
    let uxw = v3::cross(u, w);
    let l2 = v3::dot(uxw, uxw);
    if l2 <= 1e-24 {
        return None;
    }
    let t = v3::cross(
        v3::sub(v3::scale(w, v3::dot(u, u)), v3::scale(u, v3::dot(w, w))),
        uxw,
    );
    let centre = v3::add(a, v3::scale(t, 0.5 / l2));
    let r = v3::dist(a, centre);
    let normal = v3::normalize(uxw)?;
    let eps = 1e-9 * r.max(1.0);
    let on = pts.iter().all(|p| {
        let d = v3::sub(*p, centre);
        (v3::norm(d) - r).abs() <= eps && v3::dot(d, normal).abs() <= eps
    });
    if !on {
        return None;
    }
    let chord = pts
        .windows(2)
        .map(|w| v3::dist(w[0], w[1]))
        .fold(0.0, f64::max);
    Some((
        centre,
        normal,
        r,
        r - (r * r - chord * chord / 4.0).max(0.0).sqrt(),
    ))
}

impl Origins {
    /// `p` moved onto a circle, in its plane, out from its centre.
    fn onto_circle(&self, c: usize, p: V3) -> V3 {
        let (centre, normal, r, _) = self.circles[c];
        let d = v3::sub(p, centre);
        let flat = v3::sub(d, v3::scale(normal, v3::dot(d, normal)));
        match v3::normalize(flat) {
            Some(dir) => v3::add(centre, v3::scale(dir, r)),
            None => p,
        }
    }

    /// Where a plane crossing a circle's own plane meets the circle, nearest
    /// `p`; `None` for a plane parallel to it.
    fn through_plane(&self, c: usize, origin: V3, normal: V3, p: V3) -> Option<V3> {
        let (centre, n, r, _) = self.circles[c];
        let dir = v3::cross(n, normal);
        let l = v3::norm(dir);
        if l <= 1e-6 {
            return None;
        }
        let dir = v3::scale(dir, 1.0 / l);
        // The point of the line nearest the centre: in the circle's plane,
        // on the other plane, square to the line.
        let across = v3::cross(dir, n);
        let k = v3::dot(normal, v3::sub(origin, centre)) / v3::dot(normal, across);
        if !k.is_finite() || k.abs() > r {
            return None;
        }
        let foot = v3::add(centre, v3::scale(across, k));
        let half = (r * r - k * k).max(0.0).sqrt();
        [1.0, -1.0]
            .map(|s| v3::add(foot, v3::scale(dir, s * half)))
            .into_iter()
            .min_by(|u, w| v3::dist(*u, p).total_cmp(&v3::dist(*w, p)))
    }

    /// Where two circles in one plane cross, nearest `p`.
    fn crossing(&self, a: usize, b: usize, p: V3) -> Option<V3> {
        let (ca, na, ra, _) = self.circles[a];
        let (cb, nb, rb, _) = self.circles[b];
        let scale = ra.max(rb).max(1.0);
        if v3::norm(v3::cross(na, nb)) > 1e-9 || v3::dot(na, v3::sub(cb, ca)).abs() > 1e-9 * scale {
            return None;
        }
        let d = v3::sub(cb, ca);
        let dist = v3::norm(d);
        if dist <= 1e-12 * scale || dist > ra + rb || dist < (ra - rb).abs() {
            return None;
        }
        let x = v3::scale(d, 1.0 / dist);
        let y = v3::cross(na, x);
        // Along the line of centres, then either way across it.
        let along = (dist * dist + ra * ra - rb * rb) / (2.0 * dist);
        let across = (ra * ra - along * along).max(0.0).sqrt();
        let foot = v3::add(ca, v3::scale(x, along));
        [1.0, -1.0]
            .map(|s| v3::add(foot, v3::scale(y, s * across)))
            .into_iter()
            .min_by(|u, w| v3::dist(*u, p).total_cmp(&v3::dist(*w, p)))
    }

    fn off_circle(&self, c: usize, p: V3) -> f64 {
        v3::dist(p, self.onto_circle(c, p))
    }
}

/// Where a point claimed by a plane, a cylinder tangent to it within the
/// tolerance, and one more plane is: on the line the two touch along, where
/// it crosses the other plane. `None` where the claims are not that.
fn tangent_point(surfaces: &[&Surface], claims: &[usize], p: V3, tolerance: f64) -> Option<V3> {
    if claims.len() != 3 {
        return None;
    }
    for &c in claims {
        let Surface::Cylinder {
            origin,
            axis,
            radius,
            ..
        } = surfaces[c]
        else {
            continue;
        };
        let planes: Vec<(V3, V3)> = claims
            .iter()
            .filter(|&&s| s != c)
            .filter_map(|&s| match surfaces[s] {
                Surface::Plane { origin, normal, .. } => Some((*origin, *normal)),
                _ => None,
            })
            .collect();
        if planes.len() != 2 {
            continue;
        }
        for (k, &(po, pn)) in planes.iter().enumerate() {
            // Along the axis, and as far from it as the radius.
            if v3::dot(pn, *axis).abs() > 1e-9 {
                continue;
            }
            let gap = v3::dot(pn, v3::sub(*origin, po));
            if (gap.abs() - radius).abs() > tolerance {
                continue;
            }
            // The line of the cylinder nearest the plane.
            let toward = if gap > 0.0 { v3::scale(pn, -1.0) } else { pn };
            let base = v3::add(*origin, v3::scale(toward, *radius));
            // Where it crosses the other plane.
            let (qo, qn) = planes[1 - k];
            let along = v3::dot(qn, *axis);
            if along.abs() <= 1e-9 {
                return None;
            }
            let t = v3::dot(qn, v3::sub(qo, base)) / along;
            let q = v3::add(base, v3::scale(*axis, t));
            return (v3::dist(p, q) <= tolerance * 1e3).then_some(q);
        }
    }
    None
}

/// Vertices that settled onto one point are one vertex: every reference to
/// the later turned to the earlier, a ring's step from one to itself
/// dropped, and an edge that shrank to a point dropped with it.
fn merge_coincident(vertices: &[V3], faces: &mut [Face], edges: &mut Vec<Edge>, tolerance: f64) {
    let near = tolerance * 0.1;
    let key = |p: V3| p.map(|x| (x / near).floor() as i64);
    let mut grid: HashMap<[i64; 3], Vec<usize>> = HashMap::new();
    let mut onto: Vec<usize> = (0..vertices.len()).collect();
    for (i, p) in vertices.iter().enumerate() {
        let k = key(*p);
        let mut found = None;
        'look: for dx in -1..=1 {
            for dy in -1..=1 {
                for dz in -1..=1 {
                    for &j in grid
                        .get(&[k[0] + dx, k[1] + dy, k[2] + dz])
                        .into_iter()
                        .flatten()
                    {
                        if v3::dist(vertices[j], *p) <= near {
                            found = Some(j);
                            break 'look;
                        }
                    }
                }
            }
        }
        match found {
            Some(j) => onto[i] = j,
            None => grid.entry(k).or_default().push(i),
        }
    }
    if onto.iter().enumerate().all(|(i, &j)| i == j) {
        return;
    }
    let mut gone = vec![false; edges.len()];
    for (i, e) in edges.iter_mut().enumerate() {
        for v in &mut e.vertices {
            *v = onto[*v];
        }
        e.vertices.dedup();
        if e.vertices.len() < 2 || (e.vertices.len() == 2 && e.closed) {
            gone[i] = true;
        }
    }
    for f in faces.iter_mut() {
        f.edges.retain(|&e| !gone[e]);
        for l in f.loops.iter_mut().flatten() {
            let mut vs = Vec::with_capacity(l.vertices.len());
            let mut uv = Vec::with_capacity(l.uv.len());
            for (k, &v) in l.vertices.iter().enumerate() {
                let v = onto[v];
                if vs.last() != Some(&v) {
                    vs.push(v);
                    uv.push(l.uv[k]);
                }
            }
            while vs.len() > 1 && vs.first() == vs.last() {
                vs.pop();
                uv.pop();
            }
            l.vertices = vs;
            l.uv = uv;
        }
    }
    // Edges no face names any more go, and the rest are renumbered.
    let mut index = vec![usize::MAX; edges.len()];
    let mut kept = Vec::with_capacity(edges.len());
    for (i, e) in edges.drain(..).enumerate() {
        if !gone[i] {
            index[i] = kept.len();
            kept.push(e);
        }
    }
    *edges = kept;
    for f in faces.iter_mut() {
        for e in &mut f.edges {
            *e = index[*e];
        }
    }
}

/// Put each vertex exactly on every surface it belongs to.
///
/// A run's end is found by halving along a chord, a point where one edge
/// meets another is put on a chord, a crossing is evaluated on one of the two
/// surfaces: each is within the tolerance and no closer. An edge whose
/// points are not on its curve is not that curve — read back, a circle with
/// one such point is no circle. Two surfaces settle by Newton on the pair;
/// three or more, at a corner, by projecting onto each in turn.
fn settle(
    surfaces: &[Surface],
    vertices: &mut [V3],
    faces: &mut [Face],
    edges: &[Edge],
    origins: &Origins,
    tolerance: f64,
) {
    let mut claims: HashMap<usize, Vec<usize>> = HashMap::new();
    let mut claim = |v: usize, s: usize| {
        let c = claims.entry(v).or_default();
        if !c.contains(&s) {
            c.push(s);
        }
    };
    for f in faces.iter() {
        for l in f.loops.iter().flatten() {
            for &v in &l.vertices {
                claim(v, f.surface);
            }
        }
        for &e in &f.edges {
            for &v in &edges[e].vertices {
                claim(v, f.surface);
            }
        }
    }
    // And the curve of any edge of the inputs a vertex lies on: a solid's
    // outline laid on a face of the other lies between two pieces of one
    // plane, which say nothing about where along it a point is; the edge it
    // came from does.
    let mut on_circles: HashMap<usize, Vec<usize>> = HashMap::new();
    for (v, c) in claims.iter_mut() {
        // On the edge to rounding, as a sample of it or a point found on
        // one of its chords is: near it within the tolerance is not enough,
        // since a point that close to a circle need not be on it.
        for (a, b, circle) in origins.at(vertices[*v], tolerance * 1e-5) {
            for s in [a, b] {
                let s = surfaces.len() + s;
                if !c.contains(&s) {
                    c.push(s);
                }
            }
            if let Some(k) = circle {
                let list = on_circles.entry(*v).or_default();
                if !list.contains(&k) {
                    list.push(k);
                }
            }
        }
    }
    // The ends of an edge whose inner points are all on one circle are on it
    // too: they are where it meets the next curve, found within the
    // tolerance of a chord rather than on it.
    for e in edges {
        let n = e.vertices.len();
        if n < 2 {
            continue;
        }
        let common: Vec<usize> = if n == 2 {
            // No inner point: the circle of an input edge its middle runs
            // along.
            let m = lerp3(vertices[e.vertices[0]], vertices[e.vertices[1]], 0.5);
            origins
                .at(m, tolerance)
                .into_iter()
                .filter_map(|x| x.2)
                .filter(|&c| {
                    origins
                        .at(m, tolerance.max(origins.circles[c].3 * 2.0))
                        .iter()
                        .any(|x| x.2 == Some(c))
                })
                .collect()
        } else {
            let inner = &e.vertices[1..n - 1];
            on_circles
                .get(&inner[0])
                .cloned()
                .unwrap_or_default()
                .into_iter()
                .filter(|c| {
                    inner
                        .iter()
                        .all(|v| on_circles.get(v).is_some_and(|l| l.contains(c)))
                })
                .collect()
        };
        for &end in [e.vertices[0], e.vertices[n - 1]].iter() {
            for &c in &common {
                // Within the sag of the circle's chords: a crossing found on
                // a chord is that far off it.
                if origins.off_circle(c, vertices[end]) <= tolerance.max(origins.circles[c].3 * 2.0)
                {
                    let list = on_circles.entry(end).or_default();
                    if !list.contains(&c) {
                        list.push(c);
                    }
                    claims.entry(end).or_default();
                }
            }
        }
    }
    let result = surfaces;
    let surfaces: Vec<&Surface> = result.iter().chain(origins.surfaces.iter()).collect();
    let worst = |p: V3, ss: &[usize]| {
        ss.iter()
            .map(|&s| surfaces[s].distance(p).abs())
            .fold(0.0, f64::max)
    };
    let probe: Option<V3> = std::env::var("FUSE_PROBE").ok().and_then(|s| {
        let v: Vec<f64> = s.split(',').filter_map(|x| x.trim().parse().ok()).collect();
        (v.len() == 3).then(|| [v[0], v[1], v[2]])
    });
    let mut moved: HashMap<usize, V3> = HashMap::new();
    for (&v, ss) in &claims {
        let p = vertices[v];
        let cs: &[usize] = on_circles.get(&v).map(|c| c.as_slice()).unwrap_or(&[]);
        if probe.is_some_and(|x| v3::dist(x, p) < 0.05) {
            eprintln!(
                "  [probe] vertex {v} at {p:?}: surfaces {:?} circles {:?} near origins {:?}",
                ss.iter().map(|&s| surfaces[s].kind()).collect::<Vec<_>>(),
                cs.iter().map(|&c| origins.circles[c]).collect::<Vec<_>>(),
                origins.at(p, tolerance)
            );
        }
        let worst = |q: V3| {
            cs.iter()
                .map(|&c| origins.off_circle(c, q))
                .fold(worst(q, ss), f64::max)
        };
        let was = worst(p);
        if ss.len() + cs.len() < 2 || was <= 1e-12 {
            continue;
        }
        // A plane and a cylinder lying along it that touch within the
        // tolerance touch along one line of the cylinder: the one nearest the
        // plane. A point on both is on that line; with a third plane, where
        // the line crosses it. Taking turns onto the two crawls along the gap
        // between them and stops a tolerance short of where they touch — a
        // pocket floor's side came back bent there.
        let tangent = cs
            .is_empty()
            .then(|| tangent_point(&surfaces, ss, p, tolerance))
            .flatten();
        let q = if let Some(q) = tangent {
            q
        } else if ss.len() == 2 && cs.is_empty() {
            match super::intersect::settle(surfaces[ss[0]], surfaces[ss[1]], p, tolerance) {
                Some(q) => q,
                None => continue,
            }
        } else if let Some(q) = (cs.len() >= 2)
            .then(|| origins.crossing(cs[0], cs[1], p))
            .flatten()
            .or_else(|| {
                // A circle and a plane across its own: where the line they
                // share meets it.
                let c = *cs.first()?;
                ss.iter().find_map(|&s| match surfaces[s] {
                    Surface::Plane { origin, normal, .. } => {
                        origins.through_plane(c, *origin, *normal, p)
                    }
                    _ => None,
                })
            })
        {
            // Two circles in one plane cross where a formula says: taking
            // turns onto each crawls where they meet at a shallow angle, and
            // stops short of it.
            q
        } else {
            // Onto each surface and each circle in turn, until on all.
            let mut q = p;
            for _ in 0..200 {
                for &s in ss {
                    let surface = surfaces[s];
                    if let Some((u, w)) = surface.invert(q) {
                        q = surface.point(u, w);
                    }
                }
                for &c in cs {
                    q = origins.onto_circle(c, q);
                }
                if worst(q) <= 1e-12 {
                    break;
                }
            }
            q
        };
        // As far as the tolerance, or as far as a chord of the circle sags:
        // a point found on the chord is that far from the curve.
        let reach = cs
            .iter()
            .map(|&c| origins.circles[c].3 * 2.0)
            .fold(tolerance * 4.0, f64::max);
        if probe.is_some_and(|x| v3::dist(x, p) < 0.05) {
            eprintln!(
                "  [probe] -> {q:?}: moved {:.3e} of {reach:.3e}, worst {:.3e} was {was:.3e}; per surface {:?}",
                v3::dist(p, q),
                worst(q),
                ss.iter().map(|&s| surfaces[s].distance(q)).collect::<Vec<_>>()
            );
        }
        // Further, where the point it lands on is exactly on everything it
        // belongs to: a line meeting an arc at a shallow angle crosses the
        // chord well along from the arc — half a millimetre for a 0.01 sag
        // at about a degree.
        // A tangency within the tolerance is further still: along the line
        // they touch, a curve within the tolerance of it runs the square
        // root of the gap — a fifth of a millimetre for a thousandth.
        let exact = worst(q) <= tolerance * 1e-9;
        let far = if tangent.is_some() {
            tolerance * 1e3
        } else if exact {
            reach * 50.0
        } else {
            reach
        };
        if v3::dist(p, q) <= far && worst(q) < was {
            moved.insert(v, q);
        }
        if std::env::var_os("THREERS_BREP_DEBUG").is_some() && worst(q) > 1e-9 {
            eprintln!(
                "  [fuse] vertex {v} unsettled: off by {:.2e} (was {was:.2e}), moved {:.2e} of {reach:.2e}, {} surfaces {:?} {} circles",
                worst(q),
                v3::dist(p, q),
                ss.len(),
                ss.iter().map(|&s| surfaces[s].kind()).collect::<Vec<_>>(),
                cs.len()
            );
        }
    }
    for (v, q) in &moved {
        vertices[*v] = *q;
    }
    for f in faces.iter_mut() {
        let s = surfaces[f.surface];
        let (pu, pv) = s.periodic();
        for l in f.loops.iter_mut().flatten() {
            for k in 0..l.vertices.len() {
                let Some(q) = moved.get(&l.vertices[k]) else {
                    continue;
                };
                let Some((u, w)) = s.invert(*q) else {
                    continue;
                };
                let old = l.uv[k];
                let near = |x: f64, a: f64| {
                    x - std::f64::consts::TAU * ((x - a) / std::f64::consts::TAU).round()
                };
                l.uv[k] = [
                    if pu { near(u, old[0]) } else { u },
                    if pv { near(w, old[1]) } else { w },
                ];
            }
        }
    }
}

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

    /// Volume of a body bounded by planes, from its loops: each face's area
    /// times its plane's distance from the origin, over three.
    fn volume(body: &Body) -> f64 {
        body.faces()
            .iter()
            .map(|f| {
                let Surface::Plane { origin, normal, .. } = &body.surfaces()[f.surface] else {
                    panic!("not flat");
                };
                let area: f64 = f.loops.iter().flatten().map(|l| l.area).sum();
                let sign = if f.flipped { -1.0 } else { 1.0 };
                sign * v3::dot(*normal, *origin) * area / 3.0
            })
            .sum()
    }

    fn check(a: &Body, b: &Body, op: BooleanOp, expected: f64) {
        let r = planar(a, b, op, 1e-6).unwrap().unwrap();
        assert!(
            r.faces().is_empty() || r.is_valid_solid(1e-6),
            "{op:?}: {:?}",
            r.defects(1e-6)
        );
        let v = volume(&r);
        assert!(
            (v - expected).abs() < 1e-9,
            "{op:?}: volume {v}, expected {expected}"
        );
    }

    #[test]
    fn two_boxes_overlapping_at_a_corner() {
        let a = Body::cuboid([4.0, 4.0, 4.0]);
        let b = Body::cuboid([4.0, 4.0, 4.0])
            .translated([2.0, 2.0, 2.0])
            .unwrap();
        check(&a, &b, BooleanOp::Intersection, 8.0);
        check(&a, &b, BooleanOp::Difference, 56.0);
        check(&a, &b, BooleanOp::Union, 120.0);
    }

    #[test]
    fn a_corner_cut_has_occts_topology() {
        // Sides chained between the same two faces: a corner cut out of a
        // box is 9 faces, 21 edges and 14 vertices, as OpenCASCADE has it.
        let a = Body::cuboid([4.0, 4.0, 4.0]);
        let b = Body::cuboid([4.0, 4.0, 4.0])
            .translated([2.0, 2.0, 2.0])
            .unwrap();
        let r = planar(&a, &b, BooleanOp::Difference, 1e-6)
            .unwrap()
            .unwrap();
        assert_eq!(
            (r.faces().len(), r.edges().len(), r.vertices().len()),
            (9, 21, 14)
        );
    }

    #[test]
    fn boxes_sharing_part_of_a_wall() {
        // Side by side, offset: the walls between them overlap in part, and
        // the tops and bottoms lie in one plane.
        let a = Body::cuboid([2.0, 2.0, 2.0]);
        let b = Body::cuboid([2.0, 2.0, 2.0])
            .translated([2.0, 1.0, 0.0])
            .unwrap();
        check(&a, &b, BooleanOp::Union, 16.0);
        check(&a, &b, BooleanOp::Intersection, 0.0);
        check(&a, &b, BooleanOp::Difference, 8.0);
    }

    #[test]
    fn a_slot_cut_through_to_the_floor() {
        // A tool whose bottom lies on the floor and whose ends stand out of
        // both sides: the bar comes back as two blocks.
        let a = Body::cuboid([6.0, 2.0, 2.0]);
        let b = Body::cuboid([2.0, 4.0, 2.0])
            .translated([0.0, 0.0, 1.0])
            .unwrap();
        check(&a, &b, BooleanOp::Difference, 24.0 - 4.0);
        check(&a, &b, BooleanOp::Union, 24.0 + 16.0 - 4.0);
        check(&a, &b, BooleanOp::Intersection, 4.0);
    }

    #[test]
    fn a_box_inside_another_leaves_a_cavity() {
        let a = Body::cuboid([4.0, 4.0, 4.0]);
        let b = Body::cuboid([2.0, 2.0, 2.0]);
        check(&a, &b, BooleanOp::Difference, 56.0);
        check(&a, &b, BooleanOp::Intersection, 8.0);
        check(&a, &b, BooleanOp::Union, 64.0);
    }

    #[test]
    fn a_bar_cut_by_a_cylinder_through_its_floor() {
        let a = Body::cuboid([4.0, 4.0, 2.0]);
        // Through the floor and out of one side, clear of the top.
        let b = Body::cylinder([0.0, 0.5, -0.5], [0.0, 1.0, 0.0], 1.2, 3.0);
        for op in [
            BooleanOp::Difference,
            BooleanOp::Union,
            BooleanOp::Intersection,
        ] {
            let r = general(&a, &b, op, 1e-3).unwrap().unwrap();
            assert!(r.is_valid_solid(1e-3), "{op:?}: {:?}", r.defects(1e-3));
        }
    }

    #[test]
    fn a_collar_on_a_plate_round_its_hole() {
        // The hole's rim and the collar's inner rim are one circle sampled
        // two ways, and both the plate's top and the collar's foot lie in
        // one plane.
        let plate = Body::cuboid([40.0, 40.0, 10.0])
            .boolean(
                &Body::cylinder([0.0, 0.0, -6.0], [0.0, 0.0, 1.0], 2.5, 12.0),
                BooleanOp::Difference,
                1e-3,
            )
            .unwrap();
        let collar = Body::cylinder([0.0, 0.0, 5.0], [0.0, 0.0, 1.0], 4.5, 1.2)
            .boolean(
                &Body::cylinder([0.0, 0.0, 4.0], [0.0, 0.0, 1.0], 2.5, 3.2),
                BooleanOp::Difference,
                1e-3,
            )
            .unwrap();
        let r = general(&plate, &collar, BooleanOp::Union, 1e-3)
            .unwrap()
            .unwrap();
        assert!(r.is_valid_solid(1e-3), "{:?}", r.defects(1e-3));
    }

    #[test]
    fn spheres_that_kiss_graze() {
        let a = Body::sphere([0.0; 3], 1.0);
        let b = Body::sphere([2.0, 0.0, 0.0], 1.0);
        for op in [
            BooleanOp::Difference,
            BooleanOp::Union,
            BooleanOp::Intersection,
        ] {
            assert!(matches!(
                general(&a, &b, op, 1e-3),
                Some(Err(Declined::TangentialContact { .. }))
            ));
        }
    }

    /// The volume of a body bounded by planes and cylinders, from its faces:
    /// `(p·n)/3` over each, a plane by its loops' area, a cylinder by Green's
    /// theorem in its parameters, where `p·n` is `r + c·n(u)` and `dA` is
    /// `r du dv`. Not from a tessellation, which has errors of its own.
    fn exact_volume(r: &Body) -> f64 {
        r.faces()
            .iter()
            .map(|f| {
                let sign = if f.flipped { -1.0 } else { 1.0 };
                let loops = f.loops.as_ref().expect("stated loops");
                sign * match &r.surfaces()[f.surface] {
                    Surface::Plane { origin, normal, .. } => {
                        v3::dot(*origin, *normal) * loops.iter().map(|l| l.area).sum::<f64>() / 3.0
                    }
                    Surface::Cylinder {
                        origin,
                        axis,
                        x_dir,
                        radius,
                    } => {
                        let y = v3::cross(*axis, *x_dir);
                        let (cx, cy) = (v3::dot(*origin, *x_dir), v3::dot(*origin, y));
                        let big =
                            |u: f64| radius * (radius * u + cx * u.sin() - cy * u.cos()) / 3.0;
                        let mut sum = 0.0;
                        for l in loops {
                            let n = l.uv.len();
                            for k in 0..n {
                                let (a, b) = (l.uv[k], l.uv[(k + 1) % n]);
                                let m = (a[0] + b[0]) / 2.0;
                                sum += (big(a[0]) + 4.0 * big(m) + big(b[0])) / 6.0 * (b[1] - a[1]);
                            }
                        }
                        sum
                    }
                    other => panic!("no exact volume for a {}", other.kind()),
                }
            })
            .sum()
    }

    #[test]
    fn a_bore_through_a_pockets_end_wall() {
        // PartDesignExample's second pocket: a cylinder whose axis lies in
        // the end wall of an earlier pocket. The volume it takes is the
        // disc's area over its height, less the part of the disc over the
        // pocket — ∫ √(r² − y²) dy from −min(r, 25) to −5 — over the
        // pocket's depth of 15. At r = 25 the cylinder touches the pocket's
        // side wall along its corner; at 24.7 it clears it. Right, or
        // declined. (Its mesh was 86 out here: the fill of a notched band,
        // not the solid.)
        let block = Body::cuboid([140.0, 70.0, 100.0]);
        let pocket = Body::cuboid([100.0, 20.0, 15.0])
            .translated([-12.5, -15.0, 42.5])
            .unwrap();
        let base = general(&block, &pocket, BooleanOp::Difference, 1e-4)
            .unwrap()
            .unwrap();
        for r in [25.0f64, 24.7] {
            let bore = Body::cylinder([37.5, 0.0, 0.0], [0.0, 0.0, 1.0], r, 50.0);
            let f = |y: f64| {
                y / 2.0 * (r * r - y * y).max(0.0).sqrt()
                    + r * r / 2.0 * (y / r).clamp(-1.0, 1.0).asin()
            };
            let strip = f(-5.0) - f(-r.min(25.0));
            let removed = std::f64::consts::PI * r * r * 50.0 - strip * 15.0;
            let expected = 140.0 * 70.0 * 100.0 - 100.0 * 20.0 * 15.0 - removed;
            if let Ok(out) = general(&base, &bore, BooleanOp::Difference, 1e-4).unwrap() {
                assert!(out.is_valid_solid(1e-4), "{:?}", out.defects(1e-4));
                let got = exact_volume(&out);
                assert!(
                    (got - expected).abs() < 0.1,
                    "r {r}: volume {got}, expected {expected}"
                );
            }
        }
    }

    #[test]
    fn a_plate_drilled_through() {
        let plate = Body::cuboid([10.0, 8.0, 2.0]);
        let drill = Body::cylinder([0.0, 0.0, -3.0], [0.0, 0.0, 1.0], 2.0, 6.0);
        let r = general(&plate, &drill, BooleanOp::Difference, 1e-3)
            .unwrap()
            .unwrap();
        assert!(r.is_valid_solid(1e-3), "{:?}", r.defects(1e-3));
        // Top, bottom, four sides, and the bore as one face with its seam.
        assert_eq!(r.faces().len(), 7);
    }
}