pointcloud_example.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // Generate points:
  38. generateRandomPointCloud(cloud, N);
  39. num_t query_pt[3] = {0.5, 0.5, 0.5};
  40. // construct a kd-tree index:
  41. using my_kd_tree_t = nanoflann::KDTreeSingleIndexAdaptor<
  42. nanoflann::L2_Simple_Adaptor<num_t, PointCloud<num_t>>,
  43. PointCloud<num_t>, 3 /* dim */
  44. >;
  45. dump_mem_usage();
  46. my_kd_tree_t index(3 /*dim*/, cloud, {10 /* max leaf */});
  47. dump_mem_usage();
  48. {
  49. // do a knn search
  50. const size_t num_results = 1;
  51. size_t ret_index;
  52. num_t out_dist_sqr;
  53. nanoflann::KNNResultSet<num_t> resultSet(num_results);
  54. resultSet.init(&ret_index, &out_dist_sqr);
  55. index.findNeighbors(
  56. resultSet, &query_pt[0], nanoflann::SearchParams(10));
  57. std::cout << "knnSearch(nn=" << num_results << "): \n";
  58. std::cout << "ret_index=" << ret_index
  59. << " out_dist_sqr=" << out_dist_sqr << std::endl;
  60. }
  61. {
  62. // Unsorted radius search:
  63. const num_t radius = 1;
  64. std::vector<std::pair<size_t, num_t>> indices_dists;
  65. nanoflann::RadiusResultSet<num_t, size_t> resultSet(
  66. radius, indices_dists);
  67. index.findNeighbors(resultSet, query_pt, nanoflann::SearchParams());
  68. // Get worst (furthest) point, without sorting:
  69. std::pair<size_t, num_t> worst_pair = resultSet.worst_item();
  70. std::cout << "Worst pair: idx=" << worst_pair.first
  71. << " dist=" << worst_pair.second << std::endl;
  72. }
  73. }
  74. int main()
  75. {
  76. // Randomize Seed
  77. srand(static_cast<unsigned int>(time(nullptr)));
  78. kdtree_demo<float>(1000000);
  79. kdtree_demo<double>(1000000);
  80. return 0;
  81. }