Debian Codegraph @

examples/01-filter/marching_cubes.py

raw
1 """
2 .. _marching_cubes_example:
3
4 Marching Cubes
5 ~~~~~~~~~~~~~~
6
7 Generate a surface from a scalar field using the flying edges and
8 marching cubes filters as provided by the :func:`contour
9 <pyvista.DataSetFilters.contour>` filter.
10
11 Special thanks to GitHub user `stla <https://gist.github.com/stla>`_
12 for providing examples.
13
14 """
15
16 import numpy as np
17 import pyvista as pv
18
19 # %%
20 # Spider Cage
21 # ~~~~~~~~~~~
22 # Use the marching cubes algorithm to extract the isosurface
23 # generated from the spider cage function.
24
25 a = 0.9
26
27
28 def spider_cage(x, y, z):
29 x2 = x * x
30 y2 = y * y
31 x2_y2 = x2 + y2
32 with np.errstate(divide='ignore', invalid='ignore'):
33 return (
34 np.sqrt((x2 - y2) ** 2 / x2_y2 + 3 * (z * np.sin(a)) ** 2) - 3
35 ) ** 2 + 6 * (np.sqrt((x * y) ** 2 / x2_y2 + (z * np.cos(a)) ** 2) - 1.5) ** 2
36
37
38 # create a uniform grid to sample the function with
39 n = 100
40 x_min, y_min, z_min = -5, -5, -3
41 grid = pv.ImageData(
42 dimensions=(n, n, n),
43 spacing=(abs(x_min) / n * 2, abs(y_min) / n * 2, abs(z_min) / n * 2),
44 origin=(x_min, y_min, z_min),
45 )
46 x, y, z = grid.points.T
47
48 # sample and plot
49 values = spider_cage(x, y, z)
50 mesh = grid.contour([1], values, method='marching_cubes')
51 dist = np.linalg.norm(mesh.points, axis=1)
52 mesh.plot(scalars=dist, smooth_shading=True, cmap='plasma', show_scalar_bar=False)
53
54
55 # %%
56 # Barth Sextic
57 # ~~~~~~~~~~~~
58 # Use the flying edges algorithm to extract the isosurface
59 # generated from the Barth sextic function.
60
61
62 phi = (1 + np.sqrt(5)) / 2
63 phi2 = phi * phi
64
65
66 def barth_sextic(x, y, z):
67 x2 = x * x
68 y2 = y * y
69 z2 = z * z
70 arr = (
71 3 * (phi2 * x2 - y2) * (phi2 * y2 - z2) * (phi2 * z2 - x2)
72 - (1 + 2 * phi) * (x2 + y2 + z2 - 1) ** 2
73 )
74 nan_mask = x2 + y2 + z2 > 3.1
75 arr[nan_mask] = np.nan
76 return arr
77
78
79 # create a uniform grid to sample the function with
80 n = 100
81 k = 2.0
82 x_min, y_min, z_min = -k, -k, -k
83 grid = pv.ImageData(
84 dimensions=(n, n, n),
85 spacing=(abs(x_min) / n * 2, abs(y_min) / n * 2, abs(z_min) / n * 2),
86 origin=(x_min, y_min, z_min),
87 )
88 x, y, z = grid.points.T
89
90 # sample and plot
91 values = barth_sextic(x, y, z)
92 mesh = grid.contour([0], values, method='flying_edges')
93 dist = np.linalg.norm(mesh.points, axis=1)
94 mesh.plot(scalars=dist, smooth_shading=True, cmap='plasma', show_scalar_bar=False)
95
96
97 # %%
98 # Animate Barth Sextic
99 # ~~~~~~~~~~~~~~~~~~~~
100 # Show 20 frames of various isocurves extracted from the Barth sextic
101 # function.
102
103
104 def angle_to_range(angle):
105 return -2 * np.sin(angle)
106
107
108 pl = pv.Plotter(window_size=[800, 800], off_screen=True)
109
110 pl.open_gif('barth_sextic.gif')
111
112 for angle in np.linspace(0, np.pi, 20, endpoint=False):
113 # clear the plotter before adding each frame's mesh
114 pl.clear()
115 pl.enable_lightkit()
116 mesh = grid.contour([angle_to_range(angle)], values, method='flying_edges')
117 dist = np.linalg.norm(mesh.points, axis=1)
118 pl.add_mesh(
119 mesh,
120 scalars=dist,
121 smooth_shading=True,
122 rng=[0.5, 1.5],
123 cmap='plasma',
124 show_scalar_bar=False,
125 )
126 pl.write_frame()
127
128 pl.close()
129 # %%
130 # .. tags:: filter