From 38b70d0dc5c39e6278bf30c67e1e12f7ac8c1fa0 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Tue, 27 Nov 2018 01:09:06 -0800 Subject: libpad: add ix2_reset() This basically amounts to adding a pad_reset() wrapper. Also incorporated resets into the test, while fixing a stupid bug there. --- src/ix2.c | 19 +++++++++++++++++++ src/ix2.h | 1 + src/test.c | 53 +++++++++++++++++++++++++++++++---------------------- 3 files changed, 51 insertions(+), 22 deletions(-) diff --git a/src/ix2.c b/src/ix2.c index 3569ae6..9a8fd02 100644 --- a/src/ix2.c +++ b/src/ix2.c @@ -150,7 +150,9 @@ static void init_node(ix2_node_t *node, bb2f_t *aabb) assert(node); assert(aabb); + node->children = NULL; INIT_LIST_HEAD(&node->objects); + node->n_objects = 0; node->center.x = aabb->min.x; node->center.y = aabb->min.y; @@ -450,6 +452,23 @@ fail_ix2: } +/* Reset the index ix2 to its state as-returned by ix2_new() + * + * Note this doesn't free memory, it just discards the index while keeping all + * the memory around in the caches for efficient reuse. + */ +void ix2_reset(ix2_t *ix2) +{ + assert(ix2); + + pad_reset(ix2->node_pad); + pad_reset(ix2->ref_pad); + pad_reset(ix2->object_pad); + + init_node(&ix2->root, &ix2->aabb); +} + + /* 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) diff --git a/src/ix2.h b/src/ix2.h index 0f6f69f..b76e174 100644 --- a/src/ix2.h +++ b/src/ix2.h @@ -31,6 +31,7 @@ typedef enum ix2_search_status_t { typedef ix2_search_status_t (*ix2_search_cb)(void *cb_context, ix2_object_t *ix2_object, v2f_t *ix2_object_position, bb2f_t *ix2_object_aabb, void *object); ix2_t * ix2_new(bb2f_t *aabb, unsigned max_per_node, unsigned max_depth); +void ix2_reset(ix2_t *ix2); void ix2_free(ix2_t *ix2); ix2_object_t * ix2_object_new(ix2_t *ix2, v2f_t *position, v2f_t *origin, bb2f_t *aabb, void *object); void ix2_object_free(ix2_t *ix2, ix2_object_t *object); diff --git a/src/test.c b/src/test.c index a1918ab..d00f63a 100644 --- a/src/test.c +++ b/src/test.c @@ -63,37 +63,46 @@ int main(int argc, char *argv[]) return 1; } - for (int i = 0; i < sizeof(objects) / sizeof(*objects); i++) { - objects[i].ix = ix2_object_new(ix2, NULL, NULL, &objects[i].aabb, &objects[i]); - if (!o) { - fprintf(stderr, "unable to insert object %i\n", i); - return 1; + for (int n = 0; n < 20; n++) { + for (int i = 0; i < sizeof(objects) / sizeof(*objects); i++) { + objects[i].ix = ix2_object_new(ix2, NULL, NULL, &objects[i].aabb, &objects[i]); + if (!objects[i].ix) { + fprintf(stderr, "unable to insert object %i\n", i); + return 1; + } } - } - for (int i = 0; i < sizeof(objects) / sizeof(*objects); i++) { - /* TODO: actually verify the expected objects are hit */ - if (!ix2_search_by_point(ix2, &objects[i].aabb.min, cb, &objects[i])) { - fprintf(stderr, "unable to lookup object %i by min point\n", i); - return 1; + for (int i = 0; i < sizeof(objects) / sizeof(*objects); i++) { + /* TODO: actually verify the expected objects are hit */ + if (!ix2_search_by_point(ix2, &objects[i].aabb.min, cb, &objects[i])) { + fprintf(stderr, "unable to lookup object %i by min point\n", i); + return 1; + } + + if (!ix2_search_by_point(ix2, &objects[i].aabb.max, cb, &objects[i])) { + fprintf(stderr, "unable to lookup object %i by max point\n", i); + return 1; + } } - if (!ix2_search_by_point(ix2, &objects[i].aabb.max, cb, &objects[i])) { - fprintf(stderr, "unable to lookup object %i by max point\n", i); - return 1; + for (int i = 0; i < sizeof(objects) / sizeof(*objects); i++) { + /* TODO: actually verify the expected objects are hit */ + if (!ix2_search_by_aabb(ix2, NULL, NULL, &objects[i].aabb, cb, &objects[i])) { + fprintf(stderr, "unable to lookup object %i by perfect aabb\n", i); + return 1; + } } - } - for (int i = 0; i < sizeof(objects) / sizeof(*objects); i++) { - /* TODO: actually verify the expected objects are hit */ - if (!ix2_search_by_aabb(ix2, NULL, NULL, &objects[i].aabb, cb, &objects[i])) { - fprintf(stderr, "unable to lookup object %i by perfect aabb\n", i); - return 1; + if (n % 2) { + fprintf(stderr, "explicitly freeing all objects\n"); + for (int i = 0; i < sizeof(objects) / sizeof(*objects); i++) + ix2_object_free(ix2, objects[i].ix); + } else { + fprintf(stderr, "resetting\n"); + ix2_reset(ix2); } } - for (int i = 0; i < sizeof(objects) / sizeof(*objects); i++) - ix2_object_free(ix2, objects[i].ix); ix2_free(ix2); -- cgit v1.2.3