• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <boost/geometry/index/rtree.hpp>
12 
13 #include <boost/chrono.hpp>
14 #include <boost/foreach.hpp>
15 #include <boost/random.hpp>
16 
main()17 int main()
18 {
19     namespace bg = boost::geometry;
20     namespace bgi = bg::index;
21     typedef boost::chrono::thread_clock clock_t;
22     typedef boost::chrono::duration<float> dur_t;
23 
24     size_t values_count = 500000;
25     size_t queries_count = 200000;
26 
27     std::vector< boost::tuple<float, float, float> > coords;
28 
29     //randomize values
30     {
31         boost::mt19937 rng;
32         //rng.seed(static_cast<unsigned int>(std::time(0)));
33         float max_val = static_cast<float>(values_count / 2);
34         boost::uniform_real<float> range(-max_val, max_val);
35         boost::variate_generator<boost::mt19937&, boost::uniform_real<float> > rnd(rng, range);
36 
37         coords.reserve(values_count);
38 
39         std::cout << "randomizing data\n";
40         for ( size_t i = 0 ; i < values_count ; ++i )
41         {
42             coords.push_back(boost::make_tuple(rnd(), rnd(), rnd()));
43         }
44         std::cout << "randomized\n";
45     }
46 
47     typedef bg::model::point<float, 3, bg::cs::cartesian> P;
48     typedef bg::model::box<P> B;
49     //typedef bgi::rtree<B, bgi::linear<32, 8> > RT;
50     //typedef bgi::rtree<B, bgi::quadratic<32, 8> > RT;
51     typedef bgi::rtree<B, bgi::rstar<8, 3> > RT;
52 
53     std::cout << "sizeof rtree: " << sizeof(RT) << std::endl;
54 
55     for (;;)
56     {
57         RT t;
58 
59         // inserting test
60         {
61             clock_t::time_point start = clock_t::now();
62             for (size_t i = 0 ; i < values_count ; ++i )
63             {
64                 float x = boost::get<0>(coords[i]);
65                 float y = boost::get<1>(coords[i]);
66                 float z = boost::get<2>(coords[i]);
67                 B b(P(x - 0.5f, y - 0.5f, z - 0.5f), P(x + 0.5f, y + 0.5f, z + 0.5f));
68 
69                 t.insert(b);
70             }
71             dur_t time = clock_t::now() - start;
72             std::cout << time << " - insert " << values_count << '\n';
73         }
74 
75         std::vector<B> result;
76         result.reserve(100);
77         B result_one;
78 
79         {
80             clock_t::time_point start = clock_t::now();
81             size_t temp = 0;
82             for (size_t i = 0 ; i < queries_count ; ++i )
83             {
84                 float x = boost::get<0>(coords[i]);
85                 float y = boost::get<1>(coords[i]);
86                 float z = boost::get<2>(coords[i]);
87                 result.clear();
88                 t.query(bgi::intersects(B(P(x - 10, y - 10, z - 10), P(x + 10, y + 10, z + 10))), std::back_inserter(result));
89                 temp += result.size();
90             }
91             dur_t time = clock_t::now() - start;
92             std::cout << time << " - query(B) " << queries_count << " found " << temp << '\n';
93         }
94 
95         {
96             clock_t::time_point start = clock_t::now();
97             size_t temp = 0;
98             for (size_t i = 0 ; i < queries_count / 2 ; ++i )
99             {
100                 float x1 = boost::get<0>(coords[i]);
101                 float y1 = boost::get<1>(coords[i]);
102                 float z1 = boost::get<2>(coords[i]);
103                 float x2 = boost::get<0>(coords[i+1]);
104                 float y2 = boost::get<1>(coords[i+1]);
105                 float z2 = boost::get<2>(coords[i+1]);
106                 float x3 = boost::get<0>(coords[i+2]);
107                 float y3 = boost::get<1>(coords[i+2]);
108                 float z3 = boost::get<2>(coords[i+2]);
109                 result.clear();
110                 t.query(
111                     bgi::intersects(B(P(x1 - 10, y1 - 10, z1 - 10), P(x1 + 10, y1 + 10, z1 + 10)))
112                     &&
113                     !bgi::within(B(P(x2 - 10, y2 - 10, z2 - 10), P(x2 + 10, y2 + 10, z2 + 10)))
114                     &&
115                     !bgi::overlaps(B(P(x3 - 10, y3 - 10, z3 - 10), P(x3 + 10, y3 + 10, z3 + 10)))
116                     ,
117                     std::back_inserter(result)
118                     );
119                 temp += result.size();
120             }
121             dur_t time = clock_t::now() - start;
122             std::cout << time << " - query(i && !w && !o) " << queries_count << " found " << temp << '\n';
123         }
124 
125         result.clear();
126 
127         {
128             clock_t::time_point start = clock_t::now();
129             size_t temp = 0;
130             for (size_t i = 0 ; i < queries_count / 10 ; ++i )
131             {
132                 float x = boost::get<0>(coords[i]) - 100;
133                 float y = boost::get<1>(coords[i]) - 100;
134                 float z = boost::get<2>(coords[i]) - 100;
135                 result.clear();
136                 temp += t.query(bgi::nearest(P(x, y, z), 5), std::back_inserter(result));
137             }
138             dur_t time = clock_t::now() - start;
139             std::cout << time << " - query(nearest(P, 5)) " << (queries_count / 10) << " found " << temp << '\n';
140         }
141 
142         {
143             clock_t::time_point start = clock_t::now();
144             for (size_t i = 0 ; i < values_count / 10 ; ++i )
145             {
146                 float x = boost::get<0>(coords[i]);
147                 float y = boost::get<1>(coords[i]);
148                 float z = boost::get<2>(coords[i]);
149                 B b(P(x - 0.5f, y - 0.5f, z - 0.5f), P(x + 0.5f, y + 0.5f, z + 0.5f));
150 
151                 t.remove(b);
152             }
153             dur_t time = clock_t::now() - start;
154             std::cout << time << " - remove " << values_count / 10 << '\n';
155         }
156 
157         std::cout << "------------------------------------------------\n";
158     }
159 
160     return 0;
161 }
162