Add comparison with kdtree for nearest neighbor

This commit is contained in:
Jackson Campolattaro 2020-11-03 18:29:29 -05:00
parent b1571bfa45
commit 514f937278
4 changed files with 58 additions and 20 deletions

View File

@ -10,7 +10,6 @@
#include <CGAL/Orthogonal_k_neighbor_search.h>
#include <iostream>
#include <fstream>
#include <chrono>
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;

View File

@ -3,12 +3,13 @@
#include "util.h"
#include <CGAL/Octree.h>
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Octree.h>
#include <CGAL/Search_traits_3.h>
#include <CGAL/Orthogonal_k_neighbor_search.h>
#include <iostream>
#include <fstream>
#include <chrono>
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
@ -18,21 +19,30 @@ typedef Point_set::Point_map Point_map;
typedef CGAL::Octree<Kernel, Point_set, Point_map> Octree;
typedef CGAL::Search_traits_3<Kernel> Kd_tree_traits;
typedef CGAL::Orthogonal_k_neighbor_search<Kd_tree_traits> Kd_tree_search;
typedef Kd_tree_search::Tree Kdtree;
using std::chrono::high_resolution_clock;
using std::chrono::duration_cast;
using std::chrono::microseconds;
int main(int argc, char **argv) {
size_t k = 10;
// Set output file
std::ofstream file;
file.open((argc > 1) ? argv[1] : "../nearest_neighbor_benchmark.csv");
// Add header for CSV
file << "Number of Points,Search Time (ms) \n";
file << "Number of Points,Octree,kDTree \n";
// Perform tests for various dataset sizes
for (size_t num_points = 10; num_points < 1000000; num_points *= 1.1) {
for (size_t num_points = 100; num_points < 10000000; num_points *= 1.05) {
file << num_points << ",";
std::cout << num_points << std::endl;
// Create a collection of the right number of points
auto points = generate<Kernel>(num_points);
@ -40,22 +50,34 @@ int main(int argc, char **argv) {
// Create a search point
auto search_point = *generate<Kernel>().points().begin();
// Build the tree (not timed)
Octree octree(points, points.point_map());
octree.refine();
{
// Build the tree (not timed)
auto octreePoints = points;
Octree octree(octreePoints, octreePoints.point_map());
octree.refine();
auto elapsedTime = bench<microseconds>(
[&] {
// Find the nearest point to the search point
std::vector<Point> nearest_neighbors;
octree.nearest_neighbors(search_point, 10, std::back_inserter(nearest_neighbors));
}
);
auto octreeTime = bench<microseconds>(
[&] {
std::vector<Point> nearest_neighbors;
octree.nearest_neighbors(search_point, k, std::back_inserter(nearest_neighbors));
}
);
file << octreeTime.count() << ",";
}
file << num_points << ",";
file << elapsedTime.count() << "\n";
{
auto kdtreePoints = points;
Kdtree kdtree(kdtreePoints.points().begin(), kdtreePoints.points().end());
kdtree.build();
auto kdtreeTime = bench<microseconds>(
[&] {
Kd_tree_search search(kdtree, search_point, k);
}
);
file << kdtreeTime.count() << "\n";
}
std::cout << num_points << std::endl;
}
file.close();

View File

@ -10,7 +10,7 @@ set style data lines
set title 'Time to construct a tree from points'
set xlabel "Number of points"
set ylabel "Time (ms)"
set ylabel "Time (us)"
set key autotitle columnhead
set datafile separator ","

View File

@ -0,0 +1,17 @@
reset
set terminal png size 800,500
set output 'nearest_neighbor_benchmark.png'
set grid
set style data lines
#set logscale x
set title 'Time to find the neighbors of a point using a tree'
set xlabel "Number of points"
set ylabel "Time (us)"
set key autotitle columnhead
set datafile separator ","
plot for [col=2:3] 'nearest_neighbor_benchmark.csv' using 1:col