composite_index.h
Go to the documentation of this file.
00001 /***********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright 2008-2009  Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
00005  * Copyright 2008-2009  David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
00006  *
00007  * THE BSD LICENSE
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions
00011  * are met:
00012  *
00013  * 1. Redistributions of source code must retain the above copyright
00014  *    notice, this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in the
00017  *    documentation and/or other materials provided with the distribution.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00020  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00021  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00022  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00023  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00024  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00028  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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         // number of randomized trees to use (for kdtree)
00052         (*this)["trees"] = trees;
00053         // branching factor
00054         (*this)["branching"] = branching;
00055         // max iterations to perform in one kmeans clustering (kmeans tree)
00056         (*this)["iterations"] = iterations;
00057         // algorithm used for picking the initial cluster centers for kmeans tree
00058         (*this)["centers_init"] = centers_init;
00059         // cluster boundary index. Used when searching the kmeans tree
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         /* nothing to do here */
00221     }
00222 
00223     void freeIndex()
00224     {
00225         /* nothing to do here */
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_


rtabmap
Author(s): Mathieu Labbe
autogenerated on Sat Jul 23 2016 11:44:15