]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Visualization/MPRActors.cxx
6419daa47830969507a127700e00471876eae15d
[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   this->Slices[ 1 ][ 0 ]->PushActorsInto( w, false );
215   this->Slices[ 1 ][ 1 ]->PushActorsInto( w, false );
216   this->Slices[ 1 ][ 2 ]->PushActorsInto( w, false );
217
218   /*
219
220   this->Slices[ 0 ][ 0 ]->PushActorsInto( y, false );
221   this->Slices[ 0 ][ 0 ]->PushActorsInto( z, false );
222
223   this->Slices[ 0 ][ 1 ]->PushActorsInto( x, false );
224   this->Slices[ 0 ][ 1 ]->PushActorsInto( z, false );
225
226   this->Slices[ 0 ][ 2 ]->PushActorsInto( x, false );
227   this->Slices[ 0 ][ 2 ]->PushActorsInto( y, false );
228
229   vtkRenderer* wren =
230     ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
231   if( wren != NULL )
232     wren->AddActor( this->ImageOutlineActor );
233   */
234 }
235
236 // -------------------------------------------------------------------------
237 void cpExtensions::Visualization::MPRActors::
238 PopActorsFrom(
239   vtkRenderWindow* x, vtkRenderWindow* y, vtkRenderWindow* z,
240   vtkRenderWindow* w
241   )
242 {
243   this->Slices[ 0 ][ 0 ]->PopActorsFrom( x );
244   this->Slices[ 0 ][ 1 ]->PopActorsFrom( y );
245   this->Slices[ 0 ][ 2 ]->PopActorsFrom( z );
246   this->Slices[ 1 ][ 0 ]->PopActorsFrom( w );
247   this->Slices[ 1 ][ 1 ]->PopActorsFrom( w );
248   this->Slices[ 1 ][ 2 ]->PopActorsFrom( w );
249
250   vtkRenderer* wren =
251     ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
252   if( wren != NULL )
253     wren->RemoveActor( this->ImageOutlineActor );
254
255   this->Slices[ 0 ][ 0 ]->Clear( );
256   this->Slices[ 0 ][ 1 ]->Clear( );
257   this->Slices[ 0 ][ 2 ]->Clear( );
258   this->Slices[ 1 ][ 0 ]->Clear( );
259   this->Slices[ 1 ][ 1 ]->Clear( );
260   this->Slices[ 1 ][ 2 ]->Clear( );
261 }
262
263 // -------------------------------------------------------------------------
264 void cpExtensions::Visualization::MPRActors::
265 SetLookupTableAsColor( unsigned int i, double r, double g, double b )
266 {
267   this->Slices[ 0 ][ 0 ]->SetLookupTableAsColor( 0, r, g, b );
268   this->Slices[ 0 ][ 1 ]->Modified( );
269   this->Slices[ 0 ][ 2 ]->Modified( );
270   this->Slices[ 1 ][ 0 ]->Modified( );
271   this->Slices[ 1 ][ 1 ]->Modified( );
272   this->Slices[ 1 ][ 2 ]->Modified( );
273   this->Modified( );
274 }
275
276 // -------------------------------------------------------------------------
277 double cpExtensions::Visualization::MPRActors::
278 GetWindow( )
279 {
280   return( this->Slices[ 0 ][ 0 ]->GetWindow( ) );
281 }
282
283 // -------------------------------------------------------------------------
284 double cpExtensions::Visualization::MPRActors::
285 GetLevel( )
286 {
287   return( this->Slices[ 0 ][ 0 ]->GetLevel( ) );
288 }
289
290 // -------------------------------------------------------------------------
291 int cpExtensions::Visualization::MPRActors::
292 GetSliceNumberMinValue( const int& axis ) const
293 {
294   return( this->Slices[ 0 ][ axis ]->GetSliceNumberMinValue( ) );
295 }
296
297 // -------------------------------------------------------------------------
298 int cpExtensions::Visualization::MPRActors::
299 GetSliceNumberMaxValue( const int& axis ) const
300 {
301   return( this->Slices[ 0 ][ axis ]->GetSliceNumberMaxValue( ) );
302 }
303
304 // -------------------------------------------------------------------------
305 int cpExtensions::Visualization::MPRActors::
306 GetSlice( const int& axis ) const
307 {
308   return( this->Slices[ 0 ][ axis ]->GetSliceNumber( ) );
309 }
310
311 // -------------------------------------------------------------------------
312 void cpExtensions::Visualization::MPRActors::
313 SetSlice( const int& axis, const int& slice )
314 {
315   vtkImageData* image = this->Slices[ 0 ][ 0 ]->GetInputImage( 0 );
316   if( image == NULL )
317     return;
318
319   // Get image data extent
320   int ext[ 6 ];
321   image->GetExtent( ext );
322
323   // Check if the slice is valid
324   int real = slice;
325   if( slice < ext[ axis << 1 ] )
326     real = ext[ axis << 1 ];
327   if( ext[ ( axis << 1 ) + 1 ] < slice )
328     real = ext[ ( axis << 1 ) + 1 ];
329
330   // Change slice
331   this->Slices[ 0 ][ axis ]->SetSliceNumber( real );
332   this->Slices[ 1 ][ axis ]->SetSliceNumber( real );
333   this->Modified( );
334 }
335
336 // -------------------------------------------------------------------------
337 void cpExtensions::Visualization::MPRActors::
338 SetSlice( const int& axis, const double& slice )
339 {
340   vtkImageData* image = this->Slices[ 0 ][ 0 ]->GetInputImage( 0 );
341   if( image == NULL )
342     return;
343
344   double x[ 3 ] = { double( 0 ) };
345   double pcoords[ 3 ];
346   int ijk[ 3 ];
347
348   x[ axis ] = slice;
349   image->ComputeStructuredCoordinates( x, ijk, pcoords );
350   this->SetSlice( axis, ijk[ axis ] );
351 }
352
353 // -------------------------------------------------------------------------
354 void cpExtensions::Visualization::MPRActors::
355 ResetSlices( )
356 {
357   for( unsigned int i = 0; i < 2; ++i )
358     for( unsigned int j = 0; j < 3; ++j )
359       this->Slices[ i ][ j ]->SetSliceNumber(
360         this->Slices[ i ][ j ]->GetSliceNumberMinValue( )
361         );
362 }
363
364 // -------------------------------------------------------------------------
365 cpExtensions::Visualization::MPRActors::
366 MPRActors( )
367   : Superclass( )
368 {
369   this->ImageOutlineActor = vtkSmartPointer< vtkActor >::New( );
370   for( unsigned int i = 0; i < 2; ++i )
371     for( unsigned int j = 0; j < 3; ++j )
372       this->Slices[ i ][ j ] = vtkSmartPointer< ImageSliceActors >::New( );
373
374   this->Slices[ 0 ][ 0 ]->AssociateSlice( this->Slices[ 1 ][ 0 ] );
375   this->Slices[ 0 ][ 1 ]->AssociateSlice( this->Slices[ 1 ][ 1 ] );
376   this->Slices[ 0 ][ 2 ]->AssociateSlice( this->Slices[ 1 ][ 2 ] );
377
378   for( unsigned int i = 0; i < 3; ++ i )
379   {
380     this->Slices[ 0 ][ i ]->AddRenderCommand(
381       Self::_RenderCommand, this
382       );
383     this->Slices[ 0 ][ i ]->AddWindowLevelCommand(
384       Self::_WindowLevelCommand, this
385       );
386     // this->Slices[ 0 ][ i ]->AddSlicesCommand( Self::_SlicesCommand, this );
387
388   } // rof
389 }
390
391 // -------------------------------------------------------------------------
392 cpExtensions::Visualization::MPRActors::
393 ~MPRActors( )
394 {
395 }
396
397 // -------------------------------------------------------------------------
398 void cpExtensions::Visualization::MPRActors::
399 _SlicesCommand( double* pos, int axis, void* data )
400 {
401   /*
402     MPRActors* actors = reinterpret_cast< MPRActors* >( data );
403     if( actors == NULL )
404     return;
405     for( unsigned int j = 0; j < 3; ++j )
406     if( actors->Slices[ 0 ][ j ]->GetAxis( ) != axis )
407     actors->Slices[ 0 ][ j ]->SetSlice( pos );
408     actors->Modified( );
409   */
410 }
411
412 // -------------------------------------------------------------------------
413 void cpExtensions::Visualization::MPRActors::
414 _WindowLevelCommand( double window, double level, void* data )
415 {
416   MPRActors* actors = reinterpret_cast< MPRActors* >( data );
417   if( actors == NULL )
418     return;
419   actors->Slices[ 0 ][ 0 ]->Render( );
420   actors->Slices[ 0 ][ 1 ]->Render( );
421   actors->Slices[ 0 ][ 2 ]->Render( );
422   actors->Slices[ 1 ][ 0 ]->Render( );
423   actors->Slices[ 1 ][ 1 ]->Render( );
424   actors->Slices[ 1 ][ 2 ]->Render( );
425 }
426
427 // -------------------------------------------------------------------------
428 void cpExtensions::Visualization::MPRActors::
429 _RenderCommand( void* data )
430 {
431   MPRActors* actors = reinterpret_cast< MPRActors* >( data );
432   if( actors == NULL )
433     return;
434   actors->Slices[ 0 ][ 0 ]->Render( );
435   actors->Slices[ 0 ][ 1 ]->Render( );
436   actors->Slices[ 0 ][ 2 ]->Render( );
437   actors->Slices[ 1 ][ 0 ]->Render( );
438   actors->Slices[ 1 ][ 1 ]->Render( );
439   actors->Slices[ 1 ][ 2 ]->Render( );
440 }
441
442 // eof - $RCSfile$