TKDTree.h

Go to the documentation of this file.
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();  // build the tree
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    // Get indexes of left and right daughter nodes
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    // Other getters
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    //Getters for internal variables.
00046    Int_t   GetRowT0() {return fRowT0;}      //! smallest terminal row
00047    Int_t   GetCrossNode() {return fCrossNode;}  //! cross node
00048    Int_t   GetOffset() {return fOffset;}     //! offset in fIndPoints
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 &); // not implemented
00072    TKDTree<Index, Value>& operator=(const TKDTree<Index, Value>&); // not implemented
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;  //! 0 - not owner, 2 - owner of the pointer array, 1 - owner of the whole 2-d array
00080    Int_t   fNNodes;     // size of node array
00081    Int_t   fTotalNodes; // total number of nodes (fNNodes + terminal nodes)
00082    Index   fNDim;       // number of dimensions
00083    Index   fNDimm;      // dummy 2*fNDim
00084    Index   fNPoints;    // number of multidimensional points
00085    Index   fBucketSize; // size of the terminal nodes
00086    UChar_t *fAxis;      //[fNNodes] nodes cutting axis
00087    Value   *fValue;     //[fNNodes] nodes cutting value
00088    //
00089    Value   *fRange;     //[fNDimm] range of data for each dimension
00090    Value   **fData;     //! data points
00091    Value   *fBoundaries;//! nodes boundaries
00092 
00093 
00094    Index   *fIndPoints; //! array of points indexes
00095    Int_t   fRowT0;      //! smallest terminal row - first row that contains terminal nodes
00096    Int_t   fCrossNode;  //! cross node - node that begins the last row (with terminal nodes only)
00097    Int_t   fOffset;     //! offset in fIndPoints - if there are 2 rows, that contain terminal nodes
00098                         //  fOffset returns the index in the fIndPoints array of the first point
00099                         //  that belongs to the first node on the second row.
00100 
00101 
00102    ClassDef(TKDTree, 1)  // KD tree
00103 };
00104       
00105       
00106 typedef TKDTree<Int_t, Double_t> TKDTreeID;
00107 typedef TKDTree<Int_t, Float_t> TKDTreeIF;
00108 
00109 // Test functions:
00110 TKDTreeIF *  TKDTreeTestBuild();
00111 
00112 #endif
00113 

Generated on Tue Jul 5 14:26:58 2011 for ROOT_528-00b_version by  doxygen 1.5.1