1 #include "InteractorStyleImage.h"
2 #include "VolumeActors.h"
6 #include <vtkAbstractPropPicker.h>
7 #include <vtkAnnotatedCubeActor.h>
8 #include <vtkAxesActor.h>
10 #include <vtkCommand.h>
11 #include <vtkImageData.h>
12 #include <vtkPropAssembly.h>
13 #include <vtkProperty.h>
14 #include <vtkRendererCollection.h>
15 #include <vtkRenderWindow.h>
16 #include <vtkRenderWindowInteractor.h>
18 // -------------------------------------------------------------------------
19 int idms::InteractorStyleImage::SliceEvent = vtkCommand::UserEvent + 1;
21 // -------------------------------------------------------------------------
22 idms::InteractorStyleImage::
23 Self* idms::InteractorStyleImage::
26 return( new Self( ) );
29 // -------------------------------------------------------------------------
30 void idms::InteractorStyleImage::
31 Configure( VolumeActors* actors, const int& axis )
33 this->Actors = actors;
35 this->SetModeToNavigation( );
38 this->PropPicker->AddPickList( actors->GetXPlaneActor( ) );
40 this->PropPicker->AddPickList( actors->GetYPlaneActor( ) );
41 else // if( axis == 2 )
42 this->PropPicker->AddPickList( actors->GetZPlaneActor( ) );
47 // -------------------------------------------------------------------------
48 void idms::InteractorStyleImage::
49 SetModeToNavigation( )
51 this->Mode = Self::NavigationMode;
54 // -------------------------------------------------------------------------
55 void idms::InteractorStyleImage::
56 SetModeToDeformation( )
58 this->Mode = Self::DeformationMode;
61 // -------------------------------------------------------------------------
62 void idms::InteractorStyleImage::
63 SetInteractor( vtkRenderWindowInteractor* rwi )
65 this->Superclass::SetInteractor( rwi );
66 this->OrientationWidget->SetInteractor( rwi );
67 this->LineWdg->SetInteractor( rwi );
71 rwi->SetPicker( this->PropPicker );
73 // Enable 2D orientation widget
74 this->OrientationWidget->SetEnabled( 1 );
75 this->OrientationWidget->InteractiveOff( );
77 // Disable line widget
78 this->LineWdg->SetEnabled( 0 );
80 // Get camera, avoiding segfaults
82 rwi->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
85 vtkCamera* cam = ren->GetActiveCamera( );
89 // Parallel projections are better when displaying 2D images
90 cam->ParallelProjectionOn( );
91 cam->SetFocalPoint( double( 0 ), double( 0 ), double( 0 ) );
92 this->Plane->SetOrigin( double( 0 ), double( 0 ), double( 0 ) );
95 cam->SetPosition( double( 1 ), double( 0 ), double( 0 ) );
96 cam->SetViewUp ( double( 0 ), double( 1 ), double( 0 ) );
97 this->Plane->SetNormal( double( 1 ), double( 0 ), double( 0 ) );
99 else if( this->Axis == 1 )
101 cam->SetPosition( double( 0 ), double( 1 ), double( 0 ) );
102 cam->SetViewUp ( double( 0 ), double( 0 ), double( -1 ) );
103 this->Plane->SetNormal( double( 0 ), double( 1 ), double( 0 ) );
105 else // if( this->Axis == 2 )
107 cam->SetPosition( double( 0 ), double( 0 ), double( 1 ) );
108 cam->SetViewUp ( double( 0 ), double( 1 ), double( 0 ) );
109 this->Plane->SetNormal( double( 0 ), double( 0 ), double( 1 ) );
114 // -------------------------------------------------------------------------
115 void idms::InteractorStyleImage::
118 if( this->Mode == Self::DeformationMode )
119 this->_UpdateCursor( );
122 this->Superclass::OnMouseMove( );
125 // -------------------------------------------------------------------------
126 void idms::InteractorStyleImage::
129 if( this->Mode == Self::DeformationMode )
131 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
132 if( this->Actors != NULL && rwi != NULL )
134 if( rwi->GetShiftKey( ) == 1 )
136 this->Actors->GetRegionActor( )->GetProperty( )->SetOpacity( 0.3 );
137 this->UpdatingRegion = true;
140 this->Superclass::OnLeftButtonDown( );
143 this->Superclass::OnLeftButtonDown( );
145 else if( this->Mode == Self::NavigationMode )
146 this->Superclass::OnLeftButtonDown( );
149 // -------------------------------------------------------------------------
150 void idms::InteractorStyleImage::
153 if( this->Mode == Self::DeformationMode )
155 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
156 if( this->Actors != NULL && rwi != NULL )
158 if( rwi->GetShiftKey( ) == 1 )
160 this->Actors->GetRegionActor( )->GetProperty( )->SetOpacity( 0 );
161 this->UpdatingRegion = false;
164 this->Superclass::OnLeftButtonUp( );
167 this->Superclass::OnLeftButtonUp( );
169 else if( this->Mode == Self::NavigationMode )
170 this->Superclass::OnLeftButtonUp( );
173 // -------------------------------------------------------------------------
174 void idms::InteractorStyleImage::
175 OnMouseWheelForward( )
177 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
178 if( this->Actors == NULL || rwi == NULL )
181 if( rwi->GetShiftKey( ) == 1 )
183 int s = this->Actors->GetSlice( this->Axis ) + off;
184 int maxs = this->Actors->GetSliceNumberMaxValue( this->Axis );
185 this->Actors->SetSlice( this->Axis, ( s < maxs )? s: maxs );
186 this->Actors->Render( );
188 if( this->Mode == Self::DeformationMode )
189 this->_UpdateCursor( );
191 this->InvokeEvent( Self::SliceEvent, &( this->Axis ) );
194 // -------------------------------------------------------------------------
195 void idms::InteractorStyleImage::
196 OnMouseWheelBackward( )
198 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
199 if( this->Actors == NULL || rwi == NULL )
202 if( rwi->GetShiftKey( ) == 1 )
204 int s = this->Actors->GetSlice( this->Axis ) - off;
205 int mins = this->Actors->GetSliceNumberMinValue( this->Axis );
206 this->Actors->SetSlice( this->Axis, ( s < mins )? mins: s );
207 this->Actors->Render( );
209 if( this->Mode == Self::DeformationMode )
210 this->_UpdateCursor( );
212 this->InvokeEvent( Self::SliceEvent, &( this->Axis ) );
215 // -------------------------------------------------------------------------
216 void idms::InteractorStyleImage::
219 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
223 switch( rwi->GetKeyCode( ) )
228 rwi->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
232 this->Actors->GetImageBounds( bounds );
233 ren->ResetCamera( bounds );
241 this->Actors->ResetWindowLevel( );
242 this->Actors->Render( );
248 idx[ 0 ] = this->Actors->GetSlice( 0 );
249 idx[ 1 ] = this->Actors->GetSlice( 1 );
250 idx[ 2 ] = this->Actors->GetSlice( 2 );
251 this->Actors->AddSeed( idx );
259 // -------------------------------------------------------------------------
260 void idms::InteractorStyleImage::
263 if( this->Mode == Self::DeformationMode )
265 this->Actors->Render( );
267 else if( this->Mode == Self::NavigationMode )
268 this->Superclass::Pan( );
271 // -------------------------------------------------------------------------
272 void idms::InteractorStyleImage::
275 if( this->Mode == Self::NavigationMode )
278 if( this->_PickPosition( pos ) )
280 if( this->Axis != 0 ) this->Actors->SetSlice( 0, pos[ 0 ] );
281 if( this->Axis != 1 ) this->Actors->SetSlice( 1, pos[ 1 ] );
282 if( this->Axis != 2 ) this->Actors->SetSlice( 2, pos[ 2 ] );
283 this->Actors->Render( );
287 else if( this->Mode == Self::DeformationMode )
294 // -------------------------------------------------------------------------
295 void idms::InteractorStyleImage::
298 if( this->Mode == Self::NavigationMode )
300 if( this->State != VTKIS_NONE )
302 this->StartState( VTKIS_WINDOW_LEVEL );
304 this->WindowLevelInitial[ 0 ] = this->Actors->GetWindow( );
305 this->WindowLevelInitial[ 1 ] = this->Actors->GetLevel( );
307 else if( this->Mode == Self::DeformationMode )
314 // -------------------------------------------------------------------------
315 void idms::InteractorStyleImage::
318 if( this->Mode == Self::NavigationMode )
320 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
324 rwi->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
329 this->WindowLevelCurrentPosition[ 0 ] = rwi->GetEventPosition( )[ 0 ];
330 this->WindowLevelCurrentPosition[ 1 ] = rwi->GetEventPosition( )[ 1 ];
331 int* size = ren->GetSize( );
333 this->WindowLevelCurrentPosition[ 0 ] -
334 this->WindowLevelStartPosition[ 0 ]
335 ) / double( size[ 0 ] );
337 this->WindowLevelStartPosition[ 1 ] -
338 this->WindowLevelCurrentPosition[ 1 ]
339 ) / double( size[ 1 ] );
341 double w = this->WindowLevelInitial[ 0 ] * ( double( 1 ) + sw );
342 double l = this->WindowLevelInitial[ 1 ] * ( double( 1 ) + sl );
343 double minw = this->Actors->GetMinWindow( );
344 double maxw = this->Actors->GetMaxWindow( );
345 double minl = this->Actors->GetMinLevel( );
346 double maxl = this->Actors->GetMaxLevel( );
348 if( w < minw ) w = minw;
349 if( maxw < w ) w = maxw;
350 if( l < minl ) l = minl;
351 if( maxl < l ) l = maxl;
353 this->Actors->SetWindowLevel( w, l );
354 this->Actors->Render( );
356 else if( this->Mode == Self::DeformationMode )
363 // -------------------------------------------------------------------------
364 idms::InteractorStyleImage::
365 InteractorStyleImage( )
367 Mode( Self::NavigationMode ),
372 vtkSmartPointer< vtkAnnotatedCubeActor > cube =
373 vtkSmartPointer< vtkAnnotatedCubeActor >::New( );
374 cube->GetCubeProperty( )->SetColor( 0.9, 0.7, 0.2 );
375 cube->GetTextEdgesProperty( )->SetLineWidth( 1 );
376 cube->GetTextEdgesProperty( )->SetDiffuse( 0 );
377 cube->GetTextEdgesProperty( )->SetAmbient( 1 );
378 cube->GetTextEdgesProperty( )->SetColor( 0.18, 0.28, 0.23 );
379 cube->GetXPlusFaceProperty( )->SetColor( 1, 0, 0 );
380 cube->GetXPlusFaceProperty( )->SetInterpolationToFlat( );
381 cube->GetXMinusFaceProperty( )->SetColor( 1, 0, 0 );
382 cube->GetXMinusFaceProperty( )->SetInterpolationToFlat( );
383 cube->GetYPlusFaceProperty( )->SetColor( 0, 1, 0 );
384 cube->GetYPlusFaceProperty( )->SetInterpolationToFlat( );
385 cube->GetYMinusFaceProperty( )->SetColor( 0, 1, 0 );
386 cube->GetYMinusFaceProperty( )->SetInterpolationToFlat( );
387 cube->GetZPlusFaceProperty( )->SetColor( 0, 0, 1 );
388 cube->GetZPlusFaceProperty( )->SetInterpolationToFlat( );
389 cube->GetZMinusFaceProperty( )->SetColor( 0, 0, 1 );
390 cube->GetZMinusFaceProperty( )->SetInterpolationToFlat( );
392 vtkSmartPointer< vtkAxesActor > axes =
393 vtkSmartPointer< vtkAxesActor >::New( );
394 axes->AxisLabelsOff( );
395 axes->SetShaftTypeToCylinder( );
396 axes->SetTotalLength( 2, 2, 2 );
398 vtkSmartPointer< vtkPropAssembly > actors =
399 vtkSmartPointer< vtkPropAssembly >::New( );
400 actors->AddPart( cube );
401 actors->AddPart( axes );
403 this->OrientationWidget =
404 vtkSmartPointer< vtkOrientationMarkerWidget >::New( );
405 this->OrientationWidget->SetOutlineColor( 0.93, 0.57, 0.13 );
406 this->OrientationWidget->SetOrientationMarker( actors );
407 this->OrientationWidget->SetViewport( 0.0, 0.0, 0.2, 0.2 );
409 this->Plane = vtkSmartPointer< vtkPlane >::New( );
411 this->PropPicker = vtkSmartPointer< vtkPropPicker >::New( );
412 this->PropPicker->PickFromListOn( );
414 this->LineRep = vtkSmartPointer< vtkLineRepresentation >::New( );
415 this->LineWdg = vtkSmartPointer< vtkLineWidget2 >::New( );
417 this->LineWdg->SetRepresentation( this->LineRep );
420 // -------------------------------------------------------------------------
421 idms::InteractorStyleImage::
422 ~InteractorStyleImage( )
426 // -------------------------------------------------------------------------
427 bool idms::InteractorStyleImage::
428 _PickPosition( double pos[ 3 ] )
430 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
431 if( this->Actors == NULL || rwi == NULL )
433 this->FindPokedRenderer(
434 rwi->GetEventPosition( )[ 0 ],
435 rwi->GetEventPosition( )[ 1 ]
437 int success = rwi->GetPicker( )->Pick(
438 rwi->GetEventPosition( )[ 0 ],
439 rwi->GetEventPosition( )[ 1 ],
441 this->CurrentRenderer
447 vtkAbstractPropPicker* picker =
448 vtkAbstractPropPicker::SafeDownCast( rwi->GetPicker( ) );
452 this->PropPicker->GetPickPosition( pos );
461 // -------------------------------------------------------------------------
462 void idms::InteractorStyleImage::
466 if( this->_PickPosition( p ) )
468 this->Actors->SetCursorPosition( p );
469 if( this->UpdatingRegion )
470 this->Actors->SetRegionRadius( p );
472 this->Actors->SetRegionPosition( p );
473 this->Actors->RenderAuxiliaryInteractors( );
478 vtkImageData* img = this->Actors->GetImage( );
479 bool inside = ( img->ComputeStructuredCoordinates( p, i, c ) != 0 );