diff options
Diffstat (limited to 'libdimension')
-rw-r--r-- | libdimension/cone.c | 35 | ||||
-rw-r--r-- | libdimension/csg.c | 30 | ||||
-rw-r--r-- | libdimension/dimension/object.h | 2 | ||||
-rw-r--r-- | libdimension/plane.c | 13 | ||||
-rw-r--r-- | libdimension/torus.c | 32 | ||||
-rw-r--r-- | libdimension/triangle.c | 15 |
6 files changed, 69 insertions, 58 deletions
diff --git a/libdimension/cone.c b/libdimension/cone.c index fe01e85..d0ae28e 100644 --- a/libdimension/cone.c +++ b/libdimension/cone.c @@ -127,13 +127,15 @@ dmnsn_object * dmnsn_new_cone_cap(double r) { dmnsn_cone_cap *cap = DMNSN_MALLOC(dmnsn_cone_cap); - dmnsn_init_object(&cap->object); - cap->object.intersection_fn = dmnsn_cone_cap_intersection_fn; - cap->object.inside_fn = dmnsn_cone_cap_inside_fn; - cap->object.bounding_box.min = dmnsn_new_vector(-r, 0.0, -r); - cap->object.bounding_box.max = dmnsn_new_vector(+r, 0.0, +r); cap->r = r; - return &cap->object; + + dmnsn_object *object = &cap->object; + dmnsn_init_object(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); + object->bounding_box.max = dmnsn_new_vector(+r, 0.0, +r); + return object; } /* Allocate a new cone object */ @@ -141,26 +143,29 @@ dmnsn_object * dmnsn_new_cone(double r1, double r2, bool open) { dmnsn_cone *cone = DMNSN_MALLOC(dmnsn_cone); - dmnsn_init_object(&cone->object); - cone->object.intersection_fn = dmnsn_cone_intersection_fn; - cone->object.inside_fn = dmnsn_cone_inside_fn; - double rmax = dmnsn_max(r1, r2); - cone->object.bounding_box.min = dmnsn_new_vector(-rmax, -1.0, -rmax); - cone->object.bounding_box.max = dmnsn_new_vector(rmax, 1.0, rmax); cone->r1 = r1; cone->r2 = r2; + dmnsn_object *object = &cone->object; + dmnsn_init_object(object); + object->intersection_fn = dmnsn_cone_intersection_fn; + object->inside_fn = dmnsn_cone_inside_fn; + + double rmax = dmnsn_max(r1, r2); + object->bounding_box.min = dmnsn_new_vector(-rmax, -1.0, -rmax); + object->bounding_box.max = dmnsn_new_vector(rmax, 1.0, rmax); + if (open) { - return &cone->object; + return object; } /* 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); - cap1->intrinsic_trans = dmnsn_translation_matrix( + cap1->intrinsic_trans = dmnsn_translation_matrix( dmnsn_new_vector(0.0, -1.0, 0.0) ); - cap2->intrinsic_trans = dmnsn_translation_matrix( + cap2->intrinsic_trans = dmnsn_translation_matrix( dmnsn_new_vector(0.0, +1.0, 0.0) ); /* Flip the normal around for the top cap */ diff --git a/libdimension/csg.c b/libdimension/csg.c index b364e27..38c5983 100644 --- a/libdimension/csg.c +++ b/libdimension/csg.c @@ -41,16 +41,16 @@ dmnsn_csg_union_intersection_fn(const dmnsn_object *object, dmnsn_line line, dmnsn_intersection *intersection) { - dmnsn_bvh *bvh = ((const dmnsn_csg_union *)object)->bvh; - return dmnsn_bvh_intersection(bvh, line, intersection, true); + const dmnsn_csg_union *csg = (const dmnsn_csg_union *)object; + return dmnsn_bvh_intersection(csg->bvh, line, intersection, true); } /** CSG union inside callback. */ static bool dmnsn_csg_union_inside_fn(const dmnsn_object *object, dmnsn_vector point) { - dmnsn_bvh *bvh = ((const dmnsn_csg_union *)object)->bvh; - return dmnsn_bvh_inside(bvh, point); + const dmnsn_csg_union *csg = (const dmnsn_csg_union *)object; + return dmnsn_bvh_inside(csg->bvh, point); } /** CSG union initialization callback. */ @@ -79,19 +79,21 @@ dmnsn_object * dmnsn_new_csg_union(const dmnsn_array *objects) { dmnsn_csg_union *csg = DMNSN_MALLOC(dmnsn_csg_union); - dmnsn_init_object(&csg->object); + csg->bvh = NULL; + + dmnsn_object *object = &csg->object; + dmnsn_init_object(object); - DMNSN_ARRAY_FOREACH (dmnsn_object **, object, objects) { - dmnsn_array_push(csg->object.children, object); + DMNSN_ARRAY_FOREACH (dmnsn_object **, child, objects) { + dmnsn_array_push(object->children, child); } - csg->object.split_children = true; - csg->object.intersection_fn = dmnsn_csg_union_intersection_fn; - csg->object.inside_fn = dmnsn_csg_union_inside_fn; - csg->object.initialize_fn = dmnsn_csg_union_initialize_fn; - csg->object.free_fn = dmnsn_csg_union_free_fn; - csg->bvh = NULL; + 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; - return &csg->object; + return object; } /** diff --git a/libdimension/dimension/object.h b/libdimension/dimension/object.h index 4d75582..8d76cc7 100644 --- a/libdimension/dimension/object.h +++ b/libdimension/dimension/object.h @@ -69,7 +69,7 @@ typedef bool dmnsn_object_inside_fn(const dmnsn_object *object, /** * Object destruction callback. - * @param[in] object The object to delete. + * @param[in,out] object The object to delete. */ typedef void dmnsn_object_free_fn(dmnsn_object *object); diff --git a/libdimension/plane.c b/libdimension/plane.c index dc7fc1a..e2c8ae8 100644 --- a/libdimension/plane.c +++ b/libdimension/plane.c @@ -46,13 +46,14 @@ dmnsn_object * dmnsn_new_plane(dmnsn_vector normal) { dmnsn_plane *plane = DMNSN_MALLOC(dmnsn_plane); - dmnsn_init_object(&plane->object); - - plane->object.intersection_fn = dmnsn_plane_intersection_fn; - plane->object.inside_fn = dmnsn_plane_inside_fn; - plane->object.bounding_box = dmnsn_infinite_bounding_box(); plane->normal = normal; - return &plane->object; + + dmnsn_object *object = &plane->object; + dmnsn_init_object(object); + object->intersection_fn = dmnsn_plane_intersection_fn; + object->inside_fn = dmnsn_plane_inside_fn; + object->bounding_box = dmnsn_infinite_bounding_box(); + return object; } /* Returns the closest intersection of `line' with `plane' */ diff --git a/libdimension/torus.c b/libdimension/torus.c index 53e8fcb..158e196 100644 --- a/libdimension/torus.c +++ b/libdimension/torus.c @@ -83,7 +83,7 @@ dmnsn_torus_intersection_fn(const dmnsn_object *object, dmnsn_line l, { const dmnsn_torus *torus = (const dmnsn_torus *)object; double R = torus->major, r = torus->minor; - double R2 = R*R, r2 = r*r; + double RR = R*R, rr = r*r; if (!dmnsn_torus_bound_intersection(torus, l)) { return false; @@ -102,9 +102,9 @@ dmnsn_torus_intersection_fn(const dmnsn_object *object, dmnsn_line l, double poly[5]; poly[4] = nn*nn; poly[3] = 4*nn*nx0; - poly[2] = 2.0*(nn*(x0x0 - r2) + 2.0*nx0*nx0 - R2*nnmod); - poly[1] = 4.0*(nx0*(x0x0 - r2) - R2*nx0mod); - poly[0] = x0x0*x0x0 + R2*(R2 - 2.0*x0x0mod) - r2*(2.0*(R2 + x0x0) - r2); + poly[2] = 2.0*(nn*(x0x0 - rr) + 2.0*nx0*nx0 - RR*nnmod); + poly[1] = 4.0*(nx0*(x0x0 - rr) - RR*nx0mod); + poly[0] = x0x0*x0x0 + RR*(RR - 2.0*x0x0mod) - rr*(2.0*(RR + x0x0) - rr); double x[4]; size_t n = dmnsn_polynomial_solve(poly, 4, x); @@ -116,8 +116,9 @@ dmnsn_torus_intersection_fn(const dmnsn_object *object, dmnsn_line l, t = dmnsn_min(t, x[i]); } - if (t < 0.0) + if (t < 0.0) { return false; + } dmnsn_vector p = dmnsn_line_point(l, t); dmnsn_vector center = dmnsn_vector_mul( @@ -145,16 +146,17 @@ dmnsn_object * dmnsn_new_torus(double major, double minor) { dmnsn_torus *torus = DMNSN_MALLOC(dmnsn_torus); - dmnsn_init_object(&torus->object); - torus->object.intersection_fn = dmnsn_torus_intersection_fn; - torus->object.inside_fn = dmnsn_torus_inside_fn; - torus->object.bounding_box.min = dmnsn_new_vector( - -(major + minor), -minor, -(major + minor) - ); - torus->object.bounding_box.max = dmnsn_new_vector( - major + minor, minor, major + minor - ); torus->major = major; torus->minor = minor; - return &torus->object; + + dmnsn_object *object = &torus->object; + dmnsn_init_object(object); + object->intersection_fn = dmnsn_torus_intersection_fn; + object->inside_fn = dmnsn_torus_inside_fn; + + double extent = major + minor; + object->bounding_box.min = dmnsn_new_vector(-extent, -minor, -extent); + object->bounding_box.max = dmnsn_new_vector(extent, minor, extent); + + return object; } diff --git a/libdimension/triangle.c b/libdimension/triangle.c index 9ddc3fc..afe9065 100644 --- a/libdimension/triangle.c +++ b/libdimension/triangle.c @@ -78,11 +78,12 @@ dmnsn_new_triangle(dmnsn_vector a, dmnsn_vector b, dmnsn_vector c, triangle->nab = dmnsn_vector_sub(nb, na); triangle->nac = dmnsn_vector_sub(nc, na); - dmnsn_init_object(&triangle->object); - triangle->object.intersection_fn = dmnsn_triangle_intersection_fn; - triangle->object.inside_fn = dmnsn_triangle_inside_fn; - triangle->object.bounding_box.min = dmnsn_zero; - triangle->object.bounding_box.max = dmnsn_new_vector(1.0, 1.0, 0.0); + dmnsn_object *object = &triangle->object; + dmnsn_init_object(object); + object->intersection_fn = dmnsn_triangle_intersection_fn; + object->inside_fn = dmnsn_triangle_inside_fn; + object->bounding_box.min = dmnsn_zero; + object->bounding_box.max = dmnsn_new_vector(1.0, 1.0, 0.0); /* * Make a change-of-basis matrix @@ -93,9 +94,9 @@ dmnsn_new_triangle(dmnsn_vector a, dmnsn_vector b, dmnsn_vector c, dmnsn_vector ab = dmnsn_vector_sub(b, a); dmnsn_vector ac = dmnsn_vector_sub(c, a); dmnsn_vector normal = dmnsn_vector_cross(ab, ac); - triangle->object.intrinsic_trans = dmnsn_new_matrix4(ab, ac, normal, a); + object->intrinsic_trans = dmnsn_new_matrix4(ab, ac, normal, a); - return &triangle->object; + return object; } /* Allocate a new flat triangle */ |