RTAB-Map 0.23.10
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 RTABMAP_CORE_EXPORT FlannIndex
38{
39public:
40 // A forward of the internal enum, indexes should match. See src/rtflann/defines.h
41 enum flann_algorithm_t
42 {
43 FLANN_INDEX_LINEAR = 0,
44 FLANN_INDEX_KDTREE = 1,
45 FLANN_INDEX_KDTREE_SINGLE = 4,
46 FLANN_INDEX_LSH = 6,
47 };
48
49 FlannIndex();
50 virtual ~FlannIndex();
51
52 void release();
53 std::vector<unsigned char> serializeIndex(bool computeChecksum = true) const;
54
55 size_t indexedFeatures() const;
56
57 // return Bytes
58 size_t memoryUsed() const;
59
60 // Note that useDistanceL1 doesn't have any effect if LSH is used
61 void buildIndex(
62 flann_algorithm_t algorithm,
63 const cv::Mat & features,
64 bool useDistanceL1 = false,
65 float rebalancingFactor = 2.0f);
66 // Return false if the indexData doesn't correspond to expected features used and parameters.
67 bool loadIndex(
68 const std::vector<unsigned char> & indexData,
69 flann_algorithm_t algorithm,
70 const cv::Mat & features,
71 bool useDistanceL1 = false,
72 float rebalancingFactor = 2.0f,
73 std::string * errorMsg = NULL);
74 bool loadIndex(
75 const unsigned char * indexData,
76 size_t indexDataSize,
77 flann_algorithm_t algorithm,
78 const cv::Mat & features,
79 bool useDistanceL1 = false,
80 float rebalancingFactor = 2.0f,
81 std::string * errorMsg = NULL);
82
83 bool isBuilt();
84
85 int featuresType() const {return featuresType_;}
86 int featuresDim() const {return featuresDim_;}
87
88 std::vector<unsigned int> addPoints(const cv::Mat & features);
89
90 void removePoint(unsigned int index);
91
92 // return squared distances (indices should be casted in size_t)
93 void knnSearch(
94 const cv::Mat & query,
95 cv::Mat & indices,
96 cv::Mat & dists,
97 int knn,
98 int checks = 32,
99 float eps = 0.0,
100 bool sorted = true) const;
101
102 // return squared distances
103 void radiusSearch(
104 const cv::Mat & query,
105 std::vector<std::vector<size_t> > & indices,
106 std::vector<std::vector<float> > & dists,
107 float radius,
108 int maxNeighbors = 0,
109 int checks = 32,
110 float eps = 0.0,
111 bool sorted = true) const;
112
113private:
114 void * index_;
115 unsigned int nextIndex_;
116 int featuresType_;
117 int featuresDim_;
118 bool useDistanceL1_; // true=EUCLEDIAN_L2 false=MANHATTAN_L1
119 float rebalancingFactor_;
120 flann_algorithm_t algorithm_;
121
122 // keep feature in memory until the tree is rebuilt
123 // (in case the word is deleted when removed from the VWDictionary)
124 std::map<int, cv::Mat> addedDescriptors_;
125 std::list<int> removedIndexes_;
126};
127
128} /* namespace rtabmap */
129
130#endif /* CORELIB_SRC_FLANNINDEX_H_ */