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 );
214 this->Slices[ 1 ][ 0 ]->PushActorsInto( w, false );
215 this->Slices[ 1 ][ 1 ]->PushActorsInto( w, false );
216 this->Slices[ 1 ][ 2 ]->PushActorsInto( w, false );
219 ( x != NULL )? x->GetRenderers( )->GetFirstRenderer( ): NULL;
221 ( y != NULL )? y->GetRenderers( )->GetFirstRenderer( ): NULL;
223 ( z != NULL )? z->GetRenderers( )->GetFirstRenderer( ): NULL;
225 ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
229 xren->AddActor( this->Slices[ 0 ][ 1 ]->GetPlaneActor( ) );
230 xren->AddActor( this->Slices[ 0 ][ 2 ]->GetPlaneActor( ) );
235 yren->AddActor( this->Slices[ 0 ][ 0 ]->GetPlaneActor( ) );
236 yren->AddActor( this->Slices[ 0 ][ 2 ]->GetPlaneActor( ) );
241 zren->AddActor( this->Slices[ 0 ][ 0 ]->GetPlaneActor( ) );
242 zren->AddActor( this->Slices[ 0 ][ 1 ]->GetPlaneActor( ) );
246 wren->AddActor( this->ImageOutlineActor );
249 // -------------------------------------------------------------------------
250 void cpExtensions::Visualization::MPRActors::
252 vtkRenderWindow* x, vtkRenderWindow* y, vtkRenderWindow* z,
256 this->Slices[ 0 ][ 0 ]->PopActorsFrom( x );
257 this->Slices[ 0 ][ 1 ]->PopActorsFrom( y );
258 this->Slices[ 0 ][ 2 ]->PopActorsFrom( z );
259 this->Slices[ 1 ][ 0 ]->PopActorsFrom( w );
260 this->Slices[ 1 ][ 1 ]->PopActorsFrom( w );
261 this->Slices[ 1 ][ 2 ]->PopActorsFrom( w );
264 ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
266 wren->RemoveActor( this->ImageOutlineActor );
268 this->Slices[ 0 ][ 0 ]->Clear( );
269 this->Slices[ 0 ][ 1 ]->Clear( );
270 this->Slices[ 0 ][ 2 ]->Clear( );
271 this->Slices[ 1 ][ 0 ]->Clear( );
272 this->Slices[ 1 ][ 1 ]->Clear( );
273 this->Slices[ 1 ][ 2 ]->Clear( );
276 // -------------------------------------------------------------------------
277 void cpExtensions::Visualization::MPRActors::
278 SetLookupTableAsColor( unsigned int i, double r, double g, double b )
280 this->Slices[ 0 ][ 0 ]->SetLookupTableAsColor( 0, r, g, b );
281 this->Slices[ 0 ][ 1 ]->Modified( );
282 this->Slices[ 0 ][ 2 ]->Modified( );
283 this->Slices[ 1 ][ 0 ]->Modified( );
284 this->Slices[ 1 ][ 1 ]->Modified( );
285 this->Slices[ 1 ][ 2 ]->Modified( );
289 // -------------------------------------------------------------------------
290 double cpExtensions::Visualization::MPRActors::
293 return( this->Slices[ 0 ][ 0 ]->GetWindow( ) );
296 // -------------------------------------------------------------------------
297 double cpExtensions::Visualization::MPRActors::
300 return( this->Slices[ 0 ][ 0 ]->GetLevel( ) );
303 // -------------------------------------------------------------------------
304 int cpExtensions::Visualization::MPRActors::
305 GetSliceNumberMinValue( const int& axis ) const
307 return( this->Slices[ 0 ][ axis ]->GetSliceNumberMinValue( ) );
310 // -------------------------------------------------------------------------
311 int cpExtensions::Visualization::MPRActors::
312 GetSliceNumberMaxValue( const int& axis ) const
314 return( this->Slices[ 0 ][ axis ]->GetSliceNumberMaxValue( ) );
317 // -------------------------------------------------------------------------
318 int cpExtensions::Visualization::MPRActors::
319 GetSlice( const int& axis ) const
321 return( this->Slices[ 0 ][ axis ]->GetSliceNumber( ) );
324 // -------------------------------------------------------------------------
325 void cpExtensions::Visualization::MPRActors::
326 SetSlice( const int& axis, const int& slice )
328 vtkImageData* image = this->Slices[ 0 ][ 0 ]->GetInputImage( 0 );
332 // Get image data extent
334 image->GetExtent( ext );
336 // Check if the slice is valid
338 if( slice < ext[ axis << 1 ] )
339 real = ext[ axis << 1 ];
340 if( ext[ ( axis << 1 ) + 1 ] < slice )
341 real = ext[ ( axis << 1 ) + 1 ];
344 this->Slices[ 0 ][ axis ]->SetSliceNumber( real );
345 this->Slices[ 1 ][ axis ]->SetSliceNumber( real );
349 // -------------------------------------------------------------------------
350 void cpExtensions::Visualization::MPRActors::
351 SetSlice( const int& axis, const double& slice )
353 vtkImageData* image = this->Slices[ 0 ][ 0 ]->GetInputImage( 0 );
357 double x[ 3 ] = { double( 0 ) };
362 image->ComputeStructuredCoordinates( x, ijk, pcoords );
363 this->SetSlice( axis, ijk[ axis ] );
366 // -------------------------------------------------------------------------
367 void cpExtensions::Visualization::MPRActors::
370 for( unsigned int i = 0; i < 2; ++i )
371 for( unsigned int j = 0; j < 3; ++j )
372 this->Slices[ i ][ j ]->SetSliceNumber(
373 this->Slices[ i ][ j ]->GetSliceNumberMaxValue( )
377 // -------------------------------------------------------------------------
378 cpExtensions::Visualization::MPRActors::
382 this->ImageOutlineActor = vtkSmartPointer< vtkActor >::New( );
383 for( unsigned int i = 0; i < 2; ++i )
384 for( unsigned int j = 0; j < 3; ++j )
385 this->Slices[ i ][ j ] = vtkSmartPointer< ImageSliceActors >::New( );
387 this->Slices[ 0 ][ 0 ]->AssociateSlice( this->Slices[ 1 ][ 0 ] );
388 this->Slices[ 0 ][ 1 ]->AssociateSlice( this->Slices[ 1 ][ 1 ] );
389 this->Slices[ 0 ][ 2 ]->AssociateSlice( this->Slices[ 1 ][ 2 ] );
391 for( unsigned int i = 0; i < 3; ++ i )
393 this->Slices[ 0 ][ i ]->AddRenderCommand(
394 Self::_RenderCommand, this
396 this->Slices[ 0 ][ i ]->AddWindowLevelCommand(
397 Self::_WindowLevelCommand, this
399 this->Slices[ 0 ][ i ]->AddSlicesCommand( Self::_SlicesCommand, this );
404 // -------------------------------------------------------------------------
405 cpExtensions::Visualization::MPRActors::
410 // -------------------------------------------------------------------------
411 void cpExtensions::Visualization::MPRActors::
412 _SlicesCommand( double* pos, int axis, void* data )
414 MPRActors* actors = reinterpret_cast< MPRActors* >( data );
417 for( unsigned int j = 0; j < 3; ++j )
419 if( actors->Slices[ 0 ][ j ]->GetAxis( ) != axis )
421 actors->Slices[ 0 ][ j ]->SetSlice( pos );
422 actors->Slices[ 1 ][ j ]->SetSlice( pos );
423 actors->Slices[ 0 ][ j ]->Render( 1e-3 );
424 actors->Slices[ 1 ][ j ]->Render( 1e-3 );
431 // -------------------------------------------------------------------------
432 void cpExtensions::Visualization::MPRActors::
433 _WindowLevelCommand( double window, double level, void* data )
435 MPRActors* actors = reinterpret_cast< MPRActors* >( data );
438 actors->Slices[ 0 ][ 0 ]->Render( 1e-3 );
439 actors->Slices[ 0 ][ 1 ]->Render( 1e-3 );
440 actors->Slices[ 0 ][ 2 ]->Render( 1e-3 );
441 actors->Slices[ 1 ][ 0 ]->Render( 1e-3 );
442 actors->Slices[ 1 ][ 1 ]->Render( 1e-3 );
443 actors->Slices[ 1 ][ 2 ]->Render( 1e-3 );
446 // -------------------------------------------------------------------------
447 void cpExtensions::Visualization::MPRActors::
448 _RenderCommand( void* data )
450 MPRActors* actors = reinterpret_cast< MPRActors* >( data );
453 actors->Slices[ 0 ][ 0 ]->Render( 1e-3 );
454 actors->Slices[ 0 ][ 1 ]->Render( 1e-3 );
455 actors->Slices[ 0 ][ 2 ]->Render( 1e-3 );
456 actors->Slices[ 1 ][ 0 ]->Render( 1e-3 );
457 actors->Slices[ 1 ][ 1 ]->Render( 1e-3 );
458 actors->Slices[ 1 ][ 2 ]->Render( 1e-3 );