Holey Cow#

Repair a holey cow

import numpy as np

# sphinx_gallery_thumbnail_number = 1
import pyvista as pv
from pyvista import examples

import pymeshfix as mf
cow = examples.download_cow()

# Add holes and cast to triangulated PolyData
cow["random"] = np.random.rand(cow.n_cells)
holy_cow = cow.threshold(0.9, invert=True).extract_geometry().triangulate()
print(holy_cow)
PolyData (0x7fed9c7c9960)
  N Cells:    5233
  N Points:   2901
  N Strips:   0
  X Bounds:   -4.446e+00, 5.998e+00
  Y Bounds:   -3.637e+00, 2.760e+00
  Z Bounds:   -1.701e+00, 1.701e+00
  N Arrays:   1
# A nice camera location of the cow
cpos = [(6.56, 8.73, 22.03), (0.77, -0.44, 0.0), (-0.13, 0.93, -0.35)]

meshfix = mf.MeshFix(holy_cow)
holes = meshfix.extract_holes()

# Render the mesh and outline the holes
p = pv.Plotter()
p.add_mesh(holy_cow, color=True)
p.add_mesh(holes, color="r", line_width=8)
p.camera_position = cpos
p.enable_eye_dome_lighting()  # helps depth perception
p.show()
cow

Repair the holey cow

meshfix.repair(verbose=True)
Removed 1 small components
Patching holes...
Patched 182 holes
Fixing degeneracies and intersections

Show the repaired result

repaired = meshfix.mesh
print(repaired)
PolyData (0x7fed9c7c9a80)
  N Cells:    5446
  N Points:   2725
  N Strips:   0
  X Bounds:   -3.696e+00, 5.998e+00
  Y Bounds:   -3.637e+00, 2.760e+00
  Z Bounds:   -1.701e+00, 1.701e+00
  N Arrays:   0
repaired.plot(cpos=cpos)
cow

Total running time of the script: (0 minutes 0.803 seconds)

Gallery generated by Sphinx-Gallery