all_indices.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #include "general.h"
  31. #include "nn_index.h"
  32. #include "kdtree_index.h"
  33. #include "kdtree_single_index.h"
  34. #include "kmeans_index.h"
  35. #include "composite_index.h"
  36. #include "linear_index.h"
  37. #include "hierarchical_clustering_index.h"
  38. #include "lsh_index.h"
  39. #include "autotuned_index.h"
  40. namespace cvflann
  41. {
  42. template<typename KDTreeCapability, typename VectorSpace, typename Distance>
  43. struct index_creator
  44. {
  45. static NNIndex<Distance>* create(const Matrix<typename Distance::ElementType>& dataset, const IndexParams& params, const Distance& distance)
  46. {
  47. flann_algorithm_t index_type = get_param<flann_algorithm_t>(params, "algorithm");
  48. NNIndex<Distance>* nnIndex;
  49. switch (index_type) {
  50. case FLANN_INDEX_LINEAR:
  51. nnIndex = new LinearIndex<Distance>(dataset, params, distance);
  52. break;
  53. case FLANN_INDEX_KDTREE_SINGLE:
  54. nnIndex = new KDTreeSingleIndex<Distance>(dataset, params, distance);
  55. break;
  56. case FLANN_INDEX_KDTREE:
  57. nnIndex = new KDTreeIndex<Distance>(dataset, params, distance);
  58. break;
  59. case FLANN_INDEX_KMEANS:
  60. nnIndex = new KMeansIndex<Distance>(dataset, params, distance);
  61. break;
  62. case FLANN_INDEX_COMPOSITE:
  63. nnIndex = new CompositeIndex<Distance>(dataset, params, distance);
  64. break;
  65. case FLANN_INDEX_AUTOTUNED:
  66. nnIndex = new AutotunedIndex<Distance>(dataset, params, distance);
  67. break;
  68. case FLANN_INDEX_HIERARCHICAL:
  69. nnIndex = new HierarchicalClusteringIndex<Distance>(dataset, params, distance);
  70. break;
  71. case FLANN_INDEX_LSH:
  72. nnIndex = new LshIndex<Distance>(dataset, params, distance);
  73. break;
  74. default:
  75. throw FLANNException("Unknown index type");
  76. }
  77. return nnIndex;
  78. }
  79. };
  80. template<typename VectorSpace, typename Distance>
  81. struct index_creator<False,VectorSpace,Distance>
  82. {
  83. static NNIndex<Distance>* create(const Matrix<typename Distance::ElementType>& dataset, const IndexParams& params, const Distance& distance)
  84. {
  85. flann_algorithm_t index_type = get_param<flann_algorithm_t>(params, "algorithm");
  86. NNIndex<Distance>* nnIndex;
  87. switch (index_type) {
  88. case FLANN_INDEX_LINEAR:
  89. nnIndex = new LinearIndex<Distance>(dataset, params, distance);
  90. break;
  91. case FLANN_INDEX_KMEANS:
  92. nnIndex = new KMeansIndex<Distance>(dataset, params, distance);
  93. break;
  94. case FLANN_INDEX_HIERARCHICAL:
  95. nnIndex = new HierarchicalClusteringIndex<Distance>(dataset, params, distance);
  96. break;
  97. case FLANN_INDEX_LSH:
  98. nnIndex = new LshIndex<Distance>(dataset, params, distance);
  99. break;
  100. default:
  101. throw FLANNException("Unknown index type");
  102. }
  103. return nnIndex;
  104. }
  105. };
  106. template<typename Distance>
  107. struct index_creator<False,False,Distance>
  108. {
  109. static NNIndex<Distance>* create(const Matrix<typename Distance::ElementType>& dataset, const IndexParams& params, const Distance& distance)
  110. {
  111. flann_algorithm_t index_type = get_param<flann_algorithm_t>(params, "algorithm");
  112. NNIndex<Distance>* nnIndex;
  113. switch (index_type) {
  114. case FLANN_INDEX_LINEAR:
  115. nnIndex = new LinearIndex<Distance>(dataset, params, distance);
  116. break;
  117. case FLANN_INDEX_HIERARCHICAL:
  118. nnIndex = new HierarchicalClusteringIndex<Distance>(dataset, params, distance);
  119. break;
  120. case FLANN_INDEX_LSH:
  121. nnIndex = new LshIndex<Distance>(dataset, params, distance);
  122. break;
  123. default:
  124. throw FLANNException("Unknown index type");
  125. }
  126. return nnIndex;
  127. }
  128. };
  129. template<typename Distance>
  130. NNIndex<Distance>* create_index_by_type(const Matrix<typename Distance::ElementType>& dataset, const IndexParams& params, const Distance& distance)
  131. {
  132. return index_creator<typename Distance::is_kdtree_distance,
  133. typename Distance::is_vector_space_distance,
  134. Distance>::create(dataset, params,distance);
  135. }
  136. }
  137. #endif /* OPENCV_FLANN_ALL_INDICES_H_ */