Surface Smoothing

Smoothing rough edges of a surface mesh

# sphinx_gallery_thumbnail_number = 4
import pyvista as pv
from pyvista import examples

Suppose you extract a volumetric subset of a dataset that has roughly defined edges. Perhaps you’d like a smooth representation of that model region. This can be achieved by extracting the bounding surface of the volume and applying a pyvista.PolyData.smooth() filter.

The below code snippet loads a sample roughly edged volumetric dataset:

# Vector to view rough edges
cpos = [-2, 5, 3]

# Load dataset
data = examples.load_uniform()
# Extract a rugged volume
vol = data.threshold_percent(30, invert=1)
vol.plot(show_edges=True, cpos=cpos)
../../_images/sphx_glr_surface-smoothing_001.png

Out:

[(-5.270462178580788, 28.92615544645197, 19.155693267871182), (4.5, 4.5, 4.5), (0.0, 0.0, 1.0)]

Extract the outer surface of the volume using the pyvista.DataSetFilters.extract_geometry() filter and then apply the smoothing filter:

# Get the out surface as PolyData
surf = vol.extract_geometry()
# Smooth the surface
smooth = surf.smooth()
smooth.plot(show_edges=True, cpos=cpos)
../../_images/sphx_glr_surface-smoothing_002.png

Out:

[(-5.270449328884762, 28.92608494200338, 19.155646578892483), (4.499989034226136, 4.499989034226136, 4.499989034226136), (0.0, 0.0, 1.0)]

Not smooth enough? Try increasing the number of iterations for the Laplacian smoothing algorithm:

# Smooth the surface even more
smooth = surf.smooth(n_iter=100)
smooth.plot(show_edges=True, cpos=cpos)
../../_images/sphx_glr_surface-smoothing_003.png

Out:

[(-5.26914349711325, 28.91898708436228, 19.15094977536953), (4.498893811879498, 4.498893811880407, 4.498893811880407), (0.0, 0.0, 1.0)]

Still not smooth enough? Increase the number of iterations for the Laplacian smoothing algorithm to a crazy high value:

# Smooth the surface EVEN MORE
smooth = surf.smooth(n_iter=1000)
smooth.plot(show_edges=True, cpos=cpos)
../../_images/sphx_glr_surface-smoothing_004.png

Out:

[(-4.9153654971275795, 27.54192325871539, 18.2684121856174), (4.358145575970411, 4.358145575970411, 4.358145575970411), (0.0, 0.0, 1.0)]

Total running time of the script: ( 0 minutes 9.749 seconds)

Gallery generated by Sphinx-Gallery