# AreaPicking

VTKExamples/Cxx/Picking/AreaPicking

### Description

This example creates 3 points+vertices. Currently, the address of the picked prop is 0 (this is not correct). A red bounding box is drawn around every picked prop.

- For '' vtkInteractorStyleTrackballCamera''' - use 'p' to pick at the current mouse position

- For '' vtkInteractorStyleRubberBandPick''' - use 'r' and left-mouse to draw a selection box used to pick

### Code

AreaPicking.cxx

    #include <vtkSmartPointer.h>

    #include <vtkPoints.h>
    #include <vtkXMLPolyDataWriter.h>
    #include <vtkPolyData.h>
    #include <vtkCellArray.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkActor.h>
    #include <vtkProperty.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderer.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkInteractorStyleRubberBandPick.h>
    #include <vtkInteractorStyleTrackballCamera.h>
    #include <vtkInteractorStyleTrackball.h>
    #include <vtkAreaPicker.h>
    #include <vtkCallbackCommand.h>
    #include <vtkProp3DCollection.h>

    #include <vtkNamedColors.h>

    namespace
    {
    void PickCallbackFunction ( vtkObject* caller, long unsigned int eventId, void* clientData, void* callData );
    }

    int main(int, char *[])
    {
      // Create a set of points
      vtkSmartPointer<vtkPoints> points =
        vtkSmartPointer<vtkPoints>::New();
      vtkSmartPointer<vtkCellArray> vertices =
        vtkSmartPointer<vtkCellArray>::New();
      vtkIdType pid[1];
      pid[0] = points->InsertNextPoint ( 1.0, 0.0, 0.0 );
      vertices->InsertNextCell ( 1,pid );
      pid[0] = points->InsertNextPoint ( 0.0, 0.0, 0.0 );
      vertices->InsertNextCell ( 1,pid );
      pid[0] = points->InsertNextPoint ( 0.0, 1.0, 0.0 );
      vertices->InsertNextCell ( 1,pid );

      // Create a polydata
      vtkSmartPointer<vtkPolyData> polydata =
        vtkSmartPointer<vtkPolyData>::New();
      polydata->SetPoints ( points );
      polydata->SetVerts ( vertices );

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

      vtkSmartPointer<vtkNamedColors> colors =
        vtkSmartPointer<vtkNamedColors>::New();

      vtkSmartPointer<vtkActor> actor =
        vtkSmartPointer<vtkActor>::New();
      actor->SetMapper(mapper);
      actor->GetProperty()->SetPointSize(5);
      actor->GetProperty()->SetColor(colors->GetColor3d("Black").GetData());

      vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
      vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->AddRenderer(renderer);

      vtkSmartPointer<vtkAreaPicker> areaPicker =
        vtkSmartPointer<vtkAreaPicker>::New();

      vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      renderWindowInteractor->SetRenderWindow(renderWindow);
      renderWindowInteractor->SetPicker(areaPicker);

      renderer->AddActor(actor);

      renderer->SetBackground(colors->GetColor3d("Gold").GetData());

      renderWindow->Render();

      // For vtkInteractorStyleRubberBandPick - use 'r' and left-mouse to draw a selection box used to pick
      vtkSmartPointer<vtkInteractorStyleRubberBandPick> style =
        vtkSmartPointer<vtkInteractorStyleRubberBandPick>::New();

      // For vtkInteractorStyleTrackballCamera - use 'p' to pick at the current mouse position
    //  vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
    //    vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New(); //like paraview
      style->SetCurrentRenderer(renderer);
      renderWindowInteractor->SetInteractorStyle( style );

      vtkSmartPointer<vtkCallbackCommand> pickCallback =
        vtkSmartPointer<vtkCallbackCommand>::New();
      pickCallback->SetCallback ( PickCallbackFunction );

      areaPicker->AddObserver ( vtkCommand::EndPickEvent, pickCallback );

      renderWindowInteractor->Start();

      return EXIT_SUCCESS;
    }

    namespace
    {
    void PickCallbackFunction(vtkObject* caller,
                              long unsigned int vtkNotUsed(eventId),
                              void* vtkNotUsed(clientData),
                              void* vtkNotUsed(callData))
    {
      std::cout << "Pick." << std::endl;
      vtkAreaPicker* areaPicker = static_cast<vtkAreaPicker*>(caller);
      vtkProp3DCollection* props = areaPicker->GetProp3Ds();
      props->InitTraversal();

      for(vtkIdType i = 0; i < props->GetNumberOfItems(); i++)
      {
        vtkProp3D* prop = props->GetNextProp3D();
        std::cout << "Picked prop: " << prop << std::endl;
      }
    }
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(AreaPicking)

    find_package(VTK COMPONENTS
      vtkCommonColor
      vtkCommonCore
      vtkCommonDataModel
      vtkIOXML
      vtkInteractionStyle
      vtkRenderingCore
      vtkRenderingFreeType
      vtkRenderingOpenGL2 QUIET)
    if (NOT VTK_FOUND)
      message("Skipping AreaPicking: ${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(AreaPicking MACOSX_BUNDLE AreaPicking.cxx )
      target_link_libraries(AreaPicking PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(AreaPicking MACOSX_BUNDLE AreaPicking.cxx )
      target_link_libraries(AreaPicking PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS AreaPicking
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build AreaPicking

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

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

    ./AreaPicking

WINDOWS USERS

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