Hugin trunk 0.1
Loading...
Searching...
No Matches
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
35namespace KDTreeSpace
36{
37
38template <class KE> class BestMatch;
39template <class KE, class TYPE> class HyperRectangle;
40template <class KE, class VTYPE> class QueueEntry;
41template <class KE, class VTYPE> class KDTree;
42
43template <class VTYPE>
45{
46 virtual VTYPE& getVectorElem (int iPos) const = 0; // access to the vector elements.
47};
48
49/******************************************************************************
50* BestMatch
51******************************************************************************/
52template <class KE>
54{
55public:
57 double _distance; // square distance from target
58 const KE* _match;
59};
60
61/******************************************************************************
62* HyperRectangle
63******************************************************************************/
64template <class KE, class TYPE>
66{
67public:
69 explicit HyperRectangle(int iDim);
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******************************************************************************/
84template <class KE, class VTYPE>
86{
87public:
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******************************************************************************/
102template <class KE, class VTYPE>
104{
105public:
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.
116
117 // recursive construct.
119
120 // Destructor
121 ~KDTree();
122
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
130private:
131 void init(const ItemPtrVector_t& iElemsPtrList); // prepare the structure.
132 ItemPtrVectorIt_t choosePivot(const ItemPtrVector_t& iElemsPtrList); // choose the pivot point
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
BestMatch(const KE *iMatch, double iDistance)
Definition KDTree.h:56
const KE * _match
Definition KDTree.h:58
bool split(HyperRectangle &oLeft, HyperRectangle &oRight, int iSplitDim, TYPE iSplitVal)
Definition KDTreeImpl.h:404
std::vector< TYPE > _leftTop
Definition KDTree.h:78
bool isTargetIn(const KE &iTarget)
Definition KDTreeImpl.h:487
std::vector< TYPE > _rightBottom
Definition KDTree.h:78
double calcSqDistance(const KE &iTarget)
Definition KDTreeImpl.h:428
bool hasHyperSphereIntersect(const KE &iTarget, double iSqDistance)
Definition KDTreeImpl.h:459
virtual VTYPE & getVectorElem(int iPos) const =0
std::list< QueueEntry< KE, VTYPE > > QueueEntryList_t
Definition KDTree.h:112
lfeat::bounded_set< BestMatch< KE >, std::greater< BestMatch< KE > > > BestMatchLimitedSet_t
Definition KDTree.h:111
double calcSqDist(const KE *i1, const KE *i2)
Definition KDTreeImpl.h:227
void recurseNearestNeighboursBBF(const KE &iTarget, HyperRectangle< KE, VTYPE > &iHR, BestMatchLimitedSet_t &ioBestMatches, QueueEntryList_t &ioSearchQueue, int &ioRemainingUnqueues)
Definition KDTreeImpl.h:258
std::vector< KE >::const_iterator ItemVectorIt_t
Definition KDTree.h:107
ItemPtrVectorIt_t choosePivot(const ItemPtrVector_t &iElemsPtrList)
Definition KDTreeImpl.h:163
std::vector< constKE * >::const_iterator ItemPtrVectorIt_t
Definition KDTree.h:109
KDTree< KE, VTYPE > * _leftKD
Definition KDTree.h:146
const KE * _pivot
Definition KDTree.h:142
KDTree< KE, VTYPE > * _rightKD
Definition KDTree.h:147
std::vector< KE > ItemVector_t
Definition KDTree.h:106
std::vector< const KE * > ItemPtrVector_t
Definition KDTree.h:108
std::set< BestMatch< KE >, std::greater< BestMatch< KE > > > BestMatchSet_t
Definition KDTree.h:110
void init(const ItemPtrVector_t &iElemsPtrList)
Definition KDTreeImpl.h:98
BestMatchSet_t getNearestNeighboursBBF(const KE &iTarget, int iNbBestMatches, int iNbSearchSteps)
Definition KDTreeImpl.h:240
KDTree< KE, VTYPE > * _kdTree
Definition KDTree.h:92
HyperRectangle< KE, VTYPE > & _HR
Definition KDTree.h:91
QueueEntry(HyperRectangle< KE, VTYPE > &iHR, KDTree< KE, VTYPE > *iKDTree, double iDistance)
Definition KDTree.h:88
std::vector< deghosting::BImagePtr > threshold(const std::vector< deghosting::FImagePtr > &inputImages, const double threshold, const uint16_t flags)
Threshold function used for creating alpha masks for images.
Definition threshold.h:41