]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Interaction/ImageSliceStyle.cxx
Start contour tracer widget debugging: change interactor style.
[cpPlugins.git] / lib / cpExtensions / Interaction / ImageSliceStyle.cxx
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>
11 /* TODO
12    #include <cpExtensions/Visualization/ImageViewerActors.h>
13 */
14
15 // -------------------------------------------------------------------------
16 cpExtensions::Interaction::ImageSliceStyle::
17 Self* cpExtensions::Interaction::ImageSliceStyle::
18 New( )
19 {
20   return( new Self( ) );
21 }
22
23 // -------------------------------------------------------------------------
24 void cpExtensions::Interaction::ImageSliceStyle::
25 OnMouseMove( )
26 {
27   static bool cursor_visible = false;
28
29   int idx[ 2 ];
30   double pnt[ 3 ];
31   if( this->_PickPointOnImageActor( idx, pnt ) )
32   {
33     this->m_Cursor->SetCursor( pnt, false );
34     if( !cursor_visible )
35     {
36       this->m_Cursor->VisibilityOn( );
37       cursor_visible = true;
38
39     } // fi
40     this->Interactor->Render( );
41   }
42   else
43   {
44     if( cursor_visible )
45     {
46       this->m_Cursor->VisibilityOff( );
47       cursor_visible = false;
48       this->Interactor->Render( );
49
50     } // fi
51
52   } // fi
53
54   this->Superclass::OnMouseMove( );
55 }
56
57 /* TODO
58 cpExtensions::Visualization::ImageViewerActors*
59 cpExtensions::Interaction::ImageSliceStyle::
60 GetActors( )
61 {
62   return( this->m_Actors );
63 }
64
65 // -------------------------------------------------------------------------
66 const cpExtensions::Visualization::ImageViewerActors*
67 cpExtensions::Interaction::ImageSliceStyle::
68 GetActors( ) const
69 {
70   return( this->m_Actors );
71 }
72
73 // -------------------------------------------------------------------------
74 void cpExtensions::Interaction::ImageSliceStyle::
75 SetActors( cpExtensions::Visualization::ImageViewerActors* actors )
76 {
77   this->m_Actors = actors;
78   if( actors != NULL )
79   {
80     this->m_PropPicker = vtkSmartPointer< vtkPropPicker >::New( );
81     this->m_PropPicker->PickFromListOn( );
82     this->m_PropPicker->GetPickList( )->RemoveAllItems( );
83     this->m_PropPicker->
84       AddPickList( this->m_Actors->GetWindowLevelImageActor( ) );
85   }
86   else
87     this->m_PropPicker = NULL;
88 }
89
90 // -------------------------------------------------------------------------
91 void cpExtensions::Interaction::ImageSliceStyle::
92 OnMouseMove( )
93 {
94   this->Superclass::OnMouseMove( );
95   if( this->m_Actors.GetPointer( ) == NULL )
96     return;
97
98   // Slice synch
99   static int idx[ 2 ];
100   static double pos[ 3 ];
101   if( this->_PickPosition( idx, pos ) )
102   {
103     if(
104       this->Interactor->GetControlKey( ) == 1 &&
105       this->GetButtonID( ) == Self::ButtonID_Left
106       )
107     {
108       this->m_Actors->SetSuperCursor( pos, this->m_Actors->GetOrientation( ) == 2 );
109       this->InvokeEvent( vtkCommand::UserEvent + 1, pos );
110     }
111     else
112       this->m_Actors->SetCursor( pos, this->m_Actors->GetOrientation( ) == 2 );
113   }
114   else
115     this->m_Actors->HideViewerActors( );
116   this->Interactor->Render( );
117 }
118
119 // -------------------------------------------------------------------------
120 void cpExtensions::Interaction::ImageSliceStyle::
121 OnMouseWheelForward( )
122 {
123   static int s = 0;
124   if( this->m_Actors.GetPointer( ) == NULL )
125     return;
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( );
133 }
134
135 // -------------------------------------------------------------------------
136 void cpExtensions::Interaction::ImageSliceStyle::
137 OnMouseWheelBackward( )
138 {
139   static int s = 0;
140   if( this->m_Actors.GetPointer( ) == NULL )
141     return;
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( );
149 }
150
151 // -------------------------------------------------------------------------
152 void cpExtensions::Interaction::ImageSliceStyle::
153 OnChar( )
154 {
155 }
156 */
157
158 // -------------------------------------------------------------------------
159 cpExtensions::Interaction::ImageSliceStyle::
160 ImageSliceStyle( )
161   : Superclass( )
162 {
163   this->SetInteractionModeToImage2D( );
164   this->m_PropPicker = vtkSmartPointer< vtkPropPicker >::New( );
165   this->m_PropPicker->PickFromListOn( );
166
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 );
171 }
172
173 // -------------------------------------------------------------------------
174 cpExtensions::Interaction::ImageSliceStyle::
175 ~ImageSliceStyle( )
176 {
177 }
178
179 // -------------------------------------------------------------------------
180 vtkProp* cpExtensions::Interaction::ImageSliceStyle::
181 _ImageActor( int i )
182 {
183   if( !this->CurrentRenderer )
184     return( NULL );
185
186   vtkPropCollection* props = this->CurrentRenderer->GetViewProps( );
187   vtkProp* prop = 0;
188   vtkAssemblyPath* path;
189   vtkImageSlice* imageProp = NULL;
190   vtkCollectionSimpleIterator pit;
191
192   for( int k = 0; k < 2; k++ )
193   {
194     int j = 0;
195     for( props->InitTraversal( pit ); ( prop = props->GetNextProp( pit ) ); )
196     {
197       bool foundImageProp = false;
198       for( prop->InitPathTraversal( ); ( path = prop->GetNextPath( ) ); )
199       {
200         vtkProp* tryProp = path->GetLastNode( )->GetViewProp( );
201         if( ( imageProp = vtkImageSlice::SafeDownCast( tryProp ) ) != 0 )
202         {
203           if( j == i )
204           {
205             foundImageProp = true;
206             break;
207
208           } // fi
209           imageProp = 0;
210           j++;
211
212         } // fi
213
214       } // rof
215       if( foundImageProp )
216         break;
217     } // rof
218     if( i < 0 )
219       i += j;
220
221   } // rof
222   return( imageProp );
223 }
224
225 // -------------------------------------------------------------------------
226 bool cpExtensions::Interaction::ImageSliceStyle::
227 _PickPointOnImageActor( int idx[ 2 ], double pnt[ 3 ] )
228 {
229   // Check if data is valid
230   if(
231     this->Interactor == NULL ||
232     this->CurrentRenderer == NULL ||
233     this->m_PropPicker.GetPointer( ) == NULL
234     )
235     return( false );
236
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 ] ) );
241
242   // Check if the image has changed
243   this->Interactor->GetPicker( )->
244     Pick( idx[ 0 ], idx[ 1 ], 0.0, this->CurrentRenderer );
245   auto picker =
246     vtkAbstractPropPicker::SafeDownCast( this->Interactor->GetPicker( ) );
247   if( picker == NULL )
248     return( false );
249   TWLActor* curr_actor = dynamic_cast< TWLActor* >( picker->GetProp3D( ) );
250   if( curr_actor != this->m_WLActor.GetPointer( ) && curr_actor != NULL )
251   {
252     this->m_WLActor = curr_actor;
253     this->m_PropPicker->GetPickList( )->RemoveAllItems( );
254     this->m_PropPicker->AddPickList( curr_actor );
255
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 );
261
262   } // fi
263   if( this->m_WLActor.GetPointer( ) == NULL )
264   {
265     this->m_PropPicker->GetPickList( )->RemoveAllItems( );
266     return( false );
267
268   } // fi
269
270   // Pick a 3D position
271   int r = this->m_PropPicker->Pick(
272     double( idx[ 0 ] ), double( idx[ 1 ] ), double( 0 ),
273     this->CurrentRenderer
274     );
275   if( r == 0 )
276     return( false );
277   this->m_PropPicker->GetPickPosition( pnt );
278   return( true );
279 }
280
281 // eof - $RCSfile$