# Examples from pyvista.DataObjectFilters.validate_mesh
# =====================================================

# 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

# Print the report. By default, the report is relatively minimal, and only includes
# an error message if the mesh is invalid.
print(report)

# Use `report_body` to explicitly show the validated fields.
report = mesh.validate_mesh(report_body='fields')
print(report)

# Load a mesh with invalid cells, for example,
# `download_cow()`
# and validate it. Use `'cells'` to only validate the cells specifically.
mesh = examples.download_cow()
report = mesh.validate_mesh('cells', report_body='fields')

# Alternatively, use `exclude_fields` to remove fields from the report instead.
# For example, excluding `data` and `points` is the same as including `cells`.
report_excluded = mesh.validate_mesh(exclude_fields=['data', 'points'])
report_excluded == report

# Show the report. Note that only cell validation fields are reported (array and point
# fields are omitted).
print(report)

report.is_valid

# Show what the issues are.
report.invalid_fields

# Show the cell ids of the non-convex cells.
report.non_convex

# Access the same underlying mesh array of non-convex cell ids that was internally computed
# by `cell_validator()`.
report.mesh.field_data['non_convex']

# 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)

# Show a validation report for cells with intersecting edges and unused points only.
report = mesh.validate_mesh(
    ['intersecting_edges', 'unused_points'], report_body='fields'
)
print(report)

# Even though other fields are invalid (that is, `non_convex`), for these specific
# validation fields the mesh is considered valid.
report.is_valid

# 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()
print(report)

# Validate again but show the fields in report body.
# 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.
report = multi.validate_mesh(report_body='fields')
print(report)

# Subreports for each block can be accessed with indexing.
len(report)
subreport = report[0]
subreport.non_convex

# ----------------------------------------------------------------------
# Generated by sphinx-examples-as-code https://github.com/pyvista/sphinx-examples-as-code
