Browse Source

Merge topic 'ctkVTKRenderView'

  ENH: ctkVTKRenderView - Added function setFocalPoint and resetFocalPoint
  ENH: ctkVTKRenderView - Add pitch, yaw, roll feature
  ENH: ctkVTKRenderView - Add zoomIn, zoomOut and setZoomFactor method
Jean-Christophe Fillion-Robin 15 years ago
parent
commit
abe48772a7

+ 147 - 5
Libs/Visualization/VTK/Widgets/ctkVTKRenderView.cpp

@@ -31,6 +31,7 @@
 #include <vtkRendererCollection.h>
 #include <vtkRenderWindowInteractor.h>
 #include <vtkTextProperty.h>
+#include <vtkCamera.h>
 
 //--------------------------------------------------------------------------
 static ctkLogger logger("org.commontk.visualization.vtk.widgets.ctkVTKRenderView");
@@ -49,6 +50,11 @@ ctkVTKRenderViewPrivate::ctkVTKRenderViewPrivate()
   this->CornerAnnotation = vtkSmartPointer<vtkCornerAnnotation>::New();
   this->RenderPending = false;
   this->RenderEnabled = false;
+  this->ZoomFactor = 0.05;
+  this->RotateDegrees = 5;
+  this->PitchDirection = ctkVTKRenderView::PitchUp;
+  this->RollDirection = ctkVTKRenderView::RollRight;
+  this->YawDirection = ctkVTKRenderView::YawLeft;
 }
 
 // --------------------------------------------------------------------------
@@ -93,6 +99,24 @@ void ctkVTKRenderViewPrivate::setupDefaultInteractor()
   p->setInteractor(this->RenderWindow->GetInteractor());
 }
 
+//----------------------------------------------------------------------------
+void ctkVTKRenderViewPrivate::zoom(double zoomFactor)
+{
+  Q_ASSERT(this->Renderer->IsActiveCameraCreated());
+  vtkCamera * camera = this->Renderer->GetActiveCamera();
+
+  if (camera->GetParallelProjection())
+    {
+    camera->SetParallelScale(camera->GetParallelScale() / (1 + zoomFactor));
+    }
+  else
+    {
+    camera->Dolly(1 + zoomFactor);
+    this->Renderer->ResetCameraClippingRange();
+    this->Renderer->UpdateLightsGeometryToFollowCamera();
+    }
+}
+
 //---------------------------------------------------------------------------
 // ctkVTKRenderView methods
 
@@ -112,11 +136,6 @@ ctkVTKRenderView::ctkVTKRenderView(QWidget* _parent) : Superclass(_parent)
   d->setupDefaultInteractor();
 }
 
-// --------------------------------------------------------------------------
-ctkVTKRenderView::~ctkVTKRenderView()
-{
-}
-
 //----------------------------------------------------------------------------
 void ctkVTKRenderView::scheduleRender()
 {
@@ -267,3 +286,126 @@ CTK_GET_CXX(ctkVTKRenderView, vtkRenderer*, renderer, Renderer);
 //----------------------------------------------------------------------------
 CTK_SET_CXX(ctkVTKRenderView, bool, setRenderEnabled, RenderEnabled);
 CTK_GET_CXX(ctkVTKRenderView, bool, renderEnabled, RenderEnabled);
+
+//----------------------------------------------------------------------------
+CTK_GET_CXX(ctkVTKRenderView, int, rotateDegrees, RotateDegrees);
+
+//----------------------------------------------------------------------------
+void ctkVTKRenderView::setRotateDegrees(int newRotateDegrees)
+{
+  CTK_D(ctkVTKRenderView);
+  d->RotateDegrees = qAbs(newRotateDegrees);
+}
+
+//----------------------------------------------------------------------------
+CTK_GET_CXX(ctkVTKRenderView, ctkVTKRenderView::PitchDirection, pitchDirection, PitchDirection);
+CTK_SET_CXX(ctkVTKRenderView, ctkVTKRenderView::PitchDirection, setPitchDirection, PitchDirection);
+
+//----------------------------------------------------------------------------
+CTK_GET_CXX(ctkVTKRenderView, ctkVTKRenderView::RollDirection, rollDirection, RollDirection);
+CTK_SET_CXX(ctkVTKRenderView, ctkVTKRenderView::RollDirection, setRollDirection, RollDirection);
+
+//----------------------------------------------------------------------------
+CTK_GET_CXX(ctkVTKRenderView, ctkVTKRenderView::YawDirection, yawDirection, YawDirection);
+CTK_SET_CXX(ctkVTKRenderView, ctkVTKRenderView::YawDirection, setYawDirection, YawDirection);
+
+//----------------------------------------------------------------------------
+void ctkVTKRenderView::pitch()
+{
+  CTK_D(ctkVTKRenderView);
+  if (!d->Renderer->IsActiveCameraCreated())
+    {
+    return;
+    }
+  vtkCamera *cam = d->Renderer->GetActiveCamera();
+  cam->Elevation(d->PitchDirection == Self::PitchDown ? d->RotateDegrees : -d->RotateDegrees);
+  cam->OrthogonalizeViewUp();
+  d->Renderer->UpdateLightsGeometryToFollowCamera();
+}
+
+//----------------------------------------------------------------------------
+void ctkVTKRenderView::roll()
+{
+  CTK_D(ctkVTKRenderView);
+  if (!d->Renderer->IsActiveCameraCreated())
+    {
+    return;
+    }
+  vtkCamera *cam = d->Renderer->GetActiveCamera();
+  cam->Roll(d->RollDirection == Self::RollLeft ? d->RotateDegrees : -d->RotateDegrees);
+  cam->OrthogonalizeViewUp();
+  d->Renderer->UpdateLightsGeometryToFollowCamera();
+}
+
+//----------------------------------------------------------------------------
+void ctkVTKRenderView::yaw()
+{
+  CTK_D(ctkVTKRenderView);
+  if (!d->Renderer->IsActiveCameraCreated())
+    {
+    return;
+    }
+  vtkCamera *cam = d->Renderer->GetActiveCamera();
+  cam->Azimuth(d->YawDirection == Self::YawLeft ? d->RotateDegrees : -d->RotateDegrees);
+  cam->OrthogonalizeViewUp();
+  d->Renderer->UpdateLightsGeometryToFollowCamera();
+}
+
+//----------------------------------------------------------------------------
+void ctkVTKRenderView::setZoomFactor(double newZoomFactor)
+{
+  CTK_D(ctkVTKRenderView);
+  d->ZoomFactor = qBound(0.0, qAbs(newZoomFactor), 1.0);
+}
+
+//----------------------------------------------------------------------------
+CTK_GET_CXX(ctkVTKRenderView, double, zoomFactor, ZoomFactor);
+
+//----------------------------------------------------------------------------
+void ctkVTKRenderView::zoomIn()
+{
+  CTK_D(ctkVTKRenderView);
+  if (!d->Renderer->IsActiveCameraCreated())
+    {
+    return;
+    }
+  d->zoom(d->ZoomFactor);
+}
+
+//----------------------------------------------------------------------------
+void ctkVTKRenderView::zoomOut()
+{
+  CTK_D(ctkVTKRenderView);
+  if (!d->Renderer->IsActiveCameraCreated())
+    {
+    return;
+    }
+  d->zoom(-d->ZoomFactor);
+}
+
+//----------------------------------------------------------------------------
+void ctkVTKRenderView::setFocalPoint(int x, int y, int z)
+{
+  CTK_D(ctkVTKRenderView);
+  if (!d->Renderer->IsActiveCameraCreated())
+    {
+    return;
+    }
+  vtkCamera * camera = d->Renderer->GetActiveCamera();
+  camera->SetFocalPoint(x, y, z);
+  camera->ComputeViewPlaneNormal();
+  camera->OrthogonalizeViewUp();
+  d->Renderer->UpdateLightsGeometryToFollowCamera();
+}
+
+//----------------------------------------------------------------------------
+void ctkVTKRenderView::resetFocalPoint()
+{
+  CTK_D(ctkVTKRenderView);
+  double bounds[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
+  d->Renderer->ComputeVisiblePropBounds(bounds);
+  double x_center = (bounds[1] + bounds[0]) / 2.0;
+  double y_center = (bounds[3] + bounds[2]) / 2.0;
+  double z_center = (bounds[5] + bounds[4]) / 2.0;
+  this->setFocalPoint(x_center, y_center, z_center);
+}

+ 67 - 1
Libs/Visualization/VTK/Widgets/ctkVTKRenderView.h

@@ -44,11 +44,25 @@ class CTK_VISUALIZATION_VTK_WIDGETS_EXPORT ctkVTKRenderView : public QWidget
   Q_PROPERTY(bool renderEnabled READ renderEnabled WRITE setRenderEnabled)
   Q_PROPERTY(bool orientationWidgetVisible READ orientationWidgetVisible
              WRITE setOrientationWidgetVisible)
+  Q_PROPERTY(double zoomFactor READ zoomFactor WRITE setZoomFactor)
+  Q_PROPERTY(int rotateDegrees READ rotateDegrees WRITE setRotateDegrees)
+  Q_ENUMS(PitchDirection)
+  Q_PROPERTY(PitchDirection pitchDirection READ pitchDirection WRITE setPitchDirection)
+  Q_ENUMS(RollDirection)
+  Q_PROPERTY(RollDirection rollDirection READ rollDirection WRITE setRollDirection)
+  Q_ENUMS(YawDirection)
+  Q_PROPERTY(YawDirection yawDirection READ yawDirection WRITE setYawDirection)
+
 public:
+
+  enum PitchDirection { PitchUp, PitchDown };
+  enum RollDirection {RollLeft, RollRight};
+  enum YawDirection {YawLeft, YawRight};
+
   /// Constructors
   typedef QWidget   Superclass;
   explicit ctkVTKRenderView(QWidget* parent = 0);
-  virtual ~ctkVTKRenderView();
+  virtual ~ctkVTKRenderView(){}
 
 public slots:
 
@@ -70,6 +84,43 @@ public slots:
   /// Show/Hide Orientation widget
   void setOrientationWidgetVisible(bool visible);
 
+  /// Set absolute amount degrees the view should be either pitched, rolled or yawed with.
+  /// \sa pitch setPitchDirection roll setRollDirection yaw setYawDirection
+  /// \note The default is 5 degrees
+  void setRotateDegrees(int newRotateDegrees);
+
+  /// Pitch view of X degrees. X been set using setRotateDegrees
+  /// \sa setRotateDegrees
+  void pitch();
+
+  /// Rool view of X degrees. X been set using setRotateDegrees
+  /// \sa setRotateDegrees
+  void roll();
+
+  /// Yaw view of X degrees. X been set using setRotateDegrees
+  /// \sa setRotateDegrees
+  void yaw();
+
+  /// \brief Set zoom factor
+  /// zoomFactor is a value between 0.0 and 1.0
+  /// \note The default value is 0.05
+  void setZoomFactor(double newZoomFactor);
+
+  /// Zoom in using the \a zoomfactor
+  /// \sa setZoomFactor
+  void zoomIn();
+
+  /// Zoom out using the \a zoomfactor
+  /// \sa setZoomFactor
+  void zoomOut();
+
+  /// Set the focal point
+  void setFocalPoint(int x, int y, int z);
+
+  /// \brief Reset focal point
+  /// The visible scene bbox is computed, then the camera is recentered around its centroid.
+  void resetFocalPoint();
+
 public:
   /// Get underlying RenderWindow
   vtkRenderWindow* renderWindow()const;
@@ -101,6 +152,21 @@ public:
 
   /// Return if rendering is enabled
   bool renderEnabled() const;
+
+  /// Return amount of degrees used to either pitch, roll or yaw the view
+  int rotateDegrees()const;
+
+  PitchDirection pitchDirection()const;
+  void setPitchDirection(PitchDirection newPitchDirection);
+
+  RollDirection rollDirection()const;
+  void setRollDirection(RollDirection newRollDirection);
+
+  YawDirection yawDirection()const;
+  void setYawDirection(YawDirection newYawDirection);
+
+  /// Return zoom factor
+  double zoomFactor()const;
   
 private:
   CTK_DECLARE_PRIVATE(ctkVTKRenderView);

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

@@ -50,6 +50,8 @@ class ctkVTKRenderViewPrivate : public QObject,
 public:
   ctkVTKRenderViewPrivate();
 
+  void zoom(double zoomFactor);
+
   /// Convenient setup methods
   void setupCornerAnnotation();
   void setupRendering();
@@ -64,6 +66,11 @@ public:
   vtkSmartPointer<vtkAxesActor>                 Axes;
   vtkSmartPointer<vtkOrientationMarkerWidget>   Orientation;
   vtkSmartPointer<vtkCornerAnnotation>          CornerAnnotation;
+  double                                        ZoomFactor;
+  int                                           RotateDegrees;
+  ctkVTKRenderView::PitchDirection              PitchDirection;
+  ctkVTKRenderView::RollDirection               RollDirection;
+  ctkVTKRenderView::YawDirection                YawDirection;
 
   vtkWeakPointer<vtkRenderWindowInteractor>     CurrentInteractor;