Skip to content

Commit 7890f83

Browse files
committed
ENH: Added a couple of static methods to ModuleLogicMixin for the purpose of
* getting IJK coordinates for XYZ position * creating a cropped volume from a volume + ROI * creating a masked volume from a volume + label * creating a vtk tube filter * creating a label from a volume with upper/lower threshold and output label value * getting island count from an image * setting node visibility * setting node intersection visibility
1 parent dccff61 commit 7890f83

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

SlicerProstateUtils/mixins.py

+88
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,82 @@ def getCentroidForLabel(labelNode, value):
504504
centroid = [RASDir[0], -RASDir[2], -RASDir[1]]
505505
return centroid
506506

507+
@staticmethod
508+
def roundInt(value, exceptionReturnValue=0):
509+
try:
510+
return int(round(value))
511+
except ValueError:
512+
return exceptionReturnValue
513+
514+
@staticmethod
515+
def getIJKForXYZ(sliceWidget, p):
516+
xyz = sliceWidget.sliceView().convertRASToXYZ(p)
517+
layerLogic = sliceWidget.sliceLogic().GetBackgroundLayer()
518+
xyToIJK = layerLogic.GetXYToIJKTransform()
519+
ijkFloat = xyToIJK.TransformDoublePoint(xyz)
520+
ijk = [ModuleLogicMixin.roundInt(value) for value in ijkFloat]
521+
return ijk
522+
523+
@staticmethod
524+
def createCroppedVolume(inputVolume, roi):
525+
cropVolumeLogic = slicer.modules.cropvolume.logic()
526+
cropVolumeParameterNode = slicer.vtkMRMLCropVolumeParametersNode()
527+
cropVolumeParameterNode.SetROINodeID(roi.GetID())
528+
cropVolumeParameterNode.SetInputVolumeNodeID(inputVolume.GetID())
529+
cropVolumeParameterNode.SetVoxelBased(True)
530+
cropVolumeLogic.Apply(cropVolumeParameterNode)
531+
croppedVolume = slicer.mrmlScene.GetNodeByID(cropVolumeParameterNode.GetOutputVolumeNodeID())
532+
return croppedVolume
533+
534+
@staticmethod
535+
def createMaskedVolume(inputVolume, labelVolume, outputVolumeName=None):
536+
maskedVolume = slicer.vtkMRMLScalarVolumeNode()
537+
if outputVolumeName:
538+
maskedVolume.SetName(outputVolumeName)
539+
slicer.mrmlScene.AddNode(maskedVolume)
540+
params = {'InputVolume': inputVolume, 'MaskVolume': labelVolume, 'OutputVolume': maskedVolume}
541+
slicer.cli.run(slicer.modules.maskscalarvolume, None, params, wait_for_completion=True)
542+
return maskedVolume
543+
544+
@staticmethod
545+
def createVTKTubeFilter(startPoint, endPoint, radius, numSides):
546+
lineSource = vtk.vtkLineSource()
547+
lineSource.SetPoint1(startPoint)
548+
lineSource.SetPoint2(endPoint)
549+
550+
tubeFilter = vtk.vtkTubeFilter()
551+
tubeFilter.SetInputConnection(lineSource.GetOutputPort())
552+
tubeFilter.SetRadius(radius)
553+
tubeFilter.SetNumberOfSides(numSides)
554+
tubeFilter.CappingOn()
555+
tubeFilter.Update()
556+
return tubeFilter
557+
558+
@staticmethod
559+
def createLabelMapFromCroppedVolume(volume, name, lowerThreshold=0, upperThreshold=2000, labelValue=1):
560+
volumesLogic = slicer.modules.volumes.logic()
561+
labelVolume = volumesLogic.CreateAndAddLabelVolume(volume, name)
562+
imageData = labelVolume.GetImageData()
563+
imageThreshold = vtk.vtkImageThreshold()
564+
imageThreshold.SetInputData(imageData)
565+
imageThreshold.ThresholdBetween(lowerThreshold, upperThreshold)
566+
imageThreshold.SetInValue(labelValue)
567+
imageThreshold.Update()
568+
labelVolume.SetAndObserveImageData(imageThreshold.GetOutput())
569+
return labelVolume
570+
571+
@staticmethod
572+
def getIslandCount(image, index):
573+
imageSize = image.GetSize()
574+
index = [0, 0, index]
575+
extractor = sitk.ExtractImageFilter()
576+
extractor.SetSize([imageSize[0], imageSize[1], 0])
577+
extractor.SetIndex(index)
578+
slice = extractor.Execute(image)
579+
cc = sitk.ConnectedComponentImageFilter()
580+
cc.Execute(slice)
581+
return cc.GetObjectCount()
582+
507583
@staticmethod
508584
def applyOtsuFilter(volume):
509585
outputVolume = slicer.vtkMRMLScalarVolumeNode()
@@ -641,6 +717,18 @@ def setAndObserveDisplayNode(node):
641717
node.SetAndObserveDisplayNodeID(displayNode.GetID())
642718
return displayNode
643719

720+
@staticmethod
721+
def setNodeVisibility(node, visible):
722+
displayNode = node.GetDisplayNode()
723+
if displayNode is not None:
724+
displayNode.SetVisibility(visible)
725+
726+
@staticmethod
727+
def setNodeSliceIntersectionVisibility(node, visible):
728+
displayNode = node.GetDisplayNode()
729+
if displayNode is not None:
730+
displayNode.SetSliceIntersectionVisibility(visible)
731+
644732
@staticmethod
645733
def isVolumeExtentValid(volume):
646734
imageData = volume.GetImageData()

0 commit comments

Comments
 (0)