-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.cpp
260 lines (223 loc) · 6.29 KB
/
main.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <aoi.h>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <algorithm>
#include <chrono>
long long get_tick_count(void)
{
typedef std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> nanoClock_type;
nanoClock_type tp = std::chrono::time_point_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now());
return tp.time_since_epoch().count();
}
#ifdef _DEBUG
#define myassert(X) assert(X)
#else
#define myassert(X) \
if (!(X))\
{\
throw "error!";\
}
#endif
// Need to inherit aoi::Object
class A : public aoi::Object
{
public:
A(float x, float y) : aoi::Object(x, y) {}
private:
};
typedef aoi::Scene<A, 16> SceneType;
void _test_add(SceneType& scn, std::vector<A*>& items)
{
float x = float(rand() % int(scn.GetBounds().Right() - scn.GetBounds().Left())) + scn.GetBounds().Left();
float y = float(rand() % int(scn.GetBounds().Top() - scn.GetBounds().Bottom())) + scn.GetBounds().Bottom();
A* temp = new A(x, y);
myassert(scn.Insert(temp));
items.push_back(temp);
}
void _test_delete(SceneType& scn, std::vector<A*>& items, size_t count)
{
size_t itemsNum = items.size();
if (itemsNum == 0)
{
return;
}
std::random_shuffle(items.begin(), items.end());
for (size_t i = 0; i < (std::min)(count, itemsNum); i++)
{
A* temp = items.back();
myassert(scn.Remove(temp));
delete(temp);
items.pop_back();
}
}
void _test_query(SceneType& scn, std::vector<A*>& items)
{
unsigned testCount = 0;
aoi::Rect queryArea = aoi::Rect(
float(rand() % 10),
float(rand() % int(scn.GetBounds().Right() - scn.GetBounds().Left())) + scn.GetBounds().Left(),
float(rand() % 10),
float(rand() % int(scn.GetBounds().Top() - scn.GetBounds().Bottom())) + scn.GetBounds().Bottom());
for (size_t i = 0; i < items.size(); i++)
{
if (queryArea.Contains(items[i]))
{
testCount++;
}
}
unsigned findCount = 0;
aoi::Object* ptr = scn.Query(queryArea);
while (ptr)
{
findCount++;
ptr = ptr->Next();
}
//printf("find obj count:%u, test count:%u, total count:%u\n", findCount, testCount, scn.GetItemCount());
myassert(testCount == findCount);
}
void _test_query(SceneType& scn, std::vector<A*>& items, float radius)
{
unsigned testCount = 0;
size_t index = rand() % items.size();
aoi::Rect queryArea = aoi::Rect(
items[index]->X - radius,
items[index]->X + radius,
items[index]->Y - radius,
items[index]->Y + radius);
for (size_t i = 0; i < items.size(); i++)
{
if (queryArea.Contains(items[i]))
{
testCount++;
}
}
unsigned findCount = 0;
aoi::Object* ptr = scn.Query(items[index], radius);
while (ptr)
{
findCount++;
ptr = ptr->Next();
}
//printf("find obj count:%u, test count:%u, total count:%u\n", findCount, testCount, scn.GetItemCount());
myassert(testCount == findCount);
}
void test1()
{
aoi::Rect rect(0, 1000, 0, 1000);
SceneType scn(rect);
// Test insertion
std::vector<A*> items;
for (size_t i = 0; i < 8192; i++)
{
_test_add(scn, items);
}
// Test query
for (size_t i = 0; i < 1000; i++)
{
_test_query(scn, items);
_test_query(scn, items, float(rand() % 200 + 50));
}
// Test delete
unsigned itemsNum = items.size();
_test_delete(scn, items, itemsNum);
items.clear();
//printf("delete obj count:%u, total count:%u\n", itemsNum, scn.GetItemCount());
}
void test2()
{
aoi::Rect rect(0, 1000, 0, 1000);
SceneType scn(rect);
std::vector<A*> items;
while (true)
{
unsigned op = rand() % 10;
if (op <= 6)
{
_test_add(scn, items);
}
else if (op <= 8)
{
int itemsNum = (int)items.size();
_test_delete(scn, items, itemsNum % 3 + 1);
}
else
{
_test_query(scn, items);
_test_query(scn, items, float(rand() % 200 + 50));
}
}
}
void test3()
{
unsigned w = 1000;
unsigned h = 1000;
float r = 0.6f;
aoi::Rect rect(0, float(w), 0, float(h));
SceneType scn(rect);
size_t PLAYER_COUNT = 5000;
size_t QUERYCOUNT = 10000;
auto t1 = get_tick_count();
for (size_t i = 0; i < PLAYER_COUNT; i++)
{
rand();
rand();
}
auto t2 = get_tick_count();
//printf("rand cost:%lldns %fns/op\n", t2 - t1, float(t2 - t1) / COUNT);
long long ttr = t2 - t1;
long long ttrop = (long long)(float(t2 - t1) / PLAYER_COUNT);
printf("width:%u, heigth:%u\n", w, h);
printf("player count: %u, query redius: %f\n", unsigned(PLAYER_COUNT), r);
std::vector<A*> items;
t1 = get_tick_count();
for (size_t i = 0; i < PLAYER_COUNT; i++)
{
_test_add(scn, items);
}
t2 = get_tick_count();
printf("insert cost:%12lldns %12.3fns/op\n", t2 - t1 - ttr, float(t2 - t1) / PLAYER_COUNT - ttrop);
t1 = get_tick_count();
for (size_t i = 0; i < QUERYCOUNT; i++)
{
for (size_t j = 0; j < PLAYER_COUNT; j++)
{
aoi::Rect rect(items[j]->X - r, items[j]->X + r, items[j]->Y - r, items[j]->Y + r);
aoi::Object* ptr = scn.Query(items[j], float(r));
}
}
t2 = get_tick_count();
printf("query1 cost:%12lldns %12.3fns/op %12.3fms/op\n", t2 - t1, float(t2 - t1) / QUERYCOUNT, float(t2 - t1) / QUERYCOUNT / 1000000);
t1 = get_tick_count();
for (size_t i = 0; i < QUERYCOUNT; i++)
{
for (size_t j = 0; j < PLAYER_COUNT; j++)
{
aoi::Rect rect(items[j]->X - r, items[j]->X + r, items[j]->Y - r, items[j]->Y + r);
aoi::Object* ptr = scn.Query(rect);
}
}
t2 = get_tick_count();
printf("query2 cost:%12lldns %12.3fns/op %12.3fms/op\n", t2 - t1, float(t2 - t1) / QUERYCOUNT, float(t2 - t1) / QUERYCOUNT / 1000000);
}
void test4()
{
aoi::Rect rect(0, 1000, 0, 1000);
SceneType scn(rect);
for (size_t i = 0; i < 10000; i++)
{
float x = 0;
float y = 0;
A* temp = new A(x, y);
myassert(scn.Insert(temp));
}
}
int main()
{
srand((unsigned)time(0));
//test1();
//test2();
test3();
//test4();
return 0;
}