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 int cpPlugins::Extensions::Visualization::ImageSliceActors::
53 return( this->SliceMapper->GetOrientation( ) );
56 // -------------------------------------------------------------------------
57 int cpPlugins::Extensions::Visualization::ImageSliceActors::
58 GetSliceNumber( ) const
60 return( this->SliceMapper->GetSliceNumber( ) );
63 // -------------------------------------------------------------------------
64 int cpPlugins::Extensions::Visualization::ImageSliceActors::
65 GetSliceNumberMinValue( ) const
67 return( this->SliceMapper->GetSliceNumberMinValue( ) );
70 // -------------------------------------------------------------------------
71 int cpPlugins::Extensions::Visualization::ImageSliceActors::
72 GetSliceNumberMaxValue( ) const
74 return( this->SliceMapper->GetSliceNumberMaxValue( ) );
77 // -------------------------------------------------------------------------
78 void cpPlugins::Extensions::Visualization::ImageSliceActors::
79 SetSliceNumber( const int& slice )
81 if( this->InputAlgorithm == NULL )
83 this->SliceMapper->SetSliceNumber( slice );
84 this->SliceMapper->Update( );
86 if( this->SegmentationAlgorithm != NULL )
88 this->SegmentationSliceMapper->SetSliceNumber( slice );
89 this->SegmentationSliceMapper->Update( );
94 vtkAlgorithm* algo = this->SliceMapper->GetInputAlgorithm( );
95 vtkInformation* info = algo->GetOutputInformation( 0 );
97 double ori[ 3 ], spac[ 3 ], pos[ 3 ];
98 info->Get( vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT( ), ext );
99 info->Get( vtkDataObject::ORIGIN( ), ori );
100 info->Get( vtkDataObject::SPACING( ), spac );
101 this->SliceMapper->GetSlicePlane( )->GetOrigin( pos );
103 // Prevent obscuring voxels by offsetting the plane geometry
106 ori[ 0 ] + ( spac[ 0 ] * double( ext[ 0 ] ) ),
107 ori[ 0 ] + ( spac[ 0 ] * double( ext[ 1 ] ) )
111 ori[ 1 ] + ( spac[ 1 ] * double( ext[ 2 ] ) ),
112 ori[ 1 ] + ( spac[ 1 ] * double( ext[ 3 ] ) )
116 ori[ 2 ] + ( spac[ 2 ] * double( ext[ 4 ] ) ),
117 ori[ 2 ] + ( spac[ 2 ] * double( ext[ 5 ] ) )
120 if( spac[ 0 ] < double( 0 ) )
122 double t = xbnds[ 0 ];
123 xbnds[ 0 ] = xbnds[ 1 ];
127 if( spac[ 1 ] < double( 0 ) )
129 double t = ybnds[ 0 ];
130 ybnds[ 0 ] = ybnds[ 1 ];
134 if( spac[ 2 ] < double( 0 ) )
136 double t = zbnds[ 0 ];
137 zbnds[ 0 ] = zbnds[ 1 ];
142 int axis = this->SliceMapper->GetOrientation( );
143 this->PlaneActor->GetProperty( )->SetRepresentationToWireframe( );
144 this->PlaneActor->GetProperty( )->SetLineWidth( 2 );
145 vtkPoints* plane_points = this->PlaneSource->GetPoints( );
146 if( axis == 0 ) // YZ, x-normal
148 plane_points->SetPoint( 0, pos[ 0 ], ybnds[ 0 ], zbnds[ 0 ] );
149 plane_points->SetPoint( 1, pos[ 0 ], ybnds[ 1 ], zbnds[ 0 ] );
150 plane_points->SetPoint( 2, pos[ 0 ], ybnds[ 1 ], zbnds[ 1 ] );
151 plane_points->SetPoint( 3, pos[ 0 ], ybnds[ 0 ], zbnds[ 1 ] );
152 this->PlaneActor->GetProperty( )->SetColor( 1, 0, 0 );
154 else if( axis == 1 ) // ZX, y-normal
156 plane_points->SetPoint( 0, xbnds[ 0 ], pos[ 1 ], zbnds[ 0 ] );
157 plane_points->SetPoint( 1, xbnds[ 0 ], pos[ 1 ], zbnds[ 1 ] );
158 plane_points->SetPoint( 2, xbnds[ 1 ], pos[ 1 ], zbnds[ 1 ] );
159 plane_points->SetPoint( 3, xbnds[ 1 ], pos[ 1 ], zbnds[ 0 ] );
160 this->PlaneActor->GetProperty( )->SetColor( 0, 1, 0 );
164 plane_points->SetPoint( 0, xbnds[ 0 ], ybnds[ 0 ], pos[ 2 ] );
165 plane_points->SetPoint( 1, xbnds[ 1 ], ybnds[ 0 ], pos[ 2 ] );
166 plane_points->SetPoint( 2, xbnds[ 1 ], ybnds[ 1 ], pos[ 2 ] );
167 plane_points->SetPoint( 3, xbnds[ 0 ], ybnds[ 1 ], pos[ 2 ] );
168 this->PlaneActor->GetProperty( )->SetColor( 0, 0, 1 );
171 this->PlaneSource->Modified( );
172 this->PlaneMapper->Modified( );
173 this->PlaneActor->Modified( );
177 // -------------------------------------------------------------------------
178 void cpPlugins::Extensions::Visualization::ImageSliceActors::
179 UpdateText( const double& w, const double& l )
182 int axId = this->SliceMapper->GetOrientation( );
183 if ( axId == 0 ) axis = 'X';
184 else if( axId == 1 ) axis = 'Y';
185 else if( axId == 2 ) axis = 'Z';
188 this->TextBuffer, "Axis: %c (%d)\nWin/Lev: %.2f/%.2f",
189 axis, this->SliceMapper->GetSliceNumber( ), w, l
191 this->TextActor->SetInput( this->TextBuffer );
192 this->TextActor->Modified( );
196 // -------------------------------------------------------------------------
197 cpPlugins::Extensions::Visualization::ImageSliceActors::
200 InputAlgorithm( NULL ),
201 SegmentationAlgorithm( NULL )
203 this->SliceMapper = vtkSmartPointer< vtkImageSliceMapper >::New( );
204 this->SegmentationSliceMapper =
205 vtkSmartPointer< vtkImageSliceMapper >::New( );
206 this->PlaneSource = vtkSmartPointer< vtkPolyData >::New( );
207 this->PlaneMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
208 this->ImageActor = vtkSmartPointer< vtkImageActor >::New( );
209 this->SegmentationActor = vtkSmartPointer< vtkImageActor >::New( );
210 this->TextActor = vtkSmartPointer< vtkTextActor >::New( );
211 this->PlaneActor = vtkSmartPointer< vtkActor >::New( );
213 this->ImageActorIndex = this->GetNumberOfItems( );
214 this->SegmentationActorIndex = this->ImageActorIndex + 1;
215 this->TextActorIndex = this->ImageActorIndex + 2;
216 this->PlaneActorIndex = this->ImageActorIndex + 3;
217 this->AddItem( this->ImageActor );
218 this->AddItem( this->SegmentationActor );
219 this->AddItem( this->TextActor );
220 this->AddItem( this->PlaneActor );
223 vtkSmartPointer< vtkPoints > plane_points =
224 vtkSmartPointer< vtkPoints >::New( );
225 vtkSmartPointer< vtkCellArray > plane_lines =
226 vtkSmartPointer< vtkCellArray >::New( );
228 plane_points->InsertNextPoint( 0, 0, 0 );
229 plane_points->InsertNextPoint( 0, 1, 0 );
230 plane_points->InsertNextPoint( 1, 1, 0 );
231 plane_points->InsertNextPoint( 1, 0, 0 );
232 plane_lines->InsertNextCell( 5 );
233 plane_lines->InsertCellPoint( 0 );
234 plane_lines->InsertCellPoint( 1 );
235 plane_lines->InsertCellPoint( 2 );
236 plane_lines->InsertCellPoint( 3 );
237 plane_lines->InsertCellPoint( 0 );
238 this->PlaneSource->SetPoints( plane_points );
239 this->PlaneSource->SetLines( plane_lines );
241 this->PlaneMapper->SetInputData( this->PlaneSource );
242 this->PlaneActor->SetMapper( this->PlaneMapper );
244 this->TextActor->SetTextScaleModeToNone( );
245 vtkTextProperty* textprop = this->TextActor->GetTextProperty( );
246 textprop->SetColor( 1, 1, 1 );
247 textprop->SetFontFamilyToCourier( );
248 textprop->SetFontSize( 18 );
249 textprop->BoldOff( );
250 textprop->ItalicOff( );
251 textprop->ShadowOff( );
252 textprop->SetJustificationToLeft( );
253 textprop->SetVerticalJustificationToBottom( );
254 vtkCoordinate* coord = this->TextActor->GetPositionCoordinate( );
255 coord->SetCoordinateSystemToNormalizedViewport( );
256 coord->SetValue( 0.01, 0.01 );
260 // -------------------------------------------------------------------------
261 cpPlugins::Extensions::Visualization::ImageSliceActors::