1 #include <cpExtensions/Visualization/MPRActors.h>
3 #include <vtkAlgorithmOutput.h>
4 #include <vtkImageData.h>
5 #include <vtkInformation.h>
6 #include <vtkOutlineSource.h>
7 #include <vtkProperty.h>
8 #include <vtkRenderer.h>
9 #include <vtkWindowLevelLookupTable.h>
11 // -------------------------------------------------------------------------
12 cpExtensions::Visualization::MPRActors*
13 cpExtensions::Visualization::MPRActors::
16 return( new Self( ) );
19 // -------------------------------------------------------------------------
20 cpExtensions::Visualization::
21 ImageSliceActors* cpExtensions::Visualization::MPRActors::
22 GetSliceActors( const int& i ) const
25 return( this->Slices[ 0 ][ i ] );
30 // -------------------------------------------------------------------------
31 int cpExtensions::Visualization::MPRActors::
32 AddInputConnection( vtkAlgorithmOutput* aout )
34 int N = this->ImageMaps.size( );
37 this->ImageMaps.push_back(
38 vtkSmartPointer< vtkImageMapToColors >::New( )
40 this->ImageMaps[ 0 ]->SetInputConnection( aout );
41 this->SetLookupTableToWindowLevel( 0 );
47 // Check if the image share the same space
48 vtkImageData* ref_image = this->_Image( 0 );
49 vtkImageData* new_image =
50 vtkImageData::SafeDownCast(
51 aout->GetProducer( )->GetOutputInformation( 0 )->
52 Get( vtkDataObject::DATA_OBJECT( ) )
54 int ref_ext[ 6 ], new_ext[ 6 ];
55 ref_image->GetExtent( ref_ext );
56 new_image->GetExtent( new_ext );
58 ref_ext[ 0 ] == new_ext[ 0 ] && ref_ext[ 1 ] == new_ext[ 1 ] &&
59 ref_ext[ 2 ] == new_ext[ 2 ] && ref_ext[ 3 ] == new_ext[ 3 ] &&
60 ref_ext[ 4 ] == new_ext[ 4 ] && ref_ext[ 5 ] == new_ext[ 5 ]
63 this->ImageMaps.push_back(
64 vtkSmartPointer< vtkImageMapToColors >::New( )
66 this->ImageMaps[ N ]->SetInputConnection( aout );
67 this->SetLookupTableToWindowLevel( N );
77 // -------------------------------------------------------------------------
78 int cpExtensions::Visualization::MPRActors::
79 AddInputData( vtkImageData* image )
81 int N = this->ImageMaps.size( );
84 this->ImageMaps.push_back(
85 vtkSmartPointer< vtkImageMapToColors >::New( )
87 this->ImageMaps[ 0 ]->SetInputData( image );
88 this->SetLookupTableToWindowLevel( 0 );
94 // Check if the image share the same space
95 vtkImageData* ref_image = this->_Image( 0 );
96 vtkImageData* new_image = image;
97 int ref_ext[ 6 ], new_ext[ 6 ];
98 ref_image->GetExtent( ref_ext );
99 new_image->GetExtent( new_ext );
101 ref_ext[ 0 ] == new_ext[ 0 ] && ref_ext[ 1 ] == new_ext[ 1 ] &&
102 ref_ext[ 2 ] == new_ext[ 2 ] && ref_ext[ 3 ] == new_ext[ 3 ] &&
103 ref_ext[ 4 ] == new_ext[ 4 ] && ref_ext[ 5 ] == new_ext[ 5 ]
106 this->ImageMaps.push_back(
107 vtkSmartPointer< vtkImageMapToColors >::New( )
109 this->ImageMaps[ N ]->SetInputData( image );
110 this->SetLookupTableToWindowLevel( N );
120 // -------------------------------------------------------------------------
121 void cpExtensions::Visualization::MPRActors::
122 PushDataInto( vtkRenderer* x, vtkRenderer* y, vtkRenderer* z, vtkRenderer* w )
124 vtkRenderer* rends[] = { x, y, z };
125 for( int i = 0; i < 3; ++i )
127 if( rends[ i ] != NULL )
131 k < this->Slices[ 0 ][ i ]->GetNumberOfImageActors( );
134 rends[ i ]->AddActor( this->Slices[ 0 ][ i ]->GetImageActor( k ) );
135 rends[ i ]->AddActor( this->Slices[ 0 ][ i ]->GetTextActor( ) );
136 for( int j = 0; j < 3; ++j )
137 rends[ i ]->AddActor( this->Slices[ 0 ][ j ]->GetPlaneActor( ) );
144 k < this->Slices[ 1 ][ i ]->GetNumberOfImageActors( );
147 w->AddActor( this->Slices[ 1 ][ i ]->GetImageActor( k ) );
148 w->AddActor( this->Slices[ 1 ][ i ]->GetPlaneActor( ) );
154 w->AddActor( this->ImageOutlineActor );
157 // -------------------------------------------------------------------------
158 void cpExtensions::Visualization::MPRActors::
166 vtkRenderer* rends[] = { x, y, z };
167 for( int i = 0; i < 3; ++i )
169 if( rends[ i ] != NULL )
173 k < this->Slices[ 0 ][ i ]->GetNumberOfImageActors( );
176 rends[ i ]->RemoveActor( this->Slices[ 0 ][ i ]->GetImageActor( k ) );
177 rends[ i ]->RemoveActor( this->Slices[ 0 ][ i ]->GetTextActor( ) );
178 for( int j = 0; j < 3; ++j )
179 rends[ i ]->RemoveActor( this->Slices[ 0 ][ j ]->GetPlaneActor( ) );
186 k < this->Slices[ 1 ][ i ]->GetNumberOfImageActors( );
189 w->RemoveActor( this->Slices[ 1 ][ i ]->GetImageActor( k ) );
190 w->RemoveActor( this->Slices[ 1 ][ i ]->GetPlaneActor( ) );
196 w->RemoveActor( this->ImageOutlineActor );
199 // -------------------------------------------------------------------------
200 void cpExtensions::Visualization::MPRActors::
201 SetLookupTable( unsigned int i, vtkScalarsToColors* lut )
203 if( i < this->ImageMaps.size( ) )
205 this->ImageMaps[ i ]->SetLookupTable( lut );
206 this->ImageMaps[ i ]->Update( );
212 // -------------------------------------------------------------------------
213 vtkScalarsToColors* cpExtensions::Visualization::MPRActors::
214 GetLookupTable( unsigned int i ) const
216 if( i < this->ImageMaps.size( ) )
217 return( this->ImageMaps[ i ]->GetLookupTable( ) );
222 // -------------------------------------------------------------------------
223 void cpExtensions::Visualization::MPRActors::
224 SetLookupTableToWindowLevel( unsigned int i )
226 // Check if the input has been configured
227 vtkImageData* image = this->_Image( i );
232 image->GetScalarRange( r );
234 vtkSmartPointer< vtkWindowLevelLookupTable > lut =
235 vtkSmartPointer< vtkWindowLevelLookupTable >::New( );
236 lut->SetScaleToLinear( );
237 lut->SetTableRange( r );
240 this->SetLookupTable( i, lut );
243 // -------------------------------------------------------------------------
244 double cpExtensions::Visualization::MPRActors::
245 GetMinWindow( unsigned int i ) const
250 // -------------------------------------------------------------------------
251 double cpExtensions::Visualization::MPRActors::
252 GetMaxWindow( unsigned int i ) const
254 // Check if the input has been configured
255 vtkImageData* image = this->_Image( i );
257 return( double( 0 ) );
260 image->GetScalarRange( r );
261 return( r[ 1 ] - r[ 0 ] );
264 // -------------------------------------------------------------------------
265 double cpExtensions::Visualization::MPRActors::
266 GetMinLevel( unsigned int i ) const
268 // Check if the input has been configured
269 vtkImageData* image = this->_Image( i );
271 return( double( 0 ) );
274 image->GetScalarRange( r );
278 // -------------------------------------------------------------------------
279 double cpExtensions::Visualization::MPRActors::
280 GetMaxLevel( unsigned int i ) const
282 // Check if the input has been configured
283 vtkImageData* image = this->_Image( i );
285 return( double( 0 ) );
288 image->GetScalarRange( r );
292 // -------------------------------------------------------------------------
293 double cpExtensions::Visualization::MPRActors::
294 GetWindow( unsigned int i ) const
296 vtkWindowLevelLookupTable* lut =
297 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
299 return( lut->GetWindow( ) );
301 return( double( 0 ) );
304 // -------------------------------------------------------------------------
305 double cpExtensions::Visualization::MPRActors::
306 GetLevel( unsigned int i ) const
308 vtkWindowLevelLookupTable* lut =
309 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
311 return( lut->GetLevel( ) );
313 return( double( 0 ) );
316 // -------------------------------------------------------------------------
317 void cpExtensions::Visualization::MPRActors::
318 SetWindow( unsigned int i, const double& w )
320 vtkWindowLevelLookupTable* lut =
321 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
326 this->ImageMaps[ i ]->Modified( );
332 // -------------------------------------------------------------------------
333 void cpExtensions::Visualization::MPRActors::
334 SetLevel( unsigned int i, const double& l )
336 vtkWindowLevelLookupTable* lut =
337 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
342 this->ImageMaps[ i ]->Modified( );
348 // -------------------------------------------------------------------------
349 void cpExtensions::Visualization::MPRActors::
350 ResetWindowLevel( unsigned int i )
352 vtkImageData* image = this->_Image( i );
353 vtkWindowLevelLookupTable* lut =
354 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
355 if( image != NULL && lut != NULL )
358 image->GetScalarRange( r );
359 lut->SetTableRange( r );
361 this->ImageMaps[ i ]->Modified( );
367 // -------------------------------------------------------------------------
368 int cpExtensions::Visualization::MPRActors::
369 GetSliceNumberMinValue( const int& axis ) const
371 return( this->Slices[ 0 ][ axis ]->GetSliceNumberMinValue( ) );
374 // -------------------------------------------------------------------------
375 int cpExtensions::Visualization::MPRActors::
376 GetSliceNumberMaxValue( const int& axis ) const
378 return( this->Slices[ 0 ][ axis ]->GetSliceNumberMaxValue( ) );
381 // -------------------------------------------------------------------------
382 int cpExtensions::Visualization::MPRActors::
383 GetSlice( const int& axis ) const
385 return( this->Slices[ 0 ][ axis ]->GetSliceNumber( ) );
388 // -------------------------------------------------------------------------
389 void cpExtensions::Visualization::MPRActors::
390 SetSlice( const int& axis, const int& slice )
392 vtkImageData* image = this->_Image( 0 );
396 // Get image data extent
398 image->GetExtent( ext );
400 // Check if the slice is valid
402 if( slice < ext[ axis << 1 ] )
403 real = ext[ axis << 1 ];
404 if( ext[ ( axis << 1 ) + 1 ] < slice )
405 real = ext[ ( axis << 1 ) + 1 ];
408 this->Slices[ 0 ][ axis ]->SetSliceNumber( real );
409 this->Slices[ 1 ][ axis ]->SetSliceNumber( real );
413 // -------------------------------------------------------------------------
414 void cpExtensions::Visualization::MPRActors::
415 SetSlice( const int& axis, const double& slice )
417 vtkImageData* image = this->_Image( 0 );
421 double x[ 3 ] = { double( 0 ) };
426 image->ComputeStructuredCoordinates( x, ijk, pcoords );
427 this->SetSlice( axis, ijk[ axis ] );
430 // -------------------------------------------------------------------------
431 void cpExtensions::Visualization::MPRActors::
434 for( unsigned int i = 0; i < 2; ++i )
435 for( unsigned int j = 0; j < 3; ++j )
436 this->Slices[ i ][ j ]->SetSliceNumber(
437 this->Slices[ i ][ j ]->GetSliceNumberMinValue( )
441 // -------------------------------------------------------------------------
442 cpExtensions::Visualization::MPRActors::
446 this->ImageOutlineActor = vtkSmartPointer< vtkActor >::New( );
447 for( unsigned int i = 0; i < 2; ++i )
448 for( unsigned int j = 0; j < 3; ++j )
449 this->Slices[ i ][ j ] = vtkSmartPointer< ImageSliceActors >::New( );
452 // -------------------------------------------------------------------------
453 cpExtensions::Visualization::MPRActors::
458 // -------------------------------------------------------------------------
459 vtkImageData* cpExtensions::Visualization::MPRActors::
460 _Image( unsigned int i ) const
462 if( i < this->ImageMaps.size( ) )
464 vtkAlgorithm* algo = this->ImageMaps[ i ]->GetInputAlgorithm( );
465 vtkInformation* info = algo->GetOutputInformation( 0 );
467 vtkImageData::SafeDownCast( info->Get( vtkDataObject::DATA_OBJECT( ) ) )
474 // -------------------------------------------------------------------------
475 void cpExtensions::Visualization::MPRActors::
476 _Update( unsigned int i )
478 // Check if the input has been configured
479 vtkImageData* image = this->_Image( i );
482 this->ImageMaps[ i ]->Update( );
484 for( int j = 0; j < 2; ++j )
486 for( int k = 0; k < 3; ++k )
488 this->Slices[ j ][ k ]->AddInputConnection(
489 this->ImageMaps[ i ]->GetOutputPort( ), k
491 this->Slices[ j ][ k ]->UpdateText( );
501 image->GetBounds( bb );
503 vtkSmartPointer< vtkOutlineSource > img_ol =
504 vtkSmartPointer< vtkOutlineSource >::New( );
505 img_ol->SetBounds( bb );
507 vtkSmartPointer< vtkPolyDataMapper > img_ol_mapper =
508 vtkSmartPointer< vtkPolyDataMapper >::New( );
509 img_ol_mapper->SetInputConnection( img_ol->GetOutputPort( ) );
510 this->ImageOutlineActor->SetMapper( img_ol_mapper );
511 this->ImageOutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
512 this->ImageOutlineActor->GetProperty( )->SetLineWidth( 1 );
514 this->AddItem( this->ImageOutlineActor );