home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / evbl0627.zip / everblue_20010627.zip / x11 / poly.h < prev    next >
Text File  |  1999-11-02  |  11KB  |  297 lines

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