1 #include <cpExtensions/Visualization/BaseInteractorStyle.h>
5 #include <vtkCallbackCommand.h>
7 #include <vtkRenderer.h>
8 #include <vtkRenderWindowInteractor.h>
10 // -------------------------------------------------------------------------
11 const long cpExtensions::Visualization::BaseInteractorStyle::MouseButtonEvent::
12 MAX_DOUBLE_CLICK = 200; // ms
14 // -------------------------------------------------------------------------
15 cpExtensions::Visualization::BaseInteractorStyle::
16 Self* cpExtensions::Visualization::BaseInteractorStyle::
22 // -------------------------------------------------------------------------
23 void cpExtensions::Visualization::BaseInteractorStyle::
24 DelegateTDxEvent( unsigned long event, void* calldata )
27 std::cerr << "No TDx support at this time!" << std::endl;
31 // -------------------------------------------------------------------------
32 void cpExtensions::Visualization::BaseInteractorStyle::
35 // Get current position on the associated actors
36 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
41 bool alt = ( rwi->GetAltKey( ) == 1 );
42 bool ctr = ( rwi->GetControlKey( ) == 1 );
43 bool sft = ( rwi->GetShiftKey( ) == 1 );
44 ButtonID button = this->GetButtonID( );
46 // Invoke possible generic events
47 if( button == Self::ButtonID_Right )
49 if( !alt && !ctr && !sft )
51 this->FindPokedRenderer(
52 rwi->GetEventPosition( )[ 0 ],
53 rwi->GetEventPosition( )[ 1 ]
59 else if( button == Self::ButtonID_Middle )
61 if( !alt && !ctr && !sft )
63 this->FindPokedRenderer(
64 rwi->GetEventPosition( )[ 0 ],
65 rwi->GetEventPosition( )[ 1 ]
74 // -------------------------------------------------------------------------
75 void cpExtensions::Visualization::BaseInteractorStyle::
78 this->ActiveButton = Self::ButtonID_Left;
81 // -------------------------------------------------------------------------
82 void cpExtensions::Visualization::BaseInteractorStyle::
85 this->ActiveButton = Self::ButtonID_None;
88 // -------------------------------------------------------------------------
89 void cpExtensions::Visualization::BaseInteractorStyle::
92 this->ActiveButton = Self::ButtonID_Middle;
94 // Get current position on the associated actors
95 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
100 bool alt = ( rwi->GetAltKey( ) == 1 );
101 bool ctr = ( rwi->GetControlKey( ) == 1 );
102 bool sft = ( rwi->GetShiftKey( ) == 1 );
104 if( !alt && !ctr && !sft )
108 // -------------------------------------------------------------------------
109 void cpExtensions::Visualization::BaseInteractorStyle::
112 this->ActiveButton = Self::ButtonID_None;
114 // Get current position on the associated actors
115 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
120 bool alt = ( rwi->GetAltKey( ) == 1 );
121 bool ctr = ( rwi->GetControlKey( ) == 1 );
122 bool sft = ( rwi->GetShiftKey( ) == 1 );
124 switch( this->State )
134 // -------------------------------------------------------------------------
135 void cpExtensions::Visualization::BaseInteractorStyle::
138 this->ActiveButton = Self::ButtonID_Right;
140 // Get current position on the associated actors
141 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
146 bool alt = ( rwi->GetAltKey( ) == 1 );
147 bool ctr = ( rwi->GetControlKey( ) == 1 );
148 bool sft = ( rwi->GetShiftKey( ) == 1 );
150 if( !alt && !ctr && !sft )
154 // -------------------------------------------------------------------------
155 void cpExtensions::Visualization::BaseInteractorStyle::
158 this->ActiveButton = Self::ButtonID_None;
160 // Get current position on the associated actors
161 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
166 bool alt = ( rwi->GetAltKey( ) == 1 );
167 bool ctr = ( rwi->GetControlKey( ) == 1 );
168 bool sft = ( rwi->GetShiftKey( ) == 1 );
170 switch( this->State )
180 // -------------------------------------------------------------------------
181 void cpExtensions::Visualization::BaseInteractorStyle::
184 if( this->CurrentRenderer == NULL )
187 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
188 double *center = this->CurrentRenderer->GetCenter( );
189 int dy = rwi->GetEventPosition( )[ 1 ] - rwi->GetLastEventPosition( )[ 1 ];
190 double dyf = this->MotionFactor * dy / center[ 1 ];
191 this->_Dolly( std::pow( 1.1, dyf ) );
194 // -------------------------------------------------------------------------
195 void cpExtensions::Visualization::BaseInteractorStyle::
198 if( this->CurrentRenderer == NULL )
201 vtkRenderWindowInteractor* rwi = this->Interactor;
202 double viewFocus[ 4 ], focalDepth, viewPoint[ 3 ];
203 double newPickPoint[ 4 ], oldPickPoint[ 4 ], motionVector[ 3 ];
205 // Calculate the focal depth since we'll be using it a lot
206 vtkCamera* camera = this->CurrentRenderer->GetActiveCamera( );
207 camera->GetFocalPoint( viewFocus );
208 this->ComputeWorldToDisplay(
209 viewFocus[ 0 ], viewFocus[ 1 ], viewFocus[ 2 ], viewFocus
211 focalDepth = viewFocus[ 2 ];
212 this->ComputeDisplayToWorld(
213 rwi->GetEventPosition( )[ 0 ],
214 rwi->GetEventPosition( )[ 1 ],
219 // Has to recalc old mouse point since the viewport has moved,
220 // so can't move it outside the loop
221 this->ComputeDisplayToWorld(
222 rwi->GetLastEventPosition( )[ 0 ],
223 rwi->GetLastEventPosition( )[ 1 ],
228 // Camera motion is reversed
229 motionVector[ 0 ] = oldPickPoint[ 0 ] - newPickPoint[ 0 ];
230 motionVector[ 1 ] = oldPickPoint[ 1 ] - newPickPoint[ 1 ];
231 motionVector[ 2 ] = oldPickPoint[ 2 ] - newPickPoint[ 2 ];
233 camera->GetFocalPoint( viewFocus );
234 camera->GetPosition( viewPoint );
235 camera->SetFocalPoint(
236 motionVector[ 0 ] + viewFocus[ 0 ],
237 motionVector[ 1 ] + viewFocus[ 1 ],
238 motionVector[ 2 ] + viewFocus[ 2 ]
241 motionVector[ 0 ] + viewPoint[ 0 ],
242 motionVector[ 1 ] + viewPoint[ 1 ],
243 motionVector[ 2 ] + viewPoint[ 2 ]
245 if( rwi->GetLightFollowCamera( ) )
246 this->CurrentRenderer->UpdateLightsGeometryToFollowCamera( );
250 // -------------------------------------------------------------------------
251 cpExtensions::Visualization::BaseInteractorStyle::
252 BaseInteractorStyle( )
254 MotionFactor( double( 10 ) )
256 this->LeftButtonEvent.Reset( );
257 this->MiddleButtonEvent.Reset( );
258 this->RightButtonEvent.Reset( );
259 this->ActiveButton = Self::ButtonID_None;
261 this->EventCallbackCommand->SetCallback( Self::_ProcessEvents );
264 // -------------------------------------------------------------------------
265 cpExtensions::Visualization::BaseInteractorStyle::
266 ~BaseInteractorStyle( )
270 // -------------------------------------------------------------------------
271 void cpExtensions::Visualization::BaseInteractorStyle::
272 _Dolly( double factor )
274 if( this->CurrentRenderer == NULL )
277 vtkCamera* camera = this->CurrentRenderer->GetActiveCamera( );
278 if( camera->GetParallelProjection( ) == 0 )
280 camera->Dolly( factor );
281 if( this->AutoAdjustCameraClippingRange )
282 this->CurrentRenderer->ResetCameraClippingRange( );
285 camera->SetParallelScale( camera->GetParallelScale( ) / factor );
286 if( this->Interactor->GetLightFollowCamera( ) )
287 this->CurrentRenderer->UpdateLightsGeometryToFollowCamera( );
288 this->Interactor->Render( );
291 // -------------------------------------------------------------------------
292 void cpExtensions::Visualization::BaseInteractorStyle::
300 // Get active style and interactor
301 Self* s = reinterpret_cast< Self* >( clientdata );
308 case vtkCommand::MouseMoveEvent:
313 case vtkCommand::LeftButtonPressEvent:
315 unsigned char nc = s->LeftButtonEvent.Clicks( );
317 s->OnLeftDoubleClick( );
320 s->OnLeftButtonDown( );
323 case vtkCommand::LeftButtonReleaseEvent:
325 s->LeftButtonEvent.Release( );
326 s->OnLeftButtonUp( );
329 case vtkCommand::MiddleButtonPressEvent:
331 unsigned char nc = s->MiddleButtonEvent.Clicks( );
333 s->OnMiddleDoubleClick( );
336 s->OnMiddleButtonDown( );
339 case vtkCommand::MiddleButtonReleaseEvent:
341 s->MiddleButtonEvent.Release( );
342 s->OnMiddleButtonUp( );
345 case vtkCommand::RightButtonPressEvent:
347 unsigned char nc = s->RightButtonEvent.Clicks( );
349 s->OnRightDoubleClick( );
352 s->OnRightButtonDown( );
355 case vtkCommand::RightButtonReleaseEvent:
357 s->RightButtonEvent.Release( );
358 s->OnRightButtonUp( );
361 case vtkCommand::MouseWheelForwardEvent:
363 s->OnMouseWheelForward( );
366 case vtkCommand::MouseWheelBackwardEvent:
368 s->OnMouseWheelBackward( );
371 case vtkCommand::KeyPressEvent:
377 case vtkCommand::KeyReleaseEvent:
383 case vtkCommand::CharEvent:
388 case vtkCommand::ExposeEvent:
393 case vtkCommand::ConfigureEvent:
398 case vtkCommand::EnterEvent:
403 case vtkCommand::LeaveEvent:
408 case vtkCommand::TimerEvent:
413 case vtkCommand::DeleteEvent:
415 s->SetInteractor( 0 );
418 case vtkCommand::TDxMotionEvent:
419 case vtkCommand::TDxButtonPressEvent:
420 case vtkCommand::TDxButtonReleaseEvent:
422 s->DelegateTDxEvent( event, calldata );