pyvista.PointSet.validate_mesh#
- PointSet.validate_mesh(
- validation_fields: Literal['cell_data_wrong_length', 'point_data_wrong_length'] | Literal['non_finite_points', 'unused_points'] | Literal['coincident_points', 'degenerate_faces', 'intersecting_edges', 'intersecting_faces', 'invalid_point_references', 'inverted_faces', 'negative_size', 'non_contiguous_edges', 'non_convex', 'non_planar_faces', 'wrong_number_of_points', 'zero_size'] | Literal['data', 'points', 'cells'] | Literal['memory_safe'] | Sequence[Literal['cell_data_wrong_length', 'point_data_wrong_length', 'non_finite_points', 'unused_points', 'coincident_points', 'degenerate_faces', 'intersecting_edges', 'intersecting_faces', 'invalid_point_references', 'inverted_faces', 'negative_size', 'non_contiguous_edges', 'non_convex', 'non_planar_faces', 'wrong_number_of_points', 'zero_size', 'data', 'points', 'cells', 'memory_safe']] | None = None,
- action: Literal['warn', 'error'] | None = None,
Validate this mesh’s array data, points, and cells.
This method returns a
MeshValidationReportdataclass with information about the validity of a mesh. The dataclass contains validation fields which are specific to the mesh’s data, points, and cells. By default, all validation fields below are checked and included in the report. Optionally, only a subset of fields may be requested, and a warning or error may be raised if the mesh is not valid.Data validation fields
Fields specific to
cell_dataandpoint_dataarrays.cell_data_wrong_length: Ensure the length of each cell data array matchesn_cells.point_data_wrong_length: Ensure the length of each point data array matchesn_points.
Note
When setting new arrays using PyVista’s API, similar array validation checks are already implicitly performed. As such, these checks may be redundant in many cases. They are most useful for validating newly loaded or
wrappedmeshes.Point validation fields
non_finite_points: Ensure all points have real values (i.e. noNaNorInf).unused_points: Ensure all points are referenced by at least one cell.
Cell validation fields
coincident_points: Ensure there are no duplicate coordinates or repeated use of the same connectivity entry.degenerate_faces: Ensure faces do not collapse to a line or a point through repeated collocated vertices.intersecting_edges: Ensure two edges of a 2D cell do not intersect.intersecting_faces: Ensure two faces of a 3D cell do not intersect.invalid_point_references: Ensure all points referenced by cells are valid point ids that can be indexed bypoints.inverted_faces: Ensure the faces of a cell point in the direction required by itsCellType.negative_size: Ensure 1D, 2D, and 3D cells have positive length, area, and volume, respectively.non_contiguous_edges: Ensure edges around the perimeter of a 2D cell are contiguous.non_convex: Ensure all 2D and 3D cells are convex.non_planar_faces: Ensure vertices for a face all lie in the same plane.wrong_number_of_points: Ensure each cell has the minimum number of points needed to describe it.zero_size: Ensure 1D, 2D, and 3D cells have non-zero length, area, and volume, respectively.
Note
All cell fields are computed using
cell_validator(), and the field names correspond toCellStatusnames.For each field, its value is:
Noneif the field is omitted from the report,an empty list
[]if the field is included but there is no issue to report for it, ora list of invalid items (e.g. invalid array names or cell/point ids).
In addition to the validation fields above, the report includes properties for convenience:
is_valid: evaluates toTruewhen all fields areNoneor empty.invalid_fields: tuple of validation field names where problems were detected.mesh: a shallow copy of the validated mesh. If any cell fields are included which are computed bycell_validator(), this mesh includes the output from that filter.message: message string generated by the report. The message contains a compact summary of any problems detected, and is formatted for printing to console. This is the message used when theactionkeyword is set for emitting warnings or raising errors. This value isNoneif the mesh is valid.
Validating composite
MultiBlockis also supported. In this case, all mesh blocks are validated separately and the results are aggregated and reported per-block.Added in version 0.47.
Changed in version 0.48: Include cell fields
zero_sizeandnegative_size.Changed in version 0.48: Report fields are now sorted in alphabetical order. Point fields are also reported before cell fields.
- Parameters:
- validation_fields
str| sequence[str], default: (‘data’, ‘cells’, ‘points’) Select which field(s) to include in the validation report. All data, point, and cell fields are included by default. Specify individual fields by name, or use group name(s) to include multiple related validation fields:
'data'to include all data fields'points'to include all point fields'cells'to include all cell fields'memory_safe'to include all fields that, if invalid, may cause a segmentation fault and crash Python. This option includescell_data_wrong_length,point_data_wrong_length, andinvalid_point_references.
Fields that are excluded from the report will have a value of
None.- action‘warn’ | ‘error’,
optional Issue a warning or raise an error if the mesh is not valid for the specified fields. By default, no action is taken.
- validation_fields
- Returns:
MeshValidationReportReport dataclass with information about mesh validity.
Examples
Create a
Sphere()and check if it’s a valid mesh.>>> import pyvista as pv >>> from pyvista import examples >>> mesh = pv.Sphere() >>> report = mesh.validate_mesh() >>> report.is_valid True
Print the full report.
>>> print(report) Mesh Validation Report ---------------------- Mesh: Type : PolyData N Points : 842 N Cells : 1680 Cell types : {TRIANGLE} Report summary: Is valid : True Invalid fields : () Invalid data arrays: Cell data wrong length : [] Point data wrong length : [] Invalid point ids: Non-finite points : [] Unused points : [] Invalid cell ids: Coincident points : [] Degenerate faces : [] Intersecting edges : [] Intersecting faces : [] Invalid point references : [] Inverted faces : [] Negative size : [] Non-contiguous edges : [] Non-convex : [] Non-planar faces : [] Wrong number of points : [] Zero size : []
Load a mesh with invalid cells, e.g.
download_cow()and validate it. Use'cells'to only validate the cells specifically.>>> mesh = examples.download_cow() >>> report = mesh.validate_mesh('cells')
Show the report. Note that only cell validation fields are reported (array and point fields are omitted).
>>> print(report) Mesh Validation Report ---------------------- Mesh: Type : PolyData N Points : 2903 N Cells : 3263 Cell types : {TRIANGLE, POLYGON, QUAD} Report summary: Is valid : False Invalid fields (1) : ('non_convex',) Invalid cell ids: Coincident points : [] Degenerate faces : [] Intersecting edges : [] Intersecting faces : [] Invalid point references : [] Inverted faces : [] Negative size : [] Non-contiguous edges : [] Non-convex (3) : [1013, 1532, 3250] Non-planar faces : [] Wrong number of points : [] Zero size : []
>>> report.is_valid False
Show what the issue(s) are.
>>> report.invalid_fields ('non_convex',)
Show the cell ids of the non-convex cells.
>>> report.non_convex [1013, 1532, 3250]
Access the same underlying mesh array of non-convex cell ids that was internally computed by
cell_validator().>>> report.mesh.field_data['non_convex'] pyvista_ndarray([1013, 1532, 3250])
Print the message generated by the report. This is the message used when the
actionkeyword is set for emitting warnings or raising errors.>>> print(report.message) PolyData mesh is not valid due to the following problems: - Mesh has 3 non-convex QUAD cells. Invalid cell ids: [1013, 1532, 3250]
Show a validation report for cells with intersecting edges and unused points only.
>>> report = mesh.validate_mesh(['intersecting_edges', 'unused_points']) >>> print(report) Mesh Validation Report ---------------------- Mesh: Type : PolyData N Points : 2903 N Cells : 3263 Cell types : {TRIANGLE, POLYGON, QUAD} Report summary: Is valid : True Invalid fields : () Invalid point ids: Unused points : [] Invalid cell ids: Intersecting edges : []
Even though other fields are invalid (i.e.
non_convex), for these specific validation fields the mesh is considered valid.>>> report.is_valid True
Do minimal validation to ensure the mesh properties are “memory_safe”. This helps to avoid a segmentation fault which may be caused by invalid memory accesses by VTK. In this case, we use
actionto raise an error if the mesh is not valid.>>> _ = mesh.validate_mesh('memory_safe', action='error')
Validate the mesh as a
MultiBlockinstead.>>> multi = mesh.cast_to_multiblock() >>> report = multi.validate_mesh()
Instead of reporting problems with specific arrays, point ids, or cell ids, the errors are reported by block id. Here, block id
0is reported as having non-convex cells.>>> print(report) Mesh Validation Report ---------------------- Mesh: Type : MultiBlock N Blocks : 1 Report summary: Is valid : False Invalid fields (1) : ('non_convex',) Blocks with invalid data arrays: Cell data wrong length : [] Point data wrong length : [] Blocks with invalid points: Non-finite points : [] Unused points : [] Blocks with invalid cells: Coincident points : [] Degenerate faces : [] Intersecting edges : [] Intersecting faces : [] Invalid point references : [] Inverted faces : [] Negative size : [] Non-contiguous edges : [] Non-convex (1) : [0] Non-planar faces : [] Wrong number of points : [] Zero size : []
The report message still contains specifics about the invalid cell ids though.
>>> print(report.message) MultiBlock mesh is not valid due to the following problems: - Block id 0 'Block-00' PolyData mesh is not valid due to the following problems: - Mesh has 3 non-convex QUAD cells. Invalid cell ids: [1013, 1532, 3250]
And subreports for each block can be accessed with indexing.
>>> len(report) 1 >>> subreport = report[0] >>> subreport.non_convex [1013, 1532, 3250]