Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save drouin-simon/e2b5ecf77d53697e2e20c1d8fd016ea3 to your computer and use it in GitHub Desktop.

Select an option

Save drouin-simon/e2b5ecf77d53697e2e20c1d8fd016ea3 to your computer and use it in GitHub Desktop.
Tie a sphere transform to the ROI of a FiberBundle managed by SlicerDMRI
import numpy as np
# Get the transform of the sphere
t = slicer.mrmlScene.GetNodesByName("center").GetItemAsObject(0)
mat = vtk.vtkMatrix4x4()
t.GetMatrixTransformToParent(mat)
# Get the region extracting the ROI from the fibers
fbn = slicer.mrmlScene.GetNodesByClass("vtkMRMLFiberBundleNode").GetItemAsObject(0)
efr = fbn.GetExtractFromROI()
# Create a sphere implicit function and enable masking
sphereFunc = vtk.vtkSphere()
sphereFunc.SetRadius(5.0)
sphereFunc.SetCenter([mat.GetElement(0, 3), mat.GetElement(1, 3), mat.GetElement(2, 3)])
efr.SetImplicitFunction(sphereFunc)
efr.Modified()
fbn.SetSelectWithMarkups(True)
# Setup the callback to move the roi when the transform changes
def onTransformModified(caller, event):
caller.GetMatrixTransformToParent(mat)
sphereFunc.SetCenter([mat.GetElement(0, 3), mat.GetElement(1, 3), mat.GetElement(2, 3)])
efr.Modified()
observerTag = t.AddObserver(slicer.vtkMRMLTransformNode.TransformModifiedEvent, onTransformModified)
@drouin-simon

Copy link
Copy Markdown
Author

This script will clip a fiber bundle to maintain only the fibers going through a spherical region. The script updates the clipping every time a transform named "center" is modified.

To use this script:

  • Make sure SlicerDMRI is installed
  • Load a fiber bundle
  • Create a sphere model with a radius of 5mm
  • Create a Transform and name it "center" and assign it to the sphere model
  • Run the script in the Python console

After running the script, fibers going through the sphere should be conserved and others clipped. Moving the transform should automatically update the clipping.

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