Correctly apply camera matrix

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Correctly apply camera matrix

andyjk
This post was updated on .
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 grab the current camera matrix then reapply it later ?






--
Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
https://public.kitware.com/mailman/listinfo/vtkusers
Reply | Threaded
Open this post in threaded view
|

Re: Correctly apply camera matrix

Ken Martin
The best approach in VTK is to save the key variables, camera position, focal point, vup, dop, view angle. This is because the interaction methods directly adjust those parameters, not the computed matrix.

On Mon, Jul 16, 2018 at 4:13 AM, andyjk <[hidden email]> wrote:
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
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
https://public.kitware.com/mailman/listinfo/vtkusers



--
Ken Martin PhD
Distinguished Engineer
Kitware Inc.
101 East Weaver Street
Carrboro, North Carolina
27510 USA

This communication, including all attachments, contains confidential and legally privileged information, and it is intended only for the use of the addressee.  Access to this email by anyone else is unauthorized. If you are not the intended recipient, any disclosure, copying, distribution or any action taken in reliance on it is prohibited and may be unlawful. If you received this communication in error please notify us immediately and destroy the original message.  Thank you.

_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
https://public.kitware.com/mailman/listinfo/vtkusers
Reply | Threaded
Open this post in threaded view
|

Re: Correctly apply camera matrix

andyjk
Thanks Ken

Looks like this is the sensible way to do it, so I have abandoned the
transformation matrix idea.



--
Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
https://public.kitware.com/mailman/listinfo/vtkusers
Reply | Threaded
Open this post in threaded view
|

Re: Correctly apply camera matrix

Bill Lorensen
These two examples save the camera to a file or store it in field data:

https://lorensen.github.io/VTKExamples/site/Cxx/Utilities/SaveSceneToFieldData/
https://lorensen.github.io/VTKExamples/site/Cxx/Utilities/SaveSceneToFile/


On Mon, Jul 16, 2018 at 9:46 AM, andyjk <[hidden email]> wrote:

> Thanks Ken
>
> Looks like this is the sensible way to do it, so I have abandoned the
> transformation matrix idea.
>
>
>
> --
> Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>
> Search the list archives at: http://markmail.org/search/?q=vtkusers
>
> Follow this link to subscribe/unsubscribe:
> https://public.kitware.com/mailman/listinfo/vtkusers



--
Unpaid intern in BillsParadise at noware dot com
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
https://public.kitware.com/mailman/listinfo/vtkusers