diff --git a/src/mykdtree.hpp b/src/mykdtree.hpp index e87b376..9d83d5b 100644 --- a/src/mykdtree.hpp +++ b/src/mykdtree.hpp @@ -7,20 +7,19 @@ namespace CosmoTool { - template + template struct KDDef { - typedef float CoordType; + typedef CType CoordType; typedef float KDCoordinates[N]; - static const int NumCubes = 1 << (N); }; - template + template struct KDCell { bool active; ValType val; - typename KDDef::KDCoordinates coord; + typename KDDef::KDCoordinates coord; }; class NotEnoughCells: public Exception @@ -30,114 +29,113 @@ namespace CosmoTool { ~NotEnoughCells() throw () {} }; - template + template struct KDTreeNode { - KDCell *value; - KDTreeNode *children[2]; - typename KDDef::KDCoordinates minBound, maxBound; + KDCell *value; + KDTreeNode *children[2]; + typename KDDef::KDCoordinates minBound, maxBound; }; - template + template class RecursionInfoCells { public: - typename KDDef::KDCoordinates x; - typename KDDef::CoordType r, r2; - KDCell **cells; - typename KDDef::CoordType *distances; + typename KDDef::KDCoordinates x; + typename KDDef::CoordType r, r2; + KDCell **cells; + typename KDDef::CoordType *distances; uint32_t currentRank; uint32_t numCells; }; - template + template class RecursionMultipleInfo { public: - const typename KDDef::KDCoordinates& x; - BoundedQueue< KDCell *, typename KDDef::CoordType> queue; + const typename KDDef::KDCoordinates& x; + BoundedQueue< KDCell *, typename KDDef::CoordType> queue; int traversed; - RecursionMultipleInfo(const typename KDDef::KDCoordinates& rx, - KDCell **cells, + RecursionMultipleInfo(const typename KDDef::KDCoordinates& rx, + KDCell **cells, uint32_t numCells) : x(rx), queue(cells, numCells, INFINITY),traversed(0) { } }; - template + template class KDTree { public: - static const int NumCubes = KDDef::NumCubes; - - public: - typedef typename KDDef::CoordType CoordType; + typedef typename KDDef::CoordType CoordType; typedef typename KDDef::KDCoordinates coords; + typedef KDCell Cell; + typedef KDTreeNode Node; - KDTree(KDCell *cells, uint32_t Ncells); + KDTree(Cell *cells, uint32_t Ncells); ~KDTree(); uint32_t getIntersection(const coords& x, CoordType r, - KDCell **cells, + Cell **cells, uint32_t numCells) throw (NotEnoughCells); uint32_t getIntersection(const coords& x, CoordType r, - KDCell **cells, + Cell **cells, CoordType *distances, uint32_t numCells) throw (NotEnoughCells); - KDCell *getNearestNeighbour(const coords& x); + Cell *getNearestNeighbour(const coords& x); void getNearestNeighbours(const coords& x, uint32_t N, - KDCell **cells); + Cell **cells); void getNearestNeighbours(const coords& x, uint32_t N, - KDCell **cells, + Cell **cells, CoordType *distances); - KDTreeNode *getRoot() { return root; } + Node *getRoot() { return root; } void optimize(); - KDTreeNode *getAllNodes() { return nodes; } + Node *getAllNodes() { return nodes; } uint32_t getNumNodes() const { return lastNode; } uint32_t countActives() const; protected: - KDTreeNode *nodes; + Node *nodes; uint32_t numNodes; uint32_t lastNode; - KDTreeNode *root; - KDCell **sortingHelper; + Node *root; + Cell **sortingHelper; - KDTreeNode *buildTree(KDCell **cell0, - uint32_t N, - uint32_t depth, - coords minBound, - coords maxBound); + Node *buildTree(Cell **cell0, + uint32_t N, + uint32_t depth, + coords minBound, + coords maxBound); - void recursiveIntersectionCells(RecursionInfoCells& info, - KDTreeNode *node, + void recursiveIntersectionCells(RecursionInfoCells& info, + Node *node, int level) throw (NotEnoughCells); - CoordType computeDistance(KDCell *cell, const coords& x); - void recursiveNearest(KDTreeNode *node, + CoordType computeDistance(Cell *cell, const coords& x); + void recursiveNearest(Node *node, int level, const coords& x, CoordType& R2, - KDCell*& cell); - void recursiveMultipleNearest(RecursionMultipleInfo& info, KDTreeNode *node, + Cell*& cell); + void recursiveMultipleNearest(RecursionMultipleInfo& info, Node *node, int level); }; - template - uint32_t gatherActiveCells(KDCell **cells, uint32_t numCells); + template + uint32_t gatherActiveCells(KDCell **cells, uint32_t numCells); }; diff --git a/src/mykdtree.tcc b/src/mykdtree.tcc index 66ec29e..183e48e 100644 --- a/src/mykdtree.tcc +++ b/src/mykdtree.tcc @@ -5,7 +5,7 @@ namespace CosmoTool { - template + template class CellCompare { public: @@ -14,7 +14,7 @@ namespace CosmoTool { rank = k; } - bool operator()(const KDCell *a, const KDCell *b) const + bool operator()(const KDCell *a, const KDCell *b) const { return (a->coord[rank] < b->coord[rank]); } @@ -22,27 +22,27 @@ namespace CosmoTool { int rank; }; - template - KDTree::~KDTree() + template + KDTree::~KDTree() { } - template - KDTree::KDTree(KDCell *cells, uint32_t Ncells) + template + KDTree::KDTree(Cell *cells, uint32_t Ncells) { numNodes = Ncells; - nodes = new KDTreeNode[numNodes]; + nodes = new Node[numNodes]; - sortingHelper = new KDCell *[Ncells]; + sortingHelper = new Cell *[Ncells]; for (uint32_t i = 0; i < Ncells; i++) sortingHelper[i] = &cells[i]; optimize(); } - template - void KDTree::optimize() + template + void KDTree::optimize() { coords absoluteMin, absoluteMax; @@ -62,13 +62,13 @@ namespace CosmoTool { std::cout << " done." << std::endl; } - template - uint32_t KDTree::getIntersection(const coords& x, CoordType r, - KDCell **cells, - uint32_t numCells) + template + uint32_t KDTree::getIntersection(const coords& x, CoordType r, + KDTree::Cell **cells, + uint32_t numCells) throw (NotEnoughCells) { - RecursionInfoCells info; + RecursionInfoCells info; memcpy(info.x, x, sizeof(x)); info.r = r; @@ -82,11 +82,11 @@ namespace CosmoTool { return info.currentRank; } - template - uint32_t KDTree::getIntersection(const coords& x, CoordType r, - KDCell **cells, - CoordType *distances, - uint32_t numCells) + template + uint32_t KDTree::getIntersection(const coords& x, CoordType r, + Cell **cells, + CoordType *distances, + uint32_t numCells) throw (NotEnoughCells) { RecursionInfoCells info; @@ -103,10 +103,10 @@ namespace CosmoTool { return info.currentRank; } - template - void KDTree::recursiveIntersectionCells(RecursionInfoCells& info, - KDTreeNode *node, - int level) + template + void KDTree::recursiveIntersectionCells(RecursionInfoCells& info, + Node *node, + int level) throw (NotEnoughCells) { int axis = level % N; @@ -147,9 +147,9 @@ namespace CosmoTool { } } - template - uint32_t gatherActiveCells(KDCell **cells, - uint32_t Ncells) + template + uint32_t gatherActiveCells(KDCell **cells, + uint32_t Ncells) { uint32_t swapId = Ncells-1; uint32_t i = 0; @@ -172,24 +172,24 @@ namespace CosmoTool { return swapId+1; } - template - KDTreeNode *KDTree::buildTree(KDCell **cell0, - uint32_t Ncells, - uint32_t depth, - coords minBound, - coords maxBound) + template + KDTreeNode *KDTree::buildTree(Cell **cell0, + uint32_t Ncells, + uint32_t depth, + coords minBound, + coords maxBound) { if (Ncells == 0) return 0; int axis = depth % N; - KDTreeNode *node = &nodes[lastNode++]; + Node *node = &nodes[lastNode++]; uint32_t mid = Ncells/2; coords tmpBound; // Isolate the environment { - CellCompare compare(axis); + CellCompare compare(axis); std::sort(cell0, cell0+Ncells, compare); } @@ -211,8 +211,8 @@ namespace CosmoTool { return node; } - template - uint32_t KDTree::countActives() const + template + uint32_t KDTree::countActives() const { uint32_t numActive = 0; for (uint32_t i = 0; i < lastNode; i++) @@ -223,9 +223,9 @@ namespace CosmoTool { return numActive; } - template - typename KDDef::CoordType - KDTree::computeDistance(KDCell *cell, const coords& x) + template + typename KDDef::CoordType + KDTree::computeDistance(Cell *cell, const coords& x) { CoordType d2 = 0; @@ -237,18 +237,18 @@ namespace CosmoTool { return d2; } - template + template void - KDTree::recursiveNearest( - KDTreeNode *node, + KDTree::recursiveNearest( + Node *node, int level, const coords& x, CoordType& R2, - KDCell *& best) + Cell *& best) { CoordType d2 = 0; int axis = level % N; - KDTreeNode *other, *go; + Node *other, *go; if (x[axis] < node->value->coord[axis]) { @@ -311,26 +311,26 @@ namespace CosmoTool { } } - template - KDCell * - KDTree::getNearestNeighbour(const coords& x) + template + KDCell * + KDTree::getNearestNeighbour(const coords& x) { CoordType R2 = INFINITY; - KDCell *best = 0; + Cell *best = 0; recursiveNearest(root, 0, x, R2, best); return best; } - template + template void - KDTree::recursiveMultipleNearest(RecursionMultipleInfo& info, KDTreeNode *node, + KDTree::recursiveMultipleNearest(RecursionMultipleInfo& info, Node *node, int level) { CoordType d2 = 0; int axis = level % N; - KDTreeNode *other, *go; + Node *other, *go; if (info.x[axis] < node->value->coord[axis]) { @@ -380,9 +380,9 @@ namespace CosmoTool { } } - template - void KDTree::getNearestNeighbours(const coords& x, uint32_t N2, - KDCell **cells) + template + void KDTree::getNearestNeighbours(const coords& x, uint32_t N2, + Cell **cells) { RecursionMultipleInfo info(x, cells, N2); @@ -391,13 +391,13 @@ namespace CosmoTool { recursiveMultipleNearest(info, root, 0); - std::cout << "Traversed = " << info.traversed << std::endl; + // std::cout << "Traversed = " << info.traversed << std::endl; } - template - void KDTree::getNearestNeighbours(const coords& x, uint32_t N2, - KDCell **cells, - CoordType *distances) + template + void KDTree::getNearestNeighbours(const coords& x, uint32_t N2, + Cell **cells, + CoordType *distances) { RecursionMultipleInfo info(x, cells, N2); @@ -405,9 +405,9 @@ namespace CosmoTool { cells[i] = 0; recursiveMultipleNearest(info, root, 0); - memcpy(distances, info.getPriorities(), sizeof(CoordType)*N2); + memcpy(distances, info.queue.getPriorities(), sizeof(CoordType)*N2); - std::cout << "Traversed = " << info.traversed << std::endl; + // std::cout << "Traversed = " << info.traversed << std::endl; } };