linear_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_LINEAR_INDEX_H_
00032 #define RTABMAP_FLANN_LINEAR_INDEX_H_
00033 
00034 #include "rtflann/general.h"
00035 #include "nn_index.h"
00036 
00037 namespace rtflann
00038 {
00039 
00040 struct LinearIndexParams : public IndexParams
00041 {
00042     LinearIndexParams()
00043     {
00044         (* this)["algorithm"] = FLANN_INDEX_LINEAR;
00045     }
00046 };
00047 
00048 template <typename Distance>
00049 class LinearIndex : public NNIndex<Distance>
00050 {
00051 public:
00052 
00053     typedef typename Distance::ElementType ElementType;
00054     typedef typename Distance::ResultType DistanceType;
00055 
00056     typedef NNIndex<Distance> BaseClass;
00057 
00058     LinearIndex(const IndexParams& params = LinearIndexParams(), Distance d = Distance()) :
00059         BaseClass(params, d)
00060     {
00061     }
00062 
00063     LinearIndex(const Matrix<ElementType>& input_data, const IndexParams& params = LinearIndexParams(), Distance d = Distance()) :
00064         BaseClass(params, d)
00065     {
00066         setDataset(input_data);
00067     }
00068 
00069     LinearIndex(const LinearIndex& other) : BaseClass(other)
00070     {
00071     }
00072 
00073     LinearIndex& operator=(LinearIndex other)
00074     {
00075         this->swap(other);
00076         return *this;
00077     }
00078 
00079     virtual ~LinearIndex()
00080     {
00081     }
00082 
00083     BaseClass* clone() const
00084     {
00085         return new LinearIndex(*this);
00086     }
00087 
00088     void addPoints(const Matrix<ElementType>& points, float rebuild_threshold = 2)
00089     {
00090         assert(points.cols==veclen_);
00091         extendDataset(points);
00092     }
00093 
00094     flann_algorithm_t getType() const
00095     {
00096         return FLANN_INDEX_LINEAR;
00097     }
00098 
00099 
00100     int usedMemory() const
00101     {
00102         return 0;
00103     }
00104 
00105     template<typename Archive>
00106     void serialize(Archive& ar)
00107     {
00108         ar.setObject(this);
00109 
00110         ar & *static_cast<NNIndex<Distance>*>(this);
00111 
00112         if (Archive::is_loading::value) {
00113             index_params_["algorithm"] = getType();
00114         }
00115     }
00116 
00117     void saveIndex(FILE* stream)
00118     {
00119         serialization::SaveArchive sa(stream);
00120         sa & *this;
00121     }
00122 
00123     void loadIndex(FILE* stream)
00124     {
00125         serialization::LoadArchive la(stream);
00126         la & *this;
00127     }
00128 
00129     void findNeighbors(ResultSet<DistanceType>& resultSet, const ElementType* vec, const SearchParams& /*searchParams*/) const
00130     {
00131         if (removed_) {
00132                 for (size_t i = 0; i < points_.size(); ++i) {
00133                         if (removed_points_.test(i)) continue;
00134                         DistanceType dist = distance_(points_[i], vec, veclen_);
00135                         resultSet.addPoint(dist, i);
00136                 }
00137         }
00138         else {
00139                 for (size_t i = 0; i < points_.size(); ++i) {
00140                         DistanceType dist = distance_(points_[i], vec, veclen_);
00141                         resultSet.addPoint(dist, i);
00142                 }
00143         }
00144     }
00145 protected:
00146     void buildIndexImpl()
00147     {
00148         /* nothing to do here for linear search */
00149     }
00150 
00151     void freeIndex()
00152     {
00153         /* nothing to do here for linear search */
00154     }
00155 
00156 private:
00157 
00158     USING_BASECLASS_SYMBOLS
00159 };
00160 
00161 }
00162 
00163 #endif // FLANN_LINEAR_INDEX_H_


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