Beam Visualisation in Slicer RT

Hi guys,

I am develeping a tool for slicer that needs a very similar functionality compared to Slicer RT.

I want to display beam in the slicer scene just like it is done in Slcier RT. But i am not very far with that.
I was trying to get into that and managed to draw lines. I found out about vtkMRMLModel(Display)Node and so on.

But could someone please give me a more or less detailed explenation how this task was carried out in Slicer RT, maybe even with a little bit of code.

I’d be very thankful.
Cheers

So, the code for how SlicerRT generates the beam model is here:

But it is not the most informative. In general, what you are going to want to do is

  1. Create a vtkPolyData containing the model
  2. Attach the polydata to a vtkMRMLModelNode (SetAndObservePolyData)
  3. Use the Model Node’s associated display node properties to set color, opacity, etc

To create the polydata in (1), there a a number of quick ways to create a simple cylinder model:

  1. vtkCylinderSource
  2. vtkTubeFilter
1 Like

Thanks a lot for the reply.

Could you please give me a quick example of lets say connecting 3 points with straight lines and displaying them on slicer.
I think I would be able to work from there on.

Please note I am working in Python.

Here is a small example

import slicer
import vtk

#Create node for model
lineNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLModelNode')
lineNode.SetName('Lines')
lineNode.CreateDefaultDisplayNodes()

#Create Polydata, 
points = vtk.vtkPoints()
points.SetNumberOfPoints(3)

points.SetPoint(0, 0.0, 0.0, 0.0)
points.SetPoint(1,   0.0, 20.0, 0.0)
points.SetPoint(2,   0.0,  0.0, 20.0)

line = vtk.vtkLineSource()
line.SetPoints(points)
line.Update()

#attach polydata to model node
lineNode.SetAndObservePolyData(line.GetOutput())

#Visibility
disp = lineNode.GetModelDisplayNode()
disp.SetVisibility(True)
disp.SetLineWidth(10.0)

The python script repository is a good resource for slicer python code snippets

2 Likes

You can make things even simpler by using Models module logic’s AddModel method (it creates model and display nodes and add them to the scene):

points = vtk.vtkPoints()
points.SetNumberOfPoints(3)
points.SetPoint(0, 0.0, 0.0, 0.0)
points.SetPoint(1,   0.0, 20.0, 0.0)
points.SetPoint(2,   0.0,  0.0, 20.0)
line = vtk.vtkLineSource()
line.SetPoints(points)
lineNode = slicer.modules.models.logic().AddModel(line.GetOutputPort())
2 Likes

Hi thanks a lot.
This worked well and i could play around with it and using many line sources would serve my task.
I was wondering if a source already exists that combines the line sources and models a rectangular divergent beam.

I was trying to figure out how Slicer RT did it

points = vtk.vtkPoints()
points.SetNumberOfPoints(5)
cellArray = vtk.vtkCellArray()

x1 = -100.0
x2 = 100
y1 = -100
y2 = 100

points.SetPoint(0, 0.0, 0.0, -212.0)
points.SetPoint(1, x1, y1, -212.0)
points.SetPoint(2, x1, y2, -212.0)
points.SetPoint(3, x2, y2, -212.0)
points.SetPoint(3, x2, y1, -212.0)

cellArray.InsertNextCell(3)
cellArray.InsertCellPoint(0)
cellArray.InsertCellPoint(1)
cellArray.InsertCellPoint(2)

cellArray.InsertNextCell(3)
cellArray.InsertCellPoint(0)
cellArray.InsertCellPoint(2)
cellArray.InsertCellPoint(3)

cellArray.InsertNextCell(3)
cellArray.InsertCellPoint(0)
cellArray.InsertCellPoint(3)
cellArray.InsertCellPoint(4)

cellArray.InsertNextCell(3)
cellArray.InsertCellPoint(0)
cellArray.InsertCellPoint(4)
cellArray.InsertCellPoint(1)


cellArray.InsertNextCell(4)
cellArray.InsertCellPoint(1)
cellArray.InsertCellPoint(2)
cellArray.InsertCellPoint(3)
cellArray.InsertCellPoint(4)


line = vtk.vtkLineSource()
line.SetPoints(points)
line.SetPolys(cellArray)

lineNode = slicer.modules.models.logic().AddModel(line.GetOutputPort())

And I am not quite sure which type of vtkSource was used here.
And what is the reason, that the lines, that I already got with your help, are only visible in 3D part, but not in the other scenes (R, Y, G).

Thanks a lot again and have a nice weekend!

What would you like to achieve? Why don’t you use/customize/enhance SlicerRT instead of renplementing selected features of it?

I am trying to create treatment planning modalities. And I have thought about extending RT. But I need to do quite some things differently then performed in Slicer RT.
For example the source itself is different already, so I need to create an own model therefore anyways.
That’s why I was trying to break it down, how this task was carried out in Slicer RT.
But I’m struggling a little, that’s why I asked for help :blush:

1 Like

SlicerRT is created to be modular and extensible. External Beam Planning supports adding any kind of dose computation engine and model. I have a hard time imagining how things could get easier for you starting from scratch and reimplementing 90% of the same features to add your own 10%. Also, working in an established open-source project ensures always high quality code and outsources some of the maintenance burden, so I strongly suggest taking some time to look at the SlicerRT code instead of starting from hello-world type code as you do here. We are here to help, so if you have any question about how things are done in SlicerRT and why, we are happy to answer!

1 Like

Thanks for your reply.

Can I please ask how lines created like shown above, can also be made visible in the red, yellow and green scenes. For now they are only visible in the 3d represetation of the cube.

Thanks