VTK/Examples/KDTree

From KitwarePublic

< VTK | Examples(Redirected from KDTree)
Jump to: navigation, search

Fix: Remove usage of drand48()

Fix: Add CMakeLists.txt

KDTree.cxx

#include <vtkPoints.h>
#include <vtkSmartPointer.h>
#include <vtkIdList.h>
#include <vtkKdTree.h>
 
int main(int argc, char *argv[])
{
  //Create some random points
  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
  for(unsigned int i = 0; i < 10; i++)
    {
    double point[3] = {drand48(), drand48(), drand48()};
    vtkstd::cout << "Point " << i << ": (" << point[0] << ", " << point[1] << ", " << point[2] << ")" << vtkstd::endl;
    points->InsertNextPoint(Point);
    }
 
  //Create the tree
  vtkSmartPointer<vtkKdTree> pointTree = vtkSmartPointer<vtkKdTree>::New();
  pointTree->BuildLocatorFromPoints(points);
 
  // Find the k closest points to (0,0,0)
  unsigned int k = 2;
  double testPoint[3] = {0.0, 0.0, 0.0};
  vtkSmartPointer<vtkIdList> result = vtkSmartPointer<vtkIdList>::New();
 
  pointTree->FindClosestNPoints(k, testPoint, result);
 
  for(int i = 0; i < k; i++)
    {
    int point_ind = static_cast<int>(result->GetId(i));
    double p[3];
    points->GetPoint(point_ind, p);
    vtkstd::cout << "Closest point " << i << ": Point " << point_ind << ": (" << p[0] << ", " << p[1] << ", " << p[2] << ")" << vtkstd::endl;
    }
 
  return 0;
}

CMakeLists.txt

 
Personal tools