Skip to content

Commit 65b3913

Browse files
committed
new version of the kdTree from Anna
In particular, FindNearestNeighbors() function has been completely rewritten, and now 1) actually works correctly, 2) uses Euclidean metric, 3) has more user friendly calling sequence, 4) is recursive and more readable (as fast as before). Some new functions had to be added to TKDTree for this, such as Distance(), which finds distance between a random point and a point in the kdtree, and DistanceToNode() which finds the distance between a point and a node of the kdtree, GetNodePointsIndexes() which returns ranges of indexes of original points, contained in a given node. Also, the test macro has been updated with a test function, that compares TKDTree::FindNearestNeighbors() results with simple sorting. fixed also some problem in the TMath test git-svn-id: http://root.cern.ch/svn/root/trunk@26356 27541ba8-7e3a-0410-8455-c3a389f83636
1 parent 4ce0b6a commit 65b3913

File tree

5 files changed

+338
-226
lines changed

5 files changed

+338
-226
lines changed

math/mathcore/inc/TKDTree.h

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,21 @@ template <typename Index, typename Value> class TKDTree : public TObject
1919

2020
void Build(); // build the tree
2121

22+
Double_t Distance(const Value *point, Index ind, Int_t type=2) const;
23+
void DistanceToNode(const Value *point, Index inode, Value &min, Value &max, Int_t type=2);
24+
2225
// Get indexes of left and right daughter nodes
23-
Int_t GetLeft(Int_t inode) const {return inode*2+1;}
24-
Int_t GetRight(Int_t inode) const {return (inode+1)*2;}
25-
Int_t GetParent(Int_t inode) const {return inode/2;}
26+
Int_t GetLeft(Int_t inode) const {return inode*2+1;}
27+
Int_t GetRight(Int_t inode) const {return (inode+1)*2;}
28+
Int_t GetParent(Int_t inode) const {return (inode-1)/2;}
2629
//
2730
// Other getters
2831
Index* GetPointsIndexes(Int_t node) const;
32+
void GetNodePointsIndexes(Int_t node, Int_t &first1, Int_t &last1, Int_t &first2, Int_t &last2) const;
2933
UChar_t GetNodeAxis(Int_t id) const {return (id < 0 || id >= fNNodes) ? 0 : fAxis[id];}
3034
Value GetNodeValue(Int_t id) const {return (id < 0 || id >= fNNodes) ? 0 : fValue[id];}
3135
Int_t GetNNodes() const {return fNNodes;}
36+
Int_t GetTotalNodes() const {return fTotalNodes;}
3237
Value* GetBoundaries();
3338
Value* GetBoundariesExact();
3439
Value* GetBoundary(const Int_t node);
@@ -44,15 +49,17 @@ template <typename Index, typename Value> class TKDTree : public TObject
4449
Index* GetIndPoints() {return fIndPoints;}
4550
Index GetBucketSize() {return fBucketSize;}
4651

47-
Bool_t FindNearestNeighbors(const Value *point, const Int_t kNN, Index *&i, Value *&d);
48-
Index FindNode(const Value * point);
52+
void FindNearestNeighbors(const Value *point, const Int_t k, Index *ind, Value *dist);
53+
Index FindNode(const Value * point) const;
4954
void FindPoint(Value * point, Index &index, Int_t &iter);
5055
void FindInRangeA(Value * point, Value * delta, Index *res , Index &npoints,Index & iter, Int_t bnode);
5156
void FindInRangeB(Value * point, Value * delta, Index *res , Index &npoints,Index & iter, Int_t bnode);
5257
void FindBNodeA(Value * point, Value * delta, Int_t &inode);
5358
Bool_t IsTerminal(Index inode) const {return (inode>=fNNodes);}
5459
Int_t IsOwner() { return fDataOwner; }
5560
Value KOrdStat(Index ntotal, Value *a, Index k, Index *index) const;
61+
62+
5663
void MakeBoundaries(Value *range = 0x0);
5764
void MakeBoundariesExact();
5865
void SetData(Index npoints, Index ndim, UInt_t bsize, Value **data);
@@ -64,11 +71,12 @@ template <typename Index, typename Value> class TKDTree : public TObject
6471
TKDTree(const TKDTree &); // not implemented
6572
TKDTree<Index, Value>& operator=(const TKDTree<Index, Value>&); // not implemented
6673
void CookBoundaries(const Int_t node, Bool_t left);
67-
74+
void UpdateNearestNeighbors(Index inode, const Value *point, Int_t kNN, Index *ind, Value *dist);
6875

6976
protected:
7077
Int_t fDataOwner; //! 0 - not owner, 2 - owner of the pointer array, 1 - owner of the whole 2-d array
7178
Int_t fNNodes; // size of node array
79+
Int_t fTotalNodes; // total number of nodes (fNNodes + terminal nodes)
7280
Index fNDim; // number of dimensions
7381
Index fNDimm; // dummy 2*fNDim
7482
Index fNPoints; // number of multidimensional points
@@ -88,12 +96,6 @@ template <typename Index, typename Value> class TKDTree : public TObject
8896
// fOffset returns the index in the fIndPoints array of the first point
8997
// that belongs to the first node on the second row.
9098

91-
// kNN related data
92-
Int_t fkNNdim; //! current kNN arrays allocated dimension
93-
Index *fkNN; //! k nearest neighbors indexes
94-
Value *fkNNdist; //! k nearest neighbors distances
95-
Value *fDistBuffer;//! working space for kNN
96-
Index *fIndBuffer; //! working space for kNN
9799

98100
ClassDef(TKDTree, 1) // KD tree
99101
};

0 commit comments

Comments
 (0)