Main MRPT website > C++ reference for MRPT 1.5.7
descriptor_kdtrees.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 #ifndef mrpt_vision_descriptor_kdtrees_H
11 #define mrpt_vision_descriptor_kdtrees_H
12 
13 #include <mrpt/vision/types.h>
14 #include <mrpt/vision/CFeature.h>
15 
16 namespace mrpt
17 {
18  namespace vision
19  {
20  namespace detail {
21  // Private auxiliary classes
22  template <typename distance_t,typename element_t = uint8_t> struct TSIFTDesc2KDTree_Adaptor;
23  template <typename distance_t,typename element_t = float> struct TSURFDesc2KDTree_Adaptor;
24  }
25 
26  /** \defgroup mrptvision_descr_kdtrees KD-Tree construction of visual descriptors
27  * \ingroup mrpt_vision_grp
28  */
29 
30  /** \addtogroup mrptvision_descr_kdtrees
31  @{ */
32 
33  /** A kd-tree builder for sets of features with SIFT descriptors.
34  * Example of usage:
35  * \code
36  * TSIFTDescriptorsKDTreeIndex<double> feats_kdtree(feats);
37  * feats_kdtree.get_kdtree().knnSearch( ... );
38  * \endcode
39  * \sa CFeatureList, mrpt::vision::find_descriptor_pairings
40  */
41  template <
42  typename distance_t,
43  class metric_t = nanoflann::L2_Simple_Adaptor<uint8_t/*SIFT desc elements*/,detail::TSIFTDesc2KDTree_Adaptor<distance_t>, distance_t>
44  >
46  {
47  public:
48  typedef typename nanoflann::KDTreeSingleIndexAdaptor<metric_t,detail::TSIFTDesc2KDTree_Adaptor<distance_t> > kdtree_t;
49 
50  /** Constructor from a list of SIFT features.
51  * Automatically build the KD-tree index. The list of features must NOT be empty or an exception will be raised.
52  */
54  m_adaptor(feats),
55  m_kdtree(NULL),
56  m_feats(feats)
57  {
58  ASSERT_(!feats.empty() && feats[0]->descriptors.hasDescriptorSIFT())
59  this->regenerate_kdtreee();
60  }
61 
62  /** Re-creates the kd-tree, which must be done whenever the data source (the CFeatureList) changes. */
64  {
65  if (m_kdtree) delete m_kdtree;
66 
67  nanoflann::KDTreeSingleIndexAdaptorParams params;
68  m_kdtree = new kdtree_t( m_feats[0]->descriptors.SIFT.size() /* DIM */ , m_adaptor, params );
69  m_kdtree->buildIndex();
70  }
71 
72  /** Access to the kd-tree object */
73  kdtree_t & get_kdtree() { return *m_kdtree; }
74  const kdtree_t & get_kdtree() const { return *m_kdtree; }
75 
77  {
78  delete m_kdtree;
79  m_kdtree=NULL;
80  }
81 
82  private:
85 
87  }; // end of TSIFTDescriptorsKDTreeIndex
88 
89 
90  /** A kd-tree builder for sets of features with SURF descriptors.
91  * Example of usage:
92  * \code
93  * TSURFDescriptorsKDTreeIndex<double> feats_kdtree(feats);
94  * feats_kdtree.get_kdtree().knnSearch( ... );
95  * \endcode
96  * \sa CFeatureList, mrpt::vision::find_descriptor_pairings
97  */
98  template <
99  typename distance_t,
100  class metric_t = nanoflann::L2_Simple_Adaptor<float/*SURF desc elements*/,detail::TSURFDesc2KDTree_Adaptor<distance_t>, distance_t>
101  >
103  {
104  public:
105  typedef typename nanoflann::KDTreeSingleIndexAdaptor<metric_t,detail::TSURFDesc2KDTree_Adaptor<distance_t> > kdtree_t;
106 
107  /** Constructor from a list of SIFT features.
108  * Automatically build the KD-tree index. The list of features must NOT be empty or an exception will be raised.
109  */
111  m_adaptor(feats),
112  m_kdtree(NULL),
113  m_feats(feats)
114  {
115  ASSERT_(!feats.empty() && feats[0]->descriptors.hasDescriptorSIFT())
116  this->regenerate_kdtreee();
117  }
118 
119  /** Re-creates the kd-tree, which must be done whenever the data source (the CFeatureList) changes. */
121  {
122  if (m_kdtree) delete m_kdtree;
123 
124  nanoflann::KDTreeSingleIndexAdaptorParams params;
125  m_kdtree = new kdtree_t( m_feats[0]->descriptors.SIFT.size() /* DIM */ , m_adaptor, params );
126  m_kdtree->buildIndex();
127  }
128 
129  /** Access to the kd-tree object */
130  kdtree_t & get_kdtree() { return *m_kdtree; }
131  const kdtree_t & get_kdtree() const { return *m_kdtree; }
132 
134  {
135  delete m_kdtree;
136  m_kdtree=NULL;
137  }
138 
139  private:
142 
144  }; // end of TSURFDescriptorsKDTreeIndex
145 
146  /** @} */
147 
148  namespace detail
149  {
150  template <typename distance_t,typename element_t>
151  struct TSIFTDesc2KDTree_Adaptor
152  {
154  TSIFTDesc2KDTree_Adaptor(const CFeatureList &feats) : m_feats(feats) { }
155  // Must return the number of data points
156  inline size_t kdtree_get_point_count() const { return m_feats.size(); }
157  // Must return the Euclidean (L2) distance between the vector "p1[0:size-1]" and the data point with index "idx_p2" stored in the class:
158  inline distance_t kdtree_distance(const element_t *p1, const size_t idx_p2,size_t size) const
159  {
160  const size_t dim=m_feats[idx_p2]->descriptors.SIFT.size();
161  const element_t *p2 = &m_feats[idx_p2]->descriptors.SIFT[0];
162  distance_t d=0;
163  for (size_t i=0;i<dim;i++)
164  {
165  d+=(*p1-*p2)*(*p1-*p2);
166  p1++;
167  p2++;
168  }
169  return d;
170  }
171  // Must return the dim'th component of the idx'th point in the class:
172  inline element_t kdtree_get_pt(const size_t idx, int dim) const { return m_feats[idx]->descriptors.SIFT[dim]; }
173  template <class BBOX> bool kdtree_get_bbox(BBOX &bb) const { return false; }
174  };
175 
176  template <typename distance_t,typename element_t>
177  struct TSURFDesc2KDTree_Adaptor
178  {
180  TSURFDesc2KDTree_Adaptor(const CFeatureList &feats) : m_feats(feats) { }
181  // Must return the number of data points
182  inline size_t kdtree_get_point_count() const { return m_feats.size(); }
183  // Must return the Euclidean (L2) distance between the vector "p1[0:size-1]" and the data point with index "idx_p2" stored in the class:
184  inline distance_t kdtree_distance(const element_t *p1, const size_t idx_p2,size_t size) const
185  {
186  const size_t dim=m_feats[idx_p2]->descriptors.SURF.size();
187  const element_t *p2 = &m_feats[idx_p2]->descriptors.SURF[0];
188  distance_t d=0;
189  for (size_t i=0;i<dim;i++)
190  {
191  d+=(*p1-*p2)*(*p1-*p2);
192  p1++;
193  p2++;
194  }
195  return d;
196  }
197  // Must return the dim'th component of the idx'th point in the class:
198  inline element_t kdtree_get_pt(const size_t idx, int dim) const { return m_feats[idx]->descriptors.SURF[dim]; }
199  template <class BBOX> bool kdtree_get_bbox(BBOX &bb) const { return false; }
200  };
201  } // end detail
202  }
203 }
204 #endif
205 
A kd-tree builder for sets of features with SURF descriptors.
A kd-tree builder for sets of features with SIFT descriptors.
TSURFDescriptorsKDTreeIndex(const CFeatureList &feats)
Constructor from a list of SIFT features.
kdtree_t & get_kdtree()
Access to the kd-tree object.
size_t size() const
Definition: CFeature.h:280
void regenerate_kdtreee()
Re-creates the kd-tree, which must be done whenever the data source (the CFeatureList) changes...
detail::TSIFTDesc2KDTree_Adaptor< distance_t > m_adaptor
element_t kdtree_get_pt(const size_t idx, int dim) const
kdtree_t & get_kdtree()
Access to the kd-tree object.
void regenerate_kdtreee()
Re-creates the kd-tree, which must be done whenever the data source (the CFeatureList) changes...
unsigned char uint8_t
Definition: rptypes.h:43
nanoflann::KDTreeSingleIndexAdaptor< metric_t, detail::TSIFTDesc2KDTree_Adaptor< distance_t > > kdtree_t
distance_t kdtree_distance(const element_t *p1, const size_t idx_p2, size_t size) const
A list of visual features, to be used as output by detectors, as input/output by trackers, etc.
Definition: CFeature.h:211
detail::TSURFDesc2KDTree_Adaptor< distance_t > m_adaptor
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
distance_t kdtree_distance(const element_t *p1, const size_t idx_p2, size_t size) const
nanoflann::KDTreeSingleIndexAdaptor< metric_t, detail::TSURFDesc2KDTree_Adaptor< distance_t > > kdtree_t
#define ASSERT_(f)
TSIFTDescriptorsKDTreeIndex(const CFeatureList &feats)
Constructor from a list of SIFT features.
element_t kdtree_get_pt(const size_t idx, int dim) const
GLsizeiptr size
Definition: glext.h:3779
GLenum const GLfloat * params
Definition: glext.h:3514



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019