1 #include <cpExtensions/Visualization/MPRActors.h>
3 #include <vtkAlgorithmOutput.h>
4 #include <vtkImageData.h>
5 #include <vtkOutlineSource.h>
6 #include <vtkProperty.h>
7 #include <vtkRenderer.h>
8 #include <vtkRendererCollection.h>
9 #include <vtkRenderWindow.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->Slices[ 0 ][ 0 ]->GetNumberOfImageActors( );
37 this->Slices[ 0 ][ 0 ]->AddInputConnection( aout, 0 );
38 vtkImageMapToColors* imap = this->Slices[ 0 ][ 0 ]->GetImageMap( 0 );
39 for( unsigned int i = 0; i < 2; ++i )
40 for( unsigned int j = 0; j < 3; ++j )
41 if( i != 0 || j != 0 )
42 this->Slices[ i ][ j ]->AddInputConnection(
43 ( ( imap != NULL )? imap->GetOutputPort( ): aout ), j
46 // Create bounding box
47 vtkImageData* new_image = dynamic_cast< vtkImageData* >(
48 aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
53 new_image->GetBounds( bb );
55 vtkSmartPointer< vtkOutlineSource > img_ol =
56 vtkSmartPointer< vtkOutlineSource >::New( );
57 img_ol->SetBounds( bb );
59 vtkSmartPointer< vtkPolyDataMapper > img_ol_mapper =
60 vtkSmartPointer< vtkPolyDataMapper >::New( );
61 img_ol_mapper->SetInputConnection( img_ol->GetOutputPort( ) );
62 this->ImageOutlineActor->SetMapper( img_ol_mapper );
63 this->ImageOutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
64 this->ImageOutlineActor->GetProperty( )->SetLineWidth( 1 );
66 this->AddItem( this->ImageOutlineActor );
70 // Check if the image share the same space
71 vtkImageData* new_image = dynamic_cast< vtkImageData* >(
72 aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
74 if( new_image != NULL )
76 vtkAlgorithmOutput* ref_aout =
77 this->Slices[ 0 ][ 0 ]->GetImageMap( 0 )->GetOutputPort( );
78 vtkImageData* ref_image = dynamic_cast< vtkImageData* >(
79 ref_aout->GetProducer( )->GetOutputDataObject( ref_aout->GetIndex( ) )
81 if( ref_image != NULL )
83 int ref_ext[ 6 ], new_ext[ 6 ];
84 ref_image->GetExtent( ref_ext );
85 new_image->GetExtent( new_ext );
87 ref_ext[ 0 ] == new_ext[ 0 ] && ref_ext[ 1 ] == new_ext[ 1 ] &&
88 ref_ext[ 2 ] == new_ext[ 2 ] && ref_ext[ 3 ] == new_ext[ 3 ] &&
89 ref_ext[ 4 ] == new_ext[ 4 ] && ref_ext[ 5 ] == new_ext[ 5 ]
92 this->Slices[ 0 ][ 0 ]->AddInputConnection( aout, 0 );
93 vtkImageMapToColors* imap = this->Slices[ 0 ][ 0 ]->GetImageMap( N );
94 for( unsigned int i = 0; i < 2; ++i )
95 for( unsigned int j = 0; j < 3; ++j )
96 if( i != 0 || j != 0 )
97 this->Slices[ i ][ j ]->AddInputConnection(
98 ( ( imap != NULL )? imap->GetOutputPort( ): aout ), j
114 // -------------------------------------------------------------------------
115 int cpExtensions::Visualization::MPRActors::
116 AddInputData( vtkImageData* new_image )
118 int N = this->Slices[ 0 ][ 0 ]->GetNumberOfImageActors( );
121 this->Slices[ 0 ][ 0 ]->AddInputData( new_image, 0 );
122 vtkImageMapToColors* imap = this->Slices[ 0 ][ 0 ]->GetImageMap( 0 );
123 for( unsigned int i = 0; i < 2; ++i )
124 for( unsigned int j = 0; j < 3; ++j )
125 if( i != 0 || j != 0 )
128 this->Slices[ i ][ j ]->AddInputConnection(
129 imap->GetOutputPort( ), j
132 this->Slices[ i ][ j ]->AddInputData( new_image, j );
138 new_image->GetBounds( bb );
140 vtkSmartPointer< vtkOutlineSource > img_ol =
141 vtkSmartPointer< vtkOutlineSource >::New( );
142 img_ol->SetBounds( bb );
144 vtkSmartPointer< vtkPolyDataMapper > img_ol_mapper =
145 vtkSmartPointer< vtkPolyDataMapper >::New( );
146 img_ol_mapper->SetInputConnection( img_ol->GetOutputPort( ) );
147 this->ImageOutlineActor->SetMapper( img_ol_mapper );
148 this->ImageOutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
149 this->ImageOutlineActor->GetProperty( )->SetLineWidth( 1 );
151 this->AddItem( this->ImageOutlineActor );
155 // Check if the image share the same space
156 vtkAlgorithmOutput* ref_aout =
157 this->Slices[ 0 ][ 0 ]->GetImageMap( 0 )->GetOutputPort( );
158 vtkImageData* ref_image = dynamic_cast< vtkImageData* >(
159 ref_aout->GetProducer( )->GetOutputDataObject( ref_aout->GetIndex( ) )
161 if( ref_image != NULL )
163 int ref_ext[ 6 ], new_ext[ 6 ];
164 ref_image->GetExtent( ref_ext );
165 new_image->GetExtent( new_ext );
167 ref_ext[ 0 ] == new_ext[ 0 ] && ref_ext[ 1 ] == new_ext[ 1 ] &&
168 ref_ext[ 2 ] == new_ext[ 2 ] && ref_ext[ 3 ] == new_ext[ 3 ] &&
169 ref_ext[ 4 ] == new_ext[ 4 ] && ref_ext[ 5 ] == new_ext[ 5 ]
172 this->Slices[ 0 ][ 0 ]->AddInputData( new_image, 0 );
173 vtkImageMapToColors* imap = this->Slices[ 0 ][ 0 ]->GetImageMap( N );
174 for( unsigned int i = 0; i < 2; ++i )
175 for( unsigned int j = 0; j < 3; ++j )
176 if( i != 0 || j != 0 )
179 this->Slices[ i ][ j ]->AddInputConnection(
180 imap->GetOutputPort( ), j
183 this->Slices[ i ][ j ]->AddInputData( new_image, j );
197 // -------------------------------------------------------------------------
198 unsigned int cpExtensions::Visualization::MPRActors::
199 GetNumberOfImages( ) const
201 return( this->Slices[ 0 ][ 0 ]->GetNumberOfImageActors( ) );
204 // -------------------------------------------------------------------------
205 void cpExtensions::Visualization::MPRActors::
207 vtkRenderWindow* x, vtkRenderWindow* y, vtkRenderWindow* z,
211 this->Slices[ 0 ][ 0 ]->PushActorsInto( x, true );
212 this->Slices[ 0 ][ 1 ]->PushActorsInto( y, true );
213 this->Slices[ 0 ][ 2 ]->PushActorsInto( z, true );
216 ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
217 vtkRenderer* rends[ ] =
219 ( x != NULL )? x->GetRenderers( )->GetFirstRenderer( ): NULL,
220 ( y != NULL )? y->GetRenderers( )->GetFirstRenderer( ): NULL,
221 ( z != NULL )? z->GetRenderers( )->GetFirstRenderer( ): NULL
223 for( int i = 0; i < 3; ++i )
225 if( rends[ i ] != NULL )
226 for( int j = 0; j < 3; ++j )
228 rends[ i ]->AddActor( this->Slices[ 0 ][ j ]->GetPlaneActor( ) );
233 k < this->Slices[ 1 ][ i ]->GetNumberOfImageActors( );
236 wren->AddActor( this->Slices[ 1 ][ i ]->GetImageActor( k ) );
237 wren->AddActor( this->Slices[ 1 ][ i ]->GetPlaneActor( ) );
243 wren->AddActor( this->ImageOutlineActor );
245 for( unsigned int j = 0; j < 3; ++j )
248 ImageInteractorStyle* st =
249 dynamic_cast< ImageInteractorStyle* >( this->Slices[ 0 ][ j ]->GetStyle( ) );
251 st->AssociateInteractor( w->GetInteractor( ) );
252 for( unsigned int l = 0; l < 3; ++l )
254 st->AssociateInteractor(
255 this->Slices[ 0 ][ l ]->GetStyle( )->GetInteractor( )
262 // -------------------------------------------------------------------------
263 void cpExtensions::Visualization::MPRActors::
265 vtkRenderWindow* x, vtkRenderWindow* y, vtkRenderWindow* z,
269 this->Slices[ 0 ][ 0 ]->PopActorsFrom( x );
270 this->Slices[ 0 ][ 1 ]->PopActorsFrom( y );
271 this->Slices[ 0 ][ 2 ]->PopActorsFrom( z );
274 ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
275 vtkRenderer* rends[ ] =
277 ( x != NULL )? x->GetRenderers( )->GetFirstRenderer( ): NULL,
278 ( y != NULL )? y->GetRenderers( )->GetFirstRenderer( ): NULL,
279 ( z != NULL )? z->GetRenderers( )->GetFirstRenderer( ): NULL
281 for( int i = 0; i < 3; ++i )
283 if( rends[ i ] != NULL )
284 for( int j = 0; j < 3; ++j )
286 rends[ i ]->RemoveActor( this->Slices[ 0 ][ j ]->GetPlaneActor( ) );
291 k < this->Slices[ 1 ][ i ]->GetNumberOfImageActors( );
294 wren->RemoveActor( this->Slices[ 1 ][ i ]->GetImageActor( k ) );
295 wren->RemoveActor( this->Slices[ 1 ][ i ]->GetPlaneActor( ) );
301 wren->RemoveActor( this->ImageOutlineActor );
303 this->Slices[ 0 ][ 0 ]->Clear( );
304 this->Slices[ 0 ][ 1 ]->Clear( );
305 this->Slices[ 0 ][ 2 ]->Clear( );
306 this->Slices[ 1 ][ 0 ]->Clear( );
307 this->Slices[ 1 ][ 1 ]->Clear( );
308 this->Slices[ 1 ][ 2 ]->Clear( );
311 // -------------------------------------------------------------------------
312 void cpExtensions::Visualization::MPRActors::
313 SetLookupTableAsColor( unsigned int i, double r, double g, double b )
315 this->Slices[ 0 ][ 0 ]->SetLookupTableAsColor( 0, r, g, b );
316 this->Slices[ 0 ][ 1 ]->Modified( );
317 this->Slices[ 0 ][ 2 ]->Modified( );
318 this->Slices[ 1 ][ 0 ]->Modified( );
319 this->Slices[ 1 ][ 1 ]->Modified( );
320 this->Slices[ 1 ][ 2 ]->Modified( );
324 // -------------------------------------------------------------------------
325 double cpExtensions::Visualization::MPRActors::
328 return( this->Slices[ 0 ][ 0 ]->GetWindow( ) );
331 // -------------------------------------------------------------------------
332 double cpExtensions::Visualization::MPRActors::
335 return( this->Slices[ 0 ][ 0 ]->GetLevel( ) );
338 // -------------------------------------------------------------------------
339 int cpExtensions::Visualization::MPRActors::
340 GetSliceNumberMinValue( const int& axis ) const
342 return( this->Slices[ 0 ][ axis ]->GetSliceNumberMinValue( ) );
345 // -------------------------------------------------------------------------
346 int cpExtensions::Visualization::MPRActors::
347 GetSliceNumberMaxValue( const int& axis ) const
349 return( this->Slices[ 0 ][ axis ]->GetSliceNumberMaxValue( ) );
352 // -------------------------------------------------------------------------
353 int cpExtensions::Visualization::MPRActors::
354 GetSlice( const int& axis ) const
356 return( this->Slices[ 0 ][ axis ]->GetSliceNumber( ) );
359 // -------------------------------------------------------------------------
360 void cpExtensions::Visualization::MPRActors::
361 SetSlice( const int& axis, const int& slice )
363 vtkImageData* image = this->_Image( 0 );
367 // Get image data extent
369 image->GetExtent( ext );
371 // Check if the slice is valid
373 if( slice < ext[ axis << 1 ] )
374 real = ext[ axis << 1 ];
375 if( ext[ ( axis << 1 ) + 1 ] < slice )
376 real = ext[ ( axis << 1 ) + 1 ];
379 this->Slices[ 0 ][ axis ]->SetSliceNumber( real );
380 this->Slices[ 1 ][ axis ]->SetSliceNumber( real );
384 // -------------------------------------------------------------------------
385 void cpExtensions::Visualization::MPRActors::
386 SetSlice( const int& axis, const double& slice )
388 vtkImageData* image = this->_Image( 0 );
392 double x[ 3 ] = { double( 0 ) };
397 image->ComputeStructuredCoordinates( x, ijk, pcoords );
398 this->SetSlice( axis, ijk[ axis ] );
401 // -------------------------------------------------------------------------
402 void cpExtensions::Visualization::MPRActors::
405 for( unsigned int i = 0; i < 2; ++i )
406 for( unsigned int j = 0; j < 3; ++j )
407 this->Slices[ i ][ j ]->SetSliceNumber(
408 this->Slices[ i ][ j ]->GetSliceNumberMinValue( )
412 // -------------------------------------------------------------------------
413 cpExtensions::Visualization::MPRActors::
417 this->ImageOutlineActor = vtkSmartPointer< vtkActor >::New( );
418 for( unsigned int i = 0; i < 2; ++i )
419 for( unsigned int j = 0; j < 3; ++j )
420 this->Slices[ i ][ j ] = vtkSmartPointer< ImageSliceActors >::New( );
423 this->Slices[ 0 ][ 0 ]->AssociateSlice( this->Slices[ 1 ][ 0 ] );
424 this->Slices[ 0 ][ 1 ]->AssociateSlice( this->Slices[ 1 ][ 1 ] );
425 this->Slices[ 0 ][ 2 ]->AssociateSlice( this->Slices[ 1 ][ 2 ] );
428 this->Slices[ 0 ][ 0 ]->AddSlicesCommand( TSlicesCommand command, void* data );
429 AddWindowLevelCommand( TWindowLevelCommand command, void* data );
434 this->Slices[ 0 ][ 0 ]->SetSlicesCommand( Self::_SetSlices, this );
435 this->Slices[ 0 ][ 1 ]->SetSlicesCommand( Self::_SetSlices, this );
436 this->Slices[ 0 ][ 2 ]->SetSlicesCommand( Self::_SetSlices, this );
440 // -------------------------------------------------------------------------
441 cpExtensions::Visualization::MPRActors::
446 // -------------------------------------------------------------------------
447 vtkImageData* cpExtensions::Visualization::MPRActors::
448 _Image( unsigned int i ) const
451 if( i < this->ImageMaps.size( ) )
453 vtkAlgorithm* algo = this->ImageMaps[ i ]->GetInputAlgorithm( );
454 vtkInformation* info = algo->GetOutputInformation( 0 );
456 vtkImageData::SafeDownCast(
457 info->Get( vtkDataObject::DATA_OBJECT( ) )
466 // -------------------------------------------------------------------------
467 void cpExtensions::Visualization::MPRActors::
468 _Update( unsigned int i )
471 // Check if the input has been configured
472 vtkImageData* image = this->_Image( i );
475 this->ImageMaps[ i ]->Update( );
477 for( int j = 0; j < 2; ++j )
479 for( int k = 0; k < 3; ++k )
481 this->Slices[ j ][ k ]->AddInputConnection(
482 this->ImageMaps[ i ]->GetOutputPort( ), k
484 this->Slices[ j ][ k ]->UpdateText( );
486 // Add all of slice's props
487 this->Slices[ j ][ k ]->InitTraversal( );
488 vtkProp* prop = this->Slices[ j ][ k ]->GetNextProp( );
489 while( prop != NULL )
491 this->AddItem( prop );
492 prop = this->Slices[ j ][ k ]->GetNextProp( );
504 image->GetBounds( bb );
506 vtkSmartPointer< vtkOutlineSource > img_ol =
507 vtkSmartPointer< vtkOutlineSource >::New( );
508 img_ol->SetBounds( bb );
510 vtkSmartPointer< vtkPolyDataMapper > img_ol_mapper =
511 vtkSmartPointer< vtkPolyDataMapper >::New( );
512 img_ol_mapper->SetInputConnection( img_ol->GetOutputPort( ) );
513 this->ImageOutlineActor->SetMapper( img_ol_mapper );
514 this->ImageOutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
515 this->ImageOutlineActor->GetProperty( )->SetLineWidth( 1 );
517 this->AddItem( this->ImageOutlineActor );
523 // -------------------------------------------------------------------------
524 void cpExtensions::Visualization::MPRActors::
525 _SlicesCommand( double* pos, int axis, void* data )
528 MPRActors* actors = reinterpret_cast< MPRActors* >( data );
531 for( unsigned int j = 0; j < 3; ++j )
532 if( actors->Slices[ 0 ][ j ]->GetAxis( ) != axis )
533 actors->Slices[ 0 ][ j ]->SetSlice( pos );
538 // -------------------------------------------------------------------------
539 void cpExtensions::Visualization::MPRActors::
540 _WindowLevelCommand( double window, double level, void* data )
544 // -------------------------------------------------------------------------
545 void cpExtensions::Visualization::MPRActors::
546 _RenderCommand( void* data )