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 );
198 // -------------------------------------------------------------------------
199 void cpExtensions::Visualization::MPRActors::
208 vtkRenderer* rends[] = { x, y, z };
209 for( int i = 0; i < 3; ++i )
211 if( rends[ i ] != NULL )
215 k < this->Slices[ 0 ][ i ]->GetNumberOfImageActors( );
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( ) );
228 k < this->Slices[ 1 ][ i ]->GetNumberOfImageActors( );
231 w->RemoveActor( this->Slices[ 1 ][ i ]->GetImageActor( k ) );
232 w->RemoveActor( this->Slices[ 1 ][ i ]->GetPlaneActor( ) );
238 w->RemoveActor( this->ImageOutlineActor );
242 // -------------------------------------------------------------------------
244 void cpExtensions::Visualization::MPRActors::
253 // -------------------------------------------------------------------------
254 void cpExtensions::Visualization::MPRActors::
255 SetLookupTable( unsigned int i, vtkScalarsToColors* lut )
258 if( i < this->ImageMaps.size( ) )
260 this->ImageMaps[ i ]->SetLookupTable( lut );
261 this->ImageMaps[ i ]->Update( );
268 // -------------------------------------------------------------------------
269 vtkScalarsToColors* cpExtensions::Visualization::MPRActors::
270 GetLookupTable( unsigned int i ) const
273 if( i < this->ImageMaps.size( ) )
274 return( this->ImageMaps[ i ]->GetLookupTable( ) );
280 // -------------------------------------------------------------------------
281 void cpExtensions::Visualization::MPRActors::
282 SetLookupTableToWindowLevel( unsigned int i )
285 // Check if the input has been configured
286 vtkImageData* image = this->_Image( i );
291 image->GetScalarRange( r );
293 vtkSmartPointer< vtkWindowLevelLookupTable > lut =
294 vtkSmartPointer< vtkWindowLevelLookupTable >::New( );
295 lut->SetScaleToLinear( );
296 lut->SetTableRange( r );
299 this->SetLookupTable( i, lut );
303 // -------------------------------------------------------------------------
304 double cpExtensions::Visualization::MPRActors::
305 GetMinWindow( unsigned int i ) const
310 // -------------------------------------------------------------------------
311 double cpExtensions::Visualization::MPRActors::
312 GetMaxWindow( unsigned int i ) const
315 // Check if the input has been configured
316 vtkImageData* image = this->_Image( i );
318 return( double( 0 ) );
321 image->GetScalarRange( r );
322 return( r[ 1 ] - r[ 0 ] );
326 // -------------------------------------------------------------------------
327 double cpExtensions::Visualization::MPRActors::
328 GetMinLevel( unsigned int i ) const
331 // Check if the input has been configured
332 vtkImageData* image = this->_Image( i );
334 return( double( 0 ) );
337 image->GetScalarRange( r );
342 // -------------------------------------------------------------------------
343 double cpExtensions::Visualization::MPRActors::
344 GetMaxLevel( 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 );
358 // -------------------------------------------------------------------------
359 double cpExtensions::Visualization::MPRActors::
360 GetWindow( unsigned int i ) const
363 vtkWindowLevelLookupTable* lut =
364 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
366 return( lut->GetWindow( ) );
368 return( double( 0 ) );
372 // -------------------------------------------------------------------------
373 double cpExtensions::Visualization::MPRActors::
374 GetLevel( unsigned int i ) const
377 vtkWindowLevelLookupTable* lut =
378 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
380 return( lut->GetLevel( ) );
382 return( double( 0 ) );
386 // -------------------------------------------------------------------------
387 void cpExtensions::Visualization::MPRActors::
388 SetWindow( unsigned int i, const double& w )
391 vtkWindowLevelLookupTable* lut =
392 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
397 this->ImageMaps[ i ]->Modified( );
404 // -------------------------------------------------------------------------
405 void cpExtensions::Visualization::MPRActors::
406 SetLevel( unsigned int i, const double& l )
409 vtkWindowLevelLookupTable* lut =
410 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
415 this->ImageMaps[ i ]->Modified( );
422 // -------------------------------------------------------------------------
423 void cpExtensions::Visualization::MPRActors::
424 SetWindowLevel( unsigned int i, const double& w, const double& l )
427 vtkWindowLevelLookupTable* lut =
428 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
434 this->ImageMaps[ i ]->Modified( );
436 for( unsigned int j = 0; j < 3; ++j )
437 this->Slices[ 0 ][ j ]->UpdateText( w, l );
445 // -------------------------------------------------------------------------
446 void cpExtensions::Visualization::MPRActors::
447 ResetWindowLevel( unsigned int i )
450 vtkImageData* image = this->_Image( i );
451 vtkWindowLevelLookupTable* lut =
452 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
453 if( image != NULL && lut != NULL )
456 image->GetScalarRange( r );
457 lut->SetTableRange( r );
458 lut->SetWindow( r[ 1 ] - r[ 0 ] );
459 lut->SetLevel( ( r[ 1 ] + r[ 0 ] ) / double( 2 ) );
461 this->ImageMaps[ i ]->Modified( );
468 // -------------------------------------------------------------------------
469 void cpExtensions::Visualization::MPRActors::
470 SetLookupTableToColor(
471 unsigned int i, const double& r, const double& g, const double& b
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 );
482 // Check if the input has been configured
483 vtkImageData* image = this->_Image( i );
488 image->GetScalarRange( range );
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;
495 double saturation = ( std::fabs( cmax ) > _0 )? d / cmax: _0;
501 hue = std::fmod( ( g - b ) / d, _6 );
503 hue = ( ( b - r ) / d ) + _2;
505 hue = ( ( r - g ) / d ) + _4;
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 );
522 this->SetLookupTable( i, lut );
526 // -------------------------------------------------------------------------
527 int cpExtensions::Visualization::MPRActors::
528 GetSliceNumberMinValue( const int& axis ) const
530 return( this->Slices[ 0 ][ axis ]->GetSliceNumberMinValue( ) );
533 // -------------------------------------------------------------------------
534 int cpExtensions::Visualization::MPRActors::
535 GetSliceNumberMaxValue( const int& axis ) const
537 return( this->Slices[ 0 ][ axis ]->GetSliceNumberMaxValue( ) );
540 // -------------------------------------------------------------------------
541 int cpExtensions::Visualization::MPRActors::
542 GetSlice( const int& axis ) const
544 return( this->Slices[ 0 ][ axis ]->GetSliceNumber( ) );
547 // -------------------------------------------------------------------------
548 void cpExtensions::Visualization::MPRActors::
549 SetSlice( const int& axis, const int& slice )
551 vtkImageData* image = this->_Image( 0 );
555 // Get image data extent
557 image->GetExtent( ext );
559 // Check if the slice is valid
561 if( slice < ext[ axis << 1 ] )
562 real = ext[ axis << 1 ];
563 if( ext[ ( axis << 1 ) + 1 ] < slice )
564 real = ext[ ( axis << 1 ) + 1 ];
567 this->Slices[ 0 ][ axis ]->SetSliceNumber( real );
568 this->Slices[ 1 ][ axis ]->SetSliceNumber( real );
572 // -------------------------------------------------------------------------
573 void cpExtensions::Visualization::MPRActors::
574 SetSlice( const int& axis, const double& slice )
576 vtkImageData* image = this->_Image( 0 );
580 double x[ 3 ] = { double( 0 ) };
585 image->ComputeStructuredCoordinates( x, ijk, pcoords );
586 this->SetSlice( axis, ijk[ axis ] );
589 // -------------------------------------------------------------------------
590 void cpExtensions::Visualization::MPRActors::
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( )
600 // -------------------------------------------------------------------------
601 cpExtensions::Visualization::MPRActors::
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( );
611 // -------------------------------------------------------------------------
612 cpExtensions::Visualization::MPRActors::
617 // -------------------------------------------------------------------------
618 vtkImageData* cpExtensions::Visualization::MPRActors::
619 _Image( unsigned int i ) const
622 if( i < this->ImageMaps.size( ) )
624 vtkAlgorithm* algo = this->ImageMaps[ i ]->GetInputAlgorithm( );
625 vtkInformation* info = algo->GetOutputInformation( 0 );
627 vtkImageData::SafeDownCast(
628 info->Get( vtkDataObject::DATA_OBJECT( ) )
637 // -------------------------------------------------------------------------
638 void cpExtensions::Visualization::MPRActors::
639 _Update( unsigned int i )
642 // Check if the input has been configured
643 vtkImageData* image = this->_Image( i );
646 this->ImageMaps[ i ]->Update( );
648 for( int j = 0; j < 2; ++j )
650 for( int k = 0; k < 3; ++k )
652 this->Slices[ j ][ k ]->AddInputConnection(
653 this->ImageMaps[ i ]->GetOutputPort( ), k
655 this->Slices[ j ][ k ]->UpdateText( );
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 )
662 this->AddItem( prop );
663 prop = this->Slices[ j ][ k ]->GetNextProp( );
675 image->GetBounds( bb );
677 vtkSmartPointer< vtkOutlineSource > img_ol =
678 vtkSmartPointer< vtkOutlineSource >::New( );
679 img_ol->SetBounds( bb );
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 );
688 this->AddItem( this->ImageOutlineActor );