PointPicker
VTKExamples/Cxx/Interaction/PointPicker
Description¶
This example demonstrates how to get the closest point in the data set to the mouse click.
Code¶
PointPicker.cxx
#include <vtkSmartPointer.h> #include <vtkRendererCollection.h> #include <vtkPointPicker.h> #include <vtkSphereSource.h> #include <vtkPolyDataMapper.h> #include <vtkRenderer.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkActor.h> #include <vtkInteractorStyleTrackballCamera.h> #include <vtkObjectFactory.h> // Define interaction style class MouseInteractorStylePP : public vtkInteractorStyleTrackballCamera { public: static MouseInteractorStylePP* New(); vtkTypeMacro(MouseInteractorStylePP, vtkInteractorStyleTrackballCamera); virtual void OnLeftButtonDown() { std::cout << "Picking pixel: " << this->Interactor->GetEventPosition()[0] << " " << this->Interactor->GetEventPosition()[1] << std::endl; this->Interactor->GetPicker()->Pick(this->Interactor->GetEventPosition()[0], this->Interactor->GetEventPosition()[1], 0, // always zero. this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()); double picked[3]; this->Interactor->GetPicker()->GetPickPosition(picked); std::cout << "Picked value: " << picked[0] << " " << picked[1] << " " << picked[2] << std::endl; // Forward events vtkInteractorStyleTrackballCamera::OnLeftButtonDown(); } }; vtkStandardNewMacro(MouseInteractorStylePP); int main(int, char *[]) { vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New(); sphereSource->Update(); vtkSmartPointer<vtkPointPicker> pointPicker = vtkSmartPointer<vtkPointPicker>::New(); // Create a mapper and actor vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New(); mapper->SetInputConnection(sphereSource->GetOutputPort()); vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New(); actor->SetMapper(mapper); // Create a renderer, render window, and interactor vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New(); vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); renderWindow->AddRenderer(renderer); vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); renderWindowInteractor->SetPicker(pointPicker); renderWindowInteractor->SetRenderWindow(renderWindow); vtkSmartPointer<MouseInteractorStylePP> style = vtkSmartPointer<MouseInteractorStylePP>::New(); renderWindowInteractor->SetInteractorStyle( style ); // Add the actor to the scene renderer->AddActor(actor); renderer->SetBackground(1,1,1); // Background color white // Render and interact renderWindow->Render(); renderWindowInteractor->Start(); return EXIT_SUCCESS; }
CMakeLists.txt¶
cmake_minimum_required(VERSION 2.8) PROJECT(PointPicker) find_package(VTK REQUIRED) include(${VTK_USE_FILE}) add_executable(PointPicker MACOSX_BUNDLE PointPicker.cxx) target_link_libraries(PointPicker ${VTK_LIBRARIES})
Download and Build PointPicker¶
Danger
The generation of tar files has not been ported to the new VTKExamples. Some tarballs may be missing or out-of-date.
Click here to download PointPicker and its CMakeLists.txt file. Once the tarball PointPicker.tar has been downloaded and extracted,
cd PointPicker/build
If VTK is installed:
cmake ..
If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:
cmake -DVTK_DIR:PATH=/home/me/vtk_build ..
Build the project:
make
and run it:
./PointPicker
WINDOWS USERS PLEASE NOTE: Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.