linear_index.h
Go to the documentation of this file.
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_LINEAR_INDEX_H_
32 #define OPENCV_FLANN_LINEAR_INDEX_H_
33 
34 #include "general.h"
35 #include "nn_index.h"
36 
37 namespace cvflann
38 {
39 
41 {
43  {
44  (* this)["algorithm"] = FLANN_INDEX_LINEAR;
45  }
46 };
47 
48 template <typename Distance>
49 class LinearIndex : public NNIndex<Distance>
50 {
51 public:
52 
53  typedef typename Distance::ElementType ElementType;
54  typedef typename Distance::ResultType DistanceType;
55 
56 
58  Distance d = Distance()) :
59  dataset_(inputData), index_params_(params), distance_(d)
60  {
61  }
62 
63  LinearIndex(const LinearIndex&);
65 
67  {
68  return FLANN_INDEX_LINEAR;
69  }
70 
71 
72  size_t size() const
73  {
74  return dataset_.rows;
75  }
76 
77  size_t veclen() const
78  {
79  return dataset_.cols;
80  }
81 
82 
83  int usedMemory() const
84  {
85  return 0;
86  }
87 
88  void buildIndex()
89  {
90  /* nothing to do here for linear search */
91  }
92 
93  void saveIndex(FILE*)
94  {
95  /* nothing to do here for linear search */
96  }
97 
98 
99  void loadIndex(FILE*)
100  {
101  /* nothing to do here for linear search */
102 
103  index_params_["algorithm"] = getType();
104  }
105 
106  void findNeighbors(ResultSet<DistanceType>& resultSet, const ElementType* vec, const SearchParams& /*searchParams*/)
107  {
108  ElementType* data = dataset_.data;
109  for (size_t i = 0; i < dataset_.rows; ++i, data += dataset_.cols) {
110  DistanceType dist = distance_(data, vec, dataset_.cols);
111  resultSet.addPoint(dist, (int)i);
112  }
113  }
114 
116  {
117  return index_params_;
118  }
119 
120 private:
122  const Matrix<ElementType> dataset_;
124  IndexParams index_params_;
126  Distance distance_;
127 
128 };
129 
130 }
131 
132 #endif // OPENCV_FLANN_LINEAR_INDEX_H_
flann_algorithm_t
Definition: defines.h:81
LinearIndexParams()
Definition: linear_index.h:42
IndexParams getParameters() const
Definition: linear_index.h:115
size_t veclen() const
Definition: linear_index.h:77
Definition: linear_index.h:40
size_t cols
Definition: matrix.h:52
void buildIndex()
Builds the index.
Definition: linear_index.h:88
virtual void addPoint(DistanceType dist, int index)=0
Distance::ResultType DistanceType
Definition: linear_index.h:54
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: core_c.h:403
int d
Definition: legacy.hpp:3064
Definition: params.h:44
int usedMemory() const
Definition: linear_index.h:83
Distance::ElementType ElementType
Definition: linear_index.h:53
Definition: linear_index.h:49
void saveIndex(FILE *)
Saves the index to a stream.
Definition: linear_index.h:93
flann_algorithm_t getType() const
Definition: linear_index.h:66
size_t size() const
Definition: linear_index.h:72
Definition: result_set.h:66
std::map< std::string, any > IndexParams
Definition: params.h:42
GLenum const GLfloat * params
Definition: compat.hpp:688
T * data
Definition: matrix.h:54
LinearIndex(const Matrix< ElementType > &inputData, const IndexParams &params=LinearIndexParams(), Distance d=Distance())
Definition: linear_index.h:57
void loadIndex(FILE *)
Loads the index from a stream.
Definition: linear_index.h:99
Definition: nn_index.h:48
size_t rows
Definition: matrix.h:51
CvPoint3D64f double * dist
Definition: legacy.hpp:556
LinearIndex & operator=(const LinearIndex &)
Definition: defines.h:83
void findNeighbors(ResultSet< DistanceType > &resultSet, const ElementType *vec, const SearchParams &)
Method that searches for nearest-neighbours.
Definition: linear_index.h:106