Search code examples
c++vtkpoint-cloud-libraryqvtkwidget

How to rotate a vtk scene around a specific point?


I have a 3D vtk scene representing a point cloud, displayed through a QVTKWidget. vtk7.1, Qt5.8.

I want to be able to rotate the scene around specific coordinates, but I don't know how to proceed.

I like the trackball interaction. I just need to set the center, but I'm a bit lost in VTK api. I think I can do this by changing the rotation matrix : InvTranslation + Rotation + Translation should do the trick. I see two ways of doing it :

1)

  • Get the Rotation Matrix computed by VTK
  • Generate a new matrix
  • Apply the matrix.

2)

  • set a transform to vtk to apply before the process
  • set a transform to vtk to apply after the process

Am I in the right direction? If yes, how I can implement one of these solutions..?

Thank i advance,

Etienne.


Solution

  • Problem solved. The change of focale would also change the view. SO, I aplied a few geometric transform, and there it is.

    // vtk Element /////////////////////////////////////////////////////////
    vtkRenderWindowInteractor *rwi = widget->GetInteractor();
    vtkRenderer *renderer = widget->GetRenderWindow()->GetRenderers()->GetFirstRenderer();
    vtkCamera *camera = renderer->GetActiveCamera();
    
    // Camera Parameters ///////////////////////////////////////////////////
    double *focalPoint = camera->GetFocalPoint();
    double *viewUp = camera->GetViewUp();
    double *position = camera->GetPosition();
    double axis[3];
    axis[0] = -camera->GetViewTransformMatrix()->GetElement(0,0);
    axis[1] = -camera->GetViewTransformMatrix()->GetElement(0,1);
    axis[2] = -camera->GetViewTransformMatrix()->GetElement(0,2);
    
    // Build The transformatio /////////////////////////////////////////////////
    vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
    transform->Identity();
    
    transform->Translate(d->center[0], d->center[1], d->center[2]);
    transform->RotateWXYZ(rxf, viewUp); // Azimuth
    transform->RotateWXYZ(ryf, axis);   // Elevation
    transform->Translate(-d->center[0], -d->center[1], -d->center[2]);
    
    double newPosition[3];
    transform->TransformPoint(position,newPosition); // Transform Position
    double newFocalPoint[3];
    transform->TransformPoint(focalPoint, newFocalPoint); // Transform Focal Point
    
    camera->SetPosition(newPosition);
    camera->SetFocalPoint(newFocalPoint);
    
    // Orhthogonalize View Up //////////////////////////////////////////////////
    camera->OrthogonalizeViewUp();
    renderer->ResetCameraClippingRange();
    
    rwi->Render();