1 #include <cpPlugins/Extensions/Visualization/ImageInteractorStyle.h>
3 #include <vtkAnnotatedCubeActor.h>
4 #include <vtkAxesActor.h>
5 #include <vtkCallbackCommand.h>
7 #include <vtkCommand.h>
8 #include <vtkPropAssembly.h>
9 #include <vtkProperty.h>
10 #include <vtkRendererCollection.h>
11 #include <vtkRenderWindow.h>
12 #include <vtkRenderWindowInteractor.h>
14 #include <cpPlugins/Extensions/Visualization/ImageSliceActors.h>
15 #include <cpPlugins/Extensions/Visualization/MPRActors.h>
17 // -------------------------------------------------------------------------
18 const int cpPlugins::Extensions::Visualization::
19 ImageInteractorStyle::SliceEvent = vtkCommand::UserEvent + 1;
21 // -------------------------------------------------------------------------
22 cpPlugins::Extensions::Visualization::ImageInteractorStyle::
23 Self* cpPlugins::Extensions::Visualization::ImageInteractorStyle::
26 return( new Self( ) );
29 // -------------------------------------------------------------------------
30 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
31 Configure( ImageSliceActors* slice_actors, MPRActors* mpr_actors )
33 this->m_SliceActors = slice_actors;
34 this->m_MPRActors = mpr_actors;
35 this->SetModeToNavigation( );
36 this->PropPicker->AddPickList( slice_actors->GetImageActor( ) );
40 // -------------------------------------------------------------------------
41 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
42 AssociateInteractor( vtkRenderWindowInteractor* interactor )
44 if( interactor != NULL )
46 this->AssociatedInteractors.push_back( interactor );
52 // -------------------------------------------------------------------------
53 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
54 SetModeToNavigation( )
56 this->Mode = Self::NavigationMode;
59 // -------------------------------------------------------------------------
60 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
61 SetModeToDeformation( )
63 this->Mode = Self::DeformationMode;
66 // -------------------------------------------------------------------------
67 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
68 SetInteractor( vtkRenderWindowInteractor* interactor, const int& axis )
70 this->Superclass::SetInteractor( interactor );
71 this->OrientationWidget->SetInteractor( interactor );
72 interactor->SetInteractorStyle( this );
73 if( interactor == NULL )
76 // TODO: interactor->SetPicker( this->PropPicker );
78 // Get camera, avoiding segfaults
80 interactor->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
83 vtkCamera* cam = ren->GetActiveCamera( );
87 // Parallel projections are better when displaying 2D images
88 cam->ParallelProjectionOn( );
89 cam->SetFocalPoint( double( 0 ), double( 0 ), double( 0 ) );
92 cam->SetPosition( double( 1 ), double( 0 ), double( 0 ) );
93 cam->SetViewUp ( double( 0 ), double( 1 ), double( 0 ) );
97 cam->SetPosition( double( 0 ), double( 1 ), double( 0 ) );
98 cam->SetViewUp ( double( 0 ), double( 0 ), double( -1 ) );
100 else // if( axis == 2 )
102 cam->SetPosition( double( 0 ), double( 0 ), double( 1 ) );
103 cam->SetViewUp ( double( 0 ), double( 1 ), double( 0 ) );
108 // Enable 2D orientation widget
109 this->OrientationWidget->SetEnabled( 1 );
110 this->OrientationWidget->InteractiveOff( );
113 // -------------------------------------------------------------------------
114 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
117 if( this->m_MPRActors == NULL )
121 if( this->CursorMoving )
123 bool picked = this->_PickPosition( pos );
126 for( int i = 0; i < 3; ++i )
127 if( this->m_SliceActors->GetAxis( ) != i )
128 this->m_MPRActors->SetSlice( i, pos[ i ] );
129 this->Interactor->Render( );
130 this->_RenderAssociateInteractors( );
136 switch( this->State )
138 case VTKIS_WINDOW_LEVEL:
139 this->WindowLevel( );
152 // -------------------------------------------------------------------------
153 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
156 int x = this->Interactor->GetEventPosition( )[ 0 ];
157 int y = this->Interactor->GetEventPosition( )[ 1 ];
159 this->FindPokedRenderer( x, y );
160 if( this->CurrentRenderer == NULL )
162 this->GrabFocus( this->EventCallbackCommand );
164 if( this->Interactor->GetControlKey( ) )
165 this->StartCursorMoving( );
168 if (!this->Interactor->GetShiftKey() && !this->Interactor->GetControlKey())
170 this->WindowLevelStartPosition[0] = x;
171 this->WindowLevelStartPosition[1] = y;
172 this->StartWindowLevel();
175 // If shift is held down, do a rotation
176 else if (this->InteractionMode == VTKIS_IMAGE3D &&
177 this->Interactor->GetShiftKey())
182 // If ctrl is held down in slicing mode, slice the image
183 else if (this->InteractionMode == VTKIS_IMAGE_SLICING &&
184 this->Interactor->GetControlKey())
189 // The rest of the button + key combinations remain the same
193 this->Superclass::OnLeftButtonDown();
198 // -------------------------------------------------------------------------
199 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
202 if( this->CursorMoving )
204 this->EndCursorMoving( );
205 if( this->Interactor )
206 this->ReleaseFocus( );
211 // -------------------------------------------------------------------------
212 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
213 OnMiddleButtonDown( )
215 int x = this->Interactor->GetEventPosition( )[ 0 ];
216 int y = this->Interactor->GetEventPosition( )[ 1 ];
218 this->FindPokedRenderer( x, y );
219 if( this->CurrentRenderer == NULL )
221 this->GrabFocus( this->EventCallbackCommand );
223 if( this->Interactor->GetAltKey( ) )
226 else if( this->Interactor->GetControlKey( ) )
229 else if( this->Interactor->GetShiftKey( ) )
236 // -------------------------------------------------------------------------
237 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
240 switch( this->State )
248 // -------------------------------------------------------------------------
249 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
252 int x = this->Interactor->GetEventPosition( )[ 0 ];
253 int y = this->Interactor->GetEventPosition( )[ 1 ];
255 this->FindPokedRenderer( x, y );
256 if( this->CurrentRenderer == NULL )
258 this->GrabFocus( this->EventCallbackCommand );
260 if( this->Interactor->GetControlKey( ) )
262 this->WindowLevelStartPosition[ 0 ] = x;
263 this->WindowLevelStartPosition[ 1 ] = y;
264 this->StartWindowLevel( );
272 // -------------------------------------------------------------------------
273 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
276 switch( this->State )
278 case VTKIS_WINDOW_LEVEL:
280 this->EndWindowLevel( );
281 if( this->Interactor )
282 this->ReleaseFocus( );
291 // -------------------------------------------------------------------------
292 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
293 OnMouseWheelForward( )
295 if( this->m_SliceActors == NULL || this->Interactor == NULL )
298 if( this->Interactor->GetShiftKey( ) == 1 )
300 int s = this->m_SliceActors->GetSliceNumber( ) + off;
301 int maxs = this->m_SliceActors->GetSliceNumberMaxValue( );
302 this->m_SliceActors->SetSliceNumber( ( s < maxs )? s: maxs );
303 this->Interactor->Render( );
304 this->_RenderAssociateInteractors( );
307 // -------------------------------------------------------------------------
308 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
309 OnMouseWheelBackward( )
311 if( this->m_SliceActors == NULL || this->Interactor == NULL )
314 if( this->Interactor->GetShiftKey( ) == 1 )
316 int s = this->m_SliceActors->GetSliceNumber( ) - off;
317 int mins = this->m_SliceActors->GetSliceNumberMinValue( );
318 this->m_SliceActors->SetSliceNumber( ( mins < s )? s: mins );
319 this->Interactor->Render( );
320 this->_RenderAssociateInteractors( );
323 // -------------------------------------------------------------------------
324 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
327 switch( this->Interactor->GetKeyCode( ) )
332 this->Interactor->GetRenderWindow( )->
333 GetRenderers( )->GetFirstRenderer( );
336 this->Interactor->Render( );
339 case 'w': case 'W': case 'l': case 'L':
341 if( this->m_MPRActors != NULL )
343 this->m_MPRActors->ResetWindowLevel( );
344 this->Interactor->Render( );
345 this->_RenderAssociateInteractors( );
353 // -------------------------------------------------------------------------
354 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
357 if( this->Mode == Self::NavigationMode )
359 if( this->Interactor == NULL )
362 this->Interactor->GetRenderWindow( )->
363 GetRenderers( )->GetFirstRenderer( );
368 this->WindowLevelCurrentPosition[ 0 ] =
369 this->Interactor->GetEventPosition( )[ 0 ];
370 this->WindowLevelCurrentPosition[ 1 ] =
371 this->Interactor->GetEventPosition( )[ 1 ];
372 int* size = ren->GetSize( );
374 this->WindowLevelCurrentPosition[ 0 ] -
375 this->WindowLevelStartPosition[ 0 ]
376 ) / double( size[ 0 ] );
378 this->WindowLevelStartPosition[ 1 ] -
379 this->WindowLevelCurrentPosition[ 1 ]
380 ) / double( size[ 1 ] );
382 double w = this->WindowLevelInitial[ 0 ] * ( double( 1 ) + sw );
383 double l = this->WindowLevelInitial[ 1 ] * ( double( 1 ) + sl );
384 double minw = this->m_MPRActors->GetMinWindow( );
385 double maxw = this->m_MPRActors->GetMaxWindow( );
386 double minl = this->m_MPRActors->GetMinLevel( );
387 double maxl = this->m_MPRActors->GetMaxLevel( );
389 if( w < minw ) w = minw;
390 if( maxw < w ) w = maxw;
391 if( l < minl ) l = minl;
392 if( maxl < l ) l = maxl;
394 this->m_MPRActors->SetWindowLevel( w, l );
395 this->Interactor->Render( );
396 this->_RenderAssociateInteractors( );
398 else if( this->Mode == Self::DeformationMode )
405 // -------------------------------------------------------------------------
406 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
409 if( this->State != VTKIS_NONE )
411 if( this->Mode == Self::NavigationMode )
413 this->StartState( VTKIS_WINDOW_LEVEL );
415 this->WindowLevelInitial[ 0 ] = this->m_MPRActors->GetWindow( );
416 this->WindowLevelInitial[ 1 ] = this->m_MPRActors->GetLevel( );
418 else if( this->Mode == Self::DeformationMode )
425 // -------------------------------------------------------------------------
426 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
429 if( this->Mode == Self::NavigationMode )
431 if( this->State != VTKIS_WINDOW_LEVEL )
442 // -------------------------------------------------------------------------
443 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
446 if( this->CursorMoving )
448 this->CursorMoving = true;
451 // -------------------------------------------------------------------------
452 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
455 if( !( this->CursorMoving ) )
457 this->CursorMoving = false;
460 // -------------------------------------------------------------------------
461 cpPlugins::Extensions::Visualization::ImageInteractorStyle::
462 ImageInteractorStyle( )
464 Mode( Self::NavigationMode ),
465 m_SliceActors( NULL ),
467 CursorMoving( false )
470 vtkSmartPointer< vtkAnnotatedCubeActor > cube =
471 vtkSmartPointer< vtkAnnotatedCubeActor >::New( );
472 cube->GetCubeProperty( )->SetColor( 0.9, 0.7, 0.2 );
473 cube->GetTextEdgesProperty( )->SetLineWidth( 1 );
474 cube->GetTextEdgesProperty( )->SetDiffuse( 0 );
475 cube->GetTextEdgesProperty( )->SetAmbient( 1 );
476 cube->GetTextEdgesProperty( )->SetColor( 0.18, 0.28, 0.23 );
477 cube->GetXPlusFaceProperty( )->SetColor( 1, 0, 0 );
478 cube->GetXPlusFaceProperty( )->SetInterpolationToFlat( );
479 cube->GetXMinusFaceProperty( )->SetColor( 1, 0, 0 );
480 cube->GetXMinusFaceProperty( )->SetInterpolationToFlat( );
481 cube->GetYPlusFaceProperty( )->SetColor( 0, 1, 0 );
482 cube->GetYPlusFaceProperty( )->SetInterpolationToFlat( );
483 cube->GetYMinusFaceProperty( )->SetColor( 0, 1, 0 );
484 cube->GetYMinusFaceProperty( )->SetInterpolationToFlat( );
485 cube->GetZPlusFaceProperty( )->SetColor( 0, 0, 1 );
486 cube->GetZPlusFaceProperty( )->SetInterpolationToFlat( );
487 cube->GetZMinusFaceProperty( )->SetColor( 0, 0, 1 );
488 cube->GetZMinusFaceProperty( )->SetInterpolationToFlat( );
490 vtkSmartPointer< vtkAxesActor > axes =
491 vtkSmartPointer< vtkAxesActor >::New( );
492 axes->AxisLabelsOff( );
493 axes->SetShaftTypeToCylinder( );
494 axes->SetTotalLength( 2, 2, 2 );
496 vtkSmartPointer< vtkPropAssembly > actors =
497 vtkSmartPointer< vtkPropAssembly >::New( );
498 actors->AddPart( cube );
499 actors->AddPart( axes );
501 this->OrientationWidget =
502 vtkSmartPointer< vtkOrientationMarkerWidget >::New( );
503 this->OrientationWidget->SetOutlineColor( 0.93, 0.57, 0.13 );
504 this->OrientationWidget->SetOrientationMarker( actors );
505 this->OrientationWidget->SetViewport( 0.0, 0.0, 0.2, 0.2 );
507 this->PropPicker = vtkSmartPointer< vtkPropPicker >::New( );
508 this->PropPicker->PickFromListOn( );
511 // -------------------------------------------------------------------------
512 cpPlugins::Extensions::Visualization::ImageInteractorStyle::
513 ~ImageInteractorStyle( )
517 // -------------------------------------------------------------------------
518 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
519 _RenderAssociateInteractors( )
521 std::vector< vtkRenderWindowInteractor* >::iterator rIt =
522 this->AssociatedInteractors.begin( );
523 for( ; rIt != this->AssociatedInteractors.end( ); ++rIt )
527 // -------------------------------------------------------------------------
528 bool cpPlugins::Extensions::Visualization::ImageInteractorStyle::
529 _PickPosition( double pos[ 3 ] )
531 if( this->m_SliceActors == NULL )
534 double x = double( this->Interactor->GetEventPosition( )[ 0 ] );
535 double y = double( this->Interactor->GetEventPosition( )[ 1 ] );
536 this->FindPokedRenderer( x, y );
538 this->PropPicker->Pick( x, y, double( 0 ), this->CurrentRenderer );
541 this->PropPicker->GetPickPosition( pos );
545 // -------------------------------------------------------------------------
546 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
549 std::cout << "upcur" << std::endl;