# Picking

VTKExamples/Cxx/Interaction/Picking

### Code

Picking.cxx

    #include <vtkSmartPointer.h>
    #include <vtkActor.h>
    #include <vtkSphereSource.h>
    #include <vtkRendererCollection.h>
    #include <vtkCellArray.h>
    #include <vtkInteractorStyleTrackballCamera.h>
    #include <vtkObjectFactory.h>
    #include <vtkPlaneSource.h>
    #include <vtkPoints.h>
    #include <vtkPolyData.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkPropPicker.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderer.h>

    // Handle mouse events
    class MouseInteractorStyle2 : public vtkInteractorStyleTrackballCamera
    {
      public:
        static MouseInteractorStyle2* New();
        vtkTypeMacro(MouseInteractorStyle2, vtkInteractorStyleTrackballCamera);

        virtual void OnLeftButtonDown()
        {
          int* clickPos = this->GetInteractor()->GetEventPosition();

          // Pick from this location.
          vtkSmartPointer<vtkPropPicker>  picker =
            vtkSmartPointer<vtkPropPicker>::New();
          picker->Pick(clickPos[0], clickPos[1], 0, this->GetDefaultRenderer());

          double* pos = picker->GetPickPosition();
          std::cout << "Pick position (world coordinates) is: "
                    << pos[0] << " " << pos[1]
                    << " " << pos[2] << std::endl;

          std::cout << "Picked actor: " << picker->GetActor() << std::endl;
          //Create a sphere
          vtkSmartPointer<vtkSphereSource> sphereSource =
            vtkSmartPointer<vtkSphereSource>::New();
          sphereSource->SetCenter(pos[0], pos[1], pos[2]);
          sphereSource->SetRadius(0.1);

          //Create a mapper and actor
          vtkSmartPointer<vtkPolyDataMapper> mapper =
            vtkSmartPointer<vtkPolyDataMapper>::New();
          mapper->SetInputConnection(sphereSource->GetOutputPort());

          vtkSmartPointer<vtkActor> actor =
            vtkSmartPointer<vtkActor>::New();
          actor->SetMapper(mapper);

          //this->GetInteractor()->GetRenderWindow()->GetRenderers()->GetDefaultRenderer()->AddActor(actor);
          this->GetDefaultRenderer()->AddActor(actor);
          // Forward events
          vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
        }

      private:

    };

    vtkStandardNewMacro(MouseInteractorStyle2);

    // Execute application.
    int main(int, char *[])
    {
      vtkSmartPointer<vtkPlaneSource> planeSource =
        vtkSmartPointer<vtkPlaneSource>::New();
      planeSource->Update();

      // Create a polydata object
      vtkPolyData* polydata = planeSource->GetOutput();

      // Create a mapper
      vtkSmartPointer<vtkPolyDataMapper> mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      mapper->SetInputData ( polydata );

      // Create an actor
      vtkSmartPointer<vtkActor> actor =
        vtkSmartPointer<vtkActor>::New();
      actor->SetMapper ( mapper );

      std::cout << "Actor address: " << actor << std::endl;

      // A renderer and render window
      vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
      vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->AddRenderer ( renderer );

      // An interactor
      vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      renderWindowInteractor->SetRenderWindow ( renderWindow );

      // Set the custom stype to use for interaction.
      vtkSmartPointer<MouseInteractorStyle2> style =
        vtkSmartPointer<MouseInteractorStyle2>::New();
      style->SetDefaultRenderer(renderer);

      renderWindowInteractor->SetInteractorStyle( style );

      // Add the actors to the scene
      renderer->AddActor ( actor );
      renderer->SetBackground ( 0,0,1 );

      // Render and interact
      renderWindow->Render();
      renderWindowInteractor->Initialize();
      renderWindowInteractor->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 2.8)

    PROJECT(Picking)

    find_package(VTK REQUIRED)
    include(${VTK_USE_FILE})

    add_executable(Picking MACOSX_BUNDLE Picking.cxx )

    target_link_libraries(Picking ${VTK_LIBRARIES})

### Download and Build Picking

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

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

    ./Picking

WINDOWS USERS

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