MRPT  1.9.9
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-2018, 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 {
22 // Private auxiliary classes
23 template <typename distance_t, typename element_t = uint8_t>
25 template <typename distance_t, typename element_t = float>
27 } // namespace detail
28 
29 /** \defgroup mrptvision_descr_kdtrees KD-Tree construction of visual
30  * descriptors
31  * \ingroup mrpt_vision_grp
32  */
33 
34 /** \addtogroup mrptvision_descr_kdtrees
35  @{ */
36 
37 /** A kd-tree builder for sets of features with SIFT descriptors.
38  * Example of usage:
39  * \code
40  * TSIFTDescriptorsKDTreeIndex<double> feats_kdtree(feats);
41  * feats_kdtree.get_kdtree().knnSearch( ... );
42  * \endcode
43  * \sa CFeatureList, mrpt::vision::find_descriptor_pairings
44  */
45 template <
46  typename distance_t,
47  class metric_t = nanoflann::L2_Simple_Adaptor<
48  uint8_t /*SIFT desc elements*/,
51 {
52  public:
55 
56  /** Constructor from a list of SIFT features.
57  * Automatically build the KD-tree index. The list of features must NOT be
58  * empty or an exception will be raised.
59  */
61  : m_adaptor(feats), m_kdtree(nullptr), m_feats(feats)
62  {
63  ASSERT_(!feats.empty() && feats[0]->descriptors.hasDescriptorSIFT());
64  this->regenerate_kdtreee();
65  }
66 
67  /** Re-creates the kd-tree, which must be done whenever the data source (the
68  * CFeatureList) changes. */
70  {
71  if (m_kdtree) delete m_kdtree;
72 
74  m_kdtree = new kdtree_t(
75  m_feats[0]->descriptors.SIFT.size() /* DIM */, m_adaptor, params);
76  m_kdtree->buildIndex();
77  }
78 
79  /** Access to the kd-tree object */
80  kdtree_t& get_kdtree() { return *m_kdtree; }
81  const kdtree_t& get_kdtree() const { return *m_kdtree; }
83  {
84  delete m_kdtree;
85  m_kdtree = nullptr;
86  }
87 
88  private:
91 
93 }; // end of TSIFTDescriptorsKDTreeIndex
94 
95 /** A kd-tree builder for sets of features with SURF descriptors.
96  * Example of usage:
97  * \code
98  * TSURFDescriptorsKDTreeIndex<double> feats_kdtree(feats);
99  * feats_kdtree.get_kdtree().knnSearch( ... );
100  * \endcode
101  * \sa CFeatureList, mrpt::vision::find_descriptor_pairings
102  */
103 template <
104  typename distance_t,
105  class metric_t = nanoflann::L2_Simple_Adaptor<
106  float /*SURF desc elements*/,
109 {
110  public:
113 
114  /** Constructor from a list of SIFT features.
115  * Automatically build the KD-tree index. The list of features must NOT be
116  * empty or an exception will be raised.
117  */
119  : m_adaptor(feats), m_kdtree(nullptr), m_feats(feats)
120  {
121  ASSERT_(!feats.empty() && feats[0]->descriptors.hasDescriptorSIFT());
122  this->regenerate_kdtreee();
123  }
124 
125  /** Re-creates the kd-tree, which must be done whenever the data source (the
126  * CFeatureList) changes. */
128  {
129  if (m_kdtree) delete m_kdtree;
130 
132  m_kdtree = new kdtree_t(
133  m_feats[0]->descriptors.SIFT.size() /* DIM */, m_adaptor, params);
134  m_kdtree->buildIndex();
135  }
136 
137  /** Access to the kd-tree object */
138  kdtree_t& get_kdtree() { return *m_kdtree; }
139  const kdtree_t& get_kdtree() const { return *m_kdtree; }
141  {
142  delete m_kdtree;
143  m_kdtree = nullptr;
144  }
145 
146  private:
149 
151 }; // end of TSURFDescriptorsKDTreeIndex
152 
153 /** @} */
154 
155 namespace detail
156 {
157 template <typename distance_t, typename element_t>
158 struct TSIFTDesc2KDTree_Adaptor
159 {
162  // Must return the number of data points
163  inline size_t kdtree_get_point_count() const { return m_feats.size(); }
164  // Must return the Euclidean (L2) distance between the vector "p1[0:size-1]"
165  // and the data point with index "idx_p2" stored in the class:
166  inline distance_t kdtree_distance(
167  const element_t* p1, const size_t idx_p2, size_t size) const
168  {
169  const size_t dim = m_feats[idx_p2]->descriptors.SIFT.size();
170  const element_t* p2 = &m_feats[idx_p2]->descriptors.SIFT[0];
171  distance_t d = 0;
172  for (size_t i = 0; i < dim; i++)
173  {
174  d += (*p1 - *p2) * (*p1 - *p2);
175  p1++;
176  p2++;
177  }
178  return d;
179  }
180  // Must return the dim'th component of the idx'th point in the class:
181  inline element_t kdtree_get_pt(const size_t idx, int dim) const
182  {
183  return m_feats[idx]->descriptors.SIFT[dim];
184  }
185  template <class BBOX>
186  bool kdtree_get_bbox(BBOX& bb) const
187  {
188  return false;
189  }
190 };
191 
192 template <typename distance_t, typename element_t>
193 struct TSURFDesc2KDTree_Adaptor
194 {
197  // Must return the number of data points
198  inline size_t kdtree_get_point_count() const { return m_feats.size(); }
199  // Must return the Euclidean (L2) distance between the vector "p1[0:size-1]"
200  // and the data point with index "idx_p2" stored in the class:
201  inline distance_t kdtree_distance(
202  const element_t* p1, const size_t idx_p2, size_t size) const
203  {
204  const size_t dim = m_feats[idx_p2]->descriptors.SURF.size();
205  const element_t* p2 = &m_feats[idx_p2]->descriptors.SURF[0];
206  distance_t d = 0;
207  for (size_t i = 0; i < dim; i++)
208  {
209  d += (*p1 - *p2) * (*p1 - *p2);
210  p1++;
211  p2++;
212  }
213  return d;
214  }
215  // Must return the dim'th component of the idx'th point in the class:
216  inline element_t kdtree_get_pt(const size_t idx, int dim) const
217  {
218  return m_feats[idx]->descriptors.SURF[dim];
219  }
220  template <class BBOX>
221  bool kdtree_get_bbox(BBOX& bb) const
222  {
223  return false;
224  }
225 };
226 } // namespace detail
227 } // namespace vision
228 } // namespace mrpt
229 #endif
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.
Squared Euclidean (L2) distance functor (suitable for low-dimensionality datasets, like 2D or 3D point clouds) Corresponding distance traits: nanoflann::metric_L2_Simple.
Definition: nanoflann.hpp:355
kdtree_t & get_kdtree()
Access to the kd-tree object.
size_t size() const
Definition: CFeature.h:386
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:41
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
typename nanoflann::KDTreeSingleIndexAdaptor< metric_t, detail::TSURFDesc2KDTree_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:304
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
Parameters (see README.md)
Definition: nanoflann.hpp:403
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:3923
typename nanoflann::KDTreeSingleIndexAdaptor< metric_t, detail::TSIFTDesc2KDTree_Adaptor< distance_t > > kdtree_t
GLenum const GLfloat * params
Definition: glext.h:3534



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020