]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Visualization/MPRActors.cxx
Intermediary commit
[cpPlugins.git] / lib / cpExtensions / Visualization / MPRActors.cxx
1 #include <cpExtensions/Visualization/MPRActors.h>
2
3 #include <vtkImageData.h>
4 #include <vtkRenderer.h>
5 #include <vtkRendererCollection.h>
6 #include <vtkRenderWindow.h>
7
8 // -------------------------------------------------------------------------
9 cpExtensions::Visualization::MPRActors*
10 cpExtensions::Visualization::MPRActors::
11 New( )
12 {
13   return( new Self( ) );
14 }
15
16 // -------------------------------------------------------------------------
17 cpExtensions::Visualization::
18 ImageSliceActors* cpExtensions::Visualization::MPRActors::
19 GetSliceActors( const int& i ) const
20 {
21   if( i < 3 )
22     return( this->Slices[ 0 ][ i ] );
23   else
24     return( NULL );
25 }
26
27 // -------------------------------------------------------------------------
28 int cpExtensions::Visualization::MPRActors::
29 AddInputConnection(
30   vtkAlgorithmOutput* aout, ImageSliceActors::LUTType lut
31   )
32 {
33   int N = this->Slices[ 0 ][ 0 ]->GetNumberOfImageActors( );
34   if( N == 0 )
35   {
36     this->Slices[ 0 ][ 0 ]->AddInputConnection( aout, 0, lut );
37     vtkImageMapToColors* imap = this->Slices[ 0 ][ 0 ]->GetImageMap( 0 );
38     for( unsigned int i = 0; i < 2; ++i )
39       for( unsigned int j = 0; j < 3; ++j )
40         if( i != 0 || j != 0 )
41           this->Slices[ i ][ j ]->AddInputConnection(
42             ( ( imap != NULL )? imap->GetOutputPort( ): aout ), j,
43             ImageSliceActors::LUTType_None
44             );
45   }
46   else
47   {
48     /*
49     // Check if the image share the same space
50     vtkImageData* ref_image = this->_Image( 0 );
51     vtkImageData* new_image =
52       vtkImageData::SafeDownCast(
53         aout->GetProducer( )->GetOutputInformation( 0 )->
54         Get( vtkDataObject::DATA_OBJECT( ) )
55         );
56     int ref_ext[ 6 ], new_ext[ 6 ];
57     ref_image->GetExtent( ref_ext );
58     new_image->GetExtent( new_ext );
59     if(
60       ref_ext[ 0 ] == new_ext[ 0 ] && ref_ext[ 1 ] == new_ext[ 1 ] &&
61       ref_ext[ 2 ] == new_ext[ 2 ] && ref_ext[ 3 ] == new_ext[ 3 ] &&
62       ref_ext[ 4 ] == new_ext[ 4 ] && ref_ext[ 5 ] == new_ext[ 5 ]
63       )
64     {
65       this->ImageMaps.push_back(
66         vtkSmartPointer< vtkImageMapToColors >::New( )
67         );
68       this->ImageMaps[ N ]->SetInputConnection( aout );
69       this->SetLookupTableToColor(
70         N, double( 1 ), double( 0 ), double( 0 )
71         );
72       this->_Update( N );
73       return( N );
74     }
75     else
76     N = -1;
77   */
78
79   } // fi
80   return( N );
81 }
82
83 // -------------------------------------------------------------------------
84 int cpExtensions::Visualization::MPRActors::
85 AddInputData( vtkImageData* image, ImageSliceActors::LUTType lut )
86 {
87   int N = this->Slices[ 0 ][ 0 ]->GetNumberOfImageActors( );
88   if( N == 0 )
89   {
90     this->Slices[ 0 ][ 0 ]->AddInputData( image, 0, lut );
91     vtkImageMapToColors* imap = this->Slices[ 0 ][ 0 ]->GetImageMap( 0 );
92     for( unsigned int i = 0; i < 2; ++i )
93       for( unsigned int j = 0; j < 3; ++j )
94         if( i != 0 || j != 0 )
95         {
96           if( imap != NULL )
97             this->Slices[ i ][ j ]->AddInputConnection(
98               imap->GetOutputPort( ), j,
99               ImageSliceActors::LUTType_None
100               );
101           else
102             this->Slices[ i ][ j ]->AddInputData(
103               image, j,
104               ImageSliceActors::LUTType_None
105               );
106         } // fi
107   }
108   else
109   {
110   } // fi
111   return( N );
112   /*
113   int N = this->ImageMaps.size( );
114   if( N == 0 )
115   {
116     this->ImageMaps.push_back(
117       vtkSmartPointer< vtkImageMapToColors >::New( )
118       );
119     this->ImageMaps[ 0 ]->SetInputData( image );
120     this->SetLookupTableToWindowLevel( 0 );
121     this->_Update( 0 );
122     this->ResetWindowLevel( 0 );
123     return( 0 );
124   }
125   else
126   {
127     // Check if the image share the same space
128     vtkImageData* ref_image = this->_Image( 0 );
129     vtkImageData* new_image = image;
130     int ref_ext[ 6 ], new_ext[ 6 ];
131     ref_image->GetExtent( ref_ext );
132     new_image->GetExtent( new_ext );
133     if(
134       ref_ext[ 0 ] == new_ext[ 0 ] && ref_ext[ 1 ] == new_ext[ 1 ] &&
135       ref_ext[ 2 ] == new_ext[ 2 ] && ref_ext[ 3 ] == new_ext[ 3 ] &&
136       ref_ext[ 4 ] == new_ext[ 4 ] && ref_ext[ 5 ] == new_ext[ 5 ]
137       )
138     {
139       this->ImageMaps.push_back(
140         vtkSmartPointer< vtkImageMapToColors >::New( )
141         );
142       this->ImageMaps[ N ]->SetInputData( image );
143       this->SetLookupTableToColor(
144         N, double( 1 ), double( 0 ), double( 0 )
145         );
146       this->_Update( N );
147       return( N );
148     }
149     else
150       return( -1 );
151
152   } // fi
153   */
154 }
155
156 // -------------------------------------------------------------------------
157 void cpExtensions::Visualization::MPRActors::
158 PushActorsInto(
159   vtkRenderWindow* x, vtkRenderWindow* y, vtkRenderWindow* z,
160   vtkRenderWindow* w
161   )
162 {
163   this->Slices[ 0 ][ 0 ]->PushActorsInto( x, true );
164   this->Slices[ 0 ][ 1 ]->PushActorsInto( y, true );
165   this->Slices[ 0 ][ 2 ]->PushActorsInto( z, true );
166
167   vtkRenderer* wren =
168     ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
169   vtkRenderer* rends[ ] =
170     {
171       ( x != NULL )? x->GetRenderers( )->GetFirstRenderer( ): NULL,
172       ( y != NULL )? y->GetRenderers( )->GetFirstRenderer( ): NULL,
173       ( z != NULL )? z->GetRenderers( )->GetFirstRenderer( ): NULL
174     };
175   for( int i = 0; i < 3; ++i )
176   {
177     if( rends[ i ] != NULL )
178       for( int j = 0; j < 3; ++j )
179         if( i != j )
180           rends[ i ]->AddActor( this->Slices[ 0 ][ j ]->GetPlaneActor( ) );
181     if( wren != NULL )
182     {
183       for(
184         unsigned int k = 0;
185         k < this->Slices[ 1 ][ i ]->GetNumberOfImageActors( );
186         ++k
187         )
188         wren->AddActor( this->Slices[ 1 ][ i ]->GetImageActor( k ) );
189       wren->AddActor( this->Slices[ 1 ][ i ]->GetPlaneActor( ) );
190
191     } // fi
192
193   } // rof
194   if( wren != NULL )
195     wren->AddActor( this->ImageOutlineActor );
196 }
197
198 // -------------------------------------------------------------------------
199 void cpExtensions::Visualization::MPRActors::
200 PopDataFrom(
201   vtkRenderer* x,
202   vtkRenderer* y,
203   vtkRenderer* z,
204   vtkRenderer* w
205   )
206 {
207   /*
208   vtkRenderer* rends[] = { x, y, z };
209   for( int i = 0; i < 3; ++i )
210   {
211     if( rends[ i ] != NULL )
212     {
213       for(
214         unsigned int k = 0;
215         k < this->Slices[ 0 ][ i ]->GetNumberOfImageActors( );
216         ++k
217         )
218         rends[ i ]->RemoveActor( this->Slices[ 0 ][ i ]->GetImageActor( k ) );
219       rends[ i ]->RemoveActor( this->Slices[ 0 ][ i ]->GetTextActor( ) );
220       for( int j = 0; j < 3; ++j )
221         rends[ i ]->RemoveActor( this->Slices[ 0 ][ j ]->GetPlaneActor( ) );
222
223     } // fi
224     if( w != NULL )
225     {
226       for(
227         unsigned int k = 0;
228         k < this->Slices[ 1 ][ i ]->GetNumberOfImageActors( );
229         ++k
230         )
231         w->RemoveActor( this->Slices[ 1 ][ i ]->GetImageActor( k ) );
232       w->RemoveActor( this->Slices[ 1 ][ i ]->GetPlaneActor( ) );
233
234     } // fi
235
236   } // rof
237   if( w != NULL )
238     w->RemoveActor( this->ImageOutlineActor );
239   */
240 }
241
242 // -------------------------------------------------------------------------
243   /*
244 void cpExtensions::Visualization::MPRActors::
245 LinkInteractors( )
246 {
247   this->_Update( 0 );
248   this->_Update( 1 );
249   this->_Update( 2 );
250 }
251   */
252
253 // -------------------------------------------------------------------------
254 void cpExtensions::Visualization::MPRActors::
255 SetLookupTable( unsigned int i, vtkScalarsToColors* lut )
256 {
257   /*
258   if( i < this->ImageMaps.size( ) )
259   {
260     this->ImageMaps[ i ]->SetLookupTable( lut );
261     this->ImageMaps[ i ]->Update( );
262     this->Modified( );
263
264   } // fi
265   */
266 }
267
268 // -------------------------------------------------------------------------
269 vtkScalarsToColors* cpExtensions::Visualization::MPRActors::
270 GetLookupTable( unsigned int i ) const
271 {
272   /*
273   if( i < this->ImageMaps.size( ) )
274     return( this->ImageMaps[ i ]->GetLookupTable( ) );
275   else
276     return( NULL );
277     */
278 }
279
280 // -------------------------------------------------------------------------
281 void cpExtensions::Visualization::MPRActors::
282 SetLookupTableToWindowLevel( unsigned int i )
283 {
284   /*
285   // Check if the input has been configured
286   vtkImageData* image = this->_Image( i );
287   if( image == NULL )
288     return;
289
290   double r[ 2 ];
291   image->GetScalarRange( r );
292
293   vtkSmartPointer< vtkWindowLevelLookupTable > lut =
294     vtkSmartPointer< vtkWindowLevelLookupTable >::New( );
295   lut->SetScaleToLinear( );
296   lut->SetTableRange( r );
297   lut->Build( );
298
299   this->SetLookupTable( i, lut );
300   */
301 }
302
303 // -------------------------------------------------------------------------
304 double cpExtensions::Visualization::MPRActors::
305 GetMinWindow( unsigned int i ) const
306 {
307   return( 0 );
308 }
309
310 // -------------------------------------------------------------------------
311 double cpExtensions::Visualization::MPRActors::
312 GetMaxWindow( unsigned int i ) const
313 {
314   /*
315   // Check if the input has been configured
316   vtkImageData* image = this->_Image( i );
317   if( image == NULL )
318     return( double( 0 ) );
319
320   double r[ 2 ];
321   image->GetScalarRange( r );
322   return( r[ 1 ] - r[ 0 ] );
323   */
324 }
325
326 // -------------------------------------------------------------------------
327 double cpExtensions::Visualization::MPRActors::
328 GetMinLevel( unsigned int i ) const
329 {
330   /*
331   // Check if the input has been configured
332   vtkImageData* image = this->_Image( i );
333   if( image == NULL )
334     return( double( 0 ) );
335
336   double r[ 2 ];
337   image->GetScalarRange( r );
338   return( r[ 0 ] );
339   */
340 }
341
342 // -------------------------------------------------------------------------
343 double cpExtensions::Visualization::MPRActors::
344 GetMaxLevel( unsigned int i ) const
345 {
346   /*
347   // Check if the input has been configured
348   vtkImageData* image = this->_Image( i );
349   if( image == NULL )
350     return( double( 0 ) );
351
352   double r[ 2 ];
353   image->GetScalarRange( r );
354   return( r[ 1 ] );
355   */
356 }
357
358 // -------------------------------------------------------------------------
359 double cpExtensions::Visualization::MPRActors::
360 GetWindow( unsigned int i ) const
361 {
362   /*
363   vtkWindowLevelLookupTable* lut =
364     dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
365   if( lut != NULL )
366     return( lut->GetWindow( ) );
367   else
368     return( double( 0 ) );
369   */
370 }
371
372 // -------------------------------------------------------------------------
373 double cpExtensions::Visualization::MPRActors::
374 GetLevel( unsigned int i ) const
375 {
376   /*
377   vtkWindowLevelLookupTable* lut =
378     dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
379   if( lut != NULL )
380     return( lut->GetLevel( ) );
381   else
382     return( double( 0 ) );
383   */
384 }
385
386 // -------------------------------------------------------------------------
387 void cpExtensions::Visualization::MPRActors::
388 SetWindow( unsigned int i, const double& w )
389 {
390   /*
391   vtkWindowLevelLookupTable* lut =
392     dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
393   if( lut != NULL )
394   {
395     lut->SetWindow( w );
396     lut->Build( );
397     this->ImageMaps[ i ]->Modified( );
398     this->Modified( );
399
400   } // fi
401   */
402 }
403
404 // -------------------------------------------------------------------------
405 void cpExtensions::Visualization::MPRActors::
406 SetLevel( unsigned int i, const double& l )
407 {
408   /*
409   vtkWindowLevelLookupTable* lut =
410     dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
411   if( lut != NULL )
412   {
413     lut->SetLevel( l );
414     lut->Build( );
415     this->ImageMaps[ i ]->Modified( );
416     this->Modified( );
417
418   } // fi
419   */
420 }
421
422 // -------------------------------------------------------------------------
423 void cpExtensions::Visualization::MPRActors::
424 SetWindowLevel( unsigned int i, const double& w, const double& l )
425 {
426   /*
427   vtkWindowLevelLookupTable* lut =
428     dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
429   if( lut != NULL )
430   {
431     lut->SetWindow( w );
432     lut->SetLevel( l );
433     lut->Build( );
434     this->ImageMaps[ i ]->Modified( );
435
436     for( unsigned int j = 0; j < 3; ++j )
437       this->Slices[ 0 ][ j ]->UpdateText( w, l );
438
439     this->Modified( );
440
441   } // fi
442   */
443 }
444
445 // -------------------------------------------------------------------------
446 void cpExtensions::Visualization::MPRActors::
447 ResetWindowLevel( unsigned int i )
448 {
449   /*
450   vtkImageData* image = this->_Image( i );
451   vtkWindowLevelLookupTable* lut =
452     dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
453   if( image != NULL && lut != NULL )
454   {
455     double r[ 2 ];
456     image->GetScalarRange( r );
457     lut->SetTableRange( r );
458     lut->SetWindow( r[ 1 ] - r[ 0 ] );
459     lut->SetLevel( ( r[ 1 ] + r[ 0 ] ) / double( 2 ) );
460     lut->Build( );
461     this->ImageMaps[ i ]->Modified( );
462     this->Modified( );
463
464   } // fi
465   */
466 }
467
468 // -------------------------------------------------------------------------
469 void cpExtensions::Visualization::MPRActors::
470 SetLookupTableToColor(
471   unsigned int i, const double& r, const double& g, const double& b
472   )
473 {
474   /*
475   static const double _0 = double( 0 );
476   static const double _1 = double( 1 );
477   static const double _2 = double( 2 );
478   static const double _4 = double( 4 );
479   static const double _6 = double( 6 );
480   static const double _OPACITY = double( 0.6 );
481
482   // Check if the input has been configured
483   vtkImageData* image = this->_Image( i );
484   if( image == NULL )
485     return;
486
487   double range[ 2 ];
488   image->GetScalarRange( range );
489
490   // Get HSV from display color
491   double cmax = ( r > g )? r: g; cmax = ( b > cmax )? b: cmax;
492   double cmin = ( r < g )? r: g; cmin = ( b < cmin )? b: cmin;
493   double d = cmax - cmin;
494
495   double saturation = ( std::fabs( cmax ) > _0 )? d / cmax: _0;
496   double value = cmax;
497   double hue = _0;
498   if( d > _0 )
499   {
500     if( r == cmax )
501       hue = std::fmod( ( g - b ) / d, _6 );
502     else if( g == cmax )
503       hue = ( ( b - r ) / d ) + _2;
504     else if( b == cmax )
505       hue = ( ( r - g ) / d ) + _4;
506     hue *= _1 / _6;
507
508   } // fi
509
510   // Define new lookup table
511   vtkSmartPointer< vtkLookupTable > lut =
512     vtkSmartPointer< vtkLookupTable >::New( );
513   lut->SetScaleToLinear( );
514   lut->SetNanColor( _0, _0, _0, _0 );
515   lut->SetTableRange( range[ 0 ], range[ 1 ] );
516   lut->SetAlphaRange( _0, _OPACITY );
517   lut->SetHueRange( _0, hue );
518   lut->SetSaturationRange( _0, saturation );
519   lut->SetValueRange( _0, value );
520   lut->Build( );
521
522   this->SetLookupTable( i, lut );
523   */
524 }
525
526 // -------------------------------------------------------------------------
527 int cpExtensions::Visualization::MPRActors::
528 GetSliceNumberMinValue( const int& axis ) const
529 {
530   return( this->Slices[ 0 ][ axis ]->GetSliceNumberMinValue( ) );
531 }
532
533 // -------------------------------------------------------------------------
534 int cpExtensions::Visualization::MPRActors::
535 GetSliceNumberMaxValue( const int& axis ) const
536 {
537   return( this->Slices[ 0 ][ axis ]->GetSliceNumberMaxValue( ) );
538 }
539
540 // -------------------------------------------------------------------------
541 int cpExtensions::Visualization::MPRActors::
542 GetSlice( const int& axis ) const
543 {
544   return( this->Slices[ 0 ][ axis ]->GetSliceNumber( ) );
545 }
546
547 // -------------------------------------------------------------------------
548 void cpExtensions::Visualization::MPRActors::
549 SetSlice( const int& axis, const int& slice )
550 {
551   vtkImageData* image = this->_Image( 0 );
552   if( image == NULL )
553     return;
554
555   // Get image data extent
556   int ext[ 6 ];
557   image->GetExtent( ext );
558
559   // Check if the slice is valid
560   int real = slice;
561   if( slice < ext[ axis << 1 ] )
562     real = ext[ axis << 1 ];
563   if( ext[ ( axis << 1 ) + 1 ] < slice )
564     real = ext[ ( axis << 1 ) + 1 ];
565
566   // Change slice
567   this->Slices[ 0 ][ axis ]->SetSliceNumber( real );
568   this->Slices[ 1 ][ axis ]->SetSliceNumber( real );
569   this->Modified( );
570 }
571
572 // -------------------------------------------------------------------------
573 void cpExtensions::Visualization::MPRActors::
574 SetSlice( const int& axis, const double& slice )
575 {
576   vtkImageData* image = this->_Image( 0 );
577   if( image == NULL )
578     return;
579
580   double x[ 3 ] = { double( 0 ) };
581   double pcoords[ 3 ];
582   int ijk[ 3 ];
583
584   x[ axis ] = slice;
585   image->ComputeStructuredCoordinates( x, ijk, pcoords );
586   this->SetSlice( axis, ijk[ axis ] );
587 }
588
589 // -------------------------------------------------------------------------
590 void cpExtensions::Visualization::MPRActors::
591 ResetSlices( )
592 {
593   for( unsigned int i = 0; i < 2; ++i )
594     for( unsigned int j = 0; j < 3; ++j )
595       this->Slices[ i ][ j ]->SetSliceNumber(
596         this->Slices[ i ][ j ]->GetSliceNumberMinValue( )
597         );
598 }
599
600 // -------------------------------------------------------------------------
601 cpExtensions::Visualization::MPRActors::
602 MPRActors( )
603   : Superclass( )
604 {
605   this->ImageOutlineActor = vtkSmartPointer< vtkActor >::New( );
606   for( unsigned int i = 0; i < 2; ++i )
607     for( unsigned int j = 0; j < 3; ++j )
608       this->Slices[ i ][ j ] = vtkSmartPointer< ImageSliceActors >::New( );
609 }
610
611 // -------------------------------------------------------------------------
612 cpExtensions::Visualization::MPRActors::
613 ~MPRActors( )
614 {
615 }
616
617 // -------------------------------------------------------------------------
618 vtkImageData* cpExtensions::Visualization::MPRActors::
619 _Image( unsigned int i ) const
620 {
621   /*
622   if( i < this->ImageMaps.size( ) )
623   {
624     vtkAlgorithm* algo = this->ImageMaps[ i ]->GetInputAlgorithm( );
625     vtkInformation* info = algo->GetOutputInformation( 0 );
626     return(
627       vtkImageData::SafeDownCast(
628         info->Get( vtkDataObject::DATA_OBJECT( ) )
629         )
630       );
631   }
632   else
633   */
634   return( NULL );
635 }
636
637 // -------------------------------------------------------------------------
638 void cpExtensions::Visualization::MPRActors::
639 _Update( unsigned int i )
640 {
641   /*
642   // Check if the input has been configured
643   vtkImageData* image = this->_Image( i );
644   if( image == NULL )
645     return;
646   this->ImageMaps[ i ]->Update( );
647
648   for( int j = 0; j < 2; ++j )
649   {
650     for( int k = 0; k < 3; ++k )
651     {
652       this->Slices[ j ][ k ]->AddInputConnection(
653         this->ImageMaps[ i ]->GetOutputPort( ), k
654         );
655       this->Slices[ j ][ k ]->UpdateText( );
656
657       // Add all of slice's props
658       this->Slices[ j ][ k ]->InitTraversal( );
659       vtkProp* prop = this->Slices[ j ][ k ]->GetNextProp( );
660       while( prop != NULL )
661       {
662         this->AddItem( prop );
663         prop = this->Slices[ j ][ k ]->GetNextProp( );
664
665       } // elihw
666
667     } // rof
668
669   } // rof
670
671   if( i == 0 )
672   {
673     // Create 3D outline
674     double bb[ 6 ];
675     image->GetBounds( bb );
676
677     vtkSmartPointer< vtkOutlineSource > img_ol =
678       vtkSmartPointer< vtkOutlineSource >::New( );
679     img_ol->SetBounds( bb );
680
681     vtkSmartPointer< vtkPolyDataMapper > img_ol_mapper =
682       vtkSmartPointer< vtkPolyDataMapper >::New( );
683     img_ol_mapper->SetInputConnection( img_ol->GetOutputPort( ) );
684     this->ImageOutlineActor->SetMapper( img_ol_mapper );
685     this->ImageOutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
686     this->ImageOutlineActor->GetProperty( )->SetLineWidth( 1 );
687
688     this->AddItem( this->ImageOutlineActor );
689
690   } // fi
691   */
692 }
693
694 // eof - $RCSfile$