linear_index.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #ifndef OPENCV_FLANN_LINEAR_INDEX_H_
  31. #define OPENCV_FLANN_LINEAR_INDEX_H_
  32. #include "general.h"
  33. #include "nn_index.h"
  34. namespace cvflann
  35. {
  36. struct LinearIndexParams : public IndexParams
  37. {
  38. LinearIndexParams()
  39. {
  40. (* this)["algorithm"] = FLANN_INDEX_LINEAR;
  41. }
  42. };
  43. template <typename Distance>
  44. class LinearIndex : public NNIndex<Distance>
  45. {
  46. public:
  47. typedef typename Distance::ElementType ElementType;
  48. typedef typename Distance::ResultType DistanceType;
  49. LinearIndex(const Matrix<ElementType>& inputData, const IndexParams& params = LinearIndexParams(),
  50. Distance d = Distance()) :
  51. dataset_(inputData), index_params_(params), distance_(d)
  52. {
  53. }
  54. LinearIndex(const LinearIndex&);
  55. LinearIndex& operator=(const LinearIndex&);
  56. flann_algorithm_t getType() const CV_OVERRIDE
  57. {
  58. return FLANN_INDEX_LINEAR;
  59. }
  60. size_t size() const CV_OVERRIDE
  61. {
  62. return dataset_.rows;
  63. }
  64. size_t veclen() const CV_OVERRIDE
  65. {
  66. return dataset_.cols;
  67. }
  68. int usedMemory() const CV_OVERRIDE
  69. {
  70. return 0;
  71. }
  72. void buildIndex() CV_OVERRIDE
  73. {
  74. /* nothing to do here for linear search */
  75. }
  76. void saveIndex(FILE*) CV_OVERRIDE
  77. {
  78. /* nothing to do here for linear search */
  79. }
  80. void loadIndex(FILE*) CV_OVERRIDE
  81. {
  82. /* nothing to do here for linear search */
  83. index_params_["algorithm"] = getType();
  84. }
  85. void findNeighbors(ResultSet<DistanceType>& resultSet, const ElementType* vec, const SearchParams& /*searchParams*/) CV_OVERRIDE
  86. {
  87. ElementType* data = dataset_.data;
  88. for (size_t i = 0; i < dataset_.rows; ++i, data += dataset_.cols) {
  89. DistanceType dist = distance_(data, vec, dataset_.cols);
  90. resultSet.addPoint(dist, (int)i);
  91. }
  92. }
  93. IndexParams getParameters() const CV_OVERRIDE
  94. {
  95. return index_params_;
  96. }
  97. private:
  98. /** The dataset */
  99. const Matrix<ElementType> dataset_;
  100. /** Index parameters */
  101. IndexParams index_params_;
  102. /** Index distance */
  103. Distance distance_;
  104. };
  105. }
  106. #endif // OPENCV_FLANN_LINEAR_INDEX_H_