| 1 |
""" |
| 2 |
.. _fill_holes_example: |
| 3 |
|
| 4 |
Repair a Surface with fill_holes |
| 5 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
Seal small openings in a surface with |
| 8 |
:func:`pyvista.PolyDataFilters.fill_holes`. |
| 9 |
""" |
| 10 |
|
| 11 |
import numpy as np |
| 12 |
import pyvista as pv |
| 13 |
|
| 14 |
# sphinx_gallery_thumbnail_number = 2 |
| 15 |
|
| 16 |
# %% |
| 17 |
# Punch holes in a closed surface |
| 18 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 19 |
# Start from a watertight :func:`~pyvista.Sphere`, then drop a handful of |
| 20 |
# faces to create three open holes of different sizes. |
| 21 |
|
| 22 |
rng = np.random.default_rng(seed=0) |
| 23 |
sphere = pv.Sphere(theta_resolution=60, phi_resolution=60).triangulate() |
| 24 |
|
| 25 |
centers = sphere.cell_centers().points |
| 26 |
seed_indices = [ |
| 27 |
int(np.argmin(np.linalg.norm(centers - target, axis=1))) |
| 28 |
for target in [(0.5, 0.0, 0.0), (-0.3, 0.4, 0.1), (0.0, -0.4, -0.3)] |
| 29 |
] |
| 30 |
hole_sizes = [60, 25, 10] # cells removed per hole, controls each hole's radius |
| 31 |
|
| 32 |
drop_cells = set() |
| 33 |
for seed, size in zip(seed_indices, hole_sizes, strict=True): |
| 34 |
candidates = np.argsort(np.linalg.norm(centers - centers[seed], axis=1))[:size] |
| 35 |
drop_cells.update(int(c) for c in candidates) |
| 36 |
|
| 37 |
keep_mask = np.ones(sphere.n_cells, dtype=bool) |
| 38 |
keep_mask[list(drop_cells)] = False |
| 39 |
open_mesh = sphere.extract_cells(keep_mask).extract_surface(algorithm=None) |
| 40 |
|
| 41 |
# %% |
| 42 |
# Highlight the open boundary loops |
| 43 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 44 |
# Extracting boundary edges traces the perimeter of every hole. |
| 45 |
|
| 46 |
boundary = open_mesh.extract_feature_edges( |
| 47 |
boundary_edges=True, |
| 48 |
feature_edges=False, |
| 49 |
manifold_edges=False, |
| 50 |
non_manifold_edges=False, |
| 51 |
) |
| 52 |
|
| 53 |
pl = pv.Plotter() |
| 54 |
pl.add_mesh(open_mesh, color='wheat', smooth_shading=True, show_edges=False) |
| 55 |
pl.add_mesh(boundary, color='tomato', line_width=6) |
| 56 |
pl.show() |
| 57 |
|
| 58 |
|
| 59 |
# %% |
| 60 |
# Fill the holes |
| 61 |
# ~~~~~~~~~~~~~~ |
| 62 |
# The ``hole_size`` argument bounds the largest opening that ``fill_holes`` |
| 63 |
# will close. Set it large enough to cover every loop you want to repair. |
| 64 |
|
| 65 |
repaired = open_mesh.fill_holes(hole_size=1.0) |
| 66 |
repaired_boundary = repaired.extract_feature_edges( |
| 67 |
boundary_edges=True, |
| 68 |
feature_edges=False, |
| 69 |
manifold_edges=False, |
| 70 |
non_manifold_edges=False, |
| 71 |
) |
| 72 |
|
| 73 |
pl = pv.Plotter(shape=(1, 2)) |
| 74 |
pl.subplot(0, 0) |
| 75 |
pl.add_mesh(open_mesh, color='wheat', smooth_shading=True) |
| 76 |
pl.add_mesh(boundary, color='tomato', line_width=6) |
| 77 |
pl.add_text('Open', font_size=14) |
| 78 |
pl.subplot(0, 1) |
| 79 |
pl.add_mesh(repaired, color='wheat', smooth_shading=True, show_edges=True) |
| 80 |
if repaired_boundary.n_cells: |
| 81 |
pl.add_mesh(repaired_boundary, color='tomato', line_width=6) |
| 82 |
pl.add_text('Filled', font_size=14) |
| 83 |
pl.link_views() |
| 84 |
pl.show() |
| 85 |
|
| 86 |
|
| 87 |
# %% |
| 88 |
# Confirm the repair |
| 89 |
# ~~~~~~~~~~~~~~~~~~ |
| 90 |
# The filled surface has zero open edges, while the original had one |
| 91 |
# loop per hole. |
| 92 |
|
| 93 |
open_mesh.n_open_edges, repaired.n_open_edges |
| 94 |
# %% |
| 95 |
# .. tags:: filter |