pointcloud_custom_metric.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. using namespace std;
  34. using namespace nanoflann;
  35. // This example demonstrate how to embed a custom parameter "myParam" into
  36. // the metric class My_Custom_Metric_Adaptor, whose constructor accepts
  37. // arbitrary parameters:
  38. template <
  39. class T, class DataSource, typename _DistanceType = T,
  40. typename IndexType = uint32_t>
  41. struct My_Custom_Metric_Adaptor
  42. {
  43. using ElementType = T;
  44. using DistanceType = _DistanceType;
  45. const DataSource& data_source;
  46. double _myParam = 1.0;
  47. My_Custom_Metric_Adaptor(const DataSource& _data_source, double myParam)
  48. : data_source(_data_source), _myParam(myParam)
  49. {
  50. }
  51. inline DistanceType evalMetric(
  52. const T* a, const IndexType b_idx, size_t size) const
  53. {
  54. DistanceType result = DistanceType();
  55. for (size_t i = 0; i < size; ++i)
  56. {
  57. const DistanceType diff =
  58. a[i] - data_source.kdtree_get_pt(b_idx, i);
  59. result += std::pow(diff, _myParam);
  60. }
  61. return result;
  62. }
  63. template <typename U, typename V>
  64. inline DistanceType accum_dist(const U a, const V b, const size_t) const
  65. {
  66. return std::pow((a - b), _myParam);
  67. }
  68. };
  69. static void kdtree_custom_metric_demo(const size_t N)
  70. {
  71. using num_t = double;
  72. PointCloud<num_t> cloud;
  73. // Generate points:
  74. generateRandomPointCloud(cloud, N);
  75. num_t query_pt[3] = {0.5, 0.5, 0.5};
  76. // construct a kd-tree index:
  77. using my_kd_tree_t = KDTreeSingleIndexAdaptor<
  78. My_Custom_Metric_Adaptor<num_t, PointCloud<num_t>>, PointCloud<num_t>,
  79. 3 /* dim */
  80. >;
  81. dump_mem_usage();
  82. // This will be forwarded to the metric class:
  83. const double myMetricParam = 4.0;
  84. my_kd_tree_t index(3 /*dim*/, cloud, {10 /* max leaf */}, myMetricParam);
  85. dump_mem_usage();
  86. {
  87. // do a knn search
  88. const size_t num_results = 1;
  89. size_t ret_index;
  90. num_t out_dist_sqr;
  91. nanoflann::KNNResultSet<num_t> resultSet(num_results);
  92. resultSet.init(&ret_index, &out_dist_sqr);
  93. index.findNeighbors(resultSet, &query_pt[0]);
  94. std::cout << "knnSearch(nn=" << num_results << "\n";
  95. std::cout << "ret_index=" << ret_index
  96. << " out_dist_sqr=" << out_dist_sqr << endl;
  97. }
  98. {
  99. // Unsorted radius search:
  100. const num_t radius = 1;
  101. std::vector<nanoflann::ResultItem<size_t, num_t>> indices_dists;
  102. RadiusResultSet<num_t, size_t> resultSet(radius, indices_dists);
  103. index.findNeighbors(resultSet, query_pt);
  104. // Get worst (furthest) point, without sorting:
  105. nanoflann::ResultItem<size_t, num_t> worst_pair =
  106. resultSet.worst_item();
  107. cout << "Worst pair: idx=" << worst_pair.first
  108. << " dist=" << worst_pair.second << endl;
  109. }
  110. }
  111. int main()
  112. {
  113. // Randomize Seed
  114. srand(static_cast<unsigned int>(time(nullptr)));
  115. kdtree_custom_metric_demo(10000);
  116. return 0;
  117. }