diff options
Diffstat (limited to 'libdimension')
-rw-r--r-- | libdimension/cameras.c | 4 | ||||
-rw-r--r-- | libdimension/dimension/geometry.h | 12 | ||||
-rw-r--r-- | libdimension/geometry.c | 90 | ||||
-rw-r--r-- | libdimension/kD_splay_tree.c | 14 | ||||
-rw-r--r-- | libdimension/objects.c | 20 | ||||
-rw-r--r-- | libdimension/raytrace.c | 9 |
6 files changed, 72 insertions, 77 deletions
diff --git a/libdimension/cameras.c b/libdimension/cameras.c index 54c7e7f..3389cc7 100644 --- a/libdimension/cameras.c +++ b/libdimension/cameras.c @@ -77,10 +77,10 @@ dmnsn_perspective_camera_ray_fn(const dmnsn_camera *camera, dmnsn_line l; /* Rays originate at the origin, oddly enough */ - l.x0 = dmnsn_vector_construct(0.0, 0.0, 0.0); + l.x0 = dmnsn_new_vector(0.0, 0.0, 0.0); /* Aim at the z = 1 plane */ - l.n = dmnsn_vector_construct(x - 0.5, y - 0.5, 1.0); + l.n = dmnsn_new_vector(x - 0.5, y - 0.5, 1.0); return dmnsn_matrix_line_mul(*trans, l); } diff --git a/libdimension/dimension/geometry.h b/libdimension/dimension/geometry.h index 08ac1b3..68359aa 100644 --- a/libdimension/dimension/geometry.h +++ b/libdimension/dimension/geometry.h @@ -42,17 +42,17 @@ typedef struct { /* Shorthand for vector/matrix construction */ DMNSN_INLINE dmnsn_vector -dmnsn_vector_construct(double x, double y, double z) +dmnsn_new_vector(double x, double y, double z) { dmnsn_vector v = { x, y, z }; return v; } DMNSN_INLINE dmnsn_matrix -dmnsn_matrix_construct(double a0, double a1, double a2, double a3, - double b0, double b1, double b2, double b3, - double c0, double c1, double c2, double c3, - double d0, double d1, double d2, double d3) +dmnsn_new_matrix(double a0, double a1, double a2, double a3, + double b0, double b1, double b2, double b3, + double c0, double c1, double c2, double c3, + double d0, double d1, double d2, double d3) { dmnsn_matrix m = { { { a0, a1, a2, a3 }, { b0, b1, b2, b3 }, @@ -68,7 +68,7 @@ dmnsn_matrix dmnsn_translation_matrix(dmnsn_vector d); dmnsn_matrix dmnsn_rotation_matrix(dmnsn_vector theta); DMNSN_INLINE dmnsn_line -dmnsn_line_construct(dmnsn_vector x0, dmnsn_vector n) +dmnsn_new_line(dmnsn_vector x0, dmnsn_vector n) { dmnsn_line l = { x0, n }; return l; diff --git a/libdimension/geometry.c b/libdimension/geometry.c index 6df0102..cbcc2c1 100644 --- a/libdimension/geometry.c +++ b/libdimension/geometry.c @@ -25,30 +25,30 @@ dmnsn_matrix dmnsn_identity_matrix() { - return dmnsn_matrix_construct(1.0, 0.0, 0.0, 0.0, - 0.0, 1.0, 0.0, 0.0, - 0.0, 0.0, 1.0, 0.0, - 0.0, 0.0, 0.0, 1.0); + return dmnsn_new_matrix(1.0, 0.0, 0.0, 0.0, + 0.0, 1.0, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, + 0.0, 0.0, 0.0, 1.0); } /* Scaling matrix */ dmnsn_matrix dmnsn_scale_matrix(dmnsn_vector s) { - return dmnsn_matrix_construct(s.x, 0.0, 0.0, 0.0, - 0.0, s.y, 0.0, 0.0, - 0.0, 0.0, s.z, 0.0, - 0.0, 0.0, 0.0, 1.0); + return dmnsn_new_matrix(s.x, 0.0, 0.0, 0.0, + 0.0, s.y, 0.0, 0.0, + 0.0, 0.0, s.z, 0.0, + 0.0, 0.0, 0.0, 1.0); } /* Translation matrix */ dmnsn_matrix dmnsn_translation_matrix(dmnsn_vector d) { - return dmnsn_matrix_construct(1.0, 0.0, 0.0, d.x, - 0.0, 1.0, 0.0, d.y, - 0.0, 0.0, 1.0, d.z, - 0.0, 0.0, 0.0, 1.0); + return dmnsn_new_matrix(1.0, 0.0, 0.0, d.x, + 0.0, 1.0, 0.0, d.y, + 0.0, 0.0, 1.0, d.z, + 0.0, 0.0, 0.0, 1.0); } /* Left-handed rotation matrix; theta/|theta| = axis, |theta| = angle */ @@ -56,25 +56,23 @@ dmnsn_matrix dmnsn_rotation_matrix(dmnsn_vector theta) { /* Two trig calls, 25 multiplications, 13 additions */ - dmnsn_vector axis; - double angle, s, t, x, y, z; - angle = dmnsn_vector_norm(theta); + double angle = dmnsn_vector_norm(theta); if (angle == 0.0) { return dmnsn_identity_matrix(); } - axis = dmnsn_vector_normalize(theta); + dmnsn_vector axis = dmnsn_vector_normalize(theta); - /* Shorthand to make dmnsn_matrix_construct() call legible */ + /* Shorthand to make dmnsn_new_matrix() call legible */ - s = sin(angle); - t = 1.0 - cos(angle); + double s = sin(angle); + double t = 1.0 - cos(angle); - x = axis.x; - y = axis.y; - z = axis.z; + double x = axis.x; + double y = axis.y; + double z = axis.z; - return dmnsn_matrix_construct( + return dmnsn_new_matrix( 1.0 + t*(x*x - 1.0), -z*s + t*x*y, y*s + t*x*z, 0.0, z*s + t*x*y, 1.0 + t*(y*y - 1.0), -x*s + t*y*z, 0.0, -y*s + t*x*z, x*s + t*y*z, 1.0 + t*(z*z - 1.0), 0.0, @@ -86,8 +84,8 @@ dmnsn_rotation_matrix(dmnsn_vector theta) typedef struct { double n[2][2]; } dmnsn_matrix2; -static dmnsn_matrix2 dmnsn_matrix2_construct(double a1, double a2, - double b1, double b2); +static dmnsn_matrix2 dmnsn_new_matrix2(double a1, double a2, + double b1, double b2); static dmnsn_matrix2 dmnsn_matrix2_inverse(dmnsn_matrix2 A); static dmnsn_matrix2 dmnsn_matrix2_negate(dmnsn_matrix2 A); static dmnsn_matrix2 dmnsn_matrix2_sub(dmnsn_matrix2 lhs, dmnsn_matrix2 rhs); @@ -134,18 +132,18 @@ dmnsn_matrix_inverse(dmnsn_matrix A) } /* Partition the matrix */ - P = dmnsn_matrix2_construct(A.n[0][0], A.n[0][1], - A.n[1][0], A.n[1][1]); - Q = dmnsn_matrix2_construct(A.n[0][2], A.n[0][3], - A.n[1][2], A.n[1][3]); - R = dmnsn_matrix2_construct(A.n[2][0], A.n[2][1], - A.n[3][0], A.n[3][1]); - S = dmnsn_matrix2_construct(A.n[2][2], A.n[2][3], - A.n[3][2], A.n[3][3]); + P = dmnsn_new_matrix2(A.n[0][0], A.n[0][1], + A.n[1][0], A.n[1][1]); + Q = dmnsn_new_matrix2(A.n[0][2], A.n[0][3], + A.n[1][2], A.n[1][3]); + R = dmnsn_new_matrix2(A.n[2][0], A.n[2][1], + A.n[3][0], A.n[3][1]); + S = dmnsn_new_matrix2(A.n[2][2], A.n[2][3], + A.n[3][2], A.n[3][3]); /* Do this inversion ourselves, since we already have the determinant */ - Pi = dmnsn_matrix2_construct( P.n[1][1]/Pdet, -P.n[0][1]/Pdet, - -P.n[1][0]/Pdet, P.n[0][0]/Pdet); + Pi = dmnsn_new_matrix2( P.n[1][1]/Pdet, -P.n[0][1]/Pdet, + -P.n[1][0]/Pdet, P.n[0][0]/Pdet); /* Calculate R*inv(P), inv(P)*Q, and R*inv(P)*Q */ RPi = dmnsn_matrix2_mul(R, Pi); @@ -159,15 +157,15 @@ dmnsn_matrix_inverse(dmnsn_matrix A) PP = dmnsn_matrix2_sub(Pi, dmnsn_matrix2_mul(PiQ, RR)); /* Reconstruct the matrix */ - return dmnsn_matrix_construct(PP.n[0][0], PP.n[0][1], QQ.n[0][0], QQ.n[0][1], - PP.n[1][0], PP.n[1][1], QQ.n[1][0], QQ.n[1][1], - RR.n[0][0], RR.n[0][1], SS.n[0][0], SS.n[0][1], - RR.n[1][0], RR.n[1][1], SS.n[1][0], SS.n[1][1]); + return dmnsn_new_matrix(PP.n[0][0], PP.n[0][1], QQ.n[0][0], QQ.n[0][1], + PP.n[1][0], PP.n[1][1], QQ.n[1][0], QQ.n[1][1], + RR.n[0][0], RR.n[0][1], SS.n[0][0], SS.n[0][1], + RR.n[1][0], RR.n[1][1], SS.n[1][0], SS.n[1][1]); } /* For nice shorthand */ static dmnsn_matrix2 -dmnsn_matrix2_construct(double a1, double a2, double b1, double b2) +dmnsn_new_matrix2(double a1, double a2, double b1, double b2) { dmnsn_matrix2 m = { { { a1, a2 }, { b1, b2 } } }; @@ -180,16 +178,16 @@ dmnsn_matrix2_inverse(dmnsn_matrix2 A) { /* 4 divisions, 2 multiplications, 1 addition */ double det = A.n[0][0]*A.n[1][1] - A.n[0][1]*A.n[1][0]; - return dmnsn_matrix2_construct( A.n[1][1]/det, -A.n[0][1]/det, - -A.n[1][0]/det, A.n[0][0]/det); + return dmnsn_new_matrix2( A.n[1][1]/det, -A.n[0][1]/det, + -A.n[1][0]/det, A.n[0][0]/det); } /* Also basically a shorthand */ static dmnsn_matrix2 dmnsn_matrix2_negate(dmnsn_matrix2 A) { - return dmnsn_matrix2_construct(-A.n[0][0], -A.n[0][1], - -A.n[1][0], -A.n[1][1]); + return dmnsn_new_matrix2(-A.n[0][0], -A.n[0][1], + -A.n[1][0], -A.n[1][1]); } /* 2x2 matrix subtraction */ @@ -197,7 +195,7 @@ static dmnsn_matrix2 dmnsn_matrix2_sub(dmnsn_matrix2 lhs, dmnsn_matrix2 rhs) { /* 4 additions */ - return dmnsn_matrix2_construct( + return dmnsn_new_matrix2( lhs.n[0][0] - rhs.n[0][0], lhs.n[0][1] - rhs.n[0][1], lhs.n[1][0] - rhs.n[1][0], lhs.n[1][1] - rhs.n[1][1] ); @@ -208,7 +206,7 @@ static dmnsn_matrix2 dmnsn_matrix2_mul(dmnsn_matrix2 lhs, dmnsn_matrix2 rhs) { /* 8 multiplications, 4 additions */ - return dmnsn_matrix2_construct( + return dmnsn_new_matrix2( lhs.n[0][0]*rhs.n[0][0] + lhs.n[0][1]*rhs.n[1][0], lhs.n[0][0]*rhs.n[0][1] + lhs.n[0][1]*rhs.n[1][1], lhs.n[1][0]*rhs.n[0][0] + lhs.n[1][1]*rhs.n[1][0], diff --git a/libdimension/kD_splay_tree.c b/libdimension/kD_splay_tree.c index 924d3a9..ca31b86 100644 --- a/libdimension/kD_splay_tree.c +++ b/libdimension/kD_splay_tree.c @@ -115,31 +115,31 @@ dmnsn_kD_splay_insert(dmnsn_kD_splay_tree *tree, dmnsn_object *object) node->max = node->min; dmnsn_vector corner; - corner = dmnsn_vector_construct(object->min.x, object->min.y, object->max.z); + corner = dmnsn_new_vector(object->min.x, object->min.y, object->max.z); corner = dmnsn_matrix_vector_mul(object->trans, corner); dmnsn_kD_splay_node_swallow(node, corner, corner); - corner = dmnsn_vector_construct(object->min.x, object->max.y, object->min.z); + corner = dmnsn_new_vector(object->min.x, object->max.y, object->min.z); corner = dmnsn_matrix_vector_mul(object->trans, corner); dmnsn_kD_splay_node_swallow(node, corner, corner); - corner = dmnsn_vector_construct(object->min.x, object->max.y, object->max.z); + corner = dmnsn_new_vector(object->min.x, object->max.y, object->max.z); corner = dmnsn_matrix_vector_mul(object->trans, corner); dmnsn_kD_splay_node_swallow(node, corner, corner); - corner = dmnsn_vector_construct(object->max.x, object->min.y, object->min.z); + corner = dmnsn_new_vector(object->max.x, object->min.y, object->min.z); corner = dmnsn_matrix_vector_mul(object->trans, corner); dmnsn_kD_splay_node_swallow(node, corner, corner); - corner = dmnsn_vector_construct(object->max.x, object->min.y, object->max.z); + corner = dmnsn_new_vector(object->max.x, object->min.y, object->max.z); corner = dmnsn_matrix_vector_mul(object->trans, corner); dmnsn_kD_splay_node_swallow(node, corner, corner); - corner = dmnsn_vector_construct(object->max.x, object->max.y, object->min.z); + corner = dmnsn_new_vector(object->max.x, object->max.y, object->min.z); corner = dmnsn_matrix_vector_mul(object->trans, corner); dmnsn_kD_splay_node_swallow(node, corner, corner); - corner = dmnsn_vector_construct(object->max.x, object->max.y, object->max.z); + corner = dmnsn_new_vector(object->max.x, object->max.y, object->max.z); corner = dmnsn_matrix_vector_mul(object->trans, corner); dmnsn_kD_splay_node_swallow(node, corner, corner); diff --git a/libdimension/objects.c b/libdimension/objects.c index 845edfa..0a88561 100644 --- a/libdimension/objects.c +++ b/libdimension/objects.c @@ -42,8 +42,8 @@ dmnsn_new_sphere() if (sphere) { sphere->intersection_fn = &dmnsn_sphere_intersection_fn; sphere->inside_fn = &dmnsn_sphere_inside_fn; - sphere->min = dmnsn_vector_construct(-1.0, -1.0, -1.0); - sphere->max = dmnsn_vector_construct(1.0, 1.0, 1.0); + sphere->min = dmnsn_new_vector(-1.0, -1.0, -1.0); + sphere->max = dmnsn_new_vector(1.0, 1.0, 1.0); } return sphere; } @@ -108,8 +108,8 @@ dmnsn_new_cube() if (cube) { cube->intersection_fn = &dmnsn_cube_intersection_fn; cube->inside_fn = &dmnsn_cube_inside_fn; - cube->min = dmnsn_vector_construct(-1.0, -1.0, -1.0); - cube->max = dmnsn_vector_construct(1.0, 1.0, 1.0); + cube->min = dmnsn_new_vector(-1.0, -1.0, -1.0); + cube->max = dmnsn_new_vector(1.0, 1.0, 1.0); } return cube; } @@ -132,7 +132,7 @@ dmnsn_cube_intersection_fn(const dmnsn_object *cube, dmnsn_line line) && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) { t = t_temp; - normal = dmnsn_vector_construct(-copysign(line.n.x, 1.0), 0.0, 0.0); + normal = dmnsn_new_vector(-copysign(line.n.x, 1.0), 0.0, 0.0); } /* x = 1.0 */ @@ -142,7 +142,7 @@ dmnsn_cube_intersection_fn(const dmnsn_object *cube, dmnsn_line line) && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) { t = t_temp; - normal = dmnsn_vector_construct(-copysign(line.n.x, 1.0), 0.0, 0.0); + normal = dmnsn_new_vector(-copysign(line.n.x, 1.0), 0.0, 0.0); } } @@ -154,7 +154,7 @@ dmnsn_cube_intersection_fn(const dmnsn_object *cube, dmnsn_line line) && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) { t = t_temp; - normal = dmnsn_vector_construct(0.0, -copysign(line.n.y, 1.0), 0.0); + normal = dmnsn_new_vector(0.0, -copysign(line.n.y, 1.0), 0.0); } /* y = 1.0 */ @@ -164,7 +164,7 @@ dmnsn_cube_intersection_fn(const dmnsn_object *cube, dmnsn_line line) && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) { t = t_temp; - normal = dmnsn_vector_construct(0.0, -copysign(line.n.y, 1.0), 0.0); + normal = dmnsn_new_vector(0.0, -copysign(line.n.y, 1.0), 0.0); } } @@ -176,7 +176,7 @@ dmnsn_cube_intersection_fn(const dmnsn_object *cube, dmnsn_line line) && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) { t = t_temp; - normal = dmnsn_vector_construct(0.0, 0.0, -copysign(line.n.z, 1.0)); + normal = dmnsn_new_vector(0.0, 0.0, -copysign(line.n.z, 1.0)); } /* z = 1.0 */ @@ -186,7 +186,7 @@ dmnsn_cube_intersection_fn(const dmnsn_object *cube, dmnsn_line line) && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) { t = t_temp; - normal = dmnsn_vector_construct(0.0, 0.0, -copysign(line.n.z, 1.0)); + normal = dmnsn_new_vector(0.0, 0.0, -copysign(line.n.z, 1.0)); } } diff --git a/libdimension/raytrace.c b/libdimension/raytrace.c index 143c2bc..b37dff3 100644 --- a/libdimension/raytrace.c +++ b/libdimension/raytrace.c @@ -261,7 +261,7 @@ dmnsn_raytrace_scene_impl(dmnsn_progress *progress, dmnsn_scene *scene, static dmnsn_line dmnsn_line_add_epsilon(dmnsn_line l) { - return dmnsn_line_construct( + return dmnsn_new_line( dmnsn_vector_add( l.x0, dmnsn_vector_mul(1.0e-9, l.n) @@ -306,10 +306,7 @@ dmnsn_raytrace_light_ray(dmnsn_intersection *intersection, dmnsn_scene *scene, *color = dmnsn_black; dmnsn_vector x0 = dmnsn_line_point(intersection->ray, intersection->t); - dmnsn_line shadow_ray = dmnsn_line_construct( - x0, - dmnsn_vector_sub(light->x0, x0) - ); + dmnsn_line shadow_ray = dmnsn_new_line(x0, dmnsn_vector_sub(light->x0, x0)); /* Add epsilon to avoid hitting ourselves with the shadow ray */ shadow_ray = dmnsn_line_add_epsilon(shadow_ray); @@ -439,7 +436,7 @@ dmnsn_raytrace_shoot(dmnsn_line ray, dmnsn_scene *scene, trans.filter = 0.0; trans.trans = 0.0; - dmnsn_line trans_ray = dmnsn_line_construct( + dmnsn_line trans_ray = dmnsn_new_line( dmnsn_line_point(ray, intersection->t), ray.n ); |