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
|
#include "minunit.h"
#include "tests.h"
#include "geometry.h"
mu_test test_contains_point();
mu_test test_subdivide();
mu_test test_insert();
mu_test test_insert_two();
mu_test test_get_closest();
void quadtree_tests()
{
mu_run_test("check if contains_point() works",
test_contains_point);
mu_run_test("subdivide quadtree node",
test_subdivide);
mu_run_test("insert second point",
test_insert);
mu_run_test("insert two more points",
test_insert_two);
mu_run_test("look up nearest point",
test_get_closest);
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
mu_test test_contains_point()
{
struct quad_region_t region;
region.center = (struct point_t){ 0.5, 0.5 };
region.half_dim = 0.5;
mu_assert(quad_contains_point(region,
(struct point_t){ 0.5, 0.5 }));
mu_assert(quad_contains_point(region,
(struct point_t){ 0.25, 0.5 }));
mu_assert(quad_contains_point(region,
(struct point_t){ 0.5, 0.25 }));
mu_assert(quad_contains_point(region,
(struct point_t){ 0, 0 }));
mu_assert(!quad_contains_point(region,
(struct point_t){ 1.25, 0.5 }));
mu_assert(!quad_contains_point(region,
(struct point_t){ 0.25, 1.5 }));
mu_assert(!quad_contains_point(region,
(struct point_t){ 1, 1 }));
return 0;
}
mu_test test_subdivide()
{
struct quadtree_node_t node;
struct point_t pts[1] = { { 0.75, 0.75 } };
node.region = (struct quad_region_t){ { 0.5, 0.5 }, 0.5 };
node.id = 0;
node.nw = NULL;
node.ne = NULL;
node.sw = NULL;
node.se = NULL;
quadtree_subdivide(&node, pts);
mu_assert_equal(node.id, -1);
mu_assert_equal(node.nw->region.center.x, 0.25);
mu_assert_equal(node.nw->region.center.y, 0.25);
mu_assert_equal(node.nw->region.half_dim, 0.25);
mu_assert_equal(node.nw->nw, NULL);
mu_assert_equal(node.nw->id, -1);
mu_assert_equal(node.ne->region.center.x, 0.75);
mu_assert_equal(node.ne->region.center.y, 0.25);
mu_assert_equal(node.ne->region.half_dim, 0.25);
mu_assert_equal(node.ne->nw, NULL);
mu_assert_equal(node.ne->id, -1);
mu_assert_equal(node.sw->region.center.x, 0.25);
mu_assert_equal(node.sw->region.center.y, 0.75);
mu_assert_equal(node.sw->region.half_dim, 0.25);
mu_assert_equal(node.sw->nw, NULL);
mu_assert_equal(node.sw->id, -1);
mu_assert_equal(node.se->region.center.x, 0.75);
mu_assert_equal(node.se->region.center.y, 0.75);
mu_assert_equal(node.se->region.half_dim, 0.25);
mu_assert_equal(node.se->nw, NULL);
mu_assert_equal(node.se->id, 0);
return 0;
}
mu_test test_insert()
{
struct quadtree_node_t node;
struct point_t pts[2] = { {0.75, 0.75}, {0.25, 0.25} };
node.region = (struct quad_region_t){ { 0.5, 0.5 }, 0.5 };
node.id = 0;
node.nw = NULL;
node.ne = NULL;
node.sw = NULL;
node.se = NULL;
quadtree_insert(&node, pts, 1);
mu_assert_equal(node.id, -1);
mu_assert_equal(node.nw->region.center.x, 0.25);
mu_assert_equal(node.nw->region.center.y, 0.25);
mu_assert_equal(node.nw->region.half_dim, 0.25);
mu_assert_equal(node.nw->nw, NULL);
mu_assert_equal(node.nw->id, 1);
mu_assert_equal(node.ne->region.center.x, 0.75);
mu_assert_equal(node.ne->region.center.y, 0.25);
mu_assert_equal(node.ne->region.half_dim, 0.25);
mu_assert_equal(node.ne->nw, NULL);
mu_assert_equal(node.ne->id, -1);
mu_assert_equal(node.sw->region.center.x, 0.25);
mu_assert_equal(node.sw->region.center.y, 0.75);
mu_assert_equal(node.sw->region.half_dim, 0.25);
mu_assert_equal(node.sw->nw, NULL);
mu_assert_equal(node.sw->id, -1);
mu_assert_equal(node.se->region.center.x, 0.75);
mu_assert_equal(node.se->region.center.y, 0.75);
mu_assert_equal(node.se->region.half_dim, 0.25);
mu_assert_equal(node.se->nw, NULL);
mu_assert_equal(node.se->id, 0);
return 0;
}
mu_test test_insert_two()
{
struct quadtree_node_t node;
struct point_t pts[3] =
{ {0.75, 0.75}, {0.3, 0.3}, {0.2, 0.2} };
node.region = (struct quad_region_t){ { 0.5, 0.5 }, 0.5 };
node.id = 0;
node.nw = NULL;
node.ne = NULL;
node.sw = NULL;
node.se = NULL;
quadtree_insert(&node, pts, 1);
quadtree_insert(&node, pts, 2);
mu_assert_equal(node.id, -1);
mu_assert_equal(node.nw->region.center.x, 0.25);
mu_assert_equal(node.nw->region.center.y, 0.25);
mu_assert_equal(node.nw->region.half_dim, 0.25);
mu_assert_equal(node.nw->id, -1);
/*printf("%d, %d, %d, %d\n",
node.nw->nw->id,
node.nw->ne->id,
node.nw->sw->id,
node.nw->se->id);*/
mu_assert_equal(node.nw->nw->id, 2);
mu_assert_equal(node.nw->nw->nw, NULL);
mu_assert_equal(node.nw->ne->id, -1);
mu_assert_equal(node.nw->ne->nw, NULL);
mu_assert_equal(node.nw->sw->id, -1);
mu_assert_equal(node.nw->sw->nw, NULL);
mu_assert_equal(node.nw->se->id, 1);
mu_assert_equal(node.nw->se->nw, NULL);
mu_assert_equal(node.ne->region.center.x, 0.75);
mu_assert_equal(node.ne->region.center.y, 0.25);
mu_assert_equal(node.ne->region.half_dim, 0.25);
mu_assert_equal(node.ne->nw, NULL);
mu_assert_equal(node.ne->id, -1);
mu_assert_equal(node.sw->region.center.x, 0.25);
mu_assert_equal(node.sw->region.center.y, 0.75);
mu_assert_equal(node.sw->region.half_dim, 0.25);
mu_assert_equal(node.sw->nw, NULL);
mu_assert_equal(node.sw->id, -1);
mu_assert_equal(node.se->region.center.x, 0.75);
mu_assert_equal(node.se->region.center.y, 0.75);
mu_assert_equal(node.se->region.half_dim, 0.25);
mu_assert_equal(node.se->nw, NULL);
mu_assert_equal(node.se->id, 0);
return 0;
}
mu_test test_get_closest()
{
struct quadtree_node_t node;
struct point_t pts[3] =
{ {0.75, 0.75}, {0.3, 0.3}, {0.2, 0.2} };
node.region = (struct quad_region_t){ { 0.5, 0.5 }, 0.5 };
node.id = 0;
node.nw = NULL;
node.ne = NULL;
node.sw = NULL;
node.se = NULL;
quadtree_insert(&node, pts, 1);
quadtree_insert(&node, pts, 2);
mu_assert_equal(node.nw->nw->id, 2);
mu_assert_equal(node.nw->nw->nw, NULL);
mu_assert_equal(node.nw->ne->id, -1);
mu_assert_equal(node.nw->ne->nw, NULL);
mu_assert_equal(node.nw->sw->id, -1);
mu_assert_equal(node.nw->sw->nw, NULL);
mu_assert_equal(node.nw->se->id, 1);
mu_assert_equal(node.nw->se->nw, NULL);
int closest_75 =
quadtree_get_closest(&node, pts,
(struct point_t){0.75, 0.75});
mu_assert_equal(closest_75, 0);
int closest_33 =
quadtree_get_closest(&node, pts,
(struct point_t){0.3, 0.3});
mu_assert_equal(closest_33, 1);
int closest_22 =
quadtree_get_closest(&node, pts,
(struct point_t){0.2, 0.2});
mu_assert_equal(closest_22, 2);
int closest_252 =
quadtree_get_closest(&node, pts,
(struct point_t){0.251, 0.21});
mu_assert_equal(closest_252, 2);
int closest_nil =
quadtree_get_closest(&node, pts,
(struct point_t){100, 100});
mu_assert_equal(closest_nil, -1);
return 0;
}
|