random_test.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #define BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
  9. #include <boost/geometry.hpp>
  10. #include <boost/geometry/index/rtree.hpp>
  11. #include <boost/foreach.hpp>
  12. #include <boost/random.hpp>
  13. int main()
  14. {
  15. namespace bg = boost::geometry;
  16. namespace bgi = bg::index;
  17. size_t values_count = 1000000;
  18. size_t queries_count = 10000;
  19. size_t nearest_queries_count = 10000;
  20. unsigned neighbours_count = 10;
  21. std::vector< std::pair<float, float> > coords;
  22. //randomize values
  23. {
  24. boost::mt19937 rng;
  25. //rng.seed(static_cast<unsigned int>(std::time(0)));
  26. float max_val = static_cast<float>(values_count / 2);
  27. boost::uniform_real<float> range(-max_val, max_val);
  28. boost::variate_generator<boost::mt19937&, boost::uniform_real<float> > rnd(rng, range);
  29. coords.reserve(values_count);
  30. std::cout << "randomizing data\n";
  31. for ( size_t i = 0 ; i < values_count ; ++i )
  32. {
  33. coords.push_back(std::make_pair(rnd(), rnd()));
  34. }
  35. std::cout << "randomized\n";
  36. }
  37. typedef bg::model::point<double, 2, bg::cs::cartesian> P;
  38. typedef bg::model::box<P> B;
  39. typedef bgi::rtree<B, bgi::linear<16, 4> > RT;
  40. //typedef bgi::rtree<B, bgi::quadratic<8, 3> > RT;
  41. //typedef bgi::rtree<B, bgi::rstar<8, 3> > RT;
  42. std::cout << "sizeof rtree: " << sizeof(RT) << std::endl;
  43. {
  44. RT t;
  45. // inserting
  46. {
  47. for (size_t i = 0 ; i < values_count ; ++i )
  48. {
  49. float x = coords[i].first;
  50. float y = coords[i].second;
  51. B b(P(x - 0.5f, y - 0.5f), P(x + 0.5f, y + 0.5f));
  52. t.insert(b);
  53. }
  54. std::cout << "inserted values: " << values_count << '\n';
  55. }
  56. std::vector<B> result;
  57. result.reserve(100);
  58. // test
  59. std::vector<size_t> spatial_query_data;
  60. size_t spatial_query_index = 0;
  61. {
  62. size_t found_count = 0;
  63. for (size_t i = 0 ; i < queries_count ; ++i )
  64. {
  65. float x = coords[i].first;
  66. float y = coords[i].second;
  67. result.clear();
  68. t.query(bgi::intersects(B(P(x - 10, y - 10), P(x + 10, y + 10))), std::back_inserter(result));
  69. // test
  70. spatial_query_data.push_back(result.size());
  71. found_count += result.size();
  72. }
  73. std::cout << "spatial queries found: " << found_count << '\n';
  74. }
  75. #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
  76. {
  77. size_t found_count = 0;
  78. for (size_t i = 0 ; i < queries_count ; ++i )
  79. {
  80. float x = coords[i].first;
  81. float y = coords[i].second;
  82. result.clear();
  83. std::copy(t.qbegin_(bgi::intersects(B(P(x - 10, y - 10), P(x + 10, y + 10)))),
  84. t.qend_(bgi::intersects(B(P(x - 10, y - 10), P(x + 10, y + 10)))),
  85. std::back_inserter(result));
  86. // test
  87. if ( spatial_query_data[spatial_query_index] != result.size() )
  88. std::cout << "Spatial query error - should be: " << spatial_query_data[spatial_query_index] << ", is: " << result.size() << '\n';
  89. ++spatial_query_index;
  90. found_count += result.size();
  91. }
  92. std::cout << "incremental spatial queries found: " << found_count << '\n';
  93. }
  94. #endif
  95. // test
  96. std::vector<float> nearest_query_data;
  97. size_t nearest_query_data_index = 0;
  98. {
  99. size_t found_count = 0;
  100. for (size_t i = 0 ; i < nearest_queries_count ; ++i )
  101. {
  102. float x = coords[i].first + 100;
  103. float y = coords[i].second + 100;
  104. result.clear();
  105. t.query(bgi::nearest(P(x, y), neighbours_count), std::back_inserter(result));
  106. // test
  107. {
  108. float max_dist = 0;
  109. BOOST_FOREACH(B const& b, result)
  110. {
  111. float curr_dist = bgi::detail::comparable_distance_near(P(x, y), b);
  112. if ( max_dist < curr_dist )
  113. max_dist = curr_dist;
  114. }
  115. nearest_query_data.push_back(max_dist);
  116. }
  117. found_count += result.size();
  118. }
  119. std::cout << "nearest queries found: " << found_count << '\n';
  120. }
  121. #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
  122. {
  123. size_t found_count = 0;
  124. for (size_t i = 0 ; i < nearest_queries_count ; ++i )
  125. {
  126. float x = coords[i].first + 100;
  127. float y = coords[i].second + 100;
  128. result.clear();
  129. std::copy(t.qbegin_(bgi::nearest(P(x, y), neighbours_count)),
  130. t.qend_(bgi::nearest(P(x, y), neighbours_count)),
  131. std::back_inserter(result));
  132. // test
  133. {
  134. float max_dist = 0;
  135. BOOST_FOREACH(B const& b, result)
  136. {
  137. float curr_dist = bgi::detail::comparable_distance_near(P(x, y), b);
  138. if ( max_dist < curr_dist )
  139. max_dist = curr_dist;
  140. }
  141. if ( nearest_query_data_index < nearest_query_data.size() &&
  142. nearest_query_data[nearest_query_data_index] != max_dist )
  143. std::cout << "Max distance error - should be: " << nearest_query_data[nearest_query_data_index] << ", and is: " << max_dist << "\n";
  144. ++nearest_query_data_index;
  145. }
  146. found_count += result.size();
  147. }
  148. std::cout << "incremental nearest queries found: " << found_count << '\n';
  149. }
  150. #endif
  151. std::cout << "finished\n";
  152. }
  153. return 0;
  154. }