polygon intersector: added horizontal line reconstruction
[swftools.git] / lib / gfxpoly / test.c
index e8c140c..6e698ca 100644 (file)
@@ -1,10 +1,12 @@
 #include <stdlib.h>
 #include <stdio.h>
+#include <assert.h>
 #include <memory.h>
 #include <math.h>
 #include "../gfxtools.h"
 #include "poly.h"
 #include "convert.h"
+#include "renderpoly.h"
 
 gfxline_t*mkstar(int x1, int y1, int x2, int y2)
 {
@@ -35,7 +37,7 @@ int test1()
     b = gfxline_append(b, box1);
     b = gfxline_append(b, box2);
     b = gfxline_append(b, box3);
-    b = gfxline_append(b, star);
+    //b = gfxline_append(b, star);
 
     gfxmatrix_t matrix;
     memset(&matrix, 0, sizeof(gfxmatrix_t));
@@ -43,38 +45,60 @@ int test1()
     matrix.m00=cos(ua);matrix.m10=sin(ua);
     matrix.m01=-sin(ua);matrix.m11=cos(ua);
 
-    gfxline_transform(b, &matrix);
-    gfxpoly_t*poly = gfxpoly_fillToPoly(b);
+    //gfxline_transform(b, &matrix);
+    gfxpoly_t*poly = gfxpoly_fillToPoly(b, 0.05);
     gfxline_free(box1);
     gfxline_free(box2);
     gfxline_free(box3);
     gfxline_free(star);
 
     gfxpoly_dump(poly);
-    gfxpoly_process(poly);
+    gfxpoly_process(poly, &windrule_evenodd);
 }
 
-int test2()
+int test_square(int width, int height, int num, double gridsize, char bitmaptest)
 {
-#define N 50
-#define RANGE 150
     int t;
-    gfxline_t* line = malloc(sizeof(gfxline_t)*N);
-    for(t=0;t<N;t++) {
+    gfxline_t* line = malloc(sizeof(gfxline_t)*num);
+    for(t=0;t<num;t++) {
         line[t].type = t?gfx_lineTo:gfx_moveTo;
-        line[t].x = (lrand48()%RANGE)-RANGE/2;
-        line[t].y = (lrand48()%RANGE)-RANGE/2;
+        line[t].x = (lrand48()%width);
+        line[t].y = (lrand48()%height);
         line[t].next = &line[t+1];
     }
-    line[N-1].x = line[0].x;
-    line[N-1].y = line[0].y;
-    line[N-1].next = 0;
+    line[num-1].x = line[0].x;
+    line[num-1].y = line[0].y;
+    line[num-1].next = 0;
     
-    gfxpoly_t*poly = gfxpoly_fillToPoly(line);
-    gfxpoly_t*poly2 = gfxpoly_process(poly);
+    gfxpoly_t*poly = gfxpoly_fillToPoly(line, gridsize);
     gfxline_free(line);
-    gfxpoly_destroy(poly);
+
+    windrule_t*rule = &windrule_circular;
+    gfxpoly_t*poly2 = gfxpoly_process(poly, rule);
+    if(bitmaptest) {
+        intbbox_t bbox = intbbox_new(0, 0, width, height);
+        unsigned char*bitmap1 = render_polygon(poly, &bbox, 1.0, rule);
+        unsigned char*bitmap2 = render_polygon(poly2, &bbox, 1.0, &windrule_evenodd);
+        if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
+            save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
+            assert(!"bitmaps don't match");
+        }
+    }
     gfxpoly_destroy(poly2);
+    gfxpoly_destroy(poly);
+}
+
+int test2()
+{
+    test_square(400,400, 3, 0.05, 1);
+
+    int t;
+    for(t=0;t<400;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);
+        test_square(10,10, 200, 1.0, 0);
+    }
 }
 
 #include "../rfxswf.h"
@@ -82,17 +106,23 @@ void test3()
 {
 #undef N
 #undef RANGE
-#define N 250
-#define RANGE 150
+#define N 100
+#define RANGE 400
 
     int i;
-    gfxline_t* line = malloc(sizeof(gfxline_t)*N);
+    gfxline_t* line = malloc(sizeof(gfxline_t)*N*2);
     for(i=0;i<N;i++) {
         line[i].type = i?gfx_lineTo:gfx_moveTo;
         line[i].x = lrand48()%RANGE - RANGE/2;
         line[i].y = lrand48()%RANGE - RANGE/2;
         line[i].next = &line[i+1];
+        line[N*2-i-1].type = gfx_lineTo;
+        line[N*2-i-1].x = line[i].x;
+        line[N*2-i-1].y = line[i].y;
+        line[N*2-i-1].next = &line[N*2-i];
     }
+    line[N*2-1].next = 0;
+
     line[N-1].x = line[0].x;
     line[N-1].y = line[0].y;
     line[N-1].next = 0;
@@ -122,8 +152,8 @@ void test3()
         gfxline_t*l = gfxline_clone(line);
         gfxline_transform(l, &m);
         
-        gfxpoly_t*poly = gfxpoly_fillToPoly(l);
-        gfxpoly_t*poly2 = gfxpoly_process(poly);
+        gfxpoly_t*poly = gfxpoly_fillToPoly(l, 0.05);
+        gfxpoly_t*poly2 = gfxpoly_process(poly, &windrule_circular);
 
         tag = swf_InsertTag(tag, ST_DEFINESHAPE);
         SHAPE* s;
@@ -131,17 +161,40 @@ void test3()
         RGBA rgb;
         rgb.r = rgb.g = 0x00; rgb.b = 0xff;
         rgb.a = 255;
-        int j = swf_ShapeAddSolidFillStyle(s,&rgb);
+        int fs = swf_ShapeAddSolidFillStyle(s,&rgb);
+        int ls = swf_ShapeAddLineStyle(s,20,&rgb);
         swf_SetU16(tag,t+1);
         swf_SetRect(tag,&swf.movieSize);
         swf_SetShapeHeader(tag,s);
-        swf_ShapeSetAll(tag,s,0,0,0,j,0);
-        edge_t*e = poly2;
+
+#define FILL
+#ifdef FILL
+        swf_ShapeSetAll(tag,s,0,0,0,fs,0);
+        edge_t*e = poly2->edges;
         while(e) {
-            swf_ShapeSetMove(tag, s, e->a.x*20, e->a.y*20);
-            swf_ShapeSetLine(tag, s, e->b.x*20 - e->a.x*20, e->b.y*20 - e->a.y*20);
+#define ROTATE
+#ifdef ROTATE
+            swf_ShapeSetMove(tag, s, e->a.y, e->a.x);
+            swf_ShapeSetLine(tag, s, e->b.y - e->a.y, e->b.x - e->a.x);
+#else
+            swf_ShapeSetMove(tag, s, e->a.x, e->a.y);
+            swf_ShapeSetLine(tag, s, e->b.x - e->a.x, e->b.y - e->a.y);
+#endif
             e = e->next;
         }
+#else
+        swf_ShapeSetAll(tag,s,0,0,ls,0,0);
+        edge_t*e = poly2->edges;
+        while(e) {
+            swf_ShapeSetMove(tag, s, e->a.x, e->a.y);
+            swf_ShapeSetLine(tag, s, e->b.x - e->a.x, e->b.y - e->a.y);
+            
+            swf_ShapeSetCircle(tag, s, e->a.x, e->a.y, 5*20, 5*20);
+            swf_ShapeSetCircle(tag, s, e->b.x, e->b.y, 5*20, 5*20);
+            e = e->next;
+        }
+#endif
+
         swf_ShapeSetEnd(tag);
         swf_ShapeFree(s);
 
@@ -164,6 +217,48 @@ void test3()
     swf_SaveSWF(&swf, "test.swf");
 }
 
+#include <dirent.h>
+void test4()
+{
+    char*dir = "ps";
+    DIR*_dir = opendir(dir);
+    if(!_dir) return;
+    struct dirent*file;
+    while(1) {
+        file = readdir(_dir);
+        if (!file) 
+            break;
+        if(!strstr(file->d_name, ".ps")) 
+            continue;
+
+        char* filename = allocprintf("%s/%s", dir, file->d_name);
+        windrule_t*rule = &windrule_evenodd;
+        gfxpoly_t*poly = gfxpoly_from_file(filename, 0.01);
+        free(filename);
+
+        double zoom = 1.0;
+        intbbox_t bbox = intbbox_from_polygon(poly, zoom);
+
+        if(!gfxpoly_check(poly)) {
+            printf("bad polygon\n");
+            continue;
+        }
+
+        gfxpoly_t*poly2 = gfxpoly_process(poly, rule);
+        unsigned char*bitmap1 = render_polygon(poly, &bbox, zoom, rule);
+        unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd);
+        if(!bitmap_ok(&bbox, bitmap1) || !bitmap_ok(&bbox, bitmap2)) {
+            save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
+            assert(!"error in bitmaps");
+        }
+        if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
+            save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
+            assert(!"bitmaps don't match");
+        }
+        gfxpoly_destroy(poly2);
+    }
+}
+
 int main()
 {
     test3();