Blame gdk/gdkpoly-generic.h

Packit 98cdb6
/* $TOG: poly.h /main/5 1998/02/06 17:47:27 kaleb $ */
Packit 98cdb6
/************************************************************************
Packit 98cdb6
Packit 98cdb6
Copyright 1987, 1998  The Open Group
Packit 98cdb6
Packit 98cdb6
All Rights Reserved.
Packit 98cdb6
Packit 98cdb6
The above copyright notice and this permission notice shall be included in
Packit 98cdb6
all copies or substantial portions of the Software.
Packit 98cdb6
Packit 98cdb6
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit 98cdb6
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit 98cdb6
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
Packit 98cdb6
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
Packit 98cdb6
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit 98cdb6
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Packit 98cdb6
Packit 98cdb6
Except as contained in this notice, the name of The Open Group shall not be
Packit 98cdb6
used in advertising or otherwise to promote the sale, use or other dealings
Packit 98cdb6
in this Software without prior written authorization from The Open Group.
Packit 98cdb6
Packit 98cdb6
Packit 98cdb6
Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
Packit 98cdb6
Packit 98cdb6
                        All Rights Reserved
Packit 98cdb6
Packit 98cdb6
Permission to use, copy, modify, and distribute this software and its 
Packit 98cdb6
documentation for any purpose and without fee is hereby granted, 
Packit 98cdb6
provided that the above copyright notice appear in all copies and that
Packit 98cdb6
both that copyright notice and this permission notice appear in 
Packit 98cdb6
supporting documentation, and that the name of Digital not be
Packit 98cdb6
used in advertising or publicity pertaining to distribution of the
Packit 98cdb6
software without specific, written prior permission.  
Packit 98cdb6
Packit 98cdb6
DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
Packit 98cdb6
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
Packit 98cdb6
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
Packit 98cdb6
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
Packit 98cdb6
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
Packit 98cdb6
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
Packit 98cdb6
SOFTWARE.
Packit 98cdb6
Packit 98cdb6
************************************************************************/
Packit 98cdb6
Packit 98cdb6
/*
Packit 98cdb6
 *     This file contains a few macros to help track
Packit 98cdb6
 *     the edge of a filled object.  The object is assumed
Packit 98cdb6
 *     to be filled in scanline order, and thus the
Packit 98cdb6
 *     algorithm used is an extension of Bresenham's line
Packit 98cdb6
 *     drawing algorithm which assumes that y is always the
Packit 98cdb6
 *     major axis.
Packit 98cdb6
 *     Since these pieces of code are the same for any filled shape,
Packit 98cdb6
 *     it is more convenient to gather the library in one
Packit 98cdb6
 *     place, but since these pieces of code are also in
Packit 98cdb6
 *     the inner loops of output primitives, procedure call
Packit 98cdb6
 *     overhead is out of the question.
Packit 98cdb6
 *     See the author for a derivation if needed.
Packit 98cdb6
 */
Packit 98cdb6

Packit 98cdb6
Packit 98cdb6
/*
Packit 98cdb6
 *  In scan converting polygons, we want to choose those pixels
Packit 98cdb6
 *  which are inside the polygon.  Thus, we add .5 to the starting
Packit 98cdb6
 *  x coordinate for both left and right edges.  Now we choose the
Packit 98cdb6
 *  first pixel which is inside the pgon for the left edge and the
Packit 98cdb6
 *  first pixel which is outside the pgon for the right edge.
Packit 98cdb6
 *  Draw the left pixel, but not the right.
Packit 98cdb6
 *
Packit 98cdb6
 *  How to add .5 to the starting x coordinate:
Packit 98cdb6
 *      If the edge is moving to the right, then subtract dy from the
Packit 98cdb6
 *  error term from the general form of the algorithm.
Packit 98cdb6
 *      If the edge is moving to the left, then add dy to the error term.
Packit 98cdb6
 *
Packit 98cdb6
 *  The reason for the difference between edges moving to the left
Packit 98cdb6
 *  and edges moving to the right is simple:  If an edge is moving
Packit 98cdb6
 *  to the right, then we want the algorithm to flip immediately.
Packit 98cdb6
 *  If it is moving to the left, then we don't want it to flip until
Packit 98cdb6
 *  we traverse an entire pixel.
Packit 98cdb6
 */
Packit 98cdb6
#define BRESINITPGON(dy, x1, x2, xStart, d, m, m1, incr1, incr2) { \
Packit 98cdb6
    int dx;      /* local storage */ \
Packit 98cdb6
\
Packit 98cdb6
    /* \
Packit 98cdb6
     *  if the edge is horizontal, then it is ignored \
Packit 98cdb6
     *  and assumed not to be processed.  Otherwise, do this stuff. \
Packit 98cdb6
     */ \
Packit 98cdb6
    if ((dy) != 0) { \
Packit 98cdb6
        xStart = (x1); \
Packit 98cdb6
        dx = (x2) - xStart; \
Packit 98cdb6
        if (dx < 0) { \
Packit 98cdb6
            m = dx / (dy); \
Packit 98cdb6
            m1 = m - 1; \
Packit 98cdb6
            incr1 = -2 * dx + 2 * (dy) * m1; \
Packit 98cdb6
            incr2 = -2 * dx + 2 * (dy) * m; \
Packit 98cdb6
            d = 2 * m * (dy) - 2 * dx - 2 * (dy); \
Packit 98cdb6
        } else { \
Packit 98cdb6
            m = dx / (dy); \
Packit 98cdb6
            m1 = m + 1; \
Packit 98cdb6
            incr1 = 2 * dx - 2 * (dy) * m1; \
Packit 98cdb6
            incr2 = 2 * dx - 2 * (dy) * m; \
Packit 98cdb6
            d = -2 * m * (dy) + 2 * dx; \
Packit 98cdb6
        } \
Packit 98cdb6
    } \
Packit 98cdb6
}
Packit 98cdb6

Packit 98cdb6
#define BRESINCRPGON(d, minval, m, m1, incr1, incr2) { \
Packit 98cdb6
    if (m1 > 0) { \
Packit 98cdb6
        if (d > 0) { \
Packit 98cdb6
            minval += m1; \
Packit 98cdb6
            d += incr1; \
Packit 98cdb6
        } \
Packit 98cdb6
        else { \
Packit 98cdb6
            minval += m; \
Packit 98cdb6
            d += incr2; \
Packit 98cdb6
        } \
Packit 98cdb6
    } else {\
Packit 98cdb6
        if (d >= 0) { \
Packit 98cdb6
            minval += m1; \
Packit 98cdb6
            d += incr1; \
Packit 98cdb6
        } \
Packit 98cdb6
        else { \
Packit 98cdb6
            minval += m; \
Packit 98cdb6
            d += incr2; \
Packit 98cdb6
        } \
Packit 98cdb6
    } \
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6

Packit 98cdb6
/*
Packit 98cdb6
 *     This structure contains all of the information needed
Packit 98cdb6
 *     to run the bresenham algorithm.
Packit 98cdb6
 *     The variables may be hardcoded into the declarations
Packit 98cdb6
 *     instead of using this structure to make use of
Packit 98cdb6
 *     register declarations.
Packit 98cdb6
 */
Packit 98cdb6
typedef struct {
Packit 98cdb6
    int minor_axis;	/* minor axis        */
Packit 98cdb6
    int d;		/* decision variable */
Packit 98cdb6
    int m, m1;		/* slope and slope+1 */
Packit 98cdb6
    int incr1, incr2;	/* error increments */
Packit 98cdb6
} BRESINFO;
Packit 98cdb6
Packit 98cdb6
Packit 98cdb6
#define BRESINITPGONSTRUCT(dmaj, min1, min2, bres) \
Packit 98cdb6
	BRESINITPGON(dmaj, min1, min2, bres.minor_axis, bres.d, \
Packit 98cdb6
                     bres.m, bres.m1, bres.incr1, bres.incr2)
Packit 98cdb6
Packit 98cdb6
#define BRESINCRPGONSTRUCT(bres) \
Packit 98cdb6
        BRESINCRPGON(bres.d, bres.minor_axis, bres.m, bres.m1, bres.incr1, bres.incr2)
Packit 98cdb6
Packit 98cdb6
Packit 98cdb6
Packit 98cdb6
/*
Packit 98cdb6
 *     These are the data structures needed to scan
Packit 98cdb6
 *     convert regions.  Two different scan conversion
Packit 98cdb6
 *     methods are available -- the even-odd method, and
Packit 98cdb6
 *     the winding number method.
Packit 98cdb6
 *     The even-odd rule states that a point is inside
Packit 98cdb6
 *     the polygon if a ray drawn from that point in any
Packit 98cdb6
 *     direction will pass through an odd number of
Packit 98cdb6
 *     path segments.
Packit 98cdb6
 *     By the winding number rule, a point is decided
Packit 98cdb6
 *     to be inside the polygon if a ray drawn from that
Packit 98cdb6
 *     point in any direction passes through a different
Packit 98cdb6
 *     number of clockwise and counter-clockwise path
Packit 98cdb6
 *     segments.
Packit 98cdb6
 *
Packit 98cdb6
 *     These data structures are adapted somewhat from
Packit 98cdb6
 *     the algorithm in (Foley/Van Dam) for scan converting
Packit 98cdb6
 *     polygons.
Packit 98cdb6
 *     The basic algorithm is to start at the top (smallest y)
Packit 98cdb6
 *     of the polygon, stepping down to the bottom of
Packit 98cdb6
 *     the polygon by incrementing the y coordinate.  We
Packit 98cdb6
 *     keep a list of edges which the current scanline crosses,
Packit 98cdb6
 *     sorted by x.  This list is called the Active Edge Table (AET)
Packit 98cdb6
 *     As we change the y-coordinate, we update each entry in 
Packit 98cdb6
 *     in the active edge table to reflect the edges new xcoord.
Packit 98cdb6
 *     This list must be sorted at each scanline in case
Packit 98cdb6
 *     two edges intersect.
Packit 98cdb6
 *     We also keep a data structure known as the Edge Table (ET),
Packit 98cdb6
 *     which keeps track of all the edges which the current
Packit 98cdb6
 *     scanline has not yet reached.  The ET is basically a
Packit 98cdb6
 *     list of ScanLineList structures containing a list of
Packit 98cdb6
 *     edges which are entered at a given scanline.  There is one
Packit 98cdb6
 *     ScanLineList per scanline at which an edge is entered.
Packit 98cdb6
 *     When we enter a new edge, we move it from the ET to the AET.
Packit 98cdb6
 *
Packit 98cdb6
 *     From the AET, we can implement the even-odd rule as in
Packit 98cdb6
 *     (Foley/Van Dam).
Packit 98cdb6
 *     The winding number rule is a little trickier.  We also
Packit 98cdb6
 *     keep the EdgeTableEntries in the AET linked by the
Packit 98cdb6
 *     nextWETE (winding EdgeTableEntry) link.  This allows
Packit 98cdb6
 *     the edges to be linked just as before for updating
Packit 98cdb6
 *     purposes, but only uses the edges linked by the nextWETE
Packit 98cdb6
 *     link as edges representing spans of the polygon to
Packit 98cdb6
 *     drawn (as with the even-odd rule).
Packit 98cdb6
 */
Packit 98cdb6
Packit 98cdb6
/*
Packit 98cdb6
 * for the winding number rule
Packit 98cdb6
 */
Packit 98cdb6
#define CLOCKWISE          1
Packit 98cdb6
#define COUNTERCLOCKWISE  -1 
Packit 98cdb6
Packit 98cdb6
typedef struct _EdgeTableEntry {
Packit 98cdb6
     int ymax;             /* ycoord at which we exit this edge. */
Packit 98cdb6
     BRESINFO bres;        /* Bresenham info to run the edge     */
Packit 98cdb6
     struct _EdgeTableEntry *next;       /* next in the list     */
Packit 98cdb6
     struct _EdgeTableEntry *back;       /* for insertion sort   */
Packit 98cdb6
     struct _EdgeTableEntry *nextWETE;   /* for winding num rule */
Packit 98cdb6
     int ClockWise;        /* flag for winding number rule       */
Packit 98cdb6
} EdgeTableEntry;
Packit 98cdb6
Packit 98cdb6
Packit 98cdb6
typedef struct _ScanLineList{
Packit 98cdb6
     int scanline;              /* the scanline represented */
Packit 98cdb6
     EdgeTableEntry *edgelist;  /* header node              */
Packit 98cdb6
     struct _ScanLineList *next;  /* next in the list       */
Packit 98cdb6
} ScanLineList;
Packit 98cdb6
Packit 98cdb6
Packit 98cdb6
typedef struct {
Packit 98cdb6
     int ymax;                 /* ymax for the polygon     */
Packit 98cdb6
     int ymin;                 /* ymin for the polygon     */
Packit 98cdb6
     ScanLineList scanlines;   /* header node              */
Packit 98cdb6
} EdgeTable;
Packit 98cdb6
Packit 98cdb6
Packit 98cdb6
/*
Packit 98cdb6
 * Here is a struct to help with storage allocation
Packit 98cdb6
 * so we can allocate a big chunk at a time, and then take
Packit 98cdb6
 * pieces from this heap when we need to.
Packit 98cdb6
 */
Packit 98cdb6
#define SLLSPERBLOCK 25
Packit 98cdb6
Packit 98cdb6
typedef struct _ScanLineListBlock {
Packit 98cdb6
     ScanLineList SLLs[SLLSPERBLOCK];
Packit 98cdb6
     struct _ScanLineListBlock *next;
Packit 98cdb6
} ScanLineListBlock;
Packit 98cdb6
Packit 98cdb6
Packit 98cdb6

Packit 98cdb6
/*
Packit 98cdb6
 *
Packit 98cdb6
 *     a few macros for the inner loops of the fill code where
Packit 98cdb6
 *     performance considerations don't allow a procedure call.
Packit 98cdb6
 *
Packit 98cdb6
 *     Evaluate the given edge at the given scanline.
Packit 98cdb6
 *     If the edge has expired, then we leave it and fix up
Packit 98cdb6
 *     the active edge table; otherwise, we increment the
Packit 98cdb6
 *     x value to be ready for the next scanline.
Packit 98cdb6
 *     The winding number rule is in effect, so we must notify
Packit 98cdb6
 *     the caller when the edge has been removed so he
Packit 98cdb6
 *     can reorder the Winding Active Edge Table.
Packit 98cdb6
 */
Packit 98cdb6
#define EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) { \
Packit 98cdb6
   if (pAET->ymax == y) {          /* leaving this edge */ \
Packit 98cdb6
      pPrevAET->next = pAET->next; \
Packit 98cdb6
      pAET = pPrevAET->next; \
Packit 98cdb6
      fixWAET = 1; \
Packit 98cdb6
      if (pAET) \
Packit 98cdb6
         pAET->back = pPrevAET; \
Packit 98cdb6
   } \
Packit 98cdb6
   else { \
Packit 98cdb6
      BRESINCRPGONSTRUCT(pAET->bres); \
Packit 98cdb6
      pPrevAET = pAET; \
Packit 98cdb6
      pAET = pAET->next; \
Packit 98cdb6
   } \
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
Packit 98cdb6
/*
Packit 98cdb6
 *     Evaluate the given edge at the given scanline.
Packit 98cdb6
 *     If the edge has expired, then we leave it and fix up
Packit 98cdb6
 *     the active edge table; otherwise, we increment the
Packit 98cdb6
 *     x value to be ready for the next scanline.
Packit 98cdb6
 *     The even-odd rule is in effect.
Packit 98cdb6
 */
Packit 98cdb6
#define EVALUATEEDGEEVENODD(pAET, pPrevAET, y) { \
Packit 98cdb6
   if (pAET->ymax == y) {          /* leaving this edge */ \
Packit 98cdb6
      pPrevAET->next = pAET->next; \
Packit 98cdb6
      pAET = pPrevAET->next; \
Packit 98cdb6
      if (pAET) \
Packit 98cdb6
         pAET->back = pPrevAET; \
Packit 98cdb6
   } \
Packit 98cdb6
   else { \
Packit 98cdb6
      BRESINCRPGONSTRUCT(pAET->bres); \
Packit 98cdb6
      pPrevAET = pAET; \
Packit 98cdb6
      pAET = pAET->next; \
Packit 98cdb6
   } \
Packit 98cdb6
}