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