diff options
Diffstat (limited to 'libdimension')
-rw-r--r-- | libdimension/bench/prtree.c | 22 | ||||
-rw-r--r-- | libdimension/bench/triangle.c | 9 | ||||
-rw-r--r-- | libdimension/cone.c | 20 | ||||
-rw-r--r-- | libdimension/csg.c | 34 | ||||
-rw-r--r-- | libdimension/cube.c | 10 | ||||
-rw-r--r-- | libdimension/dimension/csg.h | 14 | ||||
-rw-r--r-- | libdimension/dimension/object.h | 24 | ||||
-rw-r--r-- | libdimension/dimension/objects.h | 24 | ||||
-rw-r--r-- | libdimension/object.c | 31 | ||||
-rw-r--r-- | libdimension/plane.c | 6 | ||||
-rw-r--r-- | libdimension/scene.c | 15 | ||||
-rw-r--r-- | libdimension/sphere.c | 10 | ||||
-rw-r--r-- | libdimension/tests/prtree.c | 11 | ||||
-rw-r--r-- | libdimension/tests/render.c | 28 | ||||
-rw-r--r-- | libdimension/torus.c | 9 | ||||
-rw-r--r-- | libdimension/triangle.c | 11 |
16 files changed, 120 insertions, 158 deletions
diff --git a/libdimension/bench/prtree.c b/libdimension/bench/prtree.c index f05f04c..c09a701 100644 --- a/libdimension/bench/prtree.c +++ b/libdimension/bench/prtree.c @@ -58,13 +58,13 @@ dmnsn_randomize_bounding_box(dmnsn_object *object) } static dmnsn_object * -dmnsn_new_fake_object(void) +dmnsn_new_fake_object(dmnsn_pool *pool) { - dmnsn_object *object = dmnsn_new_object(); + dmnsn_object *object = dmnsn_new_object(pool); /* Generate a bounding box in (-1, -1, -1), (1, 1, 1) */ dmnsn_randomize_bounding_box(object); object->intersection_fn = dmnsn_fake_intersection_fn; - object->inside_fn = dmnsn_fake_inside_fn; + object->inside_fn = dmnsn_fake_inside_fn; return object; } @@ -79,13 +79,13 @@ main(void) return EXIT_FAILURE; } - dmnsn_array *objects = DMNSN_NEW_ARRAY(dmnsn_object *); - dmnsn_texture *texture = dmnsn_new_texture(); - texture->pigment = dmnsn_new_pigment(); + dmnsn_pool *pool = dmnsn_new_pool(); + dmnsn_array *objects = DMNSN_PALLOC_ARRAY(pool, dmnsn_object *); + dmnsn_texture *texture = dmnsn_new_texture(pool); + texture->pigment = dmnsn_new_pigment(pool); for (size_t i = 0; i < nobjects; ++i) { - dmnsn_object *object = dmnsn_new_fake_object(); + dmnsn_object *object = dmnsn_new_fake_object(pool); object->texture = texture; - DMNSN_INCREF(object->texture); dmnsn_object_initialize(object); dmnsn_array_push(objects, &object); } @@ -121,10 +121,6 @@ main(void) /* Cleanup */ dmnsn_delete_bvh(bvh); - DMNSN_ARRAY_FOREACH (dmnsn_object **, object, objects) { - dmnsn_delete_object(*object); - } - dmnsn_delete_texture(texture); - dmnsn_delete_array(objects); + dmnsn_delete_pool(pool); return EXIT_SUCCESS; } diff --git a/libdimension/bench/triangle.c b/libdimension/bench/triangle.c index 18944e8..39b9322 100644 --- a/libdimension/bench/triangle.c +++ b/libdimension/bench/triangle.c @@ -30,13 +30,16 @@ main(void) return EXIT_FAILURE; } + dmnsn_pool *pool = dmnsn_new_pool(); + dmnsn_object *triangle = dmnsn_new_flat_triangle( + pool, dmnsn_new_vector(1.0, 0.0, 0.0), dmnsn_new_vector(2.0, 2.0, 1.0), dmnsn_new_vector(3.0, 0.0, 2.0) ); - triangle->texture = dmnsn_new_texture(); - triangle->texture->pigment = dmnsn_new_pigment(); + triangle->texture = dmnsn_new_texture(pool); + triangle->texture->pigment = dmnsn_new_pigment(pool); dmnsn_object_initialize(triangle); dmnsn_intersection intersection; @@ -59,7 +62,7 @@ main(void) dmnsn_assert(!intersected, "Intersected"); printf("dmnsn_triangle_intersection(false): %ld\n", sandglass.grains); - dmnsn_delete_object(triangle); + dmnsn_delete_pool(pool); return EXIT_SUCCESS; } diff --git a/libdimension/cone.c b/libdimension/cone.c index 2e52751..ef3ad33 100644 --- a/libdimension/cone.c +++ b/libdimension/cone.c @@ -124,13 +124,13 @@ dmnsn_cone_cap_inside_fn(const dmnsn_object *object, dmnsn_vector point) /** Allocate a new cone cap. */ dmnsn_object * -dmnsn_new_cone_cap(double r) +dmnsn_new_cone_cap(dmnsn_pool *pool, double r) { - dmnsn_cone_cap *cap = DMNSN_MALLOC(dmnsn_cone_cap); + dmnsn_cone_cap *cap = DMNSN_PALLOC(pool, dmnsn_cone_cap); cap->r = r; dmnsn_object *object = &cap->object; - dmnsn_init_object(object); + dmnsn_init_object(pool, object); object->intersection_fn = dmnsn_cone_cap_intersection_fn; object->inside_fn = dmnsn_cone_cap_inside_fn; object->bounding_box.min = dmnsn_new_vector(-r, 0.0, -r); @@ -140,15 +140,15 @@ dmnsn_new_cone_cap(double r) /* Allocate a new cone object */ dmnsn_object * -dmnsn_new_cone(double r1, double r2, bool open) +dmnsn_new_cone(dmnsn_pool *pool, double r1, double r2, bool open) { - dmnsn_cone *cone = DMNSN_MALLOC(dmnsn_cone); + dmnsn_cone *cone = DMNSN_PALLOC(pool, dmnsn_cone); cone->r1 = r1; cone->r2 = r2; dmnsn_object *object = &cone->object; - dmnsn_init_object(object); - object->intersection_fn = dmnsn_cone_intersection_fn; + dmnsn_init_object(pool, object); + object->intersection_fn = dmnsn_cone_intersection_fn; object->inside_fn = dmnsn_cone_inside_fn; double rmax = dmnsn_max(r1, r2); @@ -160,8 +160,8 @@ dmnsn_new_cone(double r1, double r2, bool open) } /* Implement closed cones as a union with the caps */ - dmnsn_object *cap1 = dmnsn_new_cone_cap(r1); - dmnsn_object *cap2 = dmnsn_new_cone_cap(r2); + dmnsn_object *cap1 = dmnsn_new_cone_cap(pool, r1); + dmnsn_object *cap2 = dmnsn_new_cone_cap(pool, r2); cap1->intrinsic_trans = dmnsn_translation_matrix( dmnsn_new_vector(0.0, -1.0, 0.0) ); @@ -175,7 +175,7 @@ dmnsn_new_cone(double r1, double r2, bool open) dmnsn_array_push(withcaps, &cone); dmnsn_array_push(withcaps, &cap1); dmnsn_array_push(withcaps, &cap2); - dmnsn_object *cone_cap_union = dmnsn_new_csg_union(withcaps); + dmnsn_object *cone_cap_union = dmnsn_new_csg_union(pool, withcaps); dmnsn_delete_array(withcaps); return cone_cap_union; diff --git a/libdimension/csg.c b/libdimension/csg.c index 38c5983..b77b20e 100644 --- a/libdimension/csg.c +++ b/libdimension/csg.c @@ -67,31 +67,29 @@ dmnsn_csg_union_initialize_fn(dmnsn_object *object) /** CSG union destruction callback. */ static void -dmnsn_csg_union_free_fn(dmnsn_object *object) +dmnsn_csg_union_cleanup(void *ptr) { - dmnsn_csg_union *csg = (dmnsn_csg_union *)object; + dmnsn_csg_union *csg = ptr; dmnsn_delete_bvh(csg->bvh); - dmnsn_free(csg); } /* Bulk-load a union */ dmnsn_object * -dmnsn_new_csg_union(const dmnsn_array *objects) +dmnsn_new_csg_union(dmnsn_pool *pool, const dmnsn_array *objects) { - dmnsn_csg_union *csg = DMNSN_MALLOC(dmnsn_csg_union); + dmnsn_csg_union *csg = DMNSN_PALLOC_TIDY(pool, dmnsn_csg_union, dmnsn_csg_union_cleanup); csg->bvh = NULL; dmnsn_object *object = &csg->object; - dmnsn_init_object(object); + dmnsn_init_object(pool, object); DMNSN_ARRAY_FOREACH (dmnsn_object **, child, objects) { dmnsn_array_push(object->children, child); } - object->split_children = true; + object->split_children = true; object->intersection_fn = dmnsn_csg_union_intersection_fn; - object->inside_fn = dmnsn_csg_union_inside_fn; - object->initialize_fn = dmnsn_csg_union_initialize_fn; - object->free_fn = dmnsn_csg_union_free_fn; + object->inside_fn = dmnsn_csg_union_inside_fn; + object->initialize_fn = dmnsn_csg_union_initialize_fn; return object; } @@ -207,16 +205,16 @@ dmnsn_csg_intersection_initialize_fn(dmnsn_object *csg) } dmnsn_object * -dmnsn_new_csg_intersection(dmnsn_object *A, dmnsn_object *B) +dmnsn_new_csg_intersection(dmnsn_pool *pool, dmnsn_object *A, dmnsn_object *B) { - dmnsn_object *csg = dmnsn_new_object(); + dmnsn_object *csg = dmnsn_new_object(pool); dmnsn_array_push(csg->children, &A); dmnsn_array_push(csg->children, &B); csg->intersection_fn = dmnsn_csg_intersection_intersection_fn; - csg->inside_fn = dmnsn_csg_intersection_inside_fn; - csg->initialize_fn = dmnsn_csg_intersection_initialize_fn; + csg->inside_fn = dmnsn_csg_intersection_inside_fn; + csg->initialize_fn = dmnsn_csg_intersection_initialize_fn; return csg; } @@ -254,9 +252,9 @@ dmnsn_csg_difference_initialize_fn(dmnsn_object *csg) } dmnsn_object * -dmnsn_new_csg_difference(dmnsn_object *A, dmnsn_object *B) +dmnsn_new_csg_difference(dmnsn_pool *pool, dmnsn_object *A, dmnsn_object *B) { - dmnsn_object *csg = dmnsn_new_object(); + dmnsn_object *csg = dmnsn_new_object(pool); dmnsn_array_push(csg->children, &A); dmnsn_array_push(csg->children, &B); @@ -305,9 +303,9 @@ dmnsn_csg_merge_initialize_fn(dmnsn_object *csg) } dmnsn_object * -dmnsn_new_csg_merge(dmnsn_object *A, dmnsn_object *B) +dmnsn_new_csg_merge(dmnsn_pool *pool, dmnsn_object *A, dmnsn_object *B) { - dmnsn_object *csg = dmnsn_new_object(); + dmnsn_object *csg = dmnsn_new_object(pool); dmnsn_array_push(csg->children, &A); dmnsn_array_push(csg->children, &B); diff --git a/libdimension/cube.c b/libdimension/cube.c index cc6b3ad..8a1a503 100644 --- a/libdimension/cube.c +++ b/libdimension/cube.c @@ -1,5 +1,5 @@ /************************************************************************* - * Copyright (C) 2009-2011 Tavian Barnes <tavianator@tavianator.com> * + * Copyright (C) 2009-2014 Tavian Barnes <tavianator@tavianator.com> * * * * This file is part of The Dimension Library. * * * @@ -131,11 +131,11 @@ dmnsn_cube_inside_fn(const dmnsn_object *cube, dmnsn_vector point) /* Allocate a new cube object */ dmnsn_object * -dmnsn_new_cube(void) +dmnsn_new_cube(dmnsn_pool *pool) { - dmnsn_object *cube = dmnsn_new_object(); - cube->intersection_fn = dmnsn_cube_intersection_fn; - cube->inside_fn = dmnsn_cube_inside_fn; + dmnsn_object *cube = dmnsn_new_object(pool); + cube->intersection_fn = dmnsn_cube_intersection_fn; + cube->inside_fn = dmnsn_cube_inside_fn; cube->bounding_box.min = dmnsn_new_vector(-1.0, -1.0, -1.0); cube->bounding_box.max = dmnsn_new_vector(1.0, 1.0, 1.0); return cube; diff --git a/libdimension/dimension/csg.h b/libdimension/dimension/csg.h index 9d86f71..9e29019 100644 --- a/libdimension/dimension/csg.h +++ b/libdimension/dimension/csg.h @@ -1,5 +1,5 @@ /************************************************************************* - * Copyright (C) 2010 Tavian Barnes <tavianator@tavianator.com> * + * Copyright (C) 2010-2014 Tavian Barnes <tavianator@tavianator.com> * * * * This file is part of The Dimension Library. * * * @@ -25,31 +25,35 @@ /** * CSG union. + * @param[in] pool The memory pool to allocate from. * @param[in] objects The objects from which to compose the union. * @return A union of the objects in \p objects. */ -dmnsn_object *dmnsn_new_csg_union(const dmnsn_array *objects); +dmnsn_object *dmnsn_new_csg_union(dmnsn_pool *pool, const dmnsn_array *objects); /** * CSG intersection. + * @param[in] pool The memory pool to allocate from. * @param[in,out] A The first object. * @param[in,out] B The second object. * @return The intersection of \p A and \p B. */ -dmnsn_object *dmnsn_new_csg_intersection(dmnsn_object *A, dmnsn_object *B); +dmnsn_object *dmnsn_new_csg_intersection(dmnsn_pool *pool, dmnsn_object *A, dmnsn_object *B); /** * CSG intersection. + * @param[in] pool The memory pool to allocate from. * @param[in,out] A The outer object. * @param[in,out] B The inner object. * @return The difference between \p A and \p B. */ -dmnsn_object *dmnsn_new_csg_difference(dmnsn_object *A, dmnsn_object *B); +dmnsn_object *dmnsn_new_csg_difference(dmnsn_pool *pool, dmnsn_object *A, dmnsn_object *B); /** * CSG Merge. + * @param[in] pool The memory pool to allocate from. * @param[in,out] A The first object. * @param[in,out] B The second object. * @return The merge of \p A and \p B. */ -dmnsn_object *dmnsn_new_csg_merge(dmnsn_object *A, dmnsn_object *B); +dmnsn_object *dmnsn_new_csg_merge(dmnsn_pool *pool, dmnsn_object *A, dmnsn_object *B); diff --git a/libdimension/dimension/object.h b/libdimension/dimension/object.h index 75a8aa7..9724dec 100644 --- a/libdimension/dimension/object.h +++ b/libdimension/dimension/object.h @@ -67,12 +67,6 @@ typedef bool dmnsn_object_intersection_fn(const dmnsn_object *object, typedef bool dmnsn_object_inside_fn(const dmnsn_object *object, dmnsn_vector point); -/** - * Object destruction callback. - * @param[in,out] object The object to delete. - */ -typedef void dmnsn_object_free_fn(dmnsn_object *object); - /** An object. */ struct dmnsn_object { dmnsn_texture *texture; /**< Surface properties. */ @@ -88,32 +82,26 @@ struct dmnsn_object { dmnsn_array *children; /**< Child objects. */ bool split_children; /**< Whether the child objects can be split. */ - dmnsn_object_initialize_fn *initialize_fn; /**< Initialization callback. */ + dmnsn_object_initialize_fn *initialize_fn; /**< Initialization callback. */ dmnsn_object_intersection_fn *intersection_fn; /**< Intersection callback. */ - dmnsn_object_inside_fn *inside_fn; /**< Inside callback. */ - dmnsn_object_free_fn *free_fn; /**< Destruction callback. */ + dmnsn_object_inside_fn *inside_fn; /**< Inside callback. */ - DMNSN_REFCOUNT; /**< Reference count. */ bool initialized; /**< @internal Whether the object is initialized yet. */ }; /** * Allocate a dummy object. + * @param[in] pool The memory pool to allocate from. * @return The allocated object. */ -dmnsn_object *dmnsn_new_object(void); +dmnsn_object *dmnsn_new_object(dmnsn_pool *pool); /** * Initialize a dmnsn_object field. + * @param[in] pool The memory pool to allocate from. * @param[out] object The object to initialize. */ -void dmnsn_init_object(dmnsn_object *object); - -/** - * Free an object - * @param[in,out] object The object to destroy. - */ -void dmnsn_delete_object(dmnsn_object *object); +void dmnsn_init_object(dmnsn_pool *pool, dmnsn_object *object); /** * Initialize an object and potentially its children. diff --git a/libdimension/dimension/objects.h b/libdimension/dimension/objects.h index 9ce9e46..eb476cf 100644 --- a/libdimension/dimension/objects.h +++ b/libdimension/dimension/objects.h @@ -1,5 +1,5 @@ /************************************************************************* - * Copyright (C) 2009-2011 Tavian Barnes <tavianator@tavianator.com> * + * Copyright (C) 2009-2014 Tavian Barnes <tavianator@tavianator.com> * * * * This file is part of The Dimension Library. * * * @@ -27,6 +27,7 @@ /** * A triangle, with normals interpolated between the points. + * @param[in] pool The memory pool to allocate from. * @param[in] a The first corner of the triangle. * @param[in] b The second corner of the triangle. * @param[in] c The third corner of the triangle. @@ -35,52 +36,57 @@ * @param[in] nc The normal at \p c. */ dmnsn_object *dmnsn_new_triangle( + dmnsn_pool *pool, dmnsn_vector a, dmnsn_vector b, dmnsn_vector c, dmnsn_vector na, dmnsn_vector nb, dmnsn_vector nc ); /** * A flat triangle, without normal interpolation. + * @param[in] pool The memory pool to allocate from. * @param[in] a The first corner of the triangle. * @param[in] b The second corner of the triangle. * @param[in] c The third corner of the triangle. */ -dmnsn_object *dmnsn_new_flat_triangle( - dmnsn_vector a, dmnsn_vector b, dmnsn_vector c -); +dmnsn_object *dmnsn_new_flat_triangle(dmnsn_pool *pool, dmnsn_vector a, dmnsn_vector b, dmnsn_vector c); /** * A plane. + * @param[in] pool The memory pool to allocate from. * @param[in] normal The normal vector of the plane. * @return A plane through the origin, with the given normal. */ -dmnsn_object *dmnsn_new_plane(dmnsn_vector normal); +dmnsn_object *dmnsn_new_plane(dmnsn_pool *pool, dmnsn_vector normal); /** * A sphere. + * @param[in] pool The memory pool to allocate from. * @return A sphere of radius 1, centered at the origin. */ -dmnsn_object *dmnsn_new_sphere(void); +dmnsn_object *dmnsn_new_sphere(dmnsn_pool *pool); /** * A cube. + * @param[in] pool The memory pool to allocate from. * @return An axis-aligned cube, from (-1, -1, -1) to (1, 1, 1). */ -dmnsn_object *dmnsn_new_cube(void); +dmnsn_object *dmnsn_new_cube(dmnsn_pool *pool); /** * A cylinder/cone. + * @param[in] pool The memory pool to allocate from. * @param[in] r1 The bottom radius. * @param[in] r2 The top radius. * @param[in] open Whether to render caps. * @return A cone slice, from r = \p r1 at y = -1, to r = \p r2 at y = 1 */ -dmnsn_object *dmnsn_new_cone(double r1, double r2, bool open); +dmnsn_object *dmnsn_new_cone(dmnsn_pool *pool, double r1, double r2, bool open); /** * A torus. + * @param[in] pool The memory pool to allocate from. * @param[in] major The major radius. * @param[in] minor The minor radius. * @return A torus, centered at the origin and lying in the x-z plane. */ -dmnsn_object *dmnsn_new_torus(double major, double minor); +dmnsn_object *dmnsn_new_torus(dmnsn_pool *pool, double major, double minor); diff --git a/libdimension/object.c b/libdimension/object.c index 674eea8..432e473 100644 --- a/libdimension/object.c +++ b/libdimension/object.c @@ -26,50 +26,29 @@ #include "dimension-internal.h" #include <stdlib.h> -static void -dmnsn_default_object_free_fn(dmnsn_object *object) -{ - dmnsn_free(object); -} - /* Allocate a dummy object */ dmnsn_object * -dmnsn_new_object(void) +dmnsn_new_object(dmnsn_pool *pool) { - dmnsn_object *object = DMNSN_MALLOC(dmnsn_object); - dmnsn_init_object(object); + dmnsn_object *object = DMNSN_PALLOC(pool, dmnsn_object); + dmnsn_init_object(pool, object); return object; } /* Initialize a dmnsn_object field */ void -dmnsn_init_object(dmnsn_object *object) +dmnsn_init_object(dmnsn_pool *pool, dmnsn_object *object) { object->texture = NULL; object->interior = NULL; object->trans = dmnsn_identity_matrix(); object->intrinsic_trans = dmnsn_identity_matrix(); - object->children = DMNSN_NEW_ARRAY(dmnsn_object *); + object->children = DMNSN_PALLOC_ARRAY(pool, dmnsn_object *); object->split_children = false; object->intersection_fn = NULL; object->inside_fn = NULL; object->initialize_fn = NULL; - object->free_fn = dmnsn_default_object_free_fn; object->initialized = false; - DMNSN_REFCOUNT_INIT(object); -} - -/* Free a dmnsn_object */ -void -dmnsn_delete_object(dmnsn_object *object) -{ - if (DMNSN_DECREF(object)) { - DMNSN_ARRAY_FOREACH (dmnsn_object **, child, object->children) { - dmnsn_delete_object(*child); - } - dmnsn_delete_array(object->children); - object->free_fn(object); - } } /** Recursively initialize objects. */ diff --git a/libdimension/plane.c b/libdimension/plane.c index e2c8ae8..e597306 100644 --- a/libdimension/plane.c +++ b/libdimension/plane.c @@ -43,13 +43,13 @@ static bool dmnsn_plane_inside_fn(const dmnsn_object *plane, /* Allocate a new plane */ dmnsn_object * -dmnsn_new_plane(dmnsn_vector normal) +dmnsn_new_plane(dmnsn_pool *pool, dmnsn_vector normal) { - dmnsn_plane *plane = DMNSN_MALLOC(dmnsn_plane); + dmnsn_plane *plane = DMNSN_PALLOC(pool, dmnsn_plane); plane->normal = normal; dmnsn_object *object = &plane->object; - dmnsn_init_object(object); + dmnsn_init_object(pool, object); object->intersection_fn = dmnsn_plane_intersection_fn; object->inside_fn = dmnsn_plane_inside_fn; object->bounding_box = dmnsn_infinite_bounding_box(); diff --git a/libdimension/scene.c b/libdimension/scene.c index c3aac1f..39be881 100644 --- a/libdimension/scene.c +++ b/libdimension/scene.c @@ -26,22 +26,11 @@ #include "dimension-internal.h" #include <stdlib.h> -static void -dmnsn_scene_cleanup(void *ptr) -{ - dmnsn_scene *scene = ptr; - - DMNSN_ARRAY_FOREACH (dmnsn_object **, object, scene->objects) { - dmnsn_delete_object(*object); - } - dmnsn_delete_array(scene->objects); -} - /* Allocate an empty scene */ dmnsn_scene * dmnsn_new_scene(dmnsn_pool *pool) { - dmnsn_scene *scene = DMNSN_PALLOC_TIDY(pool, dmnsn_scene, dmnsn_scene_cleanup); + dmnsn_scene *scene = DMNSN_PALLOC(pool, dmnsn_scene); scene->background = NULL; scene->default_texture = dmnsn_new_texture(pool); @@ -51,7 +40,7 @@ dmnsn_new_scene(dmnsn_pool *pool) scene->region_y = 0; scene->outer_width = 0; scene->outer_height = 0; - scene->objects = DMNSN_NEW_ARRAY(dmnsn_object *); + scene->objects = DMNSN_PALLOC_ARRAY(pool, dmnsn_object *); scene->lights = DMNSN_PALLOC_ARRAY(pool, dmnsn_light *); scene->camera = NULL; scene->quality = DMNSN_RENDER_FULL; diff --git a/libdimension/sphere.c b/libdimension/sphere.c index 96bc617..821113d 100644 --- a/libdimension/sphere.c +++ b/libdimension/sphere.c @@ -1,5 +1,5 @@ /************************************************************************* - * Copyright (C) 2009-2011 Tavian Barnes <tavianator@tavianator.com> * + * Copyright (C) 2009-2014 Tavian Barnes <tavianator@tavianator.com> * * * * This file is part of The Dimension Library. * * * @@ -60,11 +60,11 @@ dmnsn_sphere_inside_fn(const dmnsn_object *sphere, dmnsn_vector point) /* Allocate a new sphere */ dmnsn_object * -dmnsn_new_sphere(void) +dmnsn_new_sphere(dmnsn_pool *pool) { - dmnsn_object *sphere = dmnsn_new_object(); - sphere->intersection_fn = dmnsn_sphere_intersection_fn; - sphere->inside_fn = dmnsn_sphere_inside_fn; + dmnsn_object *sphere = dmnsn_new_object(pool); + sphere->intersection_fn = dmnsn_sphere_intersection_fn; + sphere->inside_fn = dmnsn_sphere_inside_fn; sphere->bounding_box.min = dmnsn_new_vector(-1.0, -1.0, -1.0); sphere->bounding_box.max = dmnsn_new_vector(1.0, 1.0, 1.0); return sphere; diff --git a/libdimension/tests/prtree.c b/libdimension/tests/prtree.c index 5c377e2..a7db68e 100644 --- a/libdimension/tests/prtree.c +++ b/libdimension/tests/prtree.c @@ -64,11 +64,13 @@ main(void) /* Treat warnings as errors for tests */ dmnsn_die_on_warnings(true); + dmnsn_pool *pool = dmnsn_new_pool(); + const size_t nobjects = 128; - dmnsn_array *objects = DMNSN_NEW_ARRAY(dmnsn_object *); + dmnsn_array *objects = DMNSN_PALLOC_ARRAY(pool, dmnsn_object *); for (size_t i = 0; i < nobjects; ++i) { - dmnsn_object *object = dmnsn_new_object(); + dmnsn_object *object = dmnsn_new_object(pool); dmnsn_randomize_bounding_box(object); object->intersection_fn = dmnsn_fake_intersection_fn; object->trans_inv = dmnsn_identity_matrix(); @@ -96,9 +98,6 @@ main(void) } dmnsn_delete_bvh(bvh); - DMNSN_ARRAY_FOREACH (dmnsn_object **, object, objects) { - dmnsn_delete_object(*object); - } - dmnsn_delete_array(objects); + dmnsn_delete_pool(pool); return EXIT_SUCCESS; } diff --git a/libdimension/tests/render.c b/libdimension/tests/render.c index d093947..f12aa8b 100644 --- a/libdimension/tests/render.c +++ b/libdimension/tests/render.c @@ -118,7 +118,7 @@ dmnsn_test_scene_add_lights(dmnsn_pool *pool, dmnsn_scene *scene) static void dmnsn_test_scene_add_hollow_cube(dmnsn_pool *pool, dmnsn_scene *scene) { - dmnsn_object *cube = dmnsn_new_cube(); + dmnsn_object *cube = dmnsn_new_cube(pool); cube->trans = dmnsn_rotation_matrix( dmnsn_new_vector(dmnsn_radians(45.0), 0.0, 0.0) ); @@ -133,13 +133,13 @@ dmnsn_test_scene_add_hollow_cube(dmnsn_pool *pool, dmnsn_scene *scene) cube->interior = dmnsn_new_interior(pool); cube->interior->ior = 1.1; - dmnsn_object *sphere = dmnsn_new_sphere(); + dmnsn_object *sphere = dmnsn_new_sphere(pool); sphere->texture = dmnsn_new_texture(pool); sphere->texture->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_green)); sphere->texture->finish.specular = dmnsn_new_phong(pool, dmnsn_sRGB_inverse_gamma(0.2), 40.0); sphere->trans = dmnsn_scale_matrix(dmnsn_new_vector(1.25, 1.25, 1.25)); - dmnsn_object *hollow_cube = dmnsn_new_csg_difference(cube, sphere); + dmnsn_object *hollow_cube = dmnsn_new_csg_difference(pool, cube, sphere); dmnsn_array_push(scene->objects, &hollow_cube); } @@ -155,18 +155,18 @@ dmnsn_test_scene_add_spike(dmnsn_pool *pool, dmnsn_scene *scene) { dmnsn_array *arrow_array = DMNSN_NEW_ARRAY(dmnsn_object *); - dmnsn_object *cylinder = dmnsn_new_cone(0.1, 0.1, false); + dmnsn_object *cylinder = dmnsn_new_cone(pool, 0.1, 0.1, false); cylinder->trans = dmnsn_scale_matrix(dmnsn_new_vector(1.0, 1.25, 1.0)); dmnsn_array_push(arrow_array, &cylinder); - dmnsn_object *cone = dmnsn_new_cone(0.1, 0.0, true); + dmnsn_object *cone = dmnsn_new_cone(pool, 0.1, 0.0, true); cone->trans = dmnsn_matrix_mul( dmnsn_translation_matrix(dmnsn_new_vector(0.0, 1.375, 0.0)), dmnsn_scale_matrix(dmnsn_new_vector(1.0, 0.125, 1.0)) ); dmnsn_array_push(arrow_array, &cone); - dmnsn_object *arrow = dmnsn_new_csg_union(arrow_array); + dmnsn_object *arrow = dmnsn_new_csg_union(pool, arrow_array); dmnsn_delete_array(arrow_array); dmnsn_pattern *gradient = dmnsn_new_gradient_pattern(pool, dmnsn_y); dmnsn_map *gradient_pigment_map = dmnsn_new_pigment_map(pool); @@ -190,18 +190,18 @@ dmnsn_test_scene_add_spike(dmnsn_pool *pool, dmnsn_scene *scene) dmnsn_array *torus_array = DMNSN_NEW_ARRAY(dmnsn_object *); - dmnsn_object *torus1 = dmnsn_new_torus(0.15, 0.05); + dmnsn_object *torus1 = dmnsn_new_torus(pool, 0.15, 0.05); torus1->trans = dmnsn_translation_matrix(dmnsn_new_vector(0.0, -1.0, 0.0)); dmnsn_array_push(torus_array, &torus1); - dmnsn_object *torus2 = dmnsn_new_torus(0.15, 0.05); + dmnsn_object *torus2 = dmnsn_new_torus(pool, 0.15, 0.05); dmnsn_array_push(torus_array, &torus2); - dmnsn_object *torus3 = dmnsn_new_torus(0.15, 0.05); + dmnsn_object *torus3 = dmnsn_new_torus(pool, 0.15, 0.05); torus3->trans = dmnsn_translation_matrix(dmnsn_new_vector(0.0, 1.0, 0.0)); dmnsn_array_push(torus_array, &torus3); - dmnsn_object *torii = dmnsn_new_csg_union(torus_array); + dmnsn_object *torii = dmnsn_new_csg_union(pool, torus_array); dmnsn_delete_array(torus_array); torii->texture = dmnsn_new_texture(pool); torii->texture->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_blue)); @@ -210,7 +210,7 @@ dmnsn_test_scene_add_spike(dmnsn_pool *pool, dmnsn_scene *scene) dmnsn_array *spike_array = DMNSN_NEW_ARRAY(dmnsn_object *); dmnsn_array_push(spike_array, &arrow); dmnsn_array_push(spike_array, &torii); - dmnsn_object *spike = dmnsn_new_csg_union(spike_array); + dmnsn_object *spike = dmnsn_new_csg_union(pool, spike_array); dmnsn_delete_array(spike_array); spike->trans = dmnsn_rotation_matrix( dmnsn_new_vector(dmnsn_radians(-45.0), 0.0, 0.0) @@ -234,7 +234,7 @@ dmnsn_test_scene_add_triangle_strip(dmnsn_pool *pool, dmnsn_scene *scene) strip_textures[1]->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_orange)); strip_textures[2]->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_yellow)); for (unsigned int i = 0; i < 128; ++i) { - dmnsn_object *triangle = dmnsn_new_flat_triangle(a, b, c); + dmnsn_object *triangle = dmnsn_new_flat_triangle(pool, a, b, c); triangle->texture = strip_textures[i%3]; dmnsn_array_push(strip_array, &triangle); @@ -243,7 +243,7 @@ dmnsn_test_scene_add_triangle_strip(dmnsn_pool *pool, dmnsn_scene *scene) c = dmnsn_vector_add(a, dmnsn_z); } - dmnsn_object *strip = dmnsn_new_csg_union(strip_array); + dmnsn_object *strip = dmnsn_new_csg_union(pool, strip_array); dmnsn_delete_array(strip_array); strip->trans = dmnsn_translation_matrix(dmnsn_new_vector(5.0, -2.0, -4.0)); dmnsn_array_push(scene->objects, &strip); @@ -252,7 +252,7 @@ dmnsn_test_scene_add_triangle_strip(dmnsn_pool *pool, dmnsn_scene *scene) static void dmnsn_test_scene_add_ground(dmnsn_pool *pool, dmnsn_scene *scene) { - dmnsn_object *plane = dmnsn_new_plane(dmnsn_new_vector(0.0, 1.0, 0.0)); + dmnsn_object *plane = dmnsn_new_plane(pool, dmnsn_new_vector(0.0, 1.0, 0.0)); plane->trans = dmnsn_translation_matrix(dmnsn_new_vector(0.0, -2.0, 0.0)); dmnsn_pattern *checker = dmnsn_new_checker_pattern(pool); dmnsn_map *small_map = dmnsn_new_pigment_map(pool); diff --git a/libdimension/torus.c b/libdimension/torus.c index 158e196..5f2085c 100644 --- a/libdimension/torus.c +++ b/libdimension/torus.c @@ -46,8 +46,7 @@ dmnsn_torus_bound_intersection(const dmnsn_torus *torus, dmnsn_line l) dmnsn_vector upper = dmnsn_line_point(l, tupper); double ldist2 = lower.x*lower.x + lower.z*lower.z; double udist2 = upper.x*upper.x + upper.z*upper.z; - if ((ldist2 < rmin2 || ldist2 > rmax2) && (udist2 < rmin2 || udist2 > rmax2)) - { + if ((ldist2 < rmin2 || ldist2 > rmax2) && (udist2 < rmin2 || udist2 > rmax2)) { /* No valid intersection with the caps, try the cylinder walls */ double dist2 = l.x0.x*l.x0.x + l.x0.z*l.x0.z; double bigcyl[3], smallcyl[3]; @@ -143,14 +142,14 @@ dmnsn_torus_inside_fn(const dmnsn_object *object, dmnsn_vector point) /* Allocate a new torus */ dmnsn_object * -dmnsn_new_torus(double major, double minor) +dmnsn_new_torus(dmnsn_pool *pool, double major, double minor) { - dmnsn_torus *torus = DMNSN_MALLOC(dmnsn_torus); + dmnsn_torus *torus = DMNSN_PALLOC(pool, dmnsn_torus); torus->major = major; torus->minor = minor; dmnsn_object *object = &torus->object; - dmnsn_init_object(object); + dmnsn_init_object(pool, object); object->intersection_fn = dmnsn_torus_intersection_fn; object->inside_fn = dmnsn_torus_inside_fn; diff --git a/libdimension/triangle.c b/libdimension/triangle.c index afe9065..94a6a95 100644 --- a/libdimension/triangle.c +++ b/libdimension/triangle.c @@ -66,20 +66,21 @@ dmnsn_triangle_inside_fn(const dmnsn_object *triangle, dmnsn_vector point) /* Allocate a new triangle */ dmnsn_object * -dmnsn_new_triangle(dmnsn_vector a, dmnsn_vector b, dmnsn_vector c, +dmnsn_new_triangle(dmnsn_pool *pool, + dmnsn_vector a, dmnsn_vector b, dmnsn_vector c, dmnsn_vector na, dmnsn_vector nb, dmnsn_vector nc) { na = dmnsn_vector_normalized(na); nb = dmnsn_vector_normalized(nb); nc = dmnsn_vector_normalized(nc); - dmnsn_triangle *triangle = DMNSN_MALLOC(dmnsn_triangle); + dmnsn_triangle *triangle = DMNSN_PALLOC(pool, dmnsn_triangle); triangle->na = na; triangle->nab = dmnsn_vector_sub(nb, na); triangle->nac = dmnsn_vector_sub(nc, na); dmnsn_object *object = &triangle->object; - dmnsn_init_object(object); + dmnsn_init_object(pool, object); object->intersection_fn = dmnsn_triangle_intersection_fn; object->inside_fn = dmnsn_triangle_inside_fn; object->bounding_box.min = dmnsn_zero; @@ -101,7 +102,7 @@ dmnsn_new_triangle(dmnsn_vector a, dmnsn_vector b, dmnsn_vector c, /* Allocate a new flat triangle */ dmnsn_object * -dmnsn_new_flat_triangle(dmnsn_vector a, dmnsn_vector b, dmnsn_vector c) +dmnsn_new_flat_triangle(dmnsn_pool *pool, dmnsn_vector a, dmnsn_vector b, dmnsn_vector c) { /* Flat triangles are just smooth triangles with identical normals at all verticies */ @@ -109,5 +110,5 @@ dmnsn_new_flat_triangle(dmnsn_vector a, dmnsn_vector b, dmnsn_vector c) dmnsn_vector_sub(b, a), dmnsn_vector_sub(c, a) ); - return dmnsn_new_triangle(a, b, c, normal, normal, normal); + return dmnsn_new_triangle(pool, a, b, c, normal, normal, normal); } |