Debian Codegraph @

examples/01-filter/clip_closed_surface.py

raw
1 """
2 .. _clip_closed_surface_example:
3
4 Clip and Cap a Closed Surface
5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6
7 Compare a standard planar clip, which leaves an open boundary, with
8 :func:`pyvista.PolyDataFilters.clip_closed_surface`, which seals the cut face.
9 """
10
11 import pyvista as pv
12 from pyvista import examples
13
14 # sphinx_gallery_thumbnail_number = 2
15
16 # %%
17 # Load a closed surface
18 # ~~~~~~~~~~~~~~~~~~~~~
19 # The :func:`~pyvista.examples.downloads.download_lucy` scan is a watertight,
20 # manifold PolyData surface with no open edges.
21
22 surface = examples.download_lucy().triangulate()
23 surface
24
25
26 # %%
27 # Define an oblique cutting plane
28 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29 # The same plane is reused for both the open and the closed-surface clip.
30
31 plane_origin = surface.center
32 plane_normal = (1, 0.3, 0)
33 plane_size = surface.length * 1.2
34 plane = pv.Plane(
35 center=plane_origin, direction=plane_normal, i_size=plane_size, j_size=plane_size
36 )
37
38 pl = pv.Plotter()
39 pl.add_mesh(surface, color='wheat', smooth_shading=True)
40 pl.add_mesh(plane, style='wireframe', color='black', line_width=2)
41 pl.view_xz()
42 pl.camera.azimuth = 35
43 pl.show()
44
45
46 # %%
47 # Compare an open cut to a capped cut
48 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49 # A standard clip leaves a boundary loop along the cut. The closed-surface
50 # clip caps that loop with new triangles and stays watertight.
51
52 open_clip = surface.clip(normal=plane_normal, origin=plane_origin)
53 closed_clip = surface.clip_closed_surface(normal=plane_normal, origin=plane_origin)
54
55 open_boundary = open_clip.extract_feature_edges(
56 boundary_edges=True,
57 feature_edges=False,
58 manifold_edges=False,
59 non_manifold_edges=False,
60 )
61
62 pl = pv.Plotter(shape=(1, 2))
63 pl.subplot(0, 0)
64 pl.add_mesh(open_clip, color='wheat', smooth_shading=True)
65 pl.add_mesh(open_boundary, color='tomato', line_width=6)
66 pl.add_mesh(plane, style='wireframe', color='black', line_width=2)
67 pl.view_xz()
68 pl.camera.azimuth = 35
69 pl.subplot(0, 1)
70 pl.add_mesh(closed_clip, color='wheat', smooth_shading=True)
71 pl.add_mesh(plane, style='wireframe', color='black', line_width=2)
72 pl.view_xz()
73 pl.camera.azimuth = 35
74 pl.link_views()
75 pl.show()
76
77
78 # %%
79 # Confirm that the capped result is watertight
80 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
81 # The open clip carries hundreds of boundary edges; the capped result has none.
82
83 open_boundary.n_cells, closed_clip.n_open_edges
84 # %%
85 # .. tags:: filter