EstervQrCode 1.1.1
Library for qr code manipulation
composite_index.h
1 /***********************************************************************
2  * Software License Agreement (BSD License)
3  *
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.
6  *
7  * THE BSD LICENSE
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
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.
18  *
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  *************************************************************************/
30 
31 #ifndef OPENCV_FLANN_COMPOSITE_INDEX_H_
32 #define OPENCV_FLANN_COMPOSITE_INDEX_H_
33 
35 
36 #include "nn_index.h"
37 #include "kdtree_index.h"
38 #include "kmeans_index.h"
39 
40 namespace cvflann
41 {
42 
46 struct CompositeIndexParams : public IndexParams
47 {
48  CompositeIndexParams(int trees = 4, int branching = 32, int iterations = 11,
49  flann_centers_init_t centers_init = FLANN_CENTERS_RANDOM, float cb_index = 0.2 )
50  {
51  (*this)["algorithm"] = FLANN_INDEX_KMEANS;
52  // number of randomized trees to use (for kdtree)
53  (*this)["trees"] = trees;
54  // branching factor
55  (*this)["branching"] = branching;
56  // max iterations to perform in one kmeans clustering (kmeans tree)
57  (*this)["iterations"] = iterations;
58  // algorithm used for picking the initial cluster centers for kmeans tree
59  (*this)["centers_init"] = centers_init;
60  // cluster boundary index. Used when searching the kmeans tree
61  (*this)["cb_index"] = cb_index;
62  }
63 };
64 
65 
71 template <typename Distance>
72 class CompositeIndex : public NNIndex<Distance>
73 {
74 public:
75  typedef typename Distance::ElementType ElementType;
76  typedef typename Distance::ResultType DistanceType;
77 
84  CompositeIndex(const Matrix<ElementType>& inputData, const IndexParams& params = CompositeIndexParams(),
85  Distance d = Distance()) : index_params_(params)
86  {
87  kdtree_index_ = new KDTreeIndex<Distance>(inputData, params, d);
88  kmeans_index_ = new KMeansIndex<Distance>(inputData, params, d);
89 
90  }
91 
92  CompositeIndex(const CompositeIndex&);
93  CompositeIndex& operator=(const CompositeIndex&);
94 
95  virtual ~CompositeIndex()
96  {
97  delete kdtree_index_;
98  delete kmeans_index_;
99  }
100 
104  flann_algorithm_t getType() const CV_OVERRIDE
105  {
106  return FLANN_INDEX_COMPOSITE;
107  }
108 
112  size_t size() const CV_OVERRIDE
113  {
114  return kdtree_index_->size();
115  }
116 
120  size_t veclen() const CV_OVERRIDE
121  {
122  return kdtree_index_->veclen();
123  }
124 
128  int usedMemory() const CV_OVERRIDE
129  {
130  return kmeans_index_->usedMemory() + kdtree_index_->usedMemory();
131  }
132 
136  void buildIndex() CV_OVERRIDE
137  {
138  Logger::info("Building kmeans tree...\n");
139  kmeans_index_->buildIndex();
140  Logger::info("Building kdtree tree...\n");
141  kdtree_index_->buildIndex();
142  }
143 
148  void saveIndex(FILE* stream) CV_OVERRIDE
149  {
150  kmeans_index_->saveIndex(stream);
151  kdtree_index_->saveIndex(stream);
152  }
153 
158  void loadIndex(FILE* stream) CV_OVERRIDE
159  {
160  kmeans_index_->loadIndex(stream);
161  kdtree_index_->loadIndex(stream);
162  }
163 
167  IndexParams getParameters() const CV_OVERRIDE
168  {
169  return index_params_;
170  }
171 
175  void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) CV_OVERRIDE
176  {
177  kmeans_index_->findNeighbors(result, vec, searchParams);
178  kdtree_index_->findNeighbors(result, vec, searchParams);
179  }
180 
181 private:
183  KMeansIndex<Distance>* kmeans_index_;
184 
186  KDTreeIndex<Distance>* kdtree_index_;
187 
189  const IndexParams index_params_;
190 };
191 
192 }
193 
195 
196 #endif //OPENCV_FLANN_COMPOSITE_INDEX_H_
CvSize size
Definition: core_c.h:112
const CvArr const CvArr CvArr * result
Definition: core_c.h:1423
#define CV_OVERRIDE
Definition: cvdef.h:792
Definition: flann.hpp:60