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