1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
"""
.. _clip_with_surface_example:
Clipping with a Surface
~~~~~~~~~~~~~~~~~~~~~~~
Clip any PyVista dataset by a :class:`pyvista.PolyData` surface mesh using
the :func:`pyvista.DataSetFilters.clip_surface` filter.
Note that we first demonstrate how the clipping is performed by computing an
implicit distance and thresholding the mesh. This thresholding is one approach
to clip by a surface, and preserve the original geometry of the given mesh,
but many folks leverage the ``clip_surface`` filter to triangulate/tessellate
the mesh geometries along the clip.
"""
from __future__ import annotations
import numpy as np
# sphinx_gallery_thumbnail_number = 4
# sphinx_gallery_start_ignore
PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True
# sphinx_gallery_end_ignore
import pyvista as pv
from pyvista import examples
# %%
surface = pv.Cone(direction=(0, 0, -1), height=3.0, radius=1, resolution=50, capping=False)
# Make a gridded dataset
n = 51
xx = yy = zz = 1 - np.linspace(0, n, n) * 2 / (n - 1)
dataset = pv.RectilinearGrid(xx, yy, zz)
# Preview the problem
p = pv.Plotter()
p.add_mesh(surface, color='w', label='Surface')
p.add_mesh(dataset, color='gold', show_edges=True, opacity=0.75, label='To Clip')
p.add_legend()
p.show()
# %%
# Take a look at the implicit function used to perform the surface clipping by
# using the :func:`pyvista.DataSetFilters.compute_implicit_distance` filter.
# The clipping operation field is performed where the ``implicit_distance``
# field is zero and the ``invert`` flag controls which sides of zero to
# preserve.
dataset.compute_implicit_distance(surface, inplace=True)
inner = dataset.threshold(0.0, scalars='implicit_distance', invert=True)
outer = dataset.threshold(0.0, scalars='implicit_distance', invert=False)
p = pv.Plotter()
p.add_mesh(surface, color='w', label='Surface', opacity=0.75)
p.add_mesh(
inner,
scalars='implicit_distance',
show_edges=True,
opacity=0.75,
label='Inner region',
clim=[-1, 1],
cmap='bwr',
)
p.add_legend()
p.show()
# %%
p = pv.Plotter()
p.add_mesh(surface, color='w', label='Surface', opacity=0.75)
p.add_mesh(
outer,
scalars='implicit_distance',
show_edges=True,
opacity=0.75,
label='Outer region',
clim=[-1, 1],
cmap='bwr',
)
p.add_legend()
p.show()
# %%
# Clip the rectilinear grid dataset using the :class:`pyvista.PolyData`
# surface mesh via the :func:`pyvista.DataSetFilters.clip_surface` filter.
# This will triangulate/tessellate the mesh geometries along the clip.
clipped = dataset.clip_surface(surface, invert=False)
# Visualize the results
p = pv.Plotter()
p.add_mesh(surface, color='w', opacity=0.75, label='Surface')
p.add_mesh(clipped, color='gold', show_edges=True, label='clipped', opacity=0.75)
p.add_legend()
p.show()
# %%
# Here is another example of clipping a mesh by a surface. This time, we'll
# generate a :class:`pyvista.ImageData` around a topography surface and then
# clip that grid using the surface to create a closed 3D model of the surface
# sphinx_gallery_start_ignore
PYVISTA_GALLERY_FORCE_STATIC = False
# sphinx_gallery_end_ignore
surface = examples.load_random_hills()
# Create a grid around that surface
grid = pv.create_grid(surface)
# Clip the grid using the surface
model = grid.clip_surface(surface)
# Compute height and display it
model.elevation().plot()
# %%
# .. tags:: filter
|