Have a recursion that only count particles

This commit is contained in:
Guilhem Lavaux 2011-06-22 13:57:06 -05:00
parent 97dbc381e0
commit 1ae7233139
3 changed files with 37 additions and 12 deletions

View File

@ -97,6 +97,7 @@ namespace CosmoTool {
CoordType *distances, CoordType *distances,
uint32_t numCells) uint32_t numCells)
throw (NotEnoughCells); throw (NotEnoughCells);
uint32_t countCells(const coords& x, CoordType r);
Cell *getNearestNeighbour(const coords& x); Cell *getNearestNeighbour(const coords& x);
@ -146,6 +147,7 @@ namespace CosmoTool {
coords minBound, coords minBound,
coords maxBound); coords maxBound);
template<bool justCount>
void recursiveIntersectionCells(RecursionInfoCells<N,ValType, CType>& info, void recursiveIntersectionCells(RecursionInfoCells<N,ValType, CType>& info,
Node *node, Node *node,
int level) int level)

View File

@ -92,7 +92,7 @@ namespace CosmoTool {
info.numCells = numCells; info.numCells = numCells;
info.distances = 0; info.distances = 0;
recursiveIntersectionCells(info, root, 0); recursiveIntersectionCells<false>(info, root, 0);
return info.currentRank; return info.currentRank;
} }
@ -113,11 +113,29 @@ namespace CosmoTool {
info.numCells = numCells; info.numCells = numCells;
info.distances = distances; info.distances = distances;
recursiveIntersectionCells(info, root, 0); recursiveIntersectionCells<false>(info, root, 0);
return info.currentRank; return info.currentRank;
} }
template<int N, typename ValType, typename CType> template<int N, typename ValType, typename CType>
uint32_t KDTree<N,ValType,CType>::countCells(const coords& x, CoordType r)
{
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);
return info.currentRank;
}
template<int N, typename ValType, typename CType>
template<bool justCount>
void KDTree<N,ValType,CType>::recursiveIntersectionCells(RecursionInfoCells<N,ValType,CType>& info, void KDTree<N,ValType,CType>::recursiveIntersectionCells(RecursionInfoCells<N,ValType,CType>& info,
Node *node, Node *node,
int level) int level)
@ -134,12 +152,15 @@ namespace CosmoTool {
d2 += delta*delta; d2 += delta*delta;
} }
if (d2 < info.r2) if (d2 < info.r2)
{
if (!justCount)
{ {
if (info.currentRank == info.numCells) if (info.currentRank == info.numCells)
throw NotEnoughCells(); throw NotEnoughCells();
info.cells[info.currentRank] = node->value; info.cells[info.currentRank] = node->value;
if (info.distances) if (info.distances)
info.distances[info.currentRank] = d2; info.distances[info.currentRank] = d2;
}
info.currentRank++; info.currentRank++;
} }
} }
@ -149,14 +170,14 @@ namespace CosmoTool {
((info.x[axis]-info.r) < node->value->coord[axis])) ((info.x[axis]-info.r) < node->value->coord[axis]))
{ {
if (node->children[0] != 0) if (node->children[0] != 0)
recursiveIntersectionCells(info, node->children[0], recursiveIntersectionCells<justCount>(info, node->children[0],
level+1); level+1);
} }
if (((info.x[axis]+info.r) > node->value->coord[axis]) && if (((info.x[axis]+info.r) > node->value->coord[axis]) &&
((info.x[axis]-info.r) < node->maxBound[axis])) ((info.x[axis]-info.r) < node->maxBound[axis]))
{ {
if (node->children[1] != 0) if (node->children[1] != 0)
recursiveIntersectionCells(info, node->children[1], recursiveIntersectionCells<justCount>(info, node->children[1],
level+1); level+1);
} }
} }

View File

@ -236,8 +236,10 @@ namespace CosmoTool {
dimList[i] = edge[i]; dimList[i] = edge[i];
fullSize *= edge[i]; fullSize *= edge[i];
} }
if (fullSize != 0) {
array = new T[fullSize]; array = new T[fullSize];
v->get(array, edge); v->get(array, edge);
}
delete[] edge; delete[] edge;
} }