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