Commit 7f8d5bc7b184335accd755464fc8a174c9956f92

Authored by tfmoraes
1 parent 705d3adf

ENH: Created a new class to store each render, actor, slice_number

.gitattributes
@@ -87,6 +87,7 @@ invesalius/data/mask.py -text @@ -87,6 +87,7 @@ invesalius/data/mask.py -text
87 invesalius/data/orientation.py -text 87 invesalius/data/orientation.py -text
88 invesalius/data/polydata_utils.py -text 88 invesalius/data/polydata_utils.py -text
89 invesalius/data/slice_.py -text 89 invesalius/data/slice_.py -text
  90 +invesalius/data/slice_data.py -text
90 invesalius/data/styles.py -text 91 invesalius/data/styles.py -text
91 invesalius/data/surface.py -text 92 invesalius/data/surface.py -text
92 invesalius/data/viewer.py -text 93 invesalius/data/viewer.py -text
invesalius/data/slice_data.py 0 → 100644
@@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
  1 +class SliceData(object):
  2 + def __init__(self):
  3 + self.renderer = None
  4 + self.actor = None
  5 + self.number = 0
invesalius/data/viewer_slice.py
@@ -29,6 +29,8 @@ import constants as const @@ -29,6 +29,8 @@ import constants as const
29 import project 29 import project
30 import cursor_actors as ca 30 import cursor_actors as ca
31 31
  32 +from slice_data import SliceData
  33 +
32 class Viewer(wx.Panel): 34 class Viewer(wx.Panel):
33 35
34 def __init__(self, prnt, orientation='AXIAL'): 36 def __init__(self, prnt, orientation='AXIAL'):
@@ -42,10 +44,10 @@ class Viewer(wx.Panel): @@ -42,10 +44,10 @@ class Viewer(wx.Panel):
42 self.mouse_pressed = 0 44 self.mouse_pressed = 0
43 45
44 # All renderers and image actors in this viewer 46 # All renderers and image actors in this viewer
45 - self.image_windows = []  
46 - # The layout from image_window, the first is number of cols, the second 47 + self.slice_data_list = []
  48 + # The layout from slice_data, the first is number of cols, the second
47 # is the number of rows 49 # is the number of rows
48 - self.layout = (1, 1) 50 + self.layout = (2, 2)
49 51
50 self.__init_gui() 52 self.__init_gui()
51 53
@@ -96,7 +98,6 @@ class Viewer(wx.Panel): @@ -96,7 +98,6 @@ class Viewer(wx.Panel):
96 self.cam = ren.GetActiveCamera() 98 self.cam = ren.GetActiveCamera()
97 self.ren = ren 99 self.ren = ren
98 100
99 -  
100 def append_mode(self, mode): 101 def append_mode(self, mode):
101 102
102 # Retrieve currently set modes 103 # Retrieve currently set modes
@@ -161,7 +162,7 @@ class Viewer(wx.Panel): @@ -161,7 +162,7 @@ class Viewer(wx.Panel):
161 cursor = ca.CursorCircle() 162 cursor = ca.CursorCircle()
162 self.cursor = cursor 163 self.cursor = cursor
163 164
164 - cursor.set_orientation(self.orientation) 165 + cursor.SetOrientation(self.orientation)
165 coordinates = {"SAGITAL": [self.slice_number, 0, 0], 166 coordinates = {"SAGITAL": [self.slice_number, 0, 0],
166 "CORONAL": [0, self.slice_number, 0], 167 "CORONAL": [0, self.slice_number, 0],
167 "AXIAL": [0, 0, self.slice_number]} 168 "AXIAL": [0, 0, self.slice_number]}
@@ -174,7 +175,6 @@ class Viewer(wx.Panel): @@ -174,7 +175,6 @@ class Viewer(wx.Panel):
174 self.interactor.Render() 175 self.interactor.Render()
175 self.cursor = cursor 176 self.cursor = cursor
176 177
177 -  
178 def OnMouseClick(self, obj, evt_vtk): 178 def OnMouseClick(self, obj, evt_vtk):
179 self.mouse_pressed = 1 179 self.mouse_pressed = 1
180 180
@@ -186,13 +186,13 @@ class Viewer(wx.Panel): @@ -186,13 +186,13 @@ class Viewer(wx.Panel):
186 186
187 mouse_x, mouse_y = self.interactor.GetEventPosition() 187 mouse_x, mouse_y = self.interactor.GetEventPosition()
188 render = self.interactor.FindPokedRenderer(mouse_x, mouse_y) 188 render = self.interactor.FindPokedRenderer(mouse_x, mouse_y)
189 - image_window = self.get_image_window(render) 189 + slice_data = self.get_slice_data(render)
190 self.pick.Pick(mouse_x, mouse_y, 0, render) 190 self.pick.Pick(mouse_x, mouse_y, 0, render)
191 191
192 coord = self.get_coordinate_cursor() 192 coord = self.get_coordinate_cursor()
193 self.cursor.SetPosition(coord) 193 self.cursor.SetPosition(coord)
194 self.cursor.SetEditionPosition( 194 self.cursor.SetEditionPosition(
195 - self.get_coordinate_cursor_edition(image_window)) 195 + self.get_coordinate_cursor_edition(slice_data))
196 self.__update_cursor_position(coord) 196 self.__update_cursor_position(coord)
197 #render.Render() 197 #render.Render()
198 198
@@ -213,12 +213,12 @@ class Viewer(wx.Panel): @@ -213,12 +213,12 @@ class Viewer(wx.Panel):
213 def OnBrushMove(self, obj, evt_vtk): 213 def OnBrushMove(self, obj, evt_vtk):
214 mouse_x, mouse_y = self.interactor.GetEventPosition() 214 mouse_x, mouse_y = self.interactor.GetEventPosition()
215 render = self.interactor.FindPokedRenderer(mouse_x, mouse_y) 215 render = self.interactor.FindPokedRenderer(mouse_x, mouse_y)
216 - image_window = self.get_image_window(render) 216 + slice_data = self.get_slice_data(render)
217 self.pick.Pick(mouse_x, mouse_y, 0, render) 217 self.pick.Pick(mouse_x, mouse_y, 0, render)
218 coord = self.get_coordinate_cursor() 218 coord = self.get_coordinate_cursor()
219 self.cursor.SetPosition(coord) 219 self.cursor.SetPosition(coord)
220 self.cursor.SetEditionPosition( 220 self.cursor.SetEditionPosition(
221 - self.get_coordinate_cursor_edition(image_window)) 221 + self.get_coordinate_cursor_edition(slice_data))
222 self.__update_cursor_position(coord) 222 self.__update_cursor_position(coord)
223 223
224 if self._brush_cursor_op == const.BRUSH_ERASE: 224 if self._brush_cursor_op == const.BRUSH_ERASE:
@@ -249,10 +249,10 @@ class Viewer(wx.Panel): @@ -249,10 +249,10 @@ class Viewer(wx.Panel):
249 ps.Publisher().sendMessage(('Set scroll position', 'AXIAL'), 249 ps.Publisher().sendMessage(('Set scroll position', 'AXIAL'),
250 coord[2]) 250 coord[2])
251 251
252 - def get_image_window(self, render):  
253 - for i in self.image_windows:  
254 - if i[0] is render:  
255 - return i 252 + def get_slice_data(self, render):
  253 + for slice_data in self.slice_data_list:
  254 + if slice_data.renderer is render:
  255 + return slice_data
256 256
257 def get_coordinate(self): 257 def get_coordinate(self):
258 # Find position 258 # Find position
@@ -297,9 +297,10 @@ class Viewer(wx.Panel): @@ -297,9 +297,10 @@ class Viewer(wx.Panel):
297 x, y, z = self.pick.GetPickPosition() 297 x, y, z = self.pick.GetPickPosition()
298 return x, y, z 298 return x, y, z
299 299
300 - def get_coordinate_cursor_edition(self, image_window): 300 + def get_coordinate_cursor_edition(self, slice_data):
301 # Find position 301 # Find position
302 - actor, slice_number = image_window[1::] 302 + actor = slice_data.actor
  303 + slice_number = slice_data.number
303 x, y, z = self.pick.GetPickPosition() 304 x, y, z = self.pick.GetPickPosition()
304 305
305 # First we fix the position origin, based on vtkActor bounds 306 # First we fix the position origin, based on vtkActor bounds
@@ -369,10 +370,9 @@ class Viewer(wx.Panel): @@ -369,10 +370,9 @@ class Viewer(wx.Panel):
369 for j in xrange(self.layout[1]): 370 for j in xrange(self.layout[1]):
370 position = ((i*proportion_x, j * proportion_y, 371 position = ((i*proportion_x, j * proportion_y,
371 (i+1)*proportion_x, (j+1)*proportion_y)) 372 (i+1)*proportion_x, (j+1)*proportion_y))
372 - ren, actor = self.create_slice_window(image)  
373 - ren.SetViewport(position)  
374 - self.image_windows.append([ren, actor, 0])  
375 - 373 + slice_data = self.create_slice_window(image)
  374 + slice_data.renderer.SetViewport(position)
  375 + self.slice_data_list.append(slice_data)
376 376
377 def SetInput(self, imagedata): 377 def SetInput(self, imagedata):
378 self.imagedata = imagedata 378 self.imagedata = imagedata
@@ -385,11 +385,11 @@ class Viewer(wx.Panel): @@ -385,11 +385,11 @@ class Viewer(wx.Panel):
385 if slice_.imagedata is None: 385 if slice_.imagedata is None:
386 slice_.SetInput(imagedata) 386 slice_.SetInput(imagedata)
387 387
388 -  
389 #actor = vtk.vtkImageActor() 388 #actor = vtk.vtkImageActor()
390 #actor.SetInput(slice_.GetOutput()) 389 #actor.SetInput(slice_.GetOutput())
391 self.load_renderers(slice_.GetOutput()) 390 self.load_renderers(slice_.GetOutput())
392 - ren, actor = self.image_windows[0][:2] 391 + ren = self.slice_data_list[0].renderer
  392 + actor = self.slice_data_list[0].actor
393 actor_bound = actor.GetBounds() 393 actor_bound = actor.GetBounds()
394 self.actor = actor 394 self.actor = actor
395 self.ren = ren 395 self.ren = ren
@@ -411,11 +411,14 @@ class Viewer(wx.Panel): @@ -411,11 +411,14 @@ class Viewer(wx.Panel):
411 411
412 #ren.AddActor(actor) 412 #ren.AddActor(actor)
413 #ren.AddActor(text_actor) 413 #ren.AddActor(text_actor)
414 - for ren, actor, n in self.image_windows:  
415 - self.__update_camera(ren, actor, n) 414 + for slice_data in self.slice_data_list:
  415 + self.__update_camera(slice_data)
416 416
  417 + number_of_slices = self.layout[0] * self.layout[1]
417 max_slice_number = actor.GetSliceNumberMax() / \ 418 max_slice_number = actor.GetSliceNumberMax() / \
418 - (self.layout[0] * self.layout[1]) 419 + number_of_slices
  420 + if actor.GetSliceNumberMax() % number_of_slices:
  421 + max_slice_number += 1
419 self.scroll.SetScrollbar(wx.SB_VERTICAL, 1, max_slice_number, 422 self.scroll.SetScrollbar(wx.SB_VERTICAL, 1, max_slice_number,
420 max_slice_number) 423 max_slice_number)
421 self.set_scroll_position(0) 424 self.set_scroll_position(0)
@@ -459,23 +462,26 @@ class Viewer(wx.Panel): @@ -459,23 +462,26 @@ class Viewer(wx.Panel):
459 462
460 def set_orientation(self, orientation): 463 def set_orientation(self, orientation):
461 self.orientation = orientation 464 self.orientation = orientation
462 - for ren, actor, n in self.image_windows:  
463 - self.__update_camera(ren, actor, n) 465 + for slice_data in self.slice_data_list:
  466 + self.__update_camera(slice_data)
464 467
465 - def create_slice_window(self, image):  
466 - render = vtk.vtkRenderer()  
467 - self.interactor.GetRenderWindow().AddRenderer(render) 468 + def create_slice_window(self, imagedata):
  469 + renderer = vtk.vtkRenderer()
  470 + self.interactor.GetRenderWindow().AddRenderer(renderer)
468 actor = vtk.vtkImageActor() 471 actor = vtk.vtkImageActor()
469 - actor.SetInput(image)  
470 - render.AddActor(actor)  
471 - return render, actor  
472 -  
473 - def __update_camera(self, ren, actor, pos): 472 + actor.SetInput(imagedata)
  473 + renderer.AddActor(actor)
  474 + slice_data = SliceData()
  475 + slice_data.renderer = renderer
  476 + slice_data.actor = actor
  477 + return slice_data
  478 +
  479 + def __update_camera(self, slice_data):
474 orientation = self.orientation 480 orientation = self.orientation
475 proj = project.Project() 481 proj = project.Project()
476 orig_orien = proj.original_orientation 482 orig_orien = proj.original_orientation
477 483
478 - cam = ren.GetActiveCamera() 484 + cam = slice_data.renderer.GetActiveCamera()
479 cam.SetFocalPoint(0, 0, 0) 485 cam.SetFocalPoint(0, 0, 0)
480 cam.SetViewUp(const.SLICE_POSITION[orig_orien][0][self.orientation]) 486 cam.SetViewUp(const.SLICE_POSITION[orig_orien][0][self.orientation])
481 cam.SetPosition(const.SLICE_POSITION[orig_orien][1][self.orientation]) 487 cam.SetPosition(const.SLICE_POSITION[orig_orien][1][self.orientation])
@@ -483,14 +489,16 @@ class Viewer(wx.Panel): @@ -483,14 +489,16 @@ class Viewer(wx.Panel):
483 cam.OrthogonalizeViewUp() 489 cam.OrthogonalizeViewUp()
484 cam.ParallelProjectionOn() 490 cam.ParallelProjectionOn()
485 491
486 - self.__update_display_extent(actor, ren, pos) 492 + self.__update_display_extent(slice_data)
487 493
488 - ren.ResetCamera()  
489 - ren.Render() 494 + slice_data.renderer.ResetCamera()
  495 + slice_data.renderer.Render()
490 496
491 - def __update_display_extent(self, actor, render, pos): 497 + def __update_display_extent(self, slice_data):
492 e = self.imagedata.GetWholeExtent() 498 e = self.imagedata.GetWholeExtent()
493 proj = project.Project() 499 proj = project.Project()
  500 +
  501 + pos = slice_data.number
494 502
495 x = (pos, pos, e[2], e[3], e[4], e[5]) 503 x = (pos, pos, e[2], e[3], e[4], e[5])
496 y = (e[0], e[1], pos, pos, e[4], e[5]) 504 y = (e[0], e[1], pos, pos, e[4], e[5])
@@ -503,9 +511,8 @@ class Viewer(wx.Panel): @@ -503,9 +511,8 @@ class Viewer(wx.Panel):
503 elif(proj.original_orientation == const.CORONAL): 511 elif(proj.original_orientation == const.CORONAL):
504 new_extent = {"SAGITAL": x,"CORONAL": z,"AXIAL": y} 512 new_extent = {"SAGITAL": x,"CORONAL": z,"AXIAL": y}
505 513
506 - actor.SetDisplayExtent(new_extent[self.orientation])  
507 - render.ResetCameraClippingRange()  
508 - #render.Render() 514 + slice_data.actor.SetDisplayExtent(new_extent[self.orientation])
  515 + slice_data.renderer.ResetCameraClippingRange()
509 516
510 def UpdateRender(self, evt): 517 def UpdateRender(self, evt):
511 self.interactor.Render() 518 self.interactor.Render()
@@ -542,13 +549,14 @@ class Viewer(wx.Panel): @@ -542,13 +549,14 @@ class Viewer(wx.Panel):
542 def set_slice_number(self, index): 549 def set_slice_number(self, index):
543 self.text_actor.SetInput(str(index)) 550 self.text_actor.SetInput(str(index))
544 self.slice_number = index 551 self.slice_number = index
545 - for n, window in enumerate(self.image_windows):  
546 - ren, actor = window[:2] 552 + for n, slice_data in enumerate(self.slice_data_list):
  553 + ren = slice_data.renderer
  554 + actor = slice_data.actor
547 pos = self.layout[0] * self.layout[1] * index + n 555 pos = self.layout[0] * self.layout[1] * index + n
548 - print pos  
549 - self.__update_display_extent(actor, ren, pos)  
550 - self.image_windows[n][2] = pos  
551 - print 556 + max = actor.GetSliceNumberMax()
  557 + if pos < max:
  558 + slice_data.number = pos
  559 + self.__update_display_extent(slice_data)
552 560
553 position = {"SAGITAL": {0: self.slice_number}, 561 position = {"SAGITAL": {0: self.slice_number},
554 "CORONAL": {1: self.slice_number}, 562 "CORONAL": {1: self.slice_number},