Debian Codegraph @

examples/99-advanced/plotting_algorithms.py

raw
1 """
2 .. _plotting_algorithms_example:
3
4 Plotting with VTK Algorithms
5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6
7 Pass a :vtk:`vtkAlgorithm` to the ``Plotter`` for dynamic visualizations.
8
9 .. note::
10 By "dynamic visualization" we mean that as the input data/source
11 changes, so will the visualization in real time.
12
13 A :vtk:`vtkAlgorithm` is the superclass for all sources, filters, and sinks
14 in VTK. It defines a generalized interface for executing data processing
15 algorithms. Pipeline connections are associated with input and output
16 ports that are independent of the type of data passing through the
17 connections.
18
19 We can connect the output port of a :vtk:`vtkAlgorithm` to PyVista's rendering
20 pipeline when adding data to the scene through methods like
21 :func:`add_mesh() <pyvista.Plotter.add_mesh>`.
22
23 This example will walk through using a few :vtk:`vtkAlgorithm` filters directly
24 and passing them to PyVista for dynamic visualization.
25 """
26
27 import pyvista as pv
28 from pyvista import examples
29 import vtk
30
31 # sphinx_gallery_start_ignore
32 # widgets do not currently work in interactive examples
33 PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True
34 # sphinx_gallery_end_ignore
35
36 # %%
37 # Use :vtk:`vtkConeSource` as a source algorithm. This source will dynamically
38 # create a cone object depending on the instances's parameters. In this
39 # example, we will connect a callback to set the cone source algorithm's
40 # resolution via :vtk:`vtkConeSource.SetResolution`.
41 algo = pv.ConeSource()
42
43
44 def update_resolution(value):
45 """Set the resolution of the cone generator."""
46 res = round(value)
47 algo.resolution = res
48
49
50 # %%
51 # Pass the :vtk:`vtkConeSource` (a :vtk:`vtkAlgorithm` subclass) directly to the
52 # plotter and connect a slider widget to our callback that adjusts the
53 # resolution.
54 pl = pv.Plotter()
55 pl.add_mesh(algo, color='red')
56 pl.add_slider_widget(update_resolution, [5, 100], title='Resolution')
57 pl.show()
58
59 # %%
60 # Here is another example using :vtk:`vtkRegularPolygonSource`.
61 poly_source = vtk.vtkRegularPolygonSource()
62 poly_source.GeneratePolygonOff()
63 poly_source.SetRadius(5.0)
64 poly_source.SetCenter(0.0, 0.0, 0.0)
65
66
67 def update_n_sides(value):
68 """Set the number of sides."""
69 res = round(value)
70 poly_source.SetNumberOfSides(res)
71
72
73 pl = pv.Plotter()
74 pl.add_mesh_clip_box(poly_source, color='red')
75 pl.add_slider_widget(update_n_sides, [3, 25], title='N Sides')
76 pl.view_xy()
77 pl.show()
78
79
80 # %%
81 # Filter Pipeline
82 # +++++++++++++++
83 # We can do this with any :vtk:`vtkAlgorithm` subclass for dynamically generating
84 # or filtering data. Here is an example of executing a pipeline of VTK filters
85 # together.
86
87 # Source mesh object (static)
88 mesh = examples.download_bunny_coarse()
89
90 # Initialize VTK algorithm to modify dynamically
91 splatter = vtk.vtkGaussianSplatter()
92
93 # Pass PyVista object as input to VTK
94 splatter.SetInputData(mesh)
95
96 # Set parameters of splatter filter
97 n = 200
98 splatter.SetSampleDimensions(n, n, n)
99 splatter.SetRadius(0.02)
100 splatter.SetExponentFactor(-10)
101 splatter.SetEccentricity(2)
102 splatter.Update()
103
104 # Pipe splatter filter into a contour filter
105 contour = vtk.vtkContourFilter()
106 contour.SetInputConnection(splatter.GetOutputPort())
107 contour.SetInputArrayToProcess(0, 0, 0, 0, 'SplatterValues')
108 contour.SetNumberOfContours(1)
109 contour.SetValue(0, 0.95 * splatter.GetRadius())
110
111 # Use PyVista to plot output of contour filter
112 pl = pv.Plotter(notebook=0)
113 pl.add_mesh(mesh, style='wireframe')
114 pl.add_mesh(contour, color=True)
115 pl.add_slider_widget(splatter.SetRadius, [0.01, 0.05])
116 pl.show()