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 _OPENCV_MINIFLANN_HPP_
44 #define _OPENCV_MINIFLANN_HPP_
45 
46 #include "opencv2/core.hpp"
47 #include "opencv2/flann/defines.h"
48 
49 namespace cv
50 {
51 
52 namespace flann
53 {
54 
55 struct CV_EXPORTS IndexParams
56 {
57     IndexParams();
58     ~IndexParams();
59 
60     String getString(const String& key, const String& defaultVal=String()) const;
61     int getInt(const String& key, int defaultVal=-1) const;
62     double getDouble(const String& key, double defaultVal=-1) const;
63 
64     void setString(const String& key, const String& value);
65     void setInt(const String& key, int value);
66     void setDouble(const String& key, double value);
67     void setFloat(const String& key, float value);
68     void setBool(const String& key, bool value);
69     void setAlgorithm(int value);
70 
71     void getAll(std::vector<String>& names,
72                 std::vector<int>& types,
73                 std::vector<String>& strValues,
74                 std::vector<double>& numValues) const;
75 
76     void* params;
77 };
78 
79 struct CV_EXPORTS KDTreeIndexParams : public IndexParams
80 {
81     KDTreeIndexParams(int trees=4);
82 };
83 
84 struct CV_EXPORTS LinearIndexParams : public IndexParams
85 {
86     LinearIndexParams();
87 };
88 
89 struct CV_EXPORTS CompositeIndexParams : public IndexParams
90 {
91     CompositeIndexParams(int trees = 4, int branching = 32, int iterations = 11,
92                          cvflann::flann_centers_init_t centers_init = cvflann::FLANN_CENTERS_RANDOM, float cb_index = 0.2f );
93 };
94 
95 struct CV_EXPORTS AutotunedIndexParams : public IndexParams
96 {
97     AutotunedIndexParams(float target_precision = 0.8f, float build_weight = 0.01f,
98                          float memory_weight = 0, float sample_fraction = 0.1f);
99 };
100 
101 struct CV_EXPORTS HierarchicalClusteringIndexParams : public IndexParams
102 {
103     HierarchicalClusteringIndexParams(int branching = 32,
104                       cvflann::flann_centers_init_t centers_init = cvflann::FLANN_CENTERS_RANDOM, int trees = 4, int leaf_size = 100 );
105 };
106 
107 struct CV_EXPORTS KMeansIndexParams : public IndexParams
108 {
109     KMeansIndexParams(int branching = 32, int iterations = 11,
110                       cvflann::flann_centers_init_t centers_init = cvflann::FLANN_CENTERS_RANDOM, float cb_index = 0.2f );
111 };
112 
113 struct CV_EXPORTS LshIndexParams : public IndexParams
114 {
115     LshIndexParams(int table_number, int key_size, int multi_probe_level);
116 };
117 
118 struct CV_EXPORTS SavedIndexParams : public IndexParams
119 {
120     SavedIndexParams(const String& filename);
121 };
122 
123 struct CV_EXPORTS SearchParams : public IndexParams
124 {
125     SearchParams( int checks = 32, float eps = 0, bool sorted = true );
126 };
127 
128 class CV_EXPORTS_W Index
129 {
130 public:
131     CV_WRAP Index();
132     CV_WRAP Index(InputArray features, const IndexParams& params, cvflann::flann_distance_t distType=cvflann::FLANN_DIST_L2);
133     virtual ~Index();
134 
135     CV_WRAP virtual void build(InputArray features, const IndexParams& params, cvflann::flann_distance_t distType=cvflann::FLANN_DIST_L2);
136     CV_WRAP virtual void knnSearch(InputArray query, OutputArray indices,
137                    OutputArray dists, int knn, const SearchParams& params=SearchParams());
138 
139     CV_WRAP virtual int radiusSearch(InputArray query, OutputArray indices,
140                              OutputArray dists, double radius, int maxResults,
141                              const SearchParams& params=SearchParams());
142 
143     CV_WRAP virtual void save(const String& filename) const;
144     CV_WRAP virtual bool load(InputArray features, const String& filename);
145     CV_WRAP virtual void release();
146     CV_WRAP cvflann::flann_distance_t getDistance() const;
147     CV_WRAP cvflann::flann_algorithm_t getAlgorithm() const;
148 
149 protected:
150     cvflann::flann_distance_t distType;
151     cvflann::flann_algorithm_t algo;
152     int featureType;
153     void* index;
154 };
155 
156 } } // namespace cv::flann
157 
158 #endif
159