Display model in slice viewer

Hi, I am trying to draw a polygon on the axial viewer given the coordinates of some fiducial points. I am using a model created from a polydata object so I can easily export it for my analysis.
I used this code in a scripted module and the polygon is correctly displayed in the 3D view but I don’t know how to display it in the red view as well, which is what I need. Is that possible?
I’ve attached a screenshot at the end.

fidList = slicer.util.getNode(‘F’)
if not fidList:
print “No points found”
return
numFids = fidList.GetNumberOfFiducials()

points = vtk.vtkPoints()
polygon = vtk.vtkPolygon()
polygon.GetPointIds().SetNumberOfIds(numFids)

points_coords =
for i in range(numFids):
fid_ras_coord = [0, 0, 0]
fidList.GetNthFiducialPosition(i, fid_ras_coord)
points.InsertNextPoint(fid_ras_coord)
polygon.GetPointIds().SetId(i, i)
points_coords.append(fid_ras_coord)

polygons = vtk.vtkCellArray()
polygons.InsertNextCell(polygon)
polygonPolyData = vtk.vtkPolyData()
polygonPolyData.SetPoints(points)
polygonPolyData.SetPolys(polygons)

model = slicer.vtkMRMLModelNode()
model.SetAndObservePolyData(polygonPolyData)
modelDisplay = slicer.vtkMRMLModelDisplayNode()

modelDisplay.SetColor(1, 1, 0)
modelDisplay.BackfaceCullingOff()
modelDisplay.SetOpacity(0.5)
modelDisplay.SetPointSize(3)

modelDisplay.SetSliceIntersectionVisibility(True)
modelDisplay.SetVisibility(True)
slicer.mrmlScene.AddNode(modelDisplay)
model.SetAndObserveDisplayNodeID(modelDisplay.GetID())
modelDisplay.SetInputPolyDataConnection(model.GetPolyDataConnection())
slicer.mrmlScene.AddNode(model)

You can only see the intersection of the polydata with a slice, so the polydata has to be a 3D surface (theoretically it could be possible to have a 2D surface that is exactly aligned with the slice viewer, but in practice perfect alignment is often impossible to achieve).
Fortunately, the Markups to model module in SlicerIGT extension does extactly that. It has several options for generating closed surface from a set of markups. It can do simple convex hull or smoothed contour with various options for making it more robust or accurate.

Simple quasi-planar surface generation:

Smooth 3D surface generation:

To generate the contours from your own module, you just need to add a vtkMRMLMarkupsToModelNode in your scene and set up its inputs and outputs. At minimum, call SetAndObserveMarkupsNodeID and SetAndObserveModelNodeID. See all methods and options at:
https://github.com/SlicerIGT/SlicerIGT/blob/master/MarkupsToModel/MRML/vtkMRMLMarkupsToModelNode.h

2 Likes

Thanks a lot for the reply.
Now I can show the polygon in all the viewers using the IGT extension.
However I could not figure out how to use vtkMRMLMarkupsToModelNode in my module. How do I set input and output?

Marta

Example: drop a few fiducials and run this code

inputMarkups = getNode('F')

outputModel = slicer.mrmlScene.AddNode(slicer.vtkMRMLModelNode())
outputModel.CreateDefaultDisplayNodes()
outputModel.GetDisplayNode().SetSliceIntersectionVisibility(True)
outputModel.GetDisplayNode().SetColor(1,0,0)

markupsToModel = slicer.mrmlScene.AddNode(slicer.vtkMRMLMarkupsToModelNode())
markupsToModel.SetAutoUpdateOutput(True)
markupsToModel.SetAndObserveModelNodeID(outputModel.GetID())
markupsToModel.SetAndObserveMarkupsNodeID(inputMarkups.GetID())
1 Like

Thank you. I am creating a model (closed surface) based on points using Markups to Model module. The results are satisfactory. Is there any related literatures I could refer to? (for reference when describing algorithms in the scientific paper). Hope for some suggestions. Your help is highly appreciated!

Crayon

Please cite 3D Slicer and in addition you can cite:

1 Like