# Beam Visualisation in Slicer RT

**URL:** <https://discourse.slicer.org/t/beam-visualisation-in-slicer-rt/9759>\
**Category:** Support\
**Created:** [January 9, 2020, 3:19pm UTC](https://discourse.slicer.org/t/beam-visualisation-in-slicer-rt/9759 "2020-01-09T15:19:19Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![electus](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@electus](https://discourse.slicer.org/u/electus)\
**Post date:** [January 9, 2020, 3:19pm UTC](https://discourse.slicer.org/t/beam-visualisation-in-slicer-rt/9759/1 "2020-01-09T15:19:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sam\_Horvath](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sam_horvath/32/3092_2.png) [@Sam\_Horvath](https://discourse.slicer.org/u/Sam_Horvath)\
**Post date:** [January 9, 2020, 3:40pm UTC](https://discourse.slicer.org/t/beam-visualisation-in-slicer-rt/9759/2 "2020-01-09T15:40:15Z")

</div>

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

> <https://github.com/SlicerRt/SlicerRT/blob/f78335ba182b5f76eea5fcf8be9b3cfe17a4872f/Beams/MRML/vtkMRMLRTBeamNode.cxx#L495>

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](https://vtk.org/Wiki/VTK/Examples/Cxx/GeometricObjects/Cylinder)
2. [vtkTubeFilter](https://vtk.org/Wiki/VTK/Examples/Cxx/PolyData/TubeFilter)

---

<div class="post-metadata">

**Author:** ![electus](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@electus](https://discourse.slicer.org/u/electus)\
**Post date:** [January 9, 2020, 3:55pm UTC](https://discourse.slicer.org/t/beam-visualisation-in-slicer-rt/9759/3 "2020-01-09T15:55:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sam\_Horvath](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sam_horvath/32/3092_2.png) [@Sam\_Horvath](https://discourse.slicer.org/u/Sam_Horvath)\
**Post date:** [January 9, 2020, 4:22pm UTC](https://discourse.slicer.org/t/beam-visualisation-in-slicer-rt/9759/4 "2020-01-09T16:22:08Z")

</div>

Here is a small example

```python
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](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository) is a good resource for slicer python code snippets

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [January 9, 2020, 4:36pm UTC](https://discourse.slicer.org/t/beam-visualisation-in-slicer-rt/9759/5 "2020-01-09T16:36:11Z")

</div>

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):

```python
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())

```

---

<div class="post-metadata">

**Author:** ![electus](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@electus](https://discourse.slicer.org/u/electus)\
**Post date:** [January 10, 2020, 2:07pm UTC](https://discourse.slicer.org/t/beam-visualisation-in-slicer-rt/9759/6 "2020-01-10T14:07:07Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [January 10, 2020, 2:18pm UTC](https://discourse.slicer.org/t/beam-visualisation-in-slicer-rt/9759/7 "2020-01-10T14:18:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![electus](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@electus](https://discourse.slicer.org/u/electus)\
**Post date:** [January 10, 2020, 6:58pm UTC](https://discourse.slicer.org/t/beam-visualisation-in-slicer-rt/9759/8 "2020-01-10T18:58:54Z")

</div>

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 😊

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [January 11, 2020, 9:22am UTC](https://discourse.slicer.org/t/beam-visualisation-in-slicer-rt/9759/9 "2020-01-11T09:22:41Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![electus](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@electus](https://discourse.slicer.org/u/electus)\
**Post date:** [January 12, 2020, 12:36pm UTC](https://discourse.slicer.org/t/beam-visualisation-in-slicer-rt/9759/10 "2020-01-12T12:36:13Z")

</div>

Thanks for your reply.

---

<div class="post-metadata">

**Author:** ![electus](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@electus](https://discourse.slicer.org/u/electus)\
**Post date:** [January 12, 2020, 12:51pm UTC](https://discourse.slicer.org/t/beam-visualisation-in-slicer-rt/9759/11 "2020-01-12T12:51:10Z")

</div>

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
