vtk points from display to world

points from display to world

目的

  • 已知條件
    • 相機的位置 compose
    • 相機的perspective transformation Matrix proj_mat
    • 該視角下的一張深度圖zbuffer
    • 該視角下的關於mesh的shoot img
  • 所求
    • img中的每一個像素的顯示座標系的座標 ⇒ 世界座標系下的座標

過程(前提:mesh歸一化)

  • init display position
# i,j爲img的某個像素的位置,pixel_d是zbuffer的i,j位置對應的值
  display_pt_[0] = i;
  display_pt_[1] = j;
  display_pt_[2] = 1.0 - pixel_d / 256;
void vtkViewport::DisplayToView()
{
  if ( this->VTKWindow )
  {
    double vx,vy,vz;
    int sizex,sizey;

    /* get physical window dimensions */
    const int *size = this->VTKWindow->GetSize();
    if (size == nullptr)
    {
      return;
    }
    sizex = size[0];
    sizey = size[1];

    vx = 0.0;
    if (sizex != 0.0)
    {
      vx = 2.0 * (this->DisplayPoint[0] - sizex*this->Viewport[0])/
        (sizex*(this->Viewport[2]-this->Viewport[0])) - 1.0;
    }

    vy = 0.0;
    if (sizey != 0.0)
    {
      vy = 2.0 * (this->DisplayPoint[1] - sizey*this->Viewport[1])/
        (sizey*(this->Viewport[3]-this->Viewport[1])) - 1.0;
    }

    vz = this->DisplayPoint[2];

    this->SetViewPoint(vx,vy,vz);
  }
}

void vtkRenderer::ViewToWorld()
{
  double result[4];
  result[0] = this->ViewPoint[0];
  result[1] = this->ViewPoint[1];
  result[2] = this->ViewPoint[2];
  result[3] = 1.0;
  this->ViewToWorld(result[0],result[1],result[2]);
  this->SetWorldPoint(result);
}

void vtkRenderer::ViewToWorld(double &x, double &y, double &z)
{
  double mat[16];
  double result[4];

  if (this->ActiveCamera == nullptr)
  {
    vtkErrorMacro("ViewToWorld: no active camera, cannot compute view to world, returning 0,0,0");
    x = y = z = 0.0;
    return;
  }

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

  // Get the transformed vector & set WorldPoint
  // while we are at it try to keep w at one
  if (result[3])
  {
    x = result[0] / result[3];
    y = result[1] / result[3];
    z = result[2] / result[3];
  }
}

參考

vtk中使用查找點集中離目標點最近的k個點

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章