dynamic_pointcloud_example.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 <nanoflann.hpp>
  32. #include "utils.h"
  33. template <typename num_t>
  34. void kdtree_demo(const size_t N)
  35. {
  36. PointCloud<num_t> cloud;
  37. // construct a kd-tree index:
  38. using my_kd_tree_t = nanoflann::KDTreeSingleIndexDynamicAdaptor<
  39. nanoflann::L2_Simple_Adaptor<num_t, PointCloud<num_t>>,
  40. PointCloud<num_t>, 3 /* dim */
  41. >;
  42. dump_mem_usage();
  43. my_kd_tree_t index(3 /*dim*/, cloud, {10 /* max leaf */});
  44. // Generate points:
  45. generateRandomPointCloud(cloud, N);
  46. num_t query_pt[3] = {0.5, 0.5, 0.5};
  47. // add points in chunks at a time
  48. size_t chunk_size = 100;
  49. for (size_t i = 0; i < N; i = i + chunk_size)
  50. {
  51. size_t end = std::min<size_t>(i + chunk_size, N - 1);
  52. // Inserts all points from [i, end]
  53. index.addPoints(i, end);
  54. }
  55. // remove a point
  56. size_t removePointIndex = N - 1;
  57. index.removePoint(removePointIndex);
  58. dump_mem_usage();
  59. {
  60. std::cout << "Searching for 1 element..." << std::endl;
  61. // do a knn search
  62. const size_t num_results = 1;
  63. size_t ret_index;
  64. num_t out_dist_sqr;
  65. nanoflann::KNNResultSet<num_t> resultSet(num_results);
  66. resultSet.init(&ret_index, &out_dist_sqr);
  67. index.findNeighbors(resultSet, query_pt, {10});
  68. std::cout << "knnSearch(nn=" << num_results << "): \n";
  69. std::cout << "ret_index=" << ret_index
  70. << " out_dist_sqr=" << out_dist_sqr << std::endl;
  71. std::cout << "point: ("
  72. << "point: (" << cloud.pts[ret_index].x << ", "
  73. << cloud.pts[ret_index].y << ", " << cloud.pts[ret_index].z
  74. << ")" << std::endl;
  75. std::cout << std::endl;
  76. }
  77. {
  78. // do a knn search searching for more than one result
  79. const size_t num_results = 5;
  80. std::cout << "Searching for " << num_results << " elements"
  81. << std::endl;
  82. size_t ret_index[num_results];
  83. num_t out_dist_sqr[num_results];
  84. nanoflann::KNNResultSet<num_t> resultSet(num_results);
  85. resultSet.init(ret_index, out_dist_sqr);
  86. index.findNeighbors(resultSet, query_pt);
  87. std::cout << "knnSearch(nn=" << num_results << "): \n";
  88. std::cout << "Results: " << std::endl;
  89. for (size_t i = 0; i < resultSet.size(); ++i)
  90. {
  91. std::cout << "#" << i << ",\t"
  92. << "index: " << ret_index[i] << ",\t"
  93. << "dist: " << out_dist_sqr[i] << ",\t"
  94. << "point: (" << cloud.pts[ret_index[i]].x << ", "
  95. << cloud.pts[ret_index[i]].y << ", "
  96. << cloud.pts[ret_index[i]].z << ")" << std::endl;
  97. }
  98. std::cout << std::endl;
  99. }
  100. {
  101. // Unsorted radius search:
  102. std::cout << "Unsorted radius search" << std::endl;
  103. const num_t radiusSqr = 1;
  104. std::vector<nanoflann::ResultItem<size_t, num_t>> indices_dists;
  105. nanoflann::RadiusResultSet<num_t, size_t> resultSet(
  106. radiusSqr, indices_dists);
  107. index.findNeighbors(resultSet, query_pt);
  108. nanoflann::ResultItem<size_t, num_t> worst_pair =
  109. resultSet.worst_item();
  110. std::cout << "Worst pair: idx=" << worst_pair.first
  111. << " dist=" << worst_pair.second << std::endl;
  112. std::cout << "point: (" << cloud.pts[worst_pair.first].x << ", "
  113. << cloud.pts[worst_pair.first].y << ", "
  114. << cloud.pts[worst_pair.first].z << ")" << std::endl;
  115. std::cout << std::endl;
  116. }
  117. }
  118. int main()
  119. {
  120. // Randomize Seed
  121. srand(static_cast<unsigned int>(time(nullptr)));
  122. kdtree_demo<float>(1000000);
  123. kdtree_demo<double>(1000000);
  124. return 0;
  125. }