]>
Commit | Line | Data |
---|---|---|
f2040a8f | 1 | #ifndef ROOT_TKDTree |
2 | #define ROOT_TKDTree | |
3 | ||
4 | #ifndef ROOT_TObject | |
5 | #include "TObject.h" | |
6 | #endif | |
7 | ||
8 | #include "TMath.h" | |
316a7f5a | 9 | |
316a7f5a | 10 | |
f2040a8f | 11 | template <typename Index, typename Value> class TKDTree : public TObject |
12 | { | |
13 | public: | |
14 | enum{ | |
15 | kDimMax = 6 | |
16 | }; | |
17 | ||
18 | TKDTree(); | |
19 | TKDTree(Index npoints, Index ndim, UInt_t bsize, Value **data); | |
20 | ~TKDTree(); | |
21 | ||
22 | // getters | |
117c62ab | 23 | inline Index* GetPointsIndexes(Int_t node) const { |
316a7f5a | 24 | if(node < fNnodes) return 0x0; |
25 | Int_t offset = (node >= fCrossNode) ? (node-fCrossNode)*fBucketSize : fOffset+(node-fNnodes)*fBucketSize; | |
26 | return &fIndPoints[offset]; | |
27 | } | |
117c62ab | 28 | inline UChar_t GetNodeAxis(Int_t id) const {return (id < 0 || id >= fNnodes) ? 0 : fAxis[id];} |
29 | inline Value GetNodeValue(Int_t id) const {return (id < 0 || id >= fNnodes) ? 0 : fValue[id];} | |
30 | inline Int_t GetNNodes() const {return fNnodes;} | |
117c62ab | 31 | inline Value* GetBoundaries(); |
32 | inline Value* GetBoundary(const Int_t node); | |
33 | static Int_t GetIndex(Int_t row, Int_t collumn){return collumn+(1<<row);} | |
34 | static inline void GetCoord(Int_t index, Int_t &row, Int_t &collumn){for (row=0; index>=(16<<row);row+=4); for (; index>=(2<<row);row++);collumn= index-(1<<row);}; | |
35 | Bool_t FindNearestNeighbors(const Value *point, const Int_t kNN, Index *&i, Value *&d); | |
36 | Index FindNode(const Value * point); | |
37 | void FindPoint(Value * point, Index &index, Int_t &iter); | |
38 | void FindInRangeA(Value * point, Value * delta, Index *res , Index &npoints,Index & iter, Int_t bnode); | |
39 | void FindInRangeB(Value * point, Value * delta, Index *res , Index &npoints,Index & iter, Int_t bnode); | |
40 | inline void FindBNodeA(Value * point, Value * delta, Int_t &inode); | |
41 | inline Bool_t IsTerminal(Index inode){return (inode>=fNnodes);} | |
316a7f5a | 42 | Value KOrdStat(Index ntotal, Value *a, Index k, Index *index); |
43 | void MakeBoundaries(Value *range = 0x0); | |
44 | void SetData(Index npoints, Index ndim, UInt_t bsize, Value **data); | |
45 | void Spread(Index ntotal, Value *a, Index *index, Value &min, Value &max); | |
f2040a8f | 46 | |
47 | protected: | |
316a7f5a | 48 | void Build(); // build tree |
f2040a8f | 49 | |
50 | private: | |
316a7f5a | 51 | TKDTree(const TKDTree &); // not implemented |
52 | TKDTree<Index, Value>& operator=(const TKDTree<Index, Value>&); // not implemented | |
117c62ab | 53 | void CookBoundaries(const Int_t node, Bool_t left); |
a9c20b1f | 54 | |
f2040a8f | 55 | |
56 | protected: | |
316a7f5a | 57 | Bool_t kDataOwner; //! Toggle ownership of the data |
f2040a8f | 58 | Int_t fNnodes; // size of node array |
316a7f5a | 59 | Index fNDim; // number of dimensions |
60 | Index fNDimm; // dummy 2*fNDim | |
f2040a8f | 61 | Index fNpoints; // number of multidimensional points |
62 | Index fBucketSize; // limit statistic for nodes | |
117c62ab | 63 | UChar_t *fAxis; //[fNnodes] nodes cutting axis |
64 | Value *fValue; //[fNnodes] nodes cutting value | |
316a7f5a | 65 | Value *fRange; //[fNDimm] range of data for each dimension |
66 | Value **fData; //! data points | |
f2040a8f | 67 | Value *fBoundaries;//! nodes boundaries - check class doc |
f2040a8f | 68 | |
316a7f5a | 69 | // kNN related data |
70 | protected: | |
71 | Int_t fkNNdim; //! current kNN arrays allocated dimension | |
72 | Index *fkNN; //! k nearest neighbors indexes | |
73 | Value *fkNNdist; //! k nearest neighbors distances | |
74 | Value *fDistBuffer;//! working space for kNN | |
75 | Index *fIndBuffer; //! working space for kNN | |
76 | ||
f2040a8f | 77 | private: |
78 | Index *fIndPoints; //! array of points indexes | |
316a7f5a | 79 | Int_t fRowT0; //! smallest terminal row |
80 | Int_t fCrossNode; //! cross node | |
81 | Int_t fOffset; //! offset in fIndPoints | |
f2040a8f | 82 | |
83 | ClassDef(TKDTree, 1) // KD tree | |
84 | }; | |
85 | ||
86 | ||
87 | typedef TKDTree<Int_t, Double_t> TKDTreeID; | |
88 | typedef TKDTree<Int_t, Float_t> TKDTreeIF; | |
89 | ||
90 | //_________________________________________________________________ | |
316a7f5a | 91 | template <typename Index, typename Value> |
92 | void TKDTree<Index, Value>::FindBNodeA(Value *point, Value *delta, Int_t &inode){ | |
f2040a8f | 93 | // |
94 | // find the smallest node covering the full range - start | |
95 | // | |
96 | inode =0; | |
97 | for (;inode<fNnodes;){ | |
117c62ab | 98 | if (TMath::Abs(point[fAxis[inode]] - fValue[inode])<delta[fAxis[inode]]) break; |
99 | inode = (point[fAxis[inode]] < fValue[inode]) ? (inode*2)+1: (inode*2)+2; | |
f2040a8f | 100 | } |
101 | } | |
102 | ||
316a7f5a | 103 | //_________________________________________________________________ |
104 | template <typename Index, typename Value> | |
105 | Value* TKDTree<Index, Value>::GetBoundaries() | |
106 | { | |
107 | if(!fBoundaries) MakeBoundaries(); | |
108 | return fBoundaries; | |
109 | } | |
110 | ||
111 | //_________________________________________________________________ | |
112 | template <typename Index, typename Value> | |
113 | Value* TKDTree<Index, Value>::GetBoundary(const Int_t node) | |
114 | { | |
115 | if(!fBoundaries) MakeBoundaries(); | |
116 | return &fBoundaries[node*2*fNDim]; | |
117 | } | |
118 | ||
f2040a8f | 119 | #endif |
120 |