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 
40 struct LinearIndexParams : public IndexParams
41 {
LinearIndexParamsLinearIndexParams42     LinearIndexParams()
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 
57     LinearIndex(const Matrix<ElementType>& inputData, const IndexParams& params = LinearIndexParams(),
58                 Distance d = Distance()) :
dataset_(inputData)59         dataset_(inputData), index_params_(params), distance_(d)
60     {
61     }
62 
63     LinearIndex(const LinearIndex&);
64     LinearIndex& operator=(const LinearIndex&);
65 
getType()66     flann_algorithm_t getType() const
67     {
68         return FLANN_INDEX_LINEAR;
69     }
70 
71 
size()72     size_t size() const
73     {
74         return dataset_.rows;
75     }
76 
veclen()77     size_t veclen() const
78     {
79         return dataset_.cols;
80     }
81 
82 
usedMemory()83     int usedMemory() const
84     {
85         return 0;
86     }
87 
buildIndex()88     void buildIndex()
89     {
90         /* nothing to do here for linear search */
91     }
92 
saveIndex(FILE *)93     void saveIndex(FILE*)
94     {
95         /* nothing to do here for linear search */
96     }
97 
98 
loadIndex(FILE *)99     void loadIndex(FILE*)
100     {
101         /* nothing to do here for linear search */
102 
103         index_params_["algorithm"] = getType();
104     }
105 
findNeighbors(ResultSet<DistanceType> & resultSet,const ElementType * vec,const SearchParams &)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 
getParameters()115     IndexParams getParameters() const
116     {
117         return index_params_;
118     }
119 
120 private:
121     /** The dataset */
122     const Matrix<ElementType> dataset_;
123     /** Index parameters */
124     IndexParams index_params_;
125     /** Index distance */
126     Distance distance_;
127 
128 };
129 
130 }
131 
132 #endif // OPENCV_FLANN_LINEAR_INDEX_H_
133