-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsurface_test.py
96 lines (59 loc) · 2.02 KB
/
surface_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import pyvista as pv
from vtkmodules.vtkCommonDataModel import vtkIterativeClosestPointTransform
import vtk
from vmtk import pypes
FILENAME = "./data/topcow_mr_001_0000.nii.gz"
REF_FILENAME = "./data/topcow_mr_001.nii.gz"
# # Read reference mesh
reader = pv.get_reader(REF_FILENAME)
mesh = reader.read()
ref_mesh = mesh.threshold(1)
if ref_mesh is None:
exit("Could not threshold ref mesh")
# # Read target mesh
reader = pv.get_reader(FILENAME)
tgt_mesh = reader.read()
tgt_mesh = tgt_mesh.threshold(-2.1e4)
if tgt_mesh is None:
exit("Could not threshold target mesh")
tgt_mesh = tgt_mesh.clip_box(ref_mesh.bounds, invert=False)
if tgt_mesh is None:
exit("Could not clip target mesh")
tgt_mesh = tgt_mesh.connectivity('largest')
if tgt_mesh is None:
exit("Could not get connectivity of target mesh")
geo_filter = vtk.vtkGeometryFilter()
geo_filter.SetInputData(tgt_mesh)
geo_filter.Update()
poly_data = geo_filter.GetOutput()
smooth_filter = vtk.vtkSmoothPolyDataFilter()
smooth_filter.SetInputData(poly_data)
smooth_filter.SetNumberOfIterations(30)
smooth_filter.SetRelaxationFactor(0.1)
smooth_filter.FeatureEdgeSmoothingOff()
smooth_filter.BoundarySmoothingOn()
smooth_filter.Update()
smooth_data = smooth_filter.GetOutput()
tgt_mesh = pv.wrap(smooth_data)
if tgt_mesh is None:
exit("could not wrap target mesh")
tgt_mesh = tgt_mesh.clean()
writer = vtk.vtkXMLPolyDataWriter()
writer.SetFileName("artery.vtp")
writer.SetInputData(tgt_mesh)
writer.Write()
print("wrote")
my_pype = pypes.PypeRun("vmtknetworkextraction -ifile artery.vtp -advancementratio 1 -ofile new_artery.vtp")
# surface = my_pype.GetScriptObject('vmtknetworkextraction', '0').Surface
# print("ran vmtk network extraction")
# reader = vtk.vtkXMLPolyDataReader()
# reader.SetFileName("new_artery.vtp")
# reader.Update()
# data = reader.GetOutput()
mesh = pv.read("new_artery.vtp")
print(f"points: {mesh.n_points}")
print(f"polys: {mesh.n_cells}")
plotter = pv.Plotter()
plotter.add_mesh(tgt_mesh, color='gray')
plotter.show()
# print(data)