Debian Codegraph @

examples/01-filter/surface_smoothing.py

raw
1 """
2 .. _surface_smoothing_example:
3
4 Surface Smoothing
5 ~~~~~~~~~~~~~~~~~
6
7 Smoothing rough edges of a surface mesh
8 """
9
10 # sphinx_gallery_thumbnail_number = 4
11 import pyvista as pv
12 from pyvista import examples
13
14 # %%
15 # Suppose you extract a volumetric subset of a dataset that has roughly defined
16 # edges. Perhaps you'd like a smooth representation of that model region. This
17 # can be achieved by extracting the bounding surface of the volume and applying
18 # a :func:`pyvista.PolyDataFilters.smooth` filter.
19 #
20 # The below code snippet loads a sample roughly edged volumetric dataset:
21
22 # Vector to view rough edges
23 cpos = [-2, 5, 3]
24
25 # Load dataset
26 data = examples.load_uniform()
27 # Extract a rugged volume
28 vol = data.threshold_percent(30, invert=1)
29 vol.plot(show_edges=True, cpos=cpos, show_scalar_bar=False)
30
31 # %%
32 # Extract the outer surface of the volume using the
33 # :func:`~pyvista.DataObjectFilters.extract_surface` filter and then apply the
34 # smoothing filter:
35
36 # Get the out surface as PolyData
37 surf = vol.extract_surface(algorithm=None)
38 # Smooth the surface
39 smooth = surf.smooth()
40 smooth.plot(show_edges=True, cpos=cpos, show_scalar_bar=False)
41
42
43 # %%
44 # Not smooth enough? Try increasing the number of iterations for the Laplacian
45 # smoothing algorithm:
46
47 # Smooth the surface even more
48 smooth = surf.smooth(n_iter=100)
49 smooth.plot(show_edges=True, cpos=cpos, show_scalar_bar=False)
50
51
52 # %%
53 # Still not smooth enough? Increase the number of iterations for the Laplacian
54 # smoothing algorithm to a crazy high value. Note how this causes the mesh to
55 # "shrink":
56
57 # Smooth the surface EVEN MORE
58 smooth = surf.smooth(n_iter=1000)
59
60 # extract the edges of the original unsmoothed mesh
61 orig_edges = surf.extract_feature_edges()
62
63 pl = pv.Plotter()
64 pl.add_mesh(smooth, show_edges=True, show_scalar_bar=False)
65 pl.camera_position = cpos
66 pl.add_mesh(orig_edges, show_scalar_bar=False, color='k', line_width=2)
67 pl.show()
68
69
70 # %%
71 # Taubin Smoothing
72 # ~~~~~~~~~~~~~~~~
73 # You can reduce the amount of surface shrinkage by using Taubin smoothing
74 # rather than the default laplacian smoothing implemented in :func:`smooth()
75 # <pyvista.PolyDataFilters.smooth>`. In this example, you can see how Taubin
76 # smoothing maintains the volume relative to the original mesh.
77 #
78 # Also, note that the number of iterations can be reduced to get the same approximate
79 # amount of smoothing. This is because Taubin smoothing is more efficient.
80
81 smooth_w_taubin = surf.smooth_taubin(n_iter=50, pass_band=0.05)
82
83 pl = pv.Plotter()
84 pl.add_mesh(smooth_w_taubin, show_edges=True, show_scalar_bar=False)
85 pl.camera_position = cpos
86 pl.add_mesh(orig_edges, show_scalar_bar=False, color='k', line_width=2)
87 pl.show()
88
89 # output the volumes of the original and smoothed meshes
90 print(f'Original surface volume: {surf.volume:.1f}')
91 print(f'Laplacian smoothed volume: {smooth.volume:.1f}')
92 print(f'Taubin smoothed volume: {smooth_w_taubin.volume:.1f}')
93 # %%
94 # .. tags:: filter