]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Visualization/MPRActors.cxx
Double click widget integrated with segmentation and deformation filters.
[cpPlugins.git] / lib / cpExtensions / Visualization / MPRActors.cxx
1 #include <cpExtensions/Visualization/MPRActors.h>
2
3 #include <vtkAlgorithmOutput.h>
4 #include <vtkImageData.h>
5 #include <vtkOutlineSource.h>
6 #include <vtkProperty.h>
7 #include <vtkRenderer.h>
8 #include <vtkRendererCollection.h>
9 #include <vtkRenderWindow.h>
10
11 // -------------------------------------------------------------------------
12 cpExtensions::Visualization::MPRActors*
13 cpExtensions::Visualization::MPRActors::
14 New( )
15 {
16   return( new Self( ) );
17 }
18
19 // -------------------------------------------------------------------------
20 cpExtensions::Visualization::
21 ImageSliceActors* cpExtensions::Visualization::MPRActors::
22 GetSliceActors( const int& i ) const
23 {
24   if( i < 3 )
25     return( this->Slices[ 0 ][ i ] );
26   else
27     return( NULL );
28 }
29
30 // -------------------------------------------------------------------------
31 int cpExtensions::Visualization::MPRActors::
32 AddInputConnection( vtkAlgorithmOutput* aout )
33 {
34   ImageBlender* blender = this->Slices[ 0 ][ 0 ]->GetBlender( );
35   if( blender == NULL )
36   {
37     this->Slices[ 0 ][ 0 ]->AddInputConnection( aout );
38     blender = this->Slices[ 0 ][ 0 ]->GetBlender( );
39     for( unsigned int i = 0; i < 2; ++i )
40     {
41       for( unsigned int j = 0; j < 3; ++j )
42       {
43         if( i != 0 || j != 0 )
44           this->Slices[ i ][ j ]->SetBlender( blender );
45         this->Slices[ i ][ j ]->SetAxis( j );
46
47       } // rof
48
49     } // rof
50     this->_CreateBoundingBox( );
51   }
52   else
53     blender->AddInputConnection( aout );
54   return( blender->GetNumberOfImages( ) - 1 );
55 }
56
57 // -------------------------------------------------------------------------
58 int cpExtensions::Visualization::MPRActors::
59 AddInputData( vtkImageData* new_image )
60 {
61   ImageBlender* blender = this->Slices[ 0 ][ 0 ]->GetBlender( );
62   if( blender == NULL )
63   {
64     this->Slices[ 0 ][ 0 ]->AddInputData( new_image );
65     blender = this->Slices[ 0 ][ 0 ]->GetBlender( );
66     for( unsigned int i = 0; i < 2; ++i )
67     {
68       for( unsigned int j = 0; j < 3; ++j )
69       {
70         if( i != 0 || j != 0 )
71           this->Slices[ i ][ j ]->SetBlender( blender );
72         this->Slices[ i ][ j ]->SetAxis( j );
73
74       } // rof
75
76     } // rof
77     this->_CreateBoundingBox( );
78   }
79   else
80     blender->AddInputData( new_image );
81   return( blender->GetNumberOfImages( ) - 1 );
82 }
83
84 // -------------------------------------------------------------------------
85 unsigned int cpExtensions::Visualization::MPRActors::
86 GetNumberOfImages( ) const
87 {
88   ImageBlender* blender = this->Slices[ 0 ][ 0 ]->GetBlender( );
89   if( blender != NULL )
90     return( blender->GetNumberOfImages( ) );
91   else
92     return( 0 );
93 }
94
95 // -------------------------------------------------------------------------
96 void cpExtensions::Visualization::MPRActors::
97 PushActorsInto(
98   vtkRenderWindow* x, vtkRenderWindow* y, vtkRenderWindow* z,
99   vtkRenderWindow* w
100   )
101 {
102   this->Slices[ 0 ][ 0 ]->PushActorsInto( x, true );
103   this->Slices[ 0 ][ 1 ]->PushActorsInto( y, true );
104   this->Slices[ 0 ][ 2 ]->PushActorsInto( z, true );
105   this->Slices[ 1 ][ 0 ]->PushActorsInto( w, false );
106   this->Slices[ 1 ][ 1 ]->PushActorsInto( w, false );
107   this->Slices[ 1 ][ 2 ]->PushActorsInto( w, false );
108
109   vtkRenderer* xren =
110     ( x != NULL )? x->GetRenderers( )->GetFirstRenderer( ): NULL;
111   vtkRenderer* yren =
112     ( y != NULL )? y->GetRenderers( )->GetFirstRenderer( ): NULL;
113   vtkRenderer* zren =
114     ( z != NULL )? z->GetRenderers( )->GetFirstRenderer( ): NULL;
115   vtkRenderer* wren =
116     ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
117
118   if( xren != NULL )
119   {
120     xren->AddActor( this->Slices[ 0 ][ 1 ]->GetPlaneActor( ) );
121     xren->AddActor( this->Slices[ 0 ][ 2 ]->GetPlaneActor( ) );
122
123   } // fi
124   if( yren != NULL )
125   {
126     yren->AddActor( this->Slices[ 0 ][ 0 ]->GetPlaneActor( ) );
127     yren->AddActor( this->Slices[ 0 ][ 2 ]->GetPlaneActor( ) );
128
129   } // fi
130   if( zren != NULL )
131   {
132     zren->AddActor( this->Slices[ 0 ][ 0 ]->GetPlaneActor( ) );
133     zren->AddActor( this->Slices[ 0 ][ 1 ]->GetPlaneActor( ) );
134
135   } // fi
136   if( wren != NULL )
137     wren->AddActor( this->ImageOutlineActor );
138 }
139
140 // -------------------------------------------------------------------------
141 void cpExtensions::Visualization::MPRActors::
142 PopActorsFrom(
143   vtkRenderWindow* x, vtkRenderWindow* y, vtkRenderWindow* z,
144   vtkRenderWindow* w
145   )
146 {
147   this->Slices[ 0 ][ 0 ]->PopActorsFrom( x );
148   this->Slices[ 0 ][ 1 ]->PopActorsFrom( y );
149   this->Slices[ 0 ][ 2 ]->PopActorsFrom( z );
150   this->Slices[ 1 ][ 0 ]->PopActorsFrom( w );
151   this->Slices[ 1 ][ 1 ]->PopActorsFrom( w );
152   this->Slices[ 1 ][ 2 ]->PopActorsFrom( w );
153
154   vtkRenderer* wren =
155     ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
156   if( wren != NULL )
157     wren->RemoveActor( this->ImageOutlineActor );
158
159   this->Slices[ 0 ][ 0 ]->Clear( );
160   this->Slices[ 0 ][ 1 ]->Clear( );
161   this->Slices[ 0 ][ 2 ]->Clear( );
162   this->Slices[ 1 ][ 0 ]->Clear( );
163   this->Slices[ 1 ][ 1 ]->Clear( );
164   this->Slices[ 1 ][ 2 ]->Clear( );
165 }
166
167 // -------------------------------------------------------------------------
168 double cpExtensions::Visualization::MPRActors::
169 GetWindow( )
170 {
171   return( this->Slices[ 0 ][ 0 ]->GetWindow( ) );
172 }
173
174 // -------------------------------------------------------------------------
175 double cpExtensions::Visualization::MPRActors::
176 GetLevel( )
177 {
178   return( this->Slices[ 0 ][ 0 ]->GetLevel( ) );
179 }
180
181 // -------------------------------------------------------------------------
182 int cpExtensions::Visualization::MPRActors::
183 GetSliceNumberMinValue( const int& axis ) const
184 {
185   return( this->Slices[ 0 ][ axis ]->GetSliceNumberMinValue( ) );
186 }
187
188 // -------------------------------------------------------------------------
189 int cpExtensions::Visualization::MPRActors::
190 GetSliceNumberMaxValue( const int& axis ) const
191 {
192   return( this->Slices[ 0 ][ axis ]->GetSliceNumberMaxValue( ) );
193 }
194
195 // -------------------------------------------------------------------------
196 int cpExtensions::Visualization::MPRActors::
197 GetSlice( const int& axis ) const
198 {
199   return( this->Slices[ 0 ][ axis ]->GetSliceNumber( ) );
200 }
201
202 // -------------------------------------------------------------------------
203 void cpExtensions::Visualization::MPRActors::
204 SetSlice( const int& axis, const int& slice )
205 {
206   ImageBlender* blender = this->Slices[ 0 ][ 0 ]->GetBlender( );
207   if( blender == NULL )
208     return;
209   vtkImageData* image =
210     dynamic_cast< vtkImageData* >( blender->GetInput( ) );
211   if( image == NULL )
212     return;
213
214   // Get image data extent
215   int ext[ 6 ];
216   image->GetExtent( ext );
217
218   // Check if the slice is valid
219   int real = slice;
220   if( slice < ext[ axis << 1 ] )
221     real = ext[ axis << 1 ];
222   if( ext[ ( axis << 1 ) + 1 ] < slice )
223     real = ext[ ( axis << 1 ) + 1 ];
224
225   // Change slice
226   this->Slices[ 0 ][ axis ]->SetSliceNumber( real );
227   this->Slices[ 1 ][ axis ]->SetSliceNumber( real );
228   this->Modified( );
229 }
230
231 // -------------------------------------------------------------------------
232 void cpExtensions::Visualization::MPRActors::
233 SetSlice( const int& axis, const double& slice )
234 {
235   ImageBlender* blender = this->Slices[ 0 ][ 0 ]->GetBlender( );
236   if( blender == NULL )
237     return;
238   vtkImageData* image =
239     dynamic_cast< vtkImageData* >( blender->GetInput( ) );
240   if( image == NULL )
241     return;
242
243   double x[ 3 ] = { double( 0 ) };
244   double pcoords[ 3 ];
245   int ijk[ 3 ];
246
247   x[ axis ] = slice;
248   image->ComputeStructuredCoordinates( x, ijk, pcoords );
249   this->SetSlice( axis, ijk[ axis ] );
250 }
251
252 // -------------------------------------------------------------------------
253 void cpExtensions::Visualization::MPRActors::
254 ResetSlices( )
255 {
256   for( unsigned int i = 0; i < 2; ++i )
257     for( unsigned int j = 0; j < 3; ++j )
258       this->Slices[ i ][ j ]->SetSliceNumber(
259         this->Slices[ i ][ j ]->GetSliceNumberMinValue( )
260         );
261 }
262
263 // -------------------------------------------------------------------------
264 cpExtensions::Visualization::MPRActors::
265 MPRActors( )
266   : Superclass( )
267 {
268   this->ImageOutlineActor = vtkSmartPointer< vtkActor >::New( );
269   for( unsigned int i = 0; i < 2; ++i )
270     for( unsigned int j = 0; j < 3; ++j )
271       this->Slices[ i ][ j ] = vtkSmartPointer< ImageSliceActors >::New( );
272
273   this->Slices[ 0 ][ 0 ]->AssociateSlice( this->Slices[ 1 ][ 0 ] );
274   this->Slices[ 0 ][ 1 ]->AssociateSlice( this->Slices[ 1 ][ 1 ] );
275   this->Slices[ 0 ][ 2 ]->AssociateSlice( this->Slices[ 1 ][ 2 ] );
276
277   for( unsigned int i = 0; i < 3; ++ i )
278   {
279     this->Slices[ 0 ][ i ]->AddRenderCommand(
280       Self::_RenderCommand, this
281       );
282     this->Slices[ 0 ][ i ]->AddWindowLevelCommand(
283       Self::_WindowLevelCommand, this
284       );
285     this->Slices[ 0 ][ i ]->AddSlicesCommand( Self::_SlicesCommand, this );
286
287   } // rof
288 }
289
290 // -------------------------------------------------------------------------
291 cpExtensions::Visualization::MPRActors::
292 ~MPRActors( )
293 {
294 }
295
296 // -------------------------------------------------------------------------
297 void cpExtensions::Visualization::MPRActors::
298 _CreateBoundingBox( )
299 {
300   ImageBlender* blender = this->Slices[ 0 ][ 0 ]->GetBlender( );
301   if( blender == NULL )
302     return;
303   if( blender->GetNumberOfImages( ) == 1 )
304   {
305     vtkImageData* image =
306       dynamic_cast< vtkImageData* >( blender->GetInput( ) );
307
308     // Create 3D outline
309     double bb[ 6 ];
310     image->GetBounds( bb );
311
312     vtkSmartPointer< vtkOutlineSource > img_ol =
313       vtkSmartPointer< vtkOutlineSource >::New( );
314     img_ol->SetBounds( bb );
315
316     vtkSmartPointer< vtkPolyDataMapper > img_ol_mapper =
317       vtkSmartPointer< vtkPolyDataMapper >::New( );
318     img_ol_mapper->SetInputConnection( img_ol->GetOutputPort( ) );
319     this->ImageOutlineActor->SetMapper( img_ol_mapper );
320     this->ImageOutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
321     this->ImageOutlineActor->GetProperty( )->SetLineWidth( 1 );
322
323     this->AddItem( this->ImageOutlineActor );
324
325   } // fi
326 }
327
328 // -------------------------------------------------------------------------
329 void cpExtensions::Visualization::MPRActors::
330 _SlicesCommand( double* pos, int axis, void* data )
331 {
332   MPRActors* actors = reinterpret_cast< MPRActors* >( data );
333   if( actors == NULL )
334     return;
335   for( unsigned int j = 0; j < 3; ++j )
336   {
337     if( actors->Slices[ 0 ][ j ]->GetAxis( ) != axis )
338     {
339       actors->Slices[ 0 ][ j ]->SetSlice( pos );
340       actors->Slices[ 1 ][ j ]->SetSlice( pos );
341       actors->Slices[ 0 ][ j ]->Render( 1e-3 );
342       actors->Slices[ 1 ][ j ]->Render( 1e-3 );
343
344     } // fi
345
346   } // rof
347 }
348
349 // -------------------------------------------------------------------------
350 void cpExtensions::Visualization::MPRActors::
351 _WindowLevelCommand( double window, double level, void* data )
352 {
353   MPRActors* actors = reinterpret_cast< MPRActors* >( data );
354   if( actors == NULL )
355     return;
356   actors->Slices[ 0 ][ 0 ]->Render( 1e-3 );
357   actors->Slices[ 0 ][ 1 ]->Render( 1e-3 );
358   actors->Slices[ 0 ][ 2 ]->Render( 1e-3 );
359   actors->Slices[ 1 ][ 0 ]->Render( 1e-3 );
360   actors->Slices[ 1 ][ 1 ]->Render( 1e-3 );
361   actors->Slices[ 1 ][ 2 ]->Render( 1e-3 );
362 }
363
364 // -------------------------------------------------------------------------
365 void cpExtensions::Visualization::MPRActors::
366 _RenderCommand( void* data )
367 {
368   MPRActors* actors = reinterpret_cast< MPRActors* >( data );
369   if( actors == NULL )
370     return;
371   actors->Slices[ 0 ][ 0 ]->Render( 1e-3 );
372   actors->Slices[ 0 ][ 1 ]->Render( 1e-3 );
373   actors->Slices[ 0 ][ 2 ]->Render( 1e-3 );
374   actors->Slices[ 1 ][ 0 ]->Render( 1e-3 );
375   actors->Slices[ 1 ][ 1 ]->Render( 1e-3 );
376   actors->Slices[ 1 ][ 2 ]->Render( 1e-3 );
377 }
378
379 // eof - $RCSfile$