pointcloud_adaptor_example.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // Declare custom container PointCloud<T>:
  33. #include "utils.h"
  34. void dump_mem_usage();
  35. // And this is the "dataset to kd-tree" adaptor class:
  36. template <typename Derived>
  37. struct PointCloudAdaptor
  38. {
  39. using coord_t = typename Derived::coord_t;
  40. const Derived& obj; //!< A const ref to the data set origin
  41. /// The constructor that sets the data set source
  42. PointCloudAdaptor(const Derived& obj_) : obj(obj_) {}
  43. /// CRTP helper method
  44. inline const Derived& derived() const { return obj; }
  45. // Must return the number of data points
  46. inline size_t kdtree_get_point_count() const
  47. {
  48. return derived().pts.size();
  49. }
  50. // Returns the dim'th component of the idx'th point in the class:
  51. // Since this is inlined and the "dim" argument is typically an immediate
  52. // value, the
  53. // "if/else's" are actually solved at compile time.
  54. inline coord_t kdtree_get_pt(const size_t idx, const size_t dim) const
  55. {
  56. if (dim == 0)
  57. return derived().pts[idx].x;
  58. else if (dim == 1)
  59. return derived().pts[idx].y;
  60. else
  61. return derived().pts[idx].z;
  62. }
  63. // Optional bounding-box computation: return false to default to a standard
  64. // bbox computation loop.
  65. // Return true if the BBOX was already computed by the class and returned
  66. // in "bb" so it can be avoided to redo it again. Look at bb.size() to
  67. // find out the expected dimensionality (e.g. 2 or 3 for point clouds)
  68. template <class BBOX>
  69. bool kdtree_get_bbox(BBOX& /*bb*/) const
  70. {
  71. return false;
  72. }
  73. }; // end of PointCloudAdaptor
  74. template <typename num_t>
  75. void kdtree_demo(const size_t N)
  76. {
  77. PointCloud<num_t> cloud;
  78. // Generate points:
  79. generateRandomPointCloud(cloud, N);
  80. using PC2KD = PointCloudAdaptor<PointCloud<num_t>>;
  81. const PC2KD pc2kd(cloud); // The adaptor
  82. // construct a kd-tree index:
  83. using my_kd_tree_t = nanoflann::KDTreeSingleIndexAdaptor<
  84. nanoflann::L2_Simple_Adaptor<num_t, PC2KD>, PC2KD, 3 /* dim */
  85. >;
  86. dump_mem_usage();
  87. auto do_knn_search = [](const my_kd_tree_t& index) {
  88. // do a knn search
  89. const size_t num_results = 1;
  90. size_t ret_index;
  91. num_t out_dist_sqr;
  92. nanoflann::KNNResultSet<num_t> resultSet(num_results);
  93. num_t query_pt[3] = {0.5, 0.5, 0.5};
  94. resultSet.init(&ret_index, &out_dist_sqr);
  95. index.findNeighbors(resultSet, &query_pt[0]);
  96. std::cout << "knnSearch(nn=" << num_results << "): \n";
  97. std::cout << "ret_index=" << ret_index
  98. << " out_dist_sqr=" << out_dist_sqr << std::endl;
  99. };
  100. my_kd_tree_t index1(3 /*dim*/, pc2kd, {10 /* max leaf */});
  101. my_kd_tree_t index2(3 /*dim*/, pc2kd);
  102. dump_mem_usage();
  103. do_knn_search(index1);
  104. do_knn_search(index2);
  105. }
  106. int main()
  107. {
  108. // Randomize Seed
  109. srand((unsigned int)time(NULL));
  110. kdtree_demo<float>(1000000);
  111. kdtree_demo<double>(1000000);
  112. return 0;
  113. }