Skip to content


# WorldPointPicker

VTKEx/Cxx/Interaction/WorldPointPicker

### Description

This example demonstrates how to get the position of the point in 3D that is exactly behind the mouse click. This point is not likely a point that exists in the data set - i.e. it can be a point on the interior of a cell.

Question

If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

### Code

WorldPointPicker.cxx

    #include <vtkSmartPointer.h>
    #include <vtkRendererCollection.h>
    #include <vtkWorldPointPicker.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 MouseInteractorStyle : public vtkInteractorStyleTrackballCamera
    {
    public:
      static MouseInteractorStyle* New();
      vtkTypeMacro(MouseInteractorStyle, vtkInteractorStyleTrackballCamera);

      virtual void OnLeftButtonDown() override
      {
        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(MouseInteractorStyle);

    int main(int, char *[])
    {
      vtkSmartPointer<vtkSphereSource> sphereSource =
        vtkSmartPointer<vtkSphereSource>::New();
      sphereSource->Update();

      vtkSmartPointer<vtkWorldPointPicker> worldPointPicker =
        vtkSmartPointer<vtkWorldPointPicker>::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(worldPointPicker);
      renderWindowInteractor->SetRenderWindow(renderWindow);

      vtkSmartPointer<MouseInteractorStyle> style =
        vtkSmartPointer<MouseInteractorStyle>::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 3.3 FATAL_ERROR)

    project(WorldPointPicker)

    find_package(VTK COMPONENTS
      vtkvtkCommonCore
      vtkvtkFiltersSources
      vtkvtkInteractionStyle
      vtkvtkRenderingContextOpenGL2
      vtkvtkRenderingCore
      vtkvtkRenderingFreeType
      vtkvtkRenderingGL2PSOpenGL2
      vtkvtkRenderingOpenGL2 QUIET)
    if (NOT VTK_FOUND)
      message("Skipping WorldPointPicker: ${VTK_NOT_FOUND_MESSAGE}")
      return ()
    endif()
    message (STATUS "VTK_VERSION: ${VTK_VERSION}")
    if (VTK_VERSION VERSION_LESS "8.90.0")
      # old system
      include(${VTK_USE_FILE})
      add_executable(WorldPointPicker MACOSX_BUNDLE WorldPointPicker.cxx )
      target_link_libraries(WorldPointPicker PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(WorldPointPicker MACOSX_BUNDLE WorldPointPicker.cxx )
      target_link_libraries(WorldPointPicker PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS WorldPointPicker
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build WorldPointPicker

Click here to download WorldPointPicker and its CMakeLists.txt file. Once the tarball WorldPointPicker.tar has been downloaded and extracted,

    cd WorldPointPicker/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:

    ./WorldPointPicker

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.