c++ - 如何使用 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 中的对象坐标
解决方案
是的,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]);
推荐阅读
- php - Laravel 5.8:迁移外键约束的格式不正确
- python - 如何使用现有数据在 CSV 文件中创建新列,然后将其用作打印排序列表的键
- flexbox - 即使内容很小,div 也会向左移动
- python - 为什么 Anaconda 默认激活 base?
- java - 让用户创建超过 1 个对象
- mysql - SQL插入查询太慢了
- amazon-web-services - dnsmasq 中的 DNS 缓存如何清除
- highcharts - 循环时出现 Hicharts sankey 图错误
- java - Android SDK Random 和 SecureRandom 返回不那么随机的值
- list - 如何使用 append/3 在 prolog 中递归构建列表?