Back to index

salome-gui  6.5.0
SVTK_InteractorStyle.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SALOME VTKViewer : build VTK viewer into Salome desktop
00024 //  File   : 
00025 //  Author : 
00026 
00027 #include "SVTK_InteractorStyle.h"
00028 
00029 #include "VTKViewer_Algorithm.h"
00030 #include "VTKViewer_Utilities.h"
00031 #include "SVTK_GenericRenderWindowInteractor.h"
00032 
00033 #include "SVTK_Selection.h"
00034 #include "SVTK_Event.h" 
00035 #include "SVTK_Selector.h"
00036 #include "SVTK_Functor.h"
00037 #include "SVTK_Actor.h"
00038 
00039 #include "VTKViewer_Algorithm.h"
00040 #include "SVTK_Functor.h"
00041 
00042 #include "SUIT_Tools.h"
00043 #include "SALOME_Actor.h"
00044 
00045 #include <vtkObjectFactory.h>
00046 #include <vtkMath.h>
00047 #include <vtkCommand.h>
00048 #include <vtkCamera.h>
00049 #include <vtkRenderer.h>
00050 #include <vtkPointPicker.h>
00051 #include <vtkRenderWindow.h>
00052 #include <vtkRenderWindowInteractor.h>
00053 #include <vtkCallbackCommand.h>
00054 #include <vtkRendererCollection.h>
00055 #include <vtkDataSet.h>
00056 #include <vtkPerspectiveTransform.h> 
00057 #include <vtkMatrix4x4.h>
00058 
00059 #include <QPixmap>
00060 #include <QWidget>
00061 #include <QRubberBand>
00062 
00063 #include <algorithm>
00064 #include <iostream>
00065 
00066 namespace
00067 {
00068   inline void GetEventPosition(vtkRenderWindowInteractor* theInteractor,
00069                                int& theX, 
00070                                int& theY)
00071   {
00072     theInteractor->GetEventPosition(theX,theY);
00073     theY = theInteractor->GetSize()[1] - theY - 1;
00074   }
00075 }
00076 
00077 
00078 vtkStandardNewMacro(SVTK_InteractorStyle);
00079 
00080 
00084 SVTK_InteractorStyle::SVTK_InteractorStyle():
00085   mySelectionEvent(new SVTK_SelectionEvent()),
00086   myPointPicker(vtkPointPicker::New()),
00087   myLastHighlitedActor(NULL),
00088   myLastPreHighlitedActor(NULL),
00089   myControllerIncrement(SVTK_ControllerIncrement::New()),
00090   myControllerOnKeyDown(SVTK_ControllerOnKeyDown::New()),
00091   myHighlightSelectionPointActor(SVTK_Actor::New()),
00092   myRectBand(0),
00093   myIsAdvancedZoomingEnabled(false)
00094 {
00095   myPointPicker->Delete();
00096 
00097   myPointPicker->SetTolerance(0.025);
00098 
00099   this->MotionFactor = 10.0;
00100   this->State = VTK_INTERACTOR_STYLE_CAMERA_NONE;
00101   this->RadianToDegree = 180.0 / vtkMath::Pi();
00102   this->ForcedState = VTK_INTERACTOR_STYLE_CAMERA_NONE;
00103 
00104   loadCursors();
00105 
00106   // set custom event handling function (to handle 3d space mouse events)
00107   EventCallbackCommand->SetCallback( SVTK_InteractorStyle::ProcessEvents );
00108 
00109   // set default values of properties.  user may edit them in preferences.
00110   mySMDecreaseSpeedBtn = 1;
00111   mySMIncreaseSpeedBtn = 2;
00112   mySMDominantCombinedSwitchBtn = 9;
00113   //
00114   myControllerIncrement->Delete();
00115   myControllerOnKeyDown->Delete();
00116 
00117   myCurrRotationPointType = SVTK::SetRotateGravity;
00118   myPrevRotationPointType = myCurrRotationPointType;
00119 
00120   myCurrFocalPointType = SVTK::SetFocalPointSelected;
00121   myPrevFocalPointType = myCurrFocalPointType;
00122 
00123   myHighlightSelectionPointActor->Delete();
00124   myHighlightSelectionPointActor->Initialize();
00125   myHighlightSelectionPointActor->PickableOff();
00126   myHighlightSelectionPointActor->SetVisibility( false );
00127   
00128   myHighlightSelectionPointActor->GetProperty()->SetPointSize(SALOME_POINT_SIZE+2);
00129   myHighlightSelectionPointActor->GetProperty()->SetLineWidth(SALOME_LINE_WIDTH+2);
00130   myHighlightSelectionPointActor->GetProperty()->SetRepresentationToPoints();
00131 
00132   myBBFirstCheck = true;
00133 }
00134 
00138 SVTK_InteractorStyle::~SVTK_InteractorStyle() 
00139 {
00140   endDrawRect();
00141 }
00142 
00146 QWidget* SVTK_InteractorStyle::GetRenderWidget()
00147 {
00148   return myInteractor->GetRenderWidget();
00149 }
00150 
00154 SVTK_Selector* SVTK_InteractorStyle::GetSelector() 
00155 {
00156   return myInteractor->GetSelector();
00157 }
00158 
00162 SVTK_SelectionEvent* SVTK_InteractorStyle::GetSelectionEvent()
00163 {
00164   mySelectionEvent->mySelectionMode = GetSelector()->SelectionMode();
00165 
00166   mySelectionEvent->myIsCtrl = Interactor->GetControlKey();
00167   mySelectionEvent->myIsShift = Interactor->GetShiftKey();
00168 
00169   mySelectionEvent->myLastX = mySelectionEvent->myX;
00170   mySelectionEvent->myLastY = mySelectionEvent->myY;
00171 
00172   GetEventPosition( this->Interactor, mySelectionEvent->myX, mySelectionEvent->myY );
00173 
00174   return mySelectionEvent.get();
00175 }
00176 
00180 SVTK_SelectionEvent* SVTK_InteractorStyle::GetSelectionEventFlipY()
00181 {
00182   mySelectionEvent->mySelectionMode = GetSelector()->SelectionMode();
00183 
00184   mySelectionEvent->myIsCtrl = Interactor->GetControlKey();
00185   mySelectionEvent->myIsShift = Interactor->GetShiftKey();
00186 
00187   mySelectionEvent->myLastX = mySelectionEvent->myX;
00188   mySelectionEvent->myLastY = mySelectionEvent->myY;
00189 
00190   this->Interactor->GetEventPosition(mySelectionEvent->myX, mySelectionEvent->myY);
00191 
00192   return mySelectionEvent.get();
00193 }
00194 
00195 void SVTK_InteractorStyle::RotateXY(int dx, int dy)
00196 {
00197   /*   if(GetCurrentRenderer() == NULL)
00198     return;
00199   
00200   int *size = GetCurrentRenderer()->GetRenderWindow()->GetSize();
00201   double aDeltaElevation = -20.0 / size[1];
00202   double aDeltaAzimuth = -20.0 / size[0];
00203   
00204   double rxf = double(dx) * aDeltaAzimuth * this->MotionFactor;
00205   double ryf = double(dy) * aDeltaElevation * this->MotionFactor;
00206   
00207   vtkCamera *cam = GetCurrentRenderer()->GetActiveCamera();
00208   cam->Azimuth(rxf);
00209   cam->Elevation(ryf);
00210   cam->OrthogonalizeViewUp();
00211 
00212   GetCurrentRenderer()->ResetCameraClippingRange(); 
00213 
00214   this->Render();*/
00215 
00216   if(GetCurrentRenderer() == NULL)
00217     return;
00218   
00219   vtkCamera *cam = GetCurrentRenderer()->GetActiveCamera();
00220 
00221   double viewFP[3], viewPos[3];
00222   cam->GetFocalPoint(viewFP);
00223   cam->GetPosition(viewPos);
00224 
00225   if ( myCurrRotationPointType == SVTK::SetRotateGravity )
00226   {
00227     vtkFloatingPointType aCenter[3];
00228     if ( ComputeBBCenter(GetCurrentRenderer(),aCenter) ) 
00229     {
00230       myRotationPointX = aCenter[0];
00231       myRotationPointY = aCenter[1];
00232       myRotationPointZ = aCenter[2];
00233     }
00234   }
00235 
00236   // Calculate corresponding transformation
00237   vtkPerspectiveTransform* aTransform = vtkPerspectiveTransform::New();
00238   aTransform->Identity();
00239   aTransform->Translate(+myRotationPointX, +myRotationPointY, +myRotationPointZ);
00240 
00241   // Azimuth transformation
00242   int *size = GetCurrentRenderer()->GetRenderWindow()->GetSize();
00243   double aDeltaAzimuth = -20.0 / size[0];
00244   
00245   double rxf = double(dx) * aDeltaAzimuth * this->MotionFactor;
00246   aTransform->RotateWXYZ(rxf, cam->GetViewUp());
00247 
00248   // Elevation transformation
00249   double aDeltaElevation = -20.0 / size[1];
00250 
00251   double ryf = double(dy) * aDeltaElevation * this->MotionFactor;
00252   vtkMatrix4x4* aMatrix = cam->GetViewTransformMatrix();
00253   const double anAxis[3] = {-aMatrix->GetElement(0,0), // mkr : 27.11.2006 : PAL14011 - Strange behaviour in rotation in VTK Viewer.
00254                             -aMatrix->GetElement(0,1), 
00255                             -aMatrix->GetElement(0,2)};
00256   
00257   aTransform->RotateWXYZ(ryf, anAxis);
00258             
00259   aTransform->Translate(-myRotationPointX, -myRotationPointY, -myRotationPointZ);
00260 
00261   // To apply the transformation
00262   cam->SetPosition(aTransform->TransformPoint(viewPos));
00263   cam->SetFocalPoint(aTransform->TransformPoint(viewFP));
00264 
00265   cam->OrthogonalizeViewUp();
00266 
00267   GetCurrentRenderer()->ResetCameraClippingRange(); 
00268 
00269   this->Render();
00270   this->InvokeEvent(SVTK::OperationFinished,NULL);
00271 }
00272 
00273 void SVTK_InteractorStyle::PanXY(int x, int y, int oldX, int oldY)
00274 {
00275   TranslateView(x, y, oldX, oldY);   
00276   this->Render();
00277   this->InvokeEvent(SVTK::OperationFinished,NULL);
00278 }
00279 
00280 void SVTK_InteractorStyle::DollyXY(int dx, int dy)
00281 {
00282   if (GetCurrentRenderer() == NULL) 
00283     return;
00284 
00285   double dxf = this->MotionFactor * (double)(dx) / (double)(GetCurrentRenderer()->GetCenter()[1]);
00286   double dyf = this->MotionFactor * (double)(dy) / (double)(GetCurrentRenderer()->GetCenter()[1]);
00287 
00288   double zoomFactor = pow((double)1.1, dxf + dyf);
00289   
00290   vtkCamera *aCam = GetCurrentRenderer()->GetActiveCamera();
00291   if (aCam->GetParallelProjection()) {
00292     int x0 = 0, y0 = 0, x1 = 0, y1 = 0;
00293     if( IsAdvancedZoomingEnabled() ) { // zoom relatively to the cursor
00294       int* aSize = GetCurrentRenderer()->GetRenderWindow()->GetSize();
00295       int w = aSize[0];
00296       int h = aSize[1];
00297       x0 = w / 2;
00298       y0 = h / 2;
00299       x1 = myOtherPoint.x();
00300       y1 = h - myOtherPoint.y();
00301       TranslateView( x0, y0, x1, y1 );
00302     }
00303     aCam->SetParallelScale(aCam->GetParallelScale()/zoomFactor);
00304     if( IsAdvancedZoomingEnabled() )
00305       TranslateView( x1, y1, x0, y0 );
00306   }
00307   else{
00308     aCam->Dolly(zoomFactor); // Move camera in/out along projection direction
00309     GetCurrentRenderer()->ResetCameraClippingRange(); 
00310   }
00311 
00312   this->Render();
00313   this->InvokeEvent(SVTK::OperationFinished,NULL);
00314 }
00315 
00316 void SVTK_InteractorStyle::SpinXY(int x, int y, int oldX, int oldY)
00317 {
00318   vtkCamera *cam;
00319 
00320   if (GetCurrentRenderer() == NULL)
00321     return;
00322 
00323   double newAngle = atan2((double)(y - GetCurrentRenderer()->GetCenter()[1]),
00324                           (double)(x - GetCurrentRenderer()->GetCenter()[0]));
00325   double oldAngle = atan2((double)(oldY -GetCurrentRenderer()->GetCenter()[1]),
00326                           (double)(oldX - GetCurrentRenderer()->GetCenter()[0]));
00327   
00328   newAngle *= this->RadianToDegree;
00329   oldAngle *= this->RadianToDegree;
00330 
00331   cam = GetCurrentRenderer()->GetActiveCamera();
00332   cam->Roll(newAngle - oldAngle);
00333   cam->OrthogonalizeViewUp();
00334       
00335   this->Render();
00336   this->InvokeEvent(SVTK::OperationFinished,NULL);
00337 }
00338 
00339 
00343 void SVTK_InteractorStyle::OnConfigure() 
00344 {
00345   this->FindPokedRenderer(0,0);
00346   this->GetCurrentRenderer()->InvokeEvent(vtkCommand::ConfigureEvent,NULL);
00347 }
00348 
00352 void SVTK_InteractorStyle::OnMouseMove() 
00353 {
00354   int x, y;
00355   GetEventPosition( this->Interactor, x, y );
00356   this->OnMouseMove( this->Interactor->GetControlKey(),
00357                      this->Interactor->GetShiftKey(),
00358                      x, y );
00359 }
00360 
00364 void SVTK_InteractorStyle::OnLeftButtonDown()
00365 {
00366   int x, y;
00367   GetEventPosition( this->Interactor, x, y );
00368   this->OnLeftButtonDown( this->Interactor->GetControlKey(),
00369                           this->Interactor->GetShiftKey(),
00370                           x, y );
00371 }
00372 
00376 void SVTK_InteractorStyle::OnLeftButtonUp()
00377 {
00378   int x, y;
00379   GetEventPosition( this->Interactor, x, y );
00380   this->OnLeftButtonUp( this->Interactor->GetControlKey(),
00381                         this->Interactor->GetShiftKey(),
00382                         x, y );
00383 }
00384 
00388 void SVTK_InteractorStyle::OnMiddleButtonDown() 
00389 {
00390   int x, y;
00391   GetEventPosition( this->Interactor, x, y );
00392   this->OnMiddleButtonDown( this->Interactor->GetControlKey(),
00393                             this->Interactor->GetShiftKey(),
00394                             x, y );
00395 }
00396 
00400 void SVTK_InteractorStyle::OnMiddleButtonUp()
00401 {
00402   int x, y;
00403   GetEventPosition( this->Interactor, x, y );
00404   this->OnMiddleButtonUp( this->Interactor->GetControlKey(),
00405                           this->Interactor->GetShiftKey(),
00406                           x, y );
00407 }
00408 
00412 void SVTK_InteractorStyle::OnRightButtonDown() 
00413 {
00414   int x, y;
00415   GetEventPosition( this->Interactor, x, y );
00416   this->OnRightButtonDown( this->Interactor->GetControlKey(),
00417                            this->Interactor->GetShiftKey(),
00418                            x, y );
00419 }
00420 
00424 void SVTK_InteractorStyle::OnRightButtonUp()
00425 {
00426   int x, y;
00427   GetEventPosition( this->Interactor, x, y );
00428   this->OnRightButtonUp( this->Interactor->GetControlKey(),
00429                          this->Interactor->GetShiftKey(),
00430                          x, y );
00431 }
00432 
00436 void SVTK_InteractorStyle::OnMouseWheelForward()
00437 {
00438   int x, y;
00439   GetEventPosition( this->Interactor, x, y );
00440   myOtherPoint = QPoint(x, y);
00441 }
00442 
00446 void SVTK_InteractorStyle::OnMouseWheelBackward()
00447 {
00448   int x, y;
00449   GetEventPosition( this->Interactor, x, y );
00450   myOtherPoint = QPoint(x, y);
00451 }
00452 
00456 void SVTK_InteractorStyle::OnMouseMove(int vtkNotUsed(ctrl), 
00457                                        int shift,
00458                                        int x, int y) 
00459 {
00460   myShiftState = shift;
00461   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
00462     onOperation(QPoint(x, y));
00463   else if (ForcedState == VTK_INTERACTOR_STYLE_CAMERA_NONE)
00464     onCursorMove(QPoint(x, y));
00465 }
00466 
00470 void SVTK_InteractorStyle::OnLeftButtonDown(int ctrl, int shift, 
00471                                             int x, int y) 
00472 {
00473   this->FindPokedRenderer(x, y);
00474   if(GetCurrentRenderer() == NULL)
00475     return;
00476 
00477   myShiftState = shift;
00478   // finishing current viewer operation
00479   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
00480     onFinishOperation();
00481     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
00482   }
00483   myOtherPoint = myPoint = QPoint(x, y);
00484   if (ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
00485     startOperation(ForcedState);
00486   } else {
00487     if (ctrl)
00488       startOperation(VTK_INTERACTOR_STYLE_CAMERA_ZOOM);
00489     else if ( myCurrRotationPointType == SVTK::StartPointSelection ||
00490               myCurrFocalPointType == SVTK::StartFocalPointSelection )
00491     {
00492       SVTK_SelectionEvent* aSelectionEvent = GetSelectionEventFlipY();
00493 
00494       bool isPicked = false;
00495       vtkActorCollection* anActorCollection = GetSelector()->Pick(aSelectionEvent, GetCurrentRenderer());
00496       
00497       if( anActorCollection )
00498       {
00499         anActorCollection->InitTraversal();
00500         while( vtkActor* aVTKActor = anActorCollection->GetNextActor() )
00501         {
00502           if( SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( aVTKActor ) )
00503           {
00504             SVTK::TPickLimiter aPickLimiter( myPointPicker, anActor );
00505             myPointPicker->Pick( aSelectionEvent->myX,
00506                                  aSelectionEvent->myY, 
00507                                  0.0, 
00508                                  GetCurrentRenderer() );
00509             int aVtkId = myPointPicker->GetPointId();
00510             if ( aVtkId >= 0 )
00511             {
00512               int anObjId = anActor->GetNodeObjId( aVtkId );
00513               vtkFloatingPointType* aCoords = anActor->GetNodeCoord(anObjId);
00514               
00515               if (myCurrRotationPointType == SVTK::StartPointSelection) {
00516                 myCurrRotationPointType = SVTK::SetRotateSelected;
00517                 
00518                 // invoke event for update coordinates in SVTK_SetRotationPointDlg
00519                 InvokeEvent(SVTK::RotationPointChanged,(void*)aCoords);
00520               }
00521               else if (myCurrFocalPointType == SVTK::StartFocalPointSelection) {
00522                 myCurrFocalPointType = SVTK::SetFocalPointSelected;
00523                 
00524                 // invoke event for update coordinates in SVTK_ViewParameterDlg
00525                 InvokeEvent(SVTK::FocalPointChanged,(void*)aCoords);
00526               }
00527 
00528               isPicked = true;
00529               break;
00530             }
00531           }
00532         }
00533       }
00534 
00535       if( !isPicked )
00536       {
00537         if (myCurrRotationPointType == SVTK::StartPointSelection) {
00538           // invoke event with no data (for SVTK_SetRotationPointDlg)
00539           InvokeEvent(SVTK::RotationPointChanged,0);
00540           myCurrRotationPointType = myPrevRotationPointType;
00541         }
00542         else if (myCurrFocalPointType == SVTK::StartFocalPointSelection) {
00543           // invoke event with no data (for SVTK_ViewParameterDlg)
00544           InvokeEvent(SVTK::FocalPointChanged,0);
00545           myCurrFocalPointType = myPrevFocalPointType;
00546         }
00547       }
00548     
00549       myHighlightSelectionPointActor->SetVisibility( false );
00550       if(GetCurrentRenderer() != NULL)
00551         GetCurrentRenderer()->RemoveActor( myHighlightSelectionPointActor.GetPointer() );
00552 
00553       GetRenderWidget()->setCursor(myDefCursor); 
00554     }
00555     else
00556       startOperation(VTK_INTERACTOR_STYLE_CAMERA_SELECT);
00557   }
00558   
00559   return;
00560 }
00561 
00565 void SVTK_InteractorStyle::OnLeftButtonUp(int vtkNotUsed(ctrl),
00566                                           int shift, 
00567                                           int vtkNotUsed(x),
00568                                           int vtkNotUsed(y))
00569 {
00570   myShiftState = shift;
00571   // finishing current viewer operation
00572   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
00573     onFinishOperation();
00574     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
00575   }
00576 }
00577 
00581 void SVTK_InteractorStyle::OnMiddleButtonDown(int ctrl,
00582                                               int shift, 
00583                                               int x, int y) 
00584 {
00585   this->FindPokedRenderer(x, y);
00586   if(GetCurrentRenderer() == NULL)
00587     return;
00588 
00589   myShiftState = shift;
00590   // finishing current viewer operation
00591   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
00592     onFinishOperation();
00593     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
00594   }
00595   myOtherPoint = myPoint = QPoint(x, y);
00596   if (ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
00597     startOperation(ForcedState);
00598   }
00599   else {
00600     if (ctrl)
00601       startOperation(VTK_INTERACTOR_STYLE_CAMERA_PAN);
00602   }
00603 }
00604 
00605 
00609 void SVTK_InteractorStyle::OnMiddleButtonUp(int vtkNotUsed(ctrl),
00610                                             int shift, 
00611                                             int vtkNotUsed(x),
00612                                             int vtkNotUsed(y))
00613 {
00614   myShiftState = shift;
00615   // finishing current viewer operation
00616   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
00617     onFinishOperation();
00618     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
00619   }
00620 }
00621 
00622 
00626 void SVTK_InteractorStyle::OnRightButtonDown(int ctrl,
00627                                              int shift, 
00628                                              int x, int y) 
00629 {
00630   this->FindPokedRenderer(x, y);
00631   if(GetCurrentRenderer() == NULL)
00632     return;
00633 
00634   myShiftState = shift;
00635   // finishing current viewer operation
00636   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
00637     onFinishOperation();
00638     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
00639   }
00640   myOtherPoint = myPoint = QPoint(x, y);
00641   if (ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
00642     startOperation(ForcedState);
00643   }
00644   else {
00645     if (ctrl)
00646       startOperation(VTK_INTERACTOR_STYLE_CAMERA_ROTATE);  
00647   }
00648 }
00649 
00653 void SVTK_InteractorStyle::OnRightButtonUp(int vtkNotUsed(ctrl),
00654                                            int shift, 
00655                                            int vtkNotUsed(x),
00656                                            int vtkNotUsed(y))
00657 {
00658   myShiftState = shift;
00659   // finishing current viewer operation
00660   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
00661     onFinishOperation();
00662     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
00663   }
00664 }
00665 
00666 /* XPM */
00667 const char* imageZoomCursor[] = { 
00668 "32 32 3 1",
00669 ". c None",
00670 "a c #000000",
00671 "# c #ffffff",
00672 "................................",
00673 "................................",
00674 ".#######........................",
00675 "..aaaaaaa.......................",
00676 "................................",
00677 ".............#####..............",
00678 "...........##.aaaa##............",
00679 "..........#.aa.....a#...........",
00680 ".........#.a.........#..........",
00681 ".........#a..........#a.........",
00682 "........#.a...........#.........",
00683 "........#a............#a........",
00684 "........#a............#a........",
00685 "........#a............#a........",
00686 "........#a............#a........",
00687 ".........#...........#.a........",
00688 ".........#a..........#a.........",
00689 ".........##.........#.a.........",
00690 "........#####.....##.a..........",
00691 ".......###aaa#####.aa...........",
00692 "......###aa...aaaaa.......#.....",
00693 ".....###aa................#a....",
00694 "....###aa.................#a....",
00695 "...###aa...............#######..",
00696 "....#aa.................aa#aaaa.",
00697 ".....a....................#a....",
00698 "..........................#a....",
00699 "...........................a....",
00700 "................................",
00701 "................................",
00702 "................................",
00703 "................................"};
00704 
00705 const char* imageRotateCursor[] = { 
00706 "32 32 3 1",
00707 ". c None",
00708 "a c #000000",
00709 "# c #ffffff",
00710 "................................",
00711 "................................",
00712 "................................",
00713 "................................",
00714 "........#.......................",
00715 ".......#.a......................",
00716 "......#######...................",
00717 ".......#aaaaa#####..............",
00718 "........#..##.a#aa##........##..",
00719 ".........a#.aa..#..a#.....##.aa.",
00720 ".........#.a.....#...#..##.aa...",
00721 ".........#a.......#..###.aa.....",
00722 "........#.a.......#a..#aa.......",
00723 "........#a.........#..#a........",
00724 "........#a.........#a.#a........",
00725 "........#a.........#a.#a........",
00726 "........#a.........#a.#a........",
00727 ".........#.........#a#.a........",
00728 "........##a........#a#a.........",
00729 "......##.a#.......#.#.a.........",
00730 "....##.aa..##.....##.a..........",
00731 "..##.aa.....a#####.aa...........",
00732 "...aa.........aaa#a.............",
00733 "................#.a.............",
00734 "...............#.a..............",
00735 "..............#.a...............",
00736 "...............a................",
00737 "................................",
00738 "................................",
00739 "................................",
00740 "................................",
00741 "................................"};
00742 
00743 
00747 void SVTK_InteractorStyle::loadCursors()
00748 {
00749   myDefCursor       = QCursor(Qt::ArrowCursor);
00750   myHandCursor      = QCursor(Qt::PointingHandCursor);
00751   myPanCursor       = QCursor(Qt::SizeAllCursor);
00752   myZoomCursor      = QCursor(QPixmap(imageZoomCursor));
00753   myRotateCursor    = QCursor(QPixmap(imageRotateCursor));
00754   mySpinCursor      = QCursor(QPixmap(imageRotateCursor)); // temporarly !!!!!!
00755   myGlobalPanCursor = QCursor(Qt::CrossCursor);
00756   myCursorState     = false;
00757 }
00758 
00759 
00763 void SVTK_InteractorStyle::startZoom()
00764 {
00765   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
00766   {
00767     onFinishOperation();
00768     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
00769   }
00770   setCursor(VTK_INTERACTOR_STYLE_CAMERA_ZOOM);
00771   ForcedState = VTK_INTERACTOR_STYLE_CAMERA_ZOOM;
00772 }
00773 
00774 
00778 void SVTK_InteractorStyle::startPan()
00779 {
00780   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
00781   {
00782     onFinishOperation();
00783     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
00784   }
00785   setCursor(VTK_INTERACTOR_STYLE_CAMERA_PAN);
00786   ForcedState = VTK_INTERACTOR_STYLE_CAMERA_PAN;
00787 }
00788 
00792 void SVTK_InteractorStyle::startRotate()
00793 {
00794   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
00795   {
00796     onFinishOperation();
00797     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
00798   }
00799   setCursor(VTK_INTERACTOR_STYLE_CAMERA_ROTATE);
00800   ForcedState = VTK_INTERACTOR_STYLE_CAMERA_ROTATE;
00801 }
00802 
00806 void SVTK_InteractorStyle::startPointSelection()
00807 {
00808   myCurrRotationPointType = SVTK::StartPointSelection;
00809 
00810   if(GetCurrentRenderer() != NULL) {
00811     GetCurrentRenderer()->AddActor( myHighlightSelectionPointActor.GetPointer() );
00812     vtkFloatingPointType aColor[3];
00813     GetCurrentRenderer()->GetBackground( aColor );
00814     myHighlightSelectionPointActor->GetProperty()->SetColor(1. - aColor[0],
00815                                                             1. - aColor[1],
00816                                                             1. - aColor[2]);
00817   }
00818 
00819   setCursor(VTK_INTERACTOR_STYLE_CAMERA_NONE);
00820 }
00821 
00825 void SVTK_InteractorStyle::startFocalPointSelection()
00826 {
00827   myCurrFocalPointType = SVTK::StartFocalPointSelection;
00828 
00829   if(GetCurrentRenderer() != NULL) {
00830     GetCurrentRenderer()->AddActor( myHighlightSelectionPointActor.GetPointer() );
00831     vtkFloatingPointType aColor[3];
00832     GetCurrentRenderer()->GetBackground( aColor );
00833     myHighlightSelectionPointActor->GetProperty()->SetColor(1. - aColor[0],
00834                                                             1. - aColor[1],
00835                                                             1. - aColor[2]);
00836   }
00837 
00838   setCursor(VTK_INTERACTOR_STYLE_CAMERA_NONE);
00839 }
00840 
00844 void SVTK_InteractorStyle::startSpin()
00845 {
00846   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
00847   {
00848     onFinishOperation();
00849     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
00850   }
00851   setCursor(VTK_INTERACTOR_STYLE_CAMERA_SPIN);
00852   ForcedState = VTK_INTERACTOR_STYLE_CAMERA_SPIN;
00853 }
00854 
00855 
00856 
00860 void SVTK_InteractorStyle::startFitArea()
00861 {
00862   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
00863   {
00864     onFinishOperation();
00865     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
00866   }
00867   setCursor(VTK_INTERACTOR_STYLE_CAMERA_FIT);
00868   ForcedState = VTK_INTERACTOR_STYLE_CAMERA_FIT;
00869 }
00870 
00871 
00875 void SVTK_InteractorStyle::startGlobalPan()
00876 {
00877   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
00878   {
00879     onFinishOperation();
00880     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
00881   }
00882   setCursor(VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN);
00883   ForcedState = VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN;
00884 
00885   // store current zoom scale
00886   myScale = GetCurrentRenderer()->GetActiveCamera()->GetParallelScale();
00887 
00888   GetCurrentRenderer()->ResetCamera();
00889 
00890   this->Render();
00891 }
00892 
00893 
00897 void SVTK_InteractorStyle::fitRect(const int left, 
00898                                    const int top, 
00899                                    const int right, 
00900                                    const int bottom)
00901 {
00902   if (GetCurrentRenderer() == NULL) 
00903     return;
00904  
00905   // move camera
00906   int x = (left + right)/2;
00907   int y = (top + bottom)/2;
00908   int *aSize = GetCurrentRenderer()->GetRenderWindow()->GetSize();
00909   int oldX = aSize[0]/2;
00910   int oldY = aSize[1]/2;
00911   TranslateView(oldX, oldY, x, y);
00912 
00913   // zoom camera
00914   double dxf = right == left ? 1.0 : (double)(aSize[0]) / (double)(abs(right - left));
00915   double dyf = bottom == top ? 1.0 : (double)(aSize[1]) / (double)(abs(bottom - top));
00916   double zoomFactor = (dxf + dyf)/2 ;
00917 
00918   vtkCamera *aCam = GetCurrentRenderer()->GetActiveCamera();
00919   if(aCam->GetParallelProjection())
00920     aCam->SetParallelScale(aCam->GetParallelScale()/zoomFactor);
00921   else{
00922     aCam->Dolly(zoomFactor);
00923     GetCurrentRenderer()->ResetCameraClippingRange();
00924   }
00925   
00926   this->Render();
00927 }
00928 
00929 
00933 void SVTK_InteractorStyle::startOperation(int operation)
00934 {
00935   switch(operation)
00936   { 
00937   case VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN:
00938   case VTK_INTERACTOR_STYLE_CAMERA_ZOOM:
00939   case VTK_INTERACTOR_STYLE_CAMERA_PAN:
00940   case VTK_INTERACTOR_STYLE_CAMERA_ROTATE:
00941   case VTK_INTERACTOR_STYLE_CAMERA_SPIN:
00942   case VTK_INTERACTOR_STYLE_CAMERA_FIT:
00943   case VTK_INTERACTOR_STYLE_CAMERA_SELECT:
00944     if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
00945       startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
00946     State = operation;
00947     if (State != VTK_INTERACTOR_STYLE_CAMERA_SELECT)
00948       setCursor(operation);
00949     onStartOperation();
00950     break;
00951   case VTK_INTERACTOR_STYLE_CAMERA_NONE:
00952   default:
00953     setCursor(VTK_INTERACTOR_STYLE_CAMERA_NONE);
00954     State = ForcedState = VTK_INTERACTOR_STYLE_CAMERA_NONE;
00955     break;
00956   }
00957 }
00958 
00959 
00963 void SVTK_InteractorStyle::setCursor(const int operation)
00964 {
00965   if (!GetRenderWidget()) return;
00966   switch (operation)
00967   {
00968     case VTK_INTERACTOR_STYLE_CAMERA_ZOOM:
00969       GetRenderWidget()->setCursor(myZoomCursor); 
00970       myCursorState = true;
00971       break;
00972     case VTK_INTERACTOR_STYLE_CAMERA_PAN:
00973       GetRenderWidget()->setCursor(myPanCursor); 
00974       myCursorState = true;
00975       break;
00976     case VTK_INTERACTOR_STYLE_CAMERA_ROTATE:
00977       GetRenderWidget()->setCursor(myRotateCursor); 
00978       myCursorState = true;
00979       break;
00980     case VTK_INTERACTOR_STYLE_CAMERA_SPIN:
00981       GetRenderWidget()->setCursor(mySpinCursor); 
00982       myCursorState = true;
00983       break;
00984     case VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN:
00985       GetRenderWidget()->setCursor(myGlobalPanCursor); 
00986       myCursorState = true;
00987       break;
00988     case VTK_INTERACTOR_STYLE_CAMERA_FIT:
00989     case VTK_INTERACTOR_STYLE_CAMERA_SELECT:
00990       GetRenderWidget()->setCursor(myHandCursor); 
00991       myCursorState = true;
00992       break;
00993     case VTK_INTERACTOR_STYLE_CAMERA_NONE:
00994     default:
00995       if ( myCurrRotationPointType == SVTK::StartPointSelection ||
00996            myCurrFocalPointType == SVTK::StartFocalPointSelection )
00997         GetRenderWidget()->setCursor(myHandCursor);
00998       else
00999         GetRenderWidget()->setCursor(myDefCursor); 
01000       myCursorState = false;
01001       break;
01002   }
01003 }
01004 
01005 
01009 void SVTK_InteractorStyle::onStartOperation()
01010 {
01011   if (!GetRenderWidget()) 
01012     return;
01013 
01014   vtkRenderWindowInteractor *aRWI = this->Interactor;
01015   vtkRenderWindow *aRenWin = aRWI->GetRenderWindow();
01016   aRenWin->SetDesiredUpdateRate(aRWI->GetDesiredUpdateRate());
01017 
01018   switch (State) {
01019     case VTK_INTERACTOR_STYLE_CAMERA_SELECT:
01020     case VTK_INTERACTOR_STYLE_CAMERA_FIT:
01021     {
01022       drawRect();
01023       break;
01024     }
01025     case VTK_INTERACTOR_STYLE_CAMERA_ZOOM:
01026     case VTK_INTERACTOR_STYLE_CAMERA_PAN:
01027     case VTK_INTERACTOR_STYLE_CAMERA_ROTATE:
01028     case VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN:
01029     case VTK_INTERACTOR_STYLE_CAMERA_SPIN:
01030       break;
01031   }
01032 }
01033 
01034 
01038 void SVTK_InteractorStyle::onFinishOperation() 
01039 {
01040   if (!GetRenderWidget()) 
01041     return;
01042 
01043   vtkRenderWindowInteractor *aRWI = this->Interactor;
01044   vtkRenderWindow *aRenWin = aRWI->GetRenderWindow();
01045   aRenWin->SetDesiredUpdateRate(aRWI->GetStillUpdateRate());
01046 
01047   SVTK_SelectionEvent* aSelectionEvent = GetSelectionEventFlipY();
01048 
01049   switch (State) {
01050     case VTK_INTERACTOR_STYLE_CAMERA_SELECT:
01051     case VTK_INTERACTOR_STYLE_CAMERA_FIT:
01052     {
01053       endDrawRect();
01054       QRect aRect(myPoint, myOtherPoint);
01055       aRect = aRect.normalized();
01056 
01057       if (State == VTK_INTERACTOR_STYLE_CAMERA_FIT) {
01058         // making fit rect opeation 
01059         int w, h;
01060         Interactor->GetSize(w, h);
01061         int x1 = aRect.left(); 
01062         int y1 = h - aRect.top() - 1;
01063         int x2 = aRect.right(); 
01064         int y2 = h - aRect.bottom() - 1;
01065         fitRect(x1, y1, x2, y2);
01066       }
01067       else {
01068         if (myPoint == myOtherPoint)
01069           {
01070             // process point selection
01071             this->FindPokedRenderer(aSelectionEvent->myX, aSelectionEvent->myY);
01072             Interactor->StartPickCallback();
01073             
01074             SALOME_Actor* aHighlightedActor = NULL;
01075             vtkActorCollection* anActorCollection = GetSelector()->Pick(aSelectionEvent, GetCurrentRenderer());
01076 
01077             aSelectionEvent->myIsRectangle = false;
01078 
01079             if(!myShiftState)
01080               GetSelector()->ClearIObjects();
01081 
01082             if( anActorCollection )
01083             {
01084               anActorCollection->InitTraversal();
01085               while( vtkActor* aVTKActor = anActorCollection->GetNextActor() )
01086               {
01087                 if( SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( aVTKActor ) )
01088                 {
01089                   if( anActor->Highlight( this, aSelectionEvent, true ) )
01090                   {
01091                     aHighlightedActor = anActor;
01092                     break;
01093                   }
01094                 }
01095               }
01096             }
01097 
01098             if( !aHighlightedActor )
01099             {
01100               if(myLastHighlitedActor.GetPointer() && myLastHighlitedActor.GetPointer() != aHighlightedActor)
01101                 myLastHighlitedActor->Highlight( this, aSelectionEvent, false );
01102             }
01103             myLastHighlitedActor = aHighlightedActor;
01104           } 
01105         else 
01106           {
01107             //processing rectangle selection
01108             Interactor->StartPickCallback();
01109             GetSelector()->StartPickCallback();
01110             aSelectionEvent->myIsRectangle = true;
01111 
01112             if(!myShiftState)
01113               GetSelector()->ClearIObjects();
01114 
01115             VTK::ActorCollectionCopy aCopy(GetCurrentRenderer()->GetActors());
01116             vtkActorCollection* aListActors = aCopy.GetActors();
01117             aListActors->InitTraversal();
01118             while(vtkActor* aActor = aListActors->GetNextActor())
01119               {
01120                 if(aActor->GetVisibility())
01121                   {
01122                     if(SALOME_Actor* aSActor = SALOME_Actor::SafeDownCast(aActor))
01123                       {
01124                         if(aSActor->hasIO())
01125                           aSActor->Highlight( this, aSelectionEvent, true );
01126                       }
01127                   }
01128               }
01129           }
01130         Interactor->EndPickCallback();
01131         GetSelector()->EndPickCallback();
01132       } 
01133     } 
01134     break;
01135   case VTK_INTERACTOR_STYLE_CAMERA_ZOOM:
01136   case VTK_INTERACTOR_STYLE_CAMERA_PAN:
01137   case VTK_INTERACTOR_STYLE_CAMERA_ROTATE:
01138   case VTK_INTERACTOR_STYLE_CAMERA_SPIN:
01139     break;
01140   case VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN: 
01141     {
01142       int w, h, x, y;
01143       Interactor->GetSize(w, h);
01144       x = myPoint.x(); 
01145       y = h - myPoint.y() - 1;
01146       Place(x, y);
01147     }
01148     break;
01149   }
01150 
01151   this->Render();
01152 }
01153 
01154 
01158 void SVTK_InteractorStyle::onOperation(QPoint mousePos) 
01159 {
01160   if (!GetRenderWidget()) 
01161     return;
01162 
01163   switch (State) {
01164   case VTK_INTERACTOR_STYLE_CAMERA_PAN: 
01165     {
01166       this->PanXY(mousePos.x(), myPoint.y(), myPoint.x(), mousePos.y());
01167       myPoint = mousePos;
01168       break;
01169     }
01170   case VTK_INTERACTOR_STYLE_CAMERA_ZOOM: 
01171     {    
01172       this->DollyXY(mousePos.x() - myPoint.x(), mousePos.y() - myPoint.y());
01173       myPoint = mousePos;
01174       break;
01175     }
01176   case VTK_INTERACTOR_STYLE_CAMERA_ROTATE: 
01177     {
01178       this->RotateXY(mousePos.x() - myPoint.x(), myPoint.y() - mousePos.y());
01179       myPoint = mousePos;
01180       break;
01181     }
01182   case VTK_INTERACTOR_STYLE_CAMERA_SPIN: 
01183     {
01184       this->SpinXY(mousePos.x(), mousePos.y(), myPoint.x(), myPoint.y());
01185       myPoint = mousePos;
01186       break;
01187     }
01188   case VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN: 
01189     {    
01190       break;
01191     }
01192   case VTK_INTERACTOR_STYLE_CAMERA_SELECT:
01193     {
01194       if (!myCursorState)
01195         setCursor(VTK_INTERACTOR_STYLE_CAMERA_SELECT);
01196     }
01197   case VTK_INTERACTOR_STYLE_CAMERA_FIT:
01198     {
01199       myOtherPoint = mousePos;
01200       drawRect();
01201       break;
01202     }
01203   }
01204 }
01205 
01210 void SVTK_InteractorStyle::onCursorMove(QPoint mousePos) 
01211 {
01212   // processing highlighting
01213   SVTK_SelectionEvent* aSelectionEvent = GetSelectionEventFlipY();
01214   this->FindPokedRenderer(aSelectionEvent->myX,aSelectionEvent->myY);
01215 
01216   bool anIsChanged = false;
01217 
01218   SALOME_Actor* aPreHighlightedActor = NULL;
01219   vtkActorCollection* anActorCollection = GetSelector()->Pick(aSelectionEvent, GetCurrentRenderer());
01220 
01221   if ( myCurrRotationPointType == SVTK::StartPointSelection ||
01222        myCurrFocalPointType == SVTK::StartFocalPointSelection )
01223   {
01224     myHighlightSelectionPointActor->SetVisibility( false );
01225 
01226     if( anActorCollection )
01227     {
01228       anActorCollection->InitTraversal();
01229       while( vtkActor* aVTKActor = anActorCollection->GetNextActor() )
01230       {
01231         if( SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( aVTKActor ) )
01232         {
01233           SVTK::TPickLimiter aPickLimiter( myPointPicker, anActor );
01234           myPointPicker->Pick( aSelectionEvent->myX, aSelectionEvent->myY, 0.0, GetCurrentRenderer() );
01235           int aVtkId = myPointPicker->GetPointId();
01236           if ( aVtkId >= 0 ) {
01237             int anObjId = anActor->GetNodeObjId( aVtkId );
01238 
01239             TColStd_IndexedMapOfInteger aMapIndex;
01240             aMapIndex.Add( anObjId );
01241             myHighlightSelectionPointActor->MapPoints( anActor, aMapIndex );
01242 
01243             myHighlightSelectionPointActor->SetVisibility( true );
01244             anIsChanged = true;
01245             break;
01246           }
01247         }
01248       }
01249     }
01250   }
01251   else {
01252     if( anActorCollection )
01253     {
01254       anActorCollection->InitTraversal();
01255       while( vtkActor* aVTKActor = anActorCollection->GetNextActor() )
01256       {
01257         if( SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( aVTKActor ) )
01258         {
01259           anIsChanged = anActor->PreHighlight( this, aSelectionEvent, true );
01260           if( anActor->isPreselected() )
01261           {
01262             aPreHighlightedActor = anActor;
01263             break;
01264           }
01265         }
01266       }
01267     }
01268 
01269     if(myLastPreHighlitedActor.GetPointer() && myLastPreHighlitedActor.GetPointer() != aPreHighlightedActor)
01270       anIsChanged |= myLastPreHighlitedActor->PreHighlight( this, aSelectionEvent, false );   
01271 
01272   }
01273   
01274   myLastPreHighlitedActor = aPreHighlightedActor;
01275 
01276   if(anIsChanged)
01277     this->Render();
01278 }
01279 
01283 void SVTK_InteractorStyle::Place(const int theX, const int theY) 
01284 {
01285   if (GetCurrentRenderer() == NULL)
01286     return;
01287 
01288   //translate view
01289   int *aSize = GetCurrentRenderer()->GetRenderWindow()->GetSize();
01290   int centerX = aSize[0]/2;
01291   int centerY = aSize[1]/2;
01292 
01293   TranslateView(centerX, centerY, theX, theY);
01294 
01295   // restore zoom scale
01296   vtkCamera *cam = GetCurrentRenderer()->GetActiveCamera();
01297   cam->SetParallelScale(myScale);
01298   GetCurrentRenderer()->ResetCameraClippingRange();
01299 
01300   this->Render();
01301 }
01302 
01303 
01304 
01308 void SVTK_InteractorStyle::TranslateView(int toX, int toY, int fromX, int fromY)
01309 {
01310   if (GetCurrentRenderer() == NULL)
01311     return;
01312 
01313   vtkCamera *cam = GetCurrentRenderer()->GetActiveCamera();
01314   double viewFocus[4], focalDepth, viewPoint[3];
01315   vtkFloatingPointType newPickPoint[4], oldPickPoint[4], motionVector[3];
01316   cam->GetFocalPoint(viewFocus);
01317 
01318   this->ComputeWorldToDisplay(viewFocus[0], viewFocus[1],
01319                               viewFocus[2], viewFocus);
01320   focalDepth = viewFocus[2];
01321 
01322   this->ComputeDisplayToWorld(double(toX), double(toY),
01323                               focalDepth, newPickPoint);
01324   this->ComputeDisplayToWorld(double(fromX),double(fromY),
01325                               focalDepth, oldPickPoint);
01326   
01327   // camera motion is reversed
01328   motionVector[0] = oldPickPoint[0] - newPickPoint[0];
01329   motionVector[1] = oldPickPoint[1] - newPickPoint[1];
01330   motionVector[2] = oldPickPoint[2] - newPickPoint[2];
01331   
01332   cam->GetFocalPoint(viewFocus);
01333   cam->GetPosition(viewPoint);
01334   cam->SetFocalPoint(motionVector[0] + viewFocus[0],
01335                      motionVector[1] + viewFocus[1],
01336                      motionVector[2] + viewFocus[2]);
01337   cam->SetPosition(motionVector[0] + viewPoint[0],
01338                    motionVector[1] + viewPoint[1],
01339                    motionVector[2] + viewPoint[2]);
01340 }
01341 
01342 void SVTK_InteractorStyle::IncrementalPan( const int incrX, const int incrY )
01343 {
01344   this->PanXY( incrX, incrY, 0, 0 );
01345 }
01346 
01347 void SVTK_InteractorStyle::IncrementalZoom( const int incr )
01348 {
01349   this->DollyXY( incr, incr );
01350 }
01351 
01352 void SVTK_InteractorStyle::IncrementalRotate( const int incrX, const int incrY )
01353 {
01354   this->RotateXY( incrX, -incrY );
01355 }
01356 
01360 void SVTK_InteractorStyle::SetInteractor( vtkRenderWindowInteractor* theInteractor )
01361 {
01362   // register EventCallbackCommand as observer of standard events (keypress, mousemove, etc)
01363   Superclass::SetInteractor( theInteractor );
01364  
01365   myInteractor = dynamic_cast<SVTK_GenericRenderWindowInteractor*>(theInteractor);
01366 
01367   if(theInteractor) { 
01368     // register EventCallbackCommand as observer of custorm event (3d space mouse event)
01369     theInteractor->AddObserver( SVTK::SpaceMouseMoveEvent, EventCallbackCommand, Priority );
01370     theInteractor->AddObserver( SVTK::SpaceMouseButtonEvent, EventCallbackCommand, Priority );
01371     theInteractor->AddObserver( SVTK::PanLeftEvent, EventCallbackCommand, Priority );
01372     theInteractor->AddObserver( SVTK::PanRightEvent, EventCallbackCommand, Priority );
01373     theInteractor->AddObserver( SVTK::PanUpEvent, EventCallbackCommand, Priority );
01374     theInteractor->AddObserver( SVTK::PanDownEvent, EventCallbackCommand, Priority );
01375     theInteractor->AddObserver( SVTK::ZoomInEvent, EventCallbackCommand, Priority );
01376     theInteractor->AddObserver( SVTK::ZoomOutEvent, EventCallbackCommand, Priority );
01377     theInteractor->AddObserver( SVTK::RotateLeftEvent, EventCallbackCommand, Priority );
01378     theInteractor->AddObserver( SVTK::RotateRightEvent, EventCallbackCommand, Priority );
01379     theInteractor->AddObserver( SVTK::RotateUpEvent, EventCallbackCommand, Priority );
01380     theInteractor->AddObserver( SVTK::RotateDownEvent, EventCallbackCommand, Priority );
01381     theInteractor->AddObserver( SVTK::PlusSpeedIncrementEvent, EventCallbackCommand, Priority );
01382     theInteractor->AddObserver( SVTK::MinusSpeedIncrementEvent, EventCallbackCommand, Priority );
01383     theInteractor->AddObserver( SVTK::SetSpeedIncrementEvent, EventCallbackCommand, Priority );
01384 
01385     theInteractor->AddObserver( SVTK::SetSMDecreaseSpeedEvent, EventCallbackCommand, Priority );
01386     theInteractor->AddObserver( SVTK::SetSMIncreaseSpeedEvent, EventCallbackCommand, Priority );
01387     theInteractor->AddObserver( SVTK::SetSMDominantCombinedSwitchEvent, EventCallbackCommand, Priority );
01388 
01389     theInteractor->AddObserver( SVTK::StartZoom, EventCallbackCommand, Priority );
01390     theInteractor->AddObserver( SVTK::StartPan, EventCallbackCommand, Priority );
01391     theInteractor->AddObserver( SVTK::StartRotate, EventCallbackCommand, Priority );
01392     theInteractor->AddObserver( SVTK::StartGlobalPan, EventCallbackCommand, Priority );
01393     theInteractor->AddObserver( SVTK::StartFitArea, EventCallbackCommand, Priority );
01394 
01395     theInteractor->AddObserver( SVTK::SetRotateGravity, EventCallbackCommand, Priority );
01396     theInteractor->AddObserver( SVTK::StartPointSelection, EventCallbackCommand, Priority );
01397 
01398     theInteractor->AddObserver( SVTK::ChangeRotationPoint, EventCallbackCommand, Priority );
01399 
01400     theInteractor->AddObserver( SVTK::SetFocalPointGravity, EventCallbackCommand, Priority );
01401     theInteractor->AddObserver( SVTK::StartFocalPointSelection, EventCallbackCommand, Priority );
01402     theInteractor->AddObserver( SVTK::SetFocalPointSelected, EventCallbackCommand, Priority );
01403   }
01404 }
01405 
01409 void SVTK_InteractorStyle::OnTimer() 
01410 {
01411   //vtkInteractorStyle::OnTimer();
01412   this->Interactor->Render();
01413   // check if bounding box was changed
01414   if ( GetCurrentRenderer() )
01415   {
01416     vtkFloatingPointType aCurrBBCenter[3];
01417     if ( ComputeBBCenter(GetCurrentRenderer(),aCurrBBCenter) )
01418     {
01419       if ( !myBBFirstCheck )
01420       {
01421         if ( fabs(aCurrBBCenter[0]-myBBCenter[0]) > 1e-38 ||
01422              fabs(aCurrBBCenter[1]-myBBCenter[1]) > 1e-38 ||
01423              fabs(aCurrBBCenter[2]-myBBCenter[2]) > 1e-38 ) {
01424           // bounding box was changed => send SVTK::RotationPointChanged event
01425           // invoke event for update coordinates in SVTK_SetRotationPointDlg
01426           InvokeEvent(SVTK::BBCenterChanged,(void*)aCurrBBCenter);
01427           for ( int i =0; i < 3; i++) myBBCenter[i] = aCurrBBCenter[i];
01428         }
01429       }
01430       else 
01431       {
01432         for ( int i =0; i < 3; i++) myBBCenter[i] = aCurrBBCenter[i];
01433         myBBFirstCheck = false;
01434       }
01435     }
01436   }
01437 }
01438 
01442 void SVTK_InteractorStyle::Render() 
01443 {
01444   this->Interactor->CreateTimer(VTKI_TIMER_FIRST);
01445 }
01446 
01447 void SVTK_InteractorStyle::onSpaceMouseMove( double* data )
01448 {
01449   // general things, do SetCurrentRenderer() within FindPokedRenderer() 
01450   int x, y;
01451   GetEventPosition( this->Interactor, x, y ); // current mouse position (from last mouse move event or any other event)
01452   FindPokedRenderer( x, y ); // calls SetCurrentRenderer
01453   
01454   IncrementalZoom( (int)data[2] );        // 1. push toward / pull backward = zoom out / zoom in
01455   IncrementalPan(  (int)data[0],  (int)data[1] );// 2. pull up / push down = pan up / down, 3. move left / right = pan left / right
01456   IncrementalRotate( 0,  (int)data[4] );   // 4. twist the control = rotate around Y axis
01457   IncrementalRotate( (int)data[3], 0  );   // 5. tilt the control forward/backward = rotate around X axis (Z axis of local coordinate system of space mouse)
01458 }
01459 
01460 void SVTK_InteractorStyle::onSpaceMouseButton( int button )
01461 {
01462   if( mySMDecreaseSpeedBtn == button ) {   
01463     ControllerIncrement()->Decrease();
01464   }
01465   if( mySMIncreaseSpeedBtn == button ) {    
01466     ControllerIncrement()->Increase();
01467   }
01468   if( mySMDominantCombinedSwitchBtn == button )    
01469     DominantCombinedSwitch();
01470 }
01471 
01472 void SVTK_InteractorStyle::DominantCombinedSwitch()
01473 {
01474   printf( "\n--DominantCombinedSwitch() NOT IMPLEMENTED--\n" );
01475 }
01476 
01480 void SVTK_InteractorStyle::drawRect()
01481 {
01482   if ( !myRectBand ) {
01483     myRectBand = new QRubberBand( QRubberBand::Rectangle, GetRenderWidget() );
01484     QPalette palette;
01485     palette.setColor(myRectBand->foregroundRole(), Qt::white);
01486     myRectBand->setPalette(palette);
01487   }
01488   myRectBand->hide();
01489 
01490   QRect aRect = SUIT_Tools::makeRect(myPoint.x(), myPoint.y(), myOtherPoint.x(), myOtherPoint.y());
01491   myRectBand->setGeometry( aRect );
01492   myRectBand->setVisible( aRect.isValid() );
01493 }
01494 
01498 void SVTK_InteractorStyle::endDrawRect()
01499 {
01500   if ( myRectBand ) myRectBand->hide();
01501 
01502   delete myRectBand;
01503   myRectBand = 0;
01504 }
01505 
01509 void SVTK_InteractorStyle::ProcessEvents( vtkObject* object,
01510                                           unsigned long event,
01511                                           void* clientData, 
01512                                           void* callData )
01513 {
01514   if ( clientData ) {
01515     vtkObject* anObject = reinterpret_cast<vtkObject*>( clientData );
01516     SVTK_InteractorStyle* self = dynamic_cast<SVTK_InteractorStyle*>( anObject );
01517     int aSpeedIncrement=self->ControllerIncrement()->Current();
01518     vtkFloatingPointType aCenter[3];
01519     vtkFloatingPointType* aSelectedPoint;
01520     if ( self ) {
01521       switch ( event ) {
01522       case SVTK::SpaceMouseMoveEvent : 
01523         self->onSpaceMouseMove( (double*)callData ); 
01524         return;
01525       case SVTK::SpaceMouseButtonEvent : 
01526         self->onSpaceMouseButton( *((int*)callData) ); 
01527         return;
01528       case SVTK::PanLeftEvent: 
01529         self->IncrementalPan(-aSpeedIncrement, 0);
01530         return;
01531       case SVTK::PanRightEvent:
01532         self->IncrementalPan(aSpeedIncrement, 0);
01533         return;
01534       case SVTK::PanUpEvent:
01535         self->IncrementalPan(0, aSpeedIncrement);
01536         return;
01537       case SVTK::PanDownEvent:
01538         self->IncrementalPan(0, -aSpeedIncrement);
01539         return;
01540       case SVTK::ZoomInEvent:
01541         self->IncrementalZoom(aSpeedIncrement);
01542         return;
01543       case SVTK::ZoomOutEvent:
01544         self->IncrementalZoom(-aSpeedIncrement);
01545         return;
01546       case SVTK::RotateLeftEvent: 
01547         self->IncrementalRotate(-aSpeedIncrement, 0);
01548         return;
01549       case SVTK::RotateRightEvent:
01550         self->IncrementalRotate(aSpeedIncrement, 0);
01551         return;
01552       case SVTK::RotateUpEvent:
01553         self->IncrementalRotate(0, -aSpeedIncrement);
01554         return;
01555       case SVTK::RotateDownEvent:
01556         self->IncrementalRotate(0, aSpeedIncrement);
01557         return;
01558       case SVTK::PlusSpeedIncrementEvent:
01559         self->ControllerIncrement()->Increase();
01560         return;
01561       case SVTK::MinusSpeedIncrementEvent:
01562         self->ControllerIncrement()->Decrease();
01563         return;
01564       case SVTK::SetSpeedIncrementEvent:
01565         self->ControllerIncrement()->SetStartValue(*((int*)callData));
01566         return;
01567 
01568       case SVTK::SetSMDecreaseSpeedEvent:
01569         self->mySMDecreaseSpeedBtn = *((int*)callData);
01570         return;
01571       case SVTK::SetSMIncreaseSpeedEvent:
01572         self->mySMIncreaseSpeedBtn = *((int*)callData);
01573         return;
01574       case SVTK::SetSMDominantCombinedSwitchEvent:
01575         self->mySMDominantCombinedSwitchBtn = *((int*)callData);
01576         return;
01577 
01578       case SVTK::StartZoom:
01579         self->startZoom();
01580         return;
01581       case SVTK::StartPan:
01582         self->startPan();
01583         return;
01584       case SVTK::StartRotate:
01585         self->startRotate();
01586         return;
01587       case SVTK::StartGlobalPan:
01588         self->startGlobalPan();
01589         return;
01590       case SVTK::StartFitArea:
01591         self->startFitArea();
01592         return;
01593 
01594       case SVTK::SetRotateGravity:
01595         if ( self->myCurrRotationPointType == SVTK::StartPointSelection )
01596         {
01597           self->myHighlightSelectionPointActor->SetVisibility( false );
01598           if( self->GetCurrentRenderer() != NULL )
01599             self->GetCurrentRenderer()->RemoveActor( self->myHighlightSelectionPointActor.GetPointer() );
01600           self->GetRenderWidget()->setCursor(self->myDefCursor); 
01601         }
01602         self->myPrevRotationPointType = self->myCurrRotationPointType;
01603         self->myCurrRotationPointType = SVTK::SetRotateGravity;
01604         if ( ComputeBBCenter(self->GetCurrentRenderer(),aCenter) ) 
01605           // invoke event for update coordinates in SVTK_SetRotationPointDlg
01606           self->InvokeEvent(SVTK::BBCenterChanged,(void*)aCenter);
01607         return;
01608       case SVTK::StartPointSelection:
01609         self->startPointSelection();
01610         return;
01611 
01612       case SVTK::ChangeRotationPoint:
01613         if ( self->myCurrRotationPointType == SVTK::StartPointSelection )
01614         {
01615           self->myHighlightSelectionPointActor->SetVisibility( false );
01616           if( self->GetCurrentRenderer() != NULL )
01617             self->GetCurrentRenderer()->RemoveActor( self->myHighlightSelectionPointActor.GetPointer() );
01618           self->GetRenderWidget()->setCursor(self->myDefCursor); 
01619         }
01620         self->myPrevRotationPointType = self->myCurrRotationPointType;
01621         self->myCurrRotationPointType = SVTK::SetRotateSelected;
01622         aSelectedPoint = (vtkFloatingPointType*)callData;
01623         self->myRotationPointX = aSelectedPoint[0];
01624         self->myRotationPointY = aSelectedPoint[1];
01625         self->myRotationPointZ = aSelectedPoint[2];
01626         return;
01627 
01628       case SVTK::SetFocalPointGravity:
01629         if ( self->myCurrFocalPointType == SVTK::StartPointSelection )
01630         {
01631           self->myHighlightSelectionPointActor->SetVisibility( false );
01632           if( self->GetCurrentRenderer() != NULL )
01633             self->GetCurrentRenderer()->RemoveActor( self->myHighlightSelectionPointActor.GetPointer() );
01634           self->GetRenderWidget()->setCursor(self->myDefCursor); 
01635         }
01636         self->myCurrFocalPointType = SVTK::SetFocalPointGravity;
01637         if ( ComputeBBCenter(self->GetCurrentRenderer(),aCenter) ) {
01638           // invoke event for update coordinates in SVTK_ViewParameterDlg
01639           self->InvokeEvent(SVTK::FocalPointChanged,(void*)aCenter);
01640         }
01641         return;
01642       case SVTK::StartFocalPointSelection:
01643         self->startFocalPointSelection();
01644         return;
01645 
01646       case SVTK::SetFocalPointSelected:
01647         if ( self->myCurrFocalPointType == SVTK::StartFocalPointSelection )
01648         {
01649           self->myHighlightSelectionPointActor->SetVisibility( false );
01650           if( self->GetCurrentRenderer() != NULL )
01651             self->GetCurrentRenderer()->RemoveActor( self->myHighlightSelectionPointActor.GetPointer() );
01652           self->GetRenderWidget()->setCursor(self->myDefCursor); 
01653         }
01654         self->myPrevFocalPointType = self->myCurrFocalPointType;
01655         self->myCurrFocalPointType = SVTK::SetFocalPointSelected;
01656         return;
01657       }
01658     }
01659   }
01660 
01661   Superclass::ProcessEvents( object, event, clientData, callData );
01662 }
01663 
01667 void SVTK_InteractorStyle::OnChar()
01668 {
01669   char key = GetInteractor()->GetKeyCode();
01670   switch ( key ) {
01671   case '+': ControllerIncrement()->Increase(); break;
01672   case '-': ControllerIncrement()->Decrease(); break;
01673   }
01674 }
01675 
01679 void SVTK_InteractorStyle::OnKeyDown()
01680 {
01681   bool bInvokeSuperclass=myControllerOnKeyDown->OnKeyDown(this);
01682   if (bInvokeSuperclass){
01683     Superclass::OnKeyDown();
01684   }
01685 }
01686 
01690 void SVTK_InteractorStyle::ActionPicking()
01691 {
01692   int x, y;
01693   Interactor->GetEventPosition( x, y ); 
01694   FindPokedRenderer( x, y ); 
01695   
01696   myOtherPoint = myPoint = QPoint(x, y);
01697   
01698   startOperation(VTK_INTERACTOR_STYLE_CAMERA_SELECT);
01699   onFinishOperation();
01700   startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
01701 }
01702 
01706 void SVTK_InteractorStyle::SetControllerOnKeyDown(SVTK_ControllerOnKeyDown* theController)
01707 {
01708   myControllerOnKeyDown=theController;
01709 }
01710 
01714 SVTK_ControllerOnKeyDown* SVTK_InteractorStyle::ControllerOnKeyDown()
01715 {
01716   return myControllerOnKeyDown.GetPointer();
01717 }
01718 
01722 void SVTK_InteractorStyle::SetControllerIncrement(SVTK_ControllerIncrement* theController)
01723 {
01724   myControllerIncrement=theController;
01725 }
01726 
01730 void SVTK_InteractorStyle::SetIncrementSpeed(const int theValue, const int theMode)
01731 {
01732   SVTK_ControllerIncrement* c = 0;
01733   switch (theMode) {
01734   case 0: c = SVTK_ControllerIncrement::New(); break;
01735   case 1: c = SVTK_GeomControllerIncrement::New(); break;
01736   }
01737   c->SetStartValue(theValue);
01738 
01739   SetControllerIncrement(c);
01740   c->Delete();
01741 }
01742 
01746 SVTK_ControllerIncrement* SVTK_InteractorStyle::ControllerIncrement()
01747 {
01748   return myControllerIncrement.GetPointer();
01749 }
01750 
01751 vtkStandardNewMacro(SVTK_ControllerIncrement);
01752 SVTK_ControllerIncrement::SVTK_ControllerIncrement()
01753 {
01754   myIncrement=10;
01755 }
01756 SVTK_ControllerIncrement::~SVTK_ControllerIncrement()
01757 {
01758 }
01759 void SVTK_ControllerIncrement::SetStartValue(const int theValue)
01760 {
01761   myIncrement=theValue;
01762 }
01763 int SVTK_ControllerIncrement::Current()const
01764 {
01765   return myIncrement;
01766 }
01767 int SVTK_ControllerIncrement::Increase()
01768 {
01769   ++myIncrement;
01770   return myIncrement;
01771 }
01772 int SVTK_ControllerIncrement::Decrease()
01773 {
01774   if (myIncrement>1){
01775     --myIncrement;
01776   }
01777   return myIncrement;
01778 }
01779 
01780 vtkStandardNewMacro(SVTK_GeomControllerIncrement);
01781 SVTK_GeomControllerIncrement::SVTK_GeomControllerIncrement()
01782 {
01783 }
01784 SVTK_GeomControllerIncrement::~SVTK_GeomControllerIncrement()
01785 {
01786 }
01787 int SVTK_GeomControllerIncrement::Increase()
01788 {
01789   myIncrement*=2;
01790   return myIncrement;
01791 }
01792 int SVTK_GeomControllerIncrement::Decrease()
01793 {
01794   myIncrement/=2;
01795   if (myIncrement<1){
01796     myIncrement=1;
01797   }
01798   return myIncrement;
01799 }
01800 
01801 vtkStandardNewMacro(SVTK_ControllerOnKeyDown);
01802 
01806 SVTK_ControllerOnKeyDown::SVTK_ControllerOnKeyDown()
01807 {
01808 }
01809 
01813 SVTK_ControllerOnKeyDown::~SVTK_ControllerOnKeyDown()
01814 {
01815 }
01816 
01817 bool SVTK_ControllerOnKeyDown::OnKeyDown(vtkInteractorStyle* theIS)
01818 {
01819   return true;
01820 }