Skip to content

Instantly share code, notes, and snippets.

@afabri
Created June 29, 2021 11:48
Show Gist options
  • Save afabri/6f524e56fa32d8e6ad7b749ee8119c10 to your computer and use it in GitHub Desktop.
Save afabri/6f524e56fa32d8e6ad7b749ee8119c10 to your computer and use it in GitHub Desktop.
Unbalanced kd tree
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Timer.h>
#include <CGAL/Orthogonal_k_neighbor_search.h>
#include <CGAL/Search_traits_3.h>
#include <list>
#include <vector>
#include <cmath>
#include <fstream>
typedef CGAL::Simple_cartesian<double> K;
typedef K::Point_3 Point_3;
typedef CGAL::Search_traits_3<K> Traits;
typedef CGAL::Euclidean_distance<Traits> Distance;
typedef CGAL::Sliding_midpoint<Traits> Fair;
typedef CGAL::Orthogonal_k_neighbor_search<Traits,Distance,Fair> Neighbor_search;
typedef Neighbor_search::Tree Tree;
int main(int argc, char* argv[])
{
const unsigned int N = 1;
std::vector<Point_3> points;
std::ifstream in(argv[1]);
Point_3 p,q;
while(in >> p >> q){
points.push_back(p);
}
Tree tree(points.begin(), points.end());
tree.build();
tree.statistics(std::cout);
// Initialize the search structure, and search all N points
Point_3 query(0,0,0);
Neighbor_search search(tree, query, N);
// report the N nearest neighbors and their distance
// This should sort all N points by increasing distance from origin
for(Neighbor_search::iterator it = search.begin(); it != search.end(); ++it)
std::cout << it->first << " "<< std::sqrt(it->second) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment