Note
Click here to download the full example code
Surface Smoothing¶
Smoothing rough edges of a surface mesh
# sphinx_gallery_thumbnail_number = 4
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)

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)

Out:
[(-5.270462173884933, 28.926155439895044, 19.155693264529337),
(4.500000001480775, 4.500000001480775, 4.500000001480775),
(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)

Out:
[(-5.2703475762355385, 28.925595495391537, 19.155326046354773),
(4.499921872800769, 4.499921872800769, 4.499921872800314),
(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)

Out:
[(-5.015056085827215, 28.092312435289877, 18.633064286399282),
(4.444192063063383, 4.444192063063383, 4.444192063063383),
(0.0, 0.0, 1.0)]
Total running time of the script: ( 0 minutes 3.445 seconds)