[vtkusers] Load/save the camera position to file and store/restore a 3d scene

Franke, Stephan Stephan.Franke at inpro.de
Thu Sep 29 05:53:10 EDT 2005


 
Hi,
 
I have problems to save/load the camera position in vtk. 
When I save the camera position I read out from the active camera the
focalpoint, the position, the viewup vector, the viewangle
and the parallelscale (like described in many vtk mails) to file. These
parameters I use to restore the scene. But after the restoring pocedure
I did not got the same 
scene like I aspect. It seams that the camera is not lying correctly in
space. The restored scene did no look like the scene before the saving
operation.
So I stored the transformation matrix of the ViewTransformObject from
the camera too to use it during the restoring procedure.
This works and I got the scene which I aspect. But following rotations,
translations or zooms with mouse/keyboard event adjusted the camera 
somewhere in space. That means when I do only a rotation about the x
axis (after restoring) I got something as result but not the rotation
about the x axis.
Did someone had the same problem and can tell me what I am doing wrong?
I did not have any idea what I can do more to get a stabile restore
scene on which 
following rotation, translation operation where execute correctly. All
transformation in my application where executed only to the camera of
the renderer.
To the bottom of the email I attached the functions for save and load
the camera position.
 
Thanks in advice for your help.
Kind regards,
Stephan Franke
 
Save:
...
        // save the active global transformation matrix to file
        pFile = new QFile(m_sFileName);
        if(pFile->open(IO_WriteOnly))
        {
             pValues = m_pDocument->GetCamera()->GetFocalPoint();
            sLine.sprintf("FOCALPOINT: %f %f %f", pValues[0],
                pValues[1], pValues[3]);
            sLines += sLine;
            sLine = "";
 
            pValues = m_pDocument->GetCamera()->GetPosition();
            sLine.sprintf("POSITION: %f %f %f", pValues[0],
                pValues[1], pValues[2]);
            sLines += sLine;
            sLine = "";       
 
            pValues = m_pDocument->GetCamera()->GetViewUp();
            sLine.sprintf("VIEWUP: %f %f %f", pValues[0], pValues[1],
                pValues[2]);
            sLines += sLine;
            sLine = "";
 
            dValue = m_pDocument->GetCamera()->GetViewAngle();
            sLine.sprintf("VIEW ANGLE: %f ", dValue);
            sLines += sLine;
            sLine = "";
 
            pValues = m_pDocument->GetCamera()->GetViewPlaneNormal();
            sLine.sprintf("VIEWPLANENORMAL: %f %f %f", pValues[0],
pValues[1],
                pValues[2]);
            sLines += sLine;
            sLine = "";
 
            dValue = m_pDocument->GetCamera()->GetParallelScale();
            sLine.sprintf("PARALLELSCALE: %f", dValue);
            sLines += sLine;
            sLine = "";
  ...
 
Load:
...
        // read the transformation for the current position to which the
camera has
        // to be moved.
        pCamera = m_pDocument->GetCamera();
        
        Reader.readLine();
 
        sLine = Reader.readLine();
        sLine.remove("FOCALPOINT: ");
        pCamera->SetFocalPoint((sLine.section(' ', 0, 0)).toDouble(), 
            (sLine.section(' ', 1, 1)).toDouble(), 
            (sLine.section(' ', 2, 2)).toDouble());
 
        sLine = Reader.readLine();
        sLine.remove("POSITION: ");
        pCamera->SetPosition((sLine.section(' ', 0, 0)).toDouble(),
            (sLine.section(' ', 1, 1)).toDouble(), 
            (sLine.section(' ', 2, 2)).toDouble());
 
        sLine = Reader.readLine();
        sLine.remove("VIEWUP: ");
        pCamera->SetViewUp((sLine.section(' ', 0, 0)).toDouble(), 
            (sLine.section(' ', 1, 1)).toDouble(), 
            (sLine.section(' ', 2, 2)).toDouble());
 
        sLine = Reader.readLine();
        sLine.remove("VIEW ANGLE: ");
        pCamera->SetViewAngle((sLine.section(' ', 0, 0)).toDouble());
 
        sLine = Reader.readLine();
        sLine.remove("VIEWPLANENORMAL: ");
        pCamera->SetViewPlaneNormal((sLine.section(' ', 0,
0)).toDouble(), 
            (sLine.section(' ', 1, 1)).toDouble(), 
            (sLine.section(' ', 2, 2)).toDouble());
 
        sLine = Reader.readLine();
        sLine.remove("PARALLELSCALE: ");
        pCamera->SetParallelScale((sLine.section(' ', 0,
0)).toDouble());
 
        // read the view transformation matrix
        pMatrix = pCamera->GetViewTransformMatrix();
        pMatrix->Identity();
       
        // jump over the matrix marker
        Reader.readLine();
 
        // first row
        sLine = Reader.readLine();
        pMatrix->SetElement(0, 0, (sLine.section(' ', 0,
0)).toDouble());
        pMatrix->SetElement(0, 1, (sLine.section(' ', 1,
1)).toDouble());
        pMatrix->SetElement(0, 2, (sLine.section(' ', 2,
2)).toDouble());
        pMatrix->SetElement(0, 3, (sLine.section(' ', 3,
3)).toDouble());
 
        // second row
        sLine = Reader.readLine();
        pMatrix->SetElement(1, 0, (sLine.section(' ', 0,
0)).toDouble());
        pMatrix->SetElement(1, 1, (sLine.section(' ', 1,
1)).toDouble());
        pMatrix->SetElement(1, 2, (sLine.section(' ', 2,
2)).toDouble());
        pMatrix->SetElement(1, 3, (sLine.section(' ', 3,
3)).toDouble());
 
        // third row
        sLine = Reader.readLine();
        pMatrix->SetElement(2, 0, (sLine.section(' ', 0,
0)).toDouble());
        pMatrix->SetElement(2, 1, (sLine.section(' ', 1,
1)).toDouble());
        pMatrix->SetElement(2, 2, (sLine.section(' ', 2,
2)).toDouble());
        pMatrix->SetElement(2, 3, (sLine.section(' ', 3,
3)).toDouble());
 
        // fourth row
        sLine = Reader.readLine();
        pMatrix->SetElement(3, 0, (sLine.section(' ', 0,
0)).toDouble());
        pMatrix->SetElement(3, 1, (sLine.section(' ', 1,
1)).toDouble());
        pMatrix->SetElement(3, 2, (sLine.section(' ', 2,
2)).toDouble());
        pMatrix->SetElement(3, 3, (sLine.section(' ', 3,
3)).toDouble());
        pCamera->Modified();
        
        // show the new view parameter to the user
        m_pDocument->GetRenderer()->ResetCameraClippingRange();
   ...         
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20050929/75d6c0a9/attachment.htm>


More information about the vtkusers mailing list