| 1 |
""" |
| 2 |
.. _clip_with_plane_box_example: |
| 3 |
|
| 4 |
Clipping with Planes & Boxes |
| 5 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
Clip/cut any dataset using planes or boxes. |
| 8 |
""" |
| 9 |
|
| 10 |
# sphinx_gallery_thumbnail_number = 2 |
| 11 |
import pyvista as pv |
| 12 |
from pyvista import examples |
| 13 |
|
| 14 |
# sphinx_gallery_start_ignore |
| 15 |
PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True |
| 16 |
# sphinx_gallery_end_ignore |
| 17 |
|
| 18 |
# %% |
| 19 |
# Clip with Plane |
| 20 |
# +++++++++++++++ |
| 21 |
# |
| 22 |
# Clip any dataset by a user defined plane using the |
| 23 |
# :func:`pyvista.DataObjectFilters.clip` filter |
| 24 |
dataset = examples.download_bunny_coarse() |
| 25 |
clipped = dataset.clip('y', invert=False) |
| 26 |
|
| 27 |
pl = pv.Plotter() |
| 28 |
pl.add_mesh(dataset, style='wireframe', color='blue', label='Input') |
| 29 |
pl.add_mesh(clipped, label='Clipped') |
| 30 |
pl.add_legend() |
| 31 |
pl.camera_position = pv.CameraPosition( |
| 32 |
position=(0.24, 0.32, 0.7), |
| 33 |
focal_point=(0.02, 0.03, -0.02), |
| 34 |
viewup=(-0.12, 0.93, -0.34), |
| 35 |
) |
| 36 |
pl.show() |
| 37 |
|
| 38 |
|
| 39 |
# %% |
| 40 |
# Clip with Bounds |
| 41 |
# ++++++++++++++++ |
| 42 |
# |
| 43 |
# Clip any dataset by a set of XYZ bounds using the |
| 44 |
# :func:`pyvista.DataObjectFilters.clip_box` filter. |
| 45 |
dataset = examples.download_office() |
| 46 |
|
| 47 |
bounds = [2, 4.5, 2, 4.5, 1, 3] |
| 48 |
clipped = dataset.clip_box(bounds) |
| 49 |
|
| 50 |
pl = pv.Plotter() |
| 51 |
pl.add_mesh(dataset, style='wireframe', color='blue', label='Input') |
| 52 |
pl.add_mesh(clipped, label='Clipped') |
| 53 |
pl.add_legend() |
| 54 |
pl.show() |
| 55 |
|
| 56 |
|
| 57 |
# %% |
| 58 |
# Clip with Rotated Box |
| 59 |
# +++++++++++++++++++++ |
| 60 |
# |
| 61 |
# Clip any dataset by an arbitrarily rotated solid box using the |
| 62 |
# :func:`pyvista.DataObjectFilters.clip_box` filter. |
| 63 |
mesh = examples.load_airplane() |
| 64 |
|
| 65 |
# Use `pv.Box()` or `pv.Cube()` to create a region of interest |
| 66 |
roi = pv.Cube( |
| 67 |
center=(0.9e3, 0.2e3, mesh.center[2]), x_length=500, y_length=500, z_length=500 |
| 68 |
) |
| 69 |
roi.rotate_z(33, inplace=True) |
| 70 |
|
| 71 |
pl = pv.Plotter() |
| 72 |
pl.add_mesh(roi, opacity=0.75, color='red') |
| 73 |
pl.add_mesh(mesh, opacity=0.5) |
| 74 |
pl.show() |
| 75 |
|
| 76 |
# %% |
| 77 |
# Run the box clipping algorithm |
| 78 |
extracted = mesh.clip_box(roi, invert=False) |
| 79 |
|
| 80 |
pl = pv.Plotter(shape=(1, 2)) |
| 81 |
pl.add_mesh(roi, opacity=0.75, color='red') |
| 82 |
pl.add_mesh(mesh) |
| 83 |
pl.subplot(0, 1) |
| 84 |
pl.add_mesh(extracted) |
| 85 |
pl.add_mesh(roi, opacity=0.75, color='red') |
| 86 |
pl.link_views() |
| 87 |
pl.view_isometric() |
| 88 |
pl.show() |
| 89 |
|
| 90 |
# %% |
| 91 |
# Crinkled Clipping |
| 92 |
# +++++++++++++++++ |
| 93 |
# Crinkled clipping is useful if you don't want the clip filter to truly clip |
| 94 |
# cells on the boundary, but want to preserve the input cell structure and to |
| 95 |
# pass the entire cell on through the boundary. |
| 96 |
# |
| 97 |
# This option is available for :func:`pyvista.DataObjectFilters.clip`, |
| 98 |
# :func:`pyvista.DataObjectFilters.clip_box`, and |
| 99 |
# :func:`pyvista.DataSetFilters.clip_surface`, but not available when clipping |
| 100 |
# by scalar in :func:`pyvista.DataSetFilters.clip_scalar`. |
| 101 |
|
| 102 |
# Input mesh |
| 103 |
mesh = pv.Wavelet() |
| 104 |
|
| 105 |
# %% |
| 106 |
# Define clipping plane |
| 107 |
normal = (1, 1, 1) |
| 108 |
plane = pv.Plane(i_size=30, j_size=30, direction=normal) |
| 109 |
|
| 110 |
# %% |
| 111 |
# Perform a standard clip |
| 112 |
clipped = mesh.clip(normal=normal) |
| 113 |
|
| 114 |
# %% |
| 115 |
# Perform a crinkled clip |
| 116 |
crinkled = mesh.clip(normal=normal, crinkle=True) |
| 117 |
|
| 118 |
# %% |
| 119 |
# Plot comparison |
| 120 |
pl = pv.Plotter(shape=(1, 2)) |
| 121 |
pl.add_mesh(clipped, show_edges=True) |
| 122 |
pl.add_mesh(plane.extract_feature_edges(), color='r') |
| 123 |
pl.subplot(0, 1) |
| 124 |
pl.add_mesh(crinkled, show_edges=True) |
| 125 |
pl.add_mesh(plane.extract_feature_edges(), color='r') |
| 126 |
pl.link_views() |
| 127 |
pl.show() |
| 128 |
# %% |
| 129 |
# .. tags:: filter |