From 7b54293bffd6f9fa2308ff4a9b2aa2d407043c13 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Tue, 12 Jun 2012 14:58:46 -0300 Subject: [PATCH] cameo/: move conversion between poly2d and "struct path" from area-poly2d.c to poly2d.c Untested. --- cameo/Makefile | 4 +- cameo/area-poly2d.c | 42 +++++---------------- cameo/poly2d.c | 90 +++++++++++++++++++++++++++++++++++++++++++++ cameo/poly2d.h | 29 +++++++++++++++ 4 files changed, 130 insertions(+), 35 deletions(-) create mode 100644 cameo/poly2d.c create mode 100644 cameo/poly2d.h diff --git a/cameo/Makefile b/cameo/Makefile index 0c079f4..be6173c 100644 --- a/cameo/Makefile +++ b/cameo/Makefile @@ -15,8 +15,8 @@ PREFIX ?= /usr/local SHELL=/bin/bash MAIN=cameo -OBJS=cameo.o excellon.o area-poly2d.o gerber.o gnuplot.o ops.o path.o shape.o \ - lex.yy.o y.tab.o +OBJS=cameo.o excellon.o area-poly2d.o gerber.o gnuplot.o ops.o path.o \ + poly2d.o shape.o lex.yy.o y.tab.o CFLAGS_WARN=-Wall -Wshadow -Wmissing-prototypes \ -Wmissing-declarations -Wno-format-zero-length diff --git a/cameo/area-poly2d.c b/cameo/area-poly2d.c index d123937..7d506b5 100644 --- a/cameo/area-poly2d.c +++ b/cameo/area-poly2d.c @@ -12,7 +12,7 @@ #include -#include +#include "poly2d.h" #include "path.h" #include "area.h" @@ -22,46 +22,22 @@ static void fill_path(const struct path *paths, double z, double overlap, struct path ***res) { const struct path *path; - const struct point *p; - double r_tool; - struct p2d *polys = NULL, *poly, *last = NULL; + double r_tool = 0; + struct p2d *polys; struct p2d *fill; - const struct v2d *v; - for (path = paths; path; path = path->next) { - if (path->first->z != z) - continue; - if (!path->first || !path->first->next) - continue; - r_tool = path->r_tool; + for (path = paths; path; path = path->next) + if (path->first && path->first->z == z) + r_tool = path->r_tool; - poly = p2d_new(); - if (last) - last->next = poly; - else - polys = poly; - last = poly; - for (p = path->first; p && p->next; p = p->next) - p2d_append(poly, v2d_new(p->x, p->y)); - p2d_close(poly); - } + polys = paths_to_polys_z(paths, z, z); fill = p2d_area(polys, r_tool, 2*r_tool-overlap); p2d_free_all(polys); - for (poly = fill; poly; poly = poly->next) { - **res = path_new(r_tool, ""); - v = poly->v; - while (v) { - path_add(**res, v->x, v->y, z); - v = v->next; - if (v == poly->v) - break; - } - if (v) - path_add(**res, v->x, v->y, z); + **res = polys_to_paths(fill, r_tool, z); + while (**res) *res = &(**res)->next; - } p2d_free_all(fill); } diff --git a/cameo/poly2d.c b/cameo/poly2d.c new file mode 100644 index 0000000..a8d1460 --- /dev/null +++ b/cameo/poly2d.c @@ -0,0 +1,90 @@ +/* + * poly2d.c - Poly2D interface functions + * + * Written 2012 by Werner Almesberger + * Copyright 2012 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + + +#include +#include + +#include "path.h" +#include "poly2d.h" + + +struct p2d *path_to_poly(const struct path *path) +{ + struct p2d *poly; + const struct point *p; + + if (!path->first || !path->first->next) + return NULL; + + poly = p2d_new(); + for (p = path->first; p && p->next; p = p->next) + p2d_append(poly, v2d_new(p->x, p->y)); + if (path_is_closed(path)) + p2d_close(poly); + return poly; +} + + +struct p2d *paths_to_polys_z(const struct path *paths, + double zmin, double zmax) +{ + struct p2d *polys = NULL, **last = &polys; + const struct path *path; + + assert(zmin <= zmax); + for (path = paths; path; path = path->next) { + if (path->first->z < zmin || path->first->z > zmax) + continue; + *last = path_to_poly(path); + last = &(*last)->next; + } + return polys; +} + + +struct p2d *paths_to_polys(const struct path *paths) +{ + return paths_to_polys_z(paths, -HUGE_VAL, HUGE_VAL); +} + + +struct path *poly_to_path(const struct p2d *poly, double r_tool, double z) +{ + struct path *path; + const struct v2d *v; + + path = path_new(r_tool, ""); + v = poly->v; + while (v) { + path_add(path, v->x, v->y, z); + v = v->next; + if (v == poly->v) + break; + } + if (v) + path_add(path, v->x, v->y, z); + return path; +} + + +struct path *polys_to_paths(const struct p2d *polys, double r_tool, double z) +{ + struct path *paths = NULL, **last = &paths; + const struct p2d *poly; + + for (poly = polys; poly; poly = poly->next) { + *last = poly_to_path(poly, r_tool, z); + last = &(*last)->next; + } + return paths; +} diff --git a/cameo/poly2d.h b/cameo/poly2d.h new file mode 100644 index 0000000..6ace8ef --- /dev/null +++ b/cameo/poly2d.h @@ -0,0 +1,29 @@ +/* + * poly2d.h - Poly2D interface functions + * + * Written 2012 by Werner Almesberger + * Copyright 2012 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + + +#ifndef POLY2D_H + +#include + +#include "path.h" + + +struct p2d *path_to_poly(const struct path *path); +struct p2d *paths_to_polys_z(const struct path *paths, + double zmin, double zmax); +struct p2d *paths_to_polys(const struct path *paths); + +struct path *poly_to_path(const struct p2d *poly, double r_tool, double z); +struct path *polys_to_paths(const struct p2d *polys, double r_tool, double z); + +#endif /* !POLY2D_H */