Reputation:
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
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 :
Upvotes: 2
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