diff --git a/invesalius/data/styles.py b/invesalius/data/styles.py index 6547b24..71b5a89 100644 --- a/invesalius/data/styles.py +++ b/invesalius/data/styles.py @@ -1966,26 +1966,11 @@ class SelectMaskPartsInteractorStyle(DefaultInteractorStyle): if (self.viewer.slice_.buffer_slices[self.orientation].mask is None): return - viewer = self.viewer - iren = viewer.interactor - + iren = self.viewer.interactor mouse_x, mouse_y = iren.GetEventPosition() - render = iren.FindPokedRenderer(mouse_x, mouse_y) - slice_data = viewer.get_slice_data(render) - - self.picker.Pick(mouse_x, mouse_y, 0, render) - - coord = self.get_coordinate_cursor() - position = slice_data.actor.GetInput().FindPoint(coord) - - if position != -1: - coord = slice_data.actor.GetInput().GetPoint(position) - - if position < 0: - position = viewer.calculate_matrix_position(coord) + x, y, z = self.viewer.get_voxel_clicked(mouse_x, mouse_y, self.picker) mask = self.viewer.slice_.current_mask.matrix[1:, 1:, 1:] - x, y, z = self.calcultate_scroll_position(position) bstruct = np.array(generate_binary_structure(3, CON3D[self.config.con_3d]), dtype='uint8') self.viewer.slice_.do_threshold_to_all_slices() @@ -2009,42 +1994,6 @@ class SelectMaskPartsInteractorStyle(DefaultInteractorStyle): self.config.mask = mask - def get_coordinate_cursor(self): - # Find position - x, y, z = self.picker.GetPickPosition() - bounds = self.viewer.slice_data.actor.GetBounds() - if bounds[0] == bounds[1]: - x = bounds[0] - elif bounds[2] == bounds[3]: - y = bounds[2] - elif bounds[4] == bounds[5]: - z = bounds[4] - return x, y, z - - def calcultate_scroll_position(self, position): - # Based in the given coord (x, y, z), returns a list with the scroll positions for each - # orientation, being the first position the sagital, second the coronal - # and the last, axial. - - if self.orientation == 'AXIAL': - image_width = self.slice_actor.GetInput().GetDimensions()[0] - axial = self.slice_data.number - coronal = position / image_width - sagital = position % image_width - - elif self.orientation == 'CORONAL': - image_width = self.slice_actor.GetInput().GetDimensions()[0] - axial = position / image_width - coronal = self.slice_data.number - sagital = position % image_width - - elif self.orientation == 'SAGITAL': - image_width = self.slice_actor.GetInput().GetDimensions()[1] - axial = position / image_width - coronal = position % image_width - sagital = self.slice_data.number - - return sagital, coronal, axial def get_style(style): STYLES = { diff --git a/invesalius/data/viewer_slice.py b/invesalius/data/viewer_slice.py index 5299af2..0aee6f8 100755 --- a/invesalius/data/viewer_slice.py +++ b/invesalius/data/viewer_slice.py @@ -977,9 +977,12 @@ class Viewer(wx.Panel): my = round((z - zi)/self.slice_.spacing[2], 0) return my, mx - def get_coordinate_cursor(self): + def get_coordinate_cursor(self, picker=None): # Find position - x, y, z = self.pick.GetPickPosition() + if picker is None: + picker = self.pick + + x, y, z = picker.GetPickPosition() bounds = self.slice_data.actor.GetBounds() if bounds[0] == bounds[1]: x = bounds[0] @@ -989,11 +992,14 @@ class Viewer(wx.Panel): z = bounds[4] return x, y, z - def get_coordinate_cursor_edition(self, slice_data): + def get_coordinate_cursor_edition(self, slice_data, picker=None): # Find position actor = slice_data.actor slice_number = slice_data.number - x, y, z = self.pick.GetPickPosition() + if picker is None: + picker = self.pick + + x, y, z = picker.GetPickPosition() # First we fix the position origin, based on vtkActor bounds bounds = actor.GetBounds() @@ -1023,6 +1029,41 @@ class Viewer(wx.Panel): return x, y, z + def get_voxel_clicked(self, mx, my, picker=None): + """ + Given the (mx, my) mouse clicked position returns the voxel coordinate + of the voxel at (that mx, my) position. + + Parameters: + mx (int): x position. + my (int): y position + picker: the picker used to get calculate the voxel coordinate. + + Returns: + voxel_coordinate (x, y, z): voxel coordinate inside the matrix. Can + be used to access the voxel value inside the matrix. + """ + if picker is None: + picker = self.pick + + slice_data = self.slice_data + renderer = slice_data.renderer + + picker.Pick(mx, my, 0, renderer) + + coord = self.get_coordinate_cursor(picker) + position = slice_data.actor.GetInput().FindPoint(coord) + + if position != -1: + coord = slice_data.actor.GetInput().GetPoint(position) + + if position < 0: + position = viewer.calculate_matrix_position(coord) + + x, y, z = self.calcultate_scroll_position(position) + + return (x, y, z) + def __bind_events(self): Publisher.subscribe(self.LoadImagedata, 'Load slice to viewer') -- libgit2 0.21.2