]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Visualization/MPRActors.cxx
Parameters are now part of the pipeline update process
[cpPlugins.git] / lib / cpExtensions / Visualization / MPRActors.cxx
1 #include <cpExtensions/Visualization/MPRActors.h>
2
3 #include <vtkAlgorithmOutput.h>
4 #include <vtkImageData.h>
5 #include <vtkRenderer.h>
6 #include <vtkRendererCollection.h>
7 #include <vtkRenderWindow.h>
8
9 // -------------------------------------------------------------------------
10 cpExtensions::Visualization::MPRActors*
11 cpExtensions::Visualization::MPRActors::
12 New( )
13 {
14   return( new Self( ) );
15 }
16
17 // -------------------------------------------------------------------------
18 cpExtensions::Visualization::
19 ImageSliceActors* cpExtensions::Visualization::MPRActors::
20 GetSliceActors( const int& i ) const
21 {
22   if( i < 3 )
23     return( this->Slices[ 0 ][ i ] );
24   else
25     return( NULL );
26 }
27
28 // -------------------------------------------------------------------------
29 int cpExtensions::Visualization::MPRActors::
30 AddInputConnection( vtkAlgorithmOutput* aout )
31 {
32   int N = this->Slices[ 0 ][ 0 ]->GetNumberOfImageActors( );
33   if( N == 0 )
34   {
35     this->Slices[ 0 ][ 0 ]->AddInputConnection( aout, 0 );
36     vtkImageMapToColors* imap = this->Slices[ 0 ][ 0 ]->GetImageMap( 0 );
37     for( unsigned int i = 0; i < 2; ++i )
38       for( unsigned int j = 0; j < 3; ++j )
39         if( i != 0 || j != 0 )
40           this->Slices[ i ][ j ]->AddInputConnection(
41             ( ( imap != NULL )? imap->GetOutputPort( ): aout ), j
42             );
43   }
44   else
45   {
46     // Check if the image share the same space
47     vtkImageData* new_image = dynamic_cast< vtkImageData* >(
48       aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
49       );
50     if( new_image != NULL )
51     {
52       vtkAlgorithmOutput* ref_aout =
53         this->Slices[ 0 ][ 0 ]->GetImageMap( 0 )->GetOutputPort( );
54       vtkImageData* ref_image = dynamic_cast< vtkImageData* >(
55         ref_aout->GetProducer( )->GetOutputDataObject( ref_aout->GetIndex( ) )
56         );
57       if( ref_image != NULL )
58       {
59         int ref_ext[ 6 ], new_ext[ 6 ];
60         ref_image->GetExtent( ref_ext );
61         new_image->GetExtent( new_ext );
62         if(
63           ref_ext[ 0 ] == new_ext[ 0 ] && ref_ext[ 1 ] == new_ext[ 1 ] &&
64           ref_ext[ 2 ] == new_ext[ 2 ] && ref_ext[ 3 ] == new_ext[ 3 ] &&
65           ref_ext[ 4 ] == new_ext[ 4 ] && ref_ext[ 5 ] == new_ext[ 5 ]
66           )
67         {
68           this->Slices[ 0 ][ 0 ]->AddInputConnection( aout, 0 );
69           vtkImageMapToColors* imap = this->Slices[ 0 ][ 0 ]->GetImageMap( N );
70           for( unsigned int i = 0; i < 2; ++i )
71             for( unsigned int j = 0; j < 3; ++j )
72               if( i != 0 || j != 0 )
73                 this->Slices[ i ][ j ]->AddInputConnection(
74                   ( ( imap != NULL )? imap->GetOutputPort( ): aout ), j
75                   );
76         }
77         else
78           N = -1;
79       }
80       else
81         N = -1;
82     }
83     else
84       N = -1;
85
86   } // fi
87   return( N );
88 }
89
90 // -------------------------------------------------------------------------
91 int cpExtensions::Visualization::MPRActors::
92 AddInputData( vtkImageData* new_image )
93 {
94   int N = this->Slices[ 0 ][ 0 ]->GetNumberOfImageActors( );
95   if( N == 0 )
96   {
97     this->Slices[ 0 ][ 0 ]->AddInputData( new_image, 0 );
98     vtkImageMapToColors* imap = this->Slices[ 0 ][ 0 ]->GetImageMap( 0 );
99     for( unsigned int i = 0; i < 2; ++i )
100       for( unsigned int j = 0; j < 3; ++j )
101         if( i != 0 || j != 0 )
102         {
103           if( imap != NULL )
104             this->Slices[ i ][ j ]->AddInputConnection(
105               imap->GetOutputPort( ), j
106               );
107           else
108             this->Slices[ i ][ j ]->AddInputData( new_image, j );
109
110         } // fi
111   }
112   else
113   {
114     // Check if the image share the same space
115     vtkAlgorithmOutput* ref_aout =
116       this->Slices[ 0 ][ 0 ]->GetImageMap( 0 )->GetOutputPort( );
117     vtkImageData* ref_image = dynamic_cast< vtkImageData* >(
118       ref_aout->GetProducer( )->GetOutputDataObject( ref_aout->GetIndex( ) )
119       );
120     if( ref_image != NULL )
121     {
122       int ref_ext[ 6 ], new_ext[ 6 ];
123       ref_image->GetExtent( ref_ext );
124       new_image->GetExtent( new_ext );
125       if(
126         ref_ext[ 0 ] == new_ext[ 0 ] && ref_ext[ 1 ] == new_ext[ 1 ] &&
127         ref_ext[ 2 ] == new_ext[ 2 ] && ref_ext[ 3 ] == new_ext[ 3 ] &&
128         ref_ext[ 4 ] == new_ext[ 4 ] && ref_ext[ 5 ] == new_ext[ 5 ]
129         )
130       {
131         this->Slices[ 0 ][ 0 ]->AddInputData( new_image, 0 );
132         vtkImageMapToColors* imap = this->Slices[ 0 ][ 0 ]->GetImageMap( N );
133         for( unsigned int i = 0; i < 2; ++i )
134           for( unsigned int j = 0; j < 3; ++j )
135             if( i != 0 || j != 0 )
136             {
137               if( imap != NULL )
138                 this->Slices[ i ][ j ]->AddInputConnection(
139                   imap->GetOutputPort( ), j
140                   );
141               else
142                 this->Slices[ i ][ j ]->AddInputData( new_image, j );
143
144             } // fi
145       }
146       else
147         N = -1;
148     }
149     else
150       N = -1;
151
152   } // fi
153   return( N );
154 }
155
156 // -------------------------------------------------------------------------
157 unsigned int cpExtensions::Visualization::MPRActors::
158 GetNumberOfImages( ) const
159 {
160   return( this->Slices[ 0 ][ 0 ]->GetNumberOfImageActors( ) );
161 }
162
163 // -------------------------------------------------------------------------
164 void cpExtensions::Visualization::MPRActors::
165 PushActorsInto(
166   vtkRenderWindow* x, vtkRenderWindow* y, vtkRenderWindow* z,
167   vtkRenderWindow* w
168   )
169 {
170   this->Slices[ 0 ][ 0 ]->PushActorsInto( x, true );
171   this->Slices[ 0 ][ 1 ]->PushActorsInto( y, true );
172   this->Slices[ 0 ][ 2 ]->PushActorsInto( z, true );
173
174   vtkRenderer* wren =
175     ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
176   vtkRenderer* rends[ ] =
177     {
178       ( x != NULL )? x->GetRenderers( )->GetFirstRenderer( ): NULL,
179       ( y != NULL )? y->GetRenderers( )->GetFirstRenderer( ): NULL,
180       ( z != NULL )? z->GetRenderers( )->GetFirstRenderer( ): NULL
181     };
182   for( int i = 0; i < 3; ++i )
183   {
184     if( rends[ i ] != NULL )
185       for( int j = 0; j < 3; ++j )
186         if( i != j )
187           rends[ i ]->AddActor( this->Slices[ 0 ][ j ]->GetPlaneActor( ) );
188     if( wren != NULL )
189     {
190       for(
191         unsigned int k = 0;
192         k < this->Slices[ 1 ][ i ]->GetNumberOfImageActors( );
193         ++k
194         )
195         wren->AddActor( this->Slices[ 1 ][ i ]->GetImageActor( k ) );
196       wren->AddActor( this->Slices[ 1 ][ i ]->GetPlaneActor( ) );
197
198     } // fi
199
200   } // rof
201   if( wren != NULL )
202     wren->AddActor( this->ImageOutlineActor );
203
204   for( unsigned int j = 0; j < 3; ++j )
205   {
206     ImageInteractorStyle* st =
207       dynamic_cast< ImageInteractorStyle* >( this->Slices[ 0 ][ j ]->GetStyle( ) );
208     if( w != NULL )
209       st->AssociateInteractor( w->GetInteractor( ) );
210     for( unsigned int l = 0; l < 3; ++l )
211       if( j != l )
212         st->AssociateInteractor(
213           this->Slices[ 0 ][ l ]->GetStyle( )->GetInteractor( )
214           );
215
216   } // rof
217 }
218
219 // -------------------------------------------------------------------------
220 void cpExtensions::Visualization::MPRActors::
221 PopActorsFrom(
222   vtkRenderWindow* x, vtkRenderWindow* y, vtkRenderWindow* z,
223   vtkRenderWindow* w
224   )
225 {
226   this->Slices[ 0 ][ 0 ]->PopActorsFrom( x );
227   this->Slices[ 0 ][ 1 ]->PopActorsFrom( y );
228   this->Slices[ 0 ][ 2 ]->PopActorsFrom( z );
229
230   vtkRenderer* wren =
231     ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
232   vtkRenderer* rends[ ] =
233     {
234       ( x != NULL )? x->GetRenderers( )->GetFirstRenderer( ): NULL,
235       ( y != NULL )? y->GetRenderers( )->GetFirstRenderer( ): NULL,
236       ( z != NULL )? z->GetRenderers( )->GetFirstRenderer( ): NULL
237     };
238   for( int i = 0; i < 3; ++i )
239   {
240     if( rends[ i ] != NULL )
241       for( int j = 0; j < 3; ++j )
242         if( i != j )
243           rends[ i ]->RemoveActor( this->Slices[ 0 ][ j ]->GetPlaneActor( ) );
244     if( wren != NULL )
245     {
246       for(
247         unsigned int k = 0;
248         k < this->Slices[ 1 ][ i ]->GetNumberOfImageActors( );
249         ++k
250         )
251         wren->RemoveActor( this->Slices[ 1 ][ i ]->GetImageActor( k ) );
252       wren->RemoveActor( this->Slices[ 1 ][ i ]->GetPlaneActor( ) );
253
254     } // fi
255
256   } // rof
257   if( wren != NULL )
258     wren->RemoveActor( this->ImageOutlineActor );
259
260   this->Slices[ 0 ][ 0 ]->Clear( );
261   this->Slices[ 0 ][ 1 ]->Clear( );
262   this->Slices[ 0 ][ 2 ]->Clear( );
263   this->Slices[ 1 ][ 0 ]->Clear( );
264   this->Slices[ 1 ][ 1 ]->Clear( );
265   this->Slices[ 1 ][ 2 ]->Clear( );
266 }
267
268 // -------------------------------------------------------------------------
269 void cpExtensions::Visualization::MPRActors::
270 SetLookupTableAsColor( unsigned int i, double r, double g, double b )
271 {
272   this->Slices[ 0 ][ 0 ]->SetLookupTableAsColor( 0, r, g, b );
273   this->Slices[ 0 ][ 1 ]->Modified( );
274   this->Slices[ 0 ][ 2 ]->Modified( );
275   this->Slices[ 1 ][ 0 ]->Modified( );
276   this->Slices[ 1 ][ 1 ]->Modified( );
277   this->Slices[ 1 ][ 2 ]->Modified( );
278   this->Modified( );
279 }
280
281 // -------------------------------------------------------------------------
282 int cpExtensions::Visualization::MPRActors::
283 GetSliceNumberMinValue( const int& axis ) const
284 {
285   return( this->Slices[ 0 ][ axis ]->GetSliceNumberMinValue( ) );
286 }
287
288 // -------------------------------------------------------------------------
289 int cpExtensions::Visualization::MPRActors::
290 GetSliceNumberMaxValue( const int& axis ) const
291 {
292   return( this->Slices[ 0 ][ axis ]->GetSliceNumberMaxValue( ) );
293 }
294
295 // -------------------------------------------------------------------------
296 int cpExtensions::Visualization::MPRActors::
297 GetSlice( const int& axis ) const
298 {
299   return( this->Slices[ 0 ][ axis ]->GetSliceNumber( ) );
300 }
301
302 // -------------------------------------------------------------------------
303 void cpExtensions::Visualization::MPRActors::
304 SetSlice( const int& axis, const int& slice )
305 {
306   vtkImageData* image = this->_Image( 0 );
307   if( image == NULL )
308     return;
309
310   // Get image data extent
311   int ext[ 6 ];
312   image->GetExtent( ext );
313
314   // Check if the slice is valid
315   int real = slice;
316   if( slice < ext[ axis << 1 ] )
317     real = ext[ axis << 1 ];
318   if( ext[ ( axis << 1 ) + 1 ] < slice )
319     real = ext[ ( axis << 1 ) + 1 ];
320
321   // Change slice
322   this->Slices[ 0 ][ axis ]->SetSliceNumber( real );
323   this->Slices[ 1 ][ axis ]->SetSliceNumber( real );
324   this->Modified( );
325 }
326
327 // -------------------------------------------------------------------------
328 void cpExtensions::Visualization::MPRActors::
329 SetSlice( const int& axis, const double& slice )
330 {
331   vtkImageData* image = this->_Image( 0 );
332   if( image == NULL )
333     return;
334
335   double x[ 3 ] = { double( 0 ) };
336   double pcoords[ 3 ];
337   int ijk[ 3 ];
338
339   x[ axis ] = slice;
340   image->ComputeStructuredCoordinates( x, ijk, pcoords );
341   this->SetSlice( axis, ijk[ axis ] );
342 }
343
344 // -------------------------------------------------------------------------
345 void cpExtensions::Visualization::MPRActors::
346 ResetSlices( )
347 {
348   for( unsigned int i = 0; i < 2; ++i )
349     for( unsigned int j = 0; j < 3; ++j )
350       this->Slices[ i ][ j ]->SetSliceNumber(
351         this->Slices[ i ][ j ]->GetSliceNumberMinValue( )
352         );
353 }
354
355 // -------------------------------------------------------------------------
356 cpExtensions::Visualization::MPRActors::
357 MPRActors( )
358   : Superclass( )
359 {
360   this->ImageOutlineActor = vtkSmartPointer< vtkActor >::New( );
361   for( unsigned int i = 0; i < 2; ++i )
362     for( unsigned int j = 0; j < 3; ++j )
363       this->Slices[ i ][ j ] = vtkSmartPointer< ImageSliceActors >::New( );
364
365   this->Slices[ 0 ][ 0 ]->AssociateSlice( this->Slices[ 1 ][ 0 ] );
366   this->Slices[ 0 ][ 1 ]->AssociateSlice( this->Slices[ 1 ][ 1 ] );
367   this->Slices[ 0 ][ 2 ]->AssociateSlice( this->Slices[ 1 ][ 2 ] );
368
369   this->Slices[ 0 ][ 0 ]->SetSlicesCommand( Self::_SetSlices, this );
370   this->Slices[ 0 ][ 1 ]->SetSlicesCommand( Self::_SetSlices, this );
371   this->Slices[ 0 ][ 2 ]->SetSlicesCommand( Self::_SetSlices, this );
372 }
373
374 // -------------------------------------------------------------------------
375 cpExtensions::Visualization::MPRActors::
376 ~MPRActors( )
377 {
378 }
379
380 // -------------------------------------------------------------------------
381 vtkImageData* cpExtensions::Visualization::MPRActors::
382 _Image( unsigned int i ) const
383 {
384   /*
385   if( i < this->ImageMaps.size( ) )
386   {
387     vtkAlgorithm* algo = this->ImageMaps[ i ]->GetInputAlgorithm( );
388     vtkInformation* info = algo->GetOutputInformation( 0 );
389     return(
390       vtkImageData::SafeDownCast(
391         info->Get( vtkDataObject::DATA_OBJECT( ) )
392         )
393       );
394   }
395   else
396   */
397   return( NULL );
398 }
399
400 // -------------------------------------------------------------------------
401 void cpExtensions::Visualization::MPRActors::
402 _Update( unsigned int i )
403 {
404   /*
405   // Check if the input has been configured
406   vtkImageData* image = this->_Image( i );
407   if( image == NULL )
408     return;
409   this->ImageMaps[ i ]->Update( );
410
411   for( int j = 0; j < 2; ++j )
412   {
413     for( int k = 0; k < 3; ++k )
414     {
415       this->Slices[ j ][ k ]->AddInputConnection(
416         this->ImageMaps[ i ]->GetOutputPort( ), k
417         );
418       this->Slices[ j ][ k ]->UpdateText( );
419
420       // Add all of slice's props
421       this->Slices[ j ][ k ]->InitTraversal( );
422       vtkProp* prop = this->Slices[ j ][ k ]->GetNextProp( );
423       while( prop != NULL )
424       {
425         this->AddItem( prop );
426         prop = this->Slices[ j ][ k ]->GetNextProp( );
427
428       } // elihw
429
430     } // rof
431
432   } // rof
433
434   if( i == 0 )
435   {
436     // Create 3D outline
437     double bb[ 6 ];
438     image->GetBounds( bb );
439
440     vtkSmartPointer< vtkOutlineSource > img_ol =
441       vtkSmartPointer< vtkOutlineSource >::New( );
442     img_ol->SetBounds( bb );
443
444     vtkSmartPointer< vtkPolyDataMapper > img_ol_mapper =
445       vtkSmartPointer< vtkPolyDataMapper >::New( );
446     img_ol_mapper->SetInputConnection( img_ol->GetOutputPort( ) );
447     this->ImageOutlineActor->SetMapper( img_ol_mapper );
448     this->ImageOutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
449     this->ImageOutlineActor->GetProperty( )->SetLineWidth( 1 );
450
451     this->AddItem( this->ImageOutlineActor );
452
453   } // fi
454   */
455 }
456
457 // -------------------------------------------------------------------------
458 void cpExtensions::Visualization::MPRActors::
459 _SetSlices( double* pos, int axis, void* data )
460 {
461   MPRActors* actors = reinterpret_cast< MPRActors* >( data );
462   if( actors == NULL )
463     return;
464   for( unsigned int j = 0; j < 3; ++j )
465     if( actors->Slices[ 0 ][ j ]->GetAxis( ) != axis )
466       actors->Slices[ 0 ][ j ]->SetSlice( pos );
467   actors->Modified( );
468 }
469
470 // eof - $RCSfile$