vector_of_vectors_example.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <nanoflann.hpp>
  29. using namespace nanoflann;
  30. #include <cstdlib>
  31. #include <ctime>
  32. #include <iostream>
  33. #include "KDTreeVectorOfVectorsAdaptor.h"
  34. const int SAMPLES_DIM = 15;
  35. typedef std::vector<std::vector<double>> my_vector_of_vectors_t;
  36. void generateRandomPointCloud(
  37. my_vector_of_vectors_t& samples, const size_t N, const size_t dim,
  38. const double max_range = 10.0)
  39. {
  40. std::cout << "Generating " << N << " random points...";
  41. samples.resize(N);
  42. for (size_t i = 0; i < N; i++)
  43. {
  44. samples[i].resize(dim);
  45. for (size_t d = 0; d < dim; d++)
  46. samples[i][d] = max_range * (rand() % 1000) / (1000.0);
  47. }
  48. std::cout << "done\n";
  49. }
  50. void kdtree_demo(const size_t nSamples, const size_t dim)
  51. {
  52. my_vector_of_vectors_t samples;
  53. const double max_range = 20;
  54. // Generate points:
  55. generateRandomPointCloud(samples, nSamples, dim, max_range);
  56. // Query point:
  57. std::vector<double> query_pt(dim);
  58. for (size_t d = 0; d < dim; d++)
  59. query_pt[d] = max_range * (rand() % 1000) / (1000.0);
  60. // construct a kd-tree index:
  61. // Dimensionality set at run-time (default: L2)
  62. // ------------------------------------------------------------
  63. typedef KDTreeVectorOfVectorsAdaptor<my_vector_of_vectors_t, double>
  64. my_kd_tree_t;
  65. my_kd_tree_t mat_index(dim /*dim*/, samples, 10 /* max leaf */);
  66. // do a knn search
  67. const size_t num_results = 3;
  68. std::vector<size_t> ret_indexes(num_results);
  69. std::vector<double> out_dists_sqr(num_results);
  70. nanoflann::KNNResultSet<double> resultSet(num_results);
  71. resultSet.init(&ret_indexes[0], &out_dists_sqr[0]);
  72. mat_index.index->findNeighbors(resultSet, &query_pt[0]);
  73. std::cout << "knnSearch(nn=" << num_results << "): \n";
  74. for (size_t i = 0; i < resultSet.size(); i++)
  75. std::cout << "ret_index[" << i << "]=" << ret_indexes[i]
  76. << " out_dist_sqr=" << out_dists_sqr[i] << std::endl;
  77. }
  78. int main()
  79. {
  80. // Randomize Seed
  81. srand(static_cast<unsigned int>(time(nullptr)));
  82. kdtree_demo(1000 /* samples */, SAMPLES_DIM /* dim */);
  83. }