1 // Boost.Geometry Index
4 // Copyright (c) 2011-2013 Adam Wulkiewicz, Lodz, Poland.
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)
12 #include <boost/geometry.hpp>
13 #include <boost/geometry/index/rtree.hpp>
15 #include <boost/chrono.hpp>
16 #include <boost/foreach.hpp>
17 #include <boost/random.hpp>
21 namespace bg = boost::geometry;
22 namespace bgi = bg::index;
23 typedef boost::chrono::thread_clock clock_t;
24 typedef boost::chrono::duration<float> dur_t;
26 size_t values_count = 1000000;
27 size_t queries_count = 100000;
28 size_t nearest_queries_count = 10000;
29 unsigned neighbours_count = 10;
31 std::vector< std::pair<float, float> > coords;
36 //rng.seed(static_cast<unsigned int>(std::time(0)));
37 float max_val = static_cast<float>(values_count / 2);
38 boost::uniform_real<float> range(-max_val, max_val);
39 boost::variate_generator<boost::mt19937&, boost::uniform_real<float> > rnd(rng, range);
41 coords.reserve(values_count);
43 std::cout << "randomizing data\n";
44 for ( size_t i = 0 ; i < values_count ; ++i )
46 coords.push_back(std::make_pair(rnd(), rnd()));
48 std::cout << "randomized\n";
51 typedef bg::model::point<double, 2, bg::cs::cartesian> P;
52 typedef bg::model::box<P> B;
53 typedef bgi::rtree<B, bgi::linear<16, 4> > RT;
54 //typedef bgi::rtree<B, bgi::quadratic<8, 3> > RT;
55 //typedef bgi::rtree<B, bgi::rstar<8, 3> > RT;
57 std::cout << "sizeof rtree: " << sizeof(RT) << std::endl;
65 clock_t::time_point start = clock_t::now();
66 for (size_t i = 0 ; i < values_count ; ++i )
68 float x = coords[i].first;
69 float y = coords[i].second;
70 B b(P(x - 0.5f, y - 0.5f), P(x + 0.5f, y + 0.5f));
74 dur_t time = clock_t::now() - start;
75 std::cout << time << " - insert " << values_count << '\n';
78 std::vector<B> result;
83 clock_t::time_point start = clock_t::now();
85 for (size_t i = 0 ; i < queries_count ; ++i )
87 float x = coords[i].first;
88 float y = coords[i].second;
90 t.query(bgi::intersects(B(P(x - 10, y - 10), P(x + 10, y + 10))), std::back_inserter(result));
91 temp += result.size();
93 dur_t time = clock_t::now() - start;
94 std::cout << time << " - query(B) " << queries_count << " found " << temp << '\n';
98 clock_t::time_point start = clock_t::now();
100 for (size_t i = 0 ; i < queries_count / 2 ; ++i )
102 float x1 = coords[i].first;
103 float y1 = coords[i].second;
104 float x2 = coords[i+1].first;
105 float y2 = coords[i+1].second;
106 float x3 = coords[i+2].first;
107 float y3 = coords[i+2].second;
110 bgi::intersects(B(P(x1 - 10, y1 - 10), P(x1 + 10, y1 + 10)))
112 !bgi::within(B(P(x2 - 10, y2 - 10), P(x2 + 10, y2 + 10)))
114 !bgi::overlaps(B(P(x3 - 10, y3 - 10), P(x3 + 10, y3 + 10)))
116 std::back_inserter(result)
118 temp += result.size();
120 dur_t time = clock_t::now() - start;
121 std::cout << time << " - query(i && !w && !o) " << queries_count << " found " << temp << '\n';
127 clock_t::time_point start = clock_t::now();
129 for (size_t i = 0 ; i < nearest_queries_count ; ++i )
131 float x = coords[i].first + 100;
132 float y = coords[i].second + 100;
134 temp += t.query(bgi::nearest(P(x, y), neighbours_count), std::back_inserter(result));
136 dur_t time = clock_t::now() - start;
137 std::cout << time << " - query(nearest(P, " << neighbours_count << ")) " << nearest_queries_count << " found " << temp << '\n';
141 clock_t::time_point start = clock_t::now();
142 for (size_t i = 0 ; i < values_count / 10 ; ++i )
144 float x = coords[i].first;
145 float y = coords[i].second;
146 B b(P(x - 0.5f, y - 0.5f), P(x + 0.5f, y + 0.5f));
150 dur_t time = clock_t::now() - start;
151 std::cout << time << " - remove " << values_count / 10 << '\n';
154 std::cout << "------------------------------------------------\n";