C++ 如何使用vtk将窗口坐标映射到对象坐标
我正在做一个vtk程序,因为我需要使用vtk将窗口坐标映射到对象坐标 我的OpenGL代码如下:C++ 如何使用vtk将窗口坐标映射到对象坐标,c++,opengl,vtk,opengl-compat,C++,Opengl,Vtk,Opengl Compat,我正在做一个vtk程序,因为我需要使用vtk将窗口坐标映射到对象坐标 我的OpenGL代码如下: 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 ); glGetInt
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);
我不知道如何使用vtk来实现这一点?任何帮助都将不胜感激。我也在谷歌上搜索并找到了一个解决方案来获得这样的模型视图矩阵
renderWindow->GetRenderers()->GetFirstRenderer()->GetActiveCamera()->GetViewTransformMatrix();
但是我不知道如何在vtk中将窗口坐标映射到对象坐标是的,vtk可以将屏幕坐标映射到世界坐标。 您可以根据自己的需要调整以下代码(仅适用于以下2D案例):
//1。获取小部件中的位置。
int*clickPosition=this->getInteractitor()->GetEventPosition();
const int x=单击位置[0];
const int y=点击位置[1];
// 2. 将屏幕坐标转换为“世界坐标”,即转换为真实坐标。
vtkSmartPointer坐标=vtkSmartPointer::New();
坐标->设置坐标系统显示();
坐标->设定值(x,y,0);
double*worldCoordinates=coordinate->GetComputedWorldValue(小部件->GetRenderWindow()->GetRenderers()->GetFirstRenderer());
双世界X(世界坐标[0]),世界(世界坐标[1]);
这是一个不适定问题,因为您无法从单个2D位置找到深度信息。在一般情况下,没有唯一的解决方案
但也有一些选择:
为什么要使用小部件?我不明白sirAh是的,这是在VTK嵌入QT窗口的环境之外,因此需要访问实际的VTK-widget。哪个VTK小部件,先生。我不太了解VTK小部件。它只是一个
QVTKOpenGLWidget
。感谢您的回答,先生,但我在visualstudio中做
// 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]);