forked from dave3d/dicom2stl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkutils.py
executable file
·374 lines (320 loc) · 9.67 KB
/
vtkutils.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#! /usr/bin/env vtkpython
#
# A collection of VTK functions for processing surfaces and volume.
#
# Written by David T. Chen from the National Library of Medicine, [email protected].
# It is covered by the Apache License, Version 2.0:
# http://www.apache.org/licenses/LICENSE-2.0
#
from __future__ import print_function
import sys
import time
import gc
import traceback
import vtk
#
# timing knick knacks
#
def roundThousand(x):
y = int(1000.0*x+0.5)
return str(float(y) * .001)
def elapsedTime(start_time):
dt = roundThousand(time.clock()-start_time)
print(" ", dt, "seconds")
#
# Isosurface extraction
#
def extractSurface(vol, isovalue=0.0):
try:
t = time.clock()
iso = vtk.vtkContourFilter()
if vtk.vtkVersion.GetVTKMajorVersion() >= 6:
iso.SetInputData(vol)
else:
iso.SetInput(vol)
iso.SetValue(0, isovalue)
iso.Update()
print("Surface extracted")
mesh = iso.GetOutput()
print(" ", mesh.GetNumberOfPolys(), "polygons")
elapsedTime(t)
iso = None
return mesh
except:
print("Iso-surface extraction failed")
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(
exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout)
return None
#
# Mesh filtering
#
def cleanMesh(mesh, connectivityFilter=False):
try:
t = time.clock()
connect = vtk.vtkPolyDataConnectivityFilter()
clean = vtk.vtkCleanPolyData()
if (connectivityFilter):
if vtk.vtkVersion.GetVTKMajorVersion() >= 6:
connect.SetInputData(mesh)
else:
connect.SetInput(mesh)
connect.SetExtractionModeToLargestRegion()
clean.SetInputConnection(connect.GetOutputPort())
else:
if vtk.vtkVersion.GetVTKMajorVersion() >= 6:
clean.SetInputData(mesh)
else:
clean.SetInput(mesh)
clean.Update()
print("Surface cleaned")
m2 = clean.GetOutput()
print(" ", m2.GetNumberOfPolys(), "polygons")
elapsedTime(t)
clean = None
connect = None
return m2
except:
print("Surface cleaning failed")
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(
exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout)
return None
def smoothMesh(mesh, nIterations=10):
try:
t = time.clock()
smooth = vtk.vtkWindowedSincPolyDataFilter()
smooth.SetNumberOfIterations(nIterations)
if vtk.vtkVersion.GetVTKMajorVersion() >= 6:
smooth.SetInputData(mesh)
else:
smooth.SetInput(mesh)
smooth.Update()
print("Surface smoothed")
m2 = smooth.GetOutput()
print(" ", m2.GetNumberOfPolys(), "polygons")
elapsedTime(t)
smooth = None
return m2
except:
print("Surface smoothing failed")
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(
exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout)
return None
def rotateMesh(mesh, axis=1, angle=0):
try:
print("Rotating surface: axis=", axis, "angle=", angle)
matrix = vtk.vtkTransform()
if axis == 0:
matrix.RotateX(angle)
if axis == 1:
matrix.RotateY(angle)
if axis == 2:
matrix.RotateZ(angle)
tfilter = vtk.vtkTransformPolyDataFilter()
tfilter.SetTransform(matrix)
if vtk.vtkVersion.GetVTKMajorVersion() >= 6:
tfilter.SetInputData(mesh)
else:
tfilter.SetInput(mesh)
tfilter.Update()
mesh2 = tfilter.GetOutput()
return mesh2
except:
print("Surface rotating failed")
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(
exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout)
return None
# @profile
def reduceMesh(mymesh, reductionFactor):
try:
t = time.clock()
deci = vtk.vtkQuadricDecimation()
deci.SetTargetReduction(reductionFactor)
if vtk.vtkVersion.GetVTKMajorVersion() >= 6:
deci.SetInputData(mymesh)
else:
deci.SetInput(mymesh)
deci.Update()
print("Surface reduced")
m2 = deci.GetOutput()
del deci
deci = None
print(" ", m2.GetNumberOfPolys(), "polygons")
elapsedTime(t)
return m2
except:
print("Surface reduction failed")
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(
exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout)
return None
#
# Mesh I/O
#
def readMesh(name):
if name.endswith(".vtk"):
return readVTKMesh(name)
if name.endswith(".ply"):
return readPLY(name)
if name.endswith(".stl"):
return readSTL(name)
print("Unknown file type: ", name)
return None
def readVTKMesh(name):
try:
reader = vtk.vtkPolyDataReader()
reader.SetFileName(name)
reader.Update()
print("Input mesh:", name)
mesh = reader.GetOutput()
del reader
reader = None
return mesh
except:
print("VTK mesh reader failed")
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(
exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout)
return None
def readSTL(name):
try:
reader = vtk.vtkSTLReader()
reader.SetFileName(name)
reader.Update()
print("Input mesh:", name)
mesh = reader.GetOutput()
del reader
reader = None
return mesh
except:
print("STL Mesh reader failed")
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(
exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout)
return None
def readPLY(name):
try:
reader = vtk.vtkPLYReader()
reader.SetFileName(name)
reader.Update()
print("Input mesh:", name)
mesh = reader.GetOutput()
del reader
reader = None
return mesh
except:
print("PLY Mesh reader failed")
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(
exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout)
return None
def writeMesh(mesh, name):
print("Writing", mesh.GetNumberOfPolys(), "polygons to", name)
if name.endswith(".vtk"):
writeVTKMesh(mesh, name)
return
if name.endswith(".ply"):
writePLY(mesh, name)
return
if name.endswith(".stl"):
writeSTL(mesh, name)
return
print("Unknown file type: ", name)
def writeVTKMesh(mesh, name):
try:
writer = vtk.vtkPolyDataWriter()
if vtk.vtkVersion.GetVTKMajorVersion() >= 6:
writer.SetInputData(mesh)
else:
writer.SetInput(mesh)
writer.SetFileTypeToBinary()
writer.SetFileName(name)
writer.Write()
print("Output mesh:", name)
writer = None
except:
print("VTK mesh writer failed")
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(
exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout)
return None
def writeSTL(mesh, name):
try:
writer = vtk.vtkSTLWriter()
if vtk.vtkVersion.GetVTKMajorVersion() >= 6:
print("writeSTL 1")
writer.SetInputData(mesh)
else:
print("writeSTL 2")
writer.SetInput(mesh)
writer.SetFileTypeToBinary()
writer.SetFileName(name)
writer.Write()
print("Output mesh:", name)
writer = None
except:
print("STL mesh writer failed")
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(
exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout)
return None
def writePLY(mesh, name):
try:
writer = vtk.vtkPLYWriter()
if vtk.vtkVersion.GetVTKMajorVersion() >= 6:
writer.SetInputData(mesh)
else:
writer.SetInput(mesh)
writer.SetFileTypeToBinary()
writer.SetFileName(name)
writer.Write()
print("Output mesh:", name)
writer = None
except:
print("PLY mesh writer failed")
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(
exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout)
return None
#
# Volume I/O
#
def readVTKVolume(name):
try:
reader = vtk.vtkStructuredPointsReader()
reader.SetFileName(name)
reader.Update()
print("Input volume:", name)
vol = reader.GetOutput()
reader = None
return vol
except:
print("VTK volume reader failed")
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(
exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout)
return None
# @profile
def memquery1():
print("Hiya 1")
# @profile
def memquery2():
print("Hiya 2")
# @profile
def memquery3():
print("Hiya 3")
#
# Main (test code)
#
if __name__ == "__main__":
print("vtkutils.py")
print("VTK version:", vtk.vtkVersion.GetVTKVersion())
print("VTK:", vtk)
mesh = readMesh("models/soft.stl")
# mesh = cleanMesh(mesh, False)
# mesh = smoothMesh(mesh)
mesh2 = reduceMesh(mesh, .50)
writeMesh(mesh2, "soft.ply")