gmsh round-trip

gmsh round-trip#

Push a pyvista.PolyData surface into a gmsh model, then pull the result back as a pyvista.UnstructuredGrid. Use this when you need gmsh’s remesher, e.g. to clean up a noisy scan or to prepare a surface for a downstream FEA tet-mesher.

This example takes a real 3D scan, the T-LESS object-5 reconstruction from Primesense RGB-D scans (CC-BY 4.0, Hodaň et al., WACV 2017), and runs it through gmsh: cleaning a noisy scan is exactly the use case this bridge exists for.

import gmsh
import pyvista as pv

import pyvista_cad
from pyvista_cad.examples import downloads

Load the real scan and decimate it. gmsh’s discrete remesher chokes on a dense scan, and thinning it first is the normal prep.

_, scan_path = downloads.scan_pair_paths()
surface = pv.read(scan_path).extract_surface().triangulate().decimate(0.9)
surface
/home/runner/work/pyvista-cad/pyvista-cad/examples/03_bridges/via_gmsh.py:29: PyVistaFutureWarning: The default value of `algorithm` for the filter
`PolyData.extract_surface` will change in the future. It currently defaults to
`'dataset_surface'`, but will change to `None`. Explicitly set the `algorithm` keyword to
silence this warning.
  surface = pv.read(scan_path).extract_surface().triangulate().decimate(0.9)
PolyData (0x7f6fab204e20)
  N Cells:    4290
  N Points:   2308
  N Strips:   0
  X Bounds:   -4.849e+01, 4.818e+01
  Y Bounds:   -2.843e+01, 2.760e+01
  Z Bounds:   -3.066e+01, 2.920e+01
  N Arrays:   0


Push into gmsh and pull back.

gmsh.initialize()
try:
    pyvista_cad.to_gmsh(surface, model_name='scan_demo')
    grid = pyvista_cad.from_gmsh()
finally:
    gmsh.finalize()

grid
UnstructuredGrid (0x7f6faa75e6e0)
  N Cells:    4290
  N Points:   2308
  X Bounds:   -4.849e+01, 4.818e+01
  Y Bounds:   -2.843e+01, 2.760e+01
  Z Bounds:   -3.066e+01, 2.920e+01
  N Arrays:   1


Render the gmsh-remeshed surface.

grid['Z'] = grid.points[:, 2]

pl = pv.Plotter()
pl.add_mesh(grid, scalars='Z', cmap='inferno', show_edges=True, edge_color='black')
pl.show()
via gmsh

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