ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
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 FLANN_HPP_
32 #define FLANN_HPP_
33 
34 
35 #include <vector>
36 #include <string>
37 #include <cassert>
38 #include <cstdio>
39 
40 #include "FLANN/general.h"
41 #include "FLANN/util/matrix.h"
42 #include "FLANN/util/params.h"
43 #include "FLANN/util/saving.h"
44 
46 
47 namespace flann
48 {
49 
54 inline void log_verbosity(int level)
55 {
56  if (level >= 0) {
57  Logger::setLevel(level);
58  }
59 }
60 
65 {
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 
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 
207  {
208  return nnIndex_->getType();
209  }
210 
214  int usedMemory() const
215  {
216  return nnIndex_->usedMemory();
217  }
218 
219 
224  {
225  return nnIndex_->getParameters();
226  }
227 
236  int knnSearch(const Matrix<ElementType>& queries,
237  Matrix<size_t>& indices,
238  Matrix<DistanceType>& dists,
239  size_t knn,
240  const SearchParams& params) const
241  {
242  return nnIndex_->knnSearch(queries, indices, dists, knn, params);
243  }
244 
254  int knnSearch(const Matrix<ElementType>& queries,
255  Matrix<int>& indices,
256  Matrix<DistanceType>& dists,
257  size_t knn,
258  const SearchParams& params) const
259  {
260  return nnIndex_->knnSearch(queries, indices, dists, knn, params);
261  }
262 
271  int knnSearch(const Matrix<ElementType>& queries,
272  std::vector< std::vector<size_t> >& indices,
273  std::vector<std::vector<DistanceType> >& dists,
274  size_t knn,
275  const SearchParams& params) const
276  {
277  return nnIndex_->knnSearch(queries, indices, dists, knn, params);
278  }
279 
289  int knnSearch(const Matrix<ElementType>& queries,
290  std::vector< std::vector<int> >& indices,
291  std::vector<std::vector<DistanceType> >& dists,
292  size_t knn,
293  const SearchParams& params) const
294  {
295  return nnIndex_->knnSearch(queries, indices, dists, knn, params);
296  }
297 
307  int radiusSearch(const Matrix<ElementType>& queries,
308  Matrix<size_t>& indices,
309  Matrix<DistanceType>& dists,
310  float radius,
311  const SearchParams& params) const
312  {
313  return nnIndex_->radiusSearch(queries, indices, dists, radius, params);
314  }
315 
325  int radiusSearch(const Matrix<ElementType>& queries,
326  Matrix<int>& indices,
327  Matrix<DistanceType>& dists,
328  float radius,
329  const SearchParams& params) const
330  {
331  return nnIndex_->radiusSearch(queries, indices, dists, radius, params);
332  }
333 
343  int radiusSearch(const Matrix<ElementType>& queries,
344  std::vector< std::vector<size_t> >& indices,
345  std::vector<std::vector<DistanceType> >& dists,
346  float radius,
347  const SearchParams& params) const
348  {
349  return nnIndex_->radiusSearch(queries, indices, dists, radius, params);
350  }
351 
361  int radiusSearch(const Matrix<ElementType>& queries,
362  std::vector< std::vector<int> >& indices,
363  std::vector<std::vector<DistanceType> >& dists,
364  float radius,
365  const SearchParams& params) const
366  {
367  return nnIndex_->radiusSearch(queries, indices, dists, radius, params);
368  }
369 
370 private:
371  IndexType* load_saved_index(const Matrix<ElementType>& dataset, const std::string& filename, Distance distance)
372  {
373  FILE* fin = fopen(filename.c_str(), "rb");
374  if (fin == NULL) {
375  return NULL;
376  }
377  IndexHeader header = load_header(fin);
378  if (header.h.data_type != flann_datatype_value<ElementType>::value) {
379  throw FLANNException("Datatype of saved index is different than of the one to be loaded.");
380  }
381 
383  params["algorithm"] = header.h.index_type;
384  IndexType* nnIndex = create_index_by_type<Distance>(header.h.index_type, dataset, params, distance);
385  rewind(fin);
386  nnIndex->loadIndex(fin);
387  fclose(fin);
388 
389  return nnIndex;
390  }
391 
392  void swap( Index& other)
393  {
394  std::swap(nnIndex_, other.nnIndex_);
395  std::swap(loaded_, other.loaded_);
396  std::swap(index_params_, other.index_params_);
397  }
398 
399 private:
401  IndexType* nnIndex_;
403  bool loaded_;
405  IndexParams index_params_;
406 };
407 
408 
409 
410 
411 
423 template <typename Distance>
426 {
427  KMeansIndex<Distance> kmeans(points, params, d);
428  kmeans.buildIndex();
429 
430  int clusterNum = kmeans.getClusterCenters(centers);
431  return clusterNum;
432 }
433 
434 }
435 #endif /* FLANN_HPP_ */
std::string filename
int points
double Distance(const Point3D< Real > &p1, const Point3D< Real > &p2)
cmdLineReadable * params[]
#define NULL
virtual int usedMemory() const =0
virtual void saveIndex(FILE *stream)=0
virtual flann_algorithm_t getType() const =0
size_t size() const
Definition: flann.hpp:198
IndexParams getParameters() const
Definition: flann.hpp:223
int usedMemory() const
Definition: flann.hpp:214
void buildIndex()
Definition: flann.hpp:137
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:289
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:236
size_t veclen() const
Definition: flann.hpp:190
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:307
void save(std::string filename)
Definition: flann.hpp:177
NNIndex< Distance > IndexType
Definition: flann.hpp:81
void addPoints(const Matrix< ElementType > &points, float rebuild_threshold=2)
Definition: flann.hpp:149
Distance::ElementType ElementType
Definition: flann.hpp:79
Index & operator=(Index other)
Definition: flann.hpp:123
int knnSearch(const Matrix< ElementType > &queries, Matrix< int > &indices, Matrix< DistanceType > &dists, size_t knn, const SearchParams &params) const
Definition: flann.hpp:254
void buildIndex(const Matrix< ElementType > &points)
Definition: flann.hpp:144
ElementType * getPoint(size_t point_id)
Definition: flann.hpp:168
Index(const IndexParams &params, Distance distance=Distance())
Definition: flann.hpp:83
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:361
Index(const Index &other)
Definition: flann.hpp:118
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:343
void removePoint(size_t point_id)
Definition: flann.hpp:158
Distance::ResultType DistanceType
Definition: flann.hpp:80
flann_algorithm_t getType() const
Definition: flann.hpp:206
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:271
virtual ~Index()
Definition: flann.hpp:129
Index(const Matrix< ElementType > &features, const IndexParams &params, Distance distance=Distance())
Definition: flann.hpp:101
int radiusSearch(const Matrix< ElementType > &queries, Matrix< int > &indices, Matrix< DistanceType > &dists, float radius, const SearchParams &params) const
Definition: flann.hpp:325
int getClusterCenters(Matrix< DistanceType > &centers)
Definition: kmeans_index.h:301
virtual void buildIndex()
Definition: nn_index.h:125
static void setLevel(int level)
Definition: logger.h:85
virtual 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: nn_index.h:305
virtual void removePoint(size_t id)
Definition: nn_index.h:161
size_t veclen() const
Definition: nn_index.h:209
int radiusSearch(const Matrix< ElementType > &queries, Matrix< size_t > &indices, Matrix< DistanceType > &dists, float radius, const SearchParams &params) const
Perform radius search.
Definition: nn_index.h:491
virtual void addPoints(const Matrix< ElementType > &points, float rebuild_threshold=2)
Incrementally add points to the index.
Definition: nn_index.h:152
size_t size() const
Definition: nn_index.h:201
IndexParams getParameters() const
Definition: nn_index.h:219
virtual ElementType * getPoint(size_t id)
Definition: nn_index.h:187
virtual void buildIndex()
Definition: nn_index.h:125
virtual NNIndex * clone() const =0
flann_algorithm_t
Definition: defines.h:80
@ FLANN_INDEX_SAVED
Definition: defines.h:91
IndexHeader load_header(FILE *stream)
Definition: saving.h:106
int hierarchicalClustering(const Matrix< typename Distance::ElementType > &points, Matrix< typename Distance::ResultType > &centers, const KMeansIndexParams &params, Distance d=Distance())
Definition: flann.hpp:424
void log_verbosity(int level)
Definition: flann.hpp:54
std::map< std::string, any > IndexParams
Definition: params.h:51
void swap(cloudViewer::core::SmallVectorImpl< T > &LHS, cloudViewer::core::SmallVectorImpl< T > &RHS)
Implement std::swap in terms of SmallVector swap.
Definition: SmallVector.h:1370
SavedIndexParams(std::string filename)
Definition: flann.hpp:66
static const flann_datatype_t value
Definition: general.h:54