Package SlicerVMTKLevelSet :: Module SlicerVMTKLevelSetGUIHelper
[hide private]
[frames] | no frames]

Source Code for Module SlicerVMTKLevelSet.SlicerVMTKLevelSetGUIHelper

  1  from Slicer import slicer 
  2  from time import strftime 
  3   
  4  from SlicerVMTKLevelSetContainer import SlicerVMTKLevelSetContainer 
  5   
  6  ### 
  7  ### Helper class for VMTK Level Set.. 
  8  ### 
9 -class SlicerVMTKLevelSetGUIHelper(object):
10 11
12 - def __init__(self,mainGUIClass):
13 14 self._mainGUIClass = mainGUIClass 15 self._isInteractiveModeActive = 0 16 self._interactiveModeHandlerClass = None 17 18 self._renderWindowInteractor = None 19 self._redSliceInteractor = None 20 self._yellowSliceInteractor = None 21 self._greenSliceInteractor = None
22
23 - def SetIsInteractiveMode(self,onoff,handlerClass):
24 self._isInteractiveModeActive = onoff 25 self._interactiveModeHandlerClass = handlerClass
26
27 - def GetIsInteractiveMode(self):
28 return self._isInteractiveModeActive
29 30
31 - def debug(self,msg):
32 33 '''debug prints to stdout (better than print because of flush)''' 34 35 # declaration of new variable without type specification 36 debugMode = 1 37 38 if debugMode: # debugMode is a bool 39 40 # the print statement needs strings as input, so every value to output has to be 41 # casted 42 print "[slicerVMTKLevelSet " + strftime("%H:%M:%S") + "] " + str(msg) 43 import sys 44 sys.stdout.flush()
45 46 ### 47 ### registers the interactors passed through from the parent class 48 ###
49 - def RegisterInteractors(self, renderWindowInteractor, redSliceInteractor, yellowSliceInteractor, greenSliceInteractor):
50 51 self._renderWindowInteractor = renderWindowInteractor 52 self._redSliceInteractor = redSliceInteractor 53 self._yellowSliceInteractor = yellowSliceInteractor 54 self._greenSliceInteractor = greenSliceInteractor
55 56 ### 57 ### click handlers for the 3D render window 58 ###
60 61 if self._isInteractiveModeActive: 62 63 self.debug("Clicked in 3D") 64 65 coordinates = self._renderWindowInteractor.GetLastEventPosition() 66 self.debug(coordinates[0]) 67 self.debug(coordinates[1])
68 69 70 ### 71 ### click handlers for the slices 72 ###
74 self.HandleClickInSliceWindow("Red",self._redSliceInteractor)
75
77 self.HandleClickInSliceWindow("Yellow",self._yellowSliceInteractor)
78
80 self.HandleClickInSliceWindow("Green",self._greenSliceInteractor)
81
82 - def HandleClickInSliceWindow(self,which,interactor):
83 84 if self._isInteractiveModeActive: 85 86 self.debug("Clicked on " +which + "Slice") 87 88 coordinates = interactor.GetLastEventPosition() 89 90 self.debug(coordinates[0]) 91 self.debug(coordinates[1]) 92 93 self._interactiveModeHandlerClass.HandleClickInSliceWindowWithCoordinates(which, coordinates)
94 95 96 ### 97 ### convert coordinates to RAS 98 ###
99 - def ConvertCoordinates2RAS(self,which,coordinates):
100 # convert to RAS 101 inPt = [coordinates[0], coordinates[1], 0, 1] 102 matrixRAS = slicer.ApplicationGUI.GetMainSliceGUI(which).GetLogic().GetSliceNode().GetXYToRAS() 103 rasPt = matrixRAS.MultiplyPoint(*inPt) 104 return rasPt
105 106 ### 107 ### convert coordinates to IJK 108 ###
109 - def ConvertCoordinates2IJK(self,which,coordinates):
110 #need RAS 111 rasPt = self.ConvertCoordinates2RAS(which,coordinates) 112 113 #convert to IJK 114 node = self._mainGUIClass.GetScriptedModuleNode() 115 if node: 116 inVolume = node.GetParameter('InputVolumeRef') 117 if not inVolume: 118 slicer.Application.ErrorMessage("No input volume found\n") 119 return 120 else: 121 matrixIJK = slicer.vtkMatrix4x4() 122 inVolume.GetRASToIJKMatrix(matrixIJK) 123 ijkPt = matrixIJK.MultiplyPoint(*rasPt) 124 return ijkPt
125 126 ### 127 ### convert RAS to IJK 128 ###
129 - def ConvertRAS2IJK(self,coordinates):
130 131 rasPt = coordinates 132 rasPt.append(1) 133 134 #convert to IJK 135 node = self._mainGUIClass.GetScriptedModuleNode() 136 if node: 137 inVolume = node.GetParameter('InputVolumeRef') 138 if not inVolume: 139 slicer.Application.ErrorMessage("No input volume found\n") 140 return 141 else: 142 matrixIJK = slicer.vtkMatrix4x4() 143 inVolume.GetRASToIJKMatrix(matrixIJK) 144 #self.debug(matrixIJK) 145 ijkPt = matrixIJK.MultiplyPoint(*rasPt) 146 return ijkPt
147
148 - def SetAndMergeInitVolume(self,resultContainer):
149 150 scene = self._mainGUIClass.GetLogic().GetMRMLScene() 151 152 volumeNode = resultContainer.GetNode() 153 threshold = resultContainer.GetThreshold() 154 155 if self._mainGUIClass._outInitVolume == None: 156 157 # no node so far 158 159 self._mainGUIClass._outInitVolume = slicer.vtkMRMLScalarVolumeNode() 160 self._mainGUIClass._outInitVolume.SetName("VMTK Level-Set Initialization Output Volume") 161 self._mainGUIClass._outInitVolume.SetScene(scene) 162 self._mainGUIClass._outInitVolume.SetAndObserveImageData(slicer.vtkImageData()) 163 scene.AddNode(self._mainGUIClass._outInitVolume) 164 165 self._mainGUIClass._outInitVolumeLast = slicer.vtkMRMLScalarVolumeNode() 166 self._mainGUIClass._outInitVolumeLast.SetName("VMTK Level-Set Initialization Output Volume (Last Step)") 167 self._mainGUIClass._outInitVolumeLast.SetScene(scene) 168 scene.AddNode(self._mainGUIClass._outInitVolumeLast) 169 170 matrix = slicer.vtkMatrix4x4() 171 172 # copy current outInitVolume to outInitVolumeLast 173 self._mainGUIClass._outInitVolumeLast.SetAndObserveImageData(self._mainGUIClass._outInitVolume.GetImageData()) 174 self._mainGUIClass._outInitVolume.GetIJKToRASMatrix(matrix) 175 self._mainGUIClass._outInitVolumeLast.SetIJKToRASMatrix(matrix) 176 self._mainGUIClass._outInitVolumeLast.SetModifiedSinceRead(1) 177 178 volumeNodeData = volumeNode.GetImageData() 179 180 # merge the oldVolume with volumeNode if oldVolume has already content 181 if self._mainGUIClass._outInitVolume.GetImageData().GetPointData().GetScalars(): 182 # oldVolume has already content 183 minFilter = slicer.vtkImageMathematics() 184 minFilter.SetOperationToMin() 185 minFilter.SetInput1(self._mainGUIClass._outInitVolume.GetImageData()) #the old one 186 minFilter.SetInput2(volumeNode.GetImageData()) #the new one 187 minFilter.Update() 188 volumeNodeData.DeepCopy(minFilter.GetOutput()) 189 190 # copy new volume to outInitVolume 191 volumeNode.GetIJKToRASMatrix(matrix) 192 self._mainGUIClass._outInitVolume.SetAndObserveImageData(volumeNodeData) 193 self._mainGUIClass._outInitVolume.SetIJKToRASMatrix(matrix) 194 self._mainGUIClass._outInitVolume.SetModifiedSinceRead(1) 195 196 outputContainer = SlicerVMTKLevelSetContainer(self._mainGUIClass._outInitVolume,threshold) 197 198 return outputContainer
199
200 - def UndoInit(self):
201 202 # copy outInitVolumeLast to outInitVolume 203 204 matrix = slicer.vtkMatrix4x4() 205 self._mainGUIClass._outInitVolumeLast.GetIJKToRASMatrix(matrix) 206 207 self._mainGUIClass._outInitVolume.SetAndObserveImageData(self._mainGUIClass._outInitVolumeLast.GetImageData()) 208 self._mainGUIClass._outInitVolume.SetIJKToRASMatrix(matrix) 209 self._mainGUIClass._outInitVolume.SetModifiedSinceRead(1) 210 211 212 outputContainer = SlicerVMTKLevelSetContainer(self._mainGUIClass._outInitVolume,0.0) 213 214 return outputContainer
215 216
217 - def SetAndMergeEvolVolume(self,resultContainer):
218 219 scene = self._mainGUIClass.GetLogic().GetMRMLScene() 220 221 volumeNode = resultContainer.GetNode() 222 threshold = resultContainer.GetThreshold() 223 224 if self._mainGUIClass._outEvolVolume == None: 225 226 # no node so far 227 228 self._mainGUIClass._outEvolVolume = slicer.vtkMRMLScalarVolumeNode() 229 self._mainGUIClass._outEvolVolume.SetName("VMTK Level-Set Evolution Output Volume") 230 self._mainGUIClass._outEvolVolume.SetScene(scene) 231 self._mainGUIClass._outEvolVolume.SetAndObserveImageData(slicer.vtkImageData()) 232 scene.AddNode(self._mainGUIClass._outEvolVolume) 233 234 self._mainGUIClass._outEvolVolumeLast = slicer.vtkMRMLScalarVolumeNode() 235 self._mainGUIClass._outEvolVolumeLast.SetName("VMTK Level-Set Evolution Output Volume (Last Step)") 236 self._mainGUIClass._outEvolVolumeLast.SetScene(scene) 237 self._mainGUIClass._outEvolVolumeLast.SetAndObserveImageData(slicer.vtkImageData()) 238 scene.AddNode(self._mainGUIClass._outEvolVolumeLast) 239 240 matrix = slicer.vtkMatrix4x4() 241 242 # copy current outEvolVolume to outEvolVolumeLast 243 self._mainGUIClass._outEvolVolumeLast.SetAndObserveImageData(self._mainGUIClass._outEvolVolume.GetImageData()) 244 self._mainGUIClass._outEvolVolume.GetIJKToRASMatrix(matrix) 245 self._mainGUIClass._outEvolVolumeLast.SetIJKToRASMatrix(matrix) 246 self._mainGUIClass._outEvolVolumeLast.SetModifiedSinceRead(1) 247 248 volumeNodeData = volumeNode.GetImageData() 249 250 # merge the oldVolume with volumeNode if oldVolume has already content 251 if self._mainGUIClass._outEvolVolume.GetImageData().GetPointData().GetScalars(): 252 # evolVolumeLast has already content 253 minFilter = slicer.vtkImageMathematics() 254 minFilter.SetOperationToMin() 255 minFilter.SetInput1(self._mainGUIClass._outEvolVolume.GetImageData()) #the old one 256 minFilter.SetInput2(volumeNode.GetImageData()) #the new one 257 minFilter.Update() 258 volumeNodeData.DeepCopy(minFilter.GetOutput()) 259 260 # copy new volume to outEvolVolume 261 volumeNode.GetIJKToRASMatrix(matrix) 262 self._mainGUIClass._outEvolVolume.SetAndObserveImageData(volumeNodeData) 263 self._mainGUIClass._outEvolVolume.SetIJKToRASMatrix(matrix) 264 self._mainGUIClass._outEvolVolume.SetModifiedSinceRead(1) 265 266 outputContainer = SlicerVMTKLevelSetContainer(self._mainGUIClass._outEvolVolume,threshold) 267 268 return outputContainer
269 270
271 - def UndoEvol(self):
272 273 # copy outEvolVolumeLast to outEvolVolume 274 275 matrix = slicer.vtkMatrix4x4() 276 self._mainGUIClass._outEvolVolumeLast.GetIJKToRASMatrix(matrix) 277 278 self._mainGUIClass._outEvolVolume.SetAndObserveImageData(self._mainGUIClass._outEvolVolumeLast.GetImageData()) 279 self._mainGUIClass._outEvolVolume.SetIJKToRASMatrix(matrix) 280 self._mainGUIClass._outEvolVolume.SetModifiedSinceRead(1) 281 282 283 outputContainer = SlicerVMTKLevelSetContainer(self._mainGUIClass._outEvolVolume,0.0) 284 285 return outputContainer
286 287 288
289 - def GenerateInitializationModel(self,resultContainer):
290 291 matrix = slicer.vtkMatrix4x4() 292 293 image = resultContainer.GetNode().GetImageData() 294 295 resultContainer.GetNode().GetIJKToRASMatrix(matrix) 296 threshold = resultContainer.GetThreshold() 297 298 polyData = slicer.vtkPolyData() 299 300 if image.GetPointData().GetScalars(): 301 # marching Cubes, only if image has content 302 polyData.DeepCopy(self._mainGUIClass.GetMyLogic().MarchingCubes(image,matrix,threshold)) 303 304 scene = self._mainGUIClass.GetLogic().GetMRMLScene() 305 306 if self._mainGUIClass._outInitModel == None: 307 # no node so far 308 self._mainGUIClass._outInitModel = slicer.vtkMRMLModelNode() 309 self._mainGUIClass._outInitModel.SetName("VMTK Level-Set Initialization Output Model") 310 self._mainGUIClass._outInitModel.SetAndObservePolyData(slicer.vtkPolyData()) 311 self._mainGUIClass._outInitModel.SetScene(scene) 312 scene.AddNode(self._mainGUIClass._outInitModel) 313 314 self._mainGUIClass._outInitModel.SetAndObservePolyData(polyData) 315 self._mainGUIClass._outInitModel.SetModifiedSinceRead(1) 316 317 if self._mainGUIClass._outInitModelDisplay!=None: 318 scene.RemoveNode(self._mainGUIClass._outInitModelDisplay) 319 320 self._mainGUIClass._outInitModelDisplay = slicer.vtkMRMLModelDisplayNode() 321 self._mainGUIClass._outInitModelDisplay.SetPolyData(self._mainGUIClass._outInitModel.GetPolyData()) 322 self._mainGUIClass._outInitModelDisplay.SetColor(0.8, 0.0, 0.0) 323 self._mainGUIClass._outInitModelDisplay.SetSliceIntersectionVisibility(1) 324 self._mainGUIClass._outInitModelDisplay.SetVisibility(1) 325 self._mainGUIClass._outInitModelDisplay.SetOpacity(0.5) 326 scene.AddNode(self._mainGUIClass._outInitModelDisplay) 327 328 self._mainGUIClass._outInitModel.SetAndObserveDisplayNodeID(self._mainGUIClass._outInitModelDisplay.GetID())
329 330
331 - def GenerateEvolutionModel(self,resultContainer):
332 333 matrix = slicer.vtkMatrix4x4() 334 335 image = resultContainer.GetNode().GetImageData() 336 337 resultContainer.GetNode().GetIJKToRASMatrix(matrix) 338 threshold = 0.0 339 340 polyData = slicer.vtkPolyData() 341 342 if image.GetPointData().GetScalars(): 343 # marching Cubes, only if image has content 344 polyData.DeepCopy(self._mainGUIClass.GetMyLogic().MarchingCubes(image,matrix,threshold)) 345 346 scene = self._mainGUIClass.GetLogic().GetMRMLScene() 347 if self._mainGUIClass._outEvolModel == None: 348 349 # no node so far 350 self._mainGUIClass._outEvolModel = slicer.vtkMRMLModelNode() 351 self._mainGUIClass._outEvolModel.SetName("VMTK Level-Set Evolution Output Model") 352 self._mainGUIClass._outEvolModel.SetAndObservePolyData(slicer.vtkPolyData()) 353 self._mainGUIClass._outEvolModel.SetScene(scene) 354 scene.AddNode(self._mainGUIClass._outEvolModel) 355 356 self._mainGUIClass._outEvolModelDisplay = slicer.vtkMRMLModelDisplayNode() 357 self._mainGUIClass._outEvolModelDisplay.SetColor(0.0, 0.0, 0.8) 358 self._mainGUIClass._outEvolModelDisplay.SetPolyData(slicer.vtkPolyData()) 359 self._mainGUIClass._outEvolModelDisplay.SetVisibility(1) 360 self._mainGUIClass._outEvolModelDisplay.SetOpacity(0.5) 361 self._mainGUIClass._outEvolModelDisplay.SetSliceIntersectionVisibility(1) 362 363 364 self._mainGUIClass._outEvolModel.SetAndObservePolyData(polyData) 365 self._mainGUIClass._outEvolModel.SetModifiedSinceRead(1) 366 self._mainGUIClass._outEvolModelDisplay.SetPolyData(self._mainGUIClass._outEvolModel.GetPolyData()) 367 self._mainGUIClass._outEvolModelDisplay.SetSliceIntersectionVisibility(1) 368 self._mainGUIClass._outEvolModelDisplay.SetVisibility(1) 369 self._mainGUIClass._outEvolModelDisplay.SetOpacity(0.5) 370 scene.AddNode(self._mainGUIClass._outEvolModelDisplay) 371 372 self._mainGUIClass._outEvolModel.SetAndObserveDisplayNodeID(self._mainGUIClass._outEvolModelDisplay.GetID()) 373 self._mainGUIClass._outEvolModelDisplay.SetSliceIntersectionVisibility(1) 374 self._mainGUIClass._outEvolModelDisplay.SetVisibility(1) 375 self._mainGUIClass._outEvolModelDisplay.SetOpacity(0.5)
376