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( ) );
294 // -------------------------------------------------------------------------
295 void cpExtensions::Visualization::MPRActors::
296 SetLookupTableToWindowLevel( unsigned int i )
299 // Check if the input has been configured
300 vtkImageData* image = this->_Image( i );
305 image->GetScalarRange( r );
307 vtkSmartPointer< vtkWindowLevelLookupTable > lut =
308 vtkSmartPointer< vtkWindowLevelLookupTable >::New( );
309 lut->SetScaleToLinear( );
310 lut->SetTableRange( r );
313 this->SetLookupTable( i, lut );
317 // -------------------------------------------------------------------------
318 double cpExtensions::Visualization::MPRActors::
319 GetMinWindow( unsigned int i ) const
324 // -------------------------------------------------------------------------
325 double cpExtensions::Visualization::MPRActors::
326 GetMaxWindow( unsigned int i ) const
329 // Check if the input has been configured
330 vtkImageData* image = this->_Image( i );
332 return( double( 0 ) );
335 image->GetScalarRange( r );
336 return( r[ 1 ] - r[ 0 ] );
340 // -------------------------------------------------------------------------
341 double cpExtensions::Visualization::MPRActors::
342 GetMinLevel( unsigned int i ) const
345 // Check if the input has been configured
346 vtkImageData* image = this->_Image( i );
348 return( double( 0 ) );
351 image->GetScalarRange( r );
356 // -------------------------------------------------------------------------
357 double cpExtensions::Visualization::MPRActors::
358 GetMaxLevel( unsigned int i ) const
361 // Check if the input has been configured
362 vtkImageData* image = this->_Image( i );
364 return( double( 0 ) );
367 image->GetScalarRange( r );
372 // -------------------------------------------------------------------------
373 double cpExtensions::Visualization::MPRActors::
374 GetWindow( unsigned int i ) const
377 vtkWindowLevelLookupTable* lut =
378 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
380 return( lut->GetWindow( ) );
382 return( double( 0 ) );
386 // -------------------------------------------------------------------------
387 double cpExtensions::Visualization::MPRActors::
388 GetLevel( unsigned int i ) const
391 vtkWindowLevelLookupTable* lut =
392 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
394 return( lut->GetLevel( ) );
396 return( double( 0 ) );
400 // -------------------------------------------------------------------------
401 void cpExtensions::Visualization::MPRActors::
402 SetWindow( unsigned int i, const double& w )
405 vtkWindowLevelLookupTable* lut =
406 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
411 this->ImageMaps[ i ]->Modified( );
418 // -------------------------------------------------------------------------
419 void cpExtensions::Visualization::MPRActors::
420 SetLevel( unsigned int i, const double& l )
423 vtkWindowLevelLookupTable* lut =
424 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
429 this->ImageMaps[ i ]->Modified( );
436 // -------------------------------------------------------------------------
437 void cpExtensions::Visualization::MPRActors::
438 SetWindowLevel( unsigned int i, const double& w, const double& l )
441 vtkWindowLevelLookupTable* lut =
442 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
448 this->ImageMaps[ i ]->Modified( );
450 for( unsigned int j = 0; j < 3; ++j )
451 this->Slices[ 0 ][ j ]->UpdateText( w, l );
459 // -------------------------------------------------------------------------
460 void cpExtensions::Visualization::MPRActors::
461 ResetWindowLevel( unsigned int i )
464 vtkImageData* image = this->_Image( i );
465 vtkWindowLevelLookupTable* lut =
466 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
467 if( image != NULL && lut != NULL )
470 image->GetScalarRange( r );
471 lut->SetTableRange( r );
472 lut->SetWindow( r[ 1 ] - r[ 0 ] );
473 lut->SetLevel( ( r[ 1 ] + r[ 0 ] ) / double( 2 ) );
475 this->ImageMaps[ i ]->Modified( );
482 // -------------------------------------------------------------------------
483 void cpExtensions::Visualization::MPRActors::
484 SetLookupTableToColor(
485 unsigned int i, const double& r, const double& g, const double& b
489 static const double _0 = double( 0 );
490 static const double _1 = double( 1 );
491 static const double _2 = double( 2 );
492 static const double _4 = double( 4 );
493 static const double _6 = double( 6 );
494 static const double _OPACITY = double( 0.6 );
496 // Check if the input has been configured
497 vtkImageData* image = this->_Image( i );
502 image->GetScalarRange( range );
504 // Get HSV from display color
505 double cmax = ( r > g )? r: g; cmax = ( b > cmax )? b: cmax;
506 double cmin = ( r < g )? r: g; cmin = ( b < cmin )? b: cmin;
507 double d = cmax - cmin;
509 double saturation = ( std::fabs( cmax ) > _0 )? d / cmax: _0;
515 hue = std::fmod( ( g - b ) / d, _6 );
517 hue = ( ( b - r ) / d ) + _2;
519 hue = ( ( r - g ) / d ) + _4;
524 // Define new lookup table
525 vtkSmartPointer< vtkLookupTable > lut =
526 vtkSmartPointer< vtkLookupTable >::New( );
527 lut->SetScaleToLinear( );
528 lut->SetNanColor( _0, _0, _0, _0 );
529 lut->SetTableRange( range[ 0 ], range[ 1 ] );
530 lut->SetAlphaRange( _0, _OPACITY );
531 lut->SetHueRange( _0, hue );
532 lut->SetSaturationRange( _0, saturation );
533 lut->SetValueRange( _0, value );
536 this->SetLookupTable( i, lut );
540 // -------------------------------------------------------------------------
541 int cpExtensions::Visualization::MPRActors::
542 GetSliceNumberMinValue( const int& axis ) const
544 return( this->Slices[ 0 ][ axis ]->GetSliceNumberMinValue( ) );
547 // -------------------------------------------------------------------------
548 int cpExtensions::Visualization::MPRActors::
549 GetSliceNumberMaxValue( const int& axis ) const
551 return( this->Slices[ 0 ][ axis ]->GetSliceNumberMaxValue( ) );
554 // -------------------------------------------------------------------------
555 int cpExtensions::Visualization::MPRActors::
556 GetSlice( const int& axis ) const
558 return( this->Slices[ 0 ][ axis ]->GetSliceNumber( ) );
561 // -------------------------------------------------------------------------
562 void cpExtensions::Visualization::MPRActors::
563 SetSlice( const int& axis, const int& slice )
565 vtkImageData* image = this->_Image( 0 );
569 // Get image data extent
571 image->GetExtent( ext );
573 // Check if the slice is valid
575 if( slice < ext[ axis << 1 ] )
576 real = ext[ axis << 1 ];
577 if( ext[ ( axis << 1 ) + 1 ] < slice )
578 real = ext[ ( axis << 1 ) + 1 ];
581 this->Slices[ 0 ][ axis ]->SetSliceNumber( real );
582 this->Slices[ 1 ][ axis ]->SetSliceNumber( real );
586 // -------------------------------------------------------------------------
587 void cpExtensions::Visualization::MPRActors::
588 SetSlice( const int& axis, const double& slice )
590 vtkImageData* image = this->_Image( 0 );
594 double x[ 3 ] = { double( 0 ) };
599 image->ComputeStructuredCoordinates( x, ijk, pcoords );
600 this->SetSlice( axis, ijk[ axis ] );
603 // -------------------------------------------------------------------------
604 void cpExtensions::Visualization::MPRActors::
607 for( unsigned int i = 0; i < 2; ++i )
608 for( unsigned int j = 0; j < 3; ++j )
609 this->Slices[ i ][ j ]->SetSliceNumber(
610 this->Slices[ i ][ j ]->GetSliceNumberMinValue( )
614 // -------------------------------------------------------------------------
615 cpExtensions::Visualization::MPRActors::
619 this->ImageOutlineActor = vtkSmartPointer< vtkActor >::New( );
620 for( unsigned int i = 0; i < 2; ++i )
621 for( unsigned int j = 0; j < 3; ++j )
622 this->Slices[ i ][ j ] = vtkSmartPointer< ImageSliceActors >::New( );
624 this->Slices[ 0 ][ 0 ]->AssociateSlice( this->Slices[ 1 ][ 0 ] );
625 this->Slices[ 0 ][ 1 ]->AssociateSlice( this->Slices[ 1 ][ 1 ] );
626 this->Slices[ 0 ][ 2 ]->AssociateSlice( this->Slices[ 1 ][ 2 ] );
628 this->Slices[ 0 ][ 0 ]->SetSlicesCommand( Self::_SetSlices, this );
629 this->Slices[ 0 ][ 1 ]->SetSlicesCommand( Self::_SetSlices, this );
630 this->Slices[ 0 ][ 2 ]->SetSlicesCommand( Self::_SetSlices, this );
633 // -------------------------------------------------------------------------
634 cpExtensions::Visualization::MPRActors::
639 // -------------------------------------------------------------------------
640 vtkImageData* cpExtensions::Visualization::MPRActors::
641 _Image( unsigned int i ) const
644 if( i < this->ImageMaps.size( ) )
646 vtkAlgorithm* algo = this->ImageMaps[ i ]->GetInputAlgorithm( );
647 vtkInformation* info = algo->GetOutputInformation( 0 );
649 vtkImageData::SafeDownCast(
650 info->Get( vtkDataObject::DATA_OBJECT( ) )
659 // -------------------------------------------------------------------------
660 void cpExtensions::Visualization::MPRActors::
661 _Update( unsigned int i )
664 // Check if the input has been configured
665 vtkImageData* image = this->_Image( i );
668 this->ImageMaps[ i ]->Update( );
670 for( int j = 0; j < 2; ++j )
672 for( int k = 0; k < 3; ++k )
674 this->Slices[ j ][ k ]->AddInputConnection(
675 this->ImageMaps[ i ]->GetOutputPort( ), k
677 this->Slices[ j ][ k ]->UpdateText( );
679 // Add all of slice's props
680 this->Slices[ j ][ k ]->InitTraversal( );
681 vtkProp* prop = this->Slices[ j ][ k ]->GetNextProp( );
682 while( prop != NULL )
684 this->AddItem( prop );
685 prop = this->Slices[ j ][ k ]->GetNextProp( );
697 image->GetBounds( bb );
699 vtkSmartPointer< vtkOutlineSource > img_ol =
700 vtkSmartPointer< vtkOutlineSource >::New( );
701 img_ol->SetBounds( bb );
703 vtkSmartPointer< vtkPolyDataMapper > img_ol_mapper =
704 vtkSmartPointer< vtkPolyDataMapper >::New( );
705 img_ol_mapper->SetInputConnection( img_ol->GetOutputPort( ) );
706 this->ImageOutlineActor->SetMapper( img_ol_mapper );
707 this->ImageOutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
708 this->ImageOutlineActor->GetProperty( )->SetLineWidth( 1 );
710 this->AddItem( this->ImageOutlineActor );
716 // -------------------------------------------------------------------------
717 void cpExtensions::Visualization::MPRActors::
718 _SetSlices( double* pos, int axis, void* data )
720 MPRActors* actors = reinterpret_cast< MPRActors* >( data );
723 for( unsigned int j = 0; j < 3; ++j )
724 if( actors->Slices[ 0 ][ j ]->GetAxis( ) != axis )
725 actors->Slices[ 0 ][ j ]->SetSlice( pos );