3d_benchmark.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Boost.Geometry Index
  2. // Additional tests
  3. // Copyright (c) 2011-2013 Adam Wulkiewicz, Lodz, Poland.
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #include <iostream>
  8. #include <boost/geometry/index/rtree.hpp>
  9. #include <boost/chrono.hpp>
  10. #include <boost/foreach.hpp>
  11. #include <boost/random.hpp>
  12. int main()
  13. {
  14. namespace bg = boost::geometry;
  15. namespace bgi = bg::index;
  16. typedef boost::chrono::thread_clock clock_t;
  17. typedef boost::chrono::duration<float> dur_t;
  18. size_t values_count = 500000;
  19. size_t queries_count = 200000;
  20. std::vector< boost::tuple<float, float, float> > coords;
  21. //randomize values
  22. {
  23. boost::mt19937 rng;
  24. //rng.seed(static_cast<unsigned int>(std::time(0)));
  25. float max_val = static_cast<float>(values_count / 2);
  26. boost::uniform_real<float> range(-max_val, max_val);
  27. boost::variate_generator<boost::mt19937&, boost::uniform_real<float> > rnd(rng, range);
  28. coords.reserve(values_count);
  29. std::cout << "randomizing data\n";
  30. for ( size_t i = 0 ; i < values_count ; ++i )
  31. {
  32. coords.push_back(boost::make_tuple(rnd(), rnd(), rnd()));
  33. }
  34. std::cout << "randomized\n";
  35. }
  36. typedef bg::model::point<float, 3, bg::cs::cartesian> P;
  37. typedef bg::model::box<P> B;
  38. //typedef bgi::rtree<B, bgi::linear<32, 8> > RT;
  39. //typedef bgi::rtree<B, bgi::quadratic<32, 8> > RT;
  40. typedef bgi::rtree<B, bgi::rstar<8, 3> > RT;
  41. std::cout << "sizeof rtree: " << sizeof(RT) << std::endl;
  42. for (;;)
  43. {
  44. RT t;
  45. // inserting test
  46. {
  47. clock_t::time_point start = clock_t::now();
  48. for (size_t i = 0 ; i < values_count ; ++i )
  49. {
  50. float x = boost::get<0>(coords[i]);
  51. float y = boost::get<1>(coords[i]);
  52. float z = boost::get<2>(coords[i]);
  53. B b(P(x - 0.5f, y - 0.5f, z - 0.5f), P(x + 0.5f, y + 0.5f, z + 0.5f));
  54. t.insert(b);
  55. }
  56. dur_t time = clock_t::now() - start;
  57. std::cout << time << " - insert " << values_count << '\n';
  58. }
  59. std::vector<B> result;
  60. result.reserve(100);
  61. B result_one;
  62. {
  63. clock_t::time_point start = clock_t::now();
  64. size_t temp = 0;
  65. for (size_t i = 0 ; i < queries_count ; ++i )
  66. {
  67. float x = boost::get<0>(coords[i]);
  68. float y = boost::get<1>(coords[i]);
  69. float z = boost::get<2>(coords[i]);
  70. result.clear();
  71. t.query(bgi::intersects(B(P(x - 10, y - 10, z - 10), P(x + 10, y + 10, z + 10))), std::back_inserter(result));
  72. temp += result.size();
  73. }
  74. dur_t time = clock_t::now() - start;
  75. std::cout << time << " - query(B) " << queries_count << " found " << temp << '\n';
  76. }
  77. {
  78. clock_t::time_point start = clock_t::now();
  79. size_t temp = 0;
  80. for (size_t i = 0 ; i < queries_count / 2 ; ++i )
  81. {
  82. float x1 = boost::get<0>(coords[i]);
  83. float y1 = boost::get<1>(coords[i]);
  84. float z1 = boost::get<2>(coords[i]);
  85. float x2 = boost::get<0>(coords[i+1]);
  86. float y2 = boost::get<1>(coords[i+1]);
  87. float z2 = boost::get<2>(coords[i+1]);
  88. float x3 = boost::get<0>(coords[i+2]);
  89. float y3 = boost::get<1>(coords[i+2]);
  90. float z3 = boost::get<2>(coords[i+2]);
  91. result.clear();
  92. t.query(
  93. bgi::intersects(B(P(x1 - 10, y1 - 10, z1 - 10), P(x1 + 10, y1 + 10, z1 + 10)))
  94. &&
  95. !bgi::within(B(P(x2 - 10, y2 - 10, z2 - 10), P(x2 + 10, y2 + 10, z2 + 10)))
  96. &&
  97. !bgi::overlaps(B(P(x3 - 10, y3 - 10, z3 - 10), P(x3 + 10, y3 + 10, z3 + 10)))
  98. ,
  99. std::back_inserter(result)
  100. );
  101. temp += result.size();
  102. }
  103. dur_t time = clock_t::now() - start;
  104. std::cout << time << " - query(i && !w && !o) " << queries_count << " found " << temp << '\n';
  105. }
  106. result.clear();
  107. {
  108. clock_t::time_point start = clock_t::now();
  109. size_t temp = 0;
  110. for (size_t i = 0 ; i < queries_count / 10 ; ++i )
  111. {
  112. float x = boost::get<0>(coords[i]) - 100;
  113. float y = boost::get<1>(coords[i]) - 100;
  114. float z = boost::get<2>(coords[i]) - 100;
  115. result.clear();
  116. temp += t.query(bgi::nearest(P(x, y, z), 5), std::back_inserter(result));
  117. }
  118. dur_t time = clock_t::now() - start;
  119. std::cout << time << " - query(nearest(P, 5)) " << (queries_count / 10) << " found " << temp << '\n';
  120. }
  121. {
  122. clock_t::time_point start = clock_t::now();
  123. for (size_t i = 0 ; i < values_count / 10 ; ++i )
  124. {
  125. float x = boost::get<0>(coords[i]);
  126. float y = boost::get<1>(coords[i]);
  127. float z = boost::get<2>(coords[i]);
  128. B b(P(x - 0.5f, y - 0.5f, z - 0.5f), P(x + 0.5f, y + 0.5f, z + 0.5f));
  129. t.remove(b);
  130. }
  131. dur_t time = clock_t::now() - start;
  132. std::cout << time << " - remove " << values_count / 10 << '\n';
  133. }
  134. std::cout << "------------------------------------------------\n";
  135. }
  136. return 0;
  137. }