Center coordinates of volume slices?

Think I got it working (C++) by

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

1 Like