Skip to content

PointLocator

vtk-examples/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.

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

PointLocator.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointLocator.h>
#include <vtkPointSource.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

#include <iostream>
#include <string>

namespace {

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

  KeyPressInteractorStyle3() : PointsPerBucket(1)
  {
    pointLocator = nullptr;
    renderWindow = nullptr;
    polydata = nullptr;
  }

  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);

} // namespace

int main(int, char*[])
{
  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkPointSource> pointSource;
  pointSource->SetNumberOfPoints(4000);
  pointSource->Update();

  // Create the tree
  vtkNew<vtkPointLocator> pointLocator;
  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;

  vtkNew<vtkPolyData> polydata;
  pointLocator->GenerateRepresentation(pointLocator->GetLevel(), polydata);

  // Visualize
  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputData(polydata);

  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetColor(colors->GetColor3d("Peru").GetData());

  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("PointLocator");

  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  vtkNew<KeyPressInteractorStyle3> style;
  style->pointLocator = pointLocator;
  style->renderWindow = renderWindow;
  style->polydata = polydata;

  renderWindowInteractor->SetInteractorStyle(style);

  renderer->AddActor(actor);
  renderer->SetBackground(colors->GetColor3d("Beige").GetData());
  renderer->ResetCamera();
  renderer->GetActiveCamera()->Elevation(60.0);
  renderer->GetActiveCamera()->Azimuth(30.0);
  renderWindow->SetWindowName("PointLocator");
  renderWindow->Render();

  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(PointLocator)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonDataModel
  FiltersSources
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "PointLocator: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
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}
)

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.