polydata_utils.py
6.35 KB
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
#--------------------------------------------------------------------------
# Software: InVesalius - Software de Reconstrucao 3D de Imagens Medicas
# Copyright: (C) 2001 Centro de Pesquisas Renato Archer
# Homepage: http://www.softwarepublico.gov.br
# Contact: invesalius@cti.gov.br
# License: GNU - GPL 2 (LICENSE.txt/LICENCA.txt)
#--------------------------------------------------------------------------
# Este programa e software livre; voce pode redistribui-lo e/ou
# modifica-lo sob os termos da Licenca Publica Geral GNU, conforme
# publicada pela Free Software Foundation; de acordo com a versao 2
# da Licenca.
#
# Este programa eh distribuido na expectativa de ser util, mas SEM
# QUALQUER GARANTIA; sem mesmo a garantia implicita de
# COMERCIALIZACAO ou de ADEQUACAO A QUALQUER PROPOSITO EM
# PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais
# detalhes.
#--------------------------------------------------------------------------
import sys
import vtk
import wx
from wx.lib.pubsub import pub as Publisher
import vtk_utils as vu
# Update progress value in GUI
UpdateProgress = vu.ShowProgress()
def ApplyDecimationFilter(polydata, reduction_factor):
"""
Reduce number of triangles of the given vtkPolyData, based on
reduction_factor.
"""
# Important: vtkQuadricDecimation presented better results than
# vtkDecimatePro
decimation = vtk.vtkQuadricDecimation()
decimation.SetInputData(polydata)
decimation.SetTargetReduction(reduction_factor)
decimation.GetOutput().ReleaseDataFlagOn()
decimation.AddObserver("ProgressEvent", lambda obj, evt:
UpdateProgress(decimation, "Reducing number of triangles..."))
return decimation.GetOutput()
def ApplySmoothFilter(polydata, iterations, relaxation_factor):
"""
Smooth given vtkPolyData surface, based on iteration and relaxation_factor.
"""
smoother = vtk.vtkSmoothPolyDataFilter()
smoother.SetInputData(polydata)
smoother.SetNumberOfIterations(iterations)
smoother.SetFeatureAngle(80)
smoother.SetRelaxationFactor(relaxation_factor)
smoother.FeatureEdgeSmoothingOn()
smoother.BoundarySmoothingOn()
smoother.GetOutput().ReleaseDataFlagOn()
smoother.AddObserver("ProgressEvent", lambda obj, evt:
UpdateProgress(smoother, "Smoothing surface..."))
return smoother.GetOutput()
def FillSurfaceHole(polydata):
"""
Fill holes in the given polydata.
"""
# Filter used to detect and fill holes. Only fill
print "Filling polydata"
filled_polydata = vtk.vtkFillHolesFilter()
filled_polydata.SetInputData(polydata)
filled_polydata.SetHoleSize(500)
return filled_polydata.GetOutput()
def CalculateSurfaceVolume(polydata):
"""
Calculate the volume from the given polydata
"""
# Filter used to calculate volume and area from a polydata
measured_polydata = vtk.vtkMassProperties()
measured_polydata.SetInputData(polydata)
return measured_polydata.GetVolume()
def CalculateSurfaceArea(polydata):
"""
Calculate the volume from the given polydata
"""
# Filter used to calculate volume and area from a polydata
measured_polydata = vtk.vtkMassProperties()
measured_polydata.SetInputData(polydata)
return measured_polydata.GetSurfaceArea()
def Merge(polydata_list):
append = vtk.vtkAppendPolyData()
for polydata in polydata_list:
triangle = vtk.vtkTriangleFilter()
triangle.SetInputData(polydata)
append.AddInputData(triangle.GetOutput())
clean = vtk.vtkCleanPolyData()
clean.SetInputData(append.GetOutput())
return append.GetOutput()
def Export(polydata, filename, bin=False):
writer = vtk.vtkXMLPolyDataWriter()
print filename, type(filename)
writer.SetFileName(filename.encode('utf-8'))
if bin:
writer.SetDataModeToBinary()
else:
writer.SetDataModeToAscii()
writer.SetInputData(polydata)
writer.Write()
def Import(filename):
reader = vtk.vtkXMLPolyDataReader()
if isinstance(filename, unicode):
reader.SetFileName(filename.encode(wx.GetDefaultPyEncoding()))
else:
reader.SetFileName(filename)
reader.Update()
return reader.GetOutput()
def JoinSeedsParts(polydata, point_id_list):
"""
The function require vtkPolyData and point id
from vtkPolyData.
"""
conn = vtk.vtkPolyDataConnectivityFilter()
conn.SetInputData(polydata)
conn.SetExtractionModeToPointSeededRegions()
UpdateProgress = vu.ShowProgress(1 + len(point_id_list))
pos = 1
for seed in point_id_list:
conn.AddSeed(seed)
UpdateProgress(pos, _("Analysing selected regions..."))
pos += 1
conn.AddObserver("ProgressEvent", lambda obj, evt:
UpdateProgress(conn, "Getting selected parts"))
conn.Update()
result = vtk.vtkPolyData()
result.DeepCopy(conn.GetOutput())
return result
def SelectLargestPart(polydata):
"""
"""
UpdateProgress = vu.ShowProgress(1)
conn = vtk.vtkPolyDataConnectivityFilter()
conn.SetInputData(polydata)
conn.SetExtractionModeToLargestRegion()
conn.AddObserver("ProgressEvent", lambda obj, evt:
UpdateProgress(conn, "Getting largest part..."))
conn.Update()
result = vtk.vtkPolyData()
result.DeepCopy(conn.GetOutput())
return result
def SplitDisconectedParts(polydata):
"""
"""
conn = vtk.vtkPolyDataConnectivityFilter()
conn.SetInputData(polydata)
conn.SetExtractionModeToAllRegions()
conn.Update()
nregions = conn.GetNumberOfExtractedRegions()
conn.SetExtractionModeToSpecifiedRegions()
conn.Update()
polydata_collection = []
# Update progress value in GUI
progress = nregions -1
if progress:
UpdateProgress = vu.ShowProgress(progress)
for region in xrange(nregions):
conn.InitializeSpecifiedRegionList()
conn.AddSpecifiedRegion(region)
conn.Update()
p = vtk.vtkPolyData()
p.DeepCopy(conn.GetOutput())
polydata_collection.append(p)
if progress:
UpdateProgress(region, _("Splitting disconnected regions..."))
return polydata_collection