Blame gdk/gdkpoly-generic.h

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