Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import pytest
import pyvista as pv
from pymeshfix import _meshfix
from pymeshfix import examples
bunny = pv.PolyData(examples.bunny_scan)
def test_load_and_save_file(tmpdir):
meshfix = _meshfix.PyTMesh()
meshfix.load_file(examples.bunny_scan)
with pytest.raises(Exception):
meshfix.load_file(examples.bunny_scan)
v, f = meshfix.return_arrays()
assert f.shape[0] == bunny.n_faces
# test saving
filename = str(tmpdir.mkdir("tmpdir").join('tmp.ply'))
meshfix.save_file(filename)
new_bunny = pv.PolyData(filename)
def test_repair_vtk():
meshin = pv.PolyData(bunny_scan)
meshfix = pymeshfix.MeshFix(meshin)
meshfix.repair()
# check arrays and output mesh
assert np.any(meshfix.v)
assert np.any(meshfix.f)
meshout = meshfix.mesh
assert meshfix.mesh.n_points
# test for any holes
pdata = meshout.extract_edges(non_manifold_edges=False, feature_edges=False,
manifold_edges=False)
assert pdata.n_points == 0
def test_fill_small_boundaries():
meshfix = _meshfix.PyTMesh()
meshfix.load_file(examples.bunny_scan)
ninit_boundaries = meshfix.boundaries()
meshfix.fill_small_boundaries(refine=False)
assert meshfix.boundaries() < ninit_boundaries
########################################### imports
from vtkplotter import Plotter, boundaries
try:
# Credits:
# https://github.com/MarcoAttene/MeshFix-V2.1
# https://github.com/akaszynski/pymeshfix
from pymeshfix._meshfix import PyTMesh, Repair
from pymeshfix.examples import bunny_scan
except:
print('Install pymeshfix with: pip install pymeshfix')
exit()
########################################### PyTMesh repair
inputmesh = bunny_scan # 'pymeshfix/examples/StanfordBunny.ply'
ouputmesh = 'meshfix_repaired.ply' # try e.g. vtkconvert -to vtk repaired.ply
tm = PyTMesh()
tm.LoadFile(inputmesh)
Repair(tm, verbose=True, joincomp=True, removeSmallestComponents=True)
tm.SaveFile(ouputmesh)
########################################### vtkplotter
vp = Plotter(shape=(2,1))
act_original = vp.load(inputmesh)
bo = boundaries(act_original)
act_repaired = vp.load(ouputmesh)
br = boundaries(act_repaired)
def with_vtk(plot=True):
""" Tests VTK interface and mesh repair of Stanford Bunny Mesh """
mesh = pv.PolyData(bunny_scan)
meshfix = pymeshfix.MeshFix(mesh)
if plot:
print('Plotting input mesh')
meshfix.plot()
meshfix.repair()
if plot:
print('Plotting repaired mesh')
meshfix.plot()
return meshfix.mesh
if __name__ == '__main__':
""" Functional Test: vtk and native """
t_start = time.time()
out_file = 'repaired.ply'
native()
outmesh = pv.PolyData(out_file)
os.remove(out_file)
assert outmesh.n_points
# test for any holes
pdata = outmesh.extract_edges(non_manifold_edges=False, feature_edges=False,
manifold_edges=False)
assert pdata.n_points == 0
# test vtk
meshin = pv.PolyData(bunny_scan)
meshfix = pymeshfix.MeshFix(meshin)
meshfix.repair()
# check arrays and output mesh
assert np.any(meshfix.v)
assert np.any(meshfix.f)
meshout = meshfix.mesh
assert meshfix.mesh.n_points
# test for any holes
pdata = meshout.extract_edges(non_manifold_edges=False, feature_edges=False,
manifold_edges=False)
print('PASS in %f seconds' % (time.time() - t_start))