Debian Codegraph @

examples/03-widgets/sphere_widget.py

raw
1 """
2 .. _sphere_widget_example:
3
4 Sphere Widget
5 ~~~~~~~~~~~~~
6
7 The sphere widget can be enabled and disabled by the
8 :func:`pyvista.Plotter.add_sphere_widget` and
9 :func:`pyvista.Plotter.clear_sphere_widgets` methods respectively.
10 This is a very versatile widget as it can control vertex location that can
11 be used to control or update the location of just about anything.
12
13 We don't have any convenient helper methods that utilize this widget out of
14 the box, but we have added a lot of ways to use this widget so that you can
15 easily add several widgets to a scene.
16
17 Let's look at a few use cases that all update a surface mesh.
18 """
19
20 # sphinx_gallery_thumbnail_number = 3
21
22 # sphinx_gallery_start_ignore
23 # widgets do not work in interactive examples
24 PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True
25 # sphinx_gallery_end_ignore
26
27 # %%
28 # Example A
29 # +++++++++
30 #
31 # Use a single sphere widget
32
33 import numpy as np
34 import pyvista as pv
35
36 # Create a triangle surface
37 surf = pv.PolyData()
38 surf.points = np.array(
39 [
40 [-10, -10, -10],
41 [10, 10, -10],
42 [-10, 10, 0],
43 ],
44 )
45 surf.faces = np.array([3, 0, 1, 2])
46
47 pl = pv.Plotter()
48
49
50 def callback(point):
51 surf.points[0] = point
52
53
54 pl.add_sphere_widget(callback)
55 pl.add_mesh(surf, color=True)
56
57 pl.show_grid()
58 pl.show()
59
60 # %%
61 # And here is a screen capture of a user interacting with this
62 #
63 # .. image:: ../../images/gifs/sphere-widget-a.gif
64
65
66 # %%
67 # Example B
68 # +++++++++
69 #
70 # Use several sphere widgets at once
71
72 import numpy as np
73 import pyvista as pv
74
75 # Create a triangle surface
76 surf = pv.PolyData()
77 surf.points = np.array(
78 [
79 [-10, -10, -10],
80 [10, 10, -10],
81 [-10, 10, 0],
82 ],
83 )
84 surf.faces = np.array([3, 0, 1, 2])
85
86
87 pl = pv.Plotter()
88
89
90 def callback(point, i):
91 surf.points[i] = point
92
93
94 pl.add_sphere_widget(callback, center=surf.points)
95 pl.add_mesh(surf, color=True)
96
97 pl.show_grid()
98 pl.show()
99
100 # %%
101 # And here is a screen capture of a user interacting with this
102 #
103 # .. image:: ../../images/gifs/sphere-widget-b.gif
104
105 # %%
106 # Example C
107 # +++++++++
108 #
109 # This one is the coolest - use four sphere widgets to update perturbations on
110 # a surface and interpolate between them with some boundary conditions
111
112 import numpy as np
113 import pyvista as pv
114 from scipy.interpolate import griddata
115
116
117 def get_colors(n):
118 """Get n colors."""
119 from itertools import cycle
120
121 import matplotlib as mpl
122
123 cycler = mpl.rcParams['axes.prop_cycle']
124 colors = cycle(cycler)
125 return [next(colors)['color'] for i in range(n)]
126
127
128 # Create a grid to interpolate to
129 xmin, xmax, ymin, ymax = 0, 100, 0, 100
130 x = np.linspace(xmin, xmax, num=25)
131 y = np.linspace(ymin, ymax, num=25)
132 xx, yy, zz = np.meshgrid(x, y, [0])
133
134 # Make sure boundary conditions exist
135 boundaries = np.array(
136 [[xmin, ymin, 0], [xmin, ymax, 0], [xmax, ymin, 0], [xmax, ymax, 0]]
137 )
138
139 # Create the PyVista mesh to hold this grid
140 surf = pv.StructuredGrid(xx, yy, zz)
141
142 # Create some initial perturbations
143 # - this array will be updated inplace
144 points = np.array([[33, 25, 45], [70, 80, 13], [51, 57, 10], [25, 69, 20]])
145
146
147 # Create an interpolation function to update that surface mesh
148 def update_surface(point, i):
149 points[i] = point
150 tp = np.vstack((points, boundaries))
151 zz = griddata(tp[:, 0:2], tp[:, 2], (xx[:, :, 0], yy[:, :, 0]), method='cubic')
152 surf.points[:, -1] = zz.ravel(order='F')
153
154
155 # Get a list of unique colors for each widget
156 colors = get_colors(len(points))
157
158 # %%
159
160 # Begin the plotting routine
161 pl = pv.Plotter()
162
163 # Add the surface to the scene
164 pl.add_mesh(surf, color=True)
165
166 # Add the widgets which will update the surface
167 pl.add_sphere_widget(update_surface, center=points, color=colors, radius=3)
168 # Add axes grid
169 pl.show_grid()
170
171 # Show it
172 pl.show()
173
174 # %%
175 # And here is a screen capture of a user interacting with this
176 #
177 # .. image:: ../../images/gifs/sphere-widget-c.gif
178 #
179 # %%
180 # .. tags:: widgets