Debian Codegraph @

examples/03-widgets/line_widget.py

raw
1 """
2 .. _line_widget_example:
3
4 Line Widget
5 ~~~~~~~~~~~
6
7 The line widget can be enabled and disabled by the
8 :func:`pyvista.Plotter.add_line_widget` and
9 :func:`pyvista.Plotter.clear_line_widgets` methods respectively.
10 Unfortunately, PyVista does not have any helper methods to utilize this
11 widget, so it is necessary to pass a custom callback method.
12
13 One particularly fun example is to use the line widget to create a source for
14 the :func:`pyvista.DataSetFilters.streamlines` filter. Again note the use of
15 the ``name`` argument in ``add_mesh``.
16 """
17
18 # sphinx_gallery_start_ignore
19 # widgets do not work in interactive examples
20 PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True
21 # sphinx_gallery_end_ignore
22
23 import numpy as np
24 import pyvista as pv
25 from pyvista import examples
26
27 pv.set_plot_theme('document')
28
29 mesh = examples.download_kitchen()
30 furniture = examples.download_kitchen(split=True)
31
32 arr = np.linalg.norm(mesh['velocity'], axis=1)
33 clim = [arr.min(), arr.max()]
34
35 # %%
36
37 pl = pv.Plotter()
38 pl.add_mesh(furniture, name='furniture', color=True)
39 pl.add_mesh(mesh.outline(), color='black')
40 pl.add_axes()
41
42
43 def simulate(pointa, pointb):
44 streamlines = mesh.streamlines(
45 n_points=10,
46 max_steps=100,
47 pointa=pointa,
48 pointb=pointb,
49 integration_direction='forward',
50 )
51 pl.add_mesh(
52 streamlines,
53 name='streamlines',
54 line_width=5,
55 render_lines_as_tubes=True,
56 clim=clim,
57 )
58
59
60 pl.add_line_widget(callback=simulate, use_vertices=True)
61 pl.show()
62
63 # %%
64 # And here is a screen capture of a user interacting with this
65 #
66 # .. image:: ../../images/gifs/line-widget-streamlines.gif
67 #
68 # %%
69 # .. tags:: widgets