# Using Common Filters
# ====================

# Using common filters like thresholding and clipping.
import pyvista as pv
from pyvista import examples

# PyVista wrapped data objects have a suite of common filters ready for immediate
# use directly on the object. These filters include the following
# (see Filters for a complete list):
# `slice`: creates a single slice through the input dataset on a user defined plane
# `slice_orthogonal`: creates a `MultiBlock` dataset of three orthogonal slices
# `slice_along_axis`: creates a `MultiBlock` dataset of many slices along a
# specified axis
# `threshold`: Thresholds a dataset by a single value or range of values
# `threshold_percent`: Threshold by percentages of the scalar range
# `clip`: Clips the dataset by a user defined plane
# `outline_corners`: Outlines the corners of the data extent
# `extract_surface`: Extract surface geometry
# To use these filters, call the method of your choice directly on your data
# object:
dataset = examples.load_uniform()
dataset.set_active_scalars('Spatial Point Data')

# Apply a threshold over a data range
threshed = dataset.threshold([100, 500])

outline = dataset.outline()

# And now there is a thresholded version of the input dataset in the new
# `threshed` object. To learn more about what keyword arguments are available to
# alter how filters are executed, print the docstring for any filter attached to
# PyVista objects with either `help(dataset.threshold)` or using `shift+tab`
# in an IPython environment.
# We can now plot this filtered dataset along side an outline of the original
# dataset
pl = pv.Plotter()
pl.add_mesh(outline, color='k')
pl.add_mesh(threshed)
pl.camera_position = [-2, 5, 3]
pl.show()

# What about other filters? Let's collect a few filter results and compare them:
contours = dataset.contour()
slices = dataset.slice_orthogonal()
glyphs = dataset.glyph(factor=1e-3, geom=pv.Sphere(), orient=False)

# Use ~pyvista.plot_compare to show each result in its own linked subplot with an
# outline of the original dataset.
datasets = {
    'Threshold': threshed,
    'Contour': contours,
    'Slices': slices,
    'Glyphs': glyphs,
}

pv.plot_compare(
    datasets,
    show_scalar_bar=False,
    reference_mesh=outline,
    cpos=[-2, 5, 3],
)

# Filter Pipeline
# ---------------

# In VTK, filters are often used in a pipeline where each algorithm passes its
# output to the next filtering algorithm. In PyVista, we can mimic the
# filtering pipeline through a chain; attaching each filter to the last filter.
# In the following example, several filters are chained together:
# First, use `remove_nan_cells()` to drop any
# cells whose scalar values are `NaN`.
# Use an `elevation` filter to generate scalar values corresponding to height.
# Use the `clip` filter to cut the dataset in half.
# Create three slices along each axial plane using the `slice_orthogonal` filter.
# Apply a filtering chain
result = dataset.remove_nan_cells().elevation().clip(normal='z').slice_orthogonal()

# And to view this filtered data, simply call the `plot` method
# (`result.plot()`) or create a rendering scene:
pl = pv.Plotter()
pl.add_mesh(outline, color='k')
pl.add_mesh(result, scalars='Elevation')
pl.view_isometric()
pl.show()

# ----------------------------------------------------------------------
# Generated by sphinx-examples-as-code https://github.com/pyvista/sphinx-examples-as-code

