def SetAndMergeEvolVolume(self,resultContainer):

        scene = self._mainGUIClass.GetLogic().GetMRMLScene()

        volumeNode = resultContainer.GetNode()
        threshold = resultContainer.GetThreshold()

        if self._mainGUIClass._outEvolVolume == None:

            # no node so far

            self._mainGUIClass._outEvolVolume = slicer.vtkMRMLScalarVolumeNode()
            self._mainGUIClass._outEvolVolume.SetName("VMTK Level-Set Evolution Output Volume")
            self._mainGUIClass._outEvolVolume.SetScene(scene)
            self._mainGUIClass._outEvolVolume.SetAndObserveImageData(slicer.vtkImageData())
            scene.AddNode(self._mainGUIClass._outEvolVolume)

            self._mainGUIClass._outEvolVolumeLast = slicer.vtkMRMLScalarVolumeNode()
            self._mainGUIClass._outEvolVolumeLast.SetName("VMTK Level-Set Evolution Output Volume (Last Step)")
            self._mainGUIClass._outEvolVolumeLast.SetScene(scene)
            self._mainGUIClass._outEvolVolumeLast.SetAndObserveImageData(slicer.vtkImageData())
            scene.AddNode(self._mainGUIClass._outEvolVolumeLast)

        matrix = slicer.vtkMatrix4x4()

        # copy current outEvolVolume to outEvolVolumeLast
        self._mainGUIClass._outEvolVolumeLast.SetAndObserveImageData(self._mainGUIClass._outEvolVolume.GetImageData())
        self._mainGUIClass._outEvolVolume.GetIJKToRASMatrix(matrix)
        self._mainGUIClass._outEvolVolumeLast.SetIJKToRASMatrix(matrix)
        self._mainGUIClass._outEvolVolumeLast.SetModifiedSinceRead(1)

        volumeNodeData = volumeNode.GetImageData()

        # merge the oldVolume with volumeNode if oldVolume has already content
        if self._mainGUIClass._outEvolVolume.GetImageData().GetPointData().GetScalars():
            # evolVolumeLast has already content
            minFilter = slicer.vtkImageMathematics()
            minFilter.SetOperationToMin()
            minFilter.SetInput1(self._mainGUIClass._outEvolVolume.GetImageData()) #the old one
            minFilter.SetInput2(volumeNode.GetImageData()) #the new one
            minFilter.Update()
            volumeNodeData.DeepCopy(minFilter.GetOutput())

        # copy new volume to outEvolVolume
        volumeNode.GetIJKToRASMatrix(matrix)
        self._mainGUIClass._outEvolVolume.SetAndObserveImageData(volumeNodeData)
        self._mainGUIClass._outEvolVolume.SetIJKToRASMatrix(matrix)
        self._mainGUIClass._outEvolVolume.SetModifiedSinceRead(1)

        outputContainer = SlicerVMTKLevelSetContainer(self._mainGUIClass._outEvolVolume,threshold)

        return outputContainer
Exemplo n.º 2
0
def dispV0(dataD, inputVolume, shpV):
    scene = slicer.MRMLScene

    r1 = slicer.vtkMRMLScalarVolumeNode()
    r11 = slicer.vtkMRMLScalarVolumeDisplayNode()
    scene.AddNode(r11)

    r1.AddAndObserveDisplayNodeID(r11.GetName())


    imgD = slicer.vtkImageData()
    imgD.SetDimensions(shpV[0], shpV[1], shpV[2])
    imgD.SetScalarTypeToShort()

    org = inputVolume.GetOrigin()
    spa = inputVolume.GetSpacing()

    mat = slicer.vtkMatrix4x4()
    inputVolume.GetIJKToRASMatrix(mat)

    r1.SetAndObserveImageData(imgD)
    r1.SetIJKToRASMatrix(mat)
    r1.SetOrigin(org[0], org[1], org[2])
    r1.SetSpacing(spa[0], spa[1], spa[2])

    scene.AddNode(r1)

    tmp = r1.GetImageData().ToArray()
    tmp[:] = dataD[:] 

    r1.GetDisplayNode().SetDefaultColorMap()
    r1.Modified()

    return
Exemplo n.º 3
0
def dispUV(dataD, org, spa, mat, shpV):
    scene = slicer.MRMLScene

    r1 = slicer.vtkMRMLScalarVolumeNode()
    r11 = slicer.vtkMRMLScalarVolumeDisplayNode()
    scene.AddNode(r11)

    r1.AddAndObserveDisplayNodeID(r11.GetName())


    imgD = slicer.vtkImageData()
    imgD.SetDimensions(shpV[0], shpV[1], shpV[2])
    imgD.SetScalarTypeToUnsignedShort()


    r1.SetAndObserveImageData(imgD)
    r1.SetIJKToRASMatrix(mat)
    r1.SetOrigin(org[0], org[1], org[2])
    r1.SetSpacing(spa[0], spa[1], spa[2])

    scene.AddNode(r1)

    tmp = r1.GetImageData().ToArray()
    print 'tmp   : ', tmp.shape
    print 'dataD : ', dataD.shape
    tmp[:] = dataD[:] 

    r1.GetDisplayNode().SetDefaultColorMap()
    r1.Modified()

    return
    def ExecuteSeeds(self,inVolumeNode,sourceSeedsNode):


        self._helper.debug("Starting execution of seeds...")

        if not inVolumeNode or not sourceSeedsNode:
            slicer.Application.ErrorMessage("Not enough information. Aborting Seeds..\n")
            return
        else:

            image = inVolumeNode.GetImageData()

            cast = slicer.vtkImageCast()
            cast.SetInput(image)
            cast.SetOutputScalarTypeToFloat()
            cast.Update()
            image = cast.GetOutput()

            seeds = sourceSeedsNode
    
            initialLevelSets = slicer.vtkImageData()
            initialLevelSets.DeepCopy(image)
            initialLevelSets.Update()

            levelSetsInputScalars = initialLevelSets.GetPointData().GetScalars()
            levelSetsInputScalars.FillComponent(0,1.0)

            for i in range(seeds.GetNumberOfFiducials()):
                rasPt = seeds.GetNthFiducialXYZ(i)
                ijkPt = self._helper.ConvertRAS2IJK(rasPt)
                id = image.ComputePointId(int(ijkPt[0]),int(ijkPt[1]),int(ijkPt[2]))
                levelSetsInputScalars.SetComponent(id,0,-1.0)

            dilateErode = slicer.vtkImageDilateErode3D()
            dilateErode.SetInput(initialLevelSets)
            dilateErode.SetDilateValue(-1.0)
            dilateErode.SetErodeValue(1.0)
            dilateErode.SetKernelSize(3,3,3)
            dilateErode.Update()

            matrix = slicer.vtkMatrix4x4()
            inVolumeNode.GetIJKToRASMatrix(matrix)

            outVolumeData = slicer.vtkImageData()
            outVolumeData.DeepCopy(dilateErode.GetOutput())
            outVolumeData.Update()

            # volume calculated...

            outVolumeNode = slicer.vtkMRMLScalarVolumeNode()
            outVolumeNode.SetAndObserveImageData(outVolumeData)
            outVolumeNode.SetIJKToRASMatrix(matrix)

            outputContainer = SlicerVMTKLevelSetContainer(outVolumeNode,0.0)

            self._helper.debug("Seeds done...")

            return outputContainer
    def ExecuteCurves(self,origInVolumeNode,inVolumeNode,numberOfIterations,propagationScaling,curvatureScaling,advectionScaling,calculateFeatureImage):

        self._helper.debug("Starting execution of Curves..")

        if not inVolumeNode or not origInVolumeNode:
            slicer.Application.ErrorMessage("Not enough information!!! Aborting Geodesic..\n")
            return
        else:

            cast = slicer.vtkImageCast()
            cast.SetInput(origInVolumeNode.GetImageData())
            cast.SetOutputScalarTypeToFloat()
            cast.Update()

            origImage = cast.GetOutput()
            image = inVolumeNode.GetImageData()

            levelSets = slicer.vtkvmtkCurvesLevelSetImageFilter()
            self._helper.debug("FeatureImageCalc: " + str(calculateFeatureImage))
            if calculateFeatureImage==1:
                levelSets.SetFeatureImage(self.BuildGradientBasedFeatureImage(origImage))
            else:
                levelSets.SetFeatureImage(origImage)
            levelSets.SetDerivativeSigma(self.FeatureDerivativeSigma)
            levelSets.SetAutoGenerateSpeedAdvection(1)
            levelSets.SetPropagationScaling(propagationScaling)
            levelSets.SetCurvatureScaling(curvatureScaling)
            levelSets.SetAdvectionScaling(advectionScaling)


            levelSets.SetInput(image)
            levelSets.SetNumberOfIterations(numberOfIterations)
            levelSets.SetIsoSurfaceValue(self.IsoSurfaceValue)
            levelSets.SetMaximumRMSError(self.MaximumRMSError)
            levelSets.SetInterpolateSurfaceLocation(1)
            levelSets.SetUseImageSpacing(1)
            levelSets.Update()


            matrix = slicer.vtkMatrix4x4()
            inVolumeNode.GetIJKToRASMatrix(matrix)

            outVolumeData = slicer.vtkImageData()
            outVolumeData.DeepCopy(levelSets.GetOutput())
            outVolumeData.Update()

            outVolumeNode = slicer.vtkMRMLScalarVolumeNode()
            outVolumeNode.SetAndObserveImageData(outVolumeData)
            outVolumeNode.SetIJKToRASMatrix(matrix)

            outputContainer = SlicerVMTKLevelSetContainer(outVolumeNode,0.0)

            return outputContainer
    def ExecuteThreshold(self,inVolumeNode,lowerThreshold,higherThreshold):


        self._helper.debug("Starting execution of Threshold...")


        if not inVolumeNode:
            slicer.Application.ErrorMessage("No input volume found. Aborting Threshold..\n")
            return
        else:

            image = inVolumeNode.GetImageData()

            cast = slicer.vtkImageCast()
            cast.SetInput(image)
            cast.SetOutputScalarTypeToFloat()
            cast.Update()
            image = cast.GetOutput()

            scalarRange = image.GetScalarRange()


            threshold = slicer.vtkImageThreshold()
            threshold.SetInput(image)

            threshold.ThresholdBetween(lowerThreshold,higherThreshold)

            threshold.ReplaceInOff()
            threshold.ReplaceOutOn()
            threshold.SetInValue(-1.0)
            threshold.SetOutValue(1.0)
            threshold.Update()

            matrix = slicer.vtkMatrix4x4()
            inVolumeNode.GetIJKToRASMatrix(matrix)

            outVolumeData = slicer.vtkImageData()
            outVolumeData.DeepCopy(threshold.GetOutput())
            outVolumeData.Update()

            # volume calculated...

            outVolumeNode = slicer.vtkMRMLScalarVolumeNode()
            outVolumeNode.SetAndObserveImageData(outVolumeData)
            outVolumeNode.SetIJKToRASMatrix(matrix)

            outputContainer = SlicerVMTKLevelSetContainer(outVolumeNode,0.0)

            self._helper.debug("Threshold done...")
    
            return outputContainer
    def ExecuteIsosurface(self,inVolumeNode,value):


        self._helper.debug("Starting execution of Isosurface...")

        if not inVolumeNode:
            slicer.Application.ErrorMessage("No input volume found. Aborting Isosurface..\n")
            return
        else:

            image = inVolumeNode.GetImageData()

            cast = slicer.vtkImageCast()
            cast.SetInput(image)
            cast.SetOutputScalarTypeToFloat()
            cast.Update()
            image = cast.GetOutput()

            imageMathematics = slicer.vtkImageMathematics()
            imageMathematics.SetInput(image)
            imageMathematics.SetConstantK(-1.0)
            imageMathematics.SetOperationToMultiplyByK()
            imageMathematics.Update()

            subtract = slicer.vtkImageMathematics()
            subtract.SetInput(imageMathematics.GetOutput())
            subtract.SetOperationToAddConstant()
            subtract.SetConstantC(value)
            subtract.Update()

            matrix = slicer.vtkMatrix4x4()
            inVolumeNode.GetIJKToRASMatrix(matrix)

            outVolumeData = slicer.vtkImageData()
            outVolumeData.DeepCopy(subtract.GetOutput())

            outVolumeData.Update()

            # volume calculated...

            outVolumeNode = slicer.vtkMRMLScalarVolumeNode()
            outVolumeNode.SetAndObserveImageData(outVolumeData)
            outVolumeNode.SetIJKToRASMatrix(matrix)

            outputContainer = SlicerVMTKLevelSetContainer(outVolumeNode,10)

            self._helper.debug("Isosurface done...")

            return outputContainer
Exemplo n.º 8
0
def dispS(dataD, inputVolume, shpV):
    scene = slicer.MRMLScene

    r1 = slicer.vtkMRMLScalarVolumeNode()
    r11 = slicer.vtkMRMLScalarVolumeDisplayNode()
    r11.ScalarVisibilityOn()
    r11.SetScalarRange(dataD.min(), dataD.max())
    scene.AddNode(r11)

    r1.AddAndObserveDisplayNodeID(r11.GetName())

    imgD = slicer.vtkImageData()
    imgD.SetDimensions(shpV[0], shpV[1], shpV[2])
    imgD.SetNumberOfScalarComponents(1)
    imgD.SetScalarTypeToDouble()
    imgD.AllocateScalars()

    org = inputVolume.GetOrigin()
    spa = inputVolume.GetSpacing()

    mat = slicer.vtkMatrix4x4()
    inputVolume.GetIJKToRASMatrix(mat)

    r1.SetAndObserveImageData(imgD)
    r1.SetIJKToRASMatrix(mat)
    r1.SetOrigin(org[0], org[1], org[2])
    r1.SetSpacing(spa[0], spa[1], spa[2])

    scene.AddNode(r1)

    tmp = r1.GetImageData().GetPointData().GetScalars().ToArray()
    print 'tmp   : ', tmp.shape
    print 'dataD : ', dataD.shape
    dataD = reshape(dataD, (shpV[0]*shpV[1]*shpV[2], 1))
    tmp[:] = dataD[:]

    r1.GetDisplayNode().SetDefaultColorMap()
    r1.Modified()

    return
    def ExecuteCollidingFronts(self,inVolumeNode,lowerThreshold,higherThreshold,sourceSeedsNode,targetSeedsNode):

        self._helper.debug("Starting execution of Colliding Fronts..")

        if not inVolumeNode or not sourceSeedsNode or not targetSeedsNode:
            self._helper.debug(inVolumeNode)
            self._helper.debug(lowerThreshold)         
            self._helper.debug(higherThreshold)  
            self._helper.debug(sourceSeedsNode)  
            self._helper.debug(targetSeedsNode)         
            slicer.Application.ErrorMessage("Not enough information!!! Aborting Colliding Fronts..\n")
            return
        else:

            sourceSeedIds = slicer.vtkIdList()
            targetSeedIds = slicer.vtkIdList()

            image = inVolumeNode.GetImageData()

            cast = slicer.vtkImageCast()
            cast.SetInput(image)
            cast.SetOutputScalarTypeToFloat()
            cast.Update()
            image = cast.GetOutput()

            rasPt = sourceSeedsNode.GetNthFiducialXYZ(0)
            self._helper.debug(rasPt)
            ijkPt = self._helper.ConvertRAS2IJK(rasPt)
            self._helper.debug(ijkPt)
            sourceSeedIds.InsertNextId(image.ComputePointId(int(ijkPt[0]),int(ijkPt[1]),int(ijkPt[2])))

            rasPt = targetSeedsNode.GetNthFiducialXYZ(0)
            self._helper.debug(rasPt)
            ijkPt = self._helper.ConvertRAS2IJK(rasPt)
            self._helper.debug(ijkPt)
            targetSeedIds.InsertNextId(image.ComputePointId(int(ijkPt[0]),int(ijkPt[1]),int(ijkPt[2])))

            scalarRange = image.GetScalarRange()
            self._helper.debug("CF: after converting seeds")

            threshold = slicer.vtkImageThreshold()
            threshold.SetInput(image)
            threshold.ThresholdBetween(lowerThreshold, higherThreshold)
            threshold.ReplaceInOff()
            threshold.ReplaceOutOn()
            threshold.SetOutValue(scalarRange[0] - scalarRange[1])
            threshold.Update()

            self._helper.debug("CF: after thresholding")
    
            scalarRange = threshold.GetOutput().GetScalarRange()

            thresholdedImage = threshold.GetOutput()

            shiftScale = slicer.vtkImageShiftScale()
            shiftScale.SetInput(thresholdedImage)
            shiftScale.SetShift(-scalarRange[0])
            shiftScale.SetScale(1/(scalarRange[1]-scalarRange[0]))
            shiftScale.Update()
            
            speedImage = shiftScale.GetOutput()

            self._helper.debug("CF: after shiftScale")            

            collidingFronts = slicer.vtkvmtkCollidingFrontsImageFilter()
            collidingFronts.SetInput(speedImage)
            collidingFronts.SetSeeds1(sourceSeedIds)
            collidingFronts.SetSeeds2(targetSeedIds)
            collidingFronts.ApplyConnectivityOn()
            collidingFronts.StopOnTargetsOn()
            collidingFronts.Update()

            self._helper.debug("CF: after CF")

            subtract = slicer.vtkImageMathematics()
            subtract.SetInput(collidingFronts.GetOutput())
            subtract.SetOperationToAddConstant()
            subtract.SetConstantC(-10.0 * collidingFronts.GetNegativeEpsilon())
            subtract.Update()

            self._helper.debug("CF: after substract")

            matrix = slicer.vtkMatrix4x4()
            inVolumeNode.GetIJKToRASMatrix(matrix)

            outVolumeData = slicer.vtkImageData()
            outVolumeData.DeepCopy(subtract.GetOutput())
            outVolumeData.Update()

            # volume calculated...

            outVolumeNode = slicer.vtkMRMLScalarVolumeNode()
            outVolumeNode.SetAndObserveImageData(outVolumeData)
            outVolumeNode.SetIJKToRASMatrix(matrix)

            outputContainer = SlicerVMTKLevelSetContainer(outVolumeNode,collidingFronts.GetNegativeEpsilon())

            self._helper.debug("Colliding Fronts done...")

            return outputContainer
    def ExecuteFastMarching(self,inVolumeNode,lowerThreshold,higherThreshold,sourceSeedsNode,targetSeedsNode):

        self._helper.debug("Starting execution of Fast Marching...")

        if not inVolumeNode or not sourceSeedsNode:
            self._helper.debug(inVolumeNode)
            self._helper.debug(lowerThreshold)         
            self._helper.debug(higherThreshold)  
            self._helper.debug(sourceSeedsNode)  
            self._helper.debug(targetSeedsNode)         
            slicer.Application.ErrorMessage("Not enough information!!! Aborting Fast Marching..\n")
            return
        else:

            sourceSeedIds = slicer.vtkIdList()
            targetSeedIds = slicer.vtkIdList()

            image = inVolumeNode.GetImageData()

            cast = slicer.vtkImageCast()
            cast.SetInput(image)
            cast.SetOutputScalarTypeToFloat()
            cast.Update()
            image = cast.GetOutput()

            for i in range(sourceSeedsNode.GetNumberOfFiducials()):
                rasPt = sourceSeedsNode.GetNthFiducialXYZ(i)
                ijkPt = self._helper.ConvertRAS2IJK(rasPt)
                sourceSeedIds.InsertNextId(image.ComputePointId(int(ijkPt[0]),int(ijkPt[1]),int(ijkPt[2])))

            if targetSeedsNode:
                for i in range(targetSeedsNode.GetNumberOfFiducials()):
                    rasPt = targetSeedsNode.GetNthFiducialXYZ(i)
                    ijkPt = self._helper.ConvertRAS2IJK(rasPt)
                    targetSeedIds.InsertNextId(image.ComputePointId(int(ijkPt[0]),int(ijkPt[1]),int(ijkPt[2])))

            scalarRange = image.GetScalarRange()

            threshold = slicer.vtkImageThreshold()
            threshold.SetInput(image)
            threshold.ThresholdBetween(lowerThreshold,higherThreshold)
            threshold.ReplaceInOff()
            threshold.ReplaceOutOn()
            threshold.SetOutValue(scalarRange[0] - scalarRange[1])
            threshold.Update()

            scalarRange = threshold.GetOutput().GetScalarRange()

            thresholdedImage = threshold.GetOutput()

            shiftScale = slicer.vtkImageShiftScale()
            shiftScale.SetInput(thresholdedImage)
            shiftScale.SetShift(-scalarRange[0])
            shiftScale.SetScale(1/(scalarRange[1]-scalarRange[0]))
            shiftScale.Update()

            speedImage = shiftScale.GetOutput()

            fastMarching = slicer.vtkvmtkFastMarchingUpwindGradientImageFilter()
            fastMarching.SetInput(speedImage)
            fastMarching.SetSeeds(sourceSeedIds)
            fastMarching.GenerateGradientImageOff()
            fastMarching.SetTargetOffset(100.0)
            fastMarching.SetTargets(targetSeedIds)
            if targetSeedIds.GetNumberOfIds() > 0:
                fastMarching.SetTargetReachedModeToOneTarget()
            else:
                fastMarching.SetTargetReachedModeToNoTargets()
            fastMarching.Update()

            if targetSeedIds.GetNumberOfIds() > 0:
                subtract = slicer.vtkImageMathematics()
                subtract.SetInput(fastMarching.GetOutput())
                subtract.SetOperationToAddConstant()
                subtract.SetConstantC(-fastMarching.GetTargetValue())
                subtract.Update()
            else:
                subtract = slicer.vtkImageThreshold()
                subtract.SetInput(fastMarching.GetOutput())
                subtract.ThresholdByLower(1000)#better value soon
                subtract.ReplaceInOff()
                subtract.ReplaceOutOn()
                subtract.SetOutValue(-1)
                subtract.Update()

            matrix = slicer.vtkMatrix4x4()
            inVolumeNode.GetIJKToRASMatrix(matrix)

            outVolumeData = slicer.vtkImageData()
            outVolumeData.DeepCopy(subtract.GetOutput())
            outVolumeData.Update()

            # volume calculated...

            outVolumeNode = slicer.vtkMRMLScalarVolumeNode()
            outVolumeNode.SetAndObserveImageData(outVolumeData)
            outVolumeNode.SetIJKToRASMatrix(matrix)

            outputContainer = SlicerVMTKLevelSetContainer(outVolumeNode,0.0)

            self._helper.debug("Fast Marching done...")

            return outputContainer
    def FM(self):
        inVolume = self._inVolumeSelector.GetSelected()
        outVolume = self._outVolumeSelector.GetSelected()
        seeds = self._seedsSelector.GetSelected()
        targetSeeds = self._targetSeedsSelector.GetSelected()

        vmtkFound = self.CheckForVmtkLibrary()

        if inVolume and outVolume and seeds and vmtkFound:

            extentValues = self._thresholdExtent.GetExtent()

            sourceSeedIds = slicer.vtkIdList()
            targetSeedIds = slicer.vtkIdList()

            image = inVolume.GetImageData()

            rasToIjkMatrix = slicer.vtkMatrix4x4()
            inVolume.GetRASToIJKMatrix(rasToIjkMatrix)


            for i in range(seeds.GetNumberOfFiducials()):
                rasPt = seeds.GetNthFiducialXYZ(i)
                rasPt.append(1)
                ijkPt = rasToIjkMatrix.MultiplyPoint(*rasPt)   
                sourceSeedIds.InsertNextId(image.ComputePointId(int(ijkPt[0]),int(ijkPt[1]),int(ijkPt[2])))

            inValue = 0
            outValue = 5

            sideBranchSwitch=0
            
            if targetSeeds:
                if targetSeeds.GetID()!=seeds.GetID():

                    #only run if different fiducial lists
                    self._helper.debug("Using target points..")

                    for i in range(targetSeeds.GetNumberOfFiducials()):
                        rasPt = targetSeeds.GetNthFiducialXYZ(i)
                        rasPt.append(1)
                        ijkPt = rasToIjkMatrix.MultiplyPoint(*rasPt)
                        targetSeedIds.InsertNextId(image.ComputePointId(int(ijkPt[0]),int(ijkPt[1]),int(ijkPt[2])))

                    if self._sbCheckButton.GetSelectedState():
                        sideBranchSwitch=1 # allow side branches only if targetseeds are available

                    # switch thresholds for labelmap when using target points
                    inValue = 5
                    outValue = 0

            result = self._logic.ExecuteFM(inVolume.GetImageData(),extentValues[0],extentValues[1],sourceSeedIds,targetSeedIds,sideBranchSwitch)

            #self._helper.debug(result)

            ijkToRasMatrix = slicer.vtkMatrix4x4()
            inVolume.GetIJKToRASMatrix(ijkToRasMatrix)

            self.CreateOutVolumeNode()

            #outVolume.LabelMapOn()
            outVolume.SetAndObserveImageData(result)
            outVolume.SetIJKToRASMatrix(ijkToRasMatrix)
            outVolume.SetModifiedSinceRead(1)

            scene = self.GetLogic().GetMRMLScene()

            newDisplayNode = slicer.vtkMRMLLabelMapVolumeDisplayNode()
            newDisplayNode.SetScene(scene)
            newDisplayNode.SetDefaultColorMap()
            slicer.MRMLScene.AddNodeNoNotify(newDisplayNode)

            labelMap = slicer.vtkMRMLScalarVolumeNode()
            labelMap.SetName("VMTKInitializationLabelMap")
            labelMap.SetAndObserveImageData(self._logic.BuildSimpleLabelMap(result,inValue,outValue))
            labelMap.SetIJKToRASMatrix(ijkToRasMatrix)
            labelMap.LabelMapOn()
            labelMap.SetAndObserveDisplayNodeID(newDisplayNode.GetID())
            labelMap.SetModifiedSinceRead(1)
            scene.AddNode(labelMap)

            slicer.ApplicationLogic.GetSelectionNode().SetReferenceActiveLabelVolumeID(labelMap.GetID())
            slicer.ApplicationLogic.PropagateVolumeSelection()

            displayNode = inVolume.GetDisplayNode() # reset threshold for visualization
            extentValues = self._thresholdExtent.GetExtentRange()

            displayNode.SetLowerThreshold(extentValues[0])
            displayNode.SetUpperThreshold(extentValues[1])
            displayNode.SetApplyThreshold(1)

            if self._vrCheckButton.GetSelectedState():
                self._helper.HideVR()
                if targetSeeds:
                    self._helper.VolumeRendering(outVolume,'red',targetSeeds.GetID()==seeds.GetID(),sideBranchSwitch)
                else:
                    self._helper.VolumeRendering(outVolume,'red',1)
                self._helper.ShowVR()
            else:
                self._helper.HideVR()
                self._helper.GenerateModel(outVolume,'red')
    def GAC(self):

        origVolume = self._inVolumeSelector.GetSelected()            
        segmentationVolume = self._outVolumeSelector.GetSelected()      
        outVolume = self._outEvolVolumeSelector.GetSelected() 
        seeds = self._seedsSelector.GetSelected()
        targetSeeds = self._targetSeedsSelector.GetSelected()

        vmtkFound = self.CheckForVmtkLibrary()

        if origVolume and segmentationVolume and outVolume and vmtkFound:

            numberOfIterations = int(self._iterationsSpinbox.GetWidget().GetValue())
            propagationScaling = self._propagationScale.GetValue()
            curvatureScaling = self._curvatureScale.GetValue()
            advectionScaling = self._advectionScale.GetValue()

            if (self._evolMethod.GetWidget().GetWidget(0).GetSelectedState()==1):
                result = self._logic.ExecuteGAC(origVolume.GetImageData(),segmentationVolume.GetImageData(),numberOfIterations,propagationScaling,curvatureScaling,advectionScaling,'geodesic')
            else:
                result = self._logic.ExecuteGAC(origVolume.GetImageData(),segmentationVolume.GetImageData(),numberOfIterations,propagationScaling,curvatureScaling,advectionScaling,'curves')

            ijkToRasMatrix = slicer.vtkMatrix4x4()
            origVolume.GetIJKToRASMatrix(ijkToRasMatrix)

            #outVolume.LabelMapOn()
            outVolume.SetAndObserveImageData(result)
            outVolume.SetIJKToRASMatrix(ijkToRasMatrix)
            outVolume.SetModifiedSinceRead(1)

            inValue = 0
            outValue = 5

            if seeds and targetSeeds:                
                if targetSeeds.GetID()!=seeds.GetID():
                    # switch thresholds for labelmap when using target points
                    inValue = 5
                    outValue = 0

            scene = self.GetLogic().GetMRMLScene()

            newDisplayNode = slicer.vtkMRMLLabelMapVolumeDisplayNode()
            newDisplayNode.SetScene(scene)
            newDisplayNode.SetDefaultColorMap()
            slicer.MRMLScene.AddNodeNoNotify(newDisplayNode)

            labelMap = slicer.vtkMRMLScalarVolumeNode()
            labelMap.SetName("VMTKEvolutionLabelMap")
            labelMap.SetAndObserveImageData(self._logic.BuildSimpleLabelMap(result,inValue,outValue))
            labelMap.SetIJKToRASMatrix(ijkToRasMatrix)
            labelMap.LabelMapOn()
            labelMap.SetAndObserveDisplayNodeID(newDisplayNode.GetID())
            scene.AddNode(labelMap)

            slicer.ApplicationLogic.GetSelectionNode().SetReferenceActiveLabelVolumeID(labelMap.GetID())
            slicer.ApplicationLogic.GetSelectionNode().SetReferenceActiveVolumeID(origVolume.GetID())
            slicer.ApplicationLogic.PropagateVolumeSelection()

            #slicer.ApplicationLogic.GetSelectionNode().SetReferenceActiveLabelVolumeID(outVolume.GetID())
            #slicer.ApplicationLogic.PropagateVolumeSelection()

            if self._outInitModelDisplay != None: # deactivate old init model if it exists
                #nodeId = self.GetLogic().GetMRMLScene().GetNodeByID(self._outInitModelDisplay.GetID())
                #if nodeId:
                    #if self.GetLogic().GetMRMLScene().IsNodePresent(nodeId):
                        self._helper.debug("hiding old model..")
                        self._outInitModelDisplay.SetVisibility(0)
                        self._outInitModelDisplay.SetModifiedSinceRead(1)

            if self._vrCheckButton.GetSelectedState():
                self._helper.HideVR()
                if targetSeeds:
                    self._helper.VolumeRendering(outVolume,'blue',targetSeeds.GetID()==seeds.GetID())
                else:
                    self._helper.VolumeRendering(outVolume,'blue',1)
                #self._helper.VolumeRendering(outVolume,'blue')
                self._helper.ShowVR()
            else:
                self._helper.HideVR()
                self._helper.GenerateModel(outVolume,'blue')