Commit 011095a80c04584e83c8a6de3e57f6f2460ded59

Authored by Thiago Franco de Moraes
1 parent 89f4d440

Converting polydata_utils to unix format

Showing 1 changed file with 200 additions and 200 deletions   Show diff stats
invesalius/data/polydata_utils.py
1   -#--------------------------------------------------------------------------
2   -# Software: InVesalius - Software de Reconstrucao 3D de Imagens Medicas
3   -# Copyright: (C) 2001 Centro de Pesquisas Renato Archer
4   -# Homepage: http://www.softwarepublico.gov.br
5   -# Contact: invesalius@cti.gov.br
6   -# License: GNU - GPL 2 (LICENSE.txt/LICENCA.txt)
7   -#--------------------------------------------------------------------------
8   -# Este programa e software livre; voce pode redistribui-lo e/ou
9   -# modifica-lo sob os termos da Licenca Publica Geral GNU, conforme
10   -# publicada pela Free Software Foundation; de acordo com a versao 2
11   -# da Licenca.
12   -#
13   -# Este programa eh distribuido na expectativa de ser util, mas SEM
14   -# QUALQUER GARANTIA; sem mesmo a garantia implicita de
15   -# COMERCIALIZACAO ou de ADEQUACAO A QUALQUER PROPOSITO EM
16   -# PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais
17   -# detalhes.
18   -#--------------------------------------------------------------------------
19   -
20   -import sys
21   -
22   -import vtk
23   -import wx
24   -from wx.lib.pubsub import pub as Publisher
25   -
26   -import vtk_utils as vu
27   -
28   -# Update progress value in GUI
29   -UpdateProgress = vu.ShowProgress()
30   -
31   -def ApplyDecimationFilter(polydata, reduction_factor):
32   - """
33   - Reduce number of triangles of the given vtkPolyData, based on
34   - reduction_factor.
35   - """
36   - # Important: vtkQuadricDecimation presented better results than
37   - # vtkDecimatePro
38   - decimation = vtk.vtkQuadricDecimation()
39   - decimation.SetInputData(polydata)
40   - decimation.SetTargetReduction(reduction_factor)
41   - decimation.GetOutput().ReleaseDataFlagOn()
42   - decimation.AddObserver("ProgressEvent", lambda obj, evt:
43   - UpdateProgress(decimation, "Reducing number of triangles..."))
44   - return decimation.GetOutput()
45   -
46   -def ApplySmoothFilter(polydata, iterations, relaxation_factor):
47   - """
48   - Smooth given vtkPolyData surface, based on iteration and relaxation_factor.
49   - """
50   - smoother = vtk.vtkSmoothPolyDataFilter()
51   - smoother.SetInputData(polydata)
52   - smoother.SetNumberOfIterations(iterations)
53   - smoother.SetFeatureAngle(80)
54   - smoother.SetRelaxationFactor(relaxation_factor)
55   - smoother.FeatureEdgeSmoothingOn()
56   - smoother.BoundarySmoothingOn()
57   - smoother.GetOutput().ReleaseDataFlagOn()
58   - smoother.AddObserver("ProgressEvent", lambda obj, evt:
59   - UpdateProgress(smoother, "Smoothing surface..."))
60   -
61   - return smoother.GetOutput()
62   -
63   -
64   -
65   -def FillSurfaceHole(polydata):
66   - """
67   - Fill holes in the given polydata.
68   - """
69   - # Filter used to detect and fill holes. Only fill
70   - print "Filling polydata"
71   - filled_polydata = vtk.vtkFillHolesFilter()
72   - filled_polydata.SetInputData(polydata)
73   - filled_polydata.SetHoleSize(500)
74   - return filled_polydata.GetOutput()
75   -
76   -def CalculateSurfaceVolume(polydata):
77   - """
78   - Calculate the volume from the given polydata
79   - """
80   - # Filter used to calculate volume and area from a polydata
81   - measured_polydata = vtk.vtkMassProperties()
82   - measured_polydata.SetInputData(polydata)
83   - return measured_polydata.GetVolume()
84   -
85   -def CalculateSurfaceArea(polydata):
86   - """
87   - Calculate the volume from the given polydata
88   - """
89   - # Filter used to calculate volume and area from a polydata
90   - measured_polydata = vtk.vtkMassProperties()
91   - measured_polydata.SetInputData(polydata)
92   - return measured_polydata.GetSurfaceArea()
93   -
94   -def Merge(polydata_list):
95   - append = vtk.vtkAppendPolyData()
96   -
97   - for polydata in polydata_list:
98   - triangle = vtk.vtkTriangleFilter()
99   - triangle.SetInputData(polydata)
100   - triangle.Update()
101   - append.AddInputData(triangle.GetOutput())
102   -
103   - append.Update()
104   - clean = vtk.vtkCleanPolyData()
105   - clean.SetInputData(append.GetOutput())
106   - clean.Update()
107   -
108   - return append.GetOutput()
109   -
110   -def Export(polydata, filename, bin=False):
111   - writer = vtk.vtkXMLPolyDataWriter()
112   - print filename, type(filename)
113   - writer.SetFileName(filename.encode('utf-8'))
114   - if bin:
115   - writer.SetDataModeToBinary()
116   - else:
117   - writer.SetDataModeToAscii()
118   - writer.SetInputData(polydata)
119   - writer.Write()
120   -
121   -def Import(filename):
122   - reader = vtk.vtkXMLPolyDataReader()
123   - if isinstance(filename, unicode):
124   - reader.SetFileName(filename.encode(wx.GetDefaultPyEncoding()))
125   - else:
126   - reader.SetFileName(filename)
127   - reader.Update()
128   - return reader.GetOutput()
129   -
130   -def JoinSeedsParts(polydata, point_id_list):
131   - """
132   - The function require vtkPolyData and point id
133   - from vtkPolyData.
134   - """
135   - conn = vtk.vtkPolyDataConnectivityFilter()
136   - conn.SetInputData(polydata)
137   - conn.SetExtractionModeToPointSeededRegions()
138   - UpdateProgress = vu.ShowProgress(1 + len(point_id_list))
139   - pos = 1
140   - for seed in point_id_list:
141   - conn.AddSeed(seed)
142   - UpdateProgress(pos, _("Analysing selected regions..."))
143   - pos += 1
144   -
145   - conn.AddObserver("ProgressEvent", lambda obj, evt:
146   - UpdateProgress(conn, "Getting selected parts"))
147   - conn.Update()
148   -
149   - result = vtk.vtkPolyData()
150   - result.DeepCopy(conn.GetOutput())
151   - return result
152   -
153   -def SelectLargestPart(polydata):
154   - """
155   - """
156   - UpdateProgress = vu.ShowProgress(1)
157   - conn = vtk.vtkPolyDataConnectivityFilter()
158   - conn.SetInputData(polydata)
159   - conn.SetExtractionModeToLargestRegion()
160   - conn.AddObserver("ProgressEvent", lambda obj, evt:
161   - UpdateProgress(conn, "Getting largest part..."))
162   - conn.Update()
163   -
164   - result = vtk.vtkPolyData()
165   - result.DeepCopy(conn.GetOutput())
166   - return result
167   -
168   -def SplitDisconectedParts(polydata):
169   - """
170   - """
171   - conn = vtk.vtkPolyDataConnectivityFilter()
172   - conn.SetInputData(polydata)
173   - conn.SetExtractionModeToAllRegions()
174   - conn.Update()
175   -
176   - nregions = conn.GetNumberOfExtractedRegions()
177   -
178   - conn.SetExtractionModeToSpecifiedRegions()
179   - conn.Update()
180   -
181   - polydata_collection = []
182   -
183   - # Update progress value in GUI
184   - progress = nregions -1
185   - if progress:
186   - UpdateProgress = vu.ShowProgress(progress)
187   -
188   - for region in xrange(nregions):
189   - conn.InitializeSpecifiedRegionList()
190   - conn.AddSpecifiedRegion(region)
191   - conn.Update()
192   -
193   - p = vtk.vtkPolyData()
194   - p.DeepCopy(conn.GetOutput())
195   -
196   - polydata_collection.append(p)
197   - if progress:
198   - UpdateProgress(region, _("Splitting disconnected regions..."))
199   -
200   - return polydata_collection
  1 +#--------------------------------------------------------------------------
  2 +# Software: InVesalius - Software de Reconstrucao 3D de Imagens Medicas
  3 +# Copyright: (C) 2001 Centro de Pesquisas Renato Archer
  4 +# Homepage: http://www.softwarepublico.gov.br
  5 +# Contact: invesalius@cti.gov.br
  6 +# License: GNU - GPL 2 (LICENSE.txt/LICENCA.txt)
  7 +#--------------------------------------------------------------------------
  8 +# Este programa e software livre; voce pode redistribui-lo e/ou
  9 +# modifica-lo sob os termos da Licenca Publica Geral GNU, conforme
  10 +# publicada pela Free Software Foundation; de acordo com a versao 2
  11 +# da Licenca.
  12 +#
  13 +# Este programa eh distribuido na expectativa de ser util, mas SEM
  14 +# QUALQUER GARANTIA; sem mesmo a garantia implicita de
  15 +# COMERCIALIZACAO ou de ADEQUACAO A QUALQUER PROPOSITO EM
  16 +# PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais
  17 +# detalhes.
  18 +#--------------------------------------------------------------------------
  19 +
  20 +import sys
  21 +
  22 +import vtk
  23 +import wx
  24 +from wx.lib.pubsub import pub as Publisher
  25 +
  26 +import vtk_utils as vu
  27 +
  28 +# Update progress value in GUI
  29 +UpdateProgress = vu.ShowProgress()
  30 +
  31 +def ApplyDecimationFilter(polydata, reduction_factor):
  32 + """
  33 + Reduce number of triangles of the given vtkPolyData, based on
  34 + reduction_factor.
  35 + """
  36 + # Important: vtkQuadricDecimation presented better results than
  37 + # vtkDecimatePro
  38 + decimation = vtk.vtkQuadricDecimation()
  39 + decimation.SetInputData(polydata)
  40 + decimation.SetTargetReduction(reduction_factor)
  41 + decimation.GetOutput().ReleaseDataFlagOn()
  42 + decimation.AddObserver("ProgressEvent", lambda obj, evt:
  43 + UpdateProgress(decimation, "Reducing number of triangles..."))
  44 + return decimation.GetOutput()
  45 +
  46 +def ApplySmoothFilter(polydata, iterations, relaxation_factor):
  47 + """
  48 + Smooth given vtkPolyData surface, based on iteration and relaxation_factor.
  49 + """
  50 + smoother = vtk.vtkSmoothPolyDataFilter()
  51 + smoother.SetInputData(polydata)
  52 + smoother.SetNumberOfIterations(iterations)
  53 + smoother.SetFeatureAngle(80)
  54 + smoother.SetRelaxationFactor(relaxation_factor)
  55 + smoother.FeatureEdgeSmoothingOn()
  56 + smoother.BoundarySmoothingOn()
  57 + smoother.GetOutput().ReleaseDataFlagOn()
  58 + smoother.AddObserver("ProgressEvent", lambda obj, evt:
  59 + UpdateProgress(smoother, "Smoothing surface..."))
  60 +
  61 + return smoother.GetOutput()
  62 +
  63 +
  64 +
  65 +def FillSurfaceHole(polydata):
  66 + """
  67 + Fill holes in the given polydata.
  68 + """
  69 + # Filter used to detect and fill holes. Only fill
  70 + print "Filling polydata"
  71 + filled_polydata = vtk.vtkFillHolesFilter()
  72 + filled_polydata.SetInputData(polydata)
  73 + filled_polydata.SetHoleSize(500)
  74 + return filled_polydata.GetOutput()
  75 +
  76 +def CalculateSurfaceVolume(polydata):
  77 + """
  78 + Calculate the volume from the given polydata
  79 + """
  80 + # Filter used to calculate volume and area from a polydata
  81 + measured_polydata = vtk.vtkMassProperties()
  82 + measured_polydata.SetInputData(polydata)
  83 + return measured_polydata.GetVolume()
  84 +
  85 +def CalculateSurfaceArea(polydata):
  86 + """
  87 + Calculate the volume from the given polydata
  88 + """
  89 + # Filter used to calculate volume and area from a polydata
  90 + measured_polydata = vtk.vtkMassProperties()
  91 + measured_polydata.SetInputData(polydata)
  92 + return measured_polydata.GetSurfaceArea()
  93 +
  94 +def Merge(polydata_list):
  95 + append = vtk.vtkAppendPolyData()
  96 +
  97 + for polydata in polydata_list:
  98 + triangle = vtk.vtkTriangleFilter()
  99 + triangle.SetInputData(polydata)
  100 + triangle.Update()
  101 + append.AddInputData(triangle.GetOutput())
  102 +
  103 + append.Update()
  104 + clean = vtk.vtkCleanPolyData()
  105 + clean.SetInputData(append.GetOutput())
  106 + clean.Update()
  107 +
  108 + return append.GetOutput()
  109 +
  110 +def Export(polydata, filename, bin=False):
  111 + writer = vtk.vtkXMLPolyDataWriter()
  112 + print filename, type(filename)
  113 + writer.SetFileName(filename.encode('utf-8'))
  114 + if bin:
  115 + writer.SetDataModeToBinary()
  116 + else:
  117 + writer.SetDataModeToAscii()
  118 + writer.SetInputData(polydata)
  119 + writer.Write()
  120 +
  121 +def Import(filename):
  122 + reader = vtk.vtkXMLPolyDataReader()
  123 + if isinstance(filename, unicode):
  124 + reader.SetFileName(filename.encode(wx.GetDefaultPyEncoding()))
  125 + else:
  126 + reader.SetFileName(filename)
  127 + reader.Update()
  128 + return reader.GetOutput()
  129 +
  130 +def JoinSeedsParts(polydata, point_id_list):
  131 + """
  132 + The function require vtkPolyData and point id
  133 + from vtkPolyData.
  134 + """
  135 + conn = vtk.vtkPolyDataConnectivityFilter()
  136 + conn.SetInputData(polydata)
  137 + conn.SetExtractionModeToPointSeededRegions()
  138 + UpdateProgress = vu.ShowProgress(1 + len(point_id_list))
  139 + pos = 1
  140 + for seed in point_id_list:
  141 + conn.AddSeed(seed)
  142 + UpdateProgress(pos, _("Analysing selected regions..."))
  143 + pos += 1
  144 +
  145 + conn.AddObserver("ProgressEvent", lambda obj, evt:
  146 + UpdateProgress(conn, "Getting selected parts"))
  147 + conn.Update()
  148 +
  149 + result = vtk.vtkPolyData()
  150 + result.DeepCopy(conn.GetOutput())
  151 + return result
  152 +
  153 +def SelectLargestPart(polydata):
  154 + """
  155 + """
  156 + UpdateProgress = vu.ShowProgress(1)
  157 + conn = vtk.vtkPolyDataConnectivityFilter()
  158 + conn.SetInputData(polydata)
  159 + conn.SetExtractionModeToLargestRegion()
  160 + conn.AddObserver("ProgressEvent", lambda obj, evt:
  161 + UpdateProgress(conn, "Getting largest part..."))
  162 + conn.Update()
  163 +
  164 + result = vtk.vtkPolyData()
  165 + result.DeepCopy(conn.GetOutput())
  166 + return result
  167 +
  168 +def SplitDisconectedParts(polydata):
  169 + """
  170 + """
  171 + conn = vtk.vtkPolyDataConnectivityFilter()
  172 + conn.SetInputData(polydata)
  173 + conn.SetExtractionModeToAllRegions()
  174 + conn.Update()
  175 +
  176 + nregions = conn.GetNumberOfExtractedRegions()
  177 +
  178 + conn.SetExtractionModeToSpecifiedRegions()
  179 + conn.Update()
  180 +
  181 + polydata_collection = []
  182 +
  183 + # Update progress value in GUI
  184 + progress = nregions -1
  185 + if progress:
  186 + UpdateProgress = vu.ShowProgress(progress)
  187 +
  188 + for region in xrange(nregions):
  189 + conn.InitializeSpecifiedRegionList()
  190 + conn.AddSpecifiedRegion(region)
  191 + conn.Update()
  192 +
  193 + p = vtk.vtkPolyData()
  194 + p.DeepCopy(conn.GetOutput())
  195 +
  196 + polydata_collection.append(p)
  197 + if progress:
  198 + UpdateProgress(region, _("Splitting disconnected regions..."))
  199 +
  200 + return polydata_collection
... ...