1 #include <cpExtensions/Visualization/Image3DMPR.h>
3 #include <vtkCommand.h>
4 #include <vtkInteractorStyleSwitch.h>
6 #include <cpExtensions/Visualization/SeedWidgetCorrector.h>
10 class cpPlugins_Extensions_Visualization_InteractionModeCallback
14 static cpPlugins_Extensions_Visualization_InteractionModeCallback* New( )
16 return( new cpPlugins_Extensions_Visualization_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 // -------------------------------------------------------------------------
76 cpExtensions::Visualization::Image3DMPR::
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( );
106 cpPlugins_Extensions_Visualization_InteractionModeCallback
108 vtkSmartPointer< _TInteractionCB > int_cb =
109 vtkSmartPointer< _TInteractionCB >::New( );
110 int_cb->WidgetX = this->m_WidgetX;
111 int_cb->WidgetY = this->m_WidgetY;
112 int_cb->WidgetZ = this->m_WidgetZ;
113 int_cb->SeedWidget = this->m_SeedWidget;
114 this->m_Interactor->AddObserver( vtkCommand::KeyPressEvent, int_cb );
117 cpExtensions::Visualization::SeedWidgetCorrector
119 vtkSmartPointer< _TCorrector > corr_cb =
120 vtkSmartPointer< _TCorrector >::New( );
121 corr_cb->Configure( this->m_Picker );
122 this->m_SeedWidget->AddObserver( vtkCommand::PlacePointEvent, corr_cb );
125 // -------------------------------------------------------------------------
126 cpExtensions::Visualization::Image3DMPR::
131 // -------------------------------------------------------------------------
132 void cpExtensions::Visualization::Image3DMPR::
133 SetImage( vtkImageData* image )
135 this->m_Image = image;
138 this->m_Outline->SetBounds( this->m_Image->GetBounds( ) );
139 this->m_OutlineMapper->
140 SetInputConnection( this->m_Outline->GetOutputPort( ) );
141 this->m_OutlineActor->SetMapper( this->m_OutlineMapper );
142 this->m_OutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
145 this->m_Picker->SetTolerance( 0.005 );
148 this->m_WidgetX->DisplayTextOn( );
149 this->m_WidgetX->SetInputData( this->m_Image );
150 this->m_WidgetX->SetPlaneOrientationToXAxes( );
151 this->m_WidgetX->SetSliceIndex( this->m_Image->GetExtent( )[ 0 ] );
152 this->m_WidgetX->SetPicker( this->m_Picker );
153 this->m_WidgetX->SetKeyPressActivationValue( 'x' );
154 this->m_WidgetX->GetPlaneProperty( )->SetLineWidth( 3 );
155 this->m_WidgetX->GetPlaneProperty( )->SetColor( 0, 1, 1 );
157 this->m_WidgetY->DisplayTextOn( );
158 this->m_WidgetY->SetInputData( this->m_Image );
159 this->m_WidgetY->SetPlaneOrientationToYAxes( );
160 this->m_WidgetY->SetSliceIndex( this->m_Image->GetExtent( )[ 2 ] );
161 this->m_WidgetY->SetPicker( this->m_Picker );
162 this->m_WidgetY->SetKeyPressActivationValue( 'y' );
163 this->m_WidgetY->GetPlaneProperty( )->SetColor( 1, 0, 1 );
164 this->m_WidgetY->GetPlaneProperty( )->SetLineWidth( 3 );
165 this->m_WidgetY->SetLookupTable( this->m_WidgetX->GetLookupTable( ) );
167 this->m_WidgetZ->DisplayTextOn( );
168 this->m_WidgetZ->SetInputData( this->m_Image );
169 this->m_WidgetZ->SetPlaneOrientationToZAxes( );
170 this->m_WidgetZ->SetSliceIndex( this->m_Image->GetExtent( )[ 4 ] );
171 this->m_WidgetZ->SetPicker( this->m_Picker );
172 this->m_WidgetZ->SetKeyPressActivationValue( 'z' );
173 this->m_WidgetZ->GetPlaneProperty( )->SetColor( 1, 1, 0 );
174 this->m_WidgetZ->GetPlaneProperty( )->SetLineWidth( 3 );
175 this->m_WidgetZ->SetLookupTable( this->m_WidgetX->GetLookupTable( ) );
178 this->m_Window->AddRenderer( this->m_Renderer );
179 this->m_Interactor->SetRenderWindow( this->m_Window );
183 this->m_Image->GetSpacing( spac );
184 double min_spacing = spac[ 0 ];
185 for( unsigned int d = 1; d < 3; d++ )
186 min_spacing = ( spac[ d ] < min_spacing )? spac[ d ]: min_spacing;
188 vtkInteractorStyleSwitch* iswitch =
189 dynamic_cast< vtkInteractorStyleSwitch* >(
190 this->m_Interactor->GetInteractorStyle( )
192 if( iswitch != NULL )
193 iswitch->SetCurrentStyleToTrackballCamera( );
196 this->m_Renderer->AddActor( this->m_OutlineActor );
199 this->m_WidgetX->SetInteractor( this->m_Interactor );
200 this->m_WidgetY->SetInteractor( this->m_Interactor );
201 this->m_WidgetZ->SetInteractor( this->m_Interactor );
202 this->m_SeedWidget->SetInteractor( this->m_Interactor );
204 this->m_WidgetX->On( );
205 this->m_WidgetY->On( );
206 this->m_WidgetZ->On( );
207 this->m_SeedWidget->On( );
208 this->m_SeedWidget->ProcessEventsOff( );
209 this->m_Interactor->SetPicker( this->m_Picker );
211 this->m_Interactor->GetPickingManager( )->AddPicker( this->m_Picker );
212 this->m_Interactor->GetPickingManager( )->EnabledOn( );
216 // -------------------------------------------------------------------------
217 void cpExtensions::Visualization::Image3DMPR::
218 SetBackground( double r, double g, double b )
220 this->m_Renderer->SetBackground( r, g, b );
223 // -------------------------------------------------------------------------
224 void cpExtensions::Visualization::Image3DMPR::
225 SetSize( unsigned int w, unsigned int h )
227 this->m_Window->SetSize( w, h );
230 // -------------------------------------------------------------------------
231 void cpExtensions::Visualization::Image3DMPR::
232 SetWindowLevel( double w, double l )
234 this->m_WidgetX->SetWindowLevel( w, l );
235 this->m_WidgetY->SetWindowLevel( w, l );
236 this->m_WidgetZ->SetWindowLevel( w, l );
239 // -------------------------------------------------------------------------
240 void cpExtensions::Visualization::Image3DMPR::
241 AddPolyData( vtkPolyData* pd, double opacity )
243 unsigned int i = this->m_PolyDatas.size( );
245 this->m_PolyDatas.push_back( pd );
246 this->m_Mappers.push_back( vtkSmartPointer< vtkPolyDataMapper >::New( ) );
247 this->m_Actors.push_back( vtkSmartPointer< vtkActor >::New( ) );
249 this->m_Mappers[ i ]->SetInputData( pd );
250 this->m_Actors[ i ]->SetMapper( this->m_Mappers[ i ] );
251 this->m_Actors[ i ]->GetProperty( )->SetOpacity( opacity );
252 this->m_Actors[ i ]->GetProperty( )->SetLineWidth( 3 );
253 this->m_Actors[ i ]->GetProperty( )->SetPointSize( 5 );
254 this->m_Renderer->AddActor( this->m_Actors[ i ] );
257 // -------------------------------------------------------------------------
258 void cpExtensions::Visualization::Image3DMPR::
259 AddPolyData( vtkPolyData* pd, double r, double g, double b, double opacity )
261 unsigned int i = this->m_PolyDatas.size( );
263 this->m_PolyDatas.push_back( pd );
264 this->m_Mappers.push_back( vtkSmartPointer< vtkPolyDataMapper >::New( ) );
265 this->m_Actors.push_back( vtkSmartPointer< vtkActor >::New( ) );
267 this->m_Mappers[ i ]->SetInputData( pd );
268 this->m_Mappers[ i ]->ScalarVisibilityOff( );
269 this->m_Actors[ i ]->SetMapper( this->m_Mappers[ i ] );
270 this->m_Actors[ i ]->GetProperty( )->SetColor( r, g, b );
271 this->m_Actors[ i ]->GetProperty( )->SetOpacity( opacity );
272 this->m_Actors[ i ]->GetProperty( )->SetLineWidth( 3 );
273 this->m_Actors[ i ]->GetProperty( )->SetPointSize( 5 );
274 this->m_Renderer->AddActor( this->m_Actors[ i ] );
277 // -------------------------------------------------------------------------
278 void cpExtensions::Visualization::Image3DMPR::
279 AddPolyData( vtkPolyData* pd, vtkLookupTable* lut, double opacity )
281 unsigned int i = this->m_PolyDatas.size( );
283 this->m_PolyDatas.push_back( pd );
284 this->m_Mappers.push_back( vtkSmartPointer< vtkPolyDataMapper >::New( ) );
285 this->m_Actors.push_back( vtkSmartPointer< vtkActor >::New( ) );
287 this->m_Mappers[ i ]->SetInputData( pd );
288 this->m_Mappers[ i ]->SetLookupTable( lut );
289 this->m_Actors[ i ]->SetMapper( this->m_Mappers[ i ] );
290 this->m_Actors[ i ]->GetProperty( )->SetOpacity( opacity );
291 this->m_Renderer->AddActor( this->m_Actors[ i ] );
294 // -------------------------------------------------------------------------
295 unsigned int cpExtensions::Visualization::Image3DMPR::
296 GetNumberOfSeeds( ) const
298 return( this->m_SeedRepresentation->GetNumberOfSeeds( ) );
301 // -------------------------------------------------------------------------
302 void cpExtensions::Visualization::Image3DMPR::
303 GetSeed( int n, double* s ) const
305 vtkHandleWidget* hWdg = this->m_SeedWidget->GetSeed( n );
308 vtkHandleRepresentation* hRep =
309 dynamic_cast< vtkHandleRepresentation* >( hWdg->GetRepresentation( ) );
312 hRep->GetWorldPosition( s );
315 // -------------------------------------------------------------------------
316 unsigned int cpExtensions::Visualization::Image3DMPR::
317 AddSeed( const double& x, const double& y, const double& z ) const
319 double pos[ 3 ] = { x, y, z };
321 int hnd_id = this->m_SeedRepresentation->CreateHandle( pos );
322 vtkHandleWidget* hnd = this->m_SeedWidget->CreateNewHandle( );
323 this->m_SeedRepresentation->GetHandleRepresentation( hnd_id )->
324 SetWorldPosition( pos );
325 hnd->SetEnabled( 1 );
327 return( this->GetNumberOfSeeds( ) - 1 );
330 // -------------------------------------------------------------------------
331 vtkRenderWindow* cpExtensions::Visualization::Image3DMPR::
334 return( this->m_Window );
337 // -------------------------------------------------------------------------
338 vtkRenderer* cpExtensions::Visualization::Image3DMPR::
341 return( this->m_Renderer );
344 // -------------------------------------------------------------------------
345 void cpExtensions::Visualization::Image3DMPR::
348 this->m_WidgetX->On( );
349 this->m_WidgetY->On( );
350 this->m_WidgetZ->On( );
352 this->m_Renderer->ResetCamera( );
353 this->m_Interactor->Initialize( );
354 this->m_Interactor->Start( );
357 // -------------------------------------------------------------------------
358 void cpExtensions::Visualization::Image3DMPR::
361 this->m_Window->Render( );