# Center coordinates of volume slices?

**URL:** <https://discourse.slicer.org/t/center-coordinates-of-volume-slices/23133>\
**Category:** Development\
**Created:** [April 26, 2022, 7:36pm UTC](https://discourse.slicer.org/t/center-coordinates-of-volume-slices/23133 "2022-04-26T19:36:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [April 26, 2022, 7:36pm UTC](https://discourse.slicer.org/t/center-coordinates-of-volume-slices/23133/1 "2022-04-26T19:36:11Z")

</div>

How do I get the center coordinates for a volume that is shifted to the left and zoomed in a bit during maniplations?

Example: from

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/4/147ac1b9565b50508fb2e0c32f55bc8064d3bb54.jpeg)

to

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/9/491a480b73ac18f205fc9a7285368eda936d2020.jpeg)

I am interested in the screen coordinates. Thanks.

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [April 26, 2022, 10:42pm UTC](https://discourse.slicer.org/t/center-coordinates-of-volume-slices/23133/2 "2022-04-26T22:42:28Z")

</div>

I think you may need to use [this method](https://apidocs.slicer.org/v4.8/classvtkMRMLSliceNode.html#a8ebef5d6a77bcabc4282ebc59651ac52) to get the XYToRASMatrix and invert it, then transform the sliceCenterRAS with it

I’m not 100% sure it will work, hope it helps

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [April 27, 2022, 9:18am UTC](https://discourse.slicer.org/t/center-coordinates-of-volume-slices/23133/3 "2022-04-27T09:18:33Z")

</div>

Thanks, Mauro, I think this got me on the right track.

There is a [function in vtkSlicerMarkupsWidgetRepresentation2D](https://github.com/Slicer/Slicer/blob/55aa11474f67fe1fc7ebef3feab1746da9338772/Modules/Loadable/Markups/VTKWidgets/vtkSlicerMarkupsWidgetRepresentation2D.cxx#L161) which I could use with a world coordinate of 0.,0.,0. to be at the center of the CT volume and receive it’s display coordinate.

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [April 27, 2022, 9:54am UTC](https://discourse.slicer.org/t/center-coordinates-of-volume-slices/23133/4 "2022-04-27T09:54:22Z")

</div>

I think you would need the sliceCenterRAS, that normally is not (0,0,0). You can get it by averaging the bounds on each axis of the corresponding CT volume

Hope it helps

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [April 29, 2022, 10:22am UTC](https://discourse.slicer.org/t/center-coordinates-of-volume-slices/23133/5 "2022-04-29T10:22:13Z")

</div>

Sorry, that has not solved it for me yet.

Managed to call GetXYToRAS() and to do the invert. Could anyone explain where the above metioned “sliceCenterRAS” is and how it is used?  
Or how I could read the centers from the vtkMatrix4x4 matrix?

I would need to get CT volume centers as xy display coordinates for each axial, coronal and saggital 2D view.

Thanks.

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [April 29, 2022, 5:37pm UTC](https://discourse.slicer.org/t/center-coordinates-of-volume-slices/23133/6 "2022-04-29T17:37:20Z")

</div>

> [@rbumm](#):
>
> I would need to get CT volume centers as xy display coordinates for each axial, coronal and saggital 2D view.

Yes. Sorry, that’s the correct name for the variable volumeCenterRAS, not sliceCenterRAS.

And you can get it like this:

```auto
bounds=[0,0,0,0,0,0]
volume = getNode('myCT')
volume.GetRASBounds(bounds)
volumeCenterRAS = [(bounds[1]+bounds[0])/2,(bounds[3]+bounds[2])/2,(bounds[5]+bounds[4])/2,1]

```

I think this should work as RASToXY is a projection matrix:  
`RASToXY.MultiplyPoint(volumeCenterRAS, sliceCenterXY)`

Please let me know how it goes

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [April 29, 2022, 6:46pm UTC](https://discourse.slicer.org/t/center-coordinates-of-volume-slices/23133/7 "2022-04-29T18:46:07Z")

</div>

Thx Mauro, can only try that out next week. I´ll be doing this in C++ and need to see how to get to the volume node reliably in that module I am working on.

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [May 4, 2022, 3:57pm UTC](https://discourse.slicer.org/t/center-coordinates-of-volume-slices/23133/8 "2022-05-04T15:57:40Z")

</div>

Think I got it working (C++) by

```auto
//----------------------------------------------------------------------
void GetBackgroundVolumeXYCenter(double slicePos[2])
{
    vtkMRMLSliceCompositeNode* compositeNode = nullptr;
    compositeNode = vtkMRMLSliceCompositeNode::SafeDownCast(this->GetSliceNode()->GetScene()->GetFirstNodeByClass("vtkMRMLSliceCompositeNode"));
    const char* backgroundVolumeID = compositeNode->GetBackgroundVolumeID();

    double volumeBounds[6] = { 0.0 };
    double volumeCenterRAS[4] = { 0.0 };
    double centerPosDisplay[4] = { 0.0 };

    if (backgroundVolumeID)
    {
        vtkMRMLScalarVolumeNode* sliceBackgroundVolumeNode = vtkMRMLScalarVolumeNode::SafeDownCast(this->GetSliceNode()->
            GetScene()->GetNodeByID(backgroundVolumeID));
        if (sliceBackgroundVolumeNode)
        {
            sliceBackgroundVolumeNode->GetRASBounds(volumeBounds);
            // xmin,ymax,ymin,ymax,zmin,zmax
            volumeCenterRAS[0] = (volumeBounds[1] + volumeBounds[0]) / 2.;
            volumeCenterRAS[1] = (volumeBounds[3] + volumeBounds[2]) / 2.;
            volumeCenterRAS[2] = (volumeBounds[5] + volumeBounds[4]) / 2.;
            volumeCenterRAS[3] = 1.;
            this->GetWorldToSliceCoordinates(volumeCenterRAS, centerPosDisplay);
        }
    }
    slicePos[0] = centerPosDisplay[0];
    slicePos[1] = centerPosDisplay[1];
}

```

This assumes you have a function this-\>GetSliceNode() and this-\>GetWorldToSliceCoordinates() in your code, as in [vtkSlicerMarkupsWidgetRepresentation2D](https://github.com/Slicer/Slicer/blob/master/Modules/Loadable/Markups/VTKWidgets/vtkSlicerMarkupsWidgetRepresentation2D.cxx)
