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
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkDataSetSurfaceFilter.h"
#include "vtkBezierCurve.h"
#include "vtkBezierQuadrilateral.h"
#include "vtkBezierTriangle.h"
#include "vtkCell.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkCellTypes.h"
#include "vtkDoubleArray.h"
#include "vtkGenericCell.h"
#include "vtkHexahedron.h"
#include "vtkIdList.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkLagrangeQuadrilateral.h"
#include "vtkLagrangeTriangle.h"
#include "vtkLogger.h"
#include "vtkMergePoints.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
#include "vtkPyramid.h"
#include "vtkRectilinearGrid.h"
#include "vtkRectilinearGridGeometryFilter.h"
#include "vtkSmartPointer.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkStructuredData.h"
#include "vtkStructuredGrid.h"
#include "vtkStructuredGridGeometryFilter.h"
#include "vtkStructuredPoints.h"
#include "vtkTetra.h"
#include "vtkUniformGrid.h"
#include "vtkUnsignedCharArray.h"
#include "vtkUnstructuredGrid.h"
#include "vtkUnstructuredGridBase.h"
#include "vtkUnstructuredGridGeometryFilter.h"
#include "vtkVector.h"
#include "vtkVoxel.h"
#include "vtkWedge.h"
#include <algorithm>
#include <cassert>
#include <numeric>
#include <unordered_map>
namespace
{
constexpr int FSize = sizeof(vtkFastGeomQuad);
constexpr int SizeId = sizeof(vtkIdType);
constexpr int PointerSize = sizeof(void*);
constexpr bool Is64BitsSystem = PointerSize == 8;
constexpr bool IsId64Bits = SizeId == 8;
constexpr bool EasyToComputeSize = !Is64BitsSystem || IsId64Bits;
constexpr int FSizeDivSizeId = FSize / SizeId;
inline int sizeofFastQuad(int numPts)
{
return FSize +
(EasyToComputeSize ? numPts * SizeId : (numPts + (numPts & 1 /*fast %2*/)) * SizeId);
}
/**
* Implementation to compute the external polydata for a structured grid with
* blanking. The algorithm, which we call "Shrinking Faces",
* takes the min and max face along each axis and then for each cell on the
* face, keep on advancing the cell in the direction of the axis till a visible
* cell is found and then extracts the face long the chosen axis. For min face,
* this advancing is done in the positive direction of the axis while it's in
* reverse for the max face. This works well for generating an outer shell and
* is quite fast too. However we miss internal faces. So in non-fast mode, we
* don't reverse the direction instead continue along the axis while
* flip-flopping between detecting visible or invisible cells and then picking
* the appropriate face to extract.
*
* This implementation only supports 3D grids. For 2D/1D grids, the standard
* algorithm for extracting surface is adequate.
*
* This function returns false if data is not appropriate in which case the
* caller should simply fall back to the default case without blanking.
*/
template <typename DataSetT>
bool StructuredExecuteWithBlanking(
DataSetT* input, vtkPolyData* output, vtkDataSetSurfaceFilter* self)
{
if (input == nullptr)
{
return false;
}
int inExtent[6];
input->GetExtent(inExtent);
if (vtkStructuredData::GetDataDimension(inExtent) != 3 || !input->HasAnyBlankCells())
{
// no need to use this logic for non 3D cells or if no blanking is provided.
return false;
}
vtkLogScopeF(TRACE, "StructuredExecuteWithBlanking (fastMode=%d)", (int)self->GetFastMode());
vtkNew<vtkPoints> points;
points->Allocate(input->GetNumberOfPoints() / 2);
output->AllocateEstimate(input->GetNumberOfCells(), 4);
output->SetPoints(points);
// Extracts a either the min (or max) face along the `axis` for the cell
// identified by `cellId` in the input dataset.
auto getFace = [&inExtent](const int ijk[3], const int axis, bool minFace)
{
const int iAxis = (axis + 1) % 3;
const int jAxis = (axis + 2) % 3;
int ptIjk[3] = { ijk[0], ijk[1], ijk[2] };
if (!minFace)
{
++ptIjk[axis];
}
std::array<vtkIdType, 4> face;
face[0] = vtkStructuredData::ComputePointIdForExtent(inExtent, ptIjk);
++ptIjk[iAxis];
face[1] = vtkStructuredData::ComputePointIdForExtent(inExtent, ptIjk);
++ptIjk[jAxis];
face[2] = vtkStructuredData::ComputePointIdForExtent(inExtent, ptIjk);
--ptIjk[iAxis];
face[3] = vtkStructuredData::ComputePointIdForExtent(inExtent, ptIjk);
if (minFace)
{
// invert face order to get an outside pointing normal.
return std::array<vtkIdType, 4>({ face[0], face[3], face[2], face[1] });
}
return face;
};
// Passes data arrays. Also adds `originalIds` the output if `arrayName`
// non-null.
auto passData = [](vtkIdTypeArray* originalIds, vtkDataSetAttributes* inputDSA,
vtkDataSetAttributes* outputDSA, const char* arrayName)
{
const auto numValues = originalIds->GetNumberOfTuples();
outputDSA->CopyGlobalIdsOn();
outputDSA->CopyFieldOff(vtkDataSetAttributes::GhostArrayName());
outputDSA->CopyAllocate(inputDSA, numValues);
vtkNew<vtkIdList> fromIds;
fromIds->SetArray(originalIds->GetPointer(0), numValues); // don't forget to call `Release`
vtkNew<vtkIdList> toIds;
toIds->SetNumberOfIds(numValues);
std::iota(toIds->begin(), toIds->end(), 0);
outputDSA->CopyData(inputDSA, fromIds, toIds);
fromIds->Release(); // necessary to avoid double delete.
// unmark global ids, if any since we don't really preserve input global
// ids.
outputDSA->SetActiveAttribute(-1, vtkDataSetAttributes::GLOBALIDS);
if (arrayName)
{
originalIds->SetName(arrayName);
outputDSA->AddArray(originalIds);
}
outputDSA->Squeeze();
};
// This map is used to avoid inserting same point multiple times in the
// output. Since points are looked up using their ids, we simply use that to
// uniquify points and don't need any locator.
// key: input point id, value: output point id.
std::unordered_map<vtkIdType, vtkIdType> pointMap;
vtkNew<vtkIdTypeArray> originalPtIds;
originalPtIds->Allocate(input->GetNumberOfPoints());
vtkNew<vtkIdTypeArray> originalCellIds;
originalCellIds->Allocate(input->GetNumberOfCells());
auto addFaceToOutput = [&](const std::array<vtkIdType, 4>& ptIds, vtkIdType inCellId)
{
vtkIdType outPtIds[5];
for (int cc = 0; cc < 4; ++cc)
{
auto iter = pointMap.find(ptIds[cc]);
if (iter != pointMap.end())
{
outPtIds[cc] = iter->second;
}
else
{
double pt[3];
input->GetPoint(ptIds[cc], pt);
outPtIds[cc] = points->InsertNextPoint(pt);
pointMap.insert(std::make_pair(ptIds[cc], outPtIds[cc]));
originalPtIds->InsertNextValue(ptIds[cc]);
}
}
outPtIds[4] = outPtIds[0];
output->InsertNextCell(VTK_POLYGON, 5, outPtIds);
originalCellIds->InsertNextValue(inCellId);
};
for (int axis = 0; axis < 3; ++axis)
{
const int iAxis = (axis + 1) % 3;
const int jAxis = (axis + 2) % 3;
const int extent[6] = { inExtent[2 * iAxis], inExtent[2 * iAxis + 1], inExtent[2 * jAxis],
inExtent[2 * jAxis + 1], inExtent[2 * axis], inExtent[2 * axis + 1] };
// iterate over cells
for (int i = extent[0]; i < extent[1]; ++i)
{
int ijk[3];
ijk[iAxis] = i;
for (int j = extent[2]; j < extent[3]; ++j)
{
ijk[jAxis] = j;
bool minFace = true;
for (int k = extent[4]; k < extent[5]; ++k)
{
ijk[axis] = k;
const auto cellId = vtkStructuredData::ComputeCellIdForExtent(inExtent, ijk);
const bool cellVisible = input->IsCellVisible(cellId);
if ((minFace && cellVisible) || (!minFace && !cellVisible))
{
ijk[axis] =
minFace ? k : (k - 1); // this ensure correct cell-data is picked for the face.
addFaceToOutput(getFace(ijk, axis, /*minFace=*/minFace),
vtkStructuredData::ComputeCellIdForExtent(inExtent, ijk));
if (self->GetFastMode())
{
// in fast mode, we immediately start iterating from the other
// side instead to find the capping surface. we can ignore
// interior surfaces for speed.
// find max-face (reverse order)
for (int reverseK = extent[5] - 1; reverseK >= k; --reverseK)
{
ijk[axis] = reverseK;
const auto reverseCellId = vtkStructuredData::ComputeCellIdForExtent(inExtent, ijk);
if (input->IsCellVisible(reverseCellId))
{
addFaceToOutput(getFace(ijk, axis, /*minFace=*/false), reverseCellId);
break;
}
}
break;
}
minFace = !minFace;
}
}
// If not in fast mode, and we've stepped out of the volume without a
// capping-surface, add the capping surface.
if (!minFace && !self->GetFastMode())
{
const auto cellId = vtkStructuredData::ComputeCellIdForExtent(inExtent, ijk);
ijk[axis] = extent[5] - 1;
addFaceToOutput(getFace(ijk, axis, false), cellId);
}
}
}
}
// Now copy cell and point data. We want to copy global ids, however we don't
// want them to be flagged as global ids. So we do this.
passData(originalPtIds, input->GetPointData(), output->GetPointData(),
self->GetPassThroughPointIds() ? self->GetOriginalPointIdsName() : nullptr);
passData(originalCellIds, input->GetCellData(), output->GetCellData(),
self->GetPassThroughCellIds() ? self->GetOriginalCellIdsName() : nullptr);
output->Squeeze();
return true;
}
}
VTK_ABI_NAMESPACE_BEGIN
class vtkDataSetSurfaceFilter::vtkEdgeInterpolationMap
{
public:
void AddEdge(vtkIdType endpoint1, vtkIdType endpoint2, vtkIdType midpoint)
{
if (endpoint1 > endpoint2)
std::swap(endpoint1, endpoint2);
Map.insert(std::make_pair(std::make_pair(endpoint1, endpoint2), midpoint));
}
vtkIdType FindEdge(vtkIdType endpoint1, vtkIdType endpoint2)
{
if (endpoint1 == endpoint2)
{
return endpoint1;
}
if (endpoint1 > endpoint2)
std::swap(endpoint1, endpoint2);
MapType::iterator iter = Map.find(std::make_pair(endpoint1, endpoint2));
if (iter != Map.end())
{
return iter->second;
}
else
{
return -1;
}
}
void clear() { Map.clear(); }
protected:
struct HashFunction
{
public:
size_t operator()(std::pair<vtkIdType, vtkIdType> edge) const
{
return static_cast<size_t>(edge.first + edge.second);
}
};
typedef std::unordered_map<std::pair<vtkIdType, vtkIdType>, vtkIdType, HashFunction> MapType;
MapType Map;
};
vtkObjectFactoryNewMacro(vtkDataSetSurfaceFilter);
//------------------------------------------------------------------------------
vtkDataSetSurfaceFilter::vtkDataSetSurfaceFilter()
{
this->QuadHash = nullptr;
this->PointMap = nullptr;
this->EdgeMap = nullptr;
this->QuadHashLength = 0;
this->NumberOfNewCells = 0;
// Quad allocation stuff.
this->FastGeomQuadArrayLength = 0;
this->NumberOfFastGeomQuadArrays = 0;
this->FastGeomQuadArrays = nullptr;
this->NextArrayIndex = 0;
this->NextQuadIndex = 0;
this->FastMode = false;
this->PieceInvariant = 0;
this->PassThroughCellIds = 0;
this->PassThroughPointIds = 0;
this->OriginalCellIds = nullptr;
this->OriginalPointIds = nullptr;
this->OriginalCellIdsName = nullptr;
this->OriginalPointIdsName = nullptr;
this->NonlinearSubdivisionLevel = 1;
this->MatchBoundariesIgnoringCellOrder = 0;
this->AllowInterpolation = true;
this->Delegation = false;
}
//------------------------------------------------------------------------------
vtkDataSetSurfaceFilter::~vtkDataSetSurfaceFilter()
{
this->SetOriginalCellIdsName(nullptr);
this->SetOriginalPointIdsName(nullptr);
if (this->OriginalPointIds)
{
this->OriginalPointIds->Delete();
this->OriginalPointIds = nullptr;
}
if (this->OriginalCellIds)
{
this->OriginalCellIds->Delete();
this->OriginalCellIds = nullptr;
}
}
//------------------------------------------------------------------------------
int vtkDataSetSurfaceFilter::RequestData(vtkInformation* vtkNotUsed(request),
vtkInformationVector** inputVector, vtkInformationVector* outputVector)
{
// get the info objects
vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation* outInfo = outputVector->GetInformationObject(0);
// get the input and output
vtkDataSet* input = vtkDataSet::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkPolyData* output = vtkPolyData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkIdType numCells = input->GetNumberOfCells();
int wholeExt[6] = { 0, -1, 0, -1, 0, -1 };
if (input->CheckAttributes())
{
return 1;
}
if (numCells == 0)
{
vtkDebugMacro(<< "Number of cells is zero, no data to process.");
return 1;
}
if (input->GetExtentType() == VTK_3D_EXTENT)
{
const int* wholeExt32;
wholeExt32 = inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT());
std::copy(wholeExt32, wholeExt32 + 6, wholeExt);
}
switch (input->GetDataObjectType())
{
case VTK_UNSTRUCTURED_GRID:
case VTK_UNSTRUCTURED_GRID_BASE:
{
this->UnstructuredGridExecute(input, output);
output->CheckAttributes();
return 1;
}
case VTK_RECTILINEAR_GRID:
{
auto rg = vtkRectilinearGrid::SafeDownCast(input);
return this->StructuredExecute(input, output, rg->GetExtent(), wholeExt);
}
case VTK_STRUCTURED_GRID:
{
auto sg = vtkStructuredGrid::SafeDownCast(input);
return this->StructuredExecute(input, output, sg->GetExtent(), wholeExt);
}
case VTK_UNIFORM_GRID:
case VTK_STRUCTURED_POINTS:
case VTK_IMAGE_DATA:
{
auto img = vtkImageData::SafeDownCast(input);
return this->StructuredExecute(input, output, img->GetExtent(), wholeExt);
}
case VTK_POLY_DATA:
{
vtkPolyData* inPd = vtkPolyData::SafeDownCast(input);
output->ShallowCopy(inPd);
if (this->PassThroughCellIds)
{
// make a 1:1 mapping
this->OriginalCellIds = vtkIdTypeArray::New();
this->OriginalCellIds->SetName(this->GetOriginalCellIdsName());
this->OriginalCellIds->SetNumberOfComponents(1);
vtkCellData* outputCD = output->GetCellData();
outputCD->AddArray(this->OriginalCellIds);
vtkIdType numTup = output->GetNumberOfCells();
this->OriginalCellIds->SetNumberOfValues(numTup);
for (vtkIdType cId = 0; cId < numTup; cId++)
{
if (this->CheckAbort())
{
break;
}
this->OriginalCellIds->SetValue(cId, cId);
}
this->OriginalCellIds->Delete();
this->OriginalCellIds = nullptr;
}
if (this->PassThroughPointIds)
{
// make a 1:1 mapping
this->OriginalPointIds = vtkIdTypeArray::New();
this->OriginalPointIds->SetName(this->GetOriginalPointIdsName());
this->OriginalPointIds->SetNumberOfComponents(1);
vtkPointData* outputPD = output->GetPointData();
outputPD->AddArray(this->OriginalPointIds);
vtkIdType numTup = output->GetNumberOfPoints();
this->OriginalPointIds->SetNumberOfValues(numTup);
for (vtkIdType cId = 0; cId < numTup; cId++)
{
if (this->CheckAbort())
{
break;
}
this->OriginalPointIds->SetValue(cId, cId);
}
this->OriginalPointIds->Delete();
this->OriginalPointIds = nullptr;
}
return 1;
}
default:
return this->DataSetExecute(input, output);
}
}
//------------------------------------------------------------------------------
void vtkDataSetSurfaceFilter::EstimateStructuredDataArraySizes(
vtkIdType* ext, vtkIdType* wholeExt, vtkIdType& numPoints, vtkIdType& numCells)
{
// Sanity Checks
assert(ext != nullptr);
assert(wholeExt != nullptr);
numPoints = numCells = 0;
// xMin face
if (ext[0] == wholeExt[0] && ext[2] != ext[3] && ext[4] != ext[5] && ext[0] != ext[1])
{
numCells += (ext[3] - ext[2]) * (ext[5] - ext[4]);
numPoints += (ext[3] - ext[2] + 1) * (ext[5] - ext[4] + 1);
}
// xMax face
if (ext[1] == wholeExt[1] && ext[2] != ext[3] && ext[4] != ext[5])
{
numCells += (ext[3] - ext[2]) * (ext[5] - ext[4]);
numPoints += (ext[3] - ext[2] + 1) * (ext[5] - ext[4] + 1);
}
// yMin face
if (ext[2] == wholeExt[2] && ext[0] != ext[1] && ext[4] != ext[5] && ext[2] != ext[3])
{
numCells += (ext[1] - ext[0]) * (ext[5] - ext[4]);
numPoints += (ext[1] - ext[0] + 1) * (ext[5] - ext[4] + 1);
}
// yMax face
if (ext[3] == wholeExt[3] && ext[0] != ext[1] && ext[4] != ext[5])
{
numCells += (ext[1] - ext[0]) * (ext[5] - ext[4]);
numPoints += (ext[1] - ext[0] + 1) * (ext[5] - ext[4] + 1);
}
// zMin face
if (ext[4] == wholeExt[4] && ext[0] != ext[1] && ext[2] != ext[3] && ext[4] != ext[5])
{
numCells += (ext[1] - ext[0]) * (ext[3] - ext[2]);
numPoints += (ext[1] - ext[0] + 1) * (ext[3] - ext[2] + 1);
}
// zMax face
if (ext[5] == wholeExt[5] && ext[0] != ext[1] && ext[2] != ext[3])
{
numCells += (ext[1] - ext[0]) * (ext[3] - ext[2]);
numPoints += (ext[1] - ext[0] + 1) * (ext[3] - ext[2] + 1);
}
}
//------------------------------------------------------------------------------
int vtkDataSetSurfaceFilter::UniformGridExecute(
vtkDataSet* input, vtkPolyData* output, vtkIdType* ext, vtkIdType* wholeExt, bool extractface[6])
{
vtkIdType numPoints, numCells;
vtkPoints* gridPnts = vtkPoints::New();
vtkCellArray* gridCells = vtkCellArray::New();
int originalPassThroughCellIds = this->PassThroughCellIds;
// Lets figure out the max number of cells and points we are going to have
numPoints = numCells = 0;
this->EstimateStructuredDataArraySizes(ext, wholeExt, numPoints, numCells);
gridPnts->Allocate(numPoints);
gridCells->AllocateEstimate(numCells, 1);
output->SetPoints(gridPnts);
gridPnts->Delete();
output->SetPolys(gridCells);
gridCells->Delete();
// Allocate attributes for copying.
output->GetPointData()->CopyGlobalIdsOn();
output->GetPointData()->CopyAllocate(input->GetPointData(), numPoints);
output->GetCellData()->CopyGlobalIdsOn();
output->GetCellData()->CopyAllocate(input->GetCellData(), numCells);
if (this->PassThroughCellIds)
{
this->OriginalCellIds = vtkIdTypeArray::New();
this->OriginalCellIds->SetName(this->GetOriginalCellIdsName());
this->OriginalCellIds->SetNumberOfComponents(1);
this->OriginalCellIds->Allocate(numCells);
output->GetCellData()->AddArray(this->OriginalCellIds);
}
if (this->PassThroughPointIds)
{
this->OriginalPointIds = vtkIdTypeArray::New();
this->OriginalPointIds->SetName(this->GetOriginalPointIdsName());
this->OriginalPointIds->SetNumberOfComponents(1);
this->OriginalPointIds->Allocate(numPoints);
output->GetPointData()->AddArray(this->OriginalPointIds);
}
// xMin face
if (extractface[0])
this->ExecuteFaceQuads(input, output, 0, ext, 0, 1, 2, wholeExt, true);
// xMax face
if (extractface[1])
this->ExecuteFaceQuads(input, output, 1, ext, 0, 2, 1, wholeExt, true);
// yMin face
if (extractface[2])
this->ExecuteFaceQuads(input, output, 0, ext, 1, 2, 0, wholeExt, true);
// yMax face
if (extractface[3])
this->ExecuteFaceQuads(input, output, 1, ext, 1, 0, 2, wholeExt, true);
// zMin face
if (extractface[4])
this->ExecuteFaceQuads(input, output, 0, ext, 2, 0, 1, wholeExt, true);
// zMax face
if (extractface[5])
this->ExecuteFaceQuads(input, output, 1, ext, 2, 1, 0, wholeExt, true);
output->Squeeze();
this->PassThroughCellIds = originalPassThroughCellIds;
if (this->OriginalPointIds)
{
this->OriginalPointIds->Delete();
this->OriginalPointIds = nullptr;
}
if (this->OriginalCellIds)
{
this->OriginalCellIds->Delete();
this->OriginalCellIds = nullptr;
}
return 1;
}
//------------------------------------------------------------------------------
int vtkDataSetSurfaceFilter::StructuredExecute(
vtkDataSet* input, vtkPolyData* output, vtkIdType* ext, vtkIdType* wholeExt)
{
if (this->Delegation)
{
vtkLogScopeF(
TRACE, "StructuredExecute Using GeometryFilter (fastMode=%d)", (int)this->GetFastMode());
vtkNew<vtkGeometryFilter> geometryFilter;
vtkGeometryFilterHelper::CopyFilterParams(this, geometryFilter);
int wholeExtent[6];
std::copy(wholeExt, wholeExt + 6, wholeExtent);
return geometryFilter->StructuredExecute(input, output, wholeExtent, nullptr, nullptr);
}
if (::StructuredExecuteWithBlanking(vtkImageData::SafeDownCast(input), output, this) ||
::StructuredExecuteWithBlanking(vtkStructuredGrid::SafeDownCast(input), output, this) ||
::StructuredExecuteWithBlanking(vtkRectilinearGrid::SafeDownCast(input), output, this))
{
return 1;
}
return this->StructuredExecuteNoBlanking(input, output, ext, wholeExt);
}
//------------------------------------------------------------------------------
// It is a pain that structured data sets do not share a common super class
// other than data set, and data set does not allow access to extent!
int vtkDataSetSurfaceFilter::StructuredExecuteNoBlanking(
vtkDataSet* input, vtkPolyData* output, vtkIdType* ext, vtkIdType* wholeExt)
{
vtkRectilinearGrid* rgrid = vtkRectilinearGrid::SafeDownCast(input);
vtkStructuredGrid* sgrid = vtkStructuredGrid::SafeDownCast(input);
if (rgrid || sgrid)
{
// Fetch the grid dimension
int iext[6];
std::copy(ext, ext + 6, iext);
int dimension = vtkStructuredData::GetDataDimension(iext);
if (dimension == 1)
{
// Use specialized filter in case of 1D grid
if (rgrid)
{
vtkNew<vtkRectilinearGridGeometryFilter> filter;
filter->SetInputData(input);
filter->SetExtent(ext[0], ext[1], ext[2], ext[3], ext[4], ext[5]);
filter->SetContainerAlgorithm(this);
filter->Update();
output->ShallowCopy(filter->GetOutput());
return 1;
}
else if (sgrid)
{
vtkNew<vtkStructuredGridGeometryFilter> filter;
filter->SetInputData(input);
filter->SetExtent(ext[0], ext[1], ext[2], ext[3], ext[4], ext[5]);
filter->SetContainerAlgorithm(this);
filter->Update();
output->ShallowCopy(filter->GetOutput());
return 1;
}
}
}
vtkIdType numPoints, cellArraySize;
vtkCellArray* outPolys;
vtkPoints* outPoints;
// Cell Array Size is a pretty good estimate.
// Lets figure out how many cells and points we are going to have.
// It may be overkill computing the exact amount, but we can do it, so ...
cellArraySize = numPoints = 0;
// xMin face
if (ext[0] == wholeExt[0] && ext[2] != ext[3] && ext[4] != ext[5] && ext[0] != ext[1])
{
cellArraySize += (ext[3] - ext[2]) * (ext[5] - ext[4]);
numPoints += (ext[3] - ext[2] + 1) * (ext[5] - ext[4] + 1);
}
// xMax face
if (ext[1] == wholeExt[1] && ext[2] != ext[3] && ext[4] != ext[5])
{
cellArraySize += (ext[3] - ext[2]) * (ext[5] - ext[4]);
numPoints += (ext[3] - ext[2] + 1) * (ext[5] - ext[4] + 1);
}
// yMin face
if (ext[2] == wholeExt[2] && ext[0] != ext[1] && ext[4] != ext[5] && ext[2] != ext[3])
{
cellArraySize += (ext[1] - ext[0]) * (ext[5] - ext[4]);
numPoints += (ext[1] - ext[0] + 1) * (ext[5] - ext[4] + 1);
}
// yMax face
if (ext[3] == wholeExt[3] && ext[0] != ext[1] && ext[4] != ext[5])
{
cellArraySize += (ext[1] - ext[0]) * (ext[5] - ext[4]);
numPoints += (ext[1] - ext[0] + 1) * (ext[5] - ext[4] + 1);
}
// zMin face
if (ext[4] == wholeExt[4] && ext[0] != ext[1] && ext[2] != ext[3] && ext[4] != ext[5])
{
cellArraySize += (ext[1] - ext[0]) * (ext[3] - ext[2]);
numPoints += (ext[1] - ext[0] + 1) * (ext[3] - ext[2] + 1);
}
// zMax face
if (ext[5] == wholeExt[5] && ext[0] != ext[1] && ext[2] != ext[3])
{
cellArraySize += (ext[1] - ext[0]) * (ext[3] - ext[2]);
numPoints += (ext[1] - ext[0] + 1) * (ext[3] - ext[2] + 1);
}
int originalPassThroughCellIds = this->PassThroughCellIds;
outPolys = vtkCellArray::New();
outPolys->AllocateEstimate(cellArraySize, 4);
output->SetPolys(outPolys);
outPolys->Delete();
outPoints = vtkPoints::New();
int dataType;
switch (input->GetDataObjectType())
{
case VTK_RECTILINEAR_GRID:
{
dataType = rgrid->GetXCoordinates()->GetDataType();
break;
}
case VTK_STRUCTURED_GRID:
{
dataType = sgrid->GetPoints()->GetDataType();
break;
}
case VTK_UNIFORM_GRID:
case VTK_STRUCTURED_POINTS:
case VTK_IMAGE_DATA:
{
dataType = VTK_DOUBLE;
break;
}
default:
dataType = VTK_DOUBLE;
vtkErrorMacro("Invalid data set type: " << input->GetDataObjectType());
outPoints->Delete();
return 1;
}
outPoints->SetDataType(dataType);
outPoints->Allocate(numPoints);
output->SetPoints(outPoints);
outPoints->Delete();
// Allocate attributes for copying.
output->GetPointData()->CopyGlobalIdsOn();
output->GetPointData()->CopyAllocate(input->GetPointData(), numPoints);
output->GetCellData()->CopyGlobalIdsOn();
output->GetCellData()->CopyAllocate(input->GetCellData(), cellArraySize);
if (this->PassThroughCellIds)
{
this->OriginalCellIds = vtkIdTypeArray::New();
this->OriginalCellIds->SetName(this->GetOriginalCellIdsName());
this->OriginalCellIds->SetNumberOfComponents(1);
this->OriginalCellIds->Allocate(cellArraySize);
output->GetCellData()->AddArray(this->OriginalCellIds);
}
if (this->PassThroughPointIds)
{
this->OriginalPointIds = vtkIdTypeArray::New();
this->OriginalPointIds->SetName(this->GetOriginalPointIdsName());
this->OriginalPointIds->SetNumberOfComponents(1);
this->OriginalPointIds->Allocate(numPoints);
output->GetPointData()->AddArray(this->OriginalPointIds);
}
// xMin face
this->ExecuteFaceQuads(input, output, 0, ext, 0, 1, 2, wholeExt);
// xMax face
this->ExecuteFaceQuads(input, output, 1, ext, 0, 2, 1, wholeExt);
// yMin face
this->ExecuteFaceQuads(input, output, 0, ext, 1, 2, 0, wholeExt);
// yMax face
this->ExecuteFaceQuads(input, output, 1, ext, 1, 0, 2, wholeExt);
// zMin face
this->ExecuteFaceQuads(input, output, 0, ext, 2, 0, 1, wholeExt);
// zMax face
this->ExecuteFaceQuads(input, output, 1, ext, 2, 1, 0, wholeExt);
output->Squeeze();
if (this->OriginalCellIds != nullptr)
{
this->OriginalCellIds->Delete();
this->OriginalCellIds = nullptr;
}
if (this->OriginalPointIds != nullptr)
{
this->OriginalPointIds->Delete();
this->OriginalPointIds = nullptr;
}
this->PassThroughCellIds = originalPassThroughCellIds;
this->CheckAbort();
return 1;
}
//------------------------------------------------------------------------------
void vtkDataSetSurfaceFilter::ExecuteFaceQuads(vtkDataSet* input, vtkPolyData* output, int maxFlag,
vtkIdType* ext, int aAxis, int bAxis, int cAxis, vtkIdType* wholeExt, bool checkVisibility)
{
vtkPoints* outPts;
vtkCellArray* outPolys;
vtkPointData *inPD, *outPD;
vtkCellData *inCD, *outCD;
vtkIdType pInc[3];
vtkIdType qInc[3];
vtkIdType cOutInc;
double pt[3];
vtkIdType inStartPtId;
vtkIdType inStartCellId;
vtkIdType outStartPtId;
vtkIdType outPtId;
vtkIdType inId, outId;
vtkIdType ib, ic;
int aA2, bA2, cA2;
outPts = output->GetPoints();
outPD = output->GetPointData();
inPD = input->GetPointData();
outCD = output->GetCellData();
inCD = input->GetCellData();
pInc[0] = 1;
pInc[1] = (ext[1] - ext[0] + 1);
pInc[2] = (ext[3] - ext[2] + 1) * pInc[1];
// quad increments (cell increments, but cInc could be confused with c axis).
qInc[0] = 1;
qInc[1] = ext[1] - ext[0];
// The conditions are for when we have one or more degenerate axes (2d or 1d cells).
if (qInc[1] == 0)
{
qInc[1] = 1;
}
qInc[2] = (ext[3] - ext[2]) * qInc[1];
if (qInc[2] == 0)
{
qInc[2] = qInc[1];
}
// Temporary variables to avoid many multiplications.
aA2 = aAxis * 2;
bA2 = bAxis * 2;
cA2 = cAxis * 2;
// We might as well put the test for this face here.
if (ext[bA2] == ext[bA2 + 1] || ext[cA2] == ext[cA2 + 1])
{
return;
}
if (maxFlag)
{
if (ext[aA2 + 1] < wholeExt[aA2 + 1])
{
return;
}
}
else
{ // min faces have a slightly different condition to avoid coincident faces.
if (ext[aA2] == ext[aA2 + 1] || ext[aA2] > wholeExt[aA2])
{
return;
}
}
// Assuming no ghost cells ...
inStartPtId = inStartCellId = 0;
// I put this confusing conditional to fix a regression test.
// If we are creating a maximum face, then we indeed have to offset
// the input cell Ids. However, vtkGeometryFilter created a 2d
// image as a max face, but the cells are copied as a min face (no
// offset). Hence maxFlag = 1 and there should be no offset.
if (maxFlag && ext[aA2] < ext[1 + aA2])
{
inStartPtId = pInc[aAxis] * (ext[aA2 + 1] - ext[aA2]);
inStartCellId = qInc[aAxis] * (ext[aA2 + 1] - ext[aA2] - 1);
}
vtkUniformGrid* grid = static_cast<vtkUniformGrid*>(input);
assert(grid != nullptr);
outStartPtId = outPts->GetNumberOfPoints();
// Make the points for this face.
for (ic = ext[cA2]; ic <= ext[cA2 + 1]; ++ic)
{
for (ib = ext[bA2]; ib <= ext[bA2 + 1]; ++ib)
{
inId = inStartPtId + (ib - ext[bA2]) * pInc[bAxis] + (ic - ext[cA2]) * pInc[cAxis];
input->GetPoint(inId, pt);
outId = outPts->InsertNextPoint(pt);
// Copy point data.
outPD->CopyData(inPD, inId, outId);
this->RecordOrigPointId(outId, inId);
}
}
// Do the cells.
cOutInc = ext[bA2 + 1] - ext[bA2] + 1;
outPolys = output->GetPolys();
// Old method for creating quads (needed for cell data.).
for (ic = ext[cA2]; ic < ext[cA2 + 1]; ++ic)
{
for (ib = ext[bA2]; ib < ext[bA2 + 1]; ++ib)
{
outPtId = outStartPtId + (ib - ext[bA2]) + (ic - ext[cA2]) * cOutInc;
inId = inStartCellId + (ib - ext[bA2]) * qInc[bAxis] + (ic - ext[cA2]) * qInc[cAxis];
if (checkVisibility && grid->IsCellVisible(inId))
{
outId = outPolys->InsertNextCell(4);
outPolys->InsertCellPoint(outPtId);
outPolys->InsertCellPoint(outPtId + cOutInc);
outPolys->InsertCellPoint(outPtId + cOutInc + 1);
outPolys->InsertCellPoint(outPtId + 1);
// Copy cell data.
outCD->CopyData(inCD, inId, outId);
this->RecordOrigCellId(outId, inId);
}
}
}
}
//------------------------------------------------------------------------------
void vtkDataSetSurfaceFilter::ExecuteFaceQuads(vtkDataSet* input, vtkPolyData* output, int maxFlag,
vtkIdType* ext, int aAxis, int bAxis, int cAxis, vtkIdType* wholeExt)
{
vtkPoints* outPts;
vtkCellArray* outPolys;
vtkPointData *inPD, *outPD;
vtkCellData *inCD, *outCD;
vtkIdType pInc[3];
vtkIdType qInc[3];
vtkIdType cOutInc;
double pt[3];
vtkIdType inStartPtId;
vtkIdType inStartCellId;
vtkIdType outStartPtId;
vtkIdType outPtId;
vtkIdType inId, outId;
vtkIdType ib, ic;
int aA2, bA2, cA2;
outPts = output->GetPoints();
outPD = output->GetPointData();
inPD = input->GetPointData();
outCD = output->GetCellData();
inCD = input->GetCellData();
pInc[0] = 1;
pInc[1] = (ext[1] - ext[0] + 1);
pInc[2] = (ext[3] - ext[2] + 1) * pInc[1];
// quad increments (cell increments, but cInc could be confused with c axis).
qInc[0] = 1;
qInc[1] = ext[1] - ext[0];
// The conditions are for when we have one or more degenerate axes (2d or 1d cells).
if (qInc[1] == 0)
{
qInc[1] = 1;
}
qInc[2] = (ext[3] - ext[2]) * qInc[1];
if (qInc[2] == 0)
{
qInc[2] = qInc[1];
}
// Temporary variables to avoid many multiplications.
aA2 = aAxis * 2;
bA2 = bAxis * 2;
cA2 = cAxis * 2;
// We might as well put the test for this face here.
if (ext[bA2] == ext[bA2 + 1] || ext[cA2] == ext[cA2 + 1])
{
return;
}
if (maxFlag)
{
if (ext[aA2 + 1] < wholeExt[aA2 + 1])
{
return;
}
}
else
{ // min faces have a slightly different condition to avoid coincident faces.
if (ext[aA2] == ext[aA2 + 1] || ext[aA2] > wholeExt[aA2])
{
return;
}
}
// Assuming no ghost cells ...
inStartPtId = inStartCellId = 0;
// I put this confusing conditional to fix a regression test.
// If we are creating a maximum face, then we indeed have to offset
// the input cell Ids. However, vtkGeometryFilter created a 2d
// image as a max face, but the cells are copied as a min face (no
// offset). Hence maxFlag = 1 and there should be no offset.
if (maxFlag && ext[aA2] < ext[1 + aA2])
{
inStartPtId = pInc[aAxis] * (ext[aA2 + 1] - ext[aA2]);
inStartCellId = qInc[aAxis] * (ext[aA2 + 1] - ext[aA2] - 1);
}
outStartPtId = outPts->GetNumberOfPoints();
// Make the points for this face.
for (ic = ext[cA2]; ic <= ext[cA2 + 1]; ++ic)
{
for (ib = ext[bA2]; ib <= ext[bA2 + 1]; ++ib)
{
inId = inStartPtId + (ib - ext[bA2]) * pInc[bAxis] + (ic - ext[cA2]) * pInc[cAxis];
input->GetPoint(inId, pt);
outId = outPts->InsertNextPoint(pt);
// Copy point data.
outPD->CopyData(inPD, inId, outId);
this->RecordOrigPointId(outId, inId);
}
}
// Do the cells.
cOutInc = ext[bA2 + 1] - ext[bA2] + 1;
outPolys = output->GetPolys();
// Old method for creating quads (needed for cell data.).
for (ic = ext[cA2]; ic < ext[cA2 + 1]; ++ic)
{
for (ib = ext[bA2]; ib < ext[bA2 + 1]; ++ib)
{
outPtId = outStartPtId + (ib - ext[bA2]) + (ic - ext[cA2]) * cOutInc;
inId = inStartCellId + (ib - ext[bA2]) * qInc[bAxis] + (ic - ext[cA2]) * qInc[cAxis];
outId = outPolys->InsertNextCell(4);
outPolys->InsertCellPoint(outPtId);
outPolys->InsertCellPoint(outPtId + cOutInc);
outPolys->InsertCellPoint(outPtId + cOutInc + 1);
outPolys->InsertCellPoint(outPtId + 1);
// Copy cell data.
outCD->CopyData(inCD, inId, outId);
this->RecordOrigCellId(outId, inId);
}
}
}
//------------------------------------------------------------------------------
int vtkDataSetSurfaceFilter::DataSetExecute(vtkDataSet* input, vtkPolyData* output)
{
vtkIdType cellId, newCellId;
int i, j;
vtkIdType numPts = input->GetNumberOfPoints();
vtkIdType numCells = input->GetNumberOfCells();
vtkCell* face;
double x[3];
vtkIdList* cellIds;
vtkIdList* pts;
vtkPoints* newPts;
vtkIdType ptId, pt;
int npts;
vtkPointData* pd = input->GetPointData();
vtkCellData* cd = input->GetCellData();
vtkPointData* outputPD = output->GetPointData();
vtkCellData* outputCD = output->GetCellData();
if (numCells == 0)
{
vtkDebugMacro(<< "Number of cells is zero, no data to process.");
return 1;
}
if (this->PassThroughCellIds)
{
this->OriginalCellIds = vtkIdTypeArray::New();
this->OriginalCellIds->SetName(this->GetOriginalCellIdsName());
this->OriginalCellIds->SetNumberOfComponents(1);
this->OriginalCellIds->Allocate(numCells);
outputCD->AddArray(this->OriginalCellIds);
}
if (this->PassThroughPointIds)
{
this->OriginalPointIds = vtkIdTypeArray::New();
this->OriginalPointIds->SetName(this->GetOriginalPointIdsName());
this->OriginalPointIds->SetNumberOfComponents(1);
this->OriginalPointIds->Allocate(numPts);
outputPD->AddArray(this->OriginalPointIds);
}
cellIds = vtkIdList::New();
pts = vtkIdList::New();
vtkDebugMacro(<< "Executing geometry filter");
// Allocate
//
newPts = vtkPoints::New();
// we don't know what type of data the input points are so
// we keep the output points to have the default type (float)
newPts->Allocate(numPts, numPts / 2);
output->AllocateEstimate(numCells, 3);
outputPD->CopyGlobalIdsOn();
outputPD->CopyAllocate(pd, numPts, numPts / 2);
outputCD->CopyGlobalIdsOn();
outputCD->CopyAllocate(cd, numCells, numCells / 2);
// Traverse cells to extract geometry
//
bool abort = false;
vtkIdType progressInterval = numCells / 20 + 1;
for (cellId = 0; cellId < numCells && !abort; cellId++)
{
// Progress and abort method support
if (!(cellId % progressInterval))
{
vtkDebugMacro(<< "Process cell #" << cellId);
this->UpdateProgress(static_cast<double>(cellId) / numCells);
abort = this->CheckAbort();
}
vtkCell* cell = input->GetCell(cellId);
switch (cell->GetCellDimension())
{
// create new points and then cell
case 0:
case 1:
case 2:
{
int type = cell->GetCellType();
if (type == VTK_EMPTY_CELL)
{
// Empty cells are not supported by vtkPolyData
break;
}
npts = cell->GetNumberOfPoints();
pts->Reset();
for (i = 0; i < npts; i++)
{
ptId = cell->GetPointId(i);
input->GetPoint(ptId, x);
pt = newPts->InsertNextPoint(x);
outputPD->CopyData(pd, ptId, pt);
this->RecordOrigPointId(pt, ptId);
pts->InsertId(i, pt);
}
newCellId = output->InsertNextCell(type, pts);
if (newCellId > 0)
{
outputCD->CopyData(cd, cellId, newCellId);
this->RecordOrigCellId(newCellId, cellId);
}
break;
}
case 3:
for (j = 0; j < cell->GetNumberOfFaces(); j++)
{
face = cell->GetFace(j);
input->GetCellNeighbors(cellId, face->PointIds, cellIds);
bool noNeighbors = cellIds->GetNumberOfIds() <= 0;
if (noNeighbors)
{
npts = face->GetNumberOfPoints();
pts->Reset();
for (i = 0; i < npts; i++)
{
ptId = face->GetPointId(i);
input->GetPoint(ptId, x);
pt = newPts->InsertNextPoint(x);
outputPD->CopyData(pd, ptId, pt);
this->RecordOrigPointId(pt, ptId);
pts->InsertId(i, pt);
}
newCellId = output->InsertNextCell(face->GetCellType(), pts);
if (newCellId > 0)
{
outputCD->CopyData(cd, cellId, newCellId);
this->RecordOrigCellId(newCellId, cellId);
}
}
}
break;
} // switch
} // for all cells
vtkDebugMacro(<< "Extracted " << newPts->GetNumberOfPoints() << " points,"
<< output->GetNumberOfCells() << " cells.");
// Update ourselves and release memory
//
output->SetPoints(newPts);
newPts->Delete();
if (this->OriginalCellIds)
{
this->OriginalCellIds->Delete();
this->OriginalCellIds = nullptr;
}
if (this->OriginalPointIds)
{
this->OriginalPointIds->Delete();
this->OriginalPointIds = nullptr;
}
// free storage
output->Squeeze();
cellIds->Delete();
pts->Delete();
return 1;
}
//------------------------------------------------------------------------------
int vtkDataSetSurfaceFilter::RequestUpdateExtent(vtkInformation* vtkNotUsed(request),
vtkInformationVector** inputVector, vtkInformationVector* outputVector)
{
// get the info objects
vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation* outInfo = outputVector->GetInformationObject(0);
int piece, numPieces, ghostLevels;
piece = outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER());
numPieces = outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES());
ghostLevels = outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_GHOST_LEVELS());
if (numPieces > 1 && this->PieceInvariant)
{
// The special execute for structured data handle boundaries internally.
// PolyData does not need any ghost levels.
vtkDataObject* dobj = inInfo->Get(vtkDataObject::DATA_OBJECT());
if (dobj && !strcmp(dobj->GetClassName(), "vtkUnstructuredGrid"))
{ // Processing does nothing for ghost levels yet so ...
// Be careful to set output ghost level value one less than default
// when they are implemented. I had trouble with multiple executes.
++ghostLevels;
}
}
inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER(), piece);
inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES(), numPieces);
inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_GHOST_LEVELS(), ghostLevels);
inInfo->Set(vtkStreamingDemandDrivenPipeline::EXACT_EXTENT(), 1);
return 1;
}
//------------------------------------------------------------------------------
int vtkDataSetSurfaceFilter::FillInputPortInformation(int, vtkInformation* info)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
return 1;
}
//------------------------------------------------------------------------------
void vtkDataSetSurfaceFilter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "PieceInvariant: " << this->GetPieceInvariant() << endl;
os << indent << "PassThroughCellIds: " << (this->GetPassThroughCellIds() ? "On\n" : "Off\n");
os << indent << "PassThroughPointIds: " << (this->GetPassThroughPointIds() ? "On\n" : "Off\n");
os << indent << "OriginalCellIdsName: " << this->GetOriginalCellIdsName() << endl;
os << indent << "OriginalPointIdsName: " << this->GetOriginalPointIdsName() << endl;
os << indent << "NonlinearSubdivisionLevel: " << this->GetNonlinearSubdivisionLevel() << endl;
os << indent
<< "MatchBoundariesIgnoringCellOrder: " << this->GetMatchBoundariesIgnoringCellOrder() << endl;
os << indent << "FastMode: " << this->GetFastMode() << endl;
os << indent << "AllowInterpolation: " << this->GetAllowInterpolation() << endl;
os << indent << "Delegation: " << this->GetDelegation() << endl;
}
//========================================================================
// Coordinate the delegation process.
int vtkDataSetSurfaceFilter::UnstructuredGridExecute(vtkDataSet* dataSetInput, vtkPolyData* output)
{
switch (dataSetInput->GetDataObjectType())
{
case VTK_UNSTRUCTURED_GRID:
return this->UnstructuredGridExecute(dataSetInput, output, nullptr);
case VTK_UNSTRUCTURED_GRID_BASE:
return this->UnstructuredGridBaseExecute(dataSetInput, output);
default:
return 0;
}
}
//------------------------------------------------------------------------------
// This method may delegate to vtkGeometryFilter. The "info", if passed in,
// provides information about the unstructured grid. This avoids the possibility of
// repeated evaluations, and back and forth delegation, as vtkGeometryFilter and
// vtkDataSetSurfaceFilter coordinate their efforts.
int vtkDataSetSurfaceFilter::UnstructuredGridExecute(
vtkDataSet* dataSetInput, vtkPolyData* output, vtkGeometryFilterHelper* info)
{
vtkUnstructuredGrid* input = vtkUnstructuredGrid::SafeDownCast(dataSetInput);
// If no info, then compute information about the unstructured grid.
// Depending on the outcome, we may process the data ourselves, or send over
// to the faster vtkGeometryFilter.
bool mayDelegate = (info == nullptr && this->Delegation);
bool info_owned = false;
if (info == nullptr)
{
info = vtkGeometryFilterHelper::CharacterizeUnstructuredGrid(input);
info_owned = true;
}
bool handleSubdivision = (!info->IsLinear);
// Before we start doing anything interesting, check if we need handle
// non-linear cells using sub-division.
if (info->IsLinear && mayDelegate)
{
vtkNew<vtkGeometryFilter> gf;
vtkGeometryFilterHelper::CopyFilterParams(this, gf.Get());
gf->UnstructuredGridExecute(dataSetInput, output, info, nullptr);
delete info;
return 1;
}
if (info_owned)
{
delete info;
}
// If here, the data is gnarly and this filter will process it.
return this->UnstructuredGridExecuteInternal(input, output, handleSubdivision);
}
//------------------------------------------------------------------------------
// Unoptimized version of UnstructuredGridExecute for non vtkUnstructuredGrid instances
int vtkDataSetSurfaceFilter::UnstructuredGridBaseExecute(
vtkDataSet* dataSetInput, vtkPolyData* output)
{
vtkUnstructuredGridBase* input = vtkUnstructuredGridBase::SafeDownCast(dataSetInput);
// Before we start doing anything interesting, check if we need handle
// non-linear cells using sub-division.
bool handleSubdivision = false;
if (this->NonlinearSubdivisionLevel >= 1)
{
// Check to see if the data actually has nonlinear cells. Handling
// nonlinear cells adds unnecessary work if we only have linear cells.
vtkIdType numCells = input->GetNumberOfCells();
if (input->IsHomogeneous())
{
if (numCells >= 1)
{
handleSubdivision = !vtkCellTypes::IsLinear(input->GetCellType(0));
}
}
else
{
for (vtkIdType cellId = 0; cellId < numCells; ++cellId)
{
if (!vtkCellTypes::IsLinear(input->GetCellType(cellId)))
{
handleSubdivision = true;
break;
}
}
}
}
return this->UnstructuredGridExecuteInternal(input, output, handleSubdivision);
}
//========================================================================
// Tris are now degenerate quads so we only need one hash table.
// We might want to change the method names from QuadHash to just Hash.
int vtkDataSetSurfaceFilter::UnstructuredGridExecuteInternal(
vtkUnstructuredGridBase* input, vtkPolyData* output, bool handleSubdivision)
{
vtkSmartPointer<vtkUnstructuredGrid> tempInput;
if (handleSubdivision)
{
// Since this filter only properly subdivides 2D cells past
// level 1, we convert 3D cells to 2D by using
// vtkUnstructuredGridGeometryFilter.
vtkNew<vtkUnstructuredGridGeometryFilter> uggf;
vtkNew<vtkUnstructuredGrid> clone;
clone->ShallowCopy(input);
uggf->SetInputData(clone);
uggf->SetPassThroughCellIds(this->PassThroughCellIds);
uggf->SetOriginalCellIdsName(this->GetOriginalCellIdsName());
uggf->SetPassThroughPointIds(this->PassThroughPointIds);
uggf->SetMatchBoundariesIgnoringCellOrder(this->MatchBoundariesIgnoringCellOrder);
uggf->SetOriginalPointIdsName(this->GetOriginalPointIdsName());
uggf->DuplicateGhostCellClippingOff();
uggf->SetContainerAlgorithm(this);
// Disable point merging as it may prevent the correct visualization
// of non-continuous attributes.
uggf->MergingOff();
uggf->Update();
tempInput = vtkSmartPointer<vtkUnstructuredGrid>::New();
tempInput->ShallowCopy(uggf->GetOutputDataObject(0));
input = tempInput;
if (this->CheckAbort())
{
return 1;
}
}
vtkUnsignedCharArray* ghosts = input->GetPointGhostArray();
vtkUnsignedCharArray* ghostCells = input->GetCellGhostArray();
vtkCellArray* newVerts;
vtkCellArray* newLines;
vtkCellArray* newPolys;
vtkPoints* newPts;
int progressCount;
vtkIdType i, j, k;
int cellType;
vtkIdType numPts = input->GetNumberOfPoints();
vtkIdType numCells = input->GetNumberOfCells();
vtkGenericCell* cell;
vtkNew<vtkIdList> pointIdList;
const vtkIdType* ids;
vtkIdType numFacePts;
vtkIdType inPtId, outPtId, numCellPts;
vtkPointData* inputPD = input->GetPointData();
vtkCellData* inputCD = input->GetCellData();
vtkFieldData* inputFD = input->GetFieldData();
vtkCellData* cd = input->GetCellData();
vtkPointData* outputPD = output->GetPointData();
vtkCellData* outputCD = output->GetCellData();
vtkFieldData* outputFD = output->GetFieldData();
vtkFastGeomQuad* q;
// Shallow copy field data not associated with points or cells
outputFD->ShallowCopy(inputFD);
// These are for the default case/
vtkIdList* pts;
vtkCell* face;
int flag2D = 0;
// These are for subdividing quadratic cells
std::vector<double> parametricCoords;
std::unique_ptr<vtkEdgeInterpolationMap> localEdgeMap(new vtkEdgeInterpolationMap());
vtkIdList* outPts;
vtkIdList* pts2;
pts = vtkIdList::New();
outPts = vtkIdList::New();
pts2 = vtkIdList::New();
cell = vtkGenericCell::New();
std::vector<double> weights;
this->NumberOfNewCells = 0;
this->InitializeQuadHash(numPts);
// Allocate
//
newPts = vtkPoints::New();
newPts->SetDataType(input->GetPoints()->GetData()->GetDataType());
newPts->Allocate(numPts);
newPolys = vtkCellArray::New();
newPolys->AllocateEstimate(numCells, 3);
newVerts = vtkCellArray::New();
newLines = vtkCellArray::New();
if (this->NonlinearSubdivisionLevel < 2)
{
outputPD->CopyGlobalIdsOn();
outputPD->CopyAllocate(inputPD, numPts, numPts / 2);
}
else
{
outputPD->InterpolateAllocate(inputPD, numPts, numPts / 2);
}
outputCD->CopyGlobalIdsOn();
outputCD->CopyAllocate(inputCD, numCells, numCells / 2);
if (this->PassThroughCellIds)
{
this->OriginalCellIds = vtkIdTypeArray::New();
this->OriginalCellIds->SetName(this->GetOriginalCellIdsName());
this->OriginalCellIds->SetNumberOfComponents(1);
}
if (this->PassThroughPointIds)
{
this->OriginalPointIds = vtkIdTypeArray::New();
this->OriginalPointIds->SetName(this->GetOriginalPointIdsName());
this->OriginalPointIds->SetNumberOfComponents(1);
}
// First insert all points. Points have to come first in poly data.
for (vtkIdType cellId = 0; cellId < numCells; cellId++)
{
cellType = input->GetCellType(cellId);
// A couple of common cases to see if things go faster.
if (cellType == VTK_VERTEX || cellType == VTK_POLY_VERTEX)
{
input->GetCellPoints(cellId, numCellPts, ids, pointIdList);
newVerts->InsertNextCell(numCellPts);
for (i = 0; i < numCellPts; i++)
{
outPtId = this->GetOutputPointId(ids[i], input, newPts, outputPD);
newVerts->InsertCellPoint(outPtId);
}
this->RecordOrigCellId(this->NumberOfNewCells, cellId);
outputCD->CopyData(cd, cellId, this->NumberOfNewCells++);
}
}
// Traverse cells to extract geometry
//
progressCount = 0;
bool abort = false;
vtkIdType progressInterval = numCells / 20 + 1;
// First insert all points lines in output and 3D geometry in hash.
// Save 2D geometry for second pass.
for (vtkIdType cellId = 0; cellId < numCells && !abort; cellId++)
{
// We skip cells marked as hidden
if (ghostCells &&
(ghostCells->GetValue(cellId) & vtkDataSetAttributes::CellGhostTypes::HIDDENCELL))
{
continue;
}
// Progress and abort method support
if (progressCount >= progressInterval)
{
vtkDebugMacro(<< "Process cell #" << cellId);
this->UpdateProgress(static_cast<double>(cellId) / numCells);
abort = this->CheckAbort();
progressCount = 0;
}
progressCount++;
cellType = input->GetCellType(cellId);
switch (cellType)
{
case VTK_VERTEX:
case VTK_POLY_VERTEX:
case VTK_EMPTY_CELL:
// Do nothing -- these were handled previously.
break;
case VTK_LINE:
case VTK_POLY_LINE:
input->GetCellPoints(cellId, numCellPts, ids, pointIdList);
newLines->InsertNextCell(numCellPts);
for (i = 0; i < numCellPts; i++)
{
outPtId = this->GetOutputPointId(ids[i], input, newPts, outputPD);
newLines->InsertCellPoint(outPtId);
}
this->RecordOrigCellId(this->NumberOfNewCells, cellId);
outputCD->CopyData(cd, cellId, this->NumberOfNewCells++);
break;
case VTK_LAGRANGE_CURVE:
case VTK_QUADRATIC_EDGE:
case VTK_CUBIC_LINE:
{
input->GetCellPoints(cellId, numCellPts, ids, pointIdList);
if (this->NonlinearSubdivisionLevel <= 1)
{
int numCellPtsAfterSubdivision = this->NonlinearSubdivisionLevel == 0 ? 2 : numCellPts;
newLines->InsertNextCell(numCellPtsAfterSubdivision);
outPtId = this->GetOutputPointId(ids[0], input, newPts, outputPD);
newLines->InsertCellPoint(outPtId);
for (i = 2; i < numCellPtsAfterSubdivision; i++)
{
outPtId = this->GetOutputPointId(ids[i], input, newPts, outputPD);
newLines->InsertCellPoint(outPtId);
}
outPtId = this->GetOutputPointId(ids[1], input, newPts, outputPD);
newLines->InsertCellPoint(outPtId);
}
else
{
int numDeltaPtsAfterSubdivision = std::pow(2, this->NonlinearSubdivisionLevel - 1);
int numCellPtsAfterSubdivision = numDeltaPtsAfterSubdivision * (numCellPts - 1) + 1;
newLines->InsertNextCell(numCellPtsAfterSubdivision);
outPtId = this->GetOutputPointId(ids[0], input, newPts, outputPD);
newLines->InsertCellPoint(outPtId);
double paramCoordDelta = 1. / (numCellPtsAfterSubdivision - 1);
input->GetCell(cellId, cell);
weights.resize(cell->GetNumberOfPoints());
double inParamCoords[3];
inParamCoords[1] = inParamCoords[2] = 0.;
for (i = 0; i < (numCellPts - 1); i++)
{
for (j = 0; j < numDeltaPtsAfterSubdivision - 1; j++)
{
inParamCoords[0] = paramCoordDelta * (numDeltaPtsAfterSubdivision * i + j + 1);
outPtId = GetInterpolatedPointId(
input, cell, inParamCoords, weights.data(), newPts, outputPD);
newLines->InsertCellPoint(outPtId);
}
if (i < numCellPts - 2)
{
outPtId = this->GetOutputPointId(ids[i + 2], input, newPts, outputPD);
newLines->InsertCellPoint(outPtId);
}
}
outPtId = this->GetOutputPointId(ids[1], input, newPts, outputPD);
newLines->InsertCellPoint(outPtId);
}
this->RecordOrigCellId(this->NumberOfNewCells, cellId);
outputCD->CopyData(cd, cellId, this->NumberOfNewCells++);
break;
}
case VTK_BEZIER_CURVE:
{
input->GetCellPoints(cellId, numCellPts, ids, pointIdList);
if (this->NonlinearSubdivisionLevel == 0 || !AllowInterpolation)
{
int numCellPtsAfterSubdivision = this->NonlinearSubdivisionLevel == 0 ? 2 : numCellPts;
newLines->InsertNextCell(numCellPtsAfterSubdivision);
outPtId = this->GetOutputPointId(ids[0], input, newPts, outputPD);
newLines->InsertCellPoint(outPtId);
for (i = 2; i < numCellPtsAfterSubdivision; i++)
{
outPtId = this->GetOutputPointId(ids[i], input, newPts, outputPD);
newLines->InsertCellPoint(outPtId);
}
outPtId = this->GetOutputPointId(ids[1], input, newPts, outputPD);
newLines->InsertCellPoint(outPtId);
}
else
{
int numDeltaPtsAfterSubdivision = std::pow(2, this->NonlinearSubdivisionLevel - 1);
int numCellPtsAfterSubdivision = numDeltaPtsAfterSubdivision * (numCellPts - 1) + 1;
newLines->InsertNextCell(numCellPtsAfterSubdivision);
input->GetCell(cellId, cell);
input->SetCellOrderAndRationalWeights(cellId, cell);
weights.resize(cell->GetNumberOfPoints());
double* pc = cell->GetParametricCoords();
outPtId = this->GetOutputPointId(ids[0], input, newPts, outputPD);
newLines->InsertCellPoint(outPtId);
if (this->NonlinearSubdivisionLevel == 1)
{
for (i = 2; i < numCellPts; i++)
{
outPtId = this->GetOutputPointIdAndInterpolate(
i, input, cell, pc, weights.data(), newPts, outputPD);
newLines->InsertCellPoint(outPtId);
}
}
else
{
double paramCoordDelta = 1. / (numCellPtsAfterSubdivision - 1);
double inParamCoords[3];
inParamCoords[1] = inParamCoords[2] = 0.;
for (i = 0; i < (numCellPts - 1); i++)
{
for (j = 0; j < numDeltaPtsAfterSubdivision - 1; j++)
{
inParamCoords[0] = paramCoordDelta * (numDeltaPtsAfterSubdivision * i + j + 1);
outPtId = GetInterpolatedPointId(
input, cell, inParamCoords, weights.data(), newPts, outputPD);
newLines->InsertCellPoint(outPtId);
}
if (i < numCellPts - 2)
{
outPtId = this->GetOutputPointIdAndInterpolate(
i + 2, input, cell, pc, weights.data(), newPts, outputPD);
newLines->InsertCellPoint(outPtId);
}
}
}
outPtId = this->GetOutputPointId(ids[1], input, newPts, outputPD);
newLines->InsertCellPoint(outPtId);
}
this->RecordOrigCellId(this->NumberOfNewCells, cellId);
outputCD->CopyData(cd, cellId, this->NumberOfNewCells++);
break;
}
case VTK_HEXAHEDRON:
input->GetCellPoints(cellId, numCellPts, ids, pointIdList);
this->InsertQuadInHash(ids[0], ids[1], ids[5], ids[4], cellId);
this->InsertQuadInHash(ids[0], ids[3], ids[2], ids[1], cellId);
this->InsertQuadInHash(ids[0], ids[4], ids[7], ids[3], cellId);
this->InsertQuadInHash(ids[1], ids[2], ids[6], ids[5], cellId);
this->InsertQuadInHash(ids[2], ids[3], ids[7], ids[6], cellId);
this->InsertQuadInHash(ids[4], ids[5], ids[6], ids[7], cellId);
break;
case VTK_VOXEL:
input->GetCellPoints(cellId, numCellPts, ids, pointIdList);
this->InsertQuadInHash(ids[0], ids[1], ids[5], ids[4], cellId);
this->InsertQuadInHash(ids[0], ids[2], ids[3], ids[1], cellId);
this->InsertQuadInHash(ids[0], ids[4], ids[6], ids[2], cellId);
this->InsertQuadInHash(ids[1], ids[3], ids[7], ids[5], cellId);
this->InsertQuadInHash(ids[2], ids[6], ids[7], ids[3], cellId);
this->InsertQuadInHash(ids[4], ids[5], ids[7], ids[6], cellId);
break;
case VTK_TETRA:
input->GetCellPoints(cellId, numCellPts, ids, pointIdList);
this->InsertTriInHash(ids[0], ids[1], ids[3], cellId, 2);
this->InsertTriInHash(ids[0], ids[2], ids[1], cellId, 3);
this->InsertTriInHash(ids[0], ids[3], ids[2], cellId, 1);
this->InsertTriInHash(ids[1], ids[2], ids[3], cellId, 0);
break;
case VTK_PENTAGONAL_PRISM:
input->GetCellPoints(cellId, numCellPts, ids, pointIdList);
this->InsertQuadInHash(ids[0], ids[1], ids[6], ids[5], cellId);
this->InsertQuadInHash(ids[1], ids[2], ids[7], ids[6], cellId);
this->InsertQuadInHash(ids[2], ids[3], ids[8], ids[7], cellId);
this->InsertQuadInHash(ids[3], ids[4], ids[9], ids[8], cellId);
this->InsertQuadInHash(ids[4], ids[0], ids[5], ids[9], cellId);
this->InsertPolygonInHash(ids, 5, cellId);
this->InsertPolygonInHash(&ids[5], 5, cellId);
break;
case VTK_HEXAGONAL_PRISM:
input->GetCellPoints(cellId, numCellPts, ids, pointIdList);
this->InsertQuadInHash(ids[0], ids[1], ids[7], ids[6], cellId);
this->InsertQuadInHash(ids[1], ids[2], ids[8], ids[7], cellId);
this->InsertQuadInHash(ids[2], ids[3], ids[9], ids[8], cellId);
this->InsertQuadInHash(ids[3], ids[4], ids[10], ids[9], cellId);
this->InsertQuadInHash(ids[4], ids[5], ids[11], ids[10], cellId);
this->InsertQuadInHash(ids[5], ids[0], ids[6], ids[11], cellId);
this->InsertPolygonInHash(ids, 6, cellId);
this->InsertPolygonInHash(&ids[6], 6, cellId);
break;
case VTK_PYRAMID:
input->GetCellPoints(cellId, numCellPts, ids, pointIdList);
this->InsertQuadInHash(ids[3], ids[2], ids[1], ids[0], cellId);
this->InsertTriInHash(ids[0], ids[1], ids[4], cellId);
this->InsertTriInHash(ids[1], ids[2], ids[4], cellId);
this->InsertTriInHash(ids[2], ids[3], ids[4], cellId);
this->InsertTriInHash(ids[3], ids[0], ids[4], cellId);
break;
case VTK_WEDGE:
input->GetCellPoints(cellId, numCellPts, ids, pointIdList);
this->InsertQuadInHash(ids[0], ids[2], ids[5], ids[3], cellId);
this->InsertQuadInHash(ids[1], ids[0], ids[3], ids[4], cellId);
this->InsertQuadInHash(ids[2], ids[1], ids[4], ids[5], cellId);
this->InsertTriInHash(ids[0], ids[1], ids[2], cellId);
this->InsertTriInHash(ids[3], ids[5], ids[4], cellId);
break;
case VTK_PIXEL:
case VTK_QUAD:
case VTK_TRIANGLE:
case VTK_POLYGON:
case VTK_TRIANGLE_STRIP:
case VTK_QUADRATIC_TRIANGLE:
case VTK_BIQUADRATIC_TRIANGLE:
case VTK_QUADRATIC_QUAD:
case VTK_QUADRATIC_LINEAR_QUAD:
case VTK_BIQUADRATIC_QUAD:
case VTK_QUADRATIC_POLYGON:
case VTK_LAGRANGE_TRIANGLE:
case VTK_LAGRANGE_QUADRILATERAL:
case VTK_BEZIER_TRIANGLE:
case VTK_BEZIER_QUADRILATERAL:
// save 2D cells for third pass
flag2D = 1;
break;
default:
{
// Default way of getting faces. Differentiates between linear
// and higher order cells.
input->GetCell(cellId, cell);
if (cell->IsLinear())
{
if (cell->GetCellDimension() == 3)
{
int numFaces = cell->GetNumberOfFaces();
for (j = 0; j < numFaces; j++)
{
face = cell->GetFace(j);
numFacePts = face->GetNumberOfPoints();
if (numFacePts == 4)
{
this->InsertQuadInHash(face->PointIds->GetId(0), face->PointIds->GetId(1),
face->PointIds->GetId(2), face->PointIds->GetId(3), cellId);
}
else if (numFacePts == 3)
{
this->InsertTriInHash(face->PointIds->GetId(0), face->PointIds->GetId(1),
face->PointIds->GetId(2), cellId);
}
else
{
this->InsertPolygonInHash(
face->PointIds->GetPointer(0), face->PointIds->GetNumberOfIds(), cellId);
}
} // for all cell faces
} // if 3D
else
{
vtkDebugMacro("Missing cell type.");
}
} // a linear cell type
else // process nonlinear cells via triangulation
{
input->SetCellOrderAndRationalWeights(cellId, cell);
if (cell->GetCellDimension() == 1)
{
cell->TriangulateIds(0, pts);
for (i = 0; i < pts->GetNumberOfIds(); i += 2)
{
newLines->InsertNextCell(2);
inPtId = pts->GetId(i);
this->RecordOrigCellId(this->NumberOfNewCells, cellId);
outputCD->CopyData(cd, cellId, this->NumberOfNewCells++);
outPtId = this->GetOutputPointId(inPtId, input, newPts, outputPD);
newLines->InsertCellPoint(outPtId);
inPtId = pts->GetId(i + 1);
outPtId = this->GetOutputPointId(inPtId, input, newPts, outputPD);
newLines->InsertCellPoint(outPtId);
}
}
else if (cell->GetCellDimension() == 2)
{
vtkWarningMacro(<< "2-D nonlinear cells must be processed with all other 2-D cells.");
}
else // 3D nonlinear cell
{
vtkIdList* cellIds = vtkIdList::New();
int numFaces = cell->GetNumberOfFaces();
for (j = 0; j < numFaces; j++)
{
face = cell->GetFace(j);
input->GetCellNeighbors(cellId, face->PointIds, cellIds);
if (cellIds->GetNumberOfIds() <= 0)
{
// FIXME: Face could not be consistent. vtkOrderedTriangulator is a better option
if (this->NonlinearSubdivisionLevel >= 1)
{
// TODO: Handle NonlinearSubdivisionLevel > 1 correctly.
face->TriangulateIds(0, pts);
for (i = 0; i < pts->GetNumberOfIds(); i += 3)
{
this->InsertTriInHash(
pts->GetId(i), pts->GetId(i + 1), pts->GetId(i + 2), cellId);
}
}
else
{
switch (face->GetCellType())
{
case VTK_QUADRATIC_TRIANGLE:
case VTK_LAGRANGE_TRIANGLE:
case VTK_BEZIER_TRIANGLE:
this->InsertTriInHash(face->PointIds->GetId(0), face->PointIds->GetId(1),
face->PointIds->GetId(2), cellId);
break;
case VTK_QUADRATIC_QUAD:
case VTK_BIQUADRATIC_QUAD:
case VTK_QUADRATIC_LINEAR_QUAD:
case VTK_LAGRANGE_QUADRILATERAL:
case VTK_BEZIER_QUADRILATERAL:
this->InsertQuadInHash(face->PointIds->GetId(0), face->PointIds->GetId(1),
face->PointIds->GetId(2), face->PointIds->GetId(3), cellId);
break;
default:
vtkWarningMacro(<< "Encountered unknown nonlinear face.");
break;
} // switch cell type
} // subdivision level
} // cell has ids
} // for faces
cellIds->Delete();
} // 3d cell
} // nonlinear cell
} // default switch case
} // switch(cellType)
} // for all cells.
// It would be possible to add these (except for polygons with 5+ sides)
// to the hashes. Alternatively, the higher order 2d cells could be handled
// in the following loop.
// Now insert 2DCells. Because of poly datas (cell data) ordering,
// the 2D cells have to come after points and lines.
for (vtkIdType cellId = 0; cellId < numCells && !abort && flag2D; ++cellId)
{
// We skip cells marked as hidden
if (ghostCells &&
(ghostCells->GetValue(cellId) & vtkDataSetAttributes::CellGhostTypes::HIDDENCELL))
{
continue;
}
cellType = input->GetCellType(cellId);
input->GetCellPoints(cellId, numCellPts, ids, pointIdList);
// If we have a quadratic face and our subdivision level is zero, just treat
// it as a linear cell. This should work so long as the first points of the
// quadratic cell correspond to all those of the equivalent linear cell
// (which all the current definitions do).
if (this->NonlinearSubdivisionLevel < 1)
{
switch (cellType)
{
case VTK_QUADRATIC_TRIANGLE:
case VTK_LAGRANGE_TRIANGLE:
case VTK_BEZIER_TRIANGLE:
cellType = VTK_TRIANGLE;
numCellPts = 3;
break;
case VTK_QUADRATIC_QUAD:
case VTK_BIQUADRATIC_QUAD:
case VTK_QUADRATIC_LINEAR_QUAD:
case VTK_LAGRANGE_QUADRILATERAL:
case VTK_BEZIER_QUADRILATERAL:
cellType = VTK_QUAD;
numCellPts = 4;
break;
}
}
// A couple of common cases to see if things go faster.
if (cellType == VTK_PIXEL)
{ // Do we really want to insert the 2D cells into a hash?
pts->Reset();
pts->InsertId(0, this->GetOutputPointId(ids[0], input, newPts, outputPD));
pts->InsertId(1, this->GetOutputPointId(ids[1], input, newPts, outputPD));
pts->InsertId(2, this->GetOutputPointId(ids[3], input, newPts, outputPD));
pts->InsertId(3, this->GetOutputPointId(ids[2], input, newPts, outputPD));
newPolys->InsertNextCell(pts);
this->RecordOrigCellId(this->NumberOfNewCells, cellId);
outputCD->CopyData(cd, cellId, this->NumberOfNewCells++);
}
else if (cellType == VTK_POLYGON || cellType == VTK_TRIANGLE || cellType == VTK_QUAD)
{
pts->Reset();
for (i = 0; i < numCellPts; i++)
{
outPtId = this->GetOutputPointId(ids[i], input, newPts, outputPD);
pts->InsertId(i, outPtId);
}
newPolys->InsertNextCell(pts);
this->RecordOrigCellId(this->NumberOfNewCells, cellId);
outputCD->CopyData(cd, cellId, this->NumberOfNewCells++);
}
else if (cellType == VTK_TRIANGLE_STRIP)
{
// Change strips to triangles so we do not have to worry about order.
int toggle = 0;
vtkIdType ptIds[3];
// This check is not really necessary. It was put here because of another (now fixed) bug.
if (numCellPts > 1)
{
ptIds[0] = this->GetOutputPointId(ids[0], input, newPts, outputPD);
ptIds[1] = this->GetOutputPointId(ids[1], input, newPts, outputPD);
for (i = 2; i < numCellPts; ++i)
{
ptIds[2] = this->GetOutputPointId(ids[i], input, newPts, outputPD);
newPolys->InsertNextCell(3, ptIds);
this->RecordOrigCellId(this->NumberOfNewCells, cellId);
outputCD->CopyData(cd, cellId, this->NumberOfNewCells++);
ptIds[toggle] = ptIds[2];
toggle = !toggle;
}
}
}
else if (cellType == VTK_QUADRATIC_TRIANGLE || cellType == VTK_BIQUADRATIC_TRIANGLE ||
cellType == VTK_QUADRATIC_QUAD || cellType == VTK_BIQUADRATIC_QUAD ||
cellType == VTK_QUADRATIC_LINEAR_QUAD || cellType == VTK_QUADRATIC_POLYGON ||
cellType == VTK_LAGRANGE_TRIANGLE || cellType == VTK_LAGRANGE_QUADRILATERAL ||
cellType == VTK_BEZIER_TRIANGLE || cellType == VTK_BEZIER_QUADRILATERAL)
{
// If one of the points is hidden (meaning invalid), do not
// extract surface cell.
// Removed checking for whether all points are ghost, because that's an
// incorrect assumption.
bool oneHidden = false;
if (ghosts)
{
for (i = 0; i < numCellPts; i++)
{
unsigned char val = ghosts->GetValue(ids[i]);
if (val & vtkDataSetAttributes::HIDDENPOINT)
{
oneHidden = true;
break;
}
}
}
if (oneHidden)
{
continue;
}
// Note: we should not be here if this->NonlinearSubdivisionLevel is less
// than 1. See the check above.
input->GetCell(cellId, cell);
double* pc = cell->GetParametricCoords();
// If the cell is of Bezier type, the weights might be rational and the degree nonuniform.
// This need to be initiated.
input->SetCellOrderAndRationalWeights(cellId, cell);
// Get the triangulation of the first subdivision level.
// Note that the output of TriangulateLocalIds records triangles in pts where each 3 points
// defines a triangle. The returned ids are local ids with respect to the cell.
cell->TriangulateLocalIds(0, pts);
assert(pts->GetNumberOfIds() % 3 == 0);
// Start to fill outPts with the cell points
numFacePts = cell->GetNumberOfPoints();
outPts->Reset();
weights.resize(numFacePts);
// For Bezier cells, the points that are not at the corners are overload to get the
// projection of the non-interpolate points. numFacePtsToCopy is the number of points to be
// copied, and numFacePts - numFacePtsToCopy will be the number of points that are
// interpolated.
vtkIdType numFacePtsToCopy = !AllowInterpolation ||
(cellType != VTK_BEZIER_QUADRILATERAL && cellType != VTK_BEZIER_TRIANGLE)
? numFacePts
: (cellType == VTK_BEZIER_QUADRILATERAL ? 4 : 3);
// Points that are copied:
for (i = 0; i < numFacePtsToCopy; i++)
{
outPts->InsertNextId(this->GetOutputPointId(cell->GetPointId(i), input, newPts, outputPD));
}
// Points that are interpolated (only for Bezier cells when AllowInterpolation is true )
for (i = numFacePtsToCopy; i < numFacePts; i++)
{
outPts->InsertNextId(this->GetOutputPointIdAndInterpolate(
i, input, cell, pc, weights.data(), newPts, outputPD));
}
bool isDegenerateCell = false;
auto isDegeneratedSubTriangle = [&](vtkIdType ii)
{
return outPts->GetId(pts->GetId(ii)) == outPts->GetId(pts->GetId(ii + 1)) ||
outPts->GetId(pts->GetId(ii)) == outPts->GetId(pts->GetId(ii + 2)) ||
outPts->GetId(pts->GetId(ii + 1)) == outPts->GetId(pts->GetId(ii + 2));
};
// Do any further subdivision if necessary.
if (this->NonlinearSubdivisionLevel > 1 && pc)
{
for (i = 0; i < pts->GetNumberOfIds(); i += 3)
{
if (isDegeneratedSubTriangle(i))
{
isDegenerateCell = true;
break;
}
}
vtkIdType maxNumberOfIds =
std::pow(4, this->NonlinearSubdivisionLevel - 1) * pts->GetNumberOfIds();
pts2->Allocate(maxNumberOfIds);
// We are going to need parametric coordinates to further subdivide.
parametricCoords.resize(maxNumberOfIds * 3);
std::copy(&pc[0], &pc[0] + numFacePts * 3, parametricCoords.begin());
// localEdgeMap is similar to this->EdgeMap, but only stores local ids
localEdgeMap->clear();
auto isEqualTo1Or0 = [](double a, double e = 1e-10)
{ return (std::abs(a) <= e) || (std::abs(a - 1) <= e); };
vtkIdType localIdCpt = numFacePts;
vtkIdType pt1, pt2, id;
vtkIdType inPts[6];
// Subdivide these triangles as many more times as necessary. Remember
// that we have already done the first subdivision.
for (j = 1; j < this->NonlinearSubdivisionLevel; j++)
{
pts2->Reset();
if (isDegenerateCell)
{
// For degenerate cells, we can have multiple parametric points linked to the same
// output point. But we need to select a single one. The rule is to give priority to
// the points that are on the contour of the parametric space. This is necessary for
// connecting adjacent cells. The way we give this priority is by calling
// this->EdgeMap->FindEgde/AddEdge for those points first. So a first iteration over pts
// is performed to add those points. During the second iteration (the one not specific
// to degenerate cells), when trying to add a duplicate point, the edge map will return
// the output id of the already existing point.
double coords[3];
for (i = 0; i < pts->GetNumberOfIds(); i += 3)
{
for (k = 0; k < 3; k++)
{
pt1 = pts->GetId(i + k);
pt2 = pts->GetId(i + ((k < 2) ? (k + 1) : 0));
{
coords[0] = 0.5 * (parametricCoords[pt1 * 3] + parametricCoords[pt2 * 3]);
coords[1] = 0.5 * (parametricCoords[pt1 * 3 + 1] + parametricCoords[pt2 * 3 + 1]);
coords[2] = 0.5 * (parametricCoords[pt1 * 3 + 2] + parametricCoords[pt2 * 3 + 2]);
if (isEqualTo1Or0(coords[0]) || isEqualTo1Or0(coords[1]))
{
this->GetInterpolatedPointId(outPts->GetId(pt1), outPts->GetId(pt2), input,
cell, coords, weights.data(), newPts, outputPD);
}
}
}
}
}
// Each triangle will be split into 4 triangles.
for (i = 0; i < pts->GetNumberOfIds(); i += 3)
{
// Hold the input point ids and parametric coordinates. First 3
// indices are the original points. Second three are the midpoints
// in the edges (0,1), (1,2) and (2,0), respectively (see comment
// below).
for (k = 0; k < 3; k++)
{
inPts[k] = pts->GetId(i + k);
pt1 = inPts[k];
pt2 = pts->GetId(i + ((k < 2) ? (k + 1) : 0));
id = localEdgeMap->FindEdge(pt1, pt2);
if (id == -1)
{
id = localIdCpt;
parametricCoords[id * 3] =
0.5 * (parametricCoords[pt1 * 3] + parametricCoords[pt2 * 3]);
parametricCoords[id * 3 + 1] =
0.5 * (parametricCoords[pt1 * 3 + 1] + parametricCoords[pt2 * 3 + 1]);
parametricCoords[id * 3 + 2] =
0.5 * (parametricCoords[pt1 * 3 + 2] + parametricCoords[pt2 * 3 + 2]);
localEdgeMap->AddEdge(pt1, pt2, id);
outPts->InsertNextId(
this->GetInterpolatedPointId(outPts->GetId(pt1), outPts->GetId(pt2), input, cell,
¶metricCoords[id * 3], weights.data(), newPts, outputPD));
localIdCpt++;
}
inPts[k + 3] = id;
}
// * 0
// / \ Use the 6 points recorded
// / \ in inPts and paramCoords
// 3 *-----* 5 to create the 4 triangles
// / \ / \ shown here.
// / \ / \ .
// *-----*-----*
// 1 4 2
static const int subtriangles[12] = { 0, 3, 5, 3, 1, 4, 3, 4, 5, 5, 4, 2 };
for (int subId : subtriangles)
{
pts2->InsertNextId(inPts[subId]);
}
} // Iterate over triangles
// Now that we have recorded the subdivided triangles in pts2 , swap them with pts to
// make them the current ones.
std::swap(pts, pts2);
} // Iterate over subdivision levels
}
for (i = 0; i < pts->GetNumberOfIds(); i += 3)
{
if (isDegenerateCell && isDegeneratedSubTriangle(i))
{
continue; // Do not record the degenerate triangle
}
newPolys->InsertNextCell(3);
newPolys->InsertCellPoint(outPts->GetId(pts->GetId(i)));
newPolys->InsertCellPoint(outPts->GetId(pts->GetId(i + 1)));
newPolys->InsertCellPoint(outPts->GetId(pts->GetId(i + 2)));
this->RecordOrigCellId(this->NumberOfNewCells, cellId);
outputCD->CopyData(cd, cellId, this->NumberOfNewCells++);
}
}
} // for all cells.
// Now transfer geometry from hash to output (only triangles and quads).
this->InitQuadHashTraversal();
while ((q = this->GetNextVisibleQuadFromHash()))
{
// If one of the points is hidden (meaning invalid), do not
// extract surface cell.
// Removed checking for whether all points are ghost, because that's an
// incorrect assumption.
bool oneHidden = false;
// handle all polys
for (i = 0; i < q->numPts; i++)
{
if (ghosts)
{
unsigned char val = ghosts->GetValue(q->ptArray[i]);
if (val & vtkDataSetAttributes::HIDDENPOINT)
{
oneHidden = true;
}
}
q->ptArray[i] = this->GetOutputPointId(q->ptArray[i], input, newPts, outputPD);
}
if (oneHidden)
{
continue;
}
newPolys->InsertNextCell(q->numPts, q->ptArray);
this->RecordOrigCellId(this->NumberOfNewCells, q);
outputCD->CopyData(inputCD, q->SourceId, this->NumberOfNewCells++);
}
if (this->PassThroughCellIds)
{
outputCD->AddArray(this->OriginalCellIds);
}
if (this->PassThroughPointIds)
{
outputPD->AddArray(this->OriginalPointIds);
}
// Update ourselves and release memory
//
cell->Delete();
pts->Delete();
outPts->Delete();
pts2->Delete();
output->SetPoints(newPts);
newPts->Delete();
output->SetPolys(newPolys);
newPolys->Delete();
if (newVerts->GetNumberOfCells() > 0)
{
output->SetVerts(newVerts);
}
newVerts->Delete();
newVerts = nullptr;
if (newLines->GetNumberOfCells() > 0)
{
output->SetLines(newLines);
}
newLines->Delete();
// free storage
output->Squeeze();
if (this->OriginalCellIds != nullptr)
{
this->OriginalCellIds->Delete();
this->OriginalCellIds = nullptr;
}
if (this->OriginalPointIds != nullptr)
{
this->OriginalPointIds->Delete();
this->OriginalPointIds = nullptr;
}
this->DeleteQuadHash();
return 1;
}
//------------------------------------------------------------------------------
void vtkDataSetSurfaceFilter::InitializeQuadHash(vtkIdType numPoints)
{
vtkIdType i;
if (this->QuadHash)
{
this->DeleteQuadHash();
}
// Prepare our special quad allocator (for efficiency).
this->InitFastGeomQuadAllocation(numPoints);
this->QuadHash = new vtkFastGeomQuad*[numPoints];
this->QuadHashLength = numPoints;
this->PointMap = new vtkIdType[numPoints];
for (i = 0; i < numPoints; ++i)
{
this->QuadHash[i] = nullptr;
this->PointMap[i] = -1;
}
this->EdgeMap = new vtkEdgeInterpolationMap;
}
//------------------------------------------------------------------------------
void vtkDataSetSurfaceFilter::DeleteQuadHash()
{
vtkIdType i;
this->DeleteAllFastGeomQuads();
for (i = 0; i < this->QuadHashLength; ++i)
{
this->QuadHash[i] = nullptr;
}
delete[] this->QuadHash;
this->QuadHash = nullptr;
this->QuadHashLength = 0;
delete[] this->PointMap;
this->PointMap = nullptr;
delete this->EdgeMap;
this->EdgeMap = nullptr;
}
//------------------------------------------------------------------------------
void vtkDataSetSurfaceFilter::InsertQuadInHash(
vtkIdType a, vtkIdType b, vtkIdType c, vtkIdType d, vtkIdType sourceId)
{
vtkIdType tmp;
vtkFastGeomQuad *quad, **end;
// Reorder to get smallest id in a.
if (b < a && b < c && b < d)
{
tmp = a;
a = b;
b = c;
c = d;
d = tmp;
}
else if (c < a && c < b && c < d)
{
tmp = a;
a = c;
c = tmp;
tmp = b;
b = d;
d = tmp;
}
else if (d < a && d < b && d < c)
{
tmp = a;
a = d;
d = c;
c = b;
b = tmp;
}
// Look for existing quad in the hash;
end = this->QuadHash + a;
quad = *end;
while (quad)
{
end = &(quad->Next);
// a has to match in this bin.
// c should be independent of point order.
if (quad->numPts == 4 && c == quad->ptArray[2])
{
// Check both orders for b and d.
if ((b == quad->ptArray[1] && d == quad->ptArray[3]) ||
(b == quad->ptArray[3] && d == quad->ptArray[1]))
{
// We have a match.
quad->SourceId = -1;
// That is all we need to do. Hide any quad shared by two or more cells.
return;
}
}
quad = *end;
}
// Create a new quad and add it to the hash.
quad = this->NewFastGeomQuad(4);
quad->Next = nullptr;
quad->SourceId = sourceId;
quad->ptArray[0] = a;
quad->ptArray[1] = b;
quad->ptArray[2] = c;
quad->ptArray[3] = d;
*end = quad;
}
//------------------------------------------------------------------------------
void vtkDataSetSurfaceFilter::InsertTriInHash(
vtkIdType a, vtkIdType b, vtkIdType c, vtkIdType sourceId, vtkIdType vtkNotUsed(faceId) /*= -1*/)
{
vtkIdType tmp;
vtkFastGeomQuad *quad, **end;
// Reorder to get smallest id in a.
if (b < a && b < c)
{
tmp = a;
a = b;
b = c;
c = tmp;
}
else if (c < a && c < b)
{
tmp = a;
a = c;
c = b;
b = tmp;
}
// We can't put the second smallest in b because it might change the order
// of the vertices in the final triangle.
// Look for existing tri in the hash;
end = this->QuadHash + a;
quad = *end;
while (quad)
{
end = &(quad->Next);
// a has to match in this bin.
if (quad->numPts == 3)
{
if ((b == quad->ptArray[1] && c == quad->ptArray[2]) ||
(b == quad->ptArray[2] && c == quad->ptArray[1]))
{
// We have a match.
quad->SourceId = -1;
// That is all we need to do. Hide any tri shared by two or more cells.
return;
}
}
quad = *end;
}
// Create a new quad and add it to the hash.
quad = this->NewFastGeomQuad(3);
quad->Next = nullptr;
quad->SourceId = sourceId;
quad->ptArray[0] = a;
quad->ptArray[1] = b;
quad->ptArray[2] = c;
*end = quad;
}
// Insert a polygon into the hash.
// Input: an array of vertex ids
// the start index of the polygon in the array
// the end index of the polygon in the array
// the cellId of the polygon
//------------------------------------------------------------------------------
void vtkDataSetSurfaceFilter::InsertPolygonInHash(
const vtkIdType* ids, int numPts, vtkIdType sourceId)
{
// sanity check
if (numPts == 0)
{
return;
}
vtkFastGeomQuad *quad, **end;
// find the index to the smallest id
vtkIdType offset = 0;
for (int i = 0; i < numPts; i++)
{
if (ids[i] < ids[offset])
{
offset = i;
}
}
// copy ids into ordered array with smallest id first
vtkIdType* tab = new vtkIdType[numPts];
for (int i = 0; i < numPts; i++)
{
tab[i] = ids[(offset + i) % numPts];
}
// Look for existing hex in the hash;
end = this->QuadHash + tab[0];
quad = *end;
while (quad)
{
end = &(quad->Next);
// a has to match in this bin.
// first just check the polygon size.
bool match = true;
if (numPts == quad->numPts)
{
if (tab[0] == quad->ptArray[0])
{
// if the first two points match loop through forwards
// checking all points
if (numPts > 1 && tab[1] == quad->ptArray[1])
{
for (int i = 2; i < numPts; ++i)
{
if (tab[i] != quad->ptArray[i])
{
match = false;
break;
}
}
}
else
{
// check if the points go in the opposite direction
for (int i = 1; i < numPts; ++i)
{
if (tab[numPts - i] != quad->ptArray[i])
{
match = false;
break;
}
}
}
}
else
{
match = false;
}
}
else
{
match = false;
}
if (match)
{
// We have a match.
quad->SourceId = -1;
// That is all we need to do. Hide any tri shared by two or more cells.
delete[] tab;
return;
}
quad = *end;
}
// Create a new quad and add it to the hash.
quad = this->NewFastGeomQuad(numPts);
// mark the structure as a polygon
quad->Next = nullptr;
quad->SourceId = sourceId;
for (int i = 0; i < numPts; i++)
{
quad->ptArray[i] = tab[i];
}
*end = quad;
delete[] tab;
}
//------------------------------------------------------------------------------
void vtkDataSetSurfaceFilter::InitFastGeomQuadAllocation(vtkIdType numberOfCells)
{
int idx;
this->DeleteAllFastGeomQuads();
// Allocate 100 pointers to arrays.
// This should be plenty (unless we have triangle strips) ...
this->NumberOfFastGeomQuadArrays = 100;
this->FastGeomQuadArrays = new unsigned char*[this->NumberOfFastGeomQuadArrays];
// Initialize all to nullptr;
for (idx = 0; idx < this->NumberOfFastGeomQuadArrays; ++idx)
{
this->FastGeomQuadArrays[idx] = nullptr;
}
// Set pointer to the beginning.
this->NextArrayIndex = 0;
this->NextQuadIndex = 0;
// size the chunks based on the size of a quadrilateral
int quadSize = sizeofFastQuad(4);
// Lets keep the chunk size relatively small.
if (numberOfCells < 100)
{
this->FastGeomQuadArrayLength = 50 * quadSize;
}
else
{
this->FastGeomQuadArrayLength = (numberOfCells / 2) * quadSize;
}
}
//------------------------------------------------------------------------------
void vtkDataSetSurfaceFilter::DeleteAllFastGeomQuads()
{
for (int idx = 0; idx < this->NumberOfFastGeomQuadArrays; ++idx)
{
delete[] this->FastGeomQuadArrays[idx];
this->FastGeomQuadArrays[idx] = nullptr;
}
delete[] this->FastGeomQuadArrays;
this->FastGeomQuadArrays = nullptr;
this->FastGeomQuadArrayLength = 0;
this->NumberOfFastGeomQuadArrays = 0;
this->NextArrayIndex = 0;
this->NextQuadIndex = 0;
}
//------------------------------------------------------------------------------
vtkFastGeomQuad* vtkDataSetSurfaceFilter::NewFastGeomQuad(int numPts)
{
if (this->FastGeomQuadArrayLength == 0)
{
vtkErrorMacro("Face hash allocation has not been initialized.");
return nullptr;
}
// see if there's room for this one
int polySize = sizeofFastQuad(numPts);
if (this->NextQuadIndex + polySize > this->FastGeomQuadArrayLength)
{
++(this->NextArrayIndex);
this->NextQuadIndex = 0;
}
// Although this should not happen often, check first.
if (this->NextArrayIndex >= this->NumberOfFastGeomQuadArrays)
{
int idx, num;
unsigned char** newArrays;
num = this->NumberOfFastGeomQuadArrays * 2;
newArrays = new unsigned char*[num];
for (idx = 0; idx < num; ++idx)
{
newArrays[idx] = nullptr;
if (idx < this->NumberOfFastGeomQuadArrays)
{
newArrays[idx] = this->FastGeomQuadArrays[idx];
}
}
delete[] this->FastGeomQuadArrays;
this->FastGeomQuadArrays = newArrays;
this->NumberOfFastGeomQuadArrays = num;
}
// Next: allocate a new array if necessary.
if (this->FastGeomQuadArrays[this->NextArrayIndex] == nullptr)
{
this->FastGeomQuadArrays[this->NextArrayIndex] =
new unsigned char[this->FastGeomQuadArrayLength];
}
vtkFastGeomQuad* q = reinterpret_cast<vtkFastGeomQuad*>(
this->FastGeomQuadArrays[this->NextArrayIndex] + this->NextQuadIndex);
q->numPts = numPts;
q->ptArray = (vtkIdType*)q + FSizeDivSizeId;
this->NextQuadIndex += polySize;
return q;
}
//------------------------------------------------------------------------------
void vtkDataSetSurfaceFilter::InitQuadHashTraversal()
{
this->QuadHashTraversalIndex = 0;
if (this->QuadHashLength == 0)
{
this->QuadHashTraversal = nullptr;
}
else
{
this->QuadHashTraversal = this->QuadHash[0];
}
}
//------------------------------------------------------------------------------
vtkFastGeomQuad* vtkDataSetSurfaceFilter::GetNextVisibleQuadFromHash()
{
vtkFastGeomQuad* quad;
quad = this->QuadHashTraversal;
// Move till traversal until we have a quad to return.
// Note: the current traversal has not been returned yet.
while (quad == nullptr || quad->SourceId == -1)
{
if (quad)
{ // The quad must be hidden. Move to the next.
quad = quad->Next;
}
else
{ // must be the end of the linked list. Move to the next bin.
this->QuadHashTraversalIndex += 1;
if (this->QuadHashTraversalIndex >= this->QuadHashLength)
{ // There are no more bins.
this->QuadHashTraversal = nullptr;
return nullptr;
}
quad = this->QuadHash[this->QuadHashTraversalIndex];
}
}
// Now we have a quad to return. Set the traversal to the next entry.
this->QuadHashTraversal = quad->Next;
return quad;
}
//------------------------------------------------------------------------------
vtkIdType vtkDataSetSurfaceFilter::GetOutputPointId(
vtkIdType inPtId, vtkDataSet* input, vtkPoints* outPts, vtkPointData* outPD)
{
vtkIdType outPtId;
outPtId = this->PointMap[inPtId];
if (outPtId == -1)
{
outPtId = outPts->InsertNextPoint(input->GetPoint(inPtId));
outPD->CopyData(input->GetPointData(), inPtId, outPtId);
this->PointMap[inPtId] = outPtId;
this->RecordOrigPointId(outPtId, inPtId);
}
return outPtId;
}
//------------------------------------------------------------------------------
vtkIdType vtkDataSetSurfaceFilter::GetOutputPointIdAndInterpolate(vtkIdType cellPtId,
vtkDataSet* input, vtkCell* cell, double* pc, double* weights, vtkPoints* outPts,
vtkPointData* outPD)
{
vtkIdType outPtId;
vtkIdType inPtId = cell->GetPointId(cellPtId);
outPtId = this->PointMap[inPtId];
if (outPtId == -1)
{
int subId = -1;
double wcoords[3];
cell->EvaluateLocation(subId, pc + 3 * cellPtId, wcoords, weights);
outPtId = outPts->InsertNextPoint(wcoords);
outPD->InterpolatePoint(input->GetPointData(), outPtId, cell->GetPointIds(), weights);
this->PointMap[inPtId] = outPtId;
this->RecordOrigPointId(outPtId, inPtId);
}
return outPtId;
}
//------------------------------------------------------------------------------
vtkIdType vtkDataSetSurfaceFilter::GetInterpolatedPointId(vtkIdType edgePtA, vtkIdType edgePtB,
vtkDataSet* input, vtkCell* cell, double* pcoords, double* weights, vtkPoints* outPts,
vtkPointData* outPD)
{
vtkIdType outPtId = this->EdgeMap->FindEdge(edgePtA, edgePtB);
if (outPtId == -1)
{
int subId = -1;
double wcoords[3];
cell->EvaluateLocation(subId, pcoords, wcoords, weights);
outPtId = outPts->InsertNextPoint(wcoords);
outPD->InterpolatePoint(input->GetPointData(), outPtId, cell->GetPointIds(), weights);
this->RecordOrigPointId(outPtId, -1);
this->EdgeMap->AddEdge(edgePtA, edgePtB, outPtId);
}
return outPtId;
}
vtkIdType vtkDataSetSurfaceFilter::GetInterpolatedPointId(vtkDataSet* input, vtkCell* cell,
double pcoords[3], double* weights, vtkPoints* outPts, vtkPointData* outPD)
{
int subId = -1;
double wcoords[3];
cell->EvaluateLocation(subId, pcoords, wcoords, weights);
vtkIdType outPtId = outPts->InsertNextPoint(wcoords);
outPD->InterpolatePoint(input->GetPointData(), outPtId, cell->GetPointIds(), weights);
this->RecordOrigPointId(outPtId, -1);
return outPtId;
}
//------------------------------------------------------------------------------
void vtkDataSetSurfaceFilter::RecordOrigCellId(vtkIdType destIndex, vtkIdType originalId)
{
if (this->OriginalCellIds != nullptr)
{
this->OriginalCellIds->InsertValue(destIndex, originalId);
}
}
//------------------------------------------------------------------------------
void vtkDataSetSurfaceFilter::RecordOrigCellId(vtkIdType destIndex, vtkFastGeomQuad* quad)
{
if (this->OriginalCellIds != nullptr)
{
this->OriginalCellIds->InsertValue(destIndex, quad->SourceId);
}
}
//------------------------------------------------------------------------------
void vtkDataSetSurfaceFilter::RecordOrigPointId(vtkIdType destIndex, vtkIdType originalId)
{
if (this->OriginalPointIds != nullptr)
{
this->OriginalPointIds->InsertValue(destIndex, originalId);
}
}
VTK_ABI_NAMESPACE_END
|