1 #ifndef __CPM__VTK__POINTPICKERWIDGET__H__
2 #define __CPM__VTK__POINTPICKERWIDGET__H__
4 #include <vtk3DWidget.h>
6 #include <vtkCallbackCommand.h>
8 #include <vtkPolyDataMapper.h>
9 #include <vtkPropPicker.h>
10 #include <vtkRendererCollection.h>
11 #include <vtkSmartPointer.h>
12 #include <vtkSphereSource.h>
14 #include <cpm/Algorithms/Base/FindClosestPointInMesh.h>
15 #include <cpm/VTK/MeshMapper.h>
22 class PointPickerWidget
26 typedef PointPickerWidget Self;
29 typedef MeshMapper< M > TMapper;
30 typedef cpm::Algorithms::Base::FindClosestPointInMesh< M > TClosestPoint;
33 vtkTypeMacro( PointPickerWidget, vtk3DWidget );
38 return( new Self( ) );
41 void SetMesh( M* mesh )
44 this->m_MeshMapper = vtkSmartPointer< TMapper >::New( );
45 this->m_MeshMapper->SetInputData( this->m_Mesh );
47 this->m_MeshActor = vtkSmartPointer< vtkActor >::New( );
48 this->m_MeshActor->SetMapper( this->m_MeshMapper );
50 this->m_ClosestPoint = TClosestPoint::New( );
51 this->m_ClosestPoint->SetMesh( mesh );
52 this->m_ClosestPoint->SetBucketSize( 16 );
53 this->m_ClosestPoint->Build( );
58 virtual void PlaceWidget( double bounds[ 6 ] )
60 virtual void PlaceWidget( )
62 virtual void PlaceWidget(
63 double xmin, double xmax,
64 double ymin, double ymax,
65 double zmin, double zmax
69 void SetEnabled( int enabling )
71 if( !this->Interactor )
78 if( !this->CurrentRenderer )
80 this->SetCurrentRenderer(
81 this->Interactor->FindPokedRenderer(
82 this->Interactor->GetLastEventPosition( )[ 0 ],
83 this->Interactor->GetLastEventPosition( )[ 1 ]
86 if( this->CurrentRenderer == NULL )
92 this->Interactor->AddObserver(
93 vtkCommand::MouseMoveEvent,
94 this->EventCallbackCommand,
98 this->Interactor->AddObserver(
99 vtkCommand::LeftButtonPressEvent,
100 this->EventCallbackCommand,
103 this->Interactor->AddObserver(
104 vtkCommand::LeftButtonReleaseEvent,
105 this->EventCallbackCommand,
108 this->Interactor->AddObserver(
109 vtkCommand::RightButtonPressEvent,
110 this->EventCallbackCommand,
113 this->Interactor->AddObserver(
114 vtkCommand::RightButtonReleaseEvent,
115 this->EventCallbackCommand,
120 this->CurrentRenderer->AddActor( this->m_MeshActor );
121 this->CurrentRenderer->AddActor( this->m_SphereActor );
122 this->InvokeEvent( vtkCommand::EnableEvent, NULL );
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 );
137 this->Interactor->Render( );
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( );
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 );
156 virtual ~PointPickerWidget( )
160 static void ProcessEvents(
167 Self* wdg = reinterpret_cast< Self* >( clientdata );
173 case vtkCommand::MouseMoveEvent:
174 wdg->OnMouseMoveEvent( );
181 void OnMouseMoveEvent( )
183 if( this->CurrentRenderer == NULL )
186 // Does it have an actor picker?
187 vtkPropPicker* picker =
188 dynamic_cast< vtkPropPicker* >( this->Interactor->GetPicker( ) );
192 // Get last event position
193 double X = double( this->Interactor->GetEventPosition( )[ 0 ] );
194 double Y = double( this->Interactor->GetEventPosition( )[ 1 ] );
196 // Ok, try to pick an actor
197 if( picker->PickProp( X, Y, this->CurrentRenderer ) == 0 )
200 // Is the picked actor the current mesh actor?
202 dynamic_cast< vtkActor* >( picker->GetProp3D( ) );
203 if( actor != this->m_MeshActor.GetPointer( ) )
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(
218 this->m_SphereMapper->Modified( );
219 this->m_SphereActor->Modified( );
220 this->Interactor->Render( );
224 // Purposely not implemented
225 PointPickerWidget( const Self& );
226 void operator=( const Self& );
229 unsigned int BucketSize;
231 typename M::Pointer m_Mesh;
232 vtkSmartPointer< TMapper > m_MeshMapper;
233 vtkSmartPointer< vtkActor > m_MeshActor;
235 typename TClosestPoint::Pointer m_ClosestPoint;
237 vtkSmartPointer< vtkSphereSource > m_Sphere;
238 vtkSmartPointer< vtkPolyDataMapper > m_SphereMapper;
239 vtkSmartPointer< vtkActor > m_SphereActor;
246 #endif // __CPM__VTK__POINTPICKERWIDGET__H__