| 1 |
""" |
| 2 |
.. _using_filters_example: |
| 3 |
|
| 4 |
Using Common Filters |
| 5 |
~~~~~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
Using common filters like thresholding and clipping. |
| 8 |
""" |
| 9 |
|
| 10 |
# sphinx_gallery_thumbnail_number = 2 |
| 11 |
import pyvista as pv |
| 12 |
from pyvista import examples |
| 13 |
|
| 14 |
# %% |
| 15 |
# PyVista wrapped data objects have a suite of common filters ready for immediate |
| 16 |
# use directly on the object. These filters include the following |
| 17 |
# (see :ref:`filters` for a complete list): |
| 18 |
# |
| 19 |
# * ``slice``: creates a single slice through the input dataset on a user defined plane |
| 20 |
# * ``slice_orthogonal``: creates a ``MultiBlock`` dataset of three orthogonal slices |
| 21 |
# * ``slice_along_axis``: creates a ``MultiBlock`` dataset of many slices along a |
| 22 |
# specified axis |
| 23 |
# * ``threshold``: Thresholds a dataset by a single value or range of values |
| 24 |
# * ``threshold_percent``: Threshold by percentages of the scalar range |
| 25 |
# * ``clip``: Clips the dataset by a user defined plane |
| 26 |
# * ``outline_corners``: Outlines the corners of the data extent |
| 27 |
# * ``extract_surface``: Extract surface geometry |
| 28 |
# |
| 29 |
# To use these filters, call the method of your choice directly on your data |
| 30 |
# object: |
| 31 |
|
| 32 |
dataset = examples.load_uniform() |
| 33 |
dataset.set_active_scalars('Spatial Point Data') |
| 34 |
|
| 35 |
# Apply a threshold over a data range |
| 36 |
threshed = dataset.threshold([100, 500]) |
| 37 |
|
| 38 |
outline = dataset.outline() |
| 39 |
|
| 40 |
# %% |
| 41 |
# And now there is a thresholded version of the input dataset in the new |
| 42 |
# ``threshed`` object. To learn more about what keyword arguments are available to |
| 43 |
# alter how filters are executed, print the docstring for any filter attached to |
| 44 |
# PyVista objects with either ``help(dataset.threshold)`` or using ``shift+tab`` |
| 45 |
# in an IPython environment. |
| 46 |
# |
| 47 |
# We can now plot this filtered dataset along side an outline of the original |
| 48 |
# dataset |
| 49 |
|
| 50 |
pl = pv.Plotter() |
| 51 |
pl.add_mesh(outline, color='k') |
| 52 |
pl.add_mesh(threshed) |
| 53 |
pl.camera_position = [-2, 5, 3] |
| 54 |
pl.show() |
| 55 |
|
| 56 |
|
| 57 |
# %% |
| 58 |
# What about other filters? Let's collect a few filter results and compare them: |
| 59 |
|
| 60 |
contours = dataset.contour() |
| 61 |
slices = dataset.slice_orthogonal() |
| 62 |
glyphs = dataset.glyph(factor=1e-3, geom=pv.Sphere(), orient=False) |
| 63 |
|
| 64 |
pl = pv.Plotter(shape=(2, 2)) |
| 65 |
# Show the threshold |
| 66 |
pl.add_mesh(outline, color='k') |
| 67 |
pl.add_mesh(threshed, show_scalar_bar=False) |
| 68 |
pl.camera_position = [-2, 5, 3] |
| 69 |
# Show the contour |
| 70 |
pl.subplot(0, 1) |
| 71 |
pl.add_mesh(outline, color='k') |
| 72 |
pl.add_mesh(contours, show_scalar_bar=False) |
| 73 |
pl.camera_position = [-2, 5, 3] |
| 74 |
# Show the slices |
| 75 |
pl.subplot(1, 0) |
| 76 |
pl.add_mesh(outline, color='k') |
| 77 |
pl.add_mesh(slices, show_scalar_bar=False) |
| 78 |
pl.camera_position = [-2, 5, 3] |
| 79 |
# Show the glyphs |
| 80 |
pl.subplot(1, 1) |
| 81 |
pl.add_mesh(outline, color='k') |
| 82 |
pl.add_mesh(glyphs, show_scalar_bar=False) |
| 83 |
pl.camera_position = [-2, 5, 3] |
| 84 |
|
| 85 |
pl.link_views() |
| 86 |
pl.show() |
| 87 |
|
| 88 |
# %% |
| 89 |
# Filter Pipeline |
| 90 |
# +++++++++++++++ |
| 91 |
# |
| 92 |
# In VTK, filters are often used in a pipeline where each algorithm passes its |
| 93 |
# output to the next filtering algorithm. In PyVista, we can mimic the |
| 94 |
# filtering pipeline through a chain; attaching each filter to the last filter. |
| 95 |
# In the following example, several filters are chained together: |
| 96 |
# |
| 97 |
# 1. First, use :func:`~pyvista.DataSetFilters.remove_nan_cells` to drop any |
| 98 |
# cells whose scalar values are ``NaN``. |
| 99 |
# 2. Use an ``elevation`` filter to generate scalar values corresponding to height. |
| 100 |
# 3. Use the ``clip`` filter to cut the dataset in half. |
| 101 |
# 4. Create three slices along each axial plane using the ``slice_orthogonal`` filter. |
| 102 |
|
| 103 |
# Apply a filtering chain |
| 104 |
result = dataset.remove_nan_cells().elevation().clip(normal='z').slice_orthogonal() |
| 105 |
|
| 106 |
# %% |
| 107 |
# And to view this filtered data, simply call the ``plot`` method |
| 108 |
# (``result.plot()``) or create a rendering scene: |
| 109 |
|
| 110 |
pl = pv.Plotter() |
| 111 |
pl.add_mesh(outline, color='k') |
| 112 |
pl.add_mesh(result, scalars='Elevation') |
| 113 |
pl.view_isometric() |
| 114 |
pl.show() |
| 115 |
# %% |
| 116 |
# .. tags:: filter |