Welcome to mirror list, hosted at ThFree Co, Russian Federation.

delaunay_2d.c « intern « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7199e461c6ccbd8b383568ff99a4ed2b15667c62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

/** \file
 * \ingroup bli
 *
 * Constrained 2d Delaunay Triangulation.
 */

#include "MEM_guardedalloc.h"

#include "BLI_array.h"
#include "BLI_bitmap.h"
#include "BLI_linklist.h"
#include "BLI_math.h"
#include "BLI_memarena.h"
#include "BLI_mempool.h"

#include "BLI_delaunay_2d.h"

/* Uncomment this define to get helpful debugging functions etc. defined. */
// #define DEBUG_CDT

struct CDTEdge;
struct CDTFace;
struct CDTVert;

typedef struct SymEdge {
  struct SymEdge *next; /* In face, doing CCW traversal of face. */
  struct SymEdge *rot;  /* CCW around vert. */
  struct CDTVert *vert; /* Vert at origin. */
  struct CDTEdge *edge; /* Undirected edge this is for. */
  struct CDTFace *face; /* Face on left side. */
} SymEdge;

typedef struct CDTVert {
  double co[2];        /* Coordinate. */
  SymEdge *symedge;    /* Some edge attached to it. */
  LinkNode *input_ids; /* List of corresponding vertex input ids. */
  int index;           /* Index into array that cdt keeps. */
  int merge_to_index;  /* Index of a CDTVert that this has merged to. -1 if no merge. */
  int visit_index;     /* Which visit epoch has this been seen. */
} CDTVert;

typedef struct CDTEdge {
  LinkNode *input_ids; /* List of input edge ids that this is part of. */
  SymEdge symedges[2]; /* The directed edges for this edge. */
  bool in_queue;       /* Used in flipping algorithm. */
} CDTEdge;

typedef struct CDTFace {
  SymEdge *symedge;    /* A symedge in face; only used during output, so only valid then. */
  LinkNode *input_ids; /* List of input face ids that this is part of. */
  int visit_index;     /* Which visit epoch has this been seen. */
  bool deleted;        /* Marks this face no longer used. */
  bool in_queue;       /* Used in remove_small_features algorithm. */
} CDTFace;

typedef struct CDT_state {
  LinkNode *edges;          /* List of CDTEdge pointer. */
  LinkNode *faces;          /* List of CDTFace pointer. */
  CDTFace *outer_face;      /* Which CDTFace is the outer face. */
  CDTVert **vert_array;     /* Array of CDTVert pointer, grows. */
  int vert_array_len;       /* Current length of vert_array. */
  int vert_array_len_alloc; /* Allocated length of vert_array. */
  int input_vert_tot;       /* How many verts were in input (will be first in vert_array). */
  double minx;              /* Used for debug drawing. */
  double miny;              /* Used for debug drawing. */
  double maxx;              /* Used for debug drawing. */
  double maxy;              /* Used for debug drawing. */
  double margin;            /* Used for debug drawing. */
  int visit_count; /* Used for visiting things without having to initialized their visit fields. */
  int face_edge_offset;  /* Input edge id where we start numbering the face edges. */
  MemArena *arena;       /* Most allocations are done from here, so can free all at once at end. */
  BLI_mempool *listpool; /* Allocations of ListNodes done from this pool. */
  double epsilon;        /* The user-specified nearness limit. */
  double epsilon_squared; /* Square of epsilon. */
  bool output_prepared;   /* Set after the mesh has been modified for output (may not be all
                             triangles now). */
} CDT_state;

#define DLNY_ARENASIZE 1 << 14

#ifdef DEBUG_CDT
#  ifdef __GNUC__
#    define ATTU __attribute__((unused))
#  else
#    define ATTU
#  endif
#  define F2(p) p[0], p[1]
#  define F3(p) p[0], p[1], p[2]
struct CrossData;
ATTU static void dump_se(const SymEdge *se, const char *lab);
ATTU static void dump_se_short(const SymEdge *se, const char *lab);
ATTU static void dump_v(const CDTVert *v, const char *lab);
ATTU static void dump_se_cycle(const SymEdge *se, const char *lab, const int limit);
ATTU static void dump_id_list(const LinkNode *id_list, const char *lab);
ATTU static void dump_cross_data(struct CrossData *cd, const char *lab);
ATTU static void dump_cdt(const CDT_state *cdt, const char *lab);
ATTU static void dump_cdt_vert_neighborhood(CDT_state *cdt, int v, int maxdist, const char *lab);
ATTU static void cdt_draw(CDT_state *cdt, const char *lab);
ATTU static void cdt_draw_region(
    CDT_state *cdt, const char *lab, double minx, double miny, double maxx, double maxy);

ATTU static void cdt_draw_vertex_region(CDT_state *cdt, int v, double dist, const char *lab);
ATTU static void cdt_draw_edge_region(
    CDT_state *cdt, int v1, int v2, double dist, const char *lab);
ATTU static void write_cdt_input_to_file(const CDT_input *inp);
ATTU static void validate_cdt(CDT_state *cdt,
                              bool check_all_tris,
                              bool check_delaunay,
                              bool check_visibility);
#endif

static void exactinit(void);
static double orient2d(const double *pa, const double *pb, const double *pc);
static double incircle(const double *pa, const double *pb, const double *pc, const double *pd);

/** Return other #SymEdge for same #CDTEdge as se. */
BLI_INLINE SymEdge *sym(const SymEdge *se)
{
  return se->next->rot;
}

/** Return SymEdge whose next is se. */
BLI_INLINE SymEdge *prev(const SymEdge *se)
{
  return se->rot->next->rot;
}

/**
 * Return true if a -- b -- c are in that order, assuming they are on a straight line according to
 * orient2d and we know the order is either `abc` or `bac`.
 * This means `ab . ac` and `bc . ac` must both be non-negative.  */
static bool in_line(const double a[2], const double b[2], const double c[2])
{
  double ab[2], bc[2], ac[2];
  sub_v2_v2v2_db(ab, b, a);
  sub_v2_v2v2_db(bc, c, b);
  sub_v2_v2v2_db(ac, c, a);
  if (dot_v2v2_db(ab, ac) < 0.0) {
    return false;
  }
  return dot_v2v2_db(bc, ac) >= 0.0;
}

#ifndef NDEBUG
/** Is s2 reachable from s1 by next pointers with < limit hops? */
static bool reachable(SymEdge *s1, SymEdge *s2, int limit)
{
  int count = 0;
  for (SymEdge *s = s1; s && count < limit; s = s->next) {
    if (s == s2) {
      return true;
    }
    count++;
  }
  return false;
}
#endif

/** Using array to store these instead of linked list so can make a random selection from them. */
static CDTVert *add_cdtvert(CDT_state *cdt, double x, double y)
{
  CDTVert *v = BLI_memarena_alloc(cdt->arena, sizeof(*v));
  v->co[0] = x;
  v->co[1] = y;
  v->input_ids = NULL;
  v->symedge = NULL;
  if (cdt->vert_array_len == cdt->vert_array_len_alloc) {
    CDTVert **old_array = cdt->vert_array;
    cdt->vert_array_len_alloc *= 4;
    cdt->vert_array = BLI_memarena_alloc(cdt->arena,
                                         cdt->vert_array_len_alloc * sizeof(cdt->vert_array[0]));
    memmove(cdt->vert_array, old_array, cdt->vert_array_len * sizeof(cdt->vert_array[0]));
  }
  BLI_assert(cdt->vert_array_len < cdt->vert_array_len_alloc);
  v->index = cdt->vert_array_len;
  v->merge_to_index = -1;
  v->visit_index = 0;
  cdt->vert_array[cdt->vert_array_len++] = v;
  return v;
}

static CDTEdge *add_cdtedge(
    CDT_state *cdt, CDTVert *v1, CDTVert *v2, CDTFace *fleft, CDTFace *fright)
{
  CDTEdge *e = BLI_memarena_alloc(cdt->arena, sizeof(*e));
  SymEdge *se = &e->symedges[0];
  SymEdge *sesym = &e->symedges[1];
  e->input_ids = NULL;
  e->in_queue = false;
  BLI_linklist_prepend_arena(&cdt->edges, (void *)e, cdt->arena);
  se->edge = sesym->edge = e;
  se->face = fleft;
  sesym->face = fright;
  se->vert = v1;
  if (v1->symedge == NULL) {
    v1->symedge = se;
  }
  sesym->vert = v2;
  if (v2->symedge == NULL) {
    v2->symedge = sesym;
  }
  se->next = sesym->next = se->rot = sesym->rot = NULL;
  return e;
}

static CDTFace *add_cdtface(CDT_state *cdt)
{
  CDTFace *f = BLI_memarena_alloc(cdt->arena, sizeof(*f));
  f->visit_index = 0;
  f->deleted = false;
  f->symedge = NULL;
  f->input_ids = NULL;
  f->in_queue = false;
  BLI_linklist_prepend_arena(&cdt->faces, (void *)f, cdt->arena);
  return f;
}

static bool id_in_list(const LinkNode *id_list, int id)
{
  const LinkNode *ln;

  for (ln = id_list; ln; ln = ln->next) {
    if (POINTER_AS_INT(ln->link) == id) {
      return true;
    }
  }
  return false;
}

/** is any id in (range_start, range_start+1, ... , range_end) in id_list? */
static bool id_range_in_list(const LinkNode *id_list, int range_start, int range_end)
{
  const LinkNode *ln;
  int id;

  for (ln = id_list; ln; ln = ln->next) {
    id = POINTER_AS_INT(ln->link);
    if (id >= range_start && id <= range_end) {
      return true;
    }
  }
  return false;
}

static void add_to_input_ids(LinkNode **dst, int input_id, CDT_state *cdt)
{
  if (!id_in_list(*dst, input_id)) {
    BLI_linklist_prepend_arena(dst, POINTER_FROM_INT(input_id), cdt->arena);
  }
}

static void add_list_to_input_ids(LinkNode **dst, const LinkNode *src, CDT_state *cdt)
{
  const LinkNode *ln;

  for (ln = src; ln; ln = ln->next) {
    add_to_input_ids(dst, POINTER_AS_INT(ln->link), cdt);
  }
}

BLI_INLINE bool is_border_edge(const CDTEdge *e, const CDT_state *cdt)
{
  return e->symedges[0].face == cdt->outer_face || e->symedges[1].face == cdt->outer_face;
}

BLI_INLINE bool is_constrained_edge(const CDTEdge *e)
{
  return e->input_ids != NULL;
}

BLI_INLINE bool is_deleted_edge(const CDTEdge *e)
{
  return e->symedges[0].next == NULL;
}

BLI_INLINE bool is_original_vert(const CDTVert *v, CDT_state *cdt)
{
  return (v->index < cdt->input_vert_tot);
}

/** Return the Symedge that goes from v1 to v2, if it exists, else return NULL. */
static SymEdge *find_symedge_between_verts(const CDTVert *v1, const CDTVert *v2)
{
  SymEdge *tstart, *t;

  t = tstart = v1->symedge;
  do {
    if (t->next->vert == v2) {
      return t;
    }
  } while ((t = t->rot) != tstart);
  return NULL;
}

/** Return the SymEdge attached to v that has face f, if it exists, else return NULL. */
static SymEdge *find_symedge_with_face(const CDTVert *v, const CDTFace *f)
{
  SymEdge *tstart, *t;

  t = tstart = v->symedge;
  do {
    if (t->face == f) {
      return t;
    }
  } while ((t = t->rot) != tstart);
  return NULL;
}

/** Is there already an edge between a and b? */
static inline bool exists_edge(const CDTVert *v1, const CDTVert *v2)
{
  return find_symedge_between_verts(v1, v2) != NULL;
}

/** Is the vertex v incident on face f? */
static bool vert_touches_face(const CDTVert *v, const CDTFace *f)
{
  SymEdge *se = v->symedge;
  do {
    if (se->face == f) {
      return true;
    }
  } while ((se = se->rot) != v->symedge);
  return false;
}

/**
 * Assume s1 and s2 are both SymEdges in a face with > 3 sides,
 * and one is not the next of the other.
 * Add an edge from s1->v to s2->v, splitting the face in two.
 * The original face will continue to be associated with the subface
 * that has s1, and a new face will be made for s2's new face.
 * Return the new diagonal's CDTEdge *.
 */
static CDTEdge *add_diagonal(CDT_state *cdt, SymEdge *s1, SymEdge *s2)
{
  CDTEdge *ediag;
  CDTFace *fold, *fnew;
  SymEdge *sdiag, *sdiagsym;
  SymEdge *s1prev, *s1prevsym, *s2prev, *s2prevsym, *se;
  BLI_assert(reachable(s1, s2, 20000));
  BLI_assert(reachable(s2, s1, 20000));
  fold = s1->face;
  fnew = add_cdtface(cdt);
  s1prev = prev(s1);
  s1prevsym = sym(s1prev);
  s2prev = prev(s2);
  s2prevsym = sym(s2prev);
  ediag = add_cdtedge(cdt, s1->vert, s2->vert, fnew, fold);
  sdiag = &ediag->symedges[0];
  sdiagsym = &ediag->symedges[1];
  sdiag->next = s2;
  sdiagsym->next = s1;
  s2prev->next = sdiagsym;
  s1prev->next = sdiag;
  s1->rot = sdiag;
  sdiag->rot = s1prevsym;
  s2->rot = sdiagsym;
  sdiagsym->rot = s2prevsym;
#ifdef DEBUG_CDT
  BLI_assert(reachable(s2, sdiag, 2000));
#endif
  for (se = s2; se != sdiag; se = se->next) {
    se->face = fnew;
  }
  add_list_to_input_ids(&fnew->input_ids, fold->input_ids, cdt);
  return ediag;
}

/**
 * Add a dangling edge from an isolated v to the vert at se in the same face as se->face.
 */
static CDTEdge *add_vert_to_symedge_edge(CDT_state *cdt, CDTVert *v, SymEdge *se)
{
  CDTEdge *e;
  SymEdge *se_rot, *se_rotsym, *new_se, *new_se_sym;

  se_rot = se->rot;
  se_rotsym = sym(se_rot);
  e = add_cdtedge(cdt, v, se->vert, se->face, se->face);
  new_se = &e->symedges[0];
  new_se_sym = &e->symedges[1];
  new_se->next = se;
  new_se_sym->next = new_se;
  new_se->rot = new_se;
  new_se_sym->rot = se_rot;
  se->rot = new_se_sym;
  se_rotsym->next = new_se_sym;
  return e;
}

/**
 * Connect the verts of se1 and se2, assuming that currently those two #SymEdges are on
 * the outer boundary (have face == outer_face) of two components that are isolated from
 * each other.
 */
static CDTEdge *connect_separate_parts(CDT_state *cdt, SymEdge *se1, SymEdge *se2)
{
  CDTEdge *e;
  SymEdge *se1_rot, *se1_rotsym, *se2_rot, *se2_rotsym, *new_se, *new_se_sym;

  BLI_assert(se1->face == cdt->outer_face && se2->face == cdt->outer_face);
  se1_rot = se1->rot;
  se1_rotsym = sym(se1_rot);
  se2_rot = se2->rot;
  se2_rotsym = sym(se2_rot);
  e = add_cdtedge(cdt, se1->vert, se2->vert, cdt->outer_face, cdt->outer_face);
  new_se = &e->symedges[0];
  new_se_sym = &e->symedges[1];
  new_se->next = se2;
  new_se_sym->next = se1;
  new_se->rot = se1_rot;
  new_se_sym->rot = se2_rot;
  se1->rot = new_se;
  se2->rot = new_se_sym;
  se1_rotsym->next = new_se;
  se2_rotsym->next = new_se_sym;
  return e;
}

/**
 * Split \a se at fraction \a lambda,
 * and return the new #CDTEdge that is the new second half.
 * Copy the edge input_ids into the new one.
 */
static CDTEdge *split_edge(CDT_state *cdt, SymEdge *se, double lambda)
{
  const double *a, *b;
  double p[2];
  CDTVert *v;
  CDTEdge *e;
  SymEdge *sesym, *newse, *newsesym, *senext, *sesymprev, *sesymprevsym;
  /* Split e at lambda. */
  a = se->vert->co;
  b = se->next->vert->co;
  sesym = sym(se);
  sesymprev = prev(sesym);
  sesymprevsym = sym(sesymprev);
  senext = se->next;
  p[0] = (1.0 - lambda) * a[0] + lambda * b[0];
  p[1] = (1.0 - lambda) * a[1] + lambda * b[1];
  v = add_cdtvert(cdt, p[0], p[1]);
  e = add_cdtedge(cdt, v, se->next->vert, se->face, sesym->face);
  sesym->vert = v;
  newse = &e->symedges[0];
  newsesym = &e->symedges[1];
  se->next = newse;
  newsesym->next = sesym;
  newse->next = senext;
  newse->rot = sesym;
  sesym->rot = newse;
  senext->rot = newsesym;
  newsesym->rot = sesymprevsym;
  sesymprev->next = newsesym;
  if (newsesym->vert->symedge == sesym) {
    newsesym->vert->symedge = newsesym;
  }
  add_list_to_input_ids(&e->input_ids, se->edge->input_ids, cdt);
  return e;
}

/**
 * Delete an edge from the structure. The new combined face on either side of
 * the deleted edge will be the one that was e's face.
 * There will be now an unused face, marked by setting its deleted flag,
 * and an unused #CDTEdge, marked by setting the next and rot pointers of
 * its #SymEdge(s) to NULL.
 * <pre>
 *        .  v2               .
 *       / \                 / \
 *      /f|j\               /   \
 *     /  |  \             /     \
 *        |
 *      A |  B                A
 *    \  e|   /           \       /
 *     \  | /              \     /
 *      \h|i/               \   /
 *        .  v1               .
 * </pre>
 * Also handle variant cases where one or both ends
 * are attached only to e.
 */
static void delete_edge(CDT_state *cdt, SymEdge *e)
{
  SymEdge *esym, *f, *h, *i, *j, *k, *jsym, *hsym;
  CDTFace *aface, *bface;
  CDTVert *v1, *v2;
  bool v1_isolated, v2_isolated;

  esym = sym(e);
  v1 = e->vert;
  v2 = esym->vert;
  aface = e->face;
  bface = esym->face;
  f = e->next;
  h = prev(e);
  i = esym->next;
  j = prev(esym);
  jsym = sym(j);
  hsym = sym(h);
  v1_isolated = (i == e);
  v2_isolated = (f == esym);

  if (!v1_isolated) {
    h->next = i;
    i->rot = hsym;
  }
  if (!v2_isolated) {
    j->next = f;
    f->rot = jsym;
  }
  if (!v1_isolated && !v2_isolated && aface != bface) {
    for (k = i; k != f; k = k->next) {
      k->face = aface;
    }
  }

  /* If e was representative symedge for v1 or v2, fix that. */
  if (v1_isolated) {
    v1->symedge = NULL;
  }
  else if (v1->symedge == e) {
    v1->symedge = i;
  }
  if (v2_isolated) {
    v2->symedge = NULL;
  }
  else if (v2->symedge == esym) {
    v2->symedge = f;
  }

  /* Mark SymEdge as deleted by setting all its pointers to NULL. */
  e->next = e->rot = NULL;
  esym->next = esym->rot = NULL;
  if (!v1_isolated && !v2_isolated && aface != bface) {
    bface->deleted = true;
    if (cdt->outer_face == bface) {
      cdt->outer_face = aface;
    }
  }
}

static CDT_state *cdt_init(const CDT_input *in)
{
  int i;
  MemArena *arena = BLI_memarena_new(DLNY_ARENASIZE, __func__);
  CDT_state *cdt = BLI_memarena_calloc(arena, sizeof(CDT_state));

  cdt->epsilon = (double)in->epsilon;
  cdt->epsilon_squared = cdt->epsilon * cdt->epsilon;
  cdt->arena = arena;
  cdt->input_vert_tot = in->verts_len;
  cdt->vert_array_len_alloc = 2 * in->verts_len;
  cdt->vert_array = BLI_memarena_alloc(arena,
                                       cdt->vert_array_len_alloc * sizeof(*cdt->vert_array));
  cdt->listpool = BLI_mempool_create(
      sizeof(LinkNode), 128 + 4 * in->verts_len, 128 + in->verts_len, 0);

  for (i = 0; i < in->verts_len; i++) {
    add_cdtvert(cdt, (double)(in->vert_coords[i][0]), (double)(in->vert_coords[i][1]));
  }
  cdt->outer_face = add_cdtface(cdt);
  return cdt;
}

static void new_cdt_free(CDT_state *cdt)
{
  BLI_mempool_destroy(cdt->listpool);
  BLI_memarena_free(cdt->arena);
}

typedef struct SiteInfo {
  CDTVert *v;
  int orig_index;
} SiteInfo;

static int site_lexicographic_cmp(const void *a, const void *b)
{
  const SiteInfo *s1 = a;
  const SiteInfo *s2 = b;
  const double *co1 = s1->v->co;
  const double *co2 = s2->v->co;

  if (co1[0] < co2[0]) {
    return -1;
  }
  if (co1[0] > co2[0]) {
    return 1;
  }
  if (co1[1] < co2[1]) {
    return -1;
  }
  if (co1[1] > co2[1]) {
    return 1;
  }
  if (s1->orig_index < s2->orig_index) {
    return -1;
  }
  if (s1->orig_index > s2->orig_index) {
    return 1;
  }
  return 0;
}

BLI_INLINE bool vert_left_of_symedge(CDTVert *v, SymEdge *se)
{
  return orient2d(v->co, se->vert->co, se->next->vert->co) > 0.0;
}

BLI_INLINE bool vert_right_of_symedge(CDTVert *v, SymEdge *se)
{
  return orient2d(v->co, se->next->vert->co, se->vert->co) > 0.0;
}

/* Is se above basel? */
BLI_INLINE bool dc_tri_valid(SymEdge *se, SymEdge *basel, SymEdge *basel_sym)
{
  return orient2d(se->next->vert->co, basel_sym->vert->co, basel->vert->co) > 0.0;
}

/* Delaunay triangulate sites[start} to sites[end-1].
 * Assume sites are lexicographically sorted by coordinate.
 * Return SymEdge of ccw convex hull at left-most point in *r_le
 * and that of right-most point of cw convex null in *r_re.
 */
static void dc_tri(
    CDT_state *cdt, SiteInfo *sites, int start, int end, SymEdge **r_le, SymEdge **r_re)
{
  int n = end - start;
  int n2;
  CDTVert *v1, *v2, *v3;
  CDTEdge *ea, *eb, *ebasel;
  SymEdge *ldo, *ldi, *rdi, *rdo, *basel, *basel_sym, *lcand, *rcand, *t;
  double orient;
  bool valid_lcand, valid_rcand;
#ifdef DEBUG_CDT
  char label_buf[100];
  int dbg_level = 0;

  if (dbg_level > 0) {
    fprintf(stderr, "DC_TRI start=%d end=%d\n", start, end);
  }
#endif

  BLI_assert(r_le != NULL && r_re != NULL);
  if (n <= 1) {
    *r_le = NULL;
    *r_re = NULL;
    return;
  }
  if (n <= 3) {
    v1 = sites[start].v;
    v2 = sites[start + 1].v;
    ea = add_cdtedge(cdt, v1, v2, cdt->outer_face, cdt->outer_face);
    ea->symedges[0].next = &ea->symedges[1];
    ea->symedges[1].next = &ea->symedges[0];
    ea->symedges[0].rot = &ea->symedges[0];
    ea->symedges[1].rot = &ea->symedges[1];
    if (n == 2) {
      *r_le = &ea->symedges[0];
      *r_re = &ea->symedges[1];
      return;
    }
    v3 = sites[start + 2].v;
    eb = add_vert_to_symedge_edge(cdt, v3, &ea->symedges[1]);
    orient = orient2d(v1->co, v2->co, v3->co);
    if (orient > 0.0) {
      add_diagonal(cdt, &eb->symedges[0], &ea->symedges[0]);
      *r_le = &ea->symedges[0];
      *r_re = &eb->symedges[0];
    }
    else if (orient < 0.0) {
      add_diagonal(cdt, &ea->symedges[0], &eb->symedges[0]);
      *r_le = ea->symedges[0].rot;
      *r_re = eb->symedges[0].rot;
    }
    else {
      /* Collinear points. Just return a line. */
      *r_le = &ea->symedges[0];
      *r_re = &eb->symedges[0];
    }
    return;
  }
  /* Here: n >= 4.  Divide and conquer. */
  n2 = n / 2;
  BLI_assert(n2 >= 2 && end - (start + n2) >= 2);

  /* Delaunay triangulate two halves, L and R. */
  dc_tri(cdt, sites, start, start + n2, &ldo, &ldi);
  dc_tri(cdt, sites, start + n2, end, &rdi, &rdo);
#ifdef DEBUG_CDT
  if (dbg_level > 0) {
    fprintf(stderr, "\nDC_TRI merge step for start=%d, end=%d\n", start, end);
    dump_se(ldo, "ldo");
    dump_se(ldi, "ldi");
    dump_se(rdi, "rdi");
    dump_se(rdo, "rdo");
    if (dbg_level > 1) {
      sprintf(label_buf, "dc_tri(%d,%d)(%d,%d)", start, start + n2, start + n2, end);
      /* dump_cdt(cdt, label_buf); */
      cdt_draw(cdt, label_buf);
    }
  }
#endif

  /* Find lower common tangent of L and R. */
  for (;;) {
    if (vert_left_of_symedge(rdi->vert, ldi)) {
      ldi = ldi->next;
    }
    else if (vert_right_of_symedge(ldi->vert, rdi)) {
      rdi = sym(rdi)->rot; /* Previous edge to rdi with same right face. */
    }
    else {
      break;
    }
  }
#ifdef DEBUG_CDT
  if (dbg_level > 0) {
    fprintf(stderr, "common lower tangent is between\n");
    dump_se(rdi, "rdi");
    dump_se(ldi, "ldi");
  }
#endif
  ebasel = connect_separate_parts(cdt, sym(rdi)->next, ldi);
  basel = &ebasel->symedges[0];
  basel_sym = &ebasel->symedges[1];
#ifdef DEBUG_CDT
  if (dbg_level > 1) {
    dump_se(basel, "basel");
    cdt_draw(cdt, "after basel made");
  }
#endif
  if (ldi->vert == ldo->vert) {
    ldo = basel_sym;
  }
  if (rdi->vert == rdo->vert) {
    rdo = basel;
  }

  /* Merge loop. */
  for (;;) {
    /* Locate the first point lcand->next->vert encountered by rising bubble,
     * and delete L edges out of basel->next->vert that fail the circle test. */
    lcand = basel_sym->rot;
    rcand = basel_sym->next;
#ifdef DEBUG_CDT
    if (dbg_level > 1) {
      fprintf(stderr, "\ntop of merge loop\n");
      dump_se(lcand, "lcand");
      dump_se(rcand, "rcand");
      dump_se(basel, "basel");
    }
#endif
    if (dc_tri_valid(lcand, basel, basel_sym)) {
#ifdef DEBUG_CDT
      if (dbg_level > 1) {
        fprintf(stderr, "found valid lcand\n");
        dump_se(lcand, "  lcand");
      }
#endif
      while (incircle(basel_sym->vert->co,
                      basel->vert->co,
                      lcand->next->vert->co,
                      lcand->rot->next->vert->co) > 0.0) {
#ifdef DEBUG_CDT
        if (dbg_level > 1) {
          fprintf(stderr, "incircle says to remove lcand\n");
          dump_se(lcand, "  lcand");
        }
#endif
        t = lcand->rot;
        delete_edge(cdt, sym(lcand));
        lcand = t;
      }
    }
    /* Symmetrically, locate first R point to be hit and delete R edges. */
    if (dc_tri_valid(rcand, basel, basel_sym)) {
#ifdef DEBUG_CDT
      if (dbg_level > 1) {
        fprintf(stderr, "found valid rcand\n");
        dump_se(rcand, "  rcand");
      }
#endif
      while (incircle(basel_sym->vert->co,
                      basel->vert->co,
                      rcand->next->vert->co,
                      sym(rcand)->next->next->vert->co) > 0.0) {
#ifdef DEBUG_CDT
        if (dbg_level > 0) {
          fprintf(stderr, "incircle says to remove rcand\n");
          dump_se(lcand, "  rcand");
        }
#endif
        t = sym(rcand)->next;
        delete_edge(cdt, rcand);
        rcand = t;
      }
    }
    /* If both lcand and rcand are invalid, then basel is the common upper tangent. */
    valid_lcand = dc_tri_valid(lcand, basel, basel_sym);
    valid_rcand = dc_tri_valid(rcand, basel, basel_sym);
#ifdef DEBUG_CDT
    if (dbg_level > 0) {
      fprintf(
          stderr, "after bubbling up, valid_lcand=%d, valid_rcand=%d\n", valid_lcand, valid_rcand);
      dump_se(lcand, "lcand");
      dump_se(rcand, "rcand");
    }
#endif
    if (!valid_lcand && !valid_rcand) {
      break;
    }
    /* The next cross edge to be connected is to either lcand->next->vert or rcand->next->vert;
     * if both are valid, choose the appropriate one using the incircle test.
     */
    if (!valid_lcand ||
        (valid_rcand &&
         incircle(lcand->next->vert->co, lcand->vert->co, rcand->vert->co, rcand->next->vert->co) >
             0.0)) {
#ifdef DEBUG_CDT
      if (dbg_level > 0) {
        fprintf(stderr, "connecting rcand\n");
        dump_se(basel_sym, "  se1=basel_sym");
        dump_se(rcand->next, "  se2=rcand->next");
      }
#endif
      ebasel = add_diagonal(cdt, rcand->next, basel_sym);
    }
    else {
#ifdef DEBUG_CDT
      if (dbg_level > 0) {
        fprintf(stderr, "connecting lcand\n");
        dump_se(sym(lcand), "  se1=sym(lcand)");
        dump_se(basel_sym->next, "  se2=basel_sym->next");
      }
#endif
      ebasel = add_diagonal(cdt, basel_sym->next, sym(lcand));
    }
    basel = &ebasel->symedges[0];
    basel_sym = &ebasel->symedges[1];
    BLI_assert(basel_sym->face == cdt->outer_face);
#ifdef DEBUG_CDT
    if (dbg_level > 2) {
      cdt_draw(cdt, "after adding new crossedge");
      // dump_cdt(cdt, "after adding new crossedge");
    }
#endif
  }
  *r_le = ldo;
  *r_re = rdo;
  BLI_assert(sym(ldo)->face == cdt->outer_face && rdo->face == cdt->outer_face);
}

/* Guibas-Stolfi Divide-and_Conquer algorithm. */
static void dc_triangulate(CDT_state *cdt, SiteInfo *sites, int nsites)
{
  int i, j, n;
  SymEdge *le, *re;

  /* Compress sites in place to eliminated verts that merge to others. */
  i = 0;
  j = 0;
  while (j < nsites) {
    /* Invariante: sites[0..i-1] have non-merged verts from 0..(j-1) in them. */
    sites[i] = sites[j++];
    if (sites[i].v->merge_to_index < 0) {
      i++;
    }
  }
  n = i;
  if (n == 0) {
    return;
  }
  dc_tri(cdt, sites, 0, n, &le, &re);
}

/**
 * Do a Delaunay Triangulation of the points in cdt->vert_array.
 * This  is only a first step in the Constrained Delaunay triangulation,
 * because it doesn't yet deal with the segment constraints.
 * The algorithm used is the Divide & Conquer algorithm from the
 * Guibas-Stolfi "Primitives for the Manipulation of General Subdivision
 * and the Computation of Voronoi Diagrams" paper.
 * The data structure here is similar to but not exactly the same as
 * the quad-edge structure described in that paper.
 * The incircle and ccw tests are done using Shewchuk's exact
 * primitives (see below), so that this routine is robust.
 *
 * As a preprocessing step, we want to merge all vertices that are
 * within cdt->epsilon of each other. This is accomplished by lexicographically
 * sorting the coordinates first (which is needed anyway for the D&C algorithm).
 * The CDTVerts with merge_to_index not equal to -1 are after this regarded
 * as having been merged into the vertex with the corresponding index.
 */
static void initial_triangulation(CDT_state *cdt)
{
  int i, j, n;
  SiteInfo *sites;
  double *ico, *jco;
  double xend, yend, xcur;
  double epsilon = cdt->epsilon;
  double epsilon_squared = cdt->epsilon_squared;
#ifdef SJF_WAY
  CDTEdge *e;
  CDTVert *va, *vb;
#endif
#ifdef DEBUG_CDT
  int dbg_level = 0;

  if (dbg_level > 0) {
    fprintf(stderr, "\nINITIAL TRIANGULATION\n\n");
  }
#endif

  /* First sort the vertices by lexicographic order of their
   * coordinates, breaking ties by putting earlier original-index
   * vertices first.
   */
  n = cdt->vert_array_len;
  if (n <= 1) {
    return;
  }
  sites = MEM_malloc_arrayN(n, sizeof(SiteInfo), __func__);
  for (i = 0; i < n; i++) {
    sites[i].v = cdt->vert_array[i];
    sites[i].orig_index = i;
  }
  qsort(sites, n, sizeof(SiteInfo), site_lexicographic_cmp);
#ifdef DEBUG_CDT
  if (dbg_level > 0) {
    fprintf(stderr, "after sorting\n");
    for (i = 0; i < n; i++) {
      fprintf(stderr, "%d: orig index: %d, (%f,%f)\n", i, sites[i].orig_index, F2(sites[i].v->co));
    }
  }
#endif

  /* Now de-duplicate according to user-defined epsilon.
   * We will merge a vertex into an earlier-indexed vertex
   * that is within epsilon (Euclidean distance).
   * Merges may cascade. So we may end up merging two things
   * that are farther than epsilon by transitive merging. Oh well.
   * Assume that merges are rare, so use simple searches in the
   * lexicographic ordering - likely we will soon hit y's with
   * the same x that are farther away than epsilon, and then
   * skipping ahead to the next biggest x, are likely to soon
   * find one of those farther away than epsilon.
   */
  for (i = 0; i < n - 1; i++) {
    ico = sites[i].v->co;
    /* Start j at next place that has both x and y coords within epsilon. */
    xend = ico[0] + epsilon;
    yend = ico[1] + epsilon;
    j = i + 1;
    while (j < n) {
      jco = sites[j].v->co;
      if (jco[0] > xend) {
        break; /* No more j's to process. */
      }
      if (jco[1] > yend) {
        /* Get past any string of v's with the same x and too-big y. */
        xcur = jco[0];
        while (++j < n) {
          if (sites[j].v->co[0] > xcur) {
            break;
          }
        }
        BLI_assert(j == n || sites[j].v->co[0] > xcur);
        if (j == n) {
          break;
        }
        jco = sites[j].v->co;
        if (jco[0] > xend || jco[1] > yend) {
          break;
        }
      }
      /* When here, vertex i and j are within epsilon by box test.
       * The Euclidean distance test is stricter, so need to do it too, now.
       */
      BLI_assert(j < n && jco[0] <= xend && jco[1] <= yend);
      if (len_squared_v2v2_db(ico, jco) <= epsilon_squared) {
        sites[j].v->merge_to_index = (sites[i].v->merge_to_index == -1) ?
                                         sites[i].orig_index :
                                         sites[i].v->merge_to_index;
#ifdef DEBUG_CDT
        if (dbg_level > 0) {
          fprintf(stderr,
                  "merged orig vert %d to %d\n",
                  sites[j].orig_index,
                  sites[j].v->merge_to_index);
        }
#endif
      }
      j++;
    }
  }

  /* Now add non-dup vertices into triangulation in lexicographic order. */

  dc_triangulate(cdt, sites, n);
  MEM_freeN(sites);
}

/**
 * Use #LinkNode linked list as stack of #SymEdges, allocating from `cdt->listpool` .
 */
typedef LinkNode *Stack;

BLI_INLINE void push(Stack *stack, SymEdge *se, CDT_state *cdt)
{
  BLI_linklist_prepend_pool(stack, se, cdt->listpool);
}

BLI_INLINE SymEdge *pop(Stack *stack, CDT_state *cdt)
{
  return (SymEdge *)BLI_linklist_pop_pool(stack, cdt->listpool);
}

BLI_INLINE bool is_empty(Stack *stack)
{
  return *stack == NULL;
}

/**
 * Re-triangulates, assuring constrained delaunay condition,
 * the pseudo-polygon that cycles from se.
 * "pseudo" because a vertex may be repeated.
 * See Anglada paper, "An Improved incremental algorithm
 * for constructing restricted Delaunay triangulations".
 */
static void re_delaunay_triangulate(CDT_state *cdt, SymEdge *se)
{
  SymEdge *ss, *first, *cse;
  CDTVert *a, *b, *c, *v;
  CDTEdge *ebc, *eca;
  int count;
#ifdef DEBUG_CDT
  SymEdge *last;
  const int dbg_level = 0;
#endif

  if (se->face == cdt->outer_face || sym(se)->face == cdt->outer_face) {
    return;
  }
#ifdef DEBUG_CDT
  if (dbg_level > 0) {
    fprintf(stderr, "retriangulate");
    dump_se_cycle(se, "poly ", 1000);
  }
#endif
  /* 'se' is a diagonal just added, and it is base of area to retriangulate (face on its left) */
  count = 1;
  for (ss = se->next; ss != se; ss = ss->next) {
    count++;
  }
  if (count <= 3) {
#ifdef DEBUG_CDT
    if (dbg_level > 0) {
      fprintf(stderr, "nothing to do\n");
    }
#endif
    return;
  }
  /* First and last are the SymEdges whose verts are first and last off of base,
   * continuing from 'se'. */
  first = se->next->next;
  /* We want to make a triangle with 'se' as base and some other c as 3rd vertex. */
  a = se->vert;
  b = se->next->vert;
  c = first->vert;
  cse = first;
#ifdef DEBUG_CDT
  last = prev(se);
  if (dbg_level > 1) {
    dump_se(first, "first");
    dump_se(last, "last");
    dump_v(a, "a");
    dump_v(b, "b");
    dump_v(c, "c");
  }
#endif
  for (ss = first->next; ss != se; ss = ss->next) {
    v = ss->vert;
    if (incircle(a->co, b->co, c->co, v->co) > 0.0) {
      c = v;
      cse = ss;
#ifdef DEBUG_CDT
      if (dbg_level > 1) {
        dump_v(c, "new c ");
      }
#endif
    }
  }
  /* Add diagonals necessary to make abc a triangle. */
#ifdef DEBUG_CDT
  if (dbg_level > 0) {
    fprintf(stderr, "make triangle abc exist where\n");
    dump_v(a, "  a");
    dump_v(b, "  b");
    dump_v(c, "  c");
  }
#endif
  ebc = NULL;
  eca = NULL;
  if (!exists_edge(b, c)) {
    ebc = add_diagonal(cdt, se->next, cse);
#ifdef DEBUG_CDT
    if (dbg_level > 1) {
      fprintf(stderr, "added edge ebc\n");
      dump_se(&ebc->symedges[0], "  ebc");
    }
#endif
  }
  if (!exists_edge(c, a)) {
    eca = add_diagonal(cdt, cse, se);
#ifdef DEBUG_CDT
    if (dbg_level > 1) {
      fprintf(stderr, "added edge eca\n");
      dump_se(&eca->symedges[0], "  eca");
    }
#endif
  }
  /* Now recurse. */
  if (ebc) {
    re_delaunay_triangulate(cdt, &ebc->symedges[1]);
  }
  if (eca) {
    re_delaunay_triangulate(cdt, &eca->symedges[1]);
  }
}

static double tri_orient(const SymEdge *t)
{
  return orient2d(t->vert->co, t->next->vert->co, t->next->next->vert->co);
}

/**
 * The CrossData struct gives defines either an endpoint or an intermediate point
 * in the path we will take to insert an edge constraint.
 * Each such point will either be
 * (a) a vertex or
 * (b) a fraction lambda (0 < lambda < 1) along some #SymEdge.]
 *
 * In general, lambda=0 indicates case a and lambda != 0 indicates case be.
 * The 'in' edge gives the destination attachment point of a diagonal from the previous crossing,
 * and the 'out' edge gives the origin attachment point of a diagonal to the next crossing.
 * But in some cases, 'in' and 'out' are undefined or not needed, and will be NULL.
 *
 * For case (a), 'vert' will be the vertex, and lambda will be 0, and 'in' will be the #SymEdge
 * from 'vert' that has as face the one that you go through to get to this vertex. If you go
 * exactly along an edge then we set 'in' to NULL, since it won't be needed. The first crossing
 * will have 'in' = NULL. We set 'out' to the #SymEdge that has the face we go though to get to the
 * next crossing, or, if the next crossing is a case (a), then it is the edge that goes to that
 * next vertex. 'out' wlll be NULL for the last one.
 *
 * For case (b), vert will be NULL at first, and later filled in with the created split vertex,
 * and 'in' will be the #SymEdge that we go through, and lambda will be between 0 and 1,
 * the fraction from in's vert to in->next's vert to put the split vertex.
 * 'out' is not needed in this case, since the attachment point will be the sym of the first
 * half of the split edge.
 */
typedef struct CrossData {
  double lambda;
  CDTVert *vert;
  SymEdge *in;
  SymEdge *out;
} CrossData;

static bool get_next_crossing_from_vert(CDT_state *cdt,
                                        CrossData *cd,
                                        CrossData *cd_next,
                                        const CDTVert *v2);

/**
 * As part of finding crossings, we found a case where the next crossing goes through vert v.
 * If it came from a previous vert in cd, then cd_out is the edge that leads from that to v.
 * Else cd_out can be NULL, because it won't be used.
 * Set *cd_next to indicate this. We can set 'in' but not 'out'.  We can set the 'out' of the
 * current cd.
 */
static void fill_crossdata_for_through_vert(CDTVert *v,
                                            SymEdge *cd_out,
                                            CrossData *cd,
                                            CrossData *cd_next)
{
  SymEdge *se;
#ifdef DEBUG_CDT
  int dbg_level = 0;
#endif

  cd_next->lambda = 0.0;
  cd_next->vert = v;
  cd_next->in = NULL;
  cd_next->out = NULL;
  if (cd->lambda == 0.0) {
    cd->out = cd_out;
  }
  else {
    /* One of the edges in the triangle with edge sym(cd->in) contains v. */
    se = sym(cd->in);
    if (se->vert != v) {
      se = se->next;
      if (se->vert != v) {
        se = se->next;
      }
    }
    BLI_assert(se->vert == v);
    cd_next->in = se;
  }
#ifdef DEBUG_CDT
  if (dbg_level > 0) {
    dump_cross_data(cd, "cd through vert, cd");
    dump_cross_data(cd_next, "cd_next through vert, cd");
  }
#endif
}

/**
 * As part of finding crossings, we found a case where orient tests say that the next crossing
 * is on the #SymEdge t, while intersecting with the ray from \a curco to \a v2.
 * Find the intersection point and fill in the #CrossData for that point.
 * It may turn out that when doing the intersection, we get an answer that says that
 * this case is better handled as through-vertex case instead, so we may do that.
 * In the latter case, we want to avoid a situation where the current crossing is on an edge
 * and the next will be an endpoint of the same edge. When that happens, we "rewrite history"
 * and turn the current crossing into a vert one, and then extend from there.
 *
 * We cannot fill cd_next's 'out' edge yet, in the case that the next one ends up being a vert
 * case. We need to fill in cd's 'out' edge if it was a vert case.
 */
static void fill_crossdata_for_intersect(CDT_state *cdt,
                                         const double *curco,
                                         const CDTVert *v2,
                                         SymEdge *t,
                                         CrossData *cd,
                                         CrossData *cd_next)
{
  CDTVert *va, *vb, *vc;
  double lambda, mu, len_ab;
  SymEdge *se_vcva, *se_vcvb;
  int isect;
#ifdef DEBUG_CDT
  int dbg_level = 0;
#endif

  va = t->vert;
  vb = t->next->vert;
  vc = t->next->next->vert;
  se_vcvb = sym(t->next);
  se_vcva = t->next->next;
  BLI_assert(se_vcva->vert == vc && se_vcva->next->vert == va);
  BLI_assert(se_vcvb->vert == vc && se_vcvb->next->vert == vb);
  UNUSED_VARS_NDEBUG(vc);
  isect = isect_seg_seg_v2_lambda_mu_db(va->co, vb->co, curco, v2->co, &lambda, &mu);
#ifdef DEBUG_CDT
  if (dbg_level > 0) {
    double co[2];
    fprintf(stderr, "crossdata for intersect gets lambda=%.17g, mu=%.17g\n", lambda, mu);
    fprintf(stderr,
            "isect=%s\n",
            isect == 2 ? "cross" : (isect == 1 ? "exact" : (isect == 0 ? "none" : "colinear")));
    fprintf(stderr,
            "va=v%d=(%g,%g), vb=v%d=(%g,%g), vc=v%d, curco=(%g,%g), v2=(%g,%g)\n",
            va->index,
            F2(va->co),
            vb->index,
            F2(vb->co),
            vc->index,
            F2(curco),
            F2(v2->co));
    dump_se_short(se_vcva, "vcva=");
    dump_se_short(se_vcvb, " vcvb=");
    interp_v2_v2v2_db(co, va->co, vb->co, lambda);
    fprintf(stderr, "\nco=(%.17g,%.17g)\n", F2(co));
  }
#endif
  switch (isect) {
    case ISECT_LINE_LINE_CROSS:
      len_ab = len_v2v2_db(va->co, vb->co);
#ifdef DEBUG_CDT
      if (dbg_level > 0) {
        fprintf(stderr,
                "len_ab=%g, near a=%g, near b=%g\n",
                len_ab,
                lambda * len_ab,
                (1.0 - lambda) * len_ab);
      }
#endif
      if (lambda * len_ab <= cdt->epsilon) {
        fill_crossdata_for_through_vert(va, se_vcva, cd, cd_next);
      }
      else if ((1.0 - lambda) * len_ab <= cdt->epsilon) {
        fill_crossdata_for_through_vert(vb, se_vcvb, cd, cd_next);
      }
      else {
        *cd_next = (CrossData){lambda, NULL, t, NULL};
        if (cd->lambda == 0.0) {
          cd->out = se_vcva;
        }
      }
      break;
    case ISECT_LINE_LINE_EXACT:
      if (lambda == 0.0) {
        fill_crossdata_for_through_vert(va, se_vcva, cd, cd_next);
      }
      else if (lambda == 1.0) {
        fill_crossdata_for_through_vert(vb, se_vcvb, cd, cd_next);
      }
      else {
        *cd_next = (CrossData){lambda, NULL, t, NULL};
        if (cd->lambda == 0.0) {
          cd->out = se_vcva;
        }
      }
      break;
    case ISECT_LINE_LINE_NONE:
      /* It should be very near one end or other of segment. */
      if (lambda <= 0.5) {
        fill_crossdata_for_through_vert(va, se_vcva, cd, cd_next);
      }
      else {
        fill_crossdata_for_through_vert(vb, se_vcvb, cd, cd_next);
      }
      break;
    case ISECT_LINE_LINE_COLINEAR:
      if (len_squared_v2v2_db(va->co, v2->co) <= len_squared_v2v2_db(vb->co, v2->co)) {
        fill_crossdata_for_through_vert(va, se_vcva, cd, cd_next);
      }
      else {
        fill_crossdata_for_through_vert(vb, se_vcvb, cd, cd_next);
      }
      break;
  }
}

/**
 * As part of finding the crossings of a ray to v2, find the next crossing after 'cd', assuming
 * 'cd' represents a crossing that goes through a vertex.
 *
 * We do a rotational scan around cd's vertex, looking for the triangle where the ray from cd->vert
 * to v2 goes between the two arms from cd->vert, or where it goes along one of the edges.
 */
static bool get_next_crossing_from_vert(CDT_state *cdt,
                                        CrossData *cd,
                                        CrossData *cd_next,
                                        const CDTVert *v2)
{
  SymEdge *t, *tstart;
  CDTVert *vcur, *va, *vb;
  double orient1, orient2;
  bool ok;
#ifdef DEBUG_CDT
  int dbg_level = 0;

  if (dbg_level > 0) {
    fprintf(stderr, "\nget_next_crossing_from_vert\n");
    dump_v(cd->vert, "  cd->vert");
  }
#endif

  t = tstart = cd->vert->symedge;
  vcur = cd->vert;
  ok = false;
  do {
    /*
     * The ray from vcur to v2 has to go either between two successive
     * edges around vcur or exactly along them. This time through the
     * loop, check to see if the ray goes along vcur-va
     * or between vcur-va and vcur-vb, where va is the end of t
     * and vb is the next vertex (on the next rot edge around vcur, but
     * should also be the next vert of triangle starting with vcur-va.
     */
    if (t->face != cdt->outer_face && tri_orient(t) < 0.0) {
      fprintf(stderr, "BAD TRI orientation %g\n", tri_orient(t)); /* TODO: handle this */
#ifdef DEBUG_CDT
      dump_se_cycle(t, "top of ccw scan loop", 4);
#endif
    }
    va = t->next->vert;
    vb = t->next->next->vert;
    orient1 = orient2d(t->vert->co, va->co, v2->co);
#ifdef DEBUG_CDT
    if (dbg_level > 1) {
      fprintf(stderr, "non-final through vert case\n");
      dump_v(va, " va");
      dump_v(vb, " vb");
      fprintf(stderr, "orient1=%g\n", orient1);
    }
#endif
    if (orient1 == 0.0 && in_line(vcur->co, va->co, v2->co)) {
#ifdef DEBUG_CDT
      if (dbg_level > 1) {
        fprintf(stderr, "ray goes through va\n");
      }
#endif
      fill_crossdata_for_through_vert(va, t, cd, cd_next);
      ok = true;
      break;
    }
    if (t->face != cdt->outer_face) {
      orient2 = orient2d(vcur->co, vb->co, v2->co);
#ifdef DEBUG_CDT
      if (dbg_level > 1) {
        fprintf(stderr, "orient2=%g\n", orient2);
      }
#endif
      /* Don't handle orient2 == 0.0 case here: next rotation will get it. */
      if (orient1 > 0.0 && orient2 < 0.0) {
#ifdef DEBUG_CDT
        if (dbg_level > 1) {
          fprintf(stderr, "segment intersects\n");
        }
#endif
        t = t->next;
        fill_crossdata_for_intersect(cdt, vcur->co, v2, t, cd, cd_next);
        ok = true;
        break;
      }
    }
  } while ((t = t->rot) != tstart);
#ifdef DEBUG_CDT
  if (!ok) {
    /* Didn't find the exit! Shouldn't happen. */
    fprintf(stderr, "shouldn't happen: can't find next crossing from vert\n");
  }
#endif
  return ok;
}

/**
 * As part of finding the crossings of a ray to 'v2', find the next crossing after 'cd', assuming
 * 'cd' represents a crossing that goes through a an edge, not at either end of that edge.
 *
 * We have the triangle 'vb-va-vc', where va and vb are the split edge and 'vc' is the third vertex
 * on that new side of the edge (should be closer to v2). The next crossing should be through 'vc'
 * or intersecting 'vb-vc' or 'va-vc'.
 */
static void get_next_crossing_from_edge(CDT_state *cdt,
                                        CrossData *cd,
                                        CrossData *cd_next,
                                        const CDTVert *v2)
{
  double curco[2];
  double orient;
  CDTVert *va, *vb, *vc;
  SymEdge *se_ac;
#ifdef DEBUG_CDT
  int dbg_level = 0;

  if (dbg_level > 0) {
    fprintf(stderr, "\nget_next_crossing_from_edge\n");
    fprintf(stderr, "  lambda=%.17g\n", cd->lambda);
    dump_se_short(cd->in, "  cd->in");
  }
#endif

  va = cd->in->vert;
  vb = cd->in->next->vert;
  interp_v2_v2v2_db(curco, va->co, vb->co, cd->lambda);
#ifdef DEBUG_CDT
  if (dbg_level > 0) {
    fprintf(stderr, "  curco=(%.17g,%.17g)\n", F2(curco));
  }
#endif
  se_ac = sym(cd->in)->next;
  vc = se_ac->next->vert;
  orient = orient2d(curco, v2->co, vc->co);
#ifdef DEBUG_CDT
  if (dbg_level > 1) {
    fprintf(stderr, "now searching with third vertex ");
    dump_v(vc, "vc");
    fprintf(stderr, "orient2d(cur, v2, vc) = %g\n", orient);
  }
#endif
  if (orient < 0.0) {
#ifdef DEBUG_CDT
    if (dbg_level > 1) {
      fprintf(stderr, "curco--v2 intersects vb--vc\n");
    }
#endif
    fill_crossdata_for_intersect(cdt, curco, v2, se_ac->next, cd, cd_next);
  }
  else if (orient > 0.0) {
#ifdef DEBUG_CDT
    if (dbg_level > 1) {
      fprintf(stderr, "curco--v2 intersects va--vc\n");
    }
#endif
    fill_crossdata_for_intersect(cdt, curco, v2, se_ac, cd, cd_next);
  }
  else {
#ifdef DEBUG_CDT
    if (dbg_level > 1) {
      fprintf(stderr, "orient==0 case, so going through or to vc\n");
    }
#endif
    *cd_next = (CrossData){0.0, vc, se_ac->next, NULL};
  }
}

/**
 * Add a constrained edge between v1 and v2 to cdt structure.
 * This may result in a number of #CDTEdges created, due to intersections
 * and partial overlaps with existing cdt vertices and edges.
 * Each created #CDTEdge will have input_id added to its input_ids list.
 *
 * If \a r_edges is not NULL, the #CDTEdges generated or found that go from
 * v1 to v2 are put into that linked list, in order.
 *
 * Assumes that #BLI_constrained_delaunay_get_output has not been called yet.
 */
static void add_edge_constraint(
    CDT_state *cdt, CDTVert *v1, CDTVert *v2, int input_id, LinkNode **r_edges)
{
  SymEdge *t, *se, *tstart, *tnext;
  int i, j, n, visit;
  bool ok;
  CrossData *crossings = NULL;
  CrossData *cd, *cd_prev, *cd_next;
  CDTVert *v;
  CDTEdge *edge;
  LinkNodePair edge_list = {NULL, NULL};
  BLI_array_staticdeclare(crossings, 128);
#ifdef DEBUG_CDT
  int dbg_level = 0;

  if (dbg_level > 0) {
    fprintf(stderr, "\nADD_EDGE_CONSTRAINT\n");
    dump_v(v1, "  1");
    dump_v(v2, "  2");
  }
#endif

  if (r_edges) {
    *r_edges = NULL;
  }

  /*
   * Handle two special cases first:
   * 1) The two end vertices are the same (can happen because of merging).
   * 2) There is already an edge between v1 and v2.
   */
  if (v1 == v2) {
    return;
  }
  if ((t = find_symedge_between_verts(v1, v2)) != NULL) {
#ifdef DEBUG_CDT
    if (dbg_level > 0) {
      fprintf(stderr, "segment already there\n");
    }
#endif
    add_to_input_ids(&t->edge->input_ids, input_id, cdt);
    if (r_edges != NULL) {
      BLI_linklist_append_pool(&edge_list, t->edge, cdt->listpool);
      *r_edges = edge_list.list;
    }
    return;
  }

  /*
   * Fill crossings array with CrossData points for intersection path from v1 to v2.
   *
   * At every point, the crossings array has the path so far, except that
   * the .out field of the last element of it may not be known yet -- if that
   * last element is a vertex, then we won't know the output edge until we
   * find the next crossing.
   *
   * To protect against infinite loops, we keep track of which vertices
   * we have visited by setting their visit_index to a new visit epoch.
   *
   * We check a special case first: where the segment is already there in
   * one hop. Saves a bunch of orient2d tests in that common case.
   */
  visit = ++cdt->visit_count;
  BLI_array_grow_one(crossings);
  crossings[0] = (CrossData){0.0, v1, NULL, NULL};
  while (!((n = BLI_array_len(crossings)) > 0 && crossings[n - 1].vert == v2)) {
    BLI_array_grow_one(crossings);
    cd = &crossings[n - 1];
    cd_next = &crossings[n];
    if (crossings[n - 1].lambda == 0.0) {
      ok = get_next_crossing_from_vert(cdt, cd, cd_next, v2);
    }
    else {
      get_next_crossing_from_edge(cdt, cd, cd_next, v2);
    }
    if (!ok || BLI_array_len(crossings) == 100000) {
      /* Shouldn't happen but if does, just bail out. */
#ifdef DEBUG_CDT
      fprintf(stderr, "FAILURE adding segment, bailing out\n");
#endif
      BLI_array_free(crossings);
      return;
    }
#ifdef DEBUG_CDT
    if (dbg_level > 1) {
      fprintf(stderr, "crossings[%d]: ", n);
      dump_cross_data(&crossings[n], "");
    }
#endif
    if (crossings[n].lambda == 0.0) {
      if (crossings[n].vert->visit_index == visit) {
        fprintf(stderr, "WHOOPS, REVISIT. Bailing out.\n"); /*TODO: desperation search here. */
        BLI_array_free(crossings);
        return;
      }
      crossings[n].vert->visit_index = visit;
    }
  }

#ifdef DEBUG_CDT
  if (dbg_level > 0) {
    fprintf(stderr, "\ncrossings found\n");
    for (i = 0; i < BLI_array_len(crossings); i++) {
      fprintf(stderr, "%d: ", i);
      dump_cross_data(&crossings[i], "");
      if (crossings[i].lambda == 0.0) {
        if (i == 0 || crossings[i - 1].lambda == 0.0) {
          BLI_assert(crossings[i].in == NULL);
        }
        else {
          BLI_assert(crossings[i].in != NULL && crossings[i].in->vert == crossings[i].vert);
          BLI_assert(crossings[i].in->face == sym(crossings[i - 1].in)->face);
        }
        if (i == BLI_array_len(crossings) - 1) {
          BLI_assert(crossings[i].vert == v2);
          BLI_assert(crossings[i].out == NULL);
        }
        else {
          BLI_assert(crossings[i].out->vert == crossings[i].vert);
          if (crossings[i + 1].lambda == 0.0) {
            BLI_assert(crossings[i].out->next->vert == crossings[i + 1].vert);
          }
          else {
            BLI_assert(crossings[i].out->face == crossings[i + 1].in->face);
          }
        }
      }
      else {
        if (i > 0 && crossings[i - 1].lambda == 0.0) {
          BLI_assert(crossings[i].in->face == crossings[i - 1].out->face);
        }
        BLI_assert(crossings[i].out == NULL);
      }
    }
  }
#endif

  /*
   * Postprocess crossings.
   * Some crossings may have an intersection crossing followed
   * by a vertex crossing that is on the same edge that was just
   * intersected. We prefer to go directly from the previous
   * crossing directly to the vertex. This may chain backwards.
   *
   * This loop marks certain crossings as "deleted", by setting
   * their lambdas to -1.0.
   */
  for (i = 2; i < BLI_array_len(crossings); i++) {
    cd = &crossings[i];
    if (cd->lambda == 0.0) {
      v = cd->vert;
      for (j = i - 1; j > 0; j--) {
        cd_prev = &crossings[j];
        if ((cd_prev->lambda == 0.0 && cd_prev->vert != v) ||
            (cd_prev->lambda != 0.0 && cd_prev->in->vert != v && cd_prev->in->next->vert != v)) {
          break;
        }
        cd_prev->lambda = -1.0; /* Mark cd_prev as 'deleted'. */
#ifdef DEBUG_CDT
        if (dbg_level > 0) {
          fprintf(stderr, "deleted crossing %d\n", j);
        }
#endif
      }
      if (j < i - 1) {
        /* Some crossings were deleted. Fix the in and out edges across gap. */
        cd_prev = &crossings[j];
        if (cd_prev->lambda == 0.0) {
          se = find_symedge_between_verts(cd_prev->vert, v);
          if (se == NULL) {
#ifdef DEBUG_CDT
            fprintf(stderr, "FAILURE(a) in delete crossings, bailing out.\n");
#endif
            BLI_array_free(crossings);
            return;
          }
          cd_prev->out = se;
          cd->in = NULL;
        }
        else {
          se = find_symedge_with_face(v, sym(cd_prev->in)->face);
          if (se == NULL) {
#ifdef DEBUG_CDT
            fprintf(stderr, "FAILURE(b) in delete crossings, bailing out.\n");
#endif
            BLI_array_free(crossings);
            return;
          }
          cd->in = se;
        }
#ifdef DEBUG_CDT
        if (dbg_level > 0) {
          fprintf(stderr, "after deleting crossings:\n");
          fprintf(stderr, "cross[%d]: ", j);
          dump_cross_data(cd_prev, "");
          fprintf(stderr, "cross[%d]: ", i);
          dump_cross_data(cd, "");
        }
#endif
      }
    }
  }

  /*
   * Insert all intersection points on constrained edges.
   */
  for (i = 0; i < BLI_array_len(crossings); i++) {
    cd = &crossings[i];
    if (cd->lambda != 0.0 && cd->lambda != -1.0 && is_constrained_edge(cd->in->edge)) {
      edge = split_edge(cdt, cd->in, cd->lambda);
      cd->vert = edge->symedges[0].vert;
#ifdef DEBUG_CDT
      if (dbg_level > 1) {
        fprintf(stderr, "insert vert for crossing %d: ", i);
        dump_v(cd->vert, "inserted");
      }
#endif
    }
  }

  /*
   * Remove any crossed, non-intersected edges.
   */
  for (i = 0; i < BLI_array_len(crossings); i++) {
    cd = &crossings[i];
    if (cd->lambda != 0.0 && cd->lambda != -1.0 && !is_constrained_edge(cd->in->edge)) {
      delete_edge(cdt, cd->in);
#ifdef DEBUG_CDT
      if (dbg_level > 1) {
        fprintf(stderr, "delete edge for crossing %d\n", i);
      }
#endif
    }
  }

  /*
   * Insert segments for v1->v2.
   */
  tstart = crossings[0].out;
  for (i = 1; i < BLI_array_len(crossings); i++) {
    cd = &crossings[i];
    if (cd->lambda == -1.0) {
      continue; /* This crossing was deleted. */
    }
    t = tnext = NULL;
    if (cd->lambda != 0.0) {
      if (is_constrained_edge(cd->in->edge)) {
        t = cd->vert->symedge;
        tnext = sym(t)->next;
      }
    }
    else if (cd->lambda == 0.0) {
      t = cd->in;
      tnext = cd->out;
      if (t == NULL) {
        /* Previous non-deleted crossing must also have been a vert, and segment should exist. */
        for (j = i - 1; j >= 0; j--) {
          cd_prev = &crossings[j];
          if (cd_prev->lambda != -1.0) {
            break;
          }
        }
        BLI_assert(cd_prev->lambda == 0.0);
        BLI_assert(cd_prev->out->next->vert == cd->vert);
#ifdef DEBUG_CDT
        if (dbg_level > 1) {
          fprintf(stderr, "edge to crossing %d already there\n", i);
        }
#endif
        edge = cd_prev->out->edge;
        add_to_input_ids(&edge->input_ids, input_id, cdt);
      }
    }
    if (t != NULL) {
#ifdef DEBUG_CDT
      if (dbg_level > 1) {
        fprintf(stderr, "edge to crossing %d: insert diagonal between\n", i);
        dump_se(tstart, "  ");
        dump_se(t, " ");
        dump_se_cycle(tstart, "tstart", 100);
        dump_se_cycle(t, "t", 100);
      }
#endif
      if (tstart->next->vert == t->vert) {
        edge = tstart->edge;
#ifdef DEBUG_CDT
        if (dbg_level > 1) {
          fprintf(stderr, "already there (b)\n");
        }
#endif
      }
      else {
        edge = add_diagonal(cdt, tstart, t);
      }
      add_to_input_ids(&edge->input_ids, input_id, cdt);
#ifdef DEBUG_CDT
      if (dbg_level > 1) {
        fprintf(stderr, "added\n");
      }
#endif
      if (r_edges != NULL) {
        BLI_linklist_append_pool(&edge_list, edge, cdt->listpool);
      }
      /* Now retriangulate upper and lower gaps. */
      re_delaunay_triangulate(cdt, &edge->symedges[0]);
      re_delaunay_triangulate(cdt, &edge->symedges[1]);
    }
    if (i < BLI_array_len(crossings) - 1) {
      if (tnext != NULL) {
        tstart = tnext;
#ifdef DEBUG_CDT
        if (dbg_level > 1) {
          fprintf(stderr, "now tstart = ");
          dump_se(tstart, "");
        }
#endif
      }
    }
  }

  if (r_edges) {
    *r_edges = edge_list.list;
  }
  BLI_array_free(crossings);
}

/**
 * Add face_id to the input_ids lists of all #CDTFace's on the interior of the input face with that
 * id. face_symedge is on edge of the boundary of the input face, with assumption that interior is
 * on the left of that SymEdge.
 *
 * The algorithm is: starting from the #CDTFace for face_symedge, add the face_id and then
 * process all adjacent faces where the adjacency isn't across an edge that was a constraint added
 * for the boundary of the input face.
 * fedge_start..fedge_end is the inclusive range of edge input ids that are for the given face.
 *
 * Note: if the input face is not CCW oriented, we'll be labeling the outside, not the inside.
 * Note 2: if the boundary has self-crossings, this method will arbitrarily pick one of the
 * contiguous set of faces enclosed by parts of the boundary, leaving the other such untagged. This
 * may be a feature instead of a bug if the first contiguous section is most of the face and the
 * others are tiny self-crossing triangles at some parts of the boundary. On the other hand, if
 * decide we want to handle these in full generality, then will need a more complicated algorithm
 * (using "inside" tests and a parity rule) to decide on the interior.
 */
static void add_face_ids(
    CDT_state *cdt, SymEdge *face_symedge, int face_id, int fedge_start, int fedge_end)
{
  Stack stack;
  SymEdge *se, *se_start, *se_sym;
  CDTFace *face, *face_other;
  int visit;

  /* Can't loop forever since eventually would visit every face. */
  cdt->visit_count++;
  visit = cdt->visit_count;
  stack = NULL;
  push(&stack, face_symedge, cdt);
  while (!is_empty(&stack)) {
    se = pop(&stack, cdt);
    face = se->face;
    if (face->visit_index == visit) {
      continue;
    }
    face->visit_index = visit;
    add_to_input_ids(&face->input_ids, face_id, cdt);
    se_start = se;
    for (se = se->next; se != se_start; se = se->next) {
      if (!id_range_in_list(se->edge->input_ids, fedge_start, fedge_end)) {
        se_sym = sym(se);
        face_other = se_sym->face;
        if (face_other->visit_index != visit) {
          push(&stack, se_sym, cdt);
        }
      }
    }
  }
}

/* Delete_edge but try not to mess up outer face.
 * Also faces have symedges now, so make sure not
 * to mess those up either. */
static void dissolve_symedge(CDT_state *cdt, SymEdge *se)
{
  SymEdge *symse = sym(se);
  if (symse->face == cdt->outer_face) {
    se = sym(se);
    symse = sym(se);
  }
  if (cdt->outer_face->symedge == se || cdt->outer_face->symedge == symse) {
    /* Advancing by 2 to get past possible 'sym(se)'. */
    if (se->next->next == se) {
      cdt->outer_face->symedge = NULL;
    }
    else {
      cdt->outer_face->symedge = se->next->next;
    }
  }
  else {
    if (se->face->symedge == se) {
      se->face->symedge = se->next;
    }
    if (symse->face->symedge == symse) {
      symse->face->symedge = symse->next;
    }
  }
  delete_edge(cdt, se);
}

/* Slow way to get face and start vertex index within face for edge id e. */
static bool get_face_edge_id_indices(const CDT_input *in, int e, int *r_f, int *r_fi)
{
  int f;
  int id;

  id = in->edges_len;
  if (e < id) {
    return false;
  }
  for (f = 0; f < in->faces_len; f++) {
    if (e < id + in->faces_len_table[f]) {
      *r_f = f;
      *r_fi = e - id;
      return true;
    }
    id += in->faces_len_table[f];
  }
  return false;
}

/* Is pt_co when snapped to segment seg1 seg2 all of:
 * a) strictly within that segment
 * b) within epsilon from original pt_co
 * c) pt_co is not within epsilon of either seg1 or seg2.
 * Return true if so, and return in *r_lambda the fraction of the way from seg1 to seg2 of the
 * snapped point.
 */
static bool check_vert_near_segment(const double *pt_co,
                                    const double *seg1,
                                    const double *seg2,
                                    double epsilon_squared,
                                    double *r_lambda)
{
  double lambda, snap_co[2];

  lambda = closest_to_line_v2_db(snap_co, pt_co, seg1, seg2);
  *r_lambda = lambda;
  if (lambda <= 0.0 || lambda >= 1.0) {
    return false;
  }
  if (len_squared_v2v2_db(pt_co, snap_co) > epsilon_squared) {
    return false;
  }
  if (len_squared_v2v2_db(pt_co, seg1) <= epsilon_squared ||
      len_squared_v2v2_db(pt_co, seg2) <= epsilon_squared) {
    return false;
  }
  return true;
}

typedef struct EdgeVertLambda {
  int e_id;
  int v_id;
  double lambda;
} EdgeVertLambda;

/* For sorting first by edge id, then by lambda, then by vert id. */
static int evl_cmp(const void *a, const void *b)
{
  const EdgeVertLambda *area = a;
  const EdgeVertLambda *sb = b;

  if (area->e_id < sb->e_id) {
    return -1;
  }
  if (area->e_id > sb->e_id) {
    return 1;
  }
  if (area->lambda < sb->lambda) {
    return -1;
  }
  if (area->lambda > sb->lambda) {
    return 1;
  }
  if (area->v_id < sb->v_id) {
    return -1;
  }
  if (area->v_id > sb->v_id) {
    return 1;
  }
  return 0;
}

/**
 * If epsilon > 0, and input doesn't have skip_modify_input == true,
 * check input to see if any constraint edge ends (including face edges) come
 * within epsilon of another edge.
 * For all such cases, we want to split the constraint edge at the point nearest to near vertex
 * and move the vertex coordinates to be on that edge.
 * But exclude cases where they come within epsilon of either end because those will be handled
 * by vertex merging in the main triangulation algorithm.
 *
 * If any such splits are found, make a new CDT_input reflecting this change, and provide an
 * edge map to map from edge ids in the new input space to edge ids in the old input space.
 *
 * TODO: replace naive O(n^2) algorithm with kdopbvh-based one.
 */
static const CDT_input *modify_input_for_near_edge_ends(const CDT_input *input, int **r_edge_map)
{
  CDT_input *new_input = NULL;
  int e, eprev, e1, e2, f, fi, flen, start, i, j;
  int i_new, i_old, i_evl;
  int v11, v12, v21, v22;
  double co11[2], co12[2], co21[2], co22[2];
  double lambda;
  double eps = (double)input->epsilon;
  double eps_sq = eps * eps;
  int tot_edge_constraints, edges_len, tot_face_edges;
  int new_tot_face_edges, new_tot_con_edges;
  int delta_con_edges, delta_face_edges, cur_e_cnt;
  int *edge_map;
  int evl_len;
  EdgeVertLambda *edge_vert_lambda = NULL;
  BLI_array_staticdeclare(edge_vert_lambda, 128);
#ifdef DEBUG_CDT
  EdgeVertLambda *evl;
  int dbg_level = 0;

  if (dbg_level > 0) {
    fprintf(stderr, "\nMODIFY INPUT\n\n");
  }
#endif

  *r_edge_map = NULL;
  if (input->epsilon == 0.0 || input->skip_input_modify ||
      (input->edges_len == 0 && input->faces_len == 0)) {
    return input;
  }

  /* Edge constraints are union of the explicitly provided edges and the implicit edges
   * that are part of the provided faces. We index constraints by have the first input->edges_len
   * ints standing for the explicit edge with the same index, and the rest being face edges in
   * the order that the faces appear and then edges within those faces, with indices offset by
   * input->edges_len.
   * Calculate tot_edge_constraints to be the sum of the two kinds of edges.
   * We first have to count the number of face edges.
   * That is the same as the number of vertices in the faces table, which
   * we can find by adding the last length to the last start.
   */
  edges_len = input->edges_len;
  tot_edge_constraints = edges_len;
  if (input->faces_len > 0) {
    tot_face_edges = input->faces_start_table[input->faces_len - 1] +
                     input->faces_len_table[input->faces_len - 1];
  }
  else {
    tot_face_edges = 0;
  }
  tot_edge_constraints = edges_len + tot_face_edges;

  for (e1 = 0; e1 < tot_edge_constraints - 1; e1++) {
    if (e1 < edges_len) {
      v11 = input->edges[e1][0];
      v12 = input->edges[e1][1];
    }
    else {
      if (!get_face_edge_id_indices(input, e1, &f, &fi)) {
        /* Must be bad input. Will be caught later so don't need to signal here. */
        continue;
      }
      start = input->faces_start_table[f];
      flen = input->faces_len_table[f];
      v11 = input->faces[start + fi];
      v12 = input->faces[(fi == flen - 1) ? start : start + fi + 1];
    }
    for (e2 = e1 + 1; e2 < tot_edge_constraints; e2++) {
      if (e2 < edges_len) {
        v21 = input->edges[e2][0];
        v22 = input->edges[e2][1];
      }
      else {
        if (!get_face_edge_id_indices(input, e2, &f, &fi)) {
          continue;
        }
        start = input->faces_start_table[f];
        flen = input->faces_len_table[f];
        v21 = input->faces[start + fi];
        v22 = input->faces[(fi == flen - 1) ? start : start + fi + 1];
      }
      copy_v2db_v2fl(co11, input->vert_coords[v11]);
      copy_v2db_v2fl(co12, input->vert_coords[v12]);
      copy_v2db_v2fl(co21, input->vert_coords[v21]);
      copy_v2db_v2fl(co22, input->vert_coords[v22]);
      if (check_vert_near_segment(co11, co21, co22, eps_sq, &lambda)) {

        BLI_array_append(edge_vert_lambda, ((EdgeVertLambda){e2, v11, lambda}));
      }
      if (check_vert_near_segment(co12, co21, co22, eps_sq, &lambda)) {
        BLI_array_append(edge_vert_lambda, ((EdgeVertLambda){e2, v12, lambda}));
      }
      if (check_vert_near_segment(co21, co11, co12, eps_sq, &lambda)) {
        BLI_array_append(edge_vert_lambda, ((EdgeVertLambda){e1, v21, lambda}));
      }
      if (check_vert_near_segment(co22, co11, co12, eps_sq, &lambda)) {
        BLI_array_append(edge_vert_lambda, ((EdgeVertLambda){e1, v22, lambda}));
      }
    }
  }

  evl_len = BLI_array_len(edge_vert_lambda);
  if (evl_len > 0) {
    /* Sort to bring splits for each edge together,
     * and for each edge, to be in order of lambda. */
    qsort(edge_vert_lambda, evl_len, sizeof(EdgeVertLambda), evl_cmp);
#ifdef DEBUG_CDT
    if (dbg_level > 0) {
      fprintf(stderr, "\nafter sorting\n");
      for (i = 0; i < evl_len; i++) {
        evl = &edge_vert_lambda[i];
        fprintf(stderr, "e%d, v%d, %g\n", evl->e_id, evl->v_id, evl->lambda);
      }
    }
#endif

    /* Remove dups in edge_vert_lambda, where dup means that the edge is the
     * same, and the verts are either the same or will be merged by epsilon-nearness.
     */
    i = 0;
    j = 0;
    /* In loop, copy from position j to position i. */
    for (j = 0; j < evl_len;) {
      int k;
      if (i != j) {
        memmove(&edge_vert_lambda[i], &edge_vert_lambda[j], sizeof(EdgeVertLambda));
      }
      for (k = j + 1; k < evl_len; k++) {
        int vj = edge_vert_lambda[j].v_id;
        int vk = edge_vert_lambda[k].v_id;
        if (vj != vk) {
          if (len_squared_v2v2(input->vert_coords[vj], input->vert_coords[vk]) > (float)eps_sq) {
            break;
          }
        }
      }
      j = k;
      i++;
    }

    if (i != evl_len) {
      evl_len = i;
#ifdef DEBUG_CDT
      if (dbg_level > 0) {
        fprintf(stderr, "\nduplicates eliminated\n");
        for (i = 0; i < evl_len; i++) {
          evl = &edge_vert_lambda[i];
          fprintf(stderr, "e%d, v%d, %g\n", evl->e_id, evl->v_id, evl->lambda);
        }
      }
#endif
    }
    /* Find delta in number of constraint edges and face edges.
     * This may be overestimates of true number, due to duplicates. */
    delta_con_edges = 0;
    delta_face_edges = 0;
    cur_e_cnt = 0;
    eprev = -1;
    for (i = 0; i < evl_len; i++) {
      e = edge_vert_lambda[i].e_id;
      if (i > 0 && e > eprev) {
        /* New edge group. Previous group had cur_e_cnt split vertices.
         * That is the delta in the number of edges needed in input since
         * there will be cur_e_cnt + 1 edges replacing one edge.
         */
        if (eprev < edges_len) {
          delta_con_edges += cur_e_cnt;
        }
        else {
          delta_face_edges += cur_e_cnt;
        }
        cur_e_cnt = 1;
        ;
      }
      else {
        cur_e_cnt++;
      }
      eprev = e;
    }
    if (eprev < edges_len) {
      delta_con_edges += cur_e_cnt;
    }
    else {
      delta_face_edges += cur_e_cnt;
    }
    new_tot_con_edges = input->edges_len + delta_con_edges;
    if (input->faces_len > 0) {
      new_tot_face_edges = input->faces_start_table[input->faces_len - 1] +
                           input->faces_len_table[input->faces_len - 1] + delta_face_edges;
    }
    else {
      new_tot_face_edges = 0;
    }

    /* Allocate new CDT_input, now we know sizes needed (perhaps overestimated a bit).
     * Caller will be responsible for freeing it and its arrays.
     */
    new_input = MEM_callocN(sizeof(CDT_input), __func__);
    new_input->epsilon = input->epsilon;
    new_input->verts_len = input->verts_len;
    new_input->vert_coords = (float(*)[2])MEM_malloc_arrayN(
        new_input->verts_len, 2 * sizeof(float), __func__);
    /* We don't do it now, but may decide to change coords of snapped verts. */
    memmove(new_input->vert_coords,
            input->vert_coords,
            (size_t)new_input->verts_len * sizeof(float) * 2);

    if (edges_len > 0) {
      new_input->edges_len = new_tot_con_edges;
      new_input->edges = (int(*)[2])MEM_malloc_arrayN(
          new_tot_con_edges, 2 * sizeof(int), __func__);
    }

    if (input->faces_len > 0) {
      new_input->faces_len = input->faces_len;
      new_input->faces_start_table = (int *)MEM_malloc_arrayN(
          new_input->faces_len, sizeof(int), __func__);
      new_input->faces_len_table = (int *)MEM_malloc_arrayN(
          new_input->faces_len, sizeof(int), __func__);
      new_input->faces = (int *)MEM_malloc_arrayN(new_tot_face_edges, sizeof(int), __func__);
    }

    edge_map = (int *)MEM_malloc_arrayN(
        new_tot_con_edges + new_tot_face_edges, sizeof(int), __func__);
    *r_edge_map = edge_map;

    i_new = i_old = i_evl = 0;
    e = edge_vert_lambda[0].e_id;
    /* First do new constraint edges. */
    for (i_old = 0; i_old < edges_len; i_old++) {
      if (i_old < e) {
        /* Edge for i_old not split; copy it into new_input. */
        new_input->edges[i_new][0] = input->edges[i_old][0];
        new_input->edges[i_new][1] = input->edges[i_old][1];
        edge_map[i_new] = i_old;
        i_new++;
      }
      else {
        /* Edge for i_old is split. */
        BLI_assert(i_old == e);
        new_input->edges[i_new][0] = input->edges[i_old][0];
        new_input->edges[i_new][1] = edge_vert_lambda[i_evl].v_id;
        edge_map[i_new] = i_old;
        i_new++;
        i_evl++;
        while (i_evl < evl_len && e == edge_vert_lambda[i_evl].e_id) {
          new_input->edges[i_new][0] = new_input->edges[i_new - 1][1];
          new_input->edges[i_new][1] = edge_vert_lambda[i_evl].v_id;
          edge_map[i_new] = i_old;
          i_new++;
          i_evl++;
        }
        new_input->edges[i_new][0] = new_input->edges[i_new - 1][1];
        new_input->edges[i_new][1] = input->edges[i_old][1];
        edge_map[i_new] = i_old;
        i_new++;
        if (i_evl < evl_len) {
          e = edge_vert_lambda[i_evl].e_id;
        }
        else {
          e = INT_MAX;
        }
      }
    }
    BLI_assert(i_new <= new_tot_con_edges);
    new_input->edges_len = i_new;

    /* Now do face constraints. */
    if (input->faces_len > 0) {
      f = 0;
      i_new = 0; /* Now will index cur place in new_input->faces. */
      while (i_old < tot_edge_constraints) {
        flen = input->faces_len_table[f];
        BLI_assert(i_old - edges_len == input->faces_start_table[f]);
        new_input->faces_start_table[f] = i_new;
        if (i_old + flen - 1 < e) {
          /* Face f is not split. */
          for (j = 0; j < flen; j++) {
            new_input->faces[i_new] = input->faces[i_old - edges_len + j];
            edge_map[i_new + new_input->edges_len] = i_old + j;
            i_new++;
          }
          i_old += flen;
          new_input->faces_len_table[f] = flen;
          f++;
        }
        else {
          /* Face f has at least one split edge. */
          int i_new_start = i_new;
          for (j = 0; j < flen; j++) {
            if (i_old + j < e) {
              /* jth edge of f is not split. */
              new_input->faces[i_new] = input->faces[i_old - edges_len + j];
              edge_map[i_new + new_input->edges_len] = i_old + j;
              i_new++;
            }
            else {
              /* jth edge of f is split. */
              BLI_assert(i_old + j == e);
              new_input->faces[i_new] = input->faces[i_old - edges_len + j];
              edge_map[i_new + new_input->edges_len] = i_old + j;
              i_new++;
              while (i_evl < evl_len && e == edge_vert_lambda[i_evl].e_id) {
                new_input->faces[i_new] = edge_vert_lambda[i_evl].v_id;
                edge_map[i_new + new_input->edges_len] = i_old + j;
                i_new++;
                i_evl++;
              }
              if (i_evl < evl_len) {
                e = edge_vert_lambda[i_evl].e_id;
              }
              else {
                e = INT_MAX;
              }
            }
          }
          new_input->faces_len_table[f] = i_new - i_new_start;
          i_old += flen;
          f++;
        }
      }
    }

#ifdef DEBUG_CDT
    if (dbg_level > 0) {
      fprintf(stderr, "\nnew constraint edges\n");
      for (i = 0; i < new_input->edges_len; i++) {
        fprintf(stderr, "  e%d: (%d,%d)\n", i, new_input->edges[i][0], new_input->edges[i][1]);
      }
      fprintf(stderr, "\nnew faces\n");
      for (f = 0; f < new_input->faces_len; f++) {
        flen = new_input->faces_len_table[f];
        start = new_input->faces_start_table[f];
        fprintf(stderr, "  f%d: start=%d, len=%d\n    ", f, start, flen);
        for (i = start; i < start + flen; i++) {
          fprintf(stderr, "%d ", new_input->faces[i]);
        }
        fprintf(stderr, "\n");
      }
      fprintf(stderr, "\nedge map (new->old)\n");
      for (i = 0; i < new_tot_con_edges + new_tot_face_edges; i++) {
        fprintf(stderr, "  %d->%d\n", i, edge_map[i]);
      }
    }
#endif
  }

  BLI_array_free(edge_vert_lambda);
  if (new_input != NULL) {
    return (const CDT_input *)new_input;
  }
  return input;
}

static void free_modified_input(CDT_input *input)
{
  MEM_freeN(input->vert_coords);
  if (input->edges != NULL) {
    MEM_freeN(input->edges);
  }
  if (input->faces != NULL) {
    MEM_freeN(input->faces);
    MEM_freeN(input->faces_len_table);
    MEM_freeN(input->faces_start_table);
  }
  MEM_freeN(input);
}

/* Return true if we can merge se's vert into se->next's vert
 * without making the area of any new triangle formed by doing
 * that into a zero or negative area triangle.*/
static bool can_collapse(const SymEdge *se)
{
  SymEdge *loop_se;
  const double *co = se->next->vert->co;

  for (loop_se = se->rot; loop_se != se && loop_se->rot != se; loop_se = loop_se->rot) {
    if (orient2d(co, loop_se->next->vert->co, loop_se->rot->next->vert->co) <= 0.0) {
      return false;
    }
  }
  return true;
}

/*
 * Merge one end of e onto the other, fixing up surrounding faces.
 *
 * General situation looks something like:
 *
 *             c-----e
 *           /  \   / \
 *          /    \ /   \
 *         a------b-----f
 *          \    / \   /
 *           \  /   \ /
 *             d-----g
 *
 * where ab is the tiny edge. We want to merge a and b and delete edge ab.
 * We don't want to change the coordinates of input vertices [We could revisit this
 * in the future, as API def doesn't prohibit this, but callers will appreciate if they
 * don't change.]
 * Sometimes the collapse shouldn't happen because the triangles formed by the changed
 * edges may end up with zero or negative area (see can_collapse, above).
 * So don't choose a collapse direction that is not allowed or one that has an original vertex
 * as origin and a non-original vertex as destination.
 * If both collapse directions are allowed by that rule, pick the one with the lower original
 * index.
 *
 * After merging, the faces abc and adb disappear (if they are not the outer face).
 * Suppose we merge b onto a.
 * Then edges cb and db are deleted. Face cbe becomes cae and face bdg becomes adg.
 * Any other faces attached to b now have a in their place.
 * We can do this by rotating edges round b, replacing their vert references with a.
 * Similar statements can be made about what happens when a merges into b;
 * in code below we'll swap a and b to make above lettering work for a b->a merge.
 * Return the vert at the collapsed edge, if a collapse happens.
 */
static CDTVert *collapse_tiny_edge(CDT_state *cdt, CDTEdge *e)
{
  CDTVert *va, *vb;
  SymEdge *ab_se, *ba_se, *bd_se, *bc_se, *ad_se, *ac_se;
  SymEdge *bg_se, *be_se, *se, *gb_se, *ca_se;
  bool can_collapse_a_to_b, can_collapse_b_to_a;
#ifdef DEBUG_CDT
  int dbg_level = 0;
#endif

  ab_se = &e->symedges[0];
  ba_se = &e->symedges[1];
  va = ab_se->vert;
  vb = ba_se->vert;
#ifdef DEBUG_CDT
  if (dbg_level > 0) {
    fprintf(stderr, "\ncollapse_tiny_edge\n");
    dump_se(&e->symedges[0], "tiny edge");
    fprintf(stderr, "a = [%d], b = [%d]\n", va->index, vb->index);
    validate_cdt(cdt, true, false, true);
  }
#endif
  can_collapse_a_to_b = can_collapse(ab_se);
  can_collapse_b_to_a = can_collapse(ba_se);
  /* Now swap a and b if necessary and possible, so that from this point on we are collapsing b to
   * a. */
  if (va->index > vb->index || !can_collapse_b_to_a) {
    if (can_collapse_a_to_b && !(is_original_vert(va, cdt) && !is_original_vert(vb, cdt))) {
      SWAP(CDTVert *, va, vb);
      ab_se = &e->symedges[1];
      ba_se = &e->symedges[0];
#ifdef DEBUG_CDT
      if (dbg_level > 0) {
        fprintf(stderr, "swapped a and b\n");
      }
#endif
    }
    else {
      /* Neither collapse direction is OK. */
#ifdef DEBUG_CDT
      if (dbg_level > 0) {
        fprintf(stderr, "neither collapse direction ok\n");
      }
#endif
      return NULL;
    }
  }
  bc_se = ab_se->next;
  bd_se = ba_se->rot;
  if (bd_se == ba_se) {
    /* Shouldn't happen. Wire edge in outer face. */
    fprintf(stderr, "unexpected wire edge\n");
    return NULL;
  }
  vb->merge_to_index = va->merge_to_index == -1 ? va->index : va->merge_to_index;
  vb->symedge = NULL;
#ifdef DEBUG_CDT
  if (dbg_level > 0) {
    fprintf(stderr,
            "vb = v[%d] merges to va = v[%d], vb->merge_to_index=%d\n",
            vb->index,
            va->index,
            vb->merge_to_index);
  }
#endif
  /* First fix the vertex of intermediate triangles, like bgf. */
  for (se = bd_se->rot; se != bc_se; se = se->rot) {
#ifdef DEBUG_CDT
    if (dbg_level > 0) {
      dump_se(se, "intermediate tri edge, setting vert to va");
    }
#endif
    se->vert = va;
  }
  ad_se = sym(sym(bd_se)->rot);
  ca_se = bc_se->next;
  ac_se = sym(ca_se);
  if (bd_se->rot != bc_se) {
    bg_se = bd_se->rot;
    be_se = sym(bc_se)->next;
    gb_se = sym(bg_se);
  }
  else {
    bg_se = NULL;
    be_se = NULL;
  }
#ifdef DEBUG_CDT
  if (dbg_level > 0) {
    fprintf(stderr, "delete bd, inputs to ad\n");
    dump_se(bd_se, "  bd");
    dump_se(ad_se, "  ad");
    fprintf(stderr, "delete bc, inputs to ac\n");
    dump_se(bc_se, "  bc");
    dump_se(ac_se, "  ac");
    fprintf(stderr, "delete ab\n");
    dump_se(ab_se, "  ab");
    if (bg_se != NULL) {
      fprintf(stderr, "fix up bg, be\n");
      dump_se(bg_se, "  bg");
      dump_se(be_se, "  be");
    }
  }
#endif
  add_list_to_input_ids(&ad_se->edge->input_ids, bd_se->edge->input_ids, cdt);
  delete_edge(cdt, bd_se);
  add_list_to_input_ids(&ac_se->edge->input_ids, bc_se->edge->input_ids, cdt);
  delete_edge(cdt, sym(bc_se));
  /* At this point we have this:
   *
   *             c-----e
   *           /      / \
   *          /      /   \
   *         a------b-----f
   *          \      \   /
   *           \      \ /
   *             d-----g
   *
   * Or, if there is not bg_se and be_se, like this:
   *
   *             c
   *           /
   *          /
   *         a------b
   *          \
   *           \
   *             d
   *
   * (But we've already changed the vert field for bg, bf, ..., be to be va.)
   */
  if (bg_se != NULL) {
    gb_se->next = ad_se;
    ad_se->rot = bg_se;
    ca_se->next = be_se;
    be_se->rot = ac_se;
    bg_se->vert = va;
    be_se->vert = va;
  }
  else {
    ca_se->next = ad_se;
    ad_se->rot = ac_se;
  }
  /* Don't use delete_edge as it changes too much. */
  ab_se->next = ab_se->rot = NULL;
  ba_se->next = ba_se->rot = NULL;
  if (va->symedge == ab_se) {
    va->symedge = ac_se;
  }
  return va;
}

/*
 * Check to see if e is tiny (length <= epsilon) and queue it if so.
 */
static void maybe_enqueue_small_feature(CDT_state *cdt, CDTEdge *e, LinkNodePair *tiny_edge_queue)
{
  SymEdge *se, *sesym;
#ifdef DEBUG_CDT
  int dbg_level = 0;

  if (dbg_level > 0) {
    fprintf(stderr, "\nmaybe_enqueue_small_features\n");
    dump_se(&e->symedges[0], "  se0");
  }
#endif

  if (is_deleted_edge(e) || e->in_queue) {
#ifdef DEBUG_CDT
    if (dbg_level > 0) {
      fprintf(stderr, "returning because of e conditions\n");
    }
#endif
    return;
  }
  se = &e->symedges[0];
  sesym = &e->symedges[1];
  if (len_squared_v2v2_db(se->vert->co, sesym->vert->co) <= cdt->epsilon_squared) {
    BLI_linklist_append_pool(tiny_edge_queue, e, cdt->listpool);
    e->in_queue = true;
#ifdef DEBUG_CDT
    if (dbg_level > 0) {
      fprintf(stderr, "Queue tiny edge\n");
    }
#endif
  }
}

/* Consider all edges in rot ring around v for possible enqueing as small features .*/
static void maybe_enqueue_small_features(CDT_state *cdt, CDTVert *v, LinkNodePair *tiny_edge_queue)
{
  SymEdge *se, *se_start;

  se = se_start = v->symedge;
  if (!se_start) {
    return;
  }
  do {
    maybe_enqueue_small_feature(cdt, se->edge, tiny_edge_queue);
  } while ((se = se->rot) != se_start);
}

/* Collapse small edges (length <= epsilon) until no more exist.
 */
static void remove_small_features(CDT_state *cdt)
{
  double epsilon = cdt->epsilon;
  LinkNodePair tiny_edge_queue = {NULL, NULL};
  BLI_mempool *pool = cdt->listpool;
  LinkNode *ln;
  CDTEdge *e;
  CDTVert *v_change;
#ifdef DEBUG_CDT
  int dbg_level = 0;

  if (dbg_level > 0) {
    fprintf(stderr, "\nREMOVE_SMALL_FEATURES, epsilon=%g\n", epsilon);
  }
#endif

  if (epsilon == 0.0) {
    return;
  }

  for (ln = cdt->edges; ln; ln = ln->next) {
    e = (CDTEdge *)ln->link;
    maybe_enqueue_small_feature(cdt, e, &tiny_edge_queue);
  }

  while (tiny_edge_queue.list != NULL) {
    e = (CDTEdge *)BLI_linklist_pop_pool(&tiny_edge_queue.list, pool);
    if (tiny_edge_queue.list == NULL) {
      tiny_edge_queue.last_node = NULL;
    }
    e->in_queue = false;
    if (is_deleted_edge(e)) {
      continue;
    }
#ifdef DEBUG_CDT
    if (dbg_level > 0) {
      fprintf(stderr, "collapse tiny edge\n");
      dump_se(&e->symedges[0], "");
    }
#endif
    v_change = collapse_tiny_edge(cdt, e);
    if (v_change) {
      maybe_enqueue_small_features(cdt, v_change, &tiny_edge_queue);
    }
  }
}

/* Remove all non-constraint edges. */
static void remove_non_constraint_edges(CDT_state *cdt)
{
  LinkNode *ln;
  CDTEdge *e;
  SymEdge *se;

  for (ln = cdt->edges; ln; ln = ln->next) {
    e = (CDTEdge *)ln->link;
    se = &e->symedges[0];
    if (!is_deleted_edge(e) && !is_constrained_edge(e)) {
      dissolve_symedge(cdt, se);
    }
  }
}

/*
 * Remove the non-constraint edges, but leave enough of them so that all of the
 * faces that would be bmesh faces (that is, the faces that have some input representative)
 * are valid: they can't have holes, they can't have repeated vertices, and they can't have
 * repeated edges.
 *
 * Not essential, but to make the result look more aesthetically nice,
 * remove the edges in order of decreasing length, so that it is more likely that the
 * final remaining support edges are short, and therefore likely to make a fairly
 * direct path from an outer face to an inner hole face.
 */

/* For sorting edges by decreasing length (squared). */
struct EdgeToSort {
  double len_squared;
  CDTEdge *e;
};

static int edge_to_sort_cmp(const void *a, const void *b)
{
  const struct EdgeToSort *e1 = a;
  const struct EdgeToSort *e2 = b;

  if (e1->len_squared > e2->len_squared) {
    return -1;
  }
  if (e1->len_squared < e2->len_squared) {
    return 1;
  }
  return 0;
}

static void remove_non_constraint_edges_leave_valid_bmesh(CDT_state *cdt)
{
  LinkNode *ln;
  CDTEdge *e;
  SymEdge *se, *se2;
  CDTFace *fleft, *fright;
  bool dissolve;
  size_t nedges;
  int i, ndissolvable;
  const double *co1, *co2;
  struct EdgeToSort *sorted_edges;

  nedges = 0;
  for (ln = cdt->edges; ln; ln = ln->next) {
    nedges++;
  }
  if (nedges == 0) {
    return;
  }
  sorted_edges = BLI_memarena_alloc(cdt->arena, nedges * sizeof(*sorted_edges));
  i = 0;
  for (ln = cdt->edges; ln; ln = ln->next) {
    e = (CDTEdge *)ln->link;
    if (!is_deleted_edge(e) && !is_constrained_edge(e)) {
      sorted_edges[i].e = e;
      co1 = e->symedges[0].vert->co;
      co2 = e->symedges[1].vert->co;
      sorted_edges[i].len_squared = len_squared_v2v2_db(co1, co2);
      i++;
    }
  }
  ndissolvable = i;
  qsort(sorted_edges, ndissolvable, sizeof(*sorted_edges), edge_to_sort_cmp);
  for (i = 0; i < ndissolvable; i++) {
    e = sorted_edges[i].e;
    se = &e->symedges[0];
    dissolve = true;
    if (true /*!edge_touches_frame(e)*/) {
      fleft = se->face;
      fright = sym(se)->face;
      if (fleft != cdt->outer_face && fright != cdt->outer_face &&
          (fleft->input_ids != NULL || fright->input_ids != NULL)) {
        /* Is there another symedge with same left and right faces?
         * Or is there a vertex not part of e touching the same left and right faces? */
        for (se2 = se->next; dissolve && se2 != se; se2 = se2->next) {
          if (sym(se2)->face == fright ||
              (se2->vert != se->next->vert && vert_touches_face(se2->vert, fright))) {
            dissolve = false;
          }
        }
      }
    }
    if (dissolve) {
      dissolve_symedge(cdt, se);
    }
  }
}

static void remove_outer_edges_until_constraints(CDT_state *cdt)
{
  LinkNode *fstack = NULL;
  SymEdge *se, *se_start;
  CDTFace *f, *fsym;
  int visit = ++cdt->visit_count;
#ifdef DEBUG_CDT
  int dbg_level = 0;

  if (dbg_level > 0) {
    fprintf(stderr, "remove_outer_edges_until_constraints\n");
  }
#endif

  cdt->outer_face->visit_index = visit;
  /* Walk around outer face, adding faces on other side of dissolvable edges to stack. */
  se_start = se = cdt->outer_face->symedge;
  do {
    if (!is_constrained_edge(se->edge)) {
      fsym = sym(se)->face;
      if (fsym->visit_index != visit) {
#ifdef DEBUG_CDT
        if (dbg_level > 0) {
          fprintf(stderr, "pushing f=%p from symedge ", fsym);
          dump_se(se, "an outer edge");
        }
#endif
        BLI_linklist_prepend_pool(&fstack, fsym, cdt->listpool);
      }
    }
  } while ((se = se->next) != se_start);

  while (fstack != NULL) {
    LinkNode *to_dissolve = NULL;
    bool dissolvable;
    f = (CDTFace *)BLI_linklist_pop_pool(&fstack, cdt->listpool);
    if (f->visit_index == visit) {
#ifdef DEBUG_CDT
      if (dbg_level > 0) {
        fprintf(stderr, "skipping f=%p, already visited\n", f);
      }
#endif
      continue;
    }
    BLI_assert(f != cdt->outer_face);
#ifdef DEBUG_CDT
    if (dbg_level > 0) {
      fprintf(stderr, "top of loop, f=%p\n", f);
      dump_se_cycle(f->symedge, "visit", 10000);
      if (dbg_level > 1) {
        dump_cdt(cdt, "cdt at top of loop");
        cdt_draw(cdt, "top of dissolve loop");
      }
    }
#endif
    f->visit_index = visit;
    se_start = se = f->symedge;
    do {
      dissolvable = !is_constrained_edge(se->edge);
#ifdef DEBUG_CDT
      if (dbg_level > 1) {
        dump_se(se, "edge in f");
        fprintf(stderr, "  dissolvable=%d\n", dissolvable);
      }
#endif
      if (dissolvable) {
        fsym = sym(se)->face;
#ifdef DEBUG_CDT
        if (dbg_level > 1) {
          dump_se_cycle(fsym->symedge, "fsym", 10000);
          fprintf(stderr, "  visited=%d\n", fsym->visit_index == visit);
        }
#endif
        if (fsym->visit_index != visit) {
#ifdef DEBUG_CDT
          if (dbg_level > 0) {
            fprintf(stderr, "pushing face %p\n", fsym);
            dump_se_cycle(fsym->symedge, "pushed", 10000);
          }
#endif
          BLI_linklist_prepend_pool(&fstack, fsym, cdt->listpool);
        }
        else {
          BLI_linklist_prepend_pool(&to_dissolve, se, cdt->listpool);
        }
      }
      se = se->next;
    } while (se != se_start);
    while (to_dissolve != NULL) {
      se = (SymEdge *)BLI_linklist_pop_pool(&to_dissolve, cdt->listpool);
      if (se->next != NULL) {
        dissolve_symedge(cdt, se);
      }
    }
  }
}

/**
 * Remove edges and merge faces to get desired output, as per options.
 * \note the cdt cannot be further changed after this.
 */
static void prepare_cdt_for_output(CDT_state *cdt, const CDT_output_type output_type)
{
  CDTFace *f;
  CDTEdge *e;
  LinkNode *ln;

  cdt->output_prepared = true;
  if (cdt->edges == NULL) {
    return;
  }

  /* Make sure all non-deleted faces have a symedge. */
  for (ln = cdt->edges; ln; ln = ln->next) {
    e = (CDTEdge *)ln->link;
    if (!is_deleted_edge(e)) {
      if (e->symedges[0].face->symedge == NULL) {
        e->symedges[0].face->symedge = &e->symedges[0];
      }
      if (e->symedges[1].face->symedge == NULL) {
        e->symedges[1].face->symedge = &e->symedges[1];
      }
    }
  }
#ifdef DEBUG_CDT
  /* All non-deleted faces should have a symedge now. */
  for (ln = cdt->faces; ln; ln = ln->next) {
    f = (CDTFace *)ln->link;
    if (!f->deleted) {
      BLI_assert(f->symedge != NULL);
    }
  }
#else
  UNUSED_VARS(f);
#endif

  if (output_type == CDT_CONSTRAINTS) {
    remove_non_constraint_edges(cdt);
  }
  else if (output_type == CDT_CONSTRAINTS_VALID_BMESH) {
    remove_non_constraint_edges_leave_valid_bmesh(cdt);
  }
  else if (output_type == CDT_INSIDE) {
    remove_outer_edges_until_constraints(cdt);
  }
}

static CDT_result *cdt_get_output(CDT_state *cdt,
                                  const CDT_input *input,
                                  const CDT_output_type output_type)
{
  int i, j, nv, ne, nf, faces_len_total;
  int orig_map_size, orig_map_index;
  int *vert_to_output_map;
  CDT_result *result;
  CDTVert *v;
  LinkNode *lne, *lnf, *ln;
  SymEdge *se, *se_start;
  CDTEdge *e;
  CDTFace *f;
#ifdef DEBUG_CDT
  int dbg_level = 0;

  if (dbg_level > 0) {
    fprintf(stderr, "\nCDT_GET_OUTPUT\n\n");
  }
#endif

  prepare_cdt_for_output(cdt, output_type);

  result = (CDT_result *)MEM_callocN(sizeof(*result), __func__);
  if (cdt->vert_array_len == 0) {
    return result;
  }

#ifdef DEBUG_CDT
  if (dbg_level > 1) {
    dump_cdt(cdt, "cdt to output");
  }
#endif

  /* All verts without a merge_to_index will be output.
   * vert_to_output_map[i] will hold the output vertex index
   * corresponding to the vert in position i in cdt->vert_array.
   * Since merging picked the leftmost-lowermost representative,
   * that is not necessarily the same as the vertex with the lowest original
   * index (i.e., index in cdt->vert_array), so we need two passes:
   * one to get the non-merged-to vertices in vert_to_output_map,
   * and a second to put in the merge targets for merged-to vertices.
   */
  vert_to_output_map = BLI_memarena_alloc(cdt->arena, (size_t)cdt->vert_array_len * sizeof(int *));
  nv = 0;
  for (i = 0; i < cdt->vert_array_len; i++) {
    v = cdt->vert_array[i];
    if (v->merge_to_index == -1) {
      vert_to_output_map[i] = nv;
      nv++;
    }
  }
  if (nv <= 0) {
    return result;
  }
  if (nv < cdt->vert_array_len) {
    for (i = 0; i < input->verts_len; i++) {
      v = cdt->vert_array[i];
      if (v->merge_to_index != -1) {
        add_to_input_ids(&cdt->vert_array[v->merge_to_index]->input_ids, i, cdt);
        vert_to_output_map[i] = vert_to_output_map[v->merge_to_index];
      }
    }
  }

  result->verts_len = nv;
  result->vert_coords = MEM_malloc_arrayN(nv, sizeof(result->vert_coords[0]), __func__);

  /* Make the vertex "orig" map arrays, mapping output verts to lists of input ones. */
  orig_map_size = 0;
  for (i = 0; i < cdt->vert_array_len; i++) {
    if (cdt->vert_array[i]->merge_to_index == -1) {
      orig_map_size += 1 + BLI_linklist_count(cdt->vert_array[i]->input_ids);
    }
  }
  result->verts_orig_len_table = MEM_malloc_arrayN(nv, sizeof(int), __func__);
  result->verts_orig_start_table = MEM_malloc_arrayN(nv, sizeof(int), __func__);
  result->verts_orig = MEM_malloc_arrayN(orig_map_size, sizeof(int), __func__);

  orig_map_index = 0;
  i = 0;
  for (j = 0; j < cdt->vert_array_len; j++) {
    v = cdt->vert_array[j];
    if (v->merge_to_index == -1) {
      result->vert_coords[i][0] = (float)v->co[0];
      result->vert_coords[i][1] = (float)v->co[1];
      result->verts_orig_start_table[i] = orig_map_index;
      if (j < input->verts_len) {
        result->verts_orig[orig_map_index++] = j;
      }
      for (ln = v->input_ids; ln; ln = ln->next) {
        result->verts_orig[orig_map_index++] = POINTER_AS_INT(ln->link);
      }
      result->verts_orig_len_table[i] = orig_map_index - result->verts_orig_start_table[i];
      i++;
    }
  }

  ne = 0;
  orig_map_size = 0;
  for (ln = cdt->edges; ln; ln = ln->next) {
    e = (CDTEdge *)ln->link;
    if (!is_deleted_edge(e)) {
      ne++;
      if (e->input_ids) {
        orig_map_size += BLI_linklist_count(e->input_ids);
      }
    }
  }
  if (ne != 0) {
    result->edges_len = ne;
    result->face_edge_offset = cdt->face_edge_offset;
    result->edges = MEM_malloc_arrayN(ne, sizeof(result->edges[0]), __func__);
    result->edges_orig_len_table = MEM_malloc_arrayN(ne, sizeof(int), __func__);
    result->edges_orig_start_table = MEM_malloc_arrayN(ne, sizeof(int), __func__);
    if (orig_map_size > 0) {
      result->edges_orig = MEM_malloc_arrayN(orig_map_size, sizeof(int), __func__);
    }
    orig_map_index = 0;
    i = 0;
    for (lne = cdt->edges; lne; lne = lne->next) {
      e = (CDTEdge *)lne->link;
      if (!is_deleted_edge(e)) {
        result->edges[i][0] = vert_to_output_map[e->symedges[0].vert->index];
        result->edges[i][1] = vert_to_output_map[e->symedges[1].vert->index];
        result->edges_orig_start_table[i] = orig_map_index;
        for (ln = e->input_ids; ln; ln = ln->next) {
          result->edges_orig[orig_map_index++] = POINTER_AS_INT(ln->link);
        }
        result->edges_orig_len_table[i] = orig_map_index - result->edges_orig_start_table[i];
        i++;
      }
    }
  }

  nf = 0;
  faces_len_total = 0;
  orig_map_size = 0;
  for (ln = cdt->faces; ln; ln = ln->next) {
    f = (CDTFace *)ln->link;
    if (!f->deleted && f != cdt->outer_face) {
      nf++;
      se = se_start = f->symedge;
      BLI_assert(se != NULL);
      do {
        faces_len_total++;
        se = se->next;
      } while (se != se_start);
      if (f->input_ids) {
        orig_map_size += BLI_linklist_count(f->input_ids);
      }
    }
  }

  if (nf != 0) {
    result->faces_len = nf;
    result->faces_len_table = MEM_malloc_arrayN(nf, sizeof(int), __func__);
    result->faces_start_table = MEM_malloc_arrayN(nf, sizeof(int), __func__);
    result->faces = MEM_malloc_arrayN(faces_len_total, sizeof(int), __func__);
    result->faces_orig_len_table = MEM_malloc_arrayN(nf, sizeof(int), __func__);
    result->faces_orig_start_table = MEM_malloc_arrayN(nf, sizeof(int), __func__);
    if (orig_map_size > 0) {
      result->faces_orig = MEM_malloc_arrayN(orig_map_size, sizeof(int), __func__);
    }
    orig_map_index = 0;
    i = 0;
    j = 0;
    for (lnf = cdt->faces; lnf; lnf = lnf->next) {
      f = (CDTFace *)lnf->link;
      if (!f->deleted && f != cdt->outer_face) {
        result->faces_start_table[i] = j;
        se = se_start = f->symedge;
        do {
          result->faces[j++] = vert_to_output_map[se->vert->index];
          se = se->next;
        } while (se != se_start);
        result->faces_len_table[i] = j - result->faces_start_table[i];
        result->faces_orig_start_table[i] = orig_map_index;
        for (ln = f->input_ids; ln; ln = ln->next) {
          result->faces_orig[orig_map_index++] = POINTER_AS_INT(ln->link);
        }
        result->faces_orig_len_table[i] = orig_map_index - result->faces_orig_start_table[i];
        i++;
      }
    }
  }
  return result;
}

/**
 * Calculate the Constrained Delaunay Triangulation of the 2d elements given in \a input.
 *
 * A Delaunay triangulation of a set of vertices is a triangulation where no triangle in the
 * triangulation has a circumcircle that strictly contains another vertex. Delaunay triangulations
 * are avoid long skinny triangles: they maximize the minimum angle of all triangles in the
 * triangulation.
 *
 * A Constrained Delaunay Triangulation adds the requirement that user-provided line segments must
 * appear as edges in the output (perhaps divided into several sub-segments). It is not required
 * that the input edges be non-intersecting: this routine will calculate the intersections. This
 * means that besides triangulating, this routine is also useful for general and robust 2d edge and
 * face intersection.
 *
 * This routine also takes an epsilon parameter in the \a input. Input vertices closer than epsilon
 * will be merged, and we collapse tiny edges (less than epsilon length).
 *
 * The current initial Deluanay triangulation algorithm is the Guibas-Stolfi Divide and Conquer
 * algorithm (see "Primitives for the Manipulation of General Subdivisions and the Computation of
 * Voronoi Diagrams"). and uses Shewchuk's exact predicates to issues where numeric errors cause
 * inconsistent geometric judgments. This is followed by inserting edge constraints (including the
 * edges implied by faces) using the algorithms discussed in "Fully Dynamic Constrained Delaunay
 * Triangulations" by Kallmann, Bieri, and Thalmann.
 *
 * \param input: points to a CDT_input struct which contains the vertices, edges, and faces to be
 * triangulated. \param output_type: specifies which edges to remove after doing the triangulation.
 * \return A pointer to an allocated CDT_result struct, which describes the triangulation in terms
 * of vertices, edges, and faces, and also has tables to map output elements back to input
 * elements. The caller must use BLI_delaunay_2d_cdt_free() on the result when done with it.
 *
 * See the header file BLI_delaunay_2d.h for details of the CDT_input and CDT_result structs and
 * the CDT_output_type enum.
 */
CDT_result *BLI_delaunay_2d_cdt_calc(const CDT_input *input, const CDT_output_type output_type)
{
  int nv = input->verts_len;
  int ne = input->edges_len;
  int nf = input->faces_len;
  int i, iv1, iv2, f, fedge_start, fedge_end, ei;
  CDT_state *cdt;
  CDTVert *v1, *v2;
  CDTEdge *face_edge;
  SymEdge *face_symedge;
  LinkNode *edge_list;
  CDT_result *result;
  const CDT_input *input_orig;
  int *new_edge_map;
  static bool called_exactinit = false;
#ifdef DEBUG_CDT
  int dbg_level = 0;
#endif

  /* The exact orientation and incircle primitives need a one-time initialization of certain
   * constants. */
  if (!called_exactinit) {
    exactinit();
    called_exactinit = true;
  }
#ifdef DEBUG_CDT
  if (dbg_level > 0) {
    fprintf(stderr,
            "\n\nCDT CALC, nv=%d, ne=%d, nf=%d, eps=%g\n",
            input->verts_len,
            input->edges_len,
            input->faces_len,
            input->epsilon);
  }
  if (dbg_level == -1) {
    write_cdt_input_to_file(input);
  }
#endif

  if ((nv > 0 && input->vert_coords == NULL) || (ne > 0 && input->edges == NULL) ||
      (nf > 0 && (input->faces == NULL || input->faces_start_table == NULL ||
                  input->faces_len_table == NULL))) {
#ifdef DEBUG_CDT
    fprintf(stderr, "invalid input: unexpected NULL array(s)\n");
#endif
    return NULL;
  }

  input_orig = input;
  input = modify_input_for_near_edge_ends(input, &new_edge_map);
  if (input != input_orig) {
    nv = input->verts_len;
    ne = input->edges_len;
    nf = input->faces_len;
#ifdef DEBUG_CDT
    if (dbg_level > 0) {
      fprintf(stderr, "input modified for near ends; now ne=%d\n", ne);
    }
#endif
  }
  cdt = cdt_init(input);
  initial_triangulation(cdt);
#ifdef DEBUG_CDT
  if (dbg_level > 0) {
    validate_cdt(cdt, true, false, false);
    if (dbg_level > 1) {
      cdt_draw(cdt, "after initial triangulation");
    }
  }
#endif

  for (i = 0; i < ne; i++) {
    iv1 = input->edges[i][0];
    iv2 = input->edges[i][1];
    if (iv1 < 0 || iv1 >= nv || iv2 < 0 || iv2 >= nv) {
#ifdef DEBUG_CDT
      fprintf(stderr, "edge indices for e%d not valid: v1=%d, v2=%d, nv=%d\n", i, iv1, iv2, nv);
#endif
      continue;
    }
    v1 = cdt->vert_array[iv1];
    v2 = cdt->vert_array[iv2];
    if (v1->merge_to_index != -1) {
      v1 = cdt->vert_array[v1->merge_to_index];
    }
    if (v2->merge_to_index != -1) {
      v2 = cdt->vert_array[v2->merge_to_index];
    }
    if (new_edge_map) {
      ei = new_edge_map[i];
    }
    else {
      ei = i;
    }
    add_edge_constraint(cdt, v1, v2, ei, NULL);
#ifdef DEBUG_CDT
    if (dbg_level > 3) {
      char namebuf[60];
      sprintf(namebuf, "after edge constraint %d = (%d,%d)\n", i, iv1, iv2);
      cdt_draw(cdt, namebuf);
      // dump_cdt(cdt, namebuf);
      validate_cdt(cdt, true, true, false);
    }
#endif
  }

  cdt->face_edge_offset = ne;
  for (f = 0; f < nf; f++) {
    int flen = input->faces_len_table[f];
    int fstart = input->faces_start_table[f];
    if (flen <= 2) {
#ifdef DEBUG_CDT
      fprintf(stderr, "face %d has length %d; ignored\n", f, flen);
#endif
      continue;
    }
    for (i = 0; i < flen; i++) {
      int face_edge_id = cdt->face_edge_offset + fstart + i;
      if (new_edge_map) {
        face_edge_id = new_edge_map[face_edge_id];
      }
      iv1 = input->faces[fstart + i];
      iv2 = input->faces[fstart + ((i + 1) % flen)];
      if (iv1 < 0 || iv1 >= nv || iv2 < 0 || iv2 >= nv) {
#ifdef DEBUG_CDT
        fprintf(stderr, "face indices not valid: f=%d, iv1=%d, iv2=%d, nv=%d\n", f, iv1, iv2, nv);
#endif
        continue;
      }
      v1 = cdt->vert_array[iv1];
      v2 = cdt->vert_array[iv2];
      if (v1->merge_to_index != -1) {
        v1 = cdt->vert_array[v1->merge_to_index];
      }
      if (v2->merge_to_index != -1) {
        v2 = cdt->vert_array[v2->merge_to_index];
      }
      add_edge_constraint(cdt, v1, v2, face_edge_id, &edge_list);
#ifdef DEBUG_CDT
      if (dbg_level > 2) {
        fprintf(stderr, "edges for edge %d:\n", i);
        for (LinkNode *ln = edge_list; ln; ln = ln->next) {
          CDTEdge *cdt_e = (CDTEdge *)ln->link;
          fprintf(stderr,
                  "  (%.2f,%.2f)->(%.2f,%.2f)\n",
                  F2(cdt_e->symedges[0].vert->co),
                  F2(cdt_e->symedges[1].vert->co));
        }
      }
      if (dbg_level > 2) {
        cdt_draw(cdt, "after a face edge");
        if (dbg_level > 3) {
          dump_cdt(cdt, "after a face edge");
        }
        validate_cdt(cdt, true, true, false);
      }
#endif
      if (i == 0) {
        face_edge = (CDTEdge *)edge_list->link;
        face_symedge = &face_edge->symedges[0];
        if (face_symedge->vert != v1) {
          face_symedge = &face_edge->symedges[1];
          BLI_assert(face_symedge->vert == v1);
        }
      }
      BLI_linklist_free_pool(edge_list, NULL, cdt->listpool);
    }
    fedge_start = cdt->face_edge_offset + fstart;
    fedge_end = fedge_start + flen - 1;
    add_face_ids(cdt, face_symedge, f, fedge_start, fedge_end);
  }
#ifdef DEBUG_CDT
  if (dbg_level > 0) {
    validate_cdt(cdt, true, true, false);
  }
  if (dbg_level > 1) {
    cdt_draw(cdt, "after adding edges and faces");
    if (dbg_level > 2) {
      dump_cdt(cdt, "after adding edges and faces");
    }
  }
#endif

  if (cdt->epsilon > 0.0) {
    remove_small_features(cdt);
#ifdef DEBUG_CDT
    if (dbg_level > 2) {
      cdt_draw(cdt, "after remove small features\n");
      if (dbg_level > 3) {
        dump_cdt(cdt, "after remove small features\n");
      }
    }
#endif
  }

  result = cdt_get_output(cdt, input, output_type);
#ifdef DEBUG_CDT
  if (dbg_level > 0) {
    cdt_draw(cdt, "final");
  }
#endif

  if (input != input_orig) {
    free_modified_input((CDT_input *)input);
  }
  new_cdt_free(cdt);
  return result;
}

void BLI_delaunay_2d_cdt_free(CDT_result *result)
{
  if (result == NULL) {
    return;
  }
  if (result->vert_coords) {
    MEM_freeN(result->vert_coords);
  }
  if (result->edges) {
    MEM_freeN(result->edges);
  }
  if (result->faces) {
    MEM_freeN(result->faces);
  }
  if (result->faces_start_table) {
    MEM_freeN(result->faces_start_table);
  }
  if (result->faces_len_table) {
    MEM_freeN(result->faces_len_table);
  }
  if (result->verts_orig) {
    MEM_freeN(result->verts_orig);
  }
  if (result->verts_orig_start_table) {
    MEM_freeN(result->verts_orig_start_table);
  }
  if (result->verts_orig_len_table) {
    MEM_freeN(result->verts_orig_len_table);
  }
  if (result->edges_orig) {
    MEM_freeN(result->edges_orig);
  }
  if (result->edges_orig_start_table) {
    MEM_freeN(result->edges_orig_start_table);
  }
  if (result->edges_orig_len_table) {
    MEM_freeN(result->edges_orig_len_table);
  }
  if (result->faces_orig) {
    MEM_freeN(result->faces_orig);
  }
  if (result->faces_orig_start_table) {
    MEM_freeN(result->faces_orig_start_table);
  }
  if (result->faces_orig_len_table) {
    MEM_freeN(result->faces_orig_len_table);
  }
  MEM_freeN(result);
}

#ifdef DEBUG_CDT

ATTU static const char *vertname(const CDTVert *v)
{
  static char vertnamebuf[20];

  sprintf(vertnamebuf, "[%d]", v->index);
  return vertnamebuf;
}

ATTU static const char *sename(const SymEdge *se)
{
  static char senamebuf[20];

  sprintf(senamebuf, "{%x}", (POINTER_AS_UINT(se)) & 0xFFFF);
  return senamebuf;
}

static void dump_v(const CDTVert *v, const char *lab)
{
  fprintf(stderr, "%s%s(%.10f,%.10f)\n", lab, vertname(v), F2(v->co));
}

static void dump_se(const SymEdge *se, const char *lab)
{
  if (se->next) {
    fprintf(stderr,
            "%s%s((%.10f,%.10f)->(%.10f,%.10f))",
            lab,
            vertname(se->vert),
            F2(se->vert->co),
            F2(se->next->vert->co));
    fprintf(stderr, "%s\n", vertname(se->next->vert));
  }
  else {
    fprintf(stderr, "%s%s((%.10f,%.10f)->NULL)\n", lab, vertname(se->vert), F2(se->vert->co));
  }
}

static void dump_se_short(const SymEdge *se, const char *lab)
{
  if (se == NULL) {
    fprintf(stderr, "%sNULL", lab);
  }
  else {
    fprintf(stderr, "%s%s", lab, vertname(se->vert));
    fprintf(stderr, "%s", se->next == NULL ? "[NULL]" : vertname(se->next->vert));
  }
}

static void dump_se_cycle(const SymEdge *se, const char *lab, const int limit)
{
  int count = 0;
  const SymEdge *s = se;
  fprintf(stderr, "%s:\n", lab);
  do {
    dump_se(s, "  ");
    s = s->next;
    count++;
  } while (s != se && count < limit);
  if (count == limit) {
    fprintf(stderr, "  limit hit without cycle!\n");
  }
}

static void dump_id_list(const LinkNode *id_list, const char *lab)
{
  const LinkNode *ln;
  if (!id_list) {
    return;
  }
  fprintf(stderr, "%s", lab);
  for (ln = id_list; ln; ln = ln->next) {
    fprintf(stderr, "%d%c", POINTER_AS_INT(ln->link), ln->next ? ' ' : '\n');
  }
}

static void dump_cross_data(struct CrossData *cd, const char *lab)
{
  fprintf(stderr, "%s", lab);
  if (cd->lambda == 0.0) {
    fprintf(stderr, "v%d", cd->vert->index);
  }
  else {
    fprintf(stderr, "lambda=%.17g", cd->lambda);
  }
  dump_se_short(cd->in, " in=");
  dump_se_short(cd->out, " out=");
  fprintf(stderr, "\n");
}

/* If filter_fn != NULL, only dump vert v its edges when filter_fn(cdt, v, filter_data) is true. */
#  define PL(p) (POINTER_AS_UINT(p) & 0xFFFF)
static void dump_cdt_filtered(const CDT_state *cdt,
                              bool (*filter_fn)(const CDT_state *, int, void *),
                              void *filter_data,
                              const char *lab)
{
  LinkNode *ln;
  CDTVert *v, *vother;
  CDTEdge *e;
  CDTFace *f;
  SymEdge *se;
  int i, cnt;

  fprintf(stderr, "\nCDT %s\n", lab);
  fprintf(stderr, "\nVERTS\n");
  for (i = 0; i < cdt->vert_array_len; i++) {
    if (filter_fn && !filter_fn(cdt, i, filter_data)) {
      continue;
    }
    v = cdt->vert_array[i];
    fprintf(stderr, "%s %x: (%f,%f) symedge=%x", vertname(v), PL(v), F2(v->co), PL(v->symedge));
    if (v->merge_to_index == -1) {
      fprintf(stderr, "\n");
    }
    else {
      fprintf(stderr, " merge to %s\n", vertname(cdt->vert_array[v->merge_to_index]));
      continue;
    }
    dump_id_list(v->input_ids, "  ");
    se = v->symedge;
    cnt = 0;
    if (se) {
      fprintf(stderr, "  edges out:\n");
      do {
        if (se->next == NULL) {
          fprintf(stderr, "    [NULL next/rot symedge, se=%x\n", PL(se));
          break;
        }
        if (se->next->next == NULL) {
          fprintf(stderr, "    [NULL next-next/rot symedge, se=%x\n", PL(se));
          break;
        }
        vother = sym(se)->vert;
        fprintf(stderr, "    %s (e=%x, se=%x)\n", vertname(vother), PL(se->edge), PL(se));
        se = se->rot;
        cnt++;
      } while (se != v->symedge && cnt < 25);
      fprintf(stderr, "\n");
    }
  }
  if (filter_fn) {
    return;
  }
  fprintf(stderr, "\nEDGES\n");
  for (ln = cdt->edges; ln; ln = ln->next) {
    e = (CDTEdge *)ln->link;
    if (e->symedges[0].next == NULL) {
      continue;
    }
    fprintf(stderr, "%x:\n", PL(e));
    for (i = 0; i < 2; i++) {
      se = &e->symedges[i];
      fprintf(stderr,
              "  se[%d] @%x: next=%x, rot=%x, vert=%x [%s] (%.2f,%.2f), edge=%x, face=%x\n",
              i,
              PL(se),
              PL(se->next),
              PL(se->rot),
              PL(se->vert),
              vertname(se->vert),
              F2(se->vert->co),
              PL(se->edge),
              PL(se->face));
    }
    dump_id_list(e->input_ids, "  ");
  }
  fprintf(stderr, "\nFACES\n");
  for (ln = cdt->faces; ln; ln = ln->next) {
    f = (CDTFace *)ln->link;
    if (f->deleted) {
      continue;
    }
    if (f == cdt->outer_face) {
      fprintf(stderr, "%x: outer", PL(f));
    }
    fprintf(stderr, " symedge=%x\n", PL(f->symedge));
    dump_id_list(f->input_ids, "  ");
  }
  fprintf(stderr, "\nOTHER\n");
  fprintf(stderr, "outer_face=%x\n", PL(cdt->outer_face));
  fprintf(
      stderr, "minx=%f, maxx=%f, miny=%f, maxy=%f\n", cdt->minx, cdt->maxx, cdt->miny, cdt->maxy);
  fprintf(stderr, "margin=%f\n", cdt->margin);
}
#  undef PL

static void dump_cdt(const CDT_state *cdt, const char *lab)
{
  dump_cdt_filtered(cdt, NULL, NULL, lab);
}

typedef struct ReachableFilterData {
  int vstart_index;
  int maxdist;
} ReachableFilterData;

/* Stupid algorithm will repeat itself. Don't use for large n. */
static bool reachable_filter(const CDT_state *cdt, int v_index, void *filter_data)
{
  CDTVert *v;
  SymEdge *se;
  ReachableFilterData *rfd_in = (ReachableFilterData *)filter_data;
  ReachableFilterData rfd_next;

  if (v_index == rfd_in->vstart_index) {
    return true;
  }
  if (rfd_in->maxdist <= 0 || v_index < 0 || v_index >= cdt->vert_array_len) {
    return false;
  }
  else {
    v = cdt->vert_array[v_index];
    se = v->symedge;
    if (se != NULL) {
      rfd_next.vstart_index = rfd_in->vstart_index;
      rfd_next.maxdist = rfd_in->maxdist - 1;
      do {
        if (reachable_filter(cdt, se->next->vert->index, &rfd_next)) {
          return true;
        }
        se = se->rot;
      } while (se != v->symedge);
    }
  }
  return false;
}

static void set_min_max(CDT_state *cdt)
{
  int i;
  double minx, maxx, miny, maxy;
  double *co;

  minx = miny = DBL_MAX;
  maxx = maxy = -DBL_MAX;
  for (i = 0; i < cdt->vert_array_len; i++) {
    co = cdt->vert_array[i]->co;
    if (co[0] < minx) {
      minx = co[0];
    }
    if (co[0] > maxx) {
      maxx = co[0];
    }
    if (co[1] < miny) {
      miny = co[1];
    }
    if (co[1] > maxy) {
      maxy = co[1];
    }
  }
  if (minx != DBL_MAX) {
    cdt->minx = minx;
    cdt->miny = miny;
    cdt->maxx = maxx;
    cdt->maxy = maxy;
  }
}

static void dump_cdt_vert_neighborhood(CDT_state *cdt, int v, int maxdist, const char *lab)
{
  ReachableFilterData rfd;
  rfd.vstart_index = v;
  rfd.maxdist = maxdist;
  dump_cdt_filtered(cdt, reachable_filter, &rfd, lab);
}

/*
 * Make an html file with svg in it to display the argument cdt.
 * Mouse-overs will reveal the coordinates of vertices and edges.
 * Constraint edges are drawn thicker than non-constraint edges.
 * The first call creates DRAWFILE; subsequent calls append to it.
 */
#  define DRAWFILE "/tmp/debug_draw.html"
#  define MAX_DRAW_WIDTH 2000
#  define MAX_DRAW_HEIGHT 1400
#  define THIN_LINE 1
#  define THICK_LINE 4
#  define VERT_RADIUS 3
#  define DRAW_VERT_LABELS 1
#  define DRAW_EDGE_LABELS 0

static void cdt_draw_region(
    CDT_state *cdt, const char *lab, double minx, double miny, double maxx, double maxy)
{
  static bool append = false;
  FILE *f = fopen(DRAWFILE, append ? "a" : "w");
  int view_width, view_height;
  double width, height, aspect, scale;
  LinkNode *ln;
  CDTVert *v, *u;
  CDTEdge *e;
  int i, strokew;

  width = maxx - minx;
  height = maxy - miny;
  aspect = height / width;
  view_width = MAX_DRAW_WIDTH;
  view_height = (int)(view_width * aspect);
  if (view_height > MAX_DRAW_HEIGHT) {
    view_height = MAX_DRAW_HEIGHT;
    view_width = (int)(view_height / aspect);
  }
  scale = view_width / width;

#  define SX(x) ((x - minx) * scale)
#  define SY(y) ((maxy - y) * scale)

  if (!f) {
    printf("couldn't open file %s\n", DRAWFILE);
    return;
  }
  fprintf(f, "<div>%s</div>\n<div>\n", lab);
  fprintf(f,
          "<svg version=\"1.1\" "
          "xmlns=\"http://www.w3.org/2000/svg\" "
          "xmlns:xlink=\"http://www.w3.org/1999/xlink\" "
          "xml:space=\"preserve\"\n");
  fprintf(f, "width=\"%d\" height=\"%d\">/n", view_width, view_height);

  for (ln = cdt->edges; ln; ln = ln->next) {
    e = (CDTEdge *)ln->link;
    if (is_deleted_edge(e)) {
      continue;
    }
    u = e->symedges[0].vert;
    v = e->symedges[1].vert;
    strokew = is_constrained_edge(e) ? THICK_LINE : THIN_LINE;
    fprintf(f,
            "<line fill=\"none\" stroke=\"black\" stroke-width=\"%d\" "
            "x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\">\n",
            strokew,
            SX(u->co[0]),
            SY(u->co[1]),
            SX(v->co[0]),
            SY(v->co[1]));
    fprintf(f, "  <title>%s", vertname(u));
    fprintf(f, "%s</title>\n", vertname(v));
    fprintf(f, "</line>\n");
#  if DRAW_EDGE_LABELS
    fprintf(f,
            "<text x=\"%f\" y=\"%f\" font-size=\"small\">",
            SX(0.5 * (u->co[0] + v->co[0])),
            SY(0.5 * (u->co[1] + v->co[1])));
    fprintf(f, "%s", vertname(u));
    fprintf(f, "%s", vertname(v));
    fprintf(f, "%s", sename(&e->symedges[0]));
    fprintf(f, "%s</text>\n", sename(&e->symedges[1]));
#  endif
  }
  i = 0;
  for (; i < cdt->vert_array_len; i++) {
    v = cdt->vert_array[i];
    if (v->merge_to_index != -1) {
      continue;
    }
    fprintf(f,
            "<circle fill=\"black\" cx=\"%f\" cy=\"%f\" r=\"%d\">\n",
            SX(v->co[0]),
            SY(v->co[1]),
            VERT_RADIUS);
    fprintf(f, "  <title>%s(%.10f,%.10f)</title>\n", vertname(v), v->co[0], v->co[1]);
    fprintf(f, "</circle>\n");
#  if DRAW_VERT_LABELS
    fprintf(f,
            "<text x=\"%f\" y=\"%f\" font-size=\"small\">%s</text>\n",
            SX(v->co[0]) + VERT_RADIUS,
            SY(v->co[1]) - VERT_RADIUS,
            vertname(v));
#  endif
  }

  fprintf(f, "</svg>\n</div>\n");
  fclose(f);
  append = true;
#  undef SX
#  undef SY
}

static void cdt_draw(CDT_state *cdt, const char *lab)
{
  double draw_margin, minx, maxx, miny, maxy;

  set_min_max(cdt);
  draw_margin = (cdt->maxx - cdt->minx + cdt->maxy - cdt->miny + 1) * 0.05;
  minx = cdt->minx - draw_margin;
  maxx = cdt->maxx + draw_margin;
  miny = cdt->miny - draw_margin;
  maxy = cdt->maxy + draw_margin;
  cdt_draw_region(cdt, lab, minx, miny, maxx, maxy);
}

static void cdt_draw_vertex_region(CDT_state *cdt, int v, double dist, const char *lab)
{
  const double *co = cdt->vert_array[v]->co;
  cdt_draw_region(cdt, lab, co[0] - dist, co[1] - dist, co[0] + dist, co[1] + dist);
}

static void cdt_draw_edge_region(CDT_state *cdt, int v1, int v2, double dist, const char *lab)
{
  const double *co1 = cdt->vert_array[v1]->co;
  const double *co2 = cdt->vert_array[v2]->co;
  double minx, miny, maxx, maxy;

  minx = min_dd(co1[0], co2[0]);
  miny = min_dd(co1[1], co2[1]);
  maxx = max_dd(co1[0], co2[0]);
  maxy = max_dd(co1[1], co2[1]);
  cdt_draw_region(cdt, lab, minx - dist, miny - dist, maxx + dist, maxy + dist);
}

#  define CDTFILE "/tmp/cdtinput.txt"
static void write_cdt_input_to_file(const CDT_input *inp)
{
  int i, j;
  FILE *f = fopen(CDTFILE, "w");

  fprintf(f, "%d %d %d\n", inp->verts_len, inp->edges_len, inp->faces_len);
  for (i = 0; i < inp->verts_len; i++) {
    fprintf(f, "%.17f %.17f\n", inp->vert_coords[i][0], inp->vert_coords[i][1]);
  }
  for (i = 0; i < inp->edges_len; i++) {
    fprintf(f, "%d %d\n", inp->edges[i][0], inp->edges[i][1]);
  }
  for (i = 0; i < inp->faces_len; i++) {
    for (j = 0; j < inp->faces_len_table[i]; j++) {
      fprintf(f, "%d ", inp->faces[j + inp->faces_start_table[i]]);
    }
    fprintf(f, "\n");
  }
  fclose(f);
}

#  ifndef NDEBUG /* Only used in assert. */
/*
 * Is a visible from b: i.e., ab crosses no edge of cdt?
 * If constrained is true, consider only constrained edges as possible crossed edges.
 * In any case, don't count an edge ab itself.
 * Note: this is an expensive test if there are a lot of edges.
 */
static bool is_visible(const CDTVert *a, const CDTVert *b, bool constrained, const CDT_state *cdt)
{
  const LinkNode *ln;
  const CDTEdge *e;
  const SymEdge *se, *senext;
  double lambda, mu;
  int ikind;

  for (ln = cdt->edges; ln; ln = ln->next) {
    e = (const CDTEdge *)ln->link;
    if (is_deleted_edge(e) || is_border_edge(e, cdt)) {
      continue;
    }
    if (constrained && !is_constrained_edge(e)) {
      continue;
    }
    se = (const SymEdge *)&e->symedges[0];
    senext = se->next;
    if ((a == se->vert || a == senext->vert) || b == se->vert || b == se->next->vert) {
      continue;
    }
    ikind = isect_seg_seg_v2_lambda_mu_db(
        a->co, b->co, se->vert->co, senext->vert->co, &lambda, &mu);
    if (ikind != ISECT_LINE_LINE_NONE) {
      if (ikind == ISECT_LINE_LINE_COLINEAR) {
        /* TODO: special test here for overlap. */
        continue;
      }
      /* Allow an intersection very near or at ends, to allow for numerical error. */
      if (lambda > FLT_EPSILON && (1.0 - lambda) > FLT_EPSILON && mu > FLT_EPSILON &&
          (1.0 - mu) > FLT_EPSILON) {
        return false;
      }
    }
  }
  return true;
}
#  endif

#  ifndef NDEBUG /* Only used in assert. */
/*
 * Check that edge ab satisfies constrained delaunay condition:
 * That is, for all non-constraint, non-border edges ab,
 * (1) ab is visible in the constraint graph; and
 * (2) there is a circle through a and b such that any vertex v connected by an edge to a or b
 *     is not inside that circle.
 * The argument 'se' specifies ab by: a is se's vert and b is se->next's vert.
 * Return true if check is OK.
 */
static bool is_delaunay_edge(const SymEdge *se)
{
  int i;
  CDTVert *a, *b, *c;
  const SymEdge *sesym, *curse, *ss;
  bool ok[2];

  if (!is_constrained_edge(se->edge)) {
    return true;
  }
  sesym = sym(se);
  a = se->vert;
  b = se->next->vert;
  /* Try both the triangles adjacent to se's edge for circle. */
  for (i = 0; i < 2; i++) {
    ok[i] = true;
    curse = (i == 0) ? se : sesym;
    a = curse->vert;
    b = curse->next->vert;
    c = curse->next->next->vert;
    for (ss = curse->rot; ss != curse; ss = ss->rot) {
      ok[i] |= incircle(a->co, b->co, c->co, ss->next->vert->co) <= 0.0;
    }
  }
  return ok[0] || ok[1];
}
#  endif

#  ifndef NDEBUG
static bool plausible_non_null_ptr(void *p)
{
  return p > (void *)0x1000;
}
#  endif

static void validate_cdt(CDT_state *cdt,
                         bool check_all_tris,
                         bool check_delaunay,
                         bool check_visibility)
{
  LinkNode *ln;
  int totedges, totfaces, totverts;
  CDTEdge *e;
  SymEdge *se, *sesym, *s;
  CDTVert *v, *v1, *v2, *v3;
  CDTFace *f;
  int i, limit;
  bool isborder;

  if (cdt->output_prepared) {
    return;
  }
  if (cdt->edges == NULL || cdt->edges->next == NULL) {
    return;
  }

  BLI_assert(cdt != NULL);
  totedges = 0;
  for (ln = cdt->edges; ln; ln = ln->next) {
    e = (CDTEdge *)ln->link;
    se = &e->symedges[0];
    sesym = &e->symedges[1];
    if (is_deleted_edge(e)) {
      BLI_assert(se->rot == NULL && sesym->next == NULL && sesym->rot == NULL);
      continue;
    }
    totedges++;
    isborder = is_border_edge(e, cdt);
    BLI_assert(se->vert != sesym->vert);
    BLI_assert(se->edge == sesym->edge && se->edge == e);
    BLI_assert(sym(se) == sesym && sym(sesym) == se);
    for (i = 0; i < 2; i++) {
      se = &e->symedges[i];
      v = se->vert;
      f = se->face;
      BLI_assert(plausible_non_null_ptr(v));
      if (f != NULL) {
        BLI_assert(plausible_non_null_ptr(f));
      }
      BLI_assert(plausible_non_null_ptr(se->next));
      BLI_assert(plausible_non_null_ptr(se->rot));
      if (check_all_tris && se->face != cdt->outer_face) {
        limit = 3;
      }
      else {
        limit = 10000;
      }
      BLI_assert(reachable(se->next, se, limit));
      if (limit == 3) {
        v1 = se->vert;
        v2 = se->next->vert;
        v3 = se->next->next->vert;
        /* The triangle should be positively oriented, but because
         * the insertion of intersection vertices doesn't use exact
         * arithmetic, this may not be true, so allow a little slop. */
        BLI_assert(orient2d(v1->co, v2->co, v3->co) >= -FLT_EPSILON);
        BLI_assert(orient2d(v2->co, v3->co, v1->co) >= -FLT_EPSILON);
        BLI_assert(orient2d(v3->co, v1->co, v2->co) >= -FLT_EPSILON);
      }
      UNUSED_VARS_NDEBUG(limit);
      BLI_assert(se->next->next != se);
      s = se;
      do {
        BLI_assert(prev(s)->next == s);
        BLI_assert(s->rot == sym(prev(s)));
        s = s->next;
      } while (s != se);
    }
    if (check_visibility) {
      BLI_assert(isborder || is_visible(se->vert, se->next->vert, false, cdt));
    }
    if (!isborder && check_delaunay) {
      BLI_assert(is_delaunay_edge(se));
    }
  }
  totverts = 0;
  for (i = 0; i < cdt->vert_array_len; i++) {
    v = cdt->vert_array[i];
    BLI_assert(plausible_non_null_ptr(v));
    if (v->merge_to_index != -1) {
      BLI_assert(v->merge_to_index >= 0 && v->merge_to_index < cdt->vert_array_len);
      continue;
    }
    totverts++;
    BLI_assert(cdt->vert_array_len <= 1 || v->symedge->vert == v);
  }
  totfaces = 0;
  for (ln = cdt->faces; ln; ln = ln->next) {
    f = (CDTFace *)ln->link;
    BLI_assert(plausible_non_null_ptr(f));
    if (f->deleted) {
      continue;
    }
    totfaces++;
    if (f == cdt->outer_face) {
      continue;
    }
  }
  /* Euler's formula for planar graphs. */
  if (check_all_tris && totfaces > 1) {
    BLI_assert(totverts - totedges + totfaces == 2);
  }
}
#endif

/* Jonathan Shewchuk's adaptive predicates, trimmed to those needed here.
 * Permission obtained by private communication from Jonathan to include this code in Blender.
 */

/*
 *  Routines for Arbitrary Precision Floating-point Arithmetic
 *  and Fast Robust Geometric Predicates
 *  (predicates.c)
 *
 *  May 18, 1996
 *
 *  Placed in the public domain by
 *  Jonathan Richard Shewchuk
 *  School of Computer Science
 *  Carnegie Mellon University
 *  5000 Forbes Avenue
 *  Pittsburgh, Pennsylvania  15213-3891
 *  jrs@cs.cmu.edu
 *
 *  This file contains C implementation of algorithms for exact addition
 *    and multiplication of floating-point numbers, and predicates for
 *    robustly performing the orientation and incircle tests used in
 *    computational geometry.  The algorithms and underlying theory are
 *    described in Jonathan Richard Shewchuk.  "Adaptive Precision Floating-
 *    Point Arithmetic and Fast Robust Geometric Predicates."  Technical
 *    Report CMU-CS-96-140, School of Computer Science, Carnegie Mellon
 *    University, Pittsburgh, Pennsylvania, May 1996.  (Submitted to
 *    Discrete & Computational Geometry.)
 *
 *  This file, the paper listed above, and other information are available
 *    from the Web page http://www.cs.cmu.edu/~quake/robust.html .
 *
 *  Using this code:
 *
 *  First, read the short or long version of the paper (from the Web page
 *    above).
 *
 *  Be sure to call exactinit() once, before calling any of the arithmetic
 *    functions or geometric predicates.  Also be sure to turn on the
 *    optimizer when compiling this file.
 *
 * On some machines, the exact arithmetic routines might be defeated by the
 *   use of internal extended precision floating-point registers.  Sometimes
 *   this problem can be fixed by defining certain values to be volatile,
 *   thus forcing them to be stored to memory and rounded off.  This isn't
 *   a great solution, though, as it slows the arithmetic down.
 *
 * To try this out, write "#define INEXACT volatile" below.  Normally,
 *   however, INEXACT should be defined to be nothing.  ("#define INEXACT".)
 */

#define INEXACT /* Nothing */
/* #define INEXACT volatile */

/* Which of the following two methods of finding the absolute values is
 *   fastest is compiler-dependent.  A few compilers can inline and optimize
 *   the fabs() call; but most will incur the overhead of a function call,
 *   which is disastrously slow.  A faster way on IEEE machines might be to
 *   mask the appropriate bit, but that's difficult to do in C.
 */

#define Absolute(a) ((a) >= 0.0 ? (a) : -(a))
/* #define Absolute(a)  fabs(a) */

/* Many of the operations are broken up into two pieces, a main part that
 *   performs an approximate operation, and a "tail" that computes the
 *   roundoff error of that operation.
 *
 * The operations Fast_Two_Sum(), Fast_Two_Diff(), Two_Sum(), Two_Diff(),
 *   Split(), and Two_Product() are all implemented as described in the
 *   reference.  Each of these macros requires certain variables to be
 *   defined in the calling routine.  The variables `bvirt', `c', `abig',
 *   `_i', `_j', `_k', `_l', `_m', and `_n' are declared `INEXACT' because
 *   they store the result of an operation that may incur roundoff error.
 *   The input parameter `x' (or the highest numbered `x_' parameter) must
 *   also be declared `INEXACT'.
 */

#define Fast_Two_Sum_Tail(a, b, x, y) \
  bvirt = x - a; \
  y = b - bvirt

#define Fast_Two_Sum(a, b, x, y) \
  x = (double)(a + b); \
  Fast_Two_Sum_Tail(a, b, x, y)

#define Fast_Two_Diff_Tail(a, b, x, y) \
  bvirt = a - x; \
  y = bvirt - b

#define Fast_Two_Diff(a, b, x, y) \
  x = (double)(a - b); \
  Fast_Two_Diff_Tail(a, b, x, y)

#define Two_Sum_Tail(a, b, x, y) \
  bvirt = (double)(x - a); \
  avirt = x - bvirt; \
  bround = b - bvirt; \
  around = a - avirt; \
  y = around + bround

#define Two_Sum(a, b, x, y) \
  x = (double)(a + b); \
  Two_Sum_Tail(a, b, x, y)

#define Two_Diff_Tail(a, b, x, y) \
  bvirt = (double)(a - x); \
  avirt = x + bvirt; \
  bround = bvirt - b; \
  around = a - avirt; \
  y = around + bround

#define Two_Diff(a, b, x, y) \
  x = (double)(a - b); \
  Two_Diff_Tail(a, b, x, y)

#define Split(a, ahi, alo) \
  c = (double)(splitter * a); \
  abig = (double)(c - a); \
  ahi = c - abig; \
  alo = a - ahi

#define Two_Product_Tail(a, b, x, y) \
  Split(a, ahi, alo); \
  Split(b, bhi, blo); \
  err1 = x - (ahi * bhi); \
  err2 = err1 - (alo * bhi); \
  err3 = err2 - (ahi * blo); \
  y = (alo * blo) - err3

#define Two_Product(a, b, x, y) \
  x = (double)(a * b); \
  Two_Product_Tail(a, b, x, y)

#define Two_Product_Presplit(a, b, bhi, blo, x, y) \
  x = (double)(a * b); \
  Split(a, ahi, alo); \
  err1 = x - (ahi * bhi); \
  err2 = err1 - (alo * bhi); \
  err3 = err2 - (ahi * blo); \
  y = (alo * blo) - err3

#define Square_Tail(a, x, y) \
  Split(a, ahi, alo); \
  err1 = x - (ahi * ahi); \
  err3 = err1 - ((ahi + ahi) * alo); \
  y = (alo * alo) - err3

#define Square(a, x, y) \
  x = (double)(a * a); \
  Square_Tail(a, x, y)

#define Two_One_Sum(a1, a0, b, x2, x1, x0) \
  Two_Sum(a0, b, _i, x0); \
  Two_Sum(a1, _i, x2, x1)

#define Two_One_Diff(a1, a0, b, x2, x1, x0) \
  Two_Diff(a0, b, _i, x0); \
  Two_Sum(a1, _i, x2, x1)

#define Two_Two_Sum(a1, a0, b1, b0, x3, x2, x1, x0) \
  Two_One_Sum(a1, a0, b0, _j, _0, x0); \
  Two_One_Sum(_j, _0, b1, x3, x2, x1)

#define Two_Two_Diff(a1, a0, b1, b0, x3, x2, x1, x0) \
  Two_One_Diff(a1, a0, b0, _j, _0, x0); \
  Two_One_Diff(_j, _0, b1, x3, x2, x1)

static double splitter;  /* = 2^ceiling(p / 2) + 1.  Used to split floats in half. */
static double m_epsilon; /* = 2^(-p).  Used to estimate roundoff errors. */
/* A set of coefficients used to calculate maximum roundoff errors. */
static double resulterrbound;
static double ccwerrboundA, ccwerrboundB, ccwerrboundC;
static double o3derrboundA, o3derrboundB, o3derrboundC;
static double iccerrboundA, iccerrboundB, iccerrboundC;
static double isperrboundA, isperrboundB, isperrboundC;

/*  exactinit()   Initialize the variables used for exact arithmetic.
 *
 *  `epsilon' is the largest power of two such that 1.0 + epsilon = 1.0 in
 *  floating-point arithmetic.  `epsilon' bounds the relative roundoff
 *  error.  It is used for floating-point error analysis.
 *
 *  `splitter' is used to split floating-point numbers into two
 *  half-length significands for exact multiplication.
 *
 *  I imagine that a highly optimizing compiler might be too smart for its
 *  own good, and somehow cause this routine to fail, if it pretends that
 *  floating-point arithmetic is too much like real arithmetic.
 *
 *  Don't change this routine unless you fully understand it.
 */

static void exactinit(void)
{
  double half;
  double check, lastcheck;
  int every_other;

  every_other = 1;
  half = 0.5;
  m_epsilon = 1.0;
  splitter = 1.0;
  check = 1.0;
  /* Repeatedly divide `epsilon' by two until it is too small to add to
   *   one without causing roundoff.  (Also check if the sum is equal to
   *   the previous sum, for machines that round up instead of using exact
   *   rounding.  Not that this library will work on such machines anyway.
   */
  do {
    lastcheck = check;
    m_epsilon *= half;
    if (every_other) {
      splitter *= 2.0;
    }
    every_other = !every_other;
    check = 1.0 + m_epsilon;
  } while ((check != 1.0) && (check != lastcheck));
  splitter += 1.0;

  /* Error bounds for orientation and incircle tests. */
  resulterrbound = (3.0 + 8.0 * m_epsilon) * m_epsilon;
  ccwerrboundA = (3.0 + 16.0 * m_epsilon) * m_epsilon;
  ccwerrboundB = (2.0 + 12.0 * m_epsilon) * m_epsilon;
  ccwerrboundC = (9.0 + 64.0 * m_epsilon) * m_epsilon * m_epsilon;
  o3derrboundA = (7.0 + 56.0 * m_epsilon) * m_epsilon;
  o3derrboundB = (3.0 + 28.0 * m_epsilon) * m_epsilon;
  o3derrboundC = (26.0 + 288.0 * m_epsilon) * m_epsilon * m_epsilon;
  iccerrboundA = (10.0 + 96.0 * m_epsilon) * m_epsilon;
  iccerrboundB = (4.0 + 48.0 * m_epsilon) * m_epsilon;
  iccerrboundC = (44.0 + 576.0 * m_epsilon) * m_epsilon * m_epsilon;
  isperrboundA = (16.0 + 224.0 * m_epsilon) * m_epsilon;
  isperrboundB = (5.0 + 72.0 * m_epsilon) * m_epsilon;
  isperrboundC = (71.0 + 1408.0 * m_epsilon) * m_epsilon * m_epsilon;
}

/*  fast_expansion_sum_zeroelim()   Sum two expansions, eliminating zero
 *                                  components from the output expansion.
 *
 *  Sets h = e + f.  See the long version of my paper for details.
 *
 *  If round-to-even is used (as with IEEE 754), maintains the strongly
 *  non-overlapping property.  (That is, if e is strongly non-overlapping, h
 *  will be also.)  Does NOT maintain the non-overlapping or non-adjacent
 *  properties.
 */

static int fast_expansion_sum_zeroelim(
    int elen, const double *e, int flen, const double *f, double *h) /* h cannot be e or f. */
{
  double Q;
  INEXACT double Qnew;
  INEXACT double hh;
  INEXACT double bvirt;
  double avirt, bround, around;
  int eindex, findex, hindex;
  double enow, fnow;

  enow = e[0];
  fnow = f[0];
  eindex = findex = 0;
  if ((fnow > enow) == (fnow > -enow)) {
    Q = enow;
    enow = e[++eindex];
  }
  else {
    Q = fnow;
    fnow = f[++findex];
  }
  hindex = 0;
  if ((eindex < elen) && (findex < flen)) {
    if ((fnow > enow) == (fnow > -enow)) {
      Fast_Two_Sum(enow, Q, Qnew, hh);
      enow = e[++eindex];
    }
    else {
      Fast_Two_Sum(fnow, Q, Qnew, hh);
      fnow = f[++findex];
    }
    Q = Qnew;
    if (hh != 0.0) {
      h[hindex++] = hh;
    }
    while ((eindex < elen) && (findex < flen)) {
      if ((fnow > enow) == (fnow > -enow)) {
        Two_Sum(Q, enow, Qnew, hh);
        enow = e[++eindex];
      }
      else {
        Two_Sum(Q, fnow, Qnew, hh);
        fnow = f[++findex];
      }
      Q = Qnew;
      if (hh != 0.0) {
        h[hindex++] = hh;
      }
    }
  }
  while (eindex < elen) {
    Two_Sum(Q, enow, Qnew, hh);
    enow = e[++eindex];
    Q = Qnew;
    if (hh != 0.0) {
      h[hindex++] = hh;
    }
  }
  while (findex < flen) {
    Two_Sum(Q, fnow, Qnew, hh);
    fnow = f[++findex];
    Q = Qnew;
    if (hh != 0.0) {
      h[hindex++] = hh;
    }
  }
  if ((Q != 0.0) || (hindex == 0)) {
    h[hindex++] = Q;
  }
  return hindex;
}

/*  scale_expansion_zeroelim()   Multiply an expansion by a scalar,
 *                               eliminating zero components from the
 *                               output expansion.
 *
 *  Sets h = be.  See either version of my paper for details.
 *
 *  Maintains the nonoverlapping property.  If round-to-even is used (as
 *  with IEEE 754), maintains the strongly nonoverlapping and nonadjacent
 *  properties as well.  (That is, if e has one of these properties, so
 *  will h.)
 */

static int scale_expansion_zeroelim(int elen,
                                    const double *e,
                                    double b,
                                    double *h) /* e and h cannot be the same. */
{
  INEXACT double Q, sum;
  double hh;
  INEXACT double product1;
  double product0;
  int eindex, hindex;
  double enow;
  INEXACT double bvirt;
  double avirt, bround, around;
  INEXACT double c;
  INEXACT double abig;
  double ahi, alo, bhi, blo;
  double err1, err2, err3;

  Split(b, bhi, blo);
  Two_Product_Presplit(e[0], b, bhi, blo, Q, hh);
  hindex = 0;
  if (hh != 0) {
    h[hindex++] = hh;
  }
  for (eindex = 1; eindex < elen; eindex++) {
    enow = e[eindex];
    Two_Product_Presplit(enow, b, bhi, blo, product1, product0);
    Two_Sum(Q, product0, sum, hh);
    if (hh != 0) {
      h[hindex++] = hh;
    }
    Fast_Two_Sum(product1, sum, Q, hh);
    if (hh != 0) {
      h[hindex++] = hh;
    }
  }
  if ((Q != 0.0) || (hindex == 0)) {
    h[hindex++] = Q;
  }
  return hindex;
}

/*  estimate()   Produce a one-word estimate of an expansion's value.
 *
 *  See either version of my paper for details.
 */

static double estimate(int elen, const double *e)
{
  double Q;
  int eindex;

  Q = e[0];
  for (eindex = 1; eindex < elen; eindex++) {
    Q += e[eindex];
  }
  return Q;
}

/*  orient2d()   Adaptive exact 2D orientation test.  Robust.
 *
 *               Return a positive value if the points pa, pb, and pc occur
 *               in counterclockwise order; a negative value if they occur
 *               in clockwise order; and zero if they are collinear.  The
 *               result is also a rough approximation of twice the signed
 *               area of the triangle defined by the three points.
 *
 *  This uses exact arithmetic to ensure a correct answer.  The
 *  result returned is the determinant of a matrix.
 *  This determinant is computed adaptively, in the sense that exact
 *  arithmetic is used only to the degree it is needed to ensure that the
 *  returned value has the correct sign.  Hence, orient2d() is usually quite
 *  fast, but will run more slowly when the input points are collinear or
 *  nearly so.
 */

static double orient2dadapt(const double *pa, const double *pb, const double *pc, double detsum)
{
  INEXACT double acx, acy, bcx, bcy;
  double acxtail, acytail, bcxtail, bcytail;
  INEXACT double detleft, detright;
  double detlefttail, detrighttail;
  double det, errbound;
  double B[4], C1[8], C2[12], D[16];
  INEXACT double B3;
  int C1length, C2length, Dlength;
  double u[4];
  INEXACT double u3;
  INEXACT double s1, t1;
  double s0, t0;

  INEXACT double bvirt;
  double avirt, bround, around;
  INEXACT double c;
  INEXACT double abig;
  double ahi, alo, bhi, blo;
  double err1, err2, err3;
  INEXACT double _i, _j;
  double _0;

  acx = (double)(pa[0] - pc[0]);
  bcx = (double)(pb[0] - pc[0]);
  acy = (double)(pa[1] - pc[1]);
  bcy = (double)(pb[1] - pc[1]);

  Two_Product(acx, bcy, detleft, detlefttail);
  Two_Product(acy, bcx, detright, detrighttail);

  Two_Two_Diff(detleft, detlefttail, detright, detrighttail, B3, B[2], B[1], B[0]);
  B[3] = B3;

  det = estimate(4, B);
  errbound = ccwerrboundB * detsum;
  if ((det >= errbound) || (-det >= errbound)) {
    return det;
  }

  Two_Diff_Tail(pa[0], pc[0], acx, acxtail);
  Two_Diff_Tail(pb[0], pc[0], bcx, bcxtail);
  Two_Diff_Tail(pa[1], pc[1], acy, acytail);
  Two_Diff_Tail(pb[1], pc[1], bcy, bcytail);

  if ((acxtail == 0.0) && (acytail == 0.0) && (bcxtail == 0.0) && (bcytail == 0.0)) {
    return det;
  }

  errbound = ccwerrboundC * detsum + resulterrbound * Absolute(det);
  det += (acx * bcytail + bcy * acxtail) - (acy * bcxtail + bcx * acytail);
  if ((det >= errbound) || (-det >= errbound)) {
    return det;
  }

  Two_Product(acxtail, bcy, s1, s0);
  Two_Product(acytail, bcx, t1, t0);
  Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);
  u[3] = u3;
  C1length = fast_expansion_sum_zeroelim(4, B, 4, u, C1);

  Two_Product(acx, bcytail, s1, s0);
  Two_Product(acy, bcxtail, t1, t0);
  Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);
  u[3] = u3;
  C2length = fast_expansion_sum_zeroelim(C1length, C1, 4, u, C2);

  Two_Product(acxtail, bcytail, s1, s0);
  Two_Product(acytail, bcxtail, t1, t0);
  Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);
  u[3] = u3;
  Dlength = fast_expansion_sum_zeroelim(C2length, C2, 4, u, D);

  return (D[Dlength - 1]);
}

static double orient2d(const double *pa, const double *pb, const double *pc)
{
  double detleft, detright, det;
  double detsum, errbound;

  detleft = (pa[0] - pc[0]) * (pb[1] - pc[1]);
  detright = (pa[1] - pc[1]) * (pb[0] - pc[0]);
  det = detleft - detright;

  if (detleft > 0.0) {
    if (detright <= 0.0) {
      return det;
    }
    detsum = detleft + detright;
  }
  else if (detleft < 0.0) {
    if (detright >= 0.0) {
      return det;
    }
    detsum = -detleft - detright;
  }
  else {
    return det;
  }

  errbound = ccwerrboundA * detsum;
  if ((det >= errbound) || (-det >= errbound)) {
    return det;
  }

  return orient2dadapt(pa, pb, pc, detsum);
}

/*  incircle()   Adaptive exact 2D incircle test.  Robust.
 *
 *               Return a positive value if the point pd lies inside the
 *               circle passing through pa, pb, and pc; a negative value if
 *               it lies outside; and zero if the four points are cocircular.
 *               The points pa, pb, and pc must be in counterclockwise
 *               order, or the sign of the result will be reversed.
 *
 *  This uses exact arithmetic to ensure a correct answer.
 *  The result returned is the determinant of a matrix.
 *  This determinant is computed adaptively, in the sense that exact
 *  arithmetic is used only to the degree it is needed to ensure that the
 *  returned value has the correct sign.  Hence, incircle() is usually quite
 *  fast, but will run more slowly when the input points are cocircular or
 *  nearly so.
 */

static double incircleadapt(
    const double *pa, const double *pb, const double *pc, const double *pd, double permanent)
{
  INEXACT double adx, bdx, cdx, ady, bdy, cdy;
  double det, errbound;

  INEXACT double bdxcdy1, cdxbdy1, cdxady1, adxcdy1, adxbdy1, bdxady1;
  double bdxcdy0, cdxbdy0, cdxady0, adxcdy0, adxbdy0, bdxady0;
  double bc[4], ca[4], ab[4];
  INEXACT double bc3, ca3, ab3;
  double axbc[8], axxbc[16], aybc[8], ayybc[16], adet[32];
  int axbclen, axxbclen, aybclen, ayybclen, alen;
  double bxca[8], bxxca[16], byca[8], byyca[16], bdet[32];
  int bxcalen, bxxcalen, bycalen, byycalen, blen;
  double cxab[8], cxxab[16], cyab[8], cyyab[16], cdet[32];
  int cxablen, cxxablen, cyablen, cyyablen, clen;
  double abdet[64];
  int ablen;
  double fin1[1152], fin2[1152];
  double *finnow, *finother, *finswap;
  int finlength;

  double adxtail, bdxtail, cdxtail, adytail, bdytail, cdytail;
  INEXACT double adxadx1, adyady1, bdxbdx1, bdybdy1, cdxcdx1, cdycdy1;
  double adxadx0, adyady0, bdxbdx0, bdybdy0, cdxcdx0, cdycdy0;
  double aa[4], bb[4], cc[4];
  INEXACT double aa3, bb3, cc3;
  INEXACT double ti1, tj1;
  double ti0, tj0;
  double u[4], v[4];
  INEXACT double u3, v3;
  double temp8[8], temp16a[16], temp16b[16], temp16c[16];
  double temp32a[32], temp32b[32], temp48[48], temp64[64];
  int temp8len, temp16alen, temp16blen, temp16clen;
  int temp32alen, temp32blen, temp48len, temp64len;
  double axtbb[8], axtcc[8], aytbb[8], aytcc[8];
  int axtbblen, axtcclen, aytbblen, aytcclen;
  double bxtaa[8], bxtcc[8], bytaa[8], bytcc[8];
  int bxtaalen, bxtcclen, bytaalen, bytcclen;
  double cxtaa[8], cxtbb[8], cytaa[8], cytbb[8];
  int cxtaalen, cxtbblen, cytaalen, cytbblen;
  double axtbc[8], aytbc[8], bxtca[8], bytca[8], cxtab[8], cytab[8];
  int axtbclen, aytbclen, bxtcalen, bytcalen, cxtablen, cytablen;
  double axtbct[16], aytbct[16], bxtcat[16], bytcat[16], cxtabt[16], cytabt[16];
  int axtbctlen, aytbctlen, bxtcatlen, bytcatlen, cxtabtlen, cytabtlen;
  double axtbctt[8], aytbctt[8], bxtcatt[8];
  double bytcatt[8], cxtabtt[8], cytabtt[8];
  int axtbcttlen, aytbcttlen, bxtcattlen, bytcattlen, cxtabttlen, cytabttlen;
  double abt[8], bct[8], cat[8];
  int abtlen, bctlen, catlen;
  double abtt[4], bctt[4], catt[4];
  int abttlen, bcttlen, cattlen;
  INEXACT double abtt3, bctt3, catt3;
  double negate;

  INEXACT double bvirt;
  double avirt, bround, around;
  INEXACT double c;
  INEXACT double abig;
  double ahi, alo, bhi, blo;
  double err1, err2, err3;
  INEXACT double _i, _j;
  double _0;

  adx = (double)(pa[0] - pd[0]);
  bdx = (double)(pb[0] - pd[0]);
  cdx = (double)(pc[0] - pd[0]);
  ady = (double)(pa[1] - pd[1]);
  bdy = (double)(pb[1] - pd[1]);
  cdy = (double)(pc[1] - pd[1]);

  Two_Product(bdx, cdy, bdxcdy1, bdxcdy0);
  Two_Product(cdx, bdy, cdxbdy1, cdxbdy0);
  Two_Two_Diff(bdxcdy1, bdxcdy0, cdxbdy1, cdxbdy0, bc3, bc[2], bc[1], bc[0]);
  bc[3] = bc3;
  axbclen = scale_expansion_zeroelim(4, bc, adx, axbc);
  axxbclen = scale_expansion_zeroelim(axbclen, axbc, adx, axxbc);
  aybclen = scale_expansion_zeroelim(4, bc, ady, aybc);
  ayybclen = scale_expansion_zeroelim(aybclen, aybc, ady, ayybc);
  alen = fast_expansion_sum_zeroelim(axxbclen, axxbc, ayybclen, ayybc, adet);

  Two_Product(cdx, ady, cdxady1, cdxady0);
  Two_Product(adx, cdy, adxcdy1, adxcdy0);
  Two_Two_Diff(cdxady1, cdxady0, adxcdy1, adxcdy0, ca3, ca[2], ca[1], ca[0]);
  ca[3] = ca3;
  bxcalen = scale_expansion_zeroelim(4, ca, bdx, bxca);
  bxxcalen = scale_expansion_zeroelim(bxcalen, bxca, bdx, bxxca);
  bycalen = scale_expansion_zeroelim(4, ca, bdy, byca);
  byycalen = scale_expansion_zeroelim(bycalen, byca, bdy, byyca);
  blen = fast_expansion_sum_zeroelim(bxxcalen, bxxca, byycalen, byyca, bdet);

  Two_Product(adx, bdy, adxbdy1, adxbdy0);
  Two_Product(bdx, ady, bdxady1, bdxady0);
  Two_Two_Diff(adxbdy1, adxbdy0, bdxady1, bdxady0, ab3, ab[2], ab[1], ab[0]);
  ab[3] = ab3;
  cxablen = scale_expansion_zeroelim(4, ab, cdx, cxab);
  cxxablen = scale_expansion_zeroelim(cxablen, cxab, cdx, cxxab);
  cyablen = scale_expansion_zeroelim(4, ab, cdy, cyab);
  cyyablen = scale_expansion_zeroelim(cyablen, cyab, cdy, cyyab);
  clen = fast_expansion_sum_zeroelim(cxxablen, cxxab, cyyablen, cyyab, cdet);

  ablen = fast_expansion_sum_zeroelim(alen, adet, blen, bdet, abdet);
  finlength = fast_expansion_sum_zeroelim(ablen, abdet, clen, cdet, fin1);

  det = estimate(finlength, fin1);
  errbound = iccerrboundB * permanent;
  if ((det >= errbound) || (-det >= errbound)) {
    return det;
  }

  Two_Diff_Tail(pa[0], pd[0], adx, adxtail);
  Two_Diff_Tail(pa[1], pd[1], ady, adytail);
  Two_Diff_Tail(pb[0], pd[0], bdx, bdxtail);
  Two_Diff_Tail(pb[1], pd[1], bdy, bdytail);
  Two_Diff_Tail(pc[0], pd[0], cdx, cdxtail);
  Two_Diff_Tail(pc[1], pd[1], cdy, cdytail);
  if ((adxtail == 0.0) && (bdxtail == 0.0) && (cdxtail == 0.0) && (adytail == 0.0) &&
      (bdytail == 0.0) && (cdytail == 0.0)) {
    return det;
  }

  errbound = iccerrboundC * permanent + resulterrbound * Absolute(det);
  det += ((adx * adx + ady * ady) *
              ((bdx * cdytail + cdy * bdxtail) - (bdy * cdxtail + cdx * bdytail)) +
          2.0 * (adx * adxtail + ady * adytail) * (bdx * cdy - bdy * cdx)) +
         ((bdx * bdx + bdy * bdy) *
              ((cdx * adytail + ady * cdxtail) - (cdy * adxtail + adx * cdytail)) +
          2.0 * (bdx * bdxtail + bdy * bdytail) * (cdx * ady - cdy * adx)) +
         ((cdx * cdx + cdy * cdy) *
              ((adx * bdytail + bdy * adxtail) - (ady * bdxtail + bdx * adytail)) +
          2.0 * (cdx * cdxtail + cdy * cdytail) * (adx * bdy - ady * bdx));
  if ((det >= errbound) || (-det >= errbound)) {
    return det;
  }

  finnow = fin1;
  finother = fin2;

  if ((bdxtail != 0.0) || (bdytail != 0.0) || (cdxtail != 0.0) || (cdytail != 0.0)) {
    Square(adx, adxadx1, adxadx0);
    Square(ady, adyady1, adyady0);
    Two_Two_Sum(adxadx1, adxadx0, adyady1, adyady0, aa3, aa[2], aa[1], aa[0]);
    aa[3] = aa3;
  }
  if ((cdxtail != 0.0) || (cdytail != 0.0) || (adxtail != 0.0) || (adytail != 0.0)) {
    Square(bdx, bdxbdx1, bdxbdx0);
    Square(bdy, bdybdy1, bdybdy0);
    Two_Two_Sum(bdxbdx1, bdxbdx0, bdybdy1, bdybdy0, bb3, bb[2], bb[1], bb[0]);
    bb[3] = bb3;
  }
  if ((adxtail != 0.0) || (adytail != 0.0) || (bdxtail != 0.0) || (bdytail != 0.0)) {
    Square(cdx, cdxcdx1, cdxcdx0);
    Square(cdy, cdycdy1, cdycdy0);
    Two_Two_Sum(cdxcdx1, cdxcdx0, cdycdy1, cdycdy0, cc3, cc[2], cc[1], cc[0]);
    cc[3] = cc3;
  }

  if (adxtail != 0.0) {
    axtbclen = scale_expansion_zeroelim(4, bc, adxtail, axtbc);
    temp16alen = scale_expansion_zeroelim(axtbclen, axtbc, 2.0 * adx, temp16a);

    axtcclen = scale_expansion_zeroelim(4, cc, adxtail, axtcc);
    temp16blen = scale_expansion_zeroelim(axtcclen, axtcc, bdy, temp16b);

    axtbblen = scale_expansion_zeroelim(4, bb, adxtail, axtbb);
    temp16clen = scale_expansion_zeroelim(axtbblen, axtbb, -cdy, temp16c);

    temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a, temp16blen, temp16b, temp32a);
    temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c, temp32alen, temp32a, temp48);
    finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len, temp48, finother);
    finswap = finnow;
    finnow = finother;
    finother = finswap;
  }
  if (adytail != 0.0) {
    aytbclen = scale_expansion_zeroelim(4, bc, adytail, aytbc);
    temp16alen = scale_expansion_zeroelim(aytbclen, aytbc, 2.0 * ady, temp16a);

    aytbblen = scale_expansion_zeroelim(4, bb, adytail, aytbb);
    temp16blen = scale_expansion_zeroelim(aytbblen, aytbb, cdx, temp16b);

    aytcclen = scale_expansion_zeroelim(4, cc, adytail, aytcc);
    temp16clen = scale_expansion_zeroelim(aytcclen, aytcc, -bdx, temp16c);

    temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a, temp16blen, temp16b, temp32a);
    temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c, temp32alen, temp32a, temp48);
    finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len, temp48, finother);
    finswap = finnow;
    finnow = finother;
    finother = finswap;
  }
  if (bdxtail != 0.0) {
    bxtcalen = scale_expansion_zeroelim(4, ca, bdxtail, bxtca);
    temp16alen = scale_expansion_zeroelim(bxtcalen, bxtca, 2.0 * bdx, temp16a);

    bxtaalen = scale_expansion_zeroelim(4, aa, bdxtail, bxtaa);
    temp16blen = scale_expansion_zeroelim(bxtaalen, bxtaa, cdy, temp16b);

    bxtcclen = scale_expansion_zeroelim(4, cc, bdxtail, bxtcc);
    temp16clen = scale_expansion_zeroelim(bxtcclen, bxtcc, -ady, temp16c);

    temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a, temp16blen, temp16b, temp32a);
    temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c, temp32alen, temp32a, temp48);
    finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len, temp48, finother);
    finswap = finnow;
    finnow = finother;
    finother = finswap;
  }
  if (bdytail != 0.0) {
    bytcalen = scale_expansion_zeroelim(4, ca, bdytail, bytca);
    temp16alen = scale_expansion_zeroelim(bytcalen, bytca, 2.0 * bdy, temp16a);

    bytcclen = scale_expansion_zeroelim(4, cc, bdytail, bytcc);
    temp16blen = scale_expansion_zeroelim(bytcclen, bytcc, adx, temp16b);

    bytaalen = scale_expansion_zeroelim(4, aa, bdytail, bytaa);
    temp16clen = scale_expansion_zeroelim(bytaalen, bytaa, -cdx, temp16c);

    temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a, temp16blen, temp16b, temp32a);
    temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c, temp32alen, temp32a, temp48);
    finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len, temp48, finother);
    finswap = finnow;
    finnow = finother;
    finother = finswap;
  }
  if (cdxtail != 0.0) {
    cxtablen = scale_expansion_zeroelim(4, ab, cdxtail, cxtab);
    temp16alen = scale_expansion_zeroelim(cxtablen, cxtab, 2.0 * cdx, temp16a);

    cxtbblen = scale_expansion_zeroelim(4, bb, cdxtail, cxtbb);
    temp16blen = scale_expansion_zeroelim(cxtbblen, cxtbb, ady, temp16b);

    cxtaalen = scale_expansion_zeroelim(4, aa, cdxtail, cxtaa);
    temp16clen = scale_expansion_zeroelim(cxtaalen, cxtaa, -bdy, temp16c);

    temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a, temp16blen, temp16b, temp32a);
    temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c, temp32alen, temp32a, temp48);
    finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len, temp48, finother);
    finswap = finnow;
    finnow = finother;
    finother = finswap;
  }
  if (cdytail != 0.0) {
    cytablen = scale_expansion_zeroelim(4, ab, cdytail, cytab);
    temp16alen = scale_expansion_zeroelim(cytablen, cytab, 2.0 * cdy, temp16a);

    cytaalen = scale_expansion_zeroelim(4, aa, cdytail, cytaa);
    temp16blen = scale_expansion_zeroelim(cytaalen, cytaa, bdx, temp16b);

    cytbblen = scale_expansion_zeroelim(4, bb, cdytail, cytbb);
    temp16clen = scale_expansion_zeroelim(cytbblen, cytbb, -adx, temp16c);

    temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a, temp16blen, temp16b, temp32a);
    temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c, temp32alen, temp32a, temp48);
    finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len, temp48, finother);
    finswap = finnow;
    finnow = finother;
    finother = finswap;
  }

  if ((adxtail != 0.0) || (adytail != 0.0)) {
    if ((bdxtail != 0.0) || (bdytail != 0.0) || (cdxtail != 0.0) || (cdytail != 0.0)) {
      Two_Product(bdxtail, cdy, ti1, ti0);
      Two_Product(bdx, cdytail, tj1, tj0);
      Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);
      u[3] = u3;
      negate = -bdy;
      Two_Product(cdxtail, negate, ti1, ti0);
      negate = -bdytail;
      Two_Product(cdx, negate, tj1, tj0);
      Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
      v[3] = v3;
      bctlen = fast_expansion_sum_zeroelim(4, u, 4, v, bct);

      Two_Product(bdxtail, cdytail, ti1, ti0);
      Two_Product(cdxtail, bdytail, tj1, tj0);
      Two_Two_Diff(ti1, ti0, tj1, tj0, bctt3, bctt[2], bctt[1], bctt[0]);
      bctt[3] = bctt3;
      bcttlen = 4;
    }
    else {
      bct[0] = 0.0;
      bctlen = 1;
      bctt[0] = 0.0;
      bcttlen = 1;
    }

    if (adxtail != 0.0) {
      temp16alen = scale_expansion_zeroelim(axtbclen, axtbc, adxtail, temp16a);
      axtbctlen = scale_expansion_zeroelim(bctlen, bct, adxtail, axtbct);
      temp32alen = scale_expansion_zeroelim(axtbctlen, axtbct, 2.0 * adx, temp32a);
      temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a, temp32alen, temp32a, temp48);
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len, temp48, finother);
      finswap = finnow;
      finnow = finother;
      finother = finswap;
      if (bdytail != 0.0) {
        temp8len = scale_expansion_zeroelim(4, cc, adxtail, temp8);
        temp16alen = scale_expansion_zeroelim(temp8len, temp8, bdytail, temp16a);
        finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen, temp16a, finother);
        finswap = finnow;
        finnow = finother;
        finother = finswap;
      }
      if (cdytail != 0.0) {
        temp8len = scale_expansion_zeroelim(4, bb, -adxtail, temp8);
        temp16alen = scale_expansion_zeroelim(temp8len, temp8, cdytail, temp16a);
        finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen, temp16a, finother);
        finswap = finnow;
        finnow = finother;
        finother = finswap;
      }

      temp32alen = scale_expansion_zeroelim(axtbctlen, axtbct, adxtail, temp32a);
      axtbcttlen = scale_expansion_zeroelim(bcttlen, bctt, adxtail, axtbctt);
      temp16alen = scale_expansion_zeroelim(axtbcttlen, axtbctt, 2.0 * adx, temp16a);
      temp16blen = scale_expansion_zeroelim(axtbcttlen, axtbctt, adxtail, temp16b);
      temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a, temp16blen, temp16b, temp32b);
      temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a, temp32blen, temp32b, temp64);
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len, temp64, finother);
      finswap = finnow;
      finnow = finother;
      finother = finswap;
    }
    if (adytail != 0.0) {
      temp16alen = scale_expansion_zeroelim(aytbclen, aytbc, adytail, temp16a);
      aytbctlen = scale_expansion_zeroelim(bctlen, bct, adytail, aytbct);
      temp32alen = scale_expansion_zeroelim(aytbctlen, aytbct, 2.0 * ady, temp32a);
      temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a, temp32alen, temp32a, temp48);
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len, temp48, finother);
      finswap = finnow;
      finnow = finother;
      finother = finswap;

      temp32alen = scale_expansion_zeroelim(aytbctlen, aytbct, adytail, temp32a);
      aytbcttlen = scale_expansion_zeroelim(bcttlen, bctt, adytail, aytbctt);
      temp16alen = scale_expansion_zeroelim(aytbcttlen, aytbctt, 2.0 * ady, temp16a);
      temp16blen = scale_expansion_zeroelim(aytbcttlen, aytbctt, adytail, temp16b);
      temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a, temp16blen, temp16b, temp32b);
      temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a, temp32blen, temp32b, temp64);
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len, temp64, finother);
      finswap = finnow;
      finnow = finother;
      finother = finswap;
    }
  }
  if ((bdxtail != 0.0) || (bdytail != 0.0)) {
    if ((cdxtail != 0.0) || (cdytail != 0.0) || (adxtail != 0.0) || (adytail != 0.0)) {
      Two_Product(cdxtail, ady, ti1, ti0);
      Two_Product(cdx, adytail, tj1, tj0);
      Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);
      u[3] = u3;
      negate = -cdy;
      Two_Product(adxtail, negate, ti1, ti0);
      negate = -cdytail;
      Two_Product(adx, negate, tj1, tj0);
      Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
      v[3] = v3;
      catlen = fast_expansion_sum_zeroelim(4, u, 4, v, cat);

      Two_Product(cdxtail, adytail, ti1, ti0);
      Two_Product(adxtail, cdytail, tj1, tj0);
      Two_Two_Diff(ti1, ti0, tj1, tj0, catt3, catt[2], catt[1], catt[0]);
      catt[3] = catt3;
      cattlen = 4;
    }
    else {
      cat[0] = 0.0;
      catlen = 1;
      catt[0] = 0.0;
      cattlen = 1;
    }

    if (bdxtail != 0.0) {
      temp16alen = scale_expansion_zeroelim(bxtcalen, bxtca, bdxtail, temp16a);
      bxtcatlen = scale_expansion_zeroelim(catlen, cat, bdxtail, bxtcat);
      temp32alen = scale_expansion_zeroelim(bxtcatlen, bxtcat, 2.0 * bdx, temp32a);
      temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a, temp32alen, temp32a, temp48);
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len, temp48, finother);
      finswap = finnow;
      finnow = finother;
      finother = finswap;
      if (cdytail != 0.0) {
        temp8len = scale_expansion_zeroelim(4, aa, bdxtail, temp8);
        temp16alen = scale_expansion_zeroelim(temp8len, temp8, cdytail, temp16a);
        finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen, temp16a, finother);
        finswap = finnow;
        finnow = finother;
        finother = finswap;
      }
      if (adytail != 0.0) {
        temp8len = scale_expansion_zeroelim(4, cc, -bdxtail, temp8);
        temp16alen = scale_expansion_zeroelim(temp8len, temp8, adytail, temp16a);
        finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen, temp16a, finother);
        finswap = finnow;
        finnow = finother;
        finother = finswap;
      }

      temp32alen = scale_expansion_zeroelim(bxtcatlen, bxtcat, bdxtail, temp32a);
      bxtcattlen = scale_expansion_zeroelim(cattlen, catt, bdxtail, bxtcatt);
      temp16alen = scale_expansion_zeroelim(bxtcattlen, bxtcatt, 2.0 * bdx, temp16a);
      temp16blen = scale_expansion_zeroelim(bxtcattlen, bxtcatt, bdxtail, temp16b);
      temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a, temp16blen, temp16b, temp32b);
      temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a, temp32blen, temp32b, temp64);
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len, temp64, finother);
      finswap = finnow;
      finnow = finother;
      finother = finswap;
    }
    if (bdytail != 0.0) {
      temp16alen = scale_expansion_zeroelim(bytcalen, bytca, bdytail, temp16a);
      bytcatlen = scale_expansion_zeroelim(catlen, cat, bdytail, bytcat);
      temp32alen = scale_expansion_zeroelim(bytcatlen, bytcat, 2.0 * bdy, temp32a);
      temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a, temp32alen, temp32a, temp48);
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len, temp48, finother);
      finswap = finnow;
      finnow = finother;
      finother = finswap;

      temp32alen = scale_expansion_zeroelim(bytcatlen, bytcat, bdytail, temp32a);
      bytcattlen = scale_expansion_zeroelim(cattlen, catt, bdytail, bytcatt);
      temp16alen = scale_expansion_zeroelim(bytcattlen, bytcatt, 2.0 * bdy, temp16a);
      temp16blen = scale_expansion_zeroelim(bytcattlen, bytcatt, bdytail, temp16b);
      temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a, temp16blen, temp16b, temp32b);
      temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a, temp32blen, temp32b, temp64);
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len, temp64, finother);
      finswap = finnow;
      finnow = finother;
      finother = finswap;
    }
  }
  if ((cdxtail != 0.0) || (cdytail != 0.0)) {
    if ((adxtail != 0.0) || (adytail != 0.0) || (bdxtail != 0.0) || (bdytail != 0.0)) {
      Two_Product(adxtail, bdy, ti1, ti0);
      Two_Product(adx, bdytail, tj1, tj0);
      Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);
      u[3] = u3;
      negate = -ady;
      Two_Product(bdxtail, negate, ti1, ti0);
      negate = -adytail;
      Two_Product(bdx, negate, tj1, tj0);
      Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
      v[3] = v3;
      abtlen = fast_expansion_sum_zeroelim(4, u, 4, v, abt);

      Two_Product(adxtail, bdytail, ti1, ti0);
      Two_Product(bdxtail, adytail, tj1, tj0);
      Two_Two_Diff(ti1, ti0, tj1, tj0, abtt3, abtt[2], abtt[1], abtt[0]);
      abtt[3] = abtt3;
      abttlen = 4;
    }
    else {
      abt[0] = 0.0;
      abtlen = 1;
      abtt[0] = 0.0;
      abttlen = 1;
    }

    if (cdxtail != 0.0) {
      temp16alen = scale_expansion_zeroelim(cxtablen, cxtab, cdxtail, temp16a);
      cxtabtlen = scale_expansion_zeroelim(abtlen, abt, cdxtail, cxtabt);
      temp32alen = scale_expansion_zeroelim(cxtabtlen, cxtabt, 2.0 * cdx, temp32a);
      temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a, temp32alen, temp32a, temp48);
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len, temp48, finother);
      finswap = finnow;
      finnow = finother;
      finother = finswap;
      if (adytail != 0.0) {
        temp8len = scale_expansion_zeroelim(4, bb, cdxtail, temp8);
        temp16alen = scale_expansion_zeroelim(temp8len, temp8, adytail, temp16a);
        finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen, temp16a, finother);
        finswap = finnow;
        finnow = finother;
        finother = finswap;
      }
      if (bdytail != 0.0) {
        temp8len = scale_expansion_zeroelim(4, aa, -cdxtail, temp8);
        temp16alen = scale_expansion_zeroelim(temp8len, temp8, bdytail, temp16a);
        finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen, temp16a, finother);
        finswap = finnow;
        finnow = finother;
        finother = finswap;
      }

      temp32alen = scale_expansion_zeroelim(cxtabtlen, cxtabt, cdxtail, temp32a);
      cxtabttlen = scale_expansion_zeroelim(abttlen, abtt, cdxtail, cxtabtt);
      temp16alen = scale_expansion_zeroelim(cxtabttlen, cxtabtt, 2.0 * cdx, temp16a);
      temp16blen = scale_expansion_zeroelim(cxtabttlen, cxtabtt, cdxtail, temp16b);
      temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a, temp16blen, temp16b, temp32b);
      temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a, temp32blen, temp32b, temp64);
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len, temp64, finother);
      finswap = finnow;
      finnow = finother;
      finother = finswap;
    }
    if (cdytail != 0.0) {
      temp16alen = scale_expansion_zeroelim(cytablen, cytab, cdytail, temp16a);
      cytabtlen = scale_expansion_zeroelim(abtlen, abt, cdytail, cytabt);
      temp32alen = scale_expansion_zeroelim(cytabtlen, cytabt, 2.0 * cdy, temp32a);
      temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a, temp32alen, temp32a, temp48);
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len, temp48, finother);
      finswap = finnow;
      finnow = finother;
      finother = finswap;

      temp32alen = scale_expansion_zeroelim(cytabtlen, cytabt, cdytail, temp32a);
      cytabttlen = scale_expansion_zeroelim(abttlen, abtt, cdytail, cytabtt);
      temp16alen = scale_expansion_zeroelim(cytabttlen, cytabtt, 2.0 * cdy, temp16a);
      temp16blen = scale_expansion_zeroelim(cytabttlen, cytabtt, cdytail, temp16b);
      temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a, temp16blen, temp16b, temp32b);
      temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a, temp32blen, temp32b, temp64);
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len, temp64, finother);
      finswap = finnow;
      finnow = finother;
      finother = finswap;
    }
  }

  return finnow[finlength - 1];
}

static double incircle(const double *pa, const double *pb, const double *pc, const double *pd)
{
  double adx, bdx, cdx, ady, bdy, cdy;
  double bdxcdy, cdxbdy, cdxady, adxcdy, adxbdy, bdxady;
  double alift, blift, clift;
  double det;
  double permanent, errbound;

  adx = pa[0] - pd[0];
  bdx = pb[0] - pd[0];
  cdx = pc[0] - pd[0];
  ady = pa[1] - pd[1];
  bdy = pb[1] - pd[1];
  cdy = pc[1] - pd[1];

  bdxcdy = bdx * cdy;
  cdxbdy = cdx * bdy;
  alift = adx * adx + ady * ady;

  cdxady = cdx * ady;
  adxcdy = adx * cdy;
  blift = bdx * bdx + bdy * bdy;

  adxbdy = adx * bdy;
  bdxady = bdx * ady;
  clift = cdx * cdx + cdy * cdy;

  det = alift * (bdxcdy - cdxbdy) + blift * (cdxady - adxcdy) + clift * (adxbdy - bdxady);

  permanent = (Absolute(bdxcdy) + Absolute(cdxbdy)) * alift +
              (Absolute(cdxady) + Absolute(adxcdy)) * blift +
              (Absolute(adxbdy) + Absolute(bdxady)) * clift;
  errbound = iccerrboundA * permanent;
  if ((det > errbound) || (-det > errbound)) {
    return det;
  }

  return incircleadapt(pa, pb, pc, pd, permanent);
}