-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoi.c
553 lines (453 loc) · 11.9 KB
/
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
#include "aoi.h"
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define AOI_RADIS 10.0f
#define AOI_RADIS2 (AOI_RADIS * AOI_RADIS)
#define INVALID_ID (uint32_t)(~0)
#define PRE_ALLOC 16
#define DIST2(p1, p2) ( \
(p1[0] - p2[0]) * (p1[0] - p2[0]) \
+ (p1[1] - p2[1]) * (p1[1] - p2[1]) \
+ (p1[2] - p2[2]) * (p1[2] - p2[2]) \
)
#define MODE_WATCHER 1
#define MODE_MARKER 2
#define MODE_MOVE 4
#define MODE_DROP 8
typedef struct object {
int ref;
uint32_t id;
int version;
int mode;
float last[3];
float position[3];
} object;
typedef struct object_set {
int cap;
int number;
object **slot;
} object_set;
typedef struct pair_list {
struct pair_list *next;
object *watcher;
int watcher_version;
object *marker;
int marker_version;
} pair_list;
typedef struct map_slot {
uint32_t id;
object *obj;
int next;
} map_slot;
typedef struct map {
int size;
int lastfree;
map_slot *slot;
} map;
typedef struct aoi_space {
float radius;
float radius2;
aoi_Alloc alloc;
void *alloc_ud;
map *object;
object_set *watcher_static;
object_set *marker_static;
object_set *watcher_move;
object_set *marker_move;
pair_list *hot;
} aoi_space;
static inline object *new_object(aoi_space *space, uint32_t id)
{
object *obj = space->alloc(space->alloc_ud, NULL, 0, sizeof(*obj));
obj->ref = 1;
obj->id = id;
obj->version = 0;
obj->mode = 0;
return obj;
}
static inline map_slot *mainposition(map *m, uint32_t id)
{
uint32_t hash = id & (m->size - 1);
return &m->slot[hash];
}
static void rehash(aoi_space *space, map *m);
static void map_insert(aoi_space *space, map *m, uint32_t id, object *obj)
{
map_slot *s = mainposition(m, id);
if (INVALID_ID == s->id) {
s->id = id;
s->obj = obj;
return;
}
if (s != mainposition(m, s->id)) {
map_slot *last = mainposition(m, s->id);
while (s - m->slot != last->next) {
assert(last->next >= 0);
last = &m->slot[last->next];
}
uint32_t temp_id = s->id;
object *temp_obj = s->obj;
last->next = s->next;
s->id = id;
s->obj = obj;
s->next = -1;
if (temp_obj)
map_insert(space, m, temp_id, temp_obj);
return;
}
while (m->lastfree >= 0) {
map_slot *temp = &m->slot[m->lastfree--];
if (INVALID_ID == temp->id) {
temp->id = id;
temp->obj = obj;
temp->next = s->next;
s->next = (int)(temp - m->slot);
return;
}
}
rehash(space, m);
map_insert(space, m, id, obj);
}
static void rehash(aoi_space *space, map *m)
{
map_slot *old_slot = m->slot;
int old_size = m->size;
m->size = 2 * old_size;
m->lastfree = m->size - 1;
m->slot = space->alloc(space->alloc_ud, NULL, 0, m->size * sizeof(map_slot));
int i;
for (i = 0; i < m->size; i++) {
map_slot *s = &m->slot[i];
s->id = INVALID_ID;
s->obj = NULL;
s->next = -1;
}
for (i = 0; i < old_size; i++) {
map_slot *s = &old_slot[i];
if (s->obj)
map_insert(space, m, s->id, s->obj);
}
space->alloc(space->alloc_ud, old_slot, old_size * sizeof(map_slot), 0);
}
static object *map_query(aoi_space *space, map *m, uint32_t id)
{
map_slot *s = mainposition(m, id);
for (;;) {
if (id == s->id) {
if (s->obj == NULL)
s->obj = new_object(space, id);
return s->obj;
}
if (s->next < 0)
break;
s = &m->slot[s->next];
}
object *obj = new_object(space, id);
map_insert(space, m, id, obj);
return obj;
}
static void map_foreach(map *m, void (*func)(void *ud, object *obj), void *ud)
{
int i;
for (i = 0; i < m->size; i++)
if (m->slot[i].obj)
func(ud, m->slot[i].obj);
}
static object *map_drop(map *m, uint32_t id)
{
map_slot *s = mainposition(m, id);
for (;;) {
if (id == s->id) {
object *obj = s->obj;
s->obj = NULL;
return obj;
}
if (s->next < 0)
break;
s = &m->slot[s->next];
}
return NULL;
}
static inline void map_delete(aoi_space *space, map *m)
{
space->alloc(space->alloc_ud, m->slot, m->size * sizeof(map_slot), 0);
space->alloc(space->alloc_ud, m, sizeof(*m), 0);
}
static map *map_new(aoi_space *space)
{
int i;
map *m = space->alloc(space->alloc_ud, NULL, 0, sizeof(*m));
m->size = PRE_ALLOC;
m->lastfree = PRE_ALLOC - 1;
m->slot = space->alloc(space->alloc_ud, NULL, 0, m->size * sizeof(map_slot));
for (i = 0; i < m->size; i++) {
struct map_slot *s = &m->slot[i];
s->id = INVALID_ID;
s->obj = NULL;
s->next = -1;
}
return m;
}
static inline object *grab_object(object *obj)
{
obj->ref++;
return obj;
}
static inline void delete_object(aoi_space *space, object *obj)
{
space->alloc(space->alloc_ud, obj, sizeof(*obj), 0);
}
static inline void drop_object(aoi_space *space, object *obj)
{
obj->ref--;
if (obj->ref == 0) {
map_drop(space->object, obj->id);
delete_object(space, obj);
}
}
static inline object_set *set_new(aoi_space *space)
{
object_set *set = space->alloc(space->alloc_ud, NULL, 0, sizeof(*set));
set->cap = PRE_ALLOC;
set->number = 0;
set->slot = space->alloc(space->alloc_ud, NULL, 0, set->cap * sizeof(object *));
return set;
}
aoi_space *aoi_create(float radius, aoi_Alloc alloc, void *ud)
{
aoi_space *space = alloc(ud, NULL, 0, sizeof(*space));
space->radius = radius;
space->radius2 = space->radius * space->radius;
space->alloc = alloc;
space->alloc_ud = ud;
space->object = map_new(space);
space->watcher_static = set_new(space);
space->marker_static = set_new(space);
space->watcher_move = set_new(space);
space->marker_move = set_new(space);
space->hot = NULL;
return space;
}
static void delete_pair_list(aoi_space *space)
{
pair_list *p = space->hot;
while (p) {
struct pair_list *next = p->next;
space->alloc(space->alloc_ud, p, sizeof(*p), 0);
p = next;
}
}
static inline void delete_set(aoi_space *space, object_set *set)
{
space->alloc(space->alloc_ud, set->slot, set->cap * sizeof(object *), 0);
space->alloc(space->alloc_ud, set, sizeof(*set), 0);
}
void aoi_release(aoi_space *space)
{
delete_pair_list(space);
delete_set(space, space->watcher_static);
delete_set(space, space->marker_static);
delete_set(space, space->watcher_move);
delete_set(space, space->marker_move);
map_foreach(space->object, (void (*)(void *, object *))delete_object, space);
map_delete(space, space->object);
space->alloc(space->alloc_ud, space, sizeof(*space), 0);
}
static inline void copy_position(float dst[3], const float src[3])
{
dst[0] = src[0], dst[1] = src[1], dst[2] = src[2];
}
static inline bool change_mode(object *obj, bool set_watcher, bool set_marker)
{
bool change = false;
if (obj->mode == 0) {
if (set_watcher)
obj->mode |= MODE_WATCHER;
if (set_marker)
obj->mode |= MODE_MARKER;
return true;
}
if (set_watcher) {
if (!(obj->mode & MODE_WATCHER)) {
obj->mode |= MODE_WATCHER;
change = true;
}
} else {
if (obj->mode & MODE_WATCHER) {
obj->mode &= ~MODE_WATCHER;
change = true;
}
}
if (set_marker) {
if (!(obj->mode & MODE_MARKER)) {
obj->mode |= MODE_MARKER;
change = true;
}
} else {
if (obj->mode & MODE_MARKER) {
obj->mode &= ~MODE_MARKER;
change = true;
}
}
return change;
}
static inline bool is_near(float radius2, const float p1[3], const float p2[3])
{
return DIST2(p1, p2) < radius2 * 0.25f;
}
static inline float dist2(const object *p1, const object *p2)
{
float d = DIST2(p1->position, p2->position);
return d;
}
void aoi_update(aoi_space *space, uint32_t id, const char *mode, const float pos[3])
{
object *obj = map_query(space, space->object, id);
int i;
bool set_watcher = false;
bool set_marker = false;
for (i = 0; mode[i]; i++) {
char m = mode[i];
switch (m) {
case 'w': set_watcher = true; break;
case 'm': set_marker = true; break;
case 'd':
if (!(obj->mode & MODE_DROP)) {
obj->mode = MODE_DROP;
drop_object(space, obj);
}
return;
}
}
if (obj->mode & MODE_DROP) {
obj->mode &= ~MODE_DROP;
grab_object(obj);
}
bool changed = change_mode(obj, set_watcher, set_marker);
copy_position(obj->position, pos);
if (changed || !is_near(space->radius2, obj->last, pos)) {
// new object or change object mode
// or position changed
copy_position(obj->last, obj->position);
obj->mode |= MODE_MOVE;
obj->version++;
}
}
static inline void drop_pair(aoi_space *space, pair_list *p)
{
drop_object(space, p->watcher);
drop_object(space, p->marker);
space->alloc(space->alloc_ud, p, sizeof(*p), 0);
}
static void flush_pair(aoi_space *space, aoi_Callback cb, void *ud)
{
pair_list **last = &(space->hot);
pair_list *p = *last;
while (p) {
pair_list *next = p->next;
if (p->watcher->version != p->watcher_version
|| p->marker->version != p->marker_version
|| (p->watcher->mode & MODE_DROP)
|| (p->marker->mode & MODE_DROP)
) {
drop_pair(space, p);
*last = next;
} else {
float distance2 = dist2(p->watcher, p->marker);
if (distance2 > space->radius2 * 4) {
drop_pair(space, p);
*last = next;
} else if (distance2 < space->radius2) {
cb(ud, p->watcher->id, p->marker->id);
drop_pair(space, p);
*last = next;
} else {
last = &(p->next);
}
}
p = next;
}
}
static inline void set_push_back(aoi_space *space, object_set *set, object *obj)
{
if (set->number >= set->cap) {
int cap = set->cap * 2;
set->slot = space->alloc(space->alloc_ud, set->slot, set->cap * sizeof(object *), cap * sizeof(object *));
set->cap = cap;
}
set->slot[set->number++] = obj;
}
static void set_push(void *s, object *obj)
{
aoi_space *space = (aoi_space *)s;
int mode = obj->mode;
if (mode & MODE_WATCHER) {
if (mode & MODE_MOVE) {
set_push_back(space, space->watcher_move, obj);
obj->mode &= ~MODE_MOVE;
} else
set_push_back(space, space->watcher_static, obj);
}
if (mode & MODE_MARKER) {
if (mode & MODE_MOVE) {
set_push_back(space, space->marker_move, obj);
obj->mode &= ~MODE_MOVE;
} else
set_push_back(space, space->marker_static, obj);
}
}
static inline void gen_pair(aoi_space *space, object *watcher, object *marker, aoi_Callback cb, void *ud)
{
if (watcher == marker)
return;
float distance2 = dist2(watcher, marker);
if (distance2 < space->radius2) {
cb(ud, watcher->id, marker->id);
return;
}
if (distance2 > space->radius2 * 4)
return;
pair_list *p = space->alloc(space->alloc_ud, NULL, 0, sizeof(*p));
p->watcher = grab_object(watcher);
p->watcher_version = watcher->version;
p->marker = grab_object(marker);
p->marker_version = marker->version;
p->next = space->hot;
space->hot = p;
}
static void gen_pair_list(aoi_space *space, object_set *watcher, object_set *marker, aoi_Callback cb, void *ud)
{
int i, j;
for (i = 0; i < watcher->number; i++)
for (j = 0; j < marker->number; j++)
gen_pair(space, watcher->slot[i], marker->slot[j], cb, ud);
}
void aoi_message(aoi_space *space, aoi_Callback cb, void *ud)
{
flush_pair(space, cb, ud);
space->watcher_static->number = 0;
space->watcher_move->number = 0;
space->marker_static->number = 0;
space->marker_move->number = 0;
map_foreach(space->object, set_push, space);
gen_pair_list(space, space->watcher_static, space->marker_move, cb, ud);
gen_pair_list(space, space->watcher_move, space->marker_static, cb, ud);
gen_pair_list(space, space->watcher_move, space->marker_move, cb, ud);
}
static void *default_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
{
(void)ud; (void)osize; // not used;
if (nsize == 0) {
free(ptr);
return NULL;
}
return realloc(ptr, nsize);
}
aoi_space *aoi_new(float radius)
{
return aoi_create(radius, default_alloc, NULL);
}