Fixes. Support for precomputing the number of elements in a subtree

This commit is contained in:
Guilhem Lavaux 2009-12-07 15:46:39 +01:00
parent 1290fdb54c
commit 2e097cb8cd
2 changed files with 39 additions and 4 deletions

View File

@ -35,6 +35,9 @@ namespace CosmoTool {
KDCell<N,ValType,CType> *value; KDCell<N,ValType,CType> *value;
KDTreeNode<N,ValType,CType> *children[2]; KDTreeNode<N,ValType,CType> *children[2];
typename KDDef<N,CType>::KDCoordinates minBound, maxBound; typename KDDef<N,CType>::KDCoordinates minBound, maxBound;
#ifdef __KD_TREE_NUMNODES
uint32_t numNodes;
#endif
}; };
template<int N, typename ValType, typename CType = ComputePrecision> template<int N, typename ValType, typename CType = ComputePrecision>
@ -105,6 +108,17 @@ namespace CosmoTool {
uint32_t countActives() const; 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: protected:
Node *nodes; Node *nodes;
uint32_t numNodes; uint32_t numNodes;
@ -124,7 +138,7 @@ namespace CosmoTool {
int level) int level)
throw (NotEnoughCells); throw (NotEnoughCells);
CoordType computeDistance(Cell *cell, const coords& x); CoordType computeDistance(const Cell *cell, const coords& x) const;
void recursiveNearest(Node *node, void recursiveNearest(Node *node,
int level, int level,
const coords& x, const coords& x,
@ -132,6 +146,7 @@ namespace CosmoTool {
Cell*& cell); Cell*& cell);
void recursiveMultipleNearest(RecursionMultipleInfo<N,ValType,CType>& info, Node *node, void recursiveMultipleNearest(RecursionMultipleInfo<N,ValType,CType>& info, Node *node,
int level); int level);
}; };
template<int N, typename T, typename CType> template<int N, typename T, typename CType>

View File

@ -2,6 +2,7 @@
#include <algorithm> #include <algorithm>
#include <limits> #include <limits>
#include <iostream> #include <iostream>
#include <cassert>
namespace CosmoTool { namespace CosmoTool {
@ -53,8 +54,20 @@ namespace CosmoTool {
lastNode = 0; lastNode = 0;
for (int i = 0; i < N; i++) for (int i = 0; i < N; i++)
{ {
absoluteMin[i] = -std::numeric_limits<typeof (absoluteMin[0])>::max(); absoluteMin[i] = std::numeric_limits<typeof (absoluteMin[0])>::max();
absoluteMax[i] = std::numeric_limits<typeof (absoluteMax[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];
}
} }
std::cout << " rebuilding the tree..." << std::endl; std::cout << " rebuilding the tree..." << std::endl;
@ -208,6 +221,12 @@ namespace CosmoTool {
node->children[1] = buildTree(cell0+mid+1, Ncells-mid-1, depth, node->children[1] = buildTree(cell0+mid+1, Ncells-mid-1, depth,
tmpBound, maxBound); 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; return node;
} }
@ -225,7 +244,7 @@ namespace CosmoTool {
template<int N, typename ValType, typename CType> template<int N, typename ValType, typename CType>
typename KDDef<N,CType>::CoordType typename KDDef<N,CType>::CoordType
KDTree<N,ValType,CType>::computeDistance(Cell *cell, const coords& x) KDTree<N,ValType,CType>::computeDistance(const Cell *cell, const coords& x) const
{ {
CoordType d2 = 0; CoordType d2 = 0;
@ -410,4 +429,5 @@ namespace CosmoTool {
// std::cout << "Traversed = " << info.traversed << std::endl; // std::cout << "Traversed = " << info.traversed << std::endl;
} }
}; };