From 7f23d9efe7e270c59e4da6d728dcf39bcba5dff8 Mon Sep 17 00:00:00 2001 From: tatiana Date: Tue, 21 Jul 2009 21:02:39 +0000 Subject: [PATCH] ADD: Brush add, remove and threshold operations are working --- invesalius/data/slice_.py | 49 ++++++++++++++++++++++++++++--------------------- invesalius/data/viewer_slice.py | 21 +++++++++++++++++++-- invesalius/gui/task_slice.py | 60 ++++++++++++++++++++++++++++++++++++------------------------ 3 files changed, 83 insertions(+), 47 deletions(-) diff --git a/invesalius/data/slice_.py b/invesalius/data/slice_.py index e822b61..9c5f3d6 100644 --- a/invesalius/data/slice_.py +++ b/invesalius/data/slice_.py @@ -21,8 +21,8 @@ class Slice(object): def __bind_events(self): ps.Publisher().subscribe(self.SetThresholdRange, 'Set threshold values') - #ps.Publisher().subscribe(self.SetEditionThresholdRange, - # 'Set edition threshold values') + ps.Publisher().subscribe(self.SetEditionThresholdRange, + 'Set edition threshold values') ps.Publisher().subscribe(self.OnChangeCurrentMaskColour, 'Change mask colour') ps.Publisher().subscribe(self.AddMask, 'Create new mask') @@ -34,12 +34,21 @@ class Slice(object): ps.Publisher().subscribe(self.ShowMask, 'Show mask') ps.Publisher().subscribe(self.ChangeMaskName, 'Change mask name') ps.Publisher().subscribe(self.EraseMaskPixel, 'Erase mask pixel') - + ps.Publisher().subscribe(self.EditMaskPixel, 'Edit mask pixel') + ps.Publisher().subscribe(self.AddMaskPixel, 'Add mask pixel') def EraseMaskPixel(self, pubsub_evt): position = pubsub_evt.data self.ErasePixel(position) + def EditMaskPixel(self, pubsub_evt): + position = pubsub_evt.data + self.EditPixelBasedOnThreshold(position) + + def AddMaskPixel(self, pubsub_evt): + position = pubsub_evt.data + self.DrawPixel(position) + def ChangeMaskName(self, pubsub_evt): index, name = pubsub_evt.data @@ -362,43 +371,41 @@ class Slice(object): self.current_mask.threshold_range)) ps.Publisher().sendMessage('Update slice viewer') - #def SetEditionThresholdRange(self, evt): - # thresh_min, thresh_max = evt.data - # self.current_mask.edition_threshold_range = thresh_min, thresh_max + def SetEditionThresholdRange(self, evt_pubsub): + if self.current_mask: + thresh_min, thresh_max = evt_pubsub.data + self.current_mask.edition_threshold_range = thresh_min, thresh_max def ErasePixel(self, position): """ Delete pixel, based on x, y and z position coordinates. """ x, y, z = position - imagedata = self.current_mask.imagedata colour = self.imagedata.GetScalarRange()[0]# - 1 # Important to effect erase + imagedata = self.current_mask.imagedata imagedata.SetScalarComponentFromDouble(x, y, z, 0, colour) imagedata.Update() - def DrawPixel(self, x, y, z, colour=None): + def DrawPixel(self, position, colour=None): """ Draw pixel, based on x, y and z position coordinates. """ - imagedata = self.current_mask.imagedata - if colour is None: + x, y, z = position + if not colour: colour = self.imagedata.GetScalarRange()[1] + imagedata = self.current_mask.imagedata imagedata.SetScalarComponentFromDouble(x, y, z, 0, colour) + imagedata.Update() - def EditPixelBasedOnThreshold(self, x, y, z): + def EditPixelBasedOnThreshold(self, position): """ Erase or draw pixel based on edition threshold range. """ - - pixel_colour = imagedata.GetScalarComponentAsDouble(x, y, z, 0) + x, y, z = position + colour = self.imagedata.GetScalarComponentAsDouble(x, y, z, 0) thresh_min, thresh_max = self.current_mask.edition_threshold_range - if (pixel_colour >= thresh_min) and (pixel_colour <= thresh_max): - self.DrawPixel(x, y, z, pixel_colour) - # TODO: See if the code bellow is really necessary - #if (pixel_colour <= 0): - # self.DrawPixel(x, y, z, 1) - #else: - # self.DrawPixel(x, y, z, pixel_colour) + if (colour >= thresh_min) and (colour <= thresh_max): + self.DrawPixel(position, colour) else: - self.ErasePixel(x, y, z) + self.ErasePixel(position) diff --git a/invesalius/data/viewer_slice.py b/invesalius/data/viewer_slice.py index 1e10bf3..a34ae8e 100755 --- a/invesalius/data/viewer_slice.py +++ b/invesalius/data/viewer_slice.py @@ -45,6 +45,9 @@ class Viewer(wx.Panel): self.orientation = orientation self.slice_number = 0 + self._brush_cursor_op = 'Draw' + self.brush_cursor_size = 30 + # VTK pipeline and actors self.__config_interactor() self.pick = vtk.vtkCellPicker() @@ -184,11 +187,19 @@ class Viewer(wx.Panel): self.cursor.SetPosition(coord) self.cursor.SetEditionPosition(self.GetCoordinateCursorEdition()) self.ren.Render() + + if self._brush_cursor_op == 'Erase': + evt_msg = 'Erase mask pixel' + elif self._brush_cursor_op == 'Draw': + evt_msg = 'Add mask pixel' + elif self._brush_cursor_op == 'Threshold': + evt_msg = 'Edit mask pixel' + if self.mouse_pressed: print "Edit pixel region based on origin:", coord pixels = self.cursor.GetPixels() for coord in pixels: - ps.Publisher().sendMessage('Erase mask pixel', coord) + ps.Publisher().sendMessage(evt_msg, coord) self.interactor.Render() def OnCrossMove(self, obj, evt_vtk): @@ -300,6 +311,12 @@ class Viewer(wx.Panel): ps.Publisher().subscribe(self.ChangeBrushSize,'Set edition brush size') ps.Publisher().subscribe(self.ChangeBrushColour, 'Add mask') ps.Publisher().subscribe(self.ChangeBrushActor, 'Set brush format') + ps.Publisher().subscribe(self.ChangeBrushOperation, 'Set edition operation') + + + def ChangeBrushOperation(self, pubsub_evt): + print pubsub_evt.data + self._brush_cursor_op = pubsub_evt.data def __bind_events_wx(self): self.scroll.Bind(wx.EVT_SCROLL, self.OnScrollBar) @@ -409,7 +426,7 @@ class Viewer(wx.Panel): def SetColour(self, pubsub_evt): colour_wx = pubsub_evt.data colour_vtk = [colour/float(255) for colour in colour_wx] - self._brush_cursor_colour = vtk_colour + self._brush_cursor_colour = colour_vtk self.cursor.SetColour(colour_vtk) self.interactor.Render() diff --git a/invesalius/gui/task_slice.py b/invesalius/gui/task_slice.py index b3f0f96..b32543a 100644 --- a/invesalius/gui/task_slice.py +++ b/invesalius/gui/task_slice.py @@ -303,7 +303,8 @@ class MaskProperties(wx.Panel): index, name = pubsub_evt.data self.combo_mask_name.SetString(index, name) self.combo_mask_name.Refresh() - + + def SetThresholdValues(self, pubsub_evt): thresh_min, thresh_max = pubsub_evt.data self.bind_evt_gradient = False @@ -421,6 +422,7 @@ class EditionTools(wx.Panel): gradient_thresh = grad.GradientSlider(self, -1, 0, 5000, 0, 5000, (0, 0, 255, 100)) self.gradient_thresh = gradient_thresh + self.bind_evt_gradient = True # Add lines into main sizer sizer = wx.BoxSizer(wx.VERTICAL) @@ -439,38 +441,48 @@ class EditionTools(wx.Panel): def __bind_events_wx(self): - pass - #self.Bind(grad.EVT_THRESHOLD_CHANGE, self.OnGradientChanged, - # self.gradient_thresh) + self.Bind(grad.EVT_THRESHOLD_CHANGE, self.OnGradientChanged, + self.gradient_thresh) def __bind_events(self): - #ps.Publisher().subscribe(self.SetThresholdBounds, - # 'Update threshold limits') + ps.Publisher().subscribe(self.SetThresholdBounds, + 'Update threshold limits') #ps.Publisher().subscribe(self.SetThresholdValues, # 'Set threshold values in gradient') ps.Publisher().subscribe(self.ChangeMaskColour, 'Change mask colour') + ps.Publisher().subscribe(self.SetGradientColour, 'Add mask') def ChangeMaskColour(self, pubsub_evt): colour = pubsub_evt.data self.gradient_thresh.SetColour(colour) - - #def SetThresholdValues(self, pubsub_evt): - # thresh_min = pubsub_evt.data[0][0] - # thresh_max = pubsub_evt.data[0][1] - # self.gradient_thresh.SetMinValue(thresh_min) - # self.gradient_thresh.SetMaxValue(thresh_max) - - #def SetThresholdBounds(self, pubsub_evt): - # thresh_min = pubsub_evt.data[0] - # thresh_max = pubsub_evt.data[1] - # self.gradient_thresh.SetMinRange(thresh_min) - # self.gradient_thresh.SetMaxRange(thresh_max) - - #def OnGradientChanged(self, evt): - # thresh_min = self.gradient_thresh.GetMinValue() - # thresh_max = self.gradient_thresh.GetMaxValue() - # #ps.Publisher().sendMessage('Set edition threshold values', - # # (thresh_min, thresh_max)) + + def SetGradientColour(self, pubsub_evt): + vtk_colour = pubsub_evt.data[3] + wx_colour = [c*255 for c in vtk_colour] + self.gradient_thresh.SetColour(wx_colour) + + def SetThresholdValues(self, pubsub_evt): + thresh_min, thresh_max = pubsub_evt.data + self.bind_evt_gradient = False + self.gradient_thresh.SetMinValue(thresh_min) + self.gradient_thresh.SetMaxValue(thresh_max) + self.bind_evt_gradient = True + + def SetThresholdBounds(self, pubsub_evt): + thresh_min = pubsub_evt.data[0] + thresh_max = pubsub_evt.data[1] + print thresh_min, thresh_max + self.gradient_thresh.SetMinRange(thresh_min) + self.gradient_thresh.SetMaxRange(thresh_max) + self.gradient_thresh.SetMinValue(thresh_min) + self.gradient_thresh.SetMaxValue(thresh_max) + + def OnGradientChanged(self, evt): + thresh_min = self.gradient_thresh.GetMinValue() + thresh_max = self.gradient_thresh.GetMaxValue() + if self.bind_evt_gradient: + ps.Publisher().sendMessage('Set edition threshold values', + (thresh_min, thresh_max)) def OnMenu(self, evt): """Button's menu event""" -- libgit2 0.21.2