matrix_example.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /***********************************************************************
  2. * Software License Agreement (BSD License)
  3. *
  4. * Copyright 2011-2024 Jose Luis Blanco (joseluisblancoc@gmail.com).
  5. * 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. #include <Eigen/Dense>
  29. #include <cstdlib>
  30. #include <ctime>
  31. #include <iostream>
  32. #include <nanoflann.hpp>
  33. constexpr int SAMPLES_DIM = 15;
  34. template <typename Der>
  35. void generateRandomPointCloud(
  36. Eigen::MatrixBase<Der>& mat, const size_t N, const size_t dim,
  37. const typename Der::Scalar max_range = 10)
  38. {
  39. std::cout << "Generating " << N << " random points...";
  40. mat.resize(N, dim);
  41. for (size_t i = 0; i < N; i++)
  42. for (size_t d = 0; d < dim; d++)
  43. mat(i, d) =
  44. max_range * (rand() % 1000) / typename Der::Scalar(1000);
  45. std::cout << "done\n";
  46. }
  47. template <typename num_t>
  48. void kdtree_demo(const size_t nSamples, const size_t dim)
  49. {
  50. using matrix_t = Eigen::Matrix<num_t, Eigen::Dynamic, Eigen::Dynamic>;
  51. matrix_t mat(nSamples, dim);
  52. const num_t max_range = 20;
  53. // Generate points:
  54. generateRandomPointCloud(mat, nSamples, dim, max_range);
  55. // cout << mat << endl;
  56. // Query point:
  57. std::vector<num_t> query_pt(dim);
  58. for (size_t d = 0; d < dim; d++)
  59. query_pt[d] = max_range * (rand() % 1000) / num_t(1000);
  60. // ------------------------------------------------------------
  61. // construct a kd-tree index:
  62. // Some of the different possibilities (uncomment just one)
  63. // ------------------------------------------------------------
  64. // Dimensionality set at run-time (default: L2)
  65. #if 1
  66. using my_kd_tree_t = nanoflann::KDTreeEigenMatrixAdaptor<matrix_t>;
  67. #elif 0
  68. // Dimensionality set at compile-time: Explicit selection of the distance
  69. // metric: L2
  70. using my_kd_tree_t = nanoflann::KDTreeEigenMatrixAdaptor<
  71. matrix_t, SAMPLES_DIM /*fixed size*/, nanoflann::metric_L2>;
  72. #elif 0
  73. // Dimensionality set at compile-time: Explicit selection of the distance
  74. // metric: L2_simple
  75. using my_kd_tree_t = nanoflann::KDTreeEigenMatrixAdaptor<
  76. matrix_t, SAMPLES_DIM /*fixed size*/, nanoflann::metric_L2_Simple>;
  77. #elif 0
  78. // Dimensionality set at compile-time: Explicit selection of the distance
  79. // metric: L1
  80. using my_kd_tree_t = nanoflann::KDTreeEigenMatrixAdaptor<
  81. matrix_t, SAMPLES_DIM /*fixed size*/, nanoflann::metric_L1>;
  82. #elif 0
  83. // Dimensionality set at compile-time: Explicit selection of the distance
  84. // metric: L2 Row Major matrix layout
  85. // Eigen::Matrix<num_t, Dynamic, Dynamic> mat(dim, nSamples);
  86. using my_kd_tree_t = nanoflann::KDTreeEigenMatrixAdaptor<
  87. matrix_t, SAMPLES_DIM /*fixed size*/, nanoflann::metric_L2, true>;
  88. #endif
  89. my_kd_tree_t mat_index(dim, std::cref(mat), 10 /* max leaf */);
  90. // do a knn search
  91. const size_t num_results = 3;
  92. std::vector<size_t> ret_indexes(num_results);
  93. std::vector<num_t> out_dists_sqr(num_results);
  94. nanoflann::KNNResultSet<num_t> resultSet(num_results);
  95. resultSet.init(&ret_indexes[0], &out_dists_sqr[0]);
  96. mat_index.index_->findNeighbors(resultSet, &query_pt[0]);
  97. std::cout << "knnSearch(nn=" << num_results << "): \n";
  98. for (size_t i = 0; i < resultSet.size(); i++)
  99. std::cout << "ret_index[" << i << "]=" << ret_indexes[i]
  100. << " out_dist_sqr=" << out_dists_sqr[i] << std::endl;
  101. }
  102. int main(int argc, char** argv)
  103. {
  104. // Randomize Seed
  105. // srand(static_cast<unsigned int>(time(nullptr)));
  106. kdtree_demo<float>(1000 /* samples */, SAMPLES_DIM /* dim */);
  107. return 0;
  108. }