summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2010-07-03 22:35:59 -0600
committerTavian Barnes <tavianator@gmail.com>2010-07-03 22:35:59 -0600
commitf0d866e2ad137b339eae91c49ce22630f3a9adf9 (patch)
treeadcedd46c6f5e81bf392fe5712d8081e217c0bac
parent2b5d6e5f42f625049e3f9df765e0cf3fbecc6520 (diff)
downloaddimension-f0d866e2ad137b339eae91c49ce22630f3a9adf9.tar.xz
New dmnsn_radians() and dmnsn_degrees() conversion functions.
-rw-r--r--dimension/parse.c14
-rw-r--r--dimension/realize.c8
-rw-r--r--libdimension/dimension/geometry.h14
-rw-r--r--tests/libdimension/tests.c6
4 files changed, 26 insertions, 16 deletions
diff --git a/dimension/parse.c b/dimension/parse.c
index fc067cf..7b33b1d 100644
--- a/dimension/parse.c
+++ b/dimension/parse.c
@@ -564,7 +564,7 @@ dmnsn_eval_zeroary(dmnsn_astnode astnode, dmnsn_symbol_table *symtable)
switch (astnode.type) {
case DMNSN_AST_PI:
- dmnsn_make_ast_float(&ret, 4*atan(1.0));
+ dmnsn_make_ast_float(&ret, M_PI);
break;
case DMNSN_AST_TRUE:
dmnsn_make_ast_integer(&ret, 1);
@@ -736,7 +736,7 @@ dmnsn_eval_unary(dmnsn_astnode astnode, dmnsn_symbol_table *symtable)
dmnsn_make_ast_float(&ret, cosh(n));
break;
case DMNSN_AST_DEGREES:
- dmnsn_make_ast_float(&ret, n*45.0/atan(1.0));
+ dmnsn_make_ast_float(&ret, dmnsn_degrees(n));
break;
case DMNSN_AST_EXP:
dmnsn_make_ast_float(&ret, exp(n));
@@ -754,7 +754,7 @@ dmnsn_eval_unary(dmnsn_astnode astnode, dmnsn_symbol_table *symtable)
dmnsn_make_ast_float(&ret, log(n)/log(10.0));
break;
case DMNSN_AST_RADIANS:
- dmnsn_make_ast_float(&ret, n*atan(1.0)/45.0);
+ dmnsn_make_ast_float(&ret, dmnsn_radians(n));
break;
case DMNSN_AST_SIN:
dmnsn_make_ast_float(&ret, sin(n));
@@ -840,7 +840,7 @@ dmnsn_eval_unary(dmnsn_astnode astnode, dmnsn_symbol_table *symtable)
dmnsn_make_ast_float(&ret, cosh(n));
break;
case DMNSN_AST_DEGREES:
- dmnsn_make_ast_float(&ret, n*45.0/atan(1.0));
+ dmnsn_make_ast_float(&ret, dmnsn_degrees(n));
break;
case DMNSN_AST_EXP:
dmnsn_make_ast_float(&ret, exp(n));
@@ -858,7 +858,7 @@ dmnsn_eval_unary(dmnsn_astnode astnode, dmnsn_symbol_table *symtable)
dmnsn_make_ast_float(&ret, log(n)/log(10.0));
break;
case DMNSN_AST_RADIANS:
- dmnsn_make_ast_float(&ret, n*atan(1.0)/45.0);
+ dmnsn_make_ast_float(&ret, dmnsn_radians(n));
break;
case DMNSN_AST_SIN:
dmnsn_make_ast_float(&ret, sin(n));
@@ -1141,7 +1141,7 @@ dmnsn_eval_binary(dmnsn_astnode astnode, dmnsn_symbol_table *symtable)
az.type == DMNSN_AST_INTEGER ? *(long *)az.ptr : *(double *)az.ptr
);
- axis = dmnsn_vector_mul(atan(1.0)/45.0, axis);
+ axis = dmnsn_vector_mul(dmnsn_radians(1.0), axis);
r = dmnsn_transform_vector(dmnsn_rotation_matrix(axis), r);
ret = dmnsn_copy_astnode(astnode);
@@ -1247,7 +1247,7 @@ dmnsn_eval_binary(dmnsn_astnode astnode, dmnsn_symbol_table *symtable)
az.type == DMNSN_AST_INTEGER ? *(long *)az.ptr : *(double *)az.ptr
);
- axis = dmnsn_vector_mul(atan(1.0)/45.0, axis);
+ axis = dmnsn_vector_mul(dmnsn_radians(1.0), axis);
r = dmnsn_transform_vector(
dmnsn_rotation_matrix(dmnsn_new_vector(axis.x, 0.0, 0.0)),
diff --git a/dimension/realize.c b/dimension/realize.c
index 563327c..967c5db 100644
--- a/dimension/realize.c
+++ b/dimension/realize.c
@@ -142,13 +142,11 @@ dmnsn_realize_rotation(dmnsn_astnode astnode)
{
dmnsn_assert(astnode.type == DMNSN_AST_ROTATION, "Expected a rotation.");
- const double deg2rad = atan(1.0)/45.0;
-
dmnsn_astnode angle_node;
dmnsn_array_get(astnode.children, 0, &angle_node);
dmnsn_vector angle = dmnsn_vector_mul(
- deg2rad,
+ dmnsn_radians(1.0),
dmnsn_realize_vector(angle_node)
);
@@ -266,8 +264,6 @@ dmnsn_realize_camera(dmnsn_astnode astnode)
{
dmnsn_assert(astnode.type == DMNSN_AST_CAMERA, "Expected a camera.");
- const double deg2rad = atan(1.0)/45.0;
-
dmnsn_astnode_type camera_type = DMNSN_AST_PERSPECTIVE;
dmnsn_vector location = dmnsn_new_vector(0.0, 0.0, 0.0);
dmnsn_vector direction = dmnsn_new_vector(0.0, 0.0, 1.0);
@@ -363,7 +359,7 @@ dmnsn_realize_camera(dmnsn_astnode astnode)
case DMNSN_AST_ANGLE:
{
dmnsn_array_get(item->children, 0, &child);
- double angle = deg2rad*dmnsn_realize_float(child);
+ double angle = dmnsn_radians(dmnsn_realize_float(child));
direction = dmnsn_vector_mul(
0.5*dmnsn_vector_norm(right)/tan(angle/2.0),
dmnsn_vector_normalize(direction)
diff --git a/libdimension/dimension/geometry.h b/libdimension/dimension/geometry.h
index d55126d..c9f036f 100644
--- a/libdimension/dimension/geometry.h
+++ b/libdimension/dimension/geometry.h
@@ -70,7 +70,7 @@ static const dmnsn_vector dmnsn_x = { 1.0, 0.0, 0.0 };
static const dmnsn_vector dmnsn_y = { 0.0, 1.0, 0.0 };
static const dmnsn_vector dmnsn_z = { 0.0, 0.0, 1.0 };
-/* Min/max macros */
+/* Scalar functions */
DMNSN_INLINE double
dmnsn_min(double a, double b)
@@ -84,6 +84,18 @@ dmnsn_max(double a, double b)
return a > b ? a : b;
}
+DMNSN_INLINE double
+dmnsn_radians(double degrees)
+{
+ return degrees*M_PI/180.0;
+}
+
+DMNSN_INLINE double
+dmnsn_degrees(double radians)
+{
+ return radians*180.0/M_PI;
+}
+
/* Shorthand for vector/matrix construction */
DMNSN_INLINE dmnsn_vector
diff --git a/tests/libdimension/tests.c b/tests/libdimension/tests.c
index 68fac12..393a1d6 100644
--- a/tests/libdimension/tests.c
+++ b/tests/libdimension/tests.c
@@ -57,7 +57,7 @@ dmnsn_new_default_scene()
trans
);
trans = dmnsn_matrix_mul(
- dmnsn_rotation_matrix(dmnsn_new_vector(0.0, 1.0, 0.0)),
+ dmnsn_rotation_matrix(dmnsn_new_vector(0.0, dmnsn_radians(53.0), 0.0)),
trans
);
@@ -86,7 +86,9 @@ dmnsn_new_default_scene()
cube->interior = dmnsn_new_interior();
cube->interior->ior = 1.1;
- cube->trans = dmnsn_rotation_matrix(dmnsn_new_vector(0.75, 0.0, 0.0));
+ cube->trans = dmnsn_rotation_matrix(
+ dmnsn_new_vector(dmnsn_radians(45.0), 0.0, 0.0)
+ );
dmnsn_object *csg = dmnsn_new_csg_difference(cube, sphere);
dmnsn_array_push(scene->objects, &csg);