Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | Related Pages

poly-generic.h

00001 /* $TOG: poly.h /main/5 1998/02/06 17:47:27 kaleb $ */
00002 /************************************************************************
00003 
00004 Copyright 1987, 1998  The Open Group
00005 
00006 All Rights Reserved.
00007 
00008 The above copyright notice and this permission notice shall be included in
00009 all copies or substantial portions of the Software.
00010 
00011 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00012 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00013 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00014 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00015 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00016 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 
00018 Except as contained in this notice, the name of The Open Group shall not be
00019 used in advertising or otherwise to promote the sale, use or other dealings
00020 in this Software without prior written authorization from The Open Group.
00021 
00022 
00023 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
00024 
00025                         All Rights Reserved
00026 
00027 Permission to use, copy, modify, and distribute this software and its 
00028 documentation for any purpose and without fee is hereby granted, 
00029 provided that the above copyright notice appear in all copies and that
00030 both that copyright notice and this permission notice appear in 
00031 supporting documentation, and that the name of Digital not be
00032 used in advertising or publicity pertaining to distribution of the
00033 software without specific, written prior permission.  
00034 
00035 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
00036 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
00037 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
00038 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
00039 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
00040 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00041 SOFTWARE.
00042 
00043 ************************************************************************/
00044 
00045 /*
00046  *     This file contains a few macros to help track
00047  *     the edge of a filled object.  The object is assumed
00048  *     to be filled in scanline order, and thus the
00049  *     algorithm used is an extension of Bresenham's line
00050  *     drawing algorithm which assumes that y is always the
00051  *     major axis.
00052  *     Since these pieces of code are the same for any filled shape,
00053  *     it is more convenient to gather the library in one
00054  *     place, but since these pieces of code are also in
00055  *     the inner loops of output primitives, procedure call
00056  *     overhead is out of the question.
00057  *     See the author for a derivation if needed.
00058  */
00059 
00060 
00061 /*
00062  *  In scan converting polygons, we want to choose those pixels
00063  *  which are inside the polygon.  Thus, we add .5 to the starting
00064  *  x coordinate for both left and right edges.  Now we choose the
00065  *  first pixel which is inside the pgon for the left edge and the
00066  *  first pixel which is outside the pgon for the right edge.
00067  *  Draw the left pixel, but not the right.
00068  *
00069  *  How to add .5 to the starting x coordinate:
00070  *      If the edge is moving to the right, then subtract dy from the
00071  *  error term from the general form of the algorithm.
00072  *      If the edge is moving to the left, then add dy to the error term.
00073  *
00074  *  The reason for the difference between edges moving to the left
00075  *  and edges moving to the right is simple:  If an edge is moving
00076  *  to the right, then we want the algorithm to flip immediately.
00077  *  If it is moving to the left, then we don't want it to flip until
00078  *  we traverse an entire pixel.
00079  */
00080 
00081 // Stolen from Gtk+ and C++-ized by Ron Steinke, January 2003
00082 
00083 #define BRESINITPGON(dy, x1, x2, xStart, d, m, m1, incr1, incr2) { \
00084     int dx;      /* local storage */ \
00085 \
00086     /* \
00087      *  if the edge is horizontal, then it is ignored \
00088      *  and assumed not to be processed.  Otherwise, do this stuff. \
00089      */ \
00090     if ((dy) != 0) { \
00091         xStart = (x1); \
00092         dx = (x2) - xStart; \
00093         if (dx < 0) { \
00094             m = dx / (dy); \
00095             m1 = m - 1; \
00096             incr1 = -2 * dx + 2 * (dy) * m1; \
00097             incr2 = -2 * dx + 2 * (dy) * m; \
00098             d = 2 * m * (dy) - 2 * dx - 2 * (dy); \
00099         } else { \
00100             m = dx / (dy); \
00101             m1 = m + 1; \
00102             incr1 = 2 * dx - 2 * (dy) * m1; \
00103             incr2 = 2 * dx - 2 * (dy) * m; \
00104             d = -2 * m * (dy) + 2 * dx; \
00105         } \
00106     } \
00107 }
00108 
00109 #define BRESINCRPGON(d, minval, m, m1, incr1, incr2) { \
00110     if (m1 > 0) { \
00111         if (d > 0) { \
00112             minval += m1; \
00113             d += incr1; \
00114         } \
00115         else { \
00116             minval += m; \
00117             d += incr2; \
00118         } \
00119     } else {\
00120         if (d >= 0) { \
00121             minval += m1; \
00122             d += incr1; \
00123         } \
00124         else { \
00125             minval += m; \
00126             d += incr2; \
00127         } \
00128     } \
00129 }
00130 
00131 
00132 /*
00133  *     This structure contains all of the information needed
00134  *     to run the bresenham algorithm.
00135  *     The variables may be hardcoded into the declarations
00136  *     instead of using this structure to make use of
00137  *     register declarations.
00138  */
00139 typedef struct {
00140     int minor_axis; /* minor axis        */
00141     int d;      /* decision variable */
00142     int m, m1;      /* slope and slope+1 */
00143     int incr1, incr2;   /* error increments */
00144 } BRESINFO;
00145 
00146 
00147 #define BRESINITPGONSTRUCT(dmaj, min1, min2, bres) \
00148     BRESINITPGON(dmaj, min1, min2, bres.minor_axis, bres.d, \
00149                      bres.m, bres.m1, bres.incr1, bres.incr2)
00150 
00151 #define BRESINCRPGONSTRUCT(bres) \
00152         BRESINCRPGON(bres.d, bres.minor_axis, bres.m, bres.m1, bres.incr1, bres.incr2)
00153 
00154 
00155 
00156 /*
00157  *     These are the data structures needed to scan
00158  *     convert regions.  Two different scan conversion
00159  *     methods are available -- the even-odd method, and
00160  *     the winding number method.
00161  *     The even-odd rule states that a point is inside
00162  *     the polygon if a ray drawn from that point in any
00163  *     direction will pass through an odd number of
00164  *     path segments.
00165  *     By the winding number rule, a point is decided
00166  *     to be inside the polygon if a ray drawn from that
00167  *     point in any direction passes through a different
00168  *     number of clockwise and counter-clockwise path
00169  *     segments.
00170  *
00171  *     These data structures are adapted somewhat from
00172  *     the algorithm in (Foley/Van Dam) for scan converting
00173  *     polygons.
00174  *     The basic algorithm is to start at the top (smallest y)
00175  *     of the polygon, stepping down to the bottom of
00176  *     the polygon by incrementing the y coordinate.  We
00177  *     keep a list of edges which the current scanline crosses,
00178  *     sorted by x.  This list is called the Active Edge Table (AET)
00179  *     As we change the y-coordinate, we update each entry in 
00180  *     in the active edge table to reflect the edges new xcoord.
00181  *     This list must be sorted at each scanline in case
00182  *     two edges intersect.
00183  *     We also keep a data structure known as the Edge Table (ET),
00184  *     which keeps track of all the edges which the current
00185  *     scanline has not yet reached.  The ET is basically a
00186  *     list of ScanLineList structures containing a list of
00187  *     edges which are entered at a given scanline.  There is one
00188  *     ScanLineList per scanline at which an edge is entered.
00189  *     When we enter a new edge, we move it from the ET to the AET.
00190  *
00191  *     From the AET, we can implement the even-odd rule as in
00192  *     (Foley/Van Dam).
00193  *     The winding number rule is a little trickier.  We also
00194  *     keep the EdgeTableEntries in the AET linked by the
00195  *     nextWETE (winding EdgeTableEntry) link.  This allows
00196  *     the edges to be linked just as before for updating
00197  *     purposes, but only uses the edges linked by the nextWETE
00198  *     link as edges representing spans of the polygon to
00199  *     drawn (as with the even-odd rule).
00200  */
00201 
00202 /*
00203  * for the winding number rule
00204  */
00205 #define CLOCKWISE          1
00206 #define COUNTERCLOCKWISE  -1 
00207 
00208 typedef struct _EdgeTableEntry {
00209      int ymax;             /* ycoord at which we exit this edge. */
00210      BRESINFO bres;        /* Bresenham info to run the edge     */
00211      struct _EdgeTableEntry *next;       /* next in the list     */
00212      struct _EdgeTableEntry *back;       /* for insertion sort   */
00213      struct _EdgeTableEntry *nextWETE;   /* for winding num rule */
00214      int ClockWise;        /* flag for winding number rule       */
00215 } EdgeTableEntry;
00216 
00217 
00218 typedef struct _ScanLineList{
00219      int scanline;              /* the scanline represented */
00220      EdgeTableEntry *edgelist;  /* header node              */
00221      struct _ScanLineList *next;  /* next in the list       */
00222 } ScanLineList;
00223 
00224 
00225 typedef struct {
00226      int ymax;                 /* ymax for the polygon     */
00227      int ymin;                 /* ymin for the polygon     */
00228      ScanLineList scanlines;   /* header node              */
00229 } EdgeTable;
00230 
00231 
00232 /*
00233  * Here is a struct to help with storage allocation
00234  * so we can allocate a big chunk at a time, and then take
00235  * pieces from this heap when we need to.
00236  */
00237 #define SLLSPERBLOCK 25
00238 
00239 typedef struct _ScanLineListBlock {
00240      ScanLineList SLLs[SLLSPERBLOCK];
00241      struct _ScanLineListBlock *next;
00242 } ScanLineListBlock;
00243 
00244 
00245 
00246 /*
00247  *
00248  *     a few macros for the inner loops of the fill code where
00249  *     performance considerations don't allow a procedure call.
00250  *
00251  *     Evaluate the given edge at the given scanline.
00252  *     If the edge has expired, then we leave it and fix up
00253  *     the active edge table; otherwise, we increment the
00254  *     x value to be ready for the next scanline.
00255  *     The winding number rule is in effect, so we must notify
00256  *     the caller when the edge has been removed so he
00257  *     can reorder the Winding Active Edge Table.
00258  */
00259 #define EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) { \
00260    if (pAET->ymax == y) {          /* leaving this edge */ \
00261       pPrevAET->next = pAET->next; \
00262       pAET = pPrevAET->next; \
00263       fixWAET = 1; \
00264       if (pAET) \
00265          pAET->back = pPrevAET; \
00266    } \
00267    else { \
00268       BRESINCRPGONSTRUCT(pAET->bres); \
00269       pPrevAET = pAET; \
00270       pAET = pAET->next; \
00271    } \
00272 }
00273 
00274 
00275 /*
00276  *     Evaluate the given edge at the given scanline.
00277  *     If the edge has expired, then we leave it and fix up
00278  *     the active edge table; otherwise, we increment the
00279  *     x value to be ready for the next scanline.
00280  *     The even-odd rule is in effect.
00281  */
00282 #define EVALUATEEDGEEVENODD(pAET, pPrevAET, y) { \
00283    if (pAET->ymax == y) {          /* leaving this edge */ \
00284       pPrevAET->next = pAET->next; \
00285       pAET = pPrevAET->next; \
00286       if (pAET) \
00287          pAET->back = pPrevAET; \
00288    } \
00289    else { \
00290       BRESINCRPGONSTRUCT(pAET->bres); \
00291       pPrevAET = pAET; \
00292       pAET = pAET->next; \
00293    } \
00294 }

Generated Fri Mar 5 08:11:00 2004.
Copyright © 1998-2003 by the respective authors.

This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.