all_indices.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *************************************************************************/
  28. #ifndef OPENCV_FLANN_ALL_INDICES_H_
  29. #define OPENCV_FLANN_ALL_INDICES_H_
  30. //! @cond IGNORED
  31. #include "general.h"
  32. #include "nn_index.h"
  33. #include "kdtree_index.h"
  34. #include "kdtree_single_index.h"
  35. #include "kmeans_index.h"
  36. #include "composite_index.h"
  37. #include "linear_index.h"
  38. #include "hierarchical_clustering_index.h"
  39. #include "lsh_index.h"
  40. #include "autotuned_index.h"
  41. namespace cvflann
  42. {
  43. template<typename KDTreeCapability, typename VectorSpace, typename Distance>
  44. struct index_creator
  45. {
  46. static NNIndex<Distance>* create(const Matrix<typename Distance::ElementType>& dataset, const IndexParams& params, const Distance& distance)
  47. {
  48. flann_algorithm_t index_type = get_param<flann_algorithm_t>(params, "algorithm");
  49. NNIndex<Distance>* nnIndex;
  50. switch (index_type) {
  51. case FLANN_INDEX_LINEAR:
  52. nnIndex = new LinearIndex<Distance>(dataset, params, distance);
  53. break;
  54. case FLANN_INDEX_KDTREE_SINGLE:
  55. nnIndex = new KDTreeSingleIndex<Distance>(dataset, params, distance);
  56. break;
  57. case FLANN_INDEX_KDTREE:
  58. nnIndex = new KDTreeIndex<Distance>(dataset, params, distance);
  59. break;
  60. case FLANN_INDEX_KMEANS:
  61. nnIndex = new KMeansIndex<Distance>(dataset, params, distance);
  62. break;
  63. case FLANN_INDEX_COMPOSITE:
  64. nnIndex = new CompositeIndex<Distance>(dataset, params, distance);
  65. break;
  66. case FLANN_INDEX_AUTOTUNED:
  67. nnIndex = new AutotunedIndex<Distance>(dataset, params, distance);
  68. break;
  69. case FLANN_INDEX_HIERARCHICAL:
  70. nnIndex = new HierarchicalClusteringIndex<Distance>(dataset, params, distance);
  71. break;
  72. case FLANN_INDEX_LSH:
  73. nnIndex = new LshIndex<Distance>(dataset, params, distance);
  74. break;
  75. default:
  76. FLANN_THROW(cv::Error::StsBadArg, "Unknown index type");
  77. }
  78. return nnIndex;
  79. }
  80. };
  81. template<typename VectorSpace, typename Distance>
  82. struct index_creator<False,VectorSpace,Distance>
  83. {
  84. static NNIndex<Distance>* create(const Matrix<typename Distance::ElementType>& dataset, const IndexParams& params, const Distance& distance)
  85. {
  86. flann_algorithm_t index_type = get_param<flann_algorithm_t>(params, "algorithm");
  87. NNIndex<Distance>* nnIndex;
  88. switch (index_type) {
  89. case FLANN_INDEX_LINEAR:
  90. nnIndex = new LinearIndex<Distance>(dataset, params, distance);
  91. break;
  92. case FLANN_INDEX_KMEANS:
  93. nnIndex = new KMeansIndex<Distance>(dataset, params, distance);
  94. break;
  95. case FLANN_INDEX_HIERARCHICAL:
  96. nnIndex = new HierarchicalClusteringIndex<Distance>(dataset, params, distance);
  97. break;
  98. case FLANN_INDEX_LSH:
  99. nnIndex = new LshIndex<Distance>(dataset, params, distance);
  100. break;
  101. default:
  102. FLANN_THROW(cv::Error::StsBadArg, "Unknown index type");
  103. }
  104. return nnIndex;
  105. }
  106. };
  107. template<typename Distance>
  108. struct index_creator<False,False,Distance>
  109. {
  110. static NNIndex<Distance>* create(const Matrix<typename Distance::ElementType>& dataset, const IndexParams& params, const Distance& distance)
  111. {
  112. flann_algorithm_t index_type = get_param<flann_algorithm_t>(params, "algorithm");
  113. NNIndex<Distance>* nnIndex;
  114. switch (index_type) {
  115. case FLANN_INDEX_LINEAR:
  116. nnIndex = new LinearIndex<Distance>(dataset, params, distance);
  117. break;
  118. case FLANN_INDEX_KMEANS:
  119. nnIndex = new KMeansIndex<Distance>(dataset, params, distance);
  120. break;
  121. case FLANN_INDEX_HIERARCHICAL:
  122. nnIndex = new HierarchicalClusteringIndex<Distance>(dataset, params, distance);
  123. break;
  124. case FLANN_INDEX_LSH:
  125. nnIndex = new LshIndex<Distance>(dataset, params, distance);
  126. break;
  127. default:
  128. FLANN_THROW(cv::Error::StsBadArg, "Unknown index type");
  129. }
  130. return nnIndex;
  131. }
  132. };
  133. template<typename Distance>
  134. NNIndex<Distance>* create_index_by_type(const Matrix<typename Distance::ElementType>& dataset, const IndexParams& params, const Distance& distance)
  135. {
  136. return index_creator<typename Distance::is_kdtree_distance,
  137. typename Distance::is_vector_space_distance,
  138. Distance>::create(dataset, params,distance);
  139. }
  140. }
  141. //! @endcond
  142. #endif /* OPENCV_FLANN_ALL_INDICES_H_ */