pyvista.PointSet.validate_mesh#
- PointSet.validate_mesh(
- validation_fields: _MeshValidator._AllValidationOptions | Sequence[_MeshValidator._AllValidationOptions] = ('data', 'cells', 'points'),
- action: _MeshValidator._ActionOptions | None = None,
Validate this mesh’s array data, cells, and points.
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, cells, and points. 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
point_dataandcell_dataarrays.point_data_wrong_length: Ensure the length of each point data array matchesn_points.cell_data_wrong_length: Ensure the length of each cell data array matchesn_cells.
Cell validation fields
wrong_number_of_points: Ensure each cell has the minimum number of points needed to describe it.intersecting_edges: Ensure two edges of a 2D cell do not intersect.intersecting_faces: Ensure two faces of a 3D cell do not intersect.non_contiguous_edges: Ensure edges around the perimeter of a 2D cell are contiguous.non_convex: Ensure all 2D and 3D cells are convex.inverted_faces: Ensure the faces of a cell point in the direction required by itsCellType.non_planar_faces: Ensure vertices for a face all lie in the same plane.degenerate_faces: Ensure faces do not collapse to a line or a point through repeated collocated vertices.coincident_points: Ensure there are no duplicate coordinates or repeated use of the same connectivity entry.invalid_point_references: Ensure all points referenced by cells are valid point ids that can be indexed.
Note
Other than
invalid_point_references, all cell fields are computed usingcell_validator().Point validation fields
unused_points: Ensure all points are referenced by at least one cell.non_finite_points: Ensure all points have real values (i.e. noNaNorInf).
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.
- Parameters:
- validation_fields
str| sequence[str], default: (‘data’, ‘cells’, ‘points’) Select which field(s) to include in the validation report. All data, cell, and point 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'cells'to include all cell fields'points'to include all point fields'memory_safe'to include all fields that, if invalid, may cause a segmentation fault and crash Python. This option includespoint_data_wrong_length,cell_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 Report summary: Is valid : True Invalid fields : () Invalid data arrays: Point data wrong length : [] Cell data wrong length : [] Invalid cell ids: Wrong number of points : [] Intersecting edges : [] Intersecting faces : [] Non-contiguous edges : [] Non-convex : [] Inverted faces : [] Non-planar faces : [] Degenerate faces : [] Coincident points : [] Invalid point references : [] Invalid point ids: Unused points : [] Non-finite points : []
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 Report summary: Is valid : False Invalid fields (1) : ('non_convex',) Invalid cell ids: Wrong number of points : [] Intersecting edges : [] Intersecting faces : [] Non-contiguous edges : [] Non-convex (3) : [1013, 1532, 3250] Inverted faces : [] Non-planar faces : [] Degenerate faces : [] Coincident points : [] Invalid point references : []
>>> 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 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 Report summary: Is valid : True Invalid fields : () Invalid cell ids: Intersecting edges : [] Invalid point ids: Unused points : []
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: Point data wrong length : [] Cell data wrong length : [] Blocks with invalid cells: Wrong number of points : [] Intersecting edges : [] Intersecting faces : [] Non-contiguous edges : [] Non-convex (1) : [0] Inverted faces : [] Non-planar faces : [] Degenerate faces : [] Coincident points : [] Invalid point references : [] Blocks with invalid points: Unused points : [] Non-finite points : []
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 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]