From 8a4cf33657f213a7836d52fd3569daf38a955541 Mon Sep 17 00:00:00 2001 From: tfmoraes Date: Thu, 13 Sep 2012 14:19:40 +0000 Subject: [PATCH] FIX: Resolved the problem with non-ascii characters in mask and surface name by not appending its name on the file used to mantain them --- .gitattributes | 1 + invesalius/data/mask.py | 2 +- invesalius/data/polydata_utils.py | 397 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- invesalius/data/surface.py | 2 +- invesalius/project.py | 27 +++++++++++++++++++++++++-- 5 files changed, 228 insertions(+), 201 deletions(-) diff --git a/.gitattributes b/.gitattributes index e107c7c..18549ff 100644 --- a/.gitattributes +++ b/.gitattributes @@ -845,6 +845,7 @@ invesalius/.svnignore -text invesalius/data/bases.py -text invesalius/data/co_registration.py -text invesalius/data/converters.py -text +invesalius/data/polydata_utils.py -text svneol=native#unset invesalius/gui/import_network_panel.py -text invesalius/gui/preferences.py -text invesalius/net/__init__.py -text diff --git a/invesalius/data/mask.py b/invesalius/data/mask.py index 40c691b..18254b7 100644 --- a/invesalius/data/mask.py +++ b/invesalius/data/mask.py @@ -53,7 +53,7 @@ class Mask(): def SavePlist(self, dir_temp): mask = {} - filename = u'mask%d_%s' % (self.index, self.name) + filename = u'mask_%d' % self.index mask_filename = u'%s.dat' % filename mask_filepath = os.path.join(dir_temp, mask_filename) self._save_mask(mask_filepath) diff --git a/invesalius/data/polydata_utils.py b/invesalius/data/polydata_utils.py index 0dcb7a6..9732580 100644 --- a/invesalius/data/polydata_utils.py +++ b/invesalius/data/polydata_utils.py @@ -1,197 +1,200 @@ -#-------------------------------------------------------------------------- -# 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.SetInput(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.SetInput(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.SetInput(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.SetInput(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.SetInput(polydata) - return measured_polydata.GetSurfaceArea() - -def Merge(polydata_list): - append = vtk.vtkAppendPolyData() - - for polydata in polydata_list: - triangle = vtk.vtkTriangleFilter() - triangle.SetInput(polydata) - append.AddInput(triangle.GetOutput()) - - clean = vtk.vtkCleanPolyData() - clean.SetInput(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.SetInput(polydata) - writer.Write() - -def Import(filename): - reader = vtk.vtkXMLPolyDataReader() - reader.SetFileName(filename.encode('utf-8')) - reader.Update() - return reader.GetOutput() - -def JoinSeedsParts(polydata, point_id_list): - """ - The function require vtkPolyData and point id - from vtkPolyData. - """ - conn = vtk.vtkPolyDataConnectivityFilter() - conn.SetInput(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()) - result.Update() - return result - -def SelectLargestPart(polydata): - """ - """ - UpdateProgress = vu.ShowProgress(1) - conn = vtk.vtkPolyDataConnectivityFilter() - conn.SetInput(polydata) - conn.SetExtractionModeToLargestRegion() - conn.AddObserver("ProgressEvent", lambda obj, evt: - UpdateProgress(conn, "Getting largest part...")) - conn.Update() - - result = vtk.vtkPolyData() - result.DeepCopy(conn.GetOutput()) - result.Update() - return result - -def SplitDisconectedParts(polydata): - """ - """ - conn = vtk.vtkPolyDataConnectivityFilter() - conn.SetInput(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()) - p.Update() - - polydata_collection.append(p) - if progress: - UpdateProgress(region, _("Splitting disconnected regions...")) - - return polydata_collection +#-------------------------------------------------------------------------- +# 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.SetInput(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.SetInput(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.SetInput(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.SetInput(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.SetInput(polydata) + return measured_polydata.GetSurfaceArea() + +def Merge(polydata_list): + append = vtk.vtkAppendPolyData() + + for polydata in polydata_list: + triangle = vtk.vtkTriangleFilter() + triangle.SetInput(polydata) + append.AddInput(triangle.GetOutput()) + + clean = vtk.vtkCleanPolyData() + clean.SetInput(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.SetInput(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.SetInput(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()) + result.Update() + return result + +def SelectLargestPart(polydata): + """ + """ + UpdateProgress = vu.ShowProgress(1) + conn = vtk.vtkPolyDataConnectivityFilter() + conn.SetInput(polydata) + conn.SetExtractionModeToLargestRegion() + conn.AddObserver("ProgressEvent", lambda obj, evt: + UpdateProgress(conn, "Getting largest part...")) + conn.Update() + + result = vtk.vtkPolyData() + result.DeepCopy(conn.GetOutput()) + result.Update() + return result + +def SplitDisconectedParts(polydata): + """ + """ + conn = vtk.vtkPolyDataConnectivityFilter() + conn.SetInput(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()) + p.Update() + + polydata_collection.append(p) + if progress: + UpdateProgress(region, _("Splitting disconnected regions...")) + + return polydata_collection diff --git a/invesalius/data/surface.py b/invesalius/data/surface.py index 97380a5..5eb7310 100644 --- a/invesalius/data/surface.py +++ b/invesalius/data/surface.py @@ -63,7 +63,7 @@ class Surface(): self.name = name def SavePlist(self, dir_temp): - filename = 'surface_%d_%s' % (self.index, self.name) + filename = 'surface_%d' % self.index vtp_filename = filename + '.vtp' vtp_filepath = os.path.join(dir_temp, vtp_filename) pu.Export(self.polydata, vtp_filepath, bin=True) diff --git a/invesalius/project.py b/invesalius/project.py index fd16ec2..9192ae3 100755 --- a/invesalius/project.py +++ b/invesalius/project.py @@ -274,7 +274,7 @@ class Project(object): ow.SetInstance(fow) filelist = Extract(filename, tempfile.mkdtemp()) - dirpath = os.path.abspath(os.path.split(filelist[0])[0]).decode(wx.GetDefaultPyEncoding()) + dirpath = os.path.abspath(os.path.split(filelist[0])[0]) # Opening the main file from invesalius 3 project main_plist = os.path.join(dirpath ,'main.plist') @@ -335,9 +335,32 @@ def Compress(folder, filename): tar.close() shutil.move(tmpdir_+ ".inv3", filename) os.chdir(current_dir) - + def Extract(filename, folder): tar = tarfile.open(filename, "r:gz") + idir = os.path.split(tar.getnames()[0])[0] + os.mkdir(os.path.join(folder, idir.decode('utf8'))) + filelist = [] + for t in tar.getmembers(): + fsrc = tar.extractfile(t) + + fname = os.path.join(folder, t.name.decode('utf-8')) + fdst = file(fname, 'wb') + + shutil.copyfileobj(fsrc, fdst) + + filelist.append(fname) + fsrc.close() + fdst.close() + del fsrc + del fdst + tar.close() + print filelist + return filelist + + +def Extract_(filename, folder): + tar = tarfile.open(filename, "r:gz") #tar.list(verbose=True) tar.extractall(folder) filelist = [os.path.join(folder, i) for i in tar.getnames()] -- libgit2 0.21.2