如何使用 vtk 将 window 坐标映射到对象坐标

How to map window coordinates into object coordinates using vtk

我正在做一个 vtk 程序,因为我需要使用 vtk

将 window 坐标映射到对象坐标

我的 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 );
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 来做到这一点?任何帮助都非常重要 appreciated.I 还用谷歌搜索并找到了一个解决方案来获得像这样的模型视图矩阵

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

但我不知道如何将 window 坐标映射到 vtk

中的对象坐标

是的,VTK 可以将屏幕坐标映射到世界坐标。 您可以根据需要调整以下代码(仅限二维情况下):

// 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]);

这是一个不适定问题,因为您无法从单个 2D 位置找到深度信息。在一般情况下,没有唯一的解决方案。

但是有一些选择:

  1. 您已经完成了从物体坐标到屏幕坐标的投影,您可以将深度信息保存在某处以进行反投影。
  2. 您想获取屏幕上对象的 3D 位置。所以你使用视频游戏技术,比如光线追踪。思路是从相机发出一条射线,将射线与物体的交点作为物体位置。它在 vtk https://blog.kitware.com/ray-casting-ray-tracing-with-vtk/ 中实现。