MRPT  1.9.9
descriptor_kdtrees.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
12 #include <mrpt/vision/CFeature.h>
13 #include <mrpt/vision/types.h>
14 
15 namespace mrpt
16 {
17 namespace vision
18 {
19 namespace detail
20 {
21 // Private auxiliary classes
22 template <typename distance_t, typename element_t = uint8_t>
24 template <typename distance_t, typename element_t = float>
26 } // namespace detail
27 
28 /** \defgroup mrptvision_descr_kdtrees KD-Tree construction of visual
29  * descriptors
30  * \ingroup mrpt_vision_grp
31  */
32 
33 /** \addtogroup mrptvision_descr_kdtrees
34  @{ */
35 
36 /** A kd-tree builder for sets of features with SIFT descriptors.
37  * Example of usage:
38  * \code
39  * TSIFTDescriptorsKDTreeIndex<double> feats_kdtree(feats);
40  * feats_kdtree.get_kdtree().knnSearch( ... );
41  * \endcode
42  * \sa CFeatureList, mrpt::vision::find_descriptor_pairings
43  */
44 template <
45  typename distance_t,
46  class metric_t = nanoflann::L2_Simple_Adaptor<
47  uint8_t /*SIFT desc elements*/,
50 {
51  public:
54 
55  /** Constructor from a list of SIFT features.
56  * Automatically build the KD-tree index. The list of features must NOT be
57  * empty or an exception will be raised.
58  */
60  : m_adaptor(feats), m_kdtree(nullptr), m_feats(feats)
61  {
62  ASSERT_(!feats.empty() && feats[0].descriptors.hasDescriptorSIFT());
63  this->regenerate_kdtreee();
64  }
65 
66  /** Re-creates the kd-tree, which must be done whenever the data source (the
67  * CFeatureList) changes. */
69  {
70  if (m_kdtree) delete m_kdtree;
71 
73  m_kdtree = new kdtree_t(
74  m_feats[0].descriptors.SIFT->size() /* DIM */, m_adaptor, params);
75  m_kdtree->buildIndex();
76  }
77 
78  /** Access to the kd-tree object */
79  kdtree_t& get_kdtree() { return *m_kdtree; }
80  const kdtree_t& get_kdtree() const { return *m_kdtree; }
82  {
83  delete m_kdtree;
84  m_kdtree = nullptr;
85  }
86 
87  private:
90 
92 }; // end of TSIFTDescriptorsKDTreeIndex
93 
94 /** A kd-tree builder for sets of features with SURF descriptors.
95  * Example of usage:
96  * \code
97  * TSURFDescriptorsKDTreeIndex<double> feats_kdtree(feats);
98  * feats_kdtree.get_kdtree().knnSearch( ... );
99  * \endcode
100  * \sa CFeatureList, mrpt::vision::find_descriptor_pairings
101  */
102 template <
103  typename distance_t,
104  class metric_t = nanoflann::L2_Simple_Adaptor<
105  float /*SURF desc elements*/,
108 {
109  public:
112 
113  /** Constructor from a list of SIFT features.
114  * Automatically build the KD-tree index. The list of features must NOT be
115  * empty or an exception will be raised.
116  */
118  : m_adaptor(feats), m_kdtree(nullptr), m_feats(feats)
119  {
120  ASSERT_(!feats.empty() && feats[0].descriptors.hasDescriptorSIFT());
121  this->regenerate_kdtreee();
122  }
123 
124  /** Re-creates the kd-tree, which must be done whenever the data source (the
125  * CFeatureList) changes. */
127  {
128  if (m_kdtree) delete m_kdtree;
129 
131  m_kdtree = new kdtree_t(
132  m_feats[0].descriptors.SIFT->size() /* DIM */, m_adaptor, params);
133  m_kdtree->buildIndex();
134  }
135 
136  /** Access to the kd-tree object */
137  kdtree_t& get_kdtree() { return *m_kdtree; }
138  const kdtree_t& get_kdtree() const { return *m_kdtree; }
140  {
141  delete m_kdtree;
142  m_kdtree = nullptr;
143  }
144 
145  private:
148 
150 }; // end of TSURFDescriptorsKDTreeIndex
151 
152 /** @} */
153 
154 namespace detail
155 {
156 template <typename distance_t, typename element_t>
157 struct TSIFTDesc2KDTree_Adaptor
158 {
161  // Must return the number of data points
162  inline size_t kdtree_get_point_count() const { return m_feats.size(); }
163  // Must return the Euclidean (L2) distance between the vector "p1[0:size-1]"
164  // and the data point with index "idx_p2" stored in the class:
165  inline distance_t kdtree_distance(
166  const element_t* p1, const size_t idx_p2, size_t size) const
167  {
168  const size_t dim = m_feats[idx_p2].descriptors.SIFT->size();
169  const element_t* p2 = &(*m_feats[idx_p2].descriptors.SIFT)[0];
170  distance_t d = 0;
171  for (size_t i = 0; i < dim; i++)
172  {
173  d += (*p1 - *p2) * (*p1 - *p2);
174  p1++;
175  p2++;
176  }
177  return d;
178  }
179  // Must return the dim'th component of the idx'th point in the class:
180  inline element_t kdtree_get_pt(const size_t idx, int dim) const
181  {
182  return (*m_feats[idx].descriptors.SIFT)[dim];
183  }
184  template <class BBOX>
185  bool kdtree_get_bbox(BBOX& bb) const
186  {
187  return false;
188  }
189 };
190 
191 template <typename distance_t, typename element_t>
192 struct TSURFDesc2KDTree_Adaptor
193 {
196  // Must return the number of data points
197  inline size_t kdtree_get_point_count() const { return m_feats.size(); }
198  // Must return the Euclidean (L2) distance between the vector "p1[0:size-1]"
199  // and the data point with index "idx_p2" stored in the class:
200  inline distance_t kdtree_distance(
201  const element_t* p1, const size_t idx_p2, size_t size) const
202  {
203  const size_t dim = m_feats[idx_p2].descriptors.SURF->size();
204  const element_t* p2 = &(*m_feats[idx_p2].descriptors.SURF)[0];
205  distance_t d = 0;
206  for (size_t i = 0; i < dim; i++)
207  {
208  d += (*p1 - *p2) * (*p1 - *p2);
209  p1++;
210  p2++;
211  }
212  return d;
213  }
214  // Must return the dim'th component of the idx'th point in the class:
215  inline element_t kdtree_get_pt(const size_t idx, int dim) const
216  {
217  return (*m_feats[idx].descriptors.SURF)[dim];
218  }
219  template <class BBOX>
220  bool kdtree_get_bbox(BBOX& bb) const
221  {
222  return false;
223  }
224 };
225 } // namespace detail
226 } // namespace vision
227 } // namespace mrpt
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 MATRIXLIKE &m, const int dim)
size_t size() const
Definition: CFeature.h:352
void regenerate_kdtreee()
Re-creates the kd-tree, which must be done whenever the data source (the CFeatureList) changes...
mrpt::vision::TStereoCalibParams params
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...
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
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:275
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
typename nanoflann::KDTreeSingleIndexAdaptor< metric_t, detail::TSIFTDesc2KDTree_Adaptor< distance_t > > kdtree_t



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020