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 {
60 static IndexParams* createFromParameters(const FLANNParameters& p);
62 void fromParameters(const FLANNParameters&) {};
63 void toParameters(FLANNParameters&) { };
66 struct LinearIndexParams : public IndexParams {
67 LinearIndexParams() {};
69 NNIndex* createIndex(const Matrix<float>& dataset) const;
74 struct KDTreeIndexParams : public IndexParams {
75 KDTreeIndexParams(int trees_ = 4) : trees(trees_) {};
77 int trees; // number of randomized trees to use (for kdtree)
79 NNIndex* createIndex(const Matrix<float>& dataset) const;
81 void fromParameters(const FLANNParameters& p)
86 void toParameters(FLANNParameters& p)
94 struct KMeansIndexParams : public IndexParams {
95 KMeansIndexParams(int branching_ = 32, int iterations_ = 11,
96 flann_centers_init_t centers_init_ = CENTERS_RANDOM, float cb_index_ = 0.2 ) :
97 branching(branching_),
98 iterations(iterations_),
99 centers_init(centers_init_),
100 cb_index(cb_index_) {};
102 int branching; // branching factor (for kmeans tree)
103 int iterations; // max iterations to perform in one kmeans clustering (kmeans tree)
104 flann_centers_init_t centers_init; // algorithm used for picking the initial cluster centers for kmeans tree
105 float cb_index; // cluster boundary index. Used when searching the kmeans tree
108 NNIndex* createIndex(const Matrix<float>& dataset) const;
110 void fromParameters(const FLANNParameters& p)
112 branching = p.branching;
113 iterations = p.iterations;
114 centers_init = p.centers_init;
115 cb_index = p.cb_index;
118 void toParameters(FLANNParameters& p)
120 p.algorithm = KMEANS;
121 p.branching = branching;
122 p.iterations = iterations;
123 p.centers_init = centers_init;
124 p.cb_index = cb_index;
130 struct CompositeIndexParams : public IndexParams {
131 CompositeIndexParams(int trees_ = 4, int branching_ = 32, int iterations_ = 11,
132 flann_centers_init_t centers_init_ = CENTERS_RANDOM, float cb_index_ = 0.2 ) :
134 branching(branching_),
135 iterations(iterations_),
136 centers_init(centers_init_),
137 cb_index(cb_index_) {};
139 int trees; // number of randomized trees to use (for kdtree)
140 int branching; // branching factor (for kmeans tree)
141 int iterations; // max iterations to perform in one kmeans clustering (kmeans tree)
142 flann_centers_init_t centers_init; // algorithm used for picking the initial cluster centers for kmeans tree
143 float cb_index; // cluster boundary index. Used when searching the kmeans tree
145 NNIndex* createIndex(const Matrix<float>& dataset) const;
147 void fromParameters(const FLANNParameters& p)
150 branching = p.branching;
151 iterations = p.iterations;
152 centers_init = p.centers_init;
153 cb_index = p.cb_index;
156 void toParameters(FLANNParameters& p)
158 p.algorithm = COMPOSITE;
160 p.branching = branching;
161 p.iterations = iterations;
162 p.centers_init = centers_init;
163 p.cb_index = cb_index;
168 struct AutotunedIndexParams : public IndexParams {
169 AutotunedIndexParams( float target_precision_ = 0.9, float build_weight_ = 0.01,
170 float memory_weight_ = 0, float sample_fraction_ = 0.1) :
171 target_precision(target_precision_),
172 build_weight(build_weight_),
173 memory_weight(memory_weight_),
174 sample_fraction(sample_fraction_) {};
176 float target_precision; // precision desired (used for autotuning, -1 otherwise)
177 float build_weight; // build tree time weighting factor
178 float memory_weight; // index memory weighting factor
179 float sample_fraction; // what fraction of the dataset to use for autotuning
181 NNIndex* createIndex(const Matrix<float>& dataset) const;
183 void fromParameters(const FLANNParameters& p)
185 target_precision = p.target_precision;
186 build_weight = p.build_weight;
187 memory_weight = p.memory_weight;
188 sample_fraction = p.sample_fraction;
191 void toParameters(FLANNParameters& p)
193 p.algorithm = AUTOTUNED;
194 p.target_precision = target_precision;
195 p.build_weight = build_weight;
196 p.memory_weight = memory_weight;
197 p.sample_fraction = sample_fraction;
202 struct SavedIndexParams : public IndexParams {
204 throw FLANNException("I don't know which index to load");
206 SavedIndexParams(std::string filename_) : filename(filename_) {}
208 std::string filename; // filename of the stored index
210 NNIndex* createIndex(const Matrix<float>& dataset) const;
214 struct SearchParams {
215 SearchParams(int checks_ = 32) :
226 Index(const Matrix<float>& features, const IndexParams& params);
230 void knnSearch(const Matrix<float>& queries, Matrix<int>& indices, Matrix<float>& dists, int knn, const SearchParams& params);
232 int radiusSearch(const Matrix<float>& query, Matrix<int> indices, Matrix<float> dists, float radius, const SearchParams& params);
234 void save(std::string filename);
242 int hierarchicalClustering(const Matrix<float>& features, Matrix<float>& centers, const KMeansIndexParams& params);
246 #endif /* FLANN_HPP_ */