]> Creatis software - cpPlugins.git/blobdiff - lib/cpExtensions/Visualization/BaseInteractorStyle.cxx
Intermediary commit
[cpPlugins.git] / lib / cpExtensions / Visualization / BaseInteractorStyle.cxx
index ba89696a5679d698af2fc10570d242e7e228d890..6b9c8efe30fb3c5dbbebe9752e442a40797f1d98 100644 (file)
@@ -1,6 +1,11 @@
 #include <cpExtensions/Visualization/BaseInteractorStyle.h>
 
+#include <cmath>
+
 #include <vtkCallbackCommand.h>
+#include <vtkCamera.h>
+#include <vtkRenderer.h>
+#include <vtkRenderWindowInteractor.h>
 
 // -------------------------------------------------------------------------
 const long cpExtensions::Visualization::BaseInteractorStyle::MouseButtonEvent::
@@ -23,10 +28,230 @@ DelegateTDxEvent( unsigned long event, void* calldata )
   std::exit( 1 );
 }
 
+// -------------------------------------------------------------------------
+void cpExtensions::Visualization::BaseInteractorStyle::
+OnMouseMove( )
+{
+  // Get current position on the associated actors
+  vtkRenderWindowInteractor* rwi = this->GetInteractor( );
+  if( rwi == NULL )
+    return;
+
+  // Get modifiers
+  bool alt = ( rwi->GetAltKey( ) == 1 );
+  bool ctr = ( rwi->GetControlKey( ) == 1 );
+  bool sft = ( rwi->GetShiftKey( ) == 1 );
+  ButtonID button = this->GetButtonID( );
+
+  // Invoke possible generic events
+  if( button == Self::ButtonID_Right )
+  {
+    if( !alt && !ctr && !sft )
+    {
+      this->FindPokedRenderer(
+        rwi->GetEventPosition( )[ 0 ],
+        rwi->GetEventPosition( )[ 1 ]
+        );
+      this->Dolly( );
+
+    } // fi
+  }
+  else if( button == Self::ButtonID_Middle )
+  {
+    if( !alt && !ctr && !sft )
+    {
+      this->FindPokedRenderer(
+        rwi->GetEventPosition( )[ 0 ],
+        rwi->GetEventPosition( )[ 1 ]
+        );
+      this->Pan( );
+
+    } // fi
+
+  } // fi
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Visualization::BaseInteractorStyle::
+OnLeftButtonDown( )
+{
+  this->ActiveButton = Self::ButtonID_Left;
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Visualization::BaseInteractorStyle::
+OnLeftButtonUp( )
+{
+  this->ActiveButton = Self::ButtonID_None;
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Visualization::BaseInteractorStyle::
+OnMiddleButtonDown( )
+{
+  this->ActiveButton = Self::ButtonID_Middle;
+
+  // Get current position on the associated actors
+  vtkRenderWindowInteractor* rwi = this->GetInteractor( );
+  if( rwi == NULL )
+    return;
+
+  // Get modifiers
+  bool alt = ( rwi->GetAltKey( ) == 1 );
+  bool ctr = ( rwi->GetControlKey( ) == 1 );
+  bool sft = ( rwi->GetShiftKey( ) == 1 );
+
+  if( !alt && !ctr && !sft )
+    this->StartPan( );
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Visualization::BaseInteractorStyle::
+OnMiddleButtonUp( )
+{
+  this->ActiveButton = Self::ButtonID_None;
+
+  // Get current position on the associated actors
+  vtkRenderWindowInteractor* rwi = this->GetInteractor( );
+  if( rwi == NULL )
+    return;
+
+  // Get modifiers
+  bool alt = ( rwi->GetAltKey( ) == 1 );
+  bool ctr = ( rwi->GetControlKey( ) == 1 );
+  bool sft = ( rwi->GetShiftKey( ) == 1 );
+
+  switch( this->State )
+  {
+  case VTKIS_PAN:
+    this->EndPan( );
+    break;
+  default:
+    break;
+  } // hctiws
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Visualization::BaseInteractorStyle::
+OnRightButtonDown( )
+{
+  this->ActiveButton = Self::ButtonID_Right;
+
+  // Get current position on the associated actors
+  vtkRenderWindowInteractor* rwi = this->GetInteractor( );
+  if( rwi == NULL )
+    return;
+
+  // Get modifiers
+  bool alt = ( rwi->GetAltKey( ) == 1 );
+  bool ctr = ( rwi->GetControlKey( ) == 1 );
+  bool sft = ( rwi->GetShiftKey( ) == 1 );
+
+  if( !alt && !ctr && !sft )
+    this->StartDolly( );
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Visualization::BaseInteractorStyle::
+OnRightButtonUp( )
+{
+  this->ActiveButton = Self::ButtonID_None;
+
+  // Get current position on the associated actors
+  vtkRenderWindowInteractor* rwi = this->GetInteractor( );
+  if( rwi == NULL )
+    return;
+
+  // Get modifiers
+  bool alt = ( rwi->GetAltKey( ) == 1 );
+  bool ctr = ( rwi->GetControlKey( ) == 1 );
+  bool sft = ( rwi->GetShiftKey( ) == 1 );
+
+  switch( this->State )
+  {
+  case VTKIS_DOLLY:
+    this->EndDolly( );
+    break;
+  default:
+    break;
+  } // hctiws
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Visualization::BaseInteractorStyle::
+Dolly( )
+{
+  if( this->CurrentRenderer == NULL )
+    return;
+
+  vtkRenderWindowInteractor* rwi = this->GetInteractor( );
+  double *center = this->CurrentRenderer->GetCenter( );
+  int dy = rwi->GetEventPosition( )[ 1 ] - rwi->GetLastEventPosition( )[ 1 ];
+  double dyf = this->MotionFactor * dy / center[ 1 ];
+  this->_Dolly( std::pow( 1.1, dyf ) );
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Visualization::BaseInteractorStyle::
+Pan( )
+{
+  if( this->CurrentRenderer == NULL )
+    return;
+
+  vtkRenderWindowInteractor* rwi = this->Interactor;
+  double viewFocus[ 4 ], focalDepth, viewPoint[ 3 ];
+  double newPickPoint[ 4 ], oldPickPoint[ 4 ], motionVector[ 3 ];
+
+  // Calculate the focal depth since we'll be using it a lot
+  vtkCamera* camera = this->CurrentRenderer->GetActiveCamera( );
+  camera->GetFocalPoint( viewFocus );
+  this->ComputeWorldToDisplay(
+    viewFocus[ 0 ], viewFocus[ 1 ], viewFocus[ 2 ], viewFocus
+    );
+  focalDepth = viewFocus[ 2 ];
+  this->ComputeDisplayToWorld(
+    rwi->GetEventPosition( )[ 0 ],
+    rwi->GetEventPosition( )[ 1 ],
+    focalDepth,
+    newPickPoint
+    );
+
+  // Has to recalc old mouse point since the viewport has moved,
+  // so can't move it outside the loop
+  this->ComputeDisplayToWorld(
+    rwi->GetLastEventPosition( )[ 0 ],
+    rwi->GetLastEventPosition( )[ 1 ],
+    focalDepth,
+    oldPickPoint
+    );
+
+  // Camera motion is reversed
+  motionVector[ 0 ] = oldPickPoint[ 0 ] - newPickPoint[ 0 ];
+  motionVector[ 1 ] = oldPickPoint[ 1 ] - newPickPoint[ 1 ];
+  motionVector[ 2 ] = oldPickPoint[ 2 ] - newPickPoint[ 2 ];
+
+  camera->GetFocalPoint( viewFocus );
+  camera->GetPosition( viewPoint );
+  camera->SetFocalPoint(
+    motionVector[ 0 ] + viewFocus[ 0 ],
+    motionVector[ 1 ] + viewFocus[ 1 ],
+    motionVector[ 2 ] + viewFocus[ 2 ]
+    );
+  camera->SetPosition(
+    motionVector[ 0 ] + viewPoint[ 0 ],
+    motionVector[ 1 ] + viewPoint[ 1 ],
+    motionVector[ 2 ] + viewPoint[ 2 ]
+    );
+  if( rwi->GetLightFollowCamera( ) )
+    this->CurrentRenderer->UpdateLightsGeometryToFollowCamera( );
+  rwi->Render( );
+}
+
 // -------------------------------------------------------------------------
 cpExtensions::Visualization::BaseInteractorStyle::
 BaseInteractorStyle( )
-  : Superclass( )
+  : Superclass( ),
+    MotionFactor( double( 10 ) )
 {
   this->LeftButtonEvent.Reset( );
   this->MiddleButtonEvent.Reset( );
@@ -42,6 +267,27 @@ cpExtensions::Visualization::BaseInteractorStyle::
 {
 }
 
+// -------------------------------------------------------------------------
+void cpExtensions::Visualization::BaseInteractorStyle::
+_Dolly( double factor )
+{
+  if( this->CurrentRenderer == NULL )
+    return;
+
+  vtkCamera* camera = this->CurrentRenderer->GetActiveCamera( );
+  if( camera->GetParallelProjection( ) == 0 )
+  {
+    camera->Dolly( factor );
+    if( this->AutoAdjustCameraClippingRange )
+      this->CurrentRenderer->ResetCameraClippingRange( );
+  }
+  else
+    camera->SetParallelScale( camera->GetParallelScale( ) / factor );
+  if( this->Interactor->GetLightFollowCamera( ) )
+    this->CurrentRenderer->UpdateLightsGeometryToFollowCamera( );
+  this->Interactor->Render( );
+}
+
 // -------------------------------------------------------------------------
 void cpExtensions::Visualization::BaseInteractorStyle::
 _ProcessEvents(
@@ -71,13 +317,13 @@ _ProcessEvents(
       s->OnLeftDoubleClick( );
     else if( nc == 1 )
       s->OnLeftClick( );
-    s->ActiveButton = Self::ButtonID_Left;
+    s->OnLeftButtonDown( );
   }
   break;
   case vtkCommand::LeftButtonReleaseEvent:
   {
     s->LeftButtonEvent.Release( );
-    s->ActiveButton = Self::ButtonID_None;
+    s->OnLeftButtonUp( );
   }
   break;
   case vtkCommand::MiddleButtonPressEvent:
@@ -87,13 +333,13 @@ _ProcessEvents(
       s->OnMiddleDoubleClick( );
     else if( nc == 1 )
       s->OnMiddleClick( );
-    s->ActiveButton = Self::ButtonID_Middle;
+    s->OnMiddleButtonDown( );
   }
   break;
   case vtkCommand::MiddleButtonReleaseEvent:
   {
     s->MiddleButtonEvent.Release( );
-    s->ActiveButton = Self::ButtonID_None;
+    s->OnMiddleButtonUp( );
   }
   break;
   case vtkCommand::RightButtonPressEvent:
@@ -103,13 +349,13 @@ _ProcessEvents(
       s->OnRightDoubleClick( );
     else if( nc == 1 )
       s->OnRightClick( );
-    s->ActiveButton = Self::ButtonID_Right;
+    s->OnRightButtonDown( );
   }
   break;
   case vtkCommand::RightButtonReleaseEvent:
   {
     s->RightButtonEvent.Release( );
-    s->ActiveButton = Self::ButtonID_None;
+    s->OnRightButtonUp( );
   }
   break;
   case vtkCommand::MouseWheelForwardEvent: