Skip to content

Commit

Permalink
BUG: Fix VRGs not generating in center AUT space
Browse files Browse the repository at this point in the history
  • Loading branch information
NicerNewerCar committed Jun 21, 2024
1 parent b3a8fc1 commit 94e8c3c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 36 deletions.
56 changes: 33 additions & 23 deletions AutoscoperM/AutoscoperM.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ def onGenerateVRG(self):
self.logic.createPathsIfNotExists(os.path.join(mainOutputDir, tfmPath))
if not self.logic.IsSequenceVolume(volumeNode):
volumeNode.AddCenteringTransform()
tfmNode = slicer.util.getNode(f"{volumeNode.GetName()} centering transform")
tfmNode = volumeNode.GetParentTransformNode()
volumeNode.HardenTransform()
volumeNode.SetAndObserveTransformNodeID(None)
tfmPath = os.path.join(mainOutputDir, tfmPath, "Origin2Dicom.tfm")
Expand All @@ -513,33 +513,42 @@ def onGenerateVRG(self):
slicer.mrmlScene.RemoveNode(tfmNode)
else:
# Just export the first frame
currentNode, _ = self.logic.getItemInSequence(volumeNode, 0)
proxyNode = self.logic.getItemInSequence(volumeNode, 0)[0]
proxyNode.AddCenteringTransform()
tfmNode = proxyNode.GetParentTransformNode()
proxyNode.SetAndObserveTransformNodeID(None)

tfmPath = os.path.join(mainOutputDir, tfmPath, "Origin2Dicom.tfm")
tfmNode.Inverse()
slicer.util.saveNode(tfmNode, tfmPath)
tfmNode.Inverse()

currentNode = volumeNode.GetNthDataNode(0)
currentNode.AddCenteringTransform()
tfmNode = currentNode.GetParentTransformNode()
currentNode.HardenTransform()
currentNode.SetAndObserveTransformNodeID(None)
tfmPath = os.path.join(mainOutputDir, tfmPath, "Origin2Dicom.tfm")
tfmNode.Inverse()
slicer.util.saveNode(tfmNode, tfmPath)
slicer.mrmlScene.RemoveNode(tfmNode)
volumeNode.SetDataNodeAtValue(currentNode, "0")

# Harden and remove the transform from the sequence
# Apply to all frames
for i in range(1, volumeNode.GetNumberOfDataNodes()):
currentNode, _ = self.logic.getItemInSequence(volumeNode, i)
currentNode.AddCenteringTransform()
tfmNode = currentNode.GetParentTransformNode()
currentNode = volumeNode.GetNthDataNode(i)
currentNode.SetAndObserveTransformNodeID(tfmNode.GetID())
currentNode.HardenTransform()
currentNode.SetAndObserveTransformNodeID(None)
slicer.mrmlScene.RemoveNode(tfmNode)
volumeNode.SetDataNodeAtValue(currentNode, f"{i}")

# slicer.mrmlScene.RemoveNode(tfmNode)

numFrames = 1
currentNode = volumeNode
curName = volumeNode.GetName()
if self.logic.IsSequenceVolume(currentNode):
numFrames = volumeNode.GetNumberOfDataNodes()
currentNode, curName = self.logic.getItemInSequence(volumeNode, 0)
currentNode = volumeNode.GetNthDataNode(0)
curName = currentNode.GetName()
bounds = [0] * 6
currentNode.GetBounds(bounds)
currentNode.GetRASBounds(bounds)

# Generate all possible camera positions
camOffset = self.ui.camOffSetSpin.value
Expand Down Expand Up @@ -572,7 +581,8 @@ def onGenerateVRG(self):
self.updateProgressBar(progress)

if self.logic.IsSequenceVolume(volumeNode):
currentNode, curName = self.logic.getNextItemInSequence(volumeNode)
currentNode = volumeNode.GetNthDataNode(i)
curName = currentNode.GetName()

# Optimize the camera positions
bestCameras = RadiographGeneration.optimizeCameras(
Expand Down Expand Up @@ -1239,15 +1249,15 @@ def generateVRGForCameras(
"""
self.createPathsIfNotExists(outputDir)
# Apply a thresh of 0 to the volume to remove air from the volume
thresholdScalarVolume = slicer.modules.thresholdscalarvolume
parameters = {
"InputVolume": volumeNode.GetID(),
"OutputVolume": volumeNode.GetID(),
"ThresholdValue": 0,
"ThresholdType": "Below",
"Lower": 0,
}
slicer.cli.runSync(thresholdScalarVolume, None, parameters)
# thresholdScalarVolume = slicer.modules.thresholdscalarvolume
# parameters = {
# "InputVolume": volumeNode.GetID(),
# "OutputVolume": volumeNode.GetID(),
# "ThresholdValue": 0,
# "ThresholdType": "Below",
# "Lower": 0,
# }
# slicer.cli.runSync(thresholdScalarVolume, None, parameters)

# write a temporary volume to disk
volumeFileName = "AutoscoperM_VRG_GEN_TEMP.mhd"
Expand Down
2 changes: 1 addition & 1 deletion AutoscoperM/AutoscoperMLib/RadiographGeneration.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def generateNCameras(
:return: List of cameras
"""
# find the center of the bounds
center = [(bounds[0] + bounds[1]) / 2, (bounds[2] + bounds[3]) / 2, (bounds[4] + bounds[5]) / 2]
center = [(bounds[i + 1] + bounds[i]) / 2 for i in range(0, 6, 2)]

# find the largest dimension of the bounds
largestDimension = max([bounds[1] - bounds[0], bounds[3] - bounds[2], bounds[5] - bounds[4]])
Expand Down
6 changes: 6 additions & 0 deletions Tracking3D/Tracking3DLib/TreeNode.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ def exportTransformsAsTRAFile(self):
currentArray = currentArray @ neutralArray
transforms[idx] = self.autoscoperLogic.numpyToVtk(currentArray)

# Move each transform from the DICOM space into AUT space
# TODO: Get this file from user
o2dFile = r"AutoscoperM-Pre-Processing\Autoscoper Scene\Transforms\Origin2Dicom.tfm"
o2dFile = os.path.join(slicer.mrmlScene.GetCacheManager().GetRemoteCacheDirectory(), o2dFile)
transforms = [self.autoscoperLogic.applyOrigin2DicomTransform(tfm, o2dFile) for tfm in transforms]

# Write out tra
# TODO: Make the directory user defined
exportDir = r"AutoscoperM-Pre-Processing\Tracking"
Expand Down
34 changes: 22 additions & 12 deletions VirtualRadiographGeneration/VirtualRadiographGeneration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python-real

import concurrent.futures as cf
import glob
import json
import os
Expand All @@ -26,9 +25,18 @@ def generateVRG(
:param height: Height of the output image
"""

# Zero threshold the image
thresholder = vtk.vtkImageThreshold()
thresholder.ThresholdByLower(0)
thresholder.ReplaceInOn()
thresholder.SetInValue(0)
thresholder.SetInputData(volumeImageData)
thresholder.Update()
thresholdedImageData = thresholder.GetOutput()

# find the min and max scalar values
hist = vtk.vtkImageHistogramStatistics()
hist.SetInputData(volumeImageData)
hist.SetInputData(thresholdedImageData)
hist.Update()
minVal = hist.GetMinimum()
maxVal = hist.GetMaximum()
Expand All @@ -46,7 +54,7 @@ def generateVRG(

# create the volume mapper
volumeMapper = vtk.vtkGPUVolumeRayCastMapper()
volumeMapper.SetInputData(volumeImageData)
volumeMapper.SetInputData(thresholdedImageData)
volumeMapper.SetBlendModeToComposite()
volumeMapper.SetBlendModeToComposite()
volumeMapper.SetUseJittering(False)
Expand Down Expand Up @@ -157,12 +165,14 @@ def _createVTKCameras(cameraDir: str, radiographMainDir: str):
reader.Update()

# generate the virtual radiograph
with cf.ThreadPoolExecutor() as executor:
futures = [
executor.submit(
generateVRG, cam, reader.GetOutput(), os.path.join(camDir, outputFileName), outputWidth, outputHeight
)
for cam, camDir in cameras.items()
]
for future in cf.as_completed(futures):
future.result()
# with cf.ThreadPoolExecutor() as executor:
# futures = [
# executor.submit(
# generateVRG, cam, reader.GetOutput(), os.path.join(camDir, outputFileName), outputWidth, outputHeight
# )
# for cam, camDir in cameras.items()
# ]
# for future in cf.as_completed(futures):
# future.result()
for cam, camDir in cameras.items():
generateVRG(cam, reader.GetOutput(), os.path.join(camDir, outputFileName), outputWidth, outputHeight)

0 comments on commit 94e8c3c

Please sign in to comment.