Skip to content

IncrementalOctreePointLocator

Repository source: IncrementalOctreePointLocator

Question

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

Code

IncrementalOctreePointLocator.cxx

#include <vtkIncrementalOctreePointLocator.h>
#include <vtkMath.h>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>

#include <cmath>

int main(int, char*[])
{
  // Setup point coordinates.
  double x[3] = {1.0, 0.0, 0.0};
  double y[3] = {0.0, 1.0, 0.0};
  double z[3] = {0.0, 0.0, 1.0};

  vtkNew<vtkPoints> points;
  points->InsertNextPoint(x);
  points->InsertNextPoint(y);
  points->InsertNextPoint(z);

  vtkNew<vtkPolyData> polydata;
  polydata->SetPoints(points);

  // Create the tree
  vtkNew<vtkIncrementalOctreePointLocator> octree;
  octree->SetDataSet(polydata);
  octree->BuildLocator();

  auto pointCoordinates = [](double const* pt) {
    std::cout << "Coordinates: " << pt[0] << " " << pt[1] << " " << pt[2]
              << std::endl;
  };
  double testPoint[3] = {2.0, 0.0, 0.0};
  std::cout << "Test Point ";
  pointCoordinates(testPoint);

  auto closestPoint = [&octree, pointCoordinates](double* testPoint) {
    // Find the closest points to TestPoint.
    vtkIdType id = octree->FindClosestPoint(testPoint);
    std::cout << "The closest point is point " << id << std::endl;

    // Get the coordinates of the closest point.
    double pt[3];
    octree->GetDataSet()->GetPoint(id, pt);
    pointCoordinates(pt);
    std::cout << "Distance: "
              << std::sqrt(vtkMath::Distance2BetweenPoints(testPoint, pt))
              << std::endl;
  };

  closestPoint(testPoint);

  // Insert another point.
  double pnew[3] = {2.1, 0, 0};
  octree->InsertNextPoint(pnew);

  closestPoint(testPoint);

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(IncrementalOctreePointLocator)

find_package(VTK COMPONENTS 
  CommonCore
  CommonDataModel
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "IncrementalOctreePointLocator: 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(IncrementalOctreePointLocator MACOSX_BUNDLE IncrementalOctreePointLocator.cxx )
  target_link_libraries(IncrementalOctreePointLocator PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS IncrementalOctreePointLocator
  MODULES ${VTK_LIBRARIES}
)

Download and Build IncrementalOctreePointLocator

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

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

./IncrementalOctreePointLocator

WINDOWS USERS

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