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