cae-tools/cameo/poly2d.c

94 lines
1.9 KiB
C

/*
* poly2d.c - Poly2D interface functions
*
* Written 2012, 2015 by Werner Almesberger
* Copyright 2012, 2015 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 <stdlib.h>
#include <math.h>
#include <assert.h>
#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);
if (!*last)
continue;
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;
}