github.com/kaydxh/golang@v0.0.131/pkg/gocv/cgo/third_path/opencv4/include/opencv2/flann/nn_index.h (about) 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_NNINDEX_H 32 #define OPENCV_FLANN_NNINDEX_H 33 34 #include "matrix.h" 35 #include "result_set.h" 36 #include "params.h" 37 38 //! @cond IGNORED 39 40 namespace cvflann 41 { 42 43 /** 44 * Nearest-neighbour index base class 45 */ 46 template <typename Distance> 47 class NNIndex 48 { 49 typedef typename Distance::ElementType ElementType; 50 typedef typename Distance::ResultType DistanceType; 51 52 public: 53 54 virtual ~NNIndex() {} 55 56 /** 57 * \brief Builds the index 58 */ 59 virtual void buildIndex() = 0; 60 61 /** 62 * \brief Perform k-nearest neighbor search 63 * \param[in] queries The query points for which to find the nearest neighbors 64 * \param[out] indices The indices of the nearest neighbors found 65 * \param[out] dists Distances to the nearest neighbors found 66 * \param[in] knn Number of nearest neighbors to return 67 * \param[in] params Search parameters 68 */ 69 virtual void knnSearch(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, int knn, const SearchParams& params) 70 { 71 CV_Assert(queries.cols == veclen()); 72 CV_Assert(indices.rows >= queries.rows); 73 CV_Assert(dists.rows >= queries.rows); 74 CV_Assert(int(indices.cols) >= knn); 75 CV_Assert(int(dists.cols) >= knn); 76 77 #if 0 78 KNNResultSet<DistanceType> resultSet(knn); 79 for (size_t i = 0; i < queries.rows; i++) { 80 resultSet.init(indices[i], dists[i]); 81 findNeighbors(resultSet, queries[i], params); 82 } 83 #else 84 KNNUniqueResultSet<DistanceType> resultSet(knn); 85 for (size_t i = 0; i < queries.rows; i++) { 86 resultSet.clear(); 87 findNeighbors(resultSet, queries[i], params); 88 if (get_param(params,"sorted",true)) resultSet.sortAndCopy(indices[i], dists[i], knn); 89 else resultSet.copy(indices[i], dists[i], knn); 90 } 91 #endif 92 } 93 94 /** 95 * \brief Perform radius search 96 * \param[in] query The query point 97 * \param[out] indices The indinces of the neighbors found within the given radius 98 * \param[out] dists The distances to the nearest neighbors found 99 * \param[in] radius The radius used for search 100 * \param[in] params Search parameters 101 * \returns Number of neighbors found 102 */ 103 virtual int radiusSearch(const Matrix<ElementType>& query, Matrix<int>& indices, Matrix<DistanceType>& dists, float radius, const SearchParams& params) 104 { 105 if (query.rows != 1) { 106 fprintf(stderr, "I can only search one feature at a time for range search\n"); 107 return -1; 108 } 109 assert(query.cols == veclen()); 110 assert(indices.cols == dists.cols); 111 112 int n = 0; 113 int* indices_ptr = NULL; 114 DistanceType* dists_ptr = NULL; 115 if (indices.cols > 0) { 116 n = (int)indices.cols; 117 indices_ptr = indices[0]; 118 dists_ptr = dists[0]; 119 } 120 121 RadiusUniqueResultSet<DistanceType> resultSet((DistanceType)radius); 122 resultSet.clear(); 123 findNeighbors(resultSet, query[0], params); 124 if (n>0) { 125 if (get_param(params,"sorted",true)) resultSet.sortAndCopy(indices_ptr, dists_ptr, n); 126 else resultSet.copy(indices_ptr, dists_ptr, n); 127 } 128 129 return (int)resultSet.size(); 130 } 131 132 /** 133 * \brief Saves the index to a stream 134 * \param stream The stream to save the index to 135 */ 136 virtual void saveIndex(FILE* stream) = 0; 137 138 /** 139 * \brief Loads the index from a stream 140 * \param stream The stream from which the index is loaded 141 */ 142 virtual void loadIndex(FILE* stream) = 0; 143 144 /** 145 * \returns number of features in this index. 146 */ 147 virtual size_t size() const = 0; 148 149 /** 150 * \returns The dimensionality of the features in this index. 151 */ 152 virtual size_t veclen() const = 0; 153 154 /** 155 * \returns The amount of memory (in bytes) used by the index. 156 */ 157 virtual int usedMemory() const = 0; 158 159 /** 160 * \returns The index type (kdtree, kmeans,...) 161 */ 162 virtual flann_algorithm_t getType() const = 0; 163 164 /** 165 * \returns The index parameters 166 */ 167 virtual IndexParams getParameters() const = 0; 168 169 170 /** 171 * \brief Method that searches for nearest-neighbours 172 */ 173 virtual void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) = 0; 174 }; 175 176 } 177 178 //! @endcond 179 180 #endif //OPENCV_FLANN_NNINDEX_H