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