From 0cb8dcce563a36dd2adb8e4d941cbac4adcb8561 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 20 Oct 2010 19:08:05 -0400 Subject: Add cones to dimension. --- dimension/common.nonterminals | 1 + dimension/common.rules | 13 +++++++++++++ dimension/common.terminals | 2 +- dimension/grammar.epilogue | 1 + dimension/lexer.l | 1 + dimension/parse.h | 1 + dimension/realize.c | 30 +++++++++++++++++++++--------- tests/dimension/demo.pov | 11 +++++++++-- tests/dimension/demo.sh | 21 ++++++++++++++++----- 9 files changed, 64 insertions(+), 17 deletions(-) diff --git a/dimension/common.nonterminals b/dimension/common.nonterminals index 09a4f5f..ad95ccb 100644 --- a/dimension/common.nonterminals +++ b/dimension/common.nonterminals @@ -40,6 +40,7 @@ %type OBJECT %type FINITE_SOLID_OBJECT %type BOX +%type CONE %type CYLINDER %type MAYBE_OPEN %type SPHERE diff --git a/dimension/common.rules b/dimension/common.rules index d91cac3..040f4a6 100644 --- a/dimension/common.rules +++ b/dimension/common.rules @@ -283,6 +283,7 @@ OBJECT: FINITE_SOLID_OBJECT ; FINITE_SOLID_OBJECT: BOX + | CONE | CYLINDER | SPHERE ; @@ -297,6 +298,18 @@ BOX: "box" "{" } ; +CONE: "cone" "{" + VECTOR "," FLOAT "," VECTOR "," FLOAT + MAYBE_OPEN + OBJECT_MODIFIERS + "}" + { + dmnsn_astnode object + = dmnsn_new_astnode5(DMNSN_AST_CONE, @$, $3, $5, $7, $9, $10); + $$ = dmnsn_new_astnode2(DMNSN_AST_OBJECT, @$, object, $11); + } +; + CYLINDER: "cylinder" "{" VECTOR "," VECTOR "," FLOAT MAYBE_OPEN diff --git a/dimension/common.terminals b/dimension/common.terminals index af75bb4..ffe13dc 100644 --- a/dimension/common.terminals +++ b/dimension/common.terminals @@ -134,7 +134,7 @@ %token DMNSN_T_COMPONENT %token DMNSN_T_COMPOSITE %token DMNSN_T_CONCAT -%token DMNSN_T_CONE +%token DMNSN_T_CONE "cone" %token DMNSN_T_CONFIDENCE %token DMNSN_T_CONIC_SWEEP %token DMNSN_T_CONSERVE_ENERGY diff --git a/dimension/grammar.epilogue b/dimension/grammar.epilogue index d766a07..6e5a90b 100644 --- a/dimension/grammar.epilogue +++ b/dimension/grammar.epilogue @@ -134,6 +134,7 @@ dmnsn_astnode_string(dmnsn_astnode_type astnode_type) dmnsn_astnode_map(DMNSN_AST_OBJECT, "object"); dmnsn_astnode_map(DMNSN_AST_BOX, "box"); + dmnsn_astnode_map(DMNSN_AST_CONE, "cone"); dmnsn_astnode_map(DMNSN_AST_CYLINDER, "cylinder"); dmnsn_astnode_map(DMNSN_AST_DIFFERENCE, "difference"); dmnsn_astnode_map(DMNSN_AST_INTERSECTION, "intersection"); diff --git a/dimension/lexer.l b/dimension/lexer.l index dd25363..c871b91 100644 --- a/dimension/lexer.l +++ b/dimension/lexer.l @@ -184,6 +184,7 @@ unsigned long wchar; "camera" RETURN_TOKEN(DMNSN_T_CAMERA); "color" RETURN_TOKEN(DMNSN_T_COLOR); "colour" RETURN_TOKEN(DMNSN_T_COLOR); +"cone" RETURN_TOKEN(DMNSN_T_CONE); "cos" RETURN_TOKEN(DMNSN_T_COS); "cosh" RETURN_TOKEN(DMNSN_T_COSH); "cylinder" RETURN_TOKEN(DMNSN_T_CYLINDER); diff --git a/dimension/parse.h b/dimension/parse.h index d9b9195..556465e 100644 --- a/dimension/parse.h +++ b/dimension/parse.h @@ -48,6 +48,7 @@ typedef enum { DMNSN_AST_OBJECT, DMNSN_AST_BOX, + DMNSN_AST_CONE, DMNSN_AST_CYLINDER, DMNSN_AST_DIFFERENCE, DMNSN_AST_INTERSECTION, diff --git a/dimension/realize.c b/dimension/realize.c index dc315d8..b5f6e50 100644 --- a/dimension/realize.c +++ b/dimension/realize.c @@ -845,17 +845,28 @@ dmnsn_realize_box(dmnsn_astnode astnode) static dmnsn_object * dmnsn_realize_cylinder(dmnsn_astnode astnode) { - dmnsn_assert(astnode.type == DMNSN_AST_CYLINDER, "Expected a cylinder."); - - dmnsn_astnode pnode1, pnode2, radius, open; - dmnsn_array_get(astnode.children, 0, &pnode1); - dmnsn_array_get(astnode.children, 1, &pnode2); - dmnsn_array_get(astnode.children, 2, &radius); - dmnsn_array_get(astnode.children, 3, &open); + dmnsn_astnode pnode1, rnode1, pnode2, rnode2, open; + + if (astnode.type == DMNSN_AST_CONE) { + dmnsn_array_get(astnode.children, 0, &pnode1); + dmnsn_array_get(astnode.children, 1, &rnode1); + dmnsn_array_get(astnode.children, 2, &pnode2); + dmnsn_array_get(astnode.children, 3, &rnode2); + dmnsn_array_get(astnode.children, 4, &open); + } else if (astnode.type == DMNSN_AST_CYLINDER) { + dmnsn_array_get(astnode.children, 0, &pnode1); + dmnsn_array_get(astnode.children, 1, &pnode2); + dmnsn_array_get(astnode.children, 2, &rnode1); + dmnsn_array_get(astnode.children, 2, &rnode2); + dmnsn_array_get(astnode.children, 3, &open); + } else { + dmnsn_assert(false, "Expected a cone or cylinder."); + } dmnsn_vector p1 = dmnsn_realize_vector(pnode1); dmnsn_vector p2 = dmnsn_realize_vector(pnode2); - double r = dmnsn_realize_float(radius); + double r1 = dmnsn_realize_float(rnode1); + double r2 = dmnsn_realize_float(rnode2); dmnsn_vector dir = dmnsn_vector_sub(p2, p1); double l = dmnsn_vector_norm(dir); @@ -864,7 +875,7 @@ dmnsn_realize_cylinder(dmnsn_astnode astnode) double theta2 = dmnsn_vector_axis_angle(dmnsn_y, dir, dmnsn_z); dmnsn_object *cylinder - = dmnsn_new_cylinder(r, r, dmnsn_realize_integer(open)); + = dmnsn_new_cylinder(r1, r2, dmnsn_realize_integer(open)); /* Transformations: lift the cylinder to start at the origin, scale, rotate, and translate properly */ cylinder->trans = dmnsn_translation_matrix(dmnsn_new_vector(0.0, 1.0, 0.0)); @@ -1034,6 +1045,7 @@ dmnsn_realize_object(dmnsn_astnode astnode, dmnsn_array *lights) case DMNSN_AST_BOX: object = dmnsn_realize_box(onode); break; + case DMNSN_AST_CONE: case DMNSN_AST_CYLINDER: object = dmnsn_realize_cylinder(onode); break; diff --git a/tests/dimension/demo.pov b/tests/dimension/demo.pov index 69c9d4e..0ec053b 100644 --- a/tests/dimension/demo.pov +++ b/tests/dimension/demo.pov @@ -71,8 +71,15 @@ difference { } } -cylinder { - -1.25*y, 1.25*y, 0.1 +union { + cylinder { + -1.25*y, 1.25*y, 0.1 + } + cone { + 1.25*y, 0.1, 1.5*y, 0 + open + } + pigment { color rgb <1, 0, 0> } diff --git a/tests/dimension/demo.sh b/tests/dimension/demo.sh index 2bf21b9..fc37226 100755 --- a/tests/dimension/demo.sh +++ b/tests/dimension/demo.sh @@ -77,11 +77,22 @@ demo_exp=$(echo -n \ (phong_size (float 40))))))) object-modifiers) (object - (cylinder - (vector (float 0) (float -1.25) (float 0) (float 0) (float 0)) - (vector (float 0) (float 1.25) (float 0) (float 0) (float 0)) - (float 0.1) - (integer 0)) + (union + (object + (cylinder + (vector (float 0) (float -1.25) (float 0) (float 0) (float 0)) + (vector (float 0) (float 1.25) (float 0) (float 0) (float 0)) + (float 0.1) + (integer 0)) + object-modifiers) + (object + (cone + (vector (float 0) (float 1.25) (float 0) (float 0) (float 0)) + (float 0.1) + (vector (float 0) (float 1.5) (float 0) (float 0) (float 0)) + (integer 0) + (integer 1)) + object-modifiers)) (object-modifiers (pigment (vector (integer 1) (integer 0) (integer 0) -- cgit v1.2.3