From 6f30bab648ffda1aa2a3e5f125fa8c1f4c44c2f9 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Mon, 3 Jan 2011 01:25:42 -0300 Subject: [PATCH] cameo: added "rotate" command - lang.l, lang.y (rotate): added command "rotate", with cumulative rotation - README: documented "rotate" - README: corrected typo in description of the translation commands --- cameo/README | 12 ++++++++++-- cameo/lang.l | 1 + cameo/lang.y | 38 +++++++++++++++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/cameo/README b/cameo/README index bb4dad0..d76a5fc 100644 --- a/cameo/README +++ b/cameo/README @@ -88,6 +88,7 @@ Horizontal adjustments: align array translate + rotate reset "translate" shifts the currently loaded paths by the specified distance. @@ -110,12 +111,19 @@ The reference point is specified with a number as follows: two arguments define the step between pieces while the second two arguments define how many steps in each direction are taken. -"align" sets moves the toolpath to an absolute position while "array" and +"align" moves the toolpath to an absolute position while "array" and "translate" move relative to the current position. The total translation is remembered also across "clean", so that an alignment can be applied to multiple toolpaths. -"reset" sets the translation to zero. The way "reset" is currently +"rotate" rotates the currenly loaded paths by the specified number of +degrees in a counter-clockwise direction. Like translations, rotations +are accumulated and are automatically applied to files loaded later. +Note that rotation is applied before translation. Therefore, using +"rotate" after "translate", "array", or "align" may yield unexpected +results. + +"reset" sets translation and rotation to zero. The way "reset" is currently implemented, it does not affect the currently loaded toolpath. (But it's better to use it only with "clean".) diff --git a/cameo/lang.l b/cameo/lang.l index 605f41c..9799082 100644 --- a/cameo/lang.l +++ b/cameo/lang.l @@ -49,6 +49,7 @@ NUM -?[0-9]+\.?[0-9]* optimize return TOK_OPTIMIZE; remainder return TOK_REMAINDER; reset return TOK_RESET; +rotate return TOK_ROTATE; translate return TOK_TRANSLATE; z return TOK_Z; diff --git a/cameo/lang.y b/cameo/lang.y index 938115b..5f8a927 100644 --- a/cameo/lang.y +++ b/cameo/lang.y @@ -13,6 +13,8 @@ #include +#include +#include #include "path.h" #include "ops.h" @@ -23,7 +25,8 @@ #include "y.tab.h" -static double xo = 0, yo = 0, zo = 0; +static double xo = 0, yo = 0, zo = 0; /* origin */ +static double rot = 0; static struct path *paths = NULL; static struct path *remain = NULL; @@ -55,6 +58,27 @@ static void translate(struct path *list, double x, double y, double z) } +static void rotate(struct path *list, double angle) +{ + double m[2][2], tmp; + struct point *p; + + angle = angle/180.0*M_PI; + m[0][0] = cos(angle); + m[0][1] = -sin(angle); + m[1][0] = -m[0][1]; + m[1][1] = m[0][0]; + while (list) { + for (p = list->first; p; p = p->next) { + tmp = p->x*m[0][0]+p->y*m[0][1]; + p->y = p->x*m[1][0]+p->y*m[1][1]; + p->x = tmp; + } + list = list->next; + } +} + + static double ref_pick_1(int ref, double a, double b) { switch (ref) { @@ -151,7 +175,7 @@ static struct path **classify(struct path **anchor, struct path *path) %token TOK_ALIGN TOK_ARRAY TOK_CLEAR TOK_DRILL TOK_EMPTY %token TOK_MILL TOK_OFFSET TOK_OPTIMIZE TOK_REMAINDER TOK_RESET -%token TOK_TRANSLATE TOK_Z +%token TOK_ROTATE TOK_TRANSLATE TOK_Z %token TOK_APPEND TOK_GERBER TOK_GNUPLOT TOK_EXCELLON TOK_WRITE %token TOK_DOG TOK_INSIDE @@ -201,7 +225,7 @@ command: } | TOK_RESET { - xo = yo = 0; + xo = yo = rot = 0; } | TOK_OFFSET offset_options { @@ -212,6 +236,11 @@ command: { paths = optimize_paths(paths); } + | TOK_ROTATE number + { + rotate(paths, $2); + rot += $2; + } | TOK_TRANSLATE dimen dimen { translate(paths, $2, $3, 0); @@ -231,6 +260,7 @@ command: struct path *new; new = gerber_read($3, $2/2); + rotate(new, rot); translate(new, xo, yo, 0); add_paths(new); } @@ -239,6 +269,7 @@ command: struct path *new; new = gnuplot_read($3, $2/2); + rotate(new, rot); translate(new, xo, yo, 0); add_paths(new); } @@ -247,6 +278,7 @@ command: struct path *new; new = excellon_read($2); + rotate(new, rot); translate(new, xo, yo, 0); add_paths(new); }