forked from nushoin/RTree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.cpp
146 lines (117 loc) · 2.84 KB
/
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
//
// Test.cpp
//
// This is a direct port of the C version of the RTree test program.
//
#include <iostream>
#include "RTree.h"
using namespace std;
typedef int ValueType;
struct Rect
{
Rect() {}
Rect(int a_minX, int a_minY, int a_maxX, int a_maxY)
{
min[0] = a_minX;
min[1] = a_minY;
max[0] = a_maxX;
max[1] = a_maxY;
}
int min[2];
int max[2];
};
struct Rect rects[] =
{
Rect(0, 0, 2, 2), // xmin, ymin, xmax, ymax (for 2 dimensional RTree)
Rect(5, 5, 7, 7),
Rect(8, 5, 9, 6),
Rect(7, 1, 9, 2),
};
int nrects = sizeof(rects) / sizeof(rects[0]);
Rect search_rect(6, 4, 10, 6); // search will find above rects that this one overlaps
bool MySearchCallback(ValueType id)
{
cout << "Hit data rect " << id << "\n";
return true; // keep going
}
int main()
{
typedef RTree<ValueType, int, 2, float> MyTree;
MyTree tree;
int i, nhits;
cout << "nrects = " << nrects << "\n";
for(i=0; i<nrects; i++)
{
tree.Insert(rects[i].min, rects[i].max, i); // Note, all values including zero are fine in this version
}
nhits = tree.Search(search_rect.min, search_rect.max, MySearchCallback);
cout << "Search resulted in " << nhits << " hits\n";
// Iterator test
int itIndex = 0;
MyTree::Iterator it;
for( tree.GetFirst(it);
!tree.IsNull(it);
tree.GetNext(it) )
{
int value = tree.GetAt(it);
int boundsMin[2] = {0,0};
int boundsMax[2] = {0,0};
it.GetBounds(boundsMin, boundsMax);
cout << "it[" << itIndex++ << "] " << value << " = (" << boundsMin[0] << "," << boundsMin[1] << "," << boundsMax[0] << "," << boundsMax[1] << ")\n";
}
// Iterator test, alternate syntax
itIndex = 0;
tree.GetFirst(it);
while( !it.IsNull() )
{
int value = *it;
++it;
cout << "it[" << itIndex++ << "] " << value << "\n";
}
// test copy constructor
MyTree copy = tree;
// Iterator test
itIndex = 0;
for (copy.GetFirst(it);
!copy.IsNull(it);
copy.GetNext(it) )
{
int value = copy.GetAt(it);
int boundsMin[2] = {0,0};
int boundsMax[2] = {0,0};
it.GetBounds(boundsMin, boundsMax);
cout << "it[" << itIndex++ << "] " << value << " = (" << boundsMin[0] << "," << boundsMin[1] << "," << boundsMax[0] << "," << boundsMax[1] << ")\n";
}
// Iterator test, alternate syntax
itIndex = 0;
copy.GetFirst(it);
while( !it.IsNull() )
{
int value = *it;
++it;
cout << "it[" << itIndex++ << "] " << value << "\n";
}
return 0;
// Output:
//
// nrects = 4
// Hit data rect 1
// Hit data rect 2
// Search resulted in 2 hits
// it[0] 0 = (0,0,2,2)
// it[1] 1 = (5,5,7,7)
// it[2] 2 = (8,5,9,6)
// it[3] 3 = (7,1,9,2)
// it[0] 0
// it[1] 1
// it[2] 2
// it[3] 3
// it[0] 0 = (0,0,2,2)
// it[1] 1 = (5,5,7,7)
// it[2] 2 = (8,5,9,6)
// it[3] 3 = (7,1,9,2)
// it[0] 0
// it[1] 1
// it[2] 2
// it[3] 3
}