diff options
Diffstat (limited to 'libdimension/triangle.c')
-rw-r--r-- | libdimension/triangle.c | 40 |
1 files changed, 20 insertions, 20 deletions
diff --git a/libdimension/triangle.c b/libdimension/triangle.c index 0e6daf4..9ddc3fc 100644 --- a/libdimension/triangle.c +++ b/libdimension/triangle.c @@ -25,16 +25,18 @@ #include "dimension.h" -typedef struct dmnsn_triangle_payload { +/** Triangle type. */ +typedef struct { + dmnsn_object object; dmnsn_vector na, nab, nac; -} dmnsn_triangle_payload; +} dmnsn_triangle; /** Triangle intersection callback. */ static bool -dmnsn_triangle_intersection_fn(const dmnsn_object *triangle, dmnsn_line l, +dmnsn_triangle_intersection_fn(const dmnsn_object *object, dmnsn_line l, dmnsn_intersection *intersection) { - const dmnsn_triangle_payload *payload = triangle->ptr; + const dmnsn_triangle *triangle = (const dmnsn_triangle *)object; /* See the change of basis in dmnsn_new_triangle() */ double t = -l.x0.z/l.n.z; @@ -43,10 +45,10 @@ dmnsn_triangle_intersection_fn(const dmnsn_object *triangle, dmnsn_line l, if (t >= 0.0 && u >= 0.0 && v >= 0.0 && u + v <= 1.0) { intersection->t = t; intersection->normal = dmnsn_vector_add( - payload->na, + triangle->na, dmnsn_vector_add( - dmnsn_vector_mul(u, payload->nab), - dmnsn_vector_mul(v, payload->nac) + dmnsn_vector_mul(u, triangle->nab), + dmnsn_vector_mul(v, triangle->nac) ) ); return true; @@ -71,18 +73,16 @@ dmnsn_new_triangle(dmnsn_vector a, dmnsn_vector b, dmnsn_vector c, nb = dmnsn_vector_normalized(nb); nc = dmnsn_vector_normalized(nc); - dmnsn_triangle_payload *payload = DMNSN_MALLOC(dmnsn_triangle_payload); - payload->na = na; - payload->nab = dmnsn_vector_sub(nb, na); - payload->nac = dmnsn_vector_sub(nc, na); + dmnsn_triangle *triangle = DMNSN_MALLOC(dmnsn_triangle); + triangle->na = na; + triangle->nab = dmnsn_vector_sub(nb, na); + triangle->nac = dmnsn_vector_sub(nc, na); - dmnsn_object *triangle = dmnsn_new_object(); - triangle->ptr = payload; - triangle->intersection_fn = dmnsn_triangle_intersection_fn; - triangle->inside_fn = dmnsn_triangle_inside_fn; - triangle->free_fn = dmnsn_free; - triangle->bounding_box.min = dmnsn_zero; - triangle->bounding_box.max = dmnsn_new_vector(1.0, 1.0, 0.0); + 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); /* * Make a change-of-basis matrix @@ -93,9 +93,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->intrinsic_trans = dmnsn_new_matrix4(ab, ac, normal, a); + triangle->object.intrinsic_trans = dmnsn_new_matrix4(ab, ac, normal, a); - return triangle; + return &triangle->object; } /* Allocate a new flat triangle */ |