2009-01-11 17:07:57 +01:00
|
|
|
#include <ctime>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <iostream>
|
2009-12-07 15:47:36 +01:00
|
|
|
#include <fstream>
|
|
|
|
#define __KD_TREE_NUMNODES
|
2009-01-11 17:07:57 +01:00
|
|
|
#include "mykdtree.hpp"
|
2012-05-20 15:21:00 +02:00
|
|
|
#include "kdtree_splitters.hpp"
|
2009-01-11 17:07:57 +01:00
|
|
|
|
2010-03-02 17:59:41 +01:00
|
|
|
#define NTRY 100
|
2009-12-07 15:47:36 +01:00
|
|
|
#define ND 3
|
2009-01-11 17:07:57 +01:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace CosmoTool;
|
|
|
|
|
2012-05-20 15:21:00 +02:00
|
|
|
typedef KDTree<ND,char,ComputePrecision,KD_homogeneous_cell_splitter<ND, char> > MyTree;
|
2009-01-11 17:07:57 +01:00
|
|
|
typedef KDCell<ND,char> MyCell;
|
|
|
|
|
|
|
|
MyCell *findNearest(MyTree::coords& xc, MyCell *cells, uint32_t Ncells)
|
|
|
|
{
|
|
|
|
MyCell *near2 = 0;
|
|
|
|
double R2 = INFINITY;
|
|
|
|
for (int i = 0; i < Ncells; i++)
|
|
|
|
{
|
|
|
|
double d2 = 0;
|
|
|
|
for (int j = 0; j < ND; j++)
|
|
|
|
{
|
|
|
|
double delta = xc[j]-cells[i].coord[j];
|
|
|
|
d2 += delta*delta;
|
|
|
|
}
|
|
|
|
if (d2 < R2)
|
|
|
|
{
|
|
|
|
near2 = &cells[i];
|
|
|
|
R2 = d2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return near2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
uint32_t Ncells = 100000;
|
|
|
|
MyCell *cells = new MyCell[Ncells];
|
|
|
|
|
|
|
|
for (int i = 0; i < Ncells; i++)
|
|
|
|
{
|
|
|
|
cells[i].active = true;
|
|
|
|
for (int l = 0; l < ND; l++)
|
|
|
|
cells[i].coord[l] = drand48();
|
|
|
|
}
|
|
|
|
|
|
|
|
MyTree tree(cells, Ncells);
|
|
|
|
|
|
|
|
MyTree::coords *xc = new MyTree::coords[NTRY];
|
|
|
|
|
|
|
|
cout << "Generating seeds..." << endl;
|
|
|
|
for (int k = 0; k < NTRY; k++)
|
|
|
|
{
|
|
|
|
for (int l = 0; l < ND; l++)
|
|
|
|
xc[k][l] = drand48();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check consistency
|
|
|
|
cout << "Check consistency..." << endl;
|
|
|
|
MyCell **ngb = new MyCell *[12];
|
2010-03-02 17:59:41 +01:00
|
|
|
double *distances = new double[12];
|
2009-01-11 17:07:57 +01:00
|
|
|
|
2009-12-07 15:47:36 +01:00
|
|
|
ofstream fngb("nearest.txt");
|
2009-01-11 17:07:57 +01:00
|
|
|
for (int k = 0; k < NTRY; k++) {
|
|
|
|
cout << "Seed = " << xc[k][0] << " " << xc[k][1] << " " << xc[k][2] << endl;
|
2010-03-02 17:59:41 +01:00
|
|
|
tree.getNearestNeighbours(xc[k], 12, ngb, distances);
|
2009-01-11 17:07:57 +01:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < 12; i++)
|
|
|
|
{
|
|
|
|
double d2 = 0;
|
|
|
|
for (int l = 0; l < 3; l++)
|
|
|
|
d2 += ({double delta = xc[k][l] - ngb[i]->coord[l]; delta*delta;});
|
2009-12-07 15:47:36 +01:00
|
|
|
fngb << ngb[i]->coord[0] << " " << ngb[i]->coord[1] << " " << ngb[i]->coord[2] << " " << sqrt(d2) << endl;
|
2009-01-11 17:07:57 +01:00
|
|
|
}
|
2010-03-02 17:59:41 +01:00
|
|
|
fngb << endl << endl;
|
|
|
|
double farther_dist = distances[11];
|
|
|
|
for (uint32_t i = 0; i < Ncells; i++)
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
// If the points is not in the list, it means it is farther than the farther point
|
|
|
|
for (int j =0; j < 12; j++)
|
|
|
|
{
|
|
|
|
if (&cells[i] == ngb[j]) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
double dist_to_seed = 0;
|
|
|
|
for (int l = 0; l < 3; l++)
|
|
|
|
{ double delta = xc[k][l]-cells[i].coord[l];
|
|
|
|
dist_to_seed += delta*delta; }
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
if (dist_to_seed <= farther_dist)
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (dist_to_seed > farther_dist)
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
2009-01-11 17:07:57 +01:00
|
|
|
}
|
2009-12-07 15:47:36 +01:00
|
|
|
|
2009-01-11 17:07:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|