1 #include <cpExtensions/Visualization/MPRActors.h>
3 #include <vtkAlgorithmOutput.h>
4 #include <vtkImageData.h>
5 #include <vtkRenderer.h>
6 #include <vtkRendererCollection.h>
7 #include <vtkRenderWindow.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->Slices[ 0 ][ 0 ]->GetNumberOfImageActors( );
35 this->Slices[ 0 ][ 0 ]->AddInputConnection( aout, 0 );
36 vtkImageMapToColors* imap = this->Slices[ 0 ][ 0 ]->GetImageMap( 0 );
37 for( unsigned int i = 0; i < 2; ++i )
38 for( unsigned int j = 0; j < 3; ++j )
39 if( i != 0 || j != 0 )
40 this->Slices[ i ][ j ]->AddInputConnection(
41 ( ( imap != NULL )? imap->GetOutputPort( ): aout ), j
46 // Check if the image share the same space
47 vtkImageData* new_image = dynamic_cast< vtkImageData* >(
48 aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
50 if( new_image != NULL )
52 vtkAlgorithmOutput* ref_aout =
53 this->Slices[ 0 ][ 0 ]->GetImageMap( 0 )->GetOutputPort( );
54 vtkImageData* ref_image = dynamic_cast< vtkImageData* >(
55 ref_aout->GetProducer( )->GetOutputDataObject( ref_aout->GetIndex( ) )
57 if( ref_image != NULL )
59 int ref_ext[ 6 ], new_ext[ 6 ];
60 ref_image->GetExtent( ref_ext );
61 new_image->GetExtent( new_ext );
63 ref_ext[ 0 ] == new_ext[ 0 ] && ref_ext[ 1 ] == new_ext[ 1 ] &&
64 ref_ext[ 2 ] == new_ext[ 2 ] && ref_ext[ 3 ] == new_ext[ 3 ] &&
65 ref_ext[ 4 ] == new_ext[ 4 ] && ref_ext[ 5 ] == new_ext[ 5 ]
68 this->Slices[ 0 ][ 0 ]->AddInputConnection( aout, 0 );
69 vtkImageMapToColors* imap = this->Slices[ 0 ][ 0 ]->GetImageMap( N );
70 for( unsigned int i = 0; i < 2; ++i )
71 for( unsigned int j = 0; j < 3; ++j )
72 if( i != 0 || j != 0 )
73 this->Slices[ i ][ j ]->AddInputConnection(
74 ( ( imap != NULL )? imap->GetOutputPort( ): aout ), j
90 // -------------------------------------------------------------------------
91 int cpExtensions::Visualization::MPRActors::
92 AddInputData( vtkImageData* new_image )
94 int N = this->Slices[ 0 ][ 0 ]->GetNumberOfImageActors( );
97 this->Slices[ 0 ][ 0 ]->AddInputData( new_image, 0 );
98 vtkImageMapToColors* imap = this->Slices[ 0 ][ 0 ]->GetImageMap( 0 );
99 for( unsigned int i = 0; i < 2; ++i )
100 for( unsigned int j = 0; j < 3; ++j )
101 if( i != 0 || j != 0 )
104 this->Slices[ i ][ j ]->AddInputConnection(
105 imap->GetOutputPort( ), j
108 this->Slices[ i ][ j ]->AddInputData( new_image, j );
114 // Check if the image share the same space
115 vtkAlgorithmOutput* ref_aout =
116 this->Slices[ 0 ][ 0 ]->GetImageMap( 0 )->GetOutputPort( );
117 vtkImageData* ref_image = dynamic_cast< vtkImageData* >(
118 ref_aout->GetProducer( )->GetOutputDataObject( ref_aout->GetIndex( ) )
120 if( ref_image != NULL )
122 int ref_ext[ 6 ], new_ext[ 6 ];
123 ref_image->GetExtent( ref_ext );
124 new_image->GetExtent( new_ext );
126 ref_ext[ 0 ] == new_ext[ 0 ] && ref_ext[ 1 ] == new_ext[ 1 ] &&
127 ref_ext[ 2 ] == new_ext[ 2 ] && ref_ext[ 3 ] == new_ext[ 3 ] &&
128 ref_ext[ 4 ] == new_ext[ 4 ] && ref_ext[ 5 ] == new_ext[ 5 ]
131 this->Slices[ 0 ][ 0 ]->AddInputData( new_image, 0 );
132 vtkImageMapToColors* imap = this->Slices[ 0 ][ 0 ]->GetImageMap( N );
133 for( unsigned int i = 0; i < 2; ++i )
134 for( unsigned int j = 0; j < 3; ++j )
135 if( i != 0 || j != 0 )
138 this->Slices[ i ][ j ]->AddInputConnection(
139 imap->GetOutputPort( ), j
142 this->Slices[ i ][ j ]->AddInputData( new_image, j );
156 // -------------------------------------------------------------------------
157 unsigned int cpExtensions::Visualization::MPRActors::
158 GetNumberOfImages( ) const
160 return( this->Slices[ 0 ][ 0 ]->GetNumberOfImageActors( ) );
163 // -------------------------------------------------------------------------
164 void cpExtensions::Visualization::MPRActors::
166 vtkRenderWindow* x, vtkRenderWindow* y, vtkRenderWindow* z,
170 this->Slices[ 0 ][ 0 ]->PushActorsInto( x, true );
171 this->Slices[ 0 ][ 1 ]->PushActorsInto( y, true );
172 this->Slices[ 0 ][ 2 ]->PushActorsInto( z, true );
175 ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
176 vtkRenderer* rends[ ] =
178 ( x != NULL )? x->GetRenderers( )->GetFirstRenderer( ): NULL,
179 ( y != NULL )? y->GetRenderers( )->GetFirstRenderer( ): NULL,
180 ( z != NULL )? z->GetRenderers( )->GetFirstRenderer( ): NULL
182 for( int i = 0; i < 3; ++i )
184 if( rends[ i ] != NULL )
185 for( int j = 0; j < 3; ++j )
187 rends[ i ]->AddActor( this->Slices[ 0 ][ j ]->GetPlaneActor( ) );
192 k < this->Slices[ 1 ][ i ]->GetNumberOfImageActors( );
195 wren->AddActor( this->Slices[ 1 ][ i ]->GetImageActor( k ) );
196 wren->AddActor( this->Slices[ 1 ][ i ]->GetPlaneActor( ) );
202 wren->AddActor( this->ImageOutlineActor );
204 for( unsigned int j = 0; j < 3; ++j )
206 ImageInteractorStyle* st =
207 dynamic_cast< ImageInteractorStyle* >( this->Slices[ 0 ][ j ]->GetStyle( ) );
209 st->AssociateInteractor( w->GetInteractor( ) );
210 for( unsigned int l = 0; l < 3; ++l )
212 st->AssociateInteractor(
213 this->Slices[ 0 ][ l ]->GetStyle( )->GetInteractor( )
219 // -------------------------------------------------------------------------
220 void cpExtensions::Visualization::MPRActors::
222 vtkRenderWindow* x, vtkRenderWindow* y, vtkRenderWindow* z,
226 this->Slices[ 0 ][ 0 ]->PopActorsFrom( x );
227 this->Slices[ 0 ][ 1 ]->PopActorsFrom( y );
228 this->Slices[ 0 ][ 2 ]->PopActorsFrom( z );
231 ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
232 vtkRenderer* rends[ ] =
234 ( x != NULL )? x->GetRenderers( )->GetFirstRenderer( ): NULL,
235 ( y != NULL )? y->GetRenderers( )->GetFirstRenderer( ): NULL,
236 ( z != NULL )? z->GetRenderers( )->GetFirstRenderer( ): NULL
238 for( int i = 0; i < 3; ++i )
240 if( rends[ i ] != NULL )
241 for( int j = 0; j < 3; ++j )
243 rends[ i ]->RemoveActor( this->Slices[ 0 ][ j ]->GetPlaneActor( ) );
248 k < this->Slices[ 1 ][ i ]->GetNumberOfImageActors( );
251 wren->RemoveActor( this->Slices[ 1 ][ i ]->GetImageActor( k ) );
252 wren->RemoveActor( this->Slices[ 1 ][ i ]->GetPlaneActor( ) );
258 wren->RemoveActor( this->ImageOutlineActor );
260 this->Slices[ 0 ][ 0 ]->Clear( );
261 this->Slices[ 0 ][ 1 ]->Clear( );
262 this->Slices[ 0 ][ 2 ]->Clear( );
263 this->Slices[ 1 ][ 0 ]->Clear( );
264 this->Slices[ 1 ][ 1 ]->Clear( );
265 this->Slices[ 1 ][ 2 ]->Clear( );
268 // -------------------------------------------------------------------------
269 void cpExtensions::Visualization::MPRActors::
270 SetLookupTableAsColor( unsigned int i, double r, double g, double b )
272 this->Slices[ 0 ][ 0 ]->SetLookupTableAsColor( 0, r, g, b );
273 this->Slices[ 0 ][ 1 ]->Modified( );
274 this->Slices[ 0 ][ 2 ]->Modified( );
275 this->Slices[ 1 ][ 0 ]->Modified( );
276 this->Slices[ 1 ][ 1 ]->Modified( );
277 this->Slices[ 1 ][ 2 ]->Modified( );
281 // -------------------------------------------------------------------------
282 int cpExtensions::Visualization::MPRActors::
283 GetSliceNumberMinValue( const int& axis ) const
285 return( this->Slices[ 0 ][ axis ]->GetSliceNumberMinValue( ) );
288 // -------------------------------------------------------------------------
289 int cpExtensions::Visualization::MPRActors::
290 GetSliceNumberMaxValue( const int& axis ) const
292 return( this->Slices[ 0 ][ axis ]->GetSliceNumberMaxValue( ) );
295 // -------------------------------------------------------------------------
296 int cpExtensions::Visualization::MPRActors::
297 GetSlice( const int& axis ) const
299 return( this->Slices[ 0 ][ axis ]->GetSliceNumber( ) );
302 // -------------------------------------------------------------------------
303 void cpExtensions::Visualization::MPRActors::
304 SetSlice( const int& axis, const int& slice )
306 vtkImageData* image = this->_Image( 0 );
310 // Get image data extent
312 image->GetExtent( ext );
314 // Check if the slice is valid
316 if( slice < ext[ axis << 1 ] )
317 real = ext[ axis << 1 ];
318 if( ext[ ( axis << 1 ) + 1 ] < slice )
319 real = ext[ ( axis << 1 ) + 1 ];
322 this->Slices[ 0 ][ axis ]->SetSliceNumber( real );
323 this->Slices[ 1 ][ axis ]->SetSliceNumber( real );
327 // -------------------------------------------------------------------------
328 void cpExtensions::Visualization::MPRActors::
329 SetSlice( const int& axis, const double& slice )
331 vtkImageData* image = this->_Image( 0 );
335 double x[ 3 ] = { double( 0 ) };
340 image->ComputeStructuredCoordinates( x, ijk, pcoords );
341 this->SetSlice( axis, ijk[ axis ] );
344 // -------------------------------------------------------------------------
345 void cpExtensions::Visualization::MPRActors::
348 for( unsigned int i = 0; i < 2; ++i )
349 for( unsigned int j = 0; j < 3; ++j )
350 this->Slices[ i ][ j ]->SetSliceNumber(
351 this->Slices[ i ][ j ]->GetSliceNumberMinValue( )
355 // -------------------------------------------------------------------------
356 cpExtensions::Visualization::MPRActors::
360 this->ImageOutlineActor = vtkSmartPointer< vtkActor >::New( );
361 for( unsigned int i = 0; i < 2; ++i )
362 for( unsigned int j = 0; j < 3; ++j )
363 this->Slices[ i ][ j ] = vtkSmartPointer< ImageSliceActors >::New( );
365 this->Slices[ 0 ][ 0 ]->AssociateSlice( this->Slices[ 1 ][ 0 ] );
366 this->Slices[ 0 ][ 1 ]->AssociateSlice( this->Slices[ 1 ][ 1 ] );
367 this->Slices[ 0 ][ 2 ]->AssociateSlice( this->Slices[ 1 ][ 2 ] );
369 this->Slices[ 0 ][ 0 ]->SetSlicesCommand( Self::_SetSlices, this );
370 this->Slices[ 0 ][ 1 ]->SetSlicesCommand( Self::_SetSlices, this );
371 this->Slices[ 0 ][ 2 ]->SetSlicesCommand( Self::_SetSlices, this );
374 // -------------------------------------------------------------------------
375 cpExtensions::Visualization::MPRActors::
380 // -------------------------------------------------------------------------
381 vtkImageData* cpExtensions::Visualization::MPRActors::
382 _Image( unsigned int i ) const
385 if( i < this->ImageMaps.size( ) )
387 vtkAlgorithm* algo = this->ImageMaps[ i ]->GetInputAlgorithm( );
388 vtkInformation* info = algo->GetOutputInformation( 0 );
390 vtkImageData::SafeDownCast(
391 info->Get( vtkDataObject::DATA_OBJECT( ) )
400 // -------------------------------------------------------------------------
401 void cpExtensions::Visualization::MPRActors::
402 _Update( unsigned int i )
405 // Check if the input has been configured
406 vtkImageData* image = this->_Image( i );
409 this->ImageMaps[ i ]->Update( );
411 for( int j = 0; j < 2; ++j )
413 for( int k = 0; k < 3; ++k )
415 this->Slices[ j ][ k ]->AddInputConnection(
416 this->ImageMaps[ i ]->GetOutputPort( ), k
418 this->Slices[ j ][ k ]->UpdateText( );
420 // Add all of slice's props
421 this->Slices[ j ][ k ]->InitTraversal( );
422 vtkProp* prop = this->Slices[ j ][ k ]->GetNextProp( );
423 while( prop != NULL )
425 this->AddItem( prop );
426 prop = this->Slices[ j ][ k ]->GetNextProp( );
438 image->GetBounds( bb );
440 vtkSmartPointer< vtkOutlineSource > img_ol =
441 vtkSmartPointer< vtkOutlineSource >::New( );
442 img_ol->SetBounds( bb );
444 vtkSmartPointer< vtkPolyDataMapper > img_ol_mapper =
445 vtkSmartPointer< vtkPolyDataMapper >::New( );
446 img_ol_mapper->SetInputConnection( img_ol->GetOutputPort( ) );
447 this->ImageOutlineActor->SetMapper( img_ol_mapper );
448 this->ImageOutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
449 this->ImageOutlineActor->GetProperty( )->SetLineWidth( 1 );
451 this->AddItem( this->ImageOutlineActor );
457 // -------------------------------------------------------------------------
458 void cpExtensions::Visualization::MPRActors::
459 _SetSlices( double* pos, int axis, void* data )
461 MPRActors* actors = reinterpret_cast< MPRActors* >( data );
464 for( unsigned int j = 0; j < 3; ++j )
465 if( actors->Slices[ 0 ][ j ]->GetAxis( ) != axis )
466 actors->Slices[ 0 ][ j ]->SetSlice( pos );