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