include/opencv2/flann/nn_index.h
Go to the documentation of this file.
00001 /***********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright 2008-2009  Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
00005  * Copyright 2008-2009  David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
00006  *
00007  * THE BSD LICENSE
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions
00011  * are met:
00012  *
00013  * 1. Redistributions of source code must retain the above copyright
00014  *    notice, this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in the
00017  *    documentation and/or other materials provided with the distribution.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00020  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00021  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00022  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00023  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00024  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00028  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *************************************************************************/
00030 
00031 #ifndef OPENCV_FLANN_NNINDEX_H
00032 #define OPENCV_FLANN_NNINDEX_H
00033 
00034 #include <string>
00035 
00036 #include "general.h"
00037 #include "matrix.h"
00038 #include "result_set.h"
00039 #include "params.h"
00040 
00041 namespace cvflann
00042 {
00043 
00047 template <typename Distance>
00048 class NNIndex
00049 {
00050     typedef typename Distance::ElementType ElementType;
00051     typedef typename Distance::ResultType DistanceType;
00052 
00053 public:
00054 
00055     virtual ~NNIndex() {}
00056 
00060     virtual void buildIndex() = 0;
00061 
00070     virtual void knnSearch(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, int knn, const SearchParams& params)
00071     {
00072         assert(queries.cols == veclen());
00073         assert(indices.rows >= queries.rows);
00074         assert(dists.rows >= queries.rows);
00075         assert(int(indices.cols) >= knn);
00076         assert(int(dists.cols) >= knn);
00077 
00078 #if 0
00079         KNNResultSet<DistanceType> resultSet(knn);
00080         for (size_t i = 0; i < queries.rows; i++) {
00081             resultSet.init(indices[i], dists[i]);
00082             findNeighbors(resultSet, queries[i], params);
00083         }
00084 #else
00085         KNNUniqueResultSet<DistanceType> resultSet(knn);
00086         for (size_t i = 0; i < queries.rows; i++) {
00087             resultSet.clear();
00088             findNeighbors(resultSet, queries[i], params);
00089             if (get_param(params,"sorted",true)) resultSet.sortAndCopy(indices[i], dists[i], knn);
00090             else resultSet.copy(indices[i], dists[i], knn);
00091         }
00092 #endif
00093     }
00094 
00104     virtual int radiusSearch(const Matrix<ElementType>& query, Matrix<int>& indices, Matrix<DistanceType>& dists, float radius, const SearchParams& params)
00105     {
00106         if (query.rows != 1) {
00107             fprintf(stderr, "I can only search one feature at a time for range search\n");
00108             return -1;
00109         }
00110         assert(query.cols == veclen());
00111         assert(indices.cols == dists.cols);
00112 
00113         int n = 0;
00114         int* indices_ptr = NULL;
00115         DistanceType* dists_ptr = NULL;
00116         if (indices.cols > 0) {
00117             n = (int)indices.cols;
00118             indices_ptr = indices[0];
00119             dists_ptr = dists[0];
00120         }
00121 
00122         RadiusUniqueResultSet<DistanceType> resultSet((DistanceType)radius);
00123         resultSet.clear();
00124         findNeighbors(resultSet, query[0], params);
00125         if (n>0) {
00126             if (get_param(params,"sorted",true)) resultSet.sortAndCopy(indices_ptr, dists_ptr, n);
00127             else resultSet.copy(indices_ptr, dists_ptr, n);
00128         }
00129 
00130         return (int)resultSet.size();
00131     }
00132 
00137     virtual void saveIndex(FILE* stream) = 0;
00138 
00143     virtual void loadIndex(FILE* stream) = 0;
00144 
00148     virtual size_t size() const = 0;
00149 
00153     virtual size_t veclen() const = 0;
00154 
00158     virtual int usedMemory() const = 0;
00159 
00163     virtual flann_algorithm_t getType() const = 0;
00164 
00168     virtual IndexParams getParameters() const = 0;
00169 
00170 
00174     virtual void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) = 0;
00175 };
00176 
00177 }
00178 
00179 #endif //OPENCV_FLANN_NNINDEX_H