1 /***********************************************************************
2 * Software License Agreement (BSD License)
4 * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
5 * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *************************************************************************/
37 #include "constants.h"
51 virtual ~IndexFactory() {}
52 virtual NNIndex* createIndex(const Matrix<float>& dataset) const = 0;
55 struct IndexParams : public IndexFactory {
61 static IndexParams* createFromParameters(const FLANNParameters& p);
63 void fromParameters(const FLANNParameters&) {};
64 void toParameters(FLANNParameters&) { };
67 struct LinearIndexParams : public IndexParams {
68 LinearIndexParams() {};
70 NNIndex* createIndex(const Matrix<float>& dataset) const;
75 struct KDTreeIndexParams : public IndexParams {
76 KDTreeIndexParams(int trees_ = 4) : trees(trees_) {};
78 int trees; // number of randomized trees to use (for kdtree)
80 NNIndex* createIndex(const Matrix<float>& dataset) const;
82 void fromParameters(const FLANNParameters& p)
87 void toParameters(FLANNParameters& p)
95 struct KMeansIndexParams : public IndexParams {
96 KMeansIndexParams(int branching_ = 32, int iterations_ = 11,
97 flann_centers_init_t centers_init_ = CENTERS_RANDOM, float cb_index_ = 0.2 ) :
98 branching(branching_),
99 iterations(iterations_),
100 centers_init(centers_init_),
101 cb_index(cb_index_) {};
103 int branching; // branching factor (for kmeans tree)
104 int iterations; // max iterations to perform in one kmeans clustering (kmeans tree)
105 flann_centers_init_t centers_init; // algorithm used for picking the initial cluster centers for kmeans tree
106 float cb_index; // cluster boundary index. Used when searching the kmeans tree
109 NNIndex* createIndex(const Matrix<float>& dataset) const;
111 void fromParameters(const FLANNParameters& p)
113 branching = p.branching;
114 iterations = p.iterations;
115 centers_init = p.centers_init;
116 cb_index = p.cb_index;
119 void toParameters(FLANNParameters& p)
121 p.algorithm = KMEANS;
122 p.branching = branching;
123 p.iterations = iterations;
124 p.centers_init = centers_init;
125 p.cb_index = cb_index;
131 struct CompositeIndexParams : public IndexParams {
132 CompositeIndexParams(int trees_ = 4, int branching_ = 32, int iterations_ = 11,
133 flann_centers_init_t centers_init_ = CENTERS_RANDOM, float cb_index_ = 0.2 ) :
135 branching(branching_),
136 iterations(iterations_),
137 centers_init(centers_init_),
138 cb_index(cb_index_) {};
140 int trees; // number of randomized trees to use (for kdtree)
141 int branching; // branching factor (for kmeans tree)
142 int iterations; // max iterations to perform in one kmeans clustering (kmeans tree)
143 flann_centers_init_t centers_init; // algorithm used for picking the initial cluster centers for kmeans tree
144 float cb_index; // cluster boundary index. Used when searching the kmeans tree
146 NNIndex* createIndex(const Matrix<float>& dataset) const;
148 void fromParameters(const FLANNParameters& p)
151 branching = p.branching;
152 iterations = p.iterations;
153 centers_init = p.centers_init;
154 cb_index = p.cb_index;
157 void toParameters(FLANNParameters& p)
159 p.algorithm = COMPOSITE;
161 p.branching = branching;
162 p.iterations = iterations;
163 p.centers_init = centers_init;
164 p.cb_index = cb_index;
169 struct AutotunedIndexParams : public IndexParams {
170 AutotunedIndexParams( float target_precision_ = 0.9, float build_weight_ = 0.01,
171 float memory_weight_ = 0, float sample_fraction_ = 0.1) :
172 target_precision(target_precision_),
173 build_weight(build_weight_),
174 memory_weight(memory_weight_),
175 sample_fraction(sample_fraction_) {};
177 float target_precision; // precision desired (used for autotuning, -1 otherwise)
178 float build_weight; // build tree time weighting factor
179 float memory_weight; // index memory weighting factor
180 float sample_fraction; // what fraction of the dataset to use for autotuning
182 NNIndex* createIndex(const Matrix<float>& dataset) const;
184 void fromParameters(const FLANNParameters& p)
186 target_precision = p.target_precision;
187 build_weight = p.build_weight;
188 memory_weight = p.memory_weight;
189 sample_fraction = p.sample_fraction;
192 void toParameters(FLANNParameters& p)
194 p.algorithm = AUTOTUNED;
195 p.target_precision = target_precision;
196 p.build_weight = build_weight;
197 p.memory_weight = memory_weight;
198 p.sample_fraction = sample_fraction;
203 struct SavedIndexParams : public IndexParams {
205 throw FLANNException("I don't know which index to load");
207 SavedIndexParams(std::string filename_) : filename(filename_) {}
209 std::string filename; // filename of the stored index
211 NNIndex* createIndex(const Matrix<float>& dataset) const;
215 struct SearchParams {
216 SearchParams(int checks_ = 32) :
227 Index(const Matrix<float>& features, const IndexParams& params);
231 void knnSearch(const Matrix<float>& queries, Matrix<int>& indices, Matrix<float>& dists, int knn, const SearchParams& params);
233 int radiusSearch(const Matrix<float>& query, Matrix<int> indices, Matrix<float> dists, float radius, const SearchParams& params);
235 void save(std::string filename);
243 int hierarchicalClustering(const Matrix<float>& features, Matrix<float>& centers, const KMeansIndexParams& params);
247 #endif /* FLANN_HPP_ */