X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=lib%2Fgfxpoly%2Fpoly.c;h=16b8ca735f7aacff006b5f593db9f67d6755cb17;hb=9b4a07900f07d05f0f39c511c189489114817aba;hp=01781ccb812bb058e69fe3ba1b24cde4c233b126;hpb=678260583255100b0dce559e0f8f2f813e6f162e;p=swftools.git diff --git a/lib/gfxpoly/poly.c b/lib/gfxpoly/poly.c index 01781cc..16b8ca7 100644 --- a/lib/gfxpoly/poly.c +++ b/lib/gfxpoly/poly.c @@ -239,8 +239,8 @@ char gfxpoly_check(gfxpoly_t*poly, char updown) DICT_ITERATE_ITEMS(d2, point_t*, p2, void*, c2) { int count = (ptroff_t)c2; if(count!=0) { - if(count>0) fprintf(stderr, "Error: Point (%d,%d) has %d more incoming than outgoing segments\n", p2->x, p2->y, count); - if(count<0) fprintf(stderr, "Error: Point (%d,%d) has %d more outgoing than incoming segments\n", p2->x, p2->y, -count); + if(count>0) fprintf(stderr, "Error: Point (%.2f,%.2f) has %d more incoming than outgoing segments\n", p2->x * poly->gridsize, p2->y * poly->gridsize, count); + if(count<0) fprintf(stderr, "Error: Point (%.2f,%.2f) has %d more outgoing than incoming segments\n", p2->x * poly->gridsize, p2->y * poly->gridsize, -count); dict_destroy(d2); return 0; } @@ -382,21 +382,26 @@ static void segment_dump(segment_t*s) static void segment_init(segment_t*s, int32_t x1, int32_t y1, int32_t x2, int32_t y2, int polygon_nr, segment_dir_t dir) { + static int segment_count=0; + s->nr = segment_count++; s->dir = dir; if(y1!=y2) { assert(y1dir = DIR_UP; if(x1>x2) { - s->dir = DIR_DOWN; + s->dir = DIR_INVERT(s->dir); int32_t x = x1;x1=x2;x2=x; int32_t y = y1;y1=y2;y2=y; } + fprintf(stderr, "Scheduling horizontal segment [%d] (%.2f,%.2f) -> (%.2f,%.2f) %s\n", + segment_count, + x1 * 0.05, y1 * 0.05, x2 * 0.05, y2 * 0.05, s->dir==DIR_UP?"up":"down"); } s->a.x = x1; s->a.y = y1; @@ -411,8 +416,6 @@ static void segment_init(segment_t*s, int32_t x1, int32_t y1, int32_t x2, int32_ s->pos = s->a; s->polygon_nr = polygon_nr; - static int segment_count=0; - s->nr = segment_count++; #ifdef CHECKS /* notice: on some systems (with some compilers), for the line @@ -461,7 +464,7 @@ static void segment_destroy(segment_t*s) free(s); } -static void advance_stroke(queue_t*queue, hqueue_t*hqueue, gfxpolystroke_t*stroke, int polygon_nr, int pos) +static void advance_stroke(queue_t*queue, hqueue_t*hqueue, gfxpolystroke_t*stroke, int polygon_nr, int pos, double gridsize) { if(!stroke) return; @@ -478,8 +481,9 @@ static void advance_stroke(queue_t*queue, hqueue_t*hqueue, gfxpolystroke_t*strok #ifdef DEBUG /*if(l->tmp) s->nr = l->tmp;*/ - fprintf(stderr, "[%d] (%d,%d) -> (%d,%d) %s (stroke %p, %d more to come)\n", - s->nr, s->a.x, s->a.y, s->b.x, s->b.y, + fprintf(stderr, "[%d] (%.2f,%.2f) -> (%.2f,%.2f) %s (stroke %p, %d more to come)\n", + s->nr, s->a.x * gridsize, s->a.y * gridsize, + s->b.x * gridsize, s->b.y * gridsize, s->dir==DIR_UP?"up":"down", stroke, stroke->num_points - 1 - pos); #endif event_t* e = event_new(); @@ -514,7 +518,7 @@ static void gfxpoly_enqueue(gfxpoly_t*p, queue_t*queue, hqueue_t*hqueue, int pol assert(stroke->points[s].y <= stroke->points[s+1].y); } #endif - advance_stroke(queue, hqueue, stroke, polygon_nr, 0); + advance_stroke(queue, hqueue, stroke, polygon_nr, 0, p->gridsize); } } @@ -799,15 +803,18 @@ static void insert_point_into_segment(status_t*status, segment_t*s, point_t p) /* non horizontal line- copy to output */ if(s->fs_out) { #ifdef DEBUG - fprintf(stderr, "[%d] receives next point (%d,%d)->(%d,%d) (drawing)\n", s->nr, - s->pos.x, s->pos.y, p.x, p.y); + fprintf(stderr, "[%d] receives next point (%.2f,%.2f)->(%.2f,%.2f) (drawing)\n", s->nr, + s->pos.x * status->gridsize, s->pos.y * status->gridsize, + p.x * status->gridsize, p.y * status->gridsize); #endif segment_dir_t dir = s->wind.is_filled?DIR_DOWN:DIR_UP; assert(s->pos.y != p.y); append_stroke(status, s->pos, p, dir, s->fs_out); } else { #ifdef DEBUG - fprintf(stderr, "[%d] receives next point (%d,%d) (omitting)\n", s->nr, p.x, p.y); + fprintf(stderr, "[%d] receives next point (%.2f,%.2f) (omitting)\n", s->nr, + p.x * status->gridsize, + p.y * status->gridsize); #endif } } else { @@ -1087,8 +1094,9 @@ static void intersect_with_horizontal(status_t*status, segment_t*h) segment_t* left = actlist_find(status->actlist, h->a, h->a); segment_t* right = actlist_find(status->actlist, h->b, h->b); - /* not strictly necessary, also done by the event */ + /* h->a.x is not strictly necessary, as it's also done by the event */ xrow_add(status->xrow, h->a.x); + xrow_add(status->xrow, h->b.x); if(!right) { assert(!left); @@ -1104,7 +1112,6 @@ static void intersect_with_horizontal(status_t*status, segment_t*h) assert(s); int32_t x = XPOS_INT(s, status->y); point_t p = {x, status->y}; - store_horizontal(status, o, p, h->fs, h->dir, h->polygon_nr); #ifdef DEBUG fprintf(stderr, "...intersecting with [%d] (%.2f,%.2f) -> (%.2f,%.2f) at (%.2f,%.2f)\n", s->nr, @@ -1156,6 +1163,7 @@ static void process_horizontals(status_t*status) horizdata_t*horiz = &status->horiz; qsort(horiz->data, horiz->num, sizeof(horizontal_t), compare_horizontals); int t; + int xstart = 0; for(t=0;tnum;t++) { horizontal_t*h = &horiz->data[t]; #ifdef DEBUG @@ -1168,42 +1176,63 @@ static void process_horizontals(status_t*status) assert(h->y == status->y); assert(xrow_contains(status->xrow, h->x1)); assert(xrow_contains(status->xrow, h->x2)); - - point_t p1 = {h->x1,h->y}; - point_t p2 = {h->x2,h->y}; - segment_t* left = actlist_find(status->actlist, p1, p2); - assert(!left || left->fs_out_ok); + + int pos = xrow_find(status->xrow, h->x1); + int x = h->x1; + assert(pos <= status->xrow->num); + assert(pos == status->xrow->num || status->xrow->x[pos] > x); + + while(x < h->x2) { + int next_x = pos < status->xrow->num ? status->xrow->x[pos] : h->x2; + pos++; + + assert(next_x > x); + + point_t p1 = {x,h->y}; + point_t p2 = {next_x,h->y}; + segment_t* left = actlist_find(status->actlist, p1, p2); + assert(!left || left->fs_out_ok); #ifdef DEBUG - if(left) { - fprintf(stderr, " segment [%d] (%.2f,%.2f -> %.2f,%2f, at %.2f,%.2f) is to the left\n", - SEGNR(left), - left->a.x * status->gridsize, - left->a.y * status->gridsize, - left->b.x * status->gridsize, - left->b.y * status->gridsize, - left->pos.x * status->gridsize, - left->pos.y * status->gridsize - ); - /* this segment might be a distance away from the left point - of the horizontal line if the horizontal line belongs to a stroke - with segments that just ended (so this horizontal line appears to - be "floating in space" from our current point of view) - assert(left->pos.y == h->y && left->pos.x == h->x1); - */ - } + fprintf(stderr, " fragment %.2f..%.2f edge=%08x\n", + x * status->gridsize, + next_x * status->gridsize, + h->fs); + if(left) { + fprintf(stderr, " segment [%d] (%.2f,%.2f -> %.2f,%2f, at %.2f,%.2f) is to the left\n", + SEGNR(left), + left->a.x * status->gridsize, + left->a.y * status->gridsize, + left->b.x * status->gridsize, + left->b.y * status->gridsize, + left->pos.x * status->gridsize, + left->pos.y * status->gridsize + ); + /* this segment might be a distance away from the left point + of the horizontal line if the horizontal line belongs to a stroke + with segments that just ended (so this horizontal line appears to + be "floating in space" from our current point of view) + assert(left->pos.y == h->y && left->pos.x == h->x1); + */ + } #endif - windstate_t below = left?left->wind:status->windrule->start(status->context); - windstate_t above = status->windrule->add(status->context, below, h->fs, DIR_INVERT(h->dir), h->polygon_nr); - edgestyle_t*fs = status->windrule->diff(&above, &below); - if(fs) { + windstate_t below = left?left->wind:status->windrule->start(status->context); + windstate_t above = status->windrule->add(status->context, below, h->fs, h->dir, h->polygon_nr); + edgestyle_t*fs = status->windrule->diff(&above, &below); + if(fs) { #ifdef DEBUG - fprintf(stderr, " ...storing\n"); + fprintf(stderr, " ...storing\n"); #endif - append_stroke(status, p1, p2, h->dir, fs); - } else { + append_stroke(status, p1, p2, DIR_INVERT(h->dir), fs); + } else { #ifdef DEBUG - fprintf(stderr, " ...ignoring\n"); + fprintf(stderr, " ...ignoring (below: (wind_nr=%d, filled=%d), above: (wind_nr=%d, filled=%d) %s\n", + below.wind_nr, below.is_filled, + above.wind_nr, above.is_filled, + h->dir==DIR_UP?"up":"down" + ); #endif + } + x = next_x; } } } @@ -1247,7 +1276,8 @@ static void event_apply(status_t*status, event_t*e) case EVENT_HORIZONTAL: { segment_t*s = e->s1; intersect_with_horizontal(status, s); - advance_stroke(&status->queue, 0, s->stroke, s->polygon_nr, s->stroke_pos); + store_horizontal(status, s->a, s->b, s->fs, s->dir, s->polygon_nr); + advance_stroke(&status->queue, 0, s->stroke, s->polygon_nr, s->stroke_pos, status->gridsize); segment_destroy(s);e->s1=0; break; } @@ -1269,7 +1299,7 @@ static void event_apply(status_t*status, event_t*e) /* schedule segment for xrow handling */ s->left = 0; s->right = status->ending_segments; status->ending_segments = s; - advance_stroke(&status->queue, 0, s->stroke, s->polygon_nr, s->stroke_pos); + advance_stroke(&status->queue, 0, s->stroke, s->polygon_nr, s->stroke_pos, status->gridsize); break; } case EVENT_START: { @@ -1334,164 +1364,6 @@ static void check_status(status_t*status) } #endif -static void add_horizontals(gfxpoly_t*poly, windrule_t*windrule, windcontext_t*context) -{ - /* - |..| |...........| | | - |..| |...........| | | - |..+ + +..| +--+ +--+ - |...........| |..| | | - |...........| |..| | | - */ - -#ifdef DEBUG - fprintf(stderr, "========================================================================\n"); -#endif - hqueue_t hqueue; - hqueue_init(&hqueue); - gfxpoly_enqueue(poly, 0, &hqueue, 0); - - actlist_t* actlist = actlist_new(); - - event_t*e = hqueue_get(&hqueue); - while(e) { - int32_t y = e->p.y; - int32_t x = 0; -#ifdef DEBUG - fprintf(stderr, "HORIZONTALS ----------------------------------- %f\n", y); - actlist_dump(actlist, y-1, 1.0); -#endif -#ifdef CHECKS - actlist_verify(actlist, y-1); -#endif - edgestyle_t*fill = 0; - int wind = 0; - - do { - assert(e->s1->fs); - if(fill && x != e->p.x) { - assert(abs(wind)==1); - assert(xp.x); - - gfxpolystroke_t*stroke = rfx_calloc(sizeof(gfxpolystroke_t)); - stroke->next = poly->strokes; - poly->strokes = stroke; - - stroke->num_points = 2; - stroke->points = malloc(sizeof(point_t)*2); - - if(wind>0) { - stroke->dir = DIR_DOWN; - } else { - stroke->dir = DIR_UP; - } -#ifdef DEBUG - fprintf(stderr, "%d) draw horizontal line from %d to %d (dir=%s)\n", y, x, e->p.x, stroke->dir==DIR_UP?"up":"down"); -#endif - - stroke->fs = fill; - point_t a,b; - a.y = b.y = y; - a.x = x; - b.x = e->p.x; - stroke->points[0] = a; - stroke->points[1] = b; -#ifdef CHECKS - /* the output should always be intersection free polygons, so check this horizontal - line isn't puncturing any segments in the active list */ - segment_t* start = actlist_find(actlist, a, a); - segment_t* s = actlist_find(actlist, b, b); - while(s!=start) { - assert(s->a.y == y || s->b.y == y); - s = s->left; - } -#endif - } - - segment_t*s = e->s1; - - segment_t*left = 0; - switch(e->type) { - case EVENT_START: { - assert(e->p.x == s->a.x && e->p.y == s->a.y); - actlist_insert(actlist, s->a, s->b, s); - event_t* e = event_new(); - e->type = EVENT_END; - e->p = s->b; - e->s1 = s; - e->s2 = 0; - hqueue_put(&hqueue, e); - left = actlist_left(actlist, s); - wind += e->s1->dir==DIR_DOWN?-1:1; - break; - } - case EVENT_END: { - left = actlist_left(actlist, s); - actlist_delete(actlist, s); - advance_stroke(0, &hqueue, s->stroke, s->polygon_nr, s->stroke_pos); - wind += e->s1->dir==DIR_DOWN?1:-1; - break; - } - default: assert(0); - } - - x = e->p.x; - - fill = fill?0:&edgestyle_default; -#if 0 - if(windrule==&windrule_evenodd) { - if(!!fill != !!fill2) { - segment_dump(s); - event_dump(e); - printf("at y=%d x=%d (hline:%p)\n", e->p.y, x, old_fill); - if(e->type==EVENT_END) { - printf(" %9p\n", s->fs); - printf(" |\n"); - } - printf(" %3d %c%2d \n", before1.is_filled, e->type==EVENT_END?'|':' ', after1.is_filled); - printf("%12p -----+----- %p\n", old_fill, fill2); - printf(" %3d %c%2d \n", before2.is_filled, e->type==EVENT_START?'|':' ', after2.is_filled); - if(e->type==EVENT_START) { - printf(" |\n"); - printf(" %9p\n", s->fs); - } - } - assert(!!fill == !!fill2); - } -#endif - -#ifdef DEBUG - fprintf(stderr, "%d) event=%s[%d] left:[%d] x:%d dir:%s\n", - y, e->type==EVENT_START?"start":"end", - s->nr, - left?left->nr:-1, - x, s->dir==DIR_UP?"up":"down"); -#endif - - if(e->type == EVENT_END) - segment_destroy(s); - - event_free(e); - e = hqueue_get(&hqueue); - } while(e && y == e->p.y); - -#ifdef CHECKS - edgestyle_t*bleeding = fill; - assert(!bleeding); - segment_t*s = actlist_leftmost(actlist); - int dir = 0; - while(s) { - dir += s->dir==DIR_UP?-1:1; - s = actlist_right(actlist, s); - } - assert(!dir); -#endif - } - - actlist_destroy(actlist); - hqueue_destroy(&hqueue); -} - gfxpoly_t* gfxpoly_process(gfxpoly_t*poly1, gfxpoly_t*poly2, windrule_t*windrule, windcontext_t*context) { current_polygon = poly1; @@ -1499,6 +1371,8 @@ gfxpoly_t* gfxpoly_process(gfxpoly_t*poly1, gfxpoly_t*poly2, windrule_t*windrule status_t status; memset(&status, 0, sizeof(status_t)); status.gridsize = poly1->gridsize; + + gfxpoly_dump(poly1); queue_init(&status.queue); gfxpoly_enqueue(poly1, &status.queue, 0, /*polygon nr*/0); @@ -1552,6 +1426,7 @@ gfxpoly_t* gfxpoly_process(gfxpoly_t*poly1, gfxpoly_t*poly2, windrule_t*windrule #ifdef DEBUG actlist_dump(status.actlist, status.y, status.gridsize); #endif + xrow_dump(status.xrow, status.gridsize); add_points_to_positively_sloped_segments(&status, status.y, &range); add_points_to_negatively_sloped_segments(&status, status.y, &range); add_points_to_ending_segments(&status, status.y); @@ -1585,9 +1460,6 @@ gfxpoly_t* gfxpoly_process(gfxpoly_t*poly1, gfxpoly_t*poly2, windrule_t*windrule stroke = stroke->next; } #endif - - //add_horizontals(p, &windrule_evenodd, context); // output is always even/odd - //add_horizontals(p, windrule, context); return p; }