main.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 Dustin Spicuzza <dustin@virtualroadside.com>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public License
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17 
18 
19 #include <string>
20 #include <ctime>
21 
22 #include <stdio.h>
24 
25 #define RANDOM_DATASET
26 //#define GUTTMAN_DATASET
27 
28 #ifdef RANDOM_DATASET
30 #else
32 #endif
33 
35 
36 
37 BoundingBox bounds(int x, int y, int w, int h)
38 {
39  BoundingBox bb;
40 
41  bb.edges[0].first = x;
42  bb.edges[0].second = x + w;
43 
44  bb.edges[1].first = y;
45  bb.edges[1].second = y + h;
46 
47  return bb;
48 }
49 
50 
51 struct Visitor {
52  int count;
54 
55  Visitor() : count(0), ContinueVisiting(true) {};
56 
57  void operator()(const RTree::Leaf * const leaf)
58  {
59 #if defined( RANDOM_DATASET )
60  //std::cout << "Visiting " << count << std::endl;
61 #elif defined( GUTTMAN_DATASET )
62  std::cout << "#" << count << ": visited " << leaf->leaf << " with bound " << leaf->bound.ToString() << std::endl;
63 #else
64  #error "Undefined dataset"
65 #endif
66  count++;
67  }
68 };
69 
70 
71 
72 int main(int argc, char ** argv)
73 {
74  RTree tree;
75  Visitor x;
76 
77  // insert a bunch of items into the tree
78  // Note: this dataset is the one shown on Guttman's original paper
79 #ifdef GUTTMAN_DATASET
80  tree.Insert( "R8" , bounds( 1,5 , 3,2 ));
81  //tree.Print("I1");
82 
83  tree.Insert( "R9", bounds( 6,1 , 2,2 ));
84  //tree.Print("I2");
85 
86  tree.Insert( "R10", bounds( 6,4 , 2,2 ));
87  //tree.Print("I3");
88 
89  tree.Insert( "R11", bounds( 9,0 , 2,14 ));
90  //tree.Print("I4");
91 
92  tree.Insert( "R13", bounds( 13,1 , 1,9 ));
93  //tree.Print("I5");
94 
95  tree.Insert( "R14", bounds( 12,5 , 2,2 ));
96  //tree.Print("I6");
97 
98  tree.Insert( "R15", bounds( 0,16 , 2,2 ));
99  //tree.Print("I7");
100 
101  tree.Insert( "R16", bounds( 3,11 , 6,7 ));
102  //tree.Print("I8");
103 
104  tree.Insert( "R17", bounds( 14,10 , 7,4 ));
105  //tree.Print("I9");
106 
107  tree.Insert( "R18", bounds( 16,8 , 2,9 ));
108  //tree.Print("I10");
109 
110  tree.Insert( "R19", bounds( 17,12 , 3,3 ));
111  //tree.Print("I11");
112 
113  BoundingBox bound = bounds( 5,10, 5,5 );
114 
115  std::cout << "Searching in " << bound.ToString() << std::endl;
116  x = tree.Query(RTree::AcceptOverlapping(bound), Visitor());
117  std::cout << "Visited " << x.count << " nodes." << std::endl;
118 
119  tree.RemoveBoundedArea(bound);
120 
121  // stretch the bounds a bit
122 
123  std::cout << "Searching in " << bound.ToString() << std::endl;
124  x = tree.Query(RTree::AcceptOverlapping(bound), Visitor());
125  std::cout << "Visited " << x.count << " nodes." << std::endl;
126 
127  BoundingBox bound2 = bounds(0,10, 10,10);
128  std::cout << "Removing enclosed area " << bound2.ToString() << std::endl;
129  tree.RemoveBoundedArea(bound2);
130 
131  std::cout << "Searching in " << bound.ToString() << std::endl;
132  x = tree.Query(RTree::AcceptOverlapping(bound), Visitor());
133  std::cout << "Visited " << x.count << " nodes." << std::endl;
134 
135 
136  Visitor y = tree.Query(RTree::AcceptAny(), Visitor());
137  std::cout << "Visited " << y.count << " nodes." << std::endl;
138 
139 
140 #endif
141 
142 
143 #ifdef RANDOM_DATASET
144  srand(time(0));
145 
146  #define nodes 20000
147 
148  for (int i = 0; i < nodes/2; i++)
149  tree.Insert(i, bounds( rand() % 1000, rand() % 1000, rand() % 10, rand() % 10));
150 
151  for (int i = 0; i < nodes/2; i++)
152  tree.Insert(i, bounds( rand() % 1000, rand() % 1000, rand() % 20, rand() % 20));
153 
154  BoundingBox bound = bounds( 100,100, 300,400 );
155 
156  x = tree.Query(RTree::AcceptAny(), Visitor());
157  std::cout << "AcceptAny: " << x.count << " nodes visited (" << tree.GetSize() << " nodes in tree)" << std::endl;
158 
159 
160  std::cout << "Searching in " << bound.ToString() << std::endl;
161  x = tree.Query(RTree::AcceptEnclosing(bound), Visitor());
162  std::cout << "Visited " << x.count << " nodes (" << tree.GetSize() << " nodes in tree)" << std::endl;
163 
164  std::cout << "Removing enclosed area " << bound.ToString() << std::endl;
165  tree.RemoveBoundedArea(bound);
166 
167  std::cout << "Searching in " << bound.ToString() << std::endl;
168  x = tree.Query(RTree::AcceptEnclosing(bound), Visitor());
169  std::cout << "Visited " << x.count << " nodes. (" << tree.GetSize() << " nodes in tree)" << std::endl;
170 
171  //tree.Print();
172 
173 #endif
174 
175 
176 
177  return 0;
178 }
179 
180 
181 /*
182 
183 http://donar.umiacs.umd.edu/quadtree/rectangles/cifquad.html
184 
185 1.0 5.0 3.0 2.0
186 6.0 1.0 2.0 2.0
187 6.0 4.0 2.0 2.0
188 9.0 0.0 2.0 14.0
189 13.0 1.0 1.0 9.0
190 12.0 5.0 2.0 2.0
191 0.0 16.0 2.0 2.0
192 3.0 11.0 6.0 7.0
193 14.0 10.0 7.0 4.0
194 16.0 8.0 2.0 9.0
195 17.0 12.0 3.0 3.0
196 
197 
198 
199 Insert-BoundingBox{(1.0,5.0)(4.0,7.0)}
200 Insert-BoundingBox{(6.0,1.0)(8.0,3.0)}
201 Insert-BoundingBox{(6.0,4.0)(8.0,6.0)}
202 Insert-BoundingBox{(9.0,0.0)(11.0,14.0)}
203 Insert-BoundingBox{(13.0,1.0)(14.0,1.0)}
204 Insert-BoundingBox{(12.0,5.0)(14.0,7.0)}
205 Insert-BoundingBox{(0.0,16.0)(2.0,18.0)}
206 Insert-BoundingBox{(3.0,11.0)(9.0,18.0)}
207 Insert-BoundingBox{(14.0,10.0)(21.0,14.0)}
208 Insert-BoundingBox{(16.0,8.0)(18.0,17.0)}
209 Insert-BoundingBox{(17.0,12.0)(20.0,15.0)}
210 
211 */
212 
213 
bool ContinueVisiting
Definition: main.cpp:53
void Insert(LeafType leaf, const BoundingBox &bound)
Definition: RStarTree.h:123
LeafType leaf
Definition: RStarTree.h:59
#define nodes
void operator()(const RTree::Leaf *const leaf)
Definition: main.cpp:57
std::size_t GetSize() const
Definition: RStarTree.h:283
Visitor()
Definition: main.cpp:55
BoundingBox bounds(int x, int y, int w, int h)
Definition: main.cpp:37
Definition: main.cpp:51
RTree::BoundingBox BoundingBox
Definition: main.cpp:34
int main(int argc, char **argv)
Definition: main.cpp:50
std::string ToString() const
int count
Definition: main.cpp:52
std::pair< double, double > edges[dimensions]
list x
Definition: train.py:276
RStarTree< int, 2, 32, 64 > RTree
Definition: main.cpp:29
Visitor Query(const Acceptor &accept, Visitor visitor)
Touches each node using the visitor pattern.
Definition: RStarTree.h:215
QTextStream & endl(QTextStream &s)
void RemoveBoundedArea(const BoundingBox &bound)
Definition: RStarTree.h:270