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 ImageBlender* blender = this->Slices[ 0 ][ 0 ]->GetBlender( );
37 this->Slices[ 0 ][ 0 ]->AddInputConnection( aout );
38 blender = this->Slices[ 0 ][ 0 ]->GetBlender( );
39 for( unsigned int i = 0; i < 2; ++i )
41 for( unsigned int j = 0; j < 3; ++j )
43 if( i != 0 || j != 0 )
44 this->Slices[ i ][ j ]->SetBlender( blender );
45 this->Slices[ i ][ j ]->SetAxis( j );
50 this->_CreateBoundingBox( );
53 blender->AddInputConnection( aout );
54 return( blender->GetNumberOfImages( ) - 1 );
57 // -------------------------------------------------------------------------
58 int cpExtensions::Visualization::MPRActors::
59 AddInputData( vtkImageData* new_image )
61 ImageBlender* blender = this->Slices[ 0 ][ 0 ]->GetBlender( );
64 this->Slices[ 0 ][ 0 ]->AddInputData( new_image );
65 blender = this->Slices[ 0 ][ 0 ]->GetBlender( );
66 for( unsigned int i = 0; i < 2; ++i )
68 for( unsigned int j = 0; j < 3; ++j )
70 if( i != 0 || j != 0 )
71 this->Slices[ i ][ j ]->SetBlender( blender );
72 this->Slices[ i ][ j ]->SetAxis( j );
77 this->_CreateBoundingBox( );
80 blender->AddInputData( new_image );
81 return( blender->GetNumberOfImages( ) - 1 );
84 // -------------------------------------------------------------------------
85 unsigned int cpExtensions::Visualization::MPRActors::
86 GetNumberOfImages( ) const
88 ImageBlender* blender = this->Slices[ 0 ][ 0 ]->GetBlender( );
90 return( blender->GetNumberOfImages( ) );
95 // -------------------------------------------------------------------------
96 void cpExtensions::Visualization::MPRActors::
98 vtkRenderWindow* x, vtkRenderWindow* y, vtkRenderWindow* z,
102 this->Slices[ 0 ][ 0 ]->PushActorsInto( x, true );
103 this->Slices[ 0 ][ 1 ]->PushActorsInto( y, true );
104 this->Slices[ 0 ][ 2 ]->PushActorsInto( z, true );
105 this->Slices[ 1 ][ 0 ]->PushActorsInto( w, false );
106 this->Slices[ 1 ][ 1 ]->PushActorsInto( w, false );
107 this->Slices[ 1 ][ 2 ]->PushActorsInto( w, false );
110 ( x != NULL )? x->GetRenderers( )->GetFirstRenderer( ): NULL;
112 ( y != NULL )? y->GetRenderers( )->GetFirstRenderer( ): NULL;
114 ( z != NULL )? z->GetRenderers( )->GetFirstRenderer( ): NULL;
116 ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
120 xren->AddActor( this->Slices[ 0 ][ 1 ]->GetPlaneActor( ) );
121 xren->AddActor( this->Slices[ 0 ][ 2 ]->GetPlaneActor( ) );
126 yren->AddActor( this->Slices[ 0 ][ 0 ]->GetPlaneActor( ) );
127 yren->AddActor( this->Slices[ 0 ][ 2 ]->GetPlaneActor( ) );
132 zren->AddActor( this->Slices[ 0 ][ 0 ]->GetPlaneActor( ) );
133 zren->AddActor( this->Slices[ 0 ][ 1 ]->GetPlaneActor( ) );
137 wren->AddActor( this->ImageOutlineActor );
140 // -------------------------------------------------------------------------
141 void cpExtensions::Visualization::MPRActors::
143 vtkRenderWindow* x, vtkRenderWindow* y, vtkRenderWindow* z,
147 this->Slices[ 0 ][ 0 ]->PopActorsFrom( x );
148 this->Slices[ 0 ][ 1 ]->PopActorsFrom( y );
149 this->Slices[ 0 ][ 2 ]->PopActorsFrom( z );
150 this->Slices[ 1 ][ 0 ]->PopActorsFrom( w );
151 this->Slices[ 1 ][ 1 ]->PopActorsFrom( w );
152 this->Slices[ 1 ][ 2 ]->PopActorsFrom( w );
155 ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
157 wren->RemoveActor( this->ImageOutlineActor );
159 this->Slices[ 0 ][ 0 ]->Clear( );
160 this->Slices[ 0 ][ 1 ]->Clear( );
161 this->Slices[ 0 ][ 2 ]->Clear( );
162 this->Slices[ 1 ][ 0 ]->Clear( );
163 this->Slices[ 1 ][ 1 ]->Clear( );
164 this->Slices[ 1 ][ 2 ]->Clear( );
167 // -------------------------------------------------------------------------
168 double cpExtensions::Visualization::MPRActors::
171 return( this->Slices[ 0 ][ 0 ]->GetWindow( ) );
174 // -------------------------------------------------------------------------
175 double cpExtensions::Visualization::MPRActors::
178 return( this->Slices[ 0 ][ 0 ]->GetLevel( ) );
181 // -------------------------------------------------------------------------
182 int cpExtensions::Visualization::MPRActors::
183 GetSliceNumberMinValue( const int& axis ) const
185 return( this->Slices[ 0 ][ axis ]->GetSliceNumberMinValue( ) );
188 // -------------------------------------------------------------------------
189 int cpExtensions::Visualization::MPRActors::
190 GetSliceNumberMaxValue( const int& axis ) const
192 return( this->Slices[ 0 ][ axis ]->GetSliceNumberMaxValue( ) );
195 // -------------------------------------------------------------------------
196 int cpExtensions::Visualization::MPRActors::
197 GetSlice( const int& axis ) const
199 return( this->Slices[ 0 ][ axis ]->GetSliceNumber( ) );
202 // -------------------------------------------------------------------------
203 void cpExtensions::Visualization::MPRActors::
204 SetSlice( const int& axis, const int& slice )
206 ImageBlender* blender = this->Slices[ 0 ][ 0 ]->GetBlender( );
207 if( blender == NULL )
209 vtkImageData* image =
210 dynamic_cast< vtkImageData* >( blender->GetInput( ) );
214 // Get image data extent
216 image->GetExtent( ext );
218 // Check if the slice is valid
220 if( slice < ext[ axis << 1 ] )
221 real = ext[ axis << 1 ];
222 if( ext[ ( axis << 1 ) + 1 ] < slice )
223 real = ext[ ( axis << 1 ) + 1 ];
226 this->Slices[ 0 ][ axis ]->SetSliceNumber( real );
227 this->Slices[ 1 ][ axis ]->SetSliceNumber( real );
231 // -------------------------------------------------------------------------
232 void cpExtensions::Visualization::MPRActors::
233 SetSlice( const int& axis, const double& slice )
235 ImageBlender* blender = this->Slices[ 0 ][ 0 ]->GetBlender( );
236 if( blender == NULL )
238 vtkImageData* image =
239 dynamic_cast< vtkImageData* >( blender->GetInput( ) );
243 double x[ 3 ] = { double( 0 ) };
248 image->ComputeStructuredCoordinates( x, ijk, pcoords );
249 this->SetSlice( axis, ijk[ axis ] );
252 // -------------------------------------------------------------------------
253 void cpExtensions::Visualization::MPRActors::
256 for( unsigned int i = 0; i < 2; ++i )
257 for( unsigned int j = 0; j < 3; ++j )
258 this->Slices[ i ][ j ]->SetSliceNumber(
259 this->Slices[ i ][ j ]->GetSliceNumberMinValue( )
263 // -------------------------------------------------------------------------
264 cpExtensions::Visualization::MPRActors::
268 this->ImageOutlineActor = vtkSmartPointer< vtkActor >::New( );
269 for( unsigned int i = 0; i < 2; ++i )
270 for( unsigned int j = 0; j < 3; ++j )
271 this->Slices[ i ][ j ] = vtkSmartPointer< ImageSliceActors >::New( );
273 this->Slices[ 0 ][ 0 ]->AssociateSlice( this->Slices[ 1 ][ 0 ] );
274 this->Slices[ 0 ][ 1 ]->AssociateSlice( this->Slices[ 1 ][ 1 ] );
275 this->Slices[ 0 ][ 2 ]->AssociateSlice( this->Slices[ 1 ][ 2 ] );
277 for( unsigned int i = 0; i < 3; ++ i )
279 this->Slices[ 0 ][ i ]->AddRenderCommand(
280 Self::_RenderCommand, this
282 this->Slices[ 0 ][ i ]->AddWindowLevelCommand(
283 Self::_WindowLevelCommand, this
285 this->Slices[ 0 ][ i ]->AddSlicesCommand( Self::_SlicesCommand, this );
290 // -------------------------------------------------------------------------
291 cpExtensions::Visualization::MPRActors::
296 // -------------------------------------------------------------------------
297 void cpExtensions::Visualization::MPRActors::
298 _CreateBoundingBox( )
300 ImageBlender* blender = this->Slices[ 0 ][ 0 ]->GetBlender( );
301 if( blender == NULL )
303 if( blender->GetNumberOfImages( ) == 1 )
305 vtkImageData* image =
306 dynamic_cast< vtkImageData* >( blender->GetInput( ) );
310 image->GetBounds( bb );
312 vtkSmartPointer< vtkOutlineSource > img_ol =
313 vtkSmartPointer< vtkOutlineSource >::New( );
314 img_ol->SetBounds( bb );
316 vtkSmartPointer< vtkPolyDataMapper > img_ol_mapper =
317 vtkSmartPointer< vtkPolyDataMapper >::New( );
318 img_ol_mapper->SetInputConnection( img_ol->GetOutputPort( ) );
319 this->ImageOutlineActor->SetMapper( img_ol_mapper );
320 this->ImageOutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
321 this->ImageOutlineActor->GetProperty( )->SetLineWidth( 1 );
323 this->AddItem( this->ImageOutlineActor );
328 // -------------------------------------------------------------------------
329 void cpExtensions::Visualization::MPRActors::
330 _SlicesCommand( double* pos, int axis, void* data )
332 MPRActors* actors = reinterpret_cast< MPRActors* >( data );
335 for( unsigned int j = 0; j < 3; ++j )
337 if( actors->Slices[ 0 ][ j ]->GetAxis( ) != axis )
339 actors->Slices[ 0 ][ j ]->SetSlice( pos );
340 actors->Slices[ 1 ][ j ]->SetSlice( pos );
341 actors->Slices[ 0 ][ j ]->Render( 1e-3 );
342 actors->Slices[ 1 ][ j ]->Render( 1e-3 );
349 // -------------------------------------------------------------------------
350 void cpExtensions::Visualization::MPRActors::
351 _WindowLevelCommand( double window, double level, void* data )
353 MPRActors* actors = reinterpret_cast< MPRActors* >( data );
356 actors->Slices[ 0 ][ 0 ]->Render( 1e-3 );
357 actors->Slices[ 0 ][ 1 ]->Render( 1e-3 );
358 actors->Slices[ 0 ][ 2 ]->Render( 1e-3 );
359 actors->Slices[ 1 ][ 0 ]->Render( 1e-3 );
360 actors->Slices[ 1 ][ 1 ]->Render( 1e-3 );
361 actors->Slices[ 1 ][ 2 ]->Render( 1e-3 );
364 // -------------------------------------------------------------------------
365 void cpExtensions::Visualization::MPRActors::
366 _RenderCommand( void* data )
368 MPRActors* actors = reinterpret_cast< MPRActors* >( data );
371 actors->Slices[ 0 ][ 0 ]->Render( 1e-3 );
372 actors->Slices[ 0 ][ 1 ]->Render( 1e-3 );
373 actors->Slices[ 0 ][ 2 ]->Render( 1e-3 );
374 actors->Slices[ 1 ][ 0 ]->Render( 1e-3 );
375 actors->Slices[ 1 ][ 1 ]->Render( 1e-3 );
376 actors->Slices[ 1 ][ 2 ]->Render( 1e-3 );