]> Creatis software - cpMesh.git/blob - lib/cpm/VTK/PointPickerWidget.h
First commit
[cpMesh.git] / lib / cpm / VTK / PointPickerWidget.h
1 #ifndef __CPM__VTK__POINTPICKERWIDGET__H__
2 #define __CPM__VTK__POINTPICKERWIDGET__H__
3
4 #include <vtk3DWidget.h>
5 #include <vtkActor.h>
6 #include <vtkCallbackCommand.h>
7 #include <vtkCamera.h>
8 #include <vtkPolyDataMapper.h>
9 #include <vtkPropPicker.h>
10 #include <vtkRendererCollection.h>
11 #include <vtkSmartPointer.h>
12 #include <vtkSphereSource.h>
13
14 #include <cpm/Algorithms/Base/FindClosestPointInMesh.h>
15 #include <cpm/VTK/MeshMapper.h>
16
17 namespace cpm
18 {
19   namespace VTK
20   {
21     template< class M >
22     class PointPickerWidget
23       : public vtk3DWidget
24     {
25     public:
26       typedef PointPickerWidget Self;
27       typedef M TMesh;
28
29       typedef MeshMapper< M > TMapper;
30       typedef cpm::Algorithms::Base::FindClosestPointInMesh< M > TClosestPoint;
31
32     public:
33       vtkTypeMacro( PointPickerWidget, vtk3DWidget );
34
35     public:
36       static Self* New( )
37         {
38           return( new Self( ) );
39         }
40
41       void SetMesh( M* mesh )
42         {
43           this->m_Mesh = mesh;
44           this->m_MeshMapper = vtkSmartPointer< TMapper >::New( );
45           this->m_MeshMapper->SetInputData( this->m_Mesh );
46
47           this->m_MeshActor = vtkSmartPointer< vtkActor >::New( );
48           this->m_MeshActor->SetMapper( this->m_MeshMapper );
49
50           this->m_ClosestPoint = TClosestPoint::New( );
51           this->m_ClosestPoint->SetMesh( mesh );
52           this->m_ClosestPoint->SetBucketSize( 16 );
53           this->m_ClosestPoint->Build( );
54
55           this->Modified( );
56         }
57
58       virtual void PlaceWidget( double bounds[ 6 ] )
59         { }
60       virtual void PlaceWidget( )
61         { }
62       virtual void PlaceWidget(
63         double xmin, double xmax,
64         double ymin, double ymax,
65         double zmin, double zmax
66         )
67         { }
68
69       void SetEnabled( int enabling )
70         {
71           if( !this->Interactor )
72             return;
73
74           if( enabling )
75           {
76             if( this->Enabled )
77               return;
78             if( !this->CurrentRenderer )
79             {
80               this->SetCurrentRenderer(
81                 this->Interactor->FindPokedRenderer(
82                   this->Interactor->GetLastEventPosition( )[ 0 ],
83                   this->Interactor->GetLastEventPosition( )[ 1 ]
84                   )
85                 );
86               if( this->CurrentRenderer == NULL )
87                 return;
88
89             } // fi
90             this->Enabled = 1;
91
92             this->Interactor->AddObserver(
93               vtkCommand::MouseMoveEvent,
94               this->EventCallbackCommand,
95               this->Priority
96               );
97             /*
98               this->Interactor->AddObserver(
99               vtkCommand::LeftButtonPressEvent,
100               this->EventCallbackCommand,
101               this->Priority
102               );
103               this->Interactor->AddObserver(
104               vtkCommand::LeftButtonReleaseEvent,
105               this->EventCallbackCommand,
106               this->Priority
107               );
108               this->Interactor->AddObserver(
109               vtkCommand::RightButtonPressEvent,
110               this->EventCallbackCommand,
111               this->Priority
112               );
113               this->Interactor->AddObserver(
114               vtkCommand::RightButtonReleaseEvent,
115               this->EventCallbackCommand,
116               this->Priority
117               );
118             */
119
120             this->CurrentRenderer->AddActor( this->m_MeshActor );
121             this->CurrentRenderer->AddActor( this->m_SphereActor );
122             this->InvokeEvent( vtkCommand::EnableEvent, NULL );
123           }
124           else
125           {
126             if( !this->Enabled )
127               return;
128             this->Enabled = 0;
129
130             this->Interactor->RemoveObserver( this->EventCallbackCommand );
131             this->CurrentRenderer->RemoveActor( this->m_MeshActor );
132             this->CurrentRenderer->RemoveActor( this->m_SphereActor );
133             this->InvokeEvent( vtkCommand::DisableEvent, NULL );
134             this->SetCurrentRenderer( NULL );
135
136           } // fi
137           this->Interactor->Render( );
138         }
139
140     protected:
141       PointPickerWidget( )
142         : Superclass( ),
143           BucketSize( 16 )
144         {
145           this->EventCallbackCommand->SetCallback( Self::ProcessEvents );
146           this->m_Sphere = vtkSmartPointer< vtkSphereSource >::New( );
147           this->m_SphereMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
148           this->m_SphereActor = vtkSmartPointer< vtkActor >::New( );
149
150           this->m_Sphere->SetRadius( 1e-3 );
151           this->m_SphereMapper->SetInputConnection( this->m_Sphere->GetOutputPort( ) );
152           this->m_SphereActor->SetMapper( this->m_SphereMapper );
153           this->m_SphereActor->GetProperty( )->SetColor( 1, 0, 0 );
154         }
155
156       virtual ~PointPickerWidget( )
157         {
158         }
159
160       static void ProcessEvents(
161         vtkObject* object,
162         unsigned long event,
163         void* clientdata,
164         void* calldata
165         )
166         {
167           Self* wdg = reinterpret_cast< Self* >( clientdata );
168           if( wdg == NULL )
169             return;
170
171           switch( event )
172           {
173           case vtkCommand::MouseMoveEvent:
174             wdg->OnMouseMoveEvent( );
175             break;
176           default:
177             break;
178           } // hctiws
179         }
180
181       void OnMouseMoveEvent( )
182         {
183           if( this->CurrentRenderer == NULL )
184             return;
185           
186           // Does it have an actor picker?
187           vtkPropPicker* picker =
188             dynamic_cast< vtkPropPicker* >( this->Interactor->GetPicker( ) );
189           if( picker == NULL )
190             return;
191
192           // Get last event position
193           double X = double( this->Interactor->GetEventPosition( )[ 0 ] );
194           double Y = double( this->Interactor->GetEventPosition( )[ 1 ] );
195
196           // Ok, try to pick an actor
197           if( picker->PickProp( X, Y, this->CurrentRenderer ) == 0 )
198             return;
199
200           // Is the picked actor the current mesh actor?
201           vtkActor* actor =
202             dynamic_cast< vtkActor* >( picker->GetProp3D( ) );
203           if( actor != this->m_MeshActor.GetPointer( ) )
204             return;
205
206           // Ok, we are ready to compute
207           double cPnt[ 3 ], aPnt[ 3 ];
208           picker->GetPickPosition( cPnt );
209           this->CurrentRenderer->GetActiveCamera( )->GetPosition( aPnt );
210           typename M::PointIdentifier pId =
211             this->m_ClosestPoint->FindClosestPoint( cPnt, aPnt );
212           typename M::PointType pnt = this->m_Mesh->GetPoint( pId );
213           this->m_Sphere->SetCenter(
214             double( pnt[ 0 ] ),
215             double( pnt[ 1 ] ),
216             double( pnt[ 2 ] )
217             );
218           this->m_SphereMapper->Modified( );
219           this->m_SphereActor->Modified( );
220           this->Interactor->Render( );
221         }
222
223     private:
224       // Purposely not implemented
225       PointPickerWidget( const Self& );
226       void operator=( const Self& );
227
228     protected:
229       unsigned int BucketSize;
230
231       typename M::Pointer         m_Mesh;
232       vtkSmartPointer< TMapper >  m_MeshMapper;
233       vtkSmartPointer< vtkActor > m_MeshActor;
234
235       typename TClosestPoint::Pointer m_ClosestPoint;
236
237       vtkSmartPointer< vtkSphereSource > m_Sphere;
238       vtkSmartPointer< vtkPolyDataMapper > m_SphereMapper;
239       vtkSmartPointer< vtkActor > m_SphereActor;
240     };
241
242   } // ecapseman
243
244 } // ecapseman
245
246 #endif // __CPM__VTK__POINTPICKERWIDGET__H__
247
248 // eof - $RCSfile$