Commit aff7c8adc60487090638fbb50f526a6938cbdb6b

Authored by Thiago Franco de Moraes
1 parent 7c25cbf9
Exists in select_part

Better method to get the position of the clicked voxel

invesalius/data/styles.py
... ... @@ -1966,26 +1966,11 @@ class SelectMaskPartsInteractorStyle(DefaultInteractorStyle):
1966 1966 if (self.viewer.slice_.buffer_slices[self.orientation].mask is None):
1967 1967 return
1968 1968  
1969   - viewer = self.viewer
1970   - iren = viewer.interactor
1971   -
  1969 + iren = self.viewer.interactor
1972 1970 mouse_x, mouse_y = iren.GetEventPosition()
1973   - render = iren.FindPokedRenderer(mouse_x, mouse_y)
1974   - slice_data = viewer.get_slice_data(render)
1975   -
1976   - self.picker.Pick(mouse_x, mouse_y, 0, render)
1977   -
1978   - coord = self.get_coordinate_cursor()
1979   - position = slice_data.actor.GetInput().FindPoint(coord)
1980   -
1981   - if position != -1:
1982   - coord = slice_data.actor.GetInput().GetPoint(position)
1983   -
1984   - if position < 0:
1985   - position = viewer.calculate_matrix_position(coord)
  1971 + x, y, z = self.viewer.get_voxel_clicked(mouse_x, mouse_y, self.picker)
1986 1972  
1987 1973 mask = self.viewer.slice_.current_mask.matrix[1:, 1:, 1:]
1988   - x, y, z = self.calcultate_scroll_position(position)
1989 1974  
1990 1975 bstruct = np.array(generate_binary_structure(3, CON3D[self.config.con_3d]), dtype='uint8')
1991 1976 self.viewer.slice_.do_threshold_to_all_slices()
... ... @@ -2009,42 +1994,6 @@ class SelectMaskPartsInteractorStyle(DefaultInteractorStyle):
2009 1994  
2010 1995 self.config.mask = mask
2011 1996  
2012   - def get_coordinate_cursor(self):
2013   - # Find position
2014   - x, y, z = self.picker.GetPickPosition()
2015   - bounds = self.viewer.slice_data.actor.GetBounds()
2016   - if bounds[0] == bounds[1]:
2017   - x = bounds[0]
2018   - elif bounds[2] == bounds[3]:
2019   - y = bounds[2]
2020   - elif bounds[4] == bounds[5]:
2021   - z = bounds[4]
2022   - return x, y, z
2023   -
2024   - def calcultate_scroll_position(self, position):
2025   - # Based in the given coord (x, y, z), returns a list with the scroll positions for each
2026   - # orientation, being the first position the sagital, second the coronal
2027   - # and the last, axial.
2028   -
2029   - if self.orientation == 'AXIAL':
2030   - image_width = self.slice_actor.GetInput().GetDimensions()[0]
2031   - axial = self.slice_data.number
2032   - coronal = position / image_width
2033   - sagital = position % image_width
2034   -
2035   - elif self.orientation == 'CORONAL':
2036   - image_width = self.slice_actor.GetInput().GetDimensions()[0]
2037   - axial = position / image_width
2038   - coronal = self.slice_data.number
2039   - sagital = position % image_width
2040   -
2041   - elif self.orientation == 'SAGITAL':
2042   - image_width = self.slice_actor.GetInput().GetDimensions()[1]
2043   - axial = position / image_width
2044   - coronal = position % image_width
2045   - sagital = self.slice_data.number
2046   -
2047   - return sagital, coronal, axial
2048 1997  
2049 1998 def get_style(style):
2050 1999 STYLES = {
... ...
invesalius/data/viewer_slice.py
... ... @@ -977,9 +977,12 @@ class Viewer(wx.Panel):
977 977 my = round((z - zi)/self.slice_.spacing[2], 0)
978 978 return my, mx
979 979  
980   - def get_coordinate_cursor(self):
  980 + def get_coordinate_cursor(self, picker=None):
981 981 # Find position
982   - x, y, z = self.pick.GetPickPosition()
  982 + if picker is None:
  983 + picker = self.pick
  984 +
  985 + x, y, z = picker.GetPickPosition()
983 986 bounds = self.slice_data.actor.GetBounds()
984 987 if bounds[0] == bounds[1]:
985 988 x = bounds[0]
... ... @@ -989,11 +992,14 @@ class Viewer(wx.Panel):
989 992 z = bounds[4]
990 993 return x, y, z
991 994  
992   - def get_coordinate_cursor_edition(self, slice_data):
  995 + def get_coordinate_cursor_edition(self, slice_data, picker=None):
993 996 # Find position
994 997 actor = slice_data.actor
995 998 slice_number = slice_data.number
996   - x, y, z = self.pick.GetPickPosition()
  999 + if picker is None:
  1000 + picker = self.pick
  1001 +
  1002 + x, y, z = picker.GetPickPosition()
997 1003  
998 1004 # First we fix the position origin, based on vtkActor bounds
999 1005 bounds = actor.GetBounds()
... ... @@ -1023,6 +1029,41 @@ class Viewer(wx.Panel):
1023 1029  
1024 1030 return x, y, z
1025 1031  
  1032 + def get_voxel_clicked(self, mx, my, picker=None):
  1033 + """
  1034 + Given the (mx, my) mouse clicked position returns the voxel coordinate
  1035 + of the voxel at (that mx, my) position.
  1036 +
  1037 + Parameters:
  1038 + mx (int): x position.
  1039 + my (int): y position
  1040 + picker: the picker used to get calculate the voxel coordinate.
  1041 +
  1042 + Returns:
  1043 + voxel_coordinate (x, y, z): voxel coordinate inside the matrix. Can
  1044 + be used to access the voxel value inside the matrix.
  1045 + """
  1046 + if picker is None:
  1047 + picker = self.pick
  1048 +
  1049 + slice_data = self.slice_data
  1050 + renderer = slice_data.renderer
  1051 +
  1052 + picker.Pick(mx, my, 0, renderer)
  1053 +
  1054 + coord = self.get_coordinate_cursor(picker)
  1055 + position = slice_data.actor.GetInput().FindPoint(coord)
  1056 +
  1057 + if position != -1:
  1058 + coord = slice_data.actor.GetInput().GetPoint(position)
  1059 +
  1060 + if position < 0:
  1061 + position = viewer.calculate_matrix_position(coord)
  1062 +
  1063 + x, y, z = self.calcultate_scroll_position(position)
  1064 +
  1065 + return (x, y, z)
  1066 +
1026 1067 def __bind_events(self):
1027 1068 Publisher.subscribe(self.LoadImagedata,
1028 1069 'Load slice to viewer')
... ...