From 160fcc1f698b3818c4fa794007e190783f6337a9 Mon Sep 17 00:00:00 2001 From: tatiana Date: Fri, 2 Oct 2009 12:57:16 +0000 Subject: [PATCH] ADD: Export surface in several formats and vtkImageData --- invesalius/constants.py | 4 ++++ invesalius/data/slice_.py | 14 ++++++++++++++ invesalius/data/surface.py | 26 ++++++++++++++++++++++---- invesalius/gui/task_exporter.py | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------- 4 files changed, 115 insertions(+), 17 deletions(-) diff --git a/invesalius/constants.py b/invesalius/constants.py index 0559f75..30d80a3 100644 --- a/invesalius/constants.py +++ b/invesalius/constants.py @@ -218,6 +218,10 @@ FILETYPE_RIB = wx.NewId() FILETYPE_STL = wx.NewId() FILETYPE_VRML = wx.NewId() FILETYPE_OBJ = wx.NewId() +FILETYPE_VTP = wx.NewId() +FILETYPE_PLY = wx.NewId() + +FILETYPE_IMAGEDATA = wx.NewId() FILETYPE_BMP = wx.NewId() FILETYPE_JPG = wx.NewId() diff --git a/invesalius/data/slice_.py b/invesalius/data/slice_.py index c356ef1..1f91870 100644 --- a/invesalius/data/slice_.py +++ b/invesalius/data/slice_.py @@ -4,6 +4,7 @@ import vtk import wx.lib.pubsub as ps import constants as const +import imagedata_utils as iu from mask import Mask from project import Project from utils import Singleton @@ -60,6 +61,7 @@ class Slice(object): 'Change colour table from background image') ps.Publisher().subscribe(self.InputImageWidget, 'Input Image in the widget') + ps.Publisher().subscribe(self.OnExportMask,'Export mask to file') def __set_current_mask_threshold_limits(self, pubsub_evt): thresh_min = pubsub_evt.data[0] @@ -544,3 +546,15 @@ class Slice(object): imagedata_mask.Update() return imagedata_mask + + + def OnExportMask(self, pubsub_evt): + #imagedata = self.current_mask.imagedata + imagedata = self.imagedata + filename, filetype = pubsub_evt.data + if (filetype == const.FILETYPE_IMAGEDATA): + iu.Export(imagedata, filename) + + + + diff --git a/invesalius/data/surface.py b/invesalius/data/surface.py index e5de68e..f6a9286 100644 --- a/invesalius/data/surface.py +++ b/invesalius/data/surface.py @@ -284,7 +284,11 @@ class SurfaceManager(): def OnExportSurface(self, pubsub_evt): filename, filetype = pubsub_evt.data - if filetype == const.FILETYPE_STL: + if (filetype == const.FILETYPE_STL) or\ + (filetype == const.FILETYPE_VTP): + + # First we identify all surfaces that are selected + # (if any) proj = prj.Project() polydata_list = [] for index in proj.surface_dict: @@ -299,10 +303,24 @@ class SurfaceManager(): else: polydata = pu.Merge(polydata_list) - writer = vtk.vtkSTLWriter() - writer.SetFileTypeToBinary() + + # Having a polydata that represents all surfaces + # selected, we write it, according to filetype + if filetype == const.FILETYPE_STL: + writer = vtk.vtkSTLWriter() + writer.SetFileTypeToBinary() + elif filetype == const.FILETYPE_VTP: + writer = vtk.vtkXMLPolyDataWriter() + elif filetype == const.FILETYPE_IV: + writer = vtk.vtkIVWriter() + elif filetype == const.FILETYPE_PLY: + writer = vtk.vtkPLYWriter() + writer.SetFileTypeToBinary() + writer.SetDataByteOrderToLittleEndian() + #writer.SetColorModeToUniformCellColor() + #writer.SetColor(255, 0, 0) + writer.SetFileName(filename) writer.SetInput(polydata) writer.Write() - diff --git a/invesalius/gui/task_exporter.py b/invesalius/gui/task_exporter.py index c442174..8bdf374 100644 --- a/invesalius/gui/task_exporter.py +++ b/invesalius/gui/task_exporter.py @@ -28,26 +28,34 @@ import wx.lib.pubsub as ps import constants as const import project as proj +BTN_MASK = wx.NewId() BTN_PICTURE = wx.NewId() BTN_SURFACE = wx.NewId() BTN_REPORT = wx.NewId() BTN_REQUEST_RP = wx.NewId() WILDCARD_SAVE_3D = "Inventor (*.iv)|*.iv|"\ + "PLY (*.ply)|*.ply|"\ "Renderman (*.rib)|*.rib|"\ "STL (*.stl)|*.stl|"\ "VRML (*.vrml)|*.vrml|"\ + "VTK PolyData (*.vtp)|*.vtp|"\ "Wavefront (*.obj)|*.obj" + INDEX_TO_TYPE_3D = {0: const.FILETYPE_IV, - 1: const.FILETYPE_RIB, - 2: const.FILETYPE_STL, - 3: const.FILETYPE_VRML, - 4: const.FILETYPE_OBJ} + 1: const.FILETYPE_PLY, + 2: const.FILETYPE_RIB, + 3: const.FILETYPE_STL, + 4: const.FILETYPE_VRML, + 5: const.FILETYPE_VTP, + 6: const.FILETYPE_OBJ} INDEX_TO_EXTENSION = {0: "iv", - 1: "rib", - 2: "stl", - 3: "vrml", - 4: "obj"} + 1: "ply", + 2: "rib", + 3: "stl", + 4: "vrml", + 5: "vtp", + 6: "obj"} WILDCARD_SAVE_2D = "BMP (*.bmp)|*.bmp|"\ "JPEG (*.jpg)|*.jpg|"\ @@ -62,6 +70,8 @@ INDEX_TO_TYPE_2D = {0: const.FILETYPE_BMP, 4: const.FILETYPE_POV, 5: const.FILETYPE_OBJ} +WILDCARD_SAVE_MASK = "VTK ImageData (*.vti)|*.vti" + class TaskPanel(wx.Panel): def __init__(self, parent): @@ -109,6 +119,17 @@ class InnerTaskPanel(wx.Panel): link_export_surface.Bind(hl.EVT_HYPERLINK_LEFT, self.OnLinkExportSurface) + tooltip = wx.ToolTip("Export 3D mask (voxels)") + link_export_mask = hl.HyperLinkCtrl(self, -1,"Export mask...") + link_export_mask.SetUnderlines(False, False, False) + link_export_mask.SetColours("BLACK", "BLACK", "BLACK") + link_export_mask.SetToolTip(tooltip) + link_export_mask.AutoBrowse(False) + link_export_mask.UpdateLink() + link_export_mask.Bind(hl.EVT_HYPERLINK_LEFT, + self.OnLinkExportMask) + + #tooltip = wx.ToolTip("Request rapid prototyping services") #link_request_rp = hl.HyperLinkCtrl(self,-1,"Request rapid prototyping...") #link_request_rp.SetUnderlines(False, False, False) @@ -136,14 +157,19 @@ class InnerTaskPanel(wx.Panel): BMP_TAKE_PICTURE = wx.Bitmap(\ "../icons/tool_photo_original.png", wx.BITMAP_TYPE_PNG) + BMP_EXPORT_MASK = wx.Bitmap("../icons/mask.png", + wx.BITMAP_TYPE_PNG) else: BMP_EXPORT_SURFACE = wx.Bitmap("../icons/surface_export.png", wx.BITMAP_TYPE_PNG) BMP_TAKE_PICTURE = wx.Bitmap("../icons/tool_photo.png", wx.BITMAP_TYPE_PNG) + BMP_EXPORT_MASK = wx.Bitmap("../icons/mask_small.png", + wx.BITMAP_TYPE_PNG) - bmp_list = [BMP_TAKE_PICTURE, BMP_EXPORT_SURFACE] + bmp_list = [BMP_TAKE_PICTURE, BMP_EXPORT_SURFACE, + BMP_EXPORT_MASK] for bmp in bmp_list: bmp.SetWidth(25) bmp.SetHeight(25) @@ -157,6 +183,9 @@ class InnerTaskPanel(wx.Panel): button_surface = pbtn.PlateButton(self, BTN_SURFACE, "", BMP_EXPORT_SURFACE, style=button_style) + button_mask = pbtn.PlateButton(self, BTN_MASK, "", + BMP_EXPORT_MASK, + style=button_style) #button_request_rp = pbtn.PlateButton(self, BTN_REQUEST_RP, "", # BMP_IMPORT, style=button_style) #button_report = pbtn.PlateButton(self, BTN_REPORT, "", @@ -170,12 +199,14 @@ class InnerTaskPanel(wx.Panel): flag_link = wx.EXPAND|wx.GROW|wx.LEFT|wx.TOP flag_button = wx.EXPAND | wx.GROW - fixed_sizer = wx.FlexGridSizer(rows=2, cols=2, hgap=2, vgap=0) + fixed_sizer = wx.FlexGridSizer(rows=3, cols=2, hgap=2, vgap=0) fixed_sizer.AddGrowableCol(0, 1) fixed_sizer.AddMany([ (link_export_picture, 1, flag_link, 3), (button_picture, 0, flag_button), (link_export_surface, 1, flag_link, 3), - (button_surface, 0, flag_button)])#, + (button_surface, 0, flag_button),#])#, + (link_export_mask, 1, flag_link, 3), + (button_mask, 0, flag_button)]) #(link_report, 0, flag_link, 3), #(button_report, 0, flag_button), #(link_request_rp, 1, flag_link, 3), @@ -193,8 +224,37 @@ class InnerTaskPanel(wx.Panel): def OnLinkExportPicture(self, evt=None): pass + def OnLinkExportMask(self, evt=None): + project = proj.Project() + print "OnLinkEportMask" + if sys.platform == 'win32': + project_name = project.name + else: + project_name = project.name+".vti" + + + dlg = wx.FileDialog(None, + "Save mask as...", # title + "", # last used directory + project_name, # filename + WILDCARD_SAVE_MASK, + wx.SAVE|wx.OVERWRITE_PROMPT) + dlg.SetFilterIndex(0) # default is VTI + + if dlg.ShowModal() == wx.ID_OK: + filename = dlg.GetPath() + print "filename", filename + extension = "vti" + if sys.platform != 'win32': + if filename.split(".")[-1] != extension: + filename = filename + "."+ extension + filetype = const.FILETYPE_IMAGEDATA + ps.Publisher().sendMessage('Export mask to file', + (filename, filetype)) + def OnLinkExportSurface(self, evt=None): + "OnLinkExportSurface" project = proj.Project() n_surface = 0 @@ -215,7 +275,7 @@ class InnerTaskPanel(wx.Panel): project_name, # filename WILDCARD_SAVE_3D, wx.SAVE|wx.OVERWRITE_PROMPT) - dlg.SetFilterIndex(2) # default is STL + dlg.SetFilterIndex(3) # default is STL if dlg.ShowModal() == wx.ID_OK: filetype_index = dlg.GetFilterIndex() @@ -252,5 +312,7 @@ class InnerTaskPanel(wx.Panel): self.OnLinkExportSurface() elif id == BTN_REPORT: self.OnLinkReport() - else: #elif id == BTN_REQUEST_RP: + elif id == BTN_REQUEST_RP: self.OnLinkRequestRP() + else:# id == BTN_MASK: + self.OnLinkExportMask() -- libgit2 0.21.2