| 1 |
""" |
| 2 |
.. _clip_volume_widget_example: |
| 3 |
|
| 4 |
Clip Volume Widget |
| 5 |
------------------ |
| 6 |
If you have a structured dataset like a :class:`pyvista.ImageData` or |
| 7 |
:class:`pyvista.RectilinearGrid`, you can clip it using the |
| 8 |
:func:`pyvista.Plotter.add_volume_clip_plane` widget to better see the internal |
| 9 |
structure of the dataset. |
| 10 |
|
| 11 |
.. image:: ../../images/gifs/volume-clip-plane-widget.gif |
| 12 |
|
| 13 |
""" |
| 14 |
|
| 15 |
# sphinx_gallery_start_ignore |
| 16 |
# widgets do not work in interactive examples |
| 17 |
PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True |
| 18 |
# sphinx_gallery_end_ignore |
| 19 |
|
| 20 |
# %% |
| 21 |
# Create the Dataset |
| 22 |
# ~~~~~~~~~~~~~~~~~~ |
| 23 |
# Create a dense :class:`pyvista.ImageData` with dimensions ``(200, 200, |
| 24 |
# 200)`` and set the active scalars to distance from the :attr:`center |
| 25 |
# <pyvista.DataSet.center>` of the grid. |
| 26 |
|
| 27 |
import numpy as np |
| 28 |
import pyvista as pv |
| 29 |
|
| 30 |
grid = pv.ImageData(dimensions=(200, 200, 200)) |
| 31 |
grid['scalars'] = np.linalg.norm(grid.center - grid.points, axis=1) |
| 32 |
grid |
| 33 |
|
| 34 |
|
| 35 |
# %% |
| 36 |
# Generate the Opacity Array |
| 37 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 38 |
# Create a banded opacity array such that our dataset shows "rings" at certain |
| 39 |
# values. Have this increase such that higher values (values farther away from |
| 40 |
# the center) are more opaque. |
| 41 |
|
| 42 |
opacity = np.zeros(100) |
| 43 |
opacity[::10] = np.geomspace(0.01, 0.75, 10) |
| 44 |
|
| 45 |
|
| 46 |
# %% |
| 47 |
# Plot a Single Clip Plane Dataset |
| 48 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 49 |
# Plot the volume with a single clip plane. |
| 50 |
# |
| 51 |
# Reverse the opacity array such that portions closer to the center are more |
| 52 |
# opaque. |
| 53 |
|
| 54 |
pl = pv.Plotter() |
| 55 |
pl.add_volume_clip_plane(grid, normal='-x', opacity=opacity[::-1], cmap='magma') |
| 56 |
pl.show() |
| 57 |
|
| 58 |
|
| 59 |
# %% |
| 60 |
# Plot Multiple Clip Planes |
| 61 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 62 |
# Plot the dataset using the :func:`pyvista.Plotter.add_volume_clip_plane` with |
| 63 |
# the output from :func:`pyvista.Plotter.add_volume` Enable constant |
| 64 |
# interaction by setting the ``interaction_event`` to ``'always'``. |
| 65 |
# |
| 66 |
# Disable the arrows to make the plot a bit clearer and flip the opacity array. |
| 67 |
|
| 68 |
pl = pv.Plotter() |
| 69 |
vol = pl.add_volume(grid, opacity=opacity) |
| 70 |
vol.prop.interpolation_type = 'linear' |
| 71 |
pl.add_volume_clip_plane( |
| 72 |
vol, |
| 73 |
normal='-x', |
| 74 |
interaction_event='always', |
| 75 |
normal_rotation=False, |
| 76 |
) |
| 77 |
pl.add_volume_clip_plane( |
| 78 |
vol, |
| 79 |
normal='-y', |
| 80 |
interaction_event='always', |
| 81 |
normal_rotation=False, |
| 82 |
) |
| 83 |
pl.show() |
| 84 |
# %% |
| 85 |
# .. tags:: widgets |