pointcloud_kdd_radius.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <cstdlib>
  29. #include <ctime>
  30. #include <iostream>
  31. #include <chrono>
  32. #include <nanoflann.hpp>
  33. #include "utils.h"
  34. using namespace std;
  35. const long long pointNum = 10000000; //节点数;
  36. const int knn = 100; //k近邻;
  37. long long queryNum = 10000;
  38. double maxRange = 1000;
  39. template <typename num_t>
  40. void kdtree_demo(const size_t N)
  41. {
  42. using std::cout;
  43. using std::endl;
  44. PointCloud<num_t> cloud;
  45. // Generate points:
  46. generateRandomPointCloud<double>(cloud, N, maxRange);
  47. // construct a kd-tree index:
  48. using my_kd_tree_t = nanoflann::KDTreeSingleIndexAdaptor<
  49. nanoflann::L2_Simple_Adaptor<num_t, PointCloud<num_t>>,
  50. PointCloud<num_t>, 3 /* dim */
  51. >;
  52. auto insertStart = std::chrono::high_resolution_clock::now();
  53. my_kd_tree_t index(3 /*dim*/, cloud, {10 /* max leaf */});
  54. auto insertEnd = std::chrono::high_resolution_clock::now();
  55. std::chrono::duration<double> duration = insertEnd - insertStart;
  56. double insertTime = duration.count();
  57. cout<< "插入数据花费的时间为"<< insertTime << "秒"<<"("<<pointNum<<")"<<endl;
  58. #if 0
  59. // Test resize of dataset and rebuild of index:
  60. cloud.pts.resize(cloud.pts.size()*0.5);
  61. index.buildIndex();
  62. #endif
  63. //const num_t query_pt[3] = {0.5, 0.5, 0.5};
  64. // ----------------------------------------------------------------
  65. // knnSearch(): Perform a search for the N closest points
  66. // ----------------------------------------------------------------
  67. {
  68. auto queryStart = std::chrono::high_resolution_clock::now();
  69. size_t num_results = knn;
  70. std::vector<uint32_t> ret_index(num_results);
  71. std::vector<num_t> out_dist_sqr(num_results);
  72. for (size_t i = 0; i < queryNum; i++)
  73. {
  74. num_t query_pt[3] ;
  75. query_pt[0] = maxRange * (rand() % 1000) / 1000;
  76. query_pt[1] = maxRange * (rand() % 1000) / 1000;
  77. query_pt[2] = maxRange * (rand() % 1000) / 1000;
  78. num_results = index.knnSearch(
  79. &query_pt[0], num_results, &ret_index[0], &out_dist_sqr[0]);
  80. ret_index.resize(num_results);
  81. out_dist_sqr.resize(num_results);
  82. }
  83. auto queryEnd = std::chrono::high_resolution_clock::now();
  84. std::chrono::duration<double> duration2 = queryEnd - queryStart;
  85. double queryTime = duration2.count();
  86. cout<< "查询数据花费的时间为"<< queryTime << "秒"<<"("<<queryNum<<")"<<endl;
  87. // In case of less points in the tree than requested:
  88. /*
  89. cout << "knnSearch(): num_results=" << num_results << "\n";
  90. for (size_t i = 0; i < num_results; i++)
  91. cout << "idx[" << i << "]=" << ret_index[i] << " dist[" << i
  92. << "]=" << out_dist_sqr[i] << endl;
  93. cout << "\n";
  94. */
  95. }
  96. /*
  97. // ----------------------------------------------------------------
  98. // radiusSearch(): Perform a search for the points within search_radius
  99. // ----------------------------------------------------------------
  100. {
  101. const num_t search_radius = static_cast<num_t>(0.1);
  102. std::vector<nanoflann::ResultItem<uint32_t, num_t>> ret_matches;
  103. // nanoflanSearchParamsameters params;
  104. // params.sorted = false;
  105. const size_t nMatches =
  106. index.radiusSearch(&query_pt[0], search_radius, ret_matches);
  107. cout << "radiusSearch(): radius=" << search_radius << " -> " << nMatches
  108. << " matches\n";
  109. for (size_t i = 0; i < nMatches; i++)
  110. cout << "idx[" << i << "]=" << ret_matches[i].first << " dist[" << i
  111. << "]=" << ret_matches[i].second << endl;
  112. cout << "\n";
  113. }
  114. */
  115. }
  116. int main()
  117. {
  118. // Randomize Seed
  119. cout<<"knn="<<knn<<endl;
  120. cout<<"pointNum="<<pointNum<<endl;
  121. srand(static_cast<unsigned int>(time(nullptr)));
  122. //kdtree_demo<float>(4);
  123. kdtree_demo<double>(pointNum);
  124. return 0;
  125. }