[vtk-developers] Coordinate conversions (World-Display)

Daniel Haehn haehn at bwh.harvard.edu
Wed Sep 8 13:42:16 EDT 2010


Hi VTK experts,

I use the following functions to convert between World and Display coordinates and vice versa. I found the method on the mailing list or in some other code.

Isn't there an easier way to do that? Is it correct at all and is it robust?

this->GetRenderer is a vtkRenderer.

//---------------------------------------------------------------------------
/// Convert display to world coordinates
double* vtkMRMLAnnotationThreeDViewDisplayableManager::GetDisplayToWorldCoordinates(double x, double y)
{
  double coordinates[3];
  coordinates[0]=(double)x;
  coordinates[1]=(double)y;
  coordinates[2]=0;

  this->GetRenderer()->SetDisplayPoint(coordinates);
  this->GetRenderer()->DisplayToView();
  this->GetRenderer()->GetViewPoint(coordinates);
  this->GetRenderer()->ViewToWorld();
  return this->GetRenderer()->GetWorldPoint();

}

//---------------------------------------------------------------------------
/// Convert world to display coordinates
double* vtkMRMLAnnotationThreeDViewDisplayableManager::GetWorldToDisplayCoordinates(double * worldPoints)
{

  vtkDebugMacro("GetWorldToDisplayCoordinates: RAS " << worldPoints[0] << ", " << worldPoints[1] << ", " << worldPoints[2])

  double * displayCoordinates;

  this->GetRenderer()->SetWorldPoint(worldPoints);
  this->GetRenderer()->WorldToView();
  displayCoordinates = this->GetRenderer()->GetViewPoint();
  this->GetRenderer()->ViewToDisplay();

  double * output = this->GetRenderer()->GetDisplayPoint();

  vtkDebugMacro("GetWorldToDisplayCoordinates: Display " << output[0] << ", " << output[1])

  return output;

}

Thank you,
Daniel


More information about the vtk-developers mailing list