From 89ea441d62d2397afb94f7692deb32f600f2e4c7 Mon Sep 17 00:00:00 2001 From: Thiago Franco de Moraes Date: Mon, 29 Aug 2016 10:21:57 -0300 Subject: [PATCH] Simplified get positions in viewer_slice --- invesalius/data/styles.py | 5 +++-- invesalius/data/viewer_slice.py | 89 +++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------ 2 files changed, 50 insertions(+), 44 deletions(-) diff --git a/invesalius/data/styles.py b/invesalius/data/styles.py index 0c954bd..8354e78 100644 --- a/invesalius/data/styles.py +++ b/invesalius/data/styles.py @@ -1162,12 +1162,13 @@ class WaterShedInteractorStyle(DefaultInteractorStyle): render = iren.FindPokedRenderer(mouse_x, mouse_y) slice_data = viewer.get_slice_data(render) - coord = self.viewer.get_coord_inside_volume(mouse_x, mouse_y, picker=None) + coord = self.viewer.get_coord_inside_volume(mouse_x, mouse_y, self.picker) slice_data.cursor.SetPosition(coord) if (self.left_pressed): cursor = slice_data.cursor - position = self.viewer.get_slice_pixel_coord_by_screen_pos(mouse_x, mouse_y, self.picker) + position = self.viewer.get_slice_pixel_coord_by_world_pos(*coord) + print ">>>", position radius = cursor.radius if position < 0: diff --git a/invesalius/data/viewer_slice.py b/invesalius/data/viewer_slice.py index abaa1f0..a5f2ae6 100755 --- a/invesalius/data/viewer_slice.py +++ b/invesalius/data/viewer_slice.py @@ -940,25 +940,23 @@ class Viewer(wx.Panel): # WARN: Return the only slice_data used in this slice_viewer. return self.slice_data - def calcultate_scroll_position(self, position): - # Based in the given coord (x, y, z), returns a list with the scroll positions for each + def calcultate_scroll_position(self, x, y): + # Based in the given coord (x, y), returns a list with the scroll positions for each # orientation, being the first position the sagital, second the coronal # and the last, axial. - image_width = self.slice_.buffer_slices[self.orientation].image.shape[1] - if self.orientation == 'AXIAL': axial = self.slice_data.number - coronal = position / image_width - sagital = position % image_width + coronal = y + sagital = x elif self.orientation == 'CORONAL': - axial = position / image_width + axial = y coronal = self.slice_data.number - sagital = position % image_width + sagital = x elif self.orientation == 'SAGITAL': - axial = position / image_width - coronal = position % image_width + axial = y + coronal = x sagital = self.slice_data.number return sagital, coronal, axial @@ -975,7 +973,7 @@ class Viewer(wx.Panel): elif self.orientation == 'SAGITAL': mx = round((y - yi)/self.slice_.spacing[1], 0) my = round((z - zi)/self.slice_.spacing[2], 0) - return my, mx + return mx, my def get_coordinate_cursor(self, picker=None): # Find position @@ -1051,19 +1049,31 @@ class Viewer(wx.Panel): picker.Pick(mx, my, 0, renderer) - coord = self.get_coordinate_cursor(picker) - position = slice_data.actor.GetInput().FindPoint(coord) + wx, wy, wz = self.get_coordinate_cursor(picker) + x, y, z = self.get_voxel_coord_by_world_pos(wx, wy, wz) - if position != -1: - coord = slice_data.actor.GetInput().GetPoint(position) + return (x, y, z) + + def get_voxel_coord_by_world_pos(self, wx, wy, wz): + """ + Given the (x, my) screen position returns the voxel coordinate + of the voxel at (that mx, my) position. - if position < 0: - position = viewer.calculate_matrix_position(coord) + Parameters: + wx (float): x position. + wy (float): y position + wz (float): z position - x, y, z = self.calcultate_scroll_position(position) + Returns: + voxel_coordinate (x, y, z): voxel coordinate inside the matrix. Can + be used to access the voxel value inside the matrix. + """ + px, py = self.get_slice_pixel_coord_by_world_pos(wx, wy, wz) + x, y, z = self.calcultate_scroll_position(px, py) return (x, y, z) + def get_slice_pixel_coord_by_screen_pos(self, mx, my, picker=None): """ Given the (mx, my) screen position returns the pixel coordinate @@ -1086,32 +1096,27 @@ class Viewer(wx.Panel): picker.Pick(mx, my, 0, renderer) - coord = self.get_coordinate_cursor(picker) - position = slice_data.actor.GetInput().FindPoint(coord) + wx, wy, wz = self.get_coordinate_cursor(picker) + return self.get_slice_pixel_coord_by_world_pos(wx, wy, wz) - if position != -1: - coord = slice_data.actor.GetInput().GetPoint(position) + return px, py - if position < 0: - px, py = viewer.calculate_matrix_position(coord) - else: - spacing = self.slice_.spacing - image = self.slice_.buffer_slices[self.orientation].image - if self.orientation == 'AXIAL': - sx = spacing[0] - sy = spacing[1] - py = position / image.shape[1] - px = position % image.shape[1] - elif self.orientation == 'CORONAL': - sx = spacing[0] - sy = spacing[2] - py = position / image.shape[1] - px = position % image.shape[1] - elif self.orientation == 'SAGITAL': - sx = spacing[2] - sy = spacing[1] - py = position / image.shape[1] - px = position % image.shape[1] + def get_slice_pixel_coord_by_world_pos(self, wx, wy, wz): + """ + Given the (wx, wy, wz) world position returns the pixel coordinate + of the slice at (that mx, my) position. + + Parameters: + mx (int): x position. + my (int): y position + picker: the picker used to get calculate the pixel coordinate. + + Returns: + voxel_coordinate (x, y): voxel coordinate inside the matrix. Can + be used to access the voxel value inside the matrix. + """ + coord = wx, wy, wz + px, py = self.calculate_matrix_position(coord) return px, py -- libgit2 0.21.2