pyvista.PointSet.validate_mesh

pyvista.PointSet.validate_mesh#

PointSet.validate_mesh(
validation_fields: _MeshValidator._AllValidationOptions | Sequence[_MeshValidator._AllValidationOptions] = ('data', 'cells', 'points'),
action: _MeshValidator._ActionOptions | None = None,
) _MeshValidationReport[_DataSetOrMultiBlockType][source]#

Validate this mesh’s array data, cells, and points.

This method returns a MeshValidationReport dataclass 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_data and cell_data arrays.

  • point_data_wrong_length: Ensure the length of each point data array matches n_points.

  • cell_data_wrong_length: Ensure the length of each cell data array matches n_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 its CellType.

  • 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 using cell_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. no NaN or Inf).

For each field, its value is:

  • None if the field is omitted from the report,

  • an empty list [] if the field is included but there is no issue to report for it, or

  • a 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 to True when all fields are None or 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 by cell_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 the action keyword is set for emitting warnings or raising errors. This value is None if the mesh is valid.

Validating composite MultiBlock is 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_fieldsstr | 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 includes point_data_wrong_length, cell_data_wrong_length, and invalid_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.

Returns:
MeshValidationReport

Report 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 action keyword 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 action to raise an error if the mesh is not valid.

>>> _ = mesh.validate_mesh('memory_safe', action='error')

Validate the mesh as a MultiBlock instead.

>>> 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 0 is 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]