Bladeren bron

Add ctkVTKRenderView::lookFromAxis(axis, fov)

Change camera to look from a given axis to the focal point
Julien Finet 14 jaren geleden
bovenliggende
commit
fbb33cdfa9

+ 8 - 0
Libs/Visualization/VTK/Widgets/Testing/Cpp/ctkVTKRenderViewTest1.cpp

@@ -81,6 +81,14 @@ int ctkVTKRenderViewTest1(int argc, char * argv [] )
   // Add actor
   renderView.renderer()->AddActor(sphereActor);
 
+  renderView.lookFromAxis(ctkAxesWidget::Right);
+  renderView.lookFromAxis(ctkAxesWidget::Left, 10);
+  renderView.lookFromAxis(ctkAxesWidget::Anterior, 1.);
+  renderView.lookFromAxis(ctkAxesWidget::Posterior, 1.);
+  renderView.lookFromAxis(ctkAxesWidget::Superior, 0.333333);
+  renderView.lookFromAxis(ctkAxesWidget::Inferior, 0.333333);
+  renderView.lookFromAxis(ctkAxesWidget::None, 100.);
+
   if (!interactive)
     {
     QTimer::singleShot(1000, &app, SLOT(quit()));

+ 51 - 0
Libs/Visualization/VTK/Widgets/ctkVTKRenderView.cpp

@@ -616,3 +616,54 @@ void ctkVTKRenderView::resetFocalPoint()
   double z_center = (bounds[5] + bounds[4]) / 2.0;
   this->setFocalPoint(x_center, y_center, z_center);
 }
+
+//----------------------------------------------------------------------------
+void ctkVTKRenderView::lookFromAxis(const ctkAxesWidget::Axis& axis, double fov)
+{
+  Q_D(ctkVTKRenderView);
+  Q_ASSERT(d->Renderer);
+  if (!d->Renderer->IsActiveCameraCreated())
+    {
+    return;
+    }
+  vtkCamera * camera = d->Renderer->GetActiveCamera();
+  Q_ASSERT(camera);
+  double widefov = fov*3;
+  double* focalPoint = camera->GetFocalPoint();
+  switch (axis)
+    {
+    case ctkAxesWidget::Right:
+      camera->SetPosition(focalPoint[0]+widefov, focalPoint[1], focalPoint[2]);
+      camera->SetViewUp(0, 0, 1);
+      break;
+    case ctkAxesWidget::Left:
+      camera->SetPosition(focalPoint[0]-widefov, focalPoint[1], focalPoint[2]);
+      camera->SetViewUp(0, 0, 1);
+      break;
+    case ctkAxesWidget::Anterior:
+      camera->SetPosition(focalPoint[0], focalPoint[1]+widefov, focalPoint[2]);
+      camera->SetViewUp(0, 0, 1);
+      break;
+    case ctkAxesWidget::Posterior:
+      camera->SetPosition(focalPoint[0], focalPoint[1]-widefov, focalPoint[2]);
+      camera->SetViewUp(0, 0, 1);
+      break;
+    case ctkAxesWidget::Superior:
+      camera->SetPosition(focalPoint[0], focalPoint[1], focalPoint[2]+widefov);
+      camera->SetViewUp(0, 1, 0);
+      break;
+    case ctkAxesWidget::Inferior:
+      camera->SetPosition(focalPoint[0], focalPoint[1], focalPoint[2]-widefov);
+      camera->SetViewUp(0, 1, 0);
+      break;
+    case ctkAxesWidget::None:
+    default:
+      // do nothing
+      return;
+      break;
+    }
+  d->Renderer->ResetCameraClippingRange();
+  camera->ComputeViewPlaneNormal();
+  camera->OrthogonalizeViewUp();
+  d->Renderer->UpdateLightsGeometryToFollowCamera();
+}

+ 7 - 0
Libs/Visualization/VTK/Widgets/ctkVTKRenderView.h

@@ -25,6 +25,7 @@
 #include <QWidget>
 
 // CTK includes
+#include <ctkAxesWidget.h>
 #include <ctkPimpl.h>
 
 #include "ctkVisualizationVTKWidgetsExport.h"
@@ -141,6 +142,12 @@ public slots:
   /// \brief Reset focal point
   /// The visible scene bbox is computed, then the camera is recentered around its centroid.
   void resetFocalPoint();
+  
+  /// \brief Change camera to look from a given axis to the focal point
+  /// Translate/Rotate the camera to look from a given axis
+  /// The Field of View (fov) controls how far from the focal point the 
+  /// camera must be (final_pos = focal_point + 3*fov).
+  void lookFromAxis(const ctkAxesWidget::Axis& axis, double fov = 10.);
 
 public:
   /// Get underlying RenderWindow