首页 > 解决方案 > 如何使用 vtk 将窗口坐标映射到对象坐标

问题描述

我正在做一个 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 );
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 中的对象坐标

标签: c++openglvtkopengl-compat

解决方案


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

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

推荐阅读