1 #include <cpExtensions/Visualization/MPRActors.h>
3 #include <vtkImageData.h>
4 #include <vtkRenderer.h>
5 #include <vtkRendererCollection.h>
6 #include <vtkRenderWindow.h>
8 // -------------------------------------------------------------------------
9 cpExtensions::Visualization::MPRActors*
10 cpExtensions::Visualization::MPRActors::
13 return( new Self( ) );
16 // -------------------------------------------------------------------------
17 cpExtensions::Visualization::
18 ImageSliceActors* cpExtensions::Visualization::MPRActors::
19 GetSliceActors( const int& i ) const
22 return( this->Slices[ 0 ][ i ] );
27 // -------------------------------------------------------------------------
28 int cpExtensions::Visualization::MPRActors::
30 vtkAlgorithmOutput* aout, ImageSliceActors::LUTType lut
33 int N = this->Slices[ 0 ][ 0 ]->GetNumberOfImageActors( );
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
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( ) )
56 int ref_ext[ 6 ], new_ext[ 6 ];
57 ref_image->GetExtent( ref_ext );
58 new_image->GetExtent( new_ext );
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 ]
65 this->ImageMaps.push_back(
66 vtkSmartPointer< vtkImageMapToColors >::New( )
68 this->ImageMaps[ N ]->SetInputConnection( aout );
69 this->SetLookupTableToColor(
70 N, double( 1 ), double( 0 ), double( 0 )
83 // -------------------------------------------------------------------------
84 int cpExtensions::Visualization::MPRActors::
85 AddInputData( vtkImageData* image, ImageSliceActors::LUTType lut )
87 int N = this->Slices[ 0 ][ 0 ]->GetNumberOfImageActors( );
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 )
97 this->Slices[ i ][ j ]->AddInputConnection(
98 imap->GetOutputPort( ), j,
99 ImageSliceActors::LUTType_None
102 this->Slices[ i ][ j ]->AddInputData(
104 ImageSliceActors::LUTType_None
113 int N = this->ImageMaps.size( );
116 this->ImageMaps.push_back(
117 vtkSmartPointer< vtkImageMapToColors >::New( )
119 this->ImageMaps[ 0 ]->SetInputData( image );
120 this->SetLookupTableToWindowLevel( 0 );
122 this->ResetWindowLevel( 0 );
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 );
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 ]
139 this->ImageMaps.push_back(
140 vtkSmartPointer< vtkImageMapToColors >::New( )
142 this->ImageMaps[ N ]->SetInputData( image );
143 this->SetLookupTableToColor(
144 N, double( 1 ), double( 0 ), double( 0 )
156 // -------------------------------------------------------------------------
157 void cpExtensions::Visualization::MPRActors::
159 vtkRenderWindow* x, vtkRenderWindow* y, vtkRenderWindow* z,
163 this->Slices[ 0 ][ 0 ]->PushActorsInto( x, true );
164 this->Slices[ 0 ][ 1 ]->PushActorsInto( y, true );
165 this->Slices[ 0 ][ 2 ]->PushActorsInto( z, true );
168 ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
169 vtkRenderer* rends[ ] =
171 ( x != NULL )? x->GetRenderers( )->GetFirstRenderer( ): NULL,
172 ( y != NULL )? y->GetRenderers( )->GetFirstRenderer( ): NULL,
173 ( z != NULL )? z->GetRenderers( )->GetFirstRenderer( ): NULL
175 for( int i = 0; i < 3; ++i )
177 if( rends[ i ] != NULL )
178 for( int j = 0; j < 3; ++j )
180 rends[ i ]->AddActor( this->Slices[ 0 ][ j ]->GetPlaneActor( ) );
185 k < this->Slices[ 1 ][ i ]->GetNumberOfImageActors( );
188 wren->AddActor( this->Slices[ 1 ][ i ]->GetImageActor( k ) );
189 wren->AddActor( this->Slices[ 1 ][ i ]->GetPlaneActor( ) );
195 wren->AddActor( this->ImageOutlineActor );
197 for( unsigned int j = 0; j < 3; ++j )
199 ImageInteractorStyle* st =
200 dynamic_cast< ImageInteractorStyle* >( this->Slices[ 0 ][ j ]->GetStyle( ) );
202 st->AssociateInteractor( w->GetInteractor( ) );
203 for( unsigned int l = 0; l < 3; ++l )
205 st->AssociateInteractor(
206 this->Slices[ 0 ][ l ]->GetStyle( )->GetInteractor( )
212 // -------------------------------------------------------------------------
213 void cpExtensions::Visualization::MPRActors::
222 vtkRenderer* rends[] = { x, y, z };
223 for( int i = 0; i < 3; ++i )
225 if( rends[ i ] != NULL )
229 k < this->Slices[ 0 ][ i ]->GetNumberOfImageActors( );
232 rends[ i ]->RemoveActor( this->Slices[ 0 ][ i ]->GetImageActor( k ) );
233 rends[ i ]->RemoveActor( this->Slices[ 0 ][ i ]->GetTextActor( ) );
234 for( int j = 0; j < 3; ++j )
235 rends[ i ]->RemoveActor( this->Slices[ 0 ][ j ]->GetPlaneActor( ) );
242 k < this->Slices[ 1 ][ i ]->GetNumberOfImageActors( );
245 w->RemoveActor( this->Slices[ 1 ][ i ]->GetImageActor( k ) );
246 w->RemoveActor( this->Slices[ 1 ][ i ]->GetPlaneActor( ) );
252 w->RemoveActor( this->ImageOutlineActor );
256 // -------------------------------------------------------------------------
258 void cpExtensions::Visualization::MPRActors::
267 // -------------------------------------------------------------------------
268 void cpExtensions::Visualization::MPRActors::
269 SetLookupTable( unsigned int i, vtkScalarsToColors* lut )
272 if( i < this->ImageMaps.size( ) )
274 this->ImageMaps[ i ]->SetLookupTable( lut );
275 this->ImageMaps[ i ]->Update( );
282 // -------------------------------------------------------------------------
283 vtkScalarsToColors* cpExtensions::Visualization::MPRActors::
284 GetLookupTable( unsigned int i ) const
287 if( i < this->ImageMaps.size( ) )
288 return( this->ImageMaps[ i ]->GetLookupTable( ) );
295 // -------------------------------------------------------------------------
296 void cpExtensions::Visualization::MPRActors::
297 SetLookupTableToWindowLevel( unsigned int i )
300 // Check if the input has been configured
301 vtkImageData* image = this->_Image( i );
306 image->GetScalarRange( r );
308 vtkSmartPointer< vtkWindowLevelLookupTable > lut =
309 vtkSmartPointer< vtkWindowLevelLookupTable >::New( );
310 lut->SetScaleToLinear( );
311 lut->SetTableRange( r );
314 this->SetLookupTable( i, lut );
318 // -------------------------------------------------------------------------
319 double cpExtensions::Visualization::MPRActors::
320 GetMinWindow( unsigned int i ) const
325 // -------------------------------------------------------------------------
326 double cpExtensions::Visualization::MPRActors::
327 GetMaxWindow( unsigned int i ) const
330 // Check if the input has been configured
331 vtkImageData* image = this->_Image( i );
333 return( double( 0 ) );
336 image->GetScalarRange( r );
337 return( r[ 1 ] - r[ 0 ] );
342 // -------------------------------------------------------------------------
343 double cpExtensions::Visualization::MPRActors::
344 GetMinLevel( unsigned int i ) const
347 // Check if the input has been configured
348 vtkImageData* image = this->_Image( i );
350 return( double( 0 ) );
353 image->GetScalarRange( r );
359 // -------------------------------------------------------------------------
360 double cpExtensions::Visualization::MPRActors::
361 GetMaxLevel( unsigned int i ) const
364 // Check if the input has been configured
365 vtkImageData* image = this->_Image( i );
367 return( double( 0 ) );
370 image->GetScalarRange( r );
376 // -------------------------------------------------------------------------
377 double cpExtensions::Visualization::MPRActors::
378 GetWindow( unsigned int i ) const
381 vtkWindowLevelLookupTable* lut =
382 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
384 return( lut->GetWindow( ) );
386 return( double( 0 ) );
391 // -------------------------------------------------------------------------
392 double cpExtensions::Visualization::MPRActors::
393 GetLevel( unsigned int i ) const
396 vtkWindowLevelLookupTable* lut =
397 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
399 return( lut->GetLevel( ) );
401 return( double( 0 ) );
406 // -------------------------------------------------------------------------
407 void cpExtensions::Visualization::MPRActors::
408 SetWindow( unsigned int i, const double& w )
411 vtkWindowLevelLookupTable* lut =
412 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
417 this->ImageMaps[ i ]->Modified( );
424 // -------------------------------------------------------------------------
425 void cpExtensions::Visualization::MPRActors::
426 SetLevel( unsigned int i, const double& l )
429 vtkWindowLevelLookupTable* lut =
430 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
435 this->ImageMaps[ i ]->Modified( );
442 // -------------------------------------------------------------------------
443 void cpExtensions::Visualization::MPRActors::
444 SetWindowLevel( unsigned int i, const double& w, const double& l )
447 vtkWindowLevelLookupTable* lut =
448 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
454 this->ImageMaps[ i ]->Modified( );
456 for( unsigned int j = 0; j < 3; ++j )
457 this->Slices[ 0 ][ j ]->UpdateText( w, l );
465 // -------------------------------------------------------------------------
466 void cpExtensions::Visualization::MPRActors::
467 ResetWindowLevel( unsigned int i )
470 vtkImageData* image = this->_Image( i );
471 vtkWindowLevelLookupTable* lut =
472 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
473 if( image != NULL && lut != NULL )
476 image->GetScalarRange( r );
477 lut->SetTableRange( r );
478 lut->SetWindow( r[ 1 ] - r[ 0 ] );
479 lut->SetLevel( ( r[ 1 ] + r[ 0 ] ) / double( 2 ) );
481 this->ImageMaps[ i ]->Modified( );
488 // -------------------------------------------------------------------------
489 void cpExtensions::Visualization::MPRActors::
490 SetLookupTableToColor(
491 unsigned int i, const double& r, const double& g, const double& b
495 static const double _0 = double( 0 );
496 static const double _1 = double( 1 );
497 static const double _2 = double( 2 );
498 static const double _4 = double( 4 );
499 static const double _6 = double( 6 );
500 static const double _OPACITY = double( 0.6 );
502 // Check if the input has been configured
503 vtkImageData* image = this->_Image( i );
508 image->GetScalarRange( range );
510 // Get HSV from display color
511 double cmax = ( r > g )? r: g; cmax = ( b > cmax )? b: cmax;
512 double cmin = ( r < g )? r: g; cmin = ( b < cmin )? b: cmin;
513 double d = cmax - cmin;
515 double saturation = ( std::fabs( cmax ) > _0 )? d / cmax: _0;
521 hue = std::fmod( ( g - b ) / d, _6 );
523 hue = ( ( b - r ) / d ) + _2;
525 hue = ( ( r - g ) / d ) + _4;
530 // Define new lookup table
531 vtkSmartPointer< vtkLookupTable > lut =
532 vtkSmartPointer< vtkLookupTable >::New( );
533 lut->SetScaleToLinear( );
534 lut->SetNanColor( _0, _0, _0, _0 );
535 lut->SetTableRange( range[ 0 ], range[ 1 ] );
536 lut->SetAlphaRange( _0, _OPACITY );
537 lut->SetHueRange( _0, hue );
538 lut->SetSaturationRange( _0, saturation );
539 lut->SetValueRange( _0, value );
542 this->SetLookupTable( i, lut );
546 // -------------------------------------------------------------------------
547 int cpExtensions::Visualization::MPRActors::
548 GetSliceNumberMinValue( const int& axis ) const
550 return( this->Slices[ 0 ][ axis ]->GetSliceNumberMinValue( ) );
553 // -------------------------------------------------------------------------
554 int cpExtensions::Visualization::MPRActors::
555 GetSliceNumberMaxValue( const int& axis ) const
557 return( this->Slices[ 0 ][ axis ]->GetSliceNumberMaxValue( ) );
560 // -------------------------------------------------------------------------
561 int cpExtensions::Visualization::MPRActors::
562 GetSlice( const int& axis ) const
564 return( this->Slices[ 0 ][ axis ]->GetSliceNumber( ) );
567 // -------------------------------------------------------------------------
568 void cpExtensions::Visualization::MPRActors::
569 SetSlice( const int& axis, const int& slice )
571 vtkImageData* image = this->_Image( 0 );
575 // Get image data extent
577 image->GetExtent( ext );
579 // Check if the slice is valid
581 if( slice < ext[ axis << 1 ] )
582 real = ext[ axis << 1 ];
583 if( ext[ ( axis << 1 ) + 1 ] < slice )
584 real = ext[ ( axis << 1 ) + 1 ];
587 this->Slices[ 0 ][ axis ]->SetSliceNumber( real );
588 this->Slices[ 1 ][ axis ]->SetSliceNumber( real );
592 // -------------------------------------------------------------------------
593 void cpExtensions::Visualization::MPRActors::
594 SetSlice( const int& axis, const double& slice )
596 vtkImageData* image = this->_Image( 0 );
600 double x[ 3 ] = { double( 0 ) };
605 image->ComputeStructuredCoordinates( x, ijk, pcoords );
606 this->SetSlice( axis, ijk[ axis ] );
609 // -------------------------------------------------------------------------
610 void cpExtensions::Visualization::MPRActors::
613 for( unsigned int i = 0; i < 2; ++i )
614 for( unsigned int j = 0; j < 3; ++j )
615 this->Slices[ i ][ j ]->SetSliceNumber(
616 this->Slices[ i ][ j ]->GetSliceNumberMinValue( )
620 // -------------------------------------------------------------------------
621 cpExtensions::Visualization::MPRActors::
625 this->ImageOutlineActor = vtkSmartPointer< vtkActor >::New( );
626 for( unsigned int i = 0; i < 2; ++i )
627 for( unsigned int j = 0; j < 3; ++j )
628 this->Slices[ i ][ j ] = vtkSmartPointer< ImageSliceActors >::New( );
630 this->Slices[ 0 ][ 0 ]->AssociateSlice( this->Slices[ 1 ][ 0 ] );
631 this->Slices[ 0 ][ 1 ]->AssociateSlice( this->Slices[ 1 ][ 1 ] );
632 this->Slices[ 0 ][ 2 ]->AssociateSlice( this->Slices[ 1 ][ 2 ] );
634 this->Slices[ 0 ][ 0 ]->SetSlicesCommand( Self::_SetSlices, this );
635 this->Slices[ 0 ][ 1 ]->SetSlicesCommand( Self::_SetSlices, this );
636 this->Slices[ 0 ][ 2 ]->SetSlicesCommand( Self::_SetSlices, this );
639 // -------------------------------------------------------------------------
640 cpExtensions::Visualization::MPRActors::
645 // -------------------------------------------------------------------------
646 vtkImageData* cpExtensions::Visualization::MPRActors::
647 _Image( unsigned int i ) const
650 if( i < this->ImageMaps.size( ) )
652 vtkAlgorithm* algo = this->ImageMaps[ i ]->GetInputAlgorithm( );
653 vtkInformation* info = algo->GetOutputInformation( 0 );
655 vtkImageData::SafeDownCast(
656 info->Get( vtkDataObject::DATA_OBJECT( ) )
665 // -------------------------------------------------------------------------
666 void cpExtensions::Visualization::MPRActors::
667 _Update( unsigned int i )
670 // Check if the input has been configured
671 vtkImageData* image = this->_Image( i );
674 this->ImageMaps[ i ]->Update( );
676 for( int j = 0; j < 2; ++j )
678 for( int k = 0; k < 3; ++k )
680 this->Slices[ j ][ k ]->AddInputConnection(
681 this->ImageMaps[ i ]->GetOutputPort( ), k
683 this->Slices[ j ][ k ]->UpdateText( );
685 // Add all of slice's props
686 this->Slices[ j ][ k ]->InitTraversal( );
687 vtkProp* prop = this->Slices[ j ][ k ]->GetNextProp( );
688 while( prop != NULL )
690 this->AddItem( prop );
691 prop = this->Slices[ j ][ k ]->GetNextProp( );
703 image->GetBounds( bb );
705 vtkSmartPointer< vtkOutlineSource > img_ol =
706 vtkSmartPointer< vtkOutlineSource >::New( );
707 img_ol->SetBounds( bb );
709 vtkSmartPointer< vtkPolyDataMapper > img_ol_mapper =
710 vtkSmartPointer< vtkPolyDataMapper >::New( );
711 img_ol_mapper->SetInputConnection( img_ol->GetOutputPort( ) );
712 this->ImageOutlineActor->SetMapper( img_ol_mapper );
713 this->ImageOutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
714 this->ImageOutlineActor->GetProperty( )->SetLineWidth( 1 );
716 this->AddItem( this->ImageOutlineActor );
722 // -------------------------------------------------------------------------
723 void cpExtensions::Visualization::MPRActors::
724 _SetSlices( double* pos, int axis, void* data )
726 MPRActors* actors = reinterpret_cast< MPRActors* >( data );
729 for( unsigned int j = 0; j < 3; ++j )
730 if( actors->Slices[ 0 ][ j ]->GetAxis( ) != axis )
731 actors->Slices[ 0 ][ j ]->SetSlice( pos );