pointcloud_custom_resultset.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. using num_t = double;
  35. template <typename _DistanceType, typename _IndexType = size_t>
  36. class MyCustomResultSet
  37. {
  38. public:
  39. using DistanceType = _DistanceType;
  40. using IndexType = _IndexType;
  41. public:
  42. const DistanceType radius;
  43. std::vector<nanoflann::ResultItem<IndexType, DistanceType>>&
  44. m_indices_dists;
  45. explicit MyCustomResultSet(
  46. DistanceType radius_,
  47. std::vector<nanoflann::ResultItem<IndexType, DistanceType>>&
  48. indices_dists)
  49. : radius(radius_), m_indices_dists(indices_dists)
  50. {
  51. init();
  52. }
  53. void init() { clear(); }
  54. void clear() { m_indices_dists.clear(); }
  55. size_t size() const { return m_indices_dists.size(); }
  56. size_t empty() const { return m_indices_dists.empty(); }
  57. bool full() const { return true; }
  58. /**
  59. * Called during search to add an element matching the criteria.
  60. * @return true if the search should be continued, false if the results are
  61. * sufficient
  62. */
  63. bool addPoint(DistanceType dist, IndexType index)
  64. {
  65. printf(
  66. "addPoint() called: dist=%f index=%u\n", dist,
  67. static_cast<unsigned int>(index));
  68. if (dist < radius) m_indices_dists.emplace_back(index, dist);
  69. return true;
  70. }
  71. DistanceType worstDist() const { return radius; }
  72. };
  73. void kdtree_demo(const size_t N)
  74. {
  75. PointCloud<num_t> cloud;
  76. // Generate points:
  77. generateRandomPointCloud(cloud, N);
  78. num_t query_pt[3] = {0.5, 0.5, 0.5};
  79. // construct a kd-tree index:
  80. using my_kd_tree_t = nanoflann::KDTreeSingleIndexAdaptor<
  81. nanoflann::L2_Simple_Adaptor<num_t, PointCloud<num_t>>,
  82. PointCloud<num_t>, 3 /* dim */
  83. >;
  84. my_kd_tree_t index(3 /*dim*/, cloud, {10 /* max leaf */});
  85. {
  86. // radius search:
  87. const num_t squaredRadius = 1;
  88. std::vector<nanoflann::ResultItem<size_t, num_t>> indices_dists;
  89. MyCustomResultSet<num_t, size_t> resultSet(
  90. squaredRadius, indices_dists);
  91. index.findNeighbors(resultSet, query_pt);
  92. std::cout << "Found: " << indices_dists.size() << " NN points."
  93. << std::endl;
  94. }
  95. }
  96. int main()
  97. {
  98. // Randomize Seed
  99. srand(static_cast<unsigned int>(time(nullptr)));
  100. kdtree_demo(10000);
  101. return 0;
  102. }