1 // Boost.Geometry Index
2 // Additional tests
3
4 // Copyright (c) 2011-2013 Adam Wulkiewicz, Lodz, Poland.
5
6 // Use, modification and distribution is subject to the Boost Software License,
7 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9
10 #include <iostream>
11
12 #define BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
13
14 #include <boost/geometry.hpp>
15 #include <boost/geometry/index/rtree.hpp>
16
17 #include <boost/foreach.hpp>
18 #include <boost/random.hpp>
19
main()20 int main()
21 {
22 namespace bg = boost::geometry;
23 namespace bgi = bg::index;
24
25 size_t values_count = 1000000;
26 size_t queries_count = 10000;
27 size_t nearest_queries_count = 10000;
28 unsigned neighbours_count = 10;
29
30 std::vector< std::pair<float, float> > coords;
31
32 //randomize values
33 {
34 boost::mt19937 rng;
35 //rng.seed(static_cast<unsigned int>(std::time(0)));
36 float max_val = static_cast<float>(values_count / 2);
37 boost::uniform_real<float> range(-max_val, max_val);
38 boost::variate_generator<boost::mt19937&, boost::uniform_real<float> > rnd(rng, range);
39
40 coords.reserve(values_count);
41
42 std::cout << "randomizing data\n";
43 for ( size_t i = 0 ; i < values_count ; ++i )
44 {
45 coords.push_back(std::make_pair(rnd(), rnd()));
46 }
47 std::cout << "randomized\n";
48 }
49
50 typedef bg::model::point<double, 2, bg::cs::cartesian> P;
51 typedef bg::model::box<P> B;
52 typedef bgi::rtree<B, bgi::linear<16, 4> > RT;
53 //typedef bgi::rtree<B, bgi::quadratic<8, 3> > RT;
54 //typedef bgi::rtree<B, bgi::rstar<8, 3> > RT;
55
56 std::cout << "sizeof rtree: " << sizeof(RT) << std::endl;
57
58 {
59 RT t;
60
61 // inserting
62 {
63 for (size_t i = 0 ; i < values_count ; ++i )
64 {
65 float x = coords[i].first;
66 float y = coords[i].second;
67 B b(P(x - 0.5f, y - 0.5f), P(x + 0.5f, y + 0.5f));
68
69 t.insert(b);
70 }
71 std::cout << "inserted values: " << values_count << '\n';
72 }
73
74 std::vector<B> result;
75 result.reserve(100);
76
77 // test
78 std::vector<size_t> spatial_query_data;
79 size_t spatial_query_index = 0;
80
81 {
82 size_t found_count = 0;
83 for (size_t i = 0 ; i < queries_count ; ++i )
84 {
85 float x = coords[i].first;
86 float y = coords[i].second;
87 result.clear();
88 t.query(bgi::intersects(B(P(x - 10, y - 10), P(x + 10, y + 10))), std::back_inserter(result));
89
90 // test
91 spatial_query_data.push_back(result.size());
92 found_count += result.size();
93 }
94 std::cout << "spatial queries found: " << found_count << '\n';
95 }
96
97 #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
98 {
99 size_t found_count = 0;
100 for (size_t i = 0 ; i < queries_count ; ++i )
101 {
102 float x = coords[i].first;
103 float y = coords[i].second;
104 result.clear();
105 std::copy(t.qbegin_(bgi::intersects(B(P(x - 10, y - 10), P(x + 10, y + 10)))),
106 t.qend_(bgi::intersects(B(P(x - 10, y - 10), P(x + 10, y + 10)))),
107 std::back_inserter(result));
108
109 // test
110 if ( spatial_query_data[spatial_query_index] != result.size() )
111 std::cout << "Spatial query error - should be: " << spatial_query_data[spatial_query_index] << ", is: " << result.size() << '\n';
112 ++spatial_query_index;
113 found_count += result.size();
114 }
115 std::cout << "incremental spatial queries found: " << found_count << '\n';
116 }
117 #endif
118
119 // test
120 std::vector<float> nearest_query_data;
121 size_t nearest_query_data_index = 0;
122
123 {
124 size_t found_count = 0;
125 for (size_t i = 0 ; i < nearest_queries_count ; ++i )
126 {
127 float x = coords[i].first + 100;
128 float y = coords[i].second + 100;
129 result.clear();
130 t.query(bgi::nearest(P(x, y), neighbours_count), std::back_inserter(result));
131
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 nearest_query_data.push_back(max_dist);
142 }
143 found_count += result.size();
144 }
145 std::cout << "nearest queries found: " << found_count << '\n';
146 }
147
148 #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
149 {
150 size_t found_count = 0;
151 for (size_t i = 0 ; i < nearest_queries_count ; ++i )
152 {
153 float x = coords[i].first + 100;
154 float y = coords[i].second + 100;
155 result.clear();
156
157 std::copy(t.qbegin_(bgi::nearest(P(x, y), neighbours_count)),
158 t.qend_(bgi::nearest(P(x, y), neighbours_count)),
159 std::back_inserter(result));
160
161 // test
162 {
163 float max_dist = 0;
164 BOOST_FOREACH(B const& b, result)
165 {
166 float curr_dist = bgi::detail::comparable_distance_near(P(x, y), b);
167 if ( max_dist < curr_dist )
168 max_dist = curr_dist;
169 }
170 if ( nearest_query_data_index < nearest_query_data.size() &&
171 nearest_query_data[nearest_query_data_index] != max_dist )
172 std::cout << "Max distance error - should be: " << nearest_query_data[nearest_query_data_index] << ", and is: " << max_dist << "\n";
173 ++nearest_query_data_index;
174 }
175 found_count += result.size();
176 }
177 std::cout << "incremental nearest queries found: " << found_count << '\n';
178 }
179 #endif
180
181 std::cout << "finished\n";
182 }
183
184 return 0;
185 }
186