Skip to content

Commit

Permalink
Add naive nearest neighbour solver and printouts
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonCampolattaro committed Jul 8, 2020
1 parent 46f0ec5 commit 4dca573
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions Octree/test/Octree/test_nearest_neighbor.cpp
Expand Up @@ -6,25 +6,69 @@
#include <CGAL/Octree/IO.h>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Point_set_3.h>
#include <CGAL/point_generators_3.h>
#include <CGAL/squared_distance_3.h>

#include <cassert>

typedef CGAL::Simple_cartesian<double> Kernel;
typedef Kernel::Point_3 Point;
typedef Kernel::FT FT;
typedef CGAL::Point_set_3<Point> Point_set;
typedef CGAL::Octree::Octree
<Point_set, typename Point_set::Point_map>
Octree;

void naive_vs_accelerated() {
void naive_vs_accelerated(std::size_t dataset_size) {

// Create a dataset
Point_set points;
CGAL::Random_points_in_cube_3<Point> generator;
points.reserve(dataset_size);
for (std::size_t i = 0; i < dataset_size; ++i)
points.insert(*(generator++));

// Choose a random point
// Choose another random point from the same bounds as the dataset
Point random_point = *(generator++);

// Use the naive algorithm to find the nearest point in the dataset
Point naive_nearest = *points.points().begin();
{
FT distance_nearest = std::numeric_limits<FT>::max();
for (auto &p : points.points()) {

FT distance_current = CGAL::squared_distance(p, random_point);
if (distance_current < distance_nearest) {

distance_nearest = distance_current;
naive_nearest = p;
}
}
}

std::cout << "Naive --> "
<< "Closest point to "
<< "(" << random_point << ") "
<< "is "
<< "(" << naive_nearest << ") "
<< "at a distance^2 of "
<< CGAL::squared_distance(naive_nearest, random_point)
<< std::endl;

// Do the same using the octree
Point octree_nearest = *generator;
{

}

std::cout << "Octree --> "
<< "Closest point to "
<< "(" << random_point << ") "
<< "is "
<< "(" << octree_nearest << ") "
<< "at a distance^2 of "
<< CGAL::squared_distance(octree_nearest, random_point)
<< std::endl;

// Check that they produce the same answer

Expand All @@ -34,7 +78,10 @@ void naive_vs_accelerated() {

int main(void) {

naive_vs_accelerated();
naive_vs_accelerated(100);
naive_vs_accelerated(1000);
naive_vs_accelerated(10000);
naive_vs_accelerated(100000);

return EXIT_SUCCESS;
}

0 comments on commit 4dca573

Please sign in to comment.