1 #include <cpExtensions/Visualization/BaseInteractorStyle.h>
5 #include <vtkCallbackCommand.h>
7 #include <vtkRenderer.h>
8 #include <vtkRenderWindowInteractor.h>
10 // -------------------------------------------------------------------------
11 long cpExtensions::Visualization::BaseInteractorStyle::_TMouseButtonEvent::
12 MaxDoubleClick = 200; // ms
14 // -------------------------------------------------------------------------
15 void cpExtensions::Visualization::BaseInteractorStyle::
16 AddMouseMoveCommand( TMouseCommand command, void* data )
18 this->m_MouseMoveCommands.push_back(
19 std::pair< TMouseCommand, void* >( command, data )
24 // -------------------------------------------------------------------------
25 void cpExtensions::Visualization::BaseInteractorStyle::
26 AddMouseClickCommand( TMouseCommand command, void* data )
28 this->m_MouseClickCommands.push_back(
29 std::pair< TMouseCommand, void* >( command, data )
34 // -------------------------------------------------------------------------
35 void cpExtensions::Visualization::BaseInteractorStyle::
36 AddMouseDoubleClickCommand( TMouseCommand command, void* data )
38 this->m_MouseDoubleClickCommands.push_back(
39 std::pair< TMouseCommand, void* >( command, data )
44 // -------------------------------------------------------------------------
45 void cpExtensions::Visualization::BaseInteractorStyle::
46 AddMouseWheelCommand( TMouseWheelCommand command, void* data )
48 this->m_MouseWheelCommands.push_back(
49 std::pair< TMouseWheelCommand, void* >( command, data )
54 // -------------------------------------------------------------------------
55 void cpExtensions::Visualization::BaseInteractorStyle::
56 AddKeyCommand( TKeyCommand command, void* data )
58 this->m_KeyCommands.push_back(
59 std::pair< TKeyCommand, void* >( command, data )
64 // -------------------------------------------------------------------------
65 void cpExtensions::Visualization::BaseInteractorStyle::
66 AddExposeCommand( TVoidCommand command, void* data )
68 this->m_ExposeCommands.push_back(
69 std::pair< TVoidCommand, void* >( command, data )
74 // -------------------------------------------------------------------------
75 void cpExtensions::Visualization::BaseInteractorStyle::
76 AddConfigureCommand( TVoidCommand command, void* data )
78 this->m_ConfigureCommands.push_back(
79 std::pair< TVoidCommand, void* >( command, data )
84 // -------------------------------------------------------------------------
85 void cpExtensions::Visualization::BaseInteractorStyle::
86 AddEnterCommand( TVoidCommand command, void* data )
88 this->m_EnterCommands.push_back(
89 std::pair< TVoidCommand, void* >( command, data )
94 // -------------------------------------------------------------------------
95 void cpExtensions::Visualization::BaseInteractorStyle::
96 AddLeaveCommand( TVoidCommand command, void* data )
98 this->m_LeaveCommands.push_back(
99 std::pair< TVoidCommand, void* >( command, data )
104 // -------------------------------------------------------------------------
105 void cpExtensions::Visualization::BaseInteractorStyle::
106 DelegateTDxEvent( unsigned long event, void* calldata )
109 std::cerr << "No TDx support at this time!" << std::endl;
113 // -------------------------------------------------------------------------
114 void cpExtensions::Visualization::BaseInteractorStyle::
117 // Get current position on the associated actors
118 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
123 bool alt = ( rwi->GetAltKey( ) == 1 );
124 bool ctr = ( rwi->GetControlKey( ) == 1 );
125 bool sft = ( rwi->GetShiftKey( ) == 1 );
126 ButtonID button = this->GetButtonID( );
128 // Invoke possible generic events
129 if( button == Self::ButtonID_Right )
131 if( !alt && !ctr && !sft )
133 this->FindPokedRenderer(
134 rwi->GetEventPosition( )[ 0 ],
135 rwi->GetEventPosition( )[ 1 ]
141 else if( button == Self::ButtonID_Middle )
143 if( !alt && !ctr && !sft )
145 this->FindPokedRenderer(
146 rwi->GetEventPosition( )[ 0 ],
147 rwi->GetEventPosition( )[ 1 ]
155 // Get mouse pointer position
157 if( !( this->_PickPosition( pos ) ) )
160 // Invoke possible specialized events
161 for( unsigned int i = 0; i < this->m_MouseMoveCommands.size( ); ++i )
162 this->m_MouseMoveCommands[ i ].first(
163 this->m_MouseMoveCommands[ i ].second, button, pos, alt, ctr, sft
167 // -------------------------------------------------------------------------
168 void cpExtensions::Visualization::BaseInteractorStyle::
169 OnMouseWheelForward( )
171 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
175 // Invoke possible events
176 for( unsigned int i = 0; i < this->m_MouseWheelCommands.size( ); ++i )
177 this->m_MouseWheelCommands[ i ].first(
178 this->m_MouseWheelCommands[ i ].second, 1,
179 rwi->GetAltKey( ) == 1,
180 rwi->GetControlKey( ) == 1,
181 rwi->GetShiftKey( ) == 1
185 // -------------------------------------------------------------------------
186 void cpExtensions::Visualization::BaseInteractorStyle::
187 OnMouseWheelBackward( )
189 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
193 // Invoke possible events
194 for( unsigned int i = 0; i < this->m_MouseWheelCommands.size( ); ++i )
195 this->m_MouseWheelCommands[ i ].first(
196 this->m_MouseWheelCommands[ i ].second, -1,
197 rwi->GetAltKey( ) == 1,
198 rwi->GetControlKey( ) == 1,
199 rwi->GetShiftKey( ) == 1
203 // -------------------------------------------------------------------------
204 void cpExtensions::Visualization::BaseInteractorStyle::
207 this->m_ActiveButton = Self::ButtonID_Left;
210 // -------------------------------------------------------------------------
211 void cpExtensions::Visualization::BaseInteractorStyle::
214 this->m_ActiveButton = Self::ButtonID_None;
217 // -------------------------------------------------------------------------
218 void cpExtensions::Visualization::BaseInteractorStyle::
219 OnMiddleButtonDown( )
221 this->m_ActiveButton = Self::ButtonID_Middle;
223 // Get current position on the associated actors
224 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
229 bool alt = ( rwi->GetAltKey( ) == 1 );
230 bool ctr = ( rwi->GetControlKey( ) == 1 );
231 bool sft = ( rwi->GetShiftKey( ) == 1 );
233 if( !alt && !ctr && !sft )
237 // -------------------------------------------------------------------------
238 void cpExtensions::Visualization::BaseInteractorStyle::
241 this->m_ActiveButton = Self::ButtonID_None;
243 // Get current position on the associated actors
244 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
249 bool alt = ( rwi->GetAltKey( ) == 1 );
250 bool ctr = ( rwi->GetControlKey( ) == 1 );
251 bool sft = ( rwi->GetShiftKey( ) == 1 );
253 switch( this->State )
263 // -------------------------------------------------------------------------
264 void cpExtensions::Visualization::BaseInteractorStyle::
267 this->m_ActiveButton = Self::ButtonID_Right;
269 // Get current position on the associated actors
270 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
275 bool alt = ( rwi->GetAltKey( ) == 1 );
276 bool ctr = ( rwi->GetControlKey( ) == 1 );
277 bool sft = ( rwi->GetShiftKey( ) == 1 );
279 if( !alt && !ctr && !sft )
283 // -------------------------------------------------------------------------
284 void cpExtensions::Visualization::BaseInteractorStyle::
287 this->m_ActiveButton = Self::ButtonID_None;
289 // Get current position on the associated actors
290 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
295 bool alt = ( rwi->GetAltKey( ) == 1 );
296 bool ctr = ( rwi->GetControlKey( ) == 1 );
297 bool sft = ( rwi->GetShiftKey( ) == 1 );
299 switch( this->State )
309 // -------------------------------------------------------------------------
310 void cpExtensions::Visualization::BaseInteractorStyle::
313 // Get current position on the associated actors
314 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
318 // Get mouse pointer position
320 if( !( this->_PickPosition( pos ) ) )
323 // Invoke possible events
324 for( unsigned int i = 0; i < this->m_MouseClickCommands.size( ); ++i )
325 this->m_MouseClickCommands[ i ].first(
326 this->m_MouseClickCommands[ i ].second,
329 rwi->GetAltKey( ) == 1,
330 rwi->GetControlKey( ) == 1,
331 rwi->GetShiftKey( ) == 1
335 // -------------------------------------------------------------------------
336 void cpExtensions::Visualization::BaseInteractorStyle::
339 // Get current position on the associated actors
340 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
344 // Get mouse pointer position
346 if( !( this->_PickPosition( pos ) ) )
349 // Invoke possible events
350 for( unsigned int i = 0; i < this->m_MouseClickCommands.size( ); ++i )
351 this->m_MouseDoubleClickCommands[ i ].first(
352 this->m_MouseDoubleClickCommands[ i ].second,
355 rwi->GetAltKey( ) == 1,
356 rwi->GetControlKey( ) == 1,
357 rwi->GetShiftKey( ) == 1
361 // -------------------------------------------------------------------------
362 void cpExtensions::Visualization::BaseInteractorStyle::
365 // Get current position on the associated actors
366 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
370 // Get mouse pointer position
372 if( !( this->_PickPosition( pos ) ) )
375 // Invoke possible events
376 for( unsigned int i = 0; i < this->m_MouseClickCommands.size( ); ++i )
377 this->m_MouseClickCommands[ i ].first(
378 this->m_MouseClickCommands[ i ].second,
379 Self::ButtonID_Middle,
381 rwi->GetAltKey( ) == 1,
382 rwi->GetControlKey( ) == 1,
383 rwi->GetShiftKey( ) == 1
387 // -------------------------------------------------------------------------
388 void cpExtensions::Visualization::BaseInteractorStyle::
389 OnMiddleDoubleClick( )
391 // Get current position on the associated actors
392 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
396 // Get mouse pointer position
398 if( !( this->_PickPosition( pos ) ) )
401 // Invoke possible events
402 for( unsigned int i = 0; i < this->m_MouseClickCommands.size( ); ++i )
403 this->m_MouseDoubleClickCommands[ i ].first(
404 this->m_MouseDoubleClickCommands[ i ].second,
405 Self::ButtonID_Middle,
407 rwi->GetAltKey( ) == 1,
408 rwi->GetControlKey( ) == 1,
409 rwi->GetShiftKey( ) == 1
413 // -------------------------------------------------------------------------
414 void cpExtensions::Visualization::BaseInteractorStyle::
417 // Get current position on the associated actors
418 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
422 // Get mouse pointer position
424 if( !( this->_PickPosition( pos ) ) )
427 // Invoke possible events
428 for( unsigned int i = 0; i < this->m_MouseClickCommands.size( ); ++i )
429 this->m_MouseClickCommands[ i ].first(
430 this->m_MouseClickCommands[ i ].second,
431 Self::ButtonID_Right,
433 rwi->GetAltKey( ) == 1,
434 rwi->GetControlKey( ) == 1,
435 rwi->GetShiftKey( ) == 1
439 // -------------------------------------------------------------------------
440 void cpExtensions::Visualization::BaseInteractorStyle::
441 OnRightDoubleClick( )
443 // Get current position on the associated actors
444 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
448 // Get mouse pointer position
450 if( !( this->_PickPosition( pos ) ) )
453 // Invoke possible events
454 for( unsigned int i = 0; i < this->m_MouseClickCommands.size( ); ++i )
455 this->m_MouseDoubleClickCommands[ i ].first(
456 this->m_MouseDoubleClickCommands[ i ].second,
457 Self::ButtonID_Right,
459 rwi->GetAltKey( ) == 1,
460 rwi->GetControlKey( ) == 1,
461 rwi->GetShiftKey( ) == 1
465 // -------------------------------------------------------------------------
466 void cpExtensions::Visualization::BaseInteractorStyle::
469 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
473 // Invoke possible events
474 for( unsigned int i = 0; i < this->m_KeyCommands.size( ); ++i )
475 this->m_KeyCommands[ i ].first(
476 this->m_KeyCommands[ i ].second,
481 // -------------------------------------------------------------------------
482 void cpExtensions::Visualization::BaseInteractorStyle::
487 // -------------------------------------------------------------------------
488 void cpExtensions::Visualization::BaseInteractorStyle::
493 // -------------------------------------------------------------------------
494 void cpExtensions::Visualization::BaseInteractorStyle::
499 // -------------------------------------------------------------------------
500 void cpExtensions::Visualization::BaseInteractorStyle::
505 // -------------------------------------------------------------------------
506 void cpExtensions::Visualization::BaseInteractorStyle::
509 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
513 // Invoke possible events
514 for( unsigned int i = 0; i < this->m_ExposeCommands.size( ); ++i )
515 this->m_ExposeCommands[ i ].first( this->m_ExposeCommands[ i ].second );
518 // -------------------------------------------------------------------------
519 void cpExtensions::Visualization::BaseInteractorStyle::
522 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
526 // Invoke possible events
527 for( unsigned int i = 0; i < this->m_ConfigureCommands.size( ); ++i )
528 this->m_ConfigureCommands[ i ].first(
529 this->m_ConfigureCommands[ i ].second
533 // -------------------------------------------------------------------------
534 void cpExtensions::Visualization::BaseInteractorStyle::
537 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
541 // Invoke possible events
542 for( unsigned int i = 0; i < this->m_EnterCommands.size( ); ++i )
543 this->m_EnterCommands[ i ].first( this->m_EnterCommands[ i ].second );
546 // -------------------------------------------------------------------------
547 void cpExtensions::Visualization::BaseInteractorStyle::
550 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
554 // Invoke possible events
555 for( unsigned int i = 0; i < this->m_LeaveCommands.size( ); ++i )
556 this->m_LeaveCommands[ i ].first( this->m_LeaveCommands[ i ].second );
559 // -------------------------------------------------------------------------
560 void cpExtensions::Visualization::BaseInteractorStyle::
563 if( this->CurrentRenderer == NULL )
566 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
567 double *center = this->CurrentRenderer->GetCenter( );
568 int dy = rwi->GetEventPosition( )[ 1 ] - rwi->GetLastEventPosition( )[ 1 ];
569 double dyf = this->m_MotionFactor * dy / center[ 1 ];
570 this->_Dolly( std::pow( 1.1, dyf ) );
573 // -------------------------------------------------------------------------
574 void cpExtensions::Visualization::BaseInteractorStyle::
577 if( this->CurrentRenderer == NULL )
580 vtkRenderWindowInteractor* rwi = this->Interactor;
581 double viewFocus[ 4 ], focalDepth, viewPoint[ 3 ];
582 double newPickPoint[ 4 ], oldPickPoint[ 4 ], motionVector[ 3 ];
584 // Calculate the focal depth since we'll be using it a lot
585 vtkCamera* camera = this->CurrentRenderer->GetActiveCamera( );
586 camera->GetFocalPoint( viewFocus );
587 this->ComputeWorldToDisplay(
588 viewFocus[ 0 ], viewFocus[ 1 ], viewFocus[ 2 ], viewFocus
590 focalDepth = viewFocus[ 2 ];
591 this->ComputeDisplayToWorld(
592 rwi->GetEventPosition( )[ 0 ],
593 rwi->GetEventPosition( )[ 1 ],
598 // Has to recalc old mouse point since the viewport has moved,
599 // so can't move it outside the loop
600 this->ComputeDisplayToWorld(
601 rwi->GetLastEventPosition( )[ 0 ],
602 rwi->GetLastEventPosition( )[ 1 ],
607 // Camera motion is reversed
608 motionVector[ 0 ] = oldPickPoint[ 0 ] - newPickPoint[ 0 ];
609 motionVector[ 1 ] = oldPickPoint[ 1 ] - newPickPoint[ 1 ];
610 motionVector[ 2 ] = oldPickPoint[ 2 ] - newPickPoint[ 2 ];
612 camera->GetFocalPoint( viewFocus );
613 camera->GetPosition( viewPoint );
614 camera->SetFocalPoint(
615 motionVector[ 0 ] + viewFocus[ 0 ],
616 motionVector[ 1 ] + viewFocus[ 1 ],
617 motionVector[ 2 ] + viewFocus[ 2 ]
620 motionVector[ 0 ] + viewPoint[ 0 ],
621 motionVector[ 1 ] + viewPoint[ 1 ],
622 motionVector[ 2 ] + viewPoint[ 2 ]
624 if( rwi->GetLightFollowCamera( ) )
625 this->CurrentRenderer->UpdateLightsGeometryToFollowCamera( );
629 // -------------------------------------------------------------------------
630 cpExtensions::Visualization::BaseInteractorStyle::
631 BaseInteractorStyle( )
633 m_MotionFactor( double( 10 ) )
635 this->m_LeftButtonEvent.Reset( );
636 this->m_MiddleButtonEvent.Reset( );
637 this->m_RightButtonEvent.Reset( );
638 this->m_ActiveButton = Self::ButtonID_None;
640 this->EventCallbackCommand->SetCallback( Self::_ProcessEvents );
643 // -------------------------------------------------------------------------
644 cpExtensions::Visualization::BaseInteractorStyle::
645 ~BaseInteractorStyle( )
649 // -------------------------------------------------------------------------
650 void cpExtensions::Visualization::BaseInteractorStyle::
651 _Dolly( double factor )
653 if( this->CurrentRenderer == NULL )
656 vtkCamera* camera = this->CurrentRenderer->GetActiveCamera( );
657 if( camera->GetParallelProjection( ) == 0 )
659 camera->Dolly( factor );
660 if( this->AutoAdjustCameraClippingRange )
661 this->CurrentRenderer->ResetCameraClippingRange( );
664 camera->SetParallelScale( camera->GetParallelScale( ) / factor );
665 if( this->Interactor->GetLightFollowCamera( ) )
666 this->CurrentRenderer->UpdateLightsGeometryToFollowCamera( );
667 this->Interactor->Render( );
670 // -------------------------------------------------------------------------
671 void cpExtensions::Visualization::BaseInteractorStyle::
679 // Get active style and interactor
680 Self* s = reinterpret_cast< Self* >( clientdata );
687 case vtkCommand::MouseMoveEvent:
692 case vtkCommand::LeftButtonPressEvent:
694 unsigned char nc = s->m_LeftButtonEvent.Clicks( );
696 s->OnLeftDoubleClick( );
699 s->OnLeftButtonDown( );
702 case vtkCommand::LeftButtonReleaseEvent:
704 s->m_LeftButtonEvent.Release( );
705 s->OnLeftButtonUp( );
708 case vtkCommand::MiddleButtonPressEvent:
710 unsigned char nc = s->m_MiddleButtonEvent.Clicks( );
712 s->OnMiddleDoubleClick( );
715 s->OnMiddleButtonDown( );
718 case vtkCommand::MiddleButtonReleaseEvent:
720 s->m_MiddleButtonEvent.Release( );
721 s->OnMiddleButtonUp( );
724 case vtkCommand::RightButtonPressEvent:
726 unsigned char nc = s->m_RightButtonEvent.Clicks( );
728 s->OnRightDoubleClick( );
731 s->OnRightButtonDown( );
734 case vtkCommand::RightButtonReleaseEvent:
736 s->m_RightButtonEvent.Release( );
737 s->OnRightButtonUp( );
740 case vtkCommand::MouseWheelForwardEvent:
742 s->OnMouseWheelForward( );
745 case vtkCommand::MouseWheelBackwardEvent:
747 s->OnMouseWheelBackward( );
750 case vtkCommand::KeyPressEvent:
756 case vtkCommand::KeyReleaseEvent:
762 case vtkCommand::CharEvent:
767 case vtkCommand::ExposeEvent:
772 case vtkCommand::ConfigureEvent:
777 case vtkCommand::EnterEvent:
782 case vtkCommand::LeaveEvent:
787 case vtkCommand::TimerEvent:
792 case vtkCommand::DeleteEvent:
794 s->SetInteractor( 0 );
797 case vtkCommand::TDxMotionEvent:
798 case vtkCommand::TDxButtonPressEvent:
799 case vtkCommand::TDxButtonReleaseEvent:
801 s->DelegateTDxEvent( event, calldata );