Debian Codegraph @

examples/99-advanced/gyroid.py

raw
1 """
2 .. _gyroid_example:
3
4 Plot a Gyroid Surface
5 ---------------------
6
7 Contour an implicit gyroid field stored on :class:`pyvista.ImageData` into a
8 periodic surface with :func:`pyvista.DataSetFilters.contour`.
9 """
10
11 import numpy as np
12 import pyvista as pv
13
14 # %%
15 # Sample the implicit field
16 # ~~~~~~~~~~~~~~~~~~~~~~~~~
17 # The gyroid is the zero level set of
18 # ``sin(x)cos(y) + sin(y)cos(z) + sin(z)cos(x)``.
19
20 n = 50
21 x, y, z = np.mgrid[
22 -np.pi : np.pi : complex(0, n),
23 -np.pi : np.pi : complex(0, n),
24 -np.pi : np.pi : complex(0, n),
25 ]
26 values = np.sin(x) * np.cos(y) + np.sin(y) * np.cos(z) + np.sin(z) * np.cos(x)
27
28 grid = pv.ImageData(dimensions=values.shape)
29 grid.origin = (-np.pi, -np.pi, -np.pi)
30 grid.spacing = (2 * np.pi / (n - 1),) * 3
31 grid['gyroid'] = values.ravel(order='F')
32 grid
33
34
35 # %%
36 # Extract the zero isosurface
37 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~
38 # The contour weaves through the periodic sample volume.
39
40 surface = grid.contour([0.0], scalars='gyroid')
41
42 pl = pv.Plotter()
43 pl.add_mesh(surface, color='royalblue', smooth_shading=True)
44 pl.show()
45 # %%
46 # .. tags:: advanced