00001 #ifndef ROOT_TKDTree
00002 #define ROOT_TKDTree
00003
00004 #ifndef ROOT_TObject
00005 #include "TObject.h"
00006 #endif
00007
00008 #include "TMath.h"
00009 #include <vector>
00010
00011 template <typename Index, typename Value> class TKDTree : public TObject
00012 {
00013 public:
00014
00015 TKDTree();
00016 TKDTree(Index npoints, Index ndim, UInt_t bsize);
00017 TKDTree(Index npoints, Index ndim, UInt_t bsize, Value **data);
00018 ~TKDTree();
00019
00020 void Build();
00021
00022 Double_t Distance(const Value *point, Index ind, Int_t type=2) const;
00023 void DistanceToNode(const Value *point, Index inode, Value &min, Value &max, Int_t type=2);
00024
00025
00026 Int_t GetLeft(Int_t inode) const {return inode*2+1;}
00027 Int_t GetRight(Int_t inode) const {return (inode+1)*2;}
00028 Int_t GetParent(Int_t inode) const {return (inode-1)/2;}
00029
00030
00031 Index* GetPointsIndexes(Int_t node) const;
00032 void GetNodePointsIndexes(Int_t node, Int_t &first1, Int_t &last1, Int_t &first2, Int_t &last2) const;
00033 UChar_t GetNodeAxis(Int_t id) const {return (id < 0 || id >= fNNodes) ? 0 : fAxis[id];}
00034 Value GetNodeValue(Int_t id) const {return (id < 0 || id >= fNNodes) ? 0 : fValue[id];}
00035 Int_t GetNNodes() const {return fNNodes;}
00036 Int_t GetTotalNodes() const {return fTotalNodes;}
00037 Value* GetBoundaries();
00038 Value* GetBoundariesExact();
00039 Value* GetBoundary(const Int_t node);
00040 Value* GetBoundaryExact(const Int_t node);
00041 Index GetNPoints() { return fNPoints; }
00042 Index GetNDim() { return fNDim; }
00043 Index GetNPointsNode(Int_t node) const;
00044
00045
00046 Int_t GetRowT0() {return fRowT0;}
00047 Int_t GetCrossNode() {return fCrossNode;}
00048 Int_t GetOffset() {return fOffset;}
00049 Index* GetIndPoints() {return fIndPoints;}
00050 Index GetBucketSize() {return fBucketSize;}
00051
00052 void FindNearestNeighbors(const Value *point, Int_t k, Index *ind, Value *dist);
00053 Index FindNode(const Value * point) const;
00054 void FindPoint(Value * point, Index &index, Int_t &iter);
00055 void FindInRange(Value *point, Value range, std::vector<Index> &res);
00056 void FindBNodeA(Value * point, Value * delta, Int_t &inode);
00057
00058 Bool_t IsTerminal(Index inode) const {return (inode>=fNNodes);}
00059 Int_t IsOwner() { return fDataOwner; }
00060 Value KOrdStat(Index ntotal, Value *a, Index k, Index *index) const;
00061
00062
00063 void MakeBoundaries(Value *range = 0x0);
00064 void MakeBoundariesExact();
00065 void SetData(Index npoints, Index ndim, UInt_t bsize, Value **data);
00066 Int_t SetData(Index idim, Value *data);
00067 void SetOwner(Int_t owner) { fDataOwner = owner; }
00068 void Spread(Index ntotal, Value *a, Index *index, Value &min, Value &max) const;
00069
00070 private:
00071 TKDTree(const TKDTree &);
00072 TKDTree<Index, Value>& operator=(const TKDTree<Index, Value>&);
00073 void CookBoundaries(const Int_t node, Bool_t left);
00074
00075 void UpdateNearestNeighbors(Index inode, const Value *point, Int_t kNN, Index *ind, Value *dist);
00076 void UpdateRange(Index inode, Value *point, Value range, std::vector<Index> &res);
00077
00078 protected:
00079 Int_t fDataOwner;
00080 Int_t fNNodes;
00081 Int_t fTotalNodes;
00082 Index fNDim;
00083 Index fNDimm;
00084 Index fNPoints;
00085 Index fBucketSize;
00086 UChar_t *fAxis;
00087 Value *fValue;
00088
00089 Value *fRange;
00090 Value **fData;
00091 Value *fBoundaries;
00092
00093
00094 Index *fIndPoints;
00095 Int_t fRowT0;
00096 Int_t fCrossNode;
00097 Int_t fOffset;
00098
00099
00100
00101
00102 ClassDef(TKDTree, 1)
00103 };
00104
00105
00106 typedef TKDTree<Int_t, Double_t> TKDTreeID;
00107 typedef TKDTree<Int_t, Float_t> TKDTreeIF;
00108
00109
00110 TKDTreeIF * TKDTreeTestBuild();
00111
00112 #endif
00113