From 2e097cb8cd6183c065cb6a48119fd1f28944e370 Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Mon, 7 Dec 2009 15:46:39 +0100 Subject: [PATCH] Fixes. Support for precomputing the number of elements in a subtree --- src/mykdtree.hpp | 17 ++++++++++++++++- src/mykdtree.tcc | 26 +++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/mykdtree.hpp b/src/mykdtree.hpp index 10c96b4..3b27ed3 100644 --- a/src/mykdtree.hpp +++ b/src/mykdtree.hpp @@ -35,6 +35,9 @@ namespace CosmoTool { KDCell *value; KDTreeNode *children[2]; typename KDDef::KDCoordinates minBound, maxBound; +#ifdef __KD_TREE_NUMNODES + uint32_t numNodes; +#endif }; template @@ -105,6 +108,17 @@ namespace CosmoTool { uint32_t countActives() const; +#ifdef __KD_TREE_NUMNODES + uint32_t getNumberInNode(const Node *n) const { return n->numNodes; } +#else + uint32_t getNumberInNode(const Node *n) const { + if (n == 0) + return 0; + return 1+getNumberInNode(n->children[0])+getNumberInNode(n->children[1]); + } +#endif + + protected: Node *nodes; uint32_t numNodes; @@ -124,7 +138,7 @@ namespace CosmoTool { int level) throw (NotEnoughCells); - CoordType computeDistance(Cell *cell, const coords& x); + CoordType computeDistance(const Cell *cell, const coords& x) const; void recursiveNearest(Node *node, int level, const coords& x, @@ -132,6 +146,7 @@ namespace CosmoTool { Cell*& cell); void recursiveMultipleNearest(RecursionMultipleInfo& info, Node *node, int level); + }; template diff --git a/src/mykdtree.tcc b/src/mykdtree.tcc index 183e48e..7faae26 100644 --- a/src/mykdtree.tcc +++ b/src/mykdtree.tcc @@ -2,6 +2,7 @@ #include #include #include +#include namespace CosmoTool { @@ -53,8 +54,20 @@ namespace CosmoTool { lastNode = 0; for (int i = 0; i < N; i++) { - absoluteMin[i] = -std::numeric_limits::max(); - absoluteMax[i] = std::numeric_limits::max(); + absoluteMin[i] = std::numeric_limits::max(); + absoluteMax[i] = -std::numeric_limits::max(); + } + // Find min and max corner + for (uint32_t i = 0; i < activeCells; i++) + { + KDCell *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]; + } } std::cout << " rebuilding the tree..." << std::endl; @@ -208,6 +221,12 @@ namespace CosmoTool { node->children[1] = buildTree(cell0+mid+1, Ncells-mid-1, depth, tmpBound, maxBound); +#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 + return node; } @@ -225,7 +244,7 @@ namespace CosmoTool { template typename KDDef::CoordType - KDTree::computeDistance(Cell *cell, const coords& x) + KDTree::computeDistance(const Cell *cell, const coords& x) const { CoordType d2 = 0; @@ -410,4 +429,5 @@ namespace CosmoTool { // std::cout << "Traversed = " << info.traversed << std::endl; } + };