saveload_example.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 <fstream>
  31. #include <iostream>
  32. #include <nanoflann.hpp>
  33. #include "utils.h"
  34. void kdtree_save_load_demo(const size_t N)
  35. {
  36. PointCloud<double> cloud;
  37. // Generate points:
  38. generateRandomPointCloud(cloud, N);
  39. double 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<double, PointCloud<double>>,
  43. PointCloud<double>, 3 /* dim */
  44. >;
  45. // Construct the index and save it:
  46. // --------------------------------------------
  47. {
  48. my_kd_tree_t index(
  49. 3 /*dim*/, cloud,
  50. nanoflann::KDTreeSingleIndexAdaptorParams(10 /* max leaf */));
  51. std::ofstream f("index.bin", std::ofstream::binary);
  52. if (f.bad()) throw std::runtime_error("Error writing index file!");
  53. index.saveIndex(f);
  54. f.close();
  55. }
  56. // Load the index from disk:
  57. // --------------------------------------------
  58. {
  59. // Important: construct the index associated to the same dataset, since
  60. // data points are NOT stored in the binary file.
  61. // Note - set KDTreeSingleIndexAdaptor::SkipInitialBuildIndex, otherwise
  62. // the tree builds an index that'll be overwritten via loadIndex
  63. my_kd_tree_t index(
  64. 3 /*dim*/, cloud,
  65. nanoflann::KDTreeSingleIndexAdaptorParams(
  66. 10 /* max leaf */, nanoflann::KDTreeSingleIndexAdaptorFlags::
  67. SkipInitialBuildIndex));
  68. std::ifstream f("index.bin", std::ofstream::binary);
  69. if (f.fail()) throw std::runtime_error("Error reading index file!");
  70. index.loadIndex(f);
  71. f.close();
  72. // do a knn search
  73. const size_t num_results = 1;
  74. size_t ret_index;
  75. double out_dist_sqr;
  76. nanoflann::KNNResultSet<double> resultSet(num_results);
  77. resultSet.init(&ret_index, &out_dist_sqr);
  78. index.findNeighbors(resultSet, &query_pt[0]);
  79. std::cout << "knnSearch(nn=" << num_results << "): \n";
  80. std::cout << "ret_index=" << ret_index
  81. << " out_dist_sqr=" << out_dist_sqr << std::endl;
  82. }
  83. // Stress test: try to save an empty index
  84. {
  85. PointCloud<double> emptyCloud;
  86. my_kd_tree_t index(3 /*dim*/, emptyCloud);
  87. std::ofstream f("index2.bin", std::ofstream::binary);
  88. if (f.bad()) throw std::runtime_error("Error writing index file!");
  89. index.saveIndex(f);
  90. f.close();
  91. }
  92. }
  93. int main()
  94. {
  95. // Randomize Seed
  96. srand(static_cast<unsigned int>(time(nullptr)));
  97. kdtree_save_load_demo(100000);
  98. return 0;
  99. }