small code refactoring
[swftools.git] / lib / gfxpoly / poly.h
1 #ifndef __poly_h__
2 #define __poly_h__
3
4 #include <stdint.h>
5 #include "../q.h"
6 #include "../types.h"
7
8 //#define DEBUG
9 #define CHECKS
10 #define SPLAY
11
12 typedef enum {DIR_UP, DIR_DOWN, DIR_UNKNOWN} segment_dir_t;
13 typedef enum {EVENT_CROSS, EVENT_END, EVENT_START, EVENT_HORIZONTAL} eventtype_t;
14 typedef enum {SLOPE_POSITIVE, SLOPE_NEGATIVE} slope_t;
15
16 typedef struct _point {
17     int32_t x;
18     int32_t y;
19 } point_t;
20
21 typedef struct _fillstyle {
22     char is_filled;
23 } fillstyle_t;
24
25 typedef struct _edge {
26     point_t a;
27     point_t b;
28     fillstyle_t*style;
29 #ifdef DEBUG
30     int tmp;
31 #endif
32     struct _edge *next;
33 } edge_t;
34
35 typedef struct _windstate
36 {
37     char is_filled;
38     int wind_nr;
39 } windstate_t;
40
41 /* TODO: maybe we should merge windcontext and windrule */
42 typedef struct _windcontext
43 {
44     int num_polygons;
45 } windcontext_t;
46
47 typedef struct _windrule
48 {
49     windstate_t (*start)(windcontext_t* num_polygons);
50     windstate_t (*add)(windcontext_t*context, windstate_t left, fillstyle_t*edge, segment_dir_t dir, int polygon_nr);
51     fillstyle_t* (*diff)(windstate_t*left, windstate_t*right);
52 } windrule_t;
53
54 #define SEGNR(s) ((s)?(s)->nr:-1)
55
56 typedef struct _gfxpolystroke {
57     segment_dir_t dir;
58     int num_points;
59     point_t*points;
60     fillstyle_t*fs;
61 } gfxpolystroke_t;
62 typedef struct _gfxcompactpoly {
63     double gridsize;
64     int num_strokes;
65     gfxpolystroke_t*strokes;
66 } gfxcompactpoly_t;
67
68 typedef struct _segment {
69     point_t a;
70     point_t b;
71     point_t delta;
72     double k; //k = a.x*b.y-a.y*b.x = delta.y*a.x - delta.x*a.y (=0 for points on the segment)
73     int32_t minx, maxx;
74     
75     segment_dir_t dir;
76     fillstyle_t*fs;
77     fillstyle_t*fs_out;
78 #ifdef CHECKS
79     char fs_out_ok;
80 #endif
81     
82     int polygon_nr;
83     windstate_t wind;
84     ptroff_t nr;
85
86 #ifdef SPLAY
87     struct _segment*parent;
88     struct _segment*leftchild;
89     struct _segment*rightchild;
90 #endif
91     struct _segment*left;
92     struct _segment*right;
93     char changed;
94
95     point_t pos;
96
97     gfxpolystroke_t*stroke;
98     int stroke_pos;
99
100     dict_t scheduled_crossings;
101 } segment_t;
102
103 #define LINE_EQ(p,s) ((double)(s)->delta.y*(p).x - (double)(s)->delta.x*(p).y - (s)->k)
104
105 /* x1 + ((x2-x1)*(y-y1)) / dy = 
106    (x1*(y2-y1) + (x2-x1)*(y-y1)) / dy =
107    (x1*(y2-y)  +  x2    *(y-y1)) / dy =
108    (x1*y2 - x2*y1 + x2*y - y*x1) / dy =
109    (k + x2*y - x1*y) / dy
110    (k + dx*y) / dy
111 */
112 //#define XPOS(s,ypos) ((s)->a.x + ((s)->delta.x * (double)((ypos) - (s)->a.y)) / (s)->delta.y)
113 #define XPOS(s,ypos) (((s)->k + (double)(s)->delta.x*ypos) / (s)->delta.y)
114
115 #define XPOS_INT(s,ypos) ((int)ceil(XPOS((s),ypos)))
116 #define XDIFF(s1,s2,ypos) (((s1)->k + (double)(s1)->delta.x*ypos)*(s2)->delta.y - \
117                            ((s2)->k + (double)(s2)->delta.x*ypos)*(s1)->delta.y)
118
119 typedef struct _gfxpoly {
120     double gridsize;
121     edge_t*edges;
122 } gfxpoly_t;
123
124 void gfxpoly_fail(char*expr, char*file, int line, const char*function);
125
126 gfxpoly_t* gfxpoly_new(double gridsize);
127 char gfxcompactpoly_check(gfxcompactpoly_t*poly);
128 int gfxcompactpoly_size(gfxcompactpoly_t*poly);
129 void gfxcompactpoly_dump(gfxcompactpoly_t*poly);
130 void gfxcompactpoly_save(gfxcompactpoly_t*poly, const char*filename);
131 gfxpoly_t* gfxpoly_process(gfxcompactpoly_t*poly, windrule_t*windrule, windcontext_t*context);
132
133 #ifndef CHECKS
134 #ifdef assert
135 #undef assert
136 #endif
137 #define assert(x)
138 #else
139 #define assert(x) ((x)?0:gfxpoly_fail(__STRING(x), __FILE__, __LINE__, __PRETTY_FUNCTION__))
140 #endif
141
142
143 #endif