switched several parts of the polygon processor to a more compact polygon representation
authorMatthias Kramm <kramm@matthias-kramms-macbook-pro.local>
Tue, 26 May 2009 21:19:46 +0000 (14:19 -0700)
committerMatthias Kramm <kramm@matthias-kramms-macbook-pro.local>
Tue, 26 May 2009 21:19:46 +0000 (14:19 -0700)
lib/gfxpoly/convert.c
lib/gfxpoly/convert.h
lib/gfxpoly/poly.c
lib/gfxpoly/poly.h
lib/gfxpoly/renderpoly.c
lib/gfxpoly/test.c

index ad884f1..717feba 100644 (file)
@@ -4,6 +4,7 @@
 #include "../gfxdevice.h"
 #include "../mem.h"
 #include "poly.h"
+#include "convert.h"
 
 /* factor that determines into how many line fragments a spline is converted */
 #define SUBFRACTION (2.4)
@@ -18,41 +19,29 @@ static edge_t*edge_new(int x1, int y1, int x2, int y2)
     return s;
 }
 
-static inline int32_t convert_coord(double x)
+static inline int32_t convert_coord(double x, double z)
 {
     /* we clamp to 31 bit instead of 32 bit because we use
        a (x1-x2) shortcut when comparing coordinates
     */
+    x *= z;
     if(x < -0x40000000) x = -0x40000000;
     if(x >  0x3fffffff) x =  0x3fffffff;
     return ceil(x);
 }
 
-static inline void gfxpoly_add_edge(gfxpoly_t*poly, double _x1, double _y1, double _x2, double _y2)
-{
-    int x1 = convert_coord(_x1);
-    int y1 = convert_coord(_y1);
-    int x2 = convert_coord(_x2);
-    int y2 = convert_coord(_y2);
-
-    if(x1!=x2 || y1!=y2) {
-        edge_t*s = edge_new(x1, y1, x2, y2);
-        s->next = poly->edges;
-        poly->edges = s;
-    }
-}
-
-static void convert_gfxline(gfxline_t*line, void*data, void(*moveto)(void*data, double x, double y), void(*lineto)(void*data, double x, double y), void(*setgridsize)(void*data, double g))
+static void convert_gfxline(gfxline_t*line, polywriter_t*w, double gridsize)
 {
     assert(!line || line[0].type == gfx_moveTo);
     double lastx=0,lasty=0;
+    double z = 1.0 / gridsize;
     while(line) {
         if(line->type == gfx_moveTo) {
            if(line->next && line->next->type != gfx_moveTo && (line->x!=lastx || line->y!=lasty)) {
-               moveto(data, line->x, line->y);
+               w->moveto(w, convert_coord(line->x,z), convert_coord(line->y,z));
            }
         } else if(line->type == gfx_lineTo) {
-            lineto(data, line->x, line->y);
+            w->lineto(w, convert_coord(line->x,z), convert_coord(line->y,z));
        } else if(line->type == gfx_splineTo) {
             int parts = (int)(sqrt(fabs(line->x-2*line->sx+lastx) + 
                                    fabs(line->y-2*line->sy+lasty))*SUBFRACTION);
@@ -63,9 +52,9 @@ static void convert_gfxline(gfxline_t*line, void*data, void(*moveto)(void*data,
                double t = (double)i*stepsize;
                double sx = (line->x*t*t + 2*line->sx*t*(1-t) + lastx*(1-t)*(1-t));
                double sy = (line->y*t*t + 2*line->sy*t*(1-t) + lasty*(1-t)*(1-t));
-               lineto(data, sx, sy);
+               w->lineto(w, convert_coord(sx,z), convert_coord(sy,z));
            }
-           lineto(data, line->x, line->y);
+           w->lineto(w, convert_coord(line->x,z), convert_coord(line->y,z));
         }
        lastx = line->x;
        lasty = line->y;
@@ -95,12 +84,13 @@ static char* readline(FILE*fi)
     }
 }
 
-static void convert_file(const char*filename, void*data, void(*moveto)(void*data, double x, double y), void(*lineto)(void*data, double x, double y),void(*setgridsize)(void*data, double gridsize))
+static void convert_file(const char*filename, polywriter_t*w, double gridsize)
 {
     FILE*fi = fopen(filename, "rb");
     if(!fi) {
         perror(filename);
     }
+    double z = 1.0 / gridsize;
     int count = 0;
     double g = 0;
     double lastx=0,lasty=0;
@@ -112,16 +102,18 @@ static void convert_file(const char*filename, void*data, void(*moveto)(void*data
         char s[256];
         if(sscanf(line, "%lf %lf %s", &x, &y, &s) == 3) {
             if(s && !strcmp(s,"moveto")) {
-               moveto(data, x, y);
+               w->moveto(w, convert_coord(x,z), convert_coord(y,z));
                 count++;
             } else if(s && !strcmp(s,"lineto")) {
-               lineto(data, x, y);
+               w->lineto(w, convert_coord(x,z), convert_coord(y,z));
                 count++;
             } else {
                 fprintf(stderr, "invalid command: %s\n", s);
             }
         } else if(sscanf(line, "%% gridsize %lf", &g) == 1) {
-           setgridsize(data, g);
+           gridsize = g;
+           z = 1.0 / gridsize;
+           w->setgridsize(w, g);
         }
         free(line);
     }
@@ -136,56 +128,60 @@ static void convert_file(const char*filename, void*data, void(*moveto)(void*data
 typedef struct _stdpoly {
     gfxpoly_t*poly;
     double lastx,lasty;
-    double z;
 } stdpoly_t;
-static void stdmoveto(void*data, double x, double y)
+
+static void stdmoveto(polywriter_t*w, int x, int y)
 {
-    stdpoly_t*d = (stdpoly_t*)data;
-    x *= d->z;
-    y *= d->z;
+    stdpoly_t*d = (stdpoly_t*)w->internal;
     d->lastx = x;d->lasty = y;
 }
-static void stdlineto(void*data, double x, double y)
+static void stdlineto(polywriter_t*w, int x, int y)
 {
-    stdpoly_t*d = (stdpoly_t*)data;
-    x *= d->z;
-    y *= d->z;
-    gfxpoly_add_edge(d->poly, d->lastx, d->lasty, x, y);
+    stdpoly_t*d = (stdpoly_t*)w->internal;
+    int x1 = d->lastx;
+    int y1 = d->lasty;
+    int x2 = x;
+    int y2 = y;
+    if(x1!=x2 || y1!=y2) {
+        edge_t*s = edge_new(x1, y1, x2, y2);
+        s->next = d->poly->edges;
+        d->poly->edges = s;
+    }
     d->lastx = x;d->lasty = y;
 }
-static void stdsetgridsize(void*data, double gridsize)
+static void stdsetgridsize(polywriter_t*w, double gridsize)
 {
-    stdpoly_t*d = (stdpoly_t*)data;
+    stdpoly_t*d = (stdpoly_t*)w->internal;
     d->poly->gridsize = gridsize;
 }
-gfxpoly_t* gfxpoly_from_gfxline(gfxline_t*line, double gridsize)
+static void* stdfinish(polywriter_t*w)
 {
-    stdpoly_t data;
-    data.poly = gfxpoly_new(gridsize);
-    data.z = 1.0 / gridsize;
-    data.lastx = data.lasty = 0;
-    convert_gfxline(line, &data, stdmoveto, stdlineto, stdsetgridsize);
-    return data.poly;
+    stdpoly_t*d = (stdpoly_t*)w->internal;
+    gfxpoly_t*poly =  d->poly;
+    free(w->internal);w->internal = 0;
+    return poly;
 }
-gfxpoly_t* gfxpoly_from_file(const char*filename, double gridsize)
+void gfxpolywriter_init(polywriter_t*w)
 {
-    stdpoly_t data;
-    data.poly = gfxpoly_new(gridsize);
-    data.z = 1.0 / gridsize;
-    data.lastx = data.lasty = 0;
-    convert_file(filename, &data, stdmoveto, stdlineto, stdsetgridsize);
-    return data.poly;
+    w->moveto = stdmoveto;
+    w->lineto = stdlineto;
+    w->setgridsize = stdsetgridsize;
+    w->finish = stdfinish;
+    stdpoly_t*data = w->internal = malloc(sizeof(stdpoly_t));
+    data->poly = gfxpoly_new(1.0);
+    data->lastx = 0;
+    data->lasty = 0;
 }
 
 typedef struct _compactpoly {
     gfxcompactpoly_t*poly;
     point_t last;
-    double z;
     int strokes_size;
     point_t*points;
     int num_points;
     int points_size;
     segment_dir_t dir;
+    char new;
 } compactpoly_t;
 
 void finish_segment(compactpoly_t*data)
@@ -195,13 +191,14 @@ void finish_segment(compactpoly_t*data)
     if(data->poly->num_strokes == data->strokes_size) {
        data->strokes_size <<= 1;
        assert(data->strokes_size > data->poly->num_strokes);
-       data->poly->strokes = rfx_realloc(data->poly->strokes, sizeof(gfxstroke_t)*data->strokes_size);
+       data->poly->strokes = rfx_realloc(data->poly->strokes, sizeof(gfxpolystroke_t)*data->strokes_size);
     }
     point_t*p = malloc(sizeof(point_t)*data->num_points);
-    gfxstroke_t*s = &data->poly->strokes[data->poly->num_strokes];
+    gfxpolystroke_t*s = &data->poly->strokes[data->poly->num_strokes];
     s->num_points = data->num_points;
     s->dir = data->dir;
     s->points = p;
+    assert(data->dir != DIR_UNKNOWN);
     if(data->dir == DIR_UP) {
        int t;
        int s = data->num_points;
@@ -211,79 +208,148 @@ void finish_segment(compactpoly_t*data)
     } else {
        memcpy(p, data->points, sizeof(point_t)*data->num_points);
     }
+#ifdef CHECKS
+    int t;
+    for(t=0;t<data->num_points-1;t++) {
+       assert(p[t].y<=p[t+1].y);
+    }
+#endif
     data->poly->num_strokes++;
 }
-static void compactmoveto(void*_data, double _x, double _y)
+static void compactmoveto(polywriter_t*w, int x, int y)
 {
-    compactpoly_t*data = (compactpoly_t*)_data;
-    data->dir = DIR_UNKNOWN;
+    compactpoly_t*data = (compactpoly_t*)w->internal;
     point_t p;
-    p.x = convert_coord(_x);
-    p.y = convert_coord(_y);
+    p.x = x;
+    p.y = y;
+    if(p.x != data->last.x || p.y != data->last.y) {
+       data->new = 1;
+    }
     data->last = p;
 }
-static void compactlineto(void*_data, double _x, double _y)
+static void compactlineto(polywriter_t*w, int x, int y)
 {
-    compactpoly_t*data = (compactpoly_t*)_data;
+    compactpoly_t*data = (compactpoly_t*)w->internal;
     point_t p;
-    p.x = convert_coord(_x);
-    p.y = convert_coord(_y);
-    if(p.y <  data->last.y && data->dir != DIR_UP ||
-       p.y >= data->last.y && data->dir != DIR_DOWN) {
+    p.x = x;
+    p.y = y;
+    if(p.x == data->last.x && p.y == data->last.y)
+       return;
+
+    if(p.y < data->last.y && data->dir != DIR_UP ||
+       p.y > data->last.y && data->dir != DIR_DOWN || 
+       data->new) {
        finish_segment(data);
        data->dir = p.y > data->last.y ? DIR_DOWN : DIR_UP;
        data->points[0] = data->last;
        data->num_points = 1;
     }
+
     if(data->points_size == data->num_points) {
        data->points_size <<= 1;
        assert(data->points_size > data->num_points);
        data->points = rfx_realloc(data->points, sizeof(point_t)*data->points_size);
     }
     data->points[data->num_points++] = p;
+    data->last = p;
 }
-static void compactsetgridsize(void*data, double gridsize)
+static void compactsetgridsize(polywriter_t*w, double gridsize)
 {
-    compactpoly_t*d = (compactpoly_t*)data;
+    compactpoly_t*d = (compactpoly_t*)w->internal;
     d->poly->gridsize = gridsize;
 }
-static void compactinit(compactpoly_t*data, double gridsize)
+/*static int compare_stroke(const void*_s1, const void*_s2)
+{
+    gfxpolystroke_t*s1 = (gfxpolystroke_t*)_s1;
+    gfxpolystroke_t*s2 = (gfxpolystroke_t*)_s2;
+    return s1->points[0].y - s2->points[0].y;
+}*/
+static void*compactfinish(polywriter_t*w)
+{
+    compactpoly_t*data = (compactpoly_t*)w->internal;
+    finish_segment(data);
+    data->poly->strokes = (gfxpolystroke_t*)rfx_realloc(data->poly->strokes, sizeof(gfxpolystroke_t)*data->poly->num_strokes);
+    //qsort(data->poly->strokes, data->poly->num_strokes, sizeof(gfxpolystroke_t), compare_stroke);
+    free(data->points);
+    gfxcompactpoly_t*poly = data->poly;
+    free(w->internal);w->internal = 0;
+    return (void*)poly;
+}
+void gfxcompactpolywriter_init(polywriter_t*w)
 {
+    w->moveto = compactmoveto;
+    w->lineto = compactlineto;
+    w->setgridsize = compactsetgridsize;
+    w->finish = compactfinish;
+    compactpoly_t*data = w->internal = rfx_calloc(sizeof(compactpoly_t));
     data->poly = rfx_calloc(sizeof(gfxcompactpoly_t));
-    data->poly->gridsize = gridsize;
-    data->z = 1.0 / gridsize;
+    data->poly->gridsize = 1.0;
     data->last.x = data->last.y = 0;
     data->strokes_size = 16;
     data->num_points = 0;
     data->points_size = 16;
+    data->new = 1;
     data->dir = DIR_UNKNOWN;
     data->points = (point_t*)rfx_alloc(sizeof(point_t)*data->points_size);
-    data->poly->strokes = (gfxstroke_t*)rfx_alloc(sizeof(gfxstroke_t)*data->strokes_size);
+    data->poly->strokes = (gfxpolystroke_t*)rfx_alloc(sizeof(gfxpolystroke_t)*data->strokes_size);
 }
-static gfxcompactpoly_t*compactfinish(compactpoly_t*data)
+
+gfxpoly_t* gfxpoly_from_gfxline(gfxline_t*line, double gridsize)
 {
-    finish_segment(data);
-    data->poly->strokes = (gfxstroke_t*)rfx_realloc(data->poly->strokes, sizeof(gfxstroke_t)*data->poly->num_strokes);
-    free(data->points);
-    return data->poly;
+    polywriter_t w;
+    gfxpolywriter_init(&w);
+    w.setgridsize(&w, gridsize);
+    convert_gfxline(line, &w, gridsize);
+    return w.finish(&w);
+}
+gfxpoly_t* gfxpoly_from_file(const char*filename, double gridsize)
+{
+    polywriter_t w;
+    gfxpolywriter_init(&w);
+    w.setgridsize(&w, gridsize);
+    convert_file(filename, &w, gridsize);
+    return w.finish(&w);
 }
 gfxcompactpoly_t* gfxcompactpoly_from_gfxline(gfxline_t*line, double gridsize)
 {
-    compactpoly_t data;
-    compactinit(&data, gridsize);
-    data.poly->gridsize = gridsize;
-    convert_gfxline(line, &data, compactmoveto, compactlineto, compactsetgridsize);
-    return compactfinish(&data);
+    polywriter_t writer;
+    gfxcompactpolywriter_init(&writer);
+    writer.setgridsize(&writer, gridsize);
+    convert_gfxline(line, &writer, gridsize);
+    return (gfxcompactpoly_t*)writer.finish(&writer);
 }
 gfxcompactpoly_t* gfxcompactpoly_from_file(const char*filename, double gridsize)
 {
-    compactpoly_t data;
-    compactinit(&data, gridsize);
-    data.poly->gridsize = gridsize;
-    convert_file(filename, &data, compactmoveto, compactlineto, compactsetgridsize);
-    return compactfinish(&data);
+    polywriter_t writer;
+    gfxcompactpolywriter_init(&writer);
+    writer.setgridsize(&writer, gridsize);
+    convert_file(filename, &writer, gridsize);
+    return (gfxcompactpoly_t*)writer.finish(&writer);
+}
+gfxpoly_t*gfxpoly_from_gfxcompactpoly(gfxcompactpoly_t*poly)
+{
+    int s,t;
+    int pass;
+    gfxpoly_t*poly2 = gfxpoly_new(poly->gridsize);
+    for(t=0;t<poly->num_strokes;t++) {
+       gfxpolystroke_t*stroke = &poly->strokes[t];
+       for(s=0;s<stroke->num_points-1;s++) {
+           point_t a = stroke->points[s];
+           point_t b = stroke->points[s+1];
+           edge_t*e = 0;
+           if(stroke->dir == DIR_UP) {
+               e = edge_new(a.x,a.y,b.x,b.y);
+           } else {
+               e = edge_new(b.x,b.y,a.x,a.y);
+           }
+           e->style = stroke->fs;
+           e->next = poly2->edges;
+           poly2->edges = e;
+       }
+    }
+    return poly2;
 }
-void gfxcompactpoly_free(gfxcompactpoly_t*poly)
+void gfxcompactpoly_destroy(gfxcompactpoly_t*poly)
 {
     int t;
     for(t=0;t<poly->num_strokes;t++) {
index 8ac8a86..e5b3545 100644 (file)
@@ -1,13 +1,28 @@
 #ifndef __poly_convert_h__
 #define __poly_convert_h__
 
+#include "../gfxdevice.h"
 #include "poly.h"
 
 gfxpoly_t* gfxpoly_from_gfxline(gfxline_t*line, double gridsize);
 gfxpoly_t* gfxpoly_from_file(const char*filename, double gridsize);
+gfxpoly_t* gfxpoly_from_gfxcompactpoly(gfxcompactpoly_t*poly);
+
+typedef struct _polywriter
+{
+    void(*moveto)(struct _polywriter*, int x, int y);
+    void(*lineto)(struct _polywriter*, int x, int y);
+    void(*setgridsize)(struct _polywriter*, double g);
+    void*(*finish)(struct _polywriter*);
+    void*internal;
+} polywriter_t;
+
+void gfxpolywriter_init(polywriter_t*w);
+void gfxcompactpolywriter_init(polywriter_t*w);
+
 gfxcompactpoly_t* gfxcompactpoly_from_gfxline(gfxline_t*line, double gridsize);
 gfxcompactpoly_t* gfxcompactpoly_from_file(const char*filename, double gridsize);
 
-void gfxcompactpoly_free(gfxcompactpoly_t*poly);
+void gfxcompactpoly_destroy(gfxcompactpoly_t*poly);
 
 #endif //__poly_convert_h__
index ca81be1..9c45bdf 100644 (file)
@@ -9,8 +9,9 @@
 #include "active.h"
 #include "xrow.h"
 #include "wind.h"
+#include "convert.h"
 
-static gfxpoly_t*current_polygon = 0;
+static gfxcompactpoly_t*current_polygon = 0;
 void gfxpoly_fail(char*expr, char*file, int line, const char*function)
 {
     if(!current_polygon) {
@@ -19,14 +20,14 @@ void gfxpoly_fail(char*expr, char*file, int line, const char*function)
     }
 
     void*md5 = init_md5();
-    
-    edge_t* s = current_polygon->edges;
-    while(s) {
-       update_md5(md5, (unsigned char*)&s->a.x, sizeof(s->a.x));
-       update_md5(md5, (unsigned char*)&s->a.y, sizeof(s->a.y));
-       update_md5(md5, (unsigned char*)&s->b.x, sizeof(s->b.x));
-       update_md5(md5, (unsigned char*)&s->b.y, sizeof(s->b.y));
-       s = s->next;
+   
+    int s,t;
+    for(s=0;s<current_polygon->num_strokes;s++) {
+       gfxpolystroke_t*stroke = &current_polygon->strokes[s];
+       for(t=0;t<stroke->num_points;t++) {
+           update_md5(md5, (unsigned char*)&stroke->points[t].x, sizeof(stroke->points[t].x));
+           update_md5(md5, (unsigned char*)&stroke->points[t].y, sizeof(stroke->points[t].y));
+       }
     }
     unsigned char h[16];
     char filename[32+4+1];
@@ -37,7 +38,7 @@ void gfxpoly_fail(char*expr, char*file, int line, const char*function)
     fprintf(stderr, "assert(%s) failed in %s in line %d: %s\n", expr, file, line, function);
     fprintf(stderr, "I'm saving a debug file \"%s\" to the current directory.\n", filename);
 
-    gfxpoly_save(current_polygon, filename);
+    gfxcompactpoly_save(current_polygon, filename);
     exit(1);
 }
 
@@ -78,11 +79,11 @@ typedef struct _status {
     int32_t y;
     actlist_t*actlist;
     heap_t*queue;
-    edge_t*output;
     xrow_t*xrow;
     windrule_t*windrule;
     windcontext_t*context;
     segment_t*ending_segments;
+    polywriter_t writer;
 #ifdef CHECKS
     dict_t*seen_crossings; //list of crossing we saw so far
     dict_t*intersecting_segs; //list of segments intersecting in this scanline
@@ -90,6 +91,13 @@ typedef struct _status {
 #endif
 } status_t;
 
+typedef struct _event {
+    eventtype_t type;
+    point_t p;
+    segment_t*s1;
+    segment_t*s2;
+} event_t;
+
 /* compare_events_simple differs from compare_events in that it schedules
    events from left to right regardless of type. It's only used in horizontal
    processing, in order to get an x-wise sorting of the current scanline */
@@ -148,12 +156,23 @@ void gfxpoly_destroy(gfxpoly_t*poly)
 }
 int gfxpoly_size(gfxpoly_t*poly)
 {
-    edge_t* s = poly->edges;
-    int t=0;
-    while(s) {
-        s = s->next;t++;
+    edge_t*e = poly->edges;
+    int count = 0;
+    while(e) {
+       count++;
+       e = e->next;
     }
-    return t;
+    return count;
+}
+int gfxcompactpoly_size(gfxcompactpoly_t*poly)
+{
+    int s,t;
+    int edges = 0;
+    for(t=0;t<poly->num_strokes;t++) {
+       gfxpolystroke_t*stroke = &poly->strokes[t];
+       edges += stroke->num_points-1;
+    }
+    return edges;
 }
 
 char gfxpoly_check(gfxpoly_t*poly)
@@ -191,6 +210,37 @@ char gfxpoly_check(gfxpoly_t*poly)
     return 1;
 }
 
+char gfxcompactpoly_check(gfxcompactpoly_t*poly)
+{
+    dict_t*d = dict_new2(&point_type);
+    int s,t;
+    for(t=0;t<poly->num_strokes;t++) {
+       gfxpolystroke_t*stroke = &poly->strokes[t];
+       for(s=0;s<stroke->num_points;s++) {
+           point_t p = stroke->points[s];
+           int num = (s>=1 && s<stroke->num_points-1)?2:1; // mid points are two points (start+end)
+           if(!dict_contains(d, &p)) {
+               dict_put(d, &p, (void*)(ptroff_t)num);
+           } else {
+               int count = (ptroff_t)dict_lookup(d, &p);
+               dict_del(d, &p);
+               count+=num;
+               dict_put(d, &p, (void*)(ptroff_t)count);
+           }
+       }
+    }
+    DICT_ITERATE_ITEMS(d, point_t*, p, void*, c) {
+        int count = (ptroff_t)c;
+        if(count&1) {
+            fprintf(stderr, "Point (%f,%f) occurs %d times\n", p->x*poly->gridsize, p->y*poly->gridsize, count);
+            dict_destroy(d);
+            return 0;
+        }
+    }
+    dict_destroy(d);
+    return 1;
+}
+
 void gfxpoly_dump(gfxpoly_t*poly)
 {
     edge_t* s = poly->edges;
@@ -202,18 +252,38 @@ void gfxpoly_dump(gfxpoly_t*poly)
     }
 }
 
-gfxpoly_t* gfxpoly_save(gfxpoly_t*poly, const char*filename)
+void gfxcompactpoly_dump(gfxcompactpoly_t*poly)
+{
+    int s,t;
+    double g = poly->gridsize;
+    fprintf(stderr, "polyon %08x (gridsize: %f)\n", poly, poly->gridsize);
+    for(t=0;t<poly->num_strokes;t++) {
+       gfxpolystroke_t*stroke = &poly->strokes[t];
+       for(s=0;s<stroke->num_points-1;s++) {
+           point_t a = stroke->points[s];
+           point_t b = stroke->points[s+1];
+           fprintf(stderr, "%s(%f,%f) -> (%f,%f)%s\n", s?" ":"[", a.x*g, a.y*g, b.x*g, b.y*g,
+                                                       s==stroke->num_points-2?"]":"");
+       }
+    }
+}
+
+void gfxcompactpoly_save(gfxcompactpoly_t*poly, const char*filename)
 {
     FILE*fi = fopen(filename, "wb");
     fprintf(fi, "%% gridsize %f\n", poly->gridsize);
     fprintf(fi, "%% begin\n");
-    edge_t* s = poly->edges;
-    while(s) {
-        fprintf(fi, "%g setgray\n", s->b.y < s->a.y ? 0.7 : 0);
-        fprintf(fi, "%d %d moveto\n", s->a.x, s->a.y);
-        fprintf(fi, "%d %d lineto\n", s->b.x, s->b.y);
-        fprintf(fi, "stroke\n");
-        s = s->next;
+    int s,t;
+    for(t=0;t<poly->num_strokes;t++) {
+       gfxpolystroke_t*stroke = &poly->strokes[t];
+       for(s=0;s<stroke->num_points-1;s++) {
+           point_t a = stroke->points[s];
+           point_t b = stroke->points[s+1];
+           fprintf(fi, "%g setgray\n", stroke->dir==DIR_UP ? 0.7 : 0);
+           fprintf(fi, "%d %d moveto\n", a.x, a.y);
+           fprintf(fi, "%d %d lineto\n", b.x, b.y);
+           fprintf(fi, "stroke\n");
+       }
     }
     fprintf(fi, "showpage\n");
     fclose(fi);
@@ -236,8 +306,6 @@ static void event_dump(event_t*e)
         fprintf(stderr, "event: segment [%d] ends at (%d,%d)\n", e->s1->nr, e->p.x, e->p.y);
     } else if(e->type == EVENT_CROSS) {
         fprintf(stderr, "event: segment [%d] and [%d] intersect at (%d,%d)\n", e->s1->nr, e->s2->nr, e->p.x, e->p.y);
-    } else if(e->type == EVENT_CORNER) {
-        fprintf(stderr, "event: segment [%d] ends, segment [%d] starts, at (%d,%d)\n", e->s1->nr, e->s2->nr, e->p.x, e->p.y);
     } else {
         assert(0);
     }
@@ -253,18 +321,17 @@ static void segment_dump(segment_t*s)
             (double)s->delta.x / s->delta.y);
 }
 
-static void segment_init(segment_t*s, int32_t x1, int32_t y1, int32_t x2, int32_t y2, int polygon_nr)
+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)
 {
-    if(y1<y2) {
-        s->dir = DIR_DOWN;
-    } else if(y1>y2) {
-        int32_t x = x1;x1=x2;x2=x;
-        int32_t y = y1;y1=y2;y2=y;
-        s->dir = DIR_UP;
+    s->dir = dir;
+    if(y1!=y2) {
+       assert(y1<y2);
     } else {
         /* up/down for horizontal segments is handled by "rotating"
            them 90° anticlockwise in screen coordinates (tilt your head to
-           the right) */
+           the right) 
+           TODO: is this still needed?
+        */
         s->dir = DIR_UP;
         if(x1>x2) {
             s->dir = DIR_DOWN;
@@ -285,11 +352,8 @@ 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;
-#define XDEBUG
-#ifdef XDEBUG
     static int segment_count=0;
     s->nr = segment_count++;
-#endif
 
 #ifdef CHECKS
     assert(LINE_EQ(s->a, s) == 0);
@@ -309,45 +373,67 @@ static void segment_init(segment_t*s, int32_t x1, int32_t y1, int32_t x2, int32_
     assert(LINE_EQ(p, s) >= 0);
 #endif
 
+    /* TODO: make this int_type */
     dict_init2(&s->scheduled_crossings, &ptr_type, 0);
 }
 
-static segment_t* segment_new(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int polygon_nr)
+static segment_t* segment_new(point_t a, point_t b, int polygon_nr, segment_dir_t dir)
 {
     segment_t*s = (segment_t*)rfx_calloc(sizeof(segment_t));
-    segment_init(s, x1, y1, x2, y2, polygon_nr);
+    segment_init(s, a.x, a.y, b.x, b.y, polygon_nr, dir);
     return s;
 }
 
-static void segment_destroy(segment_t*s)
+static void segment_clear(segment_t*s)
 {
     dict_clear(&s->scheduled_crossings);
+}
+static void segment_destroy(segment_t*s)
+{
+    segment_clear(s);
     free(s);
 }
 
-static void gfxpoly_enqueue(edge_t*list, heap_t*queue, int polygon_nr)
+static void advance_stroke(heap_t*queue, gfxpolystroke_t*stroke, int polygon_nr, int pos)
 {
-    edge_t*l;
-    for(l=list;l;l=l->next) {
-        if(l->a.x == l->b.x &&
-           l->a.y == l->b.y) {
-            fprintf(stderr, "Warning: intersector input contains zero-length segments\n");
-            continue;
-        }
-        segment_t*s = segment_new(l->a.x, l->a.y, l->b.x, l->b.y, polygon_nr);
+    while(pos < stroke->num_points-1) {
+       assert(stroke->points[pos].y <= stroke->points[pos+1].y);
+       segment_t*s = segment_new(stroke->points[pos], stroke->points[pos+1], polygon_nr, stroke->dir);
+       s->stroke = stroke;
+       s->stroke_pos = ++pos;
 #ifdef DEBUG
-        if(l->tmp)
-            s->nr = l->tmp;
-        fprintf(stderr, "[%d] (%d,%d) -> (%d,%d) %s\n",
-                s->nr, s->a.x, s->a.y, s->b.x, s->b.y,
-                s->dir==DIR_UP?"up":"down");
+       /*if(l->tmp)
+           s->nr = l->tmp;*/
+       fprintf(stderr, "[%d] (%d,%d) -> (%d,%d) %s (%d more to come)\n",
+               s->nr, s->a.x, s->a.y, s->b.x, s->b.y,
+               s->dir==DIR_UP?"up":"down", stroke->num_points - 1 - pos);
 #endif
-        event_t e = event_new();
-        e.type = s->delta.y ? EVENT_START : EVENT_HORIZONTAL;
-        e.p = s->a;
-        e.s1 = s;
-        e.s2 = 0;
-        heap_put(queue, &e);
+       event_t e = event_new();
+       e.type = s->delta.y ? EVENT_START : EVENT_HORIZONTAL;
+       e.p = s->a;
+       e.s1 = s;
+       e.s2 = 0;
+       heap_put(queue, &e);
+       if(e.type != EVENT_HORIZONTAL) {
+           break;
+       }
+    }
+}
+
+static void gfxpoly_enqueue(gfxcompactpoly_t*p, heap_t*queue, int polygon_nr)
+{
+    int t;
+    for(t=0;t<p->num_strokes;t++) {
+       gfxpolystroke_t*stroke = &p->strokes[t];
+       assert(stroke->num_points > 1);
+
+#ifdef CHECKS
+       int s;
+       for(s=0;s<stroke->num_points-1;s++) {
+           assert(stroke->points[s].y <= stroke->points[s+1].y);
+       }
+#endif
+       advance_stroke(queue, stroke, polygon_nr, 0);
     }
 }
 
@@ -392,13 +478,21 @@ static void schedule_crossing(status_t*status, segment_t*s1, segment_t*s2)
 #endif
 
     if(s1->maxx <= s2->minx) {
+#ifdef DEBUG
+            fprintf(stderr, "[%d] doesn't intersect with [%d] because: bounding boxes don't intersect\n", s1->nr, s2->nr);
+#endif
         /* bounding boxes don't intersect */
         return;
     }
 
-    if(dict_contains(&s1->scheduled_crossings, s2)) {
+    if(dict_contains(&s1->scheduled_crossings, (void*)(ptroff_t)s2->nr)) {
         /* FIXME: this whole segment hashing thing is really slow */
-        //fprintf(stderr, "Encountered crossing between [%d] and [%d] twice\n", s1->nr, s2->nr);
+#ifdef DEBUG
+        fprintf(stderr, "[%d] doesn't intersect with [%d] because: we already scheduled this intersection\n", s1->nr, s2->nr);
+//     DICT_ITERATE_KEY(&s1->scheduled_crossings, void*, x) {
+//         fprintf(stderr, "[%d]<->[%d]\n", s1->nr, (int)(ptroff_t)x);
+//     }
+#endif
         return; // we already know about this one
     }
 
@@ -411,6 +505,9 @@ static void schedule_crossing(status_t*status, segment_t*s1, segment_t*s2)
 #endif
             return;
         } else {
+#ifdef DEBUG
+            fprintf(stderr, "[%d] doesn't intersect with [%d] because: they are parallel to each other\n", s1->nr, s2->nr);
+#endif
             /* lines are parallel */
             return;
         }
@@ -419,9 +516,16 @@ static void schedule_crossing(status_t*status, segment_t*s1, segment_t*s2)
     double bsign2 = LINE_EQ(s1->b, s2);
     if(asign2<0 && bsign2<0) {
         // segment1 is completely to the left of segment2
+#ifdef DEBUG
+            fprintf(stderr, "[%d] doesn't intersect with [%d] because: [%d] is completely to the left of [%d]\n", s1->nr, s2->nr, s1->nr, s2->nr);
+#endif
         return;
     }
     if(asign2>0 && bsign2>0)  {
+       // TODO: can this ever happen?
+#ifdef DEBUG
+            fprintf(stderr, "[%d] doesn't intersect with [%d] because: [%d] is completely to the left of [%d]\n", s1->nr, s2->nr, s2->nr, s1->nr);
+#endif
         // segment2 is completely to the left of segment1
         return;
     }
@@ -443,10 +547,16 @@ static void schedule_crossing(status_t*status, segment_t*s1, segment_t*s2)
     double bsign1 = LINE_EQ(s2->b, s1);
     if(asign1<0 && bsign1<0) {
         // segment1 is completely to the left of segment2
+#ifdef DEBUG
+            fprintf(stderr, "[%d] doesn't intersect with [%d] because: [%d] is completely to the left of [%d]\n", s1->nr, s2->nr, s1->nr, s2->nr);
+#endif
         return;
     }
     if(asign1>0 && bsign1>0)  {
         // segment2 is completely to the left of segment1
+#ifdef DEBUG
+            fprintf(stderr, "[%d] doesn't intersect with [%d] because: [%d] is completely to the left of [%d]\n", s1->nr, s2->nr, s2->nr, s1->nr);
+#endif
         return;
     }
     if(asign1==0) {
@@ -489,8 +599,8 @@ static void schedule_crossing(status_t*status, segment_t*s1, segment_t*s2)
 
     /* we insert into each other's intersection history because these segments might switch
        places and we still want to look them up quickly after they did */
-    dict_put(&s1->scheduled_crossings, s2, 0);
-    dict_put(&s2->scheduled_crossings, s1, 0);
+    dict_put(&s1->scheduled_crossings, (void*)(ptroff_t)(s2->nr), 0);
+    dict_put(&s2->scheduled_crossings, (void*)(ptroff_t)(s1->nr), 0);
 
     event_t e = event_new();
     e.type = EVENT_CROSS;
@@ -556,15 +666,11 @@ static void insert_point_into_segment(status_t*status, segment_t*s, point_t p)
 #endif
         // omit horizontal lines
         if(s->pos.y != p.y) {
-            edge_t*e = rfx_calloc(sizeof(edge_t));
-#ifdef DEBUG
-            e->tmp = s->nr;
-#endif
-            e->a = s->pos;
-            e->b = p;
-            assert(e->a.y != e->b.y);
-            e->next = status->output;
-            status->output = e;
+            point_t a = s->pos;
+            point_t b = p;
+            assert(a.y != b.y);
+           status->writer.moveto(&status->writer, a.x, a.y);
+           status->writer.lineto(&status->writer, b.x, b.y);
         }
     } else {
 #ifdef DEBUG
@@ -819,16 +925,16 @@ static void recalculate_windings(status_t*status, segrange_t*range)
             fillstyle_t*fs_old = s->fs_out;
             s->fs_out = status->windrule->diff(&wind, &s->wind);
 
+#ifdef DEBUG
+            fprintf(stderr, "[%d] %s/%d/%s/%s %s\n", s->nr, s->dir==DIR_UP?"up":"down", s->wind.wind_nr, s->wind.is_filled?"fill":"nofill", s->fs_out?"draw":"omit",
+                   fs_old!=s->fs_out?"CHANGED":"");
+#endif
             assert(!(!s->changed && fs_old!=s->fs_out));
             s->changed = 0;
 
 #ifdef CHECKS
             s->fs_out_ok = 1;
 #endif
-#ifdef DEBUG
-            fprintf(stderr, "[%d] %s/%d/%s/%s %s\n", s->nr, s->dir==DIR_UP?"up":"down", s->wind.wind_nr, s->wind.is_filled?"fill":"nofill", s->fs_out?"draw":"omit",
-                   fs_old!=s->fs_out?"CHANGED":"");
-#endif
         }
         s = s->right;
     }
@@ -878,11 +984,13 @@ static void event_apply(status_t*status, event_t*e)
 {
     switch(e->type) {
         case EVENT_HORIZONTAL: {
+            segment_t*s = e->s1;
 #ifdef DEBUG
             event_dump(e);
 #endif
-            intersect_with_horizontal(status, e->s1);
-            segment_destroy(e->s1);e->s1=0;
+            intersect_with_horizontal(status, s);
+           advance_stroke(status->queue, s->stroke, s->polygon_nr, s->stroke_pos);
+            segment_destroy(s);e->s1=0;
             break;
         }
         case EVENT_END: {
@@ -906,6 +1014,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, s->stroke, s->polygon_nr, s->stroke_pos);
             break;
         }
         case EVENT_START: {
@@ -922,7 +1031,7 @@ static void event_apply(status_t*status, event_t*e)
                 schedule_crossing(status, left, s);
             if(right)
                 schedule_crossing(status, s, right);
-            schedule_endpoint(status, e->s1);
+            schedule_endpoint(status, s);
             break;
         }
         case EVENT_CROSS: {
@@ -940,8 +1049,8 @@ static void event_apply(status_t*status, event_t*e)
 #endif
                 /* ignore this crossing for now (there are some line segments in between).
                    it'll get rescheduled as soon as the "obstacles" are gone */
-                char del1 = dict_del(&e->s1->scheduled_crossings, e->s2);
-                char del2 = dict_del(&e->s2->scheduled_crossings, e->s1);
+                char del1 = dict_del(&e->s1->scheduled_crossings, (void*)(ptroff_t)e->s2->nr);
+                char del2 = dict_del(&e->s2->scheduled_crossings, (void*)(ptroff_t)e->s1->nr);
                 assert(del1 && del2);
 #ifdef CHECKS
                 point_t pair;
@@ -972,7 +1081,7 @@ static void check_status(status_t*status)
 }
 #endif
 
-static void add_horizontals(gfxpoly_t*poly, windrule_t*windrule, windcontext_t*context)
+static void add_horizontals(gfxcompactpoly_t*poly, windrule_t*windrule, windcontext_t*context)
 {
     /*
           |..|        |...........|                 |           |
@@ -986,11 +1095,14 @@ static void add_horizontals(gfxpoly_t*poly, windrule_t*windrule, windcontext_t*c
     fprintf(stderr, "========================================================================\n");
 #endif
     heap_t* queue = heap_new(sizeof(event_t), compare_events_simple);
-    gfxpoly_enqueue(poly->edges, queue, 0);
+    gfxpoly_enqueue(poly, queue, 0);
 
     actlist_t* actlist = actlist_new();
 
     event_t*e = heap_chopmax(queue);
+    int newstrokes_size = 4;
+    int num_newstrokes = 0;
+    gfxpolystroke_t*newstrokes = malloc(sizeof(gfxpolystroke_t)*newstrokes_size);
     while(e) {
         int32_t y = e->p.y;
         int32_t x = 0;
@@ -1008,20 +1120,30 @@ static void add_horizontals(gfxpoly_t*poly, windrule_t*windrule, windcontext_t*c
                 fprintf(stderr, "%d) draw horizontal line from %d to %d\n", y, x, e->p.x);
 #endif
                assert(x<e->p.x);
-                edge_t*l= malloc(sizeof(edge_t));
-                l->a.y = l->b.y = y;
+
+               if(num_newstrokes == newstrokes_size) {
+                   newstrokes_size = (newstrokes_size)<<1;
+                   newstrokes = rfx_realloc(newstrokes, sizeof(gfxpolystroke_t)*newstrokes_size);
+               }
+                gfxpolystroke_t*stroke = &newstrokes[num_newstrokes++];
+               stroke->num_points = 2;
+               stroke->points = malloc(sizeof(point_t)*2);
+               stroke->dir = DIR_UP; // FIXME
+               stroke->fs = 0;
+               point_t a,b;
+                a.y = b.y = y;
                /* we draw from low x to high x so that left/right fillstyles add up
                    (because the horizontal line's fill style controls the area *below* the line)
                 */
-                l->a.x = e->p.x;
-                l->b.x = x;
-                l->next = poly->edges;
-                poly->edges = l;
+                a.x = e->p.x;
+                b.x = 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 hacking through any segments in the active list */
-               segment_t* start = actlist_find(actlist, l->b, l->b);
-               segment_t* s = actlist_find(actlist, l->a, l->a);
+               segment_t* start = actlist_find(actlist, b, b);
+               segment_t* s = actlist_find(actlist, a, a);
                while(s!=start) {
                    assert(s->a.y == y || s->b.y == y);
                    s = s->left;
@@ -1047,6 +1169,7 @@ static void add_horizontals(gfxpoly_t*poly, windrule_t*windrule, windcontext_t*c
                 case EVENT_END: {
                     left = actlist_left(actlist, s);
                     actlist_delete(actlist, s);
+                   advance_stroke(queue, s->stroke, s->polygon_nr, s->stroke_pos);
                     break;
                 }
                 default: assert(0);
@@ -1071,16 +1194,22 @@ static void add_horizontals(gfxpoly_t*poly, windrule_t*windrule, windcontext_t*c
 
         assert(!fill); // check that polygon is not bleeding
     }
+
+    poly->strokes = rfx_realloc(poly->strokes, sizeof(gfxpolystroke_t)*(num_newstrokes+poly->num_strokes));
+    memcpy(&poly->strokes[poly->num_strokes], newstrokes, sizeof(gfxpolystroke_t)*num_newstrokes);
+    poly->num_strokes += num_newstrokes;
+    free(newstrokes);
+
     actlist_destroy(actlist);
     heap_destroy(queue);
 }
 
-gfxpoly_t* gfxpoly_process(gfxpoly_t*poly, windrule_t*windrule, windcontext_t*context)
+gfxpoly_t* gfxpoly_process(gfxcompactpoly_t*poly, windrule_t*windrule, windcontext_t*context)
 {
     current_polygon = poly;
     heap_t* queue = heap_new(sizeof(event_t), compare_events);
 
-    gfxpoly_enqueue(poly->edges, queue, /*polygon nr*/0);
+    gfxpoly_enqueue(poly, queue, /*polygon nr*/0);
 
     status_t status;
     memset(&status, 0, sizeof(status_t));
@@ -1088,8 +1217,12 @@ gfxpoly_t* gfxpoly_process(gfxpoly_t*poly, windrule_t*windrule, windcontext_t*co
     status.windrule = windrule;
     status.context = context;
     status.actlist = actlist_new();
+    gfxcompactpolywriter_init(&status.writer);
+    status.writer.setgridsize(&status.writer, poly->gridsize);
+
 #ifdef CHECKS
     status.seen_crossings = dict_new2(&point_type);
+    int lasty=heap_peek(queue)?((event_t*)heap_peek(queue))->p.y-1:0;
 #endif
 
     status.xrow = xrow_new();
@@ -1097,6 +1230,7 @@ gfxpoly_t* gfxpoly_process(gfxpoly_t*poly, windrule_t*windrule, windcontext_t*co
     event_t*e = heap_chopmax(queue);
     while(e) {
         status.y = e->p.y;
+       assert(status.y>=lasty);
 #ifdef CHECKS
         status.intersecting_segs = dict_new2(&ptr_type);
         status.segs_with_point = dict_new2(&ptr_type);
@@ -1141,9 +1275,7 @@ gfxpoly_t* gfxpoly_process(gfxpoly_t*poly, windrule_t*windrule, windcontext_t*co
     heap_destroy(queue);
     xrow_destroy(status.xrow);
 
-    gfxpoly_t*p = gfxpoly_new(poly->gridsize);
-    p->edges = status.output;
-
+    gfxcompactpoly_t*p = (gfxcompactpoly_t*)status.writer.finish(&status.writer);
     add_horizontals(p, &windrule_evenodd, context); // output is always even/odd
-    return p;
+    return gfxpoly_from_gfxcompactpoly(p);
 }
index ff623d3..7ef125c 100644 (file)
@@ -3,13 +3,14 @@
 
 #include <stdint.h>
 #include "../q.h"
+#include "../types.h"
 
 //#define DEBUG
 #define CHECKS
 #define SPLAY
 
 typedef enum {DIR_UP, DIR_DOWN, DIR_UNKNOWN} segment_dir_t;
-typedef enum {EVENT_CROSS, EVENT_END, EVENT_CORNER, EVENT_START, EVENT_HORIZONTAL} eventtype_t;
+typedef enum {EVENT_CROSS, EVENT_END, EVENT_START, EVENT_HORIZONTAL} eventtype_t;
 typedef enum {SLOPE_POSITIVE, SLOPE_NEGATIVE} slope_t;
 
 typedef struct _point {
@@ -52,6 +53,18 @@ typedef struct _windrule
 
 #define SEGNR(s) ((s)?(s)->nr:-1)
 
+typedef struct _gfxpolystroke {
+    segment_dir_t dir;
+    int num_points;
+    point_t*points;
+    fillstyle_t*fs;
+} gfxpolystroke_t;
+typedef struct _gfxcompactpoly {
+    double gridsize;
+    int num_strokes;
+    gfxpolystroke_t*strokes;
+} gfxcompactpoly_t;
+
 typedef struct _segment {
     point_t a;
     point_t b;
@@ -68,7 +81,7 @@ typedef struct _segment {
     
     int polygon_nr;
     windstate_t wind;
-    int nr;
+    ptroff_t nr;
 
 #ifdef SPLAY
     struct _segment*parent;
@@ -81,6 +94,9 @@ typedef struct _segment {
 
     point_t pos;
 
+    gfxpolystroke_t*stroke;
+    int stroke_pos;
+
     dict_t scheduled_crossings;
 } segment_t;
 
@@ -105,33 +121,14 @@ typedef struct _gfxpoly {
     edge_t*edges;
 } gfxpoly_t;
 
-typedef struct _gfxstroke {
-    segment_dir_t dir;
-    int num_points;
-    point_t*points;
-    fillstyle_t*fs;
-} gfxstroke_t;
-typedef struct _gfxcompactpoly {
-    double gridsize;
-    int num_strokes;
-    gfxstroke_t*strokes;
-} gfxcompactpoly_t;
-
 void gfxpoly_fail(char*expr, char*file, int line, const char*function);
 
 gfxpoly_t* gfxpoly_new(double gridsize);
-char gfxpoly_check(gfxpoly_t*poly);
-int gfxpoly_size(gfxpoly_t*poly);
-void gfxpoly_dump(gfxpoly_t*poly);
-gfxpoly_t* gfxpoly_save(gfxpoly_t*poly, const char*filename);
-gfxpoly_t* gfxpoly_process(gfxpoly_t*poly, windrule_t*windrule, windcontext_t*context);
-
-typedef struct _event {
-    eventtype_t type;
-    point_t p;
-    segment_t*s1;
-    segment_t*s2;
-} event_t;
+char gfxcompactpoly_check(gfxcompactpoly_t*poly);
+int gfxcompactpoly_size(gfxcompactpoly_t*poly);
+void gfxcompactpoly_dump(gfxcompactpoly_t*poly);
+void gfxcompactpoly_save(gfxcompactpoly_t*poly, const char*filename);
+gfxpoly_t* gfxpoly_process(gfxcompactpoly_t*poly, windrule_t*windrule, windcontext_t*context);
 
 #ifndef CHECKS
 #ifdef assert
index ca0ccc2..c1a428f 100644 (file)
@@ -128,6 +128,7 @@ unsigned char* render_polygon(gfxpoly_t*polygon, intbbox_t*bbox, double zoom, wi
     buf->bbox = *bbox;
     buf->zoom = zoom * polygon->gridsize;
     int width8 = (buf->width+7) >> 3;
+    char bleeding = 0;
     unsigned char* image = (unsigned char*)malloc(width8*buf->height);
     memset(image, 0, width8*buf->height);
 
@@ -173,6 +174,8 @@ unsigned char* render_polygon(gfxpoly_t*polygon, intbbox_t*bbox, double zoom, wi
             /* we're bleeding, fill over padding, too. */
             fprintf(stderr, "Polygon %08x is bleeding in line %d\n", polygon, y);
             fill_bitwise(line, lastx, width8*8);
+           assert(line[width8-1]&0x01);
+           bleeding = 1;
         }
     }
     
@@ -182,6 +185,9 @@ unsigned char* render_polygon(gfxpoly_t*polygon, intbbox_t*bbox, double zoom, wi
         }
         memset(&buf->lines[y], 0, sizeof(renderline_t));
     }
+    if(bleeding) {
+       assert(!bitmap_ok(bbox, image));
+    }
     free(buf->lines);buf->lines=0;
     return image;
 }
@@ -192,6 +198,14 @@ unsigned char* render_polygon(gfxpoly_t*polygon, intbbox_t*bbox, double zoom, wi
 static inline max(double a, double b) {return a>b?a:b;}
 static inline min(double a, double b) {return a<b?a:b;}
 
+static int adjust_x(int xmin, int xmax)
+{
+    xmax += 8;
+    while(((xmax - xmin)&0x07) != 0x04)
+        xmax++;
+    return xmax;
+}
+
 intbbox_t intbbox_new(int x1, int y1, int x2, int y2)
 {
     intbbox_t b;
@@ -199,8 +213,9 @@ intbbox_t intbbox_new(int x1, int y1, int x2, int y2)
     b.ymin = y1;
     b.xmax = x2;
     b.ymax = y2;
-    b.width = x2-x1;
-    b.height = y2-y1;
+    b.xmax = adjust_x(b.xmin, b.xmax);
+    b.width = b.xmax - b.xmin;
+    b.height = b.ymax - b.ymin;
     return b;
 }
 
@@ -246,10 +261,8 @@ intbbox_t intbbox_from_polygon(gfxpoly_t*polygon, double zoom)
        b.xmin = b.xmax;
     if(b.ymin > b.ymax) 
        b.ymin = b.ymax;
-        
-    b.xmax += 8;
-    while(((b.xmax - b.xmin)&0x07) != 0x04)
-        b.xmax++;
+    
+    b.xmax = adjust_x(b.xmin, b.xmax);
 
     b.width = b.xmax - b.xmin;
     b.height = b.ymax - b.ymin;
@@ -279,8 +292,6 @@ int bitmap_ok(intbbox_t*bbox, unsigned char*data)
 {
     int y;
     int width8 = (bbox->width+7) >> 3;
-    if((bbox->width&7) == 0)
-        return 1;
     for(y=0;y<bbox->height;y++) {
         if(data[width8-1]&0x01)
             return 0; //bleeding
index c990ca2..4e3c875 100644 (file)
@@ -127,6 +127,7 @@ gfxline_t* make_circles()
        c = crc32_add_byte(c, t^0x55);
        int r = c%100;
        b = gfxline_append(b, gfxline_makecircle(x,y,r,r));
+       //b = gfxline_append(b, gfxline_makerectangle(10,10,100,100));
     }
     return b;
 }
@@ -150,11 +151,11 @@ int test0()
        m.tx = 400*1.41/2;
        m.ty = 400*1.41/2;
        gfxline_transform(b, &m);
-       gfxpoly_t*poly = gfxpoly_from_gfxline(b, 0.05);
+       gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(b, 0.05);
 
        gfxpoly_t*poly2 = gfxpoly_process(poly, &windrule_evenodd, &onepolygon);
+       gfxcompactpoly_destroy(poly);
        gfxpoly_destroy(poly2);
-       gfxpoly_destroy(poly);
     }
     gfxline_free(b);
 }
@@ -162,7 +163,6 @@ int test0()
 int test1(int argn, char*argv[])
 {
     gfxline_t*box1 = gfxline_makerectangle(50,50,150,150);
-    // put box2 and box3 on top of each other *snicker*
     gfxline_t*box2 = gfxline_makerectangle(100,100,200,200);
     gfxline_t*box3 = gfxline_makerectangle(100,100,200,200);
     gfxline_t*star = mkstar(50,50, 150,150);
@@ -170,7 +170,6 @@ int test1(int argn, char*argv[])
     b = gfxline_append(b, box1);
     b = gfxline_append(b, box2);
     b = gfxline_append(b, box3);
-    //b = gfxline_append(b, star);
 
     gfxmatrix_t matrix;
     memset(&matrix, 0, sizeof(gfxmatrix_t));
@@ -180,16 +179,38 @@ int test1(int argn, char*argv[])
 
     //gfxline_transform(b, &matrix);
 
-    gfxpoly_t*poly = gfxpoly_from_gfxline(b, 0.05);
+    gfxline_dump(b, stderr, "");
+
+    gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(b, 0.05);
     gfxline_free(box1);
     gfxline_free(box2);
     gfxline_free(box3);
     gfxline_free(star);
 
-    gfxpoly_dump(poly);
+    gfxcompactpoly_dump(poly);
     gfxpoly_t*poly2 = gfxpoly_process(poly, &windrule_evenodd, &onepolygon);
+    gfxcompactpoly_destroy(poly);
     gfxpoly_destroy(poly2);
-    gfxpoly_destroy(poly);
+}
+
+static void test_conversion(gfxline_t*line, double gridsize)
+{
+    double zoom=1.0;
+    gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(line, gridsize);
+    gfxpoly_t*poly1 = gfxpoly_from_gfxline(line, gridsize);
+    gfxpoly_t*poly2 = gfxpoly_from_gfxcompactpoly(poly);
+    assert(gfxpoly_check(poly1));
+    assert(gfxpoly_check(poly2));
+    assert(gfxcompactpoly_check(poly));
+    intbbox_t bbox = intbbox_from_polygon(poly1, zoom);
+    unsigned char*bitmap1 = render_polygon(poly1, &bbox, zoom, &windrule_evenodd, &onepolygon);
+    assert(bitmap_ok(&bbox, bitmap1));
+    unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd, &onepolygon);
+    assert(bitmap_ok(&bbox, bitmap2));
+    if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
+       save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
+       assert(!"bitmaps don't match");
+    }
 }
 
 int test_square(int width, int height, int num, double gridsize, char bitmaptest)
@@ -205,23 +226,29 @@ int test_square(int width, int height, int num, double gridsize, char bitmaptest
     line[num-1].x = line[0].x;
     line[num-1].y = line[0].y;
     line[num-1].next = 0;
+
+    test_conversion(line, gridsize);
     
-    gfxpoly_t*poly = gfxpoly_from_gfxline(line, gridsize);
+    gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(line, gridsize);
     gfxline_free(line);
+    gfxpoly_t*poly1 = gfxpoly_from_gfxcompactpoly(poly);
 
     windrule_t*rule = &windrule_circular;
     gfxpoly_t*poly2 = gfxpoly_process(poly, rule, &onepolygon);
     if(bitmaptest) {
         intbbox_t bbox = intbbox_new(0, 0, width, height);
-        unsigned char*bitmap1 = render_polygon(poly, &bbox, 1.0, rule, &onepolygon);
+        unsigned char*bitmap1 = render_polygon(poly1, &bbox, 1.0, rule, &onepolygon);
+       assert(bitmap_ok(&bbox, bitmap1));
         unsigned char*bitmap2 = render_polygon(poly2, &bbox, 1.0, &windrule_evenodd, &onepolygon);
+       assert(bitmap_ok(&bbox, bitmap2));
         if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
             save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
             assert(!"bitmaps don't match");
         }
     }
+    gfxpoly_destroy(poly1);
     gfxpoly_destroy(poly2);
-    gfxpoly_destroy(poly);
+    gfxcompactpoly_destroy(poly);
 }
 
 int test2(int argn, char*argv[])
@@ -230,6 +257,7 @@ int test2(int argn, char*argv[])
 
     int t;
     for(t=0;t<400;t++) {
+       fprintf(stderr, "%d\n", t);
         test_square(400,400, 50, 0.05, 1);
         test_square(200,3, 1000, 1.0, 0);
         test_square(3,200, 1000, 1.0, 0);
@@ -278,8 +306,10 @@ void test3(int argn, char*argv[])
 
         gfxline_t*l = gfxline_clone(line);
         gfxline_transform(l, &m);
+
+       test_conversion(l, 0.05);
         
-        gfxpoly_t*poly = gfxpoly_from_gfxline(l, 0.05);
+        gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(l, 0.05);
         gfxpoly_t*poly2 = gfxpoly_process(poly, rule, &onepolygon);
 
         tag = swf_InsertTag(tag, ST_DEFINESHAPE);
@@ -325,7 +355,7 @@ void test3(int argn, char*argv[])
         swf_ShapeSetEnd(tag);
         swf_ShapeFree(s);
 
-        gfxpoly_destroy(poly);
+        gfxcompactpoly_destroy(poly);
         gfxpoly_destroy(poly2);
 
         gfxline_free(l);
@@ -380,24 +410,25 @@ void test4(int argn, char*argv[])
             filename = argv[1];
 
         windrule_t*rule = &windrule_evenodd;
-        gfxpoly_t*poly = gfxpoly_from_file(filename, 1.0);//0.01);
+        gfxcompactpoly_t*poly = gfxcompactpoly_from_file(filename, 1.0);//0.01);
 
         if(argn!=2)
             free(filename);
 
         double zoom = 1.0;
 
-        if(!gfxpoly_check(poly)) {
+        if(!gfxcompactpoly_check(poly)) {
             printf("bad polygon\n");
             continue;
         }
 
+       gfxpoly_t*poly1 = gfxpoly_from_gfxcompactpoly(poly);
         gfxpoly_t*poly2 = gfxpoly_process(poly, rule, &onepolygon);
 
        int pass;
        for(pass=0;pass<2;pass++) {
-           intbbox_t bbox = intbbox_from_polygon(poly, zoom);
-           unsigned char*bitmap1 = render_polygon(poly, &bbox, zoom, rule, &onepolygon);
+           intbbox_t bbox = intbbox_from_polygon(poly1, zoom);
+           unsigned char*bitmap1 = render_polygon(poly1, &bbox, zoom, rule, &onepolygon);
            unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd, &onepolygon);
            if(!bitmap_ok(&bbox, bitmap1) || !bitmap_ok(&bbox, bitmap2)) {
                save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
@@ -411,12 +442,13 @@ void test4(int argn, char*argv[])
            free(bitmap2);
            
            // second pass renders the 90° rotated version
-           rotate90(poly);
+           rotate90(poly1);
            rotate90(poly2);
        }
 
-        gfxpoly_destroy(poly);
+        gfxpoly_destroy(poly1);
         gfxpoly_destroy(poly2);
+        gfxcompactpoly_destroy(poly);
         if(argn==2) 
             break;
     }
@@ -431,29 +463,31 @@ void extract_polygons_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
     //gfxcompactpoly_t*c = gfxcompactpoly_from_gfxline(line, 0.05);
     //gfxcompactpoly_free(c);
 
-    gfxpoly_t*poly = gfxpoly_from_gfxline(line, 0.05);
+    gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(line, 0.05);
 
-    gfxline_dump(line, stderr, "");
-    gfxpoly_dump(poly);
+    //gfxline_dump(line, stderr, "");
+    //gfxcompactpoly_dump(poly);
 
-    if(gfxpoly_size(poly)>100000) {
-       fprintf(stderr, "%d segments (skipping)\n", gfxpoly_size(poly));
+    if(gfxcompactpoly_size(poly)>100000) {
+       fprintf(stderr, "%d segments (skipping)\n", gfxcompactpoly_size(poly));
        return;
     } else {
        //fprintf(stderr, "%d segments\n", gfxpoly_size(poly));
     }
 
-    if(!gfxpoly_check(poly)) {
-        gfxpoly_destroy(poly);
+    if(!gfxcompactpoly_check(poly)) {
+        gfxcompactpoly_destroy(poly);
         fprintf(stderr, "bad polygon\n");
         return;
     }
 
     windrule_t*rule = &windrule_evenodd;
+
+    gfxpoly_t*poly1 = gfxpoly_from_gfxcompactpoly(poly);
         
     double zoom = 1.0;
-    intbbox_t bbox = intbbox_from_polygon(poly, zoom);
-    unsigned char*bitmap1 = render_polygon(poly, &bbox, zoom, rule, &onepolygon);
+    intbbox_t bbox = intbbox_from_polygon(poly1, zoom);
+    unsigned char*bitmap1 = render_polygon(poly1, &bbox, zoom, rule, &onepolygon);
     if(!bitmap_ok(&bbox, bitmap1)) {
         fprintf(stderr, "bad polygon or error in renderer\n");
         return;
@@ -471,8 +505,9 @@ void extract_polygons_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
     free(bitmap1);
     free(bitmap2);
 
-    gfxpoly_destroy(poly);
+    gfxpoly_destroy(poly1);
     gfxpoly_destroy(poly2);
+    gfxcompactpoly_destroy(poly);
 }
 int extract_polygons_setparameter(gfxdevice_t*dev, const char*key, const char*value) {
     return 0;
@@ -568,6 +603,6 @@ void test5(int argn, char*argv[])
 
 int main(int argn, char*argv[])
 {
-    test3(argn, argv);
+    test4(argn, argv);
 }