# PointLocator

VTKExamples/Cxx/PolyData/PointLocator

### Description

Create a point locator for a set of points. The 'n' (next) and 'p' (previous) keys change the resolution of the cells.

### Code

PointLocator.cxx

    #include <vtkSmartPointer.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkObjectFactory.h>
    #include <vtkPointSource.h>
    #include <vtkPolyData.h>
    #include <vtkPointLocator.h>
    #include <vtkInteractorStyleTrackballCamera.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkActor.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderer.h>

    class KeyPressInteractorStyle3 : public vtkInteractorStyleTrackballCamera
    {
      public:
        static KeyPressInteractorStyle3* New();

        KeyPressInteractorStyle3() : PointsPerBucket(1) {}

        virtual void OnChar()
        {
          vtkRenderWindowInteractor *rwi = this->Interactor;
          char ch = rwi->GetKeyCode() ;

          switch (ch)
          {
            case 'n':
              cout << "Next." << endl;
              this->PointsPerBucket++;
              break;
            case 'p':
              cout << "Previous." << endl;
              if(this->PointsPerBucket > 1)
              {
                this->PointsPerBucket--;
              }
              break ;
            default:
              std::cout << "An unhandled key was pressed." << std::endl;
              break;
          }

          std::cout << "PointsPerBucket = " << this->PointsPerBucket << std::endl;

          // Create the tree
          pointLocator->SetNumberOfPointsPerBucket(this->PointsPerBucket);
          pointLocator->BuildLocator();
          pointLocator->GenerateRepresentation(1, polydata);

          renderWindow->Render();

          // Forward events
          vtkInteractorStyleTrackballCamera::OnChar();
        }

        vtkPointLocator* pointLocator;
        vtkRenderWindow* renderWindow;
        vtkPolyData* polydata;

      private:
        unsigned int PointsPerBucket;
    };

    vtkStandardNewMacro(KeyPressInteractorStyle3);

    int main(int, char *[])
    {
      vtkSmartPointer<vtkPointSource> pointSource =
        vtkSmartPointer<vtkPointSource>::New();
      pointSource->SetNumberOfPoints(4000);
      pointSource->Update();

      // Create the tree
      vtkSmartPointer<vtkPointLocator> pointLocator =
        vtkSmartPointer<vtkPointLocator>::New();
      pointLocator->SetDataSet(pointSource->GetOutput());
      pointLocator->AutomaticOn();
      pointLocator->SetNumberOfPointsPerBucket(2);
      pointLocator->BuildLocator();

      std::cout << "There are " << pointLocator->GetLevel() << " levels." << std::endl;
      std::cout << "There are " << pointLocator->GetNumberOfPointsPerBucket()
                << " points per bucket." << endl;

      vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
      pointLocator->GenerateRepresentation(pointLocator->GetLevel(), polydata);

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

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

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

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

      vtkSmartPointer<KeyPressInteractorStyle3> style =
        vtkSmartPointer<KeyPressInteractorStyle3>::New();
      style->pointLocator = pointLocator;
      style->renderWindow = renderWindow;
      style->polydata = polydata;

      renderWindowInteractor->SetInteractorStyle( style );

      renderer->AddActor(actor);
      renderer->SetBackground(1,1,1); // Background color white

      renderWindow->Render();

      renderWindowInteractor->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(PointLocator)

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

### Download and Build PointLocator

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

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

    ./PointLocator

WINDOWS USERS

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