Skip to content

Commit

Permalink
ENH: Export AUT_{bone}.stl during PV gen
Browse files Browse the repository at this point in the history
  • Loading branch information
NicerNewerCar committed Jul 25, 2024
1 parent 13d674e commit e89a735
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 60 deletions.
48 changes: 46 additions & 2 deletions AutoscoperM/AutoscoperM.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,19 +441,27 @@ def onGeneratePartialVolumes(self):
mainOutputDir = self.ui.mainOutputSelector.currentPath
tiffSubDir = self.ui.tiffSubDir.text
tfmSubDir = self.ui.tfmSubDir.text
trackingSubDir = self.ui.trackingSubDir.text
modelSubDir = self.ui.modelSubDir.text
segmentationNode = self.ui.pv_SegNodeComboBox.currentNode()
if not self.logic.validateInputs(
volumeNode=volumeNode,
segmentationNode=segmentationNode,
mainOutputDir=mainOutputDir,
volumeSubDir=tiffSubDir,
transformSubDir=tfmSubDir,
trackingSubDir=trackingSubDir,
modelSubDir=modelSubDir,
):
raise ValueError("Invalid inputs")
return

self.logic.createPathsIfNotExists(
mainOutputDir, os.path.join(mainOutputDir, tiffSubDir), os.path.join(mainOutputDir, tfmSubDir)
mainOutputDir,
os.path.join(mainOutputDir, tiffSubDir),
os.path.join(mainOutputDir, tfmSubDir),
os.path.join(mainOutputDir, trackingSubDir),
os.path.join(mainOutputDir, modelSubDir),
)
self.ui.progressBar.setValue(0)
self.ui.progressBar.setMaximum(100)
Expand All @@ -463,6 +471,8 @@ def onGeneratePartialVolumes(self):
mainOutputDir,
volumeSubDir=tiffSubDir,
transformSubDir=tfmSubDir,
trackingSubDir=trackingSubDir,
modelSubDir=modelSubDir,
progressCallback=self.updateProgressBar,
)
slicer.util.messageBox("Success!")
Expand Down Expand Up @@ -1013,6 +1023,7 @@ def saveSubVolumesFromSegmentation(
volumeSubDir: str = "Volumes",
transformSubDir: str = "Transforms",
trackingSubDir: str = "Tracking",
modelSubDir: str = "Models",
progressCallback: Optional[callable] = None,
) -> bool:
"""
Expand Down Expand Up @@ -1065,7 +1076,6 @@ def progressCallback(x):
segmentVolume.DisableModifiedEventOff()

transformFilenameBase = os.path.join(outputDir, transformSubDir, segmentName)

origin = segmentVolume.GetOrigin()
IO.writeTFMFile(f"{transformFilenameBase}_t.tfm", [1.0, 1.0, 1.0], origin)
spacing = segmentVolume.GetSpacing()
Expand All @@ -1082,6 +1092,10 @@ def progressCallback(x):
dicom2autNode = self.createAndAddDicom2AutTransformNode(origin, pvol2autNode)
dicom2autFilename = os.path.join(outputDir, transformSubDir, f"{segmentVolume.GetName()}-DICOM2AUT.tfm")
slicer.util.exportNode(dicom2autNode, dicom2autFilename)

stlFilename = os.path.join(outputDir, modelSubDir, f"AUT_{segmentVolume.GetName()}.stl")
self.exportSTLFromSegment(segmentationNode, segmentID, stlFilename, dicom2autNode.GetTransformToParent())

slicer.mrmlScene.RemoveNode(dicom2autNode)

# Create TRA file
Expand Down Expand Up @@ -1122,6 +1136,36 @@ def progressCallback(x):
slicer.app.layoutManager().resetSliceViews()
return True

@staticmethod
def exportSTLFromSegment(
segmentationNode: slicer.vtkMRMLSegmentationNode,
segmentID: str,
filename: str,
transform: Optional[vtk.vtkAbstractTransform] = None,
):
"""Utility functions for exporting a segment as an STL. Optionally takes a vtk transform."""
if not segmentationNode.CreateClosedSurfaceRepresentation():
raise ValueError(
f"Failed to generate the closed surface representation from segmentation: {segmentationNode.GetName()}"
)

polyData = vtk.vtkPolyData()
if not segmentationNode.GetClosedSurfaceRepresentation(segmentID, polyData):
raise ValueError(f"Failed to get PolyData for segmentationNode {segmentationNode.GetName()}")

if transform is not None:
transformFilter = vtk.vtkTransformPolyDataFilter()
transformFilter.SetInputData(polyData)
transformFilter.SetTransform(transform)
transformFilter.Update()
polyData = transformFilter.GetOutput()

stlNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLModelNode")
stlNode.SetAndObservePolyData(polyData)

slicer.util.exportNode(stlNode, filename)
slicer.mrmlScene.RemoveNode(stlNode)

@staticmethod
def showVolumeIn3D(volumeNode: slicer.vtkMRMLVolumeNode):
logic = slicer.modules.volumerendering.logic()
Expand Down
144 changes: 86 additions & 58 deletions AutoscoperM/Resources/UI/AutoscoperM.ui
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
<number>1</number>
</property>
<widget class="QWidget" name="AutoscoperControlTab">
<attribute name="title">
Expand Down Expand Up @@ -205,10 +205,10 @@
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
<bool>true</bool>
</property>
<property name="collapsed">
<bool>true</bool>
<bool>false</bool>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<item row="3" column="0">
Expand All @@ -218,83 +218,93 @@
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_20">
<item row="8" column="0">
<widget class="QCheckBox" name="removeVrgTmp">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>Partial Volume Transforms Subdirectory:</string>
<string>Delete Temporary VRG Files</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_13">
<item row="10" column="0">
<widget class="QCheckBox" name="idxOnly">
<property name="text">
<string>Virtual Radiograph Subdirectory:</string>
<string>Only use indices for radiograph filename</string>
</property>
</widget>
</item>
<item row="0" column="2" colspan="2">
<widget class="QLineEdit" name="tiffSubDir">
<item row="7" column="0">
<widget class="QLabel" name="label_16">
<property name="text">
<string>Volumes</string>
<string>VRG Temp Subdirectory:</string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_15">
<item row="2" column="0">
<widget class="QLabel" name="label_13">
<property name="text">
<string>VRG Resolution: (width,height)</string>
<string>Virtual Radiograph Subdirectory:</string>
</property>
</widget>
</item>
<item row="6" column="2" colspan="2">
<widget class="QLineEdit" name="vrgTempDir">
<item row="3" column="2" colspan="2">
<widget class="QLineEdit" name="cameraSubDir">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>VRG-Temp</string>
<string>Calibration</string>
</property>
</widget>
</item>
<item row="2" column="2" colspan="2">
<widget class="QLineEdit" name="vrgSubDir">
<item row="4" column="0">
<widget class="QLabel" name="label_18">
<property name="text">
<string>RadiographImages</string>
<string>Tracking Subdirectory:</string>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QCheckBox" name="removeVrgTmp">
<property name="enabled">
<bool>false</bool>
</property>
<item row="6" column="0">
<widget class="QLabel" name="label_15">
<property name="text">
<string>Delete Temporary VRG Files</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>true</bool>
<string>VRG Resolution: (width,height)</string>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QLabel" name="label_16">
<item row="1" column="2" colspan="2">
<widget class="QLineEdit" name="tfmSubDir">
<property name="text">
<string>VRG Temp Subdirectory:</string>
<string>Transforms</string>
</property>
</widget>
</item>
<item row="3" column="2" colspan="2">
<widget class="QLineEdit" name="cameraSubDir">
<property name="enabled">
<item row="9" column="0">
<widget class="QCheckBox" name="camDebugCheckbox">
<property name="text">
<string>Camera Debug Mode</string>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
</item>
<item row="0" column="2" colspan="2">
<widget class="QLineEdit" name="tiffSubDir">
<property name="text">
<string>Calibration</string>
<string>Volumes</string>
</property>
</widget>
</item>
<item row="5" column="3">
<widget class="QSpinBox" name="vrgRes_height">
<item row="6" column="2">
<widget class="QSpinBox" name="vrgRes_width">
<property name="maximum">
<number>999999999</number>
</property>
Expand All @@ -303,15 +313,22 @@
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_12">
<item row="1" column="0">
<widget class="QLabel" name="label_20">
<property name="text">
<string>Partial Volume Subdirectory:</string>
<string>Partial Volume Transforms Subdirectory:</string>
</property>
</widget>
</item>
<item row="5" column="2">
<widget class="QSpinBox" name="vrgRes_width">
<item row="2" column="2" colspan="2">
<widget class="QLineEdit" name="vrgSubDir">
<property name="text">
<string>RadiographImages</string>
</property>
</widget>
</item>
<item row="6" column="3">
<widget class="QSpinBox" name="vrgRes_height">
<property name="maximum">
<number>999999999</number>
</property>
Expand All @@ -320,27 +337,38 @@
</property>
</widget>
</item>
<item row="1" column="2" colspan="2">
<widget class="QLineEdit" name="tfmSubDir">
<item row="7" column="2" colspan="2">
<widget class="QLineEdit" name="vrgTempDir">
<property name="text">
<string>Transforms</string>
<string>VRG-Temp</string>
</property>
</widget>
</item>
<item row="8" column="0">
<widget class="QCheckBox" name="camDebugCheckbox">
<item row="0" column="0">
<widget class="QLabel" name="label_12">
<property name="text">
<string>Camera Debug Mode</string>
<string>Partial Volume Subdirectory:</string>
</property>
<property name="checked">
<bool>false</bool>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_21">
<property name="text">
<string>Model Subdirectory:</string>
</property>
</widget>
</item>
<item row="9" column="0">
<widget class="QCheckBox" name="idxOnly">
<item row="4" column="2" colspan="2">
<widget class="QLineEdit" name="trackingSubDir">
<property name="text">
<string>Only use indices for radiograph filename</string>
<string>Tracking</string>
</property>
</widget>
</item>
<item row="5" column="2" colspan="2">
<widget class="QLineEdit" name="modelSubDir">
<property name="text">
<string>Models</string>
</property>
</widget>
</item>
Expand Down

0 comments on commit e89a735

Please sign in to comment.