RTAB-Map 0.23.11
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
89 FlannIndex();
90 virtual ~FlannIndex();
91
93 void release();
94
106 std::vector<unsigned char> serializeIndex(bool computeChecksum = true) const;
107
109 size_t indexedFeatures() const;
110
115 size_t memoryUsed() const;
116
129 flann_algorithm_t algorithm,
130 const cv::Mat & features,
131 bool useDistanceL1 = false,
132 float rebalancingFactor = 2.0f);
133
149 const std::vector<unsigned char> & indexData,
150 flann_algorithm_t algorithm,
151 const cv::Mat & features,
152 bool useDistanceL1 = false,
153 float rebalancingFactor = 2.0f,
154 std::string * errorMsg = NULL);
157 const unsigned char * indexData,
158 size_t indexDataSize,
159 flann_algorithm_t algorithm,
160 const cv::Mat & features,
161 bool useDistanceL1 = false,
162 float rebalancingFactor = 2.0f,
163 std::string * errorMsg = NULL);
164
166 bool isBuilt();
167
169 int featuresType() const {return featuresType_;}
171 int featuresDim() const {return featuresDim_;}
172
180 std::vector<unsigned int> addPoints(const cv::Mat & features);
181
189 void removePoint(unsigned int index);
190
206 const cv::Mat & query,
207 cv::Mat & indices,
208 cv::Mat & dists,
209 int knn,
210 int checks = 32,
211 float eps = 0.0,
212 bool sorted = true) const;
213
230 const cv::Mat & query,
231 std::vector<std::vector<size_t> > & indices,
232 std::vector<std::vector<float> > & dists,
233 float radius,
234 int maxNeighbors = 0,
235 int checks = 32,
236 float eps = 0.0,
237 bool sorted = true) const;
238
239private:
240 void * index_; // rtflann backend
241 NanoFlannIndex * nanoIndex_; // nanoflann backend, only one of the two is set
242 unsigned int nextIndex_;
243 int featuresType_;
244 int featuresDim_;
245 bool useDistanceL1_; // true=EUCLEDIAN_L2 false=MANHATTAN_L1
246 float rebalancingFactor_;
247 flann_algorithm_t algorithm_;
248
249 // keep feature in memory until the tree is rebuilt
250 // (in case the word is deleted when removed from the VWDictionary)
251 std::map<int, cv::Mat> addedDescriptors_;
252 std::list<int> removedIndexes_;
253};
254
255} /* namespace rtabmap */
256
257#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) 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:171
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.
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.
void knnSearch(const cv::Mat &query, cv::Mat &indices, cv::Mat &dists, int knn, int checks=32, float eps=0.0, bool sorted=true) const
Search the k nearest neighbors of each query.
int featuresType() const
Definition FlannIndex.h:169