1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830
|
"""Sub-classes and wrappers for :vtk:`vtkPointSet`."""
from __future__ import annotations
from collections.abc import Iterable
from collections.abc import Sequence
import contextlib
from functools import cached_property
from functools import wraps
import numbers
from pathlib import Path
from textwrap import dedent
from typing import TYPE_CHECKING
from typing import ClassVar
from typing import Union
from typing import cast
import warnings
import numpy as np
import pyvista
from pyvista._deprecate_positional_args import _deprecate_positional_args
from . import _vtk_core as _vtk
from .cell import CellArray
from .cell import _get_connectivity_array
from .cell import _get_irregular_cells
from .cell import _get_offset_array
from .cell import _get_regular_cells
from .celltype import CellType
from .dataset import DataSet
from .errors import CellSizeError
from .errors import PointSetCellOperationError
from .errors import PointSetDimensionReductionError
from .errors import PointSetNotSupported
from .errors import PyVistaDeprecationWarning
from .errors import VTKVersionError
from .filters import PolyDataFilters
from .filters import StructuredGridFilters
from .filters import UnstructuredGridFilters
from .filters import _get_output
from .utilities.arrays import convert_array
from .utilities.cells import create_mixed_cells
from .utilities.cells import get_mixed_cells
from .utilities.cells import numpy_to_idarr
from .utilities.fileio import get_ext
from .utilities.misc import abstract_class
from .utilities.points import vtk_points
if TYPE_CHECKING:
from typing_extensions import Self
from ._typing_core import ArrayLike
from ._typing_core import BoundsTuple
from ._typing_core import CellArrayLike
from ._typing_core import MatrixLike
from ._typing_core import NumpyArray
from ._typing_core import VectorLike
_PolyDataWriterAlias = Union[
_vtk.vtkPLYWriter,
_vtk.vtkXMLPolyDataWriter,
_vtk.vtkSTLWriter,
_vtk.vtkPolyDataWriter,
_vtk.vtkHoudiniPolyDataWriter,
_vtk.vtkOBJWriter,
_vtk.vtkIVWriter,
_vtk.vtkHDFWriter,
]
_UnstructuredGridWriterAlias = Union[
_vtk.vtkXMLUnstructuredGridWriter, _vtk.vtkUnstructuredGridWriter, _vtk.vtkHDFWriter
]
DEFAULT_INPLACE_WARNING = (
'You did not specify a value for `inplace` and the default value will '
'be changing to `False` in future versions for point-based meshes (e.g., '
'`PolyData`). Please make sure you are not assuming this to be an inplace '
'operation.'
)
@abstract_class
class _PointSet(DataSet):
"""PyVista's equivalent of :vtk:`vtkPointSet`.
This holds methods common to PolyData and UnstructuredGrid.
"""
_WRITERS: ClassVar[dict[str, type[_vtk.vtkSimplePointsWriter]]] = { # type: ignore[assignment]
'.xyz': _vtk.vtkSimplePointsWriter,
}
@_deprecate_positional_args
def center_of_mass(self, scalars_weight: bool = False) -> NumpyArray[float]: # noqa: FBT001, FBT002
"""Return the coordinates for the center of mass of the mesh.
Parameters
----------
scalars_weight : bool, default: False
Flag for using the mesh scalars as weights.
Returns
-------
numpy.ndarray
Coordinates for the center of mass.
Examples
--------
>>> import pyvista as pv
>>> mesh = pv.Sphere(center=(1, 1, 1))
>>> mesh.center_of_mass()
array([1., 1., 1.])
"""
alg = _vtk.vtkCenterOfMass()
alg.SetInputDataObject(self)
alg.SetUseScalarsAsWeights(scalars_weight)
alg.Update()
return np.array(alg.GetCenter())
def shallow_copy(self, to_copy: DataSet) -> None: # type: ignore[override]
"""Create a shallow copy from a different dataset into this one.
This method mutates this dataset and returns ``None``.
Parameters
----------
to_copy : pyvista.DataSet
Data object to perform the shallow copy from.
"""
# Set default points if needed
if not to_copy.GetPoints():
to_copy.SetPoints(_vtk.vtkPoints())
DataSet.shallow_copy(self, cast('_vtk.vtkDataObject', to_copy))
@_deprecate_positional_args(allowed=['ind'])
def remove_cells(
self,
ind: VectorLike[bool] | VectorLike[int],
inplace: bool = False, # noqa: FBT001, FBT002
) -> _PointSet:
"""Remove cells.
Parameters
----------
ind : VectorLike[int] | VectorLike[bool]
Cell indices to be removed. The array can also be a
boolean array of the same size as the number of cells.
inplace : bool, default: False
Whether to update the mesh in-place.
Returns
-------
pyvista.DataSet
Same type as the input, but with the specified cells
removed.
See Also
--------
:ref:`ghost_cells_example`
Examples
--------
Remove 20 cells from an unstructured grid.
>>> from pyvista import examples
>>> import pyvista as pv
>>> hex_mesh = pv.read(examples.hexbeamfile)
>>> removed = hex_mesh.remove_cells(range(10, 20))
>>> removed.plot(color='lightblue', show_edges=True, line_width=3)
"""
if isinstance(ind, np.ndarray):
if ind.dtype == np.bool_ and ind.size != self.n_cells:
msg = f'Boolean array size must match the number of cells ({self.n_cells})'
raise ValueError(msg)
ghost_cells = np.zeros(self.n_cells, np.uint8)
ghost_cells[ind] = _vtk.vtkDataSetAttributes.DUPLICATECELL
target = self if inplace else self.copy()
target.cell_data[_vtk.vtkDataSetAttributes.GhostArrayName()] = ghost_cells
target.RemoveGhostCells()
return target
def points_to_double(self) -> _PointSet:
"""Convert the points datatype to double precision.
Returns
-------
pyvista.PointSet
Pointset with points in double precision.
Notes
-----
This operates in place.
Examples
--------
Create a mesh that has points of the type ``float32`` and
convert the points to ``float64``.
>>> import pyvista as pv
>>> mesh = pv.Sphere()
>>> mesh.points.dtype
dtype('float32')
>>> _ = mesh.points_to_double()
>>> mesh.points.dtype
dtype('float64')
"""
if self.points.dtype != np.double:
self.points = self.points.astype(np.double)
return self
# todo: `transform_all_input_vectors` is not handled when modifying inplace
@_deprecate_positional_args(allowed=['xyz'])
def translate(
self: Self,
xyz: VectorLike[float],
transform_all_input_vectors: bool = False, # noqa: FBT001, FBT002
inplace: bool = False, # noqa: FBT001, FBT002
):
"""Translate the mesh.
Parameters
----------
xyz : VectorLike[float]
A vector of three floats of cartesian values to translate the mesh with.
transform_all_input_vectors : bool, default: False
When ``True``, all input vectors are transformed. Otherwise, only
the points, normals and active vectors are transformed. This is
only valid when not updating in place.
inplace : bool, default: False
Updates mesh in-place.
Returns
-------
pyvista.PointSet
Translated pointset.
Examples
--------
Create a sphere and translate it by ``(2, 1, 2)``.
>>> import pyvista as pv
>>> mesh = pv.Sphere()
>>> mesh.center
(0.0, 0.0, 0.0)
>>> trans = mesh.translate((2, 1, 2), inplace=True)
>>> trans.center
(2.0, 1.0, 2.0)
"""
if inplace:
self.points += np.asarray(xyz)
return self
return pyvista.DataObjectFilters.translate(
self,
xyz,
transform_all_input_vectors=transform_all_input_vectors,
inplace=inplace,
)
class PointSet(_PointSet, _vtk.vtkPointSet):
"""Concrete class for storing a set of points.
This is a concrete class representing a set of points that specifies the
interface for datasets that explicitly use "point" arrays to represent
geometry. This class is useful for improving the performance of filters on
point clouds, but not plotting.
For further details see :vtk:`vtkPointSet`.
Parameters
----------
var_inp : :vtk:`vtkPointSet`, MatrixLike[float], optional
Flexible input type. Can be a :vtk:`vtkPointSet`, in which case
this PointSet object will be copied if ``deep=True`` and will
be a shallow copy if ``deep=False``.
List, numpy array, or sequence containing point locations. Must be an
``(N, 3)`` array of points.
deep : bool, default: False
Whether to copy the input ``points``, or to create a PointSet from them
without copying them. Setting ``deep=True`` ensures that the original
arrays can be modified outside the mesh without affecting the
mesh.
force_float : bool, default: True
Casts the datatype to ``float32`` if points datatype is non-float. Set
this to ``False`` to allow non-float types, though this may lead to
truncation of intermediate floats when transforming datasets.
Notes
-----
This class requires ``vtk>=9.1.0``. This is an abstract class in
``vtk<9.1.0`` and cannot be instantiated.
Examples
--------
Create a simple point cloud of 10 points from a numpy array.
>>> import numpy as np
>>> import pyvista as pv
>>> rng = np.random.default_rng(seed=0)
>>> points = rng.random((10, 3))
>>> pset = pv.PointSet(points)
Plot the pointset. Note: this casts to a :class:`pyvista.PolyData`
internally when plotting.
>>> pset.plot(point_size=10)
"""
def __new__(cls, *args, **kwargs):
"""Construct a new PointSet object.
Wrapping this is necessary for us to show an informative error
message when the VTK version is too old, causing PointSet to be
an abstract class. Since we inherit the ``__new__()`` method of
:vtk:`vtkPointSet`, we would otherwise see a generic error about
the class being abstract.
"""
if pyvista.vtk_version_info < (9, 1, 0):
msg = 'pyvista.PointSet requires VTK >= 9.1.0'
raise VTKVersionError(msg)
return super().__new__(cls, *args, **kwargs)
@_deprecate_positional_args(allowed=['var_inp'])
def __init__(self, var_inp=None, deep: bool = False, force_float: bool = True) -> None: # noqa: FBT001, FBT002
"""Initialize the pointset."""
super().__init__()
if var_inp is None:
return
elif isinstance(var_inp, _vtk.vtkPointSet):
if deep:
self.deep_copy(var_inp)
else:
self.shallow_copy(var_inp) # type: ignore[arg-type]
else:
self.SetPoints(vtk_points(var_inp, deep=deep, force_float=force_float))
def __repr__(self):
"""Return the standard representation."""
return DataSet.__repr__(self)
def __str__(self):
"""Return the standard str representation."""
return DataSet.__str__(self)
@_deprecate_positional_args
def cast_to_polydata(self, deep: bool = True): # noqa: FBT001, FBT002
"""Cast this dataset to polydata.
Parameters
----------
deep : bool, deep: True
Whether to copy the pointset points, or to create a PolyData
without copying them. Setting ``deep=True`` ensures that the
original arrays can be modified outside the PolyData without
affecting the PolyData.
Returns
-------
pyvista.PolyData
PointSet cast to a ``pyvista.PolyData``.
"""
pdata = PolyData(self.points, deep=deep)
if deep:
pdata.point_data.update(self.point_data) # update performs deep copy
else:
for key, value in self.point_data.items():
pdata.point_data[key] = value
return pdata
def cast_to_unstructured_grid(self) -> pyvista.UnstructuredGrid:
"""Cast this dataset to :class:`pyvista.UnstructuredGrid`.
A deep copy of the points and point data is made.
Returns
-------
pyvista.UnstructuredGrid
Dataset cast to a :class:`pyvista.UnstructuredGrid`.
Examples
--------
Cast a :class:`pyvista.PointSet` to a
:class:`pyvista.UnstructuredGrid`.
>>> import pyvista as pv
>>> from pyvista import examples
>>> mesh = examples.download_cloud_dark_matter()
>>> type(mesh)
<class 'pyvista.core.pointset.PointSet'>
>>> grid = mesh.cast_to_unstructured_grid()
>>> type(grid)
<class 'pyvista.core.pointset.UnstructuredGrid'>
"""
return self.cast_to_polydata(deep=False).cast_to_unstructured_grid()
@wraps(DataSet.plot) # type: ignore[has-type]
def plot(self, *args, **kwargs): # type: ignore[override] # numpydoc ignore=RT01
"""Cast to PolyData and plot."""
pdata = self.cast_to_polydata(deep=False)
kwargs.setdefault('style', 'points')
return pdata.plot(*args, **kwargs)
@wraps(PolyDataFilters.threshold) # type: ignore[has-type]
def threshold(self, *args, **kwargs): # type: ignore[override] # numpydoc ignore=RT01
"""Cast to PolyData and threshold.
Need this because cell-wise operations fail for PointSets.
"""
return self.cast_to_polydata(deep=False).threshold(*args, **kwargs).cast_to_pointset()
@wraps(PolyDataFilters.threshold_percent) # type:ignore[has-type]
def threshold_percent(self, *args, **kwargs): # type: ignore[override] # numpydoc ignore=RT01
"""Cast to PolyData and threshold.
Need this because cell-wise operations fail for PointSets.
"""
return (
self.cast_to_polydata(deep=False).threshold_percent(*args, **kwargs).cast_to_pointset()
)
@wraps(PolyDataFilters.explode)
def explode(self, *args, **kwargs): # type: ignore[override] # numpydoc ignore=RT01
"""Cast to PolyData and explode.
The explode filter relies on cells.
"""
return self.cast_to_polydata(deep=False).explode(*args, **kwargs).cast_to_pointset()
@wraps(PolyDataFilters.delaunay_3d) # type: ignore[has-type]
def delaunay_3d(self, *args, **kwargs): # type: ignore[override] # numpydoc ignore=RT01
"""Cast to PolyData and run delaunay_3d."""
return self.cast_to_polydata(deep=False).delaunay_3d(*args, **kwargs)
@property
def area(self) -> float: # numpydoc ignore=RT01
"""Return 0.0 since a PointSet has no area."""
return 0.0
@property
def volume(self) -> float: # numpydoc ignore=RT01
"""Return 0.0 since a PointSet has no volume."""
return 0.0
def contour(self, *args, **kwargs): # noqa: ARG002
"""Raise dimension reducing operations are not supported."""
msg = 'Contour and other dimension reducing filters are not supported on PointSets'
raise PointSetNotSupported(msg)
def cell_data_to_point_data(self, *args, **kwargs): # noqa: ARG002
"""Raise PointSets do not have cells."""
msg = 'PointSets contain no cells or cell data.'
raise PointSetNotSupported(msg)
def point_data_to_cell_data(self, *args, **kwargs): # noqa: ARG002
"""Raise PointSets do not have cells."""
msg = 'PointSets contain no cells or cell data.'
raise PointSetNotSupported(msg)
def triangulate(self, *args, **kwargs): # noqa: ARG002
"""Raise cell operations are not supported."""
raise PointSetCellOperationError
def decimate_boundary(self, *args, **kwargs): # noqa: ARG002
"""Raise cell operations are not supported."""
raise PointSetCellOperationError
def find_cells_along_line(self, *args, **kwargs): # noqa: ARG002
"""Raise cell operations are not supported."""
raise PointSetCellOperationError
def tessellate(self, *args, **kwargs): # noqa: ARG002
"""Raise cell operations are not supported."""
raise PointSetCellOperationError
def slice(self, *args, **kwargs): # noqa: ARG002
"""Raise dimension reducing operations are not supported."""
raise PointSetDimensionReductionError
def slice_along_axis(self, *args, **kwargs): # noqa: ARG002
"""Raise dimension reducing operations are not supported."""
raise PointSetDimensionReductionError
def slice_along_line(self, *args, **kwargs): # noqa: ARG002
"""Raise dimension reducing operations are not supported."""
raise PointSetDimensionReductionError
def slice_implicit(self, *args, **kwargs): # noqa: ARG002
"""Raise dimension reducing operations are not supported."""
raise PointSetDimensionReductionError
def slice_orthogonal(self, *args, **kwargs): # noqa: ARG002
"""Raise dimension reducing operations are not supported."""
raise PointSetDimensionReductionError
def shrink(self, *args, **kwargs): # noqa: ARG002
"""Raise cell operations are not supported."""
raise PointSetCellOperationError
def separate_cells(self, *args, **kwargs): # noqa: ARG002
"""Raise cell operations are not supported."""
raise PointSetCellOperationError
def remove_cells(self, *args, **kwargs): # noqa: ARG002
"""Raise cell operations are not supported."""
raise PointSetCellOperationError
def point_is_inside_cell(self, *args, **kwargs): # noqa: ARG002
"""Raise cell operations are not supported."""
raise PointSetCellOperationError
def extract_surface(self, *args, **kwargs): # noqa: ARG002
"""Raise extract surface are not supported."""
raise PointSetCellOperationError
def extract_geometry(self, *args, **kwargs): # noqa: ARG002
"""Raise extract geometry are not supported."""
raise PointSetCellOperationError
class PolyData(_PointSet, PolyDataFilters, _vtk.vtkPolyData):
"""Dataset consisting of surface geometry (e.g. vertices, lines, and polygons).
Can be initialized in several ways:
- Create an empty mesh
- Initialize from a :vtk:`vtkPolyData`
- Using vertices
- Using vertices and faces
- From a file
.. deprecated:: 0.44.0
The parameters ``n_faces``, ``n_lines``, ``n_strips``, and
``n_verts`` are deprecated and no longer used. They were
previously used to speed up the construction of the corresponding
cell arrays but no longer provide any benefit.
Parameters
----------
var_inp : :vtk:`vtkPolyData`, str, sequence, optional
Flexible input type. Can be a :vtk:`vtkPolyData`, in which case
this PolyData object will be copied if ``deep=True`` and will
be a shallow copy if ``deep=False``.
Also accepts a path, which may be local path as in
``'my_mesh.stl'`` or global path like ``'/tmp/my_mesh.ply'``
or ``'C:/Users/user/my_mesh.ply'``.
Otherwise, this must be a points array or list containing one
or more points. Each point must have 3 dimensions. If
``faces``, ``lines``, ``strips``, and ``verts`` are all
``None``, then the ``PolyData`` object will be created with
vertex cells with ``n_verts`` equal to the number of ``points``.
faces : sequence[int], :vtk:`vtkCellArray`, CellArray, optional
Polygonal faces of the mesh. Can be either a padded connectivity
array or an explicit cell array object.
In the padded array format, faces must contain padding
indicating the number of points in the face. For example, the
two faces ``[10, 11, 12]`` and ``[20, 21, 22, 23]`` will be
represented as ``[3, 10, 11, 12, 4, 20, 21, 22, 23]``. This
lets you have an arbitrary number of points per face.
When not including the face connectivity array, each point
will be assigned to a single vertex. This is used for point
clouds that have no connectivity.
n_faces : int, optional
Deprecated. Not used.
lines : sequence[int], :vtk:`vtkCellArray`, CellArray, optional
Line connectivity. Like ``faces``, this can be either a padded
connectivity array or an explicit cell array object. The padded
array format requires padding indicating the number of points in
a line segment. For example, the two line segments ``[0, 1]``
and ``[1, 2, 3, 4]`` will be represented as
``[2, 0, 1, 4, 1, 2, 3, 4]``.
n_lines : int, optional
Deprecated. Not used.
strips : sequence[int], :vtk:`vtkCellArray`, CellArray, optional
Triangle strips connectivity. Triangle strips require an
initial triangle, and the following points of the strip. Each
triangle is built with the new point and the two previous
points.
Just as in ``lines`` and ``faces``, this connectivity can be
specified as either a padded array or an explicit cell array
object. The padded array requires a padding indicating the
number of points. For example, a single triangle strip of the 10
point indices ``[0, 1, 2, 3, 6, 7, 4, 5, 0, 1]`` requires
padding of ``10`` and should be input as
``[10, 0, 1, 2, 3, 6, 7, 4, 5, 0, 1]``.
n_strips : int, optional
Deprecated. Not used.
deep : bool, optional
Whether to copy the inputs, or to create a mesh from them
without copying them. Setting ``deep=True`` ensures that the
original arrays can be modified outside the mesh without
affecting the mesh. Default is ``False``.
force_ext : str, optional
If initializing from a file, force the reader to treat the
file as if it had this extension as opposed to the one in the
file.
force_float : bool, optional
Casts the datatype to ``float32`` if points datatype is
non-float. Default ``True``. Set this to ``False`` to allow
non-float types, though this may lead to truncation of
intermediate floats when transforming datasets.
verts : sequence[int], :vtk:`vtkCellArray`, CellArray, optional
The verts connectivity. Like ``faces``, ``lines``, and
``strips`` this can be supplied as either a padded array or an
explicit cell array object. In the padded array format,
the padding indicates the number of vertices in each cell. For
example, ``[1, 0, 1, 1, 1, 2]`` indicates three vertex cells
each with one point, and ``[2, 0, 1, 2, 2, 3]`` indicates two
polyvertex cells each with two points.
n_verts : int, optional
Deprecated. Not used.
See Also
--------
pyvista.PolyData.from_regular_faces
pyvista.PolyData.from_irregular_faces
Examples
--------
>>> import vtk
>>> import numpy as np
>>> from pyvista import examples
>>> import pyvista as pv
Seed random number generator for reproducible plots
>>> rng = np.random.default_rng(seed=0)
Create an empty mesh.
>>> mesh = pv.PolyData()
Initialize from a :vtk:`vtkPolyData` object.
>>> vtkobj = vtk.vtkPolyData()
>>> mesh = pv.PolyData(vtkobj)
Initialize from just points, creating vertices
>>> points = np.array([[0, 0, 0], [1, 0, 0], [1, 0.5, 0], [0, 0.5, 0]])
>>> mesh = pv.PolyData(points)
Initialize from points and faces, creating polygonal faces.
>>> faces = np.hstack([[3, 0, 1, 2], [3, 0, 3, 2]])
>>> mesh = pv.PolyData(points, faces)
Initialize from points and lines.
>>> lines = np.hstack([[2, 0, 1], [2, 1, 2]])
>>> mesh = pv.PolyData(points, lines=lines)
Initialize from points and triangle strips.
>>> strips = np.hstack([[4, 0, 1, 3, 2]])
>>> mesh = pv.PolyData(points, strips=strips)
It is also possible to create with multiple cell types.
>>> verts = [1, 0]
>>> lines = [2, 1, 2]
>>> mesh = pv.PolyData(points, verts=verts, lines=lines)
Initialize from a filename.
>>> mesh = pv.PolyData(examples.antfile)
Construct a set of random line segments using a ``pv.CellArray`.
Because every line in this example has the same size, in this case
two points, we can use ``pv.CellArray.from_regular_cells`` to
construct the ``lines`` cell array. This is the most efficient
method to construct a cell array.
>>> n_points = 20
>>> n_lines = n_points // 2
>>> points = rng.random((n_points, 3))
>>> lines = rng.integers(low=0, high=n_points, size=(n_lines, 2))
>>> mesh = pv.PolyData(points, lines=pv.CellArray.from_regular_cells(lines))
>>> mesh.cell_data['line_idx'] = np.arange(n_lines)
>>> mesh.plot(scalars='line_idx')
Construct a set of random triangle strips using a ``pv.CellArray``.
Because each strip in this example can have a different number
of points, we use ``pv.CellArray.from_irregular_cells`` to construct
the ``strips`` cell array.
>>> n_strips = 4
>>> n_verts_per_strip = rng.integers(low=3, high=7, size=n_strips)
>>> n_points = 10 * sum(n_verts_per_strip)
>>> points = rng.random((n_points, 3))
>>> strips = [
... rng.integers(low=0, high=n_points, size=nv) for nv in n_verts_per_strip
... ]
>>> mesh = pv.PolyData(
... points, strips=pv.CellArray.from_irregular_cells(strips)
... )
>>> mesh.cell_data['strip_idx'] = np.arange(n_strips)
>>> mesh.plot(show_edges=True, scalars='strip_idx')
Construct a mesh reusing the ``faces`` ``pv.CellArray`` from another
mesh. The VTK methods ``GetPolys``, ``GetLines``, ``GetStrips``, and
``GetVerts`` return the underlying ``CellArray``s for the ``faces``,
``lines``, ``strips``, and ``verts`` properties respectively.
Reusing cell arrays like this can be a performance optimization for
large meshes because it avoids allocating new arrays.
>>> small_sphere = pv.Sphere().compute_normals()
>>> inflated_points = (
... small_sphere.points + 0.1 * small_sphere.point_data['Normals']
... )
>>> larger_sphere = pv.PolyData(inflated_points, faces=small_sphere.GetPolys())
>>> plotter = pv.Plotter()
>>> _ = plotter.add_mesh(small_sphere, color='red', show_edges=True)
>>> _ = plotter.add_mesh(
... larger_sphere, color='blue', opacity=0.3, show_edges=True
... )
>>> plotter.show()
See :ref:`create_poly_example` for more examples.
"""
_USE_STRICT_N_FACES = False
_WRITERS: ClassVar[
dict[
str,
(type[_PolyDataWriterAlias]),
]
] = { # type: ignore[assignment]
'.ply': _vtk.vtkPLYWriter,
'.vtp': _vtk.vtkXMLPolyDataWriter,
'.stl': _vtk.vtkSTLWriter,
'.vtk': _vtk.vtkPolyDataWriter,
'.geo': _vtk.vtkHoudiniPolyDataWriter,
'.obj': _vtk.vtkOBJWriter,
'.iv': _vtk.vtkIVWriter,
}
if _vtk.vtk_version_info >= (9, 4):
_WRITERS.update({'.vtkhdf': _vtk.vtkHDFWriter})
@_deprecate_positional_args(allowed=['var_inp', 'faces'])
def __init__( # noqa: PLR0917
self,
var_inp: _vtk.vtkPolyData | str | Path | MatrixLike[float] | None = None,
faces: CellArrayLike | None = None,
n_faces: int | None = None,
lines: CellArrayLike | None = None,
n_lines: int | None = None,
strips: CellArrayLike | None = None,
n_strips: int | None = None,
deep: bool = False, # noqa: FBT001, FBT002
force_ext: str | None = None,
force_float: bool = True, # noqa: FBT001, FBT002
verts: CellArrayLike | None = None,
n_verts: int | None = None,
) -> None:
"""Initialize the polydata."""
local_parms = locals()
super().__init__()
# allow empty input
if var_inp is None:
return
# filename
opt_kwarg = ['faces', 'n_faces', 'lines', 'n_lines']
if isinstance(var_inp, (str, Path)):
for kwarg in opt_kwarg:
if local_parms[kwarg]:
msg = 'No other arguments should be set when first parameter is a string'
raise ValueError(msg)
self._from_file(var_inp, force_ext=force_ext) # is filename
return
# PolyData-like
if isinstance(var_inp, _vtk.vtkPolyData):
for kwarg in opt_kwarg:
if local_parms[kwarg]:
msg = 'No other arguments should be set when first parameter is a PolyData'
raise ValueError(msg)
if deep:
self.deep_copy(var_inp)
else:
self.shallow_copy(var_inp) # type: ignore[arg-type]
return
# First parameter is points
if isinstance(var_inp, (np.ndarray, list, _vtk.vtkDataArray)):
self.SetPoints(vtk_points(var_inp, deep=deep, force_float=force_float))
else:
msg = f"""
Invalid Input type:
Expected first argument to be either a:
- vtkPolyData
- pyvista.PolyData
- numeric numpy.ndarray (1 or 2 dimensions)
- List (flat or nested with 3 points per vertex)
- vtkDataArray
Instead got: {type(var_inp)}"""
raise TypeError(dedent(msg.strip('\n')))
# At this point, points have been setup, add faces and/or lines
if faces is lines is strips is verts is None:
# one cell per point (point cloud case)
verts = self._make_vertex_cells(self.n_points)
for k, v in (('verts', verts), ('strips', strips), ('faces', faces), ('lines', lines)):
if v is None:
continue
# These properties can be supplied as either arrays or pre-constructed `CellArray`s
if not isinstance(v, _vtk.vtkCellArray):
try:
v = CellArray(v) # noqa: PLW2901
except CellSizeError as err:
# Raise an additional error so user knows which property triggered the error
msg = f'`{k}` cell array size is invalid.'
raise CellSizeError(msg) from err
setattr(self, k, v)
# deprecated 0.44.0, convert to error in 0.47.0, remove 0.48.0
for k, v in ( # type: ignore[assignment]
('n_verts', n_verts),
('n_strips', n_strips),
('n_faces', n_faces),
('n_lines', n_lines),
):
if v is not None:
warnings.warn(
f'`PolyData` constructor parameter `{k}` is deprecated and no longer used.',
PyVistaDeprecationWarning,
)
def _post_file_load_processing(self) -> None:
"""Execute after loading a PolyData from file."""
# When loading files with just point arrays, create and
# set the polydata vertices
if self.n_points > 0 and self.n_cells == 0:
self.verts = self._make_vertex_cells(self.n_points)
def __repr__(self) -> str:
"""Return the standard representation."""
return DataSet.__repr__(self)
def __str__(self) -> str:
"""Return the standard str representation."""
return DataSet.__str__(self)
@staticmethod
def _make_vertex_cells(npoints: int) -> NumpyArray[int]:
cells = np.empty((npoints, 2), dtype=pyvista.ID_TYPE)
cells[:, 0] = 1
cells[:, 1] = np.arange(npoints, dtype=pyvista.ID_TYPE)
return cells
@property
def verts(self) -> NumpyArray[int]: # numpydoc ignore=RT01
"""Get the vertex cells.
Returns
-------
numpy.ndarray
Array of vertex cell indices.
Examples
--------
Create a point cloud polydata and return the vertex cells.
>>> import pyvista as pv
>>> import numpy as np
>>> rng = np.random.default_rng(seed=0)
>>> points = rng.random((5, 3))
>>> pdata = pv.PolyData(points)
>>> pdata.verts
array([1, 0, 1, 1, 1, 2, 1, 3, 1, 4])
Set vertex cells. Note how the mesh plots both the surface
mesh and the additional vertices in a single plot.
>>> mesh = pv.Plane(i_resolution=3, j_resolution=3)
>>> mesh.verts = np.vstack(
... (
... np.ones(mesh.n_points, dtype=np.int64),
... np.arange(mesh.n_points),
... )
... ).T
>>> mesh.plot(
... color='lightblue',
... render_points_as_spheres=True,
... point_size=60,
... )
Vertex cells can also be set to a ``CellArray``. The following
``verts`` assignment is equivalent to the one above.
>>> mesh.verts = pv.CellArray.from_regular_cells(
... np.arange(mesh.n_points).reshape((-1, 1))
... )
"""
return _vtk.vtk_to_numpy(self.GetVerts().GetData())
@verts.setter
def verts(self, verts: CellArrayLike) -> None:
if isinstance(verts, _vtk.vtkCellArray):
self.SetVerts(verts)
else:
self.SetVerts(CellArray(verts))
@property
def lines(self) -> NumpyArray[int]: # numpydoc ignore=RT01
"""Return the connectivity array of the lines of this PolyData.
Lines can also be set by assigning a :class:`~pyvista.CellArray`.
Examples
--------
Return the lines from a spline.
>>> import pyvista as pv
>>> import numpy as np
>>> points = np.random.default_rng().random((3, 3))
>>> spline = pv.Spline(points, 10)
>>> spline.lines
array([10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
"""
return _vtk.vtk_to_numpy(self.GetLines().GetData()).ravel()
@lines.setter
def lines(self, lines: CellArrayLike) -> None:
if isinstance(lines, _vtk.vtkCellArray):
self.SetLines(lines)
else:
self.SetLines(CellArray(lines))
@property
def faces(self) -> NumpyArray[int]: # numpydoc ignore=RT01
"""Return the connectivity array of the faces of this PolyData.
The faces array is organized as::
[n0, p0_0, p0_1, ..., p0_n, n1, p1_0, p1_1, ..., p1_n, ...]
where ``n0`` is the number of points in face 0, and ``pX_Y`` is the
Y'th point in face X.
For example, a triangle and a quadrilateral might be represented as::
[3, 0, 1, 2, 4, 0, 1, 3, 4]
Where the two individual faces would be ``[3, 0, 1, 2]`` and ``[4, 0, 1, 3, 4]``.
Faces can also be set by assigning a :class:`~pyvista.CellArray` object
instead of an array.
Returns
-------
numpy.ndarray
Array of face connectivity.
See Also
--------
pyvista.PolyData.regular_faces
pyvista.PolyData.irregular_faces
Notes
-----
The array returned cannot be modified in place and will raise a
``ValueError`` if attempted.
You can, however, set the faces directly. See the example.
Examples
--------
>>> import pyvista as pv
>>> plane = pv.Plane(i_resolution=2, j_resolution=2)
>>> plane.faces
array([4, 0, 1, 4, 3, 4, 1, 2, 5, 4, 4, 3, 4, 7, 6, 4, 4, 5, 8, 7])
Note how the faces contain a "padding" indicating the number
of points per face:
>>> plane.faces.reshape(-1, 5)
array([[4, 0, 1, 4, 3],
[4, 1, 2, 5, 4],
[4, 3, 4, 7, 6],
[4, 4, 5, 8, 7]])
Set the faces directly. The following example creates a simple plane
with a single square faces and modifies it to have two triangles
instead.
>>> mesh = pv.Plane(i_resolution=1, j_resolution=1)
>>> mesh.faces = [3, 0, 1, 2, 3, 3, 2, 1]
>>> mesh.faces
array([3, 0, 1, 2, 3, 3, 2, 1])
"""
array = _vtk.vtk_to_numpy(self.GetPolys().GetData())
# Flag this array as read only to ensure users do not attempt to write to it.
array.flags['WRITEABLE'] = False
return array
@faces.setter
def faces(self, faces: CellArrayLike) -> None:
if isinstance(faces, _vtk.vtkCellArray):
self.SetPolys(faces)
else:
# TODO: faster to mutate in-place if array is same size?
self.SetPolys(CellArray(faces))
@property
def regular_faces(self) -> NumpyArray[int]: # numpydoc ignore=RT01
"""Return a face array of point indices when all faces have the same size.
Returns
-------
numpy.ndarray
Array of face indices with shape (n_faces, face_size).
See Also
--------
pyvista.PolyData.faces
Notes
-----
This property does not validate that the mesh's faces are all
actually the same size. If they're not, this property may either
raise a `ValueError` or silently return an incorrect array.
Examples
--------
Get the regular face array of a plane with 2x2 arrangement of cells
as a 4x4 array.
>>> import pyvista as pv
>>> plane = pv.Plane(i_resolution=2, j_resolution=2)
>>> plane.regular_faces
array([[0, 1, 4, 3],
[1, 2, 5, 4],
[3, 4, 7, 6],
[4, 5, 8, 7]])
"""
return _get_regular_cells(self.GetPolys())
@regular_faces.setter
def regular_faces(self, faces: MatrixLike[int]) -> None: # numpydoc ignore=PR01
"""Set the face cells from an (n_faces, face_size) array."""
self.faces = CellArray.from_regular_cells(faces)
@classmethod
@_deprecate_positional_args(allowed=['points', 'faces'])
def from_regular_faces(
cls,
points: MatrixLike[float],
faces: MatrixLike[int],
deep: bool = False, # noqa: FBT001, FBT002
):
"""Alternate `pyvista.PolyData` convenience constructor from point and regular face arrays.
Parameters
----------
points : MatrixLike[float]
A (n_points, 3) array of points.
faces : MatrixLike[int]
A (n_faces, face_size) array of face indices. For a triangle mesh, ``face_size = 3``.
deep : bool, default: False
Whether to deep copy the faces array into :vtk:`vtkCellArray` connectivity data.
Returns
-------
pyvista.PolyData
The newly constructed mesh.
See Also
--------
pyvista.PolyData.from_irregular_faces
Examples
--------
Construct a tetrahedron from four triangles
>>> import pyvista as pv
>>> points = [[1.0, 1, 1], [-1, 1, -1], [1, -1, -1], [-1, -1, 1]]
>>> faces = [[0, 1, 2], [1, 3, 2], [0, 2, 3], [0, 3, 1]]
>>> tetra = pv.PolyData.from_regular_faces(points, faces)
>>> tetra.plot()
"""
return cls(points, faces=CellArray.from_regular_cells(faces, deep=deep))
@property
def irregular_faces(self) -> tuple[NumpyArray[int], ...]: # numpydoc ignore=RT01
"""Return a tuple of face arrays.
Returns
-------
tuple[numpy.ndarray]
Tuple of length n_faces where each element is an array of point
indices for points in that face.
See Also
--------
pyvista.PolyData.faces
pyvista.PolyData.regular_faces
Examples
--------
Get the face arrays of the five faces of a pyramid.
>>> import pyvista as pv
>>> pyramid = pv.Pyramid().extract_surface()
>>> pyramid.irregular_faces # doctest: +NORMALIZE_WHITESPACE
(array([0, 1, 2, 3]),
array([0, 3, 4]),
array([0, 4, 1]),
array([3, 2, 4]),
array([2, 1, 4]))
"""
return _get_irregular_cells(self.GetPolys())
@irregular_faces.setter
def irregular_faces(self, faces: Sequence[VectorLike[int]]) -> None: # numpydoc ignore=PR01
"""Set the faces from a sequence of face arrays."""
self.faces = CellArray.from_irregular_cells(faces)
@classmethod
def from_irregular_faces(cls, points: MatrixLike[float], faces: Sequence[VectorLike[int]]):
"""Alternate `pyvista.PolyData` convenience constructor from point and ragged face arrays.
Parameters
----------
points : MatrixLike[float]
A (n_points, 3) array of points.
faces : Sequence[VectorLike[int]]
A sequence of face vectors containing point indices.
Returns
-------
pyvista.PolyData
The newly constructed mesh.
See Also
--------
pyvista.PolyData.from_regular_faces
Examples
--------
Construct a pyramid from five points and five faces
>>> import pyvista as pv
>>> points = [
... (1, 1, 0),
... (-1, 1, 0),
... (-1, -1, 0),
... (1, -1, 0),
... (0, 0, 1.61),
... ]
>>> faces = [
... (0, 1, 2, 3),
... (0, 3, 4),
... (0, 4, 1),
... (3, 2, 4),
... (2, 1, 4),
... ]
>>> pyramid = pv.PolyData.from_irregular_faces(points, faces)
>>> pyramid.plot()
"""
return cls(points, faces=CellArray.from_irregular_cells(faces))
@property
def strips(self) -> NumpyArray[int]: # numpydoc ignore=RT01
"""Return a pointer to the strips as a numpy array.
Returns
-------
numpy.ndarray
Array of strip indices.
Examples
--------
>>> import pyvista as pv
>>> polygon = pv.Rectangle()
>>> extruded = polygon.extrude((0, 0, 1), capping=False)
>>> extruded.strips
array([4, 0, 1, 4, 5, 4, 1, 2, 5, 6, 4, 2, 3, 6, 7, 4, 3, 0, 7, 4])
"""
return _vtk.vtk_to_numpy(self.GetStrips().GetData())
@strips.setter
def strips(self, strips: CellArrayLike) -> None:
if isinstance(strips, _vtk.vtkCellArray):
self.SetStrips(strips)
else:
self.SetStrips(CellArray(strips))
@property
def is_all_triangles(self) -> bool: # numpydoc ignore=RT01
"""Return if all the faces of the :class:`pyvista.PolyData` are triangles.
Returns
-------
bool
``True`` if all the faces of the :class:`pyvista.PolyData`
are triangles and does not contain any vertices or lines.
Examples
--------
Show a mesh from :func:`pyvista.Plane` is not composed of all
triangles.
>>> import pyvista as pv
>>> plane = pv.Plane()
>>> plane.is_all_triangles
False
Show that the mesh from :func:`pyvista.Sphere` contains only
triangles.
>>> sphere = pv.Sphere()
>>> sphere.is_all_triangles
True
"""
# Need to make sure there are only face cells and no lines/verts
if not self.n_faces_strict or self.n_lines or self.n_verts:
return False
# early return if not all triangular
if self._connectivity_array.size % 3:
return False
# next, check if there are three points per face
return bool((np.diff(self._offset_array) == 3).all())
def __sub__(self, cutting_mesh):
"""Compute boolean difference of two meshes."""
return self.boolean_difference(cutting_mesh)
def __isub__(self, cutting_mesh):
"""Compute boolean difference of two meshes and update this mesh."""
return self.boolean_difference(cutting_mesh)
def __and__(self, other_mesh):
"""Compute boolean intersection of two meshes."""
return self.boolean_intersection(other_mesh)
def __or__(self, other_mesh):
"""Compute boolean union of two meshes."""
return self.boolean_union(other_mesh)
@property
def _offset_array(self) -> NumpyArray[int]:
"""Return the array used to store cell offsets."""
return _get_offset_array(self.GetPolys())
@property
def _connectivity_array(self) -> NumpyArray[int]:
"""Return the array with the point ids that define the cells' connectivity."""
return _get_connectivity_array(self.GetPolys())
@property
def n_lines(self) -> int: # numpydoc ignore=RT01
"""Return the number of lines.
Examples
--------
>>> import pyvista as pv
>>> mesh = pv.Line()
>>> mesh.n_lines
1
"""
return self.GetNumberOfLines()
@property
def n_verts(self) -> int: # numpydoc ignore=RT01
"""Return the number of vertices.
A vertex is a 0D cell, which is usually a cell that references one point,
a :vtk:`vtkVertex`. It can also be a :vtk:`vtkPolyVertex`.
See `pyvista.PolyData.n_points` for the more common measure.
Examples
--------
Create a simple mesh containing just two points and return the
number of vertices. By default, when constructing a PolyData with points but no cells,
vertices are automatically created, one per point.
>>> import pyvista as pv
>>> mesh = pv.PolyData([[1.0, 0.0, 0.0], [1.0, 1.0, 1.0]])
>>> mesh.n_points, mesh.n_verts
(2, 2)
If any other cells are specified, these vertices are not created.
>>> import pyvista as pv
>>> mesh = pv.PolyData([[1.0, 0.0, 0.0], [1.0, 1.0, 1.0]], lines=[2, 0, 1])
>>> mesh.n_points, mesh.n_verts
(2, 0)
"""
return self.GetNumberOfVerts()
@property
def n_strips(self) -> int: # numpydoc ignore=RT01
"""Return the number of strips.
Examples
--------
Create a simple mesh with one triangle strip and return the
number of triangles.
>>> import pyvista as pv
>>> import numpy as np
>>> vertices = np.array([[1.0, 0.0, 0.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]])
>>> strip = np.array([3, 0, 1, 2])
>>> mesh = pv.PolyData(vertices, strips=strip)
>>> mesh.n_strips
1
"""
return self.GetNumberOfStrips()
@staticmethod
def use_strict_n_faces(mode: bool) -> None: # noqa: FBT001
"""Global opt-in to strict n_faces.
Parameters
----------
mode : bool
If true, all future calls to :attr:`n_faces <pyvista.PolyData.n_faces>`
will return the same thing as :attr:`n_faces_strict <pyvista.PolyData.n_faces_strict>`.
"""
PolyData._USE_STRICT_N_FACES = mode
@property
def n_faces(self) -> int: # numpydoc ignore=RT01
"""Return the number of cells.
.. deprecated:: 0.43.0
The current (deprecated) behavior of this property is to
return the total number of cells, i.e. the sum of the number of
vertices, lines, triangle strips, and polygonal faces.
In the future, this will change to return only the number of
polygonal faces, i.e. those cells represented in the
`pv.PolyData.faces` array. If you want the total number of cells,
use `pv.PolyData.n_cells`. If you want only the number of polygonal faces,
use `pv.PolyData.n_faces_strict`. Alternatively, you can opt into the
future behavior globally by calling `pv.PolyData.use_strict_n_faces(True)`,
in which case `pv.PolyData.n_faces` will return the same thing as
`pv.PolyData.n_faces_strict`.
"""
if PolyData._USE_STRICT_N_FACES:
return self.n_faces_strict
# deprecated 0.43.0, convert to error in 0.46.0, remove 0.49.0
msg = (
'The non-strict behavior of `pv.PolyData.n_faces` has been removed. '
'Use `pv.PolyData.n_cells` or `pv.PolyData.n_faces_strict` instead. '
'See the documentation in `pv.PolyData.n_faces` for more information.'
)
raise AttributeError(msg)
@property
def n_faces_strict(self) -> int: # numpydoc ignore=RT01
"""Return the number of polygonal faces.
Returns
-------
int :
Number of faces represented in the :attr:`n_faces <pyvista.PolyData.n_faces>` array.
Examples
--------
Create a mesh with one face and one line
>>> import pyvista as pv
>>> mesh = pv.PolyData(
... [(0.0, 0, 0), (1, 0, 0), (0, 1, 0)],
... faces=[3, 0, 1, 2],
... lines=[2, 0, 1],
... )
>>> mesh.n_cells, mesh.n_faces_strict
(2, 1)
"""
return self.GetNumberOfPolys()
@_deprecate_positional_args(allowed=['filename'])
def save( # noqa: PLR0917
self,
filename,
binary: bool = True, # noqa: FBT001, FBT002
texture=None,
recompute_normals: bool = True, # noqa: FBT001, FBT002
):
"""Write a surface mesh to disk.
Written file may be an ASCII or binary ply, stl, or vtk mesh
file.
Parameters
----------
filename : str, Path
Filename of mesh to be written. File type is inferred from
the extension of the filename unless overridden with
ftype. Can be one of many of the supported the following
types (``'.ply'``, ``'.vtp'``, ``'.stl'``, ``'.vtk``, ``'.geo'``,
``'.obj'``, ``'.iv'``).
binary : bool, default: True
Writes the file as binary when ``True`` and ASCII when ``False``.
texture : str, numpy.ndarray, optional
Write a single texture array to file when using a PLY
file. Texture array must be a 3 or 4 component array with
the datatype ``np.uint8``. Array may be a cell array or a
point array, and may also be a string if the array already
exists in the PolyData.
If a string is provided, the texture array will be saved
to disk as that name. If an array is provided, the
texture array will be saved as ``'RGBA'`` if the array
contains an alpha channel (i.e. 4 component array), or
as ``'RGB'`` if the array is just a 3 component array.
.. note::
This feature is only available when saving PLY files.
recompute_normals : bool, default: True
When ``True``, if ply or stl format is chosen, the face normals
are computed in place to ensure the mesh is properly saved.
Set this to ``False`` to save instead the already existing normal
array in the PolyData.
Notes
-----
Binary files write much faster than ASCII and have a smaller
file size.
Examples
--------
Save a mesh as a STL.
>>> import pyvista as pv
>>> sphere = pv.Sphere()
>>> sphere.save('my_mesh.stl') # doctest:+SKIP
Save a mesh as a PLY.
>>> sphere = pv.Sphere()
>>> sphere.save('my_mesh.ply') # doctest:+SKIP
Save a mesh as a PLY with a texture array. Here we also
create a simple RGB array representing the texture.
>>> import numpy as np
>>> sphere = pv.Sphere()
>>> texture = np.zeros((sphere.n_points, 3), np.uint8)
>>> # Just the green channel is set as a repeatedly
>>> # decreasing value
>>> texture[:, 1] = np.arange(sphere.n_points)[::-1]
>>> sphere.point_data['my_texture'] = texture
>>> sphere.save('my_mesh.ply', texture='my_texture') # doctest:+SKIP
Alternatively, provide just the texture array. This will be
written to the file as ``'RGB'`` since it does not contain an
alpha channel.
>>> sphere.save('my_mesh.ply', texture=texture) # doctest:+SKIP
Save a mesh as a VTK file.
>>> sphere = pv.Sphere()
>>> sphere.save('my_mesh.vtk') # doctest:+SKIP
"""
filename = Path(filename).expanduser().resolve()
ftype = get_ext(filename)
# Recompute normals prior to save. Corrects a bug were some
# triangular meshes are not saved correctly
if ftype in ['.stl', '.ply'] and recompute_normals:
with contextlib.suppress(TypeError):
self.compute_normals(inplace=True)
# validate texture
if ftype == '.ply' and texture is not None:
if isinstance(texture, str):
if self[texture].dtype != np.uint8:
msg = f'Invalid datatype {self[texture].dtype} of texture array "{texture}"'
raise ValueError(msg)
elif isinstance(texture, np.ndarray):
if texture.dtype != np.uint8:
msg = f'Invalid datatype {texture.dtype} of texture array'
raise ValueError(msg)
else:
msg = (
f'Invalid type {type(texture)} for texture. '
'Should be either a string representing a point or '
'cell array, or a numpy array.'
)
raise TypeError(msg)
super().save(filename, binary=binary, texture=texture)
@property
def volume(self) -> float: # numpydoc ignore=RT01
"""Return the approximate volume of the dataset.
This will throw a VTK error/warning if not a closed surface.
Returns
-------
float
Total volume of the mesh.
Examples
--------
>>> import pyvista as pv
>>> sphere = pv.Sphere()
>>> sphere.volume
0.5183
"""
mprop = _vtk.vtkMassProperties()
mprop.SetInputData(self.triangulate())
return mprop.GetVolume()
@property
def point_normals(self) -> pyvista.pyvista_ndarray: # numpydoc ignore=RT01
"""Return the point normals.
The active point normals are returned if they exist. Otherwise, they
are computed with :func:`~pyvista.PolyDataFilters.compute_normals`
using the default options.
Returns
-------
pyvista.pyvista_ndarray
Array of point normals.
Examples
--------
>>> import pyvista as pv
>>> sphere = pv.Sphere()
>>> sphere.point_normals
pyvista_ndarray([[ 0. , 0. , 1. ],
[ 0. , 0. , -1. ],
[ 0.10811902, 0. , 0.99413794],
...,
[ 0.31232402, -0.06638652, -0.9476532 ],
[ 0.21027282, -0.04469487, -0.97662055],
[ 0.10575636, -0.02247921, -0.99413794]],
shape=(842, 3), dtype=float32)
"""
if self.point_data.active_normals is not None:
normals = self.point_data.active_normals
else:
normals = self.compute_normals(cell_normals=False, inplace=False).point_data['Normals']
return normals
@property
def cell_normals(self) -> pyvista.pyvista_ndarray: # numpydoc ignore=RT01
"""Return the cell normals.
The active cell normals are returned if they exist. Otherwise, they
are computed with :func:`~pyvista.PolyDataFilters.compute_normals`
using the default options.
Returns
-------
pyvista.pyvista_ndarray
Array of cell normals.
Examples
--------
>>> import pyvista as pv
>>> sphere = pv.Sphere()
>>> sphere.cell_normals
pyvista_ndarray([[ 0.05413816, 0.00569015, 0.9985172 ],
[ 0.05177207, 0.01682176, 0.9985172 ],
[ 0.04714328, 0.02721819, 0.9985172 ],
...,
[ 0.26742265, -0.02810723, -0.96316934],
[ 0.1617585 , -0.01700151, -0.9866839 ],
[ 0.1617585 , -0.01700151, -0.9866839 ]],
shape=(1680, 3), dtype=float32)
"""
if self.cell_data.active_normals is not None:
normals = self.cell_data.active_normals
else:
normals = self.compute_normals(point_normals=False, inplace=False).cell_data['Normals']
return normals
@property
def face_normals(self) -> pyvista.pyvista_ndarray: # numpydoc ignore=RT01
"""Return the cell normals.
Alias to :func:`PolyData.cell_normals`.
Returns
-------
pyvista.pyvista_ndarray
Array of face normals.
Examples
--------
>>> import pyvista as pv
>>> sphere = pv.Sphere()
>>> sphere.face_normals
pyvista_ndarray([[ 0.05413816, 0.00569015, 0.9985172 ],
[ 0.05177207, 0.01682176, 0.9985172 ],
[ 0.04714328, 0.02721819, 0.9985172 ],
...,
[ 0.26742265, -0.02810723, -0.96316934],
[ 0.1617585 , -0.01700151, -0.9866839 ],
[ 0.1617585 , -0.01700151, -0.9866839 ]],
shape=(1680, 3), dtype=float32)
"""
return self.cell_normals
@cached_property
def obbTree(self) -> _vtk.vtkOBBTree: # noqa: N802 # numpydoc ignore=RT01
"""Return the obbTree of the polydata.
An obbTree is an object to generate oriented bounding box (OBB)
trees. An oriented bounding box is a bounding box that does not
necessarily line up along coordinate axes. The OBB tree is a
hierarchical tree structure of such boxes, where deeper levels of OBB
confine smaller regions of space.
.. warning::
This property is expensive to compute and is therefore cached. If the mesh's
geometry is modified, the obb tree will no longer be valid.
"""
obb_tree = _vtk.vtkOBBTree()
obb_tree.SetDataSet(self)
obb_tree.BuildLocator()
return obb_tree
@property
def n_open_edges(self) -> int: # numpydoc ignore=RT01
"""Return the number of open edges on this mesh.
Examples
--------
Return the number of open edges on a sphere.
>>> import pyvista as pv
>>> sphere = pv.Sphere()
>>> sphere.n_open_edges
0
Return the number of open edges on a plane.
>>> plane = pv.Plane(i_resolution=1, j_resolution=1)
>>> plane.n_open_edges
4
"""
alg = _vtk.vtkFeatureEdges()
alg.FeatureEdgesOff()
alg.BoundaryEdgesOn()
alg.NonManifoldEdgesOn()
alg.SetInputDataObject(self)
alg.Update()
return alg.GetOutput().GetNumberOfCells()
@property
def is_manifold(self) -> bool: # numpydoc ignore=RT01
"""Return if the mesh is manifold (no open edges).
Examples
--------
Show a sphere is manifold.
>>> import pyvista as pv
>>> pv.Sphere().is_manifold
True
Show a plane is not manifold.
>>> pv.Plane().is_manifold
False
"""
return self.n_open_edges == 0
def __del__(self) -> None:
"""Delete the object."""
# avoid a reference cycle that can't be resolved with vtkPolyData
self._glyph_geom = None
self.obbTree = None # type: ignore[assignment]
@abstract_class
class PointGrid(_PointSet):
"""Class in common with structured and unstructured grids."""
def __init__(self, *args, **kwargs) -> None: # noqa: ARG002
"""Initialize the point grid."""
super().__init__()
def plot_curvature(self: Self, curv_type='mean', **kwargs):
"""Plot the curvature of the external surface of the grid.
Parameters
----------
curv_type : str, default: "mean"
One of the following strings indicating curvature types.
- ``'mean'``
- ``'gaussian'``
- ``'maximum'``
- ``'minimum'``
**kwargs : dict, optional
Optional keyword arguments. See :func:`pyvista.plot`.
Returns
-------
list
Camera position, focal point, and view up. Returned when
``return_cpos`` is ``True``.
"""
trisurf = self.extract_surface().triangulate()
return trisurf.plot_curvature(curv_type, **kwargs)
class UnstructuredGrid(PointGrid, UnstructuredGridFilters, _vtk.vtkUnstructuredGrid):
"""Dataset used for arbitrary combinations of all possible cell types.
Can be initialized by the following:
- Creating an empty grid
- From a :vtk:`vtkPolyData` or :vtk:`vtkStructuredGrid` object
- From cell, cell types, and point arrays
- From a file
Parameters
----------
args : str, :vtk:`vtkUnstructuredGrid`, iterable
See examples below.
deep : bool, default: False
Whether to deep copy a :vtk:`vtkUnstructuredGrid` object.
Default is ``False``. Keyword only.
Examples
--------
>>> import pyvista as pv
>>> from pyvista import examples
>>> import vtk
Create an empty grid
>>> grid = pv.UnstructuredGrid()
Copy a :vtk:`vtkUnstructuredGrid`
>>> vtkgrid = vtk.vtkUnstructuredGrid()
>>> grid = pv.UnstructuredGrid(vtkgrid)
From a filename.
>>> grid = pv.UnstructuredGrid(examples.hexbeamfile)
>>> grid.plot(show_edges=True)
From arrays. Here we create a single tetrahedron.
>>> cells = [4, 0, 1, 2, 3]
>>> celltypes = [pv.CellType.TETRA]
>>> points = [
... [1.0, 1.0, 1.0],
... [1.0, -1.0, -1.0],
... [-1.0, 1.0, -1.0],
... [-1.0, -1.0, 1.0],
... ]
>>> grid = pv.UnstructuredGrid(cells, celltypes, points)
>>> grid.plot(show_edges=True)
See the :ref:`create_unstructured_surface_example` example for more details
on creating unstructured grids within PyVista.
"""
_WRITERS: ClassVar[
dict[
str,
type[_UnstructuredGridWriterAlias],
]
] = { # type: ignore[assignment]
'.vtu': _vtk.vtkXMLUnstructuredGridWriter,
'.vtk': _vtk.vtkUnstructuredGridWriter,
}
if _vtk.vtk_version_info >= (9, 4):
_WRITERS['.vtkhdf'] = _vtk.vtkHDFWriter
def __init__(self, *args, deep: bool = False, **kwargs) -> None:
"""Initialize the unstructured grid."""
super().__init__()
if not args:
return
if len(args) == 1:
if isinstance(args[0], _vtk.vtkUnstructuredGrid):
if deep:
self.deep_copy(args[0])
else:
self.shallow_copy(args[0]) # type: ignore[arg-type]
elif isinstance(args[0], (str, Path)):
self._from_file(args[0], **kwargs)
elif isinstance(args[0], (_vtk.vtkStructuredGrid, _vtk.vtkPolyData)):
vtkappend = _vtk.vtkAppendFilter()
vtkappend.AddInputData(args[0])
vtkappend.Update()
self.shallow_copy(vtkappend.GetOutput())
else:
itype = type(args[0])
msg = f'Cannot work with input type {itype}'
raise TypeError(msg)
# Cell dictionary creation
elif len(args) == 2 and isinstance(args[0], dict) and isinstance(args[1], np.ndarray):
self._from_cells_dict(args[0], args[1], deep=deep)
self._check_for_consistency()
elif len(args) == 3:
arg0_is_seq = isinstance(args[0], (np.ndarray, Sequence))
arg1_is_seq = isinstance(args[1], (np.ndarray, Sequence))
arg2_is_seq = isinstance(args[2], (np.ndarray, Sequence))
if all([arg0_is_seq, arg1_is_seq, arg2_is_seq]):
self._from_arrays(args[0], args[1], args[2], deep=deep, **kwargs)
self._check_for_consistency()
else:
msg = 'All input types must be sequences.'
raise TypeError(msg)
else:
msg = (
'Invalid parameters. Initialization with arrays requires the '
'following arrays:\n`cells`, `cell_type`, `points`'
)
raise TypeError(msg)
def __repr__(self):
"""Return the standard representation."""
return DataSet.__repr__(self)
def __str__(self):
"""Return the standard str representation."""
return DataSet.__str__(self)
def _from_cells_dict(self, cells_dict, points, *, deep: bool = True):
if points.ndim != 2 or points.shape[-1] != 3:
msg = 'Points array must be a [M, 3] array'
raise ValueError(msg)
nr_points = points.shape[0]
cell_types, cells = create_mixed_cells(cells_dict, nr_points)
self._from_arrays(cells, cell_types, points, deep=deep)
def _from_arrays(
self,
cells,
cell_type,
points,
*,
deep: bool = True,
force_float: bool = True,
) -> None:
"""Create VTK unstructured grid from numpy arrays.
Parameters
----------
cells : sequence[int]
Array of cells. Each cell contains the number of points in the
cell and the node numbers of the cell.
cell_type : sequence[int]
Cell types of each cell. Each cell type numbers can be found from
vtk documentation. More efficient if using ``np.uint8``. See
example below.
points : sequence[float]
Numpy array containing point locations.
deep : bool, default: True
When ``True``, makes a copy of the points array. Default
``False``. Cells and cell types are always copied.
force_float : bool, default: True
Casts the datatype to ``float32`` if points datatype is
non-float. Set this to ``False`` to allow non-float types,
though this may lead to truncation of intermediate floats when
transforming datasets.
Examples
--------
>>> import numpy as np
>>> from pyvista import CellType
>>> import pyvista as pv
>>> cell0_ids = [8, 0, 1, 2, 3, 4, 5, 6, 7]
>>> cell1_ids = [8, 8, 9, 10, 11, 12, 13, 14, 15]
>>> cells = np.hstack((cell0_ids, cell1_ids))
>>> cell_type = np.array([CellType.HEXAHEDRON, CellType.HEXAHEDRON], np.int8)
>>> cell1 = np.array(
... [
... [0, 0, 0],
... [1, 0, 0],
... [1, 1, 0],
... [0, 1, 0],
... [0, 0, 1],
... [1, 0, 1],
... [1, 1, 1],
... [0, 1, 1],
... ],
... dtype=np.float32,
... )
>>> cell2 = np.array(
... [
... [0, 0, 2],
... [1, 0, 2],
... [1, 1, 2],
... [0, 1, 2],
... [0, 0, 3],
... [1, 0, 3],
... [1, 1, 3],
... [0, 1, 3],
... ],
... dtype=np.float32,
... )
>>> points = np.vstack((cell1, cell2))
>>> grid = pv.UnstructuredGrid(cells, cell_type, points)
"""
# convert to arrays upfront
cells = np.asarray(cells)
cell_type = np.asarray(cell_type)
points = np.asarray(points)
# Convert to vtk arrays
vtkcells = CellArray(cells)
if cell_type.dtype != np.uint8:
cell_type = cell_type.astype(np.uint8)
cell_type = _vtk.numpy_to_vtk(cell_type, deep=deep)
points = vtk_points(points, deep=deep, force_float=force_float)
self.SetPoints(points)
self.SetCells(cell_type, vtkcells)
def _check_for_consistency(self):
"""Check if size of offsets and celltypes match the number of cells.
Checks if the number of offsets and celltypes correspond to
the number of cells. Called after initialization of the self
from arrays.
"""
if self.n_cells != self.celltypes.size:
msg = (
f'Number of cell types ({self.celltypes.size}) '
f'must match the number of cells {self.n_cells})'
)
raise ValueError(msg)
if self.n_cells != self.offset.size - 1: # pragma: no cover
msg = (
f'Size of the offset ({self.offset.size}) '
f'must be one greater than the number of cells ({self.n_cells})'
)
raise ValueError(msg)
@property
def cells(self) -> NumpyArray[int]: # numpydoc ignore=RT01
"""Return the cell data as a numpy object.
This is the old style VTK data layout::
[n0, p0_0, p0_1, ..., p0_n, n1, p1_0, p1_1, ..., p1_n, ...]
where ``n0`` is the number of points in cell 0, and ``pX_Y`` is the
Y'th point in cell X.
For example, a triangle and a line might be represented as::
[3, 0, 1, 2, 2, 0, 1]
Where the two individual cells would be ``[3, 0, 1, 2]`` and ``[2, 0, 1]``.
See Also
--------
pyvista.DataSet.get_cell
pyvista.UnstructuredGrid.cell_connectivity
pyvista.UnstructuredGrid.offset
Notes
-----
The array returned cannot be modified in place and will raise a
``ValueError`` if attempted.
You can, however, set the cells directly. See the example.
Examples
--------
Return the indices of the first two cells from the example hex
beam. Note how the cells have "padding" indicating the number
of points per cell.
>>> import pyvista as pv
>>> from pyvista import examples
>>> grid = examples.load_hexbeam()
>>> grid.cells[:18]
array([ 8, 0, 2, 8, 7, 27, 36, 90, 81, 8, 2, 1, 4, 8, 36, 18, 54,
90])
While you cannot change the array inplace, you can overwrite it. For example:
>>> grid.cells = [8, 0, 1, 2, 3, 4, 5, 6, 7]
"""
# Flag this array as read only to ensure users do not attempt to write to it.
array = _vtk.vtk_to_numpy(self._get_cells().GetData())
array.flags['WRITEABLE'] = False
return array
@cells.setter
def cells(self, cells) -> None:
vtk_idarr = numpy_to_idarr(cells, deep=False, return_ind=False)
self._get_cells().ImportLegacyFormat(vtk_idarr)
def _get_cells(self):
cells = self.GetCells()
return _vtk.vtkCellArray() if cells is None else cells # type: ignore[redundant-expr]
@property
def faces(self) -> NumpyArray[int]:
"""Return the polyhedron faces.
.. deprecated:: 0.45.0
This property is deprecated and will be removed in a future release.
VTK has deprecated `GetFaces` and `GetFaceLocations` in VTK 9.4 and
may be removed in a future release of VTK. Please use
`polyhedral_faces` instead.
Returns
-------
numpy.ndarray
Array of faces.
"""
return convert_array(self.GetFaces())
@property
def polyhedron_faces(self) -> NumpyArray[int]:
"""Return the polyhedron faces.
Returns
-------
numpy.ndarray
Array of faces.
"""
if pyvista.vtk_version_info < (9, 4):
polyhedron_faces = pyvista.convert_array(self.GetFaces())
if polyhedron_faces is None:
return np.array([], dtype=int) # type: ignore[unreachable]
cell_faces = []
i = 0
while i < len(polyhedron_faces):
faces_: list[VectorLike[int]] = []
n_faces = polyhedron_faces[i]
i += 1
while len(faces_) < n_faces:
n_vertices = polyhedron_faces[i]
faces_.append([n_vertices, *polyhedron_faces[i + 1 : i + 1 + n_vertices]])
i += n_vertices + 1
cell_faces.append(np.concatenate(faces_))
return np.concatenate(cell_faces)
else:
faces = self.GetPolyhedronFaces() # vtkCellArray
if faces is None:
return np.array([], dtype=int) # type: ignore[unreachable]
return convert_array(faces.GetData())
@property
def face_locations(self) -> NumpyArray[int]:
"""Return polyhedron face locations.
.. deprecated:: 0.45.0
This property is deprecated and will be removed in a future release.
VTK has deprecated `GetFaces` and `GetFaceLocations` in VTK 9.4 and
may be removed in a future release of VTK. Please use
`polyhedral_face_locations` instead.
Returns
-------
numpy.ndarray
Array of face locations.
"""
return convert_array(self.GetFaceLocations())
@property
def polyhedron_face_locations(self) -> NumpyArray[int]:
"""Return the polyhedron face locations.
Returns
-------
numpy.ndarray
Array of faces.
"""
if pyvista.vtk_version_info < (9, 4):
polyhedron_faces = pyvista.convert_array(self.GetFaces())
if polyhedron_faces is None:
return np.array([], dtype=int) # type: ignore[unreachable]
i, face_counts = 0, []
while i < len(polyhedron_faces):
n_faces = polyhedron_faces[i]
face_counts.append(n_faces)
face_count = 0
i += 1
while face_count < n_faces:
i += polyhedron_faces[i] + 1
face_count += 1
locations = [[0]] * self.n_cells
face_count = 0
for i, n_faces in zip(
np.flatnonzero(self.celltypes == pyvista.CellType.POLYHEDRON), face_counts
):
locations[i] = [n_faces, *(np.arange(n_faces) + face_count)]
face_count += n_faces
return np.concatenate(locations)
else:
faces = self.GetPolyhedronFaceLocations() # vtkCellArray
if faces is None:
return np.array([], dtype=int) # type: ignore[unreachable]
return convert_array(faces.GetData())
@property
def cells_dict(self) -> dict[np.uint8, NumpyArray[int]]: # numpydoc ignore=RT01
"""Return a dictionary that contains all cells mapped from cell types.
This function returns a :class:`numpy.ndarray` for each cell
type in an ordered fashion. Note that this function only
works with element types of fixed sizes.
.. versionchanged:: 0.46
An empty dict ``{}`` is returned instead of ``None`` if
the input is empty.
Returns
-------
dict
A dictionary mapping containing all cells of this unstructured grid.
Structure: vtk_enum_type (int) -> cells (:class:`numpy.ndarray`).
See Also
--------
pyvista.DataSet.get_cell
Examples
--------
Return the cells dictionary of the sample hex beam. Note how
there is only one key/value pair as the hex beam example is
composed of only all hexahedral cells, which is
``CellType.HEXAHEDRON``, which evaluates to 12.
Also note how there is no padding for the cell array. This
approach may be more helpful than the ``cells`` property when
extracting cells.
>>> import pyvista as pv
>>> from pyvista import examples
>>> hex_beam = pv.read(examples.hexbeamfile)
>>> hex_beam.cells_dict # doctest:+SKIP
{12: array([[ 0, 2, 8, 7, 27, 36, 90, 81],
[ 2, 1, 4, 8, 36, 18, 54, 90],
[ 7, 8, 6, 5, 81, 90, 72, 63],
...
[44, 26, 62, 98, 11, 10, 13, 17],
[89, 98, 80, 71, 16, 17, 15, 14],
[98, 62, 53, 80, 17, 13, 12, 15]])}
"""
return get_mixed_cells(self)
@property
def cell_connectivity(self) -> NumpyArray[int]: # numpydoc ignore=RT01
"""Return the cell connectivity as a numpy array.
This is effectively :attr:`UnstructuredGrid.cells` without the
padding.
Returns
-------
numpy.ndarray
Connectivity array.
See Also
--------
pyvista.DataSet.get_cell
Examples
--------
Return the cell connectivity for the first two cells.
>>> import pyvista as pv
>>> from pyvista import examples
>>> hex_beam = pv.read(examples.hexbeamfile)
>>> hex_beam.cell_connectivity[:16]
array([ 0, 2, 8, 7, 27, 36, 90, 81, 2, 1, 4, 8, 36, 18, 54, 90])
"""
carr = self._get_cells()
return _vtk.vtk_to_numpy(carr.GetConnectivityArray())
@_deprecate_positional_args
def linear_copy(self, deep: bool = False): # noqa: FBT001, FBT002
"""Return a copy of the unstructured grid containing only linear cells.
Converts the following cell types to their linear equivalents.
- :attr:`~pyvista.CellType.QUADRATIC_TRIANGLE` --> :attr:`~pyvista.CellType.TRIANGLE`
- :attr:`~pyvista.CellType.QUADRATIC_QUAD` --> :attr:`~pyvista.CellType.QUAD`
- :attr:`~pyvista.CellType.QUADRATIC_TETRA` --> :attr:`~pyvista.CellType.TETRA`
- :attr:`~pyvista.CellType.QUADRATIC_PYRAMID` --> :attr:`~pyvista.CellType.PYRAMID`
- :attr:`~pyvista.CellType.QUADRATIC_WEDGE` --> :attr:`~pyvista.CellType.WEDGE`
- :attr:`~pyvista.CellType.QUADRATIC_HEXAHEDRON` --> :attr:`~pyvista.CellType.HEXAHEDRON`
Parameters
----------
deep : bool, default: False
When ``True``, makes a copy of the points array.
Cells and cell types are always copied.
Returns
-------
pyvista.UnstructuredGrid
UnstructuredGrid containing only linear cells when
``deep=False``.
"""
lgrid = self.copy(deep=deep)
# grab the vtk object
vtk_cell_type = _vtk.numpy_to_vtk(self._get_cell_types_array(), deep=True)
celltype = _vtk.vtk_to_numpy(vtk_cell_type)
celltype[celltype == CellType.QUADRATIC_TETRA] = CellType.TETRA
celltype[celltype == CellType.QUADRATIC_PYRAMID] = CellType.PYRAMID
celltype[celltype == CellType.QUADRATIC_WEDGE] = CellType.WEDGE
celltype[celltype == CellType.QUADRATIC_HEXAHEDRON] = CellType.HEXAHEDRON
# track quad mask for later
quad_quad_mask = celltype == CellType.QUADRATIC_QUAD
celltype[quad_quad_mask] = CellType.QUAD
quad_tri_mask = celltype == CellType.QUADRATIC_TRIANGLE
celltype[quad_tri_mask] = CellType.TRIANGLE
cells = _vtk.vtkCellArray()
cells.DeepCopy(self._get_cells())
if pyvista.vtk_version_info >= (9, 5):
face_locations = self.GetPolyhedronFaceLocations()
faces = self.GetPolyhedronFaces()
lgrid.SetPolyhedralCells(vtk_cell_type, cells, face_locations, faces)
else:
vtk_offset = self.GetCellLocationsArray()
lgrid.SetCells(vtk_cell_type, vtk_offset, cells)
# fixing bug with display of quad cells
if np.any(quad_quad_mask):
quad_offset = lgrid.offset[:-1][quad_quad_mask]
base_point = lgrid.cell_connectivity[quad_offset]
lgrid.cell_connectivity[quad_offset + 4] = base_point
lgrid.cell_connectivity[quad_offset + 5] = base_point
lgrid.cell_connectivity[quad_offset + 6] = base_point
lgrid.cell_connectivity[quad_offset + 7] = base_point
if np.any(quad_tri_mask):
tri_offset = lgrid.offset[:-1][quad_tri_mask]
base_point = lgrid.cell_connectivity[tri_offset]
lgrid.cell_connectivity[tri_offset + 3] = base_point
lgrid.cell_connectivity[tri_offset + 4] = base_point
lgrid.cell_connectivity[tri_offset + 5] = base_point
return lgrid
@property
def celltypes(self) -> NumpyArray[np.uint8]: # numpydoc ignore=RT01
"""Return the cell types array.
The array contains integer values corresponding to the :attr:`pyvista.Cell.type`
of each cell in the dataset. See the :class:`pyvista.CellType` enum for more
information about cell type.
Returns
-------
numpy.ndarray
Array of cell types.
Examples
--------
This mesh contains only linear hexahedral cells, type
:attr:`pyvista.CellType.HEXAHEDRON`, which evaluates to 12.
>>> import pyvista as pv
>>> from pyvista import examples
>>> hex_beam = examples.load_hexbeam()
>>> hex_beam.celltypes
array([12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12], dtype=uint8)
"""
return _vtk.vtk_to_numpy(self._get_cell_types_array())
def _get_cell_types_array(self):
array = self.GetCellTypesArray()
if array is None:
array = _vtk.vtkUnsignedCharArray()
return array
@property
def offset(self) -> NumpyArray[float]: # numpydoc ignore=RT01
"""Return the cell locations array.
This is the location of the start of each cell in
:attr:`cell_connectivity`.
Returns
-------
numpy.ndarray
Array of cell offsets indicating the start of each cell.
Notes
-----
The array returned is immutable and cannot be written to. If you
need to modify this array, create a copy of it using
:func:`numpy.copy`.
Examples
--------
Return the cell offset array. Since this mesh is composed of
all hexahedral cells, note how each cell starts at 8 greater
than the prior cell.
>>> import pyvista as pv
>>> from pyvista import examples
>>> hex_beam = pv.read(examples.hexbeamfile)
>>> hex_beam.offset
array([ 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96,
104, 112, 120, 128, 136, 144, 152, 160, 168, 176, 184, 192, 200,
208, 216, 224, 232, 240, 248, 256, 264, 272, 280, 288, 296, 304,
312, 320])
"""
carr = self._get_cells()
# This will be the number of cells + 1.
array = _vtk.vtk_to_numpy(carr.GetOffsetsArray())
array.flags['WRITEABLE'] = False
return array
def cast_to_explicit_structured_grid(self):
"""Cast to an explicit structured grid.
Returns
-------
pyvista.ExplicitStructuredGrid
An explicit structured grid.
Raises
------
TypeError
If the unstructured grid doesn't have the ``'BLOCK_I'``,
``'BLOCK_J'`` and ``'BLOCK_K'`` cells arrays.
See Also
--------
pyvista.ExplicitStructuredGrid.cast_to_unstructured_grid
Examples
--------
>>> from pyvista import examples
>>> grid = examples.load_explicit_structured()
>>> grid.plot(color='w', show_edges=True, show_bounds=True)
>>> grid = grid.hide_cells(range(80, 120))
>>> grid.plot(color='w', show_edges=True, show_bounds=True)
>>> grid = grid.cast_to_unstructured_grid()
>>> grid.plot(color='w', show_edges=True, show_bounds=True)
>>> grid = grid.cast_to_explicit_structured_grid()
>>> grid.plot(color='w', show_edges=True, show_bounds=True)
"""
s1 = {'BLOCK_I', 'BLOCK_J', 'BLOCK_K'}
s2 = self.cell_data.keys()
if not s1.issubset(s2):
msg = "'BLOCK_I', 'BLOCK_J' and 'BLOCK_K' cell arrays are required"
raise TypeError(msg)
alg = _vtk.vtkUnstructuredGridToExplicitStructuredGrid()
alg.SetInputData(self)
alg.SetInputArrayToProcess(0, 0, 0, 1, 'BLOCK_I')
alg.SetInputArrayToProcess(1, 0, 0, 1, 'BLOCK_J')
alg.SetInputArrayToProcess(2, 0, 0, 1, 'BLOCK_K')
alg.Update()
grid = _get_output(alg)
grid.cell_data.remove('ConnectivityFlags') # unrequired
return grid
class StructuredGrid(PointGrid, StructuredGridFilters, _vtk.vtkStructuredGrid):
"""Dataset used for topologically regular arrays of data.
Can be initialized in one of the following several ways:
* Create empty grid.
* Initialize from a filename.
* Initialize from a :vtk:`vtkStructuredGrid` object.
* Initialize directly from one or more :class:`numpy.ndarray`. See the
example or the documentation of ``uinput``.
Parameters
----------
uinput : str, Path, :vtk:`vtkStructuredGrid`, numpy.ndarray, optional
Filename, dataset, or array to initialize the structured grid from. If
a filename is passed, pyvista will attempt to load it as a
:class:`StructuredGrid`. If passed a :vtk:`vtkStructuredGrid`, it will
be wrapped as a deep copy.
If a :class:`numpy.ndarray` is provided and ``y`` and ``z`` are empty,
this array will define the points of this :class:`StructuredGrid`.
Set the dimensions with :attr:`StructuredGrid.dimensions`.
Otherwise, this parameter will be loaded as the ``x`` points, and ``y``
and ``z`` points must be set. The shape of this array defines the shape
of the structured data and the shape should be ``(dimx, dimy,
dimz)``. Missing trailing dimensions are assumed to be ``1``.
y : numpy.ndarray, optional
Coordinates of the points in y direction. If this is passed, ``uinput``
must be a :class:`numpy.ndarray` and match the shape of ``y``.
z : numpy.ndarray, optional
Coordinates of the points in z direction. If this is passed, ``uinput``
and ``y`` must be a :class:`numpy.ndarray` and match the shape of ``z``.
deep : optional
Whether to deep copy a StructuredGrid object.
Default is ``False``. Keyword only.
**kwargs : dict, optional
Additional keyword arguments passed when reading from a file or loading
from arrays.
See Also
--------
:ref:`create_structured_surface_example`
Examples
--------
>>> import pyvista as pv
>>> import vtk
>>> import numpy as np
Create an empty structured grid.
>>> grid = pv.StructuredGrid()
Initialize from a :vtk:`vtkStructuredGrid` object
>>> vtkgrid = vtk.vtkStructuredGrid()
>>> grid = pv.StructuredGrid(vtkgrid)
Create from NumPy arrays using :func:`numpy.meshgrid`.
>>> xrng = np.linspace(-5, 5, 10)
>>> yrng = np.linspace(-8, 8, 4)
>>> zrng = np.linspace(-7, 4, 20)
>>> x, y, z = np.meshgrid(xrng, yrng, zrng, indexing='ij')
>>> grid = pv.StructuredGrid(x, y, z)
>>> grid
StructuredGrid (...)
N Cells: 513
N Points: 800
X Bounds: -5.000e+00, 5.000e+00
Y Bounds: -8.000e+00, 8.000e+00
Z Bounds: -7.000e+00, 4.000e+00
Dimensions: 10, 4, 20
N Arrays: 0
Note how the grid dimensions match the shape of the input arrays.
>>> (xrng.size, yrng.size, zrng.size)
(10, 4, 20)
"""
_WRITERS: ClassVar[
dict[str, type[_vtk.vtkStructuredGridWriter | _vtk.vtkXMLStructuredGridWriter]]
] = {'.vtk': _vtk.vtkStructuredGridWriter, '.vts': _vtk.vtkXMLStructuredGridWriter} # type: ignore[assignment]
def __init__(self, uinput=None, y=None, z=None, *args, deep: bool = False, **kwargs) -> None:
"""Initialize the structured grid."""
super().__init__()
if args:
msg = 'Too many args to create StructuredGrid.'
raise ValueError(msg)
if isinstance(uinput, _vtk.vtkStructuredGrid):
if deep:
self.deep_copy(uinput)
else:
self.shallow_copy(uinput) # type: ignore[arg-type]
elif isinstance(uinput, (str, Path)):
self._from_file(uinput, **kwargs)
elif (
isinstance(uinput, np.ndarray)
and isinstance(y, np.ndarray)
and isinstance(z, np.ndarray)
):
self._from_arrays(uinput, y, z, **kwargs)
elif isinstance(uinput, np.ndarray) and y is None and z is None:
self.points = uinput
elif uinput is None:
# do nothing, initialize as empty structured grid
pass
else:
msg = (
'Invalid parameters. Expecting one of the following:\n'
' - No arguments\n'
' - Filename as the only argument\n'
' - StructuredGrid as the only argument\n'
' - Single `numpy.ndarray` as the only argument'
' - Three `numpy.ndarray` as the first three arguments'
)
raise TypeError(msg)
def __repr__(self):
"""Return the standard representation."""
return DataSet.__repr__(self)
def __str__(self):
"""Return the standard str representation."""
return DataSet.__str__(self)
def _from_arrays(self, x, y, z, *, force_float: bool = True):
"""Create VTK structured grid directly from numpy arrays.
Parameters
----------
x : numpy.ndarray
Position of the points in x direction.
y : numpy.ndarray
Position of the points in y direction.
z : numpy.ndarray
Position of the points in z direction.
force_float : bool, optional
Casts the datatype to ``float32`` if points datatype is
non-float. Default ``True``. Set this to ``False`` to allow
non-float types, though this may lead to truncation of
intermediate floats when transforming datasets.
"""
if not (x.shape == y.shape == z.shape):
msg = 'Input point array shapes must match exactly'
raise ValueError(msg)
# make the output points the same precision as the input arrays
points = np.empty((x.size, 3), x.dtype)
points[:, 0] = x.ravel('F')
points[:, 1] = y.ravel('F')
points[:, 2] = z.ravel('F')
# ensure that the inputs are 3D
dim = list(x.shape)
while len(dim) < 3:
dim.append(1)
# Create structured grid
self.SetDimensions(dim)
self.SetPoints(vtk_points(points, force_float=force_float))
@property
def dimensions(self): # numpydoc ignore=RT01
"""Return a length 3 tuple of the grid's dimensions.
Returns
-------
tuple
Grid dimensions.
Examples
--------
>>> import pyvista as pv
>>> import numpy as np
>>> xrng = np.arange(-10, 10, 1, dtype=np.float32)
>>> yrng = np.arange(-10, 10, 2, dtype=np.float32)
>>> zrng = np.arange(-10, 10, 5, dtype=np.float32)
>>> x, y, z = np.meshgrid(xrng, yrng, zrng, indexing='ij')
>>> grid = pv.StructuredGrid(x, y, z)
>>> grid.dimensions
(20, 10, 4)
"""
dims = [0, 0, 0]
self.GetDimensions(dims)
return tuple(dims)
@dimensions.setter
def dimensions(self, dims) -> None:
nx, ny, nz = dims[0], dims[1], dims[2]
self.SetDimensions(nx, ny, nz)
self.Modified()
@property
def dimensionality(self) -> int:
"""Return the dimensionality of the grid.
Returns
-------
int
The grid dimensionality.
Examples
--------
>>> import pyvista as pv
>>> import numpy as np
>>> xrng = np.arange(-10, 10, 1, dtype=np.float32)
>>> yrng = np.arange(-10, 10, 2, dtype=np.float32)
>>> zrng = np.arange(-10, 10, 5, dtype=np.float32)
>>> x, y, z = np.meshgrid(xrng, yrng, zrng, indexing='ij')
>>> grid = pv.StructuredGrid(x, y, z)
>>> grid.dimensionality
3
"""
dims = np.asarray(self.dimensions)
return int(3 - (dims == 1).sum())
@property
def x(self): # numpydoc ignore=RT01
"""Return the X coordinates of all points.
Returns
-------
numpy.ndarray
Numpy array of all X coordinates.
Examples
--------
>>> import pyvista as pv
>>> import numpy as np
>>> xrng = np.arange(-10, 10, 1, dtype=np.float32)
>>> yrng = np.arange(-10, 10, 2, dtype=np.float32)
>>> zrng = np.arange(-10, 10, 5, dtype=np.float32)
>>> x, y, z = np.meshgrid(xrng, yrng, zrng, indexing='ij')
>>> grid = pv.StructuredGrid(x, y, z)
>>> grid.x.shape
(20, 10, 4)
"""
return self._reshape_point_array(self.points[:, 0])
@property
def y(self): # numpydoc ignore=RT01
"""Return the Y coordinates of all points."""
return self._reshape_point_array(self.points[:, 1])
@property
def z(self): # numpydoc ignore=RT01
"""Return the Z coordinates of all points."""
return self._reshape_point_array(self.points[:, 2])
@property
def points_matrix(self): # numpydoc ignore=RT01
"""Points as a 4-D matrix, with x/y/z along the last dimension."""
return self.points.reshape((*self.dimensions, 3), order='F')
def _get_attrs(self):
"""Return the representation methods (internal helper)."""
attrs = PointGrid._get_attrs(self)
attrs.append(('Dimensions', self.dimensions, '{:d}, {:d}, {:d}'))
return attrs
def __getitem__(self, key):
"""Slice subsets of the StructuredGrid, or extract an array field."""
# legacy behavior which looks for a point or cell array
if not isinstance(key, tuple):
return super().__getitem__(key)
# convert slice to VOI specification - only "basic indexing" is supported
voi = [] # type: ignore[var-annotated]
rate = []
if len(key) != 3:
msg = 'Slices must have exactly 3 dimensions.'
raise RuntimeError(msg)
for i, k in enumerate(key):
if isinstance(k, Iterable):
msg = 'Fancy indexing with iterable is not supported.'
raise TypeError(msg)
if isinstance(k, numbers.Integral):
start = stop = k
step = 1
elif isinstance(k, slice):
start = k.start if k.start is not None else 0 # type: ignore[assignment]
stop = k.stop - 1 if k.stop is not None else self.dimensions[i]
step = k.step if k.step is not None else 1
voi.extend((start, stop))
rate.append(step)
return self.extract_subset(voi, rate, boundary=False)
@_deprecate_positional_args(allowed=['ind'])
def hide_cells(self, ind, inplace: bool = False): # noqa: FBT001, FBT002
"""Hide cells without deleting them.
Hides cells by setting the ghost_cells array to ``HIDDEN_CELL``.
Parameters
----------
ind : sequence[int]
List or array of cell indices to be hidden. The array can
also be a boolean array of the same size as the number of
cells.
inplace : bool, default: False
Updates mesh in-place.
Returns
-------
pyvista.StructuredGrid
Structured grid with hidden cells.
Examples
--------
Hide part of the middle of a structured surface.
>>> import pyvista as pv
>>> import numpy as np
>>> x = np.arange(-10, 10, 0.25)
>>> y = np.arange(-10, 10, 0.25)
>>> z = 0
>>> x, y, z = np.meshgrid(x, y, z)
>>> grid = pv.StructuredGrid(x, y, z)
>>> grid = grid.hide_cells(range(79 * 30, 79 * 50))
>>> grid.plot(color=True, show_edges=True)
"""
if not inplace:
return self.copy().hide_cells(ind, inplace=True)
if isinstance(ind, np.ndarray):
if ind.dtype == np.bool_ and ind.size != self.n_cells:
msg = f'Boolean array size must match the number of cells ({self.n_cells})'
raise ValueError(msg)
ghost_cells = np.zeros(self.n_cells, np.uint8)
ghost_cells[ind] = _vtk.vtkDataSetAttributes.HIDDENCELL
# NOTE: cells cannot be removed from a structured grid, only
# hidden setting ghost_cells to a value besides
# vtk.vtkDataSetAttributes.HIDDENCELL will not hide them
# properly, additionally, calling self.RemoveGhostCells will
# have no effect
# add but do not make active
self.cell_data.set_array(ghost_cells, _vtk.vtkDataSetAttributes.GhostArrayName()) # type: ignore[arg-type]
return self
def hide_points(self, ind: VectorLike[bool] | VectorLike[int]) -> None:
"""Hide points without deleting them.
Hides points by setting the ghost_points array to ``HIDDEN_CELL``.
Parameters
----------
ind : VectorLike[bool] | VectorLike[int]
Vector of point indices to be hidden. The vector can also be a
boolean array of the same size as the number of points.
Examples
--------
Hide part of the middle of a structured surface.
>>> import pyvista as pv
>>> import numpy as np
>>> x = np.arange(-10, 10, 0.25)
>>> y = np.arange(-10, 10, 0.25)
>>> z = 0
>>> x, y, z = np.meshgrid(x, y, z)
>>> grid = pv.StructuredGrid(x, y, z)
>>> grid.hide_points(range(80 * 30, 80 * 50))
>>> grid.plot(color=True, show_edges=True)
"""
if isinstance(ind, np.ndarray):
if ind.dtype == np.bool_ and ind.size != self.n_points:
msg = f'Boolean array size must match the number of points ({self.n_points})'
raise ValueError(msg)
ghost_points = np.zeros(self.n_points, np.uint8)
ghost_points[ind] = _vtk.vtkDataSetAttributes.HIDDENPOINT
# add but do not make active
self.point_data.set_array(ghost_points, _vtk.vtkDataSetAttributes.GhostArrayName()) # type: ignore[arg-type]
def cast_to_explicit_structured_grid(self) -> ExplicitStructuredGrid:
"""Cast to an explicit structured grid.
Returns
-------
pyvista.ExplicitStructuredGrid
An explicit structured grid.
Raises
------
TypeError
If the structured grid is not 3D (i.e., any dimension is 1).
"""
if any(n == 1 for n in self.dimensions):
msg = 'Only 3D structured grid can be casted to an explicit structured grid.'
raise TypeError(msg)
ni, nj, nk = self.dimensions
grid = self.cast_to_unstructured_grid()
s1 = {'BLOCK_I', 'BLOCK_J', 'BLOCK_K'}
if not s1.issubset(self.cell_data):
i, j, k = np.unravel_index(
np.arange(self.n_cells),
shape=(ni - 1, nj - 1, nk - 1),
order='F',
)
grid.cell_data['BLOCK_I'] = i
grid.cell_data['BLOCK_J'] = j
grid.cell_data['BLOCK_K'] = k
grid = grid.cast_to_explicit_structured_grid()
if not s1.issubset(self.cell_data):
for key in s1:
grid.cell_data.pop(key, None)
return grid
def _reshape_point_array(self, array: NumpyArray[float]) -> NumpyArray[float]:
"""Reshape point data to a 3-D matrix."""
return array.reshape(self.dimensions, order='F')
def _reshape_cell_array(self, array: NumpyArray[float]) -> NumpyArray[float]:
"""Reshape cell data to a 3-D matrix."""
cell_dims = np.array(self.dimensions) - 1
cell_dims[cell_dims == 0] = 1
return array.reshape(cell_dims, order='F')
class ExplicitStructuredGrid(PointGrid, _vtk.vtkExplicitStructuredGrid):
"""Extend the functionality of the :vtk:`vtkExplicitStructuredGrid` class.
Can be initialized by the following:
- Creating an empty grid
- From a :vtk:`vtkStructuredGrid`, :vtk:`vtkExplicitStructuredGrid` or
:vtk:`vtkUnstructuredGrid` object
- From a VTU or VTK file
- From ``dims`` and ``corners`` arrays
- From ``dims``, ``cells`` and ``points`` arrays
Parameters
----------
args : :vtk:`vtkExplicitStructuredGrid`, :vtk:`vtkUnstructuredGrid`, str, Sequence
See examples below.
deep : bool, default: False
Whether to deep copy a :vtk:`vtkUnstructuredGrid` object.
See Also
--------
:ref:`create_explicit_structured_grid_example`
Examples
--------
>>> import numpy as np
>>> import pyvista as pv
>>>
>>> # grid size: ni*nj*nk cells; si, sj, sk steps
>>> ni, nj, nk = 4, 5, 6
>>> si, sj, sk = 20, 10, 1
>>>
>>> # create raw coordinate grid
>>> grid_ijk = np.mgrid[
... : (ni + 1) * si : si,
... : (nj + 1) * sj : sj,
... : (nk + 1) * sk : sk,
... ]
>>>
>>> # repeat array along each Cartesian axis for connectivity
>>> for axis in range(1, 4):
... grid_ijk = grid_ijk.repeat(2, axis=axis)
>>>
>>> # slice off unnecessarily doubled edge coordinates
>>> grid_ijk = grid_ijk[:, 1:-1, 1:-1, 1:-1]
>>>
>>> # reorder and reshape to VTK order
>>> corners = grid_ijk.transpose().reshape(-1, 3)
>>>
>>> dims = np.array([ni, nj, nk]) + 1
>>> grid = pv.ExplicitStructuredGrid(dims, corners)
>>> grid = grid.compute_connectivity()
>>> grid.plot(show_edges=True)
"""
_WRITERS: ClassVar[
dict[
str,
type[_vtk.vtkXMLUnstructuredGridWriter | _vtk.vtkUnstructuredGridWriter],
]
] = {'.vtu': _vtk.vtkXMLUnstructuredGridWriter, '.vtk': _vtk.vtkUnstructuredGridWriter} # type: ignore[assignment]
def __init__(self, *args, deep: bool = False, **kwargs): # noqa: ARG002
"""Initialize the explicit structured grid."""
super().__init__()
n = len(args)
if n > 3:
msg = 'Too many args to create ExplicitStructuredGrid.'
raise ValueError(msg)
if n == 1:
arg0 = args[0]
if isinstance(arg0, _vtk.vtkExplicitStructuredGrid):
if deep:
self.deep_copy(arg0)
else:
self.shallow_copy(arg0) # type: ignore[arg-type]
elif isinstance(arg0, (_vtk.vtkStructuredGrid, _vtk.vtkUnstructuredGrid)):
grid = arg0.cast_to_explicit_structured_grid() # type: ignore[union-attr]
self.shallow_copy(grid)
elif isinstance(arg0, (str, Path)):
grid = UnstructuredGrid(arg0)
grid = grid.cast_to_explicit_structured_grid()
self.shallow_copy(grid)
elif n == 2:
arg0, arg1 = args
if isinstance(arg0, tuple):
arg0 = np.asarray(arg0)
if isinstance(arg1, list):
arg1 = np.asarray(arg1)
arg0_is_arr = isinstance(arg0, np.ndarray)
arg1_is_arr = isinstance(arg1, np.ndarray)
if all([arg0_is_arr, arg1_is_arr]):
self._from_arrays(arg0, arg1)
elif n == 3:
arg0, arg1, arg2 = args
arg0 = np.asarray(arg0)
arg1 = np.asarray(arg1) if not isinstance(arg1, dict) else arg1
arg2 = np.asarray(arg2)
self._from_cells_points(arg0, arg1, arg2)
def __repr__(self) -> str:
"""Return the standard representation."""
return DataSet.__repr__(self)
def __str__(self) -> str:
"""Return the standard ``str`` representation."""
return DataSet.__str__(self)
def _from_arrays(self, dims: VectorLike[int], corners: MatrixLike[float]) -> None:
"""Create a VTK explicit structured grid from NumPy arrays.
Parameters
----------
dims : VectorLike[int]
A sequence of integers with shape (3,) containing the
topological dimensions of the grid.
corners : MatrixLike[float]
A sequence of numbers with shape ``(number of corners, 3)``
containing the coordinates of the corner points.
"""
if len(dims) != 3:
msg = 'Expected dimensions to be length 3.'
raise ValueError(msg)
ni, nj, nk = np.asanyarray(dims) - 1
corners = np.reshape(corners, (2 * ni, 2 * nj, 2 * nk, 3), order='F')
points = np.column_stack(
[
np.column_stack(
(
corners_[::2, ::2, ::2].ravel(order='F'),
corners_[1::2, ::2, ::2].ravel(order='F'),
corners_[1::2, 1::2, ::2].ravel(order='F'),
corners_[::2, 1::2, ::2].ravel(order='F'),
corners_[::2, ::2, 1::2].ravel(order='F'),
corners_[1::2, ::2, 1::2].ravel(order='F'),
corners_[1::2, 1::2, 1::2].ravel(order='F'),
corners_[::2, 1::2, 1::2].ravel(order='F'),
)
).ravel()
for corners_ in corners.transpose((3, 0, 1, 2))
]
)
cells = np.arange(8 * ni * nj * nk).reshape((ni * nj * nk, 8))
self._from_cells_points(dims, {CellType.HEXAHEDRON: cells}, points)
def _from_cells_points(
self,
dims: VectorLike[int],
cells: VectorLike[int] | dict[int, MatrixLike[int]],
points: MatrixLike[float],
) -> None:
"""Create a VTK explicit structured grid from cells and points arrays.
Parameters
----------
dims : VectorLike[int]
A sequence of integers with shape (3,) containing the
topological dimensions of the grid.
cells : VectorLike[int] | dict[int, MatrixLike[int]]
Array of cells. Each cell contains the number of points in the
cell and the node numbers of the cell.
points : MatrixLike[float]
Numpy array containing point locations.
"""
if len(dims) != 3:
msg = 'Expected dimensions to be length 3.'
raise ValueError(msg)
else:
n_cells = np.prod([n - 1 for n in dims]) # type: ignore[arg-type]
if isinstance(cells, dict):
celltypes = list(cells)
if not (len(celltypes) == 1 and celltypes[0] == CellType.HEXAHEDRON):
msg = f'Expected cells to be a single cell of type {CellType.HEXAHEDRON}.'
raise ValueError(msg)
cells = np.asarray(cells[celltypes[0]])
if cells.shape != (n_cells, 8):
msg = f'Expected cells to be of shape ({n_cells}, 8)'
raise ValueError(msg)
cells = np.column_stack((np.full(n_cells, 8), cells)).flatten()
elif len(cells) != 9 * n_cells:
msg = f'Expected cells to be length {9 * n_cells}'
raise ValueError(msg)
self.SetDimensions(dims[0], dims[1], dims[2]) # type: ignore[arg-type]
self.SetCells(CellArray(cells))
self.SetPoints(vtk_points(points))
def cast_to_unstructured_grid(self) -> UnstructuredGrid:
"""Cast to an unstructured grid.
Returns
-------
UnstructuredGrid
An unstructured grid. VTK adds the ``'BLOCK_I'``,
``'BLOCK_J'`` and ``'BLOCK_K'`` cell arrays. These arrays
are required to restore the explicit structured grid.
See Also
--------
pyvista.DataSetFilters.extract_cells : Extract a subset of a dataset.
pyvista.UnstructuredGrid.cast_to_explicit_structured_grid
Cast an unstructured grid to an explicit structured grid.
Notes
-----
The ghost cell array is disabled before casting the
unstructured grid in order to allow the original structure
and attributes data of the explicit structured grid to be
restored. If you don't need to restore the explicit
structured grid later or want to extract an unstructured
grid from the visible subgrid, use the ``extract_cells``
filter and the cell indices where the ghost cell array is
``0``.
Examples
--------
>>> from pyvista import examples
>>> grid = examples.load_explicit_structured()
>>> grid.plot(color='w', show_edges=True, show_bounds=True)
>>> grid = grid.hide_cells(range(80, 120))
>>> grid.plot(color='w', show_edges=True, show_bounds=True)
>>> grid = grid.cast_to_unstructured_grid()
>>> grid.plot(color='w', show_edges=True, show_bounds=True)
>>> grid = grid.cast_to_explicit_structured_grid()
>>> grid.plot(color='w', show_edges=True, show_bounds=True)
"""
grid = ExplicitStructuredGrid()
grid.copy_structure(self)
alg = _vtk.vtkExplicitStructuredGridToUnstructuredGrid()
alg.SetInputDataObject(grid)
alg.Update()
ugrid = _get_output(alg)
ugrid.cell_data.remove('vtkOriginalCellIds') # unrequired
ugrid.copy_attributes(self) # copy ghost cell array and other arrays
return ugrid
@_deprecate_positional_args
def clean( # noqa: PLR0917
self,
tolerance=0,
remove_unused_points: bool = True, # noqa: FBT001, FBT002
produce_merge_map: bool = True, # noqa: FBT001, FBT002
average_point_data: bool = True, # noqa: FBT001, FBT002
merging_array_name=None,
progress_bar: bool = False, # noqa: FBT001, FBT002
) -> ExplicitStructuredGrid:
"""Merge duplicate points and remove unused points in an ExplicitStructuredGrid.
This filter, merging coincident points as defined by a merging
tolerance and optionally removes unused points. The filter does not
modify the topology of the input dataset, nor change the types of
cells. It may however, renumber the cell connectivity ids.
This filter casts the grid to an UnstructuredGrid to clean it, then
casts the cleaned unstructured grid to an explicit structured grid.
Parameters
----------
tolerance : float, default: 0.0
The absolute point merging tolerance.
remove_unused_points : bool, default: True
Indicate whether points unused by any cell are removed from the
output. Note that when this is off, the filter can successfully
process datasets with no cells (and just points). If on in this
case, and there are no cells, the output will be empty.
produce_merge_map : bool, default: False
Indicate whether a merge map should be produced on output.
The merge map, if requested, maps each input point to its
output point id, or provides a value of -1 if the input point
is not used in the output. The merge map is associated with
the filter's output field data and is named ``"PointMergeMap"``.
average_point_data : bool, default: True
Indicate whether point coordinates and point data of merged points
are averaged. When ``True``, the data coordinates and attribute
values of all merged points are averaged. When ``False``, the point
coordinate and data of the single remaining merged point is
retained.
merging_array_name : str, optional
If a ``merging_array_name`` is specified and exists in the
``point_data``, then point merging will switch into a mode where
merged points must be both geometrically coincident and have
matching point data. When set, ``tolerance`` has no effect.
progress_bar : bool, default: False
Display a progress bar to indicate progress.
Returns
-------
ExplicitStructuredGrid
Cleaned explicit structured grid.
"""
grid = (
self.cast_to_unstructured_grid()
.clean(
tolerance=tolerance,
remove_unused_points=remove_unused_points,
produce_merge_map=produce_merge_map,
average_point_data=average_point_data,
merging_array_name=merging_array_name,
progress_bar=progress_bar,
)
.cast_to_explicit_structured_grid()
)
s1 = {'BLOCK_I', 'BLOCK_J', 'BLOCK_K'}
if not s1.issubset(self.cell_data):
for key in s1:
grid.cell_data.pop(key, None)
return grid
@_deprecate_positional_args(allowed=['filename'])
def save(
self,
filename: Path | str,
binary: bool = True, # noqa: FBT001, FBT002
texture: NumpyArray[np.uint8] | str | None = None,
) -> None:
"""Save this VTK object to file.
Parameters
----------
filename : Path, str
Output file name. VTU and VTK extensions are supported.
binary : bool, default: True
If ``True``, write as binary, else ASCII.
texture : np.ndarray, str, None
Ignored argument. Kept to maintain compatibility with supertype.
Notes
-----
VTK adds the ``'BLOCK_I'``, ``'BLOCK_J'`` and ``'BLOCK_K'``
cell arrays. These arrays are required to restore the explicit
structured grid.
Examples
--------
>>> import pyvista as pv
>>> from pyvista import examples
>>> grid = examples.load_explicit_structured() # doctest:+SKIP
>>> grid = grid.hide_cells(range(80, 120)) # doctest:+SKIP
>>> grid.save('grid.vtu') # doctest:+SKIP
>>> grid = pv.ExplicitStructuredGrid('grid.vtu') # doctest:+SKIP
>>> grid.plot(color='w', show_edges=True, show_bounds=True) # doctest:+SKIP
>>> grid.show_cells() # doctest:+SKIP
>>> grid.plot(color='w', show_edges=True, show_bounds=True) # doctest:+SKIP
"""
if texture is not None:
msg = 'Cannot save texture of a pointset.'
raise ValueError(msg)
grid = self.cast_to_unstructured_grid()
grid.save(filename, binary=binary)
@_deprecate_positional_args(allowed=['ind'])
def hide_cells(self, ind: VectorLike[int], inplace: bool = False) -> ExplicitStructuredGrid: # noqa: FBT001, FBT002
"""Hide specific cells.
Hides cells by setting the ghost cell array to ``HIDDENCELL``.
Parameters
----------
ind : sequence[int]
Cell indices to be hidden. A boolean array of the same
size as the number of cells also is acceptable.
inplace : bool, default: False
This method is applied to this grid if ``True``
or to a copy otherwise.
Returns
-------
ExplicitStructuredGrid or None
A deep copy of this grid if ``inplace=False`` with the
hidden cells, or this grid with the hidden cells if
otherwise.
Examples
--------
>>> from pyvista import examples
>>> grid = examples.load_explicit_structured()
>>> grid = grid.hide_cells(range(80, 120))
>>> grid.plot(color='w', show_edges=True, show_bounds=True)
"""
ind_arr = np.asanyarray(ind)
if inplace:
array = np.zeros(self.n_cells, dtype=np.uint8)
array[ind_arr] = _vtk.vtkDataSetAttributes.HIDDENCELL
name = _vtk.vtkDataSetAttributes.GhostArrayName()
self.cell_data[name] = array
return self
grid = self.copy()
grid.hide_cells(ind, inplace=True)
return grid
@_deprecate_positional_args
def show_cells(self, inplace: bool = False) -> ExplicitStructuredGrid: # noqa: FBT001, FBT002
"""Show hidden cells.
Shows hidden cells by setting the ghost cell array to ``0``
where ``HIDDENCELL``.
Parameters
----------
inplace : bool, default: False
This method is applied to this grid if ``True``
or to a copy otherwise.
Returns
-------
ExplicitStructuredGrid
A deep copy of this grid if ``inplace=False`` with the
hidden cells shown. Otherwise, this dataset with the
shown cells.
Examples
--------
>>> from pyvista import examples
>>> grid = examples.load_explicit_structured()
>>> grid = grid.hide_cells(range(80, 120))
>>> grid.plot(color='w', show_edges=True, show_bounds=True)
>>> grid = grid.show_cells()
>>> grid.plot(color='w', show_edges=True, show_bounds=True)
"""
if inplace:
name = _vtk.vtkDataSetAttributes.GhostArrayName()
if name in self.cell_data.keys():
array = self.cell_data[name]
ind = np.argwhere(array == _vtk.vtkDataSetAttributes.HIDDENCELL)
array[ind] = 0
return self
else:
grid = self.copy()
grid.show_cells(inplace=True)
return grid
def _dimensions(self) -> tuple[int, int, int]:
# This method is required to avoid conflict if a developer extends `ExplicitStructuredGrid`
# and reimplements `dimensions` to return, for example, the number of cells in the I, J and
dims = np.reshape(self.GetExtent(), (3, 2)) # K directions.
dims = np.diff(dims, axis=1)
dims = dims.flatten() + 1 # type: ignore[assignment]
return int(dims[0]), int(dims[1]), int(dims[2])
@property
def dimensions(self) -> tuple[int, int, int]: # numpydoc ignore=RT01
"""Return the topological dimensions of the grid.
Returns
-------
tuple[int, int, int]
Number of sampling points in the I, J and Z directions respectively.
Examples
--------
>>> from pyvista import examples
>>> grid = examples.load_explicit_structured()
>>> grid.dimensions
(5, 6, 7)
"""
return self._dimensions()
@property
def dimensionality(self) -> int:
"""Return the dimensionality of the grid.
Returns
-------
int
The grid dimensionality.
Examples
--------
>>> from pyvista import examples
>>> grid = examples.load_explicit_structured()
>>> grid.dimensionality
3
"""
dims = np.asarray(self.dimensions)
return int(3 - (dims == 1).sum())
@property
def visible_bounds(self) -> BoundsTuple: # numpydoc ignore=RT01
"""Return the bounding box of the visible cells.
Different from `bounds`, which returns the bounding box of the
complete grid, this method returns the bounding box of the
visible cells, where the ghost cell array is not
``HIDDENCELL``.
Returns
-------
tuple[float, float, float]
The limits of the visible grid in the X, Y and Z
directions respectively.
Examples
--------
>>> from pyvista import examples
>>> grid = examples.load_explicit_structured()
>>> grid = grid.hide_cells(range(80, 120))
>>> grid.bounds
BoundsTuple(x_min = 0.0,
x_max = 80.0,
y_min = 0.0,
y_max = 50.0,
z_min = 0.0,
z_max = 6.0)
>>> grid.visible_bounds
BoundsTuple(x_min = 0.0,
x_max = 80.0,
y_min = 0.0,
y_max = 50.0,
z_min = 0.0,
z_max = 4.0)
"""
name = _vtk.vtkDataSetAttributes.GhostArrayName()
if name in self.cell_data:
array = self.cell_data[name]
grid = self.extract_cells(array == 0)
return grid.bounds
else:
return self.bounds
def cell_id(self, coords: ArrayLike[int]) -> int | NumpyArray[int] | None:
"""Return the cell ID.
Parameters
----------
coords : ArrayLike[int]
Cell structured coordinates.
Returns
-------
int | numpy.ndarray | None
Cell IDs. ``None`` if ``coords`` is outside the grid extent.
See Also
--------
pyvista.ExplicitStructuredGrid.cell_coords : Return the cell structured coordinates.
Examples
--------
>>> from pyvista import examples
>>> grid = examples.load_explicit_structured()
>>> grid.cell_id((3, 4, 0))
np.int64(19)
>>> coords = [(3, 4, 0), (3, 2, 1), (1, 0, 2), (2, 3, 2)]
>>> grid.cell_id(coords)
array([19, 31, 41, 54])
"""
# `vtk.vtkExplicitStructuredGrid.ComputeCellId` is not used
# here because this method returns invalid cell IDs when
# `coords` is outside the grid extent.
if isinstance(coords, Sequence):
coords = np.asarray(coords)
if coords.ndim == 2:
ncol = coords.shape[1]
coords = [coords[:, c] for c in range(ncol)]
coords = tuple(coords)
dims = self._dimensions()
try:
ind = np.ravel_multi_index(coords, np.array(dims) - 1, order='F')
except ValueError:
return None
else:
return ind
def cell_coords(
self,
ind: int | VectorLike[int],
) -> None | MatrixLike[int]:
"""Return the cell structured coordinates.
Parameters
----------
ind : int | VectorLike[int]
Cell IDs.
Returns
-------
numpy.ndarray | None
Cell structured coordinates. ``None`` if ``ind`` is
outside the grid extent.
See Also
--------
pyvista.ExplicitStructuredGrid.cell_id : Return the cell ID.
Examples
--------
>>> from pyvista import examples
>>> grid = examples.load_explicit_structured()
>>> grid.cell_coords(19)
array([3, 4, 0])
>>> grid.cell_coords((19, 31, 41, 54))
array([[3, 4, 0],
[3, 2, 1],
[1, 0, 2],
[2, 3, 2]])
"""
dims = self._dimensions()
try:
coords = np.unravel_index(ind, np.array(dims) - 1, order='F')
except ValueError:
return None
else:
if isinstance(coords[0], np.ndarray):
return np.stack(coords, axis=1)
return np.asanyarray(coords) # type: ignore[unreachable]
def neighbors(self, ind: int | VectorLike[int], rel: str = 'connectivity') -> list[int]:
"""Return the indices of neighboring cells.
Parameters
----------
ind : int | VectorLike[int]
Cell IDs.
rel : str, default: "connectivity"
Defines the neighborhood relationship. If
``'topological'``, returns the ``(i-1, j, k)``, ``(i+1, j,
k)``, ``(i, j-1, k)``, ``(i, j+1, k)``, ``(i, j, k-1)``
and ``(i, j, k+1)`` cells. If ``'connectivity'``
(default), returns only the topological neighbors
considering faces connectivity. If ``'geometric'``,
returns the cells in the ``(i-1, j)``, ``(i+1, j)``,
``(i,j-1)`` and ``(i, j+1)`` vertical cell groups whose
faces intersect.
Returns
-------
list[int]
Indices of neighboring cells.
Examples
--------
>>> import pyvista as pv
>>> from pyvista import examples
>>> grid = examples.load_explicit_structured()
>>> cell = grid.extract_cells(31)
>>> ind = grid.neighbors(31)
>>> neighbors = grid.extract_cells(ind)
>>> plotter = pv.Plotter()
>>> _ = plotter.add_axes()
>>> _ = plotter.add_mesh(cell, color='r', show_edges=True)
>>> _ = plotter.add_mesh(neighbors, color='w', show_edges=True)
>>> plotter.show()
"""
def connectivity(ind):
indices = []
cell_coords = self.cell_coords(ind)
cell_points = self.get_cell(ind).points
if cell_points.shape[0] == 8:
faces = [
[(-1, 0, 0), (0, 4, 7, 3), (1, 5, 6, 2)],
[(+1, 0, 0), (1, 2, 6, 5), (0, 3, 7, 4)],
[(0, -1, 0), (0, 1, 5, 4), (3, 2, 6, 7)],
[(0, +1, 0), (3, 7, 6, 2), (0, 4, 5, 1)],
[(0, 0, -1), (0, 3, 2, 1), (4, 7, 6, 5)],
[(0, 0, +1), (4, 5, 6, 7), (0, 1, 2, 3)],
]
for f in faces:
coords = np.sum([cell_coords, f[0]], axis=0) # type: ignore[arg-type]
ind = self.cell_id(coords)
if ind:
points = self.get_cell(ind).points
if points.shape[0] == 8:
a1 = cell_points[f[1], :]
a2 = points[f[2], :]
if np.array_equal(a1, a2):
indices.append(ind)
return indices
def topological(ind):
indices = []
cell_coords = self.cell_coords(ind)
cell_neighbors = [(-1, 0, 0), (1, 0, 0), (0, -1, 0), (0, 1, 0), (0, 0, -1), (0, 0, 1)]
for n in cell_neighbors:
coords = np.sum([cell_coords, n], axis=0) # type: ignore[arg-type]
ind = self.cell_id(coords)
if ind:
indices.append(ind)
return indices
def geometric(ind):
indices = []
cell_coords = self.cell_coords(ind)
cell_points = self.get_cell(ind).points
if cell_points.shape[0] == 8:
for k in [-1, 1]:
coords = np.sum([cell_coords, (0, 0, k)], axis=0) # type: ignore[arg-type]
ind = self.cell_id(coords)
if ind:
indices.append(ind)
faces = [
[(-1, 0, 0), (0, 4, 3, 7), (1, 5, 2, 6)],
[(+1, 0, 0), (2, 6, 1, 5), (3, 7, 0, 4)],
[(0, -1, 0), (1, 5, 0, 4), (2, 6, 3, 7)],
[(0, +1, 0), (3, 7, 2, 6), (0, 4, 1, 5)],
]
nk = self.dimensions[2]
for f in faces:
cell_z = cell_points[f[1], 2]
cell_z = np.abs(cell_z)
cell_z = cell_z.reshape((2, 2))
cell_zmin = cell_z.min(axis=1)
cell_zmax = cell_z.max(axis=1)
coords = np.sum([cell_coords, f[0]], axis=0) # type: ignore[arg-type]
for k in range(nk):
coords[2] = k
ind = self.cell_id(coords)
if ind:
points = self.get_cell(ind).points
if points.shape[0] == 8:
z = points[f[2], 2]
z = np.abs(z)
z = z.reshape((2, 2))
zmin = z.min(axis=1)
zmax = z.max(axis=1)
if (
(zmax[0] > cell_zmin[0] and zmin[0] < cell_zmax[0])
or (zmax[1] > cell_zmin[1] and zmin[1] < cell_zmax[1])
or (zmin[0] > cell_zmax[0] and zmax[1] < cell_zmin[1])
or (zmin[1] > cell_zmax[1] and zmax[0] < cell_zmin[0])
):
indices.append(ind)
return indices
if isinstance(ind, int):
ind = [ind]
rel_map = {
'connectivity': connectivity,
'geometric': geometric,
'topological': topological,
}
if rel not in rel_map:
msg = (
f'Invalid value for `rel` of {rel}. Should be one of the '
f'following\n{rel_map.keys()}'
)
raise ValueError(msg)
rel_func = rel_map[rel]
indices = set()
for i in ind:
indices.update(rel_func(i))
return sorted(indices)
@_deprecate_positional_args
def compute_connectivity(self, inplace: bool = False) -> ExplicitStructuredGrid: # noqa: FBT001, FBT002
"""Compute the faces connectivity flags array.
This method checks the faces connectivity of the cells with
their topological neighbors. The result is stored in the
array of integers ``'ConnectivityFlags'``. Each value in this
array must be interpreted as a binary number, where the digits
shows the faces connectivity of a cell with its topological
neighbors -Z, +Z, -Y, +Y, -X and +X respectively. For example,
a cell with ``'ConnectivityFlags'`` equal to ``27``
(``011011``) indicates that this cell is connected by faces
with their neighbors ``(0, 0, 1)``, ``(0, -1, 0)``,
``(-1, 0, 0)`` and ``(1, 0, 0)``.
Parameters
----------
inplace : bool, default: False
This method is applied to this grid if ``True``
or to a copy otherwise.
Returns
-------
ExplicitStructuredGrid
A deep copy of this grid if ``inplace=False``, or this
DataSet if otherwise.
See Also
--------
ExplicitStructuredGrid.compute_connections
Compute an array with the number of connected cell faces.
Examples
--------
>>> from pyvista import examples
>>>
>>> grid = examples.load_explicit_structured()
>>> grid = grid.compute_connectivity()
>>> grid.plot(show_edges=True)
"""
if inplace:
self.ComputeFacesConnectivityFlagsArray()
return self
else:
grid = self.copy()
grid.compute_connectivity(inplace=True)
return grid
@_deprecate_positional_args
def compute_connections(self, inplace: bool = False): # noqa: FBT001, FBT002
"""Compute an array with the number of connected cell faces.
This method calculates the number of topological cell
neighbors connected by faces. The results are stored in the
``'number_of_connections'`` cell array.
Parameters
----------
inplace : bool, default: False
This method is applied to this grid if ``True`` or to a copy
otherwise.
Returns
-------
ExplicitStructuredGrid
A deep copy of this grid if ``inplace=False`` or this
DataSet if otherwise.
See Also
--------
ExplicitStructuredGrid.compute_connectivity : Compute the faces connectivity flags array.
Examples
--------
>>> from pyvista import examples
>>> grid = examples.load_explicit_structured()
>>> grid = grid.compute_connections()
>>> grid.plot(show_edges=True)
"""
if inplace:
if 'ConnectivityFlags' in self.cell_data:
array = self.cell_data['ConnectivityFlags']
else:
grid = self.compute_connectivity(inplace=False)
array = grid.cell_data['ConnectivityFlags']
array = array.reshape((-1, 1)) # type: ignore[assignment]
array = array.astype(np.uint8) # type: ignore[assignment]
array = np.unpackbits(array, axis=1) # type: ignore[assignment]
array = array.sum(axis=1)
self.cell_data['number_of_connections'] = array
return self
else:
return self.copy().compute_connections(inplace=True)
|