# 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:** 1\
**Showing post:** 8

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

---

_[View the full topic](https://discourse.slicer.org/t/center-coordinates-of-volume-slices/23133)._
