Skip to content

Instantly share code, notes, and snippets.

@ungi
Last active February 3, 2020 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ungi/3f02bd152e43140b92f5cb87347b7165 to your computer and use it in GitHub Desktop.
Save ungi/3f02bd152e43140b92f5cb87347b7165 to your computer and use it in GitHub Desktop.
# Copy and paste into Slicer Python interactor
# Drag visible fiducials to deform transform
# Scale defines how large cube will be created
# numPerEdge defines how many fiducials to put on each edge of the cube
scale = 30.0
numPerEdge = 3
# Create the FROM fiducial list, and hide it so it doesn't change with mouse interactions
fromFids = slicer.vtkMRMLMarkupsFiducialNode()
fromFids.SetName('FromFids')
slicer.mrmlScene.AddNode(fromFids)
for x in range(numPerEdge):
for y in range(numPerEdge):
for z in range(numPerEdge):
cx = (x-float(numPerEdge-1)/2.0)*scale
cy = (y-float(numPerEdge-1)/2.0)*scale
cz = (z-float(numPerEdge-1)/2.0)*scale
fromFids.AddFiducial(cx, cy, cz)
fromFids.GetDisplayNode().SetVisibility(False)
# Create the TO fiducial list, and make its label text invisibly small
toFids = slicer.vtkMRMLMarkupsFiducialNode()
toFids.SetName('T')
slicer.mrmlScene.AddNode(toFids)
for x in range(numPerEdge):
for y in range(numPerEdge):
for z in range(numPerEdge):
cx = (x-float(numPerEdge-1)/2.0)*scale
cy = (y-float(numPerEdge-1)/2.0)*scale
cz = (z-float(numPerEdge-1)/2.0)*scale
toFids.AddFiducial(cx, cy, cz)
toFids.GetDisplayNode().SetTextScale(0)
# Create the transform node to hold the deformable transformation later
tNode = slicer.vtkMRMLTransformNode()
tNode.SetName( 'TpsTransform' )
slicer.mrmlScene.AddNode( tNode )
# Function that will be called whenever a TO fiducial moves and the transform needs an update
def updateTpsTransform(caller, eventid):
numPerEdge = fromFids.GetNumberOfFiducials()
if numPerEdge != toFids.GetNumberOfFiducials():
print('Error: Fiducial numbers are not equal!')
return
fp = vtk.vtkPoints()
tp = vtk.vtkPoints()
f = [0, 0, 0]
t = [0, 0, 0]
for i in range(numPerEdge):
fromFids.GetNthFiducialPosition(i, f)
toFids.GetNthFiducialPosition(i, t)
fp.InsertNextPoint(f)
tp.InsertNextPoint(t)
tps = vtk.vtkThinPlateSplineTransform()
tps.SetSourceLandmarks( fp )
tps.SetTargetLandmarks( tp )
tps.SetBasisToR()
tNode.SetAndObserveTransformToParent( tps )
toFids.AddObserver(vtk.vtkCommand.ModifiedEvent, updateTpsTransform)
# A ROI annotation defines the region where the transform will be visualized
roi = slicer.vtkMRMLAnnotationROINode()
roi.SetName( 'RoiNode' )
slicer.mrmlScene.AddNode( roi )
roi.SetDisplayVisibility(False)
roi.SetXYZ(0, 0, 0)
roi.SetRadiusXYZ(scale, scale, scale)
# Set up transform visualization as gridlines
tNode.CreateDefaultDisplayNodes()
d = tNode.GetDisplayNode()
d.SetAndObserveRegionNode(roi)
d.SetVisualizationMode( slicer.vtkMRMLTransformDisplayNode.VIS_MODE_GRID)
d.SetVisibility(True)
@lassoan
Copy link

lassoan commented Jan 23, 2017

tps.SetBasisToR()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment