1 #include <cpExtensions/Interaction/ImageSliceStyle.h>
2 #include <cpExtensions/Visualization/CursorActors.h>
3 #include <cpExtensions/Visualization/WindowLevelImageActor.h>
4 #include <vtkAssemblyPath.h>
5 #include <vtkImageData.h>
6 #include <vtkImageSlice.h>
7 #include <vtkProperty.h>
8 #include <vtkPropCollection.h>
9 #include <vtkPropPicker.h>
10 #include <vtkRenderer.h>
12 #include <cpExtensions/Visualization/ImageViewerActors.h>
15 // -------------------------------------------------------------------------
16 cpExtensions::Interaction::ImageSliceStyle::
17 Self* cpExtensions::Interaction::ImageSliceStyle::
20 return( new Self( ) );
23 // -------------------------------------------------------------------------
24 void cpExtensions::Interaction::ImageSliceStyle::
27 static bool cursor_visible = false;
31 if( this->_PickPointOnImageActor( idx, pnt ) )
33 this->m_Cursor->SetCursor( pnt, false );
36 this->m_Cursor->VisibilityOn( );
37 cursor_visible = true;
40 this->Interactor->Render( );
46 this->m_Cursor->VisibilityOff( );
47 cursor_visible = false;
48 this->Interactor->Render( );
54 this->Superclass::OnMouseMove( );
58 cpExtensions::Visualization::ImageViewerActors*
59 cpExtensions::Interaction::ImageSliceStyle::
62 return( this->m_Actors );
65 // -------------------------------------------------------------------------
66 const cpExtensions::Visualization::ImageViewerActors*
67 cpExtensions::Interaction::ImageSliceStyle::
70 return( this->m_Actors );
73 // -------------------------------------------------------------------------
74 void cpExtensions::Interaction::ImageSliceStyle::
75 SetActors( cpExtensions::Visualization::ImageViewerActors* actors )
77 this->m_Actors = actors;
80 this->m_PropPicker = vtkSmartPointer< vtkPropPicker >::New( );
81 this->m_PropPicker->PickFromListOn( );
82 this->m_PropPicker->GetPickList( )->RemoveAllItems( );
84 AddPickList( this->m_Actors->GetWindowLevelImageActor( ) );
87 this->m_PropPicker = NULL;
90 // -------------------------------------------------------------------------
91 void cpExtensions::Interaction::ImageSliceStyle::
94 this->Superclass::OnMouseMove( );
95 if( this->m_Actors.GetPointer( ) == NULL )
100 static double pos[ 3 ];
101 if( this->_PickPosition( idx, pos ) )
104 this->Interactor->GetControlKey( ) == 1 &&
105 this->GetButtonID( ) == Self::ButtonID_Left
108 this->m_Actors->SetSuperCursor( pos, this->m_Actors->GetOrientation( ) == 2 );
109 this->InvokeEvent( vtkCommand::UserEvent + 1, pos );
112 this->m_Actors->SetCursor( pos, this->m_Actors->GetOrientation( ) == 2 );
115 this->m_Actors->HideViewerActors( );
116 this->Interactor->Render( );
119 // -------------------------------------------------------------------------
120 void cpExtensions::Interaction::ImageSliceStyle::
121 OnMouseWheelForward( )
124 if( this->m_Actors.GetPointer( ) == NULL )
126 s = this->m_Actors->GetSliceNumber( );
127 s += ( this->Interactor->GetShiftKey( ) == 1 )? 10: 1;
128 this->m_Actors->SetSliceNumber( s );
129 s = this->m_Actors->GetSliceNumber( );
130 this->InvokeEvent( vtkCommand::UserEvent + 2, &s );
131 this->Interactor->Render( );
132 this->OnMouseMove( );
135 // -------------------------------------------------------------------------
136 void cpExtensions::Interaction::ImageSliceStyle::
137 OnMouseWheelBackward( )
140 if( this->m_Actors.GetPointer( ) == NULL )
142 s = this->m_Actors->GetSliceNumber( );
143 s -= ( this->Interactor->GetShiftKey( ) == 1 )? 10: 1;
144 this->m_Actors->SetSliceNumber( s );
145 s = this->m_Actors->GetSliceNumber( );
146 this->InvokeEvent( vtkCommand::UserEvent + 2, &s );
147 this->Interactor->Render( );
148 this->OnMouseMove( );
151 // -------------------------------------------------------------------------
152 void cpExtensions::Interaction::ImageSliceStyle::
158 // -------------------------------------------------------------------------
159 cpExtensions::Interaction::ImageSliceStyle::
163 this->SetInteractionModeToImage2D( );
164 this->m_PropPicker = vtkSmartPointer< vtkPropPicker >::New( );
165 this->m_PropPicker->PickFromListOn( );
167 this->m_Cursor = vtkSmartPointer< TCursor >::New( );
168 this->m_Cursor->VisibilityOff( );
169 this->m_Cursor->GetProperty( 0 )->SetColor( 1, 1, 0 );
170 this->m_Cursor->GetProperty( 1 )->SetColor( 1, 1, 0 );
173 // -------------------------------------------------------------------------
174 cpExtensions::Interaction::ImageSliceStyle::
179 // -------------------------------------------------------------------------
180 vtkProp* cpExtensions::Interaction::ImageSliceStyle::
183 if( !this->CurrentRenderer )
186 vtkPropCollection* props = this->CurrentRenderer->GetViewProps( );
188 vtkAssemblyPath* path;
189 vtkImageSlice* imageProp = NULL;
190 vtkCollectionSimpleIterator pit;
192 for( int k = 0; k < 2; k++ )
195 for( props->InitTraversal( pit ); ( prop = props->GetNextProp( pit ) ); )
197 bool foundImageProp = false;
198 for( prop->InitPathTraversal( ); ( path = prop->GetNextPath( ) ); )
200 vtkProp* tryProp = path->GetLastNode( )->GetViewProp( );
201 if( ( imageProp = vtkImageSlice::SafeDownCast( tryProp ) ) != 0 )
205 foundImageProp = true;
225 // -------------------------------------------------------------------------
226 bool cpExtensions::Interaction::ImageSliceStyle::
227 _PickPointOnImageActor( int idx[ 2 ], double pnt[ 3 ] )
229 // Check if data is valid
231 this->Interactor == NULL ||
232 this->CurrentRenderer == NULL ||
233 this->m_PropPicker.GetPointer( ) == NULL
237 // Find the renderer where the event has been raised
238 idx[ 0 ] = this->Interactor->GetEventPosition( )[ 0 ];
239 idx[ 1 ] = this->Interactor->GetEventPosition( )[ 1 ];
240 this->FindPokedRenderer( double( idx[ 0 ] ), double( idx[ 1 ] ) );
242 // Check if the image has changed
243 this->Interactor->GetPicker( )->
244 Pick( idx[ 0 ], idx[ 1 ], 0.0, this->CurrentRenderer );
246 vtkAbstractPropPicker::SafeDownCast( this->Interactor->GetPicker( ) );
249 TWLActor* curr_actor = dynamic_cast< TWLActor* >( picker->GetProp3D( ) );
250 if( curr_actor != this->m_WLActor.GetPointer( ) && curr_actor != NULL )
252 this->m_WLActor = curr_actor;
253 this->m_PropPicker->GetPickList( )->RemoveAllItems( );
254 this->m_PropPicker->AddPickList( curr_actor );
256 this->m_Cursor->SetImageBounds( curr_actor->GetImage( )->GetBounds( ) );
257 this->m_Cursor->SetImageOrientation( curr_actor->GetOrientation( ) );
258 this->m_Cursor->InitTraversal( );
259 while( vtkProp* prop = this->m_Cursor->GetNextProp( ) )
260 this->CurrentRenderer->AddViewProp( prop );
263 if( this->m_WLActor.GetPointer( ) == NULL )
265 this->m_PropPicker->GetPickList( )->RemoveAllItems( );
270 // Pick a 3D position
271 int r = this->m_PropPicker->Pick(
272 double( idx[ 0 ] ), double( idx[ 1 ] ), double( 0 ),
273 this->CurrentRenderer
277 this->m_PropPicker->GetPickPosition( pnt );