1 #include <cpExtensions/Visualization/MPRActors.h>
3 #include <vtkAlgorithmOutput.h>
4 #include <vtkImageData.h>
5 #include <vtkInformation.h>
6 #include <vtkRenderer.h>
7 #include <vtkWindowLevelLookupTable.h>
9 // -------------------------------------------------------------------------
10 cpExtensions::Visualization::MPRActors*
11 cpExtensions::Visualization::MPRActors::
14 return( new Self( ) );
17 // -------------------------------------------------------------------------
18 cpExtensions::Visualization::
19 ImageSliceActors* cpExtensions::Visualization::MPRActors::
20 GetSliceActors( const int& i ) const
23 return( this->Slices[ 0 ][ i ] );
28 // -------------------------------------------------------------------------
29 int cpExtensions::Visualization::MPRActors::
30 AddInputConnection( vtkAlgorithmOutput* aout )
32 int N = this->ImageMaps.size( );
35 this->ImageMaps.push_back(
36 vtkSmartPointer< vtkImageMapToColors >::New( )
38 this->ImageMaps[ 0 ]->SetInputConnection( aout );
39 this->SetLookupTableToWindowLevel( 0 );
45 // Check if the image share the same space
46 vtkImageData* ref_image = this->_Image( 0 );
47 vtkImageData* new_image =
48 vtkImageData::SafeDownCast(
49 aout->GetProducer( )->GetOutputInformation( 0 )->
50 Get( vtkDataObject::DATA_OBJECT( ) )
52 int ref_ext[ 6 ], new_ext[ 6 ];
53 ref_image->GetExtent( ref_ext );
54 new_image->GetExtent( new_ext );
56 ref_ext[ 0 ] == new_ext[ 0 ] && ref_ext[ 1 ] == new_ext[ 1 ] &&
57 ref_ext[ 2 ] == new_ext[ 2 ] && ref_ext[ 3 ] == new_ext[ 3 ] &&
58 ref_ext[ 4 ] == new_ext[ 4 ] && ref_ext[ 5 ] == new_ext[ 5 ]
61 this->ImageMaps.push_back(
62 vtkSmartPointer< vtkImageMapToColors >::New( )
64 this->ImageMaps[ N ]->SetInputConnection( aout );
65 this->SetLookupTableToWindowLevel( N );
75 // -------------------------------------------------------------------------
76 int cpExtensions::Visualization::MPRActors::
77 AddInputData( vtkImageData* image )
79 int N = this->ImageMaps.size( );
82 this->ImageMaps.push_back(
83 vtkSmartPointer< vtkImageMapToColors >::New( )
85 this->ImageMaps[ 0 ]->SetInputData( image );
86 this->SetLookupTableToWindowLevel( 0 );
92 // Check if the image share the same space
93 vtkImageData* ref_image = this->_Image( 0 );
94 vtkImageData* new_image = image;
95 int ref_ext[ 6 ], new_ext[ 6 ];
96 ref_image->GetExtent( ref_ext );
97 new_image->GetExtent( new_ext );
99 ref_ext[ 0 ] == new_ext[ 0 ] && ref_ext[ 1 ] == new_ext[ 1 ] &&
100 ref_ext[ 2 ] == new_ext[ 2 ] && ref_ext[ 3 ] == new_ext[ 3 ] &&
101 ref_ext[ 4 ] == new_ext[ 4 ] && ref_ext[ 5 ] == new_ext[ 5 ]
104 this->ImageMaps.push_back(
105 vtkSmartPointer< vtkImageMapToColors >::New( )
107 this->ImageMaps[ N ]->SetInputData( image );
108 this->SetLookupTableToWindowLevel( N );
118 // -------------------------------------------------------------------------
119 void cpExtensions::Visualization::MPRActors::
120 PushDataInto( vtkRenderer* x, vtkRenderer* y, vtkRenderer* z, vtkRenderer* w )
122 vtkRenderer* rends[] = { x, y, z };
123 for( int i = 0; i < 3; ++i )
125 if( rends[ i ] != NULL )
129 k < this->Slices[ 0 ][ i ]->GetNumberOfImageActors( );
132 rends[ i ]->AddActor( this->Slices[ 0 ][ i ]->GetImageActor( k ) );
133 rends[ i ]->AddActor( this->Slices[ 0 ][ i ]->GetTextActor( ) );
134 for( int j = 0; j < 3; ++j )
135 rends[ i ]->AddActor( this->Slices[ 0 ][ j ]->GetPlaneActor( ) );
142 k < this->Slices[ 1 ][ i ]->GetNumberOfImageActors( );
145 w->AddActor( this->Slices[ 1 ][ i ]->GetImageActor( k ) );
146 w->AddActor( this->Slices[ 1 ][ i ]->GetPlaneActor( ) );
152 w->AddActor( this->ImageOutlineActor );
155 // -------------------------------------------------------------------------
156 void cpExtensions::Visualization::MPRActors::
164 vtkRenderer* rends[] = { x, y, z };
165 for( int i = 0; i < 3; ++i )
167 if( rends[ i ] != NULL )
171 k < this->Slices[ 0 ][ i ]->GetNumberOfImageActors( );
174 rends[ i ]->RemoveActor( this->Slices[ 0 ][ i ]->GetImageActor( k ) );
175 rends[ i ]->RemoveActor( this->Slices[ 0 ][ i ]->GetTextActor( ) );
176 for( int j = 0; j < 3; ++j )
177 rends[ i ]->RemoveActor( this->Slices[ 0 ][ j ]->GetPlaneActor( ) );
184 k < this->Slices[ 1 ][ i ]->GetNumberOfImageActors( );
187 w->RemoveActor( this->Slices[ 1 ][ i ]->GetImageActor( k ) );
188 w->RemoveActor( this->Slices[ 1 ][ i ]->GetPlaneActor( ) );
194 w->RemoveActor( this->ImageOutlineActor );
197 // -------------------------------------------------------------------------
198 void cpExtensions::Visualization::MPRActors::
199 SetLookupTable( unsigned int i, vtkScalarsToColors* lut )
201 if( i < this->ImageMaps.size( ) )
203 this->ImageMaps[ i ]->SetLookupTable( lut );
204 this->ImageMaps[ i ]->Update( );
210 // -------------------------------------------------------------------------
211 vtkScalarsToColors* cpExtensions::Visualization::MPRActors::
212 GetLookupTable( unsigned int i ) const
214 if( i < this->ImageMaps.size( ) )
215 return( this->ImageMaps[ i ]->GetLookupTable( ) );
220 // -------------------------------------------------------------------------
221 void cpExtensions::Visualization::MPRActors::
222 SetLookupTableToWindowLevel( unsigned int i )
224 // Check if the input has been configured
225 vtkImageData* image = this->_Image( i );
230 image->GetScalarRange( r );
232 vtkSmartPointer< vtkWindowLevelLookupTable > lut =
233 vtkSmartPointer< vtkWindowLevelLookupTable >::New( );
234 lut->SetScaleToLinear( );
235 lut->SetTableRange( r );
238 this->SetLookupTable( i, lut );
241 // -------------------------------------------------------------------------
242 double cpExtensions::Visualization::MPRActors::
243 GetMinWindow( unsigned int i ) const
248 // -------------------------------------------------------------------------
249 double cpExtensions::Visualization::MPRActors::
250 GetMaxWindow( unsigned int i ) const
252 // Check if the input has been configured
253 vtkImageData* image = this->_Image( i );
255 return( double( 0 ) );
258 image->GetScalarRange( r );
259 return( r[ 1 ] - r[ 0 ] );
262 // -------------------------------------------------------------------------
263 double cpExtensions::Visualization::MPRActors::
264 GetMinLevel( unsigned int i ) const
266 // Check if the input has been configured
267 vtkImageData* image = this->_Image( i );
269 return( double( 0 ) );
272 image->GetScalarRange( r );
276 // -------------------------------------------------------------------------
277 double cpExtensions::Visualization::MPRActors::
278 GetMaxLevel( unsigned int i ) const
280 // Check if the input has been configured
281 vtkImageData* image = this->_Image( i );
283 return( double( 0 ) );
286 image->GetScalarRange( r );
290 // -------------------------------------------------------------------------
291 double cpExtensions::Visualization::MPRActors::
292 GetWindow( unsigned int i ) const
294 vtkWindowLevelLookupTable* lut =
295 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
297 return( lut->GetWindow( ) );
299 return( double( 0 ) );
302 // -------------------------------------------------------------------------
303 double cpExtensions::Visualization::MPRActors::
304 GetLevel( unsigned int i ) const
306 vtkWindowLevelLookupTable* lut =
307 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
309 return( lut->GetLevel( ) );
311 return( double( 0 ) );
314 // -------------------------------------------------------------------------
315 void cpExtensions::Visualization::MPRActors::
316 SetWindow( unsigned int i, const double& w )
318 vtkWindowLevelLookupTable* lut =
319 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
324 this->ImageMaps[ i ]->Modified( );
330 // -------------------------------------------------------------------------
331 void cpExtensions::Visualization::MPRActors::
332 SetLevel( unsigned int i, const double& l )
334 vtkWindowLevelLookupTable* lut =
335 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
340 this->ImageMaps[ i ]->Modified( );
346 // -------------------------------------------------------------------------
347 void cpExtensions::Visualization::MPRActors::
348 ResetWindowLevel( unsigned int i )
350 vtkImageData* image = this->_Image( i );
351 vtkWindowLevelLookupTable* lut =
352 dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( i ) );
353 if( image != NULL && lut != NULL )
356 image->GetScalarRange( r );
357 lut->SetTableRange( r );
359 this->ImageMaps[ i ]->Modified( );
365 // -------------------------------------------------------------------------
366 int cpExtensions::Visualization::MPRActors::
367 GetSliceNumberMinValue( const int& axis ) const
369 return( this->Slices[ 0 ][ axis ]->GetSliceNumberMinValue( ) );
372 // -------------------------------------------------------------------------
373 int cpExtensions::Visualization::MPRActors::
374 GetSliceNumberMaxValue( const int& axis ) const
376 return( this->Slices[ 0 ][ axis ]->GetSliceNumberMaxValue( ) );
379 // -------------------------------------------------------------------------
380 int cpExtensions::Visualization::MPRActors::
381 GetSlice( const int& axis ) const
383 return( this->Slices[ 0 ][ axis ]->GetSliceNumber( ) );
386 // -------------------------------------------------------------------------
387 void cpExtensions::Visualization::MPRActors::
388 SetSlice( const int& axis, const int& slice )
390 vtkImageData* image = this->_Image( 0 );
394 // Get image data extent
396 image->GetExtent( ext );
398 // Check if the slice is valid
400 if( slice < ext[ axis << 1 ] )
401 real = ext[ axis << 1 ];
402 if( ext[ ( axis << 1 ) + 1 ] < slice )
403 real = ext[ ( axis << 1 ) + 1 ];
406 this->Slices[ 0 ][ axis ]->SetSliceNumber( real );
407 this->Slices[ 1 ][ axis ]->SetSliceNumber( real );
411 // -------------------------------------------------------------------------
412 void cpExtensions::Visualization::MPRActors::
413 SetSlice( const int& axis, const double& slice )
415 vtkImageData* image = this->_Image( 0 );
419 double x[ 3 ] = { double( 0 ) };
424 image->ComputeStructuredCoordinates( x, ijk, pcoords );
425 this->SetSlice( axis, ijk[ axis ] );
428 // -------------------------------------------------------------------------
429 void cpExtensions::Visualization::MPRActors::
435 // -------------------------------------------------------------------------
436 cpExtensions::Visualization::MPRActors::
440 this->ImageOutlineActor = vtkSmartPointer< vtkActor >::New( );
441 for( unsigned int i = 0; i < 2; ++i )
442 for( unsigned int j = 0; j < 3; ++j )
443 this->Slices[ i ][ j ] = vtkSmartPointer< ImageSliceActors >::New( );
446 // -------------------------------------------------------------------------
447 cpExtensions::Visualization::MPRActors::
452 // -------------------------------------------------------------------------
453 vtkImageData* cpExtensions::Visualization::MPRActors::
454 _Image( unsigned int i ) const
456 if( i < this->ImageMaps.size( ) )
458 vtkAlgorithm* algo = this->ImageMaps[ i ]->GetInputAlgorithm( );
459 vtkInformation* info = algo->GetOutputInformation( 0 );
461 vtkImageData::SafeDownCast( info->Get( vtkDataObject::DATA_OBJECT( ) ) )
468 // -------------------------------------------------------------------------
469 void cpExtensions::Visualization::MPRActors::
470 _Update( unsigned int i )
473 // Check if the input has been configured
474 vtkImageData* image = this->_InputImage( );
477 this->ImageMapToColors->Update( );
479 for( int j = 0; j < 2; ++j )
481 for( int i = 0; i < 3; ++i )
483 this->Slices[ j ][ i ]->AddInputConnection(
484 this->ImageMapToColors->GetOutputPort( ), i
486 this->Slices[ j ][ i ]->UpdateText( );
494 image->GetBounds( bb );
496 vtkSmartPointer< vtkOutlineSource > img_ol =
497 vtkSmartPointer< vtkOutlineSource >::New( );
498 img_ol->SetBounds( bb );
500 vtkSmartPointer< vtkPolyDataMapper > img_ol_mapper =
501 vtkSmartPointer< vtkPolyDataMapper >::New( );
502 img_ol_mapper->SetInputConnection( img_ol->GetOutputPort( ) );
503 this->ImageOutlineActor->SetMapper( img_ol_mapper );
504 this->ImageOutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
505 this->ImageOutlineActor->GetProperty( )->SetLineWidth( 1 );
507 this->AddItem( this->ImageOutlineActor );