Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef RTABMAP_FLANN_COMPOSITE_INDEX_H_
00032 #define RTABMAP_FLANN_COMPOSITE_INDEX_H_
00033
00034 #include "rtflann/general.h"
00035 #include "rtflann/algorithms/nn_index.h"
00036 #include "rtflann/algorithms/kdtree_index.h"
00037 #include "rtflann/algorithms/kmeans_index.h"
00038
00039 namespace rtflann
00040 {
00041
00045 struct CompositeIndexParams : public IndexParams
00046 {
00047 CompositeIndexParams(int trees = 4, int branching = 32, int iterations = 11,
00048 flann_centers_init_t centers_init = FLANN_CENTERS_RANDOM, float cb_index = 0.2 )
00049 {
00050 (*this)["algorithm"] = FLANN_INDEX_KMEANS;
00051
00052 (*this)["trees"] = trees;
00053
00054 (*this)["branching"] = branching;
00055
00056 (*this)["iterations"] = iterations;
00057
00058 (*this)["centers_init"] = centers_init;
00059
00060 (*this)["cb_index"] = cb_index;
00061 }
00062 };
00063
00064
00070 template <typename Distance>
00071 class CompositeIndex : public NNIndex<Distance>
00072 {
00073 public:
00074 typedef typename Distance::ElementType ElementType;
00075 typedef typename Distance::ResultType DistanceType;
00076
00077 typedef NNIndex<Distance> BaseClass;
00078
00079 typedef bool needs_kdtree_distance;
00080
00088 CompositeIndex(const IndexParams& params = CompositeIndexParams(), Distance d = Distance()) :
00089 BaseClass(params, d)
00090 {
00091 kdtree_index_ = new KDTreeIndex<Distance>(params, d);
00092 kmeans_index_ = new KMeansIndex<Distance>(params, d);
00093
00094 }
00095
00096 CompositeIndex(const Matrix<ElementType>& inputData, const IndexParams& params = CompositeIndexParams(),
00097 Distance d = Distance()) : BaseClass(params, d)
00098 {
00099 kdtree_index_ = new KDTreeIndex<Distance>(inputData, params, d);
00100 kmeans_index_ = new KMeansIndex<Distance>(inputData, params, d);
00101 }
00102
00103 CompositeIndex(const CompositeIndex& other) : BaseClass(other),
00104 kmeans_index_(other.kmeans_index_), kdtree_index_(other.kdtree_index_)
00105 {
00106 }
00107
00108 CompositeIndex& operator=(CompositeIndex other)
00109 {
00110 this->swap(other);
00111 return *this;
00112 }
00113
00114 virtual ~CompositeIndex()
00115 {
00116 delete kdtree_index_;
00117 delete kmeans_index_;
00118 }
00119
00120 BaseClass* clone() const
00121 {
00122 return new CompositeIndex(*this);
00123 }
00124
00128 flann_algorithm_t getType() const
00129 {
00130 return FLANN_INDEX_COMPOSITE;
00131 }
00132
00136 size_t size() const
00137 {
00138 return kdtree_index_->size();
00139 }
00140
00144 size_t veclen() const
00145 {
00146 return kdtree_index_->veclen();
00147 }
00148
00152 int usedMemory() const
00153 {
00154 return kmeans_index_->usedMemory() + kdtree_index_->usedMemory();
00155 }
00156
00157 using NNIndex<Distance>::buildIndex;
00161 void buildIndex()
00162 {
00163 Logger::info("Building kmeans tree...\n");
00164 kmeans_index_->buildIndex();
00165 Logger::info("Building kdtree tree...\n");
00166 kdtree_index_->buildIndex();
00167 }
00168
00169 void addPoints(const Matrix<ElementType>& points, float rebuild_threshold = 2)
00170 {
00171 kmeans_index_->addPoints(points, rebuild_threshold);
00172 kdtree_index_->addPoints(points, rebuild_threshold);
00173 }
00174
00175 void removePoint(size_t index)
00176 {
00177 kmeans_index_->removePoint(index);
00178 kdtree_index_->removePoint(index);
00179 }
00180
00181
00186 void saveIndex(FILE* stream)
00187 {
00188 kmeans_index_->saveIndex(stream);
00189 kdtree_index_->saveIndex(stream);
00190 }
00191
00196 void loadIndex(FILE* stream)
00197 {
00198 kmeans_index_->loadIndex(stream);
00199 kdtree_index_->loadIndex(stream);
00200 }
00201
00205 void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) const
00206 {
00207 kmeans_index_->findNeighbors(result, vec, searchParams);
00208 kdtree_index_->findNeighbors(result, vec, searchParams);
00209 }
00210
00211 protected:
00212 void swap(CompositeIndex& other)
00213 {
00214 std::swap(kmeans_index_, other.kmeans_index_);
00215 std::swap(kdtree_index_, other.kdtree_index_);
00216 }
00217
00218 void buildIndexImpl()
00219 {
00220
00221 }
00222
00223 void freeIndex()
00224 {
00225
00226 }
00227
00228
00229 private:
00231 KMeansIndex<Distance>* kmeans_index_;
00232
00234 KDTreeIndex<Distance>* kdtree_index_;
00235 };
00236
00237 }
00238
00239 #endif //FLANN_COMPOSITE_INDEX_H_