summaryrefslogtreecommitdiff
path: root/src/ix2.c
blob: 3569ae62c0c766f5ed9bdeff6cf27e98514e0ab8 (plain)
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/*
 *  Copyright (C) 2018  Vito Caputo - <vcaputo@pengaru.com>
 *
 *  This program is free software: you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 3 as published
 *  by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* 2d spatial index - implemented as a quadtree */

/* There are various outstanding TODO items littered throughout this code,
 * it's usable as-is but by no means finished or optimized.
 */

#include <assert.h>
#include <stdlib.h>

#include <pad.h>

#include "ix2.h"
#include "list.h"

typedef struct v2f_t {
	float	x, y;
} v2f_t;

typedef struct bb2f_t {
	v2f_t	min, max;
} bb2f_t;

typedef struct ix2_node_t ix2_node_t;

typedef struct ix2_object_t {
	list_head_t	references;	/* list of all ix2_node references to this object */
	list_head_t	hits;		/* node on hit list for non-point searches, to prevent potential double hits like when an object and search both span nodes */
					/* XXX: note this makes non-point searches non-reentrant! i.e. you can't perform another non-point search on the same ix2
					 * from a search callback operating on the same ix2.  TODO investigate efficient solutions.  An obvious one is to simply put
					 * multiple of these nodes in every object and support a small number of simultaneous non-point searches, which may suffice.
					 */
	v2f_t		position;	/* position of this object's AABB in space (may leave as 0 if .aabb is absolute) */
	v2f_t		origin;		/* origin of position within object's AABB, 0,0 = center, -1,-1 = min 1,1 = max */
	bb2f_t		aabb;		/* AABB of this object */
	float		aabb_len_sq;	/* length(aabb.max - aabb.min)^2 TODO: this isn't utilized yet, optimization potential */
	void		*object;	/* user handle linking this object to the index */
} ix2_object_t;

typedef struct ix2_object_ref_t {
	list_head_t	objects;	/* node on ix2_node objects list */
	list_head_t	references;	/* node on ix2_object references list */
	ix2_object_t	*object;	/* object being referenced */
	ix2_node_t	*node;		/* node referencing object */
} ix2_object_ref_t;

struct ix2_node_t {
	v2f_t		center;		/* spatial center point of ix2_node */
	ix2_node_t	*children;	/* array of 4 child quadrants when not a leaf, NULL when a leaf */
	unsigned	n_objects;	/* number of objects in leaf when !children */
	list_head_t	objects;	/* list of all objects in leaf when !children */
};

typedef struct ix2_t {
	pad_t		*node_pad;	/* ix2_node_t allocator */
	pad_t		*ref_pad;	/* ix2_object_ref_t allocator */
	pad_t		*object_pad;	/* ix2_object_t allocator */
	bb2f_t		aabb;		/* spatial bounds of ix2 root */
	unsigned	max_per_node;	/* maximum number of objects to put in a node until max_depth reached */
	unsigned	max_depth;	/* maximum depth of ix2 tree where nodes cease being divided further */
	ix2_node_t	root;		/* root node of ix2 quadtree */
} ix2_t;

static ix2_object_t * add_object(ix2_t *ix2, unsigned *depth, ix2_node_t *node, bb2f_t *node_aabb, ix2_object_t *object, ix2_object_ref_t **reference_cache);


/* Check if point resides within aabb */
static inline int aabb_contains_point(v2f_t *aabb_position, v2f_t *aabb_origin, bb2f_t *aabb, v2f_t *point)
{
	v2f_t	scaled_origin = {};
	v2f_t	position = {};

	assert(aabb);
	assert(point);

	if (!aabb_position)
		aabb_position = &position;

	if (aabb_origin) {
		scaled_origin.x = (aabb->max.x - aabb->min.x) * .5f * aabb_origin->x;
		scaled_origin.y = (aabb->max.y - aabb->min.y) * .5f * aabb_origin->y;
	}

	if (point->x < (aabb_position->x + scaled_origin.x + aabb->min.x))
		return 0;

	if (point->x > (aabb_position->x + scaled_origin.x + aabb->max.x))
		return 0;

	if (point->y < (aabb_position->y + scaled_origin.y + aabb->min.y))
		return 0;

	if (point->y > (aabb_position->y + scaled_origin.y + aabb->max.y))
		return 0;
	
	return 1;
}


/* check if a and b overlap */
static inline int aabb_overlap(v2f_t *a_position, v2f_t *a_origin, bb2f_t *a, v2f_t *b_position, v2f_t *b_origin, bb2f_t *b)
{
	v2f_t	a_scaled_origin = {}, b_scaled_origin = {};
	v2f_t	position = {};

	assert(a);
	assert(b);

	if (!a_position)
		a_position = &position;

	if (!b_position)
		b_position = &position;

	if (a_origin) {
		a_scaled_origin.x = (a->max.x - a->min.x) * .5f * a_origin->x;
		a_scaled_origin.y = (a->max.y - a->min.y) * .5f * a_origin->y;
	}

	if (b_origin) {
		b_scaled_origin.x = (b->max.x - b->min.x) * .5f * b_origin->x;
		b_scaled_origin.y = (b->max.y - b->min.y) * .5f * b_origin->y;
	}

	return ((a->min.x + a_position->x + a_scaled_origin.x) <= (b->max.x + b_position->x + b_scaled_origin.x) &&
		(a->max.x + a_position->x + a_scaled_origin.x) >= (b->min.x + b_position->x + b_scaled_origin.x) &&
		(a->min.y + a_position->y + a_scaled_origin.y) <= (b->max.y + b_position->y + b_scaled_origin.y) &&
		(a->max.y + a_position->y + a_scaled_origin.y) >= (b->min.y + b_position->y + b_scaled_origin.y));
}


/* initialize a node centered in aabb */
static void init_node(ix2_node_t *node, bb2f_t *aabb)
{
	assert(node);
	assert(aabb);

	INIT_LIST_HEAD(&node->objects);

	node->center.x = aabb->min.x;
	node->center.y = aabb->min.y;
	node->center.x += .5f * (aabb->max.x - aabb->min.x);
	node->center.y += .5f * (aabb->max.y - aabb->min.y);
}


/* initialize a quadrants bb2f_t array from a parent node aabb and center point */
static void init_quadrants(bb2f_t *quadrants, bb2f_t *parent_aabb, v2f_t *quadrants_center)
{
	assert(quadrants);
	assert(parent_aabb);
	assert(quadrants_center);
/*
	+-+-+
	|0|1|
	+-+-+
	|2|3|
	+-+-+
*/
	quadrants[0].min.x = parent_aabb->min.x;
	quadrants[0].min.y = quadrants_center->y;
	quadrants[0].max.x = quadrants_center->x;
	quadrants[0].max.y = parent_aabb->max.y;

	quadrants[1].min.x = quadrants_center->x;
	quadrants[1].min.y = quadrants_center->y;
	quadrants[1].max.x = parent_aabb->max.x;
	quadrants[1].max.y = parent_aabb->max.y;

	quadrants[2].min.x = parent_aabb->min.x;
	quadrants[2].min.y = parent_aabb->min.y;
	quadrants[2].max.x = quadrants_center->x;
	quadrants[2].max.y = quadrants_center->y;

	quadrants[3].min.x = quadrants_center->x;
	quadrants[3].min.y = parent_aabb->min.y;
	quadrants[3].max.x = parent_aabb->max.x;
	quadrants[3].max.y = quadrants_center->y;
}


/* Unlink a single object reference, disconnecting it from the object and node */
static void unlink_object_ref(ix2_object_ref_t *ref)
{
	assert(ref);

	/* TODO: is there an actual need to maintain and detect the unlinked state w/NULL ptrs? */
	if (!ref->node)
		return;

	list_del(&ref->objects);
	list_del(&ref->references);
	ref->node->n_objects--;
	ref->object = NULL;
	ref->node = NULL;
}


/* Unlink an object from the index, unlinking all node references.
 * If references_cache is NULL, all unlinked references are freed.
 * Otherwise, the unlinked references are added to the references_cache list.
 */
static void unlink_object(ix2_object_t *object, list_head_t *references_cache)
{
	ix2_object_ref_t	*ref, *_ref;

	assert(object);

	list_for_each_entry_safe(ref, _ref, &object->references, references) {
		unlink_object_ref(ref);

		if (references_cache) {
			list_add_tail(&ref->references, references_cache);
			continue;
		}

		pad_put(ref);
	}
}


/* Link an object into a node.
 * If ref is not NULL, it's an existing reference to move to a new node rather than allocating a new one.
 * Returns the reference on success, NULL on failure
 * On falure the supplied object is left intact.
 */
static ix2_object_ref_t * link_object(ix2_t *ix2, ix2_object_t *object, ix2_node_t *node, ix2_object_ref_t **cached_ref)
{
	ix2_object_ref_t	*ref = NULL;

	assert(object);
	assert(node);

	if (cached_ref && *cached_ref) {
		ref = *cached_ref;
		*cached_ref = NULL;
		unlink_object_ref(ref);
	}

	if (!ref)
		ref = pad_get(ix2->ref_pad, sizeof(ix2_object_ref_t));

	if (!ref)
		return NULL;

	list_add_tail(&ref->references, &object->references);
	list_add_tail(&ref->objects, &node->objects);
	ref->object = object;
	ref->node = node;
	node->n_objects++;

	return ref;
}


/* Compute the diagonal length of the aabb, leaving it squared */
static inline float aabb_len_sq(bb2f_t *aabb)
{
	float	len_sq;

	assert(aabb);

	/* TODO: import the full v2f.h and do this with v2f_sub() && v2f_dot()? */
	len_sq = (aabb->max.x - aabb->min.x) * (aabb->max.x - aabb->min.x);
	len_sq += (aabb->max.y - aabb->min.y) * (aabb->max.y - aabb->min.y);

	return len_sq;
}


/* remove an object */
static void remove_object(ix2_object_t *object)
{
	assert(object);

	unlink_object(object, NULL);
	pad_put(object);
}


/* Split node and distribute objects referenced from node->objects into a
 * newly created node->children.
 */
static ix2_node_t * split_node(ix2_t *ix2, unsigned *depth, ix2_node_t *node, bb2f_t *node_aabb)
{
	bb2f_t			quadrants[4];
	ix2_object_ref_t	*r, *_r;
	int			i;

	assert(ix2);
	assert(depth);
	assert(node);
	assert(!node->children);
	assert(node_aabb);

	/* allocate and setup the children nodes */
	node->children = pad_get(ix2->node_pad, 4 * sizeof(ix2_node_t));
	if (!node->children)
		goto _fail;

	init_quadrants(quadrants, node_aabb, &node->center);
	for (i = 0; i < 4; i++)
		init_node(&node->children[i], &quadrants[i]);

	/* distribute objects across node->children, removing node->objects
	 * references along the way.  The references are supplied
	 */
	list_for_each_entry_safe(r, _r, &node->objects, objects) {
		ix2_object_t	*o = r->object;

		/* XXX: this can probably be done more efficiently */
		for (i = 0; i < 4; i++) {
			if (!aabb_overlap(NULL, NULL, &quadrants[i], &o->position, &o->origin, &o->aabb))
				continue;

			if (!add_object(ix2, depth, &node->children[i], &quadrants[i], o, &r))
				goto _fail;
		}
	}

	return node->children;

_fail:
	/* TODO: some kind of cleanup? */
	return NULL;
}


/* Add a member object to the space described by node && node_aabb
 *
 * This function may need to divide nodes and/or recur.
 *
 * Since it may need to allocate memory for object references via
 * link_object(), and may need to allocate space for children
 * nodes, it may fail.
 *
 * As an optimization, a list of object refs may be supplied in object_refs for
 * use when linking the object.
 *
 * On failure NULL is returned and object is freed (including all its references).
 * On success the supplied object is simply returned, with potentialy new references added.
 */
static ix2_object_t * add_object(ix2_t *ix2, unsigned *depth, ix2_node_t *node, bb2f_t *node_aabb, ix2_object_t *object, ix2_object_ref_t **reference_cache)
{
	assert(ix2);
	assert(depth);
	assert(node);
	assert(node_aabb);
	assert(object);

	/* If not a leaf, simply recur on overlapping children */
	if (node->children) {
		bb2f_t	quadrants[4];
		int	i;

		*depth++;
		init_quadrants(quadrants, node_aabb, &node->center);
		for (i = 0; i < 4; i++) {
			if (!aabb_overlap(NULL, NULL, &quadrants[i], &object->position, &object->origin, &object->aabb))
				continue;

			if (!add_object(ix2, depth, &node->children[i], &quadrants[i], object, reference_cache))
				goto _fail;
		}
		*depth--;

		return object;
	}

	/* Node is a leaf, optimistically link the object to the node */
	if (!link_object(ix2, object, node, reference_cache))
		goto _fail;

	/* If the node is overflowing, split it */
	if (node->n_objects > ix2->max_per_node && *depth < ix2->max_depth) {
		*depth++;
		if (!split_node(ix2, depth, node, node_aabb))
			goto _fail;
		*depth--;
	}

	return object;

_fail:
	remove_object(object);

	return NULL;
}


/* Create a new ix2 index with bounds aabb, splitting nodes > max_per_node until max_depth
 * If the supplied aabb is NULL, a default one of -1,-1...1,1 is used.
 * Returns NULL on failure.
 */
ix2_t * ix2_new(bb2f_t *aabb, unsigned max_per_node, unsigned max_depth)
{
	bb2f_t	default_aabb = {{-1.f, -1.f}, {1.f, 1.f}};
	ix2_t	*ix2;

	if (!aabb)
		aabb = &default_aabb;

	ix2 = calloc(1, sizeof(ix2_t));
	if (!ix2)
		goto fail_ix2;

	init_node(&ix2->root, aabb);

	ix2->aabb = *aabb;
	ix2->max_per_node = max_per_node;
	ix2->max_depth = max_depth;

	ix2->node_pad = pad_new(sizeof(ix2_node_t) * 8);
	if (!ix2->node_pad)
		goto fail_node_pad;

	ix2->ref_pad = pad_new(sizeof(ix2_object_ref_t) * 64);
	if (!ix2->ref_pad)
		goto fail_ref_pad;

	ix2->object_pad = pad_new(sizeof(ix2_object_t) * 32);
	if (!ix2->object_pad)
		goto fail_object_pad;

	return ix2;

fail_object_pad:
	free(ix2->ref_pad);
fail_ref_pad:
	free(ix2->node_pad);
fail_node_pad:
	free(ix2);
fail_ix2:
	return NULL;
}


/* Free the index ix2 and everything associated with it */
/* Note the external objects which have been indexed are not freed */
void ix2_free(ix2_t *ix2)
{
	assert(ix2);

	pad_free(ix2->node_pad);
	pad_free(ix2->ref_pad);
	pad_free(ix2->object_pad);
	free(ix2);
}


/* Create and insert object spatially described by x,y,origin_x,origin_y,object_aabb into the index ix2 */
ix2_object_t * ix2_object_new(ix2_t *ix2, v2f_t *object_position, v2f_t *object_origin, bb2f_t *object_aabb, void *object)
{
	unsigned	depth = 0;
	ix2_object_t	*o;

	assert(ix2);
	assert(object_aabb);
	assert(object_aabb->min.x <= object_aabb->max.x);
	assert(object_aabb->min.y <= object_aabb->max.y);

	o = pad_get(ix2->object_pad, sizeof(ix2_object_t));
	if (!o)
		return NULL;

	INIT_LIST_HEAD(&o->references);
	INIT_LIST_HEAD(&o->hits);

	if (object_position)
		o->position = *object_position;

	if (object_origin)
		o->origin = *object_origin;

	o->aabb = *object_aabb;
	o->object = object;
	o->aabb_len_sq = aabb_len_sq(object_aabb);

	if (!aabb_overlap(NULL, NULL, &ix2->aabb, &o->position, &o->origin, &o->aabb))
		return o;

	return add_object(ix2, &depth, &ix2->root, &ix2->aabb, o, NULL);
}


/* Remove object from the index ix2 and free it. */
/* This may be either the object handle returned by ix2_node_insert(), or
 * the handle supplied to ix2_search_cb().
 */
void ix2_object_free(ix2_t *ix2, ix2_object_t *object)
{
	assert(ix2);
	assert(object);

	remove_object(object);
}


/* Move already inserted object to new position and optional aabb. */
/* If object_aabb is omitted, only the x,y coordinates are updated */
/* Returns object on success, NULL on failure. */
/* On failure object is removed and freed. XXX: change to leave where it was? */
ix2_object_t * ix2_object_move(ix2_t *ix2, ix2_object_t *object, v2f_t *object_position, v2f_t *object_origin, bb2f_t *object_aabb)
{
	unsigned	depth = 0;

	assert(ix2);
	assert(object);
	assert(!object_aabb || object_aabb->min.x <= object_aabb->max.x);
	assert(!object_aabb || object_aabb->min.y <= object_aabb->max.y);

	/* TODO FIXME: cache and reuse the references, requires changing
	 * add_object to receive a list of cached references rather than the
	 * singleton it currently expects.
	 * unlink_object() already knows how to leave those references on a
	 * supplied list head which is currently NULL.
	 */
	unlink_object(object, NULL);

	if (object_position)
		object->position = *object_position;

	if (object_origin)
		object->origin = *object_origin;

	if (object_aabb) {
		object->aabb = *object_aabb;
		object->aabb_len_sq = aabb_len_sq(object_aabb);
	}

	if (!aabb_overlap(NULL, NULL, &ix2->aabb, &object->position, &object->origin, &object->aabb))
		return object;

	return add_object(ix2, &depth, &ix2->root, &ix2->aabb, object, NULL /* TODO */);
}


/* Search for objects overlapping a point in index ix2. */
/* Returns number of cb calls performed on hits, if cb is NULL calls will be skipped but hits still counted for return. */
unsigned ix2_search_by_point(ix2_t *ix2, v2f_t *point, ix2_search_cb cb, void *cb_context)
{
	unsigned		n_hits = 0;
	ix2_object_ref_t	*ref, *_ref;
	ix2_node_t		*node;
	bb2f_t			node_aabb;

	assert(ix2);
	assert(point);

	if (!aabb_contains_point(NULL, NULL, &ix2->aabb, point))
		return 0;

	node = &ix2->root;
	node_aabb = ix2->aabb;

	/* find the leaf node containing point */
	while (node->children) {
		bb2f_t	quadrants[4];
		int	i;

		/* TODO: this can be done more efficiently -
		 * by simply comparing point to the center and
		 * deriving the index from that
		 */
		init_quadrants(quadrants, &node_aabb, &node->center);
		for (i = 0; i < 4; i++) {
			if (aabb_contains_point(NULL, NULL, &quadrants[i], point))
				break;
		}

		node = &node->children[i];
		node_aabb = quadrants[i];
	}

	/* search the leaf for matches */
	list_for_each_entry_safe(ref, _ref, &node->objects, objects) {
		if (!aabb_contains_point(&ref->object->position, &ref->object->origin, &ref->object->aabb, point))
			continue;

		if (cb) {
			ix2_search_status_t	r;
			ix2_object_t		*o = ref->object;

			r = cb(cb_context, o, &o->position, &o->aabb, o->object);
			if (r == IX2_SEARCH_STOP) {
				n_hits++;
				break;
			}

			if (r == IX2_SEARCH_IGNORE)
				continue;
		}

		n_hits++;
	}

	return n_hits;
}


/* recursively search the specified node for objects overlapping with search_aabb,
 * add hits to the list @ hits.
 */
static void search_node_by_aabb(ix2_node_t *node, bb2f_t *node_aabb, v2f_t *search_position, v2f_t *search_origin, bb2f_t *search_aabb, list_head_t *hits)
{
	ix2_object_ref_t	*ref, *_ref;

	assert(node);
	assert(node_aabb);
	assert(search_aabb);
	assert(hits);

	if (node->children) {
		bb2f_t	quadrants[4];
		int	i;

		init_quadrants(quadrants, node_aabb, &node->center);
		for (i = 0; i < 4; i++) {
			if (aabb_overlap(NULL, NULL, &quadrants[i], search_position, search_origin, search_aabb))
				search_node_by_aabb(&node->children[i], &quadrants[i], search_position, search_origin, search_aabb, hits);
		}

		return;
	}

	list_for_each_entry_safe(ref, _ref, &node->objects, objects) {
		if (!aabb_overlap(&ref->object->position, &ref->object->origin, &ref->object->aabb, search_position, search_origin, search_aabb))
			continue;

		list_move_tail(&ref->object->hits, hits);
	}
}


/* Search for objects overlapping an aabb in index ix2. */
/* Returns number of cb calls performed on hits, if cb is NULL calls will be skipped but hits still counted for return. */
unsigned ix2_search_by_aabb(ix2_t *ix2, v2f_t *search_position, v2f_t *search_origin, bb2f_t *search_aabb, ix2_search_cb cb, void *cb_context)
{
	unsigned	n_hits = 0;
	LIST_HEAD	(hits);
	ix2_object_t	*o, *_o;

	assert(ix2);
	assert(search_aabb);

	if (!aabb_overlap(NULL, NULL, &ix2->aabb, search_position, search_origin, search_aabb))
		return 0;

	/* XXX: for the aabb-based search a list of hits is first constructed,
	 * this prevents multiple callbacks on the same object by first
	 * allowing those multiple hits to simply move the object within the
	 * hits list.  In a separate pass the hits are processed with the
	 * callbacks.
	 *
	 * There are two unfortunate consquences to this approach:
	 * 1. No nested/reentrant aabb searches on the same ix2, as the hit list
	 *    node is embedded in the ix2_object_t.
	 * 2. No way to terminate the search from the callback.
	 *    This may be mitigated by adding a limit count to the search
	 *    function, but that can't address scenarios where you want to
	 *    terminate the search on something arbitrary like after the first
	 *    hit of a specific object type.
	 *
	 * TODO: investigate alternative solutions.  Obvious options:
	 * - Putting more than one hit list node in every object for limited
	 *   nesting.  
	 * - Maintain a per-search hash-table which gets accessed before
	 *   every callback to see if the object has been seen already, adding
	 *   it if not before calling the callback.  This strikes me as very
	 *   heavyweight.
	 */
	search_node_by_aabb(&ix2->root, &ix2->aabb, search_position, search_origin, search_aabb, &hits);

	list_for_each_entry_safe(o, _o, &hits, hits) {
		list_del_init(&o->hits);

		if (cb) {
			ix2_search_status_t	r;

			r = cb(cb_context, o, &o->position, &o->aabb, o->object);
			if (r == IX2_SEARCH_STOP) {
				n_hits++;
				break;
			}

			if (r == IX2_SEARCH_IGNORE)
				continue;
		}

		n_hits++;
	}

	/* just in case the search was aborted, finish deleting these nodes */
	/* FIXME: this whole hit list thing is pretty janky */
	list_for_each_entry_safe(o, _o, &hits, hits)
		list_del_init(&o->hits);

	return n_hits;
}
© All Rights Reserved