forked from nothings/stb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid_reachability.c
363 lines (310 loc) · 9.07 KB
/
grid_reachability.c
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#define STB_CONNECTED_COMPONENTS_IMPLEMENTATION
#define STBCC_GRID_COUNT_X_LOG2 10
#define STBCC_GRID_COUNT_Y_LOG2 10
#include "stb_connected_components.h"
#ifdef GRID_TEST
#include <windows.h>
#include <stdio.h>
#include <direct.h>
//#define STB_DEFINE
#include "stb.h"
//#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
//#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
typedef struct
{
uint16 x,y;
} point;
point leader[1024][1024];
uint32 color[1024][1024];
point find(int x, int y)
{
point p,q;
p = leader[y][x];
if (p.x == x && p.y == y)
return p;
q = find(p.x, p.y);
leader[y][x] = q;
return q;
}
void onion(int x1, int y1, int x2, int y2)
{
point p = find(x1,y1);
point q = find(x2,y2);
if (p.x == q.x && p.y == q.y)
return;
leader[p.y][p.x] = q;
}
void reference(uint8 *map, int w, int h)
{
int i,j;
for (j=0; j < h; ++j) {
for (i=0; i < w; ++i) {
leader[j][i].x = i;
leader[j][i].y = j;
}
}
for (j=1; j < h-1; ++j) {
for (i=1; i < w-1; ++i) {
if (map[j*w+i] == 255) {
if (map[(j+1)*w+i] == 255) onion(i,j, i,j+1);
if (map[(j)*w+i+1] == 255) onion(i,j, i+1,j);
}
}
}
for (j=0; j < h; ++j) {
for (i=0; i < w; ++i) {
uint32 c = 0xff000000;
if (leader[j][i].x == i && leader[j][i].y == j) {
if (map[j*w+i] == 255)
c = stb_randLCG() | 0xff404040;
}
color[j][i] = c;
}
}
for (j=0; j < h; ++j) {
for (i=0; i < w; ++i) {
if (leader[j][i].x != i || leader[j][i].y != j) {
point p = find(i,j);
color[j][i] = color[p.y][p.x];
}
}
}
}
void write_map(stbcc_grid *g, int w, int h, char *filename)
{
int i,j;
for (j=0; j < h; ++j) {
for (i=0; i < w; ++i) {
unsigned int c;
c = stbcc_get_unique_id(g,i,j);
c = stb_rehash_improved(c)&0xffffff;
if (c == STBCC_NULL_UNIQUE_ID)
c = 0xff000000;
else
c = (~c)^0x555555;
if (i % 32 == 0 || j %32 == 0) {
int r = (c >> 16) & 255;
int g = (c >> 8) & 255;
int b = c & 255;
r = (r+130)/2;
g = (g+130)/2;
b = (b+130)/2;
c = 0xff000000 + (r<<16) + (g<<8) + b;
}
color[j][i] = c;
}
}
stbi_write_png(filename, w, h, 4, color, 4*w);
}
void test_connected(stbcc_grid *g)
{
int n = stbcc_query_grid_node_connection(g, 512, 90, 512, 871);
//printf("%d ", n);
}
static char *message;
LARGE_INTEGER start;
void start_timer(char *s)
{
message = s;
QueryPerformanceCounter(&start);
}
void end_timer(void)
{
LARGE_INTEGER end, freq;
double tm;
QueryPerformanceCounter(&end);
QueryPerformanceFrequency(&freq);
tm = (end.QuadPart - start.QuadPart) / (double) freq.QuadPart;
printf("%6.4lf ms: %s\n", tm * 1000, message);
}
extern void quicktest(void);
int loc[5000][2];
int main(int argc, char **argv)
{
stbcc_grid *g;
int w,h, i,j,k=0, count=0, r;
uint8 *map = stbi_load("data/map_03.png", &w, &h, 0, 1);
assert(map);
quicktest();
for (j=0; j < h; ++j)
for (i=0; i < w; ++i)
map[j*w+i] = ~map[j*w+i];
for (i=0; i < w; ++i)
for (j=0; j < h; ++j)
//map[j*w+i] = (((i+1) ^ (j+1)) >> 1) & 1 ? 255 : 0;
map[j*w+i] = stb_max(abs(i-w/2),abs(j-h/2)) & 1 ? 255 : 0;
//map[j*w+i] = (((i ^ j) >> 5) ^ (i ^ j)) & 1 ? 255 : 0;
//map[j*w+i] = stb_rand() & 1 ? 255 : 0;
#if 1
for (i=0; i < 100000; ++i)
map[(stb_rand()%h)*w + stb_rand()%w] ^= 255;
#endif
_mkdir("tests/output/stbcc");
stbi_write_png("tests/output/stbcc/reference.png", w, h, 1, map, 0);
//reference(map, w, h);
g = malloc(stbcc_grid_sizeof());
printf("Size: %d\n", stbcc_grid_sizeof());
#if 0
memset(map, 0, w*h);
stbcc_init_grid(g, map, w, h);
{
int n;
char **s = stb_stringfile("c:/x/clockwork_update.txt", &n);
write_map(g, w, h, "tests/output/stbcc/base.png");
for (i=1; i < n; i += 1) {
int x,y,t;
sscanf(s[i], "%d %d %d", &x, &y, &t);
if (i == 571678)
write_map(g, w, h, stb_sprintf("tests/output/stbcc/clockwork_good.png", i));
stbcc_update_grid(g, x, y, t);
if (i == 571678)
write_map(g, w, h, stb_sprintf("tests/output/stbcc/clockwork_bad.png", i));
//if (i > 571648 && i <= 571712)
//write_map(g, w, h, stb_sprintf("tests/output/stbcc/clockwork_%06d.png", i));
}
write_map(g, w, h, stb_sprintf("tests/output/stbcc/clockwork_%06d.png", i-1));
}
return 0;
#endif
start_timer("init");
stbcc_init_grid(g, map, w, h);
end_timer();
//g = stb_file("c:/x/clockwork_path.bin", 0);
write_map(g, w, h, "tests/output/stbcc/base.png");
for (i=0; i < 5000;) {
loc[i][0] = stb_rand() % w;
loc[i][1] = stb_rand() % h;
if (stbcc_query_grid_open(g, loc[i][0], loc[i][1]))
++i;
}
r = 0;
start_timer("reachable");
for (i=0; i < 2000; ++i) {
for (j=0; j < 2000; ++j) {
int x1 = loc[i][0], y1 = loc[i][1];
int x2 = loc[2000+j][0], y2 = loc[2000+j][1];
r += stbcc_query_grid_node_connection(g, x1,y1, x2,y2);
}
}
end_timer();
printf("%d reachable\n", r);
printf("Cluster size: %d,%d\n", STBCC__CLUSTER_SIZE_X, STBCC__CLUSTER_SIZE_Y);
#if 1
for (j=0; j < 10; ++j) {
for (i=0; i < 5000; ++i) {
loc[i][0] = stb_rand() % w;
loc[i][1] = stb_rand() % h;
}
start_timer("updating 2500");
for (i=0; i < 2500; ++i) {
if (stbcc_query_grid_open(g, loc[i][0], loc[i][1]))
stbcc_update_grid(g, loc[i][0], loc[i][1], 1);
else
stbcc_update_grid(g, loc[i][0], loc[i][1], 0);
}
end_timer();
write_map(g, w, h, stb_sprintf("tests/output/stbcc/update_random_%d.png", j*i));
}
#endif
#if 0
start_timer("removing");
count = 0;
for (i=0; i < 1800; ++i) {
int x,y,a,b;
x = stb_rand() % (w-32);
y = stb_rand() % (h-32);
if (i & 1) {
for (a=0; a < 32; ++a)
for (b=0; b < 1; ++b)
if (stbcc_query_grid_open(g, x+a, y+b)) {
stbcc_update_grid(g, x+a, y+b, 1);
++count;
}
} else {
for (a=0; a < 1; ++a)
for (b=0; b < 32; ++b)
if (stbcc_query_grid_open(g, x+a, y+b)) {
stbcc_update_grid(g, x+a, y+b, 1);
++count;
}
}
//if (i % 100 == 0) write_map(g, w, h, stb_sprintf("tests/output/stbcc/open_random_%d.png", i+1));
}
end_timer();
printf("Removed %d grid spaces\n", count);
write_map(g, w, h, stb_sprintf("tests/output/stbcc/open_random_%d.png", i));
r = 0;
start_timer("reachable");
for (i=0; i < 1000; ++i) {
for (j=0; j < 1000; ++j) {
int x1 = loc[i][0], y1 = loc[i][1];
int x2 = loc[j][0], y2 = loc[j][1];
r += stbcc_query_grid_node_connection(g, x1,y1, x2,y2);
}
}
end_timer();
printf("%d reachable\n", r);
start_timer("adding");
count = 0;
for (i=0; i < 1800; ++i) {
int x,y,a,b;
x = stb_rand() % (w-32);
y = stb_rand() % (h-32);
if (i & 1) {
for (a=0; a < 32; ++a)
for (b=0; b < 1; ++b)
if (!stbcc_query_grid_open(g, x+a, y+b)) {
stbcc_update_grid(g, x+a, y+b, 0);
++count;
}
} else {
for (a=0; a < 1; ++a)
for (b=0; b < 32; ++b)
if (!stbcc_query_grid_open(g, x+a, y+b)) {
stbcc_update_grid(g, x+a, y+b, 0);
++count;
}
}
//if (i % 100 == 0) write_map(g, w, h, stb_sprintf("tests/output/stbcc/close_random_%d.png", i+1));
}
end_timer();
write_map(g, w, h, stb_sprintf("tests/output/stbcc/close_random_%d.png", i));
printf("Added %d grid spaces\n", count);
#endif
#if 0 // for map_02.png
start_timer("process");
for (k=0; k < 20; ++k) {
for (j=0; j < h; ++j) {
int any=0;
for (i=0; i < w; ++i) {
if (map[j*w+i] > 10 && map[j*w+i] < 250) {
//start_timer(stb_sprintf("open %d,%d", i,j));
stbcc_update_grid(g, i, j, 0);
test_connected(g);
//end_timer();
any = 1;
}
}
if (any) write_map(g, w, h, stb_sprintf("tests/output/stbcc/open_row_%04d.png", j));
}
for (j=0; j < h; ++j) {
int any=0;
for (i=0; i < w; ++i) {
if (map[j*w+i] > 10 && map[j*w+i] < 250) {
//start_timer(stb_sprintf("close %d,%d", i,j));
stbcc_update_grid(g, i, j, 1);
test_connected(g);
//end_timer();
any = 1;
}
}
if (any) write_map(g, w, h, stb_sprintf("tests/output/stbcc/close_row_%04d.png", j));
}
}
end_timer();
#endif
return 0;
}
#endif