1 #include <fpa/VTK/ImageMPR.h>
3 #include <vtkCommand.h>
4 #include <vtkInteractorStyleSwitch.h>
6 #include <fpa/VTK/SeedWidgetCorrector.h>
10 class fpa_VTK_InteractionModeCallback
14 static fpa_VTK_InteractionModeCallback* New( )
16 return( new fpa_VTK_InteractionModeCallback );
19 vtkObject* caller, unsigned long eventId, void* arguments )
21 if( eventId == vtkCommand::KeyPressEvent )
23 vtkRenderWindowInteractor* iren =
24 static_cast< vtkRenderWindowInteractor* >( caller );
28 switch( iren->GetKeyCode( ) )
33 bool eSeed = ( this->SeedWidget->GetProcessEvents( ) == 1 );
36 this->SeedWidget->ProcessEventsOff( );
37 this->WidgetX->InteractionOn( );
38 this->WidgetY->InteractionOn( );
39 this->WidgetZ->InteractionOn( );
43 this->SeedWidget->ProcessEventsOn( );
44 this->WidgetX->InteractionOff( );
45 this->WidgetY->InteractionOff( );
46 this->WidgetZ->InteractionOff( );
54 this->SeedWidget->ProcessEventsOff( );
55 this->WidgetX->InteractionOff( );
56 this->WidgetY->InteractionOff( );
57 this->WidgetZ->InteractionOff( );
69 vtkImagePlaneWidget* WidgetX;
70 vtkImagePlaneWidget* WidgetY;
71 vtkImagePlaneWidget* WidgetZ;
72 vtkSeedWidget* SeedWidget;
75 // -------------------------------------------------------------------------
79 this->m_Outline = vtkSmartPointer< vtkOutlineSource >::New( );
80 this->m_OutlineMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
81 this->m_OutlineActor = vtkSmartPointer< vtkActor >::New( );
82 this->m_Picker = vtkSmartPointer< vtkCellPicker >::New( );
83 this->m_WidgetX = vtkSmartPointer< vtkImagePlaneWidget >::New( );
84 this->m_WidgetY = vtkSmartPointer< vtkImagePlaneWidget >::New( );
85 this->m_WidgetZ = vtkSmartPointer< vtkImagePlaneWidget >::New( );
86 this->m_Renderer = vtkSmartPointer< vtkRenderer >::New( );
87 this->m_Window = vtkSmartPointer< vtkRenderWindow >::New( );
88 this->m_Interactor = vtkSmartPointer< vtkRenderWindowInteractor >::New( );
89 this->m_SeedHandleRepresentation =
90 vtkSmartPointer< vtkPointHandleRepresentation3D >::New( );
91 this->m_SeedRepresentation =
92 vtkSmartPointer< vtkSeedRepresentation >::New( );
93 this->m_SeedWidget = vtkSmartPointer<vtkSeedWidget>::New();
95 this->m_SeedHandleRepresentation->GetProperty()->SetColor( 1, 0, 1 );
96 this->m_SeedRepresentation->
97 SetHandleRepresentation( this->m_SeedHandleRepresentation );
98 this->m_SeedWidget->SetRepresentation( this->m_SeedRepresentation );
100 this->m_WidgetX->KeyPressActivationOff( );
101 this->m_WidgetY->KeyPressActivationOff( );
102 this->m_WidgetZ->KeyPressActivationOff( );
103 this->m_SeedWidget->KeyPressActivationOff( );
105 vtkSmartPointer< fpa_VTK_InteractionModeCallback > int_cb =
106 vtkSmartPointer< fpa_VTK_InteractionModeCallback >::New( );
107 int_cb->WidgetX = this->m_WidgetX;
108 int_cb->WidgetY = this->m_WidgetY;
109 int_cb->WidgetZ = this->m_WidgetZ;
110 int_cb->SeedWidget = this->m_SeedWidget;
111 this->m_Interactor->AddObserver( vtkCommand::KeyPressEvent, int_cb );
113 vtkSmartPointer< fpa::VTK::SeedWidgetCorrector > corr_cb =
114 vtkSmartPointer< fpa::VTK::SeedWidgetCorrector >::New( );
115 corr_cb->Configure( this->m_Picker );
116 this->m_SeedWidget->AddObserver( vtkCommand::PlacePointEvent, corr_cb );
119 // -------------------------------------------------------------------------
125 // -------------------------------------------------------------------------
126 void fpa::VTK::ImageMPR::
127 SetImage( vtkImageData* image )
129 this->m_Image = image;
132 this->m_Outline->SetBounds( this->m_Image->GetBounds( ) );
133 this->m_OutlineMapper->
134 SetInputConnection( this->m_Outline->GetOutputPort( ) );
135 this->m_OutlineActor->SetMapper( this->m_OutlineMapper );
136 this->m_OutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
139 this->m_Picker->SetTolerance( 0.005 );
142 this->m_WidgetX->DisplayTextOn( );
143 this->m_WidgetX->SetInputData( this->m_Image );
144 this->m_WidgetX->SetPlaneOrientationToXAxes( );
145 this->m_WidgetX->SetSliceIndex( this->m_Image->GetExtent( )[ 0 ] );
146 this->m_WidgetX->SetPicker( this->m_Picker );
147 this->m_WidgetX->SetKeyPressActivationValue( 'x' );
148 this->m_WidgetX->GetPlaneProperty( )->SetLineWidth( 3 );
149 this->m_WidgetX->GetPlaneProperty( )->SetColor( 0, 1, 1 );
151 this->m_WidgetY->DisplayTextOn( );
152 this->m_WidgetY->SetInputData( this->m_Image );
153 this->m_WidgetY->SetPlaneOrientationToYAxes( );
154 this->m_WidgetY->SetSliceIndex( this->m_Image->GetExtent( )[ 2 ] );
155 this->m_WidgetY->SetPicker( this->m_Picker );
156 this->m_WidgetY->SetKeyPressActivationValue( 'y' );
157 this->m_WidgetY->GetPlaneProperty( )->SetColor( 1, 0, 1 );
158 this->m_WidgetY->GetPlaneProperty( )->SetLineWidth( 3 );
159 this->m_WidgetY->SetLookupTable( this->m_WidgetX->GetLookupTable( ) );
161 this->m_WidgetZ->DisplayTextOn( );
162 this->m_WidgetZ->SetInputData( this->m_Image );
163 this->m_WidgetZ->SetPlaneOrientationToZAxes( );
164 this->m_WidgetZ->SetSliceIndex( this->m_Image->GetExtent( )[ 4 ] );
165 this->m_WidgetZ->SetPicker( this->m_Picker );
166 this->m_WidgetZ->SetKeyPressActivationValue( 'z' );
167 this->m_WidgetZ->GetPlaneProperty( )->SetColor( 1, 1, 0 );
168 this->m_WidgetZ->GetPlaneProperty( )->SetLineWidth( 3 );
169 this->m_WidgetZ->SetLookupTable( this->m_WidgetX->GetLookupTable( ) );
172 this->m_Window->AddRenderer( this->m_Renderer );
173 this->m_Interactor->SetRenderWindow( this->m_Window );
177 this->m_Image->GetSpacing( spac );
178 double min_spacing = spac[ 0 ];
179 for( unsigned int d = 1; d < 3; d++ )
180 min_spacing = ( spac[ d ] < min_spacing )? spac[ d ]: min_spacing;
182 vtkInteractorStyleSwitch* iswitch =
183 dynamic_cast< vtkInteractorStyleSwitch* >(
184 this->m_Interactor->GetInteractorStyle( )
186 if( iswitch != NULL )
187 iswitch->SetCurrentStyleToTrackballCamera( );
190 this->m_Renderer->AddActor( this->m_OutlineActor );
193 this->m_WidgetX->SetInteractor( this->m_Interactor );
194 this->m_WidgetY->SetInteractor( this->m_Interactor );
195 this->m_WidgetZ->SetInteractor( this->m_Interactor );
196 this->m_SeedWidget->SetInteractor( this->m_Interactor );
198 this->m_WidgetX->On( );
199 this->m_WidgetY->On( );
200 this->m_WidgetZ->On( );
201 this->m_SeedWidget->On( );
202 this->m_SeedWidget->ProcessEventsOff( );
203 this->m_Interactor->SetPicker( this->m_Picker );
205 this->m_Interactor->GetPickingManager( )->AddPicker( this->m_Picker );
206 this->m_Interactor->GetPickingManager( )->EnabledOn( );
210 // -------------------------------------------------------------------------
211 void fpa::VTK::ImageMPR::
212 SetBackground( double r, double g, double b )
214 this->m_Renderer->SetBackground( r, g, b );
217 // -------------------------------------------------------------------------
218 void fpa::VTK::ImageMPR::
219 SetSize( unsigned int w, unsigned int h )
221 this->m_Window->SetSize( w, h );
224 // -------------------------------------------------------------------------
225 void fpa::VTK::ImageMPR::
226 SetWindowLevel( double w, double l )
228 this->m_WidgetX->SetWindowLevel( w, l );
229 this->m_WidgetY->SetWindowLevel( w, l );
230 this->m_WidgetZ->SetWindowLevel( w, l );
233 // -------------------------------------------------------------------------
234 void fpa::VTK::ImageMPR::
235 AddPolyData( vtkPolyData* pd, double opacity )
237 unsigned int i = this->m_PolyDatas.size( );
239 this->m_PolyDatas.push_back( pd );
240 this->m_Mappers.push_back( vtkSmartPointer< vtkPolyDataMapper >::New( ) );
241 this->m_Actors.push_back( vtkSmartPointer< vtkActor >::New( ) );
243 this->m_Mappers[ i ]->SetInputData( pd );
244 this->m_Actors[ i ]->SetMapper( this->m_Mappers[ i ] );
245 this->m_Actors[ i ]->GetProperty( )->SetOpacity( opacity );
246 this->m_Actors[ i ]->GetProperty( )->SetLineWidth( 3 );
247 this->m_Actors[ i ]->GetProperty( )->SetPointSize( 5 );
248 this->m_Renderer->AddActor( this->m_Actors[ i ] );
251 // -------------------------------------------------------------------------
252 void fpa::VTK::ImageMPR::
253 AddPolyData( vtkPolyData* pd, double r, double g, double b, double opacity )
255 unsigned int i = this->m_PolyDatas.size( );
257 this->m_PolyDatas.push_back( pd );
258 this->m_Mappers.push_back( vtkSmartPointer< vtkPolyDataMapper >::New( ) );
259 this->m_Actors.push_back( vtkSmartPointer< vtkActor >::New( ) );
261 this->m_Mappers[ i ]->SetInputData( pd );
262 this->m_Mappers[ i ]->ScalarVisibilityOff( );
263 this->m_Actors[ i ]->SetMapper( this->m_Mappers[ i ] );
264 this->m_Actors[ i ]->GetProperty( )->SetColor( r, g, b );
265 this->m_Actors[ i ]->GetProperty( )->SetOpacity( opacity );
266 this->m_Actors[ i ]->GetProperty( )->SetLineWidth( 3 );
267 this->m_Actors[ i ]->GetProperty( )->SetPointSize( 5 );
268 this->m_Renderer->AddActor( this->m_Actors[ i ] );
271 // -------------------------------------------------------------------------
272 void fpa::VTK::ImageMPR::
273 AddPolyData( vtkPolyData* pd, vtkLookupTable* lut, double opacity )
275 unsigned int i = this->m_PolyDatas.size( );
277 this->m_PolyDatas.push_back( pd );
278 this->m_Mappers.push_back( vtkSmartPointer< vtkPolyDataMapper >::New( ) );
279 this->m_Actors.push_back( vtkSmartPointer< vtkActor >::New( ) );
281 this->m_Mappers[ i ]->SetInputData( pd );
282 this->m_Mappers[ i ]->SetLookupTable( lut );
283 this->m_Actors[ i ]->SetMapper( this->m_Mappers[ i ] );
284 this->m_Actors[ i ]->GetProperty( )->SetOpacity( opacity );
285 this->m_Renderer->AddActor( this->m_Actors[ i ] );
288 // -------------------------------------------------------------------------
289 unsigned int fpa::VTK::ImageMPR::
290 GetNumberOfSeeds( ) const
292 return( this->m_SeedRepresentation->GetNumberOfSeeds( ) );
295 // -------------------------------------------------------------------------
296 void fpa::VTK::ImageMPR::
297 GetSeed( int n, double* s ) const
299 vtkHandleWidget* hWdg = this->m_SeedWidget->GetSeed( n );
302 vtkHandleRepresentation* hRep =
303 dynamic_cast< vtkHandleRepresentation* >( hWdg->GetRepresentation( ) );
306 hRep->GetWorldPosition( s );
309 // -------------------------------------------------------------------------
310 unsigned int fpa::VTK::ImageMPR::
311 AddSeed( const double& x, const double& y, const double& z ) const
313 double pos[ 3 ] = { x, y, z };
315 int hnd_id = this->m_SeedRepresentation->CreateHandle( pos );
316 vtkHandleWidget* hnd = this->m_SeedWidget->CreateNewHandle( );
317 vtkHandleWidget::ComputeWorldToDisplay( this->m_Renderer, x, y, z, pos );
318 this->m_SeedRepresentation->SetSeedDisplayPosition( hnd_id, pos );
319 hnd->SetEnabled( 1 );
321 return( this->GetNumberOfSeeds( ) - 1 );
324 // -------------------------------------------------------------------------
325 vtkRenderWindow* fpa::VTK::ImageMPR::
328 return( this->m_Window );
331 // -------------------------------------------------------------------------
332 vtkRenderer* fpa::VTK::ImageMPR::
335 return( this->m_Renderer );
338 // -------------------------------------------------------------------------
339 void fpa::VTK::ImageMPR::
342 this->m_WidgetX->On( );
343 this->m_WidgetY->On( );
344 this->m_WidgetZ->On( );
346 this->m_Renderer->ResetCamera( );
347 this->m_Interactor->Initialize( );
348 this->m_Interactor->Start( );
351 // -------------------------------------------------------------------------
352 void fpa::VTK::ImageMPR::
355 this->m_Window->Render( );