diff --git a/invesalius/data/slice_.py b/invesalius/data/slice_.py index 633da4a..567ea77 100644 --- a/invesalius/data/slice_.py +++ b/invesalius/data/slice_.py @@ -93,8 +93,9 @@ class Slice(object): self._type_projection = const.PROJECTION_NORMAL self.n_border = const.PROJECTION_BORDER_SIZE - self.spacing = (1.0, 1.0, 1.0) + self._spacing = (1.0, 1.0, 1.0) self.rotations = (0, 0, 0) + self.center = [0, 0, 0] self.number_of_colours = 256 self.saturation_range = (0, 0) @@ -125,6 +126,16 @@ class Slice(object): i, e = value.min(), value.max() r = int(e) - int(i) self.histogram = np.histogram(self._matrix, r, (i, e))[0] + self.center = [(s * d/2.0) for (d, s) in zip(self.matrix.shape[::-1], self.spacing)] + + @property + def spacing(self): + return self._spacing + + @spacing.setter + def spacing(self, value): + self._spacing = value + self.center = [(s * d/2.0) for (d, s) in zip(self.matrix.shape[::-1], self.spacing)] def __bind_events(self): # General slice control @@ -568,16 +579,16 @@ class Slice(object): number_slices = 1 if np.any(self.rotations): - dz, dy, dx = self.matrix.shape + cx, cy, cz = self.center rx, ry, rz = self.rotations sx, sy, sz = self.spacing - T0 = transformations.translation_matrix((-dz/2.0 * sz, -dy/2.0 * sy, -dx/2.0 * sx)) + T0 = transformations.translation_matrix((-cz, -cy, -cx)) Rx = transformations.rotation_matrix(rx, (0, 0, 1)) Ry = transformations.rotation_matrix(ry, (0, 1, 0)) Rz = transformations.rotation_matrix(rz, (1, 0, 0)) # R = transformations.euler_matrix(rz, ry, rx, 'rzyx') R = transformations.concatenate_matrices(Rx, Ry, Rz) - T1 = transformations.translation_matrix((dz/2.0 * sz, dy/2.0 * sy, dx/2.0 * sx)) + T1 = transformations.translation_matrix((cz, cy, cx)) M = transformations.concatenate_matrices(T1, R.T, T0) diff --git a/invesalius/data/styles.py b/invesalius/data/styles.py index 5da1975..4873ebd 100644 --- a/invesalius/data/styles.py +++ b/invesalius/data/styles.py @@ -1417,7 +1417,12 @@ class ReorientImageInteractorStyle(DefaultInteractorStyle): self.line1 = None self.line2 = None + self.actors = [] + + self.picker = vtk.vtkWorldPointPicker() + self.AddObserver("KeyPressEvent", self.OnKeyPress) + self.AddObserver("MouseMoveEvent", self.OnMouseMove) self.viewer.slice_data.renderer.AddObserver("StartEvent", self.OnUpdate) def SetUp(self): @@ -1425,6 +1430,10 @@ class ReorientImageInteractorStyle(DefaultInteractorStyle): self.draw_lines() Publisher.sendMessage('Reload actual slice') + def CleanUp(self): + for actor in self.actors: + self.viewer.slice_data.renderer.RemoveActor(actor) + def OnKeyPress(self, evt, obj): key = self.viewer.interactor.GetKeyCode() if key == '+': @@ -1452,10 +1461,46 @@ class ReorientImageInteractorStyle(DefaultInteractorStyle): self.viewer.slice_.current_mask.clear_history() Publisher.sendMessage('Reload actual slice') + def OnMouseMove(self, obj, evt): + """ + This event is responsible to reorient image, set mouse cursors + """ + # Getting mouse position + iren = self.viewer.interactor + mx, my = iren.GetEventPosition() + + # Getting center value + center = self.viewer.slice_.center + coord = vtk.vtkCoordinate() + coord.SetValue(center) + cx, cy = coord.GetComputedDisplayValue(self.viewer.slice_data.renderer) + + dist_center = ((mx - cx)**2 + (my - cy)**2)**0.5 + + if dist_center <= 15: + cursor = wx.StockCursor(wx.CURSOR_SIZENESW) + + if self.left_pressed: + self.picker.Pick(mx, my, 0, self.viewer.slice_data.renderer) + x, y, z = self.picker.GetPickPosition() + icx, icy, icz = self.viewer.slice_.center + + if self.viewer.orientation == 'AXIAL': + self.viewer.slice_.center = (x, y, icz) + elif self.viewer.orientation == 'CORONAL': + self.viewer.slice_.center = (x, icy, z) + elif self.viewer.orientation == 'SAGITAL': + self.viewer.slice_.center = (icx, y, z) + Publisher.sendMessage('Update slice viewer') + else: + cursor = wx.StockCursor(wx.CURSOR_DEFAULT) + + self.viewer.interactor.SetCursor(cursor) + def OnUpdate(self, obj, evt): w, h = self.viewer.slice_data.renderer.GetSize() - center = self.viewer.slice_data.actor.GetCenter() + center = self.viewer.slice_.center coord = vtk.vtkCoordinate() coord.SetValue(center) x, y = coord.GetComputedDisplayValue(self.viewer.slice_data.renderer) @@ -1488,6 +1533,8 @@ class ReorientImageInteractorStyle(DefaultInteractorStyle): self.viewer.slice_data.renderer.AddActor(actor) + self.actors.append(actor) + return line def draw_lines(self): -- libgit2 0.21.2