Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KDTree.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2007-2008 Anael Orlinski
3 *
4 * This file is part of Panomatic.
5 *
6 * Panomatic is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * Panomatic is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Panomatic; if not, write to the Free Software
18 * <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef __kdtree_h
22 #define __kdtree_h
23 
24 #include <vector>
25 #include <list>
26 #include <functional> // for std::greater
27 
29 
30 // implementation of a kdtree is based on this book
31 // A. Moore, An introductory tutorial on kd-trees,
32 // tech. report Technical Report No. 209, Computer Laboratory,
33 // University of Cambridge, Robotics Institute, Carnegie Mellon University, 1991.
34 
35 namespace KDTreeSpace
36 {
37 
38 template <class KE> class BestMatch;
39 template <class KE, class TYPE> class HyperRectangle;
40 template <class KE, class VTYPE> class QueueEntry;
41 template <class KE, class VTYPE> class KDTree;
42 
43 template <class VTYPE>
45 {
46  virtual VTYPE& getVectorElem (int iPos) const = 0; // access to the vector elements.
47 };
48 
49 /******************************************************************************
50 * BestMatch
51 ******************************************************************************/
52 template <class KE>
53 class BestMatch
54 {
55 public:
56  BestMatch(const KE* iMatch, double iDistance) : _distance(iDistance), _match(iMatch) {}
57  double _distance; // square distance from target
58  const KE* _match;
59 };
60 
61 /******************************************************************************
62 * HyperRectangle
63 ******************************************************************************/
64 template <class KE, class TYPE>
65 class HyperRectangle
66 {
67 public:
69  explicit HyperRectangle(int iDim);
71  bool split(HyperRectangle& oLeft, HyperRectangle& oRight, int iSplitDim, TYPE iSplitVal);
72  double calcSqDistance (const KE& iTarget);
73  bool hasHyperSphereIntersect(const KE& iTarget, double iSqDistance);
74  void display();
75  bool isTargetIn (const KE& iTarget);
76 
77  int _dim;
78  std::vector<TYPE> _leftTop, _rightBottom;
79 };
80 
81 /******************************************************************************
82 * QueueEntry
83 ******************************************************************************/
84 template <class KE, class VTYPE>
85 class QueueEntry
86 {
87 public:
88  QueueEntry(HyperRectangle<KE, VTYPE> & iHR, KDTree<KE, VTYPE> * iKDTree, double iDistance) :
89  _dist(iDistance), _HR(iHR), _kdTree(iKDTree) {}
90  double _dist; // the distance from target to HyperRectangle
91  HyperRectangle<KE, VTYPE> & _HR; // reference to the hyperrectangle
93 
94  // operator < to be put in a regular set.
95  //bool operator < (const QueueEntry<KE> & iOther) { return (_dist < iOther._dist); }
96 
97 };
98 
99 /******************************************************************************
100 * KDTree
101 ******************************************************************************/
102 template <class KE, class VTYPE>
103 class KDTree
104 {
105 public:
106  typedef typename std::vector<KE> ItemVector_t;
107  typedef typename std::vector<KE>::const_iterator ItemVectorIt_t;
108  typedef typename std::vector<const KE*> ItemPtrVector_t;
109  typedef typename std::vector<const KE*>::const_iterator ItemPtrVectorIt_t;
110  typedef typename std::set<BestMatch<KE>, std::greater<BestMatch<KE> > > BestMatchSet_t;
111  typedef lfeat::bounded_set<BestMatch<KE>, std::greater<BestMatch<KE> > > BestMatchLimitedSet_t;
112  typedef typename std::list<QueueEntry<KE, VTYPE> > QueueEntryList_t;
113 
114  // constructor for the root.
115  KDTree(const ItemVector_t& iElemsList, int iDimensions);
116 
117  // recursive construct.
118  KDTree(const ItemPtrVector_t& iElemsPtrList, int iDimensions);
119 
120  // Destructor
121  ~KDTree();
122 
123  BestMatchSet_t getNearestNeighboursBBF( const KE& iTarget,
124  int iNbBestMatches,
125  int iNbSearchSteps);
126 
127  // calc the square distance between 2 entries.
128  double calcSqDist(const KE* i1, const KE* i2);
129 
130 private:
131  void init(const ItemPtrVector_t& iElemsPtrList); // prepare the structure.
132  ItemPtrVectorIt_t choosePivot(const ItemPtrVector_t& iElemsPtrList); // choose the pivot point
133  void recurseNearestNeighboursBBF(const KE& iTarget,
135  BestMatchLimitedSet_t& ioBestMatches,
136  QueueEntryList_t& ioSearchQueue,
137  int& ioRemainingUnqueues);
138  // dimension of the vectors
139  int _dims;
140 
141  // The element stored on this leaf
142  const KE* _pivot;
144 
145  // The left and the right kd-subtree.
148 
149 };
150 
151 } // namespace KDTree
152 
153 #endif
bool split(HyperRectangle &oLeft, HyperRectangle &oRight, int iSplitDim, TYPE iSplitVal)
Definition: KDTreeImpl.h:404
double calcSqDist(const KE *i1, const KE *i2)
Definition: KDTreeImpl.h:227
bool hasHyperSphereIntersect(const KE &iTarget, double iSqDistance)
Definition: KDTreeImpl.h:459
std::vector< TYPE > _leftTop
Definition: KDTree.h:78
QueueEntry(HyperRectangle< KE, VTYPE > &iHR, KDTree< KE, VTYPE > *iKDTree, double iDistance)
Definition: KDTree.h:88
KDTree< KE, VTYPE > * _rightKD
Definition: KDTree.h:147
BestMatchSet_t getNearestNeighboursBBF(const KE &iTarget, int iNbBestMatches, int iNbSearchSteps)
Definition: KDTreeImpl.h:240
double calcSqDistance(const KE &iTarget)
Definition: KDTreeImpl.h:428
BestMatch(const KE *iMatch, double iDistance)
Definition: KDTree.h:56
std::vector< KE > ItemVector_t
Definition: KDTree.h:106
KDTree< KE, VTYPE > * _kdTree
Definition: KDTree.h:92
std::list< QueueEntry< KE, VTYPE > > QueueEntryList_t
Definition: KDTree.h:112
std::vector< const KE * >::const_iterator ItemPtrVectorIt_t
Definition: KDTree.h:109
void init(const ItemPtrVector_t &iElemsPtrList)
Definition: KDTreeImpl.h:98
bool isTargetIn(const KE &iTarget)
Definition: KDTreeImpl.h:487
void recurseNearestNeighboursBBF(const KE &iTarget, HyperRectangle< KE, VTYPE > &iHR, BestMatchLimitedSet_t &ioBestMatches, QueueEntryList_t &ioSearchQueue, int &ioRemainingUnqueues)
Definition: KDTreeImpl.h:258
HyperRectangle< KE, VTYPE > & _HR
Definition: KDTree.h:91
const KE * _match
Definition: KDTree.h:58
lfeat::bounded_set< BestMatch< KE >, std::greater< BestMatch< KE > > > BestMatchLimitedSet_t
Definition: KDTree.h:111
virtual VTYPE & getVectorElem(int iPos) const =0
std::vector< const KE * > ItemPtrVector_t
Definition: KDTree.h:108
ItemPtrVectorIt_t choosePivot(const ItemPtrVector_t &iElemsPtrList)
Definition: KDTreeImpl.h:163
std::vector< TYPE > _rightBottom
Definition: KDTree.h:78
std::vector< KE >::const_iterator ItemVectorIt_t
Definition: KDTree.h:107
KDTree(const ItemVector_t &iElemsList, int iDimensions)
Definition: KDTreeImpl.h:38
std::set< BestMatch< KE >, std::greater< BestMatch< KE > > > BestMatchSet_t
Definition: KDTree.h:110
const KE * _pivot
Definition: KDTree.h:142
KDTree< KE, VTYPE > * _leftKD
Definition: KDTree.h:146