diff --git a/cameo/cameo.c b/cameo/cameo.c index 93250e5..f575555 100644 --- a/cameo/cameo.c +++ b/cameo/cameo.c @@ -1,8 +1,8 @@ /* * cameo.c - Toolpath adaptation and machine control * - * Written 2010 by Werner Almesberger - * Copyright 2010 Werner Almesberger + * Written 2010-2011 by Werner Almesberger + * Copyright 2010-2011 Werner Almesberger * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -100,7 +100,7 @@ int main(int argc, char **argv) paths = gerber_read(in, r); else paths = gnuplot_read(in, r); - tool_comp_paths(paths, dog_bone, 0); + paths = tool_comp_paths(paths, dog_bone, 0); /* @@@ memory leak */ gnuplot_write(out, paths); return 0; diff --git a/cameo/lang.y b/cameo/lang.y index 48bdd7c..2806b6c 100644 --- a/cameo/lang.y +++ b/cameo/lang.y @@ -243,8 +243,12 @@ command: } | TOK_OFFSET offset_options { - tool_comp_paths(paths, + struct path *new; + + new = tool_comp_paths(paths, !!($2 & OO_DOG), !!($2 & OO_INSIDE)); + clear_paths(); + paths = new; } | TOK_OPTIMIZE { diff --git a/cameo/ops.c b/cameo/ops.c index 3d86df2..a90599f 100644 --- a/cameo/ops.c +++ b/cameo/ops.c @@ -19,23 +19,24 @@ #include "ops.h" -static void tool_comp_1(struct path *path, int inside, int dog_bone) +static struct path *tool_comp_1(const struct path *path, int inside, + int dog_bone) { int left; - struct path *new; left = path_tool_is_left(path); if (inside) - new = path_offset(path, !left, path->notch); + return path_offset(path, !left, path->notch); else - new = path_offset(path, left, path->notch || dog_bone); - path_replace(path, new); + return path_offset(path, left, path->notch || dog_bone); } -void tool_comp_paths(struct path *paths, int dog_bone, int all_inside) +struct path *tool_comp_paths(const struct path *paths, int dog_bone, + int all_inside) { - struct path *leftmost, *path; + const struct path *leftmost, *path; + struct path *new = NULL, **anchor = &new; /* * We don't have an algorithm (yet) that can detect which paths are @@ -50,13 +51,18 @@ void tool_comp_paths(struct path *paths, int dog_bone, int all_inside) leftmost = path_find_leftmost(paths); for (path = paths; path; path = path->next) - if (path != leftmost && (all_inside || !path->outside)) - tool_comp_1(path, 1, dog_bone); + if (path != leftmost && (all_inside || !path->outside)) { + *anchor = tool_comp_1(path, 1, dog_bone); + anchor = &(*anchor)->next; + } if (!all_inside) for (path = paths; path; path = path->next) - if (path != leftmost && path->outside) - tool_comp_1(path, 0, dog_bone); - tool_comp_1(leftmost, all_inside, dog_bone); + if (path != leftmost && path->outside) { + *anchor = tool_comp_1(path, 0, dog_bone); + anchor = &(*anchor)->next; + } + *anchor = tool_comp_1(leftmost, all_inside, dog_bone); + return new; } diff --git a/cameo/ops.h b/cameo/ops.h index 52fff4d..4e8d9fb 100644 --- a/cameo/ops.h +++ b/cameo/ops.h @@ -17,7 +17,8 @@ #include "path.h" -void tool_comp_paths(struct path *paths, int dog_bone, int all_inside); +struct path *tool_comp_paths(const struct path *paths, int dog_bone, + int all_inside); struct path *try_drill(struct path *path, double d_min, double d_max); struct path *try_mill(struct path *path, double diam, double step, int any); struct path *optimize_paths(struct path *paths); diff --git a/cameo/path.c b/cameo/path.c index 6732e29..2abd18d 100644 --- a/cameo/path.c +++ b/cameo/path.c @@ -134,18 +134,6 @@ void path_add(struct path *path, double x, double y, double z) } -void path_replace(struct path *old, struct path *new) -{ - struct path *next = old->next; - - free_points(old->first); - free((void *) old->id); - *old = *new; - old->next = next; - free(new); -} - - struct path *path_reverse(const struct path *path) { struct path *new; @@ -348,10 +336,10 @@ struct path *path_offset(const struct path *path, int left, int notch) } -struct path *path_find_leftmost(struct path *path) +const struct path *path_find_leftmost(const struct path *path) { const struct point *p; - struct path *best = NULL; + const struct path *best = NULL; double best_x = HUGE_VAL; while (path) { diff --git a/cameo/path.h b/cameo/path.h index a25a4fc..d419a46 100644 --- a/cameo/path.h +++ b/cameo/path.h @@ -32,11 +32,10 @@ struct path { struct path *path_new(double r_tool, const char *id); void path_add(struct path *path, double x, double y, double z); -void path_replace(struct path *old, struct path *new); struct path *path_reverse(const struct path *path); int path_tool_is_left(const struct path *path); struct path *path_offset(const struct path *path, int left, int notch); -struct path *path_find_leftmost(struct path *path); +const struct path *path_find_leftmost(const struct path *path); void path_free(struct path *path); struct path *path_connect(struct path *path); void path_stats(const struct path *path);