-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid-aoi.c
579 lines (518 loc) · 15 KB
/
grid-aoi.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
#include "grid-aoi.h"
#ifndef unlikely
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#endif
#define MIN_VALUEVEC_SIZE 32
#define HASH_MAP_MINSIZE 32
#define AOI_MAXLAYER 8
#define calchash(key, size) ((size_t)(key)%(size_t)(size))
typedef struct dlink_node {
struct dlink_node *last;
struct dlink_node *next;
}*dlink_nodeptr;
typedef struct dlink {
struct dlink_node head;
struct dlink_node tail;
}*dlinkptr;
typedef struct aoi_object {
struct dlink_node node;
int id;
short layer;
short view_layer;
float x;
float y;
}*aoi_objectptr;
typedef struct aoi_grid {
int id;
dlinkptr layer[AOI_MAXLAYER];
}*aoi_gridptr;
/* hash map begin */
static inline int hash_node_isempty(hash_node_ptr node) {
return node->value == NULL;
}
static void hash_map_init(hash_map_ptr map) {
memset(map, 0, sizeof(*map));
map->nodes = malloc(sizeof(*map->nodes) * HASH_MAP_MINSIZE);
memset(map->nodes, 0, sizeof(*map->nodes) * HASH_MAP_MINSIZE);
map->size = HASH_MAP_MINSIZE;
map->lastfree = &map->nodes[HASH_MAP_MINSIZE - 1];
}
static void hash_map_clear(hash_map_ptr map) {
if (map->nodes) {
free(map->nodes);
}
map->nodes = NULL;
map->size = map->n = 0;
}
static hash_node_ptr hash_map_getfreepos(hash_map_ptr map) {
for (int i = 0; i < 2; i++) {
while (map->lastfree > map->nodes) {
if (hash_node_isempty(map->lastfree))
return map->lastfree;
map->lastfree--;
}
map->lastfree = &map->nodes[map->size - 1];//try again
}
return NULL;
}
static void hash_map_newkey(hash_map_ptr map, int key, void *value) {
int hash = calchash(key, map->size);
hash_node_ptr node = &map->nodes[hash];
if (!hash_node_isempty(node)) {
hash_node_ptr newnode = hash_map_getfreepos(map);
assert(newnode);
int nodehash = calchash(node->key, map->size);
if (nodehash == hash) { //main position?
if (node->next != 0)
newnode->next = (int)(node->next + node - newnode);
node->next = (int)(newnode - node);
node = newnode;
}
else {
hash_node_ptr othernode = &map->nodes[nodehash];
while (othernode + othernode->next != node) { //find previous
othernode += othernode->next;
}
othernode->next = newnode - othernode;
*newnode = *node;
if (node->next != 0) {
newnode->next += (int)(node - newnode);
node->next = 0;
}
}
}
node->value = value;
node->key = key;
map->n++;
}
static void hash_map_rehash(hash_map_ptr map, hash_map_ptr newmap) {
for (size_t i = 0; i < map->size; i++) {
hash_node_ptr p = &map->nodes[i];
if (!hash_node_isempty(p)) {
hash_map_newkey(newmap, p->key, p->value);
}
}
free(map->nodes);
map->nodes = newmap->nodes;
map->size = newmap->size;
map->lastfree = newmap->lastfree;
}
static void hash_map_reserve(hash_map_ptr map) {
size_t newsize = map->size;
if (map->n > HASH_MAP_MINSIZE * 0.5 && map->n * 1.5 > map->size) {
//expand
newsize *= 2;
}
else if (map->size > HASH_MAP_MINSIZE && map->n * 3 < map->size) {
//reduce
newsize *= 0.5;
}
if (newsize != map->size) {
hash_node_ptr newnodes = malloc(sizeof(*map->nodes) * newsize);
memset(newnodes, 0, sizeof(*newnodes) * newsize);
struct hash_map newmap;
memset(&newmap, 0, sizeof(newmap));
newmap.nodes = newnodes;
newmap.size = newsize;
newmap.lastfree = &newnodes[newsize - 1];
hash_map_rehash(map, &newmap);
}
}
static hash_node_ptr hash_map_get(hash_map_ptr map, int key) {
int hash = calchash(key, map->size);
hash_node_ptr node = &map->nodes[hash];
if (hash_node_isempty(node)) {
return NULL;
}
while (node->key != key) {
if (node->next == 0) return NULL;
node += node->next;
}
return node;
}
static int hash_map_put(hash_map_ptr map, int key, void *value) {
hash_map_reserve(map);
hash_node_ptr slot = hash_map_get(map, key);
if (slot) {
//slot->value = value;
return 0;
}
hash_map_newkey(map, key, value);
return 1;
}
static void hash_map_remove(hash_map_ptr map, int key) {
int hash = calchash(key, map->size);
hash_node_ptr node = &map->nodes[hash];
if (hash_node_isempty(node)) {
return;
}
hash_node_ptr previous = NULL;
while (node->key != key) {
if (node->next == 0) return;
previous = node;
node += node->next;
}
if (previous) {
if (node->next) {
previous->next = node + node->next - previous;
} else {
previous->next = 0;
}
} else {
//main position
if (node->next) {
hash_node_ptr nxt = node + node->next;
*node = *nxt;
if (nxt->next != 0) {
node->next = nxt + nxt->next - node;
}
node = nxt;
}
}
node->next = node->key = 0;
node->value = NULL;
map->n--;
if (map->n > 0) hash_map_reserve(map);
}
static hash_node_ptr hash_map_next(hash_map_ptr map, size_t index, size_t *outi) {
for (size_t i = index; i < map->size; i++) {
hash_node_ptr node = &map->nodes[i];
if (!hash_node_isempty(node)) {
if (outi) *outi = i + 1;
return node;
}
}
return NULL;
}
/* hash map end */
/* dlink begin */
static dlinkptr dlink_new() {
dlinkptr link = (dlinkptr)malloc(sizeof(*link));
memset(link, 0, sizeof(*link));
link->head.next = &link->tail;
link->tail.last = &link->head;
return link;
}
static void dlink_free(dlinkptr link) {
free(link);
}
static void dlink_add(dlinkptr link, dlink_nodeptr node) {
node->last = link->tail.last;
node->next = &link->tail;
link->tail.last = link->tail.last->next = node;
}
static void dlink_remove(dlink_nodeptr node) {
node->last->next = node->next;
node->next->last = node->last;
}
/* dlink end */
/* value vec begin */
static void reservevaluevec(int_valuevecptr valuevec) {
if (!valuevec->values) {
int *values = malloc(sizeof(int) * MIN_VALUEVEC_SIZE);
memset(values, 0, sizeof(int) * MIN_VALUEVEC_SIZE);
valuevec->values = values;
valuevec->n = 0;
valuevec->size = MIN_VALUEVEC_SIZE;
} else if(valuevec->n + 1 > valuevec->size) {
size_t oldsize = valuevec->size;
valuevec->size *= 2;
int *values = realloc(valuevec->values, sizeof(int) * valuevec->size);
memset(values + oldsize, 0, sizeof(int) *
(valuevec->size - oldsize));
valuevec->values = values;
}
}
inline void valuevec_init(int_valuevecptr valuevec) {
valuevec->values = NULL;
valuevec->n = valuevec->size = 0;
}
void valuevec_push(int_valuevecptr valuevec, int value) {
reservevaluevec(valuevec);
valuevec->values[valuevec->n] = value;
valuevec->n ++;
}
inline int valuevec_get(int_valuevecptr valuevec, size_t index) {
return valuevec->values[index];
}
inline void valuevec_clear(int_valuevecptr valuevec) {
valuevec->n = 0;
}
inline size_t valuevec_count(int_valuevecptr valuevec) {
return valuevec->n;
}
inline size_t valuevec_size(int_valuevecptr valuevec) {
return valuevec->size;
}
void valuevec_free(int_valuevecptr valuevec) {
if (valuevec->values)
free(valuevec->values);
valuevec->values = NULL;
valuevec->n = valuevec->size = 0;
}
/* valuevec end */
/* aoi module interface begin*/
static int getgridid(aoi_contextptr context, float x, float y) {
return (int)(y/context->grid_width) * context->x_grid_count + (int)(x/context->grid_width);
}
static inline int calcgridid(int x, int y, int x_grid_count) {
return y * x_grid_count + x;
}
static void calc_rect(aoi_contextptr context, float x, float y, int* lt_x, int* lt_y,
int* rb_x, int* rb_y) {
int xindex = (int)(x / context->grid_width);
int yindex = (int)(y / context->grid_width);
*lt_y = yindex - context->view_range;
*rb_y = yindex + context->view_range;
*lt_x = xindex - context->view_range;
*rb_x = xindex + context->view_range;
if (*lt_y < 0) *lt_y = 0;
if (*lt_x < 0) *lt_x = 0;
if (*rb_y > context->y_grid_count) *rb_y = context->y_grid_count;
if (*rb_x > context->x_grid_count) *rb_x = context->x_grid_count;
}
static aoi_objectptr newobj(int id, short layer, short view_layer, float x, float y) {
aoi_objectptr obj = (aoi_objectptr)malloc(sizeof(*obj));
memset(obj, 0, sizeof(*obj));
obj->id = id;
obj->layer = layer;
obj->view_layer = view_layer;
obj->x = x;
obj->y = y;
return obj;
}
static void freeobj(aoi_objectptr obj) {
free(obj);
}
static aoi_gridptr newgrid(int id) {
aoi_gridptr grid = (aoi_gridptr)malloc(sizeof(*grid));
memset(grid, 0, sizeof(*grid));
grid->id = id;
return grid;
}
static void freegrid(aoi_gridptr grid) {
for (int i=0; i<AOI_MAXLAYER; i++) {
if (grid->layer[i]) {
dlink_free(grid->layer[i]);
}
}
free(grid);
}
static void grid_push(aoi_contextptr context, int gridid, aoi_objectptr obj) {
hash_node_ptr gridnode = hash_map_get(&context->grid_map, gridid);
aoi_gridptr grid;
if (gridnode) {
grid = (aoi_gridptr)gridnode->value;
} else {
grid = newgrid(gridid);
hash_map_put(&context->grid_map, gridid, grid);
}
if (!grid->layer[obj->layer]) {
grid->layer[obj->layer] = dlink_new();
}
dlink_add(grid->layer[obj->layer], &obj->node);
}
static void grid_pop(aoi_contextptr context, int gridid, aoi_objectptr obj) {
hash_node_ptr gridnode = hash_map_get(&context->grid_map, gridid);
if (gridnode) {
aoi_gridptr grid = (aoi_gridptr)gridnode->value;
if (grid->layer[obj->layer]) {
dlink_remove(&obj->node);
}
}
}
static void grid_viewlist(aoi_gridptr grid, aoi_objectptr obj, int_valuevecptr outvec) {
for (int layer=0; layer < AOI_MAXLAYER; layer++) {
if (obj->view_layer & (1<<layer)) {
dlinkptr link = grid->layer[layer];
if (link) {
aoi_objectptr p = (aoi_objectptr)link->head.next;
while (p != (aoi_objectptr)&link->tail) {
if (p != obj) {
valuevec_push(outvec, p->id);
}
p = (aoi_objectptr)p->node.next;
}
}
}
}
}
static void grid_viewedlist(aoi_gridptr grid, aoi_objectptr obj, int_valuevecptr outvec) {
for (int layer=0; layer < AOI_MAXLAYER; layer++) {
dlinkptr link = grid->layer[layer];
if (link) {
aoi_objectptr p = (aoi_objectptr)link->head.next;
while (p != (aoi_objectptr)&link->tail) {
if (p != obj) {
if (p->view_layer & (1<<obj->layer)) {
valuevec_push(outvec, p->id);
}
}
p = (aoi_objectptr)p->node.next;
}
}
}
}
aoi_contextptr aoi_new(float width, float height, int grid_width, int view_range) {
aoi_contextptr newcontext = (aoi_contextptr)malloc(sizeof(*newcontext));
memset(newcontext, 0, sizeof(*newcontext));
if (grid_width <= 0 || width <= 0 || height <= 0) return NULL;
newcontext->width = width;
newcontext->height = height;
newcontext->grid_width = grid_width;
newcontext->view_range = view_range;
newcontext->x_grid_count = (int)(width / grid_width) + 1;
newcontext->y_grid_count = (int)(height / grid_width) + 1;
hash_map_init(&newcontext->obj_map);
hash_map_init(&newcontext->grid_map);
return newcontext;
}
void aoi_delete(aoi_contextptr context) {
if (!context) return;
size_t index = 0;
hash_map_ptr map = &context->obj_map;
hash_node_ptr node = hash_map_next(map, 0, &index);
while (node) {
aoi_objectptr obj = (aoi_objectptr)node->value;
freeobj(obj);
node = hash_map_next(map, index, &index);
}
hash_map_clear(map);
map = &context->grid_map;
node = hash_map_next(map, 0, &index);
while (node) {
aoi_gridptr grid = (aoi_gridptr)node->value;
freegrid(grid);
node = hash_map_next(map, index, &index);
}
hash_map_clear(map);
free(context);
}
int aoi_enter(aoi_contextptr context, int id, float x, float y, short layer,
short view_layer, int_valuevecptr enter_self, int_valuevecptr enter_other) {
if (unlikely(x < 0.0f || y < 0.0f || x > context->width || y > context->height)) {
return 1;
}
aoi_objectptr obj = newobj(id, layer, view_layer, x, y);
if(unlikely(hash_map_put(&context->obj_map, id, obj) == 0)) {//already existed
freeobj(obj);
return 2;
}
//calc rect
int lt_x, lt_y, rb_x, rb_y;
calc_rect(context, x, y, <_x, <_y, &rb_x, &rb_y);
//view list and viewed list
for (int y = lt_y; y <= rb_y; y++) {
for (int x = lt_x; x <= rb_x; x++) {
int gridid = calcgridid(x, y, context->x_grid_count);
hash_node_ptr gridnode = hash_map_get(&context->grid_map, gridid);
if (gridnode) {
aoi_gridptr grid = (aoi_gridptr)gridnode->value;
grid_viewlist(grid, obj, enter_self);
grid_viewedlist(grid, obj, enter_other);
}
}
}
//push grid
int gridid = getgridid(context, x, y);
grid_push(context, gridid, obj);
return 0;
}
int aoi_leave(aoi_contextptr context, int id, int_valuevecptr leave_other) {
hash_node_ptr objnode = hash_map_get(&context->obj_map, id);
if (unlikely(objnode == NULL)) {
return 1;
}
aoi_objectptr obj = (aoi_objectptr)objnode->value;
//pop grid
int gridid = getgridid(context, obj->x, obj->y);
grid_pop(context, gridid, obj);
//calc rect
int lt_x, lt_y, rb_x, rb_y;
calc_rect(context, obj->x, obj->y, <_x, <_y, &rb_x, &rb_y);
//leave view list
for (int y = lt_y; y <= rb_y; y++) {
for (int x = lt_x; x <= rb_x; x++) {
int gridid = calcgridid(x, y, context->x_grid_count);
hash_node_ptr gridnode = hash_map_get(&context->grid_map, gridid);
if (gridnode) {
aoi_gridptr grid = (aoi_gridptr)gridnode->value;
grid_viewedlist(grid, obj, leave_other);
}
}
}
hash_map_remove(&context->obj_map, id);
freeobj(obj);
return 0;
}
int aoi_move(aoi_contextptr context, int id, float x, float y, int_valuevecptr enter,
int_valuevecptr leave) {
hash_node_ptr objnode = hash_map_get(&context->obj_map, id);
if (unlikely(objnode == NULL)) {
return 1;
}
aoi_objectptr obj = (aoi_objectptr)objnode->value;
int ogridid = getgridid(context, obj->x, obj->y);
int ngridid = getgridid(context, x, y);
if (ogridid == ngridid) {
return 0;
}
grid_pop(context, ogridid, obj);
int olt_x, olt_y, orb_x, orb_y;
int nlt_x, nlt_y, nrb_x, nrb_y;
calc_rect(context, obj->x, obj->y, &olt_x, &olt_y, &orb_x, &orb_y);
calc_rect(context, x, y, &nlt_x, &nlt_y, &nrb_x, &nrb_y);
obj->x = x;
obj->y = y;
//leave
for (int y = olt_y; y <= orb_y; y++) {
for (int x = olt_x; x <= orb_x; x++) {
if (x >= nlt_x && x <= nrb_x && y >= nlt_y && y <= nrb_y)
continue;
int gridid = calcgridid(x, y, context->x_grid_count);
hash_node_ptr gridnode = hash_map_get(&context->grid_map, gridid);
if (gridnode) {
aoi_gridptr grid = (aoi_gridptr)gridnode->value;
grid_viewedlist(grid, obj, leave);
}
}
}
//enter
for (int y = nlt_y; y <= nrb_y; y++) {
for (int x = nlt_x; x <= nrb_x; x++) {
if (x >= olt_x && x <= orb_x && y >= olt_y && y <= orb_y)
continue;
int gridid = calcgridid(x, y, context->x_grid_count);
hash_node_ptr gridnode = hash_map_get(&context->grid_map, gridid);
if (gridnode) {
aoi_gridptr grid = (aoi_gridptr)gridnode->value;
grid_viewlist(grid, obj, enter);
}
}
}
grid_push(context, ngridid, obj);
return 0;
}
int aoi_viewlist(aoi_contextptr context, int id, int_valuevecptr viewed) {
hash_node_ptr objnode = hash_map_get(&context->obj_map, id);
if (!objnode) {
return 1;
}
aoi_objectptr obj = (aoi_objectptr)objnode->value;
int lt_x, lt_y, rb_x, rb_y;
calc_rect(context, obj->x, obj->y, <_x, <_y, &rb_x, &rb_y);
//leave view list
for (int y = lt_y; y <= rb_y; y++) {
for (int x = lt_x; x <= rb_x; x++) {
int gridid = calcgridid(x, y, context->x_grid_count);
hash_node_ptr gridnode = hash_map_get(&context->grid_map, gridid);
if (gridnode) {
aoi_gridptr grid = (aoi_gridptr)gridnode->value;
grid_viewedlist(grid, obj, viewed);
}
}
}
return 0;
}
/* aoi module interface end*/