VTK中坐标转换

通过屏幕点击的坐标获取体坐标(2D->3D)

1>获取点击位置的z

  z = renderer->GetZ (static_cast<int>(selectionX),
                      static_cast<int>(selectionY));

renderer中getZ的实现:

  // Error checking
  // Must clear previous errors first.
  while(glGetError() != GL_NO_ERROR)
  {
    ;
  }

  // Turn of texturing in case it is on - some drivers have a problem
  // getting / setting pixels with texturing enabled.
  glDisable( GL_SCISSOR_TEST );
  glPixelStorei( GL_PACK_ALIGNMENT, 1 );

  glReadPixels( x_low, y_low,
                width, height,
                GL_DEPTH_COMPONENT, GL_FLOAT,
                z_data );

if z is 1.0, we assume the user has picked a point on the
screen that has not been rendered into. Use the camera’s focal
point for the z value. The test value .999999 has to be used
instead of 1.0 because for some reason our SGI Infinite Reality
engine won’t return a 1.0 from the zbuffer

如果z为1,则获取相机焦点对应屏幕坐标处的z

    // Get camera focal point and position. Convert to display (screen)
    // coordinates. We need a depth value for z-buffer.
    camera = renderer->GetActiveCamera();
    camera->GetFocalPoint(cameraFP); cameraFP[3] = 1.0;

    renderer->SetWorldPoint(cameraFP[0],cameraFP[1],cameraFP[2],cameraFP[3]);
    renderer->WorldToDisplay();
    displayCoord = renderer->GetDisplayPoint();
    selectionZ = displayCoord[2];

2>通过vtkRenderer设置屏幕屏幕坐标并进行转换

  renderer->SetDisplayPoint (display);
  renderer->DisplayToWorld ();

DisplayToWorld 实际调用:{this->DisplayToView(); this->ViewToWorld();}

DisplayToView实现:

    size = this->VTKWindow->GetSize();
    if (size == NULL)
    {
      return;
    }
    sizex = size[0];
    sizey = size[1];
    //2* (clickX/windowwidth) - 1
    vx = 2.0 * (this->DisplayPoint[0] - sizex*this->Viewport[0])/
      (sizex*(this->Viewport[2]-this->Viewport[0])) - 1.0;
    //2* (clickY/windowheight) - 1
    vy = 2.0 * (this->DisplayPoint[1] - sizey*this->Viewport[1])/
      (sizey*(this->Viewport[3]-this->Viewport[1])) - 1.0;
    vz = this->DisplayPoint[2];

vx和vy一定在区间[-1,1]
ViewToWorld实现:

  // get the perspective transformation from the active camera
  vtkMatrix4x4 *matrix = this->ActiveCamera->
                GetCompositeProjectionTransformMatrix(
                  this->GetTiledAspectRatio(),0,1);

  // use the inverse matrix
  vtkMatrix4x4::Invert(*matrix->Element, mat);

  // Transform point to world coordinates
  result[0] = x;
  result[1] = y;
  result[2] = z;
  result[3] = 1.0;

  vtkMatrix4x4::MultiplyPoint(mat,result,result);

ActiveCamera->GetCompositeProjectionTransformMatrix实现较为复杂,有时间再弄!!

3>通过vtkRenderer获取体坐标并转换

  world = renderer->GetWorldPoint ();

  for (int i=0; i < 3; i++)
  {
    this->PickPosition[i] = world[i] / world[3];
  }
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
### 回答1: 在VTK,要获取鼠标位置的坐标可以通过以下步骤实现: 1. 创建一个vtkRenderWindowInteractor对象,该对象用于与用户交互,并捕获鼠标事件。 2. 使用vtkInteractorStyle类创建一个指定的交互器样式。例如,可以使用vtkInteractorStyleTrackballCamera样式,该样式允许用户通过鼠标操作旋转、缩放和平移场景。 3. 将交互器样式关联到vtkRenderWindowInteractor对象上,使其能够处理鼠标事件。 4. 注册一个监听器或回调函数,用于处理鼠标事件。通过重写OnMouseMove方法,可以捕获鼠标移动事件,并获取鼠标位置的坐标信息。 5. 在回调函数,可以使用vtkRenderWindowInteractor对象的GetEventPosition方法来获取鼠标位置的屏幕坐标。然后,可以使用vtkRenderer对象的ViewportToNormalizedViewport方法将屏幕坐标转换为归一化视口坐标。 6. 如果需要获取世界坐标,则可以结合相机的ProjectionTransform和ViewTransform将归一化视口坐标转换为世界坐标。 以上就是使用VTK获取鼠标位置坐标的基本步骤。根据具体的场景和需求,可能需要进一步处理坐标数据,例如显示坐标或与其他对象进行交互。 ### 回答2: 在使用 VTK(Visualization Toolkit)时,可以通过获取鼠标位置的屏幕坐标,然后转换为 VTK 的世界坐标来实现获取鼠标位置的功能。 首先,我们可以使用 VTK 的RenderWindowInteractor类来获取鼠标事件。创建一个RenderWindowInteractor对象并添加到渲染的窗口: ``` renderWindowInteractor = vtk.vtkRenderWindowInteractor() renderWindowInteractor.SetRenderWindow(renderWindow) ``` 然后,为了获取鼠标位置,可以使用vtkRenderWindowInteractor的GetEventPosition()方法。该方法返回屏幕上的坐标: ``` def mouseMoveCallback(obj, event): x, y = renderWindowInteractor.GetEventPosition() print("鼠标位置坐标:", x, y) renderWindowInteractor.AddObserver("MouseMoveEvent", mouseMoveCallback) ``` 在鼠标移动事件调用这个回调函数,就能获取到鼠标的屏幕坐标。然后,我们可以通过vtkRenderer的两个方法来转换为世界坐标: ``` def convertToWorldCoordinates(renderer, x, y): picker = vtk.vtkWorldPointPicker() picker.Pick(x, y, 0, renderer) worldCoordinates = picker.GetPickPosition() return worldCoordinates worldCoordinates = convertToWorldCoordinates(renderer, x, y) ``` 使用vtkWorldPointPicker的Pick方法将屏幕坐标转换为世界坐标。在上面的代码,0表示z轴的值,renderer表示渲染窗口的renderer。 通过这样的步骤,我们就能够使用VTK获取到鼠标位置的坐标了。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力减肥的小胖子5

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值