Commit 0439d030316245e13c2474fea15feb2130f2fd63
1 parent
10945a2a
Exists in
get_pixel
Added a function to calculate the pixel clicked
Showing
2 changed files
with
78 additions
and
54 deletions
Show diff stats
invesalius/data/styles.py
| ... | ... | @@ -1115,28 +1115,15 @@ class WaterShedInteractorStyle(DefaultInteractorStyle): |
| 1115 | 1115 | render = iren.FindPokedRenderer(mouse_x, mouse_y) |
| 1116 | 1116 | slice_data = viewer.get_slice_data(render) |
| 1117 | 1117 | |
| 1118 | - # TODO: Improve! | |
| 1119 | - #for i in self.slice_data_list: | |
| 1120 | - #i.cursor.Show(0) | |
| 1121 | - slice_data.cursor.Show() | |
| 1122 | - | |
| 1123 | - self.picker.Pick(mouse_x, mouse_y, 0, render) | |
| 1124 | - | |
| 1125 | - coord = self.get_coordinate_cursor() | |
| 1126 | - position = slice_data.actor.GetInput().FindPoint(coord) | |
| 1127 | - | |
| 1128 | - if position != -1: | |
| 1129 | - coord = slice_data.actor.GetInput().GetPoint(position) | |
| 1118 | + coord = self.viewer.get_coord_inside_volume(mouse_x, mouse_y, picker=None) | |
| 1119 | + position = self.viewer.get_slice_pixel_coord_by_screen_pos(mouse_x, mouse_y, self.picker) | |
| 1130 | 1120 | |
| 1121 | + slice_data.cursor.Show() | |
| 1131 | 1122 | slice_data.cursor.SetPosition(coord) |
| 1132 | 1123 | |
| 1133 | 1124 | cursor = slice_data.cursor |
| 1134 | - position = slice_data.actor.GetInput().FindPoint(coord) | |
| 1135 | 1125 | radius = cursor.radius |
| 1136 | 1126 | |
| 1137 | - if position < 0: | |
| 1138 | - position = viewer.calculate_matrix_position(coord) | |
| 1139 | - | |
| 1140 | 1127 | operation = self.config.operation |
| 1141 | 1128 | |
| 1142 | 1129 | if operation == BRUSH_FOREGROUND: |
| ... | ... | @@ -1175,31 +1162,12 @@ class WaterShedInteractorStyle(DefaultInteractorStyle): |
| 1175 | 1162 | render = iren.FindPokedRenderer(mouse_x, mouse_y) |
| 1176 | 1163 | slice_data = viewer.get_slice_data(render) |
| 1177 | 1164 | |
| 1178 | - # TODO: Improve! | |
| 1179 | - #for i in self.slice_data_list: | |
| 1180 | - #i.cursor.Show(0) | |
| 1181 | - | |
| 1182 | - self.picker.Pick(mouse_x, mouse_y, 0, render) | |
| 1183 | - | |
| 1184 | - #if (self.pick.GetViewProp()): | |
| 1185 | - #self.interactor.SetCursor(wx.StockCursor(wx.CURSOR_BLANK)) | |
| 1186 | - #else: | |
| 1187 | - #self.interactor.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT)) | |
| 1188 | - | |
| 1189 | - coord = self.get_coordinate_cursor() | |
| 1190 | - position = viewer.slice_data.actor.GetInput().FindPoint(coord) | |
| 1191 | - | |
| 1192 | - # when position == -1 the cursos is not over the image, so is not | |
| 1193 | - # necessary to set the cursor position to world coordinate center of | |
| 1194 | - # pixel from slice image. | |
| 1195 | - if position != -1: | |
| 1196 | - coord = slice_data.actor.GetInput().GetPoint(position) | |
| 1165 | + coord = self.viewer.get_coord_inside_volume(mouse_x, mouse_y, picker=None) | |
| 1197 | 1166 | slice_data.cursor.SetPosition(coord) |
| 1198 | - #self.__update_cursor_position(slice_data, coord) | |
| 1199 | 1167 | |
| 1200 | 1168 | if (self.left_pressed): |
| 1201 | 1169 | cursor = slice_data.cursor |
| 1202 | - position = slice_data.actor.GetInput().FindPoint(coord) | |
| 1170 | + position = self.viewer.get_slice_pixel_coord_by_screen_pos(mouse_x, mouse_y, self.picker) | |
| 1203 | 1171 | radius = cursor.radius |
| 1204 | 1172 | |
| 1205 | 1173 | if position < 0: |
| ... | ... | @@ -1306,18 +1274,6 @@ class WaterShedInteractorStyle(DefaultInteractorStyle): |
| 1306 | 1274 | |
| 1307 | 1275 | Publisher.sendMessage('Reload actual slice') |
| 1308 | 1276 | |
| 1309 | - def get_coordinate_cursor(self): | |
| 1310 | - # Find position | |
| 1311 | - x, y, z = self.picker.GetPickPosition() | |
| 1312 | - bounds = self.viewer.slice_data.actor.GetBounds() | |
| 1313 | - if bounds[0] == bounds[1]: | |
| 1314 | - x = bounds[0] | |
| 1315 | - elif bounds[2] == bounds[3]: | |
| 1316 | - y = bounds[2] | |
| 1317 | - elif bounds[4] == bounds[5]: | |
| 1318 | - z = bounds[4] | |
| 1319 | - return x, y, z | |
| 1320 | - | |
| 1321 | 1277 | def edit_mask_pixel(self, operation, n, index, position, radius, orientation): |
| 1322 | 1278 | if orientation == 'AXIAL': |
| 1323 | 1279 | mask = self.matrix[n, :, :] |
| ... | ... | @@ -1328,7 +1284,7 @@ class WaterShedInteractorStyle(DefaultInteractorStyle): |
| 1328 | 1284 | |
| 1329 | 1285 | spacing = self.viewer.slice_.spacing |
| 1330 | 1286 | if hasattr(position, '__iter__'): |
| 1331 | - py, px = position | |
| 1287 | + px, py = position | |
| 1332 | 1288 | if orientation == 'AXIAL': |
| 1333 | 1289 | sx = spacing[0] |
| 1334 | 1290 | sy = spacing[1] |
| ... | ... | @@ -1767,7 +1723,7 @@ class FloodFillMaskInteractorStyle(DefaultInteractorStyle): |
| 1767 | 1723 | viewer = self.viewer |
| 1768 | 1724 | iren = viewer.interactor |
| 1769 | 1725 | mouse_x, mouse_y = iren.GetEventPosition() |
| 1770 | - x, y, z = self.viewer.get_voxel_clicked(mouse_x, mouse_y, self.picker) | |
| 1726 | + x, y, z = self.viewer.get_voxel_coord_by_screen_pos(mouse_x, mouse_y, self.picker) | |
| 1771 | 1727 | |
| 1772 | 1728 | mask = self.viewer.slice_.current_mask.matrix[1:, 1:, 1:] |
| 1773 | 1729 | if mask[z, y, x] < self.t0 or mask[z, y, x] > self.t1: |
| ... | ... | @@ -1900,7 +1856,7 @@ class SelectMaskPartsInteractorStyle(DefaultInteractorStyle): |
| 1900 | 1856 | |
| 1901 | 1857 | iren = self.viewer.interactor |
| 1902 | 1858 | mouse_x, mouse_y = iren.GetEventPosition() |
| 1903 | - x, y, z = self.viewer.get_voxel_clicked(mouse_x, mouse_y, self.picker) | |
| 1859 | + x, y, z = self.viewer.get_voxel_coord_by_screen_pos(mouse_x, mouse_y, self.picker) | |
| 1904 | 1860 | |
| 1905 | 1861 | mask = self.viewer.slice_.current_mask.matrix[1:, 1:, 1:] |
| 1906 | 1862 | ... | ... |
invesalius/data/viewer_slice.py
| ... | ... | @@ -1029,9 +1029,9 @@ class Viewer(wx.Panel): |
| 1029 | 1029 | |
| 1030 | 1030 | return x, y, z |
| 1031 | 1031 | |
| 1032 | - def get_voxel_clicked(self, mx, my, picker=None): | |
| 1032 | + def get_voxel_coord_by_screen_pos(self, mx, my, picker=None): | |
| 1033 | 1033 | """ |
| 1034 | - Given the (mx, my) mouse clicked position returns the voxel coordinate | |
| 1034 | + Given the (mx, my) screen position returns the voxel coordinate | |
| 1035 | 1035 | of the voxel at (that mx, my) position. |
| 1036 | 1036 | |
| 1037 | 1037 | Parameters: |
| ... | ... | @@ -1064,6 +1064,74 @@ class Viewer(wx.Panel): |
| 1064 | 1064 | |
| 1065 | 1065 | return (x, y, z) |
| 1066 | 1066 | |
| 1067 | + def get_slice_pixel_coord_by_screen_pos(self, mx, my, picker=None): | |
| 1068 | + """ | |
| 1069 | + Given the (mx, my) screen position returns the pixel coordinate | |
| 1070 | + of the slice at (that mx, my) position. | |
| 1071 | + | |
| 1072 | + Parameters: | |
| 1073 | + mx (int): x position. | |
| 1074 | + my (int): y position | |
| 1075 | + picker: the picker used to get calculate the pixel coordinate. | |
| 1076 | + | |
| 1077 | + Returns: | |
| 1078 | + voxel_coordinate (x, y): voxel coordinate inside the matrix. Can | |
| 1079 | + be used to access the voxel value inside the matrix. | |
| 1080 | + """ | |
| 1081 | + if picker is None: | |
| 1082 | + picker = self.pick | |
| 1083 | + | |
| 1084 | + slice_data = self.slice_data | |
| 1085 | + renderer = slice_data.renderer | |
| 1086 | + | |
| 1087 | + picker.Pick(mx, my, 0, renderer) | |
| 1088 | + | |
| 1089 | + coord = self.get_coordinate_cursor(picker) | |
| 1090 | + position = slice_data.actor.GetInput().FindPoint(coord) | |
| 1091 | + | |
| 1092 | + if position != -1: | |
| 1093 | + coord = slice_data.actor.GetInput().GetPoint(position) | |
| 1094 | + | |
| 1095 | + if position < 0: | |
| 1096 | + px, py = viewer.calculate_matrix_position(coord) | |
| 1097 | + else: | |
| 1098 | + spacing = self.slice_.spacing | |
| 1099 | + image = self.slice_.buffer_slices[self.orientation].image | |
| 1100 | + if self.orientation == 'AXIAL': | |
| 1101 | + sx = spacing[0] | |
| 1102 | + sy = spacing[1] | |
| 1103 | + py = position / image.shape[1] | |
| 1104 | + px = position % image.shape[1] | |
| 1105 | + elif self.orientation == 'CORONAL': | |
| 1106 | + sx = spacing[0] | |
| 1107 | + sy = spacing[2] | |
| 1108 | + py = position / image.shape[1] | |
| 1109 | + px = position % image.shape[1] | |
| 1110 | + elif self.orientation == 'SAGITAL': | |
| 1111 | + sx = spacing[2] | |
| 1112 | + sy = spacing[1] | |
| 1113 | + py = position / image.shape[1] | |
| 1114 | + px = position % image.shape[1] | |
| 1115 | + | |
| 1116 | + return px, py | |
| 1117 | + | |
| 1118 | + def get_coord_inside_volume(self, mx, my, picker=None): | |
| 1119 | + if picker is None: | |
| 1120 | + picker = self.pick | |
| 1121 | + | |
| 1122 | + slice_data = self.slice_data | |
| 1123 | + renderer = slice_data.renderer | |
| 1124 | + | |
| 1125 | + picker.Pick(mx, my, 0, renderer) | |
| 1126 | + | |
| 1127 | + coord = self.get_coordinate_cursor(picker) | |
| 1128 | + position = slice_data.actor.GetInput().FindPoint(coord) | |
| 1129 | + | |
| 1130 | + if position != -1: | |
| 1131 | + coord = slice_data.actor.GetInput().GetPoint(position) | |
| 1132 | + | |
| 1133 | + return coord | |
| 1134 | + | |
| 1067 | 1135 | def __bind_events(self): |
| 1068 | 1136 | Publisher.subscribe(self.LoadImagedata, |
| 1069 | 1137 | 'Load slice to viewer') | ... | ... |