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
use crate::event_queue::*;
use crate::geom::LineSegment;
use crate::math::*;
use crate::monotone::*;
use crate::path::polygon::Polygon;
use crate::path::traits::{Build, PathBuilder};
use crate::path::{
AttributeStore, EndpointId, FillRule, IdEvent, PathEvent, PathSlice, PositionStore, Winding,
};
use crate::{FillGeometryBuilder, Orientation, VertexId};
use crate::{
FillOptions, InternalError, Side, TessellationError, TessellationResult, VertexSource,
};
use std::cmp::Ordering;
use std::f32;
use std::mem;
use std::ops::Range;
use float_next_after::NextAfter;
#[cfg(debug_assertions)]
use std::env;
type SpanIdx = i32;
type ActiveEdgeIdx = usize;
// It's a bit odd but this consistently performs a bit better than f32::max, probably
// because the latter deals with NaN.
#[inline(always)]
fn fmax(a: f32, b: f32) -> f32 {
if a > b {
a
} else {
b
}
}
fn slope(v: Vector) -> f32 {
v.x / (v.y.max(std::f32::MIN))
}
#[cfg(debug_assertions)]
macro_rules! tess_log {
($obj:ident, $fmt:expr) => (
if $obj.log {
println!($fmt);
}
);
($obj:ident, $fmt:expr, $($arg:tt)*) => (
if $obj.log {
println!($fmt, $($arg)*);
}
);
}
#[cfg(not(debug_assertions))]
macro_rules! tess_log {
($obj:ident, $fmt:expr) => {};
($obj:ident, $fmt:expr, $($arg:tt)*) => {};
}
#[derive(Copy, Clone, Debug)]
struct WindingState {
span_index: SpanIdx,
number: i16,
is_in: bool,
}
impl WindingState {
fn new() -> Self {
// The span index starts at -1 so that entering the first span (of index 0) increments
// it to zero.
WindingState {
span_index: -1,
number: 0,
is_in: false,
}
}
fn update(&mut self, fill_rule: FillRule, edge_winding: i16) {
self.number += edge_winding;
self.is_in = fill_rule.is_in(self.number);
if self.is_in {
self.span_index += 1;
}
}
}
struct ActiveEdgeScan {
vertex_events: Vec<(SpanIdx, Side)>,
edges_to_split: Vec<ActiveEdgeIdx>,
spans_to_end: Vec<SpanIdx>,
merge_event: bool,
split_event: bool,
merge_split_event: bool,
above: Range<ActiveEdgeIdx>,
winding_before_point: WindingState,
}
impl ActiveEdgeScan {
fn new() -> Self {
ActiveEdgeScan {
vertex_events: Vec::new(),
edges_to_split: Vec::new(),
spans_to_end: Vec::new(),
merge_event: false,
split_event: false,
merge_split_event: false,
above: 0..0,
winding_before_point: WindingState::new(),
}
}
fn reset(&mut self) {
self.vertex_events.clear();
self.edges_to_split.clear();
self.spans_to_end.clear();
self.merge_event = false;
self.split_event = false;
self.merge_split_event = false;
self.above = 0..0;
self.winding_before_point = WindingState::new();
}
}
#[derive(Copy, Clone, Debug)]
struct ActiveEdge {
from: Point,
to: Point,
winding: i16,
is_merge: bool,
from_id: VertexId,
src_edge: TessEventId,
range_end: f32,
}
#[test]
fn active_edge_size() {
// We want to be careful about the size of the struct.
assert_eq!(std::mem::size_of::<ActiveEdge>(), 32);
}
impl ActiveEdge {
#[inline(always)]
fn min_x(&self) -> f32 {
self.from.x.min(self.to.x)
}
#[inline(always)]
fn max_x(&self) -> f32 {
fmax(self.from.x, self.to.x)
}
}
impl ActiveEdge {
fn solve_x_for_y(&self, y: f32) -> f32 {
// Because of float precision hazard, solve_x_for_y can
// return something slightly out of the min/max range which
// causes the ordering to be inconsistent with the way the
// scan phase uses the min/max range.
LineSegment {
from: self.from,
to: self.to,
}
.solve_x_for_y(y)
.max(self.min_x())
.min(self.max_x())
}
}
struct ActiveEdges {
edges: Vec<ActiveEdge>,
}
struct Span {
/// We store `MonotoneTesselator` behind a `Box` for performance purposes.
/// For more info, see [Issue #621](https://github.com/nical/lyon/pull/621).
tess: Option<Box<MonotoneTessellator>>,
}
impl Span {
fn tess(&mut self) -> &mut MonotoneTessellator {
// this should only ever be called on a "live" span.
match self.tess.as_mut() {
None => {
debug_assert!(false);
unreachable!();
}
Some(tess) => tess,
}
}
}
struct Spans {
spans: Vec<Span>,
/// We store `MonotoneTesselator` behind a `Box` for performance purposes.
/// For more info, see [Issue #621](https://github.com/nical/lyon/pull/621).
#[allow(clippy::vec_box)]
pool: Vec<Box<MonotoneTessellator>>,
}
impl Spans {
fn begin_span(&mut self, span_idx: SpanIdx, position: &Point, vertex: VertexId) {
let mut tess = self
.pool
.pop()
.unwrap_or_else(|| Box::new(MonotoneTessellator::new()));
tess.begin(*position, vertex);
self.spans
.insert(span_idx as usize, Span { tess: Some(tess) });
}
fn end_span(
&mut self,
span_idx: SpanIdx,
position: &Point,
id: VertexId,
output: &mut dyn FillGeometryBuilder,
) {
let idx = span_idx as usize;
let span = &mut self.spans[idx];
if let Some(mut tess) = span.tess.take() {
tess.end(*position, id);
tess.flush(output);
// Recycle the allocations for future use.
self.pool.push(tess);
} else {
debug_assert!(false);
unreachable!();
}
}
fn merge_spans(
&mut self,
left_span_idx: SpanIdx,
current_position: &Point,
current_vertex: VertexId,
merge_position: &Point,
merge_vertex: VertexId,
output: &mut dyn FillGeometryBuilder,
) {
// \...\ /.
// \...x.. <-- merge vertex
// \./... <-- active_edge
// x.... <-- current vertex
let right_span_idx = left_span_idx + 1;
self.spans[left_span_idx as usize].tess().vertex(
*merge_position,
merge_vertex,
Side::Right,
);
self.spans[right_span_idx as usize].tess().vertex(
*merge_position,
merge_vertex,
Side::Left,
);
self.end_span(left_span_idx, current_position, current_vertex, output);
}
fn cleanup_spans(&mut self) {
// Get rid of the spans that were marked for removal.
self.spans.retain(|span| span.tess.is_some());
}
}
#[derive(Copy, Clone, Debug)]
struct PendingEdge {
to: Point,
sort_key: f32,
// Index in events.edge_data
src_edge: TessEventId,
winding: i16,
range_end: f32,
}
/// A Context object that can tessellate fill operations for complex paths.
///
/// <svg version="1.1" viewBox="0 0 400 200" height="200" width="400">
/// <g transform="translate(0,-852.36216)">
/// <path style="fill:#aad400;stroke:none;" transform="translate(0,852.36216)" d="M 20 20 L 20 180 L 180.30273 180 L 180.30273 20 L 20 20 z M 100 55 L 145 145 L 55 145 L 100 55 z "/>
/// <path style="fill:#aad400;fill-rule:evenodd;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-" d="m 219.75767,872.36216 0,160.00004 160.30273,0 0,-160.00004 -160.30273,0 z m 80,35 45,90 -90,0 45,-90 z"/>
/// <path style="fill:none;stroke:#000000;stroke-linecap:round;stroke-linejoin:round;stroke-" d="m 220,1032.3622 35,-35.00004 125,35.00004 -35,-35.00004 35,-125 -80,35 -80,-35 35,125"/>
/// <circle r="5" cy="872.36218" cx="20" style="color:#000000;;fill:#ff6600;fill-;stroke:#000000;" />
/// <circle r="5" cx="180.10918" cy="872.61475" style="fill:#ff6600;stroke:#000000;"/>
/// <circle r="5" cy="1032.2189" cx="180.10918" style="fill:#ff6600;stroke:#000000;"/>
/// <circle r="5" cx="20.505075" cy="1032.4714" style="fill:#ff6600;stroke:#000000;"/>
/// <circle r="5" cy="907.21252" cx="99.802048" style="fill:#ff6600;stroke:#000000;"/>
/// <circle r="5" cx="55.102798" cy="997.36865" style="fill:#ff6600;stroke:#000000;"/>
/// <circle r="5" cy="997.62122" cx="145.25891" style="fill:#ff6600;stroke:#000000;"/>
/// </g>
/// </svg>
///
/// ## Overview
///
/// The most important structure is [`FillTessellator`](struct.FillTessellator.html).
/// It implements the path fill tessellation algorithm which is by far the most advanced
/// feature in all lyon crates.
///
/// The `FillTessellator` takes a description of the input path and
/// [`FillOptions`](struct.FillOptions.html) as input. The description of the path can be an
/// `PathEvent` iterator, or an iterator of `IdEvent` with an implementation of`PositionStore`
/// to retrieve positions form endpoint and control point ids, and optionally an `AttributeStore`
/// providing custom endpoint attributes that the tessellator can hand over to the geometry builder.
///
/// The output of the tessellator is produced by the
/// [`FillGeometryBuilder`](geometry_builder/trait.FillGeometryBuilder.html) (see the
/// [`geometry_builder` documentation](geometry_builder/index.html) for more details about
/// how tessellators produce their output geometry, and how to generate custom vertex layouts).
///
/// The [tessellator's wiki page](https://github.com/nical/lyon/wiki/Tessellator) is a good place
/// to learn more about how the tessellator's algorithm works. The source code also contains
/// inline documentation for the adventurous who want to delve into more details.
///
/// The tessellator does not handle `NaN` values in any of its inputs.
///
/// ## Associating custom attributes with vertices.
///
/// It is sometimes useful to be able to link vertices generated by the tessellator back
/// with the path's original data, for example to be able to add attributes that the tessellator
/// does not know about (vertex color, texture coordinates, etc.).
///
/// The fill tessellator has two mechanisms to help with these advanced use cases. One is
/// simple to use and one that, while more complicated to use, can cover advanced scenarios.
///
/// Before going delving into these mechanisms, it is important to understand that the
/// vertices generated by the tessellator don't always correspond to the vertices existing
/// in the original path.
/// - Self-intersections, for example, introduce a new vertex where two edges meet.
/// - When several vertices are at the same position, they are merged into a single vertex
/// from the point of view of the tessellator.
/// - The tessellator does not handle curves, and uses an approximation that introduces a
/// number of line segments and therefore endpoints between the original endpoints of any
/// quadratic or cubic bézier curve.
///
/// This complicates the task of adding extra data to vertices without loosing the association
/// during tessellation.
///
/// ### Vertex sources
///
/// This is the complicated, but most powerful mechanism. The tessellator keeps track of where
/// each vertex comes from in the original path, and provides access to this information via
/// an iterator of [`VertexSource`](enum.VertexSource.html) in `FillVertex::sources`.
///
/// It is most common for the vertex source iterator to yield a single `VertexSource::Endpoint`
/// source, which happens when the vertex directly corresponds to an endpoint of the original path.
/// More complicated cases can be expressed.
/// For example if a vertex is inserted at an intersection halfway in the edge AB and two thirds
/// of the way through edge BC, the source for this new vertex is `VertexSource::Edge { from: A, to: B, t: 0.5 }`
/// and `VertexSource::Edge { from: C, to: D, t: 0.666666 }` where A, B, C and D are endpoint IDs.
///
/// To use this feature, make sure to use `FillTessellator::tessellate_with_ids` instead of
/// `FillTessellator::tessellate`.
///
/// ### Interpolated float attributes
///
/// Having to iterate over potentially several sources for each vertex can be cumbersome, in addition
/// to having to deal with generating proper values for the attributes of vertices that were introduced
/// at intersections or along curves.
///
/// In many scenarios, vertex attributes are made of floating point numbers and the most reasonable
/// way to generate new attributes is to linearly interpolate these numbers between the endpoints
/// of the edges they originate from.
///
/// Custom endpoint attributes are represented as `&[f32]` slices accessible via
/// `FillVertex::interpolated_attributes`. All vertices, whether they originate from a single
/// endpoint or some more complex source, have exactly the same number of attributes.
/// Without having to know about the meaning of attributes, the tessellator can either
/// forward the slice of attributes from a provided `AttributeStore` when possible or
/// generate the values via linear interpolation.
///
/// To use this feature, make sure to use `FillTessellator::tessellate_path` or
/// `FillTessellator::tessellate_with_ids` instead of `FillTessellator::tessellate`.
///
/// Attributes are lazily computed when calling `FillVertex::interpolated_attributes`.
/// In other words they don't add overhead when not used, however it is best to avoid calling
/// interpolated_attributes several times per vertex.
///
/// # Examples
///
/// ```
/// # extern crate lyon_tessellation as tess;
/// # use tess::path::Path;
/// # use tess::path::builder::*;
/// # use tess::path::iterator::*;
/// # use tess::math::{Point, point};
/// # use tess::geometry_builder::{VertexBuffers, simple_builder};
/// # use tess::*;
/// # fn main() {
/// // Create a simple path.
/// let mut path_builder = Path::builder();
/// path_builder.begin(point(0.0, 0.0));
/// path_builder.line_to(point(1.0, 2.0));
/// path_builder.line_to(point(2.0, 0.0));
/// path_builder.line_to(point(1.0, 1.0));
/// path_builder.end(true);
/// let path = path_builder.build();
///
/// // Create the destination vertex and index buffers.
/// let mut buffers: VertexBuffers<Point, u16> = VertexBuffers::new();
///
/// {
/// let mut vertex_builder = simple_builder(&mut buffers);
///
/// // Create the tessellator.
/// let mut tessellator = FillTessellator::new();
///
/// // Compute the tessellation.
/// let result = tessellator.tessellate_path(
/// &path,
/// &FillOptions::default(),
/// &mut vertex_builder
/// );
/// assert!(result.is_ok());
/// }
///
/// println!("The generated vertices are: {:?}.", &buffers.vertices[..]);
/// println!("The generated indices are: {:?}.", &buffers.indices[..]);
///
/// # }
/// ```
///
/// ```
/// # extern crate lyon_tessellation as tess;
/// # use tess::path::Path;
/// # use tess::path::builder::*;
/// # use tess::path::iterator::*;
/// # use tess::math::{Point, point};
/// # use tess::geometry_builder::{VertexBuffers, simple_builder};
/// # use tess::*;
/// # fn main() {
/// // Create a path with three custom endpoint attributes.
/// let mut path_builder = Path::builder_with_attributes(3);
/// path_builder.begin(point(0.0, 0.0), &[0.0, 0.1, 0.5]);
/// path_builder.line_to(point(1.0, 2.0), &[1.0, 1.0, 0.1]);
/// path_builder.line_to(point(2.0, 0.0), &[1.0, 0.0, 0.8]);
/// path_builder.line_to(point(1.0, 1.0), &[0.1, 0.3, 0.5]);
/// path_builder.end(true);
/// let path = path_builder.build();
///
/// struct MyVertex {
/// x: f32, y: f32,
/// r: f32, g: f32, b: f32, a: f32,
/// }
/// // A custom vertex constructor, see the geometry_builder module.
/// struct Ctor;
/// impl FillVertexConstructor<MyVertex> for Ctor {
/// fn new_vertex(&mut self, mut vertex: FillVertex) -> MyVertex {
/// let position = vertex.position();
/// let attrs = vertex.interpolated_attributes();
/// MyVertex {
/// x: position.x,
/// y: position.y,
/// r: attrs[0],
/// g: attrs[1],
/// b: attrs[2],
/// a: 1.0,
/// }
/// }
/// }
///
/// // Create the destination vertex and index buffers.
/// let mut buffers: VertexBuffers<MyVertex, u16> = VertexBuffers::new();
///
/// {
/// // We use our custom vertex constructor here.
/// let mut vertex_builder = BuffersBuilder::new(&mut buffers, Ctor);
///
/// // Create the tessellator.
/// let mut tessellator = FillTessellator::new();
///
/// // Compute the tessellation. Here we use tessellate_with_ids
/// // which has a slightly more complicated interface. The provides
/// // the iterator as well as storage for positions and attributes at
/// // the same time.
/// let result = tessellator.tessellate_with_ids(
/// path.id_iter(), // Iterator over ids in the path
/// &path, // PositionStore
/// Some(&path), // AttributeStore
/// &FillOptions::default(),
/// &mut vertex_builder
/// );
/// assert!(result.is_ok());
/// }
///
/// # }
/// ```
pub struct FillTessellator {
current_position: Point,
current_vertex: VertexId,
current_event_id: TessEventId,
active: ActiveEdges,
edges_below: Vec<PendingEdge>,
fill_rule: FillRule,
orientation: Orientation,
tolerance: f32,
fill: Spans,
log: bool,
assume_no_intersection: bool,
attrib_buffer: Vec<f32>,
scan: ActiveEdgeScan,
events: EventQueue,
}
impl Default for FillTessellator {
fn default() -> Self {
Self::new()
}
}
impl FillTessellator {
/// Constructor.
pub fn new() -> Self {
#[cfg(debug_assertions)]
let log = env::var("LYON_FORCE_LOGGING").is_ok();
#[cfg(not(debug_assertions))]
let log = false;
FillTessellator {
current_position: point(f32::MIN, f32::MIN),
current_vertex: VertexId::INVALID,
current_event_id: INVALID_EVENT_ID,
active: ActiveEdges { edges: Vec::new() },
edges_below: Vec::new(),
fill_rule: FillRule::EvenOdd,
orientation: Orientation::Vertical,
tolerance: FillOptions::DEFAULT_TOLERANCE,
fill: Spans {
spans: Vec::new(),
pool: Vec::new(),
},
log,
assume_no_intersection: false,
attrib_buffer: Vec::new(),
scan: ActiveEdgeScan::new(),
events: EventQueue::new(),
}
}
/// Compute the tessellation from a path iterator.
pub fn tessellate(
&mut self,
path: impl IntoIterator<Item = PathEvent>,
options: &FillOptions,
output: &mut dyn FillGeometryBuilder,
) -> TessellationResult {
let event_queue = std::mem::replace(&mut self.events, EventQueue::new());
let mut queue_builder = event_queue.into_builder();
queue_builder.set_path(
options.tolerance,
options.sweep_orientation,
path.into_iter(),
);
self.events = queue_builder.build();
self.tessellate_impl(options, None, output)
}
/// Compute the tessellation using an iterator over endpoint and control
/// point ids, storage for the positions and, optionally, storage for
/// custom endpoint attributes.
pub fn tessellate_with_ids(
&mut self,
path: impl IntoIterator<Item = IdEvent>,
positions: &impl PositionStore,
custom_attributes: Option<&dyn AttributeStore>,
options: &FillOptions,
output: &mut dyn FillGeometryBuilder,
) -> TessellationResult {
let event_queue = std::mem::replace(&mut self.events, EventQueue::new());
let mut queue_builder = event_queue.into_builder();
queue_builder.set_path_with_ids(
options.tolerance,
options.sweep_orientation,
path.into_iter(),
positions,
);
self.events = queue_builder.build();
self.tessellate_impl(options, custom_attributes, output)
}
/// Compute the tessellation from a path slice.
///
/// The tessellator will internally only track vertex sources and interpolated
/// attributes if the path has interpolated attributes.
pub fn tessellate_path<'l>(
&'l mut self,
path: impl Into<PathSlice<'l>>,
options: &'l FillOptions,
builder: &'l mut dyn FillGeometryBuilder,
) -> TessellationResult {
let path = path.into();
if path.num_attributes() > 0 {
self.tessellate_with_ids(path.id_iter(), &path, Some(&path), options, builder)
} else {
self.tessellate(path.iter(), options, builder)
}
}
/// Tessellate a `Polygon`.
pub fn tessellate_polygon(
&mut self,
polygon: Polygon<Point>,
options: &FillOptions,
output: &mut dyn FillGeometryBuilder,
) -> TessellationResult {
self.tessellate(polygon.path_events(), options, output)
}
/// Tessellate an axis-aligned rectangle.
pub fn tessellate_rectangle(
&mut self,
rect: &Rect,
_options: &FillOptions,
output: &mut dyn FillGeometryBuilder,
) -> TessellationResult {
crate::basic_shapes::fill_rectangle(rect, output)
}
/// Tessellate a circle.
pub fn tessellate_circle(
&mut self,
center: Point,
radius: f32,
options: &FillOptions,
output: &mut dyn FillGeometryBuilder,
) -> TessellationResult {
crate::basic_shapes::fill_circle(center, radius, options, output)
}
/// Tessellate an ellipse.
pub fn tessellate_ellipse(
&mut self,
center: Point,
radii: Vector,
x_rotation: Angle,
winding: Winding,
options: &FillOptions,
output: &mut dyn FillGeometryBuilder,
) -> TessellationResult {
let options = options.clone().with_intersections(false);
let mut builder = self.builder(&options, output);
builder.add_ellipse(center, radii, x_rotation, winding);
builder.build()
}
/// Tessellate directly from a sequence of `PathBuilder` commands, without
/// creating an intermediate path data structure.
///
/// The returned builder implements the [`lyon_path::traits::PathBuilder`] trait,
/// is compatible with the all `PathBuilder` adapters.
/// It also has all requirements documented in `PathBuilder` (All sub-paths must be
/// wrapped in a `begin`/`end` pair).
///
/// # Example
///
/// ```rust
/// use lyon_tessellation::{FillTessellator, FillOptions};
/// use lyon_tessellation::geometry_builder::{simple_builder, VertexBuffers};
/// use lyon_tessellation::path::traits::*;
/// use lyon_tessellation::math::{Point, point};
///
/// let mut buffers: VertexBuffers<Point, u16> = VertexBuffers::new();
/// let mut vertex_builder = simple_builder(&mut buffers);
/// let mut tessellator = FillTessellator::new();
/// let options = FillOptions::default();
///
/// // Create a temporary builder (borrows the tessellator).
/// let mut builder = tessellator.builder(&options, &mut vertex_builder);
///
/// // Build the path directly in the tessellator, skipping an intermediate data
/// // structure.
/// builder.begin(point(0.0, 0.0));
/// builder.line_to(point(10.0, 0.0));
/// builder.line_to(point(10.0, 10.0));
/// builder.line_to(point(0.0, 10.0));
/// builder.end(true);
///
/// // Finish the tessellation and get the result.
/// let result = builder.build();
/// ```
///
/// [`lyon_path::traits::PathBuilder`]: https://docs.rs/lyon_path/*/lyon_path/traits/trait.PathBuilder.html
pub fn builder<'l>(
&'l mut self,
options: &'l FillOptions,
output: &'l mut dyn FillGeometryBuilder,
) -> FillBuilder<'l> {
FillBuilder::new(self, options, output)
}
fn tessellate_impl(
&mut self,
options: &FillOptions,
attrib_store: Option<&dyn AttributeStore>,
builder: &mut dyn FillGeometryBuilder,
) -> TessellationResult {
if options.tolerance.is_nan() || options.tolerance <= 0.0 {
return Err(TessellationError::UnsupportedParamater);
}
self.reset();
if let Some(store) = attrib_store {
self.attrib_buffer.resize(store.num_attributes(), 0.0);
} else {
self.attrib_buffer.clear();
}
self.fill_rule = options.fill_rule;
self.orientation = options.sweep_orientation;
self.tolerance = options.tolerance * 0.5;
self.assume_no_intersection = !options.handle_intersections;
builder.begin_geometry();
let mut scan = mem::replace(&mut self.scan, ActiveEdgeScan::new());
let result = self.tessellator_loop(attrib_store, &mut scan, builder);
mem::swap(&mut self.scan, &mut scan);
if let Err(e) = result {
tess_log!(self, "Tessellation failed with error: {:?}.", e);
builder.abort_geometry();
return Err(e);
}
if !self.assume_no_intersection {
debug_assert!(self.active.edges.is_empty());
debug_assert!(self.fill.spans.is_empty());
}
// There shouldn't be any span left after the tessellation ends.
// If for whatever reason (bug) there are, flush them so that we don't
// miss the triangles they contain.
for span in &mut self.fill.spans {
if let Some(tess) = span.tess.as_mut() {
tess.flush(builder);
}
}
self.fill.spans.clear();
Ok(builder.end_geometry())
}
/// Enable/disable some verbose logging during the tessellation, for
/// debugging purposes.
pub fn set_logging(&mut self, is_enabled: bool) {
#[cfg(debug_assertions)]
let forced = env::var("LYON_FORCE_LOGGING").is_ok();
#[cfg(not(debug_assertions))]
let forced = false;
self.log = is_enabled || forced;
}
#[cfg_attr(feature = "profiling", inline(never))]
fn tessellator_loop(
&mut self,
attrib_store: Option<&dyn AttributeStore>,
scan: &mut ActiveEdgeScan,
output: &mut dyn FillGeometryBuilder,
) -> Result<(), TessellationError> {
log_svg_preamble(self);
let mut _prev_position = point(std::f32::MIN, std::f32::MIN);
self.current_event_id = self.events.first_id();
while self.events.valid_id(self.current_event_id) {
self.initialize_events(attrib_store, output)?;
debug_assert!(is_after(self.current_position, _prev_position));
_prev_position = self.current_position;
if let Err(e) = self.process_events(scan, output) {
// Something went wrong, attempt to salvage the state of the sweep
// line
self.recover_from_error(e, output);
// ... and try again.
self.process_events(scan, output)?
}
#[cfg(debug_assertions)]
self.check_active_edges();
self.current_event_id = self.events.next_id(self.current_event_id);
}
Ok(())
}
fn initialize_events(
&mut self,
attrib_store: Option<&dyn AttributeStore>,
output: &mut dyn FillGeometryBuilder,
) -> Result<(), TessellationError> {
let current_event = self.current_event_id;
tess_log!(
self,
"\n\n<!-- event #{} -->",
current_event
);
self.current_position = self.events.position(current_event);
if self.current_position.x.is_nan() || self.current_position.y.is_nan() {
return Err(TessellationError::UnsupportedParamater);
}
let position = match self.orientation {
Orientation::Vertical => self.current_position,
Orientation::Horizontal => reorient(self.current_position),
};
self.current_vertex = output.add_fill_vertex(FillVertex {
position,
events: &self.events,
current_event,
attrib_store,
attrib_buffer: &mut self.attrib_buffer,
})?;
let mut current_sibling = current_event;
while self.events.valid_id(current_sibling) {
let edge = &self.events.edge_data[current_sibling as usize];
// We insert "fake" edges when there are end events
// to make sure we process that vertex even if it has
// no edge below.
if edge.is_edge {
let to = edge.to;
debug_assert!(is_after(to, self.current_position));
self.edges_below.push(PendingEdge {
to,
sort_key: slope(to - self.current_position), //.angle_from_x_axis().radians,
src_edge: current_sibling,
winding: edge.winding,
range_end: edge.range.end,
});
}
current_sibling = self.events.next_sibling_id(current_sibling);
}
Ok(())
}
/// An iteration of the sweep line algorithm.
#[cfg_attr(feature = "profiling", inline(never))]
fn process_events(
&mut self,
scan: &mut ActiveEdgeScan,
output: &mut dyn FillGeometryBuilder,
) -> Result<(), InternalError> {
tess_log!(self, "<!--");
tess_log!(
self,
" events at {:?} {:?} {} edges below",
self.current_position,
self.current_vertex,
self.edges_below.len(),
);
tess_log!(self, "edges below (initially): {:#?}", self.edges_below);
// Step 1 - Scan the active edge list, deferring processing and detecting potential
// ordering issues in the active edges.
self.scan_active_edges(scan)?;
// Step 2 - Do the necessary processing on edges that end at the current point.
self.process_edges_above(scan, output);
// Step 3 - Do the necessary processing on edges that start at the current point.
self.process_edges_below(scan);
// Step 4 - Insert/remove edges to the active edge as necessary and handle
// potential self-intersections.
self.update_active_edges(scan);
tess_log!(self, "-->");
#[cfg(debug_assertions)]
self.log_active_edges();
Ok(())
}
#[cfg(debug_assertions)]
fn log_active_edges(&self) {
tess_log!(self, r#"<g class="active-edges">"#);
tess_log!(
self,
r#"<path d="M 0 {} L 1000 {}" class="sweep-line"/>"#,
self.current_position.y,
self.current_position.y
);
tess_log!(self, "<!-- active edges: -->");
for e in &self.active.edges {
if e.is_merge {
tess_log!(
self,
r#" <circle cx="{}" cy="{}" r="3px" class="merge"/>"#,
e.from.x,
e.from.y
);
} else {
tess_log!(
self,
r#" <path d="M {:.5?} {:.5?} L {:.5?} {:.5?}" class="edge", winding="{:>2}"/>"#,
e.from.x,
e.from.y,
e.to.x,
e.to.y,
e.winding,
);
}
}
tess_log!(self, "<!-- spans: {}-->", self.fill.spans.len());
tess_log!(self, "</g>");
}
#[cfg(debug_assertions)]
fn check_active_edges(&self) {
let mut winding = WindingState::new();
for (idx, edge) in self.active.edges.iter().enumerate() {
winding.update(self.fill_rule, edge.winding);
if edge.is_merge {
assert!(self.fill_rule.is_in(winding.number));
} else {
assert!(
!is_after(self.current_position, edge.to),
"error at edge {}, position {:.6?} (current: {:.6?}",
idx,
edge.to,
self.current_position,
);
}
}
assert_eq!(winding.number, 0);
let expected_span_count = (winding.span_index + 1) as usize;
assert_eq!(self.fill.spans.len(), expected_span_count);
}
/// Scan the active edges to find the information we will need for the tessellation, without
/// modifying the state of the sweep line and active spans.
///
/// During this scan we also check that the ordering of the active edges is correct.
/// If an error is detected we bail out of the scan which will cause us to sort the active
/// edge list and try to scan again (this is why have to defer any modification to after
/// the scan).
///
/// The scan happens in three steps:
/// - 1) Loop over the edges on the left of the current point to compute the winding number.
/// - 2) Loop over the edges that connect with the current point to determine what processing
/// is needed (for example end events or right events).
/// - 3) Loop over the edges on the right of the current point to detect potential edges that should
/// have been handled in the previous phases.
#[cfg_attr(feature = "profiling", inline(never))]
fn scan_active_edges(&self, scan: &mut ActiveEdgeScan) -> Result<(), InternalError> {
scan.reset();
let current_x = self.current_position.x;
let mut connecting_edges = false;
let mut active_edge_idx = 0;
let mut winding = WindingState::new();
let mut previous_was_merge = false;
// Step 1 - Iterate over edges *before* the current point.
for active_edge in &self.active.edges {
if active_edge.is_merge {
// \.....\ /...../
// \.....x...../ <--- merge vertex
// \....:..../
// ---\---:---/---- <-- sweep line
// \..:../
// An unresolved merge vertex implies the left and right spans are
// adjacent and there is no transition between the two which means
// we need to bump the span index manually.
winding.span_index += 1;
active_edge_idx += 1;
previous_was_merge = true;
continue;
}
let egde_is_before_current_point =
if points_are_equal(self.current_position, active_edge.to) {
// We just found our first edge that connects with the current point.
// We might find other ones in the next iterations.
connecting_edges = true;
false
} else if active_edge.max_x() < current_x {
true
} else if active_edge.min_x() > current_x {
tess_log!(
self,
"min_x({:?}) > current_x({:?})",
active_edge.min_x(),
current_x
);
false
} else if active_edge.from.y == active_edge.to.y {
connecting_edges = true;
false
} else {
let ex = active_edge.solve_x_for_y(self.current_position.y);
if (ex - current_x).abs() <= self.tolerance {
connecting_edges = true;
false
} else if ex > current_x {
tess_log!(self, "ex({:?}) > current_x({:?})", ex, current_x);
false
} else {
true
}
};
if !egde_is_before_current_point {
break;
}
winding.update(self.fill_rule, active_edge.winding);
previous_was_merge = false;
active_edge_idx += 1;
tess_log!(
self,
" > span: {}, in: {}",
winding.span_index,
winding.is_in
);
}
scan.above.start = active_edge_idx;
scan.winding_before_point = winding;
if previous_was_merge {
scan.winding_before_point.span_index -= 1;
scan.above.start -= 1;
// First connecting edge is a merge.
// ...:./. ...:...
// ...:/.. or ...:...
// ...X... ...X...
//
// The span on the left does not end here but it has a vertex
// on its right side.
//
// The next loop can now assume that merge edges can't make the first
// transition connecting with the current vertex,
if !connecting_edges {
// There are no edges left and right of the merge that connect with
// the current vertex. In other words the merge is the only edge
// connecting and there must be a split event formed by two edges
// below the current vertex.
//
// In this case we don't end any span and we skip splitting. The merge
// and the split cancel each-other out.
//
// ...:...
// ...:...
// ...x...
// ../ \..
scan.vertex_events
.push((winding.span_index - 1, Side::Right));
scan.vertex_events.push((winding.span_index, Side::Left));
scan.merge_split_event = true;
tess_log!(self, "split+merge");
}
}
// .......
// ...x...
// ../ \..
scan.split_event = !connecting_edges && winding.is_in && !scan.merge_split_event;
// Step 2 - Iterate over edges connecting with the current point.
tess_log!(
self,
"connecting_edges {} | edge {} | span {}",
connecting_edges,
active_edge_idx,
winding.span_index
);
if connecting_edges {
let in_before_vertex = winding.is_in;
let mut first_connecting_edge = !previous_was_merge;
for active_edge in &self.active.edges[active_edge_idx..] {
if active_edge.is_merge {
if !winding.is_in {
return Err(InternalError::MergeVertexOutside);
}
// Merge above the current vertex to resolve.
//
// Resolving a merge usually leads to a span adjacent to the merge
// ending.
//
// If there was already an edge connecting with the current vertex
// just left of the merge edge, we can end the span between that edge
// and the merge.
//
// |
// v
// \...:...
// .\..:...
// ..\.:...
// ...\:...
// ....X...
scan.spans_to_end.push(winding.span_index);
// To deal with the right side of the merge, we simply pretend it
// transitioned into the shape. Next edge that transitions out (if any)
// will close out the span as if it was surrounded be regular edges.
//
// |
// v
// ...:.../
// ...:../
// ...:./
// ...:/
// ...X
winding.span_index += 1;
active_edge_idx += 1;
first_connecting_edge = false;
continue;
}
if !self.is_edge_connecting(active_edge, active_edge_idx, scan)? {
break;
}
if !first_connecting_edge && winding.is_in {
// End event.
//
// \.../
// \./
// x
//
scan.spans_to_end.push(winding.span_index);
}
winding.update(self.fill_rule, active_edge.winding);
tess_log!(
self,
" x span: {} in: {}",
winding.span_index,
winding.is_in
);
if winding.is_in && winding.span_index >= self.fill.spans.len() as i32 {
return Err(InternalError::InsufficientNumberOfSpans);
}
active_edge_idx += 1;
first_connecting_edge = false;
}
let in_after_vertex = winding.is_in;
let vertex_is_merge_event = in_before_vertex
&& in_after_vertex
&& self.edges_below.is_empty()
&& scan.edges_to_split.is_empty();
if vertex_is_merge_event {
// .\ /. .\ |./ /.
// ..\ /.. ..\|//...
// ...x... or ...x..... (etc.)
// ....... .........
scan.merge_event = true;
}
if in_before_vertex {
// ...| ..\ /..
// ...x or ...x... (etc.)
// ...| ...:...
let first_span_index = scan.winding_before_point.span_index;
scan.vertex_events.push((first_span_index, Side::Right));
}
if in_after_vertex {
// |... ..\ /..
// x... or ...x... (etc.)
// |... ...:...
scan.vertex_events.push((winding.span_index, Side::Left));
}
}
tess_log!(self, "edges after | {}", active_edge_idx);
scan.above.end = active_edge_idx;
// Step 3 - Now Iterate over edges after the current point.
// We only do this to detect errors.
self.check_remaining_edges(active_edge_idx, current_x)
}
#[cfg_attr(feature = "profiling", inline(never))]
#[cfg_attr(not(feature = "profiling"), inline(always))]
fn check_remaining_edges(
&self,
active_edge_idx: usize,
current_x: f32,
) -> Result<(), InternalError> {
// This function typically takes about 2.5% ~ 3% of the profile, so not necessarily the best
// target for optimization. That said all of the work done here is only robustness checks
// so we could add an option to skip it.
for active_edge in &self.active.edges[active_edge_idx..] {
if active_edge.is_merge {
continue;
}
if active_edge.max_x() < current_x {
return Err(InternalError::IncorrectActiveEdgeOrder(1));
}
if points_are_equal(self.current_position, active_edge.to) {
return Err(InternalError::IncorrectActiveEdgeOrder(2));
}
if active_edge.min_x() < current_x
&& active_edge.solve_x_for_y(self.current_position.y) < current_x
{
return Err(InternalError::IncorrectActiveEdgeOrder(3));
}
}
Ok(())
}
// Returns Ok(true) if the edge connects with the current vertex, Ok(false) otherwise.
// Returns Err if the active edge order is wrong.
fn is_edge_connecting(
&self,
active_edge: &ActiveEdge,
active_edge_idx: usize,
scan: &mut ActiveEdgeScan,
) -> Result<bool, InternalError> {
if points_are_equal(self.current_position, active_edge.to) {
return Ok(true);
}
let current_x = self.current_position.x;
let threshold = self.tolerance;
let min_x = active_edge.min_x();
let max_x = active_edge.max_x();
if max_x + threshold < current_x || active_edge.to.y < self.current_position.y {
return Err(InternalError::IncorrectActiveEdgeOrder(4));
}
if min_x > current_x {
return Ok(false);
}
let ex = if active_edge.from.y != active_edge.to.y {
active_edge.solve_x_for_y(self.current_position.y)
} else if max_x >= current_x && min_x <= current_x {
current_x
} else {
active_edge.to.y
};
if (ex - current_x).abs() <= threshold {
tess_log!(
self,
"vertex on an edge! {:?} -> {:?}",
active_edge.from,
active_edge.to
);
scan.edges_to_split.push(active_edge_idx);
return Ok(true);
}
if ex < current_x {
return Err(InternalError::IncorrectActiveEdgeOrder(5));
}
tess_log!(self, "ex = {:?} (diff={})", ex, ex - current_x);
Ok(false)
}
#[cfg_attr(feature = "profiling", inline(never))]
fn process_edges_above(
&mut self,
scan: &mut ActiveEdgeScan,
output: &mut dyn FillGeometryBuilder,
) {
for &(span_index, side) in &scan.vertex_events {
tess_log!(
self,
" -> Vertex event, span: {:?} / {:?} / id: {:?}",
span_index,
side,
self.current_vertex
);
self.fill.spans[span_index as usize].tess().vertex(
self.current_position,
self.current_vertex,
side,
);
}
for &span_index in &scan.spans_to_end {
tess_log!(self, " -> End span {:?}", span_index);
self.fill.end_span(
span_index,
&self.current_position,
self.current_vertex,
output,
);
}
self.fill.cleanup_spans();
for &edge_idx in &scan.edges_to_split {
let active_edge = &mut self.active.edges[edge_idx];
let to = active_edge.to;
self.edges_below.push(PendingEdge {
to,
sort_key: slope(to - self.current_position),
src_edge: active_edge.src_edge,
winding: active_edge.winding,
range_end: active_edge.range_end,
});
tess_log!(
self,
"split {:?}, add edge below {:?} -> {:?} ({:?})",
edge_idx,
self.current_position,
self.edges_below.last().unwrap().to,
active_edge.winding,
);
active_edge.to = self.current_position;
}
if scan.merge_event {
// Merge event.
//
// ...\ /...
// ....\ /....
// .....x.....
//
let edge = &mut self.active.edges[scan.above.start];
edge.is_merge = true;
edge.from = edge.to;
edge.winding = 0;
edge.from_id = self.current_vertex;
// take the merge edge out of the range so that it isn't removed later.
scan.above.start += 1;
}
}
#[cfg_attr(feature = "profiling", inline(never))]
fn process_edges_below(&mut self, scan: &mut ActiveEdgeScan) {
let mut winding = scan.winding_before_point;
tess_log!(
self,
"connecting edges: {}..{} in: {:?}",
scan.above.start,
scan.above.end,
winding.is_in
);
tess_log!(self, "winding state before point: {:?}", winding);
tess_log!(self, "edges below: {:#?}", self.edges_below);
self.sort_edges_below();
self.handle_coincident_edges_below();
if scan.split_event {
// Split event.
//
// ...........
// .....x.....
// ..../ \....
// .../ \...
//
tess_log!(self, "split event");
let left_enclosing_edge_idx = scan.above.start - 1;
self.split_event(left_enclosing_edge_idx, winding.span_index);
}
// Go through the edges that start at the current point and emit
// start events for each time an in-out pair is found.
let mut first_pending_edge = true;
for pending_edge in &self.edges_below {
if !first_pending_edge && winding.is_in {
// Start event.
//
// x
// /.\
// /...\
//
tess_log!(
self,
" begin span {} ({})",
winding.span_index,
self.fill.spans.len()
);
self.fill.begin_span(
winding.span_index,
&self.current_position,
self.current_vertex,
);
}
winding.update(self.fill_rule, pending_edge.winding);
tess_log!(
self,
"edge below: span: {}, in: {}",
winding.span_index,
winding.is_in
);
first_pending_edge = false;
}
}
#[cfg_attr(feature = "profiling", inline(never))]
fn update_active_edges(&mut self, scan: &ActiveEdgeScan) {
let above = scan.above.start..scan.above.end;
tess_log!(
self,
" remove {} edges ({}..{})",
above.end - above.start,
above.start,
above.end
);
if !self.assume_no_intersection {
self.handle_intersections(above.clone());
}
#[cfg(debug_assertions)]
for active_edge in &self.active.edges[above.clone()] {
debug_assert!(active_edge.is_merge || !is_after(self.current_position, active_edge.to));
}
let from = self.current_position;
let from_id = self.current_vertex;
self.active.edges.splice(
above,
self.edges_below.iter().map(|edge| ActiveEdge {
from,
to: edge.to,
winding: edge.winding,
is_merge: false,
from_id,
src_edge: edge.src_edge,
range_end: edge.range_end,
}),
);
self.edges_below.clear();
}
fn split_event(&mut self, left_enclosing_edge_idx: ActiveEdgeIdx, left_span_idx: SpanIdx) {
let right_enclosing_edge_idx = left_enclosing_edge_idx + 1;
let upper_left = self.active.edges[left_enclosing_edge_idx].from;
let upper_right = self.active.edges[right_enclosing_edge_idx].from;
let right_span_idx = left_span_idx + 1;
let (upper_position, upper_id, new_span_idx) = if is_after(upper_left, upper_right) {
// |.....
// upper_left --> x.....
// /.:....
// /...x... <-- current split vertex
// /.../ \..
(
upper_left,
self.active.edges[left_enclosing_edge_idx].from_id,
left_span_idx,
)
} else {
// .....|
// .....x <-- upper_right
// ....:.\
// current split vertex --> ...x...\
// ../ \...\
(
upper_right,
self.active.edges[right_enclosing_edge_idx].from_id,
right_span_idx,
)
};
self.fill
.begin_span(new_span_idx, &upper_position, upper_id);
self.fill.spans[left_span_idx as usize].tess().vertex(
self.current_position,
self.current_vertex,
Side::Right,
);
self.fill.spans[right_span_idx as usize].tess().vertex(
self.current_position,
self.current_vertex,
Side::Left,
);
}
#[cfg_attr(feature = "profiling", inline(never))]
fn handle_intersections(&mut self, skip_range: Range<usize>) {
// Do intersection checks for all of the new edges against already active edges.
//
// If several intersections are found on the same edges we only keep the top-most.
// the active and new edges are then truncated at the intersection position and the
// lower parts are added to the event queue.
//
// In order to not break invariants of the sweep line we need to ensure that:
// - the intersection position is never ordered before the current position,
// - after truncation, edges continue being oriented downwards,
//
// Floating-point precision (or the lack thereof) prevent us from taking the
// above properties from granted even though they make sense from a purely
// geometrical perspective. Therefore we have to take great care in checking
// whether these invariants aren't broken by the insertion of the intersection,
// manually fixing things up if need be and making sure to not break more
// invariants in doing so.
let mut edges_below = mem::take(&mut self.edges_below);
for edge_below in &mut edges_below {
let below_min_x = self.current_position.x.min(edge_below.to.x);
let below_max_x = fmax(self.current_position.x, edge_below.to.x);
let below_segment = LineSegment {
from: self.current_position.to_f64(),
to: edge_below.to.to_f64(),
};
let mut tb_min = 1.0;
let mut intersection = None;
for (i, active_edge) in self.active.edges.iter().enumerate() {
if skip_range.contains(&i) {
continue;
}
if active_edge.is_merge || below_min_x > active_edge.max_x() {
continue;
}
if below_max_x < active_edge.min_x() {
// We can't early out because there might be edges further on the right
// that extend further on the left which would be missed.
//
// sweep line -> =o===/==/==
// |\ / /
// | o /
// edge below -> | /
// | /
// | / <- missed active edge
// |/
// x <- missed intersection
// /|
continue;
}
let active_segment = LineSegment {
from: active_edge.from.to_f64(),
to: active_edge.to.to_f64(),
};
if let Some((ta, tb)) = active_segment.intersection_t(&below_segment) {
if tb < tb_min && tb > 0.0 && ta > 0.0 && ta <= 1.0 {
// we only want the closest intersection;
tb_min = tb;
intersection = Some((ta, tb, i));
}
}
}
if let Some((ta, tb, active_edge_idx)) = intersection {
self.process_intersection(ta, tb, active_edge_idx, edge_below, &below_segment);
}
}
self.edges_below = edges_below;
//self.log_active_edges();
}
#[inline(never)]
fn process_intersection(
&mut self,
ta: f64,
tb: f64,
active_edge_idx: usize,
edge_below: &mut PendingEdge,
below_segment: &LineSegment<f64>,
) {
let mut intersection_position = below_segment.sample(tb).to_f32();
tess_log!(
self,
"-> intersection at: {:?} t={:?}|{:?}",
intersection_position,
ta,
tb
);
tess_log!(
self,
" from {:?}->{:?} and {:?}->{:?}",
self.active.edges[active_edge_idx].from,
self.active.edges[active_edge_idx].to,
self.current_position,
edge_below.to,
);
let active_edge = &mut self.active.edges[active_edge_idx];
if self.current_position == intersection_position {
active_edge.from = intersection_position;
let src_range = &mut self.events.edge_data[active_edge.src_edge as usize].range;
let remapped_ta = remap_t_in_range(ta as f32, src_range.start..active_edge.range_end);
src_range.start = remapped_ta;
return;
}
if !is_after(intersection_position, self.current_position) {
tess_log!(self, "fixup the intersection");
intersection_position.y = self.current_position.y.next_after(std::f32::INFINITY);
}
assert!(is_after(intersection_position, self.current_position), "!!! {:.9?} {:.9?}", intersection_position, self.current_position);
if is_near(intersection_position, edge_below.to) {
tess_log!(self, "intersection near below.to");
intersection_position = edge_below.to;
} else if is_near(intersection_position, active_edge.to) {
tess_log!(self, "intersection near active_edge.to");
intersection_position = active_edge.to;
}
let a_src_edge_data = self.events.edge_data[active_edge.src_edge as usize].clone();
let b_src_edge_data = self.events.edge_data[edge_below.src_edge as usize].clone();
let mut inserted_evt = None;
let mut flipped_active = false;
if active_edge.to != intersection_position && active_edge.from != intersection_position {
let remapped_ta = remap_t_in_range(
ta as f32,
a_src_edge_data.range.start..active_edge.range_end,
);
if is_after(active_edge.to, intersection_position) {
// Should take this branch most of the time.
inserted_evt = Some(self.events.insert_sorted(
intersection_position,
EdgeData {
range: remapped_ta as f32..active_edge.range_end,
winding: active_edge.winding,
to: active_edge.to,
is_edge: true,
..a_src_edge_data
},
self.current_event_id,
));
} else {
tess_log!(self, "flip active edge after intersection");
flipped_active = true;
self.events.insert_sorted(
active_edge.to,
EdgeData {
range: active_edge.range_end..remapped_ta as f32,
winding: -active_edge.winding,
to: intersection_position,
is_edge: true,
..a_src_edge_data
},
self.current_event_id,
);
}
active_edge.to = intersection_position;
active_edge.range_end = remapped_ta;
}
if edge_below.to != intersection_position && self.current_position != intersection_position
{
let remapped_tb =
remap_t_in_range(tb as f32, b_src_edge_data.range.start..edge_below.range_end);
if is_after(edge_below.to, intersection_position) {
let edge_data = EdgeData {
range: remapped_tb as f32..edge_below.range_end,
winding: edge_below.winding,
to: edge_below.to,
is_edge: true,
..b_src_edge_data
};
if let Some(idx) = inserted_evt {
// Should take this branch most of the time.
self.events
.insert_sibling(idx, intersection_position, edge_data);
} else {
self.events.insert_sorted(
intersection_position,
edge_data,
self.current_event_id,
);
}
} else {
tess_log!(self, "flip edge below after intersection");
self.events.insert_sorted(
edge_below.to,
EdgeData {
range: edge_below.range_end..remapped_tb as f32,
winding: -edge_below.winding,
to: intersection_position,
is_edge: true,
..b_src_edge_data
},
self.current_event_id,
);
if flipped_active {
// It is extremely rare but if we end up flipping both of the
// edges that are inserted in the event queue, then we created a
// merge event which means we have to insert a vertex event into
// the queue, otherwise the tessellator will skip over the end of
// these two edges.
self.events.vertex_event_sorted(
intersection_position,
b_src_edge_data.to_id,
self.current_event_id,
);
}
}
edge_below.to = intersection_position;
edge_below.range_end = remapped_tb;
}
}
fn sort_active_edges(&mut self) {
// Merge edges are a little subtle when it comes to sorting.
// They are points rather than edges and the best we can do is
// keep their relative ordering with their previous or next edge.
// Unfortunately this can cause merge vertices to end up outside of
// the shape.
// After sorting we go through the active edges and rearrange merge
// vertices to prevent that.
let y = self.current_position.y;
let mut keys = Vec::with_capacity(self.active.edges.len());
let mut has_merge_vertex = false;
let mut prev_x = f32::NAN;
for (i, edge) in self.active.edges.iter().enumerate() {
if edge.is_merge {
debug_assert!(!prev_x.is_nan());
has_merge_vertex = true;
keys.push((prev_x, i));
} else {
debug_assert!(!is_after(self.current_position, edge.to));
let eq_to = edge.to.y == y;
let eq_from = edge.from.y == y;
let x = if eq_to && eq_from {
let current_x = self.current_position.x;
if edge.max_x() >= current_x && edge.min_x() <= current_x {
self.current_position.x
} else {
edge.min_x()
}
} else if eq_from {
edge.from.x
} else if eq_to {
edge.to.x
} else {
edge.solve_x_for_y(y)
};
keys.push((fmax(x, edge.min_x()), i));
prev_x = x;
}
}
keys.sort_by(|a, b| match a.0.partial_cmp(&b.0).unwrap() {
Ordering::Less => Ordering::Less,
Ordering::Greater => Ordering::Greater,
Ordering::Equal => {
let a = &self.active.edges[a.1];
let b = &self.active.edges[b.1];
match (a.is_merge, b.is_merge) {
(false, false) => {
let slope_a = slope(a.to - a.from);
let slope_b = slope(b.to - b.from);
slope_b.partial_cmp(&slope_a).unwrap_or(Ordering::Equal)
}
(true, false) => Ordering::Greater,
(false, true) => Ordering::Less,
(true, true) => Ordering::Equal,
}
}
});
let mut new_active_edges = Vec::with_capacity(self.active.edges.len());
for &(_, idx) in &keys {
new_active_edges.push(self.active.edges[idx]);
}
self.active.edges = new_active_edges;
if !has_merge_vertex {
return;
}
let mut winding_number = 0;
for i in 0..self.active.edges.len() {
let needs_swap = {
let edge = &self.active.edges[i];
if edge.is_merge {
!self.fill_rule.is_in(winding_number)
} else {
winding_number += edge.winding;
false
}
};
if needs_swap {
let mut w = winding_number;
tess_log!(self, "Fixing up merge vertex after sort.");
let mut idx = i;
loop {
// Roll back previous edge winding and swap.
w -= self.active.edges[idx - 1].winding;
self.active.edges.swap(idx, idx - 1);
if self.fill_rule.is_in(w) {
break;
}
idx -= 1;
}
}
}
}
#[inline(never)]
fn recover_from_error(&mut self, _error: InternalError, output: &mut dyn FillGeometryBuilder) {
tess_log!(self, "Attempt to recover error {:?}", _error);
self.sort_active_edges();
debug_assert!(self
.active
.edges
.first()
.map(|e| !e.is_merge)
.unwrap_or(true));
// This can only happen if we ignore self-intersections,
// so we are in a pretty broken state already.
// There isn't a fully correct solution for this (other
// than properly detecting self intersections and not
// getting into this situation), but the rest of the code
// doesn't deal with merge edges being at the last position
// so we artificially move them to avoid that.
// TODO: with self-intersections properly handled it may make more sense
// to turn this into an assertion.
let len = self.active.edges.len();
if len > 1 && self.active.edges[len - 1].is_merge {
self.active.edges.swap(len - 1, len - 2);
}
let mut winding = WindingState::new();
for edge in &self.active.edges {
if edge.is_merge {
winding.span_index += 1;
} else {
winding.update(self.fill_rule, edge.winding);
}
if winding.span_index >= self.fill.spans.len() as i32 {
self.fill
.begin_span(winding.span_index, &edge.from, edge.from_id);
}
}
while self.fill.spans.len() > (winding.span_index + 1) as usize {
self.fill.spans.last_mut().unwrap().tess().flush(output);
self.fill.spans.pop();
}
tess_log!(self, "-->");
#[cfg(debug_assertions)]
self.log_active_edges();
}
#[cfg_attr(feature = "profiling", inline(never))]
fn sort_edges_below(&mut self) {
self.edges_below
.sort_unstable_by(|a, b| a.sort_key.partial_cmp(&b.sort_key).unwrap());
}
#[cfg_attr(feature = "profiling", inline(never))]
fn handle_coincident_edges_below(&mut self) {
if self.edges_below.len() < 2 {
return;
}
for idx in (0..(self.edges_below.len() - 1)).rev() {
let a_idx = idx;
let b_idx = idx + 1;
let a_slope = self.edges_below[a_idx].sort_key;
let b_slope = self.edges_below[b_idx].sort_key;
const THRESHOLD: f32 = 0.00005;
// The slope function preserves the ordering for sorting but isn't a very good approximation
// of the angle as edges get closer to horizontal.
// When edges are larger in x than y, comparing the inverse is a better approximation.
let angle_is_close = if a_slope.abs() <= 1.0 {
(a_slope - b_slope).abs() < THRESHOLD
} else {
(1.0 / a_slope - 1.0 / b_slope).abs() < THRESHOLD
};
if angle_is_close {
self.merge_coincident_edges(a_idx, b_idx);
}
}
}
#[cold]
fn merge_coincident_edges(&mut self, a_idx: usize, b_idx: usize) {
let a_to = self.edges_below[a_idx].to;
let b_to = self.edges_below[b_idx].to;
let (lower_idx, upper_idx, split) = match compare_positions(a_to, b_to) {
Ordering::Greater => (a_idx, b_idx, true),
Ordering::Less => (b_idx, a_idx, true),
Ordering::Equal => (a_idx, b_idx, false),
};
tess_log!(
self,
"coincident edges {:?} -> {:?} / {:?}",
self.current_position,
a_to,
b_to
);
tess_log!(
self,
"update winding: {:?} -> {:?}",
self.edges_below[upper_idx].winding,
self.edges_below[upper_idx].winding + self.edges_below[lower_idx].winding
);
self.edges_below[upper_idx].winding += self.edges_below[lower_idx].winding;
let split_point = self.edges_below[upper_idx].to;
tess_log!(
self,
"remove coincident edge {:?}, split:{:?}",
a_idx,
split
);
let edge = self.edges_below.remove(lower_idx);
if !split {
return;
}
let src_edge_data = self.events.edge_data[edge.src_edge as usize].clone();
let t = LineSegment {
from: self.current_position,
to: edge.to,
}
.solve_t_for_y(split_point.y);
let src_range = src_edge_data.range.start..edge.range_end;
let t_remapped = remap_t_in_range(t, src_range);
let edge_data = EdgeData {
range: t_remapped..edge.range_end,
winding: edge.winding,
to: edge.to,
is_edge: true,
..src_edge_data
};
self.events
.insert_sorted(split_point, edge_data, self.current_event_id);
}
fn reset(&mut self) {
self.current_position = point(f32::MIN, f32::MIN);
self.current_vertex = VertexId::INVALID;
self.current_event_id = INVALID_EVENT_ID;
self.active.edges.clear();
self.edges_below.clear();
self.fill.spans.clear();
}
}
pub(crate) fn points_are_equal(a: Point, b: Point) -> bool {
// TODO: Use the tolerance threshold?
a == b
}
pub(crate) fn compare_positions(a: Point, b: Point) -> Ordering {
// This function is somewhat hot during the sorting phase but it might be that inlining
// moves the cost of fetching the positions here.
// The y coordinates are rarely equal (typically less than 7% of the time) but it's
// unclear whether moving the x comparison out into a cold function helps in practice.
if a.y > b.y {
return Ordering::Greater;
}
if a.y < b.y {
return Ordering::Less;
}
if a.x > b.x {
return Ordering::Greater;
}
if a.x < b.x {
return Ordering::Less;
}
Ordering::Equal
}
#[inline]
pub(crate) fn is_after(a: Point, b: Point) -> bool {
a.y > b.y || (a.y == b.y && a.x > b.x)
}
#[inline]
pub(crate) fn is_near(a: Point, b: Point) -> bool {
(a - b).square_length() < 0.000000001
}
#[inline]
fn reorient(p: Point) -> Point {
point(p.y, -p.x)
}
/// Extra vertex information from the `FillTessellator`, accessible when building vertices.
pub struct FillVertex<'l> {
pub(crate) position: Point,
pub(crate) events: &'l EventQueue,
pub(crate) current_event: TessEventId,
pub(crate) attrib_buffer: &'l mut [f32],
pub(crate) attrib_store: Option<&'l dyn AttributeStore>,
}
impl<'l> FillVertex<'l> {
pub fn position(&self) -> Point {
self.position
}
/// Return an iterator over the sources of the vertex.
pub fn sources(&self) -> VertexSourceIterator {
VertexSourceIterator {
events: self.events,
id: self.current_event,
prev: None,
}
}
/// Returns the first endpoint that this vertex is on, if any.
///
/// This is meant to be used only in very simple cases where self-intersections,
/// overlapping vertices and curves are unexpected.
/// This will return `None` at self-intersections and between the endpoints of
/// a flattened curve. If two endpoints are at the same position only one of
/// them is returned.
///
/// See also: `FillVertex::sources`.
pub fn as_endpoint_id(&self) -> Option<EndpointId> {
let mut current = self.current_event;
while self.events.valid_id(current) {
let edge = &self.events.edge_data[current as usize];
let t = edge.range.start;
if t == 0.0 {
return Some(edge.from_id);
}
if t == 1.0 {
return Some(edge.to_id);
}
current = self.events.next_sibling_id(current)
}
None
}
/// Fetch or interpolate the custom attribute values at this vertex.
pub fn interpolated_attributes(&mut self) -> &[f32] {
if self.attrib_store.is_none() {
return &[];
}
let store = self.attrib_store.unwrap();
let mut sources = VertexSourceIterator {
events: self.events,
id: self.current_event,
prev: None,
};
let num_attributes = store.num_attributes();
let first = sources.next().unwrap();
let mut next = sources.next();
// Fast path for the single-source-single-endpoint common case.
if next.is_none() {
if let VertexSource::Endpoint { id } = first {
return store.get(id);
}
}
// First source taken out of the loop to avoid initializing the buffer.
match first {
VertexSource::Endpoint { id } => {
let a = store.get(id);
assert!(a.len() == num_attributes);
assert!(self.attrib_buffer.len() == num_attributes);
self.attrib_buffer[..num_attributes].clone_from_slice(&a[..num_attributes]);
}
VertexSource::Edge { from, to, t } => {
let a = store.get(from);
let b = store.get(to);
assert!(a.len() == num_attributes);
assert!(b.len() == num_attributes);
assert!(self.attrib_buffer.len() == num_attributes);
for i in 0..num_attributes {
self.attrib_buffer[i] = a[i] * (1.0 - t) + b[i] * t;
}
}
}
let mut div = 1.0;
loop {
match next {
Some(VertexSource::Endpoint { id }) => {
let a = store.get(id);
assert!(a.len() == num_attributes);
assert!(self.attrib_buffer.len() == num_attributes);
for (i, &att) in a.iter().enumerate() {
self.attrib_buffer[i] += att;
}
}
Some(VertexSource::Edge { from, to, t }) => {
let a = store.get(from);
let b = store.get(to);
assert!(a.len() == num_attributes);
assert!(b.len() == num_attributes);
assert!(self.attrib_buffer.len() == num_attributes);
for i in 0..num_attributes {
self.attrib_buffer[i] += a[i] * (1.0 - t) + b[i] * t;
}
}
None => {
break;
}
}
div += 1.0;
next = sources.next();
}
if div > 1.0 {
for attribute in self.attrib_buffer.iter_mut() {
*attribute /= div;
}
}
self.attrib_buffer
}
}
/// An iterator over the sources of a given vertex.
#[derive(Clone)]
pub struct VertexSourceIterator<'l> {
events: &'l EventQueue,
id: TessEventId,
prev: Option<VertexSource>,
}
impl<'l> Iterator for VertexSourceIterator<'l> {
type Item = VertexSource;
#[inline]
fn next(&mut self) -> Option<VertexSource> {
let mut src;
loop {
if self.id == INVALID_EVENT_ID {
return None;
}
let edge = &self.events.edge_data[self.id as usize];
self.id = self.events.next_sibling_id(self.id);
let t = edge.range.start;
src = if t == 0.0 {
Some(VertexSource::Endpoint { id: edge.from_id })
} else if t == 1.0 {
Some(VertexSource::Endpoint { id: edge.to_id })
} else {
Some(VertexSource::Edge {
from: edge.from_id,
to: edge.to_id,
t,
})
};
if src != self.prev {
break;
}
}
self.prev = src;
src
}
}
fn remap_t_in_range(val: f32, range: Range<f32>) -> f32 {
if range.end > range.start {
let d = range.end - range.start;
range.start + val * d
} else {
let d = range.start - range.end;
range.end + (1.0 - val) * d
}
}
pub struct FillBuilder<'l> {
events: EventQueueBuilder,
next_id: EndpointId,
first_id: EndpointId,
first_position: Point,
horizontal_sweep: bool,
tessellator: &'l mut FillTessellator,
output: &'l mut dyn FillGeometryBuilder,
options: &'l FillOptions,
}
impl<'l> FillBuilder<'l> {
fn new(
tessellator: &'l mut FillTessellator,
options: &'l FillOptions,
output: &'l mut dyn FillGeometryBuilder,
) -> Self {
let mut events =
std::mem::replace(&mut tessellator.events, EventQueue::new()).into_builder();
events.set_tolerance(options.tolerance);
FillBuilder {
events,
next_id: EndpointId(0),
first_id: EndpointId(0),
horizontal_sweep: options.sweep_orientation == Orientation::Horizontal,
first_position: point(0.0, 0.0),
tessellator,
options,
output,
}
}
#[inline]
fn position(&self, p: Point) -> Point {
if self.horizontal_sweep {
point(-p.y, p.x)
} else {
p
}
}
pub fn build(self) -> TessellationResult {
let mut event_queue = self.events.build();
std::mem::swap(&mut self.tessellator.events, &mut event_queue);
self.tessellator
.tessellate_impl(self.options, None, self.output)
}
}
impl<'l> PathBuilder for FillBuilder<'l> {
fn begin(&mut self, at: Point) -> EndpointId {
let at = self.position(at);
let id = self.next_id;
self.next_id.0 += 1;
self.first_id = id;
self.first_position = at;
self.events.begin(at, id);
id
}
fn end(&mut self, _close: bool) {
self.events.end(self.first_position, self.first_id);
}
fn line_to(&mut self, to: Point) -> EndpointId {
let to = self.position(to);
let id = self.next_id;
self.next_id.0 += 1;
self.events.line_segment(to, id, 0.0, 1.0);
id
}
fn quadratic_bezier_to(&mut self, ctrl: Point, to: Point) -> EndpointId {
let ctrl = self.position(ctrl);
let to = self.position(to);
let id = self.next_id;
self.next_id.0 += 1;
self.events.quadratic_bezier_segment(ctrl, to, id);
id
}
fn cubic_bezier_to(&mut self, ctrl1: Point, ctrl2: Point, to: Point) -> EndpointId {
let ctrl1 = self.position(ctrl1);
let ctrl2 = self.position(ctrl2);
let to = self.position(to);
let id = self.next_id;
self.next_id.0 += 1;
self.events.cubic_bezier_segment(ctrl1, ctrl2, to, id);
id
}
fn reserve(&mut self, endpoints: usize, ctrl_points: usize) {
self.events.reserve(endpoints + ctrl_points * 2);
}
/// Tessellate the stroke for an axis-aligned rounded rectangle.
fn add_circle(&mut self, center: Point, radius: f32, winding: Winding) {
// This specialized routine extracts the curves into separate sub-paths
// to nudge the tessellator towards putting them in their own monotonic
// spans. This avoids generating thin triangles from one side of the circle
// to the other.
// We can do this because we know shape is convex and we don't need to trace
// the outline.
let radius = radius.abs();
let dir = match winding {
Winding::Positive => 1.0,
Winding::Negative => -1.0,
};
self.reserve(16, 8);
let tan_pi_over_8 = 0.41421357;
let cos_pi_over_4 = f32::consts::FRAC_1_SQRT_2;
let d = radius * tan_pi_over_8;
let start = center + vector(-radius, 0.0);
self.begin(start);
let ctrl_0 = center + vector(-radius, -d * dir);
let mid_0 = center + vector(-1.0, -dir) * radius * cos_pi_over_4;
let ctrl_1 = center + vector(-d, -radius * dir);
let mid_1 = center + vector(0.0, -radius * dir);
self.quadratic_bezier_to(ctrl_0, mid_0);
self.end(false);
self.begin(mid_0);
self.quadratic_bezier_to(ctrl_1, mid_1);
self.end(false);
self.begin(mid_1);
let ctrl_0 = center + vector(d, -radius * dir);
let mid_2 = center + vector(1.0, -dir) * radius * cos_pi_over_4;
let ctrl_1 = center + vector(radius, -d * dir);
let mid_3 = center + vector(radius, 0.0);
self.quadratic_bezier_to(ctrl_0, mid_2);
self.end(false);
self.begin(mid_2);
self.quadratic_bezier_to(ctrl_1, mid_3);
self.end(false);
self.begin(mid_3);
let ctrl_0 = center + vector(radius, d * dir);
let mid_4 = center + vector(1.0, dir) * radius * cos_pi_over_4;
let ctrl_1 = center + vector(d, radius * dir);
let mid_5 = center + vector(0.0, radius * dir);
self.quadratic_bezier_to(ctrl_0, mid_4);
self.end(false);
self.begin(mid_4);
self.quadratic_bezier_to(ctrl_1, mid_5);
self.end(false);
self.begin(mid_5);
let ctrl_0 = center + vector(-d, radius * dir);
let mid_6 = center + vector(-1.0, dir) * radius * cos_pi_over_4;
let ctrl_1 = center + vector(-radius, d * dir);
self.quadratic_bezier_to(ctrl_0, mid_6);
self.end(false);
self.begin(mid_6);
self.quadratic_bezier_to(ctrl_1, start);
self.end(false);
self.begin(start);
self.line_to(mid_0);
self.line_to(mid_1);
self.line_to(mid_2);
self.line_to(mid_3);
self.line_to(mid_4);
self.line_to(mid_5);
self.line_to(mid_6);
self.close();
}
}
impl<'l> Build for FillBuilder<'l> {
type PathType = TessellationResult;
#[inline]
fn build(self) -> TessellationResult {
self.build()
}
}
fn log_svg_preamble(_tess: &FillTessellator) {
tess_log!(
_tess,
r#"
<svg viewBox="0 0 1000 1000">
<style type="text/css">
<![CDATA[
path.sweep-line {{
stroke: red;
fill: none;
}}
path.edge {{
stroke: blue;
fill: none;
}}
path.edge.select {{
stroke: green;
fill: none;
}}
circle.merge {{
fill: yellow;
stroke: orange;
fill-opacity: 1;
}}
circle.current {{
fill: white;
stroke: grey;
fill-opacity: 1;
}}
g.active-edges {{
opacity: 0;
}}
g.active-edges.select {{
opacity: 1;
}}
]]>
</style>
"#
);
}
#[cfg(test)]
use crate::geometry_builder::*;
#[cfg(test)]
fn eq(a: Point, b: Point) -> bool {
(a.x - b.x).abs() < 0.00001 && (a.y - b.y).abs() < 0.00001
}
#[cfg(test)]
fn at_endpoint(src: &VertexSource, endpoint: EndpointId) -> bool {
match src {
VertexSource::Edge { .. } => false,
VertexSource::Endpoint { id } => *id == endpoint,
}
}
#[cfg(test)]
fn on_edge(src: &VertexSource, from_id: EndpointId, to_id: EndpointId, d: f32) -> bool {
match src {
VertexSource::Edge { t, from, to, .. } => {
*from == from_id
&& *to == to_id
&& ((d - *t).abs() < 0.00001 || (1.0 - d - *t).abs() <= 0.00001)
}
VertexSource::Endpoint { .. } => false,
}
}
#[test]
fn fill_vertex_source_01() {
use crate::path::commands::PathCommands;
use crate::path::AttributeSlice;
let endpoints: &[Point] = &[point(0.0, 0.0), point(1.0, 1.0), point(0.0, 2.0)];
let attributes = &[1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0];
let mut cmds = PathCommands::builder();
cmds.begin(EndpointId(0));
cmds.line_to(EndpointId(1));
cmds.line_to(EndpointId(2));
cmds.end(true);
let cmds = cmds.build();
let mut tess = FillTessellator::new();
tess.tessellate_with_ids(
cmds.iter(),
&(endpoints, endpoints),
Some(&AttributeSlice::new(attributes, 3)),
&FillOptions::default(),
&mut CheckVertexSources { next_vertex: 0 },
)
.unwrap();
struct CheckVertexSources {
next_vertex: u32,
}
impl GeometryBuilder for CheckVertexSources {
fn begin_geometry(&mut self) {}
fn end_geometry(&mut self) -> Count {
Count {
vertices: self.next_vertex,
indices: 0,
}
}
fn abort_geometry(&mut self) {}
fn add_triangle(&mut self, _: VertexId, _: VertexId, _: VertexId) {}
}
impl FillGeometryBuilder for CheckVertexSources {
fn add_fill_vertex(
&mut self,
mut vertex: FillVertex,
) -> Result<VertexId, GeometryBuilderError> {
let pos = vertex.position();
for src in vertex.sources() {
if eq(pos, point(0.0, 0.0)) {
assert!(at_endpoint(&src, EndpointId(0)))
} else if eq(pos, point(1.0, 1.0)) {
assert!(at_endpoint(&src, EndpointId(1)))
} else if eq(pos, point(0.0, 2.0)) {
assert!(at_endpoint(&src, EndpointId(2)))
} else {
panic!()
}
}
if eq(pos, point(0.0, 0.0)) {
assert_eq!(vertex.interpolated_attributes(), &[1.0, 0.0, 0.0])
} else if eq(pos, point(1.0, 1.0)) {
assert_eq!(vertex.interpolated_attributes(), &[0.0, 1.0, 0.0])
} else if eq(pos, point(0.0, 2.0)) {
assert_eq!(vertex.interpolated_attributes(), &[0.0, 0.0, 1.0])
}
let id = self.next_vertex;
self.next_vertex += 1;
Ok(VertexId(id))
}
}
}
#[test]
fn fill_vertex_source_02() {
// Check the vertex sources of a simple self-intersecting shape.
// _
// _|_|_
// | | | |
// |_|_|_|
// |_|
//
let mut path = crate::path::Path::builder_with_attributes(3);
let a = path.begin(point(1.0, 0.0), &[1.0, 0.0, 1.0]);
let b = path.line_to(point(2.0, 0.0), &[2.0, 0.0, 1.0]);
let c = path.line_to(point(2.0, 4.0), &[3.0, 0.0, 1.0]);
let d = path.line_to(point(1.0, 4.0), &[4.0, 0.0, 1.0]);
path.end(true);
let e = path.begin(point(0.0, 1.0), &[0.0, 1.0, 2.0]);
let f = path.line_to(point(0.0, 3.0), &[0.0, 2.0, 2.0]);
let g = path.line_to(point(3.0, 3.0), &[0.0, 3.0, 2.0]);
let h = path.line_to(point(3.0, 1.0), &[0.0, 4.0, 2.0]);
path.end(true);
let path = path.build();
let mut tess = FillTessellator::new();
tess.tessellate_with_ids(
path.id_iter(),
&path,
Some(&path),
&FillOptions::default(),
&mut CheckVertexSources {
next_vertex: 0,
a,
b,
c,
d,
e,
f,
g,
h,
},
)
.unwrap();
struct CheckVertexSources {
next_vertex: u32,
a: EndpointId,
b: EndpointId,
c: EndpointId,
d: EndpointId,
e: EndpointId,
f: EndpointId,
g: EndpointId,
h: EndpointId,
}
impl GeometryBuilder for CheckVertexSources {
fn begin_geometry(&mut self) {}
fn end_geometry(&mut self) -> Count {
Count {
vertices: self.next_vertex,
indices: 0,
}
}
fn abort_geometry(&mut self) {}
fn add_triangle(&mut self, _: VertexId, _: VertexId, _: VertexId) {}
}
impl FillGeometryBuilder for CheckVertexSources {
fn add_fill_vertex(
&mut self,
mut vertex: FillVertex,
) -> Result<VertexId, GeometryBuilderError> {
let pos = vertex.position();
for src in vertex.sources() {
if eq(pos, point(1.0, 0.0)) {
assert!(at_endpoint(&src, self.a));
} else if eq(pos, point(2.0, 0.0)) {
assert!(at_endpoint(&src, self.b));
} else if eq(pos, point(2.0, 4.0)) {
assert!(at_endpoint(&src, self.c));
} else if eq(pos, point(1.0, 4.0)) {
assert!(at_endpoint(&src, self.d));
} else if eq(pos, point(0.0, 1.0)) {
assert!(at_endpoint(&src, self.e));
} else if eq(pos, point(0.0, 3.0)) {
assert!(at_endpoint(&src, self.f));
} else if eq(pos, point(3.0, 3.0)) {
assert!(at_endpoint(&src, self.g));
} else if eq(pos, point(3.0, 1.0)) {
assert!(at_endpoint(&src, self.h));
} else if eq(pos, point(1.0, 1.0)) {
assert!(
on_edge(&src, self.h, self.e, 2.0 / 3.0)
|| on_edge(&src, self.d, self.a, 3.0 / 4.0)
);
} else if eq(pos, point(2.0, 1.0)) {
assert!(
on_edge(&src, self.h, self.e, 1.0 / 3.0)
|| on_edge(&src, self.b, self.c, 1.0 / 4.0)
);
} else if eq(pos, point(1.0, 3.0)) {
assert!(
on_edge(&src, self.f, self.g, 1.0 / 3.0)
|| on_edge(&src, self.d, self.a, 1.0 / 4.0)
);
} else if eq(pos, point(2.0, 3.0)) {
assert!(
on_edge(&src, self.f, self.g, 2.0 / 3.0)
|| on_edge(&src, self.b, self.c, 3.0 / 4.0)
);
} else {
panic!()
}
}
fn assert_attr(a: &[f32], b: &[f32]) {
for i in 0..a.len() {
let are_equal = (a[i] - b[i]).abs() < 0.001;
if !are_equal {
println!("{:?} != {:?}", a, b);
}
assert!(are_equal);
}
assert_eq!(a.len(), b.len());
}
let pos = vertex.position();
let attribs = vertex.interpolated_attributes();
if eq(pos, point(1.0, 0.0)) {
assert_attr(attribs, &[1.0, 0.0, 1.0]);
} else if eq(pos, point(2.0, 0.0)) {
assert_attr(attribs, &[2.0, 0.0, 1.0]);
} else if eq(pos, point(2.0, 4.0)) {
assert_attr(attribs, &[3.0, 0.0, 1.0]);
} else if eq(pos, point(1.0, 4.0)) {
assert_attr(attribs, &[4.0, 0.0, 1.0]);
} else if eq(pos, point(0.0, 1.0)) {
assert_attr(attribs, &[0.0, 1.0, 2.0]);
} else if eq(pos, point(0.0, 3.0)) {
assert_attr(attribs, &[0.0, 2.0, 2.0]);
} else if eq(pos, point(3.0, 3.0)) {
assert_attr(attribs, &[0.0, 3.0, 2.0]);
} else if eq(pos, point(3.0, 1.0)) {
assert_attr(attribs, &[0.0, 4.0, 2.0]);
} else if eq(pos, point(1.0, 1.0)) {
assert_attr(attribs, &[0.875, 1.0, 1.5]);
} else if eq(pos, point(2.0, 1.0)) {
assert_attr(attribs, &[1.125, 1.5, 1.5]);
} else if eq(pos, point(1.0, 3.0)) {
assert_attr(attribs, &[1.625, 1.16666, 1.5]);
} else if eq(pos, point(2.0, 3.0)) {
assert_attr(attribs, &[1.375, 1.33333, 1.5]);
}
let id = self.next_vertex;
self.next_vertex += 1;
Ok(VertexId(id))
}
}
}
#[test]
fn fill_vertex_source_03() {
use crate::path::commands::PathCommands;
use crate::path::AttributeSlice;
// x---x
// \ /
// x <---
// / \
// x---x
//
// check that the attribute interpolation is weighted correctly at
// start events.
let endpoints: &[Point] = &[
point(0.0, 0.0),
point(2.0, 0.0),
point(1.0, 1.0),
point(0.0, 2.0),
point(2.0, 2.0),
point(1.0, 1.0),
];
let attributes = &[0.0, 0.0, 1.0, 0.0, 0.0, 2.0];
let mut cmds = PathCommands::builder();
cmds.begin(EndpointId(0));
cmds.line_to(EndpointId(1));
cmds.line_to(EndpointId(2));
cmds.end(true);
cmds.begin(EndpointId(3));
cmds.line_to(EndpointId(4));
cmds.line_to(EndpointId(5));
cmds.end(true);
let cmds = cmds.build();
let mut tess = FillTessellator::new();
tess.tessellate_with_ids(
cmds.iter(),
&(endpoints, endpoints),
Some(&AttributeSlice::new(attributes, 1)),
&FillOptions::default(),
&mut CheckVertexSources { next_vertex: 0 },
)
.unwrap();
struct CheckVertexSources {
next_vertex: u32,
}
impl GeometryBuilder for CheckVertexSources {
fn begin_geometry(&mut self) {}
fn end_geometry(&mut self) -> Count {
Count {
vertices: self.next_vertex,
indices: 0,
}
}
fn abort_geometry(&mut self) {}
fn add_triangle(&mut self, _: VertexId, _: VertexId, _: VertexId) {}
}
impl FillGeometryBuilder for CheckVertexSources {
fn add_fill_vertex(
&mut self,
mut vertex: FillVertex,
) -> Result<VertexId, GeometryBuilderError> {
if eq(vertex.position(), point(1.0, 1.0)) {
assert_eq!(vertex.interpolated_attributes(), &[1.5]);
assert_eq!(vertex.sources().count(), 2);
} else {
assert_eq!(vertex.interpolated_attributes(), &[0.0]);
assert_eq!(vertex.sources().count(), 1);
}
let id = self.next_vertex;
self.next_vertex += 1;
Ok(VertexId(id))
}
}
}
#[test]
fn fill_builder_vertex_source() {
let mut tess = FillTessellator::new();
let options = FillOptions::default();
let mut check = CheckVertexSources { next_vertex: 0 };
let mut builder = tess.builder(&options, &mut check);
assert_eq!(builder.begin(point(0.0, 0.0)), EndpointId(0));
assert_eq!(builder.line_to(point(1.0, 1.0)), EndpointId(1));
assert_eq!(builder.line_to(point(0.0, 2.0)), EndpointId(2));
builder.end(true);
builder.build().unwrap();
struct CheckVertexSources {
next_vertex: u32,
}
impl GeometryBuilder for CheckVertexSources {
fn begin_geometry(&mut self) {}
fn end_geometry(&mut self) -> Count {
Count {
vertices: self.next_vertex,
indices: 0,
}
}
fn abort_geometry(&mut self) {}
fn add_triangle(&mut self, _: VertexId, _: VertexId, _: VertexId) {}
}
impl FillGeometryBuilder for CheckVertexSources {
fn add_fill_vertex(
&mut self,
vertex: FillVertex,
) -> Result<VertexId, GeometryBuilderError> {
let pos = vertex.position();
for src in vertex.sources() {
if eq(pos, point(0.0, 0.0)) {
assert!(at_endpoint(&src, EndpointId(0)))
} else if eq(pos, point(1.0, 1.0)) {
assert!(at_endpoint(&src, EndpointId(1)))
} else if eq(pos, point(0.0, 2.0)) {
assert!(at_endpoint(&src, EndpointId(2)))
} else {
panic!()
}
}
let id = self.next_vertex;
self.next_vertex += 1;
Ok(VertexId(id))
}
}
}