Debian Codegraph @

examples/03-widgets/plane_widget.py

raw
1 """
2 .. _plane_widget_example:
3
4 Plane Widget
5 ~~~~~~~~~~~~
6
7 The plane widget can be enabled and disabled by the
8 :func:`pyvista.Plotter.add_plane_widget` and
9 :func:`pyvista.Plotter.clear_plane_widgets` methods respectively.
10 As with all widgets, you must provide a custom callback method to utilize that
11 plane. Considering that planes are most commonly used for clipping and slicing
12 meshes, we have included two helper methods for doing those tasks.
13
14 Let's use a plane to clip a mesh:
15 """
16
17 # sphinx_gallery_start_ignore
18 # widgets do not work in interactive examples
19 PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True
20 # sphinx_gallery_end_ignore
21
22 # sphinx_gallery_thumbnail_number = 2
23 import pyvista as pv
24 from pyvista import examples
25
26 vol = examples.download_brain()
27
28 pl = pv.Plotter()
29 pl.add_mesh_clip_plane(vol)
30 pl.show()
31
32 # %%
33 # After interacting with the scene, the clipped mesh is available as:
34 pl.widgets.plane_clipped_meshes
35
36 # %%
37 # And here is a screen capture of a user interacting with this
38 #
39 # .. image:: ../../images/gifs/plane-clip.gif
40
41 # %%
42 # Or you could slice a mesh using the plane widget:
43
44 pl = pv.Plotter()
45 pl.add_mesh_slice(vol)
46 pl.show()
47 # %%
48 # After interacting with the scene, the slice is available as:
49 pl.widgets.plane_sliced_meshes
50
51 # %%
52 # And here is a screen capture of a user interacting with this
53 #
54 # .. image:: ../../images/gifs/plane-slice.gif
55
56 # %%
57 # Or you could leverage the plane widget for some custom task like glyphing a
58 # vector field along that plane. Note that we have to pass a ``name`` when
59 # calling ``add_mesh`` to ensure that there is only one set of glyphs plotted
60 # at a time.
61
62 import pyvista as pv
63 from pyvista import examples
64
65 mesh = examples.download_carotid()
66
67 pl = pv.Plotter()
68 pl.add_mesh(mesh.contour(8).extract_largest(), opacity=0.5)
69
70
71 def my_plane_func(normal, origin):
72 slc = mesh.slice(normal=normal, origin=origin)
73 arrows = slc.glyph(orient='vectors', scale='scalars', factor=0.01)
74 pl.add_mesh(arrows, name='arrows')
75
76
77 pl.add_plane_widget(my_plane_func)
78 pl.show_grid()
79 pl.add_axes()
80 pl.show()
81
82 # %%
83 # And here is a screen capture of a user interacting with this
84 #
85 # .. image:: ../../images/gifs/plane-glyph.gif
86
87
88 # %%
89 # Further, a user can disable the arrow vector by setting the
90 # ``normal_rotation`` argument to ``False``. For example, here we
91 # programmatically set the normal vector on which we want to translate the
92 # plane and we disable the arrow to prevent its rotation.
93
94 pl = pv.Plotter()
95 pl.add_mesh_slice(vol, normal=(1, 1, 1), normal_rotation=False)
96 pl.show()
97
98 # %%
99 # The vector is also forcibly disabled anytime the ``assign_to_axis`` argument
100 # is set.
101 pl = pv.Plotter()
102 pl.add_mesh_slice(vol, assign_to_axis='z')
103 pl.show()
104
105
106 # %%
107 # Additionally, users can modify the interaction event that triggers the
108 # callback functions handled by the different plane widget helpers through the
109 # ``interaction_event`` keyword argument when available. For example,
110 # we can have continuous slicing by using the ``InteractionEvent`` observer.
111 import vtk
112
113 pl = pv.Plotter()
114 pl.add_mesh_slice(
115 vol, assign_to_axis='z', interaction_event=vtk.vtkCommand.InteractionEvent
116 )
117 pl.show()
118
119 # %%
120 # And here is a screen capture of a user interacting with this continuously via
121 # the ``InteractionEvent`` observer:
122 #
123 # .. image:: ../../images/gifs/plane-slice-continuous.gif
124 #
125 # .. tags:: widgets