]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Interaction/ImageInteractorStyle.cxx
...
[cpPlugins.git] / lib / cpExtensions / Interaction / ImageInteractorStyle.cxx
1 #include <cpExtensions/Interaction/ImageInteractorStyle.h>
2
3 #include <vtkCamera.h>
4 #include <vtkCommand.h>
5 #include <vtkImageActor.h>
6 #include <vtkImageMapper3D.h>
7 #include <vtkImageSliceMapper.h>
8 #include <vtkPropCollection.h>
9 #include <vtkProperty.h>
10 #include <vtkRenderWindowInteractor.h>
11 #include <vtkRenderer.h>
12
13 // -------------------------------------------------------------------------
14 /* TODO
15    cpExtensions::Interaction::ImageInteractorStyle::TSeedWidget::
16    TSeedWidget( vtkRenderWindowInteractor* interactor, vtkImageActor* actor )
17    {
18    this->Placer = vtkSmartPointer< vtkImageActorPointPlacer >::New( );
19    this->Handle = vtkSmartPointer< vtkPointHandleRepresentation3D >::New( );
20    this->Representation = vtkSmartPointer< vtkSeedRepresentation >::New( );
21    this->Widget = vtkSmartPointer< SeedWidget >::New( );
22
23    this->Placer->SetImageActor( actor );
24    this->Handle->GetProperty( )->SetColor( 1, 0, 0 );
25    this->Handle->SetPointPlacer( this->Placer );
26    this->Representation->SetHandleRepresentation( this->Handle );
27    this->Widget->SetRepresentation( this->Representation );
28    this->Widget->SetInteractor( interactor );
29    }
30    
31    // -------------------------------------------------------------------------
32    void cpExtensions::Interaction::ImageInteractorStyle::TSeedWidget::
33    On( )
34    {
35    this->Widget->On( );
36    }
37    
38    // -------------------------------------------------------------------------
39    void cpExtensions::Interaction::ImageInteractorStyle::TSeedWidget::
40    Off( )
41    {
42    this->Widget->Off( );
43    }
44 */
45
46 // -------------------------------------------------------------------------
47 cpExtensions::Interaction::ImageInteractorStyle::
48 Self* cpExtensions::Interaction::ImageInteractorStyle::
49 New( )
50 {
51   return( new Self );
52 }
53
54 // -------------------------------------------------------------------------
55 void cpExtensions::Interaction::ImageInteractorStyle::
56 SetCurrentRenderer( vtkRenderer* r )
57 {
58   this->Superclass::SetCurrentRenderer( r );
59
60   if( !( this->m_Configured ) )
61   {
62     auto lst = this->m_PropPicker->GetPickList( );
63     if( lst == NULL )
64       return;
65     if( lst->GetNumberOfItems( ) == 1 )
66     {
67       auto actor = dynamic_cast< vtkImageActor* >( lst->GetItemAsObject( 0 ) );
68       if( actor != NULL )
69       {
70         this->_ConfigureCamera( actor );
71         this->Modified( );
72
73       } // fi
74
75     } // fi
76
77   } // fi
78 }
79
80 // -------------------------------------------------------------------------
81 void cpExtensions::Interaction::ImageInteractorStyle::
82 AssociateImageActor( vtkImageActor* actor )
83 {
84   if( actor != NULL )
85   {
86     // Update picker
87     this->m_PropPicker->GetPickList( )->RemoveAllItems( );
88     this->m_PropPicker->AddPickList( actor );
89     this->m_Configured = false;
90     this->_ConfigureCamera( actor );
91
92     // Ok, done
93     this->Modified( );
94
95   } // fi
96 }
97
98 // -------------------------------------------------------------------------
99 /*
100 void cpExtensions::Interaction::ImageInteractorStyle::
101 SeedWidgetOn( )
102 {
103   if( this->m_SeedWidget != NULL ) delete this->m_SeedWidget;
104
105   this->m_PropPicker->GetPickList( )->InitTraversal( );
106   this->m_SeedWidget = new TSeedWidget(
107     this->Interactor,
108     dynamic_cast< vtkImageActor* >(
109       this->m_PropPicker->GetPickList( )->GetNextProp( )
110       )
111     );
112   this->m_SeedWidget->On( );
113 }
114
115 // -------------------------------------------------------------------------
116 void cpExtensions::Interaction::ImageInteractorStyle::
117 SeedWidgetOff( )
118 {
119   if( this->m_SeedWidget != NULL )
120   {
121     this->m_SeedWidget->Off( );
122     delete this->m_SeedWidget;
123     this->m_SeedWidget = NULL;
124
125   } // fi
126 }
127 */
128
129 // -------------------------------------------------------------------------
130 /*
131 void cpExtensions::Interaction::ImageInteractorStyle::
132 SetSeedWidgetCommand( vtkCommand* cmd )
133 {
134   if( cmd == NULL || this->m_SeedWidget == NULL )
135     return;
136   this->m_SeedWidget->Widget->
137     AddObserver( vtkCommand::PlacePointEvent, cmd );
138 }
139
140 // -------------------------------------------------------------------------
141 unsigned int cpExtensions::Interaction::ImageInteractorStyle::
142 GetNumberOfSeeds( ) const
143 {
144   if( this->m_SeedWidget != NULL )
145     return( this->m_SeedWidget->Representation->GetNumberOfSeeds( ) );
146   else
147     return( 0 );
148 }
149
150 // -------------------------------------------------------------------------
151 void cpExtensions::Interaction::ImageInteractorStyle::
152 GetSeedAsPoint( unsigned int id, double pos[ 3 ] ) const
153 {
154   if( this->m_SeedWidget != NULL )
155     this->m_SeedWidget->Representation->GetSeedWorldPosition( id, pos );
156 }
157
158 // -------------------------------------------------------------------------
159 void cpExtensions::Interaction::ImageInteractorStyle::
160 GetSeedAsIndex( unsigned int id, int idx[ 3 ] ) const
161 {
162      if( this->m_SeedWidget != NULL )
163      this->m_SeedWidget->Representation->GetSeedWorldPosition( id, pos );
164 }
165 */
166
167 // -------------------------------------------------------------------------
168 cpExtensions::Interaction::ImageInteractorStyle::
169 ImageInteractorStyle( )
170   : Superclass( ),
171     m_Configured( false )
172     /*
173       ,
174       m_SeedWidget( NULL )
175     */
176 {
177   this->m_PropPicker = vtkSmartPointer< vtkPropPicker >::New( );
178   this->m_PropPicker->PickFromListOn( );
179 }
180
181 // -------------------------------------------------------------------------
182 cpExtensions::Interaction::ImageInteractorStyle::
183 ~ImageInteractorStyle( )
184 {
185   // if( this->m_SeedWidget != NULL ) delete this->m_SeedWidget;
186 }
187
188 // -------------------------------------------------------------------------
189 void cpExtensions::Interaction::ImageInteractorStyle::
190 _ConfigureCamera( vtkImageActor* actor )
191 {
192   if( this->CurrentRenderer != NULL )
193   {
194     // Update camera
195     auto camera = this->CurrentRenderer->GetActiveCamera( );
196     auto mapper = dynamic_cast< vtkImageSliceMapper* >( actor->GetMapper( ) );
197     if( mapper != NULL && camera != NULL )
198     {
199       double bounds[ 6 ];
200       actor->GetBounds( bounds );
201       camera->ParallelProjectionOn( );
202       camera->SetFocalPoint( double( 0 ), double( 0 ), double( 0 ) );
203       int orientation = mapper->GetOrientation( );
204       switch( orientation )
205       {
206       case 0:
207       {
208         camera->SetPosition( double( 1 ), double( 0 ), double( 0 ) );
209         camera->SetViewUp  ( double( 0 ), double( 1 ), double( 0 ) );
210       }
211       break;
212       case 1:
213       {
214         camera->SetPosition( double( 0 ), double( 1 ), double( 0 ) );
215         camera->SetViewUp  ( double( 0 ), double( 0 ), double( -1 ) );
216       }
217       break;
218       case 2:
219       {
220         camera->SetPosition( double( 0 ), double( 0 ), double( 1 ) );
221         camera->SetViewUp  ( double( 0 ), double( 1 ), double( 0 ) );
222       }
223       break;
224       default:
225         break;
226       } // hctiws
227       this->CurrentRenderer->ResetCamera( );
228       this->m_Configured = true;
229
230     } // fi
231
232   } // fi
233 }
234
235 // -------------------------------------------------------------------------
236 bool cpExtensions::Interaction::ImageInteractorStyle::
237 _PickPosition( int idx[ 2 ], double pos[ 3 ] )
238 {
239   vtkRenderWindowInteractor* rwi = this->GetInteractor( );
240   if( rwi == NULL )
241     return( false );
242
243   // Find the renderer where the event has been raised
244   idx[ 0 ] = rwi->GetEventPosition( )[ 0 ];
245   idx[ 1 ] = rwi->GetEventPosition( )[ 1 ];
246   this->FindPokedRenderer( double( idx[ 0 ] ), double( idx[ 1 ] ) );
247
248   // Pick a 3D position
249   int r = this->m_PropPicker->Pick(
250     double( idx[ 0 ] ), double( idx[ 1 ] ), double( 0 ),
251     this->CurrentRenderer
252     );
253   if( r == 0 )
254     return( false );
255   this->m_PropPicker->GetPickPosition( pos );
256   return( true );
257 }
258
259 // eof - $RCSfile$