b9a21c3c91c47e090316e28d759194e46628ed49vboxsync/*
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncCopyright 1987, 1998 The Open Group
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncPermission to use, copy, modify, distribute, and sell this software and its
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncdocumentation for any purpose is hereby granted without fee, provided that
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncthe above copyright notice appear in all copies and that both that
b9a21c3c91c47e090316e28d759194e46628ed49vboxsynccopyright notice and this permission notice appear in supporting
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncdocumentation.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncThe above copyright notice and this permission notice shall be included
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncin all copies or substantial portions of the Software.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncIN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncOTHER DEALINGS IN THE SOFTWARE.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncExcept as contained in this notice, the name of The Open Group shall
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncnot be used in advertising or otherwise to promote the sale, use or
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncother dealings in this Software without prior written authorization
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncfrom The Open Group.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync*/
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync/*
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * fill.h
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync *
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * Created by Brian Kelleher; Oct 1985
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync *
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * Include file for filled polygon routines.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync *
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * These are the data structures needed to scan
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * convert regions. Two different scan conversion
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * methods are available -- the even-odd method, and
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * the winding number method.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * The even-odd rule states that a point is inside
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * the polygon if a ray drawn from that point in any
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * direction will pass through an odd number of
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * path segments.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * By the winding number rule, a point is decided
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * to be inside the polygon if a ray drawn from that
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * point in any direction passes through a different
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * number of clockwise and counter-clockwise path
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * segments.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync *
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * These data structures are adapted somewhat from
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * the algorithm in (Foley/Van Dam) for scan converting
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * polygons.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * The basic algorithm is to start at the top (smallest y)
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * of the polygon, stepping down to the bottom of
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * the polygon by incrementing the y coordinate. We
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * keep a list of edges which the current scanline crosses,
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * sorted by x. This list is called the Active Edge Table (AET)
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * As we change the y-coordinate, we update each entry in
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * in the active edge table to reflect the edges new xcoord.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * This list must be sorted at each scanline in case
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * two edges intersect.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * We also keep a data structure known as the Edge Table (ET),
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * which keeps track of all the edges which the current
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * scanline has not yet reached. The ET is basically a
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * list of ScanLineList structures containing a list of
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * edges which are entered at a given scanline. There is one
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * ScanLineList per scanline at which an edge is entered.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * When we enter a new edge, we move it from the ET to the AET.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync *
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * From the AET, we can implement the even-odd rule as in
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * (Foley/Van Dam).
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * The winding number rule is a little trickier. We also
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * keep the EdgeTableEntries in the AET linked by the
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * nextWETE (winding EdgeTableEntry) link. This allows
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * the edges to be linked just as before for updating
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * purposes, but only uses the edges linked by the nextWETE
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * link as edges representing spans of the polygon to
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * drawn (as with the even-odd rule).
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync/*
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * for the winding number rule
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync#define CLOCKWISE 1
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync#define COUNTERCLOCKWISE -1
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsynctypedef struct _EdgeTableEntry {
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync int ymax; /* ycoord at which we exit this edge. */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync BRESINFO bres; /* Bresenham info to run the edge */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync struct _EdgeTableEntry *next; /* next in the list */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync struct _EdgeTableEntry *back; /* for insertion sort */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync struct _EdgeTableEntry *nextWETE; /* for winding num rule */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync int ClockWise; /* flag for winding number rule */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync} EdgeTableEntry;
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsynctypedef struct _ScanLineList{
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync int scanline; /* the scanline represented */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync EdgeTableEntry *edgelist; /* header node */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync struct _ScanLineList *next; /* next in the list */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync} ScanLineList;
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsynctypedef struct {
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync int ymax; /* ymax for the polygon */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync int ymin; /* ymin for the polygon */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync ScanLineList scanlines; /* header node */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync} EdgeTable;
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync/*
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * Here is a struct to help with storage allocation
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * so we can allocate a big chunk at a time, and then take
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * pieces from this heap when we need to.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync#define SLLSPERBLOCK 25
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsynctypedef struct _ScanLineListBlock {
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync ScanLineList SLLs[SLLSPERBLOCK];
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync struct _ScanLineListBlock *next;
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync} ScanLineListBlock;
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync/*
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * number of points to buffer before sending them off
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * to scanlines() : Must be an even number
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync#define NUMPTSTOBUFFER 200
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync/*
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync *
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * a few macros for the inner loops of the fill code where
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * performance considerations don't allow a procedure call.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync *
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * Evaluate the given edge at the given scanline.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * If the edge has expired, then we leave it and fix up
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * the active edge table; otherwise, we increment the
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * x value to be ready for the next scanline.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * The winding number rule is in effect, so we must notify
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * the caller when the edge has been removed so he
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * can reorder the Winding Active Edge Table.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync#define EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) { \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync if (pAET->ymax == y) { /* leaving this edge */ \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync pPrevAET->next = pAET->next; \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync pAET = pPrevAET->next; \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync fixWAET = 1; \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync if (pAET) \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync pAET->back = pPrevAET; \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync } \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync else { \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync BRESINCRPGONSTRUCT(pAET->bres); \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync pPrevAET = pAET; \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync pAET = pAET->next; \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync } \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync}
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync/*
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * Evaluate the given edge at the given scanline.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * If the edge has expired, then we leave it and fix up
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * the active edge table; otherwise, we increment the
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * x value to be ready for the next scanline.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * The even-odd rule is in effect.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync#define EVALUATEEDGEEVENODD(pAET, pPrevAET, y) { \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync if (pAET->ymax == y) { /* leaving this edge */ \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync pPrevAET->next = pAET->next; \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync pAET = pPrevAET->next; \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync if (pAET) \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync pAET->back = pPrevAET; \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync } \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync else { \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync BRESINCRPGONSTRUCT(pAET->bres); \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync pPrevAET = pAET; \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync pAET = pAET->next; \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync } \
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync}
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync/* mipolyutil.c */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncextern _X_EXPORT Bool miCreateETandAET(
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync int /*count*/,
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync DDXPointPtr /*pts*/,
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync EdgeTable * /*ET*/,
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync EdgeTableEntry * /*AET*/,
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync EdgeTableEntry * /*pETEs*/,
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync ScanLineListBlock * /*pSLLBlock*/
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync);
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncextern _X_EXPORT void miloadAET(
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync EdgeTableEntry * /*AET*/,
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync EdgeTableEntry * /*ETEs*/
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync);
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncextern _X_EXPORT void micomputeWAET(
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync EdgeTableEntry * /*AET*/
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync);
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncextern _X_EXPORT int miInsertionSort(
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync EdgeTableEntry * /*AET*/
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync);
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncextern _X_EXPORT void miFreeStorage(
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync ScanLineListBlock * /*pSLLBlock*/
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync);