composite_index.h
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_COMPOSITE_INDEX_H_
32 #define RTABMAP_FLANN_COMPOSITE_INDEX_H_
33 
34 #include "rtflann/general.h"
38 
39 namespace rtflann
40 {
41 
45 struct CompositeIndexParams : public IndexParams
46 {
47  CompositeIndexParams(int trees = 4, int branching = 32, int iterations = 11,
48  flann_centers_init_t centers_init = FLANN_CENTERS_RANDOM, float cb_index = 0.2 )
49  {
50  (*this)["algorithm"] = FLANN_INDEX_KMEANS;
51  // number of randomized trees to use (for kdtree)
52  (*this)["trees"] = trees;
53  // branching factor
54  (*this)["branching"] = branching;
55  // max iterations to perform in one kmeans clustering (kmeans tree)
56  (*this)["iterations"] = iterations;
57  // algorithm used for picking the initial cluster centers for kmeans tree
58  (*this)["centers_init"] = centers_init;
59  // cluster boundary index. Used when searching the kmeans tree
60  (*this)["cb_index"] = cb_index;
61  }
62 };
63 
64 
70 template <typename Distance>
71 class CompositeIndex : public NNIndex<Distance>
72 {
73 public:
74  typedef typename Distance::ElementType ElementType;
75  typedef typename Distance::ResultType DistanceType;
76 
78 
79  typedef bool needs_kdtree_distance;
80 
88  CompositeIndex(const IndexParams& params = CompositeIndexParams(), Distance d = Distance()) :
90  {
93 
94  }
95 
96  CompositeIndex(const Matrix<ElementType>& inputData, const IndexParams& params = CompositeIndexParams(),
97  Distance d = Distance()) : BaseClass(params, d)
98  {
99  kdtree_index_ = new KDTreeIndex<Distance>(inputData, params, d);
100  kmeans_index_ = new KMeansIndex<Distance>(inputData, params, d);
101  }
102 
105  {
106  }
107 
109  {
110  this->swap(other);
111  return *this;
112  }
113 
114  virtual ~CompositeIndex()
115  {
117  delete kmeans_index_;
118  }
119 
120  BaseClass* clone() const
121  {
122  return new CompositeIndex(*this);
123  }
124 
128  flann_algorithm_t getType() const
129  {
130  return FLANN_INDEX_COMPOSITE;
131  }
132 
136  size_t size() const
137  {
138  return kdtree_index_->size();
139  }
140 
144  size_t veclen() const
145  {
146  return kdtree_index_->veclen();
147  }
148 
152  int usedMemory() const
153  {
154  return kmeans_index_->usedMemory() + kdtree_index_->usedMemory();
155  }
156 
161  void buildIndex()
162  {
163  Logger::info("Building kmeans tree...\n");
164  kmeans_index_->buildIndex();
165  Logger::info("Building kdtree tree...\n");
166  kdtree_index_->buildIndex();
167  }
168 
169  void addPoints(const Matrix<ElementType>& points, float rebuild_threshold = 2)
170  {
171  kmeans_index_->addPoints(points, rebuild_threshold);
172  kdtree_index_->addPoints(points, rebuild_threshold);
173  }
174 
175  void removePoint(size_t index)
176  {
177  kmeans_index_->removePoint(index);
178  kdtree_index_->removePoint(index);
179  }
180 
181 
186  void saveIndex(FILE* stream)
187  {
188  kmeans_index_->saveIndex(stream);
189  kdtree_index_->saveIndex(stream);
190  }
191 
196  void loadIndex(FILE* stream)
197  {
198  kmeans_index_->loadIndex(stream);
199  kdtree_index_->loadIndex(stream);
200  }
201 
205  void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) const
206  {
207  kmeans_index_->findNeighbors(result, vec, searchParams);
208  kdtree_index_->findNeighbors(result, vec, searchParams);
209  }
210 
211 protected:
212  void swap(CompositeIndex& other)
213  {
214  std::swap(kmeans_index_, other.kmeans_index_);
215  std::swap(kdtree_index_, other.kdtree_index_);
216  }
217 
218  void buildIndexImpl()
219  {
220  /* nothing to do here */
221  }
222 
223  void freeIndex()
224  {
225  /* nothing to do here */
226  }
227 
228 
229 private:
232 
235 };
236 
237 }
238 
239 #endif //FLANN_COMPOSITE_INDEX_H_
rtflann::CompositeIndex::buildIndex
void buildIndex()
Builds the index.
Definition: composite_index.h:189
general.h
rtflann::ResultSet
Definition: result_set.h:110
rtflann::NNIndex
Definition: nn_index.h:101
kmeans_index.h
rtflann::CompositeIndex::CompositeIndex
CompositeIndex(const IndexParams &params=CompositeIndexParams(), Distance d=Distance())
Definition: composite_index.h:116
stream
stream
rtflann::CompositeIndex::findNeighbors
void findNeighbors(ResultSet< DistanceType > &result, const ElementType *vec, const SearchParams &searchParams) const
Method that searches for nearest-neighbours.
Definition: composite_index.h:233
rtflann::CompositeIndex::~CompositeIndex
virtual ~CompositeIndex()
Definition: composite_index.h:142
rtflann::CompositeIndex::loadIndex
void loadIndex(FILE *stream)
Loads the index from a stream.
Definition: composite_index.h:224
rtflann::CompositeIndex::needs_kdtree_distance
bool needs_kdtree_distance
Definition: composite_index.h:107
nn_index.h
rtflann::CompositeIndex::veclen
size_t veclen() const
Definition: composite_index.h:172
rtflann::CompositeIndexParams::CompositeIndexParams
CompositeIndexParams(int trees=4, int branching=32, int iterations=11, flann_centers_init_t centers_init=FLANN_CENTERS_RANDOM, float cb_index=0.2)
Definition: composite_index.h:103
rtflann::FLANN_INDEX_COMPOSITE
@ FLANN_INDEX_COMPOSITE
Definition: defines.h:84
rtflann::CompositeIndex::addPoints
void addPoints(const Matrix< ElementType > &points, float rebuild_threshold=2)
Incrementally add points to the index.
Definition: composite_index.h:197
rtflann::FLANN_CENTERS_RANDOM
@ FLANN_CENTERS_RANDOM
Definition: defines.h:97
rtflann::KMeansIndex
Definition: kmeans_index.h:111
rtflann::CompositeIndex::freeIndex
void freeIndex()
Definition: composite_index.h:251
rtflann::CompositeIndex::ElementType
Distance::ElementType ElementType
Definition: composite_index.h:102
rtflann::CompositeIndex::DistanceType
Distance::ResultType DistanceType
Definition: composite_index.h:103
rtflann::CompositeIndex::size
size_t size() const
Definition: composite_index.h:164
rtflann::CompositeIndex::kmeans_index_
KMeansIndex< Distance > * kmeans_index_
Definition: composite_index.h:259
std::swap
void swap(GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &a, GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &b)
rtflann::SearchParams
Definition: params.h:87
d
d
params
SmartProjectionParams params(gtsam::HESSIAN, gtsam::ZERO_ON_DEGENERACY)
rtflann::KDTreeIndex
Definition: kdtree_index.h:101
rtflann::CompositeIndex::BaseClass
NNIndex< Distance > BaseClass
Definition: composite_index.h:105
rtflann::FLANN_INDEX_KMEANS
@ FLANN_INDEX_KMEANS
Definition: defines.h:83
rtflann::CompositeIndex::usedMemory
int usedMemory() const
Definition: composite_index.h:180
rtflann::CompositeIndex::getType
flann_algorithm_t getType() const
Definition: composite_index.h:156
rtflann::flann_algorithm_t
flann_algorithm_t
Definition: defines.h:79
rtflann::CompositeIndexParams
Definition: composite_index.h:73
rtflann::CompositeIndex::swap
void swap(CompositeIndex &other)
Definition: composite_index.h:240
rtflann::CompositeIndex::operator=
CompositeIndex & operator=(CompositeIndex other)
Definition: composite_index.h:136
rtflann::CompositeIndex::clone
BaseClass * clone() const
Definition: composite_index.h:148
rtflann::CompositeIndex::saveIndex
void saveIndex(FILE *stream)
Saves the index to a stream.
Definition: composite_index.h:214
rtflann::IndexParams
std::map< std::string, any > IndexParams
Definition: params.h:77
rtflann::CompositeIndex::kdtree_index_
KDTreeIndex< Distance > * kdtree_index_
Definition: composite_index.h:262
rtflann::CompositeIndex::buildIndexImpl
void buildIndexImpl()
Definition: composite_index.h:246
kdtree_index.h
rtflann::flann_centers_init_t
flann_centers_init_t
Definition: defines.h:95
rtflann::CompositeIndex::removePoint
void removePoint(size_t index)
Definition: composite_index.h:203
rtflann
Definition: all_indices.h:49
other
other
rtflann::Matrix< ElementType >
rtflann::CompositeIndex
Definition: composite_index.h:99


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:08