user11100830
user11100830

Reputation:

How to map window coordinates into object coordinates using vtk

I am doing a vtk progarm,In that I need to map window coordinates into object coordinates using vtk

I have the OpenGL code as:

  winX = 0.2;//some float values
  winY = 0.43;//some float values
  double posX, posY, posZ;

glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );
glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ)
gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

I don't know how to do this using vtk?Any help will be highly appreciated.I also googled and find out a solution for getting model view matrix like this

      renderWindow->GetRenderers()->GetFirstRenderer()->GetActiveCamera()->GetViewTransformMatrix();

but I have no idea how to map window coordinates into object coordinates in vtk

Upvotes: 2

Views: 1444

Answers (2)

Cryckx
Cryckx

Reputation: 719

This is an ill-posed problem because you can't find the depth information from a single 2D position. In the general case there isn't an unique solution.

But there exist some options :

  1. You have done a projection from the object coordinates to the screen coordinates and you can keep the depth information somewhere to do the back projection.
  2. You want to get the 3D position of an object on your screen. So you use video games technologies such as ray-tracing. The idea is to send a ray from the camera and to take the intersection between the ray and the object as the object position. It's implemented in vtk https://blog.kitware.com/ray-casting-ray-tracing-with-vtk/enter image description here.

Upvotes: 2

Thomas Lang
Thomas Lang

Reputation: 780

Yes, VTK can map screen coordinates into world coordinates. You can adapt the following code to your needs (below 2D case only):

// 1. Get the position in the widget.
int* clickPosition = this->GetInteractor()->GetEventPosition();
const int x = clickPosition[0];
const int y = clickPosition[1];

// 2. Transform screen coordinates to "world coordinates" i.e. into real coordinates.
vtkSmartPointer<vtkCoordinate> coordinate = vtkSmartPointer<vtkCoordinate>::New();
coordinate->SetCoordinateSystemToDisplay();
coordinate->SetValue(x, y, 0);
double* worldCoordinates = coordinate->GetComputedWorldValue(widget->GetRenderWindow()->GetRenderers()->GetFirstRenderer());

double worldX(worldCoordinates[0]), worldY(worldCoordinates[1]);

Upvotes: 3

Related Questions