1 #include <cpPlugins/Extensions/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 cpPlugins::Extensions::Visualization::ImageSliceActors*
15 cpPlugins::Extensions::Visualization::ImageSliceActors::
18 return( new Self( ) );
21 // -------------------------------------------------------------------------
22 void cpPlugins::Extensions::Visualization::ImageSliceActors::
23 SetInputConnection( vtkAlgorithmOutput* aout, int axis )
25 this->SliceMapper->SetInputConnection( aout );
26 this->SliceMapper->SetOrientation( axis );
27 this->SliceMapper->Update( );
28 this->SetSliceNumber( this->SliceMapper->GetSliceNumber( ) );
29 this->ImageActor->SetMapper( this->SliceMapper );
30 this->ImageActor->Modified( );
34 // -------------------------------------------------------------------------
35 void cpPlugins::Extensions::Visualization::ImageSliceActors::
36 SetInputData( vtkImageData* data, int axis )
38 this->SliceMapper->SetInputData( data );
39 this->SliceMapper->SetOrientation( axis );
40 this->SliceMapper->Update( );
41 this->SetSliceNumber( this->SliceMapper->GetSliceNumber( ) );
42 this->ImageActor->SetMapper( this->SliceMapper );
43 this->ImageActor->Modified( );
47 // -------------------------------------------------------------------------
48 double* cpPlugins::Extensions::Visualization::ImageSliceActors::
49 GetDisplayBounds( ) const
51 return( this->ImageActor->GetDisplayBounds( ) );
54 // -------------------------------------------------------------------------
55 void cpPlugins::Extensions::Visualization::ImageSliceActors::
56 GetDisplayBounds( double bounds[ 6 ] ) const
58 this->ImageActor->GetDisplayBounds( bounds );
61 // -------------------------------------------------------------------------
62 int cpPlugins::Extensions::Visualization::ImageSliceActors::
65 return( this->SliceMapper->GetOrientation( ) );
68 // -------------------------------------------------------------------------
69 int cpPlugins::Extensions::Visualization::ImageSliceActors::
70 GetSliceNumber( ) const
72 return( this->SliceMapper->GetSliceNumber( ) );
75 // -------------------------------------------------------------------------
76 int cpPlugins::Extensions::Visualization::ImageSliceActors::
77 GetSliceNumberMinValue( ) const
79 return( this->SliceMapper->GetSliceNumberMinValue( ) );
82 // -------------------------------------------------------------------------
83 int cpPlugins::Extensions::Visualization::ImageSliceActors::
84 GetSliceNumberMaxValue( ) const
86 return( this->SliceMapper->GetSliceNumberMaxValue( ) );
89 // -------------------------------------------------------------------------
90 void cpPlugins::Extensions::Visualization::ImageSliceActors::
91 SetSliceNumber( const int& slice )
93 this->SliceMapper->SetSliceNumber( slice );
94 this->SliceMapper->Update( );
97 vtkAlgorithm* algo = this->SliceMapper->GetInputAlgorithm( );
98 vtkInformation* info = algo->GetOutputInformation( 0 );
100 double ori[ 3 ], spac[ 3 ], pos[ 3 ];
101 info->Get( vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT( ), ext );
102 info->Get( vtkDataObject::ORIGIN( ), ori );
103 info->Get( vtkDataObject::SPACING( ), spac );
104 this->SliceMapper->GetSlicePlane( )->GetOrigin( pos );
106 // Prevent obscuring voxels by offsetting the plane geometry
109 ori[ 0 ] + ( spac[ 0 ] * double( ext[ 0 ] ) ),
110 ori[ 0 ] + ( spac[ 0 ] * double( ext[ 1 ] ) )
114 ori[ 1 ] + ( spac[ 1 ] * double( ext[ 2 ] ) ),
115 ori[ 1 ] + ( spac[ 1 ] * double( ext[ 3 ] ) )
119 ori[ 2 ] + ( spac[ 2 ] * double( ext[ 4 ] ) ),
120 ori[ 2 ] + ( spac[ 2 ] * double( ext[ 5 ] ) )
123 if( spac[ 0 ] < double( 0 ) )
125 double t = xbnds[ 0 ];
126 xbnds[ 0 ] = xbnds[ 1 ];
130 if( spac[ 1 ] < double( 0 ) )
132 double t = ybnds[ 0 ];
133 ybnds[ 0 ] = ybnds[ 1 ];
137 if( spac[ 2 ] < double( 0 ) )
139 double t = zbnds[ 0 ];
140 zbnds[ 0 ] = zbnds[ 1 ];
145 int axis = this->SliceMapper->GetOrientation( );
146 this->PlaneActor->GetProperty( )->SetRepresentationToWireframe( );
147 this->PlaneActor->GetProperty( )->SetLineWidth( 2 );
148 vtkPoints* plane_points = this->PlaneSource->GetPoints( );
149 if( axis == 0 ) // YZ, x-normal
151 plane_points->SetPoint( 0, pos[ 0 ], ybnds[ 0 ], zbnds[ 0 ] );
152 plane_points->SetPoint( 1, pos[ 0 ], ybnds[ 1 ], zbnds[ 0 ] );
153 plane_points->SetPoint( 2, pos[ 0 ], ybnds[ 1 ], zbnds[ 1 ] );
154 plane_points->SetPoint( 3, pos[ 0 ], ybnds[ 0 ], zbnds[ 1 ] );
155 this->PlaneActor->GetProperty( )->SetColor( 1, 0, 0 );
157 else if( axis == 1 ) // ZX, y-normal
159 plane_points->SetPoint( 0, xbnds[ 0 ], pos[ 1 ], zbnds[ 0 ] );
160 plane_points->SetPoint( 1, xbnds[ 0 ], pos[ 1 ], zbnds[ 1 ] );
161 plane_points->SetPoint( 2, xbnds[ 1 ], pos[ 1 ], zbnds[ 1 ] );
162 plane_points->SetPoint( 3, xbnds[ 1 ], pos[ 1 ], zbnds[ 0 ] );
163 this->PlaneActor->GetProperty( )->SetColor( 0, 1, 0 );
167 plane_points->SetPoint( 0, xbnds[ 0 ], ybnds[ 0 ], pos[ 2 ] );
168 plane_points->SetPoint( 1, xbnds[ 1 ], ybnds[ 0 ], pos[ 2 ] );
169 plane_points->SetPoint( 2, xbnds[ 1 ], ybnds[ 1 ], pos[ 2 ] );
170 plane_points->SetPoint( 3, xbnds[ 0 ], ybnds[ 1 ], pos[ 2 ] );
171 this->PlaneActor->GetProperty( )->SetColor( 0, 0, 1 );
174 this->PlaneSource->Modified( );
175 this->PlaneMapper->Modified( );
176 this->PlaneActor->Modified( );
180 // -------------------------------------------------------------------------
181 void cpPlugins::Extensions::Visualization::ImageSliceActors::
185 int axId = this->SliceMapper->GetOrientation( );
186 if ( axId == 0 ) axis = 'X';
187 else if( axId == 1 ) axis = 'Y';
188 else if( axId == 2 ) axis = 'Z';
191 this->TextBuffer, "Axis: %c (%d)",
192 axis, this->SliceMapper->GetSliceNumber( )
194 this->TextActor->SetInput( this->TextBuffer );
195 this->TextActor->Modified( );
199 // -------------------------------------------------------------------------
200 cpPlugins::Extensions::Visualization::ImageSliceActors::
204 this->SliceMapper = vtkSmartPointer< vtkImageSliceMapper >::New( );
205 this->PlaneSource = vtkSmartPointer< vtkPolyData >::New( );
206 this->PlaneMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
207 this->ImageActor = vtkSmartPointer< vtkImageActor >::New( );
208 this->TextActor = vtkSmartPointer< vtkTextActor >::New( );
209 this->PlaneActor = vtkSmartPointer< vtkActor >::New( );
211 this->ImageActorIndex = this->GetNumberOfItems( );
212 this->TextActorIndex = this->ImageActorIndex + 1;
213 this->PlaneActorIndex = this->ImageActorIndex + 2;
214 this->AddItem( this->ImageActor );
215 this->AddItem( this->TextActor );
216 this->AddItem( this->PlaneActor );
219 vtkSmartPointer< vtkPoints > plane_points =
220 vtkSmartPointer< vtkPoints >::New( );
221 vtkSmartPointer< vtkCellArray > plane_lines =
222 vtkSmartPointer< vtkCellArray >::New( );
224 plane_points->InsertNextPoint( 0, 0, 0 );
225 plane_points->InsertNextPoint( 0, 1, 0 );
226 plane_points->InsertNextPoint( 1, 1, 0 );
227 plane_points->InsertNextPoint( 1, 0, 0 );
228 plane_lines->InsertNextCell( 5 );
229 plane_lines->InsertCellPoint( 0 );
230 plane_lines->InsertCellPoint( 1 );
231 plane_lines->InsertCellPoint( 2 );
232 plane_lines->InsertCellPoint( 3 );
233 plane_lines->InsertCellPoint( 0 );
234 this->PlaneSource->SetPoints( plane_points );
235 this->PlaneSource->SetLines( plane_lines );
237 this->PlaneMapper->SetInputData( this->PlaneSource );
238 this->PlaneActor->SetMapper( this->PlaneMapper );
240 this->TextActor->SetTextScaleModeToNone( );
241 vtkTextProperty* textprop = this->TextActor->GetTextProperty( );
242 textprop->SetColor( 1, 1, 1 );
243 textprop->SetFontFamilyToCourier( );
244 textprop->SetFontSize( 18 );
245 textprop->BoldOff( );
246 textprop->ItalicOff( );
247 textprop->ShadowOff( );
248 textprop->SetJustificationToLeft( );
249 textprop->SetVerticalJustificationToBottom( );
250 vtkCoordinate* coord = this->TextActor->GetPositionCoordinate( );
251 coord->SetCoordinateSystemToNormalizedViewport( );
252 coord->SetValue( 0.01, 0.01 );
255 // -------------------------------------------------------------------------
256 cpPlugins::Extensions::Visualization::ImageSliceActors::