File size: 277,800 Bytes
fe775b6 | 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 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 | // This header created by issuing: `echo "// This header created by issuing: \`$BASH_COMMAND\` $(echo "" | cat - LICENSE README.md | sed -e "s#^..*#\/\/ &#") $(echo "" | cat - SYS_Types.h SYS_Math.h VM_SSEFunc.h VM_SIMDFunc.h VM_SIMD.h UT_Array.h UT_ArrayImpl.h UT_SmallArray.h UT_FixedVector.h UT_ParallelUtil.h UT_BVH.h UT_BVHImpl.h UT_SolidAngle.h UT_Array.cpp UT_SolidAngle.cpp | sed -e "s/^#.*include *\".*$//g")" > ~/Repos/libigl/include/igl/FastWindingNumberForSoups.h`
// MIT License
// Copyright (c) 2018 Side Effects Software Inc.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// # Fast Winding Numbers for Soups
// https://github.com/alecjacobson/WindingNumber
// Implementation of the _ACM SIGGRAPH_ 2018 paper,
// "Fast Winding Numbers for Soups and Clouds"
// Gavin Barill¹, Neil Dickson², Ryan Schmidt³, David I.W. Levin¹, Alec Jacobson¹
// ¹University of Toronto, ²SideFX, ³Gradient Space
// _Note: this implementation is for triangle soups only, not point clouds._
// This version does _not_ depend on Intel TBB. Instead it depends on
// [libigl](https://github.com/libigl/libigl)'s simpler `igl::parallel_for` (which
// uses `std::thread`)
// <del>This code, as written, depends on Intel's Threading Building Blocks (TBB) library for parallelism, but it should be fairly easy to change it to use any other means of threading, since it only uses parallel for loops with simple partitioning.</del>
// The main class of interest is UT_SolidAngle and its init and computeSolidAngle functions, which you can use by including UT_SolidAngle.h, and whose implementation is mostly in UT_SolidAngle.cpp, using a 4-way bounding volume hierarchy (BVH) implemented in the UT_BVH.h and UT_BVHImpl.h headers. The rest of the files are mostly various supporting code. UT_SubtendedAngle, for computing angles subtended by 2D curves, can also be found in UT_SolidAngle.h and UT_SolidAngle.cpp .
// An example of very similar code and how to use it to create a geometry operator (SOP) in Houdini can be found in the HDK examples (toolkit/samples/SOP/SOP_WindingNumber) for Houdini 16.5.121 and later. Query points go in the first input and the mesh geometry goes in the second input.
// Create a single header using:
// echo "// This header created by issuing: \`$BASH_COMMAND\` $(echo "" | cat - LICENSE README.md | sed -e "s#^..*#\/\/ &#") $(echo "" | cat - SYS_Types.h SYS_Math.h VM_SSEFunc.h VM_SIMD.h UT_Array.h UT_ArrayImpl.h UT_SmallArray.h UT_FixedVector.h UT_ParallelUtil.h UT_BVH.h UT_BVHImpl.h UT_SolidAngle.h UT_Array.cpp UT_SolidAngle.cpp | sed -e "s/^#.*include *\".*$//g")"
/*
* Copyright (c) 2018 Side Effects Software Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* COMMENTS:
* Common type definitions.
*/
#pragma once
#ifndef __SYS_Types__
#define __SYS_Types__
/* Include system types */
#include <limits>
#include <type_traits>
#include <sys/types.h>
#include <stdint.h>
namespace igl { namespace FastWindingNumber {
/*
* Integer types
*/
typedef signed char int8;
typedef unsigned char uint8;
typedef short int16;
typedef unsigned short uint16;
typedef int int32;
typedef unsigned int uint32;
#ifndef MBSD
typedef unsigned int uint;
#endif
/*
* Avoid using uint64.
* The extra bit of precision is NOT worth the cost in pain and suffering
* induced by use of unsigned.
*/
#if defined(_WIN32)
typedef __int64 int64;
typedef unsigned __int64 uint64;
#elif defined(MBSD)
// On MBSD, int64/uint64 are also defined in the system headers so we must
// declare these in the same way or else we get conflicts.
typedef int64_t int64;
typedef uint64_t uint64;
#elif defined(AMD64)
typedef long int64;
typedef unsigned long uint64;
#else
typedef long long int64;
typedef unsigned long long uint64;
#endif
/// The problem with int64 is that it implies that it is a fixed 64-bit quantity
/// that is saved to disk. Therefore, we need another integral type for
/// indexing our arrays.
typedef int64 exint;
/// Mark function to be inlined. If this is done, taking the address of such
/// a function is not allowed.
#if defined(__GNUC__) || defined(__clang__)
#define SYS_FORCE_INLINE __attribute__ ((always_inline)) inline
#elif defined(_MSC_VER)
#define SYS_FORCE_INLINE __forceinline
#else
#define SYS_FORCE_INLINE inline
#endif
/// Floating Point Types
typedef float fpreal32;
typedef double fpreal64;
/// SYS_FPRealUnionT for type-safe casting with integral types
template <typename T>
union SYS_FPRealUnionT;
template <>
union SYS_FPRealUnionT<fpreal32>
{
typedef int32 int_type;
typedef uint32 uint_type;
typedef fpreal32 fpreal_type;
enum {
EXPONENT_BITS = 8,
MANTISSA_BITS = 23,
EXPONENT_BIAS = 127 };
int_type ival;
uint_type uval;
fpreal_type fval;
struct
{
uint_type mantissa_val: 23;
uint_type exponent_val: 8;
uint_type sign_val: 1;
};
};
template <>
union SYS_FPRealUnionT<fpreal64>
{
typedef int64 int_type;
typedef uint64 uint_type;
typedef fpreal64 fpreal_type;
enum {
EXPONENT_BITS = 11,
MANTISSA_BITS = 52,
EXPONENT_BIAS = 1023 };
int_type ival;
uint_type uval;
fpreal_type fval;
struct
{
uint_type mantissa_val: 52;
uint_type exponent_val: 11;
uint_type sign_val: 1;
};
};
typedef union SYS_FPRealUnionT<fpreal32> SYS_FPRealUnionF;
typedef union SYS_FPRealUnionT<fpreal64> SYS_FPRealUnionD;
/// Asserts are disabled
/// @{
#define UT_ASSERT_P(ZZ) ((void)0)
#define UT_ASSERT(ZZ) ((void)0)
#define UT_ASSERT_MSG_P(ZZ, MM) ((void)0)
#define UT_ASSERT_MSG(ZZ, MM) ((void)0)
/// @}
}}
#endif
/*
* Copyright (c) 2018 Side Effects Software Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* COMMENTS:
* Miscellaneous math functions.
*/
#pragma once
#ifndef __SYS_Math__
#define __SYS_Math__
#include <float.h>
#include <limits>
#include <math.h>
namespace igl { namespace FastWindingNumber {
// NOTE:
// These have been carefully written so that in the case of equality
// we always return the first parameter. This is so that NANs in
// in the second parameter are suppressed.
#define h_min(a, b) (((a) > (b)) ? (b) : (a))
#define h_max(a, b) (((a) < (b)) ? (b) : (a))
// DO NOT CHANGE THE ABOVE WITHOUT READING THE COMMENT
#define h_abs(a) (((a) > 0) ? (a) : -(a))
static constexpr inline int16 SYSmin(int16 a, int16 b) { return h_min(a,b); }
static constexpr inline int16 SYSmax(int16 a, int16 b) { return h_max(a,b); }
static constexpr inline int16 SYSabs(int16 a) { return h_abs(a); }
static constexpr inline int32 SYSmin(int32 a, int32 b) { return h_min(a,b); }
static constexpr inline int32 SYSmax(int32 a, int32 b) { return h_max(a,b); }
static constexpr inline int32 SYSabs(int32 a) { return h_abs(a); }
static constexpr inline int64 SYSmin(int64 a, int64 b) { return h_min(a,b); }
static constexpr inline int64 SYSmax(int64 a, int64 b) { return h_max(a,b); }
static constexpr inline int64 SYSmin(int32 a, int64 b) { return h_min(a,b); }
static constexpr inline int64 SYSmax(int32 a, int64 b) { return h_max(a,b); }
static constexpr inline int64 SYSmin(int64 a, int32 b) { return h_min(a,b); }
static constexpr inline int64 SYSmax(int64 a, int32 b) { return h_max(a,b); }
static constexpr inline int64 SYSabs(int64 a) { return h_abs(a); }
static constexpr inline uint16 SYSmin(uint16 a, uint16 b) { return h_min(a,b); }
static constexpr inline uint16 SYSmax(uint16 a, uint16 b) { return h_max(a,b); }
static constexpr inline uint32 SYSmin(uint32 a, uint32 b) { return h_min(a,b); }
static constexpr inline uint32 SYSmax(uint32 a, uint32 b) { return h_max(a,b); }
static constexpr inline uint64 SYSmin(uint64 a, uint64 b) { return h_min(a,b); }
static constexpr inline uint64 SYSmax(uint64 a, uint64 b) { return h_max(a,b); }
static constexpr inline fpreal32 SYSmin(fpreal32 a, fpreal32 b) { return h_min(a,b); }
static constexpr inline fpreal32 SYSmax(fpreal32 a, fpreal32 b) { return h_max(a,b); }
static constexpr inline fpreal64 SYSmin(fpreal64 a, fpreal64 b) { return h_min(a,b); }
static constexpr inline fpreal64 SYSmax(fpreal64 a, fpreal64 b) { return h_max(a,b); }
// Some systems have size_t as a seperate type from uint. Some don't.
#if (defined(LINUX) && defined(IA64)) || defined(MBSD)
static constexpr inline size_t SYSmin(size_t a, size_t b) { return h_min(a,b); }
static constexpr inline size_t SYSmax(size_t a, size_t b) { return h_max(a,b); }
#endif
#undef h_min
#undef h_max
#undef h_abs
#define h_clamp(val, min, max, tol) \
((val <= min+tol) ? min : ((val >= max-tol) ? max : val))
static constexpr inline int
SYSclamp(int v, int min, int max)
{ return h_clamp(v, min, max, 0); }
static constexpr inline uint
SYSclamp(uint v, uint min, uint max)
{ return h_clamp(v, min, max, 0); }
static constexpr inline int64
SYSclamp(int64 v, int64 min, int64 max)
{ return h_clamp(v, min, max, int64(0)); }
static constexpr inline uint64
SYSclamp(uint64 v, uint64 min, uint64 max)
{ return h_clamp(v, min, max, uint64(0)); }
static constexpr inline fpreal32
SYSclamp(fpreal32 v, fpreal32 min, fpreal32 max, fpreal32 tol=(fpreal32)0)
{ return h_clamp(v, min, max, tol); }
static constexpr inline fpreal64
SYSclamp(fpreal64 v, fpreal64 min, fpreal64 max, fpreal64 tol=(fpreal64)0)
{ return h_clamp(v, min, max, tol); }
#undef h_clamp
static inline fpreal64 SYSsqrt(fpreal64 arg)
{ return ::sqrt(arg); }
static inline fpreal32 SYSsqrt(fpreal32 arg)
{ return ::sqrtf(arg); }
static inline fpreal64 SYSatan2(fpreal64 a, fpreal64 b)
{ return ::atan2(a, b); }
static inline fpreal32 SYSatan2(fpreal32 a, fpreal32 b)
{ return ::atan2(a, b); }
static inline fpreal32 SYSabs(fpreal32 a) { return ::fabsf(a); }
static inline fpreal64 SYSabs(fpreal64 a) { return ::fabs(a); }
}}
#endif
/*
* Copyright (c) 2018 Side Effects Software Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* COMMENTS:
* SIMD wrapper functions for SSE instructions
*/
#pragma once
#ifdef __SSE__
#ifndef __VM_SSEFunc__
#define __VM_SSEFunc__
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4799)
#endif
#define CPU_HAS_SIMD_INSTR 1
#define VM_SSE_STYLE 1
#include <emmintrin.h>
#if defined(__SSE4_1__)
#define VM_SSE41_STYLE 1
#include <smmintrin.h>
#endif
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
namespace igl { namespace FastWindingNumber {
typedef __m128 v4sf;
typedef __m128i v4si;
// Plain casting (no conversion)
// MSVC has problems casting between __m128 and __m128i, so we implement a
// custom casting routine specifically for windows.
#if defined(_MSC_VER)
static SYS_FORCE_INLINE v4sf
vm_v4sf(const v4si &a)
{
union {
v4si ival;
v4sf fval;
};
ival = a;
return fval;
}
static SYS_FORCE_INLINE v4si
vm_v4si(const v4sf &a)
{
union {
v4si ival;
v4sf fval;
};
fval = a;
return ival;
}
#define V4SF(A) vm_v4sf(A)
#define V4SI(A) vm_v4si(A)
#else
#define V4SF(A) (v4sf)A
#define V4SI(A) (v4si)A
#endif
#define VM_SHUFFLE_MASK(a0,a1, b0,b1) ((b1)<<6|(b0)<<4 | (a1)<<2|(a0))
template <int mask>
static SYS_FORCE_INLINE v4sf
vm_shuffle(const v4sf &a, const v4sf &b)
{
return _mm_shuffle_ps(a, b, mask);
}
template <int mask>
static SYS_FORCE_INLINE v4si
vm_shuffle(const v4si &a, const v4si &b)
{
return V4SI(_mm_shuffle_ps(V4SF(a), V4SF(b), mask));
}
template <int A, int B, int C, int D, typename T>
static SYS_FORCE_INLINE T
vm_shuffle(const T &a, const T &b)
{
return vm_shuffle<VM_SHUFFLE_MASK(A,B,C,D)>(a, b);
}
template <int mask, typename T>
static SYS_FORCE_INLINE T
vm_shuffle(const T &a)
{
return vm_shuffle<mask>(a, a);
}
template <int A, int B, int C, int D, typename T>
static SYS_FORCE_INLINE T
vm_shuffle(const T &a)
{
return vm_shuffle<A,B,C,D>(a, a);
}
#if defined(VM_SSE41_STYLE)
static SYS_FORCE_INLINE v4si
vm_insert(const v4si v, int32 a, int n)
{
switch (n)
{
case 0: return _mm_insert_epi32(v, a, 0);
case 1: return _mm_insert_epi32(v, a, 1);
case 2: return _mm_insert_epi32(v, a, 2);
case 3: return _mm_insert_epi32(v, a, 3);
}
return v;
}
static SYS_FORCE_INLINE v4sf
vm_insert(const v4sf v, float a, int n)
{
switch (n)
{
case 0: return _mm_insert_ps(v, _mm_set_ss(a), _MM_MK_INSERTPS_NDX(0,0,0));
case 1: return _mm_insert_ps(v, _mm_set_ss(a), _MM_MK_INSERTPS_NDX(0,1,0));
case 2: return _mm_insert_ps(v, _mm_set_ss(a), _MM_MK_INSERTPS_NDX(0,2,0));
case 3: return _mm_insert_ps(v, _mm_set_ss(a), _MM_MK_INSERTPS_NDX(0,3,0));
}
return v;
}
static SYS_FORCE_INLINE int
vm_extract(const v4si v, int n)
{
switch (n)
{
case 0: return _mm_extract_epi32(v, 0);
case 1: return _mm_extract_epi32(v, 1);
case 2: return _mm_extract_epi32(v, 2);
case 3: return _mm_extract_epi32(v, 3);
}
return 0;
}
static SYS_FORCE_INLINE float
vm_extract(const v4sf v, int n)
{
SYS_FPRealUnionF tmp;
switch (n)
{
case 0: tmp.ival = _mm_extract_ps(v, 0); break;
case 1: tmp.ival = _mm_extract_ps(v, 1); break;
case 2: tmp.ival = _mm_extract_ps(v, 2); break;
case 3: tmp.ival = _mm_extract_ps(v, 3); break;
}
return tmp.fval;
}
#else
static SYS_FORCE_INLINE v4si
vm_insert(const v4si v, int32 a, int n)
{
union { v4si vector; int32 comp[4]; };
vector = v;
comp[n] = a;
return vector;
}
static SYS_FORCE_INLINE v4sf
vm_insert(const v4sf v, float a, int n)
{
union { v4sf vector; float comp[4]; };
vector = v;
comp[n] = a;
return vector;
}
static SYS_FORCE_INLINE int
vm_extract(const v4si v, int n)
{
union { v4si vector; int32 comp[4]; };
vector = v;
return comp[n];
}
static SYS_FORCE_INLINE float
vm_extract(const v4sf v, int n)
{
union { v4sf vector; float comp[4]; };
vector = v;
return comp[n];
}
#endif
static SYS_FORCE_INLINE v4sf
vm_splats(float a)
{
return _mm_set1_ps(a);
}
static SYS_FORCE_INLINE v4si
vm_splats(uint32 a)
{
SYS_FPRealUnionF tmp;
tmp.uval = a;
return V4SI(vm_splats(tmp.fval));
}
static SYS_FORCE_INLINE v4si
vm_splats(int32 a)
{
SYS_FPRealUnionF tmp;
tmp.ival = a;
return V4SI(vm_splats(tmp.fval));
}
static SYS_FORCE_INLINE v4sf
vm_splats(float a, float b, float c, float d)
{
return vm_shuffle<0,2,0,2>(
vm_shuffle<0>(_mm_set_ss(a), _mm_set_ss(b)),
vm_shuffle<0>(_mm_set_ss(c), _mm_set_ss(d)));
}
static SYS_FORCE_INLINE v4si
vm_splats(uint32 a, uint32 b, uint32 c, uint32 d)
{
SYS_FPRealUnionF af, bf, cf, df;
af.uval = a;
bf.uval = b;
cf.uval = c;
df.uval = d;
return V4SI(vm_splats(af.fval, bf.fval, cf.fval, df.fval));
}
static SYS_FORCE_INLINE v4si
vm_splats(int32 a, int32 b, int32 c, int32 d)
{
SYS_FPRealUnionF af, bf, cf, df;
af.ival = a;
bf.ival = b;
cf.ival = c;
df.ival = d;
return V4SI(vm_splats(af.fval, bf.fval, cf.fval, df.fval));
}
static SYS_FORCE_INLINE v4si
vm_load(const int32 v[4])
{
return V4SI(_mm_loadu_ps((const float *)v));
}
static SYS_FORCE_INLINE v4sf
vm_load(const float v[4])
{
return _mm_loadu_ps(v);
}
static SYS_FORCE_INLINE void
vm_store(float dst[4], v4sf value)
{
_mm_storeu_ps(dst, value);
}
static SYS_FORCE_INLINE v4sf
vm_negate(v4sf a)
{
return _mm_sub_ps(_mm_setzero_ps(), a);
}
static SYS_FORCE_INLINE v4sf
vm_abs(v4sf a)
{
return _mm_max_ps(a, vm_negate(a));
}
static SYS_FORCE_INLINE v4sf
vm_fdiv(v4sf a, v4sf b)
{
return _mm_mul_ps(a, _mm_rcp_ps(b));
}
static SYS_FORCE_INLINE v4sf
vm_fsqrt(v4sf a)
{
return _mm_rcp_ps(_mm_rsqrt_ps(a));
}
static SYS_FORCE_INLINE v4sf
vm_madd(v4sf a, v4sf b, v4sf c)
{
return _mm_add_ps(_mm_mul_ps(a, b), c);
}
static const v4si theSSETrue = vm_splats(0xFFFFFFFF);
static SYS_FORCE_INLINE bool
vm_allbits(const v4si &a)
{
return _mm_movemask_ps(V4SF(_mm_cmpeq_epi32(a, theSSETrue))) == 0xF;
}
#define VM_EXTRACT vm_extract
#define VM_INSERT vm_insert
#define VM_SPLATS vm_splats
#define VM_LOAD vm_load
#define VM_STORE vm_store
#define VM_CMPLT(A,B) V4SI(_mm_cmplt_ps(A,B))
#define VM_CMPLE(A,B) V4SI(_mm_cmple_ps(A,B))
#define VM_CMPGT(A,B) V4SI(_mm_cmpgt_ps(A,B))
#define VM_CMPGE(A,B) V4SI(_mm_cmpge_ps(A,B))
#define VM_CMPEQ(A,B) V4SI(_mm_cmpeq_ps(A,B))
#define VM_CMPNE(A,B) V4SI(_mm_cmpneq_ps(A,B))
#define VM_ICMPLT _mm_cmplt_epi32
#define VM_ICMPGT _mm_cmpgt_epi32
#define VM_ICMPEQ _mm_cmpeq_epi32
#define VM_IADD _mm_add_epi32
#define VM_ISUB _mm_sub_epi32
#define VM_ADD _mm_add_ps
#define VM_SUB _mm_sub_ps
#define VM_MUL _mm_mul_ps
#define VM_DIV _mm_div_ps
#define VM_SQRT _mm_sqrt_ps
#define VM_ISQRT _mm_rsqrt_ps
#define VM_INVERT _mm_rcp_ps
#define VM_ABS vm_abs
#define VM_FDIV vm_fdiv
#define VM_NEG vm_negate
#define VM_FSQRT vm_fsqrt
#define VM_MADD vm_madd
#define VM_MIN _mm_min_ps
#define VM_MAX _mm_max_ps
#define VM_AND _mm_and_si128
#define VM_ANDNOT _mm_andnot_si128
#define VM_OR _mm_or_si128
#define VM_XOR _mm_xor_si128
#define VM_ALLBITS vm_allbits
#define VM_SHUFFLE vm_shuffle
// Integer to float conversions
#define VM_SSE_ROUND_MASK 0x6000
#define VM_SSE_ROUND_ZERO 0x6000
#define VM_SSE_ROUND_UP 0x4000
#define VM_SSE_ROUND_DOWN 0x2000
#define VM_SSE_ROUND_NEAR 0x0000
#define GETROUND() (_mm_getcsr()&VM_SSE_ROUND_MASK)
#define SETROUND(x) (_mm_setcsr(x|(_mm_getcsr()&~VM_SSE_ROUND_MASK)))
// The P functions must be invoked before FLOOR, the E functions invoked
// afterwards to reset the state.
#define VM_P_FLOOR() uint rounding = GETROUND(); \
SETROUND(VM_SSE_ROUND_DOWN);
#define VM_FLOOR _mm_cvtps_epi32
#define VM_INT _mm_cvttps_epi32
#define VM_E_FLOOR() SETROUND(rounding);
// Float to integer conversion
#define VM_IFLOAT _mm_cvtepi32_ps
}}
#endif
#endif
#pragma once
#ifndef __SSE__
#ifndef __VM_SIMDFunc__
#define __VM_SIMDFunc__
#include <cmath>
namespace igl { namespace FastWindingNumber {
struct v4si {
int32 v[4];
};
struct v4sf {
float v[4];
};
static SYS_FORCE_INLINE v4sf V4SF(const v4si &v) {
static_assert(sizeof(v4si) == sizeof(v4sf) && alignof(v4si) == alignof(v4sf), "v4si and v4sf must be compatible");
return *(const v4sf*)&v;
}
static SYS_FORCE_INLINE v4si V4SI(const v4sf &v) {
static_assert(sizeof(v4si) == sizeof(v4sf) && alignof(v4si) == alignof(v4sf), "v4si and v4sf must be compatible");
return *(const v4si*)&v;
}
static SYS_FORCE_INLINE int32 conditionMask(bool c) {
return c ? int32(0xFFFFFFFF) : 0;
}
static SYS_FORCE_INLINE v4sf
VM_SPLATS(float f) {
return v4sf{{f, f, f, f}};
}
static SYS_FORCE_INLINE v4si
VM_SPLATS(uint32 i) {
return v4si{{int32(i), int32(i), int32(i), int32(i)}};
}
static SYS_FORCE_INLINE v4si
VM_SPLATS(int32 i) {
return v4si{{i, i, i, i}};
}
static SYS_FORCE_INLINE v4sf
VM_SPLATS(float a, float b, float c, float d) {
return v4sf{{a, b, c, d}};
}
static SYS_FORCE_INLINE v4si
VM_SPLATS(uint32 a, uint32 b, uint32 c, uint32 d) {
return v4si{{int32(a), int32(b), int32(c), int32(d)}};
}
static SYS_FORCE_INLINE v4si
VM_SPLATS(int32 a, int32 b, int32 c, int32 d) {
return v4si{{a, b, c, d}};
}
static SYS_FORCE_INLINE v4si
VM_LOAD(const int32 v[4]) {
return v4si{{v[0], v[1], v[2], v[3]}};
}
static SYS_FORCE_INLINE v4sf
VM_LOAD(const float v[4]) {
return v4sf{{v[0], v[1], v[2], v[3]}};
}
static inline v4si VM_ICMPEQ(v4si a, v4si b) {
return v4si{{
conditionMask(a.v[0] == b.v[0]),
conditionMask(a.v[1] == b.v[1]),
conditionMask(a.v[2] == b.v[2]),
conditionMask(a.v[3] == b.v[3])
}};
}
static inline v4si VM_ICMPGT(v4si a, v4si b) {
return v4si{{
conditionMask(a.v[0] > b.v[0]),
conditionMask(a.v[1] > b.v[1]),
conditionMask(a.v[2] > b.v[2]),
conditionMask(a.v[3] > b.v[3])
}};
}
static inline v4si VM_ICMPLT(v4si a, v4si b) {
return v4si{{
conditionMask(a.v[0] < b.v[0]),
conditionMask(a.v[1] < b.v[1]),
conditionMask(a.v[2] < b.v[2]),
conditionMask(a.v[3] < b.v[3])
}};
}
static inline v4si VM_IADD(v4si a, v4si b) {
return v4si{{
(a.v[0] + b.v[0]),
(a.v[1] + b.v[1]),
(a.v[2] + b.v[2]),
(a.v[3] + b.v[3])
}};
}
static inline v4si VM_ISUB(v4si a, v4si b) {
return v4si{{
(a.v[0] - b.v[0]),
(a.v[1] - b.v[1]),
(a.v[2] - b.v[2]),
(a.v[3] - b.v[3])
}};
}
static inline v4si VM_OR(v4si a, v4si b) {
return v4si{{
(a.v[0] | b.v[0]),
(a.v[1] | b.v[1]),
(a.v[2] | b.v[2]),
(a.v[3] | b.v[3])
}};
}
static inline v4si VM_AND(v4si a, v4si b) {
return v4si{{
(a.v[0] & b.v[0]),
(a.v[1] & b.v[1]),
(a.v[2] & b.v[2]),
(a.v[3] & b.v[3])
}};
}
static inline v4si VM_ANDNOT(v4si a, v4si b) {
return v4si{{
((~a.v[0]) & b.v[0]),
((~a.v[1]) & b.v[1]),
((~a.v[2]) & b.v[2]),
((~a.v[3]) & b.v[3])
}};
}
static inline v4si VM_XOR(v4si a, v4si b) {
return v4si{{
(a.v[0] ^ b.v[0]),
(a.v[1] ^ b.v[1]),
(a.v[2] ^ b.v[2]),
(a.v[3] ^ b.v[3])
}};
}
static SYS_FORCE_INLINE int
VM_EXTRACT(const v4si v, int index) {
return v.v[index];
}
static SYS_FORCE_INLINE float
VM_EXTRACT(const v4sf v, int index) {
return v.v[index];
}
static SYS_FORCE_INLINE v4si
VM_INSERT(v4si v, int32 value, int index) {
v.v[index] = value;
return v;
}
static SYS_FORCE_INLINE v4sf
VM_INSERT(v4sf v, float value, int index) {
v.v[index] = value;
return v;
}
static inline v4si VM_CMPEQ(v4sf a, v4sf b) {
return v4si{{
conditionMask(a.v[0] == b.v[0]),
conditionMask(a.v[1] == b.v[1]),
conditionMask(a.v[2] == b.v[2]),
conditionMask(a.v[3] == b.v[3])
}};
}
static inline v4si VM_CMPNE(v4sf a, v4sf b) {
return v4si{{
conditionMask(a.v[0] != b.v[0]),
conditionMask(a.v[1] != b.v[1]),
conditionMask(a.v[2] != b.v[2]),
conditionMask(a.v[3] != b.v[3])
}};
}
static inline v4si VM_CMPGT(v4sf a, v4sf b) {
return v4si{{
conditionMask(a.v[0] > b.v[0]),
conditionMask(a.v[1] > b.v[1]),
conditionMask(a.v[2] > b.v[2]),
conditionMask(a.v[3] > b.v[3])
}};
}
static inline v4si VM_CMPLT(v4sf a, v4sf b) {
return v4si{{
conditionMask(a.v[0] < b.v[0]),
conditionMask(a.v[1] < b.v[1]),
conditionMask(a.v[2] < b.v[2]),
conditionMask(a.v[3] < b.v[3])
}};
}
static inline v4si VM_CMPGE(v4sf a, v4sf b) {
return v4si{{
conditionMask(a.v[0] >= b.v[0]),
conditionMask(a.v[1] >= b.v[1]),
conditionMask(a.v[2] >= b.v[2]),
conditionMask(a.v[3] >= b.v[3])
}};
}
static inline v4si VM_CMPLE(v4sf a, v4sf b) {
return v4si{{
conditionMask(a.v[0] <= b.v[0]),
conditionMask(a.v[1] <= b.v[1]),
conditionMask(a.v[2] <= b.v[2]),
conditionMask(a.v[3] <= b.v[3])
}};
}
static inline v4sf VM_ADD(v4sf a, v4sf b) {
return v4sf{{
(a.v[0] + b.v[0]),
(a.v[1] + b.v[1]),
(a.v[2] + b.v[2]),
(a.v[3] + b.v[3])
}};
}
static inline v4sf VM_SUB(v4sf a, v4sf b) {
return v4sf{{
(a.v[0] - b.v[0]),
(a.v[1] - b.v[1]),
(a.v[2] - b.v[2]),
(a.v[3] - b.v[3])
}};
}
static inline v4sf VM_NEG(v4sf a) {
return v4sf{{
(-a.v[0]),
(-a.v[1]),
(-a.v[2]),
(-a.v[3])
}};
}
static inline v4sf VM_MUL(v4sf a, v4sf b) {
return v4sf{{
(a.v[0] * b.v[0]),
(a.v[1] * b.v[1]),
(a.v[2] * b.v[2]),
(a.v[3] * b.v[3])
}};
}
static inline v4sf VM_DIV(v4sf a, v4sf b) {
return v4sf{{
(a.v[0] / b.v[0]),
(a.v[1] / b.v[1]),
(a.v[2] / b.v[2]),
(a.v[3] / b.v[3])
}};
}
static inline v4sf VM_MADD(v4sf a, v4sf b, v4sf c) {
return v4sf{{
(a.v[0] * b.v[0]) + c.v[0],
(a.v[1] * b.v[1]) + c.v[1],
(a.v[2] * b.v[2]) + c.v[2],
(a.v[3] * b.v[3]) + c.v[3]
}};
}
static inline v4sf VM_ABS(v4sf a) {
return v4sf{{
(a.v[0] < 0) ? -a.v[0] : a.v[0],
(a.v[1] < 0) ? -a.v[1] : a.v[1],
(a.v[2] < 0) ? -a.v[2] : a.v[2],
(a.v[3] < 0) ? -a.v[3] : a.v[3]
}};
}
static inline v4sf VM_MAX(v4sf a, v4sf b) {
return v4sf{{
(a.v[0] < b.v[0]) ? b.v[0] : a.v[0],
(a.v[1] < b.v[1]) ? b.v[1] : a.v[1],
(a.v[2] < b.v[2]) ? b.v[2] : a.v[2],
(a.v[3] < b.v[3]) ? b.v[3] : a.v[3]
}};
}
static inline v4sf VM_MIN(v4sf a, v4sf b) {
return v4sf{{
(a.v[0] > b.v[0]) ? b.v[0] : a.v[0],
(a.v[1] > b.v[1]) ? b.v[1] : a.v[1],
(a.v[2] > b.v[2]) ? b.v[2] : a.v[2],
(a.v[3] > b.v[3]) ? b.v[3] : a.v[3]
}};
}
static inline v4sf VM_INVERT(v4sf a) {
return v4sf{{
(1.0f/a.v[0]),
(1.0f/a.v[1]),
(1.0f/a.v[2]),
(1.0f/a.v[3])
}};
}
static inline v4sf VM_SQRT(v4sf a) {
return v4sf{{
std::sqrt(a.v[0]),
std::sqrt(a.v[1]),
std::sqrt(a.v[2]),
std::sqrt(a.v[3])
}};
}
static inline v4si VM_INT(v4sf a) {
return v4si{{
int32(a.v[0]),
int32(a.v[1]),
int32(a.v[2]),
int32(a.v[3])
}};
}
static inline v4sf VM_IFLOAT(v4si a) {
return v4sf{{
float(a.v[0]),
float(a.v[1]),
float(a.v[2]),
float(a.v[3])
}};
}
static SYS_FORCE_INLINE void VM_P_FLOOR() {}
static SYS_FORCE_INLINE int32 singleIntFloor(float f) {
// Casting to int32 usually truncates toward zero, instead of rounding down,
// so subtract one if the result is above f.
int32 i = int32(f);
i -= (float(i) > f);
return i;
}
static inline v4si VM_FLOOR(v4sf a) {
return v4si{{
singleIntFloor(a.v[0]),
singleIntFloor(a.v[1]),
singleIntFloor(a.v[2]),
singleIntFloor(a.v[3])
}};
}
static SYS_FORCE_INLINE void VM_E_FLOOR() {}
static SYS_FORCE_INLINE bool vm_allbits(v4si a) {
return (
(a.v[0] == -1) &&
(a.v[1] == -1) &&
(a.v[2] == -1) &&
(a.v[3] == -1)
);
}
int SYS_FORCE_INLINE _mm_movemask_ps(const v4si& v) {
return (
int(v.v[0] < 0) |
(int(v.v[1] < 0)<<1) |
(int(v.v[2] < 0)<<2) |
(int(v.v[3] < 0)<<3)
);
}
int SYS_FORCE_INLINE _mm_movemask_ps(const v4sf& v) {
// Use std::signbit just in case it needs to distinguish between +0 and -0
// or between positive and negative NaN values (e.g. these could really
// be integers instead of floats).
return (
int(std::signbit(v.v[0])) |
(int(std::signbit(v.v[1]))<<1) |
(int(std::signbit(v.v[2]))<<2) |
(int(std::signbit(v.v[3]))<<3)
);
}
}}
#endif
#endif
/*
* Copyright (c) 2018 Side Effects Software Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* COMMENTS:
* SIMD wrapper classes for 4 floats or 4 ints
*/
#pragma once
#ifndef __HDK_VM_SIMD__
#define __HDK_VM_SIMD__
#include <cstdint>
//#define FORCE_NON_SIMD
namespace igl { namespace FastWindingNumber {
class v4uf;
class v4uu {
public:
SYS_FORCE_INLINE v4uu() {}
SYS_FORCE_INLINE v4uu(const v4si &v) : vector(v) {}
SYS_FORCE_INLINE v4uu(const v4uu &v) : vector(v.vector) {}
explicit SYS_FORCE_INLINE v4uu(int32 v) { vector = VM_SPLATS(v); }
explicit SYS_FORCE_INLINE v4uu(const int32 v[4])
{ vector = VM_LOAD(v); }
SYS_FORCE_INLINE v4uu(int32 a, int32 b, int32 c, int32 d)
{ vector = VM_SPLATS(a, b, c, d); }
// Assignment
SYS_FORCE_INLINE v4uu operator=(int32 v)
{ vector = v4uu(v).vector; return *this; }
SYS_FORCE_INLINE v4uu operator=(v4si v)
{ vector = v; return *this; }
SYS_FORCE_INLINE v4uu operator=(const v4uu &v)
{ vector = v.vector; return *this; }
SYS_FORCE_INLINE void condAssign(const v4uu &val, const v4uu &c)
{ *this = (c & val) | ((!c) & *this); }
// Comparison
SYS_FORCE_INLINE v4uu operator == (const v4uu &v) const
{ return v4uu(VM_ICMPEQ(vector, v.vector)); }
SYS_FORCE_INLINE v4uu operator != (const v4uu &v) const
{ return ~(*this == v); }
SYS_FORCE_INLINE v4uu operator > (const v4uu &v) const
{ return v4uu(VM_ICMPGT(vector, v.vector)); }
SYS_FORCE_INLINE v4uu operator < (const v4uu &v) const
{ return v4uu(VM_ICMPLT(vector, v.vector)); }
SYS_FORCE_INLINE v4uu operator >= (const v4uu &v) const
{ return ~(*this < v); }
SYS_FORCE_INLINE v4uu operator <= (const v4uu &v) const
{ return ~(*this > v); }
SYS_FORCE_INLINE v4uu operator == (int32 v) const { return *this == v4uu(v); }
SYS_FORCE_INLINE v4uu operator != (int32 v) const { return *this != v4uu(v); }
SYS_FORCE_INLINE v4uu operator > (int32 v) const { return *this > v4uu(v); }
SYS_FORCE_INLINE v4uu operator < (int32 v) const { return *this < v4uu(v); }
SYS_FORCE_INLINE v4uu operator >= (int32 v) const { return *this >= v4uu(v); }
SYS_FORCE_INLINE v4uu operator <= (int32 v) const { return *this <= v4uu(v); }
// Basic math
SYS_FORCE_INLINE v4uu operator+(const v4uu &r) const
{ return v4uu(VM_IADD(vector, r.vector)); }
SYS_FORCE_INLINE v4uu operator-(const v4uu &r) const
{ return v4uu(VM_ISUB(vector, r.vector)); }
SYS_FORCE_INLINE v4uu operator+=(const v4uu &r) { return (*this = *this + r); }
SYS_FORCE_INLINE v4uu operator-=(const v4uu &r) { return (*this = *this - r); }
SYS_FORCE_INLINE v4uu operator+(int32 r) const { return *this + v4uu(r); }
SYS_FORCE_INLINE v4uu operator-(int32 r) const { return *this - v4uu(r); }
SYS_FORCE_INLINE v4uu operator+=(int32 r) { return (*this = *this + r); }
SYS_FORCE_INLINE v4uu operator-=(int32 r) { return (*this = *this - r); }
// logical/bitwise
SYS_FORCE_INLINE v4uu operator||(const v4uu &r) const
{ return v4uu(VM_OR(vector, r.vector)); }
SYS_FORCE_INLINE v4uu operator&&(const v4uu &r) const
{ return v4uu(VM_AND(vector, r.vector)); }
SYS_FORCE_INLINE v4uu operator^(const v4uu &r) const
{ return v4uu(VM_XOR(vector, r.vector)); }
SYS_FORCE_INLINE v4uu operator!() const
{ return *this == v4uu(0); }
SYS_FORCE_INLINE v4uu operator|(const v4uu &r) const { return *this || r; }
SYS_FORCE_INLINE v4uu operator&(const v4uu &r) const { return *this && r; }
SYS_FORCE_INLINE v4uu operator~() const
{ return *this ^ v4uu(0xFFFFFFFF); }
// component
SYS_FORCE_INLINE int32 operator[](int idx) const { return VM_EXTRACT(vector, idx); }
SYS_FORCE_INLINE void setComp(int idx, int32 v) { vector = VM_INSERT(vector, v, idx); }
v4uf toFloat() const;
public:
v4si vector;
};
class v4uf {
public:
SYS_FORCE_INLINE v4uf() {}
SYS_FORCE_INLINE v4uf(const v4sf &v) : vector(v) {}
SYS_FORCE_INLINE v4uf(const v4uf &v) : vector(v.vector) {}
explicit SYS_FORCE_INLINE v4uf(float v) { vector = VM_SPLATS(v); }
explicit SYS_FORCE_INLINE v4uf(const float v[4])
{ vector = VM_LOAD(v); }
SYS_FORCE_INLINE v4uf(float a, float b, float c, float d)
{ vector = VM_SPLATS(a, b, c, d); }
// Assignment
SYS_FORCE_INLINE v4uf operator=(float v)
{ vector = v4uf(v).vector; return *this; }
SYS_FORCE_INLINE v4uf operator=(v4sf v)
{ vector = v; return *this; }
SYS_FORCE_INLINE v4uf operator=(const v4uf &v)
{ vector = v.vector; return *this; }
SYS_FORCE_INLINE void condAssign(const v4uf &val, const v4uu &c)
{ *this = (val & c) | (*this & ~c); }
// Comparison
SYS_FORCE_INLINE v4uu operator == (const v4uf &v) const
{ return v4uu(VM_CMPEQ(vector, v.vector)); }
SYS_FORCE_INLINE v4uu operator != (const v4uf &v) const
{ return v4uu(VM_CMPNE(vector, v.vector)); }
SYS_FORCE_INLINE v4uu operator > (const v4uf &v) const
{ return v4uu(VM_CMPGT(vector, v.vector)); }
SYS_FORCE_INLINE v4uu operator < (const v4uf &v) const
{ return v4uu(VM_CMPLT(vector, v.vector)); }
SYS_FORCE_INLINE v4uu operator >= (const v4uf &v) const
{ return v4uu(VM_CMPGE(vector, v.vector)); }
SYS_FORCE_INLINE v4uu operator <= (const v4uf &v) const
{ return v4uu(VM_CMPLE(vector, v.vector)); }
SYS_FORCE_INLINE v4uu operator == (float v) const { return *this == v4uf(v); }
SYS_FORCE_INLINE v4uu operator != (float v) const { return *this != v4uf(v); }
SYS_FORCE_INLINE v4uu operator > (float v) const { return *this > v4uf(v); }
SYS_FORCE_INLINE v4uu operator < (float v) const { return *this < v4uf(v); }
SYS_FORCE_INLINE v4uu operator >= (float v) const { return *this >= v4uf(v); }
SYS_FORCE_INLINE v4uu operator <= (float v) const { return *this <= v4uf(v); }
// Basic math
SYS_FORCE_INLINE v4uf operator+(const v4uf &r) const
{ return v4uf(VM_ADD(vector, r.vector)); }
SYS_FORCE_INLINE v4uf operator-(const v4uf &r) const
{ return v4uf(VM_SUB(vector, r.vector)); }
SYS_FORCE_INLINE v4uf operator-() const
{ return v4uf(VM_NEG(vector)); }
SYS_FORCE_INLINE v4uf operator*(const v4uf &r) const
{ return v4uf(VM_MUL(vector, r.vector)); }
SYS_FORCE_INLINE v4uf operator/(const v4uf &r) const
{ return v4uf(VM_DIV(vector, r.vector)); }
SYS_FORCE_INLINE v4uf operator+=(const v4uf &r) { return (*this = *this + r); }
SYS_FORCE_INLINE v4uf operator-=(const v4uf &r) { return (*this = *this - r); }
SYS_FORCE_INLINE v4uf operator*=(const v4uf &r) { return (*this = *this * r); }
SYS_FORCE_INLINE v4uf operator/=(const v4uf &r) { return (*this = *this / r); }
SYS_FORCE_INLINE v4uf operator+(float r) const { return *this + v4uf(r); }
SYS_FORCE_INLINE v4uf operator-(float r) const { return *this - v4uf(r); }
SYS_FORCE_INLINE v4uf operator*(float r) const { return *this * v4uf(r); }
SYS_FORCE_INLINE v4uf operator/(float r) const { return *this / v4uf(r); }
SYS_FORCE_INLINE v4uf operator+=(float r) { return (*this = *this + r); }
SYS_FORCE_INLINE v4uf operator-=(float r) { return (*this = *this - r); }
SYS_FORCE_INLINE v4uf operator*=(float r) { return (*this = *this * r); }
SYS_FORCE_INLINE v4uf operator/=(float r) { return (*this = *this / r); }
// logical/bitwise
SYS_FORCE_INLINE v4uf operator||(const v4uu &r) const
{ return v4uf(V4SF(VM_OR(V4SI(vector), r.vector))); }
SYS_FORCE_INLINE v4uf operator&&(const v4uu &r) const
{ return v4uf(V4SF(VM_AND(V4SI(vector), r.vector))); }
SYS_FORCE_INLINE v4uf operator^(const v4uu &r) const
{ return v4uf(V4SF(VM_XOR(V4SI(vector), r.vector))); }
SYS_FORCE_INLINE v4uf operator!() const
{ return v4uf(V4SF((*this == v4uf(0.0F)).vector)); }
SYS_FORCE_INLINE v4uf operator||(const v4uf &r) const
{ return v4uf(V4SF(VM_OR(V4SI(vector), V4SI(r.vector)))); }
SYS_FORCE_INLINE v4uf operator&&(const v4uf &r) const
{ return v4uf(V4SF(VM_AND(V4SI(vector), V4SI(r.vector)))); }
SYS_FORCE_INLINE v4uf operator^(const v4uf &r) const
{ return v4uf(V4SF(VM_XOR(V4SI(vector), V4SI(r.vector)))); }
SYS_FORCE_INLINE v4uf operator|(const v4uu &r) const { return *this || r; }
SYS_FORCE_INLINE v4uf operator&(const v4uu &r) const { return *this && r; }
SYS_FORCE_INLINE v4uf operator~() const
{ return *this ^ v4uu(0xFFFFFFFF); }
SYS_FORCE_INLINE v4uf operator|(const v4uf &r) const { return *this || r; }
SYS_FORCE_INLINE v4uf operator&(const v4uf &r) const { return *this && r; }
// component
SYS_FORCE_INLINE float operator[](int idx) const { return VM_EXTRACT(vector, idx); }
SYS_FORCE_INLINE void setComp(int idx, float v) { vector = VM_INSERT(vector, v, idx); }
// more math
SYS_FORCE_INLINE v4uf abs() const { return v4uf(VM_ABS(vector)); }
SYS_FORCE_INLINE v4uf clamp(const v4uf &low, const v4uf &high) const
{ return v4uf(
VM_MIN(VM_MAX(vector, low.vector), high.vector)); }
SYS_FORCE_INLINE v4uf clamp(float low, float high) const
{ return v4uf(VM_MIN(VM_MAX(vector,
v4uf(low).vector), v4uf(high).vector)); }
SYS_FORCE_INLINE v4uf recip() const { return v4uf(VM_INVERT(vector)); }
/// This is a lie, it is a signed int.
SYS_FORCE_INLINE v4uu toUnsignedInt() const { return VM_INT(vector); }
SYS_FORCE_INLINE v4uu toSignedInt() const { return VM_INT(vector); }
v4uu floor() const
{
VM_P_FLOOR();
v4uu result = VM_FLOOR(vector);
VM_E_FLOOR();
return result;
}
/// Returns the integer part of this float, this becomes the
/// 0..1 fractional component.
v4uu splitFloat()
{
v4uu base = toSignedInt();
*this -= base.toFloat();
return base;
}
#ifdef __SSE__
template <int A, int B, int C, int D>
SYS_FORCE_INLINE v4uf swizzle() const
{
return VM_SHUFFLE<A,B,C,D>(vector);
}
#endif
SYS_FORCE_INLINE v4uu isFinite() const
{
// If the exponent is the maximum value, it's either infinite or NaN.
const v4si mask = VM_SPLATS(0x7F800000);
return ~v4uu(VM_ICMPEQ(VM_AND(V4SI(vector), mask), mask));
}
public:
v4sf vector;
};
SYS_FORCE_INLINE v4uf
v4uu::toFloat() const
{
return v4uf(VM_IFLOAT(vector));
}
//
// Custom vector operations
//
static SYS_FORCE_INLINE v4uf
sqrt(const v4uf &a)
{
return v4uf(VM_SQRT(a.vector));
}
static SYS_FORCE_INLINE v4uf
fabs(const v4uf &a)
{
return a.abs();
}
// Use this operation to mask disabled values to 0
// rval = !a ? b : 0;
static SYS_FORCE_INLINE v4uf
andn(const v4uu &a, const v4uf &b)
{
return v4uf(V4SF(VM_ANDNOT(a.vector, V4SI(b.vector))));
}
static SYS_FORCE_INLINE v4uu
andn(const v4uu &a, const v4uu &b)
{
return v4uu(VM_ANDNOT(a.vector, b.vector));
}
// rval = a ? b : c;
static SYS_FORCE_INLINE v4uf
ternary(const v4uu &a, const v4uf &b, const v4uf &c)
{
return (b & a) | andn(a, c);
}
static SYS_FORCE_INLINE v4uu
ternary(const v4uu &a, const v4uu &b, const v4uu &c)
{
return (b & a) | andn(a, c);
}
// rval = !(a && b)
static SYS_FORCE_INLINE v4uu
nand(const v4uu &a, const v4uu &b)
{
return !v4uu(VM_AND(a.vector, b.vector));
}
static SYS_FORCE_INLINE v4uf
vmin(const v4uf &a, const v4uf &b)
{
return v4uf(VM_MIN(a.vector, b.vector));
}
static SYS_FORCE_INLINE v4uf
vmax(const v4uf &a, const v4uf &b)
{
return v4uf(VM_MAX(a.vector, b.vector));
}
static SYS_FORCE_INLINE v4uf
clamp(const v4uf &a, const v4uf &b, const v4uf &c)
{
return vmax(vmin(a, c), b);
}
static SYS_FORCE_INLINE v4uf
clamp(const v4uf &a, float b, float c)
{
return vmax(vmin(a, v4uf(c)), v4uf(b));
}
static SYS_FORCE_INLINE bool
allbits(const v4uu &a)
{
return vm_allbits(a.vector);
}
static SYS_FORCE_INLINE bool
anybits(const v4uu &a)
{
return !allbits(~a);
}
static SYS_FORCE_INLINE v4uf
madd(const v4uf &v, const v4uf &f, const v4uf &a)
{
return v4uf(VM_MADD(v.vector, f.vector, a.vector));
}
static SYS_FORCE_INLINE v4uf
madd(const v4uf &v, float f, float a)
{
return v4uf(VM_MADD(v.vector, v4uf(f).vector, v4uf(a).vector));
}
static SYS_FORCE_INLINE v4uf
madd(const v4uf &v, float f, const v4uf &a)
{
return v4uf(VM_MADD(v.vector, v4uf(f).vector, a.vector));
}
static SYS_FORCE_INLINE v4uf
msub(const v4uf &v, const v4uf &f, const v4uf &s)
{
return madd(v, f, -s);
}
static SYS_FORCE_INLINE v4uf
msub(const v4uf &v, float f, float s)
{
return madd(v, f, -s);
}
static SYS_FORCE_INLINE v4uf
lerp(const v4uf &a, const v4uf &b, const v4uf &w)
{
v4uf w1 = v4uf(1.0F) - w;
return madd(a, w1, b*w);
}
static SYS_FORCE_INLINE v4uf
luminance(const v4uf &r, const v4uf &g, const v4uf &b,
float rw, float gw, float bw)
{
return v4uf(madd(r, v4uf(rw), madd(g, v4uf(gw), b * bw)));
}
static SYS_FORCE_INLINE float
dot3(const v4uf &a, const v4uf &b)
{
v4uf res = a*b;
return res[0] + res[1] + res[2];
}
static SYS_FORCE_INLINE float
dot4(const v4uf &a, const v4uf &b)
{
v4uf res = a*b;
return res[0] + res[1] + res[2] + res[3];
}
static SYS_FORCE_INLINE float
length(const v4uf &a)
{
return SYSsqrt(dot3(a, a));
}
static SYS_FORCE_INLINE v4uf
normalize(const v4uf &a)
{
return a / length(a);
}
static SYS_FORCE_INLINE v4uf
cross(const v4uf &a, const v4uf &b)
{
return v4uf(a[1]*b[2] - a[2]*b[1],
a[2]*b[0] - a[0]*b[2],
a[0]*b[1] - a[1]*b[0], 0);
}
// Currently there is no specific support for signed integers
typedef v4uu v4ui;
// Assuming that ptr is an array of elements of type STYPE, this operation
// will return the index of the first element that is aligned to (1<<ASIZE)
// bytes.
#define VM_ALIGN(ptr, ASIZE, STYPE) \
((((1<<ASIZE)-(intptr_t)ptr)&((1<<ASIZE)-1))/sizeof(STYPE))
}}
#endif
/*
* Copyright (c) 2018 Side Effects Software Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* COMMENTS:
* This is the array class implementation used by almost everything here.
*/
#pragma once
#ifndef __UT_ARRAY_H_INCLUDED__
#define __UT_ARRAY_H_INCLUDED__
#include <algorithm>
#include <functional>
#include <type_traits>
#include <string.h>
namespace igl { namespace FastWindingNumber {
/// This routine describes how to change the size of an array.
/// It must increase the current_size by at least one!
///
/// Current expected sequence of small sizes:
/// 4, 8, 16, 32, 48, 64, 80, 96, 112,
/// 128, 256, 384, 512, 640, 768, 896, 1024,
/// (increases by approx factor of 1.125 each time after this)
template <typename T>
static inline T
UTbumpAlloc(T current_size)
{
// NOTE: These must be powers of two. See below.
constexpr T SMALL_ALLOC(16);
constexpr T BIG_ALLOC(128);
// For small values, we increment by fixed amounts. For
// large values, we increment by one eighth of the current size.
// This prevents n^2 behaviour with allocation one element at a time.
// A factor of 1/8 will waste 1/16 the memory on average, and will
// double the size of the array in approximately 6 reallocations.
if (current_size < T(8))
{
return (current_size < T(4)) ? T(4) : T(8);
}
if (current_size < T(BIG_ALLOC))
{
// Snap up to next multiple of SMALL_ALLOC (must be power of 2)
return (current_size + T(SMALL_ALLOC)) & ~T(SMALL_ALLOC-1);
}
if (current_size < T(BIG_ALLOC * 8))
{
// Snap up to next multiple of BIG_ALLOC (must be power of 2)
return (current_size + T(BIG_ALLOC)) & ~T(BIG_ALLOC-1);
}
T bump = current_size >> 3; // Divided by 8.
current_size += bump;
return current_size;
}
template <typename T>
class UT_Array
{
public:
typedef T value_type;
typedef int (*Comparator)(const T *, const T *);
/// Copy constructor. It duplicates the data.
/// It's marked explicit so that it's not accidentally passed by value.
/// You can always pass by reference and then copy it, if needed.
/// If you have a line like:
/// UT_Array<int> a = otherarray;
/// and it really does need to copy instead of referencing,
/// you can rewrite it as:
/// UT_Array<int> a(otherarray);
inline explicit UT_Array(const UT_Array<T> &a);
/// Move constructor. Steals the working data from the original.
inline UT_Array(UT_Array<T> &&a) noexcept;
/// Construct based on given capacity and size
UT_Array(exint capacity, exint size)
{
myData = capacity ? allocateCapacity(capacity) : NULL;
if (capacity < size)
size = capacity;
mySize = size;
myCapacity = capacity;
trivialConstructRange(myData, mySize);
}
/// Construct based on given capacity with a size of 0
explicit UT_Array(exint capacity = 0) : myCapacity(capacity), mySize(0)
{
myData = capacity ? allocateCapacity(capacity) : NULL;
}
/// Construct with the contents of an initializer list
inline explicit UT_Array(std::initializer_list<T> init);
inline ~UT_Array();
inline void swap(UT_Array<T> &other);
/// Append an element to the current elements and return its index in the
/// array, or insert the element at a specified position; if necessary,
/// insert() grows the array to accommodate the element. The insert
/// methods use the assignment operator '=' to place the element into the
/// right spot; be aware that '=' works differently on objects and pointers.
/// The test for duplicates uses the logical equal operator '=='; as with
/// '=', the behaviour of the equality operator on pointers versus objects
/// is not the same.
/// Use the subscript operators instead of insert() if you are appending
/// to the array, or if you don't mind overwriting the element already
/// inserted at the given index.
exint append(void) { return insert(mySize); }
exint append(const T &t) { return appendImpl(t); }
exint append(T &&t) { return appendImpl(std::move(t)); }
inline void append(const T *pt, exint count);
inline void appendMultiple(const T &t, exint count);
inline exint insert(exint index);
exint insert(const T &t, exint i)
{ return insertImpl(t, i); }
exint insert(T &&t, exint i)
{ return insertImpl(std::move(t), i); }
/// Adds a new element to the array (resizing if necessary) and forwards
/// the given arguments to T's constructor.
/// NOTE: Unlike append(), the arguments cannot reference any existing
/// elements in the array. Checking for and handling such cases would
/// remove most of the performance gain versus append(T(...)). Debug builds
/// will assert that the arguments are valid.
template <typename... S>
inline exint emplace_back(S&&... s);
/// Takes another T array and concatenate it onto my end
inline exint concat(const UT_Array<T> &a);
/// Insert an element "count" times at the given index. Return the index.
inline exint multipleInsert(exint index, exint count);
/// An alias for unique element insertion at a certain index. Also used by
/// the other insertion methods.
exint insertAt(const T &t, exint index)
{ return insertImpl(t, index); }
/// Return true if given index is valid.
bool isValidIndex(exint index) const
{ return (index >= 0 && index < mySize); }
/// Remove one element from the array given its
/// position in the list, and fill the gap by shifting the elements down
/// by one position. Return the index of the element removed or -1 if
/// the index was out of bounds.
exint removeIndex(exint index)
{
return isValidIndex(index) ? removeAt(index) : -1;
}
void removeLast()
{
if (mySize) removeAt(mySize-1);
}
/// Remove the range [begin_i,end_i) of elements from the array.
inline void removeRange(exint begin_i, exint end_i);
/// Remove the range [begin_i, end_i) of elements from this array and place
/// them in the dest array, shrinking/growing the dest array as necessary.
inline void extractRange(exint begin_i, exint end_i,
UT_Array<T>& dest);
/// Removes all matching elements from the list, shuffling down and changing
/// the size appropriately.
/// Returns the number of elements left.
template <typename IsEqual>
inline exint removeIf(IsEqual is_equal);
/// Remove all matching elements. Also sets the capacity of the array.
template <typename IsEqual>
void collapseIf(IsEqual is_equal)
{
removeIf(is_equal);
setCapacity(size());
}
/// Move howMany objects starting at index srcIndex to destIndex;
/// This method will remove the elements at [srcIdx, srcIdx+howMany) and
/// then insert them at destIdx. This method can be used in place of
/// the old shift() operation.
inline void move(exint srcIdx, exint destIdx, exint howMany);
/// Cyclically shifts the entire array by howMany
inline void cycle(exint howMany);
/// Quickly set the array to a single value.
inline void constant(const T &v);
/// Zeros the array if a POD type, else trivial constructs if a class type.
inline void zero();
/// The fastest search possible, which does pointer arithmetic to find the
/// index of the element. WARNING: index() does no out-of-bounds checking.
exint index(const T &t) const { return &t - myData; }
exint safeIndex(const T &t) const
{
return (&t >= myData && &t < (myData + mySize))
? &t - myData : -1;
}
/// Set the capacity of the array, i.e. grow it or shrink it. The
/// function copies the data after reallocating space for the array.
inline void setCapacity(exint newcapacity);
void setCapacityIfNeeded(exint mincapacity)
{
if (capacity() < mincapacity)
setCapacity(mincapacity);
}
/// If the capacity is smaller than mincapacity, expand the array
/// to at least mincapacity and to at least a constant factor of the
/// array's previous capacity, to avoid having a linear number of
/// reallocations in a linear number of calls to bumpCapacity.
void bumpCapacity(exint mincapacity)
{
if (capacity() >= mincapacity)
return;
// The following 4 lines are just
// SYSmax(mincapacity, UTbumpAlloc(capacity())), avoiding SYSmax
exint bumped = UTbumpAlloc(capacity());
exint newcapacity = mincapacity;
if (bumped > mincapacity)
newcapacity = bumped;
setCapacity(newcapacity);
}
/// First bumpCapacity to ensure that there's space for newsize,
/// expanding either not at all or by at least a constant factor
/// of the array's previous capacity,
/// then set the size to newsize.
void bumpSize(exint newsize)
{
bumpCapacity(newsize);
setSize(newsize);
}
/// NOTE: bumpEntries() will be deprecated in favour of bumpSize() in a
/// future version.
void bumpEntries(exint newsize)
{
bumpSize(newsize);
}
/// Query the capacity, i.e. the allocated length of the array.
/// NOTE: capacity() >= size().
exint capacity() const { return myCapacity; }
/// Query the size, i.e. the number of occupied elements in the array.
/// NOTE: capacity() >= size().
exint size() const { return mySize; }
/// Alias of size(). size() is preferred.
exint entries() const { return mySize; }
/// Returns true iff there are no occupied elements in the array.
bool isEmpty() const { return mySize==0; }
/// Set the size, the number of occupied elements in the array.
/// NOTE: This will not do bumpCapacity, so if you call this
/// n times to increase the size, it may take
/// n^2 time.
void setSize(exint newsize)
{
if (newsize < 0)
newsize = 0;
if (newsize == mySize)
return;
setCapacityIfNeeded(newsize);
if (mySize > newsize)
trivialDestructRange(myData + newsize, mySize - newsize);
else // newsize > mySize
trivialConstructRange(myData + mySize, newsize - mySize);
mySize = newsize;
}
/// Alias of setSize(). setSize() is preferred.
void entries(exint newsize)
{
setSize(newsize);
}
/// Set the size, but unlike setSize(newsize), this function
/// will not initialize new POD elements to zero. Non-POD data types
/// will still have their constructors called.
/// This function is faster than setSize(ne) if you intend to fill in
/// data for all elements.
void setSizeNoInit(exint newsize)
{
if (newsize < 0)
newsize = 0;
if (newsize == mySize)
return;
setCapacityIfNeeded(newsize);
if (mySize > newsize)
trivialDestructRange(myData + newsize, mySize - newsize);
else if (!isPOD()) // newsize > mySize
trivialConstructRange(myData + mySize, newsize - mySize);
mySize = newsize;
}
/// Decreases, but never expands, to the given maxsize.
void truncate(exint maxsize)
{
if (maxsize >= 0 && size() > maxsize)
setSize(maxsize);
}
/// Resets list to an empty list.
void clear() {
// Don't call setSize(0) since that would require a valid default
// constructor.
trivialDestructRange(myData, mySize);
mySize = 0;
}
/// Assign array a to this array by copying each of a's elements with
/// memcpy for POD types, and with copy construction for class types.
inline UT_Array<T> & operator=(const UT_Array<T> &a);
/// Replace the contents with those from the initializer_list ilist
inline UT_Array<T> & operator=(std::initializer_list<T> ilist);
/// Move the contents of array a to this array.
inline UT_Array<T> & operator=(UT_Array<T> &&a);
/// Compare two array and return true if they are equal and false otherwise.
/// Two elements are checked against each other using operator '==' or
/// compare() respectively.
/// NOTE: The capacities of the arrays are not checked when
/// determining whether they are equal.
inline bool operator==(const UT_Array<T> &a) const;
inline bool operator!=(const UT_Array<T> &a) const;
/// Subscript operator
/// NOTE: This does NOT do any bounds checking unless paranoid
/// asserts are enabled.
T & operator()(exint i)
{
UT_ASSERT_P(i >= 0 && i < mySize);
return myData[i];
}
/// Const subscript operator
/// NOTE: This does NOT do any bounds checking unless paranoid
/// asserts are enabled.
const T & operator()(exint i) const
{
UT_ASSERT_P(i >= 0 && i < mySize);
return myData[i];
}
/// Subscript operator
/// NOTE: This does NOT do any bounds checking unless paranoid
/// asserts are enabled.
T & operator[](exint i)
{
UT_ASSERT_P(i >= 0 && i < mySize);
return myData[i];
}
/// Const subscript operator
/// NOTE: This does NOT do any bounds checking unless paranoid
/// asserts are enabled.
const T & operator[](exint i) const
{
UT_ASSERT_P(i >= 0 && i < mySize);
return myData[i];
}
/// forcedRef(exint) will grow the array if necessary, initializing any
/// new elements to zero for POD types and default constructing for
/// class types.
T & forcedRef(exint i)
{
UT_ASSERT_P(i >= 0);
if (i >= mySize)
bumpSize(i+1);
return myData[i];
}
/// forcedGet(exint) does NOT grow the array, and will return default
/// objects for out of bound array indices.
T forcedGet(exint i) const
{
return (i >= 0 && i < mySize) ? myData[i] : T();
}
T & last()
{
UT_ASSERT_P(mySize);
return myData[mySize-1];
}
const T & last() const
{
UT_ASSERT_P(mySize);
return myData[mySize-1];
}
T * getArray() const { return myData; }
const T * getRawArray() const { return myData; }
T * array() { return myData; }
const T * array() const { return myData; }
T * data() { return myData; }
const T * data() const { return myData; }
/// This method allows you to swap in a new raw T array, which must be
/// the same size as myCapacity. Use caution with this method.
T * aliasArray(T *newdata)
{ T *data = myData; myData = newdata; return data; }
template <typename IT, bool FORWARD>
class base_iterator :
public std::iterator<std::random_access_iterator_tag, T, exint>
{
public:
typedef IT& reference;
typedef IT* pointer;
// Note: When we drop gcc 4.4 support and allow range-based for
// loops, we should also drop atEnd(), which means we can drop
// myEnd here.
base_iterator() : myCurrent(NULL), myEnd(NULL) {}
// Allow iterator to const_iterator conversion
template<typename EIT>
base_iterator(const base_iterator<EIT, FORWARD> &src)
: myCurrent(src.myCurrent), myEnd(src.myEnd) {}
pointer operator->() const
{ return FORWARD ? myCurrent : myCurrent - 1; }
reference operator*() const
{ return FORWARD ? *myCurrent : myCurrent[-1]; }
reference item() const
{ return FORWARD ? *myCurrent : myCurrent[-1]; }
reference operator[](exint n) const
{ return FORWARD ? myCurrent[n] : myCurrent[-n - 1]; }
/// Pre-increment operator
base_iterator &operator++()
{
if (FORWARD) ++myCurrent; else --myCurrent;
return *this;
}
/// Post-increment operator
base_iterator operator++(int)
{
base_iterator tmp = *this;
if (FORWARD) ++myCurrent; else --myCurrent;
return tmp;
}
/// Pre-decrement operator
base_iterator &operator--()
{
if (FORWARD) --myCurrent; else ++myCurrent;
return *this;
}
/// Post-decrement operator
base_iterator operator--(int)
{
base_iterator tmp = *this;
if (FORWARD) --myCurrent; else ++myCurrent;
return tmp;
}
base_iterator &operator+=(exint n)
{
if (FORWARD)
myCurrent += n;
else
myCurrent -= n;
return *this;
}
base_iterator operator+(exint n) const
{
if (FORWARD)
return base_iterator(myCurrent + n, myEnd);
else
return base_iterator(myCurrent - n, myEnd);
}
base_iterator &operator-=(exint n)
{ return (*this) += (-n); }
base_iterator operator-(exint n) const
{ return (*this) + (-n); }
bool atEnd() const { return myCurrent == myEnd; }
void advance() { this->operator++(); }
// Comparators
template<typename ITR, bool FR>
bool operator==(const base_iterator<ITR, FR> &r) const
{ return myCurrent == r.myCurrent; }
template<typename ITR, bool FR>
bool operator!=(const base_iterator<ITR, FR> &r) const
{ return myCurrent != r.myCurrent; }
template<typename ITR>
bool operator<(const base_iterator<ITR, FORWARD> &r) const
{
if (FORWARD)
return myCurrent < r.myCurrent;
else
return r.myCurrent < myCurrent;
}
template<typename ITR>
bool operator>(const base_iterator<ITR, FORWARD> &r) const
{
if (FORWARD)
return myCurrent > r.myCurrent;
else
return r.myCurrent > myCurrent;
}
template<typename ITR>
bool operator<=(const base_iterator<ITR, FORWARD> &r) const
{
if (FORWARD)
return myCurrent <= r.myCurrent;
else
return r.myCurrent <= myCurrent;
}
template<typename ITR>
bool operator>=(const base_iterator<ITR, FORWARD> &r) const
{
if (FORWARD)
return myCurrent >= r.myCurrent;
else
return r.myCurrent >= myCurrent;
}
// Difference operator for std::distance
template<typename ITR>
exint operator-(const base_iterator<ITR, FORWARD> &r) const
{
if (FORWARD)
return exint(myCurrent - r.myCurrent);
else
return exint(r.myCurrent - myCurrent);
}
protected:
friend class UT_Array<T>;
base_iterator(IT *c, IT *e) : myCurrent(c), myEnd(e) {}
private:
IT *myCurrent;
IT *myEnd;
};
typedef base_iterator<T, true> iterator;
typedef base_iterator<const T, true> const_iterator;
typedef base_iterator<T, false> reverse_iterator;
typedef base_iterator<const T, false> const_reverse_iterator;
typedef const_iterator traverser; // For backward compatibility
/// Begin iterating over the array. The contents of the array may be
/// modified during the traversal.
iterator begin()
{
return iterator(myData, myData + mySize);
}
/// End iterator.
iterator end()
{
return iterator(myData + mySize,
myData + mySize);
}
/// Begin iterating over the array. The array may not be modified during
/// the traversal.
const_iterator begin() const
{
return const_iterator(myData, myData + mySize);
}
/// End const iterator. Consider using it.atEnd() instead.
const_iterator end() const
{
return const_iterator(myData + mySize,
myData + mySize);
}
/// Begin iterating over the array in reverse.
reverse_iterator rbegin()
{
return reverse_iterator(myData + mySize,
myData);
}
/// End reverse iterator.
reverse_iterator rend()
{
return reverse_iterator(myData, myData);
}
/// Begin iterating over the array in reverse.
const_reverse_iterator rbegin() const
{
return const_reverse_iterator(myData + mySize,
myData);
}
/// End reverse iterator. Consider using it.atEnd() instead.
const_reverse_iterator rend() const
{
return const_reverse_iterator(myData, myData);
}
/// Remove item specified by the reverse_iterator.
void removeItem(const reverse_iterator &it)
{
removeAt(&it.item() - myData);
}
/// Very dangerous methods to share arrays.
/// The array is not aware of the sharing, so ensure you clear
/// out the array prior a destructor or setCapacity operation.
void unsafeShareData(UT_Array<T> &src)
{
myData = src.myData;
myCapacity = src.myCapacity;
mySize = src.mySize;
}
void unsafeShareData(T *src, exint srcsize)
{
myData = src;
myCapacity = srcsize;
mySize = srcsize;
}
void unsafeShareData(T *src, exint size, exint capacity)
{
myData = src;
mySize = size;
myCapacity = capacity;
}
void unsafeClearData()
{
myData = NULL;
myCapacity = 0;
mySize = 0;
}
/// Returns true if the data used by the array was allocated on the heap.
inline bool isHeapBuffer() const
{
return (myData != (T *)(((char*)this) + sizeof(*this)));
}
inline bool isHeapBuffer(T* data) const
{
return (data != (T *)(((char*)this) + sizeof(*this)));
}
protected:
// Check whether T may have a constructor, destructor, or copy
// constructor. This test is conservative in that some POD types will
// not be recognized as POD by this function. To mark your type as POD,
// use the SYS_DECLARE_IS_POD() macro in SYS_TypeDecorate.h.
static constexpr SYS_FORCE_INLINE bool isPOD()
{
return std::is_pod<T>::value;
}
/// Implements both append(const T &) and append(T &&) via perfect
/// forwarding. Unlike the variadic emplace_back(), its argument may be a
/// reference to another element in the array.
template <typename S>
inline exint appendImpl(S &&s);
/// Similar to appendImpl() but for insertion.
template <typename S>
inline exint insertImpl(S &&s, exint index);
// Construct the given type
template <typename... S>
static void construct(T &dst, S&&... s)
{
new (&dst) T(std::forward<S>(s)...);
}
// Copy construct the given type
static void copyConstruct(T &dst, const T &src)
{
if (isPOD())
dst = src;
else
new (&dst) T(src);
}
static void copyConstructRange(T *dst, const T *src, exint n)
{
if (isPOD())
{
if (n > 0)
{
::memcpy((void *)dst, (const void *)src,
n * sizeof(T));
}
}
else
{
for (exint i = 0; i < n; i++)
new (&dst[i]) T(src[i]);
}
}
/// Element Constructor
static void trivialConstruct(T &dst)
{
if (!isPOD())
new (&dst) T();
else
memset((void *)&dst, 0, sizeof(T));
}
static void trivialConstructRange(T *dst, exint n)
{
if (!isPOD())
{
for (exint i = 0; i < n; i++)
new (&dst[i]) T();
}
else if (n == 1)
{
// Special case for n == 1. If the size parameter
// passed to memset is known at compile time, this
// function call will be inlined. This results in
// much faster performance than a real memset
// function call which is required in the case
// below, where n is not known until runtime.
// This makes calls to append() much faster.
memset((void *)dst, 0, sizeof(T));
}
else
memset((void *)dst, 0, sizeof(T) * n);
}
/// Element Destructor
static void trivialDestruct(T &dst)
{
if (!isPOD())
dst.~T();
}
static void trivialDestructRange(T *dst, exint n)
{
if (!isPOD())
{
for (exint i = 0; i < n; i++)
dst[i].~T();
}
}
private:
/// Pointer to the array of elements of type T
T *myData;
/// The number of elements for which we have allocated memory
exint myCapacity;
/// The actual number of valid elements in the array
exint mySize;
// The guts of the remove() methods.
inline exint removeAt(exint index);
inline T * allocateCapacity(exint num_items);
};
}}
#endif // __UT_ARRAY_H_INCLUDED__
/*
* Copyright (c) 2018 Side Effects Software Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* COMMENTS:
* This is meant to be included by UT_Array.h and includes
* the template implementations needed by external code.
*/
#pragma once
#ifndef __UT_ARRAYIMPL_H_INCLUDED__
#define __UT_ARRAYIMPL_H_INCLUDED__
#include <algorithm>
#include <utility>
#include <stdlib.h>
#include <string.h>
namespace igl { namespace FastWindingNumber {
// Implemented in UT_Array.C
extern void ut_ArrayImplFree(void *p);
template <typename T>
inline UT_Array<T>::UT_Array(const UT_Array<T> &a)
: myCapacity(a.size()), mySize(a.size())
{
if (myCapacity)
{
myData = allocateCapacity(myCapacity);
copyConstructRange(myData, a.array(), mySize);
}
else
{
myData = nullptr;
}
}
template <typename T>
inline UT_Array<T>::UT_Array(std::initializer_list<T> init)
: myCapacity(init.size()), mySize(init.size())
{
if (myCapacity)
{
myData = allocateCapacity(myCapacity);
copyConstructRange(myData, init.begin(), mySize);
}
else
{
myData = nullptr;
}
}
template <typename T>
inline UT_Array<T>::UT_Array(UT_Array<T> &&a) noexcept
{
if (!a.isHeapBuffer())
{
myData = nullptr;
myCapacity = 0;
mySize = 0;
operator=(std::move(a));
return;
}
myCapacity = a.myCapacity;
mySize = a.mySize;
myData = a.myData;
a.myCapacity = a.mySize = 0;
a.myData = nullptr;
}
template <typename T>
inline UT_Array<T>::~UT_Array()
{
// NOTE: We call setCapacity to ensure that we call trivialDestructRange,
// then call free on myData.
setCapacity(0);
}
template <typename T>
inline T *
UT_Array<T>::allocateCapacity(exint capacity)
{
T *data = (T *)malloc(capacity * sizeof(T));
// Avoid degenerate case if we happen to be aliased the wrong way
if (!isHeapBuffer(data))
{
T *prev = data;
data = (T *)malloc(capacity * sizeof(T));
ut_ArrayImplFree(prev);
}
return data;
}
template <typename T>
inline void
UT_Array<T>::swap( UT_Array<T> &other )
{
std::swap( myData, other.myData );
std::swap( myCapacity, other.myCapacity );
std::swap( mySize, other.mySize );
}
template <typename T>
inline exint
UT_Array<T>::insert(exint index)
{
if (index >= mySize)
{
bumpCapacity(index + 1);
trivialConstructRange(myData + mySize, index - mySize + 1);
mySize = index+1;
return index;
}
bumpCapacity(mySize + 1);
UT_ASSERT_P(index >= 0);
::memmove((void *)&myData[index+1], (void *)&myData[index],
((mySize-index)*sizeof(T)));
trivialConstruct(myData[index]);
mySize++;
return index;
}
template <typename T>
template <typename S>
inline exint
UT_Array<T>::appendImpl(S &&s)
{
if (mySize == myCapacity)
{
exint idx = safeIndex(s);
// NOTE: UTbumpAlloc always returns a strictly larger value.
setCapacity(UTbumpAlloc(myCapacity));
if (idx >= 0)
construct(myData[mySize], std::forward<S>(myData[idx]));
else
construct(myData[mySize], std::forward<S>(s));
}
else
{
construct(myData[mySize], std::forward<S>(s));
}
return mySize++;
}
template <typename T>
template <typename... S>
inline exint
UT_Array<T>::emplace_back(S&&... s)
{
if (mySize == myCapacity)
setCapacity(UTbumpAlloc(myCapacity));
construct(myData[mySize], std::forward<S>(s)...);
return mySize++;
}
template <typename T>
inline void
UT_Array<T>::append(const T *pt, exint count)
{
bumpCapacity(mySize + count);
copyConstructRange(myData + mySize, pt, count);
mySize += count;
}
template <typename T>
inline void
UT_Array<T>::appendMultiple(const T &t, exint count)
{
UT_ASSERT_P(count >= 0);
if (count <= 0)
return;
if (mySize + count >= myCapacity)
{
exint tidx = safeIndex(t);
bumpCapacity(mySize + count);
for (exint i = 0; i < count; i++)
copyConstruct(myData[mySize+i], tidx >= 0 ? myData[tidx] : t);
}
else
{
for (exint i = 0; i < count; i++)
copyConstruct(myData[mySize+i], t);
}
mySize += count;
}
template <typename T>
inline exint
UT_Array<T>::concat(const UT_Array<T> &a)
{
bumpCapacity(mySize + a.mySize);
copyConstructRange(myData + mySize, a.myData, a.mySize);
mySize += a.mySize;
return mySize;
}
template <typename T>
inline exint
UT_Array<T>::multipleInsert(exint beg_index, exint count)
{
exint end_index = beg_index + count;
if (beg_index >= mySize)
{
bumpCapacity(end_index);
trivialConstructRange(myData + mySize, end_index - mySize);
mySize = end_index;
return beg_index;
}
bumpCapacity(mySize+count);
::memmove((void *)&myData[end_index], (void *)&myData[beg_index],
((mySize-beg_index)*sizeof(T)));
mySize += count;
trivialConstructRange(myData + beg_index, count);
return beg_index;
}
template <typename T>
template <typename S>
inline exint
UT_Array<T>::insertImpl(S &&s, exint index)
{
if (index == mySize)
{
// This case avoids an extraneous call to trivialConstructRange()
// which the compiler may not optimize out.
(void) appendImpl(std::forward<S>(s));
}
else if (index > mySize)
{
exint src_i = safeIndex(s);
bumpCapacity(index + 1);
trivialConstructRange(myData + mySize, index - mySize);
if (src_i >= 0)
construct(myData[index], std::forward<S>(myData[src_i]));
else
construct(myData[index], std::forward<S>(s));
mySize = index + 1;
}
else // (index < mySize)
{
exint src_i = safeIndex(s);
bumpCapacity(mySize + 1);
::memmove((void *)&myData[index+1], (void *)&myData[index],
((mySize-index)*sizeof(T)));
if (src_i >= index)
++src_i;
if (src_i >= 0)
construct(myData[index], std::forward<S>(myData[src_i]));
else
construct(myData[index], std::forward<S>(s));
++mySize;
}
return index;
}
template <typename T>
inline exint
UT_Array<T>::removeAt(exint idx)
{
trivialDestruct(myData[idx]);
if (idx != --mySize)
{
::memmove((void *)&myData[idx], (void *)&myData[idx+1],
((mySize-idx)*sizeof(T)));
}
return idx;
}
template <typename T>
inline void
UT_Array<T>::removeRange(exint begin_i, exint end_i)
{
UT_ASSERT(begin_i <= end_i);
UT_ASSERT(end_i <= size());
if (end_i < size())
{
trivialDestructRange(myData + begin_i, end_i - begin_i);
::memmove((void *)&myData[begin_i], (void *)&myData[end_i],
(mySize - end_i)*sizeof(T));
}
setSize(mySize - (end_i - begin_i));
}
template <typename T>
inline void
UT_Array<T>::extractRange(exint begin_i, exint end_i, UT_Array<T>& dest)
{
UT_ASSERT_P(begin_i >= 0);
UT_ASSERT_P(begin_i <= end_i);
UT_ASSERT_P(end_i <= size());
UT_ASSERT(this != &dest);
exint nelements = end_i - begin_i;
// grow the raw array if necessary.
dest.setCapacityIfNeeded(nelements);
::memmove((void*)dest.myData, (void*)&myData[begin_i],
nelements * sizeof(T));
dest.mySize = nelements;
// we just asserted this was true, but just in case
if (this != &dest)
{
if (end_i < size())
{
::memmove((void*)&myData[begin_i], (void*)&myData[end_i],
(mySize - end_i) * sizeof(T));
}
setSize(mySize - nelements);
}
}
template <typename T>
inline void
UT_Array<T>::move(exint srcIdx, exint destIdx, exint howMany)
{
// Make sure all the parameters are valid.
if( srcIdx < 0 )
srcIdx = 0;
if( destIdx < 0 )
destIdx = 0;
// If we are told to move a set of elements that would extend beyond the
// end of the current array, trim the group.
if( srcIdx + howMany > size() )
howMany = size() - srcIdx;
// If the destIdx would have us move the source beyond the end of the
// current array, move the destIdx back.
if( destIdx + howMany > size() )
destIdx = size() - howMany;
if( srcIdx != destIdx && howMany > 0 )
{
void **tmp = 0;
exint savelen;
savelen = SYSabs(srcIdx - destIdx);
tmp = (void **)::malloc(savelen*sizeof(T));
if( srcIdx > destIdx && howMany > 0 )
{
// We're moving the group backwards. Save all the stuff that
// we would overwrite, plus everything beyond that to the
// start of the source group. Then move the source group, then
// tack the saved data onto the end of the moved group.
::memcpy(tmp, (void *)&myData[destIdx], (savelen*sizeof(T)));
::memmove((void *)&myData[destIdx], (void *)&myData[srcIdx],
(howMany*sizeof(T)));
::memcpy((void *)&myData[destIdx+howMany], tmp, (savelen*sizeof(T)));
}
if( srcIdx < destIdx && howMany > 0 )
{
// We're moving the group forwards. Save from the end of the
// group being moved to the end of the where the destination
// group will end up. Then copy the source to the destination.
// Then move back up to the original source location and drop
// in our saved data.
::memcpy(tmp, (void *)&myData[srcIdx+howMany], (savelen*sizeof(T)));
::memmove((void *)&myData[destIdx], (void *)&myData[srcIdx],
(howMany*sizeof(T)));
::memcpy((void *)&myData[srcIdx], tmp, (savelen*sizeof(T)));
}
::free(tmp);
}
}
template <typename T>
template <typename IsEqual>
inline exint
UT_Array<T>::removeIf(IsEqual is_equal)
{
// Move dst to the first element to remove.
exint dst;
for (dst = 0; dst < mySize; dst++)
{
if (is_equal(myData[dst]))
break;
}
// Now start looking at all the elements past the first one to remove.
for (exint idx = dst+1; idx < mySize; idx++)
{
if (!is_equal(myData[idx]))
{
UT_ASSERT(idx != dst);
myData[dst] = myData[idx];
dst++;
}
// On match, ignore.
}
// New size
mySize = dst;
return mySize;
}
template <typename T>
inline void
UT_Array<T>::cycle(exint howMany)
{
char *tempPtr;
exint numShift; // The number of items we shift
exint remaining; // mySize - numShift
if (howMany == 0 || mySize < 1) return;
numShift = howMany % (exint)mySize;
if (numShift < 0) numShift += mySize;
remaining = mySize - numShift;
tempPtr = new char[numShift*sizeof(T)];
::memmove(tempPtr, (void *)&myData[remaining], (numShift * sizeof(T)));
::memmove((void *)&myData[numShift], (void *)&myData[0], (remaining * sizeof(T)));
::memmove((void *)&myData[0], tempPtr, (numShift * sizeof(T)));
delete [] tempPtr;
}
template <typename T>
inline void
UT_Array<T>::constant(const T &value)
{
for (exint i = 0; i < mySize; i++)
{
myData[i] = value;
}
}
template <typename T>
inline void
UT_Array<T>::zero()
{
if (isPOD())
::memset((void *)myData, 0, mySize*sizeof(T));
else
trivialConstructRange(myData, mySize);
}
template <typename T>
inline void
UT_Array<T>::setCapacity(exint capacity)
{
// Do nothing when new capacity is the same as the current
if (capacity == myCapacity)
return;
// Special case for non-heap buffers
if (!isHeapBuffer())
{
if (capacity < mySize)
{
// Destroy the extra elements without changing myCapacity
trivialDestructRange(myData + capacity, mySize - capacity);
mySize = capacity;
}
else if (capacity > myCapacity)
{
T *prev = myData;
myData = (T *)malloc(sizeof(T) * capacity);
// myData is safe because we're already a stack buffer
UT_ASSERT_P(isHeapBuffer());
if (mySize > 0)
memcpy((void *)myData, (void *)prev, sizeof(T) * mySize);
myCapacity = capacity;
}
else
{
// Keep myCapacity unchanged in this case
UT_ASSERT_P(capacity >= mySize && capacity <= myCapacity);
}
return;
}
if (capacity == 0)
{
if (myData)
{
trivialDestructRange(myData, mySize);
free(myData);
}
myData = 0;
myCapacity = 0;
mySize = 0;
return;
}
if (capacity < mySize)
{
trivialDestructRange(myData + capacity, mySize - capacity);
mySize = capacity;
}
if (myData)
myData = (T *)realloc(myData, capacity*sizeof(T));
else
myData = (T *)malloc(sizeof(T) * capacity);
// Avoid degenerate case if we happen to be aliased the wrong way
if (!isHeapBuffer())
{
T *prev = myData;
myData = (T *)malloc(sizeof(T) * capacity);
if (mySize > 0)
memcpy((void *)myData, (void *)prev, sizeof(T) * mySize);
ut_ArrayImplFree(prev);
}
myCapacity = capacity;
UT_ASSERT(myData);
}
template <typename T>
inline UT_Array<T> &
UT_Array<T>::operator=(const UT_Array<T> &a)
{
if (this == &a)
return *this;
// Grow the raw array if necessary.
setCapacityIfNeeded(a.size());
// Make sure destructors and constructors are called on all elements
// being removed/added.
trivialDestructRange(myData, mySize);
copyConstructRange(myData, a.myData, a.size());
mySize = a.size();
return *this;
}
template <typename T>
inline UT_Array<T> &
UT_Array<T>::operator=(std::initializer_list<T> a)
{
const exint new_size = a.size();
// Grow the raw array if necessary.
setCapacityIfNeeded(new_size);
// Make sure destructors and constructors are called on all elements
// being removed/added.
trivialDestructRange(myData, mySize);
copyConstructRange(myData, a.begin(), new_size);
mySize = new_size;
return *this;
}
template <typename T>
inline UT_Array<T> &
UT_Array<T>::operator=(UT_Array<T> &&a)
{
if (!a.isHeapBuffer())
{
// Cannot steal from non-heap buffers
clear();
const exint n = a.size();
setCapacityIfNeeded(n);
if (isPOD())
{
if (n > 0)
memcpy(myData, a.myData, n * sizeof(T));
}
else
{
for (exint i = 0; i < n; ++i)
new (&myData[i]) T(std::move(a.myData[i]));
}
mySize = a.mySize;
a.mySize = 0;
return *this;
}
// else, just steal even if we're a small buffer
// Destroy all the elements we're currently holding.
if (myData)
{
trivialDestructRange(myData, mySize);
if (isHeapBuffer())
::free(myData);
}
// Move the contents of the other array to us and empty the other container
// so that it destructs cleanly.
myCapacity = a.myCapacity;
mySize = a.mySize;
myData = a.myData;
a.myCapacity = a.mySize = 0;
a.myData = nullptr;
return *this;
}
template <typename T>
inline bool
UT_Array<T>::operator==(const UT_Array<T> &a) const
{
if (this == &a) return true;
if (mySize != a.size()) return false;
for (exint i = 0; i < mySize; i++)
if (!(myData[i] == a(i))) return false;
return true;
}
template <typename T>
inline bool
UT_Array<T>::operator!=(const UT_Array<T> &a) const
{
return (!operator==(a));
}
}}
#endif // __UT_ARRAYIMPL_H_INCLUDED__
/*
* Copyright (c) 2018 Side Effects Software Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* COMMENTS:
* Special case for arrays that are usually small,
* to avoid a heap allocation when the array really is small.
*/
#pragma once
#ifndef __UT_SMALLARRAY_H_INCLUDED__
#define __UT_SMALLARRAY_H_INCLUDED__
#include <utility>
#include <stddef.h>
namespace igl { namespace FastWindingNumber {
/// An array class with the small buffer optimization, making it ideal for
/// cases when you know it will only contain a few elements at the expense of
/// increasing the object size by MAX_BYTES (subject to alignment).
template <typename T, size_t MAX_BYTES = 64>
class UT_SmallArray : public UT_Array<T>
{
// As many elements that fit into MAX_BYTES with 1 item minimum
enum { MAX_ELEMS = MAX_BYTES/sizeof(T) < 1 ? 1 : MAX_BYTES/sizeof(T) };
public:
// gcc falsely warns about our use of offsetof() on non-POD types. We can't
// easily suppress this because it has to be done in the caller at
// instantiation time. Instead, punt to a runtime check instead.
#if defined(__clang__) || defined(_MSC_VER)
#define UT_SMALL_ARRAY_SIZE_ASSERT() \
using ThisT = UT_SmallArray<T,MAX_BYTES>; \
static_assert(offsetof(ThisT, myBuffer) == sizeof(UT_Array<T>), \
"In order for UT_Array's checks for whether it needs to free the buffer to work, " \
"the buffer must be exactly following the base class memory.")
#else
#define UT_SMALL_ARRAY_SIZE_ASSERT() \
UT_ASSERT_P(!UT_Array<T>::isHeapBuffer());
#endif
/// Default construction
UT_SmallArray()
: UT_Array<T>(/*capacity*/0)
{
UT_Array<T>::unsafeShareData((T*)myBuffer, 0, MAX_ELEMS);
UT_SMALL_ARRAY_SIZE_ASSERT();
}
/// Copy constructor
/// @{
explicit UT_SmallArray(const UT_Array<T> ©)
: UT_Array<T>(/*capacity*/0)
{
UT_Array<T>::unsafeShareData((T*)myBuffer, 0, MAX_ELEMS);
UT_SMALL_ARRAY_SIZE_ASSERT();
UT_Array<T>::operator=(copy);
}
explicit UT_SmallArray(const UT_SmallArray<T,MAX_BYTES> ©)
: UT_Array<T>(/*capacity*/0)
{
UT_Array<T>::unsafeShareData((T*)myBuffer, 0, MAX_ELEMS);
UT_SMALL_ARRAY_SIZE_ASSERT();
UT_Array<T>::operator=(copy);
}
/// @}
/// Move constructor
/// @{
UT_SmallArray(UT_Array<T> &&movable) noexcept
{
UT_Array<T>::unsafeShareData((T*)myBuffer, 0, MAX_ELEMS);
UT_SMALL_ARRAY_SIZE_ASSERT();
UT_Array<T>::operator=(std::move(movable));
}
UT_SmallArray(UT_SmallArray<T,MAX_BYTES> &&movable) noexcept
{
UT_Array<T>::unsafeShareData((T*)myBuffer, 0, MAX_ELEMS);
UT_SMALL_ARRAY_SIZE_ASSERT();
UT_Array<T>::operator=(std::move(movable));
}
/// @}
/// Initializer list constructor
explicit UT_SmallArray(std::initializer_list<T> init)
{
UT_Array<T>::unsafeShareData((T*)myBuffer, 0, MAX_ELEMS);
UT_SMALL_ARRAY_SIZE_ASSERT();
UT_Array<T>::operator=(init);
}
#undef UT_SMALL_ARRAY_SIZE_ASSERT
/// Assignment operator
/// @{
UT_SmallArray<T,MAX_BYTES> &
operator=(const UT_SmallArray<T,MAX_BYTES> ©)
{
UT_Array<T>::operator=(copy);
return *this;
}
UT_SmallArray<T,MAX_BYTES> &
operator=(const UT_Array<T> ©)
{
UT_Array<T>::operator=(copy);
return *this;
}
/// @}
/// Move operator
/// @{
UT_SmallArray<T,MAX_BYTES> &
operator=(UT_SmallArray<T,MAX_BYTES> &&movable)
{
UT_Array<T>::operator=(std::move(movable));
return *this;
}
UT_SmallArray<T,MAX_BYTES> &
operator=(UT_Array<T> &&movable)
{
UT_Array<T>::operator=(std::move(movable));
return *this;
}
/// @}
UT_SmallArray<T,MAX_BYTES> &
operator=(std::initializer_list<T> src)
{
UT_Array<T>::operator=(src);
return *this;
}
private:
alignas(T) char myBuffer[MAX_ELEMS*sizeof(T)];
};
}}
#endif // __UT_SMALLARRAY_H_INCLUDED__
/*
* Copyright (c) 2018 Side Effects Software Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* COMMENTS:
* A vector class templated on its size and data type.
*/
#pragma once
#ifndef __UT_FixedVector__
#define __UT_FixedVector__
namespace igl { namespace FastWindingNumber {
template<typename T,exint SIZE,bool INSTANTIATED=false>
class UT_FixedVector
{
public:
typedef UT_FixedVector<T,SIZE,INSTANTIATED> ThisType;
typedef T value_type;
typedef T theType;
static const exint theSize = SIZE;
T vec[SIZE];
SYS_FORCE_INLINE UT_FixedVector() = default;
/// Initializes every component to the same value
SYS_FORCE_INLINE explicit UT_FixedVector(T that) noexcept
{
for (exint i = 0; i < SIZE; ++i)
vec[i] = that;
}
SYS_FORCE_INLINE UT_FixedVector(const ThisType &that) = default;
SYS_FORCE_INLINE UT_FixedVector(ThisType &&that) = default;
/// Converts vector of S into vector of T,
/// or just copies if same type.
template<typename S,bool S_INSTANTIATED>
SYS_FORCE_INLINE UT_FixedVector(const UT_FixedVector<S,SIZE,S_INSTANTIATED> &that) noexcept
{
for (exint i = 0; i < SIZE; ++i)
vec[i] = that[i];
}
template<typename S>
SYS_FORCE_INLINE UT_FixedVector(const S that[SIZE]) noexcept
{
for (exint i = 0; i < SIZE; ++i)
vec[i] = that[i];
}
SYS_FORCE_INLINE const T &operator[](exint i) const noexcept
{
UT_ASSERT_P(i >= 0 && i < SIZE);
return vec[i];
}
SYS_FORCE_INLINE T &operator[](exint i) noexcept
{
UT_ASSERT_P(i >= 0 && i < SIZE);
return vec[i];
}
SYS_FORCE_INLINE constexpr const T *data() const noexcept
{
return vec;
}
SYS_FORCE_INLINE T *data() noexcept
{
return vec;
}
SYS_FORCE_INLINE ThisType &operator=(const ThisType &that) = default;
SYS_FORCE_INLINE ThisType &operator=(ThisType &&that) = default;
template <typename S,bool S_INSTANTIATED>
SYS_FORCE_INLINE ThisType &operator=(const UT_FixedVector<S,SIZE,S_INSTANTIATED> &that) noexcept
{
for (exint i = 0; i < SIZE; ++i)
vec[i] = that[i];
return *this;
}
SYS_FORCE_INLINE const ThisType &operator=(T that) noexcept
{
for (exint i = 0; i < SIZE; ++i)
vec[i] = that;
return *this;
}
template<typename S,bool S_INSTANTIATED>
SYS_FORCE_INLINE void operator+=(const UT_FixedVector<S,SIZE,S_INSTANTIATED> &that)
{
for (exint i = 0; i < SIZE; ++i)
vec[i] += that[i];
}
SYS_FORCE_INLINE void operator+=(T that)
{
for (exint i = 0; i < SIZE; ++i)
vec[i] += that;
}
template<typename S,bool S_INSTANTIATED>
SYS_FORCE_INLINE auto operator+(const UT_FixedVector<S,SIZE,S_INSTANTIATED> &that) const -> UT_FixedVector<decltype(vec[0]+that[0]),SIZE>
{
using Type = decltype(vec[0]+that[0]);
UT_FixedVector<Type,SIZE> result;
for (exint i = 0; i < SIZE; ++i)
result[i] = vec[i] + that[i];
return result;
}
template<typename S,bool S_INSTANTIATED>
SYS_FORCE_INLINE void operator-=(const UT_FixedVector<S,SIZE,S_INSTANTIATED> &that)
{
for (exint i = 0; i < SIZE; ++i)
vec[i] -= that[i];
}
SYS_FORCE_INLINE void operator-=(T that)
{
for (exint i = 0; i < SIZE; ++i)
vec[i] -= that;
}
template<typename S,bool S_INSTANTIATED>
SYS_FORCE_INLINE auto operator-(const UT_FixedVector<S,SIZE,S_INSTANTIATED> &that) const -> UT_FixedVector<decltype(vec[0]-that[0]),SIZE>
{
using Type = decltype(vec[0]-that[0]);
UT_FixedVector<Type,SIZE> result;
for (exint i = 0; i < SIZE; ++i)
result[i] = vec[i] - that[i];
return result;
}
template<typename S,bool S_INSTANTIATED>
SYS_FORCE_INLINE void operator*=(const UT_FixedVector<S,SIZE,S_INSTANTIATED> &that)
{
for (exint i = 0; i < SIZE; ++i)
vec[i] *= that[i];
}
template<typename S,bool S_INSTANTIATED>
SYS_FORCE_INLINE auto operator*(const UT_FixedVector<S,SIZE,S_INSTANTIATED> &that) const -> UT_FixedVector<decltype(vec[0]*that[0]),SIZE>
{
using Type = decltype(vec[0]*that[0]);
UT_FixedVector<Type,SIZE> result;
for (exint i = 0; i < SIZE; ++i)
result[i] = vec[i] * that[i];
return result;
}
SYS_FORCE_INLINE void operator*=(T that)
{
for (exint i = 0; i < SIZE; ++i)
vec[i] *= that;
}
SYS_FORCE_INLINE UT_FixedVector<T,SIZE> operator*(T that) const
{
UT_FixedVector<T,SIZE> result;
for (exint i = 0; i < SIZE; ++i)
result[i] = vec[i] * that;
return result;
}
template<typename S,bool S_INSTANTIATED>
SYS_FORCE_INLINE void operator/=(const UT_FixedVector<S,SIZE,S_INSTANTIATED> &that)
{
for (exint i = 0; i < SIZE; ++i)
vec[i] /= that[i];
}
template<typename S,bool S_INSTANTIATED>
SYS_FORCE_INLINE auto operator/(const UT_FixedVector<S,SIZE,S_INSTANTIATED> &that) const -> UT_FixedVector<decltype(vec[0]/that[0]),SIZE>
{
using Type = decltype(vec[0]/that[0]);
UT_FixedVector<Type,SIZE> result;
for (exint i = 0; i < SIZE; ++i)
result[i] = vec[i] / that[i];
return result;
}
SYS_FORCE_INLINE void operator/=(T that)
{
if (std::is_integral<T>::value)
{
for (exint i = 0; i < SIZE; ++i)
vec[i] /= that;
}
else
{
that = 1/that;
for (exint i = 0; i < SIZE; ++i)
vec[i] *= that;
}
}
SYS_FORCE_INLINE UT_FixedVector<T,SIZE> operator/(T that) const
{
UT_FixedVector<T,SIZE> result;
if (std::is_integral<T>::value)
{
for (exint i = 0; i < SIZE; ++i)
result[i] = vec[i] / that;
}
else
{
that = 1/that;
for (exint i = 0; i < SIZE; ++i)
result[i] = vec[i] * that;
}
return result;
}
SYS_FORCE_INLINE void negate()
{
for (exint i = 0; i < SIZE; ++i)
vec[i] = -vec[i];
}
SYS_FORCE_INLINE UT_FixedVector<T,SIZE> operator-() const
{
UT_FixedVector<T,SIZE> result;
for (exint i = 0; i < SIZE; ++i)
result[i] = -vec[i];
return result;
}
template<typename S,bool S_INSTANTIATED>
SYS_FORCE_INLINE bool operator==(const UT_FixedVector<S,SIZE,S_INSTANTIATED> &that) const noexcept
{
for (exint i = 0; i < SIZE; ++i)
{
if (vec[i] != T(that[i]))
return false;
}
return true;
}
template<typename S,bool S_INSTANTIATED>
SYS_FORCE_INLINE bool operator!=(const UT_FixedVector<S,SIZE,S_INSTANTIATED> &that) const noexcept
{
return !(*this==that);
}
SYS_FORCE_INLINE bool isZero() const noexcept
{
for (exint i = 0; i < SIZE; ++i)
{
if (vec[i] != T(0))
return false;
}
return true;
}
SYS_FORCE_INLINE T maxComponent() const
{
T v = vec[0];
for (exint i = 1; i < SIZE; ++i)
v = (vec[i] > v) ? vec[i] : v;
return v;
}
SYS_FORCE_INLINE T minComponent() const
{
T v = vec[0];
for (exint i = 1; i < SIZE; ++i)
v = (vec[i] < v) ? vec[i] : v;
return v;
}
SYS_FORCE_INLINE T avgComponent() const
{
T v = vec[0];
for (exint i = 1; i < SIZE; ++i)
v += vec[i];
return v / SIZE;
}
SYS_FORCE_INLINE T length2() const noexcept
{
T a0(vec[0]);
T result(a0*a0);
for (exint i = 1; i < SIZE; ++i)
{
T ai(vec[i]);
result += ai*ai;
}
return result;
}
SYS_FORCE_INLINE T length() const
{
T len2 = length2();
return SYSsqrt(len2);
}
template<typename S,bool S_INSTANTIATED>
SYS_FORCE_INLINE auto dot(const UT_FixedVector<S,SIZE,S_INSTANTIATED> &that) const -> decltype(vec[0]*that[0])
{
using TheType = decltype(vec[0]*that.vec[0]);
TheType result(vec[0]*that[0]);
for (exint i = 1; i < SIZE; ++i)
result += vec[i]*that[i];
return result;
}
template<typename S,bool S_INSTANTIATED>
SYS_FORCE_INLINE auto distance2(const UT_FixedVector<S,SIZE,S_INSTANTIATED> &that) const -> decltype(vec[0]-that[0])
{
using TheType = decltype(vec[0]-that[0]);
TheType v(vec[0] - that[0]);
TheType result(v*v);
for (exint i = 1; i < SIZE; ++i)
{
v = vec[i] - that[i];
result += v*v;
}
return result;
}
template<typename S,bool S_INSTANTIATED>
SYS_FORCE_INLINE auto distance(const UT_FixedVector<S,SIZE,S_INSTANTIATED> &that) const -> decltype(vec[0]-that[0])
{
auto dist2 = distance2(that);
return SYSsqrt(dist2);
}
SYS_FORCE_INLINE T normalize()
{
T len2 = length2();
if (len2 == T(0))
return T(0);
if (len2 == T(1))
return T(1);
T len = SYSsqrt(len2);
// Check if the square root is equal 1. sqrt(1+dx) ~ 1+dx/2,
// so it may get rounded to 1 when it wasn't 1 before.
if (len != T(1))
(*this) /= len;
return len;
}
};
/// NOTE: Strictly speaking, this should use decltype(that*a[0]),
/// but in the interests of avoiding accidental precision escalation,
/// it uses T.
template<typename T,exint SIZE,bool INSTANTIATED,typename S>
SYS_FORCE_INLINE UT_FixedVector<T,SIZE> operator*(const S &that,const UT_FixedVector<T,SIZE,INSTANTIATED> &a)
{
T t(that);
UT_FixedVector<T,SIZE> result;
for (exint i = 0; i < SIZE; ++i)
result[i] = t * a[i];
return result;
}
template<typename T, exint SIZE, bool INSTANTIATED, typename S, bool S_INSTANTIATED>
SYS_FORCE_INLINE auto
dot(const UT_FixedVector<T,SIZE,INSTANTIATED> &a, const UT_FixedVector<S,SIZE,S_INSTANTIATED> &b) -> decltype(a[0]*b[0])
{
return a.dot(b);
}
template<typename T, exint SIZE, bool INSTANTIATED, typename S, bool S_INSTANTIATED>
SYS_FORCE_INLINE auto
SYSmin(const UT_FixedVector<T,SIZE,INSTANTIATED> &a, const UT_FixedVector<S,SIZE,S_INSTANTIATED> &b) -> UT_FixedVector<decltype(a[0]+b[1]), SIZE>
{
using Type = decltype(a[0]+b[1]);
UT_FixedVector<Type, SIZE> result;
for (exint i = 0; i < SIZE; ++i)
result[i] = SYSmin(Type(a[i]), Type(b[i]));
return result;
}
template<typename T, exint SIZE, bool INSTANTIATED, typename S, bool S_INSTANTIATED>
SYS_FORCE_INLINE auto
SYSmax(const UT_FixedVector<T,SIZE,INSTANTIATED> &a, const UT_FixedVector<S,SIZE,S_INSTANTIATED> &b) -> UT_FixedVector<decltype(a[0]+b[1]), SIZE>
{
using Type = decltype(a[0]+b[1]);
UT_FixedVector<Type, SIZE> result;
for (exint i = 0; i < SIZE; ++i)
result[i] = SYSmax(Type(a[i]), Type(b[i]));
return result;
}
template<typename T>
struct UT_FixedVectorTraits
{
typedef UT_FixedVector<T,1> FixedVectorType;
typedef T DataType;
static const exint TupleSize = 1;
static const bool isVectorType = false;
};
template<typename T,exint SIZE,bool INSTANTIATED>
struct UT_FixedVectorTraits<UT_FixedVector<T,SIZE,INSTANTIATED> >
{
typedef UT_FixedVector<T,SIZE,INSTANTIATED> FixedVectorType;
typedef T DataType;
static const exint TupleSize = SIZE;
static const bool isVectorType = true;
};
}}
#endif
/*
* Copyright (c) 2018 Side Effects Software Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* COMMENTS:
* Simple wrappers on tbb interface
*/
#ifndef __UT_ParallelUtil__
#define __UT_ParallelUtil__
#include <thread> // This is just included for std::thread::hardware_concurrency()
namespace igl { namespace FastWindingNumber {
namespace UT_Thread { inline int getNumProcessors() {
return std::thread::hardware_concurrency();
}}
//#include "tbb/blocked_range.h"
//#include "tbb/parallel_for.h"
////namespace tbb { class split; }
//
///// Declare prior to use.
//template <typename T>
//using UT_BlockedRange = tbb::blocked_range<T>;
//
//// Default implementation that calls range.size()
//template< typename RANGE >
//struct UT_EstimatorNumItems
//{
// UT_EstimatorNumItems() {}
//
// size_t operator()(const RANGE& range) const
// {
// return range.size();
// }
//};
//
///// This is needed by UT_CoarsenedRange
//template <typename RANGE>
//inline size_t UTestimatedNumItems(const RANGE& range)
//{
// return UT_EstimatorNumItems<RANGE>()(range);
//}
//
///// UT_CoarsenedRange: This should be used only inside
///// UT_ParallelFor and UT_ParallelReduce
///// This class wraps an existing range with a new range.
///// This allows us to use simple_partitioner, rather than
///// auto_partitioner, which has disastrous performance with
///// the default grain size in ttb 4.
//template< typename RANGE >
//class UT_CoarsenedRange : public RANGE
//{
//public:
// // Compiler-generated versions are fine:
// // ~UT_CoarsenedRange();
// // UT_CoarsenedRange(const UT_CoarsenedRange&);
//
// // Split into two sub-ranges:
// UT_CoarsenedRange(UT_CoarsenedRange& range, tbb::split spl) :
// RANGE(range, spl),
// myGrainSize(range.myGrainSize)
// {
// }
//
// // Inherited: bool empty() const
//
// bool is_divisible() const
// {
// return
// RANGE::is_divisible() &&
// (UTestimatedNumItems(static_cast<const RANGE&>(*this)) > myGrainSize);
// }
//
//private:
// size_t myGrainSize;
//
// UT_CoarsenedRange(const RANGE& base_range, const size_t grain_size) :
// RANGE(base_range),
// myGrainSize(grain_size)
// {
// }
//
// template <typename Range, typename Body>
// friend void UTparallelFor(
// const Range &range, const Body &body,
// const int subscribe_ratio, const int min_grain_size
// );
//};
//
///// Run the @c body function over a range in parallel.
///// UTparallelFor attempts to spread the range out over at most
///// subscribe_ratio * num_processor tasks.
///// The factor subscribe_ratio can be used to help balance the load.
///// UTparallelFor() uses tbb for its implementation.
///// The used grain size is the maximum of min_grain_size and
///// if UTestimatedNumItems(range) / (subscribe_ratio * num_processor).
///// If subscribe_ratio == 0, then a grain size of min_grain_size will be used.
///// A range can be split only when UTestimatedNumItems(range) exceeds the
///// grain size the range is divisible.
//
/////
///// Requirements for the Range functor are:
///// - the requirements of the tbb Range Concept
///// - UT_estimatorNumItems<Range> must return the the estimated number of work items
///// for the range. When Range::size() is not the correct estimate, then a
///// (partial) specialization of UT_estimatorNumItemsimatorRange must be provided
///// for the type Range.
/////
///// Requirements for the Body function are:
///// - @code Body(const Body &); @endcode @n
///// Copy Constructor
///// - @code Body()::~Body(); @endcode @n
///// Destructor
///// - @code void Body::operator()(const Range &range) const; @endcode
///// Function call to perform operation on the range. Note the operator is
///// @b const.
/////
///// The requirements for a Range object are:
///// - @code Range::Range(const Range&); @endcode @n
///// Copy constructor
///// - @code Range::~Range(); @endcode @n
///// Destructor
///// - @code bool Range::is_divisible() const; @endcode @n
///// True if the range can be partitioned into two sub-ranges
///// - @code bool Range::empty() const; @endcode @n
///// True if the range is empty
///// - @code Range::Range(Range &r, UT_Split) const; @endcode @n
///// Split the range @c r into two sub-ranges (i.e. modify @c r and *this)
/////
///// Example: @code
///// class Square {
///// public:
///// Square(double *data) : myData(data) {}
///// ~Square();
///// void operator()(const UT_BlockedRange<int64> &range) const
///// {
///// for (int64 i = range.begin(); i != range.end(); ++i)
///// myData[i] *= myData[i];
///// }
///// double *myData;
///// };
///// ...
/////
///// void
///// parallel_square(double *array, int64 length)
///// {
///// UTparallelFor(UT_BlockedRange<int64>(0, length), Square(array));
///// }
///// @endcode
/////
///// @see UTparallelReduce(), UT_BlockedRange()
//
//template <typename Range, typename Body>
//void UTparallelFor(
// const Range &range, const Body &body,
// const int subscribe_ratio = 2,
// const int min_grain_size = 1
//)
//{
// const size_t num_processors( UT_Thread::getNumProcessors() );
//
// UT_ASSERT( num_processors >= 1 );
// UT_ASSERT( min_grain_size >= 1 );
// UT_ASSERT( subscribe_ratio >= 0 );
//
// const size_t est_range_size( UTestimatedNumItems(range) );
//
// // Don't run on an empty range!
// if (est_range_size == 0)
// return;
//
// // Avoid tbb overhead if entire range needs to be single threaded
// if (num_processors == 1 || est_range_size <= min_grain_size)
// {
// body(range);
// return;
// }
//
// size_t grain_size(min_grain_size);
// if( subscribe_ratio > 0 )
// grain_size = std::max(
// grain_size,
// est_range_size / (subscribe_ratio * num_processors)
// );
//
// UT_CoarsenedRange< Range > coarsened_range(range, grain_size);
//
// tbb::parallel_for(coarsened_range, body, tbb::simple_partitioner());
//}
//
///// Version of UTparallelFor that is tuned for the case where the range
///// consists of lightweight items, for example,
///// float additions or matrix-vector multiplications.
//template <typename Range, typename Body>
//void
//UTparallelForLightItems(const Range &range, const Body &body)
//{
// UTparallelFor(range, body, 2, 1024);
//}
//
///// UTserialFor can be used as a debugging tool to quickly replace a parallel
///// for with a serial for.
//template <typename Range, typename Body>
//void UTserialFor(const Range &range, const Body &body)
// { body(range); }
//
}}
#endif
/*
* Copyright (c) 2018 Side Effects Software Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* COMMENTS:
* Bounding Volume Hierarchy (BVH) implementation.
* To call functions not implemented here, also include UT_BVHImpl.h
*/
#pragma once
#ifndef __HDK_UT_BVH_h__
#define __HDK_UT_BVH_h__
#include <limits>
#include <memory>
namespace igl { namespace FastWindingNumber {
template<typename T> class UT_Array;
class v4uf;
class v4uu;
namespace HDK_Sample {
namespace UT {
template<typename T,uint NAXES>
struct Box {
T vals[NAXES][2];
SYS_FORCE_INLINE Box() noexcept = default;
SYS_FORCE_INLINE constexpr Box(const Box &other) noexcept = default;
SYS_FORCE_INLINE constexpr Box(Box &&other) noexcept = default;
SYS_FORCE_INLINE Box& operator=(const Box &other) noexcept = default;
SYS_FORCE_INLINE Box& operator=(Box &&other) noexcept = default;
template<typename S>
SYS_FORCE_INLINE Box(const Box<S,NAXES>& other) noexcept {
static_assert((std::is_pod<Box<T,NAXES>>::value) || !std::is_pod<T>::value,
"UT::Box should be POD, for better performance in UT_Array, etc.");
for (uint axis = 0; axis < NAXES; ++axis) {
vals[axis][0] = T(other.vals[axis][0]);
vals[axis][1] = T(other.vals[axis][1]);
}
}
template<typename S,bool INSTANTIATED>
SYS_FORCE_INLINE Box(const UT_FixedVector<S,NAXES,INSTANTIATED>& pt) noexcept {
for (uint axis = 0; axis < NAXES; ++axis) {
vals[axis][0] = pt[axis];
vals[axis][1] = pt[axis];
}
}
template<typename S>
SYS_FORCE_INLINE Box& operator=(const Box<S,NAXES>& other) noexcept {
for (uint axis = 0; axis < NAXES; ++axis) {
vals[axis][0] = T(other.vals[axis][0]);
vals[axis][1] = T(other.vals[axis][1]);
}
return *this;
}
SYS_FORCE_INLINE const T* operator[](const size_t axis) const noexcept {
UT_ASSERT_P(axis < NAXES);
return vals[axis];
}
SYS_FORCE_INLINE T* operator[](const size_t axis) noexcept {
UT_ASSERT_P(axis < NAXES);
return vals[axis];
}
SYS_FORCE_INLINE void initBounds() noexcept {
for (uint axis = 0; axis < NAXES; ++axis) {
vals[axis][0] = std::numeric_limits<T>::max();
vals[axis][1] = -std::numeric_limits<T>::max();
}
}
/// Copy the source box.
/// NOTE: This is so that in templated code that may have a Box or a
/// UT_FixedVector, it can call initBounds and still work.
SYS_FORCE_INLINE void initBounds(const Box<T,NAXES>& src) noexcept {
for (uint axis = 0; axis < NAXES; ++axis) {
vals[axis][0] = src.vals[axis][0];
vals[axis][1] = src.vals[axis][1];
}
}
/// Initialize with the union of the source boxes.
/// NOTE: This is so that in templated code that may have Box's or a
/// UT_FixedVector's, it can call initBounds and still work.
SYS_FORCE_INLINE void initBoundsUnordered(const Box<T,NAXES>& src0, const Box<T,NAXES>& src1) noexcept {
for (uint axis = 0; axis < NAXES; ++axis) {
vals[axis][0] = SYSmin(src0.vals[axis][0], src1.vals[axis][0]);
vals[axis][1] = SYSmax(src0.vals[axis][1], src1.vals[axis][1]);
}
}
SYS_FORCE_INLINE void combine(const Box<T,NAXES>& src) noexcept {
for (uint axis = 0; axis < NAXES; ++axis) {
T& minv = vals[axis][0];
T& maxv = vals[axis][1];
const T curminv = src.vals[axis][0];
const T curmaxv = src.vals[axis][1];
minv = (minv < curminv) ? minv : curminv;
maxv = (maxv > curmaxv) ? maxv : curmaxv;
}
}
SYS_FORCE_INLINE void enlargeBounds(const Box<T,NAXES>& src) noexcept {
combine(src);
}
template<typename S,bool INSTANTIATED>
SYS_FORCE_INLINE
void initBounds(const UT_FixedVector<S,NAXES,INSTANTIATED>& pt) noexcept {
for (uint axis = 0; axis < NAXES; ++axis) {
vals[axis][0] = pt[axis];
vals[axis][1] = pt[axis];
}
}
template<bool INSTANTIATED>
SYS_FORCE_INLINE
void initBounds(const UT_FixedVector<T,NAXES,INSTANTIATED>& min, const UT_FixedVector<T,NAXES,INSTANTIATED>& max) noexcept {
for (uint axis = 0; axis < NAXES; ++axis) {
vals[axis][0] = min[axis];
vals[axis][1] = max[axis];
}
}
template<bool INSTANTIATED>
SYS_FORCE_INLINE
void initBoundsUnordered(const UT_FixedVector<T,NAXES,INSTANTIATED>& p0, const UT_FixedVector<T,NAXES,INSTANTIATED>& p1) noexcept {
for (uint axis = 0; axis < NAXES; ++axis) {
vals[axis][0] = SYSmin(p0[axis], p1[axis]);
vals[axis][1] = SYSmax(p0[axis], p1[axis]);
}
}
template<bool INSTANTIATED>
SYS_FORCE_INLINE
void enlargeBounds(const UT_FixedVector<T,NAXES,INSTANTIATED>& pt) noexcept {
for (uint axis = 0; axis < NAXES; ++axis) {
vals[axis][0] = SYSmin(vals[axis][0], pt[axis]);
vals[axis][1] = SYSmax(vals[axis][1], pt[axis]);
}
}
SYS_FORCE_INLINE
UT_FixedVector<T,NAXES> getMin() const noexcept {
UT_FixedVector<T,NAXES> v;
for (uint axis = 0; axis < NAXES; ++axis) {
v[axis] = vals[axis][0];
}
return v;
}
SYS_FORCE_INLINE
UT_FixedVector<T,NAXES> getMax() const noexcept {
UT_FixedVector<T,NAXES> v;
for (uint axis = 0; axis < NAXES; ++axis) {
v[axis] = vals[axis][1];
}
return v;
}
T diameter2() const noexcept {
T diff = (vals[0][1]-vals[0][0]);
T sum = diff*diff;
for (uint axis = 1; axis < NAXES; ++axis) {
diff = (vals[axis][1]-vals[axis][0]);
sum += diff*diff;
}
return sum;
}
T volume() const noexcept {
T product = (vals[0][1]-vals[0][0]);
for (uint axis = 1; axis < NAXES; ++axis) {
product *= (vals[axis][1]-vals[axis][0]);
}
return product;
}
T half_surface_area() const noexcept {
if (NAXES==1) {
// NOTE: Although this should technically be 1,
// that doesn't make any sense as a heuristic,
// so we fall back to the "volume" of this box.
return (vals[0][1]-vals[0][0]);
}
if (NAXES==2) {
const T d0 = (vals[0][1]-vals[0][0]);
const T d1 = (vals[1][1]-vals[1][0]);
return d0 + d1;
}
if (NAXES==3) {
const T d0 = (vals[0][1]-vals[0][0]);
const T d1 = (vals[1][1]-vals[1][0]);
const T d2 = (vals[2][1]-vals[2][0]);
return d0*d1 + d1*d2 + d2*d0;
}
if (NAXES==4) {
const T d0 = (vals[0][1]-vals[0][0]);
const T d1 = (vals[1][1]-vals[1][0]);
const T d2 = (vals[2][1]-vals[2][0]);
const T d3 = (vals[3][1]-vals[3][0]);
// This is just d0d1d2 + d1d2d3 + d2d3d0 + d3d0d1 refactored.
const T d0d1 = d0*d1;
const T d2d3 = d2*d3;
return d0d1*(d2+d3) + d2d3*(d0+d1);
}
T sum = 0;
for (uint skipped_axis = 0; skipped_axis < NAXES; ++skipped_axis) {
T product = 1;
for (uint axis = 0; axis < NAXES; ++axis) {
if (axis != skipped_axis) {
product *= (vals[axis][1]-vals[axis][0]);
}
}
sum += product;
}
return sum;
}
T axis_sum() const noexcept {
T sum = (vals[0][1]-vals[0][0]);
for (uint axis = 1; axis < NAXES; ++axis) {
sum += (vals[axis][1]-vals[axis][0]);
}
return sum;
}
template<bool INSTANTIATED0,bool INSTANTIATED1>
SYS_FORCE_INLINE void intersect(
T &box_tmin,
T &box_tmax,
const UT_FixedVector<uint,NAXES,INSTANTIATED0> &signs,
const UT_FixedVector<T,NAXES,INSTANTIATED1> &origin,
const UT_FixedVector<T,NAXES,INSTANTIATED1> &inverse_direction
) const noexcept {
for (int axis = 0; axis < NAXES; ++axis)
{
uint sign = signs[axis];
T t1 = (vals[axis][sign] - origin[axis]) * inverse_direction[axis];
T t2 = (vals[axis][sign^1] - origin[axis]) * inverse_direction[axis];
box_tmin = SYSmax(t1, box_tmin);
box_tmax = SYSmin(t2, box_tmax);
}
}
SYS_FORCE_INLINE void intersect(const Box& other, Box& dest) const noexcept {
for (int axis = 0; axis < NAXES; ++axis)
{
dest.vals[axis][0] = SYSmax(vals[axis][0], other.vals[axis][0]);
dest.vals[axis][1] = SYSmin(vals[axis][1], other.vals[axis][1]);
}
}
template<bool INSTANTIATED>
SYS_FORCE_INLINE T minDistance2(
const UT_FixedVector<T,NAXES,INSTANTIATED> &p
) const noexcept {
T diff = SYSmax(SYSmax(vals[0][0]-p[0], p[0]-vals[0][1]), T(0.0f));
T d2 = diff*diff;
for (int axis = 1; axis < NAXES; ++axis)
{
diff = SYSmax(SYSmax(vals[axis][0]-p[axis], p[axis]-vals[axis][1]), T(0.0f));
d2 += diff*diff;
}
return d2;
}
template<bool INSTANTIATED>
SYS_FORCE_INLINE T maxDistance2(
const UT_FixedVector<T,NAXES,INSTANTIATED> &p
) const noexcept {
T diff = SYSmax(p[0]-vals[0][0], vals[0][1]-p[0]);
T d2 = diff*diff;
for (int axis = 1; axis < NAXES; ++axis)
{
diff = SYSmax(p[axis]-vals[axis][0], vals[axis][1]-p[axis]);
d2 += diff*diff;
}
return d2;
}
};
/// Used by BVH::init to specify the heuristic to use for choosing between different box splits.
/// I tried putting this inside the BVH class, but I had difficulty getting it to compile.
enum class BVH_Heuristic {
/// Tries to minimize the sum of axis lengths of the boxes.
/// This is useful for applications where the probability of a box being applicable to a
/// query is proportional to the "length", e.g. the probability of a random infinite plane
/// intersecting the box.
BOX_PERIMETER,
/// Tries to minimize the "surface area" of the boxes.
/// In 3D, uses the surface area; in 2D, uses the perimeter; in 1D, uses the axis length.
/// This is what most applications, e.g. ray tracing, should use, particularly when the
/// probability of a box being applicable to a query is proportional to the surface "area",
/// e.g. the probability of a random ray hitting the box.
///
/// NOTE: USE THIS ONE IF YOU ARE UNSURE!
BOX_AREA,
/// Tries to minimize the "volume" of the boxes.
/// Uses the product of all axis lengths as a heuristic, (volume in 3D, area in 2D, length in 1D).
/// This is useful for applications where the probability of a box being applicable to a
/// query is proportional to the "volume", e.g. the probability of a random point being inside the box.
BOX_VOLUME,
/// Tries to minimize the "radii" of the boxes (i.e. the distance from the centre to a corner).
/// This is useful for applications where the probability of a box being applicable to a
/// query is proportional to the distance to the box centre, e.g. the probability of a random
/// infinite plane being within the "radius" of the centre.
BOX_RADIUS,
/// Tries to minimize the squared "radii" of the boxes (i.e. the squared distance from the centre to a corner).
/// This is useful for applications where the probability of a box being applicable to a
/// query is proportional to the squared distance to the box centre, e.g. the probability of a random
/// ray passing within the "radius" of the centre.
BOX_RADIUS2,
/// Tries to minimize the cubed "radii" of the boxes (i.e. the cubed distance from the centre to a corner).
/// This is useful for applications where the probability of a box being applicable to a
/// query is proportional to the cubed distance to the box centre, e.g. the probability of a random
/// point being within the "radius" of the centre.
BOX_RADIUS3,
/// Tries to minimize the depth of the tree by primarily splitting at the median of the max axis.
/// It may fall back to minimizing the area, but the tree depth should be unaffected.
///
/// FIXME: This is not fully implemented yet.
MEDIAN_MAX_AXIS
};
template<uint N>
class BVH {
public:
using INT_TYPE = uint;
struct Node {
INT_TYPE child[N];
static constexpr INT_TYPE theN = N;
static constexpr INT_TYPE EMPTY = INT_TYPE(-1);
static constexpr INT_TYPE INTERNAL_BIT = (INT_TYPE(1)<<(sizeof(INT_TYPE)*8 - 1));
SYS_FORCE_INLINE static INT_TYPE markInternal(INT_TYPE internal_node_num) noexcept {
return internal_node_num | INTERNAL_BIT;
}
SYS_FORCE_INLINE static bool isInternal(INT_TYPE node_int) noexcept {
return (node_int & INTERNAL_BIT) != 0;
}
SYS_FORCE_INLINE static INT_TYPE getInternalNum(INT_TYPE node_int) noexcept {
return node_int & ~INTERNAL_BIT;
}
};
private:
struct FreeDeleter {
SYS_FORCE_INLINE void operator()(Node* p) const {
if (p) {
// The pointer was allocated with malloc by UT_Array,
// so it must be freed with free.
free(p);
}
}
};
std::unique_ptr<Node[],FreeDeleter> myRoot;
INT_TYPE myNumNodes;
public:
SYS_FORCE_INLINE BVH() noexcept : myRoot(nullptr), myNumNodes(0) {}
template<BVH_Heuristic H,typename T,uint NAXES,typename BOX_TYPE,typename SRC_INT_TYPE=INT_TYPE>
inline void init(const BOX_TYPE* boxes, const INT_TYPE nboxes, SRC_INT_TYPE* indices=nullptr, bool reorder_indices=false, INT_TYPE max_items_per_leaf=1) noexcept;
template<BVH_Heuristic H,typename T,uint NAXES,typename BOX_TYPE,typename SRC_INT_TYPE=INT_TYPE>
inline void init(Box<T,NAXES> axes_minmax, const BOX_TYPE* boxes, INT_TYPE nboxes, SRC_INT_TYPE* indices=nullptr, bool reorder_indices=false, INT_TYPE max_items_per_leaf=1) noexcept;
SYS_FORCE_INLINE
INT_TYPE getNumNodes() const noexcept
{
return myNumNodes;
}
SYS_FORCE_INLINE
const Node *getNodes() const noexcept
{
return myRoot.get();
}
SYS_FORCE_INLINE
void clear() noexcept {
myRoot.reset();
myNumNodes = 0;
}
/// For each node, this effectively does:
/// LOCAL_DATA local_data[MAX_ORDER];
/// bool descend = functors.pre(nodei, parent_data);
/// if (!descend)
/// return;
/// for each child {
/// if (isitem(child))
/// functors.item(getitemi(child), nodei, local_data[child]);
/// else if (isnode(child))
/// recurse(getnodei(child), local_data);
/// }
/// functors.post(nodei, parent_nodei, data_for_parent, num_children, local_data);
template<typename LOCAL_DATA,typename FUNCTORS>
inline void traverse(
FUNCTORS &functors,
LOCAL_DATA *data_for_parent=nullptr) const noexcept;
/// This acts like the traverse function, except if the number of nodes in two subtrees
/// of a node contain at least parallel_threshold nodes, they may be executed in parallel.
/// If parallel_threshold is 0, even item_functor may be executed on items in parallel.
/// NOTE: Make sure that your functors don't depend on the order that they're executed in,
/// e.g. don't add values from sibling nodes together except in post functor,
/// else they might have nondeterministic roundoff or miss some values entirely.
template<typename LOCAL_DATA,typename FUNCTORS>
inline void traverseParallel(
INT_TYPE parallel_threshold,
FUNCTORS &functors,
LOCAL_DATA *data_for_parent=nullptr) const noexcept;
/// For each node, this effectively does:
/// LOCAL_DATA local_data[MAX_ORDER];
/// uint descend = functors.pre(nodei, parent_data);
/// if (!descend)
/// return;
/// for each child {
/// if (!(descend & (1<<child)))
/// continue;
/// if (isitem(child))
/// functors.item(getitemi(child), nodei, local_data[child]);
/// else if (isnode(child))
/// recurse(getnodei(child), local_data);
/// }
/// functors.post(nodei, parent_nodei, data_for_parent, num_children, local_data);
template<typename LOCAL_DATA,typename FUNCTORS>
inline void traverseVector(
FUNCTORS &functors,
LOCAL_DATA *data_for_parent=nullptr) const noexcept;
/// Prints a text representation of the tree to stdout.
inline void debugDump() const;
template<typename SRC_INT_TYPE>
static inline void createTrivialIndices(SRC_INT_TYPE* indices, const INT_TYPE n) noexcept;
private:
template<typename LOCAL_DATA,typename FUNCTORS>
inline void traverseHelper(
INT_TYPE nodei,
INT_TYPE parent_nodei,
FUNCTORS &functors,
LOCAL_DATA *data_for_parent=nullptr) const noexcept;
template<typename LOCAL_DATA,typename FUNCTORS>
inline void traverseParallelHelper(
INT_TYPE nodei,
INT_TYPE parent_nodei,
INT_TYPE parallel_threshold,
INT_TYPE next_node_id,
FUNCTORS &functors,
LOCAL_DATA *data_for_parent=nullptr) const noexcept;
template<typename LOCAL_DATA,typename FUNCTORS>
inline void traverseVectorHelper(
INT_TYPE nodei,
INT_TYPE parent_nodei,
FUNCTORS &functors,
LOCAL_DATA *data_for_parent=nullptr) const noexcept;
template<typename T,uint NAXES,typename BOX_TYPE,typename SRC_INT_TYPE>
static inline void computeFullBoundingBox(Box<T,NAXES>& axes_minmax, const BOX_TYPE* boxes, const INT_TYPE nboxes, SRC_INT_TYPE* indices) noexcept;
template<BVH_Heuristic H,typename T,uint NAXES,typename BOX_TYPE,typename SRC_INT_TYPE>
static inline void initNode(UT_Array<Node>& nodes, Node &node, const Box<T,NAXES>& axes_minmax, const BOX_TYPE* boxes, SRC_INT_TYPE* indices, const INT_TYPE nboxes) noexcept;
template<BVH_Heuristic H,typename T,uint NAXES,typename BOX_TYPE,typename SRC_INT_TYPE>
static inline void initNodeReorder(UT_Array<Node>& nodes, Node &node, const Box<T,NAXES>& axes_minmax, const BOX_TYPE* boxes, SRC_INT_TYPE* indices, const INT_TYPE nboxes, const INT_TYPE indices_offset, const INT_TYPE max_items_per_leaf) noexcept;
template<BVH_Heuristic H,typename T,uint NAXES,typename BOX_TYPE,typename SRC_INT_TYPE>
static inline void multiSplit(const Box<T,NAXES>& axes_minmax, const BOX_TYPE* boxes, SRC_INT_TYPE* indices, INT_TYPE nboxes, SRC_INT_TYPE* sub_indices[N+1], Box<T,NAXES> sub_boxes[N]) noexcept;
template<BVH_Heuristic H,typename T,uint NAXES,typename BOX_TYPE,typename SRC_INT_TYPE>
static inline void split(const Box<T,NAXES>& axes_minmax, const BOX_TYPE* boxes, SRC_INT_TYPE* indices, INT_TYPE nboxes, SRC_INT_TYPE*& split_indices, Box<T,NAXES>* split_boxes) noexcept;
template<INT_TYPE PARALLEL_THRESHOLD, typename SRC_INT_TYPE>
static inline void adjustParallelChildNodes(INT_TYPE nparallel, UT_Array<Node>& nodes, Node& node, UT_Array<Node>* parallel_nodes, SRC_INT_TYPE* sub_indices) noexcept;
template<typename T,typename BOX_TYPE,typename SRC_INT_TYPE>
static inline void nthElement(const BOX_TYPE* boxes, SRC_INT_TYPE* indices, const SRC_INT_TYPE* indices_end, const uint axis, SRC_INT_TYPE*const nth) noexcept;
template<typename T,typename BOX_TYPE,typename SRC_INT_TYPE>
static inline void partitionByCentre(const BOX_TYPE* boxes, SRC_INT_TYPE*const indices, const SRC_INT_TYPE*const indices_end, const uint axis, const T pivotx2, SRC_INT_TYPE*& ppivot_start, SRC_INT_TYPE*& ppivot_end) noexcept;
/// An overestimate of the number of nodes needed.
/// At worst, we could have only 2 children in every leaf, and
/// then above that, we have a geometric series with r=1/N and a=(sub_nboxes/2)/N
/// The true worst case might be a little worst than this, but
/// it's probably fairly unlikely.
SYS_FORCE_INLINE static INT_TYPE nodeEstimate(const INT_TYPE nboxes) noexcept {
return nboxes/2 + nboxes/(2*(N-1));
}
template<BVH_Heuristic H,typename T, uint NAXES>
SYS_FORCE_INLINE static T unweightedHeuristic(const Box<T, NAXES>& box) noexcept {
if (H == BVH_Heuristic::BOX_PERIMETER) {
return box.axis_sum();
}
if (H == BVH_Heuristic::BOX_AREA) {
return box.half_surface_area();
}
if (H == BVH_Heuristic::BOX_VOLUME) {
return box.volume();
}
if (H == BVH_Heuristic::BOX_RADIUS) {
T diameter2 = box.diameter2();
return SYSsqrt(diameter2);
}
if (H == BVH_Heuristic::BOX_RADIUS2) {
return box.diameter2();
}
if (H == BVH_Heuristic::BOX_RADIUS3) {
T diameter2 = box.diameter2();
return diameter2*SYSsqrt(diameter2);
}
UT_ASSERT_MSG(0, "BVH_Heuristic::MEDIAN_MAX_AXIS should be handled separately by caller!");
return T(1);
}
/// 16 equal-length spans (15 evenly-spaced splits) should be enough for a decent heuristic
static constexpr INT_TYPE NSPANS = 16;
static constexpr INT_TYPE NSPLITS = NSPANS-1;
/// At least 1/16 of all boxes must be on each side, else we could end up with a very deep tree
static constexpr INT_TYPE MIN_FRACTION = 16;
};
} // UT namespace
template<uint N>
using UT_BVH = UT::BVH<N>;
} // End HDK_Sample namespace
}}
#endif
/*
* Copyright (c) 2018 Side Effects Software Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* COMMENTS:
* Bounding Volume Hierarchy (BVH) implementation.
* The main file is UT_BVH.h; this file is separate so that
* files that don't actually need to call functions on the BVH
* won't have unnecessary headers and functions included.
*/
#pragma once
#ifndef __HDK_UT_BVHImpl_h__
#define __HDK_UT_BVHImpl_h__
#include <igl/parallel_for.h>
#include <iostream>
#include <algorithm>
namespace igl { namespace FastWindingNumber {
namespace HDK_Sample {
namespace UT {
template<typename T,uint NAXES>
SYS_FORCE_INLINE bool utBoxExclude(const UT::Box<T,NAXES>& box) noexcept {
bool has_nan_or_inf = !SYSisFinite(box[0][0]);
has_nan_or_inf |= !SYSisFinite(box[0][1]);
for (uint axis = 1; axis < NAXES; ++axis)
{
has_nan_or_inf |= !SYSisFinite(box[axis][0]);
has_nan_or_inf |= !SYSisFinite(box[axis][1]);
}
return has_nan_or_inf;
}
template<uint NAXES>
SYS_FORCE_INLINE bool utBoxExclude(const UT::Box<fpreal32,NAXES>& box) noexcept {
const int32 *pboxints = reinterpret_cast<const int32*>(&box);
// Fast check for NaN or infinity: check if exponent bits are 0xFF.
bool has_nan_or_inf = ((pboxints[0] & 0x7F800000) == 0x7F800000);
has_nan_or_inf |= ((pboxints[1] & 0x7F800000) == 0x7F800000);
for (uint axis = 1; axis < NAXES; ++axis)
{
has_nan_or_inf |= ((pboxints[2*axis] & 0x7F800000) == 0x7F800000);
has_nan_or_inf |= ((pboxints[2*axis + 1] & 0x7F800000) == 0x7F800000);
}
return has_nan_or_inf;
}
template<typename T,uint NAXES>
SYS_FORCE_INLINE T utBoxCenter(const UT::Box<T,NAXES>& box, uint axis) noexcept {
const T* v = box.vals[axis];
return v[0] + v[1];
}
template<typename T>
struct ut_BoxCentre {
constexpr static uint scale = 2;
};
template<typename T,uint NAXES,bool INSTANTIATED>
SYS_FORCE_INLINE T utBoxExclude(const UT_FixedVector<T,NAXES,INSTANTIATED>& position) noexcept {
bool has_nan_or_inf = !SYSisFinite(position[0]);
for (uint axis = 1; axis < NAXES; ++axis)
has_nan_or_inf |= !SYSisFinite(position[axis]);
return has_nan_or_inf;
}
template<uint NAXES,bool INSTANTIATED>
SYS_FORCE_INLINE bool utBoxExclude(const UT_FixedVector<fpreal32,NAXES,INSTANTIATED>& position) noexcept {
const int32 *ppositionints = reinterpret_cast<const int32*>(&position);
// Fast check for NaN or infinity: check if exponent bits are 0xFF.
bool has_nan_or_inf = ((ppositionints[0] & 0x7F800000) == 0x7F800000);
for (uint axis = 1; axis < NAXES; ++axis)
has_nan_or_inf |= ((ppositionints[axis] & 0x7F800000) == 0x7F800000);
return has_nan_or_inf;
}
template<typename T,uint NAXES,bool INSTANTIATED>
SYS_FORCE_INLINE T utBoxCenter(const UT_FixedVector<T,NAXES,INSTANTIATED>& position, uint axis) noexcept {
return position[axis];
}
template<typename T,uint NAXES,bool INSTANTIATED>
struct ut_BoxCentre<UT_FixedVector<T,NAXES,INSTANTIATED>> {
constexpr static uint scale = 1;
};
template<typename BOX_TYPE,typename SRC_INT_TYPE,typename INT_TYPE>
inline INT_TYPE utExcludeNaNInfBoxIndices(const BOX_TYPE* boxes, SRC_INT_TYPE* indices, INT_TYPE& nboxes) noexcept
{
constexpr INT_TYPE PARALLEL_THRESHOLD = 65536;
INT_TYPE ntasks = 1;
//if (nboxes >= PARALLEL_THRESHOLD)
//{
// INT_TYPE nprocessors = UT_Thread::getNumProcessors();
// ntasks = (nprocessors > 1) ? SYSmin(4*nprocessors, nboxes/(PARALLEL_THRESHOLD/2)) : 1;
//}
//if (ntasks == 1)
{
// Serial: easy case; just loop through.
const SRC_INT_TYPE* indices_end = indices + nboxes;
// Loop through forward once
SRC_INT_TYPE* psrc_index = indices;
for (; psrc_index != indices_end; ++psrc_index)
{
const bool exclude = utBoxExclude(boxes[*psrc_index]);
if (exclude)
break;
}
if (psrc_index == indices_end)
return 0;
// First NaN or infinite box
SRC_INT_TYPE* nan_start = psrc_index;
for (++psrc_index; psrc_index != indices_end; ++psrc_index)
{
const bool exclude = utBoxExclude(boxes[*psrc_index]);
if (!exclude)
{
*nan_start = *psrc_index;
++nan_start;
}
}
nboxes = nan_start-indices;
return indices_end - nan_start;
}
}
template<uint N>
template<BVH_Heuristic H,typename T,uint NAXES,typename BOX_TYPE,typename SRC_INT_TYPE>
inline void BVH<N>::init(const BOX_TYPE* boxes, const INT_TYPE nboxes, SRC_INT_TYPE* indices, bool reorder_indices, INT_TYPE max_items_per_leaf) noexcept {
Box<T,NAXES> axes_minmax;
computeFullBoundingBox(axes_minmax, boxes, nboxes, indices);
init<H>(axes_minmax, boxes, nboxes, indices, reorder_indices, max_items_per_leaf);
}
template<uint N>
template<BVH_Heuristic H,typename T,uint NAXES,typename BOX_TYPE,typename SRC_INT_TYPE>
inline void BVH<N>::init(Box<T,NAXES> axes_minmax, const BOX_TYPE* boxes, INT_TYPE nboxes, SRC_INT_TYPE* indices, bool reorder_indices, INT_TYPE max_items_per_leaf) noexcept {
// Clear the tree in advance to save memory.
myRoot.reset();
if (nboxes == 0) {
myNumNodes = 0;
return;
}
UT_Array<INT_TYPE> local_indices;
if (!indices) {
local_indices.setSizeNoInit(nboxes);
indices = local_indices.array();
createTrivialIndices(indices, nboxes);
}
// Exclude any boxes with NaNs or infinities by shifting down indices
// over the bad box indices and updating nboxes.
INT_TYPE nexcluded = utExcludeNaNInfBoxIndices(boxes, indices, nboxes);
if (nexcluded != 0) {
if (nboxes == 0) {
myNumNodes = 0;
return;
}
computeFullBoundingBox(axes_minmax, boxes, nboxes, indices);
}
UT_Array<Node> nodes;
// Preallocate an overestimate of the number of nodes needed.
nodes.setCapacity(nodeEstimate(nboxes));
nodes.setSize(1);
if (reorder_indices)
initNodeReorder<H>(nodes, nodes[0], axes_minmax, boxes, indices, nboxes, 0, max_items_per_leaf);
else
initNode<H>(nodes, nodes[0], axes_minmax, boxes, indices, nboxes);
// If capacity is more than 12.5% over the size, rellocate.
if (8*nodes.capacity() > 9*nodes.size()) {
nodes.setCapacity(nodes.size());
}
// Steal ownership of the array from the UT_Array
myRoot.reset(nodes.array());
myNumNodes = nodes.size();
nodes.unsafeClearData();
}
template<uint N>
template<typename LOCAL_DATA,typename FUNCTORS>
inline void BVH<N>::traverse(
FUNCTORS &functors,
LOCAL_DATA* data_for_parent) const noexcept
{
if (!myRoot)
return;
// NOTE: The root is always index 0.
traverseHelper(0, INT_TYPE(-1), functors, data_for_parent);
}
template<uint N>
template<typename LOCAL_DATA,typename FUNCTORS>
inline void BVH<N>::traverseHelper(
INT_TYPE nodei,
INT_TYPE parent_nodei,
FUNCTORS &functors,
LOCAL_DATA* data_for_parent) const noexcept
{
const Node &node = myRoot[nodei];
bool descend = functors.pre(nodei, data_for_parent);
if (!descend)
return;
LOCAL_DATA local_data[N];
INT_TYPE s;
for (s = 0; s < N; ++s) {
const INT_TYPE node_int = node.child[s];
if (Node::isInternal(node_int)) {
if (node_int == Node::EMPTY) {
// NOTE: Anything after this will be empty too, so we can break.
break;
}
traverseHelper(Node::getInternalNum(node_int), nodei, functors, &local_data[s]);
}
else {
functors.item(node_int, nodei, local_data[s]);
}
}
// NOTE: s is now the number of non-empty entries in this node.
functors.post(nodei, parent_nodei, data_for_parent, s, local_data);
}
template<uint N>
template<typename LOCAL_DATA,typename FUNCTORS>
inline void BVH<N>::traverseParallel(
INT_TYPE parallel_threshold,
FUNCTORS& functors,
LOCAL_DATA* data_for_parent) const noexcept
{
if (!myRoot)
return;
// NOTE: The root is always index 0.
traverseParallelHelper(0, INT_TYPE(-1), parallel_threshold, myNumNodes, functors, data_for_parent);
}
template<uint N>
template<typename LOCAL_DATA,typename FUNCTORS>
inline void BVH<N>::traverseParallelHelper(
INT_TYPE nodei,
INT_TYPE parent_nodei,
INT_TYPE parallel_threshold,
INT_TYPE next_node_id,
FUNCTORS& functors,
LOCAL_DATA* data_for_parent) const noexcept
{
const Node &node = myRoot[nodei];
bool descend = functors.pre(nodei, data_for_parent);
if (!descend)
return;
// To determine the number of nodes in a child's subtree, we take the next
// node ID minus the current child's node ID.
INT_TYPE next_nodes[N];
INT_TYPE nnodes[N];
INT_TYPE nchildren = N;
INT_TYPE nparallel = 0;
// s is currently unsigned, so we check s < N for bounds check.
// The s >= 0 check is in case s ever becomes signed, and should be
// automatically removed by the compiler for unsigned s.
for (INT_TYPE s = N-1; (std::is_signed<INT_TYPE>::value ? (s >= 0) : (s < N)); --s) {
const INT_TYPE node_int = node.child[s];
if (node_int == Node::EMPTY) {
--nchildren;
continue;
}
next_nodes[s] = next_node_id;
if (Node::isInternal(node_int)) {
// NOTE: This depends on BVH<N>::initNode appending the child nodes
// in between their content, instead of all at once.
INT_TYPE child_node_id = Node::getInternalNum(node_int);
nnodes[s] = next_node_id - child_node_id;
next_node_id = child_node_id;
}
else {
nnodes[s] = 0;
}
nparallel += (nnodes[s] >= parallel_threshold);
}
LOCAL_DATA local_data[N];
if (nparallel >= 2) {
// Do any non-parallel ones first
if (nparallel < nchildren) {
for (INT_TYPE s = 0; s < N; ++s) {
if (nnodes[s] >= parallel_threshold) {
continue;
}
const INT_TYPE node_int = node.child[s];
if (Node::isInternal(node_int)) {
if (node_int == Node::EMPTY) {
// NOTE: Anything after this will be empty too, so we can break.
break;
}
traverseHelper(Node::getInternalNum(node_int), nodei, functors, &local_data[s]);
}
else {
functors.item(node_int, nodei, local_data[s]);
}
}
}
// Now do the parallel ones
igl::parallel_for(
nparallel,
[this,nodei,&node,&nnodes,&next_nodes,¶llel_threshold,&functors,&local_data](int taski)
{
INT_TYPE parallel_count = 0;
// NOTE: The check for s < N is just so that the compiler can
// (hopefully) figure out that it can fully unroll the loop.
INT_TYPE s;
for (s = 0; s < N; ++s) {
if (nnodes[s] < parallel_threshold) {
continue;
}
if (parallel_count == taski) {
break;
}
++parallel_count;
}
const INT_TYPE node_int = node.child[s];
if (Node::isInternal(node_int)) {
UT_ASSERT_MSG_P(node_int != Node::EMPTY, "Empty entries should have been excluded above.");
traverseParallelHelper(Node::getInternalNum(node_int), nodei, parallel_threshold, next_nodes[s], functors, &local_data[s]);
}
else {
functors.item(node_int, nodei, local_data[s]);
}
});
}
else {
// All in serial
for (INT_TYPE s = 0; s < N; ++s) {
const INT_TYPE node_int = node.child[s];
if (Node::isInternal(node_int)) {
if (node_int == Node::EMPTY) {
// NOTE: Anything after this will be empty too, so we can break.
break;
}
traverseHelper(Node::getInternalNum(node_int), nodei, functors, &local_data[s]);
}
else {
functors.item(node_int, nodei, local_data[s]);
}
}
}
functors.post(nodei, parent_nodei, data_for_parent, nchildren, local_data);
}
template<uint N>
template<typename LOCAL_DATA,typename FUNCTORS>
inline void BVH<N>::traverseVector(
FUNCTORS &functors,
LOCAL_DATA* data_for_parent) const noexcept
{
if (!myRoot)
return;
// NOTE: The root is always index 0.
traverseVectorHelper(0, INT_TYPE(-1), functors, data_for_parent);
}
template<uint N>
template<typename LOCAL_DATA,typename FUNCTORS>
inline void BVH<N>::traverseVectorHelper(
INT_TYPE nodei,
INT_TYPE parent_nodei,
FUNCTORS &functors,
LOCAL_DATA* data_for_parent) const noexcept
{
const Node &node = myRoot[nodei];
INT_TYPE descend = functors.pre(nodei, data_for_parent);
if (!descend)
return;
LOCAL_DATA local_data[N];
INT_TYPE s;
for (s = 0; s < N; ++s) {
if ((descend>>s) & 1) {
const INT_TYPE node_int = node.child[s];
if (Node::isInternal(node_int)) {
if (node_int == Node::EMPTY) {
// NOTE: Anything after this will be empty too, so we can break.
descend &= (INT_TYPE(1)<<s)-1;
break;
}
traverseVectorHelper(Node::getInternalNum(node_int), nodei, functors, &local_data[s]);
}
else {
functors.item(node_int, nodei, local_data[s]);
}
}
}
// NOTE: s is now the number of non-empty entries in this node.
functors.post(nodei, parent_nodei, data_for_parent, s, local_data, descend);
}
template<uint N>
template<typename SRC_INT_TYPE>
inline void BVH<N>::createTrivialIndices(SRC_INT_TYPE* indices, const INT_TYPE n) noexcept {
igl::parallel_for(n, [indices,n](INT_TYPE i) { indices[i] = i; }, 65536);
}
template<uint N>
template<typename T,uint NAXES,typename BOX_TYPE,typename SRC_INT_TYPE>
inline void BVH<N>::computeFullBoundingBox(Box<T,NAXES>& axes_minmax, const BOX_TYPE* boxes, const INT_TYPE nboxes, SRC_INT_TYPE* indices) noexcept {
if (!nboxes) {
axes_minmax.initBounds();
return;
}
INT_TYPE ntasks = 1;
if (nboxes >= 2*4096) {
INT_TYPE nprocessors = UT_Thread::getNumProcessors();
ntasks = (nprocessors > 1) ? SYSmin(4*nprocessors, nboxes/4096) : 1;
}
if (ntasks == 1) {
Box<T,NAXES> box;
if (indices) {
box.initBounds(boxes[indices[0]]);
for (INT_TYPE i = 1; i < nboxes; ++i) {
box.combine(boxes[indices[i]]);
}
}
else {
box.initBounds(boxes[0]);
for (INT_TYPE i = 1; i < nboxes; ++i) {
box.combine(boxes[i]);
}
}
axes_minmax = box;
}
else {
UT_SmallArray<Box<T,NAXES>> parallel_boxes;
Box<T,NAXES> box;
igl::parallel_for(
nboxes,
[¶llel_boxes](int n){parallel_boxes.setSize(n);},
[¶llel_boxes,indices,&boxes](int i, int t)
{
if(indices)
{
parallel_boxes[t].combine(boxes[indices[i]]);
}else
{
parallel_boxes[t].combine(boxes[i]);
}
},
[¶llel_boxes,&box](int t)
{
if(t == 0)
{
box = parallel_boxes[0];
}else
{
box.combine(parallel_boxes[t]);
}
});
axes_minmax = box;
}
}
template<uint N>
template<BVH_Heuristic H,typename T,uint NAXES,typename BOX_TYPE,typename SRC_INT_TYPE>
inline void BVH<N>::initNode(UT_Array<Node>& nodes, Node &node, const Box<T,NAXES>& axes_minmax, const BOX_TYPE* boxes, SRC_INT_TYPE* indices, const INT_TYPE nboxes) noexcept {
if (nboxes <= N) {
// Fits in one node
for (INT_TYPE i = 0; i < nboxes; ++i) {
node.child[i] = indices[i];
}
for (INT_TYPE i = nboxes; i < N; ++i) {
node.child[i] = Node::EMPTY;
}
return;
}
SRC_INT_TYPE* sub_indices[N+1];
Box<T,NAXES> sub_boxes[N];
if (N == 2) {
sub_indices[0] = indices;
sub_indices[2] = indices+nboxes;
split<H>(axes_minmax, boxes, indices, nboxes, sub_indices[1], &sub_boxes[0]);
}
else {
multiSplit<H>(axes_minmax, boxes, indices, nboxes, sub_indices, sub_boxes);
}
// Count the number of nodes to run in parallel and fill in single items in this node
INT_TYPE nparallel = 0;
static constexpr INT_TYPE PARALLEL_THRESHOLD = 1024;
for (INT_TYPE i = 0; i < N; ++i) {
INT_TYPE sub_nboxes = sub_indices[i+1]-sub_indices[i];
if (sub_nboxes == 1) {
node.child[i] = sub_indices[i][0];
}
else if (sub_nboxes >= PARALLEL_THRESHOLD) {
++nparallel;
}
}
// NOTE: Child nodes of this node need to be placed just before the nodes in
// their corresponding subtree, in between the subtrees, because
// traverseParallel uses the difference between the child node IDs
// to determine the number of nodes in the subtree.
// Recurse
if (nparallel >= 2) {
UT_SmallArray<UT_Array<Node>> parallel_nodes;
UT_SmallArray<Node> parallel_parent_nodes;
parallel_nodes.setSize(nparallel);
parallel_parent_nodes.setSize(nparallel);
igl::parallel_for(
nparallel,
[¶llel_nodes,¶llel_parent_nodes,&sub_indices,boxes,&sub_boxes](int taski)
{
// First, find which child this is
INT_TYPE counted_parallel = 0;
INT_TYPE sub_nboxes;
INT_TYPE childi;
for (childi = 0; childi < N; ++childi) {
sub_nboxes = sub_indices[childi+1]-sub_indices[childi];
if (sub_nboxes >= PARALLEL_THRESHOLD) {
if (counted_parallel == taski) {
break;
}
++counted_parallel;
}
}
UT_ASSERT_P(counted_parallel == taski);
UT_Array<Node>& local_nodes = parallel_nodes[taski];
// Preallocate an overestimate of the number of nodes needed.
// At worst, we could have only 2 children in every leaf, and
// then above that, we have a geometric series with r=1/N and a=(sub_nboxes/2)/N
// The true worst case might be a little worst than this, but
// it's probably fairly unlikely.
local_nodes.setCapacity(nodeEstimate(sub_nboxes));
Node& parent_node = parallel_parent_nodes[taski];
// We'll have to fix the internal node numbers in parent_node and local_nodes later
initNode<H>(local_nodes, parent_node, sub_boxes[childi], boxes, sub_indices[childi], sub_nboxes);
});
INT_TYPE counted_parallel = 0;
for (INT_TYPE i = 0; i < N; ++i) {
INT_TYPE sub_nboxes = sub_indices[i+1]-sub_indices[i];
if (sub_nboxes != 1) {
INT_TYPE local_nodes_start = nodes.size();
node.child[i] = Node::markInternal(local_nodes_start);
if (sub_nboxes >= PARALLEL_THRESHOLD) {
// First, adjust the root child node
Node child_node = parallel_parent_nodes[counted_parallel];
++local_nodes_start;
for (INT_TYPE childi = 0; childi < N; ++childi) {
INT_TYPE child_child = child_node.child[childi];
if (Node::isInternal(child_child) && child_child != Node::EMPTY) {
child_child += local_nodes_start;
child_node.child[childi] = child_child;
}
}
// Make space in the array for the sub-child nodes
const UT_Array<Node>& local_nodes = parallel_nodes[counted_parallel];
++counted_parallel;
INT_TYPE n = local_nodes.size();
nodes.bumpCapacity(local_nodes_start + n);
nodes.setSizeNoInit(local_nodes_start + n);
nodes[local_nodes_start-1] = child_node;
}
else {
nodes.bumpCapacity(local_nodes_start + 1);
nodes.setSizeNoInit(local_nodes_start + 1);
initNode<H>(nodes, nodes[local_nodes_start], sub_boxes[i], boxes, sub_indices[i], sub_nboxes);
}
}
}
// Now, adjust and copy all sub-child nodes that were made in parallel
adjustParallelChildNodes<PARALLEL_THRESHOLD>(nparallel, nodes, node, parallel_nodes.array(), sub_indices);
}
else {
for (INT_TYPE i = 0; i < N; ++i) {
INT_TYPE sub_nboxes = sub_indices[i+1]-sub_indices[i];
if (sub_nboxes != 1) {
INT_TYPE local_nodes_start = nodes.size();
node.child[i] = Node::markInternal(local_nodes_start);
nodes.bumpCapacity(local_nodes_start + 1);
nodes.setSizeNoInit(local_nodes_start + 1);
initNode<H>(nodes, nodes[local_nodes_start], sub_boxes[i], boxes, sub_indices[i], sub_nboxes);
}
}
}
}
template<uint N>
template<BVH_Heuristic H,typename T,uint NAXES,typename BOX_TYPE,typename SRC_INT_TYPE>
inline void BVH<N>::initNodeReorder(UT_Array<Node>& nodes, Node &node, const Box<T,NAXES>& axes_minmax, const BOX_TYPE* boxes, SRC_INT_TYPE* indices, INT_TYPE nboxes, const INT_TYPE indices_offset, const INT_TYPE max_items_per_leaf) noexcept {
if (nboxes <= N) {
// Fits in one node
for (INT_TYPE i = 0; i < nboxes; ++i) {
node.child[i] = indices_offset+i;
}
for (INT_TYPE i = nboxes; i < N; ++i) {
node.child[i] = Node::EMPTY;
}
return;
}
SRC_INT_TYPE* sub_indices[N+1];
Box<T,NAXES> sub_boxes[N];
if (N == 2) {
sub_indices[0] = indices;
sub_indices[2] = indices+nboxes;
split<H>(axes_minmax, boxes, indices, nboxes, sub_indices[1], &sub_boxes[0]);
}
else {
multiSplit<H>(axes_minmax, boxes, indices, nboxes, sub_indices, sub_boxes);
}
// Move any children with max_items_per_leaf or fewer indices before any children with more,
// for better cache coherence when we're accessing data in a corresponding array.
INT_TYPE nleaves = 0;
UT_SmallArray<SRC_INT_TYPE> leaf_indices;
SRC_INT_TYPE leaf_sizes[N];
INT_TYPE sub_nboxes0 = sub_indices[1]-sub_indices[0];
if (sub_nboxes0 <= max_items_per_leaf) {
leaf_sizes[0] = sub_nboxes0;
for (int j = 0; j < sub_nboxes0; ++j)
leaf_indices.append(sub_indices[0][j]);
++nleaves;
}
INT_TYPE sub_nboxes1 = sub_indices[2]-sub_indices[1];
if (sub_nboxes1 <= max_items_per_leaf) {
leaf_sizes[nleaves] = sub_nboxes1;
for (int j = 0; j < sub_nboxes1; ++j)
leaf_indices.append(sub_indices[1][j]);
++nleaves;
}
for (INT_TYPE i = 2; i < N; ++i) {
INT_TYPE sub_nboxes = sub_indices[i+1]-sub_indices[i];
if (sub_nboxes <= max_items_per_leaf) {
leaf_sizes[nleaves] = sub_nboxes;
for (int j = 0; j < sub_nboxes; ++j)
leaf_indices.append(sub_indices[i][j]);
++nleaves;
}
}
if (nleaves > 0) {
// NOTE: i < N condition is because INT_TYPE is unsigned.
// i >= 0 condition is in case INT_TYPE is changed to signed.
INT_TYPE move_distance = 0;
INT_TYPE index_move_distance = 0;
for (INT_TYPE i = N-1; (std::is_signed<INT_TYPE>::value ? (i >= 0) : (i < N)); --i) {
INT_TYPE sub_nboxes = sub_indices[i+1]-sub_indices[i];
if (sub_nboxes <= max_items_per_leaf) {
++move_distance;
index_move_distance += sub_nboxes;
}
else if (move_distance > 0) {
SRC_INT_TYPE *start_src_index = sub_indices[i];
for (SRC_INT_TYPE *src_index = sub_indices[i+1]-1; src_index >= start_src_index; --src_index) {
src_index[index_move_distance] = src_index[0];
}
sub_indices[i+move_distance] = sub_indices[i]+index_move_distance;
}
}
index_move_distance = 0;
for (INT_TYPE i = 0; i < nleaves; ++i) {
INT_TYPE sub_nboxes = leaf_sizes[i];
sub_indices[i] = indices+index_move_distance;
for (int j = 0; j < sub_nboxes; ++j)
indices[index_move_distance+j] = leaf_indices[index_move_distance+j];
index_move_distance += sub_nboxes;
}
}
// Count the number of nodes to run in parallel and fill in single items in this node
INT_TYPE nparallel = 0;
static constexpr INT_TYPE PARALLEL_THRESHOLD = 1024;
for (INT_TYPE i = 0; i < N; ++i) {
INT_TYPE sub_nboxes = sub_indices[i+1]-sub_indices[i];
if (sub_nboxes <= max_items_per_leaf) {
node.child[i] = indices_offset+(sub_indices[i]-sub_indices[0]);
}
else if (sub_nboxes >= PARALLEL_THRESHOLD) {
++nparallel;
}
}
// NOTE: Child nodes of this node need to be placed just before the nodes in
// their corresponding subtree, in between the subtrees, because
// traverseParallel uses the difference between the child node IDs
// to determine the number of nodes in the subtree.
// Recurse
if (nparallel >= 2 && false) {
assert(false && "Not implemented; should never get here");
exit(1);
// // Do the parallel ones first, so that they can be inserted in the right place.
// // Although the choice may seem somewhat arbitrary, we need the results to be
// // identical whether we choose to parallelize or not, and in case we change the
// // threshold later.
// UT_SmallArray<UT_Array<Node>,4*sizeof(UT_Array<Node>)> parallel_nodes;
// parallel_nodes.setSize(nparallel);
// UT_SmallArray<Node,4*sizeof(Node)> parallel_parent_nodes;
// parallel_parent_nodes.setSize(nparallel);
// UTparallelFor(UT_BlockedRange<INT_TYPE>(0,nparallel), [¶llel_nodes,¶llel_parent_nodes,&sub_indices,boxes,&sub_boxes,indices_offset,max_items_per_leaf](const UT_BlockedRange<INT_TYPE>& r) {
// for (INT_TYPE taski = r.begin(), end = r.end(); taski < end; ++taski) {
// // First, find which child this is
// INT_TYPE counted_parallel = 0;
// INT_TYPE sub_nboxes;
// INT_TYPE childi;
// for (childi = 0; childi < N; ++childi) {
// sub_nboxes = sub_indices[childi+1]-sub_indices[childi];
// if (sub_nboxes >= PARALLEL_THRESHOLD) {
// if (counted_parallel == taski) {
// break;
// }
// ++counted_parallel;
// }
// }
// UT_ASSERT_P(counted_parallel == taski);
// UT_Array<Node>& local_nodes = parallel_nodes[taski];
// // Preallocate an overestimate of the number of nodes needed.
// // At worst, we could have only 2 children in every leaf, and
// // then above that, we have a geometric series with r=1/N and a=(sub_nboxes/2)/N
// // The true worst case might be a little worst than this, but
// // it's probably fairly unlikely.
// local_nodes.setCapacity(nodeEstimate(sub_nboxes));
// Node& parent_node = parallel_parent_nodes[taski];
// // We'll have to fix the internal node numbers in parent_node and local_nodes later
// initNodeReorder<H>(local_nodes, parent_node, sub_boxes[childi], boxes, sub_indices[childi], sub_nboxes,
// indices_offset+(sub_indices[childi]-sub_indices[0]), max_items_per_leaf);
// }
// }, 0, 1);
// INT_TYPE counted_parallel = 0;
// for (INT_TYPE i = 0; i < N; ++i) {
// INT_TYPE sub_nboxes = sub_indices[i+1]-sub_indices[i];
// if (sub_nboxes > max_items_per_leaf) {
// INT_TYPE local_nodes_start = nodes.size();
// node.child[i] = Node::markInternal(local_nodes_start);
// if (sub_nboxes >= PARALLEL_THRESHOLD) {
// // First, adjust the root child node
// Node child_node = parallel_parent_nodes[counted_parallel];
// ++local_nodes_start;
// for (INT_TYPE childi = 0; childi < N; ++childi) {
// INT_TYPE child_child = child_node.child[childi];
// if (Node::isInternal(child_child) && child_child != Node::EMPTY) {
// child_child += local_nodes_start;
// child_node.child[childi] = child_child;
// }
// }
// // Make space in the array for the sub-child nodes
// const UT_Array<Node>& local_nodes = parallel_nodes[counted_parallel];
// ++counted_parallel;
// INT_TYPE n = local_nodes.size();
// nodes.bumpCapacity(local_nodes_start + n);
// nodes.setSizeNoInit(local_nodes_start + n);
// nodes[local_nodes_start-1] = child_node;
// }
// else {
// nodes.bumpCapacity(local_nodes_start + 1);
// nodes.setSizeNoInit(local_nodes_start + 1);
// initNodeReorder<H>(nodes, nodes[local_nodes_start], sub_boxes[i], boxes, sub_indices[i], sub_nboxes,
// indices_offset+(sub_indices[i]-sub_indices[0]), max_items_per_leaf);
// }
// }
// }
// // Now, adjust and copy all sub-child nodes that were made in parallel
// adjustParallelChildNodes<PARALLEL_THRESHOLD>(nparallel, nodes, node, parallel_nodes.array(), sub_indices);
}
else {
for (INT_TYPE i = 0; i < N; ++i) {
INT_TYPE sub_nboxes = sub_indices[i+1]-sub_indices[i];
if (sub_nboxes > max_items_per_leaf) {
INT_TYPE local_nodes_start = nodes.size();
node.child[i] = Node::markInternal(local_nodes_start);
nodes.bumpCapacity(local_nodes_start + 1);
nodes.setSizeNoInit(local_nodes_start + 1);
initNodeReorder<H>(nodes, nodes[local_nodes_start], sub_boxes[i], boxes, sub_indices[i], sub_nboxes,
indices_offset+(sub_indices[i]-sub_indices[0]), max_items_per_leaf);
}
}
}
}
template<uint N>
template<BVH_Heuristic H,typename T,uint NAXES,typename BOX_TYPE,typename SRC_INT_TYPE>
inline void BVH<N>::multiSplit(const Box<T,NAXES>& axes_minmax, const BOX_TYPE* boxes, SRC_INT_TYPE* indices, INT_TYPE nboxes, SRC_INT_TYPE* sub_indices[N+1], Box<T,NAXES> sub_boxes[N]) noexcept {
sub_indices[0] = indices;
sub_indices[2] = indices+nboxes;
split<H>(axes_minmax, boxes, indices, nboxes, sub_indices[1], &sub_boxes[0]);
if (N == 2) {
return;
}
if (H == BVH_Heuristic::MEDIAN_MAX_AXIS) {
SRC_INT_TYPE* sub_indices_startend[2*N];
Box<T,NAXES> sub_boxes_unsorted[N];
sub_boxes_unsorted[0] = sub_boxes[0];
sub_boxes_unsorted[1] = sub_boxes[1];
sub_indices_startend[0] = sub_indices[0];
sub_indices_startend[1] = sub_indices[1];
sub_indices_startend[2] = sub_indices[1];
sub_indices_startend[3] = sub_indices[2];
for (INT_TYPE nsub = 2; nsub < N; ++nsub) {
SRC_INT_TYPE* selected_start = sub_indices_startend[0];
SRC_INT_TYPE* selected_end = sub_indices_startend[1];
Box<T,NAXES> sub_box = sub_boxes_unsorted[0];
// Shift results back.
for (INT_TYPE i = 0; i < nsub-1; ++i) {
sub_indices_startend[2*i ] = sub_indices_startend[2*i+2];
sub_indices_startend[2*i+1] = sub_indices_startend[2*i+3];
}
for (INT_TYPE i = 0; i < nsub-1; ++i) {
sub_boxes_unsorted[i] = sub_boxes_unsorted[i-1];
}
// Do the split
split<H>(sub_box, boxes, selected_start, selected_end-selected_start, sub_indices_startend[2*nsub-1], &sub_boxes_unsorted[nsub]);
sub_indices_startend[2*nsub-2] = selected_start;
sub_indices_startend[2*nsub] = sub_indices_startend[2*nsub-1];
sub_indices_startend[2*nsub+1] = selected_end;
// Sort pointers so that they're in the correct order
sub_indices[N] = indices+nboxes;
for (INT_TYPE i = 0; i < N; ++i) {
SRC_INT_TYPE* prev_pointer = (i != 0) ? sub_indices[i-1] : nullptr;
SRC_INT_TYPE* min_pointer = nullptr;
Box<T,NAXES> box;
for (INT_TYPE j = 0; j < N; ++j) {
SRC_INT_TYPE* cur_pointer = sub_indices_startend[2*j];
if ((cur_pointer > prev_pointer) && (!min_pointer || (cur_pointer < min_pointer))) {
min_pointer = cur_pointer;
box = sub_boxes_unsorted[j];
}
}
UT_ASSERT_P(min_pointer);
sub_indices[i] = min_pointer;
sub_boxes[i] = box;
}
}
}
else {
T sub_box_areas[N];
sub_box_areas[0] = unweightedHeuristic<H>(sub_boxes[0]);
sub_box_areas[1] = unweightedHeuristic<H>(sub_boxes[1]);
for (INT_TYPE nsub = 2; nsub < N; ++nsub) {
// Choose which one to split
INT_TYPE split_choice = INT_TYPE(-1);
T max_heuristic;
for (INT_TYPE i = 0; i < nsub; ++i) {
const INT_TYPE index_count = (sub_indices[i+1]-sub_indices[i]);
if (index_count > 1) {
const T heuristic = sub_box_areas[i]*index_count;
if (split_choice == INT_TYPE(-1) || heuristic > max_heuristic) {
split_choice = i;
max_heuristic = heuristic;
}
}
}
UT_ASSERT_MSG_P(split_choice != INT_TYPE(-1), "There should always be at least one that can be split!");
SRC_INT_TYPE* selected_start = sub_indices[split_choice];
SRC_INT_TYPE* selected_end = sub_indices[split_choice+1];
// Shift results over; we can skip the one we selected.
for (INT_TYPE i = nsub; i > split_choice; --i) {
sub_indices[i+1] = sub_indices[i];
}
for (INT_TYPE i = nsub-1; i > split_choice; --i) {
sub_boxes[i+1] = sub_boxes[i];
}
for (INT_TYPE i = nsub-1; i > split_choice; --i) {
sub_box_areas[i+1] = sub_box_areas[i];
}
// Do the split
split<H>(sub_boxes[split_choice], boxes, selected_start, selected_end-selected_start, sub_indices[split_choice+1], &sub_boxes[split_choice]);
sub_box_areas[split_choice] = unweightedHeuristic<H>(sub_boxes[split_choice]);
sub_box_areas[split_choice+1] = unweightedHeuristic<H>(sub_boxes[split_choice+1]);
}
}
}
template<uint N>
template<BVH_Heuristic H,typename T,uint NAXES,typename BOX_TYPE,typename SRC_INT_TYPE>
inline void BVH<N>::split(const Box<T,NAXES>& axes_minmax, const BOX_TYPE* boxes, SRC_INT_TYPE* indices, INT_TYPE nboxes, SRC_INT_TYPE*& split_indices, Box<T,NAXES>* split_boxes) noexcept {
if (nboxes == 2) {
split_boxes[0].initBounds(boxes[indices[0]]);
split_boxes[1].initBounds(boxes[indices[1]]);
split_indices = indices+1;
return;
}
UT_ASSERT_MSG_P(nboxes > 2, "Cases with less than 3 boxes should have already been handled!");
if (H == BVH_Heuristic::MEDIAN_MAX_AXIS) {
UT_ASSERT_MSG(0, "FIXME: Implement this!!!");
}
constexpr INT_TYPE SMALL_LIMIT = 6;
if (nboxes <= SMALL_LIMIT) {
// Special case for a small number of boxes: check all (2^(n-1))-1 partitions.
// Without loss of generality, we assume that box 0 is in partition 0,
// and that not all boxes are in partition 0.
Box<T,NAXES> local_boxes[SMALL_LIMIT];
for (INT_TYPE box = 0; box < nboxes; ++box) {
local_boxes[box].initBounds(boxes[indices[box]]);
//printf("Box %u: (%f-%f)x(%f-%f)x(%f-%f)\n", uint(box), local_boxes[box].vals[0][0], local_boxes[box].vals[0][1], local_boxes[box].vals[1][0], local_boxes[box].vals[1][1], local_boxes[box].vals[2][0], local_boxes[box].vals[2][1]);
}
const INT_TYPE partition_limit = (INT_TYPE(1)<<(nboxes-1));
INT_TYPE best_partition = INT_TYPE(-1);
T best_heuristic;
for (INT_TYPE partition_bits = 1; partition_bits < partition_limit; ++partition_bits) {
Box<T,NAXES> sub_boxes[2];
sub_boxes[0] = local_boxes[0];
sub_boxes[1].initBounds();
INT_TYPE sub_counts[2] = {1,0};
for (INT_TYPE bit = 0; bit < nboxes-1; ++bit) {
INT_TYPE dest = (partition_bits>>bit)&1;
sub_boxes[dest].combine(local_boxes[bit+1]);
++sub_counts[dest];
}
//printf("Partition bits %u: sub_box[0]: (%f-%f)x(%f-%f)x(%f-%f)\n", uint(partition_bits), sub_boxes[0].vals[0][0], sub_boxes[0].vals[0][1], sub_boxes[0].vals[1][0], sub_boxes[0].vals[1][1], sub_boxes[0].vals[2][0], sub_boxes[0].vals[2][1]);
//printf("Partition bits %u: sub_box[1]: (%f-%f)x(%f-%f)x(%f-%f)\n", uint(partition_bits), sub_boxes[1].vals[0][0], sub_boxes[1].vals[0][1], sub_boxes[1].vals[1][0], sub_boxes[1].vals[1][1], sub_boxes[1].vals[2][0], sub_boxes[1].vals[2][1]);
const T heuristic =
unweightedHeuristic<H>(sub_boxes[0])*sub_counts[0] +
unweightedHeuristic<H>(sub_boxes[1])*sub_counts[1];
//printf("Partition bits %u: heuristic = %f (= %f*%u + %f*%u)\n",uint(partition_bits),heuristic, unweightedHeuristic<H>(sub_boxes[0]), uint(sub_counts[0]), unweightedHeuristic<H>(sub_boxes[1]), uint(sub_counts[1]));
if (best_partition == INT_TYPE(-1) || heuristic < best_heuristic) {
//printf(" New best\n");
best_partition = partition_bits;
best_heuristic = heuristic;
split_boxes[0] = sub_boxes[0];
split_boxes[1] = sub_boxes[1];
}
}
#if 0 // This isn't actually necessary with the current design, because I changed how the number of subtree nodes is determined.
// If best_partition is partition_limit-1, there's only 1 box
// in partition 0. We should instead put this in partition 1,
// so that we can help always have the internal node indices first
// in each node. That gets used to (fairly) quickly determine
// the number of nodes in a sub-tree.
if (best_partition == partition_limit - 1) {
// Put the first index last.
SRC_INT_TYPE last_index = indices[0];
SRC_INT_TYPE* dest_indices = indices;
SRC_INT_TYPE* local_split_indices = indices + nboxes-1;
for (; dest_indices != local_split_indices; ++dest_indices) {
dest_indices[0] = dest_indices[1];
}
*local_split_indices = last_index;
split_indices = local_split_indices;
// Swap the boxes
const Box<T,NAXES> temp_box = sub_boxes[0];
sub_boxes[0] = sub_boxes[1];
sub_boxes[1] = temp_box;
return;
}
#endif
// Reorder the indices.
// NOTE: Index 0 is always in partition 0, so can stay put.
SRC_INT_TYPE local_indices[SMALL_LIMIT-1];
for (INT_TYPE box = 0; box < nboxes-1; ++box) {
local_indices[box] = indices[box+1];
}
SRC_INT_TYPE* dest_indices = indices+1;
SRC_INT_TYPE* src_indices = local_indices;
// Copy partition 0
for (INT_TYPE bit = 0; bit < nboxes-1; ++bit, ++src_indices) {
if (!((best_partition>>bit)&1)) {
//printf("Copying %u into partition 0\n",uint(*src_indices));
*dest_indices = *src_indices;
++dest_indices;
}
}
split_indices = dest_indices;
// Copy partition 1
src_indices = local_indices;
for (INT_TYPE bit = 0; bit < nboxes-1; ++bit, ++src_indices) {
if ((best_partition>>bit)&1) {
//printf("Copying %u into partition 1\n",uint(*src_indices));
*dest_indices = *src_indices;
++dest_indices;
}
}
return;
}
uint max_axis = 0;
T max_axis_length = axes_minmax.vals[0][1] - axes_minmax.vals[0][0];
for (uint axis = 1; axis < NAXES; ++axis) {
const T axis_length = axes_minmax.vals[axis][1] - axes_minmax.vals[axis][0];
if (axis_length > max_axis_length) {
max_axis = axis;
max_axis_length = axis_length;
}
}
if (!(max_axis_length > T(0))) {
// All boxes are a single point or NaN.
// Pick an arbitrary split point.
split_indices = indices + nboxes/2;
split_boxes[0] = axes_minmax;
split_boxes[1] = axes_minmax;
return;
}
const INT_TYPE axis = max_axis;
constexpr INT_TYPE MID_LIMIT = 2*NSPANS;
if (nboxes <= MID_LIMIT) {
// Sort along axis, and try all possible splits.
#if 1
// First, compute midpoints
T midpointsx2[MID_LIMIT];
for (INT_TYPE i = 0; i < nboxes; ++i) {
midpointsx2[i] = utBoxCenter(boxes[indices[i]], axis);
}
SRC_INT_TYPE local_indices[MID_LIMIT];
for (INT_TYPE i = 0; i < nboxes; ++i) {
local_indices[i] = i;
}
const INT_TYPE chunk_starts[5] = {0, nboxes/4, nboxes/2, INT_TYPE((3*uint64(nboxes))/4), nboxes};
// For sorting, insertion sort 4 chunks and merge them
for (INT_TYPE chunk = 0; chunk < 4; ++chunk) {
const INT_TYPE start = chunk_starts[chunk];
const INT_TYPE end = chunk_starts[chunk+1];
for (INT_TYPE i = start+1; i < end; ++i) {
SRC_INT_TYPE indexi = local_indices[i];
T vi = midpointsx2[indexi];
for (INT_TYPE j = start; j < i; ++j) {
SRC_INT_TYPE indexj = local_indices[j];
T vj = midpointsx2[indexj];
if (vi < vj) {
do {
local_indices[j] = indexi;
indexi = indexj;
++j;
if (j == i) {
local_indices[j] = indexi;
break;
}
indexj = local_indices[j];
} while (true);
break;
}
}
}
}
// Merge chunks into another buffer
SRC_INT_TYPE local_indices_temp[MID_LIMIT];
std::merge(local_indices, local_indices+chunk_starts[1],
local_indices+chunk_starts[1], local_indices+chunk_starts[2],
local_indices_temp, [&midpointsx2](const SRC_INT_TYPE a, const SRC_INT_TYPE b)->bool {
return midpointsx2[a] < midpointsx2[b];
});
std::merge(local_indices+chunk_starts[2], local_indices+chunk_starts[3],
local_indices+chunk_starts[3], local_indices+chunk_starts[4],
local_indices_temp+chunk_starts[2], [&midpointsx2](const SRC_INT_TYPE a, const SRC_INT_TYPE b)->bool {
return midpointsx2[a] < midpointsx2[b];
});
std::merge(local_indices_temp, local_indices_temp+chunk_starts[2],
local_indices_temp+chunk_starts[2], local_indices_temp+chunk_starts[4],
local_indices, [&midpointsx2](const SRC_INT_TYPE a, const SRC_INT_TYPE b)->bool {
return midpointsx2[a] < midpointsx2[b];
});
// Translate local_indices into indices
for (INT_TYPE i = 0; i < nboxes; ++i) {
local_indices[i] = indices[local_indices[i]];
}
// Copy back
for (INT_TYPE i = 0; i < nboxes; ++i) {
indices[i] = local_indices[i];
}
#else
std::stable_sort(indices, indices+nboxes, [boxes,max_axis](SRC_INT_TYPE a, SRC_INT_TYPE b)->bool {
return utBoxCenter(boxes[a], max_axis) < utBoxCenter(boxes[b], max_axis);
});
#endif
// Accumulate boxes
Box<T,NAXES> left_boxes[MID_LIMIT-1];
Box<T,NAXES> right_boxes[MID_LIMIT-1];
const INT_TYPE nsplits = nboxes-1;
Box<T,NAXES> box_accumulator(boxes[local_indices[0]]);
left_boxes[0] = box_accumulator;
for (INT_TYPE i = 1; i < nsplits; ++i) {
box_accumulator.combine(boxes[local_indices[i]]);
left_boxes[i] = box_accumulator;
}
box_accumulator.initBounds(boxes[local_indices[nsplits-1]]);
right_boxes[nsplits-1] = box_accumulator;
for (INT_TYPE i = nsplits-1; i > 0; --i) {
box_accumulator.combine(boxes[local_indices[i]]);
right_boxes[i-1] = box_accumulator;
}
INT_TYPE best_split = 0;
T best_local_heuristic =
unweightedHeuristic<H>(left_boxes[0]) +
unweightedHeuristic<H>(right_boxes[0])*(nboxes-1);
for (INT_TYPE split = 1; split < nsplits; ++split) {
const T heuristic =
unweightedHeuristic<H>(left_boxes[split])*(split+1) +
unweightedHeuristic<H>(right_boxes[split])*(nboxes-(split+1));
if (heuristic < best_local_heuristic) {
best_split = split;
best_local_heuristic = heuristic;
}
}
split_indices = indices+best_split+1;
split_boxes[0] = left_boxes[best_split];
split_boxes[1] = right_boxes[best_split];
return;
}
const T axis_min = axes_minmax.vals[max_axis][0];
const T axis_length = max_axis_length;
Box<T,NAXES> span_boxes[NSPANS];
for (INT_TYPE i = 0; i < NSPANS; ++i) {
span_boxes[i].initBounds();
}
INT_TYPE span_counts[NSPANS];
for (INT_TYPE i = 0; i < NSPANS; ++i) {
span_counts[i] = 0;
}
const T axis_min_x2 = ut_BoxCentre<BOX_TYPE>::scale*axis_min;
// NOTE: Factor of 0.5 is factored out of the average when using the average value to determine the span that a box lies in.
const T axis_index_scale = (T(1.0/ut_BoxCentre<BOX_TYPE>::scale)*NSPANS)/axis_length;
constexpr INT_TYPE BOX_SPANS_PARALLEL_THRESHOLD = 2048;
INT_TYPE ntasks = 1;
if (nboxes >= BOX_SPANS_PARALLEL_THRESHOLD) {
INT_TYPE nprocessors = UT_Thread::getNumProcessors();
ntasks = (nprocessors > 1) ? SYSmin(4*nprocessors, nboxes/(BOX_SPANS_PARALLEL_THRESHOLD/2)) : 1;
}
if (ntasks == 1) {
for (INT_TYPE indexi = 0; indexi < nboxes; ++indexi) {
const auto& box = boxes[indices[indexi]];
const T sum = utBoxCenter(box, axis);
const uint span_index = SYSclamp(int((sum-axis_min_x2)*axis_index_scale), int(0), int(NSPANS-1));
++span_counts[span_index];
Box<T,NAXES>& span_box = span_boxes[span_index];
span_box.combine(box);
}
}
else {
UT_SmallArray<Box<T,NAXES>> parallel_boxes;
UT_SmallArray<INT_TYPE> parallel_counts;
igl::parallel_for(
nboxes,
[¶llel_boxes,¶llel_counts](int n)
{
parallel_boxes.setSize( NSPANS*n);
parallel_counts.setSize(NSPANS*n);
for(int t = 0;t<n;t++)
{
for (INT_TYPE i = 0; i < NSPANS; ++i)
{
parallel_boxes[t*NSPANS+i].initBounds();
parallel_counts[t*NSPANS+i] = 0;
}
}
},
[¶llel_boxes,¶llel_counts,&boxes,indices,axis,axis_min_x2,axis_index_scale](int j, int t)
{
const auto& box = boxes[indices[j]];
const T sum = utBoxCenter(box, axis);
const uint span_index = SYSclamp(int((sum-axis_min_x2)*axis_index_scale), int(0), int(NSPANS-1));
++parallel_counts[t*NSPANS+span_index];
Box<T,NAXES>& span_box = parallel_boxes[t*NSPANS+span_index];
span_box.combine(box);
},
[¶llel_boxes,¶llel_counts,&span_boxes,&span_counts](int t)
{
for(int i = 0;i<NSPANS;i++)
{
span_counts[i] += parallel_counts[t*NSPANS + i];
span_boxes[i].combine(parallel_boxes[t*NSPANS + i]);
}
});
}
// Spans 0 to NSPANS-2
Box<T,NAXES> left_boxes[NSPLITS];
// Spans 1 to NSPANS-1
Box<T,NAXES> right_boxes[NSPLITS];
// Accumulate boxes
Box<T,NAXES> box_accumulator = span_boxes[0];
left_boxes[0] = box_accumulator;
for (INT_TYPE i = 1; i < NSPLITS; ++i) {
box_accumulator.combine(span_boxes[i]);
left_boxes[i] = box_accumulator;
}
box_accumulator = span_boxes[NSPANS-1];
right_boxes[NSPLITS-1] = box_accumulator;
for (INT_TYPE i = NSPLITS-1; i > 0; --i) {
box_accumulator.combine(span_boxes[i]);
right_boxes[i-1] = box_accumulator;
}
INT_TYPE left_counts[NSPLITS];
// Accumulate counts
INT_TYPE count_accumulator = span_counts[0];
left_counts[0] = count_accumulator;
for (INT_TYPE spliti = 1; spliti < NSPLITS; ++spliti) {
count_accumulator += span_counts[spliti];
left_counts[spliti] = count_accumulator;
}
// Check which split is optimal, making sure that at least 1/MIN_FRACTION of all boxes are on each side.
const INT_TYPE min_count = nboxes/MIN_FRACTION;
UT_ASSERT_MSG_P(min_count > 0, "MID_LIMIT above should have been large enough that nboxes would be > MIN_FRACTION");
const INT_TYPE max_count = ((MIN_FRACTION-1)*uint64(nboxes))/MIN_FRACTION;
UT_ASSERT_MSG_P(max_count < nboxes, "I'm not sure how this could happen mathematically, but it needs to be checked.");
T smallest_heuristic = std::numeric_limits<T>::infinity();
INT_TYPE split_index = -1;
for (INT_TYPE spliti = 0; spliti < NSPLITS; ++spliti) {
const INT_TYPE left_count = left_counts[spliti];
if (left_count < min_count || left_count > max_count) {
continue;
}
const INT_TYPE right_count = nboxes-left_count;
const T heuristic =
left_count*unweightedHeuristic<H>(left_boxes[spliti]) +
right_count*unweightedHeuristic<H>(right_boxes[spliti]);
if (heuristic < smallest_heuristic) {
smallest_heuristic = heuristic;
split_index = spliti;
}
}
SRC_INT_TYPE*const indices_end = indices+nboxes;
if (split_index == -1) {
// No split was anywhere close to balanced, so we fall back to searching for one.
// First, find the span containing the "balance" point, namely where left_counts goes from
// being less than min_count to more than max_count.
// If that's span 0, use max_count as the ordered index to select,
// if it's span NSPANS-1, use min_count as the ordered index to select,
// else use nboxes/2 as the ordered index to select.
//T min_pivotx2 = -std::numeric_limits<T>::infinity();
//T max_pivotx2 = std::numeric_limits<T>::infinity();
SRC_INT_TYPE* nth_index;
if (left_counts[0] > max_count) {
// Search for max_count ordered index
nth_index = indices+max_count;
//max_pivotx2 = max_axis_min_x2 + max_axis_length/(NSPANS/ut_BoxCentre<BOX_TYPE>::scale);
}
else if (left_counts[NSPLITS-1] < min_count) {
// Search for min_count ordered index
nth_index = indices+min_count;
//min_pivotx2 = max_axis_min_x2 + max_axis_length - max_axis_length/(NSPANS/ut_BoxCentre<BOX_TYPE>::scale);
}
else {
// Search for nboxes/2 ordered index
nth_index = indices+nboxes/2;
//for (INT_TYPE spliti = 1; spliti < NSPLITS; ++spliti) {
// // The second condition should be redundant, but is just in case.
// if (left_counts[spliti] > max_count || spliti == NSPLITS-1) {
// min_pivotx2 = max_axis_min_x2 + spliti*max_axis_length/(NSPANS/ut_BoxCentre<BOX_TYPE>::scale);
// max_pivotx2 = max_axis_min_x2 + (spliti+1)*max_axis_length/(NSPANS/ut_BoxCentre<BOX_TYPE>::scale);
// break;
// }
//}
}
nthElement<T>(boxes,indices,indices+nboxes,max_axis,nth_index);//,min_pivotx2,max_pivotx2);
split_indices = nth_index;
Box<T,NAXES> left_box(boxes[indices[0]]);
for (SRC_INT_TYPE* left_indices = indices+1; left_indices < nth_index; ++left_indices) {
left_box.combine(boxes[*left_indices]);
}
Box<T,NAXES> right_box(boxes[nth_index[0]]);
for (SRC_INT_TYPE* right_indices = nth_index+1; right_indices < indices_end; ++right_indices) {
right_box.combine(boxes[*right_indices]);
}
split_boxes[0] = left_box;
split_boxes[1] = right_box;
}
else {
const T pivotx2 = axis_min_x2 + (split_index+1)*axis_length/(NSPANS/ut_BoxCentre<BOX_TYPE>::scale);
SRC_INT_TYPE* ppivot_start;
SRC_INT_TYPE* ppivot_end;
partitionByCentre(boxes,indices,indices+nboxes,max_axis,pivotx2,ppivot_start,ppivot_end);
split_indices = indices + left_counts[split_index];
// Ignoring roundoff error, we would have
// split_indices >= ppivot_start && split_indices <= ppivot_end,
// but it may not always be in practice.
if (split_indices >= ppivot_start && split_indices <= ppivot_end) {
split_boxes[0] = left_boxes[split_index];
split_boxes[1] = right_boxes[split_index];
return;
}
// Roundoff error changed the split, so we need to recompute the boxes.
if (split_indices < ppivot_start) {
split_indices = ppivot_start;
}
else {//(split_indices > ppivot_end)
split_indices = ppivot_end;
}
// Emergency checks, just in case
if (split_indices == indices) {
++split_indices;
}
else if (split_indices == indices_end) {
--split_indices;
}
Box<T,NAXES> left_box(boxes[indices[0]]);
for (SRC_INT_TYPE* left_indices = indices+1; left_indices < split_indices; ++left_indices) {
left_box.combine(boxes[*left_indices]);
}
Box<T,NAXES> right_box(boxes[split_indices[0]]);
for (SRC_INT_TYPE* right_indices = split_indices+1; right_indices < indices_end; ++right_indices) {
right_box.combine(boxes[*right_indices]);
}
split_boxes[0] = left_box;
split_boxes[1] = right_box;
}
}
template<uint N>
template<uint PARALLEL_THRESHOLD, typename SRC_INT_TYPE>
inline void BVH<N>::adjustParallelChildNodes(INT_TYPE nparallel, UT_Array<Node>& nodes, Node& node, UT_Array<Node>* parallel_nodes, SRC_INT_TYPE* sub_indices) noexcept
{
// Alec: No need to parallelize this...
//UTparallelFor(UT_BlockedRange<INT_TYPE>(0,nparallel), [&node,&nodes,¶llel_nodes,&sub_indices](const UT_BlockedRange<INT_TYPE>& r) {
INT_TYPE counted_parallel = 0;
INT_TYPE childi = 0;
for(int taski = 0;taski < nparallel; taski++)
{
//for (INT_TYPE taski = r.begin(), end = r.end(); taski < end; ++taski) {
// First, find which child this is
INT_TYPE sub_nboxes;
for (; childi < N; ++childi) {
sub_nboxes = sub_indices[childi+1]-sub_indices[childi];
if (sub_nboxes >= PARALLEL_THRESHOLD) {
if (counted_parallel == taski) {
break;
}
++counted_parallel;
}
}
UT_ASSERT_P(counted_parallel == taski);
const UT_Array<Node>& local_nodes = parallel_nodes[counted_parallel];
INT_TYPE n = local_nodes.size();
INT_TYPE local_nodes_start = Node::getInternalNum(node.child[childi])+1;
++counted_parallel;
++childi;
for (INT_TYPE j = 0; j < n; ++j) {
Node local_node = local_nodes[j];
for (INT_TYPE childj = 0; childj < N; ++childj) {
INT_TYPE local_child = local_node.child[childj];
if (Node::isInternal(local_child) && local_child != Node::EMPTY) {
local_child += local_nodes_start;
local_node.child[childj] = local_child;
}
}
nodes[local_nodes_start+j] = local_node;
}
}
}
template<uint N>
template<typename T,typename BOX_TYPE,typename SRC_INT_TYPE>
void BVH<N>::nthElement(const BOX_TYPE* boxes, SRC_INT_TYPE* indices, const SRC_INT_TYPE* indices_end, const uint axis, SRC_INT_TYPE*const nth) noexcept {//, const T min_pivotx2, const T max_pivotx2) noexcept {
while (true) {
// Choose median of first, middle, and last as the pivot
T pivots[3] = {
utBoxCenter(boxes[indices[0]], axis),
utBoxCenter(boxes[indices[(indices_end-indices)/2]], axis),
utBoxCenter(boxes[*(indices_end-1)], axis)
};
if (pivots[0] < pivots[1]) {
const T temp = pivots[0];
pivots[0] = pivots[1];
pivots[1] = temp;
}
if (pivots[0] < pivots[2]) {
const T temp = pivots[0];
pivots[0] = pivots[2];
pivots[2] = temp;
}
if (pivots[1] < pivots[2]) {
const T temp = pivots[1];
pivots[1] = pivots[2];
pivots[2] = temp;
}
T mid_pivotx2 = pivots[1];
#if 0
// We limit the pivot, because we know that the true value is between min and max
if (mid_pivotx2 < min_pivotx2) {
mid_pivotx2 = min_pivotx2;
}
else if (mid_pivotx2 > max_pivotx2) {
mid_pivotx2 = max_pivotx2;
}
#endif
SRC_INT_TYPE* pivot_start;
SRC_INT_TYPE* pivot_end;
partitionByCentre(boxes,indices,indices_end,axis,mid_pivotx2,pivot_start,pivot_end);
if (nth < pivot_start) {
indices_end = pivot_start;
}
else if (nth < pivot_end) {
// nth is in the middle of the pivot range,
// which is in the right place, so we're done.
return;
}
else {
indices = pivot_end;
}
if (indices_end <= indices+1) {
return;
}
}
}
template<uint N>
template<typename T,typename BOX_TYPE,typename SRC_INT_TYPE>
void BVH<N>::partitionByCentre(const BOX_TYPE* boxes, SRC_INT_TYPE*const indices, const SRC_INT_TYPE*const indices_end, const uint axis, const T pivotx2, SRC_INT_TYPE*& ppivot_start, SRC_INT_TYPE*& ppivot_end) noexcept {
// TODO: Consider parallelizing this!
// First element >= pivot
SRC_INT_TYPE* pivot_start = indices;
// First element > pivot
SRC_INT_TYPE* pivot_end = indices;
// Loop through forward once
for (SRC_INT_TYPE* psrc_index = indices; psrc_index != indices_end; ++psrc_index) {
const T srcsum = utBoxCenter(boxes[*psrc_index], axis);
if (srcsum < pivotx2) {
if (psrc_index != pivot_start) {
if (pivot_start == pivot_end) {
// Common case: nothing equal to the pivot
const SRC_INT_TYPE temp = *psrc_index;
*psrc_index = *pivot_start;
*pivot_start = temp;
}
else {
// Less common case: at least one thing equal to the pivot
const SRC_INT_TYPE temp = *psrc_index;
*psrc_index = *pivot_end;
*pivot_end = *pivot_start;
*pivot_start = temp;
}
}
++pivot_start;
++pivot_end;
}
else if (srcsum == pivotx2) {
// Add to the pivot area
if (psrc_index != pivot_end) {
const SRC_INT_TYPE temp = *psrc_index;
*psrc_index = *pivot_end;
*pivot_end = temp;
}
++pivot_end;
}
}
ppivot_start = pivot_start;
ppivot_end = pivot_end;
}
#if 0
template<uint N>
void BVH<N>::debugDump() const {
printf("\nNode 0: {\n");
UT_WorkBuffer indent;
indent.append(80, ' ');
UT_Array<INT_TYPE> stack;
stack.append(0);
stack.append(0);
while (!stack.isEmpty()) {
int depth = stack.size()/2;
if (indent.length() < 4*depth) {
indent.append(4, ' ');
}
INT_TYPE cur_nodei = stack[stack.size()-2];
INT_TYPE cur_i = stack[stack.size()-1];
if (cur_i == N) {
printf(indent.buffer()+indent.length()-(4*(depth-1)));
printf("}\n");
stack.removeLast();
stack.removeLast();
continue;
}
++stack[stack.size()-1];
Node& cur_node = myRoot[cur_nodei];
INT_TYPE child_nodei = cur_node.child[cur_i];
if (Node::isInternal(child_nodei)) {
if (child_nodei == Node::EMPTY) {
printf(indent.buffer()+indent.length()-(4*(depth-1)));
printf("}\n");
stack.removeLast();
stack.removeLast();
continue;
}
INT_TYPE internal_node = Node::getInternalNum(child_nodei);
printf(indent.buffer()+indent.length()-(4*depth));
printf("Node %u: {\n", uint(internal_node));
stack.append(internal_node);
stack.append(0);
continue;
}
else {
printf(indent.buffer()+indent.length()-(4*depth));
printf("Tri %u\n", uint(child_nodei));
}
}
}
#endif
} // UT namespace
} // End HDK_Sample namespace
}}
#endif
/*
* Copyright (c) 2018 Side Effects Software Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* COMMENTS:
* Functions and structures for computing solid angles.
*/
#pragma once
#ifndef __HDK_UT_SolidAngle_h__
#define __HDK_UT_SolidAngle_h__
#include <memory>
namespace igl { namespace FastWindingNumber {
namespace HDK_Sample {
template<typename T>
using UT_Vector2T = UT_FixedVector<T,2>;
template<typename T>
using UT_Vector3T = UT_FixedVector<T,3>;
template <typename T>
SYS_FORCE_INLINE T cross(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2)
{
return v1[0]*v2[1] - v1[1]*v2[0];
}
template <typename T>
SYS_FORCE_INLINE
UT_Vector3T<T> cross(const UT_Vector3T<T> &v1, const UT_Vector3T<T> &v2)
{
UT_Vector3T<T> result;
// compute the cross product:
result[0] = v1[1]*v2[2] - v1[2]*v2[1];
result[1] = v1[2]*v2[0] - v1[0]*v2[2];
result[2] = v1[0]*v2[1] - v1[1]*v2[0];
return result;
}
/// Returns the signed solid angle subtended by triangle abc
/// from query point.
///
/// WARNING: This uses the right-handed normal convention, whereas most of
/// Houdini uses the left-handed normal convention, so either
/// negate the output, or swap b and c if you want it to be
/// positive inside and negative outside.
template<typename T>
inline T UTsignedSolidAngleTri(
const UT_Vector3T<T> &a,
const UT_Vector3T<T> &b,
const UT_Vector3T<T> &c,
const UT_Vector3T<T> &query)
{
// Make a, b, and c relative to query
UT_Vector3T<T> qa = a-query;
UT_Vector3T<T> qb = b-query;
UT_Vector3T<T> qc = c-query;
const T alength = qa.length();
const T blength = qb.length();
const T clength = qc.length();
// If any triangle vertices are coincident with query,
// query is on the surface, which we treat as no solid angle.
if (alength == 0 || blength == 0 || clength == 0)
return T(0);
// Normalize the vectors
qa /= alength;
qb /= blength;
qc /= clength;
// The formula on Wikipedia has roughly dot(qa,cross(qb,qc)),
// but that's unstable when qa, qb, and qc are very close,
// (e.g. if the input triangle was very far away).
// This should be equivalent, but more stable.
const T numerator = dot(qa, cross(qb-qa, qc-qa));
// If numerator is 0, regardless of denominator, query is on the
// surface, which we treat as no solid angle.
if (numerator == 0)
return T(0);
const T denominator = T(1) + dot(qa,qb) + dot(qa,qc) + dot(qb,qc);
return T(2)*SYSatan2(numerator, denominator);
}
template<typename T>
inline T UTsignedSolidAngleQuad(
const UT_Vector3T<T> &a,
const UT_Vector3T<T> &b,
const UT_Vector3T<T> &c,
const UT_Vector3T<T> &d,
const UT_Vector3T<T> &query)
{
// Make a, b, c, and d relative to query
UT_Vector3T<T> v[4] = {
a-query,
b-query,
c-query,
d-query
};
const T lengths[4] = {
v[0].length(),
v[1].length(),
v[2].length(),
v[3].length()
};
// If any quad vertices are coincident with query,
// query is on the surface, which we treat as no solid angle.
// We could add the contribution from the non-planar part,
// but in the context of a mesh, we'd still miss some, like
// we do in the triangle case.
if (lengths[0] == T(0) || lengths[1] == T(0) || lengths[2] == T(0) || lengths[3] == T(0))
return T(0);
// Normalize the vectors
v[0] /= lengths[0];
v[1] /= lengths[1];
v[2] /= lengths[2];
v[3] /= lengths[3];
// Compute (unnormalized, but consistently-scaled) barycentric coordinates
// for the query point inside the tetrahedron of points.
// If 0 or 4 of the coordinates are positive, (or slightly negative), the
// query is (approximately) inside, so the choice of triangulation matters.
// Otherwise, the triangulation doesn't matter.
const UT_Vector3T<T> diag02 = v[2]-v[0];
const UT_Vector3T<T> diag13 = v[3]-v[1];
const UT_Vector3T<T> v01 = v[1]-v[0];
const UT_Vector3T<T> v23 = v[3]-v[2];
T bary[4];
bary[0] = dot(v[3],cross(v23,diag13));
bary[1] = -dot(v[2],cross(v23,diag02));
bary[2] = -dot(v[1],cross(v01,diag13));
bary[3] = dot(v[0],cross(v01,diag02));
const T dot01 = dot(v[0],v[1]);
const T dot12 = dot(v[1],v[2]);
const T dot23 = dot(v[2],v[3]);
const T dot30 = dot(v[3],v[0]);
T omega = T(0);
// Equation of a bilinear patch in barycentric coordinates of its
// tetrahedron is x0*x2 = x1*x3. Less is one side; greater is other.
if (bary[0]*bary[2] < bary[1]*bary[3])
{
// Split 0-2: triangles 0,1,2 and 0,2,3
const T numerator012 = bary[3];
const T numerator023 = bary[1];
const T dot02 = dot(v[0],v[2]);
// If numerator is 0, regardless of denominator, query is on the
// surface, which we treat as no solid angle.
if (numerator012 != T(0))
{
const T denominator012 = T(1) + dot01 + dot12 + dot02;
omega = SYSatan2(numerator012, denominator012);
}
if (numerator023 != T(0))
{
const T denominator023 = T(1) + dot02 + dot23 + dot30;
omega += SYSatan2(numerator023, denominator023);
}
}
else
{
// Split 1-3: triangles 0,1,3 and 1,2,3
const T numerator013 = -bary[2];
const T numerator123 = -bary[0];
const T dot13 = dot(v[1],v[3]);
// If numerator is 0, regardless of denominator, query is on the
// surface, which we treat as no solid angle.
if (numerator013 != T(0))
{
const T denominator013 = T(1) + dot01 + dot13 + dot30;
omega = SYSatan2(numerator013, denominator013);
}
if (numerator123 != T(0))
{
const T denominator123 = T(1) + dot12 + dot23 + dot13;
omega += SYSatan2(numerator123, denominator123);
}
}
return T(2)*omega;
}
/// Class for quickly approximating signed solid angle of a large mesh
/// from many query points. This is useful for computing the
/// generalized winding number at many points.
///
/// NOTE: This is currently only instantiated for <float,float>.
template<typename T,typename S>
class UT_SolidAngle
{
public:
/// This is outlined so that we don't need to include UT_BVHImpl.h
inline UT_SolidAngle();
/// This is outlined so that we don't need to include UT_BVHImpl.h
inline ~UT_SolidAngle();
/// NOTE: This does not take ownership over triangle_points or positions,
/// but does keep pointers to them, so the caller must keep them in
/// scope for the lifetime of this structure.
UT_SolidAngle(
const int ntriangles,
const int *const triangle_points,
const int npoints,
const UT_Vector3T<S> *const positions,
const int order = 2)
: UT_SolidAngle()
{ init(ntriangles, triangle_points, npoints, positions, order); }
/// Initialize the tree and data.
/// NOTE: It is safe to call init on a UT_SolidAngle that has had init
/// called on it before, to re-initialize it.
inline void init(
const int ntriangles,
const int *const triangle_points,
const int npoints,
const UT_Vector3T<S> *const positions,
const int order = 2);
/// Frees myTree and myData, and clears the rest.
inline void clear();
/// Returns true if this is clear
bool isClear() const
{ return myNTriangles == 0; }
/// Returns an approximation of the signed solid angle of the mesh from the specified query_point
/// accuracy_scale is the value of (maxP/q) beyond which the approximation of the box will be used.
inline T computeSolidAngle(const UT_Vector3T<T> &query_point, const T accuracy_scale = T(2.0)) const;
private:
struct BoxData;
static constexpr uint BVH_N = 4;
UT_BVH<BVH_N> myTree;
int myNBoxes;
int myOrder;
std::unique_ptr<BoxData[]> myData;
int myNTriangles;
const int *myTrianglePoints;
int myNPoints;
const UT_Vector3T<S> *myPositions;
};
template<typename T>
inline T UTsignedAngleSegment(
const UT_Vector2T<T> &a,
const UT_Vector2T<T> &b,
const UT_Vector2T<T> &query)
{
// Make a and b relative to query
UT_Vector2T<T> qa = a-query;
UT_Vector2T<T> qb = b-query;
// If any segment vertices are coincident with query,
// query is on the segment, which we treat as no angle.
if (qa.isZero() || qb.isZero())
return T(0);
// numerator = |qa||qb|sin(theta)
const T numerator = cross(qa, qb);
// If numerator is 0, regardless of denominator, query is on the
// surface, which we treat as no solid angle.
if (numerator == 0)
return T(0);
// denominator = |qa||qb|cos(theta)
const T denominator = dot(qa,qb);
// numerator/denominator = tan(theta)
return SYSatan2(numerator, denominator);
}
/// Class for quickly approximating signed subtended angle of a large curve
/// from many query points. This is useful for computing the
/// generalized winding number at many points.
///
/// NOTE: This is currently only instantiated for <float,float>.
template<typename T,typename S>
class UT_SubtendedAngle
{
public:
/// This is outlined so that we don't need to include UT_BVHImpl.h
inline UT_SubtendedAngle();
/// This is outlined so that we don't need to include UT_BVHImpl.h
inline ~UT_SubtendedAngle();
/// NOTE: This does not take ownership over segment_points or positions,
/// but does keep pointers to them, so the caller must keep them in
/// scope for the lifetime of this structure.
UT_SubtendedAngle(
const int nsegments,
const int *const segment_points,
const int npoints,
const UT_Vector2T<S> *const positions,
const int order = 2)
: UT_SubtendedAngle()
{ init(nsegments, segment_points, npoints, positions, order); }
/// Initialize the tree and data.
/// NOTE: It is safe to call init on a UT_SolidAngle that has had init
/// called on it before, to re-initialize it.
inline void init(
const int nsegments,
const int *const segment_points,
const int npoints,
const UT_Vector2T<S> *const positions,
const int order = 2);
/// Frees myTree and myData, and clears the rest.
inline void clear();
/// Returns true if this is clear
bool isClear() const
{ return myNSegments == 0; }
/// Returns an approximation of the signed solid angle of the mesh from the specified query_point
/// accuracy_scale is the value of (maxP/q) beyond which the approximation of the box will be used.
inline T computeAngle(const UT_Vector2T<T> &query_point, const T accuracy_scale = T(2.0)) const;
private:
struct BoxData;
static constexpr uint BVH_N = 4;
UT_BVH<BVH_N> myTree;
int myNBoxes;
int myOrder;
std::unique_ptr<BoxData[]> myData;
int myNSegments;
const int *mySegmentPoints;
int myNPoints;
const UT_Vector2T<S> *myPositions;
};
} // End HDK_Sample namespace
}}
#endif
/*
* Copyright (c) 2018 Side Effects Software Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* COMMENTS:
* A wrapper function for the "free" function, used by UT_(Small)Array
*/
#include <stdlib.h>
namespace igl { namespace FastWindingNumber {
// This needs to be here or else the warning suppression doesn't work because
// the templated calling code won't otherwise be compiled until after we've
// already popped the warning.state. So we just always disable this at file
// scope here.
#if defined(__GNUC__) && !defined(__clang__)
_Pragma("GCC diagnostic push")
_Pragma("GCC diagnostic ignored \"-Wfree-nonheap-object\"")
#endif
inline void ut_ArrayImplFree(void *p)
{
free(p);
}
#if defined(__GNUC__) && !defined(__clang__)
_Pragma("GCC diagnostic pop")
#endif
} }
/*
* Copyright (c) 2018 Side Effects Software Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* COMMENTS:
* Functions and structures for computing solid angles.
*/
#include <igl/parallel_for.h>
#include <type_traits>
#include <utility>
#define SOLID_ANGLE_TIME_PRECOMPUTE 0
#if SOLID_ANGLE_TIME_PRECOMPUTE
#include <UT/UT_StopWatch.h>
#endif
#define SOLID_ANGLE_DEBUG 0
#if SOLID_ANGLE_DEBUG
#include <UT/UT_Debug.h>
#endif
#define TAYLOR_SERIES_ORDER 2
namespace igl { namespace FastWindingNumber {
namespace HDK_Sample {
template<typename T,typename S>
struct UT_SolidAngle<T,S>::BoxData
{
void clear()
{
// Set everything to zero
memset(this,0,sizeof(*this));
}
using Type = typename std::conditional<BVH_N==4 && std::is_same<T,float>::value, v4uf, UT_FixedVector<T,BVH_N>>::type;
using SType = typename std::conditional<BVH_N==4 && std::is_same<S,float>::value, v4uf, UT_FixedVector<S,BVH_N>>::type;
/// An upper bound on the squared distance from myAverageP to the farthest point in the box.
SType myMaxPDist2;
/// Centre of mass of the mesh surface in this box
UT_FixedVector<Type,3> myAverageP;
/// Unnormalized, area-weighted normal of the mesh in this box
UT_FixedVector<Type,3> myN;
#if TAYLOR_SERIES_ORDER >= 1
/// Values for Omega_1
/// @{
UT_FixedVector<Type,3> myNijDiag; // Nxx, Nyy, Nzz
Type myNxy_Nyx; // Nxy+Nyx
Type myNyz_Nzy; // Nyz+Nzy
Type myNzx_Nxz; // Nzx+Nxz
/// @}
#endif
#if TAYLOR_SERIES_ORDER >= 2
/// Values for Omega_2
/// @{
UT_FixedVector<Type,3> myNijkDiag; // Nxxx, Nyyy, Nzzz
Type mySumPermuteNxyz; // (Nxyz+Nxzy+Nyzx+Nyxz+Nzxy+Nzyx) = 2*(Nxyz+Nyzx+Nzxy)
Type my2Nxxy_Nyxx; // Nxxy+Nxyx+Nyxx = 2Nxxy+Nyxx
Type my2Nxxz_Nzxx; // Nxxz+Nxzx+Nzxx = 2Nxxz+Nzxx
Type my2Nyyz_Nzyy; // Nyyz+Nyzy+Nzyy = 2Nyyz+Nzyy
Type my2Nyyx_Nxyy; // Nyyx+Nyxy+Nxyy = 2Nyyx+Nxyy
Type my2Nzzx_Nxzz; // Nzzx+Nzxz+Nxzz = 2Nzzx+Nxzz
Type my2Nzzy_Nyzz; // Nzzy+Nzyz+Nyzz = 2Nzzy+Nyzz
/// @}
#endif
};
template<typename T,typename S>
inline UT_SolidAngle<T,S>::UT_SolidAngle()
: myTree()
, myNBoxes(0)
, myOrder(2)
, myData(nullptr)
, myNTriangles(0)
, myTrianglePoints(nullptr)
, myNPoints(0)
, myPositions(nullptr)
{}
template<typename T,typename S>
inline UT_SolidAngle<T,S>::~UT_SolidAngle()
{
// Default destruction works, but this needs to be outlined
// to avoid having to include UT_BVHImpl.h in the header,
// (for the UT_UniquePtr destructor.)
}
template<typename T,typename S>
inline void UT_SolidAngle<T,S>::init(
const int ntriangles,
const int *const triangle_points,
const int npoints,
const UT_Vector3T<S> *const positions,
const int order)
{
#if SOLID_ANGLE_DEBUG
UTdebugFormat("");
UTdebugFormat("");
UTdebugFormat("Building BVH for {} ntriangles on {} points:", ntriangles, npoints);
#endif
myOrder = order;
myNTriangles = ntriangles;
myTrianglePoints = triangle_points;
myNPoints = npoints;
myPositions = positions;
#if SOLID_ANGLE_TIME_PRECOMPUTE
UT_StopWatch timer;
timer.start();
#endif
UT_SmallArray<UT::Box<S,3>> triangle_boxes;
triangle_boxes.setSizeNoInit(ntriangles);
if (ntriangles < 16*1024)
{
const int *cur_triangle_points = triangle_points;
for (int i = 0; i < ntriangles; ++i, cur_triangle_points += 3)
{
UT::Box<S,3> &box = triangle_boxes[i];
box.initBounds(positions[cur_triangle_points[0]]);
box.enlargeBounds(positions[cur_triangle_points[1]]);
box.enlargeBounds(positions[cur_triangle_points[2]]);
}
}
else
{
igl::parallel_for(ntriangles,
[triangle_points,&triangle_boxes,positions](int i)
{
const int *cur_triangle_points = triangle_points + i*3;
UT::Box<S,3> &box = triangle_boxes[i];
box.initBounds(positions[cur_triangle_points[0]]);
box.enlargeBounds(positions[cur_triangle_points[1]]);
box.enlargeBounds(positions[cur_triangle_points[2]]);
});
}
#if SOLID_ANGLE_TIME_PRECOMPUTE
double time = timer.stop();
UTdebugFormat("{} s to create bounding boxes.", time);
timer.start();
#endif
myTree.template init<UT::BVH_Heuristic::BOX_AREA,S,3>(triangle_boxes.array(), ntriangles);
#if SOLID_ANGLE_TIME_PRECOMPUTE
time = timer.stop();
UTdebugFormat("{} s to initialize UT_BVH structure. {} nodes", time, myTree.getNumNodes());
#endif
//myTree.debugDump();
const int nnodes = myTree.getNumNodes();
myNBoxes = nnodes;
BoxData *box_data = new BoxData[nnodes];
myData.reset(box_data);
// Some data are only needed during initialization.
struct LocalData
{
// Bounding box
UT::Box<S,3> myBox;
// P and N are needed from each child for computing Nij.
UT_Vector3T<T> myAverageP;
UT_Vector3T<T> myAreaP;
UT_Vector3T<T> myN;
// Unsigned area is needed for computing the average position.
T myArea;
#if TAYLOR_SERIES_ORDER >= 1
// These are needed for computing Nijk.
UT_Vector3T<T> myNijDiag;
T myNxy; T myNyx;
T myNyz; T myNzy;
T myNzx; T myNxz;
#endif
#if TAYLOR_SERIES_ORDER >= 2
UT_Vector3T<T> myNijkDiag; // Nxxx, Nyyy, Nzzz
T mySumPermuteNxyz; // (Nxyz+Nxzy+Nyzx+Nyxz+Nzxy+Nzyx) = 2*(Nxyz+Nyzx+Nzxy)
T my2Nxxy_Nyxx; // Nxxy+Nxyx+Nyxx = 2Nxxy+Nyxx
T my2Nxxz_Nzxx; // Nxxz+Nxzx+Nzxx = 2Nxxz+Nzxx
T my2Nyyz_Nzyy; // Nyyz+Nyzy+Nzyy = 2Nyyz+Nzyy
T my2Nyyx_Nxyy; // Nyyx+Nyxy+Nxyy = 2Nyyx+Nxyy
T my2Nzzx_Nxzz; // Nzzx+Nzxz+Nxzz = 2Nzzx+Nxzz
T my2Nzzy_Nyzz; // Nzzy+Nzyz+Nyzz = 2Nzzy+Nyzz
#endif
};
struct PrecomputeFunctors
{
BoxData *const myBoxData;
const UT::Box<S,3> *const myTriangleBoxes;
const int *const myTrianglePoints;
const UT_Vector3T<S> *const myPositions;
const int myOrder;
PrecomputeFunctors(
BoxData *box_data,
const UT::Box<S,3> *triangle_boxes,
const int *triangle_points,
const UT_Vector3T<S> *positions,
const int order)
: myBoxData(box_data)
, myTriangleBoxes(triangle_boxes)
, myTrianglePoints(triangle_points)
, myPositions(positions)
, myOrder(order)
{}
constexpr SYS_FORCE_INLINE bool pre(const int nodei, LocalData *data_for_parent) const
{
return true;
}
void item(const int itemi, const int parent_nodei, LocalData &data_for_parent) const
{
const UT_Vector3T<S> *const positions = myPositions;
const int *const cur_triangle_points = myTrianglePoints + 3*itemi;
const UT_Vector3T<T> a = positions[cur_triangle_points[0]];
const UT_Vector3T<T> b = positions[cur_triangle_points[1]];
const UT_Vector3T<T> c = positions[cur_triangle_points[2]];
const UT_Vector3T<T> ab = b-a;
const UT_Vector3T<T> ac = c-a;
const UT::Box<S,3> &triangle_box = myTriangleBoxes[itemi];
data_for_parent.myBox.initBounds(triangle_box.getMin(), triangle_box.getMax());
// Area-weighted normal (unnormalized)
const UT_Vector3T<T> N = T(0.5)*cross(ab,ac);
const T area2 = N.length2();
const T area = SYSsqrt(area2);
const UT_Vector3T<T> P = (a+b+c)/3;
data_for_parent.myAverageP = P;
data_for_parent.myAreaP = P*area;
data_for_parent.myN = N;
#if SOLID_ANGLE_DEBUG
UTdebugFormat("");
UTdebugFormat("Triangle {}: P = {}; N = {}; area = {}", itemi, P, N, area);
UTdebugFormat(" box = {}", data_for_parent.myBox);
#endif
data_for_parent.myArea = area;
#if TAYLOR_SERIES_ORDER >= 1
const int order = myOrder;
if (order < 1)
return;
// NOTE: Due to P being at the centroid, triangles have Nij = 0
// contributions to Nij.
data_for_parent.myNijDiag = T(0);
data_for_parent.myNxy = 0; data_for_parent.myNyx = 0;
data_for_parent.myNyz = 0; data_for_parent.myNzy = 0;
data_for_parent.myNzx = 0; data_for_parent.myNxz = 0;
#endif
#if TAYLOR_SERIES_ORDER >= 2
if (order < 2)
return;
// If it's zero-length, the results are zero, so we can skip.
if (area == 0)
{
data_for_parent.myNijkDiag = T(0);
data_for_parent.mySumPermuteNxyz = 0;
data_for_parent.my2Nxxy_Nyxx = 0;
data_for_parent.my2Nxxz_Nzxx = 0;
data_for_parent.my2Nyyz_Nzyy = 0;
data_for_parent.my2Nyyx_Nxyy = 0;
data_for_parent.my2Nzzx_Nxzz = 0;
data_for_parent.my2Nzzy_Nyzz = 0;
return;
}
// We need to use the NORMALIZED normal to multiply the integrals by.
UT_Vector3T<T> n = N/area;
// Figure out the order of a, b, and c in x, y, and z
// for use in computing the integrals for Nijk.
UT_Vector3T<T> values[3] = {a, b, c};
int order_x[3] = {0,1,2};
if (a[0] > b[0])
std::swap(order_x[0],order_x[1]);
if (values[order_x[0]][0] > c[0])
std::swap(order_x[0],order_x[2]);
if (values[order_x[1]][0] > values[order_x[2]][0])
std::swap(order_x[1],order_x[2]);
T dx = values[order_x[2]][0] - values[order_x[0]][0];
int order_y[3] = {0,1,2};
if (a[1] > b[1])
std::swap(order_y[0],order_y[1]);
if (values[order_y[0]][1] > c[1])
std::swap(order_y[0],order_y[2]);
if (values[order_y[1]][1] > values[order_y[2]][1])
std::swap(order_y[1],order_y[2]);
T dy = values[order_y[2]][1] - values[order_y[0]][1];
int order_z[3] = {0,1,2};
if (a[2] > b[2])
std::swap(order_z[0],order_z[1]);
if (values[order_z[0]][2] > c[2])
std::swap(order_z[0],order_z[2]);
if (values[order_z[1]][2] > values[order_z[2]][2])
std::swap(order_z[1],order_z[2]);
T dz = values[order_z[2]][2] - values[order_z[0]][2];
auto &&compute_integrals = [](
const UT_Vector3T<T> &a,
const UT_Vector3T<T> &b,
const UT_Vector3T<T> &c,
const UT_Vector3T<T> &P,
T *integral_ii,
T *integral_ij,
T *integral_ik,
const int i)
{
#if SOLID_ANGLE_DEBUG
UTdebugFormat(" Splitting on {}; a = {}; b = {}; c = {}", char('x'+i), a, b, c);
#endif
// NOTE: a, b, and c must be in order of the i axis.
// We're splitting the triangle at the middle i coordinate.
const UT_Vector3T<T> oab = b - a;
const UT_Vector3T<T> oac = c - a;
const UT_Vector3T<T> ocb = b - c;
UT_ASSERT_MSG_P(oac[i] > 0, "This should have been checked by the caller.");
const T t = oab[i]/oac[i];
UT_ASSERT_MSG_P(t >= 0 && t <= 1, "Either sorting must have gone wrong, or there are input NaNs.");
const int j = (i==2) ? 0 : (i+1);
const int k = (j==2) ? 0 : (j+1);
const T jdiff = t*oac[j] - oab[j];
const T kdiff = t*oac[k] - oab[k];
UT_Vector3T<T> cross_a;
cross_a[0] = (jdiff*oab[k] - kdiff*oab[j]);
cross_a[1] = kdiff*oab[i];
cross_a[2] = jdiff*oab[i];
UT_Vector3T<T> cross_c;
cross_c[0] = (jdiff*ocb[k] - kdiff*ocb[j]);
cross_c[1] = kdiff*ocb[i];
cross_c[2] = jdiff*ocb[i];
const T area_scale_a = cross_a.length();
const T area_scale_c = cross_c.length();
const T Pai = a[i] - P[i];
const T Pci = c[i] - P[i];
// Integral over the area of the triangle of (pi^2)dA,
// by splitting the triangle into two at b, the a side
// and the c side.
const T int_ii_a = area_scale_a*(T(0.5)*Pai*Pai + T(2.0/3.0)*Pai*oab[i] + T(0.25)*oab[i]*oab[i]);
const T int_ii_c = area_scale_c*(T(0.5)*Pci*Pci + T(2.0/3.0)*Pci*ocb[i] + T(0.25)*ocb[i]*ocb[i]);
*integral_ii = int_ii_a + int_ii_c;
#if SOLID_ANGLE_DEBUG
UTdebugFormat(" integral_{}{}_a = {}; integral_{}{}_c = {}", char('x'+i), char('x'+i), int_ii_a, char('x'+i), char('x'+i), int_ii_c);
#endif
int jk = j;
T *integral = integral_ij;
T diff = jdiff;
while (true) // This only does 2 iterations, one for j and one for k
{
if (integral)
{
T obmidj = b[jk] + T(0.5)*diff;
T oabmidj = obmidj - a[jk];
T ocbmidj = obmidj - c[jk];
T Paj = a[jk] - P[jk];
T Pcj = c[jk] - P[jk];
// Integral over the area of the triangle of (pi*pj)dA
const T int_ij_a = area_scale_a*(T(0.5)*Pai*Paj + T(1.0/3.0)*Pai*oabmidj + T(1.0/3.0)*Paj*oab[i] + T(0.25)*oab[i]*oabmidj);
const T int_ij_c = area_scale_c*(T(0.5)*Pci*Pcj + T(1.0/3.0)*Pci*ocbmidj + T(1.0/3.0)*Pcj*ocb[i] + T(0.25)*ocb[i]*ocbmidj);
*integral = int_ij_a + int_ij_c;
#if SOLID_ANGLE_DEBUG
UTdebugFormat(" integral_{}{}_a = {}; integral_{}{}_c = {}", char('x'+i), char('x'+jk), int_ij_a, char('x'+i), char('x'+jk), int_ij_c);
#endif
}
if (jk == k)
break;
jk = k;
integral = integral_ik;
diff = kdiff;
}
};
T integral_xx = 0;
T integral_xy = 0;
T integral_yy = 0;
T integral_yz = 0;
T integral_zz = 0;
T integral_zx = 0;
// Note that if the span of any axis is zero, the integral must be zero,
// since there's a factor of (p_i-P_i), i.e. value minus average,
// and every value must be equal to the average, giving zero.
if (dx > 0)
{
compute_integrals(
values[order_x[0]], values[order_x[1]], values[order_x[2]], P,
&integral_xx, ((dx >= dy && dy > 0) ? &integral_xy : nullptr), ((dx >= dz && dz > 0) ? &integral_zx : nullptr), 0);
}
if (dy > 0)
{
compute_integrals(
values[order_y[0]], values[order_y[1]], values[order_y[2]], P,
&integral_yy, ((dy >= dz && dz > 0) ? &integral_yz : nullptr), ((dx < dy && dx > 0) ? &integral_xy : nullptr), 1);
}
if (dz > 0)
{
compute_integrals(
values[order_z[0]], values[order_z[1]], values[order_z[2]], P,
&integral_zz, ((dx < dz && dx > 0) ? &integral_zx : nullptr), ((dy < dz && dy > 0) ? &integral_yz : nullptr), 2);
}
UT_Vector3T<T> Niii;
Niii[0] = integral_xx;
Niii[1] = integral_yy;
Niii[2] = integral_zz;
Niii *= n;
data_for_parent.myNijkDiag = Niii;
data_for_parent.mySumPermuteNxyz = 2*(n[0]*integral_yz + n[1]*integral_zx + n[2]*integral_xy);
T Nxxy = n[0]*integral_xy;
T Nxxz = n[0]*integral_zx;
T Nyyz = n[1]*integral_yz;
T Nyyx = n[1]*integral_xy;
T Nzzx = n[2]*integral_zx;
T Nzzy = n[2]*integral_yz;
data_for_parent.my2Nxxy_Nyxx = 2*Nxxy + n[1]*integral_xx;
data_for_parent.my2Nxxz_Nzxx = 2*Nxxz + n[2]*integral_xx;
data_for_parent.my2Nyyz_Nzyy = 2*Nyyz + n[2]*integral_yy;
data_for_parent.my2Nyyx_Nxyy = 2*Nyyx + n[0]*integral_yy;
data_for_parent.my2Nzzx_Nxzz = 2*Nzzx + n[0]*integral_zz;
data_for_parent.my2Nzzy_Nyzz = 2*Nzzy + n[1]*integral_zz;
#if SOLID_ANGLE_DEBUG
UTdebugFormat(" integral_xx = {}; yy = {}; zz = {}", integral_xx, integral_yy, integral_zz);
UTdebugFormat(" integral_xy = {}; yz = {}; zx = {}", integral_xy, integral_yz, integral_zx);
#endif
#endif
}
void post(const int nodei, const int parent_nodei, LocalData *data_for_parent, const int nchildren, const LocalData *child_data_array) const
{
// NOTE: Although in the general case, data_for_parent may be null for the root call,
// this functor assumes that it's non-null, so the call below must pass a non-null pointer.
BoxData ¤t_box_data = myBoxData[nodei];
UT_Vector3T<T> N = child_data_array[0].myN;
((T*)¤t_box_data.myN[0])[0] = N[0];
((T*)¤t_box_data.myN[1])[0] = N[1];
((T*)¤t_box_data.myN[2])[0] = N[2];
UT_Vector3T<T> areaP = child_data_array[0].myAreaP;
T area = child_data_array[0].myArea;
UT_Vector3T<T> local_P = child_data_array[0].myAverageP;
((T*)¤t_box_data.myAverageP[0])[0] = local_P[0];
((T*)¤t_box_data.myAverageP[1])[0] = local_P[1];
((T*)¤t_box_data.myAverageP[2])[0] = local_P[2];
for (int i = 1; i < nchildren; ++i)
{
const UT_Vector3T<T> local_N = child_data_array[i].myN;
N += local_N;
((T*)¤t_box_data.myN[0])[i] = local_N[0];
((T*)¤t_box_data.myN[1])[i] = local_N[1];
((T*)¤t_box_data.myN[2])[i] = local_N[2];
areaP += child_data_array[i].myAreaP;
area += child_data_array[i].myArea;
const UT_Vector3T<T> local_P = child_data_array[i].myAverageP;
((T*)¤t_box_data.myAverageP[0])[i] = local_P[0];
((T*)¤t_box_data.myAverageP[1])[i] = local_P[1];
((T*)¤t_box_data.myAverageP[2])[i] = local_P[2];
}
for (int i = nchildren; i < BVH_N; ++i)
{
// Set to zero, just to avoid false positives for uses of uninitialized memory.
((T*)¤t_box_data.myN[0])[i] = 0;
((T*)¤t_box_data.myN[1])[i] = 0;
((T*)¤t_box_data.myN[2])[i] = 0;
((T*)¤t_box_data.myAverageP[0])[i] = 0;
((T*)¤t_box_data.myAverageP[1])[i] = 0;
((T*)¤t_box_data.myAverageP[2])[i] = 0;
}
data_for_parent->myN = N;
data_for_parent->myAreaP = areaP;
data_for_parent->myArea = area;
UT::Box<S,3> box(child_data_array[0].myBox);
for (int i = 1; i < nchildren; ++i)
box.enlargeBounds(child_data_array[i].myBox);
// Normalize P
UT_Vector3T<T> averageP;
if (area > 0)
averageP = areaP/area;
else
averageP = T(0.5)*(box.getMin() + box.getMax());
data_for_parent->myAverageP = averageP;
data_for_parent->myBox = box;
for (int i = 0; i < nchildren; ++i)
{
const UT::Box<S,3> &local_box(child_data_array[i].myBox);
const UT_Vector3T<T> &local_P = child_data_array[i].myAverageP;
const UT_Vector3T<T> maxPDiff = SYSmax(local_P-UT_Vector3T<T>(local_box.getMin()), UT_Vector3T<T>(local_box.getMax())-local_P);
((T*)¤t_box_data.myMaxPDist2)[i] = maxPDiff.length2();
}
for (int i = nchildren; i < BVH_N; ++i)
{
// This child is non-existent. If we set myMaxPDist2 to infinity, it will never
// use the approximation, and the traverseVector function can check for EMPTY.
((T*)¤t_box_data.myMaxPDist2)[i] = std::numeric_limits<T>::infinity();
}
#if TAYLOR_SERIES_ORDER >= 1
const int order = myOrder;
if (order >= 1)
{
// We now have the current box's P, so we can adjust Nij and Nijk
data_for_parent->myNijDiag = child_data_array[0].myNijDiag;
data_for_parent->myNxy = 0;
data_for_parent->myNyx = 0;
data_for_parent->myNyz = 0;
data_for_parent->myNzy = 0;
data_for_parent->myNzx = 0;
data_for_parent->myNxz = 0;
#if TAYLOR_SERIES_ORDER >= 2
data_for_parent->myNijkDiag = child_data_array[0].myNijkDiag;
data_for_parent->mySumPermuteNxyz = child_data_array[0].mySumPermuteNxyz;
data_for_parent->my2Nxxy_Nyxx = child_data_array[0].my2Nxxy_Nyxx;
data_for_parent->my2Nxxz_Nzxx = child_data_array[0].my2Nxxz_Nzxx;
data_for_parent->my2Nyyz_Nzyy = child_data_array[0].my2Nyyz_Nzyy;
data_for_parent->my2Nyyx_Nxyy = child_data_array[0].my2Nyyx_Nxyy;
data_for_parent->my2Nzzx_Nxzz = child_data_array[0].my2Nzzx_Nxzz;
data_for_parent->my2Nzzy_Nyzz = child_data_array[0].my2Nzzy_Nyzz;
#endif
for (int i = 1; i < nchildren; ++i)
{
data_for_parent->myNijDiag += child_data_array[i].myNijDiag;
#if TAYLOR_SERIES_ORDER >= 2
data_for_parent->myNijkDiag += child_data_array[i].myNijkDiag;
data_for_parent->mySumPermuteNxyz += child_data_array[i].mySumPermuteNxyz;
data_for_parent->my2Nxxy_Nyxx += child_data_array[i].my2Nxxy_Nyxx;
data_for_parent->my2Nxxz_Nzxx += child_data_array[i].my2Nxxz_Nzxx;
data_for_parent->my2Nyyz_Nzyy += child_data_array[i].my2Nyyz_Nzyy;
data_for_parent->my2Nyyx_Nxyy += child_data_array[i].my2Nyyx_Nxyy;
data_for_parent->my2Nzzx_Nxzz += child_data_array[i].my2Nzzx_Nxzz;
data_for_parent->my2Nzzy_Nyzz += child_data_array[i].my2Nzzy_Nyzz;
#endif
}
for (int j = 0; j < 3; ++j)
((T*)¤t_box_data.myNijDiag[j])[0] = child_data_array[0].myNijDiag[j];
((T*)¤t_box_data.myNxy_Nyx)[0] = child_data_array[0].myNxy + child_data_array[0].myNyx;
((T*)¤t_box_data.myNyz_Nzy)[0] = child_data_array[0].myNyz + child_data_array[0].myNzy;
((T*)¤t_box_data.myNzx_Nxz)[0] = child_data_array[0].myNzx + child_data_array[0].myNxz;
for (int j = 0; j < 3; ++j)
((T*)¤t_box_data.myNijkDiag[j])[0] = child_data_array[0].myNijkDiag[j];
((T*)¤t_box_data.mySumPermuteNxyz)[0] = child_data_array[0].mySumPermuteNxyz;
((T*)¤t_box_data.my2Nxxy_Nyxx)[0] = child_data_array[0].my2Nxxy_Nyxx;
((T*)¤t_box_data.my2Nxxz_Nzxx)[0] = child_data_array[0].my2Nxxz_Nzxx;
((T*)¤t_box_data.my2Nyyz_Nzyy)[0] = child_data_array[0].my2Nyyz_Nzyy;
((T*)¤t_box_data.my2Nyyx_Nxyy)[0] = child_data_array[0].my2Nyyx_Nxyy;
((T*)¤t_box_data.my2Nzzx_Nxzz)[0] = child_data_array[0].my2Nzzx_Nxzz;
((T*)¤t_box_data.my2Nzzy_Nyzz)[0] = child_data_array[0].my2Nzzy_Nyzz;
for (int i = 1; i < nchildren; ++i)
{
for (int j = 0; j < 3; ++j)
((T*)¤t_box_data.myNijDiag[j])[i] = child_data_array[i].myNijDiag[j];
((T*)¤t_box_data.myNxy_Nyx)[i] = child_data_array[i].myNxy + child_data_array[i].myNyx;
((T*)¤t_box_data.myNyz_Nzy)[i] = child_data_array[i].myNyz + child_data_array[i].myNzy;
((T*)¤t_box_data.myNzx_Nxz)[i] = child_data_array[i].myNzx + child_data_array[i].myNxz;
for (int j = 0; j < 3; ++j)
((T*)¤t_box_data.myNijkDiag[j])[i] = child_data_array[i].myNijkDiag[j];
((T*)¤t_box_data.mySumPermuteNxyz)[i] = child_data_array[i].mySumPermuteNxyz;
((T*)¤t_box_data.my2Nxxy_Nyxx)[i] = child_data_array[i].my2Nxxy_Nyxx;
((T*)¤t_box_data.my2Nxxz_Nzxx)[i] = child_data_array[i].my2Nxxz_Nzxx;
((T*)¤t_box_data.my2Nyyz_Nzyy)[i] = child_data_array[i].my2Nyyz_Nzyy;
((T*)¤t_box_data.my2Nyyx_Nxyy)[i] = child_data_array[i].my2Nyyx_Nxyy;
((T*)¤t_box_data.my2Nzzx_Nxzz)[i] = child_data_array[i].my2Nzzx_Nxzz;
((T*)¤t_box_data.my2Nzzy_Nyzz)[i] = child_data_array[i].my2Nzzy_Nyzz;
}
for (int i = nchildren; i < BVH_N; ++i)
{
// Set to zero, just to avoid false positives for uses of uninitialized memory.
for (int j = 0; j < 3; ++j)
((T*)¤t_box_data.myNijDiag[j])[i] = 0;
((T*)¤t_box_data.myNxy_Nyx)[i] = 0;
((T*)¤t_box_data.myNyz_Nzy)[i] = 0;
((T*)¤t_box_data.myNzx_Nxz)[i] = 0;
for (int j = 0; j < 3; ++j)
((T*)¤t_box_data.myNijkDiag[j])[i] = 0;
((T*)¤t_box_data.mySumPermuteNxyz)[i] = 0;
((T*)¤t_box_data.my2Nxxy_Nyxx)[i] = 0;
((T*)¤t_box_data.my2Nxxz_Nzxx)[i] = 0;
((T*)¤t_box_data.my2Nyyz_Nzyy)[i] = 0;
((T*)¤t_box_data.my2Nyyx_Nxyy)[i] = 0;
((T*)¤t_box_data.my2Nzzx_Nxzz)[i] = 0;
((T*)¤t_box_data.my2Nzzy_Nyzz)[i] = 0;
}
for (int i = 0; i < nchildren; ++i)
{
const LocalData &child_data = child_data_array[i];
UT_Vector3T<T> displacement = child_data.myAverageP - UT_Vector3T<T>(data_for_parent->myAverageP);
UT_Vector3T<T> N = child_data.myN;
// Adjust Nij for the change in centre P
data_for_parent->myNijDiag += N*displacement;
T Nxy = child_data.myNxy + N[0]*displacement[1];
T Nyx = child_data.myNyx + N[1]*displacement[0];
T Nyz = child_data.myNyz + N[1]*displacement[2];
T Nzy = child_data.myNzy + N[2]*displacement[1];
T Nzx = child_data.myNzx + N[2]*displacement[0];
T Nxz = child_data.myNxz + N[0]*displacement[2];
data_for_parent->myNxy += Nxy;
data_for_parent->myNyx += Nyx;
data_for_parent->myNyz += Nyz;
data_for_parent->myNzy += Nzy;
data_for_parent->myNzx += Nzx;
data_for_parent->myNxz += Nxz;
#if TAYLOR_SERIES_ORDER >= 2
if (order >= 2)
{
// Adjust Nijk for the change in centre P
data_for_parent->myNijkDiag += T(2)*displacement*child_data.myNijDiag + displacement*displacement*child_data.myN;
data_for_parent->mySumPermuteNxyz += (displacement[0]*(Nyz+Nzy) + displacement[1]*(Nzx+Nxz) + displacement[2]*(Nxy+Nyx));
data_for_parent->my2Nxxy_Nyxx +=
2*(displacement[1]*child_data.myNijDiag[0] + displacement[0]*child_data.myNxy + N[0]*displacement[0]*displacement[1])
+ 2*child_data.myNyx*displacement[0] + N[1]*displacement[0]*displacement[0];
data_for_parent->my2Nxxz_Nzxx +=
2*(displacement[2]*child_data.myNijDiag[0] + displacement[0]*child_data.myNxz + N[0]*displacement[0]*displacement[2])
+ 2*child_data.myNzx*displacement[0] + N[2]*displacement[0]*displacement[0];
data_for_parent->my2Nyyz_Nzyy +=
2*(displacement[2]*child_data.myNijDiag[1] + displacement[1]*child_data.myNyz + N[1]*displacement[1]*displacement[2])
+ 2*child_data.myNzy*displacement[1] + N[2]*displacement[1]*displacement[1];
data_for_parent->my2Nyyx_Nxyy +=
2*(displacement[0]*child_data.myNijDiag[1] + displacement[1]*child_data.myNyx + N[1]*displacement[1]*displacement[0])
+ 2*child_data.myNxy*displacement[1] + N[0]*displacement[1]*displacement[1];
data_for_parent->my2Nzzx_Nxzz +=
2*(displacement[0]*child_data.myNijDiag[2] + displacement[2]*child_data.myNzx + N[2]*displacement[2]*displacement[0])
+ 2*child_data.myNxz*displacement[2] + N[0]*displacement[2]*displacement[2];
data_for_parent->my2Nzzy_Nyzz +=
2*(displacement[1]*child_data.myNijDiag[2] + displacement[2]*child_data.myNzy + N[2]*displacement[2]*displacement[1])
+ 2*child_data.myNyz*displacement[2] + N[1]*displacement[2]*displacement[2];
}
#endif
}
}
#endif
#if SOLID_ANGLE_DEBUG
UTdebugFormat("");
UTdebugFormat("Node {}: nchildren = {}; maxP = {}", nodei, nchildren, SYSsqrt(current_box_data.myMaxPDist2));
UTdebugFormat(" P = {}; N = {}", current_box_data.myAverageP, current_box_data.myN);
#if TAYLOR_SERIES_ORDER >= 1
UTdebugFormat(" Nii = {}", current_box_data.myNijDiag);
UTdebugFormat(" Nxy+Nyx = {}; Nyz+Nzy = {}; Nyz+Nzy = {}", current_box_data.myNxy_Nyx, current_box_data.myNyz_Nzy, current_box_data.myNzx_Nxz);
#if TAYLOR_SERIES_ORDER >= 2
UTdebugFormat(" Niii = {}; 2(Nxyz+Nyzx+Nzxy) = {}", current_box_data.myNijkDiag, current_box_data.mySumPermuteNxyz);
UTdebugFormat(" 2Nxxy+Nyxx = {}; 2Nxxz+Nzxx = {}", current_box_data.my2Nxxy_Nyxx, current_box_data.my2Nxxz_Nzxx);
UTdebugFormat(" 2Nyyz+Nzyy = {}; 2Nyyx+Nxyy = {}", current_box_data.my2Nyyz_Nzyy, current_box_data.my2Nyyx_Nxyy);
UTdebugFormat(" 2Nzzx+Nxzz = {}; 2Nzzy+Nyzz = {}", current_box_data.my2Nzzx_Nxzz, current_box_data.my2Nzzy_Nyzz);
#endif
#endif
#endif
}
};
#if SOLID_ANGLE_TIME_PRECOMPUTE
timer.start();
#endif
const PrecomputeFunctors functors(box_data, triangle_boxes.array(), triangle_points, positions, order);
// NOTE: post-functor relies on non-null data_for_parent, so we have to pass one.
LocalData local_data;
myTree.template traverseParallel<LocalData>(4096, functors, &local_data);
//myTree.template traverse<LocalData>(functors);
#if SOLID_ANGLE_TIME_PRECOMPUTE
time = timer.stop();
UTdebugFormat("{} s to precompute coefficients.", time);
#endif
}
template<typename T,typename S>
inline void UT_SolidAngle<T, S>::clear()
{
myTree.clear();
myNBoxes = 0;
myOrder = 2;
myData.reset();
myNTriangles = 0;
myTrianglePoints = nullptr;
myNPoints = 0;
myPositions = nullptr;
}
template<typename T,typename S>
inline T UT_SolidAngle<T, S>::computeSolidAngle(const UT_Vector3T<T> &query_point, const T accuracy_scale) const
{
const T accuracy_scale2 = accuracy_scale*accuracy_scale;
struct SolidAngleFunctors
{
const BoxData *const myBoxData;
const UT_Vector3T<T> myQueryPoint;
const T myAccuracyScale2;
const UT_Vector3T<S> *const myPositions;
const int *const myTrianglePoints;
const int myOrder;
SolidAngleFunctors(
const BoxData *const box_data,
const UT_Vector3T<T> &query_point,
const T accuracy_scale2,
const int order,
const UT_Vector3T<S> *const positions,
const int *const triangle_points)
: myBoxData(box_data)
, myQueryPoint(query_point)
, myAccuracyScale2(accuracy_scale2)
, myOrder(order)
, myPositions(positions)
, myTrianglePoints(triangle_points)
{}
uint pre(const int nodei, T *data_for_parent) const
{
const BoxData &data = myBoxData[nodei];
const typename BoxData::Type maxP2 = data.myMaxPDist2;
UT_FixedVector<typename BoxData::Type,3> q;
q[0] = typename BoxData::Type(myQueryPoint[0]);
q[1] = typename BoxData::Type(myQueryPoint[1]);
q[2] = typename BoxData::Type(myQueryPoint[2]);
q -= data.myAverageP;
const typename BoxData::Type qlength2 = q[0]*q[0] + q[1]*q[1] + q[2]*q[2];
// If the query point is within a factor of accuracy_scale of the box radius,
// it's assumed to be not a good enough approximation, so it needs to descend.
// TODO: Is there a way to estimate the error?
static_assert((std::is_same<typename BoxData::Type,v4uf>::value), "FIXME: Implement support for other tuple types!");
v4uu descend_mask = (qlength2 <= maxP2*myAccuracyScale2);
uint descend_bitmask = _mm_movemask_ps(V4SF(descend_mask.vector));
constexpr uint allchildbits = ((uint(1)<<BVH_N)-1);
if (descend_bitmask == allchildbits)
{
*data_for_parent = 0;
return allchildbits;
}
// qlength2 must be non-zero, since it's strictly greater than something.
// We still need to be careful for NaNs, though, because the 4th power might cause problems.
const typename BoxData::Type qlength_m2 = typename BoxData::Type(1.0)/qlength2;
const typename BoxData::Type qlength_m1 = sqrt(qlength_m2);
// Normalize q to reduce issues with overflow/underflow, since we'd need the 7th power
// if we didn't normalize, and (1e-6)^-7 = 1e42, which overflows single-precision.
q *= qlength_m1;
typename BoxData::Type Omega_approx = -qlength_m2*dot(q,data.myN);
#if TAYLOR_SERIES_ORDER >= 1
const int order = myOrder;
if (order >= 1)
{
const UT_FixedVector<typename BoxData::Type,3> q2 = q*q;
const typename BoxData::Type qlength_m3 = qlength_m2*qlength_m1;
const typename BoxData::Type Omega_1 =
qlength_m3*(data.myNijDiag[0] + data.myNijDiag[1] + data.myNijDiag[2]
-typename BoxData::Type(3.0)*(dot(q2,data.myNijDiag) +
q[0]*q[1]*data.myNxy_Nyx +
q[0]*q[2]*data.myNzx_Nxz +
q[1]*q[2]*data.myNyz_Nzy));
Omega_approx += Omega_1;
#if TAYLOR_SERIES_ORDER >= 2
if (order >= 2)
{
const UT_FixedVector<typename BoxData::Type,3> q3 = q2*q;
const typename BoxData::Type qlength_m4 = qlength_m2*qlength_m2;
typename BoxData::Type temp0[3] = {
data.my2Nyyx_Nxyy+data.my2Nzzx_Nxzz,
data.my2Nzzy_Nyzz+data.my2Nxxy_Nyxx,
data.my2Nxxz_Nzxx+data.my2Nyyz_Nzyy
};
typename BoxData::Type temp1[3] = {
q[1]*data.my2Nxxy_Nyxx + q[2]*data.my2Nxxz_Nzxx,
q[2]*data.my2Nyyz_Nzyy + q[0]*data.my2Nyyx_Nxyy,
q[0]*data.my2Nzzx_Nxzz + q[1]*data.my2Nzzy_Nyzz
};
const typename BoxData::Type Omega_2 =
qlength_m4*(typename BoxData::Type(1.5)*dot(q, typename BoxData::Type(3)*data.myNijkDiag + UT_FixedVector<typename BoxData::Type,3>(temp0))
-typename BoxData::Type(7.5)*(dot(q3,data.myNijkDiag) + q[0]*q[1]*q[2]*data.mySumPermuteNxyz + dot(q2, UT_FixedVector<typename BoxData::Type,3>(temp1))));
Omega_approx += Omega_2;
}
#endif
}
#endif
// If q is so small that we got NaNs and we just have a
// small bounding box, it needs to descend.
const v4uu mask = Omega_approx.isFinite() & ~descend_mask;
Omega_approx = Omega_approx & mask;
descend_bitmask = (~_mm_movemask_ps(V4SF(mask.vector))) & allchildbits;
T sum = Omega_approx[0];
for (int i = 1; i < BVH_N; ++i)
sum += Omega_approx[i];
*data_for_parent = sum;
return descend_bitmask;
}
void item(const int itemi, const int parent_nodei, T &data_for_parent) const
{
const UT_Vector3T<S> *const positions = myPositions;
const int *const cur_triangle_points = myTrianglePoints + 3*itemi;
const UT_Vector3T<T> a = positions[cur_triangle_points[0]];
const UT_Vector3T<T> b = positions[cur_triangle_points[1]];
const UT_Vector3T<T> c = positions[cur_triangle_points[2]];
data_for_parent = UTsignedSolidAngleTri(a, b, c, myQueryPoint);
}
SYS_FORCE_INLINE void post(const int nodei, const int parent_nodei, T *data_for_parent, const int nchildren, const T *child_data_array, const uint descend_bits) const
{
T sum = (descend_bits&1) ? child_data_array[0] : 0;
for (int i = 1; i < nchildren; ++i)
sum += ((descend_bits>>i)&1) ? child_data_array[i] : 0;
*data_for_parent += sum;
}
};
const SolidAngleFunctors functors(myData.get(), query_point, accuracy_scale2, myOrder, myPositions, myTrianglePoints);
T sum;
myTree.traverseVector(functors, &sum);
return sum;
}
template<typename T,typename S>
struct UT_SubtendedAngle<T,S>::BoxData
{
void clear()
{
// Set everything to zero
memset(this,0,sizeof(*this));
}
using Type = typename std::conditional<BVH_N==4 && std::is_same<T,float>::value, v4uf, UT_FixedVector<T,BVH_N>>::type;
using SType = typename std::conditional<BVH_N==4 && std::is_same<S,float>::value, v4uf, UT_FixedVector<S,BVH_N>>::type;
/// An upper bound on the squared distance from myAverageP to the farthest point in the box.
SType myMaxPDist2;
/// Centre of mass of the mesh surface in this box
UT_FixedVector<Type,2> myAverageP;
/// Unnormalized, area-weighted normal of the mesh in this box
UT_FixedVector<Type,2> myN;
/// Values for Omega_1
/// @{
UT_FixedVector<Type,2> myNijDiag; // Nxx, Nyy
Type myNxy_Nyx; // Nxy+Nyx
/// @}
/// Values for Omega_2
/// @{
UT_FixedVector<Type,2> myNijkDiag; // Nxxx, Nyyy
Type my2Nxxy_Nyxx; // Nxxy+Nxyx+Nyxx = 2Nxxy+Nyxx
Type my2Nyyx_Nxyy; // Nyyx+Nyxy+Nxyy = 2Nyyx+Nxyy
/// @}
};
template<typename T,typename S>
inline UT_SubtendedAngle<T,S>::UT_SubtendedAngle()
: myTree()
, myNBoxes(0)
, myOrder(2)
, myData(nullptr)
, myNSegments(0)
, mySegmentPoints(nullptr)
, myNPoints(0)
, myPositions(nullptr)
{}
template<typename T,typename S>
inline UT_SubtendedAngle<T,S>::~UT_SubtendedAngle()
{
// Default destruction works, but this needs to be outlined
// to avoid having to include UT_BVHImpl.h in the header,
// (for the UT_UniquePtr destructor.)
}
template<typename T,typename S>
inline void UT_SubtendedAngle<T,S>::init(
const int nsegments,
const int *const segment_points,
const int npoints,
const UT_Vector2T<S> *const positions,
const int order)
{
#if SOLID_ANGLE_DEBUG
UTdebugFormat("");
UTdebugFormat("");
UTdebugFormat("Building BVH for {} segments on {} points:", nsegments, npoints);
#endif
myOrder = order;
myNSegments = nsegments;
mySegmentPoints = segment_points;
myNPoints = npoints;
myPositions = positions;
#if SOLID_ANGLE_TIME_PRECOMPUTE
UT_StopWatch timer;
timer.start();
#endif
UT_SmallArray<UT::Box<S,2>> segment_boxes;
segment_boxes.setSizeNoInit(nsegments);
if (nsegments < 16*1024)
{
const int *cur_segment_points = segment_points;
for (int i = 0; i < nsegments; ++i, cur_segment_points += 2)
{
UT::Box<S,2> &box = segment_boxes[i];
box.initBounds(positions[cur_segment_points[0]]);
box.enlargeBounds(positions[cur_segment_points[1]]);
}
}
else
{
igl::parallel_for(nsegments,
[segment_points,&segment_boxes,positions](int i)
{
const int *cur_segment_points = segment_points + i*2;
UT::Box<S,2> &box = segment_boxes[i];
box.initBounds(positions[cur_segment_points[0]]);
box.enlargeBounds(positions[cur_segment_points[1]]);
});
}
#if SOLID_ANGLE_TIME_PRECOMPUTE
double time = timer.stop();
UTdebugFormat("{} s to create bounding boxes.", time);
timer.start();
#endif
myTree.template init<UT::BVH_Heuristic::BOX_AREA,S,2>(segment_boxes.array(), nsegments);
#if SOLID_ANGLE_TIME_PRECOMPUTE
time = timer.stop();
UTdebugFormat("{} s to initialize UT_BVH structure. {} nodes", time, myTree.getNumNodes());
#endif
//myTree.debugDump();
const int nnodes = myTree.getNumNodes();
myNBoxes = nnodes;
BoxData *box_data = new BoxData[nnodes];
myData.reset(box_data);
// Some data are only needed during initialization.
struct LocalData
{
// Bounding box
UT::Box<S,2> myBox;
// P and N are needed from each child for computing Nij.
UT_Vector2T<T> myAverageP;
UT_Vector2T<T> myLengthP;
UT_Vector2T<T> myN;
// Unsigned length is needed for computing the average position.
T myLength;
// These are needed for computing Nijk.
UT_Vector2T<T> myNijDiag;
T myNxy; T myNyx;
UT_Vector2T<T> myNijkDiag; // Nxxx, Nyyy
T my2Nxxy_Nyxx; // Nxxy+Nxyx+Nyxx = 2Nxxy+Nyxx
T my2Nyyx_Nxyy; // Nyyx+Nyxy+Nxyy = 2Nyyx+Nxyy
};
struct PrecomputeFunctors
{
BoxData *const myBoxData;
const UT::Box<S,2> *const mySegmentBoxes;
const int *const mySegmentPoints;
const UT_Vector2T<S> *const myPositions;
const int myOrder;
PrecomputeFunctors(
BoxData *box_data,
const UT::Box<S,2> *segment_boxes,
const int *segment_points,
const UT_Vector2T<S> *positions,
const int order)
: myBoxData(box_data)
, mySegmentBoxes(segment_boxes)
, mySegmentPoints(segment_points)
, myPositions(positions)
, myOrder(order)
{}
constexpr SYS_FORCE_INLINE bool pre(const int nodei, LocalData *data_for_parent) const
{
return true;
}
void item(const int itemi, const int parent_nodei, LocalData &data_for_parent) const
{
const UT_Vector2T<S> *const positions = myPositions;
const int *const cur_segment_points = mySegmentPoints + 2*itemi;
const UT_Vector2T<T> a = positions[cur_segment_points[0]];
const UT_Vector2T<T> b = positions[cur_segment_points[1]];
const UT_Vector2T<T> ab = b-a;
const UT::Box<S,2> &segment_box = mySegmentBoxes[itemi];
data_for_parent.myBox = segment_box;
// Length-weighted normal (unnormalized)
UT_Vector2T<T> N;
N[0] = ab[1];
N[1] = -ab[0];
const T length2 = ab.length2();
const T length = SYSsqrt(length2);
const UT_Vector2T<T> P = T(0.5)*(a+b);
data_for_parent.myAverageP = P;
data_for_parent.myLengthP = P*length;
data_for_parent.myN = N;
#if SOLID_ANGLE_DEBUG
UTdebugFormat("");
UTdebugFormat("Triangle {}: P = {}; N = {}; length = {}", itemi, P, N, length);
UTdebugFormat(" box = {}", data_for_parent.myBox);
#endif
data_for_parent.myLength = length;
const int order = myOrder;
if (order < 1)
return;
// NOTE: Due to P being at the centroid, segments have Nij = 0
// contributions to Nij.
data_for_parent.myNijDiag = T(0);
data_for_parent.myNxy = 0; data_for_parent.myNyx = 0;
if (order < 2)
return;
// If it's zero-length, the results are zero, so we can skip.
if (length == 0)
{
data_for_parent.myNijkDiag = T(0);
data_for_parent.my2Nxxy_Nyxx = 0;
data_for_parent.my2Nyyx_Nxyy = 0;
return;
}
T integral_xx = ab[0]*ab[0]/T(12);
T integral_xy = ab[0]*ab[1]/T(12);
T integral_yy = ab[1]*ab[1]/T(12);
data_for_parent.myNijkDiag[0] = integral_xx*N[0];
data_for_parent.myNijkDiag[1] = integral_yy*N[1];
T Nxxy = N[0]*integral_xy;
T Nyxx = N[1]*integral_xx;
T Nyyx = N[1]*integral_xy;
T Nxyy = N[0]*integral_yy;
data_for_parent.my2Nxxy_Nyxx = 2*Nxxy + Nyxx;
data_for_parent.my2Nyyx_Nxyy = 2*Nyyx + Nxyy;
#if SOLID_ANGLE_DEBUG
UTdebugFormat(" integral_xx = {}; yy = {}", integral_xx, integral_yy);
UTdebugFormat(" integral_xy = {}", integral_xy);
#endif
}
void post(const int nodei, const int parent_nodei, LocalData *data_for_parent, const int nchildren, const LocalData *child_data_array) const
{
// NOTE: Although in the general case, data_for_parent may be null for the root call,
// this functor assumes that it's non-null, so the call below must pass a non-null pointer.
BoxData ¤t_box_data = myBoxData[nodei];
UT_Vector2T<T> N = child_data_array[0].myN;
((T*)¤t_box_data.myN[0])[0] = N[0];
((T*)¤t_box_data.myN[1])[0] = N[1];
UT_Vector2T<T> lengthP = child_data_array[0].myLengthP;
T length = child_data_array[0].myLength;
const UT_Vector2T<T> local_P = child_data_array[0].myAverageP;
((T*)¤t_box_data.myAverageP[0])[0] = local_P[0];
((T*)¤t_box_data.myAverageP[1])[0] = local_P[1];
for (int i = 1; i < nchildren; ++i)
{
const UT_Vector2T<T> local_N = child_data_array[i].myN;
N += local_N;
((T*)¤t_box_data.myN[0])[i] = local_N[0];
((T*)¤t_box_data.myN[1])[i] = local_N[1];
lengthP += child_data_array[i].myLengthP;
length += child_data_array[i].myLength;
const UT_Vector2T<T> local_P = child_data_array[i].myAverageP;
((T*)¤t_box_data.myAverageP[0])[i] = local_P[0];
((T*)¤t_box_data.myAverageP[1])[i] = local_P[1];
}
for (int i = nchildren; i < BVH_N; ++i)
{
// Set to zero, just to avoid false positives for uses of uninitialized memory.
((T*)¤t_box_data.myN[0])[i] = 0;
((T*)¤t_box_data.myN[1])[i] = 0;
((T*)¤t_box_data.myAverageP[0])[i] = 0;
((T*)¤t_box_data.myAverageP[1])[i] = 0;
}
data_for_parent->myN = N;
data_for_parent->myLengthP = lengthP;
data_for_parent->myLength = length;
UT::Box<S,2> box(child_data_array[0].myBox);
for (int i = 1; i < nchildren; ++i)
box.combine(child_data_array[i].myBox);
// Normalize P
UT_Vector2T<T> averageP;
if (length > 0)
averageP = lengthP/length;
else
averageP = T(0.5)*(box.getMin() + box.getMax());
data_for_parent->myAverageP = averageP;
data_for_parent->myBox = box;
for (int i = 0; i < nchildren; ++i)
{
const UT::Box<S,2> &local_box(child_data_array[i].myBox);
const UT_Vector2T<T> &local_P = child_data_array[i].myAverageP;
const UT_Vector2T<T> maxPDiff = SYSmax(local_P-UT_Vector2T<T>(local_box.getMin()), UT_Vector2T<T>(local_box.getMax())-local_P);
((T*)¤t_box_data.myMaxPDist2)[i] = maxPDiff.length2();
}
for (int i = nchildren; i < BVH_N; ++i)
{
// This child is non-existent. If we set myMaxPDist2 to infinity, it will never
// use the approximation, and the traverseVector function can check for EMPTY.
((T*)¤t_box_data.myMaxPDist2)[i] = std::numeric_limits<T>::infinity();
}
const int order = myOrder;
if (order >= 1)
{
// We now have the current box's P, so we can adjust Nij and Nijk
data_for_parent->myNijDiag = child_data_array[0].myNijDiag;
data_for_parent->myNxy = 0;
data_for_parent->myNyx = 0;
data_for_parent->myNijkDiag = child_data_array[0].myNijkDiag;
data_for_parent->my2Nxxy_Nyxx = child_data_array[0].my2Nxxy_Nyxx;
data_for_parent->my2Nyyx_Nxyy = child_data_array[0].my2Nyyx_Nxyy;
for (int i = 1; i < nchildren; ++i)
{
data_for_parent->myNijDiag += child_data_array[i].myNijDiag;
data_for_parent->myNijkDiag += child_data_array[i].myNijkDiag;
data_for_parent->my2Nxxy_Nyxx += child_data_array[i].my2Nxxy_Nyxx;
data_for_parent->my2Nyyx_Nxyy += child_data_array[i].my2Nyyx_Nxyy;
}
for (int j = 0; j < 2; ++j)
((T*)¤t_box_data.myNijDiag[j])[0] = child_data_array[0].myNijDiag[j];
((T*)¤t_box_data.myNxy_Nyx)[0] = child_data_array[0].myNxy + child_data_array[0].myNyx;
for (int j = 0; j < 2; ++j)
((T*)¤t_box_data.myNijkDiag[j])[0] = child_data_array[0].myNijkDiag[j];
((T*)¤t_box_data.my2Nxxy_Nyxx)[0] = child_data_array[0].my2Nxxy_Nyxx;
((T*)¤t_box_data.my2Nyyx_Nxyy)[0] = child_data_array[0].my2Nyyx_Nxyy;
for (int i = 1; i < nchildren; ++i)
{
for (int j = 0; j < 2; ++j)
((T*)¤t_box_data.myNijDiag[j])[i] = child_data_array[i].myNijDiag[j];
((T*)¤t_box_data.myNxy_Nyx)[i] = child_data_array[i].myNxy + child_data_array[i].myNyx;
for (int j = 0; j < 2; ++j)
((T*)¤t_box_data.myNijkDiag[j])[i] = child_data_array[i].myNijkDiag[j];
((T*)¤t_box_data.my2Nxxy_Nyxx)[i] = child_data_array[i].my2Nxxy_Nyxx;
((T*)¤t_box_data.my2Nyyx_Nxyy)[i] = child_data_array[i].my2Nyyx_Nxyy;
}
for (int i = nchildren; i < BVH_N; ++i)
{
// Set to zero, just to avoid false positives for uses of uninitialized memory.
for (int j = 0; j < 2; ++j)
((T*)¤t_box_data.myNijDiag[j])[i] = 0;
((T*)¤t_box_data.myNxy_Nyx)[i] = 0;
for (int j = 0; j < 2; ++j)
((T*)¤t_box_data.myNijkDiag[j])[i] = 0;
((T*)¤t_box_data.my2Nxxy_Nyxx)[i] = 0;
((T*)¤t_box_data.my2Nyyx_Nxyy)[i] = 0;
}
for (int i = 0; i < nchildren; ++i)
{
const LocalData &child_data = child_data_array[i];
UT_Vector2T<T> displacement = child_data.myAverageP - UT_Vector2T<T>(data_for_parent->myAverageP);
UT_Vector2T<T> N = child_data.myN;
// Adjust Nij for the change in centre P
data_for_parent->myNijDiag += N*displacement;
T Nxy = child_data.myNxy + N[0]*displacement[1];
T Nyx = child_data.myNyx + N[1]*displacement[0];
data_for_parent->myNxy += Nxy;
data_for_parent->myNyx += Nyx;
if (order >= 2)
{
// Adjust Nijk for the change in centre P
data_for_parent->myNijkDiag += T(2)*displacement*child_data.myNijDiag + displacement*displacement*child_data.myN;
data_for_parent->my2Nxxy_Nyxx +=
2*(displacement[1]*child_data.myNijDiag[0] + displacement[0]*child_data.myNxy + N[0]*displacement[0]*displacement[1])
+ 2*child_data.myNyx*displacement[0] + N[1]*displacement[0]*displacement[0];
data_for_parent->my2Nyyx_Nxyy +=
2*(displacement[0]*child_data.myNijDiag[1] + displacement[1]*child_data.myNyx + N[1]*displacement[1]*displacement[0])
+ 2*child_data.myNxy*displacement[1] + N[0]*displacement[1]*displacement[1];
}
}
}
#if SOLID_ANGLE_DEBUG
UTdebugFormat("");
UTdebugFormat("Node {}: nchildren = {}; maxP = {}", nodei, nchildren, SYSsqrt(current_box_data.myMaxPDist2));
UTdebugFormat(" P = {}; N = {}", current_box_data.myAverageP, current_box_data.myN);
UTdebugFormat(" Nii = {}", current_box_data.myNijDiag);
UTdebugFormat(" Nxy+Nyx = {}", current_box_data.myNxy_Nyx);
UTdebugFormat(" Niii = {}", current_box_data.myNijkDiag);
UTdebugFormat(" 2Nxxy+Nyxx = {}; 2Nyyx+Nxyy = {}", current_box_data.my2Nxxy_Nyxx, current_box_data.my2Nyyx_Nxyy);
#endif
}
};
#if SOLID_ANGLE_TIME_PRECOMPUTE
timer.start();
#endif
const PrecomputeFunctors functors(box_data, segment_boxes.array(), segment_points, positions, order);
// NOTE: post-functor relies on non-null data_for_parent, so we have to pass one.
LocalData local_data;
myTree.template traverseParallel<LocalData>(4096, functors, &local_data);
//myTree.template traverse<LocalData>(functors);
#if SOLID_ANGLE_TIME_PRECOMPUTE
time = timer.stop();
UTdebugFormat("{} s to precompute coefficients.", time);
#endif
}
template<typename T,typename S>
inline void UT_SubtendedAngle<T, S>::clear()
{
myTree.clear();
myNBoxes = 0;
myOrder = 2;
myData.reset();
myNSegments = 0;
mySegmentPoints = nullptr;
myNPoints = 0;
myPositions = nullptr;
}
template<typename T,typename S>
inline T UT_SubtendedAngle<T, S>::computeAngle(const UT_Vector2T<T> &query_point, const T accuracy_scale) const
{
const T accuracy_scale2 = accuracy_scale*accuracy_scale;
struct AngleFunctors
{
const BoxData *const myBoxData;
const UT_Vector2T<T> myQueryPoint;
const T myAccuracyScale2;
const UT_Vector2T<S> *const myPositions;
const int *const mySegmentPoints;
const int myOrder;
AngleFunctors(
const BoxData *const box_data,
const UT_Vector2T<T> &query_point,
const T accuracy_scale2,
const int order,
const UT_Vector2T<S> *const positions,
const int *const segment_points)
: myBoxData(box_data)
, myQueryPoint(query_point)
, myAccuracyScale2(accuracy_scale2)
, myOrder(order)
, myPositions(positions)
, mySegmentPoints(segment_points)
{}
uint pre(const int nodei, T *data_for_parent) const
{
const BoxData &data = myBoxData[nodei];
const typename BoxData::Type maxP2 = data.myMaxPDist2;
UT_FixedVector<typename BoxData::Type,2> q;
q[0] = typename BoxData::Type(myQueryPoint[0]);
q[1] = typename BoxData::Type(myQueryPoint[1]);
q -= data.myAverageP;
const typename BoxData::Type qlength2 = q[0]*q[0] + q[1]*q[1];
// If the query point is within a factor of accuracy_scale of the box radius,
// it's assumed to be not a good enough approximation, so it needs to descend.
// TODO: Is there a way to estimate the error?
static_assert((std::is_same<typename BoxData::Type,v4uf>::value), "FIXME: Implement support for other tuple types!");
v4uu descend_mask = (qlength2 <= maxP2*myAccuracyScale2);
uint descend_bitmask = _mm_movemask_ps(V4SF(descend_mask.vector));
constexpr uint allchildbits = ((uint(1)<<BVH_N)-1);
if (descend_bitmask == allchildbits)
{
*data_for_parent = 0;
return allchildbits;
}
// qlength2 must be non-zero, since it's strictly greater than something.
// We still need to be careful for NaNs, though, because the 4th power might cause problems.
const typename BoxData::Type qlength_m2 = typename BoxData::Type(1.0)/qlength2;
const typename BoxData::Type qlength_m1 = sqrt(qlength_m2);
// Normalize q to reduce issues with overflow/underflow, since we'd need the 6th power
// if we didn't normalize, and (1e-7)^-6 = 1e42, which overflows single-precision.
q *= qlength_m1;
typename BoxData::Type Omega_approx = -qlength_m1*dot(q,data.myN);
const int order = myOrder;
if (order >= 1)
{
const UT_FixedVector<typename BoxData::Type,2> q2 = q*q;
const typename BoxData::Type Omega_1 =
qlength_m2*(data.myNijDiag[0] + data.myNijDiag[1]
-typename BoxData::Type(2.0)*(dot(q2,data.myNijDiag) +
q[0]*q[1]*data.myNxy_Nyx));
Omega_approx += Omega_1;
if (order >= 2)
{
const UT_FixedVector<typename BoxData::Type,2> q3 = q2*q;
const typename BoxData::Type qlength_m3 = qlength_m2*qlength_m1;
typename BoxData::Type temp0[2] = {
data.my2Nyyx_Nxyy,
data.my2Nxxy_Nyxx
};
typename BoxData::Type temp1[2] = {
q[1]*data.my2Nxxy_Nyxx,
q[0]*data.my2Nyyx_Nxyy
};
const typename BoxData::Type Omega_2 =
qlength_m3*(dot(q, typename BoxData::Type(3)*data.myNijkDiag + UT_FixedVector<typename BoxData::Type,2>(temp0))
-typename BoxData::Type(4.0)*(dot(q3,data.myNijkDiag) + dot(q2, UT_FixedVector<typename BoxData::Type,2>(temp1))));
Omega_approx += Omega_2;
}
}
// If q is so small that we got NaNs and we just have a
// small bounding box, it needs to descend.
const v4uu mask = Omega_approx.isFinite() & ~descend_mask;
Omega_approx = Omega_approx & mask;
descend_bitmask = (~_mm_movemask_ps(V4SF(mask.vector))) & allchildbits;
T sum = Omega_approx[0];
for (int i = 1; i < BVH_N; ++i)
sum += Omega_approx[i];
*data_for_parent = sum;
return descend_bitmask;
}
void item(const int itemi, const int parent_nodei, T &data_for_parent) const
{
const UT_Vector2T<S> *const positions = myPositions;
const int *const cur_segment_points = mySegmentPoints + 2*itemi;
const UT_Vector2T<T> a = positions[cur_segment_points[0]];
const UT_Vector2T<T> b = positions[cur_segment_points[1]];
data_for_parent = UTsignedAngleSegment(a, b, myQueryPoint);
}
SYS_FORCE_INLINE void post(const int nodei, const int parent_nodei, T *data_for_parent, const int nchildren, const T *child_data_array, const uint descend_bits) const
{
T sum = (descend_bits&1) ? child_data_array[0] : 0;
for (int i = 1; i < nchildren; ++i)
sum += ((descend_bits>>i)&1) ? child_data_array[i] : 0;
*data_for_parent += sum;
}
};
const AngleFunctors functors(myData.get(), query_point, accuracy_scale2, myOrder, myPositions, mySegmentPoints);
T sum;
myTree.traverseVector(functors, &sum);
return sum;
}
// Instantiate our templates.
//template class UT_SolidAngle<fpreal32,fpreal32>;
// FIXME: The SIMD parts will need to be handled differently in order to support fpreal64.
//template class UT_SolidAngle<fpreal64,fpreal32>;
//template class UT_SolidAngle<fpreal64,fpreal64>;
//template class UT_SubtendedAngle<fpreal32,fpreal32>;
//template class UT_SubtendedAngle<fpreal64,fpreal32>;
//template class UT_SubtendedAngle<fpreal64,fpreal64>;
} // End HDK_Sample namespace
}}
|