2013-03-05 17:08:19 +01:00
|
|
|
#include "replicateGenerator.hpp"
|
2009-01-08 16:18:14 +01:00
|
|
|
#include <cstring>
|
2014-05-22 09:27:53 +02:00
|
|
|
#include "omptl/omptl_algorithm"
|
2009-01-08 16:18:14 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <limits>
|
|
|
|
#include <iostream>
|
2009-12-07 15:46:39 +01:00
|
|
|
#include <cassert>
|
2009-01-08 16:18:14 +01:00
|
|
|
|
|
|
|
namespace CosmoTool {
|
|
|
|
|
2009-02-04 02:00:46 +01:00
|
|
|
template<int N, typename ValType, typename CType>
|
2009-01-08 16:18:14 +01:00
|
|
|
class CellCompare
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CellCompare(int k)
|
|
|
|
{
|
|
|
|
rank = k;
|
|
|
|
}
|
|
|
|
|
2009-02-04 02:00:46 +01:00
|
|
|
bool operator()(const KDCell<N,ValType,CType> *a, const KDCell<N,ValType,CType> *b) const
|
2009-01-08 16:18:14 +01:00
|
|
|
{
|
|
|
|
return (a->coord[rank] < b->coord[rank]);
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
int rank;
|
|
|
|
};
|
|
|
|
|
2012-05-20 15:21:00 +02:00
|
|
|
template<int N, typename ValType, typename CType, typename CellSplitter>
|
|
|
|
KDTree<N,ValType,CType,CellSplitter>::~KDTree()
|
2009-01-08 16:18:14 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-05-20 15:21:00 +02:00
|
|
|
template<int N, typename ValType, typename CType, typename CellSplitter>
|
|
|
|
KDTree<N,ValType,CType,CellSplitter>::KDTree(Cell *cells, uint32_t Ncells)
|
2009-01-08 16:18:14 +01:00
|
|
|
{
|
2013-03-05 04:48:13 +01:00
|
|
|
periodic = false;
|
|
|
|
|
2011-02-25 00:04:42 +01:00
|
|
|
base_cell = cells;
|
2009-01-08 16:18:14 +01:00
|
|
|
numNodes = Ncells;
|
2009-02-04 02:00:46 +01:00
|
|
|
nodes = new Node[numNodes];
|
2009-01-08 16:18:14 +01:00
|
|
|
|
2009-02-04 02:00:46 +01:00
|
|
|
sortingHelper = new Cell *[Ncells];
|
2009-01-08 16:18:14 +01:00
|
|
|
for (uint32_t i = 0; i < Ncells; i++)
|
|
|
|
sortingHelper[i] = &cells[i];
|
|
|
|
|
|
|
|
optimize();
|
|
|
|
}
|
|
|
|
|
2012-05-20 15:21:00 +02:00
|
|
|
template<int N, typename ValType, typename CType, typename CellSplitter>
|
|
|
|
void KDTree<N,ValType,CType,CellSplitter>::optimize()
|
2009-01-08 16:18:14 +01:00
|
|
|
{
|
|
|
|
coords absoluteMin, absoluteMax;
|
|
|
|
|
|
|
|
std::cout << "Optimizing the tree..." << std::endl;
|
|
|
|
uint32_t activeCells = gatherActiveCells(sortingHelper, numNodes);
|
|
|
|
std::cout << " number of active cells = " << activeCells << std::endl;
|
|
|
|
|
|
|
|
lastNode = 0;
|
|
|
|
for (int i = 0; i < N; i++)
|
|
|
|
{
|
2009-12-07 15:46:39 +01:00
|
|
|
absoluteMin[i] = std::numeric_limits<typeof (absoluteMin[0])>::max();
|
|
|
|
absoluteMax[i] = -std::numeric_limits<typeof (absoluteMax[0])>::max();
|
|
|
|
}
|
|
|
|
// Find min and max corner
|
|
|
|
for (uint32_t i = 0; i < activeCells; i++)
|
|
|
|
{
|
|
|
|
KDCell<N,ValType,CType> *cell = sortingHelper[i];
|
|
|
|
|
|
|
|
for (int k = 0; k < N; k++) {
|
|
|
|
if (cell->coord[k] < absoluteMin[k])
|
|
|
|
absoluteMin[k] = cell->coord[k];
|
|
|
|
if (cell->coord[k] > absoluteMax[k])
|
|
|
|
absoluteMax[k] = cell->coord[k];
|
|
|
|
}
|
2009-01-08 16:18:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << " rebuilding the tree..." << std::endl;
|
|
|
|
root = buildTree(sortingHelper, activeCells, 0, absoluteMin, absoluteMax);
|
|
|
|
std::cout << " done." << std::endl;
|
|
|
|
}
|
|
|
|
|
2012-05-20 15:21:00 +02:00
|
|
|
template<int N, typename ValType, typename CType, typename CellSplitter>
|
|
|
|
uint32_t KDTree<N,ValType,CType,CellSplitter>::getIntersection(const coords& x, CoordType r,
|
|
|
|
KDTree<N,ValType,CType,CellSplitter>::Cell **cells,
|
2009-02-04 02:00:46 +01:00
|
|
|
uint32_t numCells)
|
2009-01-08 16:18:14 +01:00
|
|
|
throw (NotEnoughCells)
|
|
|
|
{
|
2009-02-04 02:00:46 +01:00
|
|
|
RecursionInfoCells<N,ValType,CType> info;
|
2009-01-08 16:18:14 +01:00
|
|
|
|
|
|
|
memcpy(info.x, x, sizeof(x));
|
|
|
|
info.r = r;
|
|
|
|
info.r2 = r*r;
|
|
|
|
info.cells = cells;
|
|
|
|
info.currentRank = 0;
|
|
|
|
info.numCells = numCells;
|
2009-01-11 16:39:57 +01:00
|
|
|
info.distances = 0;
|
2009-01-08 16:18:14 +01:00
|
|
|
|
2011-06-22 20:57:06 +02:00
|
|
|
recursiveIntersectionCells<false>(info, root, 0);
|
2013-03-05 17:08:19 +01:00
|
|
|
if (periodic)
|
|
|
|
{
|
2013-03-05 17:11:20 +01:00
|
|
|
ReplicateGenerator<float, N> r(x, replicate);
|
2013-03-05 17:08:19 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
coords x_new;
|
2013-03-05 17:11:20 +01:00
|
|
|
r.getPosition(info.x);
|
2013-03-05 17:08:19 +01:00
|
|
|
recursiveIntersectionCells<false>(info, root, 0);
|
|
|
|
}
|
2013-03-05 17:11:20 +01:00
|
|
|
while (r.next());
|
2013-03-05 17:08:19 +01:00
|
|
|
}
|
|
|
|
|
2009-01-08 16:18:14 +01:00
|
|
|
return info.currentRank;
|
|
|
|
}
|
|
|
|
|
2012-05-20 15:21:00 +02:00
|
|
|
template<int N, typename ValType, typename CType, typename CellSplitter>
|
|
|
|
uint32_t KDTree<N,ValType,CType,CellSplitter>::getIntersection(const coords& x, CoordType r,
|
2009-02-04 02:00:46 +01:00
|
|
|
Cell **cells,
|
|
|
|
CoordType *distances,
|
|
|
|
uint32_t numCells)
|
2009-01-11 16:39:57 +01:00
|
|
|
throw (NotEnoughCells)
|
|
|
|
{
|
|
|
|
RecursionInfoCells<N,ValType> info;
|
|
|
|
|
|
|
|
memcpy(info.x, x, sizeof(x));
|
|
|
|
info.r = r;
|
|
|
|
info.r2 = r*r;
|
|
|
|
info.cells = cells;
|
|
|
|
info.currentRank = 0;
|
|
|
|
info.numCells = numCells;
|
|
|
|
info.distances = distances;
|
|
|
|
|
2011-06-22 20:57:06 +02:00
|
|
|
recursiveIntersectionCells<false>(info, root, 0);
|
2013-03-05 17:08:19 +01:00
|
|
|
if (periodic)
|
|
|
|
{
|
2013-03-05 17:11:20 +01:00
|
|
|
ReplicateGenerator<float, N> r(x, replicate);
|
2013-03-05 17:08:19 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
coords x_new;
|
2013-03-05 17:11:20 +01:00
|
|
|
r.getPosition(info.x);
|
2013-03-05 17:08:19 +01:00
|
|
|
recursiveIntersectionCells<false>(info, root, 0);
|
|
|
|
}
|
2013-03-05 17:11:20 +01:00
|
|
|
while (r.next());
|
2013-03-05 17:08:19 +01:00
|
|
|
}
|
2009-01-11 16:39:57 +01:00
|
|
|
return info.currentRank;
|
|
|
|
}
|
|
|
|
|
2012-05-20 15:21:00 +02:00
|
|
|
template<int N, typename ValType, typename CType, typename CellSplitter>
|
|
|
|
uint32_t KDTree<N,ValType,CType,CellSplitter>::countCells(const coords& x, CoordType r)
|
2011-06-22 20:57:06 +02:00
|
|
|
{
|
|
|
|
RecursionInfoCells<N,ValType> info;
|
|
|
|
|
|
|
|
memcpy(info.x, x, sizeof(x));
|
|
|
|
info.r = r;
|
|
|
|
info.r2 = r*r;
|
|
|
|
info.cells = 0;
|
|
|
|
info.currentRank = 0;
|
|
|
|
info.numCells = 0;
|
|
|
|
info.distances = 0;
|
|
|
|
|
|
|
|
recursiveIntersectionCells<true>(info, root, 0);
|
2013-03-05 17:08:19 +01:00
|
|
|
if (periodic)
|
|
|
|
{
|
2013-03-05 17:11:20 +01:00
|
|
|
ReplicateGenerator<float, N> r(x, replicate);
|
2013-03-05 17:08:19 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
coords x_new;
|
2013-03-05 17:11:20 +01:00
|
|
|
r.getPosition(info.x);
|
2013-03-05 17:08:19 +01:00
|
|
|
recursiveIntersectionCells<true>(info, root, 0);
|
|
|
|
}
|
2013-03-05 17:11:20 +01:00
|
|
|
while (r.next());
|
2013-03-05 17:08:19 +01:00
|
|
|
}
|
|
|
|
|
2011-06-22 20:57:06 +02:00
|
|
|
return info.currentRank;
|
|
|
|
}
|
|
|
|
|
2012-05-20 15:21:00 +02:00
|
|
|
template<int N, typename ValType, typename CType, typename CellSplitter>
|
2011-06-22 20:57:06 +02:00
|
|
|
template<bool justCount>
|
2012-05-20 15:21:00 +02:00
|
|
|
void KDTree<N,ValType,CType,CellSplitter>::recursiveIntersectionCells(RecursionInfoCells<N,ValType,CType>& info,
|
2009-02-04 02:00:46 +01:00
|
|
|
Node *node,
|
|
|
|
int level)
|
2009-01-08 16:18:14 +01:00
|
|
|
throw (NotEnoughCells)
|
|
|
|
{
|
|
|
|
int axis = level % N;
|
|
|
|
CoordType d2 = 0;
|
|
|
|
|
2014-05-30 17:11:49 +02:00
|
|
|
#if __KD_TREE_ACTIVE_CELLS == 1
|
2009-01-08 16:18:14 +01:00
|
|
|
if (node->value->active)
|
2014-05-30 17:11:49 +02:00
|
|
|
#endif
|
2009-01-08 16:18:14 +01:00
|
|
|
{
|
|
|
|
for (int j = 0; j < 3; j++)
|
|
|
|
{
|
|
|
|
CoordType delta = info.x[j]-node->value->coord[j];
|
|
|
|
d2 += delta*delta;
|
|
|
|
}
|
|
|
|
if (d2 < info.r2)
|
|
|
|
{
|
2011-06-22 20:57:06 +02:00
|
|
|
if (!justCount)
|
|
|
|
{
|
|
|
|
if (info.currentRank == info.numCells)
|
|
|
|
throw NotEnoughCells();
|
|
|
|
info.cells[info.currentRank] = node->value;
|
|
|
|
if (info.distances)
|
|
|
|
info.distances[info.currentRank] = d2;
|
|
|
|
}
|
2009-01-11 16:39:57 +01:00
|
|
|
info.currentRank++;
|
2009-01-08 16:18:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The hypersphere intersects the left child node
|
|
|
|
if (((info.x[axis]+info.r) > node->minBound[axis]) &&
|
|
|
|
((info.x[axis]-info.r) < node->value->coord[axis]))
|
|
|
|
{
|
|
|
|
if (node->children[0] != 0)
|
2011-06-22 20:57:06 +02:00
|
|
|
recursiveIntersectionCells<justCount>(info, node->children[0],
|
2009-01-08 16:18:14 +01:00
|
|
|
level+1);
|
|
|
|
}
|
|
|
|
if (((info.x[axis]+info.r) > node->value->coord[axis]) &&
|
|
|
|
((info.x[axis]-info.r) < node->maxBound[axis]))
|
|
|
|
{
|
|
|
|
if (node->children[1] != 0)
|
2011-06-22 20:57:06 +02:00
|
|
|
recursiveIntersectionCells<justCount>(info, node->children[1],
|
|
|
|
level+1);
|
2009-01-08 16:18:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-04 02:00:46 +01:00
|
|
|
template<int N, typename ValType, typename CType>
|
|
|
|
uint32_t gatherActiveCells(KDCell<N,ValType,CType> **cells,
|
|
|
|
uint32_t Ncells)
|
2009-01-08 16:18:14 +01:00
|
|
|
{
|
|
|
|
uint32_t swapId = Ncells-1;
|
|
|
|
uint32_t i = 0;
|
|
|
|
|
2014-05-30 17:11:49 +02:00
|
|
|
#if __KD_TREE_ACTIVE_CELLS == 1
|
2009-01-08 16:18:14 +01:00
|
|
|
while (!cells[swapId]->active && swapId > 0)
|
|
|
|
swapId--;
|
|
|
|
|
|
|
|
while (i < swapId)
|
|
|
|
{
|
|
|
|
if (!cells[i]->active)
|
|
|
|
{
|
|
|
|
std::swap(cells[i], cells[swapId]);
|
|
|
|
while (!cells[swapId]->active && swapId > i)
|
|
|
|
{
|
|
|
|
swapId--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2014-05-30 17:11:49 +02:00
|
|
|
#endif
|
2009-01-08 16:18:14 +01:00
|
|
|
return swapId+1;
|
|
|
|
}
|
|
|
|
|
2009-02-04 02:00:46 +01:00
|
|
|
template<int N, typename ValType, typename CType>
|
2012-05-20 15:21:00 +02:00
|
|
|
void KD_default_cell_splitter<N,ValType,CType>::operator()(KDCell<N,ValType,CType> **cells, uint32_t Ncells, uint32_t& split_index, int axis, typename KDDef<N,CType>::KDCoordinates minBound, typename KDDef<N,CType>::KDCoordinates maxBound)
|
|
|
|
{
|
|
|
|
CellCompare<N,ValType,CType> compare(axis);
|
2014-05-30 17:11:49 +02:00
|
|
|
omptl::sort(cells,cells+Ncells,compare); // std::sort(cells, cells+Ncells, compare);
|
2012-05-20 15:21:00 +02:00
|
|
|
split_index = Ncells/2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<int N, typename ValType, typename CType, typename CellSplitter>
|
|
|
|
KDTreeNode<N,ValType,CType> *KDTree<N,ValType,CType,CellSplitter>::buildTree(Cell **cell0,
|
|
|
|
uint32_t Ncells,
|
|
|
|
uint32_t depth,
|
|
|
|
coords minBound,
|
|
|
|
coords maxBound)
|
2009-01-08 16:18:14 +01:00
|
|
|
{
|
|
|
|
if (Ncells == 0)
|
|
|
|
return 0;
|
|
|
|
|
2014-05-30 17:11:49 +02:00
|
|
|
Node *node;
|
2009-01-08 16:18:14 +01:00
|
|
|
int axis = depth % N;
|
2012-05-20 15:21:00 +02:00
|
|
|
uint32_t mid;
|
2009-01-08 16:18:14 +01:00
|
|
|
coords tmpBound;
|
2014-05-30 17:11:49 +02:00
|
|
|
|
|
|
|
#pragma omp critical
|
|
|
|
node = &nodes[lastNode++];
|
2009-01-08 16:18:14 +01:00
|
|
|
|
|
|
|
// Isolate the environment
|
2012-05-20 15:21:00 +02:00
|
|
|
splitter(cell0, Ncells, mid, axis, minBound, maxBound);
|
2009-01-08 16:18:14 +01:00
|
|
|
|
|
|
|
node->value = *(cell0+mid);
|
|
|
|
memcpy(&node->minBound[0], &minBound[0], sizeof(coords));
|
|
|
|
memcpy(&node->maxBound[0], &maxBound[0], sizeof(coords));
|
|
|
|
|
|
|
|
memcpy(tmpBound, maxBound, sizeof(coords));
|
|
|
|
tmpBound[axis] = node->value->coord[axis];
|
|
|
|
|
|
|
|
depth++;
|
2014-05-30 17:11:49 +02:00
|
|
|
#pragma omp task private(tmpBound)
|
|
|
|
{
|
|
|
|
node->children[0] = buildTree(cell0, mid, depth, minBound, tmpBound);
|
|
|
|
}
|
2009-01-08 16:18:14 +01:00
|
|
|
|
|
|
|
memcpy(tmpBound, minBound, sizeof(coords));
|
|
|
|
tmpBound[axis] = node->value->coord[axis];
|
2014-05-30 17:11:49 +02:00
|
|
|
#pragma omp task private(tmpBound)
|
|
|
|
{
|
|
|
|
node->children[1] = buildTree(cell0+mid+1, Ncells-mid-1, depth,
|
2009-01-08 16:18:14 +01:00
|
|
|
tmpBound, maxBound);
|
2014-05-30 17:11:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma omp taskwait
|
2009-01-08 16:18:14 +01:00
|
|
|
|
2009-12-07 15:46:39 +01:00
|
|
|
#ifdef __KD_TREE_NUMNODES
|
|
|
|
node->numNodes = (node->children[0] != 0) ? node->children[0]->numNodes : 0;
|
|
|
|
node->numNodes += (node->children[1] != 0) ? node->children[1]->numNodes : 0;
|
|
|
|
node->numNodes++;
|
|
|
|
#endif
|
|
|
|
|
2009-01-08 16:18:14 +01:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2012-05-20 15:21:00 +02:00
|
|
|
template<int N, typename ValType, typename CType, typename CellSplitter>
|
|
|
|
uint32_t KDTree<N,ValType,CType,CellSplitter>::countActives() const
|
2009-01-08 16:18:14 +01:00
|
|
|
{
|
|
|
|
uint32_t numActive = 0;
|
|
|
|
for (uint32_t i = 0; i < lastNode; i++)
|
|
|
|
{
|
2014-05-30 17:11:49 +02:00
|
|
|
#if __KD_TREE_ACTIVE_CELLS == 1
|
2009-01-08 16:18:14 +01:00
|
|
|
if (nodes[i].value->active)
|
2014-05-30 17:11:49 +02:00
|
|
|
#endif
|
2009-01-08 16:18:14 +01:00
|
|
|
numActive++;
|
|
|
|
}
|
|
|
|
return numActive;
|
|
|
|
}
|
|
|
|
|
2012-05-20 15:21:00 +02:00
|
|
|
template<int N, typename ValType, typename CType, typename CellSplitter>
|
2009-02-04 02:00:46 +01:00
|
|
|
typename KDDef<N,CType>::CoordType
|
2012-05-20 15:21:00 +02:00
|
|
|
KDTree<N,ValType,CType,CellSplitter>::computeDistance(const Cell *cell, const coords& x) const
|
2009-01-08 16:18:14 +01:00
|
|
|
{
|
|
|
|
CoordType d2 = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < N; i++)
|
|
|
|
{
|
|
|
|
CoordType delta = cell->coord[i] - x[i];
|
|
|
|
d2 += delta*delta;
|
|
|
|
}
|
|
|
|
return d2;
|
|
|
|
}
|
|
|
|
|
2012-05-20 15:21:00 +02:00
|
|
|
template<int N, typename ValType, typename CType, typename CellSplitter>
|
2009-01-08 16:18:14 +01:00
|
|
|
void
|
2012-05-20 15:21:00 +02:00
|
|
|
KDTree<N,ValType,CType,CellSplitter>::recursiveNearest(
|
2009-02-04 02:00:46 +01:00
|
|
|
Node *node,
|
2009-01-08 16:18:14 +01:00
|
|
|
int level,
|
|
|
|
const coords& x,
|
|
|
|
CoordType& R2,
|
2009-02-04 02:00:46 +01:00
|
|
|
Cell *& best)
|
2009-01-08 16:18:14 +01:00
|
|
|
{
|
|
|
|
CoordType d2 = 0;
|
|
|
|
int axis = level % N;
|
2009-02-04 02:00:46 +01:00
|
|
|
Node *other, *go;
|
2009-01-08 16:18:14 +01:00
|
|
|
|
|
|
|
if (x[axis] < node->value->coord[axis])
|
|
|
|
{
|
|
|
|
// The best is potentially in 0.
|
|
|
|
go = node->children[0];
|
|
|
|
other = node->children[1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If not it is in 1.
|
|
|
|
go = node->children[1];
|
|
|
|
other = node->children[0];
|
|
|
|
if (go == 0)
|
|
|
|
{
|
|
|
|
go = other;
|
|
|
|
other = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (go != 0)
|
|
|
|
{
|
|
|
|
recursiveNearest(go, level+1,
|
|
|
|
x, R2,best);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CoordType thisR2 = computeDistance(node->value, x);
|
|
|
|
if (thisR2 < R2)
|
|
|
|
{
|
|
|
|
R2 = thisR2;
|
|
|
|
best = node->value;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if current node is not the nearest
|
|
|
|
CoordType thisR2 =
|
|
|
|
computeDistance(node->value, x);
|
|
|
|
|
|
|
|
if (thisR2 < R2)
|
|
|
|
{
|
|
|
|
R2 = thisR2;
|
|
|
|
best = node->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we found the best. We check whether the hypersphere
|
|
|
|
// intersect the hyperplane of the other branch
|
|
|
|
|
|
|
|
CoordType delta1;
|
|
|
|
|
|
|
|
delta1 = x[axis]-node->value->coord[axis];
|
|
|
|
if (delta1*delta1 < R2)
|
|
|
|
{
|
|
|
|
// The hypersphere intersects the hyperplane. Try the
|
|
|
|
// other branch
|
|
|
|
if (other != 0)
|
|
|
|
{
|
|
|
|
recursiveNearest(other, level+1, x, R2, best);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-20 15:21:00 +02:00
|
|
|
template<int N, typename ValType, typename CType, typename CellSplitter>
|
2009-02-04 02:00:46 +01:00
|
|
|
KDCell<N,ValType,CType> *
|
2012-05-20 15:21:00 +02:00
|
|
|
KDTree<N,ValType,CType,CellSplitter>::getNearestNeighbour(const coords& x)
|
2009-01-08 16:18:14 +01:00
|
|
|
{
|
|
|
|
CoordType R2 = INFINITY;
|
2009-02-04 02:00:46 +01:00
|
|
|
Cell *best = 0;
|
2009-01-08 16:18:14 +01:00
|
|
|
|
2013-03-05 04:48:13 +01:00
|
|
|
recursiveNearest(root, 0, x, R2, best);
|
|
|
|
if (periodic)
|
|
|
|
{
|
2013-03-05 17:11:20 +01:00
|
|
|
ReplicateGenerator<float, N> r(x, replicate);
|
2013-03-05 04:48:13 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2013-03-05 17:08:19 +01:00
|
|
|
coords x_new;
|
2013-03-05 17:11:20 +01:00
|
|
|
r.getPosition(x_new);
|
2013-03-05 17:08:19 +01:00
|
|
|
recursiveNearest(root, 0, x_new, R2, best);
|
2013-03-05 04:48:13 +01:00
|
|
|
}
|
2013-03-05 17:11:20 +01:00
|
|
|
while (r.next());
|
2013-03-05 04:48:13 +01:00
|
|
|
}
|
2009-01-08 16:18:14 +01:00
|
|
|
|
|
|
|
return best;
|
|
|
|
}
|
|
|
|
|
2012-05-20 15:21:00 +02:00
|
|
|
template<int N, typename ValType, typename CType, typename CellSplitter>
|
2009-01-10 00:42:14 +01:00
|
|
|
void
|
2012-05-20 15:21:00 +02:00
|
|
|
KDTree<N,ValType,CType,CellSplitter>::recursiveMultipleNearest(RecursionMultipleInfo<N,ValType,CType>& info, Node *node,
|
2009-01-10 00:42:14 +01:00
|
|
|
int level)
|
|
|
|
{
|
|
|
|
CoordType d2 = 0;
|
|
|
|
int axis = level % N;
|
2009-02-04 02:00:46 +01:00
|
|
|
Node *other, *go;
|
2009-01-10 00:42:14 +01:00
|
|
|
|
|
|
|
if (info.x[axis] < node->value->coord[axis])
|
|
|
|
{
|
|
|
|
// The best is potentially in 0.
|
|
|
|
go = node->children[0];
|
|
|
|
other = node->children[1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If not it is in 1.
|
|
|
|
go = node->children[1];
|
|
|
|
other = node->children[0];
|
2012-05-21 20:04:21 +02:00
|
|
|
// if (go == 0)
|
|
|
|
// {
|
|
|
|
// go = other;
|
|
|
|
//other = 0;
|
|
|
|
//}
|
2009-01-10 00:42:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (go != 0)
|
|
|
|
{
|
|
|
|
recursiveMultipleNearest(info, go, level+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if current node is not the nearest
|
|
|
|
CoordType thisR2 =
|
|
|
|
computeDistance(node->value, info.x);
|
|
|
|
info.queue.push(node->value, thisR2);
|
|
|
|
info.traversed++;
|
2012-05-21 20:04:21 +02:00
|
|
|
// if (go == 0)
|
|
|
|
// return;
|
2009-01-10 00:42:14 +01:00
|
|
|
|
|
|
|
// Now we found the best. We check whether the hypersphere
|
|
|
|
// intersect the hyperplane of the other branch
|
|
|
|
|
|
|
|
CoordType delta1;
|
|
|
|
|
|
|
|
delta1 = info.x[axis]-node->value->coord[axis];
|
|
|
|
if (delta1*delta1 < info.queue.getMaxPriority())
|
|
|
|
{
|
|
|
|
// The hypersphere intersects the hyperplane. Try the
|
|
|
|
// other branch
|
|
|
|
if (other != 0)
|
|
|
|
{
|
|
|
|
recursiveMultipleNearest(info, other, level+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-20 15:21:00 +02:00
|
|
|
template<int N, typename ValType, typename CType, typename CellSplitter>
|
|
|
|
void KDTree<N,ValType,CType,CellSplitter>::getNearestNeighbours(const coords& x, uint32_t N2,
|
2009-02-04 02:00:46 +01:00
|
|
|
Cell **cells)
|
2009-01-11 16:39:57 +01:00
|
|
|
{
|
|
|
|
RecursionMultipleInfo<N,ValType> info(x, cells, N2);
|
|
|
|
|
|
|
|
for (int i = 0; i < N2; i++)
|
|
|
|
cells[i] = 0;
|
|
|
|
|
|
|
|
recursiveMultipleNearest(info, root, 0);
|
2013-03-05 17:08:19 +01:00
|
|
|
if (periodic)
|
|
|
|
{
|
2013-03-05 17:11:20 +01:00
|
|
|
ReplicateGenerator<float, N> r(x, replicate);
|
2013-03-05 17:08:19 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
coords x_new;
|
2013-03-05 17:11:20 +01:00
|
|
|
r.getPosition(info.x);
|
2013-03-05 17:08:19 +01:00
|
|
|
recursiveMultipleNearest(info, root, 0);
|
|
|
|
}
|
2013-03-05 17:11:20 +01:00
|
|
|
while (r.next());
|
2013-03-05 17:08:19 +01:00
|
|
|
}
|
2009-01-11 16:39:57 +01:00
|
|
|
|
2009-02-04 02:00:46 +01:00
|
|
|
// std::cout << "Traversed = " << info.traversed << std::endl;
|
2009-01-11 16:39:57 +01:00
|
|
|
}
|
|
|
|
|
2012-05-20 15:21:00 +02:00
|
|
|
template<int N, typename ValType, typename CType, typename CellSplitter>
|
|
|
|
void KDTree<N,ValType,CType,CellSplitter>::getNearestNeighbours(const coords& x, uint32_t N2,
|
2009-02-04 02:00:46 +01:00
|
|
|
Cell **cells,
|
|
|
|
CoordType *distances)
|
2009-01-08 16:18:14 +01:00
|
|
|
{
|
2009-01-10 00:42:14 +01:00
|
|
|
RecursionMultipleInfo<N,ValType> info(x, cells, N2);
|
|
|
|
|
|
|
|
for (int i = 0; i < N2; i++)
|
|
|
|
cells[i] = 0;
|
|
|
|
|
|
|
|
recursiveMultipleNearest(info, root, 0);
|
2013-03-05 17:08:19 +01:00
|
|
|
if (periodic)
|
|
|
|
{
|
2013-03-05 17:11:20 +01:00
|
|
|
ReplicateGenerator<float, N> r(x, replicate);
|
2009-01-10 00:42:14 +01:00
|
|
|
|
2013-03-05 17:08:19 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
coords x_new;
|
2013-03-05 17:11:20 +01:00
|
|
|
r.getPosition(info.x);
|
2013-03-05 17:08:19 +01:00
|
|
|
recursiveMultipleNearest(info, root, 0);
|
|
|
|
}
|
2013-03-05 17:11:20 +01:00
|
|
|
while (r.next());
|
2013-03-05 17:08:19 +01:00
|
|
|
}
|
|
|
|
memcpy(distances, info.queue.getPriorities(), sizeof(CoordType)*N2);
|
2009-01-08 16:18:14 +01:00
|
|
|
}
|
|
|
|
|
2011-02-25 00:04:42 +01:00
|
|
|
#ifdef __KD_TREE_SAVE_ON_DISK
|
|
|
|
#define KDTREE_DISK_SIGNATURE "KDTREE"
|
|
|
|
#define KDTREE_DISK_SIGNATURE_LEN 7
|
|
|
|
|
|
|
|
template<int N, typename CType>
|
|
|
|
struct KDTreeOnDisk
|
|
|
|
{
|
|
|
|
long cell_id;
|
|
|
|
long children_node[2];
|
|
|
|
typename KDDef<N, CType>::KDCoordinates minBound, maxBound;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct KDTreeHeader
|
|
|
|
{
|
|
|
|
char id[KDTREE_DISK_SIGNATURE_LEN];
|
|
|
|
long nodesUsed, numCells;
|
2011-03-17 19:39:20 +01:00
|
|
|
long rootId;
|
2011-02-25 00:04:42 +01:00
|
|
|
};
|
|
|
|
|
2012-05-20 15:21:00 +02:00
|
|
|
template<int N, typename ValType, typename CType, typename CellSplitter>
|
|
|
|
void KDTree<N,ValType,CType,CellSplitter>::saveTree(std::ostream& o) const
|
2011-02-25 00:04:42 +01:00
|
|
|
{
|
|
|
|
KDTreeHeader h;
|
|
|
|
|
|
|
|
strncpy(h.id, KDTREE_DISK_SIGNATURE, KDTREE_DISK_SIGNATURE_LEN);
|
|
|
|
h.nodesUsed = lastNode;
|
|
|
|
h.numCells = numNodes;
|
2011-03-17 19:39:20 +01:00
|
|
|
h.rootId = root - nodes;
|
2011-02-25 00:04:42 +01:00
|
|
|
o.write((char*)&h, sizeof(h));
|
|
|
|
|
|
|
|
for (long i = 0; i < lastNode; i++)
|
|
|
|
{
|
|
|
|
KDTreeOnDisk<N,CType> node_on_disk;
|
|
|
|
|
|
|
|
node_on_disk.cell_id = nodes[i].value - base_cell;
|
|
|
|
if (nodes[i].children[0] == 0)
|
2011-03-17 19:39:20 +01:00
|
|
|
node_on_disk.children_node[0] = -1L;
|
2011-02-25 00:04:42 +01:00
|
|
|
else
|
|
|
|
node_on_disk.children_node[0] = nodes[i].children[0] - nodes;
|
2011-03-17 19:39:20 +01:00
|
|
|
assert((node_on_disk.children_node[0] == -1) || ((node_on_disk.children_node[0] >= 0) && (node_on_disk.children_node[0] < lastNode)));
|
|
|
|
|
2011-02-25 00:04:42 +01:00
|
|
|
if (nodes[i].children[1] == 0)
|
2011-03-17 19:39:20 +01:00
|
|
|
node_on_disk.children_node[1] = -1L;
|
2011-02-25 00:04:42 +01:00
|
|
|
else
|
|
|
|
node_on_disk.children_node[1] = nodes[i].children[1] - nodes;
|
2011-03-17 19:39:20 +01:00
|
|
|
assert((node_on_disk.children_node[1] == -1) || ((node_on_disk.children_node[1] >= 0) && (node_on_disk.children_node[1] < lastNode)));
|
|
|
|
|
2011-02-25 00:04:42 +01:00
|
|
|
memcpy(node_on_disk.minBound, nodes[i].minBound, sizeof(coords));
|
|
|
|
memcpy(node_on_disk.maxBound, nodes[i].maxBound, sizeof(coords));
|
|
|
|
|
|
|
|
o.write((char *)&node_on_disk, sizeof(node_on_disk));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-20 15:21:00 +02:00
|
|
|
template<int N, typename ValType, typename CType, typename CellSplitter>
|
|
|
|
KDTree<N,ValType,CType,CellSplitter>::KDTree(std::istream& in, Cell *cells, uint32_t Ncells)
|
2011-02-25 00:04:42 +01:00
|
|
|
throw (InvalidOnDiskKDTree)
|
|
|
|
{
|
|
|
|
KDTreeHeader h;
|
|
|
|
|
|
|
|
if (!in)
|
|
|
|
throw InvalidOnDiskKDTree();
|
|
|
|
|
|
|
|
in.read((char *)&h, sizeof(h));
|
|
|
|
if (!in || strncmp(h.id, KDTREE_DISK_SIGNATURE, KDTREE_DISK_SIGNATURE_LEN) != 0)
|
2011-03-17 19:39:20 +01:00
|
|
|
{
|
|
|
|
std::cerr << "KDTree Signature invalid" << std::endl;
|
|
|
|
throw InvalidOnDiskKDTree();
|
|
|
|
}
|
2011-02-25 00:04:42 +01:00
|
|
|
|
2011-03-17 19:39:20 +01:00
|
|
|
if (h.numCells != Ncells || h.nodesUsed < 0) {
|
|
|
|
std::cerr << "The number of cells has changed (" << h.numCells << " != " << Ncells << ") or nodesUsed=" << h.nodesUsed << std::endl;
|
2011-02-25 00:04:42 +01:00
|
|
|
throw InvalidOnDiskKDTree();
|
2011-03-17 19:39:20 +01:00
|
|
|
}
|
2011-02-25 00:04:42 +01:00
|
|
|
|
|
|
|
base_cell = cells;
|
|
|
|
nodes = new Node[h.nodesUsed];
|
|
|
|
lastNode = h.nodesUsed;
|
|
|
|
numNodes = Ncells;
|
|
|
|
|
|
|
|
for (long i = 0; i < lastNode; i++)
|
|
|
|
{
|
|
|
|
KDTreeOnDisk<N,CType> node_on_disk;
|
|
|
|
|
|
|
|
in.read((char *)&node_on_disk, sizeof(node_on_disk));
|
|
|
|
|
2011-03-17 19:39:20 +01:00
|
|
|
if (!in) {
|
|
|
|
std::cerr << "End-of-file reached" << std::endl;
|
|
|
|
delete[] nodes;
|
|
|
|
throw InvalidOnDiskKDTree();
|
|
|
|
}
|
|
|
|
if (node_on_disk.cell_id > numNodes || node_on_disk.cell_id < 0 ||
|
2011-02-25 00:04:42 +01:00
|
|
|
node_on_disk.children_node[0] > lastNode || node_on_disk.children_node[0] < -1 ||
|
|
|
|
node_on_disk.children_node[1] > lastNode || node_on_disk.children_node[1] < -1)
|
|
|
|
{
|
|
|
|
delete[] nodes;
|
2011-03-17 19:39:20 +01:00
|
|
|
std::cerr << "Invalid cell id or children node id invalid" << std::endl;
|
|
|
|
std::cerr << node_on_disk.cell_id << std::endl << node_on_disk.children_node[0] << std::endl << node_on_disk.children_node[1] << std::endl;
|
2011-02-25 00:04:42 +01:00
|
|
|
throw InvalidOnDiskKDTree();
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes[i].value = base_cell + node_on_disk.cell_id;
|
|
|
|
if (node_on_disk.children_node[0] == -1)
|
|
|
|
nodes[i].children[0] = 0;
|
|
|
|
else
|
|
|
|
nodes[i].children[0] = nodes + node_on_disk.children_node[0];
|
|
|
|
|
|
|
|
if (node_on_disk.children_node[1] == -1)
|
|
|
|
nodes[i].children[1] = 0;
|
|
|
|
else
|
|
|
|
nodes[i].children[1] = nodes + node_on_disk.children_node[1];
|
|
|
|
|
|
|
|
memcpy(nodes[i].minBound, node_on_disk.minBound, sizeof(coords));
|
|
|
|
memcpy(nodes[i].maxBound, node_on_disk.maxBound, sizeof(coords));
|
|
|
|
|
|
|
|
int c;
|
|
|
|
for (c = 0; c < N; c++)
|
|
|
|
if (nodes[i].value->coord[c] < nodes[i].minBound[c] ||
|
|
|
|
nodes[i].value->coord[c] > nodes[i].maxBound[c])
|
|
|
|
break;
|
|
|
|
if (c != N)
|
|
|
|
{
|
|
|
|
delete[] nodes;
|
2011-03-17 19:39:20 +01:00
|
|
|
std::cerr << "Coordinates of the cell inconsistent with the boundaries" << std::endl
|
|
|
|
<< " X=" << nodes[i].value->coord[0] << " B=[" << nodes[i].minBound[0] << "," << nodes[i].maxBound[0] << "]" << std::endl
|
|
|
|
<< " Y=" << nodes[i].value->coord[1] << " B=[" << nodes[i].minBound[1] << "," << nodes[i].maxBound[1] << "]" << std::endl
|
|
|
|
<< " Z=" << nodes[i].value->coord[2] << " B=[" << nodes[i].minBound[2] << "," << nodes[i].maxBound[2] << "]" << std::endl;
|
2011-02-25 00:04:42 +01:00
|
|
|
throw InvalidOnDiskKDTree();
|
|
|
|
}
|
|
|
|
}
|
2011-03-17 19:39:20 +01:00
|
|
|
|
|
|
|
root = &nodes[h.rootId];
|
2011-02-25 00:04:42 +01:00
|
|
|
|
|
|
|
sortingHelper = new Cell *[Ncells];
|
|
|
|
for (uint32_t i = 0; i < Ncells; i++)
|
|
|
|
sortingHelper[i] = &cells[i];
|
|
|
|
}
|
|
|
|
#endif
|
2009-12-07 15:46:39 +01:00
|
|
|
|
2009-01-08 16:18:14 +01:00
|
|
|
};
|