Update to 2.0.0 tree from current Fremantle build
[opencv] / include / opencv / cxflann.h
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and/or other materials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 #ifndef CXFLANN_H_
44 #define CXFLANN_H_
45
46 #ifdef __cplusplus
47
48 namespace flann
49 {
50         class Index;
51 }
52
53 namespace cv {
54
55 namespace flann {
56
57 /* Nearest neighbor index algorithms */
58 enum flann_algorithm_t {
59         LINEAR = 0,
60         KDTREE = 1,
61         KMEANS = 2,
62         COMPOSITE = 3,
63         SAVED = 254,
64         AUTOTUNED = 255
65 };
66
67 enum flann_centers_init_t {
68         CENTERS_RANDOM = 0,
69         CENTERS_GONZALES = 1,
70         CENTERS_KMEANSPP = 2
71 };
72
73
74 enum flann_log_level_t {
75         LOG_NONE = 0,
76         LOG_FATAL = 1,
77         LOG_ERROR = 2,
78         LOG_WARN = 3,
79         LOG_INFO = 4
80 };
81
82 enum flann_distance_t {
83         EUCLIDEAN = 1,
84         MANHATTAN = 2,
85         MINKOWSKI = 3
86 };
87
88 class CV_EXPORTS IndexFactory
89 {
90 public:
91     virtual ~IndexFactory() {}
92         virtual ::flann::Index* createIndex(const Mat& dataset) const = 0;
93 };
94
95 struct CV_EXPORTS IndexParams : public IndexFactory {
96 protected:
97         IndexParams() {};
98
99 };
100
101 struct CV_EXPORTS LinearIndexParams : public IndexParams {
102         LinearIndexParams() {};
103
104         ::flann::Index* createIndex(const Mat& dataset) const;
105 };
106
107
108
109 struct CV_EXPORTS KDTreeIndexParams : public IndexParams {
110         KDTreeIndexParams(int trees_ = 4) : trees(trees_) {};
111
112         int trees;                 // number of randomized trees to use (for kdtree)
113
114         ::flann::Index* createIndex(const Mat& dataset) const;
115 };
116
117 struct CV_EXPORTS KMeansIndexParams : public IndexParams {
118         KMeansIndexParams(int branching_ = 32, int iterations_ = 11,
119                         flann_centers_init_t centers_init_ = CENTERS_RANDOM, float cb_index_ = 0.2 ) :
120                 branching(branching_),
121                 iterations(iterations_),
122                 centers_init(centers_init_),
123                 cb_index(cb_index_) {};
124
125         int branching;             // branching factor (for kmeans tree)
126         int iterations;            // max iterations to perform in one kmeans clustering (kmeans tree)
127         flann_centers_init_t centers_init;          // algorithm used for picking the initial cluster centers for kmeans tree
128     float cb_index;            // cluster boundary index. Used when searching the kmeans tree
129
130     ::flann::Index* createIndex(const Mat& dataset) const;
131 };
132
133
134 struct CV_EXPORTS CompositeIndexParams : public IndexParams {
135         CompositeIndexParams(int trees_ = 4, int branching_ = 32, int iterations_ = 11,
136                         flann_centers_init_t centers_init_ = CENTERS_RANDOM, float cb_index_ = 0.2 ) :
137                 trees(trees_),
138                 branching(branching_),
139                 iterations(iterations_),
140                 centers_init(centers_init_),
141                 cb_index(cb_index_) {};
142
143         int trees;                 // number of randomized trees to use (for kdtree)
144         int branching;             // branching factor (for kmeans tree)
145         int iterations;            // max iterations to perform in one kmeans clustering (kmeans tree)
146         flann_centers_init_t centers_init;          // algorithm used for picking the initial cluster centers for kmeans tree
147     float cb_index;            // cluster boundary index. Used when searching the kmeans tree
148
149     ::flann::Index* createIndex(const Mat& dataset) const;
150 };
151
152
153 struct CV_EXPORTS AutotunedIndexParams : public IndexParams {
154         AutotunedIndexParams( float target_precision_ = 0.9, float build_weight_ = 0.01,
155                         float memory_weight_ = 0, float sample_fraction_ = 0.1) :
156                 target_precision(target_precision_),
157                 build_weight(build_weight_),
158                 memory_weight(memory_weight_),
159                 sample_fraction(sample_fraction_) {};
160
161         float target_precision;    // precision desired (used for autotuning, -1 otherwise)
162         float build_weight;        // build tree time weighting factor
163         float memory_weight;       // index memory weighting factor
164     float sample_fraction;     // what fraction of the dataset to use for autotuning
165
166     ::flann::Index* createIndex(const Mat& dataset) const;
167 };
168
169
170 struct CV_EXPORTS SavedIndexParams : public IndexParams {
171         SavedIndexParams() {}
172         SavedIndexParams(std::string filename_) : filename(filename_) {}
173
174         std::string filename;           // filename of the stored index
175
176         ::flann::Index* createIndex(const Mat& dataset) const;
177 };
178
179
180 struct CV_EXPORTS SearchParams {
181         SearchParams(int checks_ = 32) :
182                 checks(checks_) {};
183
184         int checks;
185 };
186
187
188
189 class CV_EXPORTS Index {
190         ::flann::Index* nnIndex;
191
192 public:
193         Index(const Mat& features, const IndexParams& params);
194
195         ~Index();
196
197         void knnSearch(const vector<float>& queries, vector<int>& indices, vector<float>& dists, int knn, const SearchParams& params);
198         void knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const SearchParams& params);
199
200         int radiusSearch(const vector<float>& query, vector<int>& indices, vector<float>& dists, float radius, const SearchParams& params);
201         int radiusSearch(const Mat& query, Mat& indices, Mat& dists, float radius, const SearchParams& params);
202
203         void save(std::string filename);
204
205         int veclen() const;
206
207         int size() const;
208 };
209
210
211 CV_EXPORTS int hierarchicalClustering(const Mat& features, Mat& centers,
212                                       const KMeansIndexParams& params);
213
214 }
215
216 }
217
218 #endif // __cplusplus
219
220 #endif /* CXFLANN_H_ */