Debian Codegraph @

examples/99-advanced/mesh_validation.py

raw
1 """
2 .. _mesh_validation_example:
3
4 Mesh Validation
5 ~~~~~~~~~~~~~~~
6 This example explores different cases where a mesh may not be considered valid as defined
7 by the :meth:`~pyvista.DataObjectFilters.validate_mesh` method.
8
9 """
10
11 # sphinx_gallery_thumbnail_number = 5
12 import pyvista as pv
13 from pyvista.examples import plot_cell
14
15 # %%
16 # Non-convex cells
17 # ----------------
18 # Many VTK algorithms assume that cells are convex. This can result in incorrect outputs
19 # and may also affect rendering. For example, let's create :class:`~pyvista.PolyData`
20 # with a concave :attr:`~pyvista.CellType.QUAD` cell.
21 points = [
22 [-0.5, -1.0, 0.0],
23 [0.0, -0.3, 0.0],
24 [1.0, 0.0, 0.0],
25 [-0.5, 0.0, 0.0],
26 ]
27 faces = [4, 0, 1, 2, 3]
28 quad = pv.PolyData(points, faces)
29
30 # %%
31 # Use :meth:`~pyvista.DataObjectFilters.validate_mesh` to show that the cell is not
32 # convex.
33 report = quad.validate_mesh()
34 print(report.is_valid)
35 # %%
36 print(report.invalid_fields)
37
38 # %%
39 # If we plot the cell, we can see that the concave cell is incorrectly rendered as though
40 # it's convex even though it is not.
41 plot_cell(quad, 'xy')
42
43 # %%
44 # To address the convexity problem, we can :meth:`~pyvista.PolyDataFilters.triangulate`
45 # the mesh. The mesh is now valid and renders correctly.
46 triangles = quad.triangulate()
47 report = triangles.validate_mesh()
48 print(report.is_valid)
49 # %%
50 plot_cell(triangles, 'xy')
51
52 # %%
53 # Cells with inverted faces
54 # -------------------------
55 # Cells with inverted faces can result in incorrect geometric computations such as
56 # cell volume or centroid. To demonstrate this, we first create a valid
57 # :attr:`~pyvista.CellType.POLYHEDRON` cell similar to the
58 # :func:`~pyvista.examples.cells.Polyhedron` example cell.
59 points = [[0, 0, 0], [1, 0, 0], [0.5, 0.5, 0], [0, 0, 1]]
60 cells = [4, 3, 0, 2, 1, 3, 0, 1, 3, 3, 0, 3, 2, 3, 1, 2, 3]
61 cells = [len(cells), *cells.copy()]
62 polyhedron = pv.UnstructuredGrid(cells, [pv.CellType.POLYHEDRON], points)
63
64 # %%
65 # Plot the cell and show its normals. Since all points have counter-clockwise traversal,
66 # the normals all point outward and the cell is valid.
67 report = polyhedron.validate_mesh()
68 print(report.is_valid)
69 # %%
70 plot_cell(polyhedron, show_normals=True)
71
72 # %%
73 # Now swap two points in the polyhedron's connectivity to generate an otherwise identical
74 # polyhedron with a single incorrectly oriented face.
75 index1 = 3 # index of first point ID of first face
76 index2 = index1 + 1 # index of second point ID of first face
77 point_id1 = cells[index1]
78 cells[index1] = cells[index2]
79 cells[index2] = point_id1
80
81 invalid_polyhedron = pv.UnstructuredGrid(cells, [pv.CellType.POLYHEDRON], points)
82
83 # %%
84 # The cell is now invalid, and the bottom face is incorrectly oriented with its normal
85 # pointing inward.
86 report = invalid_polyhedron.validate_mesh()
87 print(report.is_valid)
88 # %%
89 print(report.invalid_fields)
90 # %%
91 plot_cell(invalid_polyhedron, show_normals=True)
92
93 # %%
94 # Now let's compare the centroid of the valid and invalid cells using
95 # :meth:`~pyvista.DataObjectFilters.cell_centers`. The computed centroids differ,
96 # demonstrating the need to have valid cells when using filters that depend on geometric
97 # properties.
98 valid_centroid = polyhedron.cell_centers().points[0].tolist()
99 print(valid_centroid)
100 # %%
101 invalid_centroid = invalid_polyhedron.cell_centers().points[0].tolist()
102 print(invalid_centroid)
103
104 # %%
105 # Self-intersecting cells
106 # -----------------------
107 # Most :class:`cell types <pyvista.CellType>` have a defined point order which must
108 # be respected. For example, let's try to create a :attr:`~pyvista.CellType.HEXAHEDRON`
109 # cell with eight points:
110 points = [
111 [0.0, 0.0, 0.0],
112 [1.0, 0.0, 0.0],
113 [0.0, 1.0, 0.0],
114 [0.0, 0.0, 1.0],
115 [1.0, 1.0, 0.0],
116 [1.0, 0.0, 1.0],
117 [0.0, 1.0, 1.0],
118 [1.0, 1.0, 1.0],
119 ]
120 cells = [8, 0, 1, 2, 3, 4, 5, 6, 7]
121 celltype = [pv.CellType.HEXAHEDRON]
122 hexahedron = pv.UnstructuredGrid(cells, celltype, points)
123
124 # %%
125 # At a quick glance, the cell may `appear` to be valid, but it is not, since the point
126 # ordering is incorrect.
127 report = hexahedron.validate_mesh()
128 print(report.is_valid)
129 # %%
130 plot_cell(hexahedron)
131
132 # %%
133 # Let's review the invalid fields reported.
134 print(report.invalid_fields)
135
136 # %%
137 # Note that the invalid fields may differ across VTK versions or even operating systems.
138 # If a cell is invalid, there are sometimes multiple inter-related issues that may be
139 # reported.
140
141 # %%
142 # From the plot above, we can visually confirm these issues since some faces appear to
143 # intersect, and others appear to be "folded" and hence there are non-planar. To
144 # investigate the ``'inverted_faces'`` problem further, let's plot the cell again with
145 # normals.
146 plot_cell(hexahedron, show_normals=True)
147
148 # %%
149 # Since some of the normals are pointing inward, this confirms that there are inverted
150 # faces. To make the cell valid, we need to re-order the cell connectivity based on the
151 # required ordering stated in the documentation for :vtk:`vtkHexahedron`.
152 cells = [8, 0, 1, 4, 2, 3, 5, 7, 6] # instead of [8, 0, 1, 2, 3, 4, 5, 6, 7]
153 celltype = [pv.CellType.HEXAHEDRON]
154 hexahedron = pv.UnstructuredGrid(cells, celltype, points)
155 report = hexahedron.validate_mesh()
156 print(report.is_valid)
157 # %%
158 plot_cell(hexahedron)
159
160 # %%
161 # Meshes with unused points
162 # -------------------------
163 # Unused points are points not associated with any cells. These points are not processed
164 # consistently by filters and are often ignored or removed. To demonstrate this, create an
165 # :class:`~pyvista.UnstructuredGrid` with a single unused point.
166 grid = pv.UnstructuredGrid()
167 grid.points = [[0.0, 0.0, 0.0]]
168 print(grid.n_points)
169 # %%
170 print(grid.n_cells)
171
172 # %%
173 # This mesh is not considered valid.
174 report = grid.validate_mesh()
175 print(report.is_valid)
176 # %%
177 print(report.invalid_fields)
178
179 # %%
180 # Use :meth:`~pyvista.DataObjectFilters.extract_surface` on the grid and observe that the
181 # unused point is removed.
182 poly = grid.extract_surface(algorithm=None)
183 print(poly.n_points)
184 # %%
185 print(poly.n_cells)
186
187 # %%
188 # To remedy this, it is recommended to always associate individual points with a
189 # :attr:`~pyvista.CellType.VERTEX` cell. E.g.:
190 points = [[0.0, 0.0, 0.0]]
191 cells = [1, 0]
192 celltypes = [pv.CellType.VERTEX]
193 grid = pv.UnstructuredGrid(cells, celltypes, points)
194 print(grid.n_points)
195 # %%
196 print(grid.n_cells)
197
198 # %%
199 # This time, the point is properly processed by the filter and is retained.
200 poly = grid.extract_surface(algorithm=None)
201 print(poly.n_points)
202 # %%
203 print(poly.n_cells)
204
205 # %%
206 # This mesh is also now considered valid.
207 report = grid.validate_mesh()
208 print(report.is_valid)