]> Pileus Git - ~andy/gtk/blob - gdk/gdkpoly-generic.h
file makefile.mingw was initially added on branch gtk-1-3-win32-production.
[~andy/gtk] / gdk / gdkpoly-generic.h
1 /* $TOG: poly.h /main/5 1998/02/06 17:47:27 kaleb $ */
2 /************************************************************************
3
4 Copyright 1987, 1998  The Open Group
5
6 All Rights Reserved.
7
8 The above copyright notice and this permission notice shall be included in
9 all copies or substantial portions of the Software.
10
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
14 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
15 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17
18 Except as contained in this notice, the name of The Open Group shall not be
19 used in advertising or otherwise to promote the sale, use or other dealings
20 in this Software without prior written authorization from The Open Group.
21
22
23 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
24
25                         All Rights Reserved
26
27 Permission to use, copy, modify, and distribute this software and its 
28 documentation for any purpose and without fee is hereby granted, 
29 provided that the above copyright notice appear in all copies and that
30 both that copyright notice and this permission notice appear in 
31 supporting documentation, and that the name of Digital not be
32 used in advertising or publicity pertaining to distribution of the
33 software without specific, written prior permission.  
34
35 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
36 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
37 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
38 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
39 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
40 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
41 SOFTWARE.
42
43 ************************************************************************/
44
45 /*
46  *     This file contains a few macros to help track
47  *     the edge of a filled object.  The object is assumed
48  *     to be filled in scanline order, and thus the
49  *     algorithm used is an extension of Bresenham's line
50  *     drawing algorithm which assumes that y is always the
51  *     major axis.
52  *     Since these pieces of code are the same for any filled shape,
53  *     it is more convenient to gather the library in one
54  *     place, but since these pieces of code are also in
55  *     the inner loops of output primitives, procedure call
56  *     overhead is out of the question.
57  *     See the author for a derivation if needed.
58  */
59 \f
60
61 /*
62  *  In scan converting polygons, we want to choose those pixels
63  *  which are inside the polygon.  Thus, we add .5 to the starting
64  *  x coordinate for both left and right edges.  Now we choose the
65  *  first pixel which is inside the pgon for the left edge and the
66  *  first pixel which is outside the pgon for the right edge.
67  *  Draw the left pixel, but not the right.
68  *
69  *  How to add .5 to the starting x coordinate:
70  *      If the edge is moving to the right, then subtract dy from the
71  *  error term from the general form of the algorithm.
72  *      If the edge is moving to the left, then add dy to the error term.
73  *
74  *  The reason for the difference between edges moving to the left
75  *  and edges moving to the right is simple:  If an edge is moving
76  *  to the right, then we want the algorithm to flip immediately.
77  *  If it is moving to the left, then we don't want it to flip until
78  *  we traverse an entire pixel.
79  */
80 #define BRESINITPGON(dy, x1, x2, xStart, d, m, m1, incr1, incr2) { \
81     int dx;      /* local storage */ \
82 \
83     /* \
84      *  if the edge is horizontal, then it is ignored \
85      *  and assumed not to be processed.  Otherwise, do this stuff. \
86      */ \
87     if ((dy) != 0) { \
88         xStart = (x1); \
89         dx = (x2) - xStart; \
90         if (dx < 0) { \
91             m = dx / (dy); \
92             m1 = m - 1; \
93             incr1 = -2 * dx + 2 * (dy) * m1; \
94             incr2 = -2 * dx + 2 * (dy) * m; \
95             d = 2 * m * (dy) - 2 * dx - 2 * (dy); \
96         } else { \
97             m = dx / (dy); \
98             m1 = m + 1; \
99             incr1 = 2 * dx - 2 * (dy) * m1; \
100             incr2 = 2 * dx - 2 * (dy) * m; \
101             d = -2 * m * (dy) + 2 * dx; \
102         } \
103     } \
104 }
105 \f
106 #define BRESINCRPGON(d, minval, m, m1, incr1, incr2) { \
107     if (m1 > 0) { \
108         if (d > 0) { \
109             minval += m1; \
110             d += incr1; \
111         } \
112         else { \
113             minval += m; \
114             d += incr2; \
115         } \
116     } else {\
117         if (d >= 0) { \
118             minval += m1; \
119             d += incr1; \
120         } \
121         else { \
122             minval += m; \
123             d += incr2; \
124         } \
125     } \
126 }
127
128 \f
129 /*
130  *     This structure contains all of the information needed
131  *     to run the bresenham algorithm.
132  *     The variables may be hardcoded into the declarations
133  *     instead of using this structure to make use of
134  *     register declarations.
135  */
136 typedef struct {
137     int minor_axis;     /* minor axis        */
138     int d;              /* decision variable */
139     int m, m1;          /* slope and slope+1 */
140     int incr1, incr2;   /* error increments */
141 } BRESINFO;
142
143
144 #define BRESINITPGONSTRUCT(dmaj, min1, min2, bres) \
145         BRESINITPGON(dmaj, min1, min2, bres.minor_axis, bres.d, \
146                      bres.m, bres.m1, bres.incr1, bres.incr2)
147
148 #define BRESINCRPGONSTRUCT(bres) \
149         BRESINCRPGON(bres.d, bres.minor_axis, bres.m, bres.m1, bres.incr1, bres.incr2)
150
151
152
153 /*
154  *     These are the data structures needed to scan
155  *     convert regions.  Two different scan conversion
156  *     methods are available -- the even-odd method, and
157  *     the winding number method.
158  *     The even-odd rule states that a point is inside
159  *     the polygon if a ray drawn from that point in any
160  *     direction will pass through an odd number of
161  *     path segments.
162  *     By the winding number rule, a point is decided
163  *     to be inside the polygon if a ray drawn from that
164  *     point in any direction passes through a different
165  *     number of clockwise and counter-clockwise path
166  *     segments.
167  *
168  *     These data structures are adapted somewhat from
169  *     the algorithm in (Foley/Van Dam) for scan converting
170  *     polygons.
171  *     The basic algorithm is to start at the top (smallest y)
172  *     of the polygon, stepping down to the bottom of
173  *     the polygon by incrementing the y coordinate.  We
174  *     keep a list of edges which the current scanline crosses,
175  *     sorted by x.  This list is called the Active Edge Table (AET)
176  *     As we change the y-coordinate, we update each entry in 
177  *     in the active edge table to reflect the edges new xcoord.
178  *     This list must be sorted at each scanline in case
179  *     two edges intersect.
180  *     We also keep a data structure known as the Edge Table (ET),
181  *     which keeps track of all the edges which the current
182  *     scanline has not yet reached.  The ET is basically a
183  *     list of ScanLineList structures containing a list of
184  *     edges which are entered at a given scanline.  There is one
185  *     ScanLineList per scanline at which an edge is entered.
186  *     When we enter a new edge, we move it from the ET to the AET.
187  *
188  *     From the AET, we can implement the even-odd rule as in
189  *     (Foley/Van Dam).
190  *     The winding number rule is a little trickier.  We also
191  *     keep the EdgeTableEntries in the AET linked by the
192  *     nextWETE (winding EdgeTableEntry) link.  This allows
193  *     the edges to be linked just as before for updating
194  *     purposes, but only uses the edges linked by the nextWETE
195  *     link as edges representing spans of the polygon to
196  *     drawn (as with the even-odd rule).
197  */
198
199 /*
200  * for the winding number rule
201  */
202 #define CLOCKWISE          1
203 #define COUNTERCLOCKWISE  -1 
204
205 typedef struct _EdgeTableEntry {
206      int ymax;             /* ycoord at which we exit this edge. */
207      BRESINFO bres;        /* Bresenham info to run the edge     */
208      struct _EdgeTableEntry *next;       /* next in the list     */
209      struct _EdgeTableEntry *back;       /* for insertion sort   */
210      struct _EdgeTableEntry *nextWETE;   /* for winding num rule */
211      int ClockWise;        /* flag for winding number rule       */
212 } EdgeTableEntry;
213
214
215 typedef struct _ScanLineList{
216      int scanline;              /* the scanline represented */
217      EdgeTableEntry *edgelist;  /* header node              */
218      struct _ScanLineList *next;  /* next in the list       */
219 } ScanLineList;
220
221
222 typedef struct {
223      int ymax;                 /* ymax for the polygon     */
224      int ymin;                 /* ymin for the polygon     */
225      ScanLineList scanlines;   /* header node              */
226 } EdgeTable;
227
228
229 /*
230  * Here is a struct to help with storage allocation
231  * so we can allocate a big chunk at a time, and then take
232  * pieces from this heap when we need to.
233  */
234 #define SLLSPERBLOCK 25
235
236 typedef struct _ScanLineListBlock {
237      ScanLineList SLLs[SLLSPERBLOCK];
238      struct _ScanLineListBlock *next;
239 } ScanLineListBlock;
240
241
242 \f
243 /*
244  *
245  *     a few macros for the inner loops of the fill code where
246  *     performance considerations don't allow a procedure call.
247  *
248  *     Evaluate the given edge at the given scanline.
249  *     If the edge has expired, then we leave it and fix up
250  *     the active edge table; otherwise, we increment the
251  *     x value to be ready for the next scanline.
252  *     The winding number rule is in effect, so we must notify
253  *     the caller when the edge has been removed so he
254  *     can reorder the Winding Active Edge Table.
255  */
256 #define EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) { \
257    if (pAET->ymax == y) {          /* leaving this edge */ \
258       pPrevAET->next = pAET->next; \
259       pAET = pPrevAET->next; \
260       fixWAET = 1; \
261       if (pAET) \
262          pAET->back = pPrevAET; \
263    } \
264    else { \
265       BRESINCRPGONSTRUCT(pAET->bres); \
266       pPrevAET = pAET; \
267       pAET = pAET->next; \
268    } \
269 }
270
271
272 /*
273  *     Evaluate the given edge at the given scanline.
274  *     If the edge has expired, then we leave it and fix up
275  *     the active edge table; otherwise, we increment the
276  *     x value to be ready for the next scanline.
277  *     The even-odd rule is in effect.
278  */
279 #define EVALUATEEDGEEVENODD(pAET, pPrevAET, y) { \
280    if (pAET->ymax == y) {          /* leaving this edge */ \
281       pPrevAET->next = pAET->next; \
282       pAET = pPrevAET->next; \
283       if (pAET) \
284          pAET->back = pPrevAET; \
285    } \
286    else { \
287       BRESINCRPGONSTRUCT(pAET->bres); \
288       pPrevAET = pAET; \
289       pAET = pAET->next; \
290    } \
291 }