cosmotool/src/mykdtree.hpp

147 lines
3.5 KiB
C++
Raw Normal View History

2009-01-08 16:18:14 +01:00
#ifndef __HV_KDTREE_HPP
#define __HV_KDTREE_HPP
#include <cmath>
#include "config.hpp"
#include "bqueue.hpp"
2009-01-08 16:18:14 +01:00
namespace CosmoTool {
template<int N>
struct KDDef
2009-01-08 16:18:14 +01:00
{
typedef float CoordType;
typedef float KDCoordinates[N];
2009-01-08 16:18:14 +01:00
static const int NumCubes = 1 << (N);
};
template<int N, typename ValType>
struct KDCell
2009-01-08 16:18:14 +01:00
{
bool active;
ValType val;
typename KDDef<N>::KDCoordinates coord;
2009-01-08 16:18:14 +01:00
};
class NotEnoughCells: public Exception
{
public:
NotEnoughCells() : Exception() {}
~NotEnoughCells() throw () {}
};
template<int N, typename ValType>
struct KDTreeNode
2009-01-08 16:18:14 +01:00
{
KDCell<N,ValType> *value;
KDTreeNode<N,ValType> *children[2];
typename KDDef<N>::KDCoordinates minBound, maxBound;
2009-01-08 16:18:14 +01:00
};
template<int N, typename ValType>
class RecursionInfoCells
{
public:
typename KDDef<N>::KDCoordinates x;
typename KDDef<N>::CoordType r, r2;
KDCell<N, ValType> **cells;
typename KDDef<N>::CoordType *distances;
2009-01-08 16:18:14 +01:00
uint32_t currentRank;
uint32_t numCells;
};
template<int N, typename ValType>
class RecursionMultipleInfo
{
public:
const typename KDDef<N>::KDCoordinates& x;
BoundedQueue< KDCell<N,ValType> *, typename KDDef<N>::CoordType> queue;
int traversed;
RecursionMultipleInfo(const typename KDDef<N>::KDCoordinates& rx,
KDCell<N,ValType> **cells,
uint32_t numCells)
: x(rx), queue(cells, numCells, INFINITY),traversed(0)
{
}
};
2009-01-08 16:18:14 +01:00
template<int N, typename ValType>
class KDTree
2009-01-08 16:18:14 +01:00
{
public:
static const int NumCubes = KDDef<N>::NumCubes;
2009-01-08 16:18:14 +01:00
public:
typedef typename KDDef<N>::CoordType CoordType;
typedef typename KDDef<N>::KDCoordinates coords;
2009-01-08 16:18:14 +01:00
KDTree(KDCell<N,ValType> *cells, uint32_t Ncells);
~KDTree();
2009-01-08 16:18:14 +01:00
uint32_t getIntersection(const coords& x, CoordType r,
KDCell<N, ValType> **cells,
uint32_t numCells)
throw (NotEnoughCells);
uint32_t getIntersection(const coords& x, CoordType r,
KDCell<N, ValType> **cells,
CoordType *distances,
2009-01-08 16:18:14 +01:00
uint32_t numCells)
throw (NotEnoughCells);
KDCell<N, ValType> *getNearestNeighbour(const coords& x);
void getNearestNeighbours(const coords& x, uint32_t N,
KDCell<N, ValType> **cells);
2009-01-08 16:18:14 +01:00
void getNearestNeighbours(const coords& x, uint32_t N,
KDCell<N, ValType> **cells,
CoordType *distances);
2009-01-08 16:18:14 +01:00
KDTreeNode<N,ValType> *getRoot() { return root; }
2009-01-08 16:18:14 +01:00
void optimize();
KDTreeNode<N,ValType> *getAllNodes() { return nodes; }
2009-01-08 16:18:14 +01:00
uint32_t getNumNodes() const { return lastNode; }
uint32_t countActives() const;
protected:
KDTreeNode<N, ValType> *nodes;
2009-01-08 16:18:14 +01:00
uint32_t numNodes;
uint32_t lastNode;
KDTreeNode<N, ValType> *root;
KDCell<N, ValType> **sortingHelper;
2009-01-08 16:18:14 +01:00
KDTreeNode<N, ValType> *buildTree(KDCell<N,ValType> **cell0,
2009-01-08 16:18:14 +01:00
uint32_t N,
uint32_t depth,
coords minBound,
coords maxBound);
void recursiveIntersectionCells(RecursionInfoCells<N,ValType>& info,
KDTreeNode<N,ValType> *node,
2009-01-08 16:18:14 +01:00
int level)
throw (NotEnoughCells);
CoordType computeDistance(KDCell<N,ValType> *cell, const coords& x);
void recursiveNearest(KDTreeNode<N, ValType> *node,
2009-01-08 16:18:14 +01:00
int level,
const coords& x,
CoordType& R2,
KDCell<N,ValType>*& cell);
void recursiveMultipleNearest(RecursionMultipleInfo<N,ValType>& info, KDTreeNode<N,ValType> *node,
int level);
2009-01-08 16:18:14 +01:00
};
template<int N, class T>
uint32_t gatherActiveCells(KDCell<N,T> **cells, uint32_t numCells);
2009-01-08 16:18:14 +01:00
};
#include "mykdtree.tcc"
#endif