| 1 |
""" |
| 2 |
.. _moving_cmap_example: |
| 3 |
|
| 4 |
Create a GIF Movie of a Static Object with a Moving Colormap |
| 5 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 6 |
Generate a gif movie of a Hopf torus with a moving colormap, |
| 7 |
by updating the scalars. |
| 8 |
This example uses :meth:`~pyvista.Plotter.open_gif` and |
| 9 |
:meth:`~pyvista.Plotter.write_frame` to create the gif. |
| 10 |
|
| 11 |
""" |
| 12 |
|
| 13 |
import numpy as np |
| 14 |
import pyvista as pv |
| 15 |
|
| 16 |
|
| 17 |
# A spherical curve |
| 18 |
def scurve(t): |
| 19 |
alpha = np.pi / 2 - (np.pi / 2 - 0.44) * np.cos(3 * t) |
| 20 |
beta = t + 0.44 * np.sin(6 * t) |
| 21 |
return np.array( |
| 22 |
[np.sin(alpha) * np.cos(beta), np.sin(alpha) * np.sin(beta), np.cos(alpha)] |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
# Hopf fiber |
| 27 |
def hopf_fiber(p, phi): |
| 28 |
return np.array( |
| 29 |
[ |
| 30 |
(1 + p[2]) * np.cos(phi), |
| 31 |
p[0] * np.sin(phi) - p[1] * np.cos(phi), |
| 32 |
p[0] * np.cos(phi) + p[1] * np.sin(phi), |
| 33 |
(1 + p[2]) * np.sin(phi), |
| 34 |
], |
| 35 |
) / np.sqrt(2 * (1 + p[2])) |
| 36 |
|
| 37 |
|
| 38 |
# Stereographic projection |
| 39 |
def stereo_proj(q): |
| 40 |
return q[0:3] / (1 - q[3]) |
| 41 |
|
| 42 |
|
| 43 |
# Parameterization of the Hopf torus |
| 44 |
def hopf_torus(t, phi): |
| 45 |
return stereo_proj(hopf_fiber(scurve(t), phi)) |
| 46 |
|
| 47 |
|
| 48 |
# Create the mesh |
| 49 |
angle_u = np.linspace(-np.pi, np.pi, 400) |
| 50 |
angle_v = np.linspace(0, np.pi, 200) |
| 51 |
u, v = np.meshgrid(angle_u, angle_v) |
| 52 |
x, y, z = hopf_torus(u, v) |
| 53 |
grid = pv.StructuredGrid(x, y, z) |
| 54 |
mesh = grid.extract_surface(algorithm=None).clean(tolerance=1e-6) |
| 55 |
|
| 56 |
# Distances normalized to [0, 2*pi] |
| 57 |
dists = np.linalg.norm(mesh.points, axis=1) |
| 58 |
dists = 2 * np.pi * (dists - dists.min()) / (dists.max() - dists.min()) |
| 59 |
|
| 60 |
mesh['distances'] = np.sin(dists) |
| 61 |
|
| 62 |
# Make the movie |
| 63 |
pl = pv.Plotter(window_size=[512, 512]) |
| 64 |
pl.set_focus([0, 0, 0]) |
| 65 |
pl.set_position([40, 0, 0]) |
| 66 |
pl.add_mesh( |
| 67 |
mesh, |
| 68 |
scalars='distances', |
| 69 |
smooth_shading=True, |
| 70 |
specular=1, |
| 71 |
cmap='nipy_spectral', |
| 72 |
show_scalar_bar=False, |
| 73 |
) |
| 74 |
pl.open_gif('Hopf_torus.gif') |
| 75 |
|
| 76 |
for t in np.linspace(0, 2 * np.pi, 30, endpoint=False): |
| 77 |
mesh['distances'] = np.sin(dists - t) |
| 78 |
pl.write_frame() |
| 79 |
|
| 80 |
pl.show() |
| 81 |
# %% |
| 82 |
# .. tags:: plot |