| 1 |
""" |
| 2 |
.. _moving_isovalue_example: |
| 3 |
|
| 4 |
Moving Isovalue |
| 5 |
~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
Make an animation of an isovalue through a volumetric dataset |
| 8 |
such as :func:`~pyvista.examples.downloads.download_brain`. |
| 9 |
This example uses :meth:`~pyvista.Plotter.open_gif` and |
| 10 |
:meth:`~pyvista.Plotter.write_frame` to create the animation. |
| 11 |
|
| 12 |
""" |
| 13 |
|
| 14 |
import numpy as np |
| 15 |
import pyvista as pv |
| 16 |
from pyvista import examples |
| 17 |
|
| 18 |
vol = examples.download_brain() |
| 19 |
vol |
| 20 |
|
| 21 |
# %% |
| 22 |
# Now lets make an array of all of the isovalues for which we want to show. |
| 23 |
values = np.linspace(5, 150, num=15) |
| 24 |
|
| 25 |
# %% |
| 26 |
# Now let's create an initial isosurface that we can plot and move |
| 27 |
surface = vol.contour(values[:1]) |
| 28 |
|
| 29 |
# %% |
| 30 |
# Precompute the surfaces |
| 31 |
surfaces = [vol.contour([v]) for v in values] |
| 32 |
|
| 33 |
# %% |
| 34 |
# Set a single surface as the one being plotted that can be overwritten |
| 35 |
surface = surfaces[0].copy() |
| 36 |
|
| 37 |
# %% |
| 38 |
|
| 39 |
filename = 'isovalue.gif' |
| 40 |
|
| 41 |
pl = pv.Plotter(off_screen=True) |
| 42 |
# Open a movie file |
| 43 |
pl.open_gif(filename) |
| 44 |
|
| 45 |
# Add initial mesh |
| 46 |
pl.add_mesh( |
| 47 |
surface, |
| 48 |
opacity=0.5, |
| 49 |
clim=vol.get_data_range(), |
| 50 |
show_scalar_bar=False, |
| 51 |
) |
| 52 |
# Add outline for reference |
| 53 |
pl.add_mesh(vol.outline_corners(), color='k') |
| 54 |
|
| 55 |
print('Orient the view, then press "q" to close window and produce movie') |
| 56 |
pl.camera_position = pv.CameraPosition( |
| 57 |
position=(392.9783280407326, 556.4341372317185, 235.51220650196404), |
| 58 |
focal_point=(88.69563012828344, 119.06774369173661, 72.61750326143748), |
| 59 |
viewup=(-0.19275936948097383, -0.2218876327549124, 0.9558293278131397), |
| 60 |
) |
| 61 |
|
| 62 |
# initial render and do NOT close |
| 63 |
pl.show(auto_close=False) |
| 64 |
|
| 65 |
# Run through each frame |
| 66 |
for surf in surfaces: |
| 67 |
surface.copy_from(surf) |
| 68 |
pl.write_frame() # Write this frame |
| 69 |
# Run through backwards |
| 70 |
for surf in surfaces[::-1]: |
| 71 |
surface.copy_from(surf) |
| 72 |
pl.write_frame() # Write this frame |
| 73 |
|
| 74 |
# Be sure to close the plotter when finished |
| 75 |
pl.close() |
| 76 |
# %% |
| 77 |
# .. tags:: plot |