Merge branch 'horizontals'
[swftools.git] / lib / gfxpoly / active.c
index 34fe9d5..c5f9c02 100644 (file)
@@ -1,7 +1,9 @@
 #include <stdlib.h>
 #include <memory.h>
 #include <math.h>
+#include "../../config.h"
 #include "../q.h"
+#include "../types.h"
 #include "active.h"
 
 actlist_t* actlist_new()
@@ -14,7 +16,7 @@ void actlist_destroy(actlist_t*a)
     free(a);
 }
 
-void actlist_dump(actlist_t*a, int32_t y)
+void actlist_dump(actlist_t*a, int32_t y, double gridsize)
 {
     segment_t*s = a->list;
     double lastx;
@@ -25,14 +27,14 @@ void actlist_dump(actlist_t*a, int32_t y)
             double x = ((double)s->delta.x*(y-s->a.y)/s->delta.y)+s->a.x;
             if(s!=a->list) {
                 if(lastx>x) 
-                    fprintf(stderr, "?%f<->%f? ", lastx, x);
+                    fprintf(stderr, "?%.2f<->%.2f? ", lastx * gridsize, x * gridsize);
             }
             lastx = x;
         }
-        fprintf(stderr, "[%d]", s->nr);
+        fprintf(stderr, "[%d]", (int)s->nr);
         s = s->right;
         if(s) fprintf(stderr, " ");
-        else fprintf(stderr, "\n");
+        else fprintf(stderr, " y=%.2f\n", y * gridsize);
     }
 }
 void actlist_verify(actlist_t*a, int32_t y)
@@ -46,7 +48,7 @@ void actlist_verify(actlist_t*a, int32_t y)
                 /* we need to re-evaluate the x of the previous segment. if we
                    try to store it, it might end up being converted to a double,
                    which will make it non-equal to (and possibly larger than) the
-                   "long double" the FPU has in it's register. This only happens
+                   "long double" the FPU has in its register. This only happens
                    when compiler optimizations are turned on. */
                 assert((XPOS(s, y) - XPOS(l, y)) >= 0);
                 assert(XDIFF(s,l,y) >= 0);
@@ -89,7 +91,21 @@ segment_t* actlist_find(actlist_t*a, point_t p1, point_t p2)
     char to_the_left = 0;
     char fail = 0;
     while(t) {
+       /* this check doesn't work out with cmp() because during horizontal
+          processing, both segments ending as well as segments starting will
+          be active in this scanline */
+       //double d = cmp(t, p1, p2);
        double d = single_cmp(t, p1);
+       if(d>=0 && to_the_left) {
+           actlist_dump(a, p1.y, 1);
+           segment_t*s = a->list;
+           while(s) {
+               fprintf(stderr, "[%d] %f/%f (%d,%d) -> (%d,%d)\n", SEGNR(s),
+                       single_cmp(s,p1), cmp(s,p1,p2),
+                       s->a.x, s->a.y, s->b.x, s->b.y);
+               s = s->right;
+           }
+       }
        assert(!(d>=0 && to_the_left));
        if(d<0) to_the_left=1;
        t = t->right;
@@ -135,6 +151,7 @@ segment_t* actlist_find(actlist_t*a, point_t p1, point_t p2)
     }
 #endif
 
+    /* this can be optimized for (is not needed in) normal mode (as opposed to horizontal postprocess mode) */
     segment_t*out = last;
     if(d<0 || (d==0 && LINE_EQ(p2,last)<0)) {
        last = last->left;
@@ -360,7 +377,7 @@ static void move_to_root(actlist_t*a, segment_t*s)
     }
 }
 
-static int actlist_splay(actlist_t*a, point_t p1, point_t p2)
+static void actlist_splay(actlist_t*a, point_t p1, point_t p2)
 {
     if(!a->list) return;
 
@@ -445,7 +462,6 @@ static void actlist_insert_after(actlist_t*a, segment_t*left, segment_t*s)
 
     if(a->root) {
        move_to_root(a, left);
-       
        if(left) {
            LINK(s,leftchild,a->root);
            // steal right child from (previous) root
@@ -454,18 +470,6 @@ static void actlist_insert_after(actlist_t*a, segment_t*left, segment_t*s)
        } else {
            LINK(s,rightchild,a->root);
        }
-
-       /*if(cmp(a->root, s->a, s->b) < 0) {
-           LINK(s,rightchild,a->root);
-           // steal left child from (previous) root
-           LINK(s,leftchild,a->root->leftchild);
-           a->root->leftchild = 0;
-       } else {
-           LINK(s,leftchild,a->root);
-           // steal right child from (previous) root
-           LINK(s,rightchild,a->root->rightchild);
-           a->root->rightchild = 0;
-       }*/
     }
     a->root = s;
     a->root->parent = 0;
@@ -501,7 +505,11 @@ void actlist_delete(actlist_t*a, segment_t*s)
     } else if(!a->root->rightchild) {
        a->root = a->root->leftchild;
     } else {
+#ifdef HAVE_LRAND48
        if(lrand48()&1) {
+#else
+       if(((ptroff_t)s)&16) {
+#endif
            // free up root->left->right
            segment_t*t = a->root->leftchild;
            while(t->rightchild) {
@@ -551,6 +559,9 @@ segment_t* actlist_leftmost(actlist_t*a)
 segment_t* actlist_rightmost(actlist_t*a)
 {
     /* this is only used in checks, so it doesn't matter that it's slow */
+#ifndef CHECKS
+    fprintf(stderr, "Warning: actlist_rightmost should not be used\n");
+#endif
     segment_t*s = a->list;
     segment_t*last = 0;
     while(s) {
@@ -566,18 +577,6 @@ void actlist_insert(actlist_t*a, point_t p1, point_t p2, segment_t*s)
     actlist_insert_after(a, left, s);
 }
 
-
-segment_t* actlist_left(actlist_t*a, segment_t*s)
-{
-    return s->left;
-}
-
-segment_t* actlist_right(actlist_t*a, segment_t*s)
-{
-    if(s) return s->right;
-    else  return a->list;
-}
-
 void actlist_swap(actlist_t*a, segment_t*s1, segment_t*s2)
 {
 #ifdef SPLAY