home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / ddx / mi / mispans.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-03  |  10.6 KB  |  416 lines

  1. /***********************************************************
  2. Copyright 1989 by Digital Equipment Corporation, Maynard, Massachusetts,
  3. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Digital or MIT not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* $XConsortium: mispans.c,v 5.1 91/07/03 17:22:37 keith Exp $ */
  26.  
  27. #include "misc.h"
  28. #include "pixmapstr.h"
  29. #include "gcstruct.h"
  30. #include "mispans.h"
  31.  
  32. /*
  33.  
  34. These routines maintain lists of Spans, in order to implement the
  35. ``touch-each-pixel-once'' rules of wide lines and arcs.
  36.  
  37. Written by Joel McCormack, Summer 1989.
  38.  
  39. */
  40.  
  41.  
  42. void miInitSpanGroup(spanGroup)
  43.     SpanGroup *spanGroup;
  44. {
  45.     spanGroup->size = 0;
  46.     spanGroup->count = 0;
  47.     spanGroup->group = NULL;
  48.     spanGroup->ymin = MAXSHORT;
  49.     spanGroup->ymax = MINSHORT;
  50. } /* InitSpanGroup */
  51.  
  52. void miAppendSpans(spanGroup, spans)
  53.     SpanGroup   *spanGroup;
  54.     Spans       *spans;
  55. {
  56.     register    int y;
  57.     register    int spansCount;
  58.  
  59.     spansCount = spans->count;
  60.     if (spansCount > 0) {
  61.     if (spanGroup->size == spanGroup->count) {
  62.         spanGroup->size = (spanGroup->size + 8) * 2;
  63.         spanGroup->group = (Spans *)
  64.         xrealloc(spanGroup->group, sizeof(Spans) * spanGroup->size);
  65.      }
  66.  
  67.     spanGroup->group[spanGroup->count] = *spans;
  68.     (spanGroup->count)++;
  69.     y = spans->points[0].y;
  70.     if (y < spanGroup->ymin) spanGroup->ymin = y;
  71.     y = spans->points[spansCount - 1].y;
  72.     if (y > spanGroup->ymax) spanGroup->ymax = y;
  73.     }
  74.     else
  75.     {
  76.     xfree (spans->points);
  77.     xfree (spans->widths);
  78.     }
  79. } /* AppendSpans */
  80.  
  81. void miFreeSpanGroup(spanGroup)
  82.     SpanGroup   *spanGroup;
  83. {
  84.     if (spanGroup->group != NULL) xfree(spanGroup->group);
  85. }
  86.  
  87. static void QuickSortSpansX(points, widths, numSpans)
  88.     register DDXPointRec    points[];
  89.     register int        widths[];
  90.     register int        numSpans;
  91. {
  92.     register int        x;
  93.     register int        i, j, m;
  94.     register DDXPointPtr    r;
  95.  
  96. /* Always called with numSpans > 1 */
  97. /* Sorts only by x, as all y should be the same */
  98.  
  99. #define ExchangeSpans(a, b)                    \
  100. {                                \
  101.     DDXPointRec     tpt;                    \
  102.     register int    tw;                        \
  103.                                 \
  104.     tpt = points[a]; points[a] = points[b]; points[b] = tpt;    \
  105.     tw = widths[a]; widths[a] = widths[b]; widths[b] = tw;  \
  106. }
  107.  
  108.     do {
  109.     if (numSpans < 9) {
  110.         /* Do insertion sort */
  111.         register int xprev;
  112.  
  113.         xprev = points[0].x;
  114.         i = 1;
  115.         do { /* while i != numSpans */
  116.         x = points[i].x;
  117.         if (xprev > x) {
  118.             /* points[i] is out of order.  Move into proper location. */
  119.             DDXPointRec tpt;
  120.             int        tw, k;
  121.  
  122.             for (j = 0; x >= points[j].x; j++) {}
  123.             tpt = points[i];
  124.             tw  = widths[i];
  125.             for (k = i; k != j; k--) {
  126.             points[k] = points[k-1];
  127.             widths[k] = widths[k-1];
  128.             }
  129.             points[j] = tpt;
  130.             widths[j] = tw;
  131.             x = points[i].x;
  132.         } /* if out of order */
  133.         xprev = x;
  134.         i++;
  135.         } while (i != numSpans);
  136.         return;
  137.     }
  138.  
  139.     /* Choose partition element, stick in location 0 */
  140.     m = numSpans / 2;
  141.     if (points[m].x > points[0].x)        ExchangeSpans(m, 0);
  142.     if (points[m].x > points[numSpans-1].x) ExchangeSpans(m, numSpans-1);
  143.     if (points[m].x > points[0].x)        ExchangeSpans(m, 0);
  144.     x = points[0].x;
  145.  
  146.         /* Partition array */
  147.         i = 0;
  148.         j = numSpans;
  149.         do {
  150.         r = &(points[i]);
  151.         do {
  152.         r++;
  153.         i++;
  154.             } while (i != numSpans && r->x < x);
  155.         r = &(points[j]);
  156.         do {
  157.         r--;
  158.         j--;
  159.             } while (x < r->x);
  160.             if (i < j) ExchangeSpans(i, j);
  161.         } while (i < j);
  162.  
  163.         /* Move partition element back to middle */
  164.         ExchangeSpans(0, j);
  165.  
  166.     /* Recurse */
  167.         if (numSpans-j-1 > 1)
  168.         QuickSortSpansX(&points[j+1], &widths[j+1], numSpans-j-1);
  169.         numSpans = j;
  170.     } while (numSpans > 1);
  171. } /* QuickSortSpans */
  172.  
  173.  
  174. static int UniquifySpansX(spans, newPoints, newWidths)
  175.     Spans            *spans;
  176.     register DDXPointRec    *newPoints;
  177.     register int        *newWidths;
  178. {
  179.     register int newx1, newx2, oldpt, i, y;
  180.     register DDXPointRec    *oldPoints;
  181.     register int        *oldWidths;
  182.     int                *startNewWidths;
  183.  
  184. /* Always called with numSpans > 1 */
  185. /* Uniquify the spans, and stash them into newPoints and newWidths.  Return the
  186.    number of unique spans. */
  187.  
  188.  
  189.     startNewWidths = newWidths;
  190.  
  191.     oldPoints = spans->points;
  192.     oldWidths = spans->widths;
  193.  
  194.     y = oldPoints->y;
  195.     newx1 = oldPoints->x;
  196.     newx2 = newx1 + *oldWidths;
  197.  
  198.     for (i = spans->count-1; i != 0; i--) {
  199.     oldPoints++;
  200.     oldWidths++;
  201.     oldpt = oldPoints->x;
  202.     if (oldpt > newx2) {
  203.         /* Write current span, start a new one */
  204.         newPoints->x = newx1;
  205.         newPoints->y = y;
  206.         *newWidths = newx2 - newx1;
  207.         newPoints++;
  208.         newWidths++;
  209.         newx1 = oldpt;
  210.         newx2 = oldpt + *oldWidths;
  211.     } else {
  212.         /* extend current span, if old extends beyond new */
  213.         oldpt = oldpt + *oldWidths;
  214.         if (oldpt > newx2) newx2 = oldpt;
  215.     }
  216.     } /* for */
  217.  
  218.     /* Write final span */
  219.     newPoints->x = newx1;
  220.     *newWidths = newx2 - newx1;
  221.     newPoints->y = y;
  222.  
  223.     return (newWidths - startNewWidths) + 1;
  224. } /* UniquifySpansX */
  225.  
  226. void
  227. miDisposeSpanGroup (spanGroup)
  228.     SpanGroup    *spanGroup;
  229. {
  230.     int        i;
  231.     Spans   *spans;
  232.  
  233.     for (i = 0; i < spanGroup->count; i++)
  234.     {
  235.     spans = spanGroup->group + i;
  236.     xfree (spans->points);
  237.     xfree (spans->widths);
  238.     }
  239. }
  240.  
  241. void miFillUniqueSpanGroup(pDraw, pGC, spanGroup)
  242.     DrawablePtr pDraw;
  243.     GCPtr    pGC;
  244.     SpanGroup   *spanGroup;
  245. {
  246.     register int    i;
  247.     register Spans  *spans;
  248.     register Spans  *yspans;
  249.     register int    *ysizes;
  250.     register int    ymin, ylength;
  251.  
  252.     /* Outgoing spans for one big call to FillSpans */
  253.     register DDXPointPtr    points;
  254.     register int        *widths;
  255.     register int        count;
  256.  
  257.     if (spanGroup->count == 0) return;
  258.  
  259.     if (spanGroup->count == 1) {
  260.     /* Already should be sorted, unique */
  261.     spans = spanGroup->group;
  262.     (*pGC->ops->FillSpans)
  263.         (pDraw, pGC, spans->count, spans->points, spans->widths, TRUE);
  264.     xfree(spans->points);
  265.     xfree(spans->widths);
  266.     }
  267.     else
  268.     {
  269.     /* Yuck.  Gross.  Radix sort into y buckets, then sort x and uniquify */
  270.     /* This seems to be the fastest thing to do.  I've tried sorting on
  271.        both x and y at the same time rather than creating into all those
  272.        y buckets, but it was somewhat slower. */
  273.  
  274.     ymin    = spanGroup->ymin;
  275.     ylength = spanGroup->ymax - ymin + 1;
  276.  
  277.     /* Allocate Spans for y buckets */
  278.     yspans = (Spans *) xalloc(ylength * sizeof(Spans));
  279.     ysizes = (int *) xalloc(ylength * sizeof (int));
  280.  
  281.     if (!yspans || !ysizes)
  282.     {
  283.         xfree (yspans);
  284.         xfree (ysizes);
  285.         miDisposeSpanGroup (spanGroup);
  286.     }
  287.     
  288.     for (i = 0; i != ylength; i++) {
  289.         ysizes[i]        = 0;
  290.         yspans[i].count  = 0;
  291.         yspans[i].points = NULL;
  292.         yspans[i].widths = NULL;
  293.     }
  294.  
  295.     /* Go through every single span and put it into the correct bucket */
  296.     count = 0;
  297.     for (i = 0, spans = spanGroup->group;
  298.         i != spanGroup->count;
  299.         i++, spans++) {
  300.         int        index;
  301.         int        j;
  302.  
  303.         for (j = 0, points = spans->points, widths = spans->widths;
  304.             j != spans->count;
  305.             j++, points++, widths++) {
  306.         index = points->y - ymin;
  307.         if (index >= 0 && index < ylength) {
  308.             Spans *newspans = &(yspans[index]);
  309.             if (newspans->count == ysizes[index]) {
  310.             DDXPointPtr newpoints;
  311.             int        *newwidths;
  312.             ysizes[index] = (ysizes[index] + 8) * 2;
  313.             newpoints = (DDXPointPtr) xrealloc(
  314.                 newspans->points,
  315.                 ysizes[index] * sizeof(DDXPointRec));
  316.             newwidths = (int *) xrealloc(
  317.                 newspans->widths,
  318.                 ysizes[index] * sizeof(int));
  319.             if (!newpoints || !newwidths)
  320.             {
  321.                 int    i;
  322.  
  323.                 for (i = 0; i < ylength; i++)
  324.                 {
  325.                 xfree (yspans[i].points);
  326.                 xfree (yspans[i].widths);
  327.                 }
  328.                 xfree (yspans);
  329.                 xfree (ysizes);
  330.                 miDisposeSpanGroup (spanGroup);
  331.                 return;
  332.             }
  333.             newspans->points = newpoints;
  334.             newspans->widths = newwidths;
  335.             }
  336.             newspans->points[newspans->count] = *points;
  337.             newspans->widths[newspans->count] = *widths;
  338.             (newspans->count)++;
  339.         } /* if y value of span in range */
  340.         } /* for j through spans */
  341.         count += spans->count;
  342.         xfree(spans->points);
  343.         spans->points = NULL;
  344.         xfree(spans->widths);
  345.         spans->widths = NULL;
  346.     } /* for i thorough Spans */
  347.  
  348.     /* Now sort by x and uniquify each bucket into the final array */
  349.     points = (DDXPointPtr) xalloc(count * sizeof(DDXPointRec));
  350.     widths = (int *)       xalloc(count * sizeof(int));
  351.     if (!points || !widths)
  352.     {
  353.         int    i;
  354.  
  355.         for (i = 0; i < ylength; i++)
  356.         {
  357.         xfree (yspans[i].points);
  358.         xfree (yspans[i].widths);
  359.         }
  360.         xfree (points);
  361.         xfree (widths);
  362.         xfree (yspans);
  363.         xfree (ysizes);
  364.         return;
  365.     }
  366.     count = 0;
  367.     for (i = 0; i != ylength; i++) {
  368.         int ycount = yspans[i].count;
  369.         if (ycount > 0) {
  370.         if (ycount > 1) {
  371.             QuickSortSpansX(yspans[i].points, yspans[i].widths, ycount);
  372.             count += UniquifySpansX
  373.             (&(yspans[i]), &(points[count]), &(widths[count]));
  374.         } else {
  375.             points[count] = yspans[i].points[0];
  376.             widths[count] = yspans[i].widths[0];
  377.             count++;
  378.         }
  379.         xfree(yspans[i].points);
  380.         xfree(yspans[i].widths);
  381.         }
  382.     }
  383.  
  384.     (*pGC->ops->FillSpans) (pDraw, pGC, count, points, widths, TRUE);
  385.     xfree(points);
  386.     xfree(widths);
  387.     xfree(yspans);
  388.     xfree(ysizes);
  389.     }
  390.  
  391.     spanGroup->count = 0;
  392.     spanGroup->ymin = MAXSHORT;
  393.     spanGroup->ymax = MINSHORT;
  394. }
  395.  
  396.  
  397. void miFillSpanGroup(pDraw, pGC, spanGroup)
  398.     DrawablePtr pDraw;
  399.     GCPtr    pGC;
  400.     SpanGroup   *spanGroup;
  401. {
  402.     register int    i;
  403.     register Spans  *spans;
  404.  
  405.     for (i = 0, spans = spanGroup->group; i != spanGroup->count; i++, spans++) {
  406.     (*pGC->ops->FillSpans)
  407.         (pDraw, pGC, spans->count, spans->points, spans->widths, TRUE);
  408.     xfree(spans->points);
  409.     xfree(spans->widths);
  410.     }
  411.  
  412.     spanGroup->count = 0;
  413.     spanGroup->ymin = MAXSHORT;
  414.     spanGroup->ymax = MINSHORT;
  415. } /* FillSpanGroup */
  416.