#ifndef __CPM__VTK__POINTPICKERWIDGET__H__ #define __CPM__VTK__POINTPICKERWIDGET__H__ #include #include #include #include #include #include #include #include #include #include #include namespace cpm { namespace VTK { template< class M > class PointPickerWidget : public vtk3DWidget { public: typedef PointPickerWidget Self; typedef M TMesh; typedef MeshMapper< M > TMapper; typedef cpm::Algorithms::Base::FindClosestPointInMesh< M > TClosestPoint; public: vtkTypeMacro( PointPickerWidget, vtk3DWidget ); public: static Self* New( ) { return( new Self( ) ); } void SetMesh( M* mesh ) { this->m_Mesh = mesh; this->m_MeshMapper = vtkSmartPointer< TMapper >::New( ); this->m_MeshMapper->SetInputData( this->m_Mesh ); this->m_MeshActor = vtkSmartPointer< vtkActor >::New( ); this->m_MeshActor->SetMapper( this->m_MeshMapper ); this->m_ClosestPoint = TClosestPoint::New( ); this->m_ClosestPoint->SetMesh( mesh ); this->m_ClosestPoint->SetBucketSize( 16 ); this->m_ClosestPoint->Build( ); this->Modified( ); } virtual void PlaceWidget( double bounds[ 6 ] ) { } virtual void PlaceWidget( ) { } virtual void PlaceWidget( double xmin, double xmax, double ymin, double ymax, double zmin, double zmax ) { } void SetEnabled( int enabling ) { if( !this->Interactor ) return; if( enabling ) { if( this->Enabled ) return; if( !this->CurrentRenderer ) { this->SetCurrentRenderer( this->Interactor->FindPokedRenderer( this->Interactor->GetLastEventPosition( )[ 0 ], this->Interactor->GetLastEventPosition( )[ 1 ] ) ); if( this->CurrentRenderer == NULL ) return; } // fi this->Enabled = 1; this->Interactor->AddObserver( vtkCommand::MouseMoveEvent, this->EventCallbackCommand, this->Priority ); /* this->Interactor->AddObserver( vtkCommand::LeftButtonPressEvent, this->EventCallbackCommand, this->Priority ); this->Interactor->AddObserver( vtkCommand::LeftButtonReleaseEvent, this->EventCallbackCommand, this->Priority ); this->Interactor->AddObserver( vtkCommand::RightButtonPressEvent, this->EventCallbackCommand, this->Priority ); this->Interactor->AddObserver( vtkCommand::RightButtonReleaseEvent, this->EventCallbackCommand, this->Priority ); */ this->CurrentRenderer->AddActor( this->m_MeshActor ); this->CurrentRenderer->AddActor( this->m_SphereActor ); this->InvokeEvent( vtkCommand::EnableEvent, NULL ); } else { if( !this->Enabled ) return; this->Enabled = 0; this->Interactor->RemoveObserver( this->EventCallbackCommand ); this->CurrentRenderer->RemoveActor( this->m_MeshActor ); this->CurrentRenderer->RemoveActor( this->m_SphereActor ); this->InvokeEvent( vtkCommand::DisableEvent, NULL ); this->SetCurrentRenderer( NULL ); } // fi this->Interactor->Render( ); } protected: PointPickerWidget( ) : Superclass( ), BucketSize( 16 ) { this->EventCallbackCommand->SetCallback( Self::ProcessEvents ); this->m_Sphere = vtkSmartPointer< vtkSphereSource >::New( ); this->m_SphereMapper = vtkSmartPointer< vtkPolyDataMapper >::New( ); this->m_SphereActor = vtkSmartPointer< vtkActor >::New( ); this->m_Sphere->SetRadius( 1e-3 ); this->m_SphereMapper->SetInputConnection( this->m_Sphere->GetOutputPort( ) ); this->m_SphereActor->SetMapper( this->m_SphereMapper ); this->m_SphereActor->GetProperty( )->SetColor( 1, 0, 0 ); } virtual ~PointPickerWidget( ) { } static void ProcessEvents( vtkObject* object, unsigned long event, void* clientdata, void* calldata ) { Self* wdg = reinterpret_cast< Self* >( clientdata ); if( wdg == NULL ) return; switch( event ) { case vtkCommand::MouseMoveEvent: wdg->OnMouseMoveEvent( ); break; default: break; } // hctiws } void OnMouseMoveEvent( ) { if( this->CurrentRenderer == NULL ) return; // Does it have an actor picker? vtkPropPicker* picker = dynamic_cast< vtkPropPicker* >( this->Interactor->GetPicker( ) ); if( picker == NULL ) return; // Get last event position double X = double( this->Interactor->GetEventPosition( )[ 0 ] ); double Y = double( this->Interactor->GetEventPosition( )[ 1 ] ); // Ok, try to pick an actor if( picker->PickProp( X, Y, this->CurrentRenderer ) == 0 ) return; // Is the picked actor the current mesh actor? vtkActor* actor = dynamic_cast< vtkActor* >( picker->GetProp3D( ) ); if( actor != this->m_MeshActor.GetPointer( ) ) return; // Ok, we are ready to compute double cPnt[ 3 ], aPnt[ 3 ]; picker->GetPickPosition( cPnt ); this->CurrentRenderer->GetActiveCamera( )->GetPosition( aPnt ); typename M::PointIdentifier pId = this->m_ClosestPoint->FindClosestPoint( cPnt, aPnt ); typename M::PointType pnt = this->m_Mesh->GetPoint( pId ); this->m_Sphere->SetCenter( double( pnt[ 0 ] ), double( pnt[ 1 ] ), double( pnt[ 2 ] ) ); this->m_SphereMapper->Modified( ); this->m_SphereActor->Modified( ); this->Interactor->Render( ); } private: // Purposely not implemented PointPickerWidget( const Self& ); void operator=( const Self& ); protected: unsigned int BucketSize; typename M::Pointer m_Mesh; vtkSmartPointer< TMapper > m_MeshMapper; vtkSmartPointer< vtkActor > m_MeshActor; typename TClosestPoint::Pointer m_ClosestPoint; vtkSmartPointer< vtkSphereSource > m_Sphere; vtkSmartPointer< vtkPolyDataMapper > m_SphereMapper; vtkSmartPointer< vtkActor > m_SphereActor; }; } // ecapseman } // ecapseman #endif // __CPM__VTK__POINTPICKERWIDGET__H__ // eof - $RCSfile$