diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2018-11-27 23:47:46 -0800 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2018-11-27 23:50:58 -0800 |
commit | 28c0347ab1af0adec0249788f8351b0bde06297e (patch) | |
tree | 74635d38ff8a4bf0022095391ea978ffc8c6d7bb | |
parent | b62ceffebf1e26d7cb8e9c1edb52020a803e6713 (diff) |
libix3: add ix3_reset()
This basically amounts to a pad_reset() wrapper.
Also incorporated resets into the test, while fixing a stupid
bug there.
-rw-r--r-- | src/ix3.c | 19 | ||||
-rw-r--r-- | src/ix3.h | 1 | ||||
-rw-r--r-- | src/test.c | 54 |
3 files changed, 51 insertions, 23 deletions
@@ -167,7 +167,9 @@ static void init_node(ix3_node_t *node, bb3f_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; @@ -508,6 +510,23 @@ fail_ix3: } +/* Reset the index ix3 to its state as-returned by ix3_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 ix3_reset(ix3_t *ix3) +{ + assert(ix3); + + pad_reset(ix3->node_pad); + pad_reset(ix3->ref_pad); + pad_reset(ix3->object_pad); + + init_node(&ix3->root, &ix3->aabb); +} + + /* Free the index ix3 and everything associated with it */ /* Note the external objects which have been indexed are not freed */ void ix3_free(ix3_t *ix3) @@ -33,6 +33,7 @@ typedef ix3_search_status_t (*ix3_search_cb)(void *cb_context, ix3_object_t *ix3 ix3_t * ix3_new(bb3f_t *aabb, unsigned max_per_node, unsigned max_depth); void ix3_free(ix3_t *ix3); ix3_object_t * ix3_object_new(ix3_t *ix3, v3f_t *position, v3f_t *origin, bb3f_t *aabb, void *object); +void ix3_reset(ix3_t *ix3); void ix3_object_free(ix3_t *ix3, ix3_object_t *object); ix3_object_t * ix3_object_move(ix3_t *ix3, ix3_object_t *object, v3f_t *object_position, v3f_t *object_origin, bb3f_t *object_aabb); int ix3_object_aabb_overlap(ix3_t *ix3, ix3_object_t *object, v3f_t *aabb_position, v3f_t *aabb_origin, bb3f_t *aabb); @@ -63,38 +63,46 @@ int main(int argc, char *argv[]) return 1; } - for (int i = 0; i < sizeof(objects) / sizeof(*objects); i++) { - objects[i].ix = ix3_object_new(ix3, 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 = ix3_object_new(ix3, 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 (!ix3_search_by_point(ix3, &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 (!ix3_search_by_point(ix3, &objects[i].aabb.min, cb, &objects[i])) { + fprintf(stderr, "unable to lookup object %i by min point\n", i); + return 1; + } + + if (!ix3_search_by_point(ix3, &objects[i].aabb.max, cb, &objects[i])) { + fprintf(stderr, "unable to lookup object %i by max point\n", i); + return 1; + } } - if (!ix3_search_by_point(ix3, &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 (!ix3_search_by_aabb(ix3, 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 (!ix3_search_by_aabb(ix3, 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++) + ix3_object_free(ix3, objects[i].ix); + } else { + fprintf(stderr, "resetting\n"); + ix3_reset(ix3); } } - for (int i = 0; i < sizeof(objects) / sizeof(*objects); i++) - ix3_object_free(ix3, objects[i].ix); - ix3_free(ix3); return 0; |