Holey CowΒΆ

Repair a holey cow

# sphinx_gallery_thumbnail_number = 1
import pyvista as pv
import pymeshfix as mf
from pyvista import examples
import numpy as np
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().tri_filter()
print(holy_cow)

Out:

WARNING:root:DEPRECATED: ``.tri_filter`` is deprecated. Use ``.triangulate`` instead.
PolyData (0x7fcc7811b348)
  N Cells:      5213
  N Points:     2903
  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()
../_images/sphx_glr_cow_001.png

Repair the holey cow

meshfix.repair(verbose=True)

Out:

Removed 1 small components
Patching holes...
Patched 205 holes
Fixing degeneracies and intersections

Show the repaired result

repaired = meshfix.mesh
print(repaired)

Out:

PolyData (0x7fcc7882e5e8)
  N Cells:      5434
  N Points:     2719
  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)
../_images/sphx_glr_cow_002.png

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

Gallery generated by Sphinx-Gallery