From b14506e501770aeaf54d1160c0073398cc29a038 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 5 May 2010 22:22:44 -0600 Subject: Write a PR-tree test. --- tests/libdimension/prtree.c | 62 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'tests/libdimension') diff --git a/tests/libdimension/prtree.c b/tests/libdimension/prtree.c index c75d8da..b3e5d52 100644 --- a/tests/libdimension/prtree.c +++ b/tests/libdimension/prtree.c @@ -22,10 +22,72 @@ */ #include "../../libdimension/dimension_impl.h" +#include #include +unsigned int calls = 0; + +bool +dmnsn_fake_intersection_fn(const dmnsn_object *object, dmnsn_line line, + dmnsn_intersection *intersection) +{ + intersection->t = (object->bounding_box.min.z - line.x0.z)/line.n.z; + ++calls; + return true; +} + +void +dmnsn_randomize_bounding_box(dmnsn_object *object) +{ + dmnsn_vector a, b; + + a.x = 2.0*((double)rand())/RAND_MAX - 1.0; + a.y = 2.0*((double)rand())/RAND_MAX - 1.0; + a.z = 2.0*((double)rand())/RAND_MAX - 1.0; + + b.x = 2.0*((double)rand())/RAND_MAX - 1.0; + b.y = 2.0*((double)rand())/RAND_MAX - 1.0; + b.z = 2.0*((double)rand())/RAND_MAX - 1.0; + + object->bounding_box.min = dmnsn_vector_min(a, b); + object->bounding_box.max = dmnsn_vector_max(a, b); +} + int main() { + const size_t nobjects = 128; + dmnsn_scene *scene = dmnsn_new_scene(); + + size_t i; + for (i = 0; i < nobjects; ++i) { + dmnsn_object *object = dmnsn_new_object(); + dmnsn_randomize_bounding_box(object); + object->intersection_fn = &dmnsn_fake_intersection_fn; + dmnsn_array_push(scene->objects, &object); + } + + dmnsn_prtree *prtree = dmnsn_new_prtree(scene->objects); + + dmnsn_intersection intersection; + dmnsn_line ray = dmnsn_new_line( + dmnsn_new_vector(0.0, 0.0, -2.0), + dmnsn_new_vector(0.0, 0.0, 1.0) + ); + + if (!dmnsn_prtree_search(prtree, ray, &intersection)) { + fprintf(stderr, "--- Didn't find intersection! ---\n"); + return EXIT_FAILURE; + } + + if (calls > DMNSN_PRTREE_B) { + fprintf(stderr, + "--- Too many intersection function calls: %u! ---\n", + calls); + return EXIT_FAILURE; + } + + dmnsn_delete_prtree(prtree); + dmnsn_delete_scene(scene); return EXIT_SUCCESS; } -- cgit v1.2.3