cae-tools/poly2d/poly2d.h

183 lines
4.7 KiB
C

/*
* poly2d.h - The public face of the 2D Polygon library
*
* Written 2012, 2013, 2015 by Werner Almesberger
* Copyright 2012, 2013, 2015 Werner Almesberger
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*/
#ifndef POLY2D_H
#define POLY2D_H
#include <stdbool.h>
#include <stdio.h>
struct v2d {
double x, y;
struct v2d *next; /* may end in NULL or may be cyclic */
};
struct p2d {
struct v2d *v; /* vertices */
struct v2d *last; /* last vertex or vertex preceding first */
struct p2d *next;
};
struct f2d {
double x[3]; /* vertices forming the face */
double y[3];
bool side[3]; /* side[i] if v[i] and v[(i+1) mod 3] are on polygon */
struct f2d *next;
};
/*
* forall_v2d* need -std=gnu99
*/
#define forall_v2d_once(var, first) \
for (const struct v2d *__fv2do_0 = (var = (first)); \
var; \
var = var->next == __fv2do_0 ? NULL : var->next)
#define forall_v2d(var, first) \
for (const struct v2d *__fv2d_0 = (var = (first)); \
var; \
({ \
var = __fv2d_0 ? var->next : NULL; \
if (var == __fv2d_0) \
__fv2d_0 = NULL; \
}))
#define forall_edges(a, b, first) \
for (const struct v2d *__fe_0 = \
(a = (first), b = a ? a->next : a, a); \
a && b; \
({ \
a = b; \
b = b == __fe_0 ? NULL : b->next; \
}))
/*
* Polygon creation
*/
struct p2d *p2d_new(void);
struct v2d *v2d_new(double x, double y);
void p2d_append(struct p2d *p, struct v2d *v);
void p2d_prepend(struct p2d *p, struct v2d *v);
void p2d_close(struct p2d *p);
/*
* Intersect line from A0 to A1 with line from B0 to B1.
*
* Returns:
* 0 if the lines are parallel,
* 1 if the lines intersect between A0-A1 and B0-B1,
* -1 if the lines intersect outside A0-A1 or B0-B1.
*
* If v2d_intersect returns non-zero, the intersection P is at
*
* P = A0+(A1-A0)*na = B0+(B1-B0)*nb
*/
int v2d_intersect(const struct v2d *a0, const struct v2d *a1,
const struct v2d *b0, const struct v2d *b1,
double *na, double *nb);
/*
* Calculate the distance between point P and the line from A to B.
* The result is negative if P is on the "right" side of A->B.
*/
double v2d_line_distance(const struct v2d *a, const struct v2d *b,
const struct v2d *p);
/*
* Duplicate a polygon
*/
struct p2d *p2d_copy(const struct p2d *p);
struct p2d *p2d_copy_all(const struct p2d *p);
/*
* Change a polygon from clockwise to counter-clockwise and vice versa.
*/
struct p2d *p2d_reverse(const struct p2d *p);
/*
* p2d_is_cw determine whether a polygon is clockwise.
* p2d_is_closed determines whether a polygon is closed.
* p2d_no_intersect determines whether a polygon does't self-intersect.
* p2d_vertices counts the number of vertices in a polygon.
*/
bool p2d_is_cw(const struct p2d *p);
bool p2d_is_closed(const struct p2d *p);
bool p2d_no_intersect(const struct p2d *p);
unsigned p2d_vertices(const struct p2d *p);
/*
* Convert a possibly self-intersecting polygon into one or more simple
* polygons. [1]
*
* http://en.wikipedia.org/wiki/Simple_polygon
*/
struct p2d *p2d_simplify(const struct p2d *p);
/*
* p2d_free deallocates a single polygon and its vertices.
* p2d_free_all deallocates all polygons in a list.
*/
void p2d_free(struct p2d *p);
void p2d_free_all(struct p2d *p);
/*
* Returns non-zero if the point is inside or on the simple polygon.
*/
bool p2d_contains_point(const struct p2d *p, const struct v2d *v);
/*
* Returns:
* 0 if polygon "b" is outside of polygon "a"
* 1 if polygon "b" is inside of polygon "a"
* -1 if the two polygons intersect
*/
int p2d_contains_poly(const struct p2d *a, const struct p2d *b);
struct p2d *p2d_offset_holes(const struct p2d *p, const struct p2d *holes,
double off);
struct p2d *p2d_offset(const struct p2d *p, double off);
void p2d_area_holes_append(const struct p2d *p,
const struct p2d *holes, double first, double next,
struct p2d ***last);
struct p2d *p2d_area_holes(const struct p2d *p, const struct p2d *holes,
double first, double next);
struct p2d *p2d_area(const struct p2d *p, double first, double next);
void f2d_tri_holes_append(const struct p2d *p, const struct p2d *holes,
struct f2d ***last);
struct f2d *f2d_tri_holes(const struct p2d *p, const struct p2d *holes);
struct f2d *f2d_tri(const struct p2d *p);
void f2d_free_all(struct f2d *f);
struct p2d *p2d_read_gnuplot(FILE *file);
bool p2d_write_gnuplot(FILE *file, const struct p2d *p);
bool p2d_write_gnuplot_all(FILE *file, const struct p2d *p);
#endif /* !POLY2D_H */