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
|
"""Attributes common to PolyData and Grid Objects."""
from __future__ import annotations
from collections.abc import Iterable
from collections.abc import Sequence
from copy import deepcopy
from functools import partial
from typing import TYPE_CHECKING
from typing import Any
from typing import Literal
from typing import NamedTuple
from typing import cast
from typing import overload
import warnings
import numpy as np
import pyvista
from pyvista._deprecate_positional_args import _deprecate_positional_args
from pyvista.typing.mypy_plugin import promote_type
from . import _vtk_core as _vtk
from ._typing_core import BoundsTuple
from .dataobject import DataObject
from .datasetattributes import DataSetAttributes
from .errors import PyVistaDeprecationWarning
from .errors import VTKVersionError
from .filters import DataSetFilters
from .filters import _get_output
from .pyvista_ndarray import pyvista_ndarray
from .utilities.arrays import CellLiteral
from .utilities.arrays import FieldAssociation
from .utilities.arrays import FieldLiteral
from .utilities.arrays import PointLiteral
from .utilities.arrays import _coerce_pointslike_arg
from .utilities.arrays import get_array
from .utilities.arrays import get_array_association
from .utilities.arrays import raise_not_matching
from .utilities.arrays import vtk_id_list_to_array
from .utilities.helpers import is_pyvista_dataset
from .utilities.misc import _NoNewAttrMixin
from .utilities.misc import abstract_class
from .utilities.points import vtk_points
if TYPE_CHECKING:
from collections.abc import Callable
from collections.abc import Generator
from collections.abc import Iterator
from typing_extensions import Self
from ._typing_core import MatrixLike
from ._typing_core import NumpyArray
from ._typing_core import VectorLike
# vector array names
DEFAULT_VECTOR_KEY = '_vectors'
class ActiveArrayInfoTuple(NamedTuple):
"""Active array info tuple.
Parameters
----------
association : pyvista.core.utilities.arrays.FieldAssociation
Association of the array.
name : str
The name of the array.
"""
association: FieldAssociation
name: str | None
def copy(self: ActiveArrayInfoTuple) -> ActiveArrayInfoTuple:
"""Return a copy of this object.
Returns
-------
ActiveArrayInfo
A copy of this object.
"""
return ActiveArrayInfoTuple(self.association, self.name)
class _ActiveArrayExistsInfoTuple(NamedTuple):
"""Active array info tuple for arrays that exist.
This named tuple is similar to ActiveArrayInfoTuple except the
`name` attribute cannot be `None`.
"""
association: FieldAssociation
name: str
class ActiveArrayInfo(_NoNewAttrMixin):
"""Active array info class with support for pickling.
.. deprecated:: 0.45
Use :class:`pyvista.core.dataset.ActiveArrayInfoTuple` instead.
Parameters
----------
association : pyvista.core.utilities.arrays.FieldAssociation
Array association.
Association of the array.
name : str
The name of the array.
"""
def __init__(self: ActiveArrayInfo, association: FieldAssociation, name: str | None) -> None:
"""Initialize."""
self.association = association
self.name = name
# Deprecated on v0.45.0, estimated removal on v0.48.0
warnings.warn(
'ActiveArrayInfo is deprecated. Use ActiveArrayInfoTuple instead.',
PyVistaDeprecationWarning,
)
def copy(self: ActiveArrayInfo) -> ActiveArrayInfo:
"""Return a copy of this object.
Returns
-------
ActiveArrayInfo
A copy of this object.
"""
return ActiveArrayInfo(self.association, self.name)
def __getstate__(self: ActiveArrayInfo) -> dict[str, Any]:
"""Support pickling."""
state = self.__dict__.copy()
state['association'] = int(self.association.value)
return state
def __setstate__(self: ActiveArrayInfo, state: dict[str, Any]) -> None:
"""Support unpickling."""
self.__dict__ = state.copy()
self.association = FieldAssociation(state['association'])
@property
def _namedtuple(self: ActiveArrayInfo) -> ActiveArrayInfoTuple:
"""Build a namedtuple on the fly to provide legacy support."""
return ActiveArrayInfoTuple(self.association, self.name)
def __iter__(self: ActiveArrayInfo) -> Iterator[FieldAssociation | str | None]:
"""Provide namedtuple-like __iter__."""
return self._namedtuple.__iter__()
def __repr__(self: ActiveArrayInfo) -> str:
"""Provide namedtuple-like __repr__."""
return self._namedtuple.__repr__()
def __getitem__(self: ActiveArrayInfo, item: int) -> FieldAssociation | str | None:
"""Provide namedtuple-like __getitem__."""
return self._namedtuple.__getitem__(item)
def __eq__(self: ActiveArrayInfo, other: object) -> bool:
"""Check equivalence (useful for serialize/deserialize tests)."""
if isinstance(other, ActiveArrayInfo):
same_association = int(self.association.value) == int(other.association.value)
return self.name == other.name and same_association
return False
__hash__ = None # type: ignore[assignment] # https://github.com/pyvista/pyvista/pull/7671
@promote_type(_vtk.vtkDataSet)
@abstract_class
class DataSet(DataSetFilters, DataObject):
"""Methods in common to spatially referenced objects.
Parameters
----------
*args :
Any extra args are passed as option to spatially referenced objects.
**kwargs :
Any extra keyword args are passed as option to spatially referenced objects.
"""
plot = pyvista._plot.plot
def __init__(self: Self, *args, **kwargs) -> None:
"""Initialize the common object."""
super().__init__(*args, **kwargs)
self._last_active_scalars_name: str | None = None
self._active_scalars_info = ActiveArrayInfoTuple(FieldAssociation.POINT, name=None)
self._active_vectors_info = ActiveArrayInfoTuple(FieldAssociation.POINT, name=None)
self._active_tensors_info = ActiveArrayInfoTuple(FieldAssociation.POINT, name=None)
# Used by glyph filter and plotter legend
self._glyph_geom: Sequence[_vtk.vtkDataSet] | None = None
def __getattr__(self: Self, item: str) -> Any:
"""Get attribute from base class if not found."""
return super().__getattribute__(item)
@property
def active_scalars_info(self: Self) -> ActiveArrayInfoTuple:
"""Return the active scalar's association and name.
Association refers to the data association (e.g. point, cell, or
field) of the active scalars.
Returns
-------
ActiveArrayInfo
The scalars info in an object with namedtuple semantics,
with attributes ``association`` and ``name``.
Notes
-----
If both cell and point scalars are present and neither have
been set active within at the dataset level, point scalars
will be made active.
Examples
--------
Create a mesh, add scalars to the mesh, and return the active
scalars info. Note how when the scalars are added, they
automatically become the active scalars.
>>> import pyvista as pv
>>> mesh = pv.Sphere()
>>> mesh['Z Height'] = mesh.points[:, 2]
>>> mesh.active_scalars_info
ActiveArrayInfoTuple(association=<FieldAssociation.POINT: 0>, name='Z Height')
"""
field, name = self._active_scalars_info
exclude = {'__custom_rgba', 'Normals', 'vtkOriginalPointIds', 'TCoords'}
if name in exclude:
name = self._last_active_scalars_name
# verify this field is still valid
if name is not None:
if field is FieldAssociation.CELL:
if self.cell_data.active_scalars_name != name:
name = None
elif field is FieldAssociation.POINT:
if self.point_data.active_scalars_name != name:
name = None
if name is None:
# check for the active scalars in point or cell arrays
self._active_scalars_info = ActiveArrayInfoTuple(field, None)
for attr in [self.point_data, self.cell_data]:
if attr.active_scalars_name is not None:
self._active_scalars_info = ActiveArrayInfoTuple(
attr.association,
attr.active_scalars_name,
)
break
return self._active_scalars_info
@property
def active_vectors_info(self: Self) -> ActiveArrayInfoTuple:
"""Return the active vector's association and name.
Association refers to the data association (e.g. point, cell, or
field) of the active vectors.
Returns
-------
ActiveArrayInfo
The vectors info in an object with namedtuple semantics,
with attributes ``association`` and ``name``.
Notes
-----
If both cell and point vectors are present and neither have
been set active within at the dataset level, point vectors
will be made active.
Examples
--------
Create a mesh, compute the normals inplace, set the active
vectors to the normals, and show that the active vectors are
the ``'Normals'`` array associated with points.
>>> import pyvista as pv
>>> mesh = pv.Sphere()
>>> _ = mesh.compute_normals(inplace=True)
>>> mesh.active_vectors_name = 'Normals'
>>> mesh.active_vectors_info
ActiveArrayInfoTuple(association=<FieldAssociation.POINT: 0>, name='Normals')
"""
field, name = self._active_vectors_info
# verify this field is still valid
if name is not None:
if field is FieldAssociation.POINT:
if self.point_data.active_vectors_name != name:
name = None
if field is FieldAssociation.CELL:
if self.cell_data.active_vectors_name != name:
name = None
if name is None:
# check for the active vectors in point or cell arrays
self._active_vectors_info = ActiveArrayInfoTuple(field, None)
for attr in [self.point_data, self.cell_data]:
name = attr.active_vectors_name
if name is not None:
self._active_vectors_info = ActiveArrayInfoTuple(attr.association, name)
break
return self._active_vectors_info
@property
def active_tensors_info(self: Self) -> ActiveArrayInfoTuple:
"""Return the active tensor's field and name: [field, name].
Returns
-------
ActiveArrayInfo
Active tensor's field and name: [field, name].
"""
return self._active_tensors_info
@property
def active_vectors(self: Self) -> pyvista_ndarray | None:
"""Return the active vectors array.
Returns
-------
Optional[pyvista_ndarray]
Active vectors array.
Examples
--------
Create a mesh, compute the normals inplace, and return the
normals vector array.
>>> import pyvista as pv
>>> mesh = pv.Sphere()
>>> _ = mesh.compute_normals(inplace=True)
>>> mesh.active_vectors # doctest:+SKIP
pyvista_ndarray([[-2.48721432e-10, -1.08815623e-09, -1.00000000e+00],
[-2.48721432e-10, -1.08815623e-09, 1.00000000e+00],
[-1.18888125e-01, 3.40539310e-03, -9.92901802e-01],
...,
[-3.11940581e-01, -6.81432486e-02, 9.47654784e-01],
[-2.09880397e-01, -4.65070531e-02, 9.76620376e-01],
[-1.15582108e-01, -2.80492082e-02, 9.92901802e-01]],
dtype=float32)
"""
field, name = self.active_vectors_info
if name is not None:
try:
if field is FieldAssociation.POINT:
return self.point_data[name]
if field is FieldAssociation.CELL:
return self.cell_data[name]
except KeyError:
return None
return None
@property
def active_tensors(self: Self) -> NumpyArray[float] | None:
"""Return the active tensors array.
Returns
-------
Optional[np.ndarray]
Active tensors array.
"""
field: FieldAssociation = self.active_tensors_info.association
name: str | None = self.active_tensors_info.name
if name is not None:
try:
if field is FieldAssociation.POINT:
return self.point_data[name]
if field is FieldAssociation.CELL:
return self.cell_data[name]
except KeyError:
return None
return None
@property
def active_tensors_name(self: Self) -> str | None:
"""Return the name of the active tensor array.
Returns
-------
str
Name of the active tensor array.
"""
return self.active_tensors_info.name
@active_tensors_name.setter
def active_tensors_name(self: Self, name: str | None) -> None:
"""Set the name of the active tensor array.
Parameters
----------
name : str
Name of the active tensor array.
"""
self.set_active_tensors(name)
@property
def active_vectors_name(self: Self) -> str | None:
"""Return the name of the active vectors array.
Returns
-------
str
Name of the active vectors array.
Examples
--------
Create a mesh, compute the normals, set them as active, and
return the name of the active vectors.
>>> import pyvista as pv
>>> mesh = pv.Sphere()
>>> mesh_w_normals = mesh.compute_normals()
>>> mesh_w_normals.active_vectors_name = 'Normals'
>>> mesh_w_normals.active_vectors_name
'Normals'
"""
return self.active_vectors_info.name
@active_vectors_name.setter
def active_vectors_name(self: Self, name: str | None) -> None:
"""Set the name of the active vectors array.
Parameters
----------
name : str
Name of the active vectors array.
"""
self.set_active_vectors(name)
@property
def active_scalars_name(self: Self) -> str | None:
"""Return the name of the active scalars.
Returns
-------
str
Name of the active scalars.
Examples
--------
Create a mesh, add scalars to the mesh, and return the name of
the active scalars.
>>> import pyvista as pv
>>> mesh = pv.Sphere()
>>> mesh['Z Height'] = mesh.points[:, 2]
>>> mesh.active_scalars_name
'Z Height'
"""
return self.active_scalars_info.name
@active_scalars_name.setter
def active_scalars_name(self: Self, name: str | None) -> None:
"""Set the name of the active scalars.
Parameters
----------
name : str
Name of the active scalars.
"""
self.set_active_scalars(name)
@property
def points(self: Self) -> pyvista_ndarray:
"""Return a reference to the points as a numpy object.
Returns
-------
pyvista_ndarray
Reference to the points as a numpy object.
Examples
--------
Create a mesh and return the points of the mesh as a numpy
array.
>>> import pyvista as pv
>>> cube = pv.Cube()
>>> points = cube.points
>>> points
pyvista_ndarray([[-0.5, -0.5, -0.5],
[-0.5, -0.5, 0.5],
[-0.5, 0.5, 0.5],
[-0.5, 0.5, -0.5],
[ 0.5, -0.5, -0.5],
[ 0.5, 0.5, -0.5],
[ 0.5, 0.5, 0.5],
[ 0.5, -0.5, 0.5]], dtype=float32)
Shift these points in the z direction and show that their
position is reflected in the mesh points.
>>> points[:, 2] += 1
>>> cube.points
pyvista_ndarray([[-0.5, -0.5, 0.5],
[-0.5, -0.5, 1.5],
[-0.5, 0.5, 1.5],
[-0.5, 0.5, 0.5],
[ 0.5, -0.5, 0.5],
[ 0.5, 0.5, 0.5],
[ 0.5, 0.5, 1.5],
[ 0.5, -0.5, 1.5]], dtype=float32)
You can also update the points in-place:
>>> cube.points[...] = 2 * points
>>> cube.points
pyvista_ndarray([[-1., -1., 1.],
[-1., -1., 3.],
[-1., 1., 3.],
[-1., 1., 1.],
[ 1., -1., 1.],
[ 1., 1., 1.],
[ 1., 1., 3.],
[ 1., -1., 3.]], dtype=float32)
"""
_points = self.GetPoints()
try:
_points = _points.GetData()
except AttributeError:
# create an empty array
vtkpts = vtk_points(np.empty((0, 3)), deep=False)
self.SetPoints(vtkpts)
_points = self.GetPoints().GetData()
return pyvista_ndarray(_points, dataset=self)
@points.setter
def points(self: Self, points: MatrixLike[float] | _vtk.vtkPoints) -> None:
"""Set a reference to the points as a numpy object.
Parameters
----------
points : MatrixLike[float] | :vtk:`vtkPoints`
Points as a array object.
"""
pdata = self.GetPoints()
if isinstance(points, pyvista_ndarray):
# simply set the underlying data
if points.VTKObject is not None and pdata is not None:
pdata.SetData(points.VTKObject)
pdata.Modified()
self.Modified()
return
# directly set the data if vtk object
if isinstance(points, _vtk.vtkPoints):
self.SetPoints(points)
if pdata is not None:
pdata.Modified()
self.Modified()
return
# otherwise, wrap and use the array
points, _ = _coerce_pointslike_arg(points, copy=False)
vtkpts = vtk_points(points, deep=False)
if not pdata:
self.SetPoints(vtkpts)
else:
pdata.SetData(vtkpts.GetData())
self.GetPoints().Modified()
self.Modified()
@property
def arrows(
self: Self,
) -> pyvista.PolyData | None:
"""Return a glyph representation of the active vector data as arrows.
Arrows will be located at the points or cells of the mesh and
their size will be dependent on the norm of the vector.
Their direction will be the "direction" of the vector.
If there are both active point and cell vectors, preference is
given to the point vectors.
Returns
-------
pyvista.PolyData
Active vectors represented as arrows.
Examples
--------
Create a mesh, compute the normals and set them active.
>>> import pyvista as pv
>>> mesh = pv.Cube()
>>> mesh_w_normals = mesh.compute_normals()
>>> mesh_w_normals.active_vectors_name = 'Normals'
Plot the active vectors as arrows. Show the original mesh as wireframe for
context.
>>> arrows = mesh_w_normals.arrows
>>> pl = pv.Plotter()
>>> _ = pl.add_mesh(mesh, style='wireframe')
>>> _ = pl.add_mesh(arrows, color='red')
>>> pl.show()
"""
vectors = self.active_vectors
if vectors is None:
return None
if vectors.ndim != 2:
msg = 'Active vectors are not vectors.'
raise ValueError(msg)
field, vectors_name = self.active_vectors_info
# Cast type since we know name is not None since vectors is not None at this point
vectors_name = cast('str', vectors_name)
scale_name = f'{vectors_name} Magnitude'
scale = np.linalg.norm(vectors, axis=1)
if field == FieldAssociation.POINT:
self.point_data.set_array(scale, scale_name)
else:
self.cell_data.set_array(scale, scale_name)
return self.glyph(orient=vectors_name, scale=scale_name)
def set_active_scalars(
self: Self,
name: str | None,
preference: PointLiteral | CellLiteral = 'cell',
) -> tuple[FieldAssociation, NumpyArray[float] | None]:
"""Find the scalars by name and appropriately sets it as active.
To deactivate any active scalars, pass ``None`` as the ``name``.
Parameters
----------
name : str, optional
Name of the scalars array to assign as active. If
``None``, deactivates active scalars for both point and
cell data.
preference : str, default: "cell"
If there are two arrays of the same name associated with
points or cells, it will prioritize an array matching this
type. Can be either ``'cell'`` or ``'point'``.
Returns
-------
pyvista.core.utilities.arrays.FieldAssociation
Association of the scalars matching ``name``.
pyvista_ndarray
An array from the dataset matching ``name``.
"""
if preference not in [
'point',
'cell',
FieldAssociation.CELL,
FieldAssociation.POINT,
]:
msg = '``preference`` must be either "point" or "cell"'
raise ValueError(msg)
if name is None:
self.GetCellData().SetActiveScalars(None)
self.GetPointData().SetActiveScalars(None)
return FieldAssociation.NONE, np.array([])
field = get_array_association(self, name, preference=preference)
if field == FieldAssociation.NONE:
if name in self.field_data:
msg = f'Data named "{name}" is a field array which cannot be active.'
raise ValueError(msg)
else:
msg = f'Data named "{name}" does not exist in this dataset.'
raise KeyError(msg)
self._last_active_scalars_name = self.active_scalars_info.name
if field == FieldAssociation.POINT:
ret = self.GetPointData().SetActiveScalars(name)
elif field == FieldAssociation.CELL:
ret = self.GetCellData().SetActiveScalars(name)
else:
msg = f'Data field ({name}) with type ({field}) not usable'
raise ValueError(msg)
if ret < 0:
msg = f'Data field "{name}" with type ({field}) could not be set as the active scalars'
raise ValueError(msg)
self._active_scalars_info = ActiveArrayInfoTuple(field, name)
if field == FieldAssociation.POINT:
return field, self.point_data.active_scalars
else: # must be cell
return field, self.cell_data.active_scalars
def set_active_vectors(
self: Self, name: str | None, preference: PointLiteral | CellLiteral = 'point'
) -> None:
"""Find the vectors by name and appropriately sets it as active.
To deactivate any active vectors, pass ``None`` as the ``name``.
Parameters
----------
name : str, optional
Name of the vectors array to assign as active.
preference : str, default: "point"
If there are two arrays of the same name associated with
points, cells, or field data, it will prioritize an array
matching this type. Can be either ``'cell'``,
``'field'``, or ``'point'``.
"""
if name is None:
self.GetCellData().SetActiveVectors(None)
self.GetPointData().SetActiveVectors(None)
field = FieldAssociation.POINT
else:
field = get_array_association(self, name, preference=preference)
if field == FieldAssociation.POINT:
ret = self.GetPointData().SetActiveVectors(name)
elif field == FieldAssociation.CELL:
ret = self.GetCellData().SetActiveVectors(name)
else:
msg = f'Data field ({name}) with type ({field}) not usable'
raise ValueError(msg)
if ret < 0:
msg = (
f'Data field ({name}) with type ({field}) could not be set as the '
f'active vectors'
)
raise ValueError(msg)
self._active_vectors_info = ActiveArrayInfoTuple(field, name)
def set_active_tensors(
self: Self, name: str | None, preference: PointLiteral | CellLiteral = 'point'
) -> None:
"""Find the tensors by name and appropriately sets it as active.
To deactivate any active tensors, pass ``None`` as the ``name``.
Parameters
----------
name : str, optional
Name of the tensors array to assign as active.
preference : str, default: "point"
If there are two arrays of the same name associated with
points, cells, or field data, it will prioritize an array
matching this type. Can be either ``'cell'``,
``'field'``, or ``'point'``.
"""
if name is None:
self.GetCellData().SetActiveTensors(None)
self.GetPointData().SetActiveTensors(None)
field = FieldAssociation.POINT
else:
field = get_array_association(self, name, preference=preference)
if field == FieldAssociation.POINT:
ret = self.GetPointData().SetActiveTensors(name)
elif field == FieldAssociation.CELL:
ret = self.GetCellData().SetActiveTensors(name)
else:
msg = f'Data field ({name}) with type ({field}) not usable'
raise ValueError(msg)
if ret < 0:
msg = (
f'Data field ({name}) with type ({field}) could not be set as the '
f'active tensors'
)
raise ValueError(msg)
self._active_tensors_info = ActiveArrayInfoTuple(field, name)
def rename_array(
self: Self,
old_name: str,
new_name: str,
preference: PointLiteral | CellLiteral | FieldLiteral = 'cell',
) -> None:
"""Change array name by searching for the array then renaming it.
Parameters
----------
old_name : str
Name of the array to rename.
new_name : str
Name to rename the array to.
preference : str, default: "cell"
If there are two arrays of the same name associated with
points, cells, or field data, it will prioritize an array
matching this type. Can be either ``'cell'``,
``'field'``, or ``'point'``.
Examples
--------
Create a cube, assign a point array to the mesh named
``'my_array'``, and rename it to ``'my_renamed_array'``.
>>> import pyvista as pv
>>> import numpy as np
>>> cube = pv.Cube()
>>> cube['my_array'] = range(cube.n_points)
>>> cube.rename_array('my_array', 'my_renamed_array')
>>> cube['my_renamed_array']
pyvista_ndarray([0, 1, 2, 3, 4, 5, 6, 7])
"""
field = get_array_association(self, old_name, preference=preference)
was_active = False
if self.active_scalars_name == old_name:
was_active = True
if field == FieldAssociation.POINT:
data = self.point_data
elif field == FieldAssociation.CELL:
data = self.cell_data
elif field == FieldAssociation.NONE:
data = self.field_data
else:
msg = f'Array with name {old_name} not found.'
raise KeyError(msg)
arr = data.pop(old_name)
# Update the array's name before reassigning. This prevents taking a copy of the array in
# `DataSetAttributes._prepare_array` which can lead to the array being garbage collected.
# See issue #5244.
arr.VTKObject.SetName(new_name) # type: ignore[union-attr]
data[new_name] = arr
if was_active and field != FieldAssociation.NONE:
self.set_active_scalars(new_name, preference=field)
@property
def active_scalars(self: Self) -> pyvista_ndarray | None:
"""Return the active scalars as an array.
Returns
-------
Optional[pyvista_ndarray]
Active scalars as an array.
"""
field, name = self.active_scalars_info
if name is not None:
try:
if field == FieldAssociation.POINT:
return self.point_data[name]
if field == FieldAssociation.CELL:
return self.cell_data[name]
except KeyError:
return None
return None
@property
def active_normals(self: Self) -> pyvista_ndarray | None:
"""Return the active normals as an array.
Returns
-------
pyvista_ndarray
Active normals of this dataset.
Notes
-----
If both point and cell normals exist, this returns point
normals by default.
Examples
--------
Compute normals on an example sphere mesh and return the
active normals for the dataset. Show that this is the same size
as the number of points.
>>> import pyvista as pv
>>> mesh = pv.Sphere()
>>> mesh = mesh.compute_normals()
>>> normals = mesh.active_normals
>>> normals.shape
(842, 3)
>>> mesh.n_points
842
"""
if self.point_data.active_normals is not None:
return self.point_data.active_normals
return self.cell_data.active_normals
def get_data_range( # type: ignore[override]
self: Self,
arr_var: str | NumpyArray[float] | None = None,
preference: PointLiteral | CellLiteral | FieldLiteral = 'cell',
) -> tuple[float, float]:
"""Get the min and max of a named array.
Parameters
----------
arr_var : str, np.ndarray, optional
The name of the array to get the range. If ``None``, the
active scalars is used.
preference : str, default: "cell"
When scalars is specified, this is the preferred array type
to search for in the dataset. Must be either ``'point'``,
``'cell'``, or ``'field'``.
Returns
-------
tuple
``(min, max)`` of the named array.
"""
if arr_var is None: # use active scalars array
arr_var = self.active_scalars_info.name
if arr_var is None:
return (np.nan, np.nan)
if isinstance(arr_var, str):
name = arr_var
arr = get_array(self, name, preference=preference, err=True)
else:
arr = arr_var # type: ignore[assignment]
# If array has no tuples return a NaN range
if arr is None:
return (np.nan, np.nan)
if arr.size == 0 or not np.issubdtype(arr.dtype, np.number):
return (np.nan, np.nan)
# Use the array range
return np.nanmin(arr), np.nanmax(arr)
@_deprecate_positional_args(allowed=['ido'])
def copy_meta_from(self: Self, ido: DataSet, deep: bool = True) -> None: # noqa: FBT001, FBT002
"""Copy pyvista meta data onto this object from another object.
Parameters
----------
ido : pyvista.DataSet
Dataset to copy the metadata from.
deep : bool, default: True
Deep or shallow copy.
"""
if deep:
self._association_complex_names = deepcopy(ido._association_complex_names)
self._association_bitarray_names = deepcopy(ido._association_bitarray_names)
self._active_scalars_info = ido.active_scalars_info.copy()
self._active_vectors_info = ido.active_vectors_info.copy()
self._active_tensors_info = ido.active_tensors_info.copy()
else:
# pass by reference
self._association_complex_names = ido._association_complex_names
self._association_bitarray_names = ido._association_bitarray_names
self._active_scalars_info = ido.active_scalars_info
self._active_vectors_info = ido.active_vectors_info
self._active_tensors_info = ido.active_tensors_info
@property
def point_data(self: Self) -> DataSetAttributes:
"""Return point data as DataSetAttributes.
Returns
-------
DataSetAttributes
Point data as DataSetAttributes.
Examples
--------
Add point arrays to a mesh and list the available ``point_data``.
>>> import pyvista as pv
>>> import numpy as np
>>> mesh = pv.Cube()
>>> mesh.clear_data()
>>> mesh.point_data['my_array'] = np.random.default_rng().random(mesh.n_points)
>>> mesh.point_data['my_other_array'] = np.arange(mesh.n_points)
>>> mesh.point_data
pyvista DataSetAttributes
Association : POINT
Active Scalars : my_array
Active Vectors : None
Active Texture : None
Active Normals : None
Contains arrays :
my_array float64 (8,) SCALARS
my_other_array int64 (8,)
Access an array from ``point_data``.
>>> mesh.point_data['my_other_array']
pyvista_ndarray([0, 1, 2, 3, 4, 5, 6, 7])
Or access it directly from the mesh.
>>> mesh['my_array'].shape
(8,)
"""
return DataSetAttributes(
self.GetPointData(),
dataset=self,
association=FieldAssociation.POINT,
)
def clear_point_data(self: Self) -> None:
"""Remove all point arrays.
Examples
--------
Clear all point arrays from a mesh.
>>> import pyvista as pv
>>> import numpy as np
>>> mesh = pv.Sphere()
>>> mesh.point_data.keys()
['Normals']
>>> mesh.clear_point_data()
>>> mesh.point_data.keys()
[]
"""
self.point_data.clear()
def clear_cell_data(self: Self) -> None:
"""Remove all cell arrays."""
self.cell_data.clear()
def clear_data(self: Self) -> None:
"""Remove all arrays from point/cell/field data.
Examples
--------
Clear all arrays from a mesh.
>>> import pyvista as pv
>>> import numpy as np
>>> mesh = pv.Sphere()
>>> mesh.point_data.keys()
['Normals']
>>> mesh.clear_data()
>>> mesh.point_data.keys()
[]
"""
self.clear_point_data()
self.clear_cell_data()
self.clear_field_data()
@property
def cell_data(self: Self) -> DataSetAttributes:
"""Return cell data as DataSetAttributes.
Returns
-------
DataSetAttributes
Cell data as DataSetAttributes.
Examples
--------
Add cell arrays to a mesh and list the available ``cell_data``.
>>> import pyvista as pv
>>> import numpy as np
>>> mesh = pv.Cube()
>>> mesh.clear_data()
>>> mesh.cell_data['my_array'] = np.random.default_rng().random(mesh.n_cells)
>>> mesh.cell_data['my_other_array'] = np.arange(mesh.n_cells)
>>> mesh.cell_data
pyvista DataSetAttributes
Association : CELL
Active Scalars : my_array
Active Vectors : None
Active Texture : None
Active Normals : None
Contains arrays :
my_array float64 (6,) SCALARS
my_other_array int64 (6,)
Access an array from ``cell_data``.
>>> mesh.cell_data['my_other_array']
pyvista_ndarray([0, 1, 2, 3, 4, 5])
Or access it directly from the mesh.
>>> mesh['my_array'].shape
(6,)
"""
return DataSetAttributes(
self.GetCellData(),
dataset=self,
association=FieldAssociation.CELL,
)
@property
def n_points(self: Self) -> int:
"""Return the number of points in the entire dataset.
Returns
-------
int
Number of points in the entire dataset.
Examples
--------
Create a mesh and return the number of points in the
mesh.
>>> import pyvista as pv
>>> cube = pv.Cube()
>>> cube.n_points
8
"""
return self.GetNumberOfPoints()
@property
def n_cells(self: Self) -> int:
"""Return the number of cells in the entire dataset.
Returns
-------
int :
Number of cells in the entire dataset.
Notes
-----
This returns the total number of cells -- for :class:`pyvista.PolyData`
this includes vertices, lines, triangle strips and polygonal faces.
Examples
--------
Create a mesh and return the number of cells in the
mesh.
>>> import pyvista as pv
>>> cube = pv.Cube()
>>> cube.n_cells
6
"""
return self.GetNumberOfCells()
@property
def number_of_points(self: Self) -> int: # pragma: no cover
"""Return the number of points.
Returns
-------
int :
Number of points.
"""
return self.GetNumberOfPoints()
@property
def number_of_cells(self: Self) -> int: # pragma: no cover
"""Return the number of cells.
Returns
-------
int :
Number of cells.
"""
return self.GetNumberOfCells()
@property
def bounds(self: Self) -> BoundsTuple:
"""Return the bounding box of this dataset.
Returns
-------
BoundsLike
Bounding box of this dataset.
The form is: ``(x_min, x_max, y_min, y_max, z_min, z_max)``.
Examples
--------
Create a cube and return the bounds of the mesh.
>>> import pyvista as pv
>>> cube = pv.Cube()
>>> cube.bounds
BoundsTuple(x_min = -0.5,
x_max = 0.5,
y_min = -0.5,
y_max = 0.5,
z_min = -0.5,
z_max = 0.5)
"""
return BoundsTuple(*self.GetBounds())
@property
def length(self: Self) -> float:
"""Return the length of the diagonal of the bounding box.
Returns
-------
float
Length of the diagonal of the bounding box.
Examples
--------
Get the length of the bounding box of a cube. This should
match ``3**(1/2)`` since it is the diagonal of a cube that is
``1 x 1 x 1``.
>>> import pyvista as pv
>>> mesh = pv.Cube()
>>> mesh.length
1.7320508075688772
"""
return self.GetLength()
@property
def center(self: Self) -> tuple[float, float, float]:
"""Return the center of the bounding box.
Returns
-------
tuple[float, float, float]
Center of the bounding box.
Examples
--------
Get the center of a mesh.
>>> import pyvista as pv
>>> mesh = pv.Sphere(center=(1, 2, 0))
>>> mesh.center
(1.0, 2.0, 0.0)
"""
return self.GetCenter()
@property
def volume(
self: Self,
) -> float:
"""Return the mesh volume.
This will return 0 for meshes with 2D cells.
Returns
-------
float
Total volume of the mesh.
Examples
--------
Get the volume of a cube of size 4x4x4.
Note that there are 5 points in each direction.
>>> import pyvista as pv
>>> mesh = pv.ImageData(dimensions=(5, 5, 5))
>>> mesh.volume
64.0
A mesh with 2D cells has no volume.
>>> mesh = pv.ImageData(dimensions=(5, 5, 1))
>>> mesh.volume
0.0
:class:`pyvista.PolyData` is special as a 2D surface can
enclose a 3D volume. This case uses a different methodology,
see :func:`pyvista.PolyData.volume`.
>>> mesh = pv.Sphere()
>>> mesh.volume
0.51825
"""
sizes = self.compute_cell_sizes(length=False, area=False, volume=True)
return sizes.cell_data['Volume'].sum().item()
@property
def area(
self: Self,
) -> float:
"""Return the mesh area if 2D.
This will return 0 for meshes with 3D cells.
Returns
-------
float
Total area of the mesh.
Examples
--------
Get the area of a square of size 2x2.
Note 5 points in each direction.
>>> import pyvista as pv
>>> mesh = pv.ImageData(dimensions=(5, 5, 1))
>>> mesh.area
16.0
A mesh with 3D cells does not have an area. To get
the outer surface area, first extract the surface using
:func:`pyvista.DataSetFilters.extract_surface`.
>>> mesh = pv.ImageData(dimensions=(5, 5, 5))
>>> mesh.area
0.0
Get the area of a sphere. Discretization error results
in slight difference from ``pi``.
>>> mesh = pv.Sphere()
>>> mesh.area
3.13
"""
sizes = self.compute_cell_sizes(length=False, area=True, volume=False)
return sizes.cell_data['Area'].sum().item()
def get_array(
self: Self,
name: str,
preference: CellLiteral | PointLiteral | FieldLiteral = 'cell',
) -> pyvista.pyvista_ndarray:
"""Search both point, cell and field data for an array.
Parameters
----------
name : str
Name of the array.
preference : str, default: "cell"
When scalars is specified, this is the preferred array
type to search for in the dataset. Must be either
``'point'``, ``'cell'``, or ``'field'``.
Returns
-------
pyvista.pyvista_ndarray
Requested array.
Examples
--------
Create a DataSet with a variety of arrays.
>>> import pyvista as pv
>>> mesh = pv.Cube()
>>> mesh.clear_data()
>>> mesh.point_data['point-data'] = range(mesh.n_points)
>>> mesh.cell_data['cell-data'] = range(mesh.n_cells)
>>> mesh.field_data['field-data'] = ['a', 'b', 'c']
>>> mesh.array_names
['point-data', 'field-data', 'cell-data']
Get the point data array.
>>> mesh.get_array('point-data')
pyvista_ndarray([0, 1, 2, 3, 4, 5, 6, 7])
Get the cell data array.
>>> mesh.get_array('cell-data')
pyvista_ndarray([0, 1, 2, 3, 4, 5])
Get the field data array.
>>> mesh.get_array('field-data')
pyvista_ndarray(['a', 'b', 'c'], dtype='<U1')
"""
arr = get_array(self, name, preference=preference, err=True)
if arr is None: # pragma: no cover
raise RuntimeError # this should never be reached with err=True
return arr
def get_array_association(
self: Self,
name: str,
preference: Literal['cell', 'point', 'field'] = 'cell',
) -> FieldAssociation:
"""Get the association of an array.
Parameters
----------
name : str
Name of the array.
preference : str, default: "cell"
When ``name`` is specified, this is the preferred array
association to search for in the dataset. Must be either
``'point'``, ``'cell'``, or ``'field'``.
Returns
-------
pyvista.core.utilities.arrays.FieldAssociation
Field association of the array.
Examples
--------
Create a DataSet with a variety of arrays.
>>> import pyvista as pv
>>> mesh = pv.Cube()
>>> mesh.clear_data()
>>> mesh.point_data['point-data'] = range(mesh.n_points)
>>> mesh.cell_data['cell-data'] = range(mesh.n_cells)
>>> mesh.field_data['field-data'] = ['a', 'b', 'c']
>>> mesh.array_names
['point-data', 'field-data', 'cell-data']
Get the point data array association.
>>> mesh.get_array_association('point-data')
<FieldAssociation.POINT: 0>
Get the cell data array association.
>>> mesh.get_array_association('cell-data')
<FieldAssociation.CELL: 1>
Get the field data array association.
>>> mesh.get_array_association('field-data')
<FieldAssociation.NONE: 2>
"""
return get_array_association(self, name, preference=preference, err=True)
def __getitem__(
self: Self,
index: tuple[str, Literal['cell', 'point', 'field']] | str,
) -> pyvista_ndarray:
"""Search both point, cell, and field data for an array."""
if isinstance(index, tuple):
name, preference = index
elif isinstance(index, str):
name = index
preference = 'cell'
else:
msg = ( # type: ignore[unreachable]
f'Index ({index}) not understood.'
' Index must be a string name or a tuple of string name and string preference.'
)
raise KeyError(msg)
return self.get_array(name, preference=preference)
def _ipython_key_completions_(self: Self) -> list[str]:
"""Tab completion of IPython."""
return self.array_names
def __setitem__(
self: Self,
name: str,
scalars: NumpyArray[float] | Sequence[float] | float,
) -> None: # numpydoc ignore=PR01,RT01
"""Add/set an array in the point_data, or cell_data accordingly.
It depends on the array's length, or specified mode.
"""
# First check points - think of case with vertex cells
# there would be the same number of cells as points but we'd want
# the data to be on the nodes.
if scalars is None:
msg = 'Empty array unable to be added.' # type: ignore[unreachable]
raise TypeError(msg)
else:
scalars = np.asanyarray(scalars)
if scalars.ndim == 0:
if np.issubdtype(scalars.dtype, np.str_):
# Always set scalar strings as field data
self.field_data[name] = scalars
return
# reshape single scalar values from 0D to 1D so that shape[0] can be indexed
scalars = scalars.reshape((1,))
# Now check array size to determine which field to place array
if scalars.shape[0] == self.n_points:
self.point_data[name] = scalars
elif scalars.shape[0] == self.n_cells:
self.cell_data[name] = scalars
else:
# Field data must be set explicitly as it could be a point of
# confusion for new users
raise_not_matching(scalars, self)
return
@property
def n_arrays(self: Self) -> int:
"""Return the number of arrays present in the dataset.
Returns
-------
int
Number of arrays present in the dataset.
"""
n = self.GetPointData().GetNumberOfArrays()
n += self.GetCellData().GetNumberOfArrays()
n += self.GetFieldData().GetNumberOfArrays()
return n
@property
def array_names(self: Self) -> list[str]:
"""Return a list of array names for the dataset.
This makes sure to put the active scalars' name first in the list.
Returns
-------
list[str]
List of array names for the dataset.
Examples
--------
Return the array names for a mesh.
>>> import pyvista as pv
>>> mesh = pv.Sphere()
>>> mesh.point_data['my_array'] = range(mesh.n_points)
>>> mesh.array_names
['my_array', 'Normals']
"""
names: list[str] = []
names.extend(self.field_data.keys())
names.extend(self.point_data.keys())
names.extend(self.cell_data.keys())
if self.active_scalars_name is not None:
names.remove(self.active_scalars_name)
names.insert(0, self.active_scalars_name)
return names
def _get_attrs(self: Self) -> list[tuple[str, Any, str]]:
"""Return the representation methods (internal helper)."""
attrs: list[tuple[str, Any, str]] = []
attrs.append(('N Cells', self.GetNumberOfCells(), '{}'))
attrs.append(('N Points', self.GetNumberOfPoints(), '{}'))
if isinstance(self, pyvista.PolyData):
attrs.append(('N Strips', self.n_strips, '{}'))
bds = self.bounds
fmt = f'{pyvista.FLOAT_FORMAT}, {pyvista.FLOAT_FORMAT}'
attrs.append(('X Bounds', (bds.x_min, bds.x_max), fmt))
attrs.append(('Y Bounds', (bds.y_min, bds.y_max), fmt))
attrs.append(('Z Bounds', (bds.z_min, bds.z_max), fmt))
# if self.n_cells <= pyvista.REPR_VOLUME_MAX_CELLS and self.n_cells > 0:
# attrs.append(("Volume", (self.volume), pyvista.FLOAT_FORMAT))
return attrs
def _repr_html_(self: Self) -> str:
"""Return a pretty representation for Jupyter notebooks.
It includes header details and information about all arrays.
"""
fmt = ''
if self.n_arrays > 0:
fmt += "<table style='width: 100%;'>"
fmt += '<tr><th>Header</th><th>Data Arrays</th></tr>'
fmt += '<tr><td>'
# Get the header info
fmt += self.head(display=False, html=True)
# Fill out arrays
if self.n_arrays > 0:
fmt += '</td><td>'
fmt += '\n'
fmt += "<table style='width: 100%;'>\n"
titles = ['Name', 'Field', 'Type', 'N Comp', 'Min', 'Max']
fmt += '<tr>' + ''.join([f'<th>{t}</th>' for t in titles]) + '</tr>\n'
row = '<tr><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td></tr>\n'
row = '<tr>' + ''.join(['<td>{}</td>' for i in range(len(titles))]) + '</tr>\n'
def format_array(
name: str,
arr: str | pyvista_ndarray,
field: Literal['Points', 'Cells', 'Fields'],
) -> str:
"""Format array information for printing (internal helper)."""
if isinstance(arr, str):
# Convert string scalar into a numpy array. Otherwise, get_data_range
# will treat the string as an array name, not an array value.
arr = pyvista.pyvista_ndarray(arr) # type: ignore[arg-type]
dl, dh = self.get_data_range(arr)
dl = pyvista.FLOAT_FORMAT.format(dl) # type: ignore[assignment]
dh = pyvista.FLOAT_FORMAT.format(dh) # type: ignore[assignment]
if name == self.active_scalars_info.name:
name = f'<b>{name}</b>'
ncomp = arr.shape[1] if arr.ndim > 1 else 1
return row.format(name, field, arr.dtype, ncomp, dl, dh)
for key, arr in self.point_data.items():
fmt += format_array(key, arr, 'Points')
for key, arr in self.cell_data.items():
fmt += format_array(key, arr, 'Cells')
for key, arr in self.field_data.items():
fmt += format_array(key, arr, 'Fields')
fmt += '</table>\n'
fmt += '\n'
fmt += '</td></tr> </table>'
return fmt
def __repr__(self: Self) -> str:
"""Return the object representation."""
return self.head(display=False, html=False)
def __str__(self: Self) -> str:
"""Return the object string representation."""
return self.head(display=False, html=False)
@_deprecate_positional_args(allowed=['mesh'])
def copy_from(self: Self, mesh: _vtk.vtkDataSet, deep: bool = True) -> None: # noqa: FBT001, FBT002
"""Overwrite this dataset inplace with the new dataset's geometries and data.
Parameters
----------
mesh : :vtk:`vtkDataSet`
The overwriting mesh.
deep : bool, default: True
Whether to perform a deep or shallow copy.
Examples
--------
Create two meshes and overwrite ``mesh_a`` with ``mesh_b``.
Show that ``mesh_a`` is equal to ``mesh_b``.
>>> import pyvista as pv
>>> mesh_a = pv.Sphere()
>>> mesh_b = pv.Cube()
>>> mesh_a.copy_from(mesh_b)
>>> mesh_a == mesh_b
True
"""
# Allow child classes to overwrite parent classes
if not isinstance(self, type(mesh)):
msg = (
f'The Input DataSet type {type(mesh)} must be '
f'compatible with the one being overwritten {type(self)}'
)
raise TypeError(msg)
if deep: # type: ignore[unreachable]
self.deep_copy(mesh)
else:
self.shallow_copy(mesh)
if is_pyvista_dataset(mesh):
self.copy_meta_from(mesh, deep=deep)
def cast_to_unstructured_grid(self: Self) -> pyvista.UnstructuredGrid:
"""Get a new representation of this object as a :class:`pyvista.UnstructuredGrid`.
Returns
-------
pyvista.UnstructuredGrid
Dataset cast into a :class:`pyvista.UnstructuredGrid`.
Examples
--------
Cast a :class:`pyvista.PolyData` to a
:class:`pyvista.UnstructuredGrid`.
>>> import pyvista as pv
>>> mesh = pv.Sphere()
>>> type(mesh)
<class 'pyvista.core.pointset.PolyData'>
>>> grid = mesh.cast_to_unstructured_grid()
>>> type(grid)
<class 'pyvista.core.pointset.UnstructuredGrid'>
"""
alg = _vtk.vtkAppendFilter()
alg.AddInputData(self)
alg.Update()
return _get_output(alg)
@_deprecate_positional_args
def cast_to_pointset(self: Self, pass_cell_data: bool = False) -> pyvista.PointSet: # noqa: FBT001, FBT002
"""Extract the points of this dataset and return a :class:`pyvista.PointSet`.
Parameters
----------
pass_cell_data : bool, default: False
Run the :func:`cell_data_to_point_data()
<pyvista.DataObjectFilters.cell_data_to_point_data>` filter and pass
cell data fields to the new pointset.
Returns
-------
pyvista.PointSet
Dataset cast into a :class:`pyvista.PointSet`.
Notes
-----
This will produce a deep copy of the points and point/cell data of
the original mesh.
See Also
--------
:ref:`create_pointset_example`
Examples
--------
>>> import pyvista as pv
>>> mesh = pv.Wavelet()
>>> pointset = mesh.cast_to_pointset()
>>> type(pointset)
<class 'pyvista.core.pointset.PointSet'>
"""
pset = pyvista.PointSet()
pset.points = self.points.copy()
out = self.cell_data_to_point_data() if pass_cell_data else self
pset.GetPointData().DeepCopy(out.GetPointData())
pset.active_scalars_name = out.active_scalars_name
return pset
@_deprecate_positional_args
def cast_to_poly_points(self: Self, pass_cell_data: bool = False) -> pyvista.PolyData: # noqa: FBT001, FBT002
"""Extract the points of this dataset and return a :class:`pyvista.PolyData`.
Parameters
----------
pass_cell_data : bool, default: False
Run the :func:`cell_data_to_point_data()
<pyvista.DataObjectFilters.cell_data_to_point_data>` filter and pass
cell data fields to the new pointset.
Returns
-------
pyvista.PolyData
Dataset cast into a :class:`pyvista.PolyData`.
Notes
-----
This will produce a deep copy of the points and point/cell data of
the original mesh.
Examples
--------
>>> from pyvista import examples
>>> mesh = examples.load_uniform()
>>> points = mesh.cast_to_poly_points(pass_cell_data=True)
>>> type(points)
<class 'pyvista.core.pointset.PolyData'>
>>> points.n_arrays
2
>>> points.point_data
pyvista DataSetAttributes
Association : POINT
Active Scalars : Spatial Point Data
Active Vectors : None
Active Texture : None
Active Normals : None
Contains arrays :
Spatial Point Data float64 (1000,) SCALARS
>>> points.cell_data
pyvista DataSetAttributes
Association : CELL
Active Scalars : None
Active Vectors : None
Active Texture : None
Active Normals : None
Contains arrays :
Spatial Cell Data float64 (1000,)
"""
pset = pyvista.PolyData(self.points.copy())
if pass_cell_data:
cell_data = self.copy()
cell_data.clear_point_data()
cell_data = cell_data.cell_data_to_point_data()
pset.GetCellData().DeepCopy(cell_data.GetPointData())
pset.GetPointData().DeepCopy(self.GetPointData())
pset.active_scalars_name = self.active_scalars_name
return pset
@overload
def find_closest_point(self: Self, point: Iterable[float], n: Literal[1] = 1) -> int: ...
@overload
def find_closest_point(
self: Self, point: Iterable[float], n: int = ...
) -> VectorLike[int]: ...
def find_closest_point(
self: Self, point: Iterable[float], n: int = 1
) -> int | VectorLike[int]:
"""Find index of closest point in this mesh to the given point.
If wanting to query many points, use a KDTree with scipy or another
library as those implementations will be easier to work with.
See: https://github.com/pyvista/pyvista-support/issues/107
Parameters
----------
point : sequence[float]
Length 3 coordinate of the point to query.
n : int, optional
If greater than ``1``, returns the indices of the ``n`` closest
points.
Returns
-------
int
The index of the point in this mesh that is closest to the given point.
See Also
--------
DataSet.find_closest_cell
DataSet.find_containing_cell
DataSet.find_cells_along_line
DataSet.find_cells_within_bounds
Examples
--------
Find the index of the closest point to ``(0, 1, 0)``.
>>> import pyvista as pv
>>> mesh = pv.Sphere()
>>> index = mesh.find_closest_point((0, 1, 0))
>>> index
239
Get the coordinate of that point.
>>> mesh.points[index]
pyvista_ndarray([-0.05218758, 0.49653167, 0.02706946], dtype=float32)
"""
if not isinstance(point, (np.ndarray, Sequence)) or len(point) != 3:
msg = 'Given point must be a length three sequence.'
raise TypeError(msg)
if not isinstance(n, int):
msg = '`n` must be a positive integer.' # type: ignore[unreachable]
raise TypeError(msg)
if n < 1:
msg = '`n` must be a positive integer.'
raise ValueError(msg)
locator = _vtk.vtkPointLocator()
locator.SetDataSet(self)
locator.BuildLocator()
if n > 1:
id_list = _vtk.vtkIdList()
locator.FindClosestNPoints(n, point, id_list) # type: ignore[arg-type]
return vtk_id_list_to_array(id_list)
return locator.FindClosestPoint(point) # type: ignore[arg-type]
@_deprecate_positional_args(allowed=['point'])
def find_closest_cell(
self: Self,
point: VectorLike[float] | MatrixLike[float],
return_closest_point: bool = False, # noqa: FBT001, FBT002
) -> int | NumpyArray[int] | tuple[int | NumpyArray[int], NumpyArray[int]]:
"""Find index of closest cell in this mesh to the given point.
Parameters
----------
point : VectorLike[float] | MatrixLike[float]
Coordinates of point to query (length 3) or a
:class:`numpy.ndarray` of ``n`` points with shape ``(n, 3)``.
return_closest_point : bool, default: False
If ``True``, the closest point within a mesh cell to that point is
returned. This is not necessarily the closest nodal point on the
mesh. Default is ``False``.
Returns
-------
int or numpy.ndarray
Index or indices of the cell in this mesh that is/are closest
to the given point(s).
.. versionchanged:: 0.35.0
Inputs of shape ``(1, 3)`` now return a :class:`numpy.ndarray`
of shape ``(1,)``.
numpy.ndarray
Point or points inside a cell of the mesh that is/are closest
to the given point(s). Only returned if
``return_closest_point=True``.
.. versionchanged:: 0.35.0
Inputs of shape ``(1, 3)`` now return a :class:`numpy.ndarray`
of the same shape.
Warnings
--------
This method may still return a valid cell index even if the point
contains a value like ``numpy.inf`` or ``numpy.nan``.
See Also
--------
DataSet.find_closest_point
DataSet.find_containing_cell
DataSet.find_cells_along_line
DataSet.find_cells_within_bounds
:ref:`distance_between_surfaces_example`
Examples
--------
Find nearest cell on a sphere centered on the
origin to the point ``[0.1, 0.2, 0.3]``.
>>> import pyvista as pv
>>> mesh = pv.Sphere()
>>> point = [0.1, 0.2, 0.3]
>>> index = mesh.find_closest_cell(point)
>>> index
338
Make sure that this cell indeed is the closest to
``[0.1, 0.2, 0.3]``.
>>> import numpy as np
>>> cell_centers = mesh.cell_centers()
>>> relative_position = cell_centers.points - point
>>> distance = np.linalg.norm(relative_position, axis=1)
>>> np.argmin(distance)
np.int64(338)
Find the nearest cells to several random points that
are centered on the origin.
>>> points = 2 * np.random.default_rng().random((5000, 3)) - 1
>>> indices = mesh.find_closest_cell(points)
>>> indices.shape
(5000,)
For the closest cell, find the point inside the cell that is
closest to the supplied point. The rectangle is a unit square
with 1 cell and 4 nodal points at the corners in the plane with
``z`` normal and ``z=0``. The closest point inside the cell is
not usually at a nodal point.
>>> unit_square = pv.Rectangle()
>>> index, closest_point = unit_square.find_closest_cell(
... [0.25, 0.25, 0.5], return_closest_point=True
... )
>>> closest_point
array([0.25, 0.25, 0. ])
But, the closest point can be a nodal point, although the index of
that point is not returned. If the closest nodal point by index is
desired, see :func:`DataSet.find_closest_point`.
>>> index, closest_point = unit_square.find_closest_cell(
... [1.0, 1.0, 0.5], return_closest_point=True
... )
>>> closest_point
array([1., 1., 0.])
"""
point, singular = _coerce_pointslike_arg(point, copy=False)
locator = _vtk.vtkCellLocator()
locator.SetDataSet(self)
locator.BuildLocator()
cell = _vtk.vtkGenericCell()
closest_cells: list[int] = []
closest_points: list[list[float]] = []
for node in point:
closest_point = [0.0, 0.0, 0.0]
cell_id = _vtk.mutable(0)
sub_id = _vtk.mutable(0)
dist2 = _vtk.mutable(0.0)
locator.FindClosestPoint(node, closest_point, cell, cell_id, sub_id, dist2) # type: ignore[call-overload]
closest_cells.append(int(cell_id))
closest_points.append(closest_point)
out_cells: int | NumpyArray[int] = (
closest_cells[0] if singular else np.array(closest_cells)
)
out_points = np.array(closest_points[0]) if singular else np.array(closest_points)
if return_closest_point:
return out_cells, out_points
return out_cells
def find_containing_cell(
self: Self,
point: VectorLike[float] | MatrixLike[float],
) -> int | NumpyArray[int]:
"""Find index of a cell that contains the given point.
Parameters
----------
point : VectorLike[float] | MatrixLike[float],
Coordinates of point to query (length 3) or a
:class:`numpy.ndarray` of ``n`` points with shape ``(n, 3)``.
Returns
-------
int or numpy.ndarray
Index or indices of the cell in this mesh that contains
the given point.
.. versionchanged:: 0.35.0
Inputs of shape ``(1, 3)`` now return a :class:`numpy.ndarray`
of shape ``(1,)``.
See Also
--------
DataSet.find_closest_point
DataSet.find_closest_cell
DataSet.find_cells_along_line
DataSet.find_cells_within_bounds
Examples
--------
A unit square with 16 equal sized cells is created and a cell
containing the point ``[0.3, 0.3, 0.0]`` is found.
>>> import pyvista as pv
>>> mesh = pv.ImageData(dimensions=[5, 5, 1], spacing=[1 / 4, 1 / 4, 0])
>>> mesh
ImageData...
>>> mesh.find_containing_cell([0.3, 0.3, 0.0])
5
A point outside the mesh domain will return ``-1``.
>>> mesh.find_containing_cell([0.3, 0.3, 1.0])
-1
Find the cells that contain 1000 random points inside the mesh.
>>> import numpy as np
>>> points = np.random.default_rng().random((1000, 3))
>>> indices = mesh.find_containing_cell(points)
>>> indices.shape
(1000,)
"""
point, singular = _coerce_pointslike_arg(point, copy=False)
locator = _vtk.vtkCellLocator()
locator.SetDataSet(self)
locator.BuildLocator()
containing_cells = [locator.FindCell(node) for node in point]
return containing_cells[0] if singular else np.array(containing_cells)
def find_cells_along_line(
self: Self,
pointa: VectorLike[float],
pointb: VectorLike[float],
tolerance: float = 0.0,
) -> NumpyArray[int]:
"""Find the index of cells whose bounds intersect a line.
Line is defined from ``pointa`` to ``pointb``.
Parameters
----------
pointa : VectorLike
Length 3 coordinate of the start of the line.
pointb : VectorLike
Length 3 coordinate of the end of the line.
tolerance : float, default: 0.0
The absolute tolerance to use to find cells along line.
Returns
-------
numpy.ndarray
Index or indices of the cell(s) whose bounds intersect
the line.
Warnings
--------
This method returns cells whose bounds intersect the line.
This means that the line may not intersect the cell itself.
To obtain cells that intersect the line, use
:func:`pyvista.DataSet.find_cells_intersecting_line`.
See Also
--------
DataSet.find_closest_point
DataSet.find_closest_cell
DataSet.find_containing_cell
DataSet.find_cells_within_bounds
DataSet.find_cells_intersecting_line
Examples
--------
>>> import pyvista as pv
>>> mesh = pv.Sphere()
>>> mesh.find_cells_along_line([0.0, 0, 0], [1.0, 0, 0])
array([ 86, 87, 1652, 1653])
"""
if np.array(pointa).size != 3:
msg = 'Point A must be a length three tuple of floats.'
raise TypeError(msg)
if np.array(pointb).size != 3:
msg = 'Point B must be a length three tuple of floats.'
raise TypeError(msg)
locator = _vtk.vtkCellLocator()
locator.SetDataSet(self)
locator.BuildLocator()
id_list = _vtk.vtkIdList()
locator.FindCellsAlongLine(
cast('Sequence[float]', pointa),
cast('Sequence[float]', pointb),
tolerance,
id_list,
)
return vtk_id_list_to_array(id_list)
def find_cells_intersecting_line(
self: Self,
pointa: VectorLike[float],
pointb: VectorLike[float],
tolerance: float = 0.0,
) -> NumpyArray[int]:
"""Find the index of cells that intersect a line.
Line is defined from ``pointa`` to ``pointb``. This
method requires vtk version >=9.2.0.
Parameters
----------
pointa : sequence[float]
Length 3 coordinate of the start of the line.
pointb : sequence[float]
Length 3 coordinate of the end of the line.
tolerance : float, default: 0.0
The absolute tolerance to use to find cells along line.
Returns
-------
numpy.ndarray
Index or indices of the cell(s) that intersect
the line.
See Also
--------
DataSet.find_closest_point
DataSet.find_closest_cell
DataSet.find_containing_cell
DataSet.find_cells_within_bounds
DataSet.find_cells_along_line
Examples
--------
>>> import pyvista as pv
>>> mesh = pv.Sphere()
>>> mesh.find_cells_intersecting_line([0.0, 0, 0], [1.0, 0, 0])
array([ 86, 1653])
"""
if pyvista.vtk_version_info < (9, 2, 0):
msg = 'pyvista.PointSet requires VTK >= 9.2.0'
raise VTKVersionError(msg)
if np.array(pointa).size != 3:
msg = 'Point A must be a length three tuple of floats.'
raise TypeError(msg)
if np.array(pointb).size != 3:
msg = 'Point B must be a length three tuple of floats.'
raise TypeError(msg)
locator = _vtk.vtkCellLocator()
locator.SetDataSet(cast('_vtk.vtkDataSet', self))
locator.BuildLocator()
id_list = _vtk.vtkIdList()
points = _vtk.vtkPoints()
cell = _vtk.vtkGenericCell()
locator.IntersectWithLine(
cast('Sequence[float]', pointa),
cast('Sequence[float]', pointb),
tolerance,
points,
id_list,
cell,
)
return vtk_id_list_to_array(id_list)
def find_cells_within_bounds(self: Self, bounds: BoundsTuple) -> NumpyArray[int]:
"""Find the index of cells in this mesh within bounds.
Parameters
----------
bounds : sequence[float]
Bounding box. The form is: ``(x_min, x_max, y_min, y_max, z_min, z_max)``.
Returns
-------
numpy.ndarray
Index or indices of the cell in this mesh that are closest
to the given point.
See Also
--------
DataSet.find_closest_point
DataSet.find_closest_cell
DataSet.find_containing_cell
DataSet.find_cells_along_line
Examples
--------
>>> import pyvista as pv
>>> mesh = pv.Cube()
>>> index = mesh.find_cells_within_bounds([-2.0, 2.0, -2.0, 2.0, -2.0, 2.0])
"""
if np.array(bounds).size != 6:
msg = 'Bounds must be a length six tuple of floats.'
raise TypeError(msg)
locator = _vtk.vtkCellTreeLocator()
locator.SetDataSet(cast('_vtk.vtkDataSet', self))
locator.BuildLocator()
id_list = _vtk.vtkIdList()
locator.FindCellsWithinBounds(list(bounds), id_list)
return vtk_id_list_to_array(id_list)
def get_cell(self: Self, index: int) -> pyvista.Cell:
"""Return a :class:`pyvista.Cell` object.
Parameters
----------
index : int
Cell ID.
Returns
-------
pyvista.Cell
The i-th pyvista.Cell.
Notes
-----
Cells returned from this method are deep copies of the original
cells. Changing properties (for example, ``points``) will not affect
the dataset they originated from.
Examples
--------
Get the 0-th cell.
>>> from pyvista import examples
>>> mesh = examples.load_airplane()
>>> cell = mesh.get_cell(0)
>>> cell
Cell ...
Get the point ids of the first cell
>>> cell.point_ids
[0, 1, 2]
Get the point coordinates of the first cell
>>> cell.points
array([[897.0, 48.8, 82.3],
[906.6, 48.8, 80.7],
[907.5, 55.5, 83.7]])
For the first cell, get the points associated with the first edge
>>> cell.edges[0].point_ids
[0, 1]
For a Tetrahedron, get the point ids of the last face
>>> mesh = examples.cells.Tetrahedron()
>>> cell = mesh.get_cell(0)
>>> cell.faces[-1].point_ids
[0, 2, 1]
"""
# must check upper bounds, otherwise segfaults (on Linux, 9.2)
if index + 1 > self.n_cells:
msg = f'Invalid index {index} for a dataset with {self.n_cells} cells.'
raise IndexError(msg)
# Note: we have to use vtkGenericCell here since
# GetCell(vtkIdType cellId, vtkGenericCell* cell) is thread-safe,
# while GetCell(vtkIdType cellId) is not.
cell = pyvista.Cell()
self.GetCell(index, cell)
cell.SetCellType(self.GetCellType(index))
return cell
@property
def cell(self: Self) -> Iterator[pyvista.Cell]:
"""A generator that provides an easy way to loop over all cells.
To access a single cell, use :func:`pyvista.DataSet.get_cell`.
.. versionchanged:: 0.39.0
Now returns a generator instead of a list.
Use ``get_cell(i)`` instead of ``cell[i]``.
Yields
------
pyvista.Cell
See Also
--------
pyvista.DataSet.get_cell
Examples
--------
Loop over the cells
>>> import pyvista as pv
>>> # Create a grid with 9 points and 4 cells
>>> mesh = pv.ImageData(dimensions=(3, 3, 1))
>>> for cell in mesh.cell: # doctest: +SKIP
... cell
"""
for i in range(self.n_cells):
yield self.get_cell(i)
def cell_neighbors(self: Self, ind: int, connections: str = 'points') -> list[int]:
"""Get the cell neighbors of the ind-th cell.
Concrete implementation of :vtk:`vtkDataSet.GetCellNeighbors`.
Parameters
----------
ind : int
Cell ID.
connections : str, default: "points"
Describe how the neighbor cell(s) must be connected to the current
cell to be considered as a neighbor.
Can be either ``'points'``, ``'edges'`` or ``'faces'``.
Returns
-------
list[int]
List of neighbor cells IDs for the ind-th cell.
Warnings
--------
For a :class:`pyvista.ExplicitStructuredGrid`, use
:func:`pyvista.ExplicitStructuredGrid.neighbors`.
See Also
--------
pyvista.DataSet.cell_neighbors_levels
Examples
--------
>>> from pyvista import examples
>>> mesh = examples.load_airplane()
Get the neighbor cell ids that have at least one point in common with
the 0-th cell.
>>> mesh.cell_neighbors(0, 'points')
[1, 2, 3, 388, 389, 11, 12, 395, 14, 209, 211, 212]
Get the neighbor cell ids that have at least one edge in common with
the 0-th cell.
>>> mesh.cell_neighbors(0, 'edges')
[1, 3, 12]
For unstructured grids with cells of dimension 3 (Tetrahedron for example),
cell neighbors can be defined using faces.
>>> mesh = examples.download_tetrahedron()
>>> mesh.cell_neighbors(0, 'faces')
[1, 5, 7]
Show a visual example.
>>> from functools import partial
>>> import pyvista as pv
>>> mesh = pv.Sphere(theta_resolution=10)
>>>
>>> pl = pv.Plotter(shape=(1, 2))
>>> pl.link_views()
>>> add_point_labels = partial(
... pl.add_point_labels,
... text_color='white',
... font_size=20,
... shape=None,
... show_points=False,
... )
>>>
>>> for i, connection in enumerate(['points', 'edges']):
... pl.subplot(0, i)
... pl.view_xy()
... _ = pl.add_title(
... f'{connection.capitalize()} neighbors',
... color='red',
... shadow=True,
... font_size=8,
... )
...
... # Add current cell
... i_cell = 0
... current_cell = mesh.extract_cells(i_cell)
... _ = pl.add_mesh(current_cell, show_edges=True, color='blue')
... _ = add_point_labels(
... current_cell.cell_centers().points,
... labels=[f'{i_cell}'],
... )
...
... # Add neighbors
... ids = mesh.cell_neighbors(i_cell, connection)
... cells = mesh.extract_cells(ids)
... _ = pl.add_mesh(cells, color='red', show_edges=True)
... _ = add_point_labels(
... cells.cell_centers().points,
... labels=[f'{i}' for i in ids],
... )
...
... # Add other cells
... ids.append(i_cell)
... others = mesh.extract_cells(ids, invert=True)
... _ = pl.add_mesh(others, show_edges=True)
>>> pl.show()
"""
if isinstance(self, _vtk.vtkExplicitStructuredGrid):
msg = 'For an ExplicitStructuredGrid, use the `neighbors` method' # type: ignore[unreachable]
raise TypeError(msg)
# Build links as recommended:
# https://vtk.org/doc/nightly/html/classvtkPolyData.html#adf9caaa01f72972d9a986ba997af0ac7
if hasattr(self, 'BuildLinks'):
self.BuildLinks()
needed = ['points', 'edges', 'faces']
if connections not in needed:
msg = f'`connections` must be one of: {needed} (got "{connections}")'
raise ValueError(msg)
cell = self.get_cell(ind)
iterators = {
'points': cell.point_ids,
'edges': range(cell.n_edges),
'faces': range(cell.n_faces),
}
def generate_ids(i: int, connections: str) -> _vtk.vtkIdList | None:
if connections == 'points':
ids = _vtk.vtkIdList()
ids.InsertNextId(i)
return ids
elif connections == 'edges':
return cell.get_edge(i).GetPointIds()
elif connections == 'faces':
return cell.get_face(i).GetPointIds()
return None # pragma: no cover
neighbors = set()
for i in iterators[connections]:
point_ids = generate_ids(i, connections)
cell_ids = _vtk.vtkIdList()
self.GetCellNeighbors(ind, point_ids, cell_ids)
neighbors.update([cell_ids.GetId(i) for i in range(cell_ids.GetNumberOfIds())])
return list(neighbors)
def point_neighbors(self: Self, ind: int) -> list[int]:
"""Get the point neighbors of the ind-th point.
Parameters
----------
ind : int
Point ID.
Returns
-------
list[int]
List of neighbor points IDs for the ind-th point.
See Also
--------
pyvista.DataSet.point_neighbors_levels
Examples
--------
Get the point neighbors of the 0-th point.
>>> import pyvista as pv
>>> mesh = pv.Sphere(theta_resolution=10)
>>> mesh.point_neighbors(0)
[2, 226, 198, 170, 142, 114, 86, 254, 58, 30]
Plot them.
>>> pl = pv.Plotter()
>>> _ = pl.add_mesh(mesh, show_edges=True)
>>>
>>> # Label the 0-th point
>>> _ = pl.add_point_labels(
... mesh.points[0], ['0'], text_color='blue', font_size=40
... )
>>>
>>> # Get the point neighbors and plot them
>>> neighbors = mesh.point_neighbors(0)
>>> _ = pl.add_point_labels(
... mesh.points[neighbors],
... labels=[f'{i}' for i in neighbors],
... text_color='red',
... font_size=40,
... )
>>> pl.camera_position = 'xy'
>>> pl.camera.zoom(7.0)
>>> pl.show()
"""
if ind + 1 > self.n_points:
msg = f'Invalid index {ind} for a dataset with {self.n_points} points.'
raise IndexError(msg)
out = []
for cell in self.point_cell_ids(ind):
out.extend([i for i in self.get_cell(cell).point_ids if i != ind])
return list(set(out))
def point_neighbors_levels(
self: Self,
ind: int,
n_levels: int = 1,
) -> Generator[list[int], None, None]:
"""Get consecutive levels of point neighbors.
Parameters
----------
ind : int
Point ID.
n_levels : int, default: 1
Number of levels to search for point neighbors.
When equal to 1, it is equivalent to :func:`pyvista.DataSet.point_neighbors`.
Returns
-------
generator[list[[int]]
A generator of list of neighbor points IDs for the ind-th point.
See Also
--------
pyvista.DataSet.point_neighbors
Examples
--------
Get the point neighbors IDs starting from the 0-th point
up until the third level.
>>> import pyvista as pv
>>> mesh = pv.Sphere(theta_resolution=10)
>>> pt_nbr_levels = mesh.point_neighbors_levels(0, 3)
>>> pt_nbr_levels = list(pt_nbr_levels)
>>> pt_nbr_levels[0]
[2, 226, 198, 170, 142, 114, 86, 30, 58, 254]
>>> pt_nbr_levels[1]
[3, 227, 255, 199, 171, 143, 115, 87, 59, 31]
>>> pt_nbr_levels[2]
[256, 32, 4, 228, 200, 172, 144, 116, 88, 60]
Visualize these points IDs.
>>> from functools import partial
>>> pl = pv.Plotter()
>>> _ = pl.add_mesh(mesh, show_edges=True)
>>>
>>> # Define partial function to add point labels
>>> add_point_labels = partial(
... pl.add_point_labels,
... text_color='white',
... font_size=40,
... point_size=10,
... )
>>>
>>> # Add the first point label
>>> _ = add_point_labels(mesh.points[0], labels=['0'], text_color='blue')
>>>
>>> # Add the neighbors to the plot
>>> neighbors = mesh.point_neighbors_levels(0, n_levels=3)
>>> for i, ids in enumerate(neighbors, start=1):
... _ = add_point_labels(
... mesh.points[ids],
... labels=[f'{i}'] * len(ids),
... text_color='red',
... )
>>>
>>> pl.view_xy()
>>> pl.camera.zoom(4.0)
>>> pl.show()
"""
method = self.point_neighbors
return self._get_levels_neihgbors(ind, n_levels, method)
def cell_neighbors_levels(
self: Self,
ind: int,
connections: str = 'points',
n_levels: int = 1,
) -> Generator[list[int], None, None]:
"""Get consecutive levels of cell neighbors.
Parameters
----------
ind : int
Cell ID.
connections : str, default: "points"
Describe how the neighbor cell(s) must be connected to the current
cell to be considered as a neighbor.
Can be either ``'points'``, ``'edges'`` or ``'faces'``.
n_levels : int, default: 1
Number of levels to search for cell neighbors.
When equal to 1, it is equivalent to :func:`pyvista.DataSet.cell_neighbors`.
Returns
-------
generator[list[int]]
A generator of list of cell IDs for each level.
Warnings
--------
For a :class:`pyvista.ExplicitStructuredGrid`, use
:func:`pyvista.ExplicitStructuredGrid.neighbors`.
See Also
--------
pyvista.DataSet.cell_neighbors
Examples
--------
Get the cell neighbors IDs starting from the 0-th cell
up until the third level.
>>> import pyvista as pv
>>> mesh = pv.Sphere(theta_resolution=10)
>>> nbr_levels = mesh.cell_neighbors_levels(0, connections='edges', n_levels=3)
>>> nbr_levels = list(nbr_levels)
>>> nbr_levels[0]
[1, 21, 9]
>>> nbr_levels[1]
[2, 8, 74, 75, 20, 507]
>>> nbr_levels[2]
[128, 129, 3, 453, 7, 77, 23, 506]
Visualize these cells IDs.
>>> from functools import partial
>>> pv.global_theme.color_cycler = [
... 'red',
... 'green',
... 'blue',
... 'purple',
... ]
>>> pl = pv.Plotter()
>>>
>>> # Define partial function to add point labels
>>> add_point_labels = partial(
... pl.add_point_labels,
... text_color='white',
... font_size=40,
... shape=None,
... show_points=False,
... )
>>>
>>> # Add the 0-th cell to the plotter
>>> cell = mesh.extract_cells(0)
>>> _ = pl.add_mesh(cell, show_edges=True)
>>> _ = add_point_labels(cell.cell_centers().points, labels=['0'])
>>> other_ids = [0]
>>>
>>> # Add the neighbors to the plot
>>> neighbors = mesh.cell_neighbors_levels(0, connections='edges', n_levels=3)
>>> for i, ids in enumerate(neighbors, start=1):
... cells = mesh.extract_cells(ids)
... _ = pl.add_mesh(cells, show_edges=True)
... _ = add_point_labels(
... cells.cell_centers().points, labels=[f'{i}'] * len(ids)
... )
... other_ids.extend(ids)
>>>
>>> # Add the cell IDs that are not neighbors (ie. the rest of the sphere)
>>> cells = mesh.extract_cells(other_ids, invert=True)
>>> _ = pl.add_mesh(cells, color='white', show_edges=True)
>>>
>>> pl.view_xy()
>>> pl.camera.zoom(6.0)
>>> pl.show()
"""
method = partial(self.cell_neighbors, connections=connections)
return self._get_levels_neihgbors(ind, n_levels, method)
def _get_levels_neihgbors(
self: Self,
ind: int,
n_levels: int,
method: Callable[[Any], Any],
) -> Generator[list[int], None, None]: # numpydoc ignore=PR01,RT01
"""Provide helper method that yields neighbors ids."""
neighbors = set(method(ind))
yield list(neighbors)
# Keep track of visited points or cells
all_visited = neighbors.copy()
all_visited.add(ind)
for _ in range(n_levels - 1):
# Get the neighbors for the next level.
new_visited = set()
for n in neighbors:
new_neighbors = method(n)
new_visited.update(new_neighbors)
neighbors = new_visited
# Only return the ones that have not been visited yet
yield list(neighbors.difference(all_visited))
all_visited.update(neighbors)
def point_cell_ids(self: Self, ind: int) -> list[int]:
"""Get the cell IDs that use the ind-th point.
Implements :vtk:`vtkDataSet.GetPointCells`.
Parameters
----------
ind : int
Point ID.
Returns
-------
list[int]
List of cell IDs using the ind-th point.
Examples
--------
Get the cell ids using the 0-th point.
>>> import pyvista as pv
>>> mesh = pv.Sphere(theta_resolution=10)
>>> mesh.point_cell_ids(0)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Plot them.
>>> pl = pv.Plotter()
>>> _ = pl.add_mesh(mesh, show_edges=True)
>>>
>>> # Label the 0-th point
>>> _ = pl.add_point_labels(
... mesh.points[0], ['0'], text_color='blue', font_size=20
... )
>>>
>>> # Get the cells ids using the 0-th point
>>> ids = mesh.point_cell_ids(0)
>>> cells = mesh.extract_cells(ids)
>>> _ = pl.add_mesh(cells, color='red', show_edges=True)
>>> centers = cells.cell_centers().points
>>> _ = pl.add_point_labels(
... centers,
... labels=[f'{i}' for i in ids],
... text_color='white',
... font_size=20,
... shape=None,
... show_points=False,
... )
>>>
>>> # Plot the other cells
>>> others = mesh.extract_cells(
... [i for i in range(mesh.n_cells) if i not in ids]
... )
>>> _ = pl.add_mesh(others, show_edges=True)
>>>
>>> pl.camera_position = 'xy'
>>> pl.camera.zoom(7.0)
>>> pl.show()
"""
# Build links as recommended:
# https://vtk.org/doc/nightly/html/classvtkPolyData.html#adf9caaa01f72972d9a986ba997af0ac7
if hasattr(self, 'BuildLinks'):
self.BuildLinks()
ids = _vtk.vtkIdList()
self.GetPointCells(ind, ids)
out = [ids.GetId(i) for i in range(ids.GetNumberOfIds())]
if (9, 4, 0) <= pyvista.vtk_version_info < (9, 5, 0):
# Need to reverse the order
return out[::-1]
return out
def point_is_inside_cell(
self: Self,
ind: int,
point: VectorLike[float] | MatrixLike[float],
) -> bool | NumpyArray[np.bool_]:
"""Return whether one or more points are inside a cell.
.. versionadded:: 0.35.0
Parameters
----------
ind : int
Cell ID.
point : VectorLike[float] | MatrixLike[float]
Point or points to query if are inside a cell.
Returns
-------
bool or numpy.ndarray
Whether point(s) is/are inside cell. A single bool is only returned if
the input point has shape ``(3,)``.
Examples
--------
>>> from pyvista import examples
>>> mesh = examples.load_hexbeam()
>>> mesh.get_cell(0).bounds
BoundsTuple(x_min = 0.0,
x_max = 0.5,
y_min = 0.0,
y_max = 0.5,
z_min = 0.0,
z_max = 0.5)
>>> mesh.point_is_inside_cell(0, [0.2, 0.2, 0.2])
True
"""
if not isinstance(ind, (int, np.integer)):
msg = f'ind must be an int, got {type(ind)}' # type: ignore[unreachable]
raise TypeError(msg)
if not 0 <= ind < self.n_cells:
msg = f'ind must be >= 0 and < {self.n_cells}, got {ind}'
raise ValueError(msg)
co_point, singular = _coerce_pointslike_arg(point, copy=False)
cell = self.GetCell(ind)
npoints = cell.GetPoints().GetNumberOfPoints()
closest_point = [0.0, 0.0, 0.0]
sub_id = _vtk.mutable(0)
pcoords = [0.0, 0.0, 0.0]
dist2 = _vtk.mutable(0.0)
weights = [0.0] * npoints
in_cell = np.empty(shape=co_point.shape[0], dtype=np.bool_)
for i, node in enumerate(co_point):
is_inside = cell.EvaluatePosition(node, closest_point, sub_id, pcoords, dist2, weights)
if not 0 <= is_inside <= 1:
msg = f'Computational difficulty encountered for point {node} in cell {ind}'
raise RuntimeError(msg)
in_cell[i] = bool(is_inside)
if singular:
return in_cell[0].item()
return in_cell
@property
def active_texture_coordinates(self: Self) -> pyvista_ndarray | None:
"""Return the active texture coordinates on the points.
Returns
-------
Optional[pyvista_ndarray]
Active texture coordinates on the points.
Examples
--------
Return the active texture coordinates from the globe example.
>>> from pyvista import examples
>>> globe = examples.load_globe()
>>> globe.active_texture_coordinates
pyvista_ndarray([[0. , 0. ],
[0. , 0.07142857],
[0. , 0.14285714],
...,
[1. , 0.85714286],
[1. , 0.92857143],
[1. , 1. ]], shape=(540, 2))
"""
return self.point_data.active_texture_coordinates
@active_texture_coordinates.setter
def active_texture_coordinates(
self: Self,
texture_coordinates: NumpyArray[float],
) -> None:
"""Set the active texture coordinates on the points.
Parameters
----------
texture_coordinates : np.ndarray
Active texture coordinates on the points.
"""
self.point_data.active_texture_coordinates = texture_coordinates
@property
def is_empty(self) -> bool: # numpydoc ignore=RT01
"""Return ``True`` if there are no points.
.. versionadded:: 0.45
Examples
--------
>>> import pyvista as pv
>>> mesh = pv.PolyData()
>>> mesh.is_empty
True
>>> mesh = pv.Sphere()
>>> mesh.is_empty
False
"""
return self.n_points == 0
|