enhanced logging
[swftools.git] / lib / gfxpoly / poly.c
index 8711a00..118629f 100644 (file)
@@ -234,18 +234,20 @@ char gfxpoly_check(gfxpoly_t*poly, char updown)
            return 0;
         }
     }
-    dict_destroy(d1);
     if(updown) {
        DICT_ITERATE_ITEMS(d2, point_t*, p2, void*, c2) {
            int count = (ptroff_t)c2;
+           assert(dict_contains(d1, p2));
+           int ocount = (ptroff_t)dict_lookup(d1, p2);
            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 (%d incoming, %d outgoing)\n", p2->x * poly->gridsize, p2->y * poly->gridsize, count, (ocount+count)/2, (ocount-count)/2);
+               if(count<0) fprintf(stderr, "Error: Point (%.2f,%.2f) has %d more outgoing than incoming segments (%d incoming, %d outgoing)\n", p2->x * poly->gridsize, p2->y * poly->gridsize, -count, (ocount+count)/2, (ocount-count)/2);
                dict_destroy(d2);
                return 0;
            }
        }
     }
+    dict_destroy(d1);
     dict_destroy(d2);
     return 1;
 }
@@ -382,6 +384,8 @@ 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(y1<y2);
@@ -390,13 +394,16 @@ static void segment_init(segment_t*s, int32_t x1, int32_t y1, int32_t x2, int32_
           "up/down" for horizontal segments is handled by "rotating"
            them 90° counterclockwise in screen coordinates (tilt your head to
            the right). In other words, the "normal" direction (what's positive dy for
-          vertical segments) is positive dx for horizontal segments.
+          vertical segments) is positive dx for horizontal segments ("down" is right).
         */
         if(x1>x2) {
             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 +418,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 
@@ -1091,8 +1096,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);
@@ -1189,9 +1195,10 @@ static void process_horizontals(status_t*status)
            segment_t* left = actlist_find(status->actlist, p1, p2);
            assert(!left || left->fs_out_ok);
 #ifdef DEBUG
-           fprintf(stderr, "  fragment %.2f..%.2f\n", 
+           fprintf(stderr, "  fragment %.2f..%.2f edge=%08x\n", 
                    x * status->gridsize,
-                   next_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), 
@@ -1213,16 +1220,19 @@ static void process_horizontals(status_t*status)
            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);
+               
+           segment_dir_t dir = above.is_filled?DIR_DOWN:DIR_UP;
            if(fs) {
+               //append_stroke(status, p1, p2, DIR_INVERT(h->dir), fs);
+               append_stroke(status, p1, p2, dir, fs);
+           }
 #ifdef DEBUG
-               fprintf(stderr, "    ...storing\n");
-#endif
-               append_stroke(status, p1, p2, h->dir, fs);
-           } else {
-#ifdef DEBUG
-               fprintf(stderr, "    ...ignoring\n");
+           fprintf(stderr, "    ...%s (below: (wind_nr=%d, filled=%d), above: (wind_nr=%d, filled=%d) %s\n",
+                   fs?"storing":"ignoring",
+                   below.wind_nr, below.is_filled,
+                   above.wind_nr, above.is_filled, 
+                   dir==DIR_UP?"up":"down");
 #endif
-           }
            x = next_x;
        }
     }
@@ -1362,6 +1372,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);