1
0
mirror of git://projects.qi-hardware.com/cae-tools.git synced 2024-12-23 09:17:42 +02:00
cae-tools/cameo/area-poly2d.c

78 lines
1.7 KiB
C
Raw Normal View History

2012-05-08 06:52:47 +03:00
/*
* area-poly2d.c - Area fill (using libpoly2d)
*
* Written 2012, 2015 by Werner Almesberger
* Copyright 2012, 2015 Werner Almesberger
2012-05-08 06:52:47 +03:00
*
* 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 <stdio.h>
2012-05-08 06:52:47 +03:00
#include <math.h>
#include "poly2d.h"
2012-05-08 06:52:47 +03:00
#include "path.h"
#include "area.h"
static void fill_path(const struct path *paths, double z, double overlap,
struct path ***res)
{
const struct path *path;
double r_tool = 0;
struct p2d *polys;
2012-05-08 06:52:47 +03:00
struct p2d *fill;
for (path = paths; path; path = path->next)
if (path->first && path->first->z == z)
r_tool = path->r_tool;
2012-05-08 06:52:47 +03:00
polys = paths_to_polys_z(paths, z, z);
2012-05-08 06:52:47 +03:00
fill = p2d_area(polys, r_tool, 2*r_tool-overlap);
p2d_free_all(polys);
**res = polys_to_paths(fill, r_tool, z);
while (**res)
2012-05-08 06:52:47 +03:00
*res = &(**res)->next;
p2d_free_all(fill);
}
struct path *area(const struct path *paths, double overlap)
{
double z = HUGE_VAL, best_z;
const struct path *path;
struct path *res = NULL, **last = &res;
unsigned n = 0, bad = 0;
2012-05-08 06:52:47 +03:00
for (path = paths; path; path = path->next) {
bad += !path_is_closed(path);
n++;
}
if (bad) {
fprintf(stderr, "%u/%u open paths\n", bad, n);
exit(1);
}
2012-05-08 06:52:47 +03:00
while (1) {
best_z = -HUGE_VAL;
for (path = paths; path; path = path->next) {
if (path->first->z >= z)
continue;
if (best_z >= path->first->z)
continue;
best_z = path->first->z;
}
if (best_z == -HUGE_VAL)
break;
fill_path(paths, best_z, overlap, &last);
z = best_z;
}
return res;
}