pointcloud_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 <cstdlib>
  29. #include <ctime>
  30. #include <iostream>
  31. #include <nanoflann.hpp>
  32. #include <type_traits>
  33. #include "utils.h"
  34. template <typename num_t>
  35. void kdtree_demo(const size_t N)
  36. {
  37. static_assert(
  38. std::is_standard_layout<nanoflann::ResultItem<num_t, size_t>>::value,
  39. "Unexpected memory layout for nanoflann::ResultItem");
  40. PointCloud<num_t> cloud;
  41. // Generate points:
  42. generateRandomPointCloud(cloud, N);
  43. num_t query_pt[3] = {0.5, 0.5, 0.5};
  44. // construct a kd-tree index:
  45. using my_kd_tree_t = nanoflann::KDTreeSingleIndexAdaptor<
  46. nanoflann::L2_Simple_Adaptor<num_t, PointCloud<num_t>>,
  47. PointCloud<num_t>, 3 /* dim */
  48. >;
  49. dump_mem_usage();
  50. my_kd_tree_t index(3 /*dim*/, cloud, {10 /* max leaf */});
  51. dump_mem_usage();
  52. {
  53. // do a knn search
  54. const size_t num_results = 1;
  55. size_t ret_index;
  56. num_t out_dist_sqr;
  57. nanoflann::KNNResultSet<num_t> resultSet(num_results);
  58. resultSet.init(&ret_index, &out_dist_sqr);
  59. index.findNeighbors(resultSet, &query_pt[0]);
  60. std::cout << "knnSearch(nn=" << num_results << "): \n";
  61. std::cout << "ret_index=" << ret_index
  62. << " out_dist_sqr=" << out_dist_sqr << std::endl;
  63. }
  64. {
  65. // radius search:
  66. const num_t squaredRadius = 1;
  67. std::vector<nanoflann::ResultItem<size_t, num_t>> indices_dists;
  68. nanoflann::RadiusResultSet<num_t, size_t> resultSet(
  69. squaredRadius, indices_dists);
  70. index.findNeighbors(resultSet, query_pt);
  71. // Get worst (furthest) point, without sorting:
  72. nanoflann::ResultItem<size_t, num_t> worst_pair =
  73. resultSet.worst_item();
  74. std::cout << "Worst pair: idx=" << worst_pair.first
  75. << " squaredDist=" << worst_pair.second << std::endl;
  76. }
  77. }
  78. int main()
  79. {
  80. // Randomize Seed
  81. srand(static_cast<unsigned int>(time(nullptr)));
  82. kdtree_demo<float>(1000000);
  83. kdtree_demo<double>(1000000);
  84. return 0;
  85. }