[vtkusers] Correctly apply camera matrix

andyjk andrewkeeling at hotmail.com
Mon Jul 16 04:13:38 EDT 2018


I am trying to save a particular camera view, then reapply it later (ie be
able to reset the the view to a previous position)

I see from here (
http://vtk.1045678.n5.nabble.com/How-to-restore-a-camera-view-td5728916.html
) that I can do this by saving  position, focal_point, View-up and
view_angle,

but I am trying to do this more neatly by saving just the 4x4 transformation
matrix of the camera (and assuming view angle is unchanged).

How can I grab the current camera transformation matrix, and later re-apply
it ?

I have tried :

1) GRAB
vtkSmartPointer<vtkMatrix4x4> mat;
		mat =
iren->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->GetActiveCamera()->GetModelViewTransformMatrix();

		saveVTKMat4x4(mat, "VTKCamera.vmx");

2) LOAD and APPLY
loadVTKMat4x4(mat, "VTKCamera.vmx");
		mat->Print(std::cout);


		vtkSmartPointer<vtkMatrix4x4> mat_inverse =
vtkSmartPointer<vtkMatrix4x4>::New();
		vtkMatrix4x4::Invert(mat, mat_inverse);
		vtkSmartPointer<vtkTransform> transform =
vtkSmartPointer<vtkTransform>::New();
		transform->SetMatrix(mat);
		transform->Update();


		vtkSmartPointer<vtkMatrix4x4> current_mat;
		current_mat = custom_camera->GetViewTransformMatrix();
		
		vtkSmartPointer<vtkMatrix4x4> current_mat_inverse =
vtkSmartPointer<vtkMatrix4x4>::New();
		vtkMatrix4x4::Invert(current_mat, current_mat_inverse);

		vtkSmartPointer<vtkTransform> transformInv =
vtkSmartPointer<vtkTransform>::New();
		transformInv->SetMatrix(current_mat_inverse);
		transformInv->Update();


		custom_camera->ApplyTransform(transformInv);
		custom_camera->Modified();
		custom_camera->ApplyTransform(transform);


[Where the save/load functions have been verified using vtk::print and are
working fine]

I presume when loading, I have to move the camera back to identity (via an
inverse transform) before applying the new transform because I think
ApplyTransform concatenates.

I find that if I don't move the camera at all between saving and loading, it
works.
If I nudge the camera a tiny bit between loading and saving, the new view is
displaced a bit more when loading, so I think I must be getting some inverse
transforms muddled up?

Is there a neat way to the current camera matrix then reapply it later ?






--
Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html


More information about the vtkusers mailing list