112 #define INVERT_TRISTRIPS FALSE 121 #define EQ(a, b) (fabs((a) - (b)) <= GPC_EPSILON) 123 #define PREV_INDEX(i, n) ((i - 1 + n) % n) 124 #define NEXT_INDEX(i, n) ((i + 1 ) % n) 126 #define OPTIMAL(v, i, n) ((v[PREV_INDEX(i, n)].y != v[i].y) || \ 127 (v[NEXT_INDEX(i, n)].y != v[i].y)) 129 #define FWD_MIN(v, i, n) ((v[PREV_INDEX(i, n)].vertex.y >= v[i].vertex.y) \ 130 && (v[NEXT_INDEX(i, n)].vertex.y > v[i].vertex.y)) 132 #define NOT_FMAX(v, i, n) (v[NEXT_INDEX(i, n)].vertex.y > v[i].vertex.y) 134 #define REV_MIN(v, i, n) ((v[PREV_INDEX(i, n)].vertex.y > v[i].vertex.y) \ 135 && (v[NEXT_INDEX(i, n)].vertex.y >= v[i].vertex.y)) 137 #define NOT_RMAX(v, i, n) (v[PREV_INDEX(i, n)].vertex.y > v[i].vertex.y) 139 #define VERTEX(e,p,s,x,y) {add_vertex(&((e)->outp[(p)]->v[(s)]), x, y); \ 140 (e)->outp[(p)]->active++;} 142 #define P_EDGE(d,e,p,i,j) {(d)= (e); \ 143 do {(d)= (d)->prev;} while (!(d)->outp[(p)]); \ 144 (i)= (d)->bot.x + (d)->dx * ((j)-(d)->bot.y);} 146 #define N_EDGE(d,e,p,i,j) {(d)= (e); \ 147 do {(d)= (d)->next;} while (!(d)->outp[(p)]); \ 148 (i)= (d)->bot.x + (d)->dx * ((j)-(d)->bot.y);} 150 #define MALLOC(p, b, s, t) {if ((b) > 0) { \ 151 p= (t*)malloc(b); if (!(p)) { \ 152 fprintf(stderr, "gpc malloc failure: %s\n", s); \ 153 exit(0);}} else p= NULL;} 155 #define FREE(p) {if (p) {free(p); (p)= NULL;}} 221 : prev(0),next(0),pred(0),succ(0),next_bound(0)
346 if (e[0].bot.x < (*b)[0].
bot.
x)
355 if (e[0].bot.x == (*b)[0].
bot.
x)
358 if (e[0].dx < (*b)[0].dx)
390 (*lmt)->first_bound= NULL;
392 return &((*lmt)->first_bound);
401 (*lmt)->first_bound= NULL;
402 (*lmt)->next= existing_node;
403 return &((*lmt)->first_bound);
411 return &((*lmt)->first_bound);
422 (*sbtree)->less= NULL;
423 (*sbtree)->more= NULL;
428 if ((*sbtree)->y > y)
435 if ((*sbtree)->y < y)
449 sbt[*entries]= sbtree->
y;
487 int c, i, min, max, num_edges, v, num_vertices;
488 int total_vertices= 0, e_index=0;
496 for(
int k=0;k<total_vertices;++k)
518 edge_table[num_vertices].vertex.y);
524 for (min= 0; min < num_vertices; min++)
527 if (
FWD_MIN(edge_table, min, num_vertices))
532 while (
NOT_FMAX(edge_table, max, num_vertices))
539 e= &edge_table[e_index];
545 for (i= 0; i < num_edges; i++)
556 (e[i].top.y - e[i].
bot.
y);
562 e[i].
succ= ((num_edges > 1) && (i < (num_edges - 1))) ?
564 e[i].
pred= ((num_edges > 1) && (i > 0)) ? &(e[i - 1]) : NULL;
574 for (min= 0; min < num_vertices; min++)
577 if (
REV_MIN(edge_table, min, num_vertices))
582 while (
NOT_RMAX(edge_table, max, num_vertices))
589 e= &edge_table[e_index];
595 for (i= 0; i < num_edges; i++)
606 (e[i].top.y - e[i].
bot.
y);
612 e[i].
succ= ((num_edges > 1) && (i < (num_edges - 1))) ?
614 e[i].
pred= ((num_edges > 1) && (i > 0)) ? &(e[i - 1]) : NULL;
640 if (edge->
xb < (*aet)->xb)
650 if (edge->
xb == (*aet)->xb)
653 if (edge->
dx < (*aet)->dx)
694 if ((*it)->point.y > y)
703 (*it)->
next= existing_node;
730 den= ((*st)->xt - (*st)->xb) - (edge->
xt - edge->
xb);
733 if ((edge->
xt >= (*st)->xt) || (edge->
dx == (*st)->dx) ||
734 (fabs(den) <= DBL_EPSILON))
743 (*st)->prev= existing_node;
748 r= (edge->
xb - (*st)->xb) / den;
749 x= (*st)->
xb + r * ((*st)->xt - (*st)->xb);
772 for (edge= aet; edge; edge= edge->
next)
793 for (nc= 0; polygon; polygon= polygon->
next)
826 if(p == NULL)
throw runtime_error(
"GPC: Something's wrong.") ;
845 if(p == NULL)
throw runtime_error(
"GPC: Something's wrong.") ;
846 if(q == NULL)
throw runtime_error(
"GPC: Something's wrong.") ;
859 for (target= p->
proxy; list; list= list->
next)
861 if (list->
proxy == target)
875 if(p == NULL)
throw runtime_error(
"GPC: Something's wrong.") ;
895 if(p == NULL)
throw runtime_error(
"GPC: Something's wrong.") ;
896 if(q == NULL)
throw runtime_error(
"GPC: Something's wrong.") ;
909 for (target= p->
proxy; list; list= list->
next)
911 if (list->
proxy == target)
943 (*p)->next= existing_min;
958 for (total= 0; tn; tn= tn->
next)
989 (*tn)->v[
LEFT]= NULL;
990 (*tn)->v[
RIGHT]= NULL;
1012 box[c].
xmin= DBL_MAX;
1013 box[c].
ymin= DBL_MAX;
1014 box[c].
xmax= -DBL_MAX;
1015 box[c].
ymax= -DBL_MAX;
1036 bbox *s_bbox, *c_bbox;
1037 int s, c, *o_table, overlap;
1043 "overlap table creation",
int);
1049 (!((s_bbox[s].
xmax < c_bbox[c].
xmin) ||
1050 (s_bbox[s].
xmin > c_bbox[c].
xmax))) &&
1051 (!((s_bbox[s].
ymax < c_bbox[c].
ymin) ||
1052 (s_bbox[s].ymin > c_bbox[c].ymax)));
1058 for (s= 0; (!overlap) && (s < subj->num_contours); s++)
1072 for (c= 0; (!overlap) && (c < clip->num_contours); c++)
1141 if (write_hole_flags)
1142 fprintf(fp,
"%d\n", p->
hole[c]);
1145 fprintf(fp,
"% .*lf % .*lf\n",
1154 int *extended_hole, c, v;
1159 *
sizeof(
int),
"contour hole addition",
int);
1168 extended_hole[c]= p->
hole[c];
1169 extended_contour[c]= p->
contour[c];
1174 extended_hole[c]= hole;
1179 extended_contour[c].vertex[v]= new_contour->
vertex[v];
1187 p->
hole= extended_hole;
1196 it_node *it= NULL, *intersect=0;
1197 edge_node *edge=0, *prev_edge=0, *next_edge=0, *succ_edge=0, *e0=0, *e1=0;
1198 edge_node *aet= NULL, *c_heap= NULL, *s_heap= NULL;
1200 polygon_node *out_poly= NULL, *p=0, *q=0, *poly=0, *npoly=0, *cf= NULL;
1203 int in[2], exists[2], parity[2]= {
LEFT,
LEFT};
1204 int c, v, contributing=0, search, scanbeam= 0, sbt_entries= 0;
1205 int vclass=0, bl=0, br=0, tl=0, tr=0;
1206 double *sbt= NULL, xb, px, yb, yt=0.0, dy=0.0, ix, iy;
1226 s_heap=
build_lmt(&lmt, &sbtree, &sbt_entries, subj,
SUBJ, op);
1228 c_heap=
build_lmt(&lmt, &sbtree, &sbt_entries, clip,
CLIP, op);
1243 MALLOC(sbt, sbt_entries *
sizeof(
double),
"sbt creation",
double);
1261 while (scanbeam < sbt_entries)
1264 yb= sbt[scanbeam++];
1265 if (scanbeam < sbt_entries)
1276 if (local_min->y == yb)
1279 for (edge= local_min->first_bound; edge; edge= edge->
next_bound)
1282 local_min= local_min->next;
1298 for (next_edge= aet->
next; next_edge; next_edge= next_edge->
next)
1301 next_edge->bundle[
ABOVE][ next_edge->type]= (next_edge->top.y != yb);
1302 next_edge->bundle[
ABOVE][!next_edge->type]=
FALSE;
1306 if (next_edge->bundle[
ABOVE][next_edge->type])
1308 if (
EQ(e0->xb, next_edge->xb) &&
EQ(e0->dx, next_edge->dx)
1309 && (e0->top.y != yb))
1311 next_edge->bundle[
ABOVE][ next_edge->type]^=
1312 e0->bundle[
ABOVE][ next_edge->type];
1313 next_edge->bundle[
ABOVE][!next_edge->type]=
1314 e0->bundle[
ABOVE][!next_edge->type];
1328 for (edge= aet; edge; edge= edge->
next)
1346 contributing= (exists[
CLIP] && (parity[
SUBJ] || horiz[
SUBJ]))
1349 && (parity[
CLIP] == parity[
SUBJ]));
1360 contributing= exists[
CLIP] || exists[
SUBJ];
1371 contributing= (exists[
CLIP] && (!parity[
SUBJ] || horiz[
SUBJ]))
1374 && (parity[
CLIP] == parity[
SUBJ]));
1394 [((exists[
CLIP] - 1) << 1) + parity[
CLIP]];
1398 [((exists[
SUBJ] - 1) << 1) + parity[
SUBJ]];
1400 vclass= tr + (tl << 1) + (br << 2) + (bl << 3);
1485 if (edge->
bot.
y == yb)
1491 if (edge->
bot.
y == yb)
1504 for (edge= aet; edge; edge= edge->
next)
1506 if (edge->
top.
y == yb)
1508 prev_edge= edge->
prev;
1509 next_edge= edge->
next;
1511 prev_edge->
next= next_edge;
1515 next_edge->
prev= prev_edge;
1524 if (prev_edge->prev)
1532 if (edge->
top.
y == yt)
1539 if (scanbeam < sbt_entries)
1546 for (intersect= it; intersect; intersect= intersect->
next)
1548 e0= intersect->ie[0];
1549 e1= intersect->ie[1];
1557 ix= intersect->point.x;
1558 iy= intersect->point.y + yb;
1563 && e0->bside[
CLIP] && e1->bside[
CLIP]);
1565 || ( e1->bundle[
ABOVE][SUBJ] && e1->bside[SUBJ])
1567 && e0->bside[
SUBJ] && e1->bside[
SUBJ]);
1577 && (in[SUBJ] ^ e1->bundle[
ABOVE][SUBJ]);
1579 && (in[SUBJ] ^ e0->bundle[
ABOVE][SUBJ]);
1581 && (in[SUBJ] ^ e1->bundle[
ABOVE][SUBJ] ^ e0->bundle[
ABOVE][SUBJ]);
1587 ^ (in[SUBJ] ^ e1->bundle[
ABOVE][SUBJ]);
1589 ^ (in[SUBJ] ^ e0->bundle[
ABOVE][SUBJ]);
1591 ^ (in[SUBJ] ^ e1->bundle[
ABOVE][SUBJ] ^ e0->bundle[
ABOVE][SUBJ]);
1597 || (in[SUBJ] ^ e1->bundle[
ABOVE][SUBJ]);
1599 || (in[SUBJ] ^ e0->bundle[
ABOVE][SUBJ]);
1601 || (in[SUBJ] ^ e1->bundle[
ABOVE][SUBJ] ^ e0->bundle[
ABOVE][SUBJ]);
1605 vclass= tr + (tl << 1) + (br << 2) + (bl << 3);
1618 e0->outp[
ABOVE]= NULL;
1626 e1->outp[
ABOVE]= NULL;
1634 e0->outp[
ABOVE]= NULL;
1635 e1->outp[
ABOVE]= NULL;
1647 e0->outp[
ABOVE]= NULL;
1655 e1->outp[
ABOVE]= NULL;
1663 e0->outp[
ABOVE]= NULL;
1664 e1->outp[
ABOVE]= NULL;
1693 if (e1->bundle[
ABOVE][CLIP])
1695 if (e0->bundle[
ABOVE][SUBJ])
1697 if (e1->bundle[
ABOVE][SUBJ])
1701 prev_edge= e0->prev;
1702 next_edge= e1->next;
1704 next_edge->prev= e0;
1711 prev_edge= prev_edge->prev;
1733 if(e0->next == NULL)
throw runtime_error(
"GPC internal error.") ;
1734 if(e1->next == NULL)
throw runtime_error(
"GPC internal error.") ;
1735 e0->next->prev= prev_edge;
1737 e0->next= next_edge;
1741 for (edge= aet; edge; edge= next_edge)
1743 next_edge= edge->
next;
1744 succ_edge= edge->
succ;
1746 if ((edge->
top.
y == yt) && succ_edge)
1753 prev_edge= edge->
prev;
1755 prev_edge->
next= succ_edge;
1759 next_edge->
prev= succ_edge;
1760 succ_edge->
prev= prev_edge;
1761 succ_edge->
next= next_edge;
1784 *
sizeof(
int),
"hole flag table creation",
int);
1789 for (poly= out_poly; poly; poly= npoly)
1794 result->
hole[c]= poly->proxy->hole;
1801 for (vtx= poly->proxy->v[LEFT]; vtx; vtx= nv)
1816 for (poly= out_poly; poly; poly= npoly)
1858 it_node *it= NULL, *intersect;
1859 edge_node *edge=0, *prev_edge=0, *next_edge=0, *succ_edge=0, *e0=0, *e1=0;
1860 edge_node *aet= NULL, *c_heap= NULL, *s_heap= NULL, *cf=0;
1866 int in[2], exists[2], parity[2]= {
LEFT,
LEFT};
1867 int s, v, contributing=0, search, scanbeam= 0, sbt_entries= 0;
1868 int vclass=0, bl=0, br=0, tl=0, tr=0;
1869 double *sbt= NULL, xb, px, nx, yb, yt=0.0, dy=0.0, ix, iy;
1877 result->
strip= NULL;
1888 s_heap=
build_lmt(&lmt, &sbtree, &sbt_entries, subj,
SUBJ, op);
1890 c_heap=
build_lmt(&lmt, &sbtree, &sbt_entries, clip,
CLIP, op);
1896 result->
strip= NULL;
1904 MALLOC(sbt, sbt_entries *
sizeof(
double),
"sbt creation",
double);
1916 while (scanbeam < sbt_entries)
1919 yb= sbt[scanbeam++];
1920 if (scanbeam < sbt_entries)
1931 if (local_min->y == yb)
1934 for (edge= local_min->first_bound; edge; edge= edge->
next_bound)
1937 local_min= local_min->next;
1953 for (next_edge= aet->
next; next_edge; next_edge= next_edge->
next)
1956 next_edge->bundle[
ABOVE][ next_edge->type]= (next_edge->top.y != yb);
1957 next_edge->bundle[
ABOVE][!next_edge->type]=
FALSE;
1961 if (next_edge->bundle[
ABOVE][next_edge->type])
1963 if (
EQ(e0->xb, next_edge->xb) &&
EQ(e0->dx, next_edge->dx)
1964 && (e0->top.y != yb))
1966 next_edge->bundle[
ABOVE][ next_edge->type]^=
1967 e0->bundle[
ABOVE][ next_edge->type];
1968 next_edge->bundle[
ABOVE][!next_edge->type]=
1969 e0->bundle[
ABOVE][!next_edge->type];
1983 for (edge= aet; edge; edge= edge->
next)
2001 contributing= (exists[
CLIP] && (parity[
SUBJ] || horiz[
SUBJ]))
2004 && (parity[
CLIP] == parity[
SUBJ]));
2015 contributing= exists[
CLIP] || exists[
SUBJ];
2026 contributing= (exists[
CLIP] && (!parity[
SUBJ] || horiz[
SUBJ]))
2029 && (parity[
CLIP] == parity[
SUBJ]));
2049 [((exists[
CLIP] - 1) << 1) + parity[
CLIP]];
2053 [((exists[
SUBJ] - 1) << 1) + parity[
SUBJ]];
2055 vclass= tr + (tl << 1) + (br << 2) + (bl << 3);
2087 if (cf->bot.y != yb)
2102 if (cf->bot.y != yb)
2128 if (edge->
bot.
y == yb)
2138 if (cf->bot.y == yb)
2144 if (edge->
bot.
y == yb)
2166 for (edge= aet; edge; edge= edge->
next)
2168 if (edge->
top.
y == yb)
2170 prev_edge= edge->
prev;
2171 next_edge= edge->
next;
2173 prev_edge->
next= next_edge;
2177 next_edge->
prev= prev_edge;
2186 if (prev_edge->prev)
2194 if (edge->
top.
y == yt)
2201 if (scanbeam < sbt_entries)
2208 for (intersect= it; intersect; intersect= intersect->
next)
2210 e0= intersect->ie[0];
2211 e1= intersect->ie[1];
2219 ix= intersect->point.x;
2220 iy= intersect->point.y + yb;
2225 && e0->bside[
CLIP] && e1->bside[
CLIP]);
2227 || ( e1->bundle[
ABOVE][SUBJ] && e1->bside[SUBJ])
2229 && e0->bside[
SUBJ] && e1->bside[
SUBJ]);
2239 && (in[SUBJ] ^ e1->bundle[
ABOVE][SUBJ]);
2241 && (in[SUBJ] ^ e0->bundle[
ABOVE][SUBJ]);
2243 && (in[SUBJ] ^ e1->bundle[
ABOVE][SUBJ] ^ e0->bundle[
ABOVE][SUBJ]);
2249 ^ (in[SUBJ] ^ e1->bundle[
ABOVE][SUBJ]);
2251 ^ (in[SUBJ] ^ e0->bundle[
ABOVE][SUBJ]);
2253 ^ (in[SUBJ] ^ e1->bundle[
ABOVE][SUBJ] ^ e0->bundle[
ABOVE][SUBJ]);
2259 || (in[SUBJ] ^ e1->bundle[
ABOVE][SUBJ]);
2261 || (in[SUBJ] ^ e0->bundle[
ABOVE][SUBJ]);
2263 || (in[SUBJ] ^ e1->bundle[
ABOVE][SUBJ] ^ e0->bundle[
ABOVE][SUBJ]);
2267 vclass= tr + (tl << 1) + (br << 2) + (bl << 3);
2282 e0->outp[
ABOVE]= NULL;
2292 e1->outp[
ABOVE]= NULL;
2299 e0->outp[
ABOVE]= NULL;
2300 e1->outp[
ABOVE]= NULL;
2322 e0->outp[
ABOVE]= NULL;
2332 e1->outp[
ABOVE]= NULL;
2340 e0->outp[
ABOVE]= NULL;
2341 e1->outp[
ABOVE]= NULL;
2347 next_edge->outp[
ABOVE]= prev_edge->outp[
ABOVE];
2384 if (e1->bundle[
ABOVE][CLIP])
2386 if (e0->bundle[
ABOVE][SUBJ])
2388 if (e1->bundle[
ABOVE][SUBJ])
2392 prev_edge= e0->prev;
2393 next_edge= e1->next;
2402 prev_edge= prev_edge->prev;
2405 if (prev_edge->bundle[
ABOVE][CLIP]
2406 || prev_edge->bundle[
ABOVE][SUBJ]
2426 e0->
next= next_edge;
2430 for (edge= aet; edge; edge= next_edge)
2432 next_edge= edge->
next;
2433 succ_edge= edge->
succ;
2435 if ((edge->
top.
y == yt) && succ_edge)
2442 prev_edge= edge->
prev;
2444 prev_edge->
next= succ_edge;
2448 next_edge->
prev= succ_edge;
2449 succ_edge->
prev= prev_edge;
2450 succ_edge->
next= next_edge;
2467 result->
strip= NULL;
2475 for (tn= tlist; tn; tn= tnn)
2522 for (lt= tn->v[LEFT]; lt; lt= ltn)
2527 for (rt= tn->v[
RIGHT]; rt; rt=rtn)
static void free_sbtree(sb_tree **sbtree)
static edge_node ** bound_list(lmt_node **lmt, double y)
gpc_vertex_list * contour
struct sbt_t_shape sb_tree
struct v_shape vertex_node
void gpc_polygon_clip(gpc_op op, gpc_polygon *subj, gpc_polygon *clip, gpc_polygon *result)
static void add_edge_to_aet(edge_node **aet, edge_node *edge, edge_node *prev)
static void build_intersection_table(it_node **it, edge_node *aet, double dy)
#define MALLOC(p, b, s, t)
static bbox * create_contour_bboxes(gpc_polygon *p)
static void add_intersection(it_node **it, edge_node *edge0, edge_node *edge1, double x, double y)
#define NOT_FMAX(v, i, n)
static void build_sbt(int *entries, double *sbt, sb_tree *sbtree)
static void add_left(polygon_node *p, double x, double y)
#define VERTEX(e, p, s, x, y)
static void reset_it(it_node **it)
void gpc_tristrip_clip(gpc_op op, gpc_polygon *subj, gpc_polygon *clip, gpc_tristrip *result)
static void add_right(polygon_node *p, double x, double y)
static void add_vertex(vertex_node **t, double x, double y)
struct sbt_t_shape * more
#define NOT_RMAX(v, i, n)
void gpc_free_tristrip(gpc_tristrip *t)
static void reset_lmt(lmt_node **lmt)
#define P_EDGE(d, e, p, i, j)
static void merge_right(polygon_node *p, polygon_node *q, polygon_node *list)
void gpc_polygon_to_tristrip(gpc_polygon *s, gpc_tristrip *t)
static int count_optimal_vertices(gpc_vertex_list c)
void gpc_free_polygon(gpc_polygon *p)
static void merge_left(polygon_node *p, polygon_node *q, polygon_node *list)
static void insert_bound(edge_node **b, edge_node *e)
void gpc_write_polygon(FILE *fp, int write_hole_flags, gpc_polygon *p)
static void add_to_sbtree(int *entries, sb_tree **sbtree, double y)
static void minimax_test(gpc_polygon *subj, gpc_polygon *clip, gpc_op op)
static void new_tristrip(polygon_node **tn, edge_node *edge, double x, double y)
void gpc_add_contour(gpc_polygon *p, gpc_vertex_list *new_contour, int hole)
static void add_local_min(polygon_node **p, edge_node *edge, double x, double y)
static int count_tristrips(polygon_node *tn)
const h_state next_h_state[3][6]
struct sbt_t_shape * less
#define N_EDGE(d, e, p, i, j)
static void add_st_edge(st_node **st, it_node **it, edge_node *edge, double dy)
static int count_contours(polygon_node *polygon)
static edge_node * build_lmt(lmt_node **lmt, sb_tree **sbtree, int *sbt_entries, gpc_polygon *p, int type, gpc_op op)