1 #include <cpPlugins/Extensions/Visualization/ImageSliceActors.h>
3 #include <vtkAlgorithmOutput.h>
4 #include <vtkCellArray.h>
5 #include <vtkInformation.h>
8 #include <vtkProperty.h>
9 #include <vtkStreamingDemandDrivenPipeline.h>
10 #include <vtkTextProperty.h>
12 // -------------------------------------------------------------------------
13 cpPlugins::Extensions::Visualization::ImageSliceActors*
14 cpPlugins::Extensions::Visualization::ImageSliceActors::
17 return( new Self( ) );
20 // -------------------------------------------------------------------------
21 void cpPlugins::Extensions::Visualization::ImageSliceActors::
22 SetInputConnection( vtkAlgorithmOutput* aout, int axis )
24 this->InputAlgorithm = aout;
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 SetSegmentationConnection( vtkAlgorithmOutput* aout )
38 this->SegmentationAlgorithm = aout;
39 this->SegmentationSliceMapper->SetInputConnection( aout );
40 this->SegmentationSliceMapper->
41 SetOrientation( this->SliceMapper->GetOrientation( ) );
42 this->SegmentationSliceMapper->
43 SetSliceNumber( this->SliceMapper->GetSliceNumber( ) );
44 this->SegmentationActor->SetMapper( this->SegmentationSliceMapper );
45 this->SegmentationActor->Modified( );
49 // -------------------------------------------------------------------------
50 double* cpPlugins::Extensions::Visualization::ImageSliceActors::
51 GetDisplayBounds( ) const
53 return( this->ImageActor->GetDisplayBounds( ) );
56 // -------------------------------------------------------------------------
57 void cpPlugins::Extensions::Visualization::ImageSliceActors::
58 GetDisplayBounds( double bounds[ 6 ] ) const
60 this->ImageActor->GetDisplayBounds( bounds );
63 // -------------------------------------------------------------------------
64 int cpPlugins::Extensions::Visualization::ImageSliceActors::
67 return( this->SliceMapper->GetOrientation( ) );
70 // -------------------------------------------------------------------------
71 int cpPlugins::Extensions::Visualization::ImageSliceActors::
72 GetSliceNumber( ) const
74 return( this->SliceMapper->GetSliceNumber( ) );
77 // -------------------------------------------------------------------------
78 int cpPlugins::Extensions::Visualization::ImageSliceActors::
79 GetSliceNumberMinValue( ) const
81 return( this->SliceMapper->GetSliceNumberMinValue( ) );
84 // -------------------------------------------------------------------------
85 int cpPlugins::Extensions::Visualization::ImageSliceActors::
86 GetSliceNumberMaxValue( ) const
88 return( this->SliceMapper->GetSliceNumberMaxValue( ) );
91 // -------------------------------------------------------------------------
92 void cpPlugins::Extensions::Visualization::ImageSliceActors::
93 SetSliceNumber( const int& slice )
95 if( this->InputAlgorithm == NULL )
97 this->SliceMapper->SetSliceNumber( slice );
98 this->SliceMapper->Update( );
100 if( this->SegmentationAlgorithm != NULL )
102 this->SegmentationSliceMapper->SetSliceNumber( slice );
103 this->SegmentationSliceMapper->Update( );
108 vtkAlgorithm* algo = this->SliceMapper->GetInputAlgorithm( );
109 vtkInformation* info = algo->GetOutputInformation( 0 );
111 double ori[ 3 ], spac[ 3 ], pos[ 3 ];
112 info->Get( vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT( ), ext );
113 info->Get( vtkDataObject::ORIGIN( ), ori );
114 info->Get( vtkDataObject::SPACING( ), spac );
115 this->SliceMapper->GetSlicePlane( )->GetOrigin( pos );
117 // Prevent obscuring voxels by offsetting the plane geometry
120 ori[ 0 ] + ( spac[ 0 ] * double( ext[ 0 ] ) ),
121 ori[ 0 ] + ( spac[ 0 ] * double( ext[ 1 ] ) )
125 ori[ 1 ] + ( spac[ 1 ] * double( ext[ 2 ] ) ),
126 ori[ 1 ] + ( spac[ 1 ] * double( ext[ 3 ] ) )
130 ori[ 2 ] + ( spac[ 2 ] * double( ext[ 4 ] ) ),
131 ori[ 2 ] + ( spac[ 2 ] * double( ext[ 5 ] ) )
134 if( spac[ 0 ] < double( 0 ) )
136 double t = xbnds[ 0 ];
137 xbnds[ 0 ] = xbnds[ 1 ];
141 if( spac[ 1 ] < double( 0 ) )
143 double t = ybnds[ 0 ];
144 ybnds[ 0 ] = ybnds[ 1 ];
148 if( spac[ 2 ] < double( 0 ) )
150 double t = zbnds[ 0 ];
151 zbnds[ 0 ] = zbnds[ 1 ];
156 int axis = this->SliceMapper->GetOrientation( );
157 this->PlaneActor->GetProperty( )->SetRepresentationToWireframe( );
158 this->PlaneActor->GetProperty( )->SetLineWidth( 2 );
159 vtkPoints* plane_points = this->PlaneSource->GetPoints( );
160 if( axis == 0 ) // YZ, x-normal
162 plane_points->SetPoint( 0, pos[ 0 ], ybnds[ 0 ], zbnds[ 0 ] );
163 plane_points->SetPoint( 1, pos[ 0 ], ybnds[ 1 ], zbnds[ 0 ] );
164 plane_points->SetPoint( 2, pos[ 0 ], ybnds[ 1 ], zbnds[ 1 ] );
165 plane_points->SetPoint( 3, pos[ 0 ], ybnds[ 0 ], zbnds[ 1 ] );
166 this->PlaneActor->GetProperty( )->SetColor( 1, 0, 0 );
168 else if( axis == 1 ) // ZX, y-normal
170 plane_points->SetPoint( 0, xbnds[ 0 ], pos[ 1 ], zbnds[ 0 ] );
171 plane_points->SetPoint( 1, xbnds[ 0 ], pos[ 1 ], zbnds[ 1 ] );
172 plane_points->SetPoint( 2, xbnds[ 1 ], pos[ 1 ], zbnds[ 1 ] );
173 plane_points->SetPoint( 3, xbnds[ 1 ], pos[ 1 ], zbnds[ 0 ] );
174 this->PlaneActor->GetProperty( )->SetColor( 0, 1, 0 );
178 plane_points->SetPoint( 0, xbnds[ 0 ], ybnds[ 0 ], pos[ 2 ] );
179 plane_points->SetPoint( 1, xbnds[ 1 ], ybnds[ 0 ], pos[ 2 ] );
180 plane_points->SetPoint( 2, xbnds[ 1 ], ybnds[ 1 ], pos[ 2 ] );
181 plane_points->SetPoint( 3, xbnds[ 0 ], ybnds[ 1 ], pos[ 2 ] );
182 this->PlaneActor->GetProperty( )->SetColor( 0, 0, 1 );
185 this->PlaneSource->Modified( );
186 this->PlaneMapper->Modified( );
187 this->PlaneActor->Modified( );
191 // -------------------------------------------------------------------------
192 void cpPlugins::Extensions::Visualization::ImageSliceActors::
193 UpdateText( const double& w, const double& l )
196 int axId = this->SliceMapper->GetOrientation( );
197 if ( axId == 0 ) axis = 'X';
198 else if( axId == 1 ) axis = 'Y';
199 else if( axId == 2 ) axis = 'Z';
202 this->TextBuffer, "Axis: %c (%d)\nWin/Lev: %.2f/%.2f",
203 axis, this->SliceMapper->GetSliceNumber( ), w, l
205 this->TextActor->SetInput( this->TextBuffer );
206 this->TextActor->Modified( );
210 // -------------------------------------------------------------------------
211 cpPlugins::Extensions::Visualization::ImageSliceActors::
214 InputAlgorithm( NULL ),
215 SegmentationAlgorithm( NULL )
217 this->SliceMapper = vtkSmartPointer< vtkImageSliceMapper >::New( );
218 this->SegmentationSliceMapper =
219 vtkSmartPointer< vtkImageSliceMapper >::New( );
220 this->PlaneSource = vtkSmartPointer< vtkPolyData >::New( );
221 this->PlaneMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
222 this->ImageActor = vtkSmartPointer< vtkImageActor >::New( );
223 this->SegmentationActor = vtkSmartPointer< vtkImageActor >::New( );
224 this->TextActor = vtkSmartPointer< vtkTextActor >::New( );
225 this->PlaneActor = vtkSmartPointer< vtkActor >::New( );
227 this->ImageActorIndex = this->GetNumberOfItems( );
228 this->SegmentationActorIndex = this->ImageActorIndex + 1;
229 this->TextActorIndex = this->ImageActorIndex + 2;
230 this->PlaneActorIndex = this->ImageActorIndex + 3;
231 this->AddItem( this->ImageActor );
232 this->AddItem( this->SegmentationActor );
233 this->AddItem( this->TextActor );
234 this->AddItem( this->PlaneActor );
237 vtkSmartPointer< vtkPoints > plane_points =
238 vtkSmartPointer< vtkPoints >::New( );
239 vtkSmartPointer< vtkCellArray > plane_lines =
240 vtkSmartPointer< vtkCellArray >::New( );
242 plane_points->InsertNextPoint( 0, 0, 0 );
243 plane_points->InsertNextPoint( 0, 1, 0 );
244 plane_points->InsertNextPoint( 1, 1, 0 );
245 plane_points->InsertNextPoint( 1, 0, 0 );
246 plane_lines->InsertNextCell( 5 );
247 plane_lines->InsertCellPoint( 0 );
248 plane_lines->InsertCellPoint( 1 );
249 plane_lines->InsertCellPoint( 2 );
250 plane_lines->InsertCellPoint( 3 );
251 plane_lines->InsertCellPoint( 0 );
252 this->PlaneSource->SetPoints( plane_points );
253 this->PlaneSource->SetLines( plane_lines );
255 this->PlaneMapper->SetInputData( this->PlaneSource );
256 this->PlaneActor->SetMapper( this->PlaneMapper );
258 this->TextActor->SetTextScaleModeToNone( );
259 vtkTextProperty* textprop = this->TextActor->GetTextProperty( );
260 textprop->SetColor( 1, 1, 1 );
261 textprop->SetFontFamilyToCourier( );
262 textprop->SetFontSize( 18 );
263 textprop->BoldOff( );
264 textprop->ItalicOff( );
265 textprop->ShadowOff( );
266 textprop->SetJustificationToLeft( );
267 textprop->SetVerticalJustificationToBottom( );
268 vtkCoordinate* coord = this->TextActor->GetPositionCoordinate( );
269 coord->SetCoordinateSystemToNormalizedViewport( );
270 coord->SetValue( 0.01, 0.01 );
274 // -------------------------------------------------------------------------
275 cpPlugins::Extensions::Visualization::ImageSliceActors::