Debian Codegraph @

examples/01-filter/mesh_quality.py

raw
1 """
2 .. _mesh_quality_example:
3
4 Computing Mesh Quality
5 ~~~~~~~~~~~~~~~~~~~~~~
6
7 Leverage powerful VTK algorithms for computing mesh quality.
8
9 Here we will use the :func:`~pyvista.DataObjectFilters.cell_quality` filter
10 to compute the cell qualities. The following quality measures are available
11 for various cell types:
12
13 .. include:: /api/core/cell_quality/cell_quality_measures_table.rst
14
15 """
16
17 # sphinx_gallery_thumbnail_number = 2
18 import pyvista as pv
19 from pyvista import examples
20
21 # %%
22 # Triangle Cell Quality
23 # ---------------------
24 # Load a :class:`~pyvista.PolyData` mesh and :meth:`~pyvista.PolyDataFilters.decimate`
25 # it to show coarse :attr:`~pyvista.CellType.TRIANGLE` cells for the example.
26 # Here we use :meth:`~pyvista.examples.downloads.download_cow`.
27
28 mesh = examples.download_cow().triangulate().decimate(0.7)
29
30 # %%
31 # Compute some valid measures for triangle cells.
32
33 measures = ['area', 'shape', 'min_angle', 'max_angle']
34 qual = mesh.cell_quality(measures)
35
36 # %%
37 # Plot the meshes in subplots for comparison. We define a custom method
38 # for adding each mesh to each subplot.
39
40
41 def add_mesh(plotter, mesh, *, scalars=None, cmap='bwr', show_edges=True):
42 # Create a copy to avoid reusing the same mesh in different plots
43 copied = mesh.copy(deep=False)
44 plotter.add_mesh(copied, scalars=scalars, cmap=cmap, show_edges=show_edges)
45 plotter.view_xy()
46
47
48 pl = pv.Plotter(shape=(2, 2))
49 pl.link_views()
50 pl.subplot(0, 0)
51 add_mesh(pl, qual, scalars=measures[0])
52 pl.subplot(0, 1)
53 add_mesh(pl, qual, scalars=measures[1])
54 pl.subplot(1, 0)
55 add_mesh(pl, qual, scalars=measures[2])
56 pl.subplot(1, 1)
57 add_mesh(pl, qual, scalars=measures[3])
58 pl.show()
59
60
61 # %%
62 # Visualize Acceptable Range
63 # ==========================
64 # The previous plots show the full range of cell quality values present in the mesh.
65 # However, it may be more useful to show the `acceptable` range of values instead.
66 # Get the acceptable range for the ``shape`` quality measure using
67 # :func:`~pyvista.cell_quality_info`.
68
69 info = pv.cell_quality_info('TRIANGLE', 'shape')
70 print(info)
71
72 # %%
73 # Plot the shape quality measure again but this time we color the cells based on
74 # the acceptable range for the measure. Cells outside of this range are saturated
75 # as blue or red and may be considered to be "poor" quality cells.
76
77 qual.plot(
78 scalars='shape',
79 clim=info.acceptable_range,
80 cmap='bwr',
81 below_color='blue',
82 above_color='red',
83 cpos='xy',
84 zoom=1.5,
85 show_axes=False,
86 )
87
88 # %%
89 # Use :meth:`~pyvista.DataSetFilters.extract_values` to extract the "poor" quality
90 # cells outside the acceptable range.
91
92 unacceptable = qual.extract_values(
93 scalars='shape', ranges=info.acceptable_range, invert=True
94 )
95
96 # %%
97 # Plot the unacceptable cells along with the original mesh as wireframe for context.
98
99 pl = pv.Plotter()
100 pl.add_mesh(mesh, style='wireframe', color='light gray')
101 pl.add_mesh(unacceptable, color='lime')
102 pl.view_xy()
103 pl.camera.zoom(1.5)
104 pl.show()
105
106 # %%
107 # Tetrahedron Cell Quality
108 # ------------------------
109 # Load a mesh with :attr:`~pyvista.CellType.TETRA` cells. Here we use
110 # :meth:`~pyvista.examples.downloads.download_letter_a`.
111
112 mesh = examples.download_letter_a()
113
114 # %%
115 # Plot some valid quality measures for tetrahedral cells.
116
117 measures = ['volume', 'collapse_ratio', 'jacobian', 'scaled_jacobian']
118 qual = mesh.cell_quality(measures)
119
120 pl = pv.Plotter(shape=(2, 2))
121 pl.link_views()
122 pl.subplot(0, 0)
123 add_mesh(pl, qual, scalars=measures[0])
124 pl.subplot(0, 1)
125 add_mesh(pl, qual, scalars=measures[1])
126 pl.subplot(1, 0)
127 add_mesh(pl, qual, scalars=measures[2])
128 pl.subplot(1, 1)
129 add_mesh(pl, qual, scalars=measures[3])
130 pl.show()
131
132 # %%
133 # .. tags:: filter