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::
118 if( this->CursorMoving && this->m_MPRActors != NULL )
120 bool picked = this->_PickPosition( pos );
123 for( int i = 0; i < 3; ++i )
124 if( this->m_SliceActors->GetAxis( ) != i )
125 this->m_MPRActors->SetSlice( i, pos[ i ] );
126 this->Interactor->Render( );
127 this->_RenderAssociateInteractors( );
134 // -------------------------------------------------------------------------
135 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
138 int x = this->Interactor->GetEventPosition( )[ 0 ];
139 int y = this->Interactor->GetEventPosition( )[ 1 ];
141 this->FindPokedRenderer( x, y );
142 if( this->CurrentRenderer == NULL )
145 // Redefine this button to handle window/level
146 this->GrabFocus( this->EventCallbackCommand );
148 if( this->Interactor->GetControlKey( ) )
149 this->StartCursorMoving( );
152 if (!this->Interactor->GetShiftKey() && !this->Interactor->GetControlKey())
154 this->WindowLevelStartPosition[0] = x;
155 this->WindowLevelStartPosition[1] = y;
156 this->StartWindowLevel();
159 // If shift is held down, do a rotation
160 else if (this->InteractionMode == VTKIS_IMAGE3D &&
161 this->Interactor->GetShiftKey())
166 // If ctrl is held down in slicing mode, slice the image
167 else if (this->InteractionMode == VTKIS_IMAGE_SLICING &&
168 this->Interactor->GetControlKey())
173 // The rest of the button + key combinations remain the same
177 this->Superclass::OnLeftButtonDown();
182 // -------------------------------------------------------------------------
183 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
186 if( this->CursorMoving )
187 this->EndCursorMoving( );
190 // -------------------------------------------------------------------------
191 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
192 OnMiddleButtonDown( )
194 std::cout << "middown" << std::endl;
197 // -------------------------------------------------------------------------
198 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
201 std::cout << "midup" << std::endl;
204 // -------------------------------------------------------------------------
205 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
208 std::cout << "ridown" << std::endl;
211 // -------------------------------------------------------------------------
212 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
215 std::cout << "riup" << std::endl;
218 // -------------------------------------------------------------------------
219 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
220 OnMouseWheelForward( )
222 if( this->m_SliceActors == NULL || this->Interactor == NULL )
225 if( this->Interactor->GetShiftKey( ) == 1 )
227 int s = this->m_SliceActors->GetSliceNumber( ) + off;
228 int maxs = this->m_SliceActors->GetSliceNumberMaxValue( );
229 this->m_SliceActors->SetSliceNumber( ( s < maxs )? s: maxs );
230 this->Interactor->Render( );
231 this->_RenderAssociateInteractors( );
234 // -------------------------------------------------------------------------
235 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
236 OnMouseWheelBackward( )
238 if( this->m_SliceActors == NULL || this->Interactor == NULL )
241 if( this->Interactor->GetShiftKey( ) == 1 )
243 int s = this->m_SliceActors->GetSliceNumber( ) - off;
244 int mins = this->m_SliceActors->GetSliceNumberMinValue( );
245 this->m_SliceActors->SetSliceNumber( ( mins < s )? s: mins );
246 this->Interactor->Render( );
247 this->_RenderAssociateInteractors( );
250 // -------------------------------------------------------------------------
251 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
254 std::cout << "char" << std::endl;
257 // -------------------------------------------------------------------------
258 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
261 std::cout << "Rotate" << std::endl;
264 // -------------------------------------------------------------------------
265 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
268 std::cout << "pan" << std::endl;
271 // -------------------------------------------------------------------------
272 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
275 std::cout << "spin" << std::endl;
278 // -------------------------------------------------------------------------
279 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
282 std::cout << "Zoom" << std::endl;
285 // -------------------------------------------------------------------------
286 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
289 std::cout << "wl" << std::endl;
292 // -------------------------------------------------------------------------
293 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
296 std::cout << "Pick" << std::endl;
299 // -------------------------------------------------------------------------
300 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
303 std::cout << "Slice" << std::endl;
306 // -------------------------------------------------------------------------
307 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
310 std::cout << "swl" << std::endl;
313 // -------------------------------------------------------------------------
314 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
317 std::cout << "ewl" << std::endl;
320 // -------------------------------------------------------------------------
321 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
324 std::cout << "sp" << std::endl;
327 // -------------------------------------------------------------------------
328 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
331 std::cout << "ep" << std::endl;
334 // -------------------------------------------------------------------------
335 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
338 std::cout << "ss" << std::endl;
341 // -------------------------------------------------------------------------
342 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
345 std::cout << "es" << std::endl;
348 // -------------------------------------------------------------------------
349 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
352 if( this->CursorMoving )
354 this->CursorMoving = true;
357 // -------------------------------------------------------------------------
358 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
361 if( !( this->CursorMoving ) )
363 this->CursorMoving = false;
366 // -------------------------------------------------------------------------
367 cpPlugins::Extensions::Visualization::ImageInteractorStyle::
368 ImageInteractorStyle( )
370 Mode( Self::NavigationMode ),
371 m_SliceActors( NULL ),
373 CursorMoving( false )
376 vtkSmartPointer< vtkAnnotatedCubeActor > cube =
377 vtkSmartPointer< vtkAnnotatedCubeActor >::New( );
378 cube->GetCubeProperty( )->SetColor( 0.9, 0.7, 0.2 );
379 cube->GetTextEdgesProperty( )->SetLineWidth( 1 );
380 cube->GetTextEdgesProperty( )->SetDiffuse( 0 );
381 cube->GetTextEdgesProperty( )->SetAmbient( 1 );
382 cube->GetTextEdgesProperty( )->SetColor( 0.18, 0.28, 0.23 );
383 cube->GetXPlusFaceProperty( )->SetColor( 1, 0, 0 );
384 cube->GetXPlusFaceProperty( )->SetInterpolationToFlat( );
385 cube->GetXMinusFaceProperty( )->SetColor( 1, 0, 0 );
386 cube->GetXMinusFaceProperty( )->SetInterpolationToFlat( );
387 cube->GetYPlusFaceProperty( )->SetColor( 0, 1, 0 );
388 cube->GetYPlusFaceProperty( )->SetInterpolationToFlat( );
389 cube->GetYMinusFaceProperty( )->SetColor( 0, 1, 0 );
390 cube->GetYMinusFaceProperty( )->SetInterpolationToFlat( );
391 cube->GetZPlusFaceProperty( )->SetColor( 0, 0, 1 );
392 cube->GetZPlusFaceProperty( )->SetInterpolationToFlat( );
393 cube->GetZMinusFaceProperty( )->SetColor( 0, 0, 1 );
394 cube->GetZMinusFaceProperty( )->SetInterpolationToFlat( );
396 vtkSmartPointer< vtkAxesActor > axes =
397 vtkSmartPointer< vtkAxesActor >::New( );
398 axes->AxisLabelsOff( );
399 axes->SetShaftTypeToCylinder( );
400 axes->SetTotalLength( 2, 2, 2 );
402 vtkSmartPointer< vtkPropAssembly > actors =
403 vtkSmartPointer< vtkPropAssembly >::New( );
404 actors->AddPart( cube );
405 actors->AddPart( axes );
407 this->OrientationWidget =
408 vtkSmartPointer< vtkOrientationMarkerWidget >::New( );
409 this->OrientationWidget->SetOutlineColor( 0.93, 0.57, 0.13 );
410 this->OrientationWidget->SetOrientationMarker( actors );
411 this->OrientationWidget->SetViewport( 0.0, 0.0, 0.2, 0.2 );
413 this->PropPicker = vtkSmartPointer< vtkPropPicker >::New( );
414 this->PropPicker->PickFromListOn( );
417 // -------------------------------------------------------------------------
418 cpPlugins::Extensions::Visualization::ImageInteractorStyle::
419 ~ImageInteractorStyle( )
423 // -------------------------------------------------------------------------
424 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
425 _RenderAssociateInteractors( )
427 std::vector< vtkRenderWindowInteractor* >::iterator rIt =
428 this->AssociatedInteractors.begin( );
429 for( ; rIt != this->AssociatedInteractors.end( ); ++rIt )
433 // -------------------------------------------------------------------------
434 bool cpPlugins::Extensions::Visualization::ImageInteractorStyle::
435 _PickPosition( double pos[ 3 ] )
437 if( this->m_SliceActors == NULL )
440 double x = double( this->Interactor->GetEventPosition( )[ 0 ] );
441 double y = double( this->Interactor->GetEventPosition( )[ 1 ] );
442 this->FindPokedRenderer( x, y );
444 this->PropPicker->Pick( x, y, double( 0 ), this->CurrentRenderer );
447 this->PropPicker->GetPickPosition( pos );
451 // -------------------------------------------------------------------------
452 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
455 std::cout << "upcur" << std::endl;