regiong.cpp

来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 1,957 行 · 第 1/4 页

CPP
1,957
字号
{
    wxASSERT(m_region.m_refData);
    const Box *box = M_REGIONDATA_OF(m_region)->GetBox(m_current);
    wxASSERT(box);
    return box->x2 - box->x1;
}

long wxRegionIteratorGeneric::GetH() const
{
    wxASSERT(m_region.m_refData);
    const Box *box = M_REGIONDATA_OF(m_region)->GetBox(m_current);
    wxASSERT(box);
    return box->y2 - box->y1;
}

wxRegionIteratorGeneric::~wxRegionIteratorGeneric()
{
}


// ========================================================================
// The guts (from X.org)
// ========================================================================

/************************************************************************

Copyright 1987, 1988, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.


Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of Digital not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

************************************************************************/

/*  1 if two BOXs overlap.
 *  0 if two BOXs do not overlap.
 *  Remember, x2 and y2 are not in the region
 */
#define EXTENTCHECK(r1, r2) \
    ((r1)->x2 > (r2)->x1 && \
     (r1)->x1 < (r2)->x2 && \
     (r1)->y2 > (r2)->y1 && \
     (r1)->y1 < (r2)->y2)

/*
 *   Check to see if there is enough memory in the present region.
 */
#define MEMCHECK(reg, rect, firstrect){\
        if ((reg)->numRects >= ((reg)->size - 1)){\
          (firstrect) = (BOX *) realloc \
          ((char *)(firstrect), (unsigned) (2 * (sizeof(BOX)) * ((reg)->size)));\
          if ((firstrect) == 0)\
            return(0);\
          (reg)->size *= 2;\
          (rect) = &(firstrect)[(reg)->numRects];\
         }\
       }

#define EMPTY_REGION(pReg) pReg->numRects = 0

#define REGION_NOT_EMPTY(pReg) pReg->numRects

#define INBOX(r, x, y) \
      ( ( ((r).x2 >  x)) && \
        ( ((r).x1 <= x)) && \
        ( ((r).y2 >  y)) && \
        ( ((r).y1 <= y)) )

/*
 * The functions in this file implement the Region abstraction, similar to one
 * used in the X11 sample server. A Region is simply an area, as the name
 * implies, and is implemented as a "y-x-banded" array of rectangles. To
 * explain: Each Region is made up of a certain number of rectangles sorted
 * by y coordinate first, and then by x coordinate.
 *
 * Furthermore, the rectangles are banded such that every rectangle with a
 * given upper-left y coordinate (y1) will have the same lower-right y
 * coordinate (y2) and vice versa. If a rectangle has scanlines in a band, it
 * will span the entire vertical distance of the band. This means that some
 * areas that could be merged into a taller rectangle will be represented as
 * several shorter rectangles to account for shorter rectangles to its left
 * or right but within its "vertical scope".
 *
 * An added constraint on the rectangles is that they must cover as much
 * horizontal area as possible. E.g. no two rectangles in a band are allowed
 * to touch.
 *
 * Whenever possible, bands will be merged together to cover a greater vertical
 * distance (and thus reduce the number of rectangles). Two bands can be merged
 * only if the bottom of one touches the top of the other and they have
 * rectangles in the same places (of the same width, of course). This maintains
 * the y-x-banding that's so nice to have...
 */

/* Create a new empty region */
Region REGION::
XCreateRegion(void)
{
    Region temp;

    if (! (temp = new REGION))
        return (Region) NULL;
    if (! (temp->rects = ( BOX * )malloc( (unsigned) sizeof( BOX )))) {
        free((char *) temp);
        return (Region) NULL;
    }
    temp->numRects = 0;
    temp->extents.x1 = 0;
    temp->extents.y1 = 0;
    temp->extents.x2 = 0;
    temp->extents.y2 = 0;
    temp->size = 1;
    return( temp );
}

bool REGION::
XClipBox(
    Region r,
    wxRect *rect)
{
    rect->x = r->extents.x1;
    rect->y = r->extents.y1;
    rect->width = r->extents.x2 - r->extents.x1;
    rect->height = r->extents.y2 - r->extents.y1;
    return true;
}

/*-
 *-----------------------------------------------------------------------
 * miSetExtents --
 *    Reset the extents of a region to what they should be. Called by
 *    miSubtract and miIntersect b/c they can't figure it out along the
 *    way or do so easily, as miUnion can.
 *
 * Results:
 *    None.
 *
 * Side Effects:
 *    The region's 'extents' structure is overwritten.
 *
 *-----------------------------------------------------------------------
 */
void REGION::
miSetExtents (Region pReg)
{
    register BoxPtr pBox,
                    pBoxEnd,
                    pExtents;

    if (pReg->numRects == 0)
    {
        pReg->extents.x1 = 0;
        pReg->extents.y1 = 0;
        pReg->extents.x2 = 0;
        pReg->extents.y2 = 0;
        return;
    }

    pExtents = &pReg->extents;
    pBox = pReg->rects;
    pBoxEnd = &pBox[pReg->numRects - 1];

    /*
     * Since pBox is the first rectangle in the region, it must have the
     * smallest y1 and since pBoxEnd is the last rectangle in the region,
     * it must have the largest y2, because of banding. Initialize x1 and
     * x2 from  pBox and pBoxEnd, resp., as good things to initialize them
     * to...
     */
    pExtents->x1 = pBox->x1;
    pExtents->y1 = pBox->y1;
    pExtents->x2 = pBoxEnd->x2;
    pExtents->y2 = pBoxEnd->y2;

    assert(pExtents->y1 < pExtents->y2);
    while (pBox <= pBoxEnd)
    {
        if (pBox->x1 < pExtents->x1)
        {
            pExtents->x1 = pBox->x1;
        }
        if (pBox->x2 > pExtents->x2)
        {
            pExtents->x2 = pBox->x2;
        }
        pBox++;
    }
    assert(pExtents->x1 < pExtents->x2);
}

bool REGION::
XDestroyRegion(
    Region r)
{
    free( (char *) r->rects );
    delete r;
    return true;
}

/* TranslateRegion(pRegion, x, y)
   translates in place
   added by raymond
*/

bool REGION::
XOffsetRegion(
    register Region pRegion,
    register int x,
    register int y)
{
    register int nbox;
    register BOX *pbox;

    pbox = pRegion->rects;
    nbox = pRegion->numRects;

    while(nbox--)
    {
        pbox->x1 += x;
        pbox->x2 += x;
        pbox->y1 += y;
        pbox->y2 += y;
        pbox++;
    }
    pRegion->extents.x1 += x;
    pRegion->extents.x2 += x;
    pRegion->extents.y1 += y;
    pRegion->extents.y2 += y;
    return 1;
}

/*======================================================================
 *  Region Intersection
 *====================================================================*/
/*-
 *-----------------------------------------------------------------------
 * miIntersectO --
 *    Handle an overlapping band for miIntersect.
 *
 * Results:
 *    None.
 *
 * Side Effects:
 *    Rectangles may be added to the region.
 *
 *-----------------------------------------------------------------------
 */
/* static void*/
int REGION::
miIntersectO (
    register Region     pReg,
    register BoxPtr     r1,
    BoxPtr              r1End,
    register BoxPtr     r2,
    BoxPtr              r2End,
    wxCoord             y1,
    wxCoord             y2)
{
    register wxCoord    x1;
    register wxCoord    x2;
    register BoxPtr     pNextRect;

    pNextRect = &pReg->rects[pReg->numRects];

    while ((r1 != r1End) && (r2 != r2End))
    {
        x1 = wxMax(r1->x1,r2->x1);
        x2 = wxMin(r1->x2,r2->x2);

        /*
         * If there's any overlap between the two rectangles, add that
         * overlap to the new region.
         * There's no need to check for subsumption because the only way
         * such a need could arise is if some region has two rectangles
         * right next to each other. Since that should never happen...
         */
        if (x1 < x2)
        {
            assert(y1<y2);

            MEMCHECK(pReg, pNextRect, pReg->rects);
            pNextRect->x1 = x1;
            pNextRect->y1 = y1;
            pNextRect->x2 = x2;
            pNextRect->y2 = y2;
            pReg->numRects += 1;
            pNextRect++;
            assert(pReg->numRects <= pReg->size);
        }

        /*
         * Need to advance the pointers. Shift the one that extends
         * to the right the least, since the other still has a chance to
         * overlap with that region's next rectangle, if you see what I mean.
         */
        if (r1->x2 < r2->x2)
        {
            r1++;
        }
        else if (r2->x2 < r1->x2)
        {
            r2++;
        }
        else
        {
            r1++;
            r2++;
        }
    }
    return 0; /* lint */
}

bool REGION::
XIntersectRegion(
    Region reg1,
    Region reg2, /* source regions     */
    register Region newReg) /* destination Region */
{
   /* check for trivial reject */
    if ( (!(reg1->numRects)) || (!(reg2->numRects))  ||
        (!EXTENTCHECK(&reg1->extents, &reg2->extents)))
        newReg->numRects = 0;
    else
        miRegionOp (newReg, reg1, reg2,
                    miIntersectO, NULL, NULL);

    /*
     * Can't alter newReg's extents before we call miRegionOp because
     * it might be one of the source regions and miRegionOp depends
     * on the extents of those regions being the same. Besides, this
     * way there's no checking against rectangles that will be nuked
     * due to coalescing, so we have to examine fewer rectangles.
     */
    miSetExtents(newReg);
    return 1;
}

void REGION::
miRegionCopy(
    register Region dstrgn,
    register Region rgn)

{
    if (dstrgn != rgn) /*  don't want to copy to itself */
    {
        if (dstrgn->size < rgn->numRects)
        {
            if (dstrgn->rects)
            {
                BOX *prevRects = dstrgn->rects;

                if (! (dstrgn->rects = (BOX *)
                       realloc((char *) dstrgn->rects,
                               (unsigned) rgn->numRects * (sizeof(BOX)))))
                {
                    free(prevRects);
                    return;
                }
            }
            dstrgn->size = rgn->numRects;
        }
        dstrgn->numRects = rgn->numRects;
        dstrgn->extents.x1 = rgn->extents.x1;
        dstrgn->extents.y1 = rgn->extents.y1;
        dstrgn->extents.x2 = rgn->extents.x2;
        dstrgn->extents.y2 = rgn->extents.y2;

        memcpy((char *) dstrgn->rects, (char *) rgn->rects,
                (int) (rgn->numRects * sizeof(BOX)));
    }
}

/*======================================================================
 * Generic Region Operator
 *====================================================================*/

/*-
 *-----------------------------------------------------------------------
 * miCoalesce --
 *    Attempt to merge the boxes in the current band with those in the
 *    previous one. Used only by miRegionOp.
 *
 * Results:
 *    The new index for the previous band.
 *
 * Side Effects:
 *    If coalescing takes place:
 *        - rectangles in the previous band will have their y2 fields
 *          altered.
 *        - pReg->numRects will be decreased.
 *
 *-----------------------------------------------------------------------
 */
/* static int*/
int REGION::
miCoalesce(
    register Region pReg,     /* Region to coalesce */
    int prevStart,            /* Index of start of previous band */
    int curStart)             /* Index of start of current band */
{
    register BoxPtr pPrevBox; /* Current box in previous band */
    register BoxPtr pCurBox;  /* Current box in current band */
    register BoxPtr pRegEnd;  /* End of region */
    int         curNumRects;  /* Number of rectangles in current
                               * band */
    int        prevNumRects;  /* Number of rectangles in previous
                               * band */
    int              bandY1;  /* Y1 coordinate for current band */

    pRegEnd = &pReg->rects[pReg->numRects];

    pPrevBox = &pReg->rects[prevStart];
    prevNumRects = curStart - prevStart;

    /*
     * Figure out how many rectangles are in the current band. Have to do
     * this because multiple bands could have been added in miRegionOp
     * at the end when one region has been exhausted.
     */
    pCurBox = &pReg->rects[curStart];
    bandY1 = pCurBox->y1;
    for (curNumRects = 0;
         (pCurBox != pRegEnd) && (pCurBox->y1 == bandY1);
         curNumRects++)
    {
        pCurBox++;
    }

    if (pCurBox != pRegEnd)
    {
        /*
         * If more than one band was added, we have to find the start
         * of the last band added so the next coalescing job can start
         * at the right place... (given when multiple bands are added,
         * this may be pointless -- see above).
         */
        pRegEnd--;
        while (pRegEnd[-1].y1 == pRegEnd->y1)
        {
            pRegEnd--;
        }
        curStart = pRegEnd - pReg->rects;
        pRegEnd = pReg->rects + pReg->numRects;
    }

    if ((curNumRects == prevNumRects) && (curNumRects != 0))
    {
        pCurBox -= curNumRects;
        /*
         * The bands may only be coalesced if the bottom of the previous
         * matches the top scanline of the current.
         */
        if (pPrevBox->y2 == pCurBox->y1)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?