summaryrefslogtreecommitdiffstats
path: root/tests/libdimension
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2010-05-05 22:22:44 -0600
committerTavian Barnes <tavianator@gmail.com>2010-05-05 22:33:28 -0600
commitb14506e501770aeaf54d1160c0073398cc29a038 (patch)
tree4c50a52520c5b9983192ad578a7988bf29f12925 /tests/libdimension
parent97c3e4a7a0c9d7b5a41294f356896ce46451e028 (diff)
downloaddimension-b14506e501770aeaf54d1160c0073398cc29a038.tar.xz
Write a PR-tree test.
Diffstat (limited to 'tests/libdimension')
-rw-r--r--tests/libdimension/prtree.c62
1 files changed, 62 insertions, 0 deletions
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 <stdio.h>
#include <stdlib.h>
+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;
}