Root/cameo/poly2d.c

1/*
2 * poly2d.c - Poly2D interface functions
3 *
4 * Written 2012, 2015 by Werner Almesberger
5 * Copyright 2012, 2015 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdlib.h>
15#include <math.h>
16#include <assert.h>
17
18#include "path.h"
19#include "poly2d.h"
20
21
22struct p2d *path_to_poly(const struct path *path)
23{
24    struct p2d *poly;
25    const struct point *p;
26
27    if (!path->first || !path->first->next)
28        return NULL;
29
30    poly = p2d_new();
31    for (p = path->first; p && p->next; p = p->next)
32        p2d_append(poly, v2d_new(p->x, p->y));
33    if (path_is_closed(path))
34        p2d_close(poly);
35    return poly;
36}
37
38
39struct p2d *paths_to_polys_z(const struct path *paths,
40    double zmin, double zmax)
41{
42    struct p2d *polys = NULL, **last = &polys;
43    const struct path *path;
44
45    assert(zmin <= zmax);
46    for (path = paths; path; path = path->next) {
47        if (path->first->z < zmin || path->first->z > zmax)
48            continue;
49        *last = path_to_poly(path);
50        if (!*last)
51            continue;
52        last = &(*last)->next;
53    }
54    return polys;
55}
56
57
58struct p2d *paths_to_polys(const struct path *paths)
59{
60    return paths_to_polys_z(paths, -HUGE_VAL, HUGE_VAL);
61}
62
63
64struct path *poly_to_path(const struct p2d *poly, double r_tool, double z)
65{
66    struct path *path;
67    const struct v2d *v;
68
69    path = path_new(r_tool, "");
70    v = poly->v;
71    while (v) {
72        path_add(path, v->x, v->y, z);
73        v = v->next;
74        if (v == poly->v)
75            break;
76    }
77    if (v)
78        path_add(path, v->x, v->y, z);
79    return path;
80}
81
82
83struct path *polys_to_paths(const struct p2d *polys, double r_tool, double z)
84{
85    struct path *paths = NULL, **last = &paths;
86    const struct p2d *poly;
87
88    for (poly = polys; poly; poly = poly->next) {
89        *last = poly_to_path(poly, r_tool, z);
90        last = &(*last)->next;
91    }
92    return paths;
93}
94

Archive Download this file

Branches:
master



interactive