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
|
"""Module managing picking events."""
from __future__ import annotations
from functools import partial
from functools import wraps
from typing import cast
import warnings
import weakref
import numpy as np
import pyvista
from pyvista.core.errors import PyVistaDeprecationWarning
from pyvista.core.utilities.misc import try_callback
from . import _vtk
from .composite_mapper import CompositePolyDataMapper
from .errors import PyVistaPickingError
from .opts import ElementType
from .opts import PickerType
PICKED_REPRESENTATION_NAMES = {
'point': '_picked_point',
'mesh': '_picked_mesh',
'through': '_picked_through_selection',
'visible': '_picked_visible_selection',
'element': '_picked_element',
'path': '_picked_path',
'horizon': '_picked_horizon',
'frustum': '_rectangle_selection_frustum',
}
def _launch_pick_event(interactor, _event):
"""Create a Pick event based on coordinate or left-click."""
click_x, click_y = interactor.GetEventPosition()
click_z = 0
picker = interactor.GetPicker()
renderer = interactor.GetInteractorStyle()._parent()._plotter.iren.get_poked_renderer()
picker.Pick(click_x, click_y, click_z, renderer)
def _poked_context_callback(plotter, *args, **kwargs):
"""Use _poked_context_callback in a poked renderer context."""
with plotter.iren.poked_subplot():
try_callback(*args, **kwargs)
class RectangleSelection:
"""Internal data structure for rectangle based selections.
Parameters
----------
frustum : _vtk.vtkPlanes
Frustum that defines the selection.
viewport : tuple[float, float, float, float]
The selected viewport coordinates, given as ``(x0, y0, x1, y1)``.
"""
def __init__(self, frustum, viewport):
self._frustum = frustum
self._viewport = viewport
@property
def frustum(self) -> _vtk.vtkPlanes: # numpydoc ignore=RT01
"""Get the selected frustum through the scene."""
return self._frustum
@property
def frustum_mesh(self) -> pyvista.PolyData: # numpydoc ignore=RT01
"""Get the frustum as a PyVista mesh."""
frustum_source = _vtk.vtkFrustumSource()
frustum_source.ShowLinesOff()
frustum_source.SetPlanes(self.frustum)
frustum_source.Update()
return cast(pyvista.PolyData, pyvista.wrap(frustum_source.GetOutput()))
@property
def viewport(self) -> tuple[float, float, float, float]: # numpydoc ignore=RT01
"""Get the selected viewport coordinates.
Coordinates are given as: ``(x0, y0, x1, y1)``
"""
return self._viewport
class PointPickingElementHandler:
"""Internal picking handler for element-based picking.
This handler is only valid for single point picking operations.
Parameters
----------
mode : ElementType, optional
The element type to pick.
callback : callable, optional
A callback function to be executed on picking events.
"""
def __init__(self, mode: ElementType = ElementType.CELL, callback=None):
self._picker_ = None
self.callback = callback
self.mode = ElementType.from_any(mode)
@property
def picker(self): # numpydoc ignore=RT01
"""Get or set the picker instance."""
return self._picker_()
@picker.setter
def picker(self, picker): # numpydoc ignore=GL08
self._picker_ = weakref.ref(picker)
def get_mesh(self):
"""Get the picked mesh.
Returns
-------
pyvista.DataSet
Picked mesh.
"""
ds = self.picker.GetDataSet()
if ds is not None:
return pyvista.wrap(ds)
return None
def get_cell(self, picked_point):
"""Get the picked cell of the picked mesh.
Parameters
----------
picked_point : sequence[float]
Coordinates of the picked point.
Returns
-------
pyvista.UnstructuredGrid
UnstructuredGrid containing the picked cell.
"""
mesh = self.get_mesh()
# cell_id = self.picker.GetCellId()
cell_id = mesh.find_containing_cell(picked_point) # more accurate
if cell_id < 0:
return None # TODO: this happens but shouldn't # pragma: no cover
cell = mesh.extract_cells(cell_id)
cell.cell_data['vtkOriginalCellIds'] = np.array([cell_id])
return cell
def get_face(self, picked_point):
"""Get the picked face of the picked cell.
Parameters
----------
picked_point : sequence[float]
Coordinates of the picked point.
Returns
-------
pyvista.UnstructuredGrid
UnstructuredGrid containing the picked face.
"""
cell = self.get_cell(picked_point).get_cell(0)
if cell.n_faces > 1:
for face in cell.faces:
contains = face.cast_to_unstructured_grid().find_containing_cell(picked_point)
if contains > -1:
break
if contains < 0:
# this shouldn't happen
raise RuntimeError('Trouble aligning point with face.')
face = face.cast_to_unstructured_grid()
face.field_data['vtkOriginalFaceIds'] = np.array([len(cell.faces) - 1])
else:
face = cell.cast_to_unstructured_grid()
face.field_data['vtkOriginalFaceIds'] = np.array([0])
return face
def get_edge(self, picked_point):
"""Get the picked edge of the picked cell.
Parameters
----------
picked_point : sequence[float]
Coordinates of the picked point.
Returns
-------
pyvista.UnstructuredGrid
UnstructuredGrid containing the picked edge.
"""
cell = self.get_cell(picked_point).get_cell(0)
if cell.n_edges > 1:
ei = (
cell.cast_to_unstructured_grid().extract_all_edges().find_closest_cell(picked_point)
)
edge = cell.edges[ei].cast_to_unstructured_grid()
edge.field_data['vtkOriginalEdgeIds'] = np.array([ei])
else:
edge = cell.cast_to_unstructured_grid()
return edge
def get_point(self, picked_point):
"""Get the picked point of the picked mesh.
Parameters
----------
picked_point : sequence[float]
Coordinates of the picked point.
Returns
-------
pyvista.PolyData
Picked mesh containing the point.
"""
mesh = self.get_mesh()
pid = mesh.find_closest_point(picked_point)
picked = pyvista.PolyData(mesh.points[pid])
picked.point_data['vtkOriginalPointIds'] = np.array([pid])
return picked
def __call__(self, picked_point, picker):
"""Perform the pick."""
self.picker = picker
mesh = self.get_mesh()
if mesh is None:
return # No selected mesh (point not on surface of mesh)
if self.mode == ElementType.MESH:
picked = mesh
elif self.mode == ElementType.CELL:
picked = self.get_cell(picked_point)
if picked is None:
return # TODO: handle
elif self.mode == ElementType.FACE:
picked = self.get_face(picked_point)
elif self.mode == ElementType.EDGE:
picked = self.get_edge(picked_point)
elif self.mode == ElementType.POINT:
picked = self.get_point(picked_point)
if self.callback:
try_callback(self.callback, picked)
class PickingInterface: # numpydoc ignore=PR01
"""An internal class to hold core picking related features."""
def __init__(self, *args, **kwargs):
"""Initialize the picking interface."""
super().__init__(*args, **kwargs)
self._picking_left_clicking_observer = None
self._picking_right_clicking_observer = None
self._picker_in_use = False
self._picked_point = None
def _clear_picking_representations(self):
"""Clear all picking representations."""
for name in PICKED_REPRESENTATION_NAMES.values():
self.remove_actor(name)
@property
def picked_point(self): # numpydoc ignore=RT01
"""Return the picked point.
This returns the picked point after selecting a point.
Returns
-------
numpy.ndarray or None
Picked point if available.
"""
return self._picked_point
def get_pick_position(self):
"""Get the pick position or area.
Returns
-------
sequence
Picked position or area as ``(x0, y0, x1, y1)``.
"""
renderer = self.iren.get_poked_renderer()
return renderer.get_pick_position()
def pick_click_position(self):
"""Get corresponding click location in the 3D plot.
Returns
-------
tuple
Three item tuple with the 3D picked position.
"""
if self.click_position is None:
self.store_click_position()
renderer = self.iren.get_poked_renderer()
self.iren.picker.Pick(self.click_position[0], self.click_position[1], 0, renderer)
return self.iren.picker.GetPickPosition()
def pick_mouse_position(self):
"""Get corresponding mouse location in the 3D plot.
Returns
-------
tuple
Three item tuple with the 3D picked position.
"""
if self.mouse_position is None:
self.store_mouse_position()
renderer = self.iren.get_poked_renderer()
self.iren.picker.Pick(self.mouse_position[0], self.mouse_position[1], 0, renderer)
return self.iren.picker.GetPickPosition()
def _init_click_picking_callback(self, left_clicking=False):
if left_clicking:
self._picking_left_clicking_observer = self.iren.add_observer(
"LeftButtonPressEvent",
partial(try_callback, _launch_pick_event),
)
else:
self._picking_right_clicking_observer = self.iren.add_observer(
"RightButtonPressEvent",
partial(try_callback, _launch_pick_event),
)
def disable_picking(self):
"""Disable any active picking and remove observers.
Examples
--------
Enable and then disable picking.
>>> import pyvista as pv
>>> mesh = pv.Sphere(center=(1, 0, 0))
>>> cube = pv.Cube()
>>> pl = pv.Plotter()
>>> _ = pl.add_mesh(mesh)
>>> _ = pl.add_mesh(cube)
>>> _ = pl.enable_mesh_picking()
>>> pl.disable_picking()
"""
# remove left and right clicking observer if available
if getattr(self, 'iren', None):
self.iren.remove_observer(self._picking_left_clicking_observer)
self.iren.remove_observer(self._picking_right_clicking_observer)
# Reset to default picker
self.iren.reset_picker()
self._picking_left_clicking_observer = None
self._picking_right_clicking_observer = None
self._picker_in_use = False
def _validate_picker_not_in_use(self):
if self._picker_in_use:
raise PyVistaPickingError(
'Picking is already enabled, please disable previous picking with `disable_picking()`.',
)
def enable_point_picking(
self,
callback=None,
tolerance=0.025,
left_clicking=False,
picker=PickerType.POINT,
show_message=True,
font_size=18,
color='pink',
point_size=10,
show_point=True,
use_picker=False,
pickable_window=False,
clear_on_no_selection=True,
**kwargs,
):
"""Enable picking at points under the cursor.
Enable picking a point at the mouse location in the render
view using the right mouse button. This point is saved to the
``.picked_point`` attribute on the plotter. Pass a callback
that takes that point as an argument. The picked
point can either be a point on the first intersecting mesh, or
a point in the 3D window.
The ``picker`` choice will help determine how the point picking
is performed.
Parameters
----------
callback : callable, optional
When input, calls this callable after a pick is made. The
picked point is input as the first parameter to this
callable.
tolerance : float, tolerance: 0.025
Specify tolerance for performing pick operation. Tolerance
is specified as fraction of rendering window
size. Rendering window size is measured across diagonal.
This is only valid for some choices of ``picker``.
left_clicking : bool, default: False
When ``True``, points can be picked by clicking the left mouse
button. Default is to use the right mouse button.
picker : str | PickerType, optional
Choice of VTK picker class type:
* ``'hardware'``: Uses ``vtkHardwarePicker`` which is more
performant for large geometries (default).
* ``'cell'``: Uses ``vtkCellPicker``.
* ``'point'``: Uses ``vtkPointPicker`` which will snap to
points on the surface of the mesh.
* ``'volume'``: Uses ``vtkVolumePicker``.
show_message : bool | str, default: True
Show the message about how to use the point picking
tool. If this is a string, that will be the message shown.
font_size : int, default: 18
Sets the size of the message.
color : ColorLike, default: "pink"
The color of the selected mesh when shown.
point_size : int, default: 10
Size of picked points if ``show_point`` is ``True``.
show_point : bool, default: True
Show the picked point after clicking.
use_picker : bool, default: False
When ``True``, the callback will also be passed the picker.
pickable_window : bool, default: False
When ``True`` and the chosen picker supports it, points in the
3D window are pickable.
clear_on_no_selection : bool, default: True
Clear the selections when no point is selected.
**kwargs : dict, optional
All remaining keyword arguments are used to control how
the picked point is interactively displayed.
Examples
--------
Enable point picking with a custom message.
>>> import pyvista as pv
>>> pl = pv.Plotter()
>>> _ = pl.add_mesh(pv.Sphere())
>>> _ = pl.add_mesh(pv.Cube(), pickable=False)
>>> pl.enable_point_picking(show_message='Pick a point')
See :ref:`point_picking_example` for a full example using this method.
"""
self._validate_picker_not_in_use()
if 'use_mesh' in kwargs:
warnings.warn(
'`use_mesh` is deprecated. See `use_picker` instead.',
PyVistaDeprecationWarning,
)
use_mesh = kwargs.pop('use_mesh')
else:
use_mesh = False
self_ = weakref.ref(self)
def _end_pick_event(picker, _event):
if (
not pickable_window
and hasattr(picker, 'GetDataSet')
and picker.GetDataSet() is None
):
# Clear the selection
self._picked_point = None
if clear_on_no_selection:
with self_().iren.poked_subplot():
self_()._clear_picking_representations()
return
with self_().iren.poked_subplot():
point = np.array(picker.GetPickPosition())
point /= self_().scale # HACK: handle scale
self_()._picked_point = point
if show_point:
_kwargs = kwargs.copy()
self_().add_mesh(
self_().picked_point,
color=color,
point_size=point_size,
name=_kwargs.pop('name', PICKED_REPRESENTATION_NAMES['point']),
pickable=_kwargs.pop('pickable', False),
reset_camera=_kwargs.pop('reset_camera', False),
**_kwargs,
)
if callable(callback):
if use_picker:
_poked_context_callback(self_(), callback, self.picked_point, picker)
elif use_mesh: # Lower priority
_poked_context_callback(
self_(),
callback,
picker.GetDataSet(),
picker.GetPointId(),
)
else:
_poked_context_callback(self_(), callback, self.picked_point)
if picker is not None: # If None, that means use already set picker
self.iren.picker = picker
if hasattr(self.iren.picker, 'SetTolerance'):
self.iren.picker.SetTolerance(tolerance)
self.iren.add_pick_observer(_end_pick_event)
self._init_click_picking_callback(left_clicking=left_clicking)
self._picker_in_use = True
# Now add text about cell-selection
if show_message:
if show_message is True:
show_message = 'Left-click' if left_clicking else 'Right-click'
show_message += ' or press P to pick under the mouse'
self._picking_text = self.add_text(
str(show_message),
font_size=font_size,
name='_point_picking_message',
)
def enable_rectangle_picking(
self,
callback=None,
show_message=True,
font_size=18,
start=False,
show_frustum=False,
style='wireframe',
color='pink',
**kwargs,
):
"""Enable rectangle based picking at cells.
Press ``"r"`` to enable rectangle based selection. Press
``"r"`` again to turn it off.
Picking with the rectangle selection tool provides two values that
are passed as the ``RectangleSelection`` object in the callback:
1. ``RectangleSelection.viewport``: the viewport coordinates of the
selection rectangle.
2. ``RectangleSelection.frustum``: the full frustum made from
the selection rectangle into the scene.
Parameters
----------
callback : callable, optional
When input, calls this callable after a selection is made.
The ``RectangleSelection`` is the only passed argument
containing the viewport coordinates of the selection and the
projected frustum.
show_message : bool | str, default: True
Show the message about how to use the cell picking tool. If this
is a string, that will be the message shown.
font_size : int, default: 18
Sets the font size of the message.
start : bool, default: True
Automatically start the cell selection tool.
show_frustum : bool, default: False
Show the frustum in the scene.
style : str, default: "wireframe"
Visualization style of the selection frustum. One of the
following: ``style='surface'``, ``style='wireframe'``, or
``style='points'``.
color : ColorLike, default: "pink"
The color of the selected frustum when shown.
**kwargs : dict, optional
All remaining keyword arguments are used to control how
the selection frustum is interactively displayed.
Examples
--------
Add a mesh and a cube to a plot and enable cell picking.
>>> import pyvista as pv
>>> mesh = pv.Sphere(center=(1, 0, 0))
>>> cube = pv.Cube()
>>> pl = pv.Plotter()
>>> _ = pl.add_mesh(mesh)
>>> _ = pl.add_mesh(cube)
>>> _ = pl.enable_rectangle_picking()
"""
self._validate_picker_not_in_use()
self_ = weakref.ref(self)
def _end_pick_helper(picker, *args):
renderer = picker.GetRenderer() # TODO: double check this is poked renderer
x0 = int(renderer.GetPickX1())
x1 = int(renderer.GetPickX2())
y0 = int(renderer.GetPickY1())
y1 = int(renderer.GetPickY2())
selection = RectangleSelection(frustum=picker.GetFrustum(), viewport=(x0, y0, x1, y1))
if show_frustum:
with self_().iren.poked_subplot():
_kwargs = kwargs.copy()
self_().add_mesh(
selection.frustum_mesh,
name=_kwargs.pop('name', PICKED_REPRESENTATION_NAMES['frustum']),
style=style,
color=color,
pickable=_kwargs.pop('pickable', False),
reset_camera=_kwargs.pop('reset_camera', False),
**_kwargs,
)
if callback is not None:
_poked_context_callback(self_(), callback, selection)
self.enable_rubber_band_style() # TODO: better handle?
self.iren.picker = 'rendered'
self.iren.add_pick_observer(_end_pick_helper)
self._picker_in_use = True
# Now add text about cell-selection
if show_message:
if show_message is True:
show_message = "Press R to toggle selection tool"
self._picking_text = self.add_text(
str(show_message),
font_size=font_size,
name='_rectangle_picking_message',
)
if start:
self.iren._style_class.StartSelect()
class PickingMethods(PickingInterface): # numpydoc ignore=PR01
"""Internal class to contain picking utilities."""
def __init__(self, *args, **kwargs):
"""Initialize the picking methods."""
super().__init__(*args, **kwargs)
self._picked_actor = None
self._picked_mesh = None
self._picked_cell = None
self._picking_text = None
self._picked_block_index = None
@property
def picked_actor(self): # numpydoc ignore=RT01
"""Return the picked mesh.
This returns the picked actor after selecting a mesh with
:func:`enable_surface_point_picking <pyvista.Plotter.enable_surface_point_picking>` or
:func:`enable_mesh_picking <pyvista.Plotter.enable_mesh_picking>`.
Returns
-------
pyvista.Actor or None
Picked actor if available.
"""
return self._picked_actor
@property
def picked_mesh(self): # numpydoc ignore=RT01
"""Return the picked mesh.
This returns the picked mesh after selecting a mesh with
:func:`enable_surface_point_picking <pyvista.Plotter.enable_surface_point_picking>` or
:func:`enable_mesh_picking <pyvista.Plotter.enable_mesh_picking>`.
Returns
-------
pyvista.DataSet or None
Picked mesh if available.
"""
return self._picked_mesh
@property
def picked_cell(self): # numpydoc ignore=RT01
"""Return the picked cell.
This returns the picked cell after selecting a cell.
Returns
-------
pyvista.Cell or None
Picked cell if available.
"""
return self._picked_cell
@property
def picked_cells(self): # numpydoc ignore=RT01
"""Return the picked cells.
This returns the picked cells after selecting cells.
Returns
-------
pyvista.Cell or None
Picked cell if available.
"""
return self._picked_cell
@property
def picked_block_index(self): # numpydoc ignore=RT01
"""Return the picked block index.
This returns the picked block index after selecting a point with
:func:`enable_point_picking <pyvista.Plotter.enable_point_picking>`.
Returns
-------
int or None
Picked block if available. If ``-1``, then a non-composite dataset
was selected.
"""
return self._picked_block_index
@wraps(PickingInterface.disable_picking)
def disable_picking(self):
"""Disable picking."""
super().disable_picking()
# remove any picking text
if hasattr(self, 'renderers'):
for renderer in self.renderers:
renderer.remove_actor(self._picking_text, render=False)
self._picking_text = None
def enable_surface_point_picking(
self,
callback=None,
show_message=True,
font_size=18,
color='pink',
show_point=True,
point_size=10,
tolerance=0.025,
pickable_window=False,
left_clicking=False,
picker=PickerType.CELL,
use_picker=False,
clear_on_no_selection=True,
**kwargs,
):
"""Enable picking of a point on the surface of a mesh.
Parameters
----------
callback : callable, optional
When input, calls this callable after a selection is made. The
``mesh`` is input as the first parameter to this callable.
show_message : bool | str, default: True
Show the message about how to use the mesh picking tool. If this
is a string, that will be the message shown.
font_size : int, default: 18
Sets the font size of the message.
color : ColorLike, default: "pink"
The color of the selected mesh when shown.
show_point : bool, default: True
Show the selection interactively.
point_size : int, default: 10
Size of picked points if ``show_point`` is ``True``.
tolerance : float, default: 0.025
Specify tolerance for performing pick operation. Tolerance
is specified as fraction of rendering window
size. Rendering window size is measured across diagonal.
.. warning::
This is ignored with the ``'hardware'`` ``picker``.
pickable_window : bool, default: False
When ``True``, points in the 3D window are pickable.
left_clicking : bool, default: False
When ``True``, meshes can be picked by clicking the left
mousebutton.
.. note::
If enabled, left-clicking will **not** display the bounding box
around the picked mesh.
picker : str | PickerType, optional
Choice of VTK picker class type:
* ``'hardware'``: Uses ``vtkHardwarePicker`` which is more
performant for large geometries (default).
* ``'cell'``: Uses ``vtkCellPicker``.
* ``'point'``: Uses ``vtkPointPicker`` which will snap to
points on the surface of the mesh.
* ``'volume'``: Uses ``vtkVolumePicker``.
use_picker : bool, default: False
When ``True``, the callback will also be passed the picker.
clear_on_no_selection : bool, default: True
Clear the selections when no point is selected.
**kwargs : dict, optional
All remaining keyword arguments are used to control how
the picked path is interactively displayed.
Notes
-----
Picked point can be accessed from :attr:`picked_point
<pyvista.Plotter.picked_point>` attribute.
Examples
--------
Add a cube to a plot and enable cell picking.
>>> import pyvista as pv
>>> cube = pv.Cube()
>>> pl = pv.Plotter()
>>> _ = pl.add_mesh(cube)
>>> _ = pl.enable_surface_point_picking()
See :ref:`surface_point_picking_example` for a full example using this method.
"""
# only allow certain pickers to be used for surface picking
# the picker class needs to have `GetDataSet()`
picker = PickerType.from_any(picker)
valid_pickers = [PickerType.POINT, PickerType.CELL, PickerType.HARDWARE, PickerType.VOLUME]
if picker not in valid_pickers:
raise ValueError(
f'Invalid picker choice for surface picking. Use one of: {valid_pickers}',
)
self_ = weakref.ref(self)
def _end_pick_event(picked_point, picker):
if not pickable_window and picker.GetActor() is None:
self_()._picked_point = None
self_()._picked_actor = None
self_()._picked_mesh = None
if clear_on_no_selection:
with self_().iren.poked_subplot():
self_()._clear_picking_representations()
return
self_()._picked_actor = picker.GetActor()
self_()._picked_mesh = picker.GetDataSet()
if show_point:
with self_().iren.poked_subplot():
_kwargs = kwargs.copy()
self_().add_mesh(
picked_point,
color=color,
point_size=point_size,
name=_kwargs.pop('name', PICKED_REPRESENTATION_NAMES['point']),
pickable=_kwargs.pop('pickable', False),
reset_camera=_kwargs.pop('reset_camera', False),
**_kwargs,
)
if callable(callback):
if use_picker:
_poked_context_callback(self_(), callback, picked_point, picker)
else:
_poked_context_callback(self_(), callback, picked_point)
self.enable_point_picking(
callback=_end_pick_event,
picker=picker,
show_point=False,
show_message=show_message,
left_clicking=left_clicking,
use_picker=True,
font_size=font_size,
tolerance=tolerance,
pickable_window=True, # let this callback handle pickable window
clear_on_no_selection=clear_on_no_selection,
)
def enable_mesh_picking(
self,
callback=None,
show=True,
show_message=True,
style='wireframe',
line_width=5,
color='pink',
font_size=18,
left_clicking=False,
use_actor=False,
picker=PickerType.CELL,
**kwargs,
):
"""Enable picking of a mesh.
Parameters
----------
callback : callable, optional
When input, calls this callable after a selection is made. The
``mesh`` is input as the first parameter to this callable.
show : bool, default: True
Show the selection interactively. Best when combined with
``left_clicking``.
show_message : bool | str, default: True
Show the message about how to use the mesh picking tool. If this
is a string, that will be the message shown.
style : str, default: "wireframe"
Visualization style of the selection. One of the following:
* ``'surface'``
* ``'wireframe'``
* ``'points'``
line_width : float, default: 5.0
Thickness of selected mesh edges.
color : ColorLike, default: "pink"
The color of the selected mesh when shown.
font_size : int, default: 18
Sets the font size of the message.
left_clicking : bool, default: False
When ``True``, meshes can be picked by clicking the left
mousebutton.
.. note::
If enabled, left-clicking will **not** display the bounding box
around the picked point.
use_actor : bool, default: False
If True, the callback will be passed the picked actor instead of
the mesh object.
picker : str | PickerType, optional
Choice of VTK picker class type:
* ``'hardware'``: Uses ``vtkHardwarePicker`` which is more
performant for large geometries (default).
* ``'cell'``: Uses ``vtkCellPicker``.
* ``'point'``: Uses ``vtkPointPicker`` which will snap to
points on the surface of the mesh.
* ``'volume'``: Uses ``vtkVolumePicker``.
**kwargs : dict, optional
All remaining keyword arguments are used to control how
the picked path is interactively displayed.
Returns
-------
vtk.vtkPropPicker
Property picker.
Examples
--------
Add a sphere and a cube to a plot and enable mesh picking. Enable
``left_clicking`` to immediately start picking on the left click and
disable showing the box. You can still press the ``p`` key to select
meshes.
>>> import pyvista as pv
>>> mesh = pv.Sphere(center=(1, 0, 0))
>>> cube = pv.Cube()
>>> pl = pv.Plotter()
>>> _ = pl.add_mesh(mesh)
>>> _ = pl.add_mesh(cube)
>>> _ = pl.enable_mesh_picking()
See :ref:`mesh_picking_example` for a full example using this method.
"""
self_ = weakref.ref(self)
def end_pick_call_back(*args): # numpydoc ignore=GL08
if callback:
if use_actor:
_poked_context_callback(self_(), callback, self_()._picked_actor)
else:
_poked_context_callback(self_(), callback, self_()._picked_mesh)
if show:
# Select the renderer where the mesh is added.
active_renderer_index = self_().renderers._active_index
loc = self_().iren.get_event_subplot_loc()
self_().subplot(*loc)
# Use try in case selection is empty or invalid
try:
with self_().iren.poked_subplot():
_kwargs = kwargs.copy()
self_().add_mesh(
self_()._picked_mesh,
name=_kwargs.pop('name', PICKED_REPRESENTATION_NAMES['mesh']),
style=style,
color=color,
line_width=line_width,
pickable=_kwargs.pop('pickable', False),
reset_camera=_kwargs.pop('reset_camera', False),
**_kwargs,
)
except Exception as e: # pragma: no cover
warnings.warn("Unable to show mesh when picking:\n\n%s", str(e))
# Reset to the active renderer.
loc = self_().renderers.index_to_loc(active_renderer_index)
self_().subplot(*loc)
# render here prior to running the callback
self_().render()
# add on-screen message about point-selection
if show_message:
if show_message is True:
show_message = 'Left-click' if left_clicking else 'Right-click'
show_message += ' or press P to pick single dataset under the mouse pointer'
self.enable_surface_point_picking(
callback=end_pick_call_back,
picker=picker,
show_point=False,
show_message=show_message,
left_clicking=left_clicking,
use_picker=True,
font_size=font_size,
pickable_window=False,
)
def enable_rectangle_through_picking(
self,
callback=None,
show=True,
style='wireframe',
line_width=5,
color='pink',
show_message=True,
font_size=18,
start=False,
show_frustum=False,
**kwargs,
):
"""Enable rectangle based cell picking through the scene.
Parameters
----------
callback : callable, optional
When input, calls this callable after a selection is made.
The picked cells is the only passed argument.
show : bool, default: True
Show the selection interactively.
style : str, default: "wireframe"
Visualization style of the selection frustum. One of the
following: ``style='surface'``, ``style='wireframe'``, or
``style='points'``.
line_width : float, default: 5.0
Thickness of selected mesh edges.
color : ColorLike, default: "pink"
The color of the selected frustum when shown.
show_message : bool | str, default: True
Show the message about how to use the cell picking tool. If this
is a string, that will be the message shown.
font_size : int, default: 18
Sets the font size of the message.
start : bool, default: True
Automatically start the cell selection tool.
show_frustum : bool, default: False
Show the frustum in the scene.
**kwargs : dict, optional
All remaining keyword arguments are used to control how
the selection frustum is interactively displayed.
"""
self_ = weakref.ref(self)
def finalize(picked): # numpydoc ignore=GL08
if picked is None:
# Indicates invalid pick
with self_().iren.poked_subplot():
self_()._clear_picking_representations()
return
self._picked_cell = picked
if show:
# Use try in case selection is empty
with self_().iren.poked_subplot():
_kwargs = kwargs.copy()
self_().add_mesh(
picked,
name=_kwargs.pop('name', PICKED_REPRESENTATION_NAMES['through']),
style=style,
color=color,
line_width=line_width,
pickable=_kwargs.pop('pickable', False),
reset_camera=_kwargs.pop('reset_camera', False),
**_kwargs,
)
if callback is not None:
_poked_context_callback(self_(), callback, self_().picked_cells)
def through_pick_callback(selection): # numpydoc ignore=GL08
picked = pyvista.MultiBlock()
renderer = self_().iren.get_poked_renderer()
for actor in renderer.actors.values():
if actor.GetMapper() and actor.GetPickable():
input_mesh = pyvista.wrap(actor.GetMapper().GetInputAsDataSet())
input_mesh.cell_data['orig_extract_id'] = np.arange(input_mesh.n_cells)
extract = _vtk.vtkExtractGeometry()
extract.SetInputData(input_mesh)
extract.SetImplicitFunction(selection.frustum)
extract.Update()
picked.append(pyvista.wrap(extract.GetOutput()))
if picked.n_blocks == 0 or picked.combine().n_cells < 1:
self_()._picked_cell = None
elif picked.n_blocks == 1:
self_()._picked_cell = picked[0]
else:
self_()._picked_cell = picked
finalize(self_()._picked_cell)
self.enable_rectangle_picking(
callback=through_pick_callback,
show_message=show_message,
font_size=font_size,
show_frustum=show_frustum,
start=start,
style=style,
color=color,
)
def enable_rectangle_visible_picking(
self,
callback=None,
show=True,
style='wireframe',
line_width=5,
color='pink',
show_message=True,
font_size=18,
start=False,
show_frustum=False,
**kwargs,
):
"""Enable rectangle based cell picking on visible surfaces.
Parameters
----------
callback : callable, optional
When input, calls this callable after a selection is made.
The picked cells is the only passed argument.
show : bool, default: True
Show the selection interactively.
style : str, default: "wireframe"
Visualization style of the selection frustum. One of the
following: ``style='surface'``, ``style='wireframe'``, or
``style='points'``.
line_width : float, default: 5.0
Thickness of selected mesh edges.
color : ColorLike, default: "pink"
The color of the selected frustum when shown.
show_message : bool | str, default: True
Show the message about how to use the cell picking tool. If this
is a string, that will be the message shown.
font_size : int, default: 18
Sets the font size of the message.
start : bool, default: True
Automatically start the cell selection tool.
show_frustum : bool, default: False
Show the frustum in the scene.
**kwargs : dict, optional
All remaining keyword arguments are used to control how
the selection frustum is interactively displayed.
"""
self_ = weakref.ref(self)
def finalize(picked): # numpydoc ignore=GL08
if picked is None:
# Indicates invalid pick
with self_().iren.poked_subplot():
self_()._clear_picking_representations()
return
if show:
# Use try in case selection is empty
with self_().iren.poked_subplot():
_kwargs = kwargs.copy()
self_().add_mesh(
picked,
name=_kwargs.pop('name', PICKED_REPRESENTATION_NAMES['visible']),
style=style,
color=color,
line_width=line_width,
pickable=_kwargs.pop('pickable', False),
reset_camera=_kwargs.pop('reset_camera', False),
**_kwargs,
)
if callback is not None:
_poked_context_callback(self_(), callback, picked)
def visible_pick_callback(selection): # numpydoc ignore=GL08
picked = pyvista.MultiBlock()
renderer = self_().iren.get_poked_renderer()
x0, y0, x1, y1 = renderer.get_pick_position()
# x0, y0, x1, y1 = selection.viewport
if x0 >= 0: # initial pick position is (-1, -1, -1, -1)
selector = _vtk.vtkOpenGLHardwareSelector()
selector.SetFieldAssociation(_vtk.vtkDataObject.FIELD_ASSOCIATION_CELLS)
selector.SetRenderer(renderer)
selector.SetArea(x0, y0, x1, y1)
selection = selector.Select()
for node in range(selection.GetNumberOfNodes()):
selection_node = selection.GetNode(node)
if selection_node is None: # pragma: no cover
# No selection
continue
cids = pyvista.convert_array(selection_node.GetSelectionList())
actor = selection_node.GetProperties().Get(_vtk.vtkSelectionNode.PROP())
# TODO: this is too hacky - find better way to avoid non-dataset actors
if not actor.GetMapper() or not hasattr(
actor.GetProperty(),
'GetRepresentation',
):
continue
# if not a surface
if actor.GetProperty().GetRepresentation() != 2: # pragma: no cover
warnings.warn(
"Display representations other than `surface` will result in incorrect results.",
)
smesh = pyvista.wrap(actor.GetMapper().GetInputAsDataSet())
smesh = smesh.copy()
smesh["original_cell_ids"] = np.arange(smesh.n_cells)
tri_smesh = smesh.extract_surface().triangulate()
cids_to_get = tri_smesh.extract_cells(cids)["original_cell_ids"]
picked.append(smesh.extract_cells(cids_to_get))
# memory leak issues on vtk==9.0.20210612.dev0
# See: https://gitlab.kitware.com/vtk/vtk/-/issues/18239#note_973826
selection.UnRegister(selection)
if len(picked) == 0 or picked.combine().n_cells < 1:
self_()._picked_cell = None
elif len(picked) == 1:
self_()._picked_cell = picked[0]
else:
self_()._picked_cell = picked
finalize(self_()._picked_cell)
self.enable_rectangle_picking(
callback=visible_pick_callback,
show_message=show_message,
font_size=font_size,
start=start,
show_frustum=show_frustum,
style=style,
color=color,
)
def enable_cell_picking(
self,
callback=None,
through=True,
show=True,
show_message=True,
style='wireframe',
line_width=5,
color='pink',
font_size=18,
start=False,
show_frustum=False,
**kwargs,
):
"""Enable picking of cells with a rectangle selection tool.
Press ``"r"`` to enable rectangle based selection. Press
``"r"`` again to turn it off. Selection will be saved to
``self.picked_cells``.
All meshes in the scene are available for picking by default.
If you would like to only pick a single mesh in the scene,
use the ``pickable=False`` argument when adding the other
meshes to the scene.
When multiple meshes are being picked, the picked cells
in ``self.picked_cells`` will be a :class:`MultiBlock`
dataset for each mesh's selection.
Uses last input mesh for input by default.
.. warning::
Visible cell picking (``through=False``) will only work if
the mesh is displayed with a ``'surface'`` representation
style (the default).
Parameters
----------
callback : callable, optional
When input, calls this callable after a selection is made.
The picked_cells are input as the first parameter to this
callable.
through : bool, default: True
When ``True`` the picker will select all cells
through the mesh(es). When ``False``, the picker will select
only visible cells on the selected surface(s).
show : bool, default: True
Show the selection interactively.
show_message : bool | str, default: True
Show the message about how to use the cell picking tool. If this
is a string, that will be the message shown.
style : str, default: "wireframe"
Visualization style of the selection. One of the
following: ``style='surface'``, ``style='wireframe'``, or
``style='points'``.
line_width : float, default: 5.0
Thickness of selected mesh edges.
color : ColorLike, default: "pink"
The color of the selected mesh when shown.
font_size : int, default: 18
Sets the font size of the message.
start : bool, default: True
Automatically start the cell selection tool.
show_frustum : bool, default: False
Show the frustum in the scene.
**kwargs : dict, optional
All remaining keyword arguments are used to control how
the selection is interactively displayed.
Examples
--------
Add a mesh and a cube to a plot and enable cell picking.
>>> import pyvista as pv
>>> mesh = pv.Sphere(center=(1, 0, 0))
>>> cube = pv.Cube()
>>> pl = pv.Plotter()
>>> _ = pl.add_mesh(mesh)
>>> _ = pl.add_mesh(cube)
>>> _ = pl.enable_cell_picking()
"""
if through:
method = self.enable_rectangle_through_picking
else:
method = self.enable_rectangle_visible_picking
method(
callback=callback,
show=show,
show_message=show_message,
style=style,
line_width=line_width,
color=color,
font_size=font_size,
start=start,
show_frustum=show_frustum,
**kwargs,
)
def enable_element_picking(
self,
callback=None,
mode='cell',
show=True,
show_message=True,
font_size=18,
color='pink',
tolerance=0.025,
pickable_window=False,
left_clicking=False,
picker=PickerType.CELL,
**kwargs,
):
"""Select individual elements on a mesh.
Parameters
----------
callback : callable, optional
When input, calls this callable after a selection is made. The
``mesh`` is input as the first parameter to this callable.
mode : str | ElementType, default: "cell"
The picking mode. Either ``"mesh"``, ``"cell"``, ``"face"``,
``"edge"``, or ``"point"``.
show : bool, default: True
Show the selection interactively.
show_message : bool | str, default: True
Show the message about how to use the mesh picking tool. If this
is a string, that will be the message shown.
font_size : int, default: 18
Sets the font size of the message.
color : ColorLike, default: "pink"
The color of the selected mesh when shown.
tolerance : float, default: 0.025
Specify tolerance for performing pick operation. Tolerance
is specified as fraction of rendering window
size. Rendering window size is measured across diagonal.
.. warning::
This is ignored with the ``'hardware'`` ``picker``.
pickable_window : bool, default: False
When ``True``, points in the 3D window are pickable.
left_clicking : bool, default: False
When ``True``, meshes can be picked by clicking the left
mousebutton.
.. note::
If enabled, left-clicking will **not** display the bounding box
around the picked mesh.
picker : str | PickerType, optional
Choice of VTK picker class type:
* ``'hardware'``: Uses ``vtkHardwarePicker`` which is more
performant for large geometries (default).
* ``'cell'``: Uses ``vtkCellPicker``.
* ``'point'``: Uses ``vtkPointPicker`` which will snap to
points on the surface of the mesh.
* ``'volume'``: Uses ``vtkVolumePicker``.
**kwargs : dict, optional
All remaining keyword arguments are used to control how
the picked path is interactively displayed.
"""
mode = ElementType.from_any(mode)
self_ = weakref.ref(self)
def _end_handler(picked):
if callback:
_poked_context_callback(self_(), callback, picked)
if mode == ElementType.CELL:
self._picked_cell = picked
if show:
if mode == ElementType.CELL:
kwargs.setdefault('color', 'pink')
elif mode == ElementType.EDGE:
kwargs.setdefault('color', 'magenta')
else:
kwargs.setdefault('color', 'pink')
if mode in [ElementType.CELL, ElementType.FACE]:
picked = picked.extract_all_edges()
with self.iren.poked_subplot():
_kwargs = kwargs.copy()
self.add_mesh(
picked,
name=_kwargs.pop('name', PICKED_REPRESENTATION_NAMES['element']),
pickable=_kwargs.pop('pickable', False),
reset_camera=_kwargs.pop('reset_camera', False),
point_size=_kwargs.pop('point_size', 5),
line_width=_kwargs.pop('line_width', 5),
**_kwargs,
)
handler = PointPickingElementHandler(mode=mode, callback=_end_handler)
self.enable_surface_point_picking(
callback=handler,
show_message=show_message,
font_size=font_size,
color=color,
show_point=False,
tolerance=tolerance,
pickable_window=pickable_window,
left_clicking=left_clicking,
picker=picker,
use_picker=True,
**kwargs,
)
def enable_block_picking(self, callback=None, side='left'):
"""Enable composite block picking.
Use this picker to return the index of a DataSet when using composite
dataset like :class:`pyvista.MultiBlock` and pass it to a callback.
Parameters
----------
callback : callable, optional
When input, this picker calls this callable after a selection is
made. The composite index is passed to ``callback`` as the first
argument and the dataset as the second argument.
side : str, default: "left"
The mouse button to track (either ``'left'`` or ``'right'``).
Also accepts ``'r'`` or ``'l'``.
Notes
-----
The picked block index can be accessed from :attr:`picked_block_index
<pyvista.Plotter.picked_block_index>` attribute.
Examples
--------
Enable block picking with a multiblock dataset. Left clicking will turn
blocks blue while right picking will turn the block back to the default
color.
>>> import pyvista as pv
>>> multiblock = pv.MultiBlock(
... [pv.Cube(), pv.Sphere(center=(0, 0, 1))]
... )
>>> pl = pv.Plotter()
>>> actor, mapper = pl.add_composite(multiblock)
>>> def turn_blue(index, dataset):
... mapper.block_attr[index].color = 'blue'
...
>>> pl.enable_block_picking(callback=turn_blue, side='left')
>>> def clear_color(index, dataset):
... mapper.block_attr[index].color = None
...
>>> pl.enable_block_picking(callback=clear_color, side='right')
>>> pl.show()
"""
# use a weak reference to enable garbage collection
renderer_ = weakref.ref(self.renderer)
self_ = weakref.ref(self)
sel_index = _vtk.vtkSelectionNode.COMPOSITE_INDEX()
sel_prop = _vtk.vtkSelectionNode.PROP()
def get_picked_block(*args, **kwargs): # numpydoc ignore=PR01
"""Get the picked block and pass it to the user callback."""
x, y = self.mouse_position
selector = _vtk.vtkOpenGLHardwareSelector()
selector.SetRenderer(renderer_())
selector.SetArea(x, y, x, y) # single pixel
selection = selector.Select()
for ii in range(selection.GetNumberOfNodes()):
node = selection.GetNode(ii)
if node is None: # pragma: no cover
continue
node_prop = node.GetProperties()
self._picked_block_index = node_prop.Get(sel_index)
# Safely return the dataset as it's possible a non pyvista
# mapper was added
mapper = node_prop.Get(sel_prop).GetMapper()
if isinstance(mapper, CompositePolyDataMapper):
dataset = mapper.block_attr.get_block(self._picked_block_index)
else: # pragma: no cover
dataset = None
if callable(callback):
_poked_context_callback(self_(), callback, self._picked_block_index, dataset)
self.track_click_position(callback=get_picked_block, viewport=True, side=side)
class PickingHelper(PickingMethods):
"""Internal container class to contain picking helper methods."""
def __init__(self, *args, **kwargs):
"""Initialize the picking methods."""
super().__init__(*args, **kwargs)
self.picked_path = None
self.picked_geodesic = None
self.picked_horizon = None
def fly_to_mouse_position(self, focus=False):
"""Focus on last stored mouse position."""
if self.mouse_position is None:
self.store_mouse_position()
click_point = self.pick_mouse_position()
if focus:
self.set_focus(click_point)
else:
self.fly_to(click_point)
def enable_fly_to_right_click(self, callback=None):
"""Set the camera to track right click positions.
A convenience method to track right click positions and fly to
the picked point in the scene. The callback will be passed the
point in 3D space.
Parameters
----------
callback : callable
Callback to call immediately after right clicking.
"""
self_ = weakref.ref(self)
def _the_callback(*args):
click_point = self.pick_mouse_position()
self.fly_to(click_point)
if callable(callback):
_poked_context_callback(self_(), callback, click_point)
self.track_click_position(callback=_the_callback, side="right")
def enable_path_picking(
self,
callback=None,
show_message=True,
font_size=18,
color='pink',
point_size=10,
line_width=5,
show_path=True,
tolerance=0.025,
**kwargs,
):
"""Enable picking at paths.
This is a convenience method for :func:`enable_point_picking
<pyvista.Plotter.enable_point_picking>` to keep track of the
picked points and create a line using those points.
The line is saved to the ``.picked_path`` attribute of this
plotter
Parameters
----------
callback : callable, optional
When given, calls this callable after a pick is made. The
entire picked path is passed as the only parameter to this
callable.
show_message : bool | str, default: True
Show the message about how to use the point picking
tool. If this is a string, that will be the message shown.
font_size : int, default: 18
Sets the size of the message.
color : ColorLike, default: "pink"
The color of the selected mesh when shown.
point_size : int, default: 10
Size of picked points if ``show_path`` is ``True``.
line_width : float, default: 5.0
Thickness of path representation if ``show_path`` is
``True``.
show_path : bool, default: True
Show the picked path interactively.
tolerance : float, default: 0.025
Specify tolerance for performing pick operation. Tolerance
is specified as fraction of rendering window
size. Rendering window size is measured across diagonal.
**kwargs : dict, optional
All remaining keyword arguments are used to control how
the picked path is interactively displayed.
"""
self_ = weakref.ref(self)
kwargs.setdefault('pickable', False)
def make_line_cells(n_points): # numpydoc ignore=GL08
cells = np.arange(0, n_points, dtype=np.int_)
return np.insert(cells, 0, n_points)
the_points = []
def _the_callback(picked_point, picker):
if picker.GetDataSet() is None:
return
the_points.append(picked_point)
self.picked_path = pyvista.PolyData(np.array(the_points))
self.picked_path.lines = make_line_cells(len(the_points))
if show_path:
with self.iren.poked_subplot():
_kwargs = kwargs.copy()
self.add_mesh(
self.picked_path,
color=color,
name=_kwargs.pop('name', PICKED_REPRESENTATION_NAMES['path']),
line_width=line_width,
point_size=point_size,
pickable=_kwargs.pop('pickable', False),
reset_camera=_kwargs.pop('reset_camera', False),
**_kwargs,
)
if callable(callback):
_poked_context_callback(self_(), callback, self.picked_path)
def _clear_path_event_watcher():
del the_points[:]
with self.iren.poked_subplot():
self._clear_picking_representations()
self.add_key_event('c', _clear_path_event_watcher)
if show_message is True:
show_message = "Press P to pick under the mouse\nPress C to clear"
self.enable_surface_point_picking(
callback=_the_callback,
use_picker=True,
font_size=font_size,
show_message=show_message,
show_point=False,
tolerance=tolerance,
clear_on_no_selection=False,
)
def enable_geodesic_picking(
self,
callback=None,
show_message=True,
font_size=18,
color='pink',
point_size=10,
line_width=5,
tolerance=0.025,
show_path=True,
keep_order=True,
**kwargs,
):
"""Enable picking at geodesic paths.
This is a convenience method for ``enable_point_picking`` to
keep track of the picked points and create a geodesic path
using those points.
The geodesic path is saved to the ``.picked_geodesic``
attribute of this plotter.
Parameters
----------
callback : callable, optional
When given, calls this callable after a pick is made. The
entire picked, geodesic path is passed as the only
parameter to this callable.
show_message : bool | str, default: True
Show the message about how to use the point picking
tool. If this is a string, that will be the message shown.
font_size : int, default: 18
Sets the size of the message.
color : ColorLike, default: "pink"
The color of the selected mesh when shown.
point_size : int, default: 10
Size of picked points if ``show_path`` is ``True``.
line_width : float, default: 5.0
Thickness of path representation if ``show_path`` is
``True``.
tolerance : float, default: 0.025
Specify tolerance for performing pick operation. Tolerance
is specified as fraction of rendering window
size. Rendering window size is measured across diagonal.
show_path : bool, default: True
Show the picked path interactively.
keep_order : bool, default: True
If ``True``, the created geodesic path is a single ordered
and cleaned line from the first point to the last.
.. note::
In older versions there were apparent discontinuities
in the resulting path due to the behavior of the
underlying VTK filter which corresponds to
``keep_order=False``.
.. versionadded:: 0.32.0
**kwargs : dict, optional
All remaining keyword arguments are used to control how
the picked path is interactively displayed.
"""
self_ = weakref.ref(self)
kwargs.setdefault('pickable', False)
self.picked_geodesic = pyvista.PolyData()
self._last_picked_idx = None
def _the_callback(picked_point, picker):
if picker.GetDataSet() is None:
return
mesh = pyvista.wrap(picker.GetDataSet())
idx = mesh.find_closest_point(picked_point)
point = mesh.points[idx]
if self._last_picked_idx is None:
self.picked_geodesic = pyvista.PolyData(point)
self.picked_geodesic['vtkOriginalPointIds'] = [idx]
else:
surface = mesh.extract_surface().triangulate()
locator = _vtk.vtkPointLocator()
locator.SetDataSet(surface)
locator.BuildLocator()
start_idx = locator.FindClosestPoint(mesh.points[self._last_picked_idx])
end_idx = locator.FindClosestPoint(point)
self.picked_geodesic += surface.geodesic(start_idx, end_idx, keep_order=keep_order)
if keep_order:
# it makes sense to remove adjacent duplicate points
self.picked_geodesic.clean(
inplace=True,
lines_to_points=False,
polys_to_lines=False,
strips_to_polys=False,
)
self._last_picked_idx = idx
if show_path:
with self.iren.poked_subplot():
_kwargs = kwargs.copy()
self.add_mesh(
self.picked_geodesic,
color=color,
name=_kwargs.pop('name', PICKED_REPRESENTATION_NAMES['path']),
line_width=line_width,
point_size=point_size,
pickable=_kwargs.pop('pickable', False),
reset_camera=_kwargs.pop('reset_camera', False),
**_kwargs,
)
if callable(callback):
_poked_context_callback(self_(), callback, self.picked_geodesic)
def _clear_g_path_event_watcher():
self.picked_geodesic = pyvista.PolyData()
with self.iren.poked_subplot():
self._clear_picking_representations()
self._last_picked_idx = None
self.add_key_event('c', _clear_g_path_event_watcher)
if show_message is True:
show_message = "Press P to pick under the mouse\nPress C to clear"
self.enable_surface_point_picking(
callback=_the_callback,
use_picker=True,
font_size=font_size,
show_message=show_message,
tolerance=tolerance,
show_point=False,
clear_on_no_selection=False,
)
def enable_horizon_picking(
self,
callback=None,
normal=(0.0, 0.0, 1.0),
width=None,
show_message=True,
font_size=18,
color='pink',
point_size=10,
line_width=5,
show_path=True,
opacity=0.75,
show_horizon=True,
**kwargs,
):
"""Enable horizon picking.
Helper for the ``enable_path_picking`` method to also show a
ribbon surface along the picked path. Ribbon is saved under
``.picked_horizon``.
Parameters
----------
callback : callable, optional
When given, calls this callable after a pick is made. The
entire picked path is passed as the only parameter to this
callable.
normal : sequence[float], default: (0.0, 0.0, 1.0)
The normal to the horizon surface's projection plane.
width : float, optional
The width of the horizon surface. Default behaviour will
dynamically change the surface width depending on its
length.
show_message : bool | str, default: True
Show the message about how to use the horizon picking
tool. If this is a string, that will be the message shown.
font_size : int, default: 18
Sets the font size of the message.
color : ColorLike, default: "pink"
The color of the horizon surface if shown.
point_size : int, default: 10
Size of picked points if ``show_horizon`` is ``True``.
line_width : float, default: 5.0
Thickness of path representation if ``show_horizon`` is
``True``.
show_path : bool, default: True
Show the picked path that the horizon is built from
interactively.
opacity : float, default: 0.75
The opacity of the horizon surface if shown.
show_horizon : bool, default: True
Show the picked horizon surface interactively.
**kwargs : dict, optional
All remaining keyword arguments are used to control how
the picked path is interactively displayed.
"""
self_ = weakref.ref(self)
def _clear_horizon_event_watcher():
self.picked_horizon = pyvista.PolyData()
with self.iren.poked_subplot():
self._clear_picking_representations()
self.add_key_event('c', _clear_horizon_event_watcher)
def _the_callback(path):
if path.n_points < 2:
_clear_horizon_event_watcher()
return
self.picked_horizon = path.ribbon(normal=normal, width=width)
if show_horizon:
with self.iren.poked_subplot():
_kwargs = kwargs.copy()
self.add_mesh(
self.picked_horizon,
name=_kwargs.get('name', PICKED_REPRESENTATION_NAMES['horizon']),
color=color,
opacity=opacity,
pickable=_kwargs.pop('pickable', False),
reset_camera=_kwargs.pop('reset_camera', False),
**_kwargs,
)
if callable(callback):
_poked_context_callback(self_(), callback, path)
self.enable_path_picking(
callback=_the_callback,
show_message=show_message,
font_size=font_size,
color=color,
point_size=point_size,
line_width=line_width,
show_path=show_path,
**kwargs,
)
|