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 vtkSmartPointer< vtkImageActor > actor =
37 vtkSmartPointer< vtkImageActor >::New( );
38 this->ImageActors.push_back( actor );
39 actor->SetMapper( mapper );
44 this->AddItem( this->TextActor );
45 this->AddItem( this->PlaneActor );
48 this->AddItem( actor );
52 // -------------------------------------------------------------------------
53 void cpExtensions::Visualization::ImageSliceActors::
54 AddInputData( vtkImageData* data, int axis )
56 unsigned int nImages = this->SliceMappers.size( );
58 vtkSmartPointer< vtkImageSliceMapper > mapper =
59 vtkSmartPointer< vtkImageSliceMapper >::New( );
60 this->SliceMappers.push_back( mapper );
61 mapper->SetInputData( data );
62 mapper->SetOrientation(
63 ( nImages == 0 )? axis: this->SliceMappers[ 0 ]->GetOrientation( )
67 vtkSmartPointer< vtkImageActor > actor =
68 vtkSmartPointer< vtkImageActor >::New( );
69 this->ImageActors.push_back( actor );
70 actor->SetMapper( mapper );
75 this->AddItem( this->TextActor );
76 this->AddItem( this->PlaneActor );
79 this->AddItem( actor );
83 // -------------------------------------------------------------------------
84 void cpExtensions::Visualization::ImageSliceActors::
87 // Unbind from container
88 this->RemoveAllItems( );
91 this->SliceMappers.clear( );
92 this->ImageActors.clear( );
94 // Reconfigure unique objects
95 this->PlaneSource = vtkSmartPointer< vtkPolyData >::New( );
96 this->PlaneMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
97 this->TextActor = vtkSmartPointer< vtkTextActor >::New( );
98 this->PlaneActor = vtkSmartPointer< vtkActor >::New( );
99 this->TextBuffer[ 0 ] = '\0';
101 // Unique objects configuration
102 vtkSmartPointer< vtkPoints > plane_points =
103 vtkSmartPointer< vtkPoints >::New( );
104 vtkSmartPointer< vtkCellArray > plane_lines =
105 vtkSmartPointer< vtkCellArray >::New( );
107 plane_points->InsertNextPoint( 0, 0, 0 );
108 plane_points->InsertNextPoint( 0, 1, 0 );
109 plane_points->InsertNextPoint( 1, 1, 0 );
110 plane_points->InsertNextPoint( 1, 0, 0 );
111 plane_lines->InsertNextCell( 5 );
112 plane_lines->InsertCellPoint( 0 );
113 plane_lines->InsertCellPoint( 1 );
114 plane_lines->InsertCellPoint( 2 );
115 plane_lines->InsertCellPoint( 3 );
116 plane_lines->InsertCellPoint( 0 );
117 this->PlaneSource->SetPoints( plane_points );
118 this->PlaneSource->SetLines( plane_lines );
120 this->PlaneMapper->SetInputData( this->PlaneSource );
121 this->PlaneActor->SetMapper( this->PlaneMapper );
123 this->TextActor->SetTextScaleModeToNone( );
124 vtkTextProperty* textprop = this->TextActor->GetTextProperty( );
125 textprop->SetColor( 1, 1, 1 );
126 textprop->SetFontFamilyToCourier( );
127 textprop->SetFontSize( 18 );
128 textprop->BoldOff( );
129 textprop->ItalicOff( );
130 textprop->ShadowOff( );
131 textprop->SetJustificationToLeft( );
132 textprop->SetVerticalJustificationToBottom( );
133 vtkCoordinate* coord = this->TextActor->GetPositionCoordinate( );
134 coord->SetCoordinateSystemToNormalizedViewport( );
135 coord->SetValue( 0.01, 0.01 );
138 // -------------------------------------------------------------------------
139 unsigned int cpExtensions::Visualization::ImageSliceActors::
140 GetNumberOfImageActors( ) const
142 return( this->ImageActors.size( ) );
145 // -------------------------------------------------------------------------
146 vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
147 GetImageActor( unsigned int id )
149 if( id < this->ImageActors.size( ) )
150 return( this->ImageActors[ id ] );
155 // -------------------------------------------------------------------------
156 const vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
157 GetImageActor( unsigned int id ) const
159 if( id < this->ImageActors.size( ) )
160 return( this->ImageActors[ id ] );
165 // -------------------------------------------------------------------------
166 vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
169 return( this->TextActor );
172 // -------------------------------------------------------------------------
173 const vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
174 GetTextActor( ) const
176 return( this->TextActor );
179 // -------------------------------------------------------------------------
180 vtkActor* cpExtensions::Visualization::ImageSliceActors::
183 return( this->PlaneActor );
186 // -------------------------------------------------------------------------
187 const vtkActor* cpExtensions::Visualization::ImageSliceActors::
188 GetPlaneActor( ) const
190 return( this->PlaneActor );
193 // -------------------------------------------------------------------------
194 double* cpExtensions::Visualization::ImageSliceActors::
195 GetDisplayBounds( ) const
197 if( this->ImageActors.size( ) > 0 )
198 return( this->ImageActors[ 0 ]->GetDisplayBounds( ) );
203 // -------------------------------------------------------------------------
204 void cpExtensions::Visualization::ImageSliceActors::
205 GetDisplayBounds( double bounds[ 6 ] ) const
207 if( this->ImageActors.size( ) > 0 )
208 this->ImageActors[ 0 ]->GetDisplayBounds( bounds );
211 // -------------------------------------------------------------------------
212 int cpExtensions::Visualization::ImageSliceActors::
215 if( this->SliceMappers.size( ) > 0 )
216 return( this->SliceMappers[ 0 ]->GetOrientation( ) );
221 // -------------------------------------------------------------------------
222 int cpExtensions::Visualization::ImageSliceActors::
223 GetSliceNumber( ) const
225 if( this->SliceMappers.size( ) > 0 )
226 return( this->SliceMappers[ 0 ]->GetSliceNumber( ) );
231 // -------------------------------------------------------------------------
232 int cpExtensions::Visualization::ImageSliceActors::
233 GetSliceNumberMinValue( ) const
235 if( this->SliceMappers.size( ) > 0 )
236 return( this->SliceMappers[ 0 ]->GetSliceNumberMinValue( ) );
241 // -------------------------------------------------------------------------
242 int cpExtensions::Visualization::ImageSliceActors::
243 GetSliceNumberMaxValue( ) const
245 if( this->SliceMappers.size( ) > 0 )
246 return( this->SliceMappers[ 0 ]->GetSliceNumberMaxValue( ) );
251 // -------------------------------------------------------------------------
252 void cpExtensions::Visualization::ImageSliceActors::
253 SetSliceNumber( const int& slice )
255 unsigned int nImages = this->SliceMappers.size( );
259 // Change visualization extent
260 for( unsigned int i = 0; i < nImages; ++i )
262 this->SliceMappers[ i ]->SetSliceNumber( slice );
263 this->SliceMappers[ i ]->Modified( );
264 this->ImageActors[ i ]->Modified( );
265 this->SliceMappers[ i ]->Update( );
270 vtkAlgorithm* algo = this->SliceMappers[ 0 ]->GetInputAlgorithm( );
271 vtkInformation* info = algo->GetOutputInformation( 0 );
273 double ori[ 3 ], spac[ 3 ], pos[ 3 ];
274 info->Get( vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT( ), ext );
275 info->Get( vtkDataObject::ORIGIN( ), ori );
276 info->Get( vtkDataObject::SPACING( ), spac );
277 this->SliceMappers[ 0 ]->GetSlicePlane( )->GetOrigin( pos );
279 // Prevent obscuring voxels by offsetting the plane geometry
282 ori[ 0 ] + ( spac[ 0 ] * double( ext[ 0 ] ) ),
283 ori[ 0 ] + ( spac[ 0 ] * double( ext[ 1 ] ) )
287 ori[ 1 ] + ( spac[ 1 ] * double( ext[ 2 ] ) ),
288 ori[ 1 ] + ( spac[ 1 ] * double( ext[ 3 ] ) )
292 ori[ 2 ] + ( spac[ 2 ] * double( ext[ 4 ] ) ),
293 ori[ 2 ] + ( spac[ 2 ] * double( ext[ 5 ] ) )
296 if( spac[ 0 ] < double( 0 ) )
298 double t = xbnds[ 0 ];
299 xbnds[ 0 ] = xbnds[ 1 ];
303 if( spac[ 1 ] < double( 0 ) )
305 double t = ybnds[ 0 ];
306 ybnds[ 0 ] = ybnds[ 1 ];
310 if( spac[ 2 ] < double( 0 ) )
312 double t = zbnds[ 0 ];
313 zbnds[ 0 ] = zbnds[ 1 ];
318 int axis = this->SliceMappers[ 0 ]->GetOrientation( );
319 this->PlaneActor->GetProperty( )->SetRepresentationToWireframe( );
320 this->PlaneActor->GetProperty( )->SetLineWidth( 2 );
321 vtkPoints* plane_points = this->PlaneSource->GetPoints( );
322 if( axis == 0 ) // YZ, x-normal
324 plane_points->SetPoint( 0, pos[ 0 ], ybnds[ 0 ], zbnds[ 0 ] );
325 plane_points->SetPoint( 1, pos[ 0 ], ybnds[ 1 ], zbnds[ 0 ] );
326 plane_points->SetPoint( 2, pos[ 0 ], ybnds[ 1 ], zbnds[ 1 ] );
327 plane_points->SetPoint( 3, pos[ 0 ], ybnds[ 0 ], zbnds[ 1 ] );
328 this->PlaneActor->GetProperty( )->SetColor( 1, 0, 0 );
330 else if( axis == 1 ) // ZX, y-normal
332 plane_points->SetPoint( 0, xbnds[ 0 ], pos[ 1 ], zbnds[ 0 ] );
333 plane_points->SetPoint( 1, xbnds[ 0 ], pos[ 1 ], zbnds[ 1 ] );
334 plane_points->SetPoint( 2, xbnds[ 1 ], pos[ 1 ], zbnds[ 1 ] );
335 plane_points->SetPoint( 3, xbnds[ 1 ], pos[ 1 ], zbnds[ 0 ] );
336 this->PlaneActor->GetProperty( )->SetColor( 0, 1, 0 );
340 plane_points->SetPoint( 0, xbnds[ 0 ], ybnds[ 0 ], pos[ 2 ] );
341 plane_points->SetPoint( 1, xbnds[ 1 ], ybnds[ 0 ], pos[ 2 ] );
342 plane_points->SetPoint( 2, xbnds[ 1 ], ybnds[ 1 ], pos[ 2 ] );
343 plane_points->SetPoint( 3, xbnds[ 0 ], ybnds[ 1 ], pos[ 2 ] );
344 this->PlaneActor->GetProperty( )->SetColor( 0, 0, 1 );
347 this->PlaneSource->Modified( );
348 this->PlaneMapper->Modified( );
349 this->PlaneActor->Modified( );
353 // -------------------------------------------------------------------------
354 void cpExtensions::Visualization::ImageSliceActors::
357 if( this->SliceMappers.size( ) > 0 )
360 int axId = this->SliceMappers[ 0 ]->GetOrientation( );
361 if ( axId == 0 ) axis = 'X';
362 else if( axId == 1 ) axis = 'Y';
363 else if( axId == 2 ) axis = 'Z';
366 this->TextBuffer, "Axis: %c (%d)",
367 axis, this->SliceMappers[ 0 ]->GetSliceNumber( )
371 this->TextBuffer[ 0 ] = '\0';
372 this->TextActor->SetInput( this->TextBuffer );
373 this->TextActor->Modified( );
377 // -------------------------------------------------------------------------
378 cpExtensions::Visualization::ImageSliceActors::
385 // -------------------------------------------------------------------------
386 cpExtensions::Visualization::ImageSliceActors::