# Slice intersection source code

**URL:** <https://discourse.slicer.org/t/slice-intersection-source-code/3024>\
**Category:** Development\
**Created:** [May 30, 2018, 6:43pm UTC](https://discourse.slicer.org/t/slice-intersection-source-code/3024 "2018-05-30T18:43:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ricardo\_Mesquita](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@Ricardo\_Mesquita](https://discourse.slicer.org/u/Ricardo_Mesquita)\
**Post date:** [May 30, 2018, 6:43pm UTC](https://discourse.slicer.org/t/slice-intersection-source-code/3024/1 "2018-05-30T18:43:47Z")

</div>

Hi all,  
i would like to know if there is some possibility to control slice intersections manually ,i.e, without mouse control. I intend to set a 3D position (x,y,z) and then the slice intersection cursor automatically goes to the correspondent XY position in the axial, sagital and coronal planes.  
Moreover, is there any source code regarding the slice intersection functionality?  
Thanks for your attention!

---

<div class="post-metadata">

**Author:** ![gleman](https://avatars.discourse-cdn.com/v4/letter/g/85e7bf/32.png) [@gleman](https://discourse.slicer.org/u/gleman)\
**Post date:** [May 30, 2018, 8:11pm UTC](https://discourse.slicer.org/t/slice-intersection-source-code/3024/2 "2018-05-30T20:11:29Z")

</div>

Not sure what you mean, but does this help? You can turn on the intersection visibility and move the slice views around programatically (turn on slices is in the wiki):

```
def turn_on_slice_intersections():
    viewNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLSliceCompositeNode')
    viewNodes.UnRegister(slicer.mrmlScene)
    viewNodes.InitTraversal()
    viewNode = viewNodes.GetNextItemAsObject()
    while viewNode:
        viewNode.SetSliceIntersectionVisibility(1)
        viewNode = viewNodes.GetNextItemAsObject()

view_to_color = {'axial':'Red','coronal':'Green','sagittal':'Yellow'}

def get_current_slice(view):
    lm = slicer.app.layoutManager()
    color = lm.sliceWidget(view_to_color[view])
    colorLogic = color.sliceLogic()
    return colorLogic.GetSliceOffset() 
    
def set_current_slice(view, new_level):
    lm = slicer.app.layoutManager()
    color = lm.sliceWidget(view_to_color[view])
    colorLogic = color.sliceLogic()
    colorLogic.SetSliceOffset(new_level)
    return True
```

---

<div class="post-metadata">

**Author:** ![leochan2009](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/leochan2009/32/519_2.png) [@leochan2009](https://discourse.slicer.org/u/leochan2009)\
**Post date:** [May 30, 2018, 9:17pm UTC](https://discourse.slicer.org/t/slice-intersection-source-code/3024/3 "2018-05-30T21:17:02Z")

</div>

I used to make the slice node go through a point. Hope it will help

def updateSliceViewBasedOnPoints(self, pos):  
redSliceNode = slicer.mrmlScene.GetNodeByID(“vtkMRMLSliceNodeRed”)  
matrixRedOri = redSliceNode.GetSliceToRAS()  
matrixRedOri.SetElement(0, 3, pos[0])  
matrixRedOri.SetElement(1, 3, pos[1])  
matrixRedOri.SetElement(2, 3, pos[2])  
redSliceNode.UpdateMatrices()

---

<div class="post-metadata">

**Author:** ![Nicole\_Aucoin](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@Nicole\_Aucoin](https://discourse.slicer.org/u/Nicole_Aucoin)\
**Post date:** [May 30, 2018, 9:53pm UTC](https://discourse.slicer.org/t/slice-intersection-source-code/3024/4 "2018-05-30T21:53:26Z")

</div>

Once you have the slice node you can call:

> redSliceNode.JumpSliceByOffsetting(pos[0], pos[1], pos[2])

or you can use JumpSliceByCentering.

If you’re already using fiducials, you can use the Markups module logic to do this for all slices by calling JumpSlicesToLocation,
