1 #include <cpExtensions/Visualization/MPRActors.h>
5 #include <vtkAlgorithmOutput.h>
6 #include <vtkImageData.h>
7 #include <vtkInformation.h>
8 #include <vtkLookupTable.h>
9 #include <vtkOutlineSource.h>
10 #include <vtkProperty.h>
11 #include <vtkRenderer.h>
12 #include <vtkWindowLevelLookupTable.h>
14 // -------------------------------------------------------------------------
15 cpExtensions::Visualization::MPRActors*
16 cpExtensions::Visualization::MPRActors::
19 return( new Self( ) );
22 // -------------------------------------------------------------------------
23 cpExtensions::Visualization::
24 ImageSliceActors* cpExtensions::Visualization::MPRActors::
25 GetSliceActors( const int& i ) const
28 return( this->Slices[ 0 ][ i ] );
33 // -------------------------------------------------------------------------
34 int cpExtensions::Visualization::MPRActors::
35 AddInputConnection( vtkAlgorithmOutput* aout )
37 int N = this->ImageMaps.size( );
40 this->ImageMaps.push_back(
41 vtkSmartPointer< vtkImageMapToColors >::New( )
43 this->ImageMaps[ 0 ]->SetInputConnection( aout );
44 this->SetLookupTableToWindowLevel( 0 );
46 this->ResetWindowLevel( 0 );
51 // Check if the image share the same space
52 vtkImageData* ref_image = this->_Image( 0 );
53 vtkImageData* new_image =
54 vtkImageData::SafeDownCast(
55 aout->GetProducer( )->GetOutputInformation( 0 )->
56 Get( vtkDataObject::DATA_OBJECT( ) )
58 int ref_ext[ 6 ], new_ext[ 6 ];
59 ref_image->GetExtent( ref_ext );
60 new_image->GetExtent( new_ext );
62 ref_ext[ 0 ] == new_ext[ 0 ] && ref_ext[ 1 ] == new_ext[ 1 ] &&
63 ref_ext[ 2 ] == new_ext[ 2 ] && ref_ext[ 3 ] == new_ext[ 3 ] &&
64 ref_ext[ 4 ] == new_ext[ 4 ] && ref_ext[ 5 ] == new_ext[ 5 ]
67 this->ImageMaps.push_back(
68 vtkSmartPointer< vtkImageMapToColors >::New( )
70 this->ImageMaps[ N ]->SetInputConnection( aout );
71 this->SetLookupTableToColor(
72 N, double( 1 ), double( 0 ), double( 0 )
83 // -------------------------------------------------------------------------
84 int cpExtensions::Visualization::MPRActors::
85 AddInputData( vtkImageData* image )
87 int N = this->ImageMaps.size( );
90 this->ImageMaps.push_back(
91 vtkSmartPointer< vtkImageMapToColors >::New( )
93 this->ImageMaps[ 0 ]->SetInputData( image );
94 this->SetLookupTableToWindowLevel( 0 );
96 this->ResetWindowLevel( 0 );
101 // Check if the image share the same space
102 vtkImageData* ref_image = this->_Image( 0 );
103 vtkImageData* new_image = image;
104 int ref_ext[ 6 ], new_ext[ 6 ];
105 ref_image->GetExtent( ref_ext );
106 new_image->GetExtent( new_ext );
108 ref_ext[ 0 ] == new_ext[ 0 ] && ref_ext[ 1 ] == new_ext[ 1 ] &&
109 ref_ext[ 2 ] == new_ext[ 2 ] && ref_ext[ 3 ] == new_ext[ 3 ] &&
110 ref_ext[ 4 ] == new_ext[ 4 ] && ref_ext[ 5 ] == new_ext[ 5 ]
113 this->ImageMaps.push_back(
114 vtkSmartPointer< vtkImageMapToColors >::New( )
116 this->ImageMaps[ N ]->SetInputData( image );
117 this->SetLookupTableToColor(
118 N, double( 1 ), double( 0 ), double( 0 )
129 // -------------------------------------------------------------------------
130 void cpExtensions::Visualization::MPRActors::
131 PushDataInto( vtkRenderer* x, vtkRenderer* y, vtkRenderer* z, vtkRenderer* w )
133 vtkRenderer* rends[] = { x, y, z };
134 for( int i = 0; i < 3; ++i )
136 if( rends[ i ] != NULL )
140 k < this->Slices[ 0 ][ i ]->GetNumberOfImageActors( );
143 rends[ i ]->AddActor( this->Slices[ 0 ][ i ]->GetImageActor( k ) );
144 rends[ i ]->AddActor( this->Slices[ 0 ][ i ]->GetTextActor( ) );
145 for( int j = 0; j < 3; ++j )
146 rends[ i ]->AddActor( this->Slices[ 0 ][ j ]->GetPlaneActor( ) );
153 k < this->Slices[ 1 ][ i ]->GetNumberOfImageActors( );
156 w->AddActor( this->Slices[ 1 ][ i ]->GetImageActor( k ) );
157 w->AddActor( this->Slices[ 1 ][ i ]->GetPlaneActor( ) );
163 w->AddActor( this->ImageOutlineActor );
166 // -------------------------------------------------------------------------
167 void cpExtensions::Visualization::MPRActors::
175 vtkRenderer* rends[] = { x, y, z };
176 for( int i = 0; i < 3; ++i )
178 if( rends[ i ] != NULL )
182 k < this->Slices[ 0 ][ i ]->GetNumberOfImageActors( );
185 rends[ i ]->RemoveActor( this->Slices[ 0 ][ i ]->GetImageActor( k ) );
186 rends[ i ]->RemoveActor( this->Slices[ 0 ][ i ]->GetTextActor( ) );
187 for( int j = 0; j < 3; ++j )
188 rends[ i ]->RemoveActor( this->Slices[ 0 ][ j ]->GetPlaneActor( ) );
195 k < this->Slices[ 1 ][ i ]->GetNumberOfImageActors( );
198 w->RemoveActor( this->Slices[ 1 ][ i ]->GetImageActor( k ) );
199 w->RemoveActor( this->Slices[ 1 ][ i ]->GetPlaneActor( ) );
205 w->RemoveActor( this->ImageOutlineActor );
208 // -------------------------------------------------------------------------
209 void cpExtensions::Visualization::MPRActors::
217 // -------------------------------------------------------------------------
218 void cpExtensions::Visualization::MPRActors::
219 SetLookupTable( unsigned int i, vtkScalarsToColors* lut )
221 if( i < this->ImageMaps.size( ) )
223 this->ImageMaps[ i ]->SetLookupTable( lut );
224 this->ImageMaps[ i ]->Update( );
230 // -------------------------------------------------------------------------
231 vtkScalarsToColors* cpExtensions::Visualization::MPRActors::
232 GetLookupTable( unsigned int i ) const
234 if( i < this->ImageMaps.size( ) )
235 return( this->ImageMaps[ i ]->GetLookupTable( ) );
240 // -------------------------------------------------------------------------
241 void cpExtensions::Visualization::MPRActors::
242 SetLookupTableToWindowLevel( unsigned int i )
244 // Check if the input has been configured
245 vtkImageData* image = this->_Image( i );
250 image->GetScalarRange( r );
252 vtkSmartPointer< vtkWindowLevelLookupTable > lut =
253 vtkSmartPointer< vtkWindowLevelLookupTable >::New( );
254 lut->SetScaleToLinear( );
255 lut->SetTableRange( r );
258 this->SetLookupTable( i, lut );
261 // -------------------------------------------------------------------------
262 double cpExtensions::Visualization::MPRActors::
263 GetMinWindow( unsigned int i ) const
268 // -------------------------------------------------------------------------
269 double cpExtensions::Visualization::MPRActors::
270 GetMaxWindow( unsigned int i ) const
272 // Check if the input has been configured
273 vtkImageData* image = this->_Image( i );
275 return( double( 0 ) );
278 image->GetScalarRange( r );
279 return( r[ 1 ] - r[ 0 ] );
282 // -------------------------------------------------------------------------
283 double cpExtensions::Visualization::MPRActors::
284 GetMinLevel( unsigned int i ) const
286 // Check if the input has been configured
287 vtkImageData* image = this->_Image( i );
289 return( double( 0 ) );
292 image->GetScalarRange( r );
296 // -------------------------------------------------------------------------
297 double cpExtensions::Visualization::MPRActors::
298 GetMaxLevel( unsigned int i ) const
300 // Check if the input has been configured
301 vtkImageData* image = this->_Image( i );
303 return( double( 0 ) );
306 image->GetScalarRange( r );
310 // -------------------------------------------------------------------------
311 double cpExtensions::Visualization::MPRActors::
312 GetWindow( unsigned int i ) const
314 vtkWindowLevelLookupTable* lut =
315 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
317 return( lut->GetWindow( ) );
319 return( double( 0 ) );
322 // -------------------------------------------------------------------------
323 double cpExtensions::Visualization::MPRActors::
324 GetLevel( unsigned int i ) const
326 vtkWindowLevelLookupTable* lut =
327 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
329 return( lut->GetLevel( ) );
331 return( double( 0 ) );
334 // -------------------------------------------------------------------------
335 void cpExtensions::Visualization::MPRActors::
336 SetWindow( unsigned int i, const double& w )
338 vtkWindowLevelLookupTable* lut =
339 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
344 this->ImageMaps[ i ]->Modified( );
350 // -------------------------------------------------------------------------
351 void cpExtensions::Visualization::MPRActors::
352 SetLevel( unsigned int i, const double& l )
354 vtkWindowLevelLookupTable* lut =
355 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
360 this->ImageMaps[ i ]->Modified( );
366 // -------------------------------------------------------------------------
367 void cpExtensions::Visualization::MPRActors::
368 SetWindowLevel( unsigned int i, const double& w, const double& l )
370 vtkWindowLevelLookupTable* lut =
371 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
377 this->ImageMaps[ i ]->Modified( );
379 for( unsigned int j = 0; j < 3; ++j )
380 this->Slices[ 0 ][ j ]->UpdateText( w, l );
387 // -------------------------------------------------------------------------
388 void cpExtensions::Visualization::MPRActors::
389 ResetWindowLevel( unsigned int i )
391 vtkImageData* image = this->_Image( i );
392 vtkWindowLevelLookupTable* lut =
393 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
394 if( image != NULL && lut != NULL )
397 image->GetScalarRange( r );
398 lut->SetTableRange( r );
399 lut->SetWindow( r[ 1 ] - r[ 0 ] );
400 lut->SetLevel( ( r[ 1 ] + r[ 0 ] ) / double( 2 ) );
402 this->ImageMaps[ i ]->Modified( );
408 // -------------------------------------------------------------------------
409 void cpExtensions::Visualization::MPRActors::
410 SetLookupTableToColor(
411 unsigned int i, const double& r, const double& g, const double& b
414 static const double _0 = double( 0 );
415 static const double _1 = double( 1 );
416 static const double _2 = double( 2 );
417 static const double _4 = double( 4 );
418 static const double _6 = double( 6 );
419 static const double _OPACITY = double( 0.6 );
421 // Check if the input has been configured
422 vtkImageData* image = this->_Image( i );
427 image->GetScalarRange( range );
429 // Get HSV from display color
430 double cmax = ( r > g )? r: g; cmax = ( b > cmax )? b: cmax;
431 double cmin = ( r < g )? r: g; cmin = ( b < cmin )? b: cmin;
432 double d = cmax - cmin;
434 double saturation = ( std::fabs( cmax ) > _0 )? d / cmax: _0;
440 hue = std::fmod( ( g - b ) / d, _6 );
442 hue = ( ( b - r ) / d ) + _2;
444 hue = ( ( r - g ) / d ) + _4;
449 // Define new lookup table
450 vtkSmartPointer< vtkLookupTable > lut =
451 vtkSmartPointer< vtkLookupTable >::New( );
452 lut->SetScaleToLinear( );
454 lut->SetAboveRangeColor( _0, _0, _0, _0 );
455 lut->SetBelowRangeColor( _0, _0, _0, _0 );
457 lut->SetNanColor( _0, _0, _0, _0 );
458 lut->SetTableRange( range[ 0 ], range[ 1 ] );
459 lut->SetAlphaRange( _0, _OPACITY );
460 lut->SetHueRange( _0, hue );
461 lut->SetSaturationRange( _0, saturation );
462 lut->SetValueRange( _0, value );
465 this->SetLookupTable( i, lut );
468 // -------------------------------------------------------------------------
469 int cpExtensions::Visualization::MPRActors::
470 GetSliceNumberMinValue( const int& axis ) const
472 return( this->Slices[ 0 ][ axis ]->GetSliceNumberMinValue( ) );
475 // -------------------------------------------------------------------------
476 int cpExtensions::Visualization::MPRActors::
477 GetSliceNumberMaxValue( const int& axis ) const
479 return( this->Slices[ 0 ][ axis ]->GetSliceNumberMaxValue( ) );
482 // -------------------------------------------------------------------------
483 int cpExtensions::Visualization::MPRActors::
484 GetSlice( const int& axis ) const
486 return( this->Slices[ 0 ][ axis ]->GetSliceNumber( ) );
489 // -------------------------------------------------------------------------
490 void cpExtensions::Visualization::MPRActors::
491 SetSlice( const int& axis, const int& slice )
493 vtkImageData* image = this->_Image( 0 );
497 // Get image data extent
499 image->GetExtent( ext );
501 // Check if the slice is valid
503 if( slice < ext[ axis << 1 ] )
504 real = ext[ axis << 1 ];
505 if( ext[ ( axis << 1 ) + 1 ] < slice )
506 real = ext[ ( axis << 1 ) + 1 ];
509 this->Slices[ 0 ][ axis ]->SetSliceNumber( real );
510 this->Slices[ 1 ][ axis ]->SetSliceNumber( real );
514 // -------------------------------------------------------------------------
515 void cpExtensions::Visualization::MPRActors::
516 SetSlice( const int& axis, const double& slice )
518 vtkImageData* image = this->_Image( 0 );
522 double x[ 3 ] = { double( 0 ) };
527 image->ComputeStructuredCoordinates( x, ijk, pcoords );
528 this->SetSlice( axis, ijk[ axis ] );
531 // -------------------------------------------------------------------------
532 void cpExtensions::Visualization::MPRActors::
535 for( unsigned int i = 0; i < 2; ++i )
536 for( unsigned int j = 0; j < 3; ++j )
537 this->Slices[ i ][ j ]->SetSliceNumber(
538 this->Slices[ i ][ j ]->GetSliceNumberMinValue( )
542 // -------------------------------------------------------------------------
543 cpExtensions::Visualization::MPRActors::
547 this->ImageOutlineActor = vtkSmartPointer< vtkActor >::New( );
548 for( unsigned int i = 0; i < 2; ++i )
549 for( unsigned int j = 0; j < 3; ++j )
550 this->Slices[ i ][ j ] = vtkSmartPointer< ImageSliceActors >::New( );
553 // -------------------------------------------------------------------------
554 cpExtensions::Visualization::MPRActors::
559 // -------------------------------------------------------------------------
560 vtkImageData* cpExtensions::Visualization::MPRActors::
561 _Image( unsigned int i ) const
563 if( i < this->ImageMaps.size( ) )
565 vtkAlgorithm* algo = this->ImageMaps[ i ]->GetInputAlgorithm( );
566 vtkInformation* info = algo->GetOutputInformation( 0 );
568 vtkImageData::SafeDownCast(
569 info->Get( vtkDataObject::DATA_OBJECT( ) )
577 // -------------------------------------------------------------------------
578 void cpExtensions::Visualization::MPRActors::
579 _Update( unsigned int i )
581 // Check if the input has been configured
582 vtkImageData* image = this->_Image( i );
585 this->ImageMaps[ i ]->Update( );
587 for( int j = 0; j < 2; ++j )
589 for( int k = 0; k < 3; ++k )
591 this->Slices[ j ][ k ]->AddInputConnection(
592 this->ImageMaps[ i ]->GetOutputPort( ), k
594 this->Slices[ j ][ k ]->UpdateText( );
596 // Add all of slice's props
597 this->Slices[ j ][ k ]->InitTraversal( );
598 vtkProp* prop = this->Slices[ j ][ k ]->GetNextProp( );
599 while( prop != NULL )
601 this->AddItem( prop );
602 prop = this->Slices[ j ][ k ]->GetNextProp( );
614 image->GetBounds( bb );
616 vtkSmartPointer< vtkOutlineSource > img_ol =
617 vtkSmartPointer< vtkOutlineSource >::New( );
618 img_ol->SetBounds( bb );
620 vtkSmartPointer< vtkPolyDataMapper > img_ol_mapper =
621 vtkSmartPointer< vtkPolyDataMapper >::New( );
622 img_ol_mapper->SetInputConnection( img_ol->GetOutputPort( ) );
623 this->ImageOutlineActor->SetMapper( img_ol_mapper );
624 this->ImageOutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
625 this->ImageOutlineActor->GetProperty( )->SetLineWidth( 1 );
627 this->AddItem( this->ImageOutlineActor );