1 #include <cpExtensions/Visualization/ImageSliceActors.h>
3 #include <vtkAlgorithmOutput.h>
4 #include <vtkCellArray.h>
5 #include <vtkImageData.h>
6 #include <vtkInformation.h>
9 #include <vtkProperty.h>
10 #include <vtkStreamingDemandDrivenPipeline.h>
11 #include <vtkTextProperty.h>
13 // -------------------------------------------------------------------------
14 cpExtensions::Visualization::ImageSliceActors*
15 cpExtensions::Visualization::ImageSliceActors::
18 return( new Self( ) );
21 // -------------------------------------------------------------------------
22 void cpExtensions::Visualization::ImageSliceActors::
23 AddInputConnection( vtkAlgorithmOutput* aout, int axis )
25 unsigned int nImages = this->SliceMappers.size( );
27 vtkSmartPointer< vtkImageSliceMapper > mapper =
28 vtkSmartPointer< vtkImageSliceMapper >::New( );
29 this->SliceMappers.push_back( mapper );
30 mapper->SetInputConnection( aout );
31 mapper->SetOrientation(
32 ( nImages == 0 )? axis: this->SliceMappers[ 0 ]->GetOrientation( )
36 this->SetSliceNumber( this->SliceMappers[ 0 ]->GetSliceNumber( ) );
38 vtkSmartPointer< vtkImageActor > actor =
39 vtkSmartPointer< vtkImageActor >::New( );
40 this->ImageActors.push_back( actor );
41 actor->SetMapper( mapper );
46 this->AddItem( this->TextActor );
47 this->AddItem( this->PlaneActor );
50 this->AddItem( actor );
54 // -------------------------------------------------------------------------
55 void cpExtensions::Visualization::ImageSliceActors::
56 AddInputData( vtkImageData* data, int axis )
58 unsigned int nImages = this->SliceMappers.size( );
60 vtkSmartPointer< vtkImageSliceMapper > mapper =
61 vtkSmartPointer< vtkImageSliceMapper >::New( );
62 this->SliceMappers.push_back( mapper );
63 mapper->SetInputData( data );
64 mapper->SetOrientation(
65 ( nImages == 0 )? axis: this->SliceMappers[ 0 ]->GetOrientation( )
69 this->SetSliceNumber( this->SliceMappers[ 0 ]->GetSliceNumber( ) );
71 vtkSmartPointer< vtkImageActor > actor =
72 vtkSmartPointer< vtkImageActor >::New( );
73 this->ImageActors.push_back( actor );
74 actor->SetMapper( mapper );
79 this->AddItem( this->TextActor );
80 this->AddItem( this->PlaneActor );
83 this->AddItem( actor );
87 // -------------------------------------------------------------------------
88 void cpExtensions::Visualization::ImageSliceActors::
91 // Unbind from container
92 this->RemoveAllItems( );
95 this->SliceMappers.clear( );
96 this->ImageActors.clear( );
98 // Reconfigure unique objects
99 this->PlaneSource = vtkSmartPointer< vtkPolyData >::New( );
100 this->PlaneMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
101 this->TextActor = vtkSmartPointer< vtkTextActor >::New( );
102 this->PlaneActor = vtkSmartPointer< vtkActor >::New( );
103 this->TextBuffer[ 0 ] = '\0';
105 // Unique objects configuration
106 vtkSmartPointer< vtkPoints > plane_points =
107 vtkSmartPointer< vtkPoints >::New( );
108 vtkSmartPointer< vtkCellArray > plane_lines =
109 vtkSmartPointer< vtkCellArray >::New( );
111 plane_points->InsertNextPoint( 0, 0, 0 );
112 plane_points->InsertNextPoint( 0, 1, 0 );
113 plane_points->InsertNextPoint( 1, 1, 0 );
114 plane_points->InsertNextPoint( 1, 0, 0 );
115 plane_lines->InsertNextCell( 5 );
116 plane_lines->InsertCellPoint( 0 );
117 plane_lines->InsertCellPoint( 1 );
118 plane_lines->InsertCellPoint( 2 );
119 plane_lines->InsertCellPoint( 3 );
120 plane_lines->InsertCellPoint( 0 );
121 this->PlaneSource->SetPoints( plane_points );
122 this->PlaneSource->SetLines( plane_lines );
124 this->PlaneMapper->SetInputData( this->PlaneSource );
125 this->PlaneActor->SetMapper( this->PlaneMapper );
127 this->TextActor->SetTextScaleModeToNone( );
128 vtkTextProperty* textprop = this->TextActor->GetTextProperty( );
129 textprop->SetColor( 1, 1, 1 );
130 textprop->SetFontFamilyToCourier( );
131 textprop->SetFontSize( 18 );
132 textprop->BoldOff( );
133 textprop->ItalicOff( );
134 textprop->ShadowOff( );
135 textprop->SetJustificationToLeft( );
136 textprop->SetVerticalJustificationToBottom( );
137 vtkCoordinate* coord = this->TextActor->GetPositionCoordinate( );
138 coord->SetCoordinateSystemToNormalizedViewport( );
139 coord->SetValue( 0.01, 0.01 );
142 // -------------------------------------------------------------------------
143 unsigned int cpExtensions::Visualization::ImageSliceActors::
144 GetNumberOfImageActors( ) const
146 return( this->ImageActors.size( ) );
149 // -------------------------------------------------------------------------
150 vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
151 GetImageActor( unsigned int id )
153 if( id < this->ImageActors.size( ) )
154 return( this->ImageActors[ id ] );
159 // -------------------------------------------------------------------------
160 const vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
161 GetImageActor( unsigned int id ) const
163 if( id < this->ImageActors.size( ) )
164 return( this->ImageActors[ id ] );
169 // -------------------------------------------------------------------------
170 vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
173 return( this->TextActor );
176 // -------------------------------------------------------------------------
177 const vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
178 GetTextActor( ) const
180 return( this->TextActor );
183 // -------------------------------------------------------------------------
184 vtkActor* cpExtensions::Visualization::ImageSliceActors::
187 return( this->PlaneActor );
190 // -------------------------------------------------------------------------
191 const vtkActor* cpExtensions::Visualization::ImageSliceActors::
192 GetPlaneActor( ) const
194 return( this->PlaneActor );
197 // -------------------------------------------------------------------------
198 double* cpExtensions::Visualization::ImageSliceActors::
199 GetDisplayBounds( ) const
201 if( this->ImageActors.size( ) > 0 )
202 return( this->ImageActors[ 0 ]->GetDisplayBounds( ) );
207 // -------------------------------------------------------------------------
208 void cpExtensions::Visualization::ImageSliceActors::
209 GetDisplayBounds( double bounds[ 6 ] ) const
211 if( this->ImageActors.size( ) > 0 )
212 this->ImageActors[ 0 ]->GetDisplayBounds( bounds );
215 // -------------------------------------------------------------------------
216 int cpExtensions::Visualization::ImageSliceActors::
219 if( this->SliceMappers.size( ) > 0 )
220 return( this->SliceMappers[ 0 ]->GetOrientation( ) );
225 // -------------------------------------------------------------------------
226 int cpExtensions::Visualization::ImageSliceActors::
227 GetSliceNumber( ) const
229 if( this->SliceMappers.size( ) > 0 )
230 return( this->SliceMappers[ 0 ]->GetSliceNumber( ) );
235 // -------------------------------------------------------------------------
236 int cpExtensions::Visualization::ImageSliceActors::
237 GetSliceNumberMinValue( ) const
239 if( this->SliceMappers.size( ) > 0 )
240 return( this->SliceMappers[ 0 ]->GetSliceNumberMinValue( ) );
245 // -------------------------------------------------------------------------
246 int cpExtensions::Visualization::ImageSliceActors::
247 GetSliceNumberMaxValue( ) const
249 if( this->SliceMappers.size( ) > 0 )
250 return( this->SliceMappers[ 0 ]->GetSliceNumberMaxValue( ) );
255 // -------------------------------------------------------------------------
256 void cpExtensions::Visualization::ImageSliceActors::
257 SetSliceNumber( const int& slice )
259 unsigned int nImages = this->SliceMappers.size( );
263 // Change visualization extent
264 for( unsigned int i = 0; i < nImages; ++i )
266 this->SliceMappers[ i ]->SetSliceNumber( slice );
267 this->SliceMappers[ i ]->Modified( );
268 this->ImageActors[ i ]->Modified( );
269 this->SliceMappers[ i ]->Update( );
274 vtkAlgorithm* algo = this->SliceMappers[ 0 ]->GetInputAlgorithm( );
275 vtkInformation* info = algo->GetOutputInformation( 0 );
277 double ori[ 3 ], spac[ 3 ], pos[ 3 ];
278 info->Get( vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT( ), ext );
279 info->Get( vtkDataObject::ORIGIN( ), ori );
280 info->Get( vtkDataObject::SPACING( ), spac );
281 this->SliceMappers[ 0 ]->GetSlicePlane( )->GetOrigin( pos );
283 // Prevent obscuring voxels by offsetting the plane geometry
286 ori[ 0 ] + ( spac[ 0 ] * double( ext[ 0 ] ) ),
287 ori[ 0 ] + ( spac[ 0 ] * double( ext[ 1 ] ) )
291 ori[ 1 ] + ( spac[ 1 ] * double( ext[ 2 ] ) ),
292 ori[ 1 ] + ( spac[ 1 ] * double( ext[ 3 ] ) )
296 ori[ 2 ] + ( spac[ 2 ] * double( ext[ 4 ] ) ),
297 ori[ 2 ] + ( spac[ 2 ] * double( ext[ 5 ] ) )
300 if( spac[ 0 ] < double( 0 ) )
302 double t = xbnds[ 0 ];
303 xbnds[ 0 ] = xbnds[ 1 ];
307 if( spac[ 1 ] < double( 0 ) )
309 double t = ybnds[ 0 ];
310 ybnds[ 0 ] = ybnds[ 1 ];
314 if( spac[ 2 ] < double( 0 ) )
316 double t = zbnds[ 0 ];
317 zbnds[ 0 ] = zbnds[ 1 ];
322 int axis = this->SliceMappers[ 0 ]->GetOrientation( );
323 this->PlaneActor->GetProperty( )->SetRepresentationToWireframe( );
324 this->PlaneActor->GetProperty( )->SetLineWidth( 2 );
325 vtkPoints* plane_points = this->PlaneSource->GetPoints( );
326 if( axis == 0 ) // YZ, x-normal
328 plane_points->SetPoint( 0, pos[ 0 ], ybnds[ 0 ], zbnds[ 0 ] );
329 plane_points->SetPoint( 1, pos[ 0 ], ybnds[ 1 ], zbnds[ 0 ] );
330 plane_points->SetPoint( 2, pos[ 0 ], ybnds[ 1 ], zbnds[ 1 ] );
331 plane_points->SetPoint( 3, pos[ 0 ], ybnds[ 0 ], zbnds[ 1 ] );
332 this->PlaneActor->GetProperty( )->SetColor( 1, 0, 0 );
334 else if( axis == 1 ) // ZX, y-normal
336 plane_points->SetPoint( 0, xbnds[ 0 ], pos[ 1 ], zbnds[ 0 ] );
337 plane_points->SetPoint( 1, xbnds[ 0 ], pos[ 1 ], zbnds[ 1 ] );
338 plane_points->SetPoint( 2, xbnds[ 1 ], pos[ 1 ], zbnds[ 1 ] );
339 plane_points->SetPoint( 3, xbnds[ 1 ], pos[ 1 ], zbnds[ 0 ] );
340 this->PlaneActor->GetProperty( )->SetColor( 0, 1, 0 );
344 plane_points->SetPoint( 0, xbnds[ 0 ], ybnds[ 0 ], pos[ 2 ] );
345 plane_points->SetPoint( 1, xbnds[ 1 ], ybnds[ 0 ], pos[ 2 ] );
346 plane_points->SetPoint( 2, xbnds[ 1 ], ybnds[ 1 ], pos[ 2 ] );
347 plane_points->SetPoint( 3, xbnds[ 0 ], ybnds[ 1 ], pos[ 2 ] );
348 this->PlaneActor->GetProperty( )->SetColor( 0, 0, 1 );
351 this->PlaneSource->Modified( );
352 this->PlaneMapper->Modified( );
353 this->PlaneActor->Modified( );
357 // -------------------------------------------------------------------------
358 void cpExtensions::Visualization::ImageSliceActors::
361 if( this->SliceMappers.size( ) > 0 )
364 int axId = this->SliceMappers[ 0 ]->GetOrientation( );
365 if ( axId == 0 ) axis = 'X';
366 else if( axId == 1 ) axis = 'Y';
367 else if( axId == 2 ) axis = 'Z';
370 this->TextBuffer, "Axis: %c (%d)",
371 axis, this->SliceMappers[ 0 ]->GetSliceNumber( )
375 this->TextBuffer[ 0 ] = '\0';
376 this->TextActor->SetInput( this->TextBuffer );
377 this->TextActor->Modified( );
381 // -------------------------------------------------------------------------
382 cpExtensions::Visualization::ImageSliceActors::
389 // -------------------------------------------------------------------------
390 cpExtensions::Visualization::ImageSliceActors::