[vtkusers] Camera matrix construction (i.e. world to view transformation)
jkamarai
Joni.Kamarainen at lut.fi
Wed Sep 7 04:48:18 EDT 2011
Just for curiosity, would it make sense to add GetCameraMatrix() or something
similar to vtkRenderer class as that would interest people doing computer
vision stuff with VTK - or maybe it already exists somewhere? In my code I
use the following (which is not very elegant, I know):
/**
* @brief Forms a 3x4 camera matrix compatible with VTK coordinates and
* definitions. The camera matrix is constructed using the processing in
* vtkRenderer::WorldToView() and vtkRenderer::ViewToDisplay() and thus
* changing them will certainly affect this.
**/
void ConstructCameraMatrixVTK(vtkRenderer *renderer, double camMatr[][4]) {
double *dispPoint;
double *viewport;
int sizex,sizey;
int *size;
// Window (image) size
size = renderer->GetRenderWindow()->GetSize();
sizex = size[0];
sizey = size[1];
// World to view transformation vPw
viewport = renderer->GetViewport();
vtkSmartPointer<vtkMatrix4x4> vPw =
renderer->GetActiveCamera()->
GetCompositePerspectiveTransformMatrix
(renderer->GetTiledAspectRatio(),0,1);
// View to camera (display) transformation constructed in a way that its
// multiplication with vPw forms the final result, i.e. cPw = cPv*vPw
// see vtkRenderer::ViewToDisplay() for details (and do some matr.
algebra)
vtkSmartPointer<vtkMatrix4x4> cPv = vtkSmartPointer<vtkMatrix4x4>::New();
cPv->SetElement(0,0,sizex*(viewport[2]-viewport[0])/2);
cPv->SetElement(0,1,0);
cPv->SetElement(0,2,0);
cPv->SetElement(0,3,sizex*(viewport[2]+viewport[0])/2);
cPv->SetElement(1,0,0);
cPv->SetElement(1,1,sizey*(viewport[3]-viewport[1])/2);
cPv->SetElement(1,2,0);
cPv->SetElement(1,3,sizey*(viewport[3]+viewport[1])/2);
cPv->SetElement(2,0,0);
cPv->SetElement(2,1,0);
cPv->SetElement(2,2,0);
cPv->SetElement(2,3,1);
cPv->SetElement(3,0,0);
cPv->SetElement(3,1,0);
cPv->SetElement(3,2,0);
cPv->SetElement(3,3,0);
// Form the final matrix
vtkSmartPointer<vtkMatrix4x4> cPw = vtkSmartPointer<vtkMatrix4x4>::New();
vtkMatrix4x4::Multiply4x4(cPv,vPw,cPw);
// Store, not that the result matrix is actually 3x4
camMatr[0][0] = cPw->GetElement(0,0);
camMatr[0][1] = cPw->GetElement(0,1);
camMatr[0][2] = cPw->GetElement(0,2);
camMatr[0][3] = cPw->GetElement(0,3);
camMatr[1][0] = cPw->GetElement(1,0);
camMatr[1][1] = cPw->GetElement(1,1);
camMatr[1][2] = cPw->GetElement(1,2);
camMatr[1][3] = cPw->GetElement(1,3);
camMatr[2][0] = cPw->GetElement(2,0);
camMatr[2][1] = cPw->GetElement(2,1);
camMatr[2][2] = cPw->GetElement(2,2);
camMatr[2][3] = cPw->GetElement(2,3);
}
--
View this message in context: http://vtk.1045678.n5.nabble.com/Camera-matrix-construction-i-e-world-to-view-transformation-tp4777799p4777799.html
Sent from the VTK - Users mailing list archive at Nabble.com.
More information about the vtkusers
mailing list