RTAB-Map 0.24.5
Real-Time Appearance-Based Mapping
Loading...
Searching...
No Matches
FlannIndex.h
1/*
2Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the Universite de Sherbrooke nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#ifndef CORELIB_SRC_FLANNINDEX_H_
29#define CORELIB_SRC_FLANNINDEX_H_
30
31#include "rtabmap/core/rtabmap_core_export.h" // DLL export/import defines
32#include <list>
33#include <opencv2/opencv.hpp>
34
35namespace rtabmap {
36
37class NanoFlannIndex;
38
55class RTABMAP_CORE_EXPORT FlannIndex
56{
57public:
76 {
77 FLANN_INDEX_LINEAR = 0,
78 FLANN_INDEX_KDTREE = 1,
79 FLANN_INDEX_KDTREE_SINGLE = 4,
80 FLANN_INDEX_LSH = 6,
81
86 NANOFLANN_INDEX_KDTREE_SINGLE = 100,
87 };
88
102 static void setDeterministicSeed(bool enable);
103
104 FlannIndex();
105 virtual ~FlannIndex();
106
108 void release();
109
121 std::vector<unsigned char> serializeIndex(bool computeChecksum = true) const;
122
124 size_t indexedFeatures() const;
125
130 size_t memoryUsed() const;
131
144 flann_algorithm_t algorithm,
145 const cv::Mat & features,
146 bool useDistanceL1 = false,
147 float rebalancingFactor = 2.0f);
148
164 const std::vector<unsigned char> & indexData,
165 flann_algorithm_t algorithm,
166 const cv::Mat & features,
167 bool useDistanceL1 = false,
168 float rebalancingFactor = 2.0f,
169 std::string * errorMsg = NULL);
172 const unsigned char * indexData,
173 size_t indexDataSize,
174 flann_algorithm_t algorithm,
175 const cv::Mat & features,
176 bool useDistanceL1 = false,
177 float rebalancingFactor = 2.0f,
178 std::string * errorMsg = NULL);
179
181 bool isBuilt();
182
184 int featuresType() const {return featuresType_;}
186 int featuresDim() const {return featuresDim_;}
187
195 std::vector<unsigned int> addPoints(const cv::Mat & features);
196
204 void removePoint(unsigned int index);
205
222 const cv::Mat & query,
223 cv::Mat & indices,
224 cv::Mat & dists,
225 int knn,
226 int checks = 32,
227 float eps = 0.0,
228 bool sorted = true,
229 int cores = 1) const;
247 const cv::Mat & query,
248 std::vector<std::vector<size_t> > & indices,
249 std::vector<std::vector<float> > & dists,
250 float radius,
251 int maxNeighbors = 0,
252 int checks = 32,
253 float eps = 0.0,
254 bool sorted = true,
255 int cores = 1) const;
256
257private:
258 void * index_; // rtflann backend
259 NanoFlannIndex * nanoIndex_; // nanoflann backend, only one of the two is set
260 unsigned int nextIndex_;
261 int featuresType_;
262 int featuresDim_;
263 bool useDistanceL1_; // true=EUCLEDIAN_L2 false=MANHATTAN_L1
264 float rebalancingFactor_;
265 flann_algorithm_t algorithm_;
266
267 // keep feature in memory until the tree is rebuilt
268 // (in case the word is deleted when removed from the VWDictionary)
269 std::map<int, cv::Mat> addedDescriptors_;
270 std::list<int> removedIndexes_;
271};
272
273} /* namespace rtabmap */
274
275#endif /* CORELIB_SRC_FLANNINDEX_H_ */
Nearest neighbor index over a set of features.
Definition FlannIndex.h:56
bool loadIndex(const unsigned char *indexData, size_t indexDataSize, flann_algorithm_t algorithm, const cv::Mat &features, bool useDistanceL1=false, float rebalancingFactor=2.0f, std::string *errorMsg=NULL)
Load an index from a raw buffer, see the overload above.
std::vector< unsigned char > serializeIndex(bool computeChecksum=true) const
Serialize the index, to be given back to loadIndex()
void radiusSearch(const cv::Mat &query, std::vector< std::vector< size_t > > &indices, std::vector< std::vector< float > > &dists, float radius, int maxNeighbors=0, int checks=32, float eps=0.0, bool sorted=true, int cores=1) const
Search the neighbors of each query within a radius.
size_t memoryUsed() const
void release()
Drop the index and everything it holds, back to the state of a new one.
size_t indexedFeatures() const
void removePoint(unsigned int index)
Remove an indexed feature, by the index addPoints() gave for it.
int featuresDim() const
Definition FlannIndex.h:186
bool loadIndex(const std::vector< unsigned char > &indexData, flann_algorithm_t algorithm, const cv::Mat &features, bool useDistanceL1=false, float rebalancingFactor=2.0f, std::string *errorMsg=NULL)
Load an index serialized by serializeIndex(), releasing any previous one.
flann_algorithm_t
The index structure built by buildIndex()
Definition FlannIndex.h:76
std::vector< unsigned int > addPoints(const cv::Mat &features)
Add features to the index.
static void setDeterministicSeed(bool enable)
Make the randomized rtflann indexes the same from one run to another.
void buildIndex(flann_algorithm_t algorithm, const cv::Mat &features, bool useDistanceL1=false, float rebalancingFactor=2.0f)
Build the index over the given features, releasing any previous one.
int featuresType() const
Definition FlannIndex.h:184
void knnSearch(const cv::Mat &query, cv::Mat &indices, cv::Mat &dists, int knn, int checks=32, float eps=0.0, bool sorted=true, int cores=1) const
Search the k nearest neighbors of each query.