flann.hpp
Go to the documentation of this file.
1 /***********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
5  * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
6  *
7  * THE BSD LICENSE
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *************************************************************************/
30 
31 #ifndef RTABMAP_FLANN_HPP_
32 #define RTABMAP_FLANN_HPP_
33 
34 
35 #include <vector>
36 #include <string>
37 #include <cassert>
38 #include <cstdio>
39 
40 #include "general.h"
41 #include "util/matrix.h"
42 #include "util/params.h"
43 #include "util/saving.h"
44 
45 #include "algorithms/all_indices.h"
46 
47 namespace rtflann
48 {
49 
54 inline void log_verbosity(int level)
55 {
56  if (level >= 0) {
57  Logger::setLevel(level);
58  }
59 }
60 
65 {
66  SavedIndexParams(std::string filename)
67  {
68  (*this)["algorithm"] = FLANN_INDEX_SAVED;
69  (*this)["filename"] = filename;
70  }
71 };
72 
73 
74 
75 template<typename Distance>
76 class Index
77 {
78 public:
79  typedef typename Distance::ElementType ElementType;
80  typedef typename Distance::ResultType DistanceType;
82 
83  Index(const IndexParams& params, Distance distance = Distance() )
84  : index_params_(params)
85  {
86  flann_algorithm_t index_type = get_param<flann_algorithm_t>(params,"algorithm");
87  loaded_ = false;
88 
89  Matrix<ElementType> features;
90  if (index_type == FLANN_INDEX_SAVED) {
91  nnIndex_ = load_saved_index(features, get_param<std::string>(params,"filename"), distance);
92  loaded_ = true;
93  }
94  else {
95  flann_algorithm_t index_type = get_param<flann_algorithm_t>(params, "algorithm");
96  nnIndex_ = create_index_by_type<Distance>(index_type, features, params, distance);
97  }
98  }
99 
100 
101  Index(const Matrix<ElementType>& features, const IndexParams& params, Distance distance = Distance() )
102  : index_params_(params)
103  {
104  flann_algorithm_t index_type = get_param<flann_algorithm_t>(params,"algorithm");
105  loaded_ = false;
106 
107  if (index_type == FLANN_INDEX_SAVED) {
108  nnIndex_ = load_saved_index(features, get_param<std::string>(params,"filename"), distance);
109  loaded_ = true;
110  }
111  else {
112  flann_algorithm_t index_type = get_param<flann_algorithm_t>(params, "algorithm");
113  nnIndex_ = create_index_by_type<Distance>(index_type, features, params, distance);
114  }
115  }
116 
117 
118  Index(const Index& other) : loaded_(other.loaded_), index_params_(other.index_params_)
119  {
120  nnIndex_ = other.nnIndex_->clone();
121  }
122 
124  {
125  this->swap(other);
126  return *this;
127  }
128 
129  virtual ~Index()
130  {
131  delete nnIndex_;
132  }
133 
137  void buildIndex()
138  {
139  if (!loaded_) {
140  nnIndex_->buildIndex();
141  }
142  }
143 
144  void buildIndex(const Matrix<ElementType>& points)
145  {
146  nnIndex_->buildIndex(points);
147  }
148 
149  void addPoints(const Matrix<ElementType>& points, float rebuild_threshold = 2)
150  {
151  nnIndex_->addPoints(points, rebuild_threshold);
152  }
153 
158  void removePoint(size_t point_id)
159  {
160  nnIndex_->removePoint(point_id);
161  }
162 
168  ElementType* getPoint(size_t point_id)
169  {
170  return nnIndex_->getPoint(point_id);
171  }
172 
177  void save(std::string filename)
178  {
179  FILE* fout = fopen(filename.c_str(), "wb");
180  if (fout == NULL) {
181  throw FLANNException("Cannot open file");
182  }
183  nnIndex_->saveIndex(fout);
184  fclose(fout);
185  }
186 
190  size_t veclen() const
191  {
192  return nnIndex_->veclen();
193  }
194 
198  size_t size() const
199  {
200  return nnIndex_->size();
201  }
202 
203  size_t removedCount() const
204  {
205  return nnIndex_->removedCount();
206  }
207 
208  size_t sizeAtBuild() const
209  {
210  return nnIndex_->sizeAtBuild();
211  }
212 
217  {
218  return nnIndex_->getType();
219  }
220 
224  int usedMemory() const
225  {
226  return nnIndex_->usedMemory();
227  }
228 
229 
234  {
235  return nnIndex_->getParameters();
236  }
237 
246  int knnSearch(const Matrix<ElementType>& queries,
247  Matrix<size_t>& indices,
248  Matrix<DistanceType>& dists,
249  size_t knn,
250  const SearchParams& params) const
251  {
252  return nnIndex_->knnSearch(queries, indices, dists, knn, params);
253  }
254 
264  int knnSearch(const Matrix<ElementType>& queries,
265  Matrix<int>& indices,
266  Matrix<DistanceType>& dists,
267  size_t knn,
268  const SearchParams& params) const
269  {
270  return nnIndex_->knnSearch(queries, indices, dists, knn, params);
271  }
272 
281  int knnSearch(const Matrix<ElementType>& queries,
282  std::vector< std::vector<size_t> >& indices,
283  std::vector<std::vector<DistanceType> >& dists,
284  size_t knn,
285  const SearchParams& params) const
286  {
287  return nnIndex_->knnSearch(queries, indices, dists, knn, params);
288  }
289 
299  int knnSearch(const Matrix<ElementType>& queries,
300  std::vector< std::vector<int> >& indices,
301  std::vector<std::vector<DistanceType> >& dists,
302  size_t knn,
303  const SearchParams& params) const
304  {
305  return nnIndex_->knnSearch(queries, indices, dists, knn, params);
306  }
307 
317  int radiusSearch(const Matrix<ElementType>& queries,
318  Matrix<size_t>& indices,
319  Matrix<DistanceType>& dists,
320  float radius,
321  const SearchParams& params) const
322  {
323  return nnIndex_->radiusSearch(queries, indices, dists, radius, params);
324  }
325 
335  int radiusSearch(const Matrix<ElementType>& queries,
336  Matrix<int>& indices,
337  Matrix<DistanceType>& dists,
338  float radius,
339  const SearchParams& params) const
340  {
341  return nnIndex_->radiusSearch(queries, indices, dists, radius, params);
342  }
343 
353  int radiusSearch(const Matrix<ElementType>& queries,
354  std::vector< std::vector<size_t> >& indices,
355  std::vector<std::vector<DistanceType> >& dists,
356  float radius,
357  const SearchParams& params) const
358  {
359  return nnIndex_->radiusSearch(queries, indices, dists, radius, params);
360  }
361 
371  int radiusSearch(const Matrix<ElementType>& queries,
372  std::vector< std::vector<int> >& indices,
373  std::vector<std::vector<DistanceType> >& dists,
374  float radius,
375  const SearchParams& params) const
376  {
377  return nnIndex_->radiusSearch(queries, indices, dists, radius, params);
378  }
379 
380 private:
381  IndexType* load_saved_index(const Matrix<ElementType>& dataset, const std::string& filename, Distance distance)
382  {
383  FILE* fin = fopen(filename.c_str(), "rb");
384  if (fin == NULL) {
385  return NULL;
386  }
389  throw FLANNException("Datatype of saved index is different than of the one to be loaded.");
390  }
391 
392  IndexParams params;
393  params["algorithm"] = header.h.index_type;
394  IndexType* nnIndex = create_index_by_type<Distance>(header.h.index_type, dataset, params, distance);
395  rewind(fin);
396  nnIndex->loadIndex(fin);
397  fclose(fin);
398 
399  return nnIndex;
400  }
401 
402  void swap( Index& other)
403  {
404  std::swap(nnIndex_, other.nnIndex_);
405  std::swap(loaded_, other.loaded_);
406  std::swap(index_params_, other.index_params_);
407  }
408 
409 private:
411  IndexType* nnIndex_;
413  bool loaded_;
416 };
417 
418 
419 
420 
421 
433 template <typename Distance>
435  const KMeansIndexParams& params, Distance d = Distance())
436 {
437  KMeansIndex<Distance> kmeans(points, params, d);
438  kmeans.buildIndex();
439 
440  int clusterNum = kmeans.getClusterCenters(centers);
441  return clusterNum;
442 }
443 
444 }
445 #endif /* RTABMAP_FLANN_HPP_ */
d
#define NULL
SavedIndexParams(std::string filename)
Definition: flann.hpp:66
std::map< std::string, any > IndexParams
Definition: params.h:51
void log_verbosity(int level)
Definition: flann.hpp:54
flann_datatype_t data_type
Definition: serialization.h:18
int radiusSearch(const Matrix< ElementType > &queries, std::vector< std::vector< int > > &indices, std::vector< std::vector< DistanceType > > &dists, float radius, const SearchParams &params) const
Definition: flann.hpp:371
IndexType * load_saved_index(const Matrix< ElementType > &dataset, const std::string &filename, Distance distance)
Definition: flann.hpp:381
void buildIndex()
Definition: flann.hpp:137
IndexParams index_params_
Definition: flann.hpp:415
void removePoint(size_t point_id)
Definition: flann.hpp:158
int knnSearch(const Matrix< ElementType > &queries, Matrix< int > &indices, Matrix< DistanceType > &dists, size_t knn, const SearchParams &params) const
Definition: flann.hpp:264
ElementType * getPoint(size_t point_id)
Definition: flann.hpp:168
size_t size() const
Definition: flann.hpp:198
int getClusterCenters(Matrix< DistanceType > &centers)
Definition: kmeans_index.h:301
GLM_FUNC_DECL genType::value_type distance(genType const &p0, genType const &p1)
void swap(linb::any &lhs, linb::any &rhs) noexcept
virtual ~Index()
Definition: flann.hpp:129
std_msgs::Header * header(M &m)
int radiusSearch(const Matrix< ElementType > &queries, Matrix< size_t > &indices, Matrix< DistanceType > &dists, float radius, const SearchParams &params) const
Perform radius search.
Definition: flann.hpp:317
NNIndex< Distance > IndexType
Definition: flann.hpp:81
IndexHeaderStruct h
Definition: saving.h:53
Index(const IndexParams &params, Distance distance=Distance())
Definition: flann.hpp:83
bool loaded_
Definition: flann.hpp:413
virtual void buildIndex()
Definition: nn_index.h:125
size_t veclen() const
Definition: flann.hpp:190
flann_algorithm_t index_type
Definition: serialization.h:19
void swap(Index &other)
Definition: flann.hpp:402
IndexType * nnIndex_
Definition: flann.hpp:411
void addPoints(const Matrix< ElementType > &points, float rebuild_threshold=2)
Definition: flann.hpp:149
flann_algorithm_t
Definition: defines.h:79
flann_algorithm_t getType() const
Definition: flann.hpp:216
Distance::ResultType DistanceType
Definition: flann.hpp:80
int radiusSearch(const Matrix< ElementType > &queries, std::vector< std::vector< size_t > > &indices, std::vector< std::vector< DistanceType > > &dists, float radius, const SearchParams &params) const
Perform radius search.
Definition: flann.hpp:353
IndexHeader load_header(FILE *stream)
Definition: saving.h:106
Index(const Matrix< ElementType > &features, const IndexParams &params, Distance distance=Distance())
Definition: flann.hpp:101
virtual void loadIndex(FILE *stream)=0
int knnSearch(const Matrix< ElementType > &queries, Matrix< size_t > &indices, Matrix< DistanceType > &dists, size_t knn, const SearchParams &params) const
Perform k-nearest neighbor search.
Definition: flann.hpp:246
void buildIndex(const Matrix< ElementType > &points)
Definition: flann.hpp:144
IndexParams getParameters() const
Definition: flann.hpp:233
void save(std::string filename)
Definition: flann.hpp:177
Index & operator=(Index other)
Definition: flann.hpp:123
int usedMemory() const
Definition: flann.hpp:224
int knnSearch(const Matrix< ElementType > &queries, std::vector< std::vector< size_t > > &indices, std::vector< std::vector< DistanceType > > &dists, size_t knn, const SearchParams &params) const
Perform k-nearest neighbor search.
Definition: flann.hpp:281
int hierarchicalClustering(const Matrix< typename Distance::ElementType > &points, Matrix< typename Distance::ResultType > &centers, const KMeansIndexParams &params, Distance d=Distance())
Definition: flann.hpp:434
size_t removedCount() const
Definition: flann.hpp:203
static void setLevel(int level)
Definition: logger.h:85
virtual NNIndex * clone() const =0
Distance::ElementType ElementType
Definition: flann.hpp:79
Index(const Index &other)
Definition: flann.hpp:118
size_t sizeAtBuild() const
Definition: flann.hpp:208
int knnSearch(const Matrix< ElementType > &queries, std::vector< std::vector< int > > &indices, std::vector< std::vector< DistanceType > > &dists, size_t knn, const SearchParams &params) const
Definition: flann.hpp:299
int radiusSearch(const Matrix< ElementType > &queries, Matrix< int > &indices, Matrix< DistanceType > &dists, float radius, const SearchParams &params) const
Definition: flann.hpp:335


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Jan 23 2023 03:37:28