]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/regiong.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/region.cpp
3 // Purpose: generic wxRegion class
4 // Author: David Elliott
8 // Copyright: (c) 2004 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
20 #include "wx/region.h"
26 // ========================================================================
27 // Classes to interface with X.org code
28 // ========================================================================
32 wxCoord x1
, x2
, y1
, y2
;
33 } Box
, BOX
, BoxRec
, *BoxPtr
;
35 typedef struct REGION
*Region
;
40 // Default constructor initializes nothing
43 REGION(const wxRect
& rect
)
49 extents
.x2
= rect
.x
+ rect
.width
;
50 extents
.y2
= rect
.y
+ rect
.height
;
56 return i
< numRects
? rects
+ i
: NULL
;
63 static bool XOffsetRegion(
64 register Region pRegion
,
67 static bool XIntersectRegion(
69 Region reg2
, /* source regions */
70 register Region newReg
); /* destination Region */
71 static bool XUnionRegion(
73 Region reg2
, /* source regions */
74 Region newReg
); /* destination Region */
75 static bool XSubtractRegion(
78 register Region regD
);
79 static bool XXorRegion(Region sra
, Region srb
, Region dr
);
80 static bool XEmptyRegion(
82 static bool XEqualRegion(Region r1
, Region r2
);
83 static bool XPointInRegion(
86 static wxRegionContain
XRectInRegion(
87 register Region region
,
89 unsigned int rwidth
, unsigned int rheight
);
92 static Region
XCreateRegion(void);
93 static void miSetExtents (
95 static bool XDestroyRegion(Region r
);
96 static int miIntersectO (
104 static void miRegionCopy(
105 register Region dstrgn
,
106 register Region rgn
);
107 static int miCoalesce(
108 register Region pReg
, /* Region to coalesce */
109 int prevStart
, /* Index of start of previous band */
110 int curStart
); /* Index of start of current band */
111 static void miRegionOp(
112 register Region newReg
, /* Place to store result */
113 Region reg1
, /* First region in operation */
114 Region reg2
, /* 2d region in operation */
116 register Region pReg
,
122 wxCoord y2
), /* Function to call for over-
124 int (*nonOverlap1Func
)(
125 register Region pReg
,
129 register wxCoord y2
), /* Function to call for non-
130 * overlapping bands in region
132 int (*nonOverlap2Func
)(
133 register Region pReg
,
137 register wxCoord y2
)); /* Function to call for non-
138 * overlapping bands in region
140 static int miUnionNonO (
141 register Region pReg
,
145 register wxCoord y2
);
146 static int miUnionO (
147 register Region pReg
,
153 register wxCoord y2
);
154 static int miSubtractNonO1 (
155 register Region pReg
,
159 register wxCoord y2
);
160 static int miSubtractO (
161 register Region pReg
,
167 register wxCoord y2
);
175 // ========================================================================
177 // ========================================================================
179 class wxRegionRefData
: public wxGDIRefData
,
189 rects
= ( BOX
* )malloc( (unsigned) sizeof( BOX
));
196 wxRegionRefData(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
200 rects
= (BOX
*)malloc(sizeof(BOX
));
203 extents
.x1
= topLeft
.x
;
204 extents
.y1
= topLeft
.y
;
205 extents
.x2
= bottomRight
.x
;
206 extents
.y2
= bottomRight
.y
;
210 wxRegionRefData(const wxRect
& rect
)
214 rects
= (BOX
*)malloc(sizeof(BOX
));
218 wxRegionRefData(const wxRegionRefData
& refData
)
223 numRects
= refData
.numRects
;
224 rects
= (Box
*)malloc(numRects
*sizeof(Box
));
225 memcpy(rects
, refData
.rects
, numRects
*sizeof(Box
));
226 extents
= refData
.extents
;
229 virtual ~wxRegionRefData()
236 wxRegionRefData(const REGION
&);
239 // ========================================================================
241 // ========================================================================
242 //IMPLEMENT_DYNAMIC_CLASS(wxRegionGeneric, wxGDIObject)
244 #define M_REGIONDATA ((wxRegionRefData *)m_refData)
245 #define M_REGIONDATA_OF(rgn) ((wxRegionRefData *)(rgn.m_refData))
247 // ----------------------------------------------------------------------------
248 // wxRegionGeneric construction
249 // ----------------------------------------------------------------------------
251 wxRegionGeneric::wxRegionGeneric()
255 wxRegionGeneric::~wxRegionGeneric()
259 wxRegionGeneric::wxRegionGeneric(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
261 m_refData
= new wxRegionRefData(wxRect(x
,y
,w
,h
));
264 wxRegionGeneric::wxRegionGeneric(const wxRect
& rect
)
266 m_refData
= new wxRegionRefData(rect
);
269 wxRegionGeneric::wxRegionGeneric(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
271 m_refData
= new wxRegionRefData(topLeft
, bottomRight
);
274 void wxRegionGeneric::Clear()
278 m_refData
= new wxRegionRefData(wxRect(0,0,0,0));
281 wxGDIRefData
*wxRegionGeneric::CreateGDIRefData() const
283 return new wxRegionRefData
;
286 wxGDIRefData
*wxRegionGeneric::CloneGDIRefData(const wxGDIRefData
*data
) const
288 return new wxRegionRefData(*(wxRegionRefData
*)data
);
291 bool wxRegionGeneric::DoIsEqual(const wxRegion
& region
) const
293 return REGION::XEqualRegion(M_REGIONDATA
,M_REGIONDATA_OF(region
));
296 bool wxRegionGeneric::DoGetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
&w
, wxCoord
&h
) const
302 REGION::XClipBox(M_REGIONDATA
,&rect
);
310 // ----------------------------------------------------------------------------
311 // wxRegionGeneric operations
312 // ----------------------------------------------------------------------------
314 bool wxRegionGeneric::DoUnionWithRect(const wxRect
& rect
)
316 if ( rect
.IsEmpty() )
324 return REGION::XUnionRegion(®ion
,M_REGIONDATA
,M_REGIONDATA
);
327 bool wxRegionGeneric::DoUnionWithRegion(const wxRegion
& region
)
330 return REGION::XUnionRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
333 bool wxRegionGeneric::DoIntersect(const wxRegion
& region
)
336 return REGION::XIntersectRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
339 bool wxRegionGeneric::DoSubtract(const wxRegion
& region
)
341 if ( region
.IsEmpty() )
347 return REGION::XSubtractRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
350 bool wxRegionGeneric::DoXor(const wxRegion
& region
)
353 return REGION::XXorRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
356 bool wxRegionGeneric::DoOffset(wxCoord x
, wxCoord y
)
359 return REGION::XOffsetRegion(M_REGIONDATA
, x
, y
);
362 // ----------------------------------------------------------------------------
363 // wxRegionGeneric comparison
364 // ----------------------------------------------------------------------------
366 bool wxRegionGeneric::IsEmpty() const
369 return REGION::XEmptyRegion(M_REGIONDATA
);
372 // Does the region contain the point (x,y)?
373 wxRegionContain
wxRegionGeneric::DoContainsPoint(wxCoord x
, wxCoord y
) const
376 return REGION::XPointInRegion(M_REGIONDATA
,x
,y
) ? wxInRegion
: wxOutRegion
;
379 // Does the region contain the rectangle rect?
380 wxRegionContain
wxRegionGeneric::DoContainsRect(const wxRect
& rect
) const
383 return REGION::XRectInRegion(M_REGIONDATA
,rect
.x
,rect
.y
,rect
.width
,rect
.height
);
386 // ========================================================================
387 // wxRegionIteratorGeneric
388 // ========================================================================
389 //IMPLEMENT_DYNAMIC_CLASS(wxRegionIteratorGeneric,wxObject)
391 wxRegionIteratorGeneric::wxRegionIteratorGeneric()
396 wxRegionIteratorGeneric::wxRegionIteratorGeneric(const wxRegionGeneric
& region
)
402 wxRegionIteratorGeneric::wxRegionIteratorGeneric(const wxRegionIteratorGeneric
& iterator
)
403 : m_region(iterator
.m_region
)
405 m_current
= iterator
.m_current
;
408 void wxRegionIteratorGeneric::Reset(const wxRegionGeneric
& region
)
414 bool wxRegionIteratorGeneric::HaveRects() const
416 return M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
419 wxRegionIteratorGeneric
& wxRegionIteratorGeneric::operator++()
425 wxRegionIteratorGeneric
wxRegionIteratorGeneric::operator++(int)
427 wxRegionIteratorGeneric
copy(*this);
432 wxRect
wxRegionIteratorGeneric::GetRect() const
434 wxASSERT(m_region
.m_refData
);
435 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
445 long wxRegionIteratorGeneric::GetX() const
447 wxASSERT(m_region
.m_refData
);
448 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
453 long wxRegionIteratorGeneric::GetY() const
455 wxASSERT(m_region
.m_refData
);
456 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
461 long wxRegionIteratorGeneric::GetW() const
463 wxASSERT(m_region
.m_refData
);
464 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
466 return box
->x2
- box
->x1
;
469 long wxRegionIteratorGeneric::GetH() const
471 wxASSERT(m_region
.m_refData
);
472 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
474 return box
->y2
- box
->y1
;
477 wxRegionIteratorGeneric::~wxRegionIteratorGeneric()
482 // ========================================================================
483 // The guts (from X.org)
484 // ========================================================================
486 /************************************************************************
488 Copyright 1987, 1988, 1998 The Open Group
490 Permission to use, copy, modify, distribute, and sell this software and its
491 documentation for any purpose is hereby granted without fee, provided that
492 the above copyright notice appear in all copies and that both that
493 copyright notice and this permission notice appear in supporting
496 The above copyright notice and this permission notice shall be included in
497 all copies or substantial portions of the Software.
499 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
500 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
501 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
502 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
503 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
504 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
506 Except as contained in this notice, the name of The Open Group shall not be
507 used in advertising or otherwise to promote the sale, use or other dealings
508 in this Software without prior written authorization from The Open Group.
511 Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
515 Permission to use, copy, modify, and distribute this software and its
516 documentation for any purpose and without fee is hereby granted,
517 provided that the above copyright notice appear in all copies and that
518 both that copyright notice and this permission notice appear in
519 supporting documentation, and that the name of Digital not be
520 used in advertising or publicity pertaining to distribution of the
521 software without specific, written prior permission.
523 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
524 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
525 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
526 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
527 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
528 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
531 ************************************************************************/
533 /* 1 if two BOXs overlap.
534 * 0 if two BOXs do not overlap.
535 * Remember, x2 and y2 are not in the region
537 #define EXTENTCHECK(r1, r2) \
538 ((r1)->x2 > (r2)->x1 && \
539 (r1)->x1 < (r2)->x2 && \
540 (r1)->y2 > (r2)->y1 && \
544 * Check to see if there is enough memory in the present region.
546 #define MEMCHECK(reg, rect, firstrect){\
547 if ((reg)->numRects >= ((reg)->size - 1)){\
548 (firstrect) = (BOX *) realloc \
549 ((char *)(firstrect), (unsigned) (2 * (sizeof(BOX)) * ((reg)->size)));\
550 if ((firstrect) == 0)\
553 (rect) = &(firstrect)[(reg)->numRects];\
557 #define EMPTY_REGION(pReg) pReg->numRects = 0
559 #define REGION_NOT_EMPTY(pReg) pReg->numRects
561 #define INBOX(r, x, y) \
562 ( ( ((r).x2 > x)) && \
563 ( ((r).x1 <= x)) && \
568 * The functions in this file implement the Region abstraction, similar to one
569 * used in the X11 sample server. A Region is simply an area, as the name
570 * implies, and is implemented as a "y-x-banded" array of rectangles. To
571 * explain: Each Region is made up of a certain number of rectangles sorted
572 * by y coordinate first, and then by x coordinate.
574 * Furthermore, the rectangles are banded such that every rectangle with a
575 * given upper-left y coordinate (y1) will have the same lower-right y
576 * coordinate (y2) and vice versa. If a rectangle has scanlines in a band, it
577 * will span the entire vertical distance of the band. This means that some
578 * areas that could be merged into a taller rectangle will be represented as
579 * several shorter rectangles to account for shorter rectangles to its left
580 * or right but within its "vertical scope".
582 * An added constraint on the rectangles is that they must cover as much
583 * horizontal area as possible. E.g. no two rectangles in a band are allowed
586 * Whenever possible, bands will be merged together to cover a greater vertical
587 * distance (and thus reduce the number of rectangles). Two bands can be merged
588 * only if the bottom of one touches the top of the other and they have
589 * rectangles in the same places (of the same width, of course). This maintains
590 * the y-x-banding that's so nice to have...
593 /* Create a new empty region */
594 Region
REGION::XCreateRegion(void)
596 Region temp
= new REGION
;
599 return (Region
) NULL
;
601 temp
->rects
= ( BOX
* )malloc( (unsigned) sizeof( BOX
));
606 return (Region
) NULL
;
609 temp
->extents
.x1
= 0;
610 temp
->extents
.y1
= 0;
611 temp
->extents
.x2
= 0;
612 temp
->extents
.y2
= 0;
617 bool REGION::XClipBox(Region r
, wxRect
*rect
)
619 rect
->x
= r
->extents
.x1
;
620 rect
->y
= r
->extents
.y1
;
621 rect
->width
= r
->extents
.x2
- r
->extents
.x1
;
622 rect
->height
= r
->extents
.y2
- r
->extents
.y1
;
627 *-----------------------------------------------------------------------
629 * Reset the extents of a region to what they should be. Called by
630 * miSubtract and miIntersect b/c they can't figure it out along the
631 * way or do so easily, as miUnion can.
637 * The region's 'extents' structure is overwritten.
639 *-----------------------------------------------------------------------
642 miSetExtents (Region pReg
)
644 register BoxPtr pBox
,
648 if (pReg
->numRects
== 0)
650 pReg
->extents
.x1
= 0;
651 pReg
->extents
.y1
= 0;
652 pReg
->extents
.x2
= 0;
653 pReg
->extents
.y2
= 0;
657 pExtents
= &pReg
->extents
;
659 pBoxEnd
= &pBox
[pReg
->numRects
- 1];
662 * Since pBox is the first rectangle in the region, it must have the
663 * smallest y1 and since pBoxEnd is the last rectangle in the region,
664 * it must have the largest y2, because of banding. Initialize x1 and
665 * x2 from pBox and pBoxEnd, resp., as good things to initialize them
668 pExtents
->x1
= pBox
->x1
;
669 pExtents
->y1
= pBox
->y1
;
670 pExtents
->x2
= pBoxEnd
->x2
;
671 pExtents
->y2
= pBoxEnd
->y2
;
673 assert(pExtents
->y1
< pExtents
->y2
);
674 while (pBox
<= pBoxEnd
)
676 if (pBox
->x1
< pExtents
->x1
)
678 pExtents
->x1
= pBox
->x1
;
680 if (pBox
->x2
> pExtents
->x2
)
682 pExtents
->x2
= pBox
->x2
;
686 assert(pExtents
->x1
< pExtents
->x2
);
693 free( (char *) r
->rects
);
698 /* TranslateRegion(pRegion, x, y)
705 register Region pRegion
,
712 pbox
= pRegion
->rects
;
713 nbox
= pRegion
->numRects
;
723 pRegion
->extents
.x1
+= x
;
724 pRegion
->extents
.x2
+= x
;
725 pRegion
->extents
.y1
+= y
;
726 pRegion
->extents
.y2
+= y
;
730 /*======================================================================
731 * Region Intersection
732 *====================================================================*/
734 *-----------------------------------------------------------------------
736 * Handle an overlapping band for miIntersect.
742 * Rectangles may be added to the region.
744 *-----------------------------------------------------------------------
749 register Region pReg
,
759 register BoxPtr pNextRect
;
761 pNextRect
= &pReg
->rects
[pReg
->numRects
];
763 while ((r1
!= r1End
) && (r2
!= r2End
))
765 x1
= wxMax(r1
->x1
,r2
->x1
);
766 x2
= wxMin(r1
->x2
,r2
->x2
);
769 * If there's any overlap between the two rectangles, add that
770 * overlap to the new region.
771 * There's no need to check for subsumption because the only way
772 * such a need could arise is if some region has two rectangles
773 * right next to each other. Since that should never happen...
779 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
786 assert(pReg
->numRects
<= pReg
->size
);
790 * Need to advance the pointers. Shift the one that extends
791 * to the right the least, since the other still has a chance to
792 * overlap with that region's next rectangle, if you see what I mean.
798 else if (r2
->x2
< r1
->x2
)
814 Region reg2
, /* source regions */
815 register Region newReg
) /* destination Region */
817 /* check for trivial reject */
818 if ( (!(reg1
->numRects
)) || (!(reg2
->numRects
)) ||
819 (!EXTENTCHECK(®1
->extents
, ®2
->extents
)))
820 newReg
->numRects
= 0;
822 miRegionOp (newReg
, reg1
, reg2
,
823 miIntersectO
, NULL
, NULL
);
826 * Can't alter newReg's extents before we call miRegionOp because
827 * it might be one of the source regions and miRegionOp depends
828 * on the extents of those regions being the same. Besides, this
829 * way there's no checking against rectangles that will be nuked
830 * due to coalescing, so we have to examine fewer rectangles.
832 miSetExtents(newReg
);
838 register Region dstrgn
,
842 if (dstrgn
!= rgn
) /* don't want to copy to itself */
844 if (dstrgn
->size
< rgn
->numRects
)
848 BOX
*prevRects
= dstrgn
->rects
;
850 dstrgn
->rects
= (BOX
*)
851 realloc((char *) dstrgn
->rects
,
852 (unsigned) rgn
->numRects
* (sizeof(BOX
)));
859 dstrgn
->size
= rgn
->numRects
;
861 dstrgn
->numRects
= rgn
->numRects
;
862 dstrgn
->extents
.x1
= rgn
->extents
.x1
;
863 dstrgn
->extents
.y1
= rgn
->extents
.y1
;
864 dstrgn
->extents
.x2
= rgn
->extents
.x2
;
865 dstrgn
->extents
.y2
= rgn
->extents
.y2
;
867 memcpy((char *) dstrgn
->rects
, (char *) rgn
->rects
,
868 (int) (rgn
->numRects
* sizeof(BOX
)));
872 /*======================================================================
873 * Generic Region Operator
874 *====================================================================*/
877 *-----------------------------------------------------------------------
879 * Attempt to merge the boxes in the current band with those in the
880 * previous one. Used only by miRegionOp.
883 * The new index for the previous band.
886 * If coalescing takes place:
887 * - rectangles in the previous band will have their y2 fields
889 * - pReg->numRects will be decreased.
891 *-----------------------------------------------------------------------
896 register Region pReg
, /* Region to coalesce */
897 int prevStart
, /* Index of start of previous band */
898 int curStart
) /* Index of start of current band */
900 register BoxPtr pPrevBox
; /* Current box in previous band */
901 register BoxPtr pCurBox
; /* Current box in current band */
902 register BoxPtr pRegEnd
; /* End of region */
903 int curNumRects
; /* Number of rectangles in current
905 int prevNumRects
; /* Number of rectangles in previous
907 int bandY1
; /* Y1 coordinate for current band */
909 pRegEnd
= &pReg
->rects
[pReg
->numRects
];
911 pPrevBox
= &pReg
->rects
[prevStart
];
912 prevNumRects
= curStart
- prevStart
;
915 * Figure out how many rectangles are in the current band. Have to do
916 * this because multiple bands could have been added in miRegionOp
917 * at the end when one region has been exhausted.
919 pCurBox
= &pReg
->rects
[curStart
];
920 bandY1
= pCurBox
->y1
;
921 for (curNumRects
= 0;
922 (pCurBox
!= pRegEnd
) && (pCurBox
->y1
== bandY1
);
928 if (pCurBox
!= pRegEnd
)
931 * If more than one band was added, we have to find the start
932 * of the last band added so the next coalescing job can start
933 * at the right place... (given when multiple bands are added,
934 * this may be pointless -- see above).
937 while (pRegEnd
[-1].y1
== pRegEnd
->y1
)
941 curStart
= pRegEnd
- pReg
->rects
;
942 pRegEnd
= pReg
->rects
+ pReg
->numRects
;
945 if ((curNumRects
== prevNumRects
) && (curNumRects
!= 0))
947 pCurBox
-= curNumRects
;
949 * The bands may only be coalesced if the bottom of the previous
950 * matches the top scanline of the current.
952 if (pPrevBox
->y2
== pCurBox
->y1
)
955 * Make sure the bands have boxes in the same places. This
956 * assumes that boxes have been added in such a way that they
957 * cover the most area possible. I.e. two boxes in a band must
958 * have some horizontal space between them.
962 if ((pPrevBox
->x1
!= pCurBox
->x1
) ||
963 (pPrevBox
->x2
!= pCurBox
->x2
))
966 * The bands don't line up so they can't be coalesced.
973 } while (prevNumRects
!= 0);
975 pReg
->numRects
-= curNumRects
;
976 pCurBox
-= curNumRects
;
977 pPrevBox
-= curNumRects
;
980 * The bands may be merged, so set the bottom y of each box
981 * in the previous band to that of the corresponding box in
986 pPrevBox
->y2
= pCurBox
->y2
;
990 } while (curNumRects
!= 0);
993 * If only one band was added to the region, we have to backup
994 * curStart to the start of the previous band.
996 * If more than one band was added to the region, copy the
997 * other bands down. The assumption here is that the other bands
998 * came from the same region as the current one and no further
999 * coalescing can be done on them since it's all been done
1000 * already... curStart is already in the right place.
1002 if (pCurBox
== pRegEnd
)
1004 curStart
= prevStart
;
1010 *pPrevBox
++ = *pCurBox
++;
1011 } while (pCurBox
!= pRegEnd
);
1020 *-----------------------------------------------------------------------
1022 * Apply an operation to two regions. Called by miUnion, miInverse,
1023 * miSubtract, miIntersect...
1029 * The new region is overwritten.
1032 * The idea behind this function is to view the two regions as sets.
1033 * Together they cover a rectangle of area that this function divides
1034 * into horizontal bands where points are covered only by one region
1035 * or by both. For the first case, the nonOverlapFunc is called with
1036 * each the band and the band's upper and lower extents. For the
1037 * second, the overlapFunc is called to process the entire band. It
1038 * is responsible for clipping the rectangles in the band, though
1039 * this function provides the boundaries.
1040 * At the end of each band, the new region is coalesced, if possible,
1041 * to reduce the number of rectangles in the region.
1043 *-----------------------------------------------------------------------
1048 register Region newReg
, /* Place to store result */
1049 Region reg1
, /* First region in operation */
1050 Region reg2
, /* 2d region in operation */
1052 register Region pReg
,
1058 wxCoord y2
), /* Function to call for over-
1060 int (*nonOverlap1Func
)(
1061 register Region pReg
,
1064 register wxCoord y1
,
1065 register wxCoord y2
), /* Function to call for non-
1066 * overlapping bands in region
1068 int (*nonOverlap2Func
)(
1069 register Region pReg
,
1072 register wxCoord y1
,
1073 register wxCoord y2
)) /* Function to call for non-
1074 * overlapping bands in region
1077 register BoxPtr r1
; /* Pointer into first region */
1078 register BoxPtr r2
; /* Pointer into 2d region */
1079 BoxPtr r1End
; /* End of 1st region */
1080 BoxPtr r2End
; /* End of 2d region */
1081 register wxCoord ybot
; /* Bottom of intersection */
1082 register wxCoord ytop
; /* Top of intersection */
1083 BoxPtr oldRects
; /* Old rects for newReg */
1084 int prevBand
; /* Index of start of
1085 * previous band in newReg */
1086 int curBand
; /* Index of start of current
1088 register BoxPtr r1BandEnd
; /* End of current band in r1 */
1089 register BoxPtr r2BandEnd
; /* End of current band in r2 */
1090 wxCoord top
; /* Top of non-overlapping
1092 wxCoord bot
; /* Bottom of non-overlapping
1097 * set r1, r2, r1End and r2End appropriately, preserve the important
1098 * parts of the destination region until the end in case it's one of
1099 * the two source regions, then mark the "new" region empty, allocating
1100 * another array of rectangles for it to use.
1104 r1End
= r1
+ reg1
->numRects
;
1105 r2End
= r2
+ reg2
->numRects
;
1107 oldRects
= newReg
->rects
;
1109 EMPTY_REGION(newReg
);
1112 * Allocate a reasonable number of rectangles for the new region. The idea
1113 * is to allocate enough so the individual functions don't need to
1114 * reallocate and copy the array, which is time consuming, yet we don't
1115 * have to worry about using too much memory. I hope to be able to
1116 * nuke the realloc() at the end of this function eventually.
1118 newReg
->size
= wxMax(reg1
->numRects
,reg2
->numRects
) * 2;
1120 newReg
->rects
= (BoxPtr
)malloc((unsigned) (sizeof(BoxRec
) * newReg
->size
));
1129 * Initialize ybot and ytop.
1130 * In the upcoming loop, ybot and ytop serve different functions depending
1131 * on whether the band being handled is an overlapping or non-overlapping
1133 * In the case of a non-overlapping band (only one of the regions
1134 * has points in the band), ybot is the bottom of the most recent
1135 * intersection and thus clips the top of the rectangles in that band.
1136 * ytop is the top of the next intersection between the two regions and
1137 * serves to clip the bottom of the rectangles in the current band.
1138 * For an overlapping band (where the two regions intersect), ytop clips
1139 * the top of the rectangles of both regions and ybot clips the bottoms.
1141 if (reg1
->extents
.y1
< reg2
->extents
.y1
)
1142 ybot
= reg1
->extents
.y1
;
1144 ybot
= reg2
->extents
.y1
;
1147 * prevBand serves to mark the start of the previous band so rectangles
1148 * can be coalesced into larger rectangles. qv. miCoalesce, above.
1149 * In the beginning, there is no previous band, so prevBand == curBand
1150 * (curBand is set later on, of course, but the first band will always
1151 * start at index 0). prevBand and curBand must be indices because of
1152 * the possible expansion, and resultant moving, of the new region's
1153 * array of rectangles.
1159 curBand
= newReg
->numRects
;
1162 * This algorithm proceeds one source-band (as opposed to a
1163 * destination band, which is determined by where the two regions
1164 * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
1165 * rectangle after the last one in the current band for their
1166 * respective regions.
1169 while ((r1BandEnd
!= r1End
) && (r1BandEnd
->y1
== r1
->y1
))
1175 while ((r2BandEnd
!= r2End
) && (r2BandEnd
->y1
== r2
->y1
))
1181 * First handle the band that doesn't intersect, if any.
1183 * Note that attention is restricted to one band in the
1184 * non-intersecting region at once, so if a region has n
1185 * bands between the current position and the next place it overlaps
1186 * the other, this entire loop will be passed through n times.
1188 if (r1
->y1
< r2
->y1
)
1190 top
= wxMax(r1
->y1
,ybot
);
1191 bot
= wxMin(r1
->y2
,r2
->y1
);
1193 if ((top
!= bot
) && (nonOverlap1Func
!= NULL
))
1195 (* nonOverlap1Func
) (newReg
, r1
, r1BandEnd
, top
, bot
);
1200 else if (r2
->y1
< r1
->y1
)
1202 top
= wxMax(r2
->y1
,ybot
);
1203 bot
= wxMin(r2
->y2
,r1
->y1
);
1205 if ((top
!= bot
) && (nonOverlap2Func
!= NULL
))
1207 (* nonOverlap2Func
) (newReg
, r2
, r2BandEnd
, top
, bot
);
1218 * If any rectangles got added to the region, try and coalesce them
1219 * with rectangles from the previous band. Note we could just do
1220 * this test in miCoalesce, but some machines incur a not
1221 * inconsiderable cost for function calls, so...
1223 if (newReg
->numRects
!= curBand
)
1225 prevBand
= miCoalesce (newReg
, prevBand
, curBand
);
1229 * Now see if we've hit an intersecting band. The two bands only
1230 * intersect if ybot > ytop
1232 ybot
= wxMin(r1
->y2
, r2
->y2
);
1233 curBand
= newReg
->numRects
;
1236 (* overlapFunc
) (newReg
, r1
, r1BandEnd
, r2
, r2BandEnd
, ytop
, ybot
);
1240 if (newReg
->numRects
!= curBand
)
1242 prevBand
= miCoalesce (newReg
, prevBand
, curBand
);
1246 * If we've finished with a band (y2 == ybot) we skip forward
1247 * in the region to the next band.
1257 } while ((r1
!= r1End
) && (r2
!= r2End
));
1260 * Deal with whichever region still has rectangles left.
1262 curBand
= newReg
->numRects
;
1265 if (nonOverlap1Func
!= NULL
)
1270 while ((r1BandEnd
< r1End
) && (r1BandEnd
->y1
== r1
->y1
))
1274 (* nonOverlap1Func
) (newReg
, r1
, r1BandEnd
,
1275 wxMax(r1
->y1
,ybot
), r1
->y2
);
1277 } while (r1
!= r1End
);
1280 else if ((r2
!= r2End
) && (nonOverlap2Func
!= NULL
))
1285 while ((r2BandEnd
< r2End
) && (r2BandEnd
->y1
== r2
->y1
))
1289 (* nonOverlap2Func
) (newReg
, r2
, r2BandEnd
,
1290 wxMax(r2
->y1
,ybot
), r2
->y2
);
1292 } while (r2
!= r2End
);
1295 if (newReg
->numRects
!= curBand
)
1297 (void) miCoalesce (newReg
, prevBand
, curBand
);
1301 * A bit of cleanup. To keep regions from growing without bound,
1302 * we shrink the array of rectangles to match the new number of
1303 * rectangles in the region. This never goes to 0, however...
1305 * Only do this stuff if the number of rectangles allocated is more than
1306 * twice the number of rectangles in the region (a simple optimization...).
1308 if (newReg
->numRects
< (newReg
->size
>> 1))
1310 if (REGION_NOT_EMPTY(newReg
))
1312 BoxPtr prev_rects
= newReg
->rects
;
1313 newReg
->size
= newReg
->numRects
;
1314 newReg
->rects
= (BoxPtr
) realloc ((char *) newReg
->rects
,
1315 (unsigned) (sizeof(BoxRec
) * newReg
->size
));
1316 if (! newReg
->rects
)
1317 newReg
->rects
= prev_rects
;
1322 * No point in doing the extra work involved in an realloc if
1323 * the region is empty
1326 free((char *) newReg
->rects
);
1327 newReg
->rects
= (BoxPtr
) malloc(sizeof(BoxRec
));
1330 free ((char *) oldRects
);
1334 /*======================================================================
1336 *====================================================================*/
1339 *-----------------------------------------------------------------------
1341 * Handle a non-overlapping band for the union operation. Just
1342 * Adds the rectangles into the region. Doesn't have to check for
1343 * subsumption or anything.
1349 * pReg->numRects is incremented and the final rectangles overwritten
1350 * with the rectangles we're passed.
1352 *-----------------------------------------------------------------------
1357 register Region pReg
,
1360 register wxCoord y1
,
1361 register wxCoord y2
)
1363 register BoxPtr pNextRect
;
1365 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1371 assert(r
->x1
< r
->x2
);
1372 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1373 pNextRect
->x1
= r
->x1
;
1375 pNextRect
->x2
= r
->x2
;
1377 pReg
->numRects
+= 1;
1380 assert(pReg
->numRects
<=pReg
->size
);
1383 return 0; /* lint */
1388 *-----------------------------------------------------------------------
1390 * Handle an overlapping band for the union operation. Picks the
1391 * left-most rectangle each time and merges it into the region.
1397 * Rectangles are overwritten in pReg->rects and pReg->numRects will
1400 *-----------------------------------------------------------------------
1406 register Region pReg
,
1411 register wxCoord y1
,
1412 register wxCoord y2
)
1414 register BoxPtr pNextRect
;
1416 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1418 #define MERGERECT(r) \
1419 if ((pReg->numRects != 0) && \
1420 (pNextRect[-1].y1 == y1) && \
1421 (pNextRect[-1].y2 == y2) && \
1422 (pNextRect[-1].x2 >= r->x1)) \
1424 if (pNextRect[-1].x2 < r->x2) \
1426 pNextRect[-1].x2 = r->x2; \
1427 assert(pNextRect[-1].x1<pNextRect[-1].x2); \
1432 MEMCHECK(pReg, pNextRect, pReg->rects); \
1433 pNextRect->y1 = y1; \
1434 pNextRect->y2 = y2; \
1435 pNextRect->x1 = r->x1; \
1436 pNextRect->x2 = r->x2; \
1437 pReg->numRects += 1; \
1440 assert(pReg->numRects<=pReg->size);\
1444 while ((r1
!= r1End
) && (r2
!= r2End
))
1446 if (r1
->x1
< r2
->x1
)
1461 } while (r1
!= r1End
);
1463 else while (r2
!= r2End
)
1467 return 0; /* lint */
1473 Region reg2
, /* source regions */
1474 Region newReg
) /* destination Region */
1476 /* checks all the simple cases */
1479 * Region 1 and 2 are the same or region 1 is empty
1481 if ( (reg1
== reg2
) || (!(reg1
->numRects
)) )
1484 miRegionCopy(newReg
, reg2
);
1489 * if nothing to union (region 2 empty)
1491 if (!(reg2
->numRects
))
1494 miRegionCopy(newReg
, reg1
);
1499 * Region 1 completely subsumes region 2
1501 if ((reg1
->numRects
== 1) &&
1502 (reg1
->extents
.x1
<= reg2
->extents
.x1
) &&
1503 (reg1
->extents
.y1
<= reg2
->extents
.y1
) &&
1504 (reg1
->extents
.x2
>= reg2
->extents
.x2
) &&
1505 (reg1
->extents
.y2
>= reg2
->extents
.y2
))
1508 miRegionCopy(newReg
, reg1
);
1513 * Region 2 completely subsumes region 1
1515 if ((reg2
->numRects
== 1) &&
1516 (reg2
->extents
.x1
<= reg1
->extents
.x1
) &&
1517 (reg2
->extents
.y1
<= reg1
->extents
.y1
) &&
1518 (reg2
->extents
.x2
>= reg1
->extents
.x2
) &&
1519 (reg2
->extents
.y2
>= reg1
->extents
.y2
))
1522 miRegionCopy(newReg
, reg2
);
1526 miRegionOp (newReg
, reg1
, reg2
, miUnionO
,
1527 miUnionNonO
, miUnionNonO
);
1529 newReg
->extents
.x1
= wxMin(reg1
->extents
.x1
, reg2
->extents
.x1
);
1530 newReg
->extents
.y1
= wxMin(reg1
->extents
.y1
, reg2
->extents
.y1
);
1531 newReg
->extents
.x2
= wxMax(reg1
->extents
.x2
, reg2
->extents
.x2
);
1532 newReg
->extents
.y2
= wxMax(reg1
->extents
.y2
, reg2
->extents
.y2
);
1537 /*======================================================================
1538 * Region Subtraction
1539 *====================================================================*/
1542 *-----------------------------------------------------------------------
1544 * Deal with non-overlapping band for subtraction. Any parts from
1545 * region 2 we discard. Anything from region 1 we add to the region.
1551 * pReg may be affected.
1553 *-----------------------------------------------------------------------
1558 register Region pReg
,
1561 register wxCoord y1
,
1562 register wxCoord y2
)
1564 register BoxPtr pNextRect
;
1566 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1572 assert(r
->x1
<r
->x2
);
1573 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1574 pNextRect
->x1
= r
->x1
;
1576 pNextRect
->x2
= r
->x2
;
1578 pReg
->numRects
+= 1;
1581 assert(pReg
->numRects
<= pReg
->size
);
1585 return 0; /* lint */
1589 *-----------------------------------------------------------------------
1591 * Overlapping band subtraction. x1 is the left-most point not yet
1598 * pReg may have rectangles added to it.
1600 *-----------------------------------------------------------------------
1605 register Region pReg
,
1610 register wxCoord y1
,
1611 register wxCoord y2
)
1613 register BoxPtr pNextRect
;
1619 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1621 while ((r1
!= r1End
) && (r2
!= r2End
))
1626 * Subtrahend missed the boat: go to next subtrahend.
1630 else if (r2
->x1
<= x1
)
1633 * Subtrahend preceeds minuend: nuke left edge of minuend.
1639 * Minuend completely covered: advance to next minuend and
1640 * reset left fence to edge of new minuend.
1649 * Subtrahend now used up since it doesn't extend beyond
1655 else if (r2
->x1
< r1
->x2
)
1658 * Left part of subtrahend covers part of minuend: add uncovered
1659 * part of minuend to region and skip to next subtrahend.
1662 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1665 pNextRect
->x2
= r2
->x1
;
1667 pReg
->numRects
+= 1;
1670 assert(pReg
->numRects
<=pReg
->size
);
1676 * Minuend used up: advance to new...
1685 * Subtrahend used up
1693 * Minuend used up: add any remaining piece before advancing.
1697 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1700 pNextRect
->x2
= r1
->x2
;
1702 pReg
->numRects
+= 1;
1704 assert(pReg
->numRects
<=pReg
->size
);
1713 * Add remaining minuend rectangles to region.
1718 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1721 pNextRect
->x2
= r1
->x2
;
1723 pReg
->numRects
+= 1;
1726 assert(pReg
->numRects
<=pReg
->size
);
1734 return 0; /* lint */
1738 *-----------------------------------------------------------------------
1740 * Subtract regS from regM and leave the result in regD.
1741 * S stands for subtrahend, M for minuend and D for difference.
1747 * regD is overwritten.
1749 *-----------------------------------------------------------------------
1752 bool REGION::XSubtractRegion(Region regM
, Region regS
, register Region regD
)
1754 /* check for trivial reject */
1755 if ( (!(regM
->numRects
)) || (!(regS
->numRects
)) ||
1756 (!EXTENTCHECK(®M
->extents
, ®S
->extents
)) )
1758 miRegionCopy(regD
, regM
);
1762 miRegionOp (regD
, regM
, regS
, miSubtractO
,
1763 miSubtractNonO1
, NULL
);
1766 * Can't alter newReg's extents before we call miRegionOp because
1767 * it might be one of the source regions and miRegionOp depends
1768 * on the extents of those regions being the unaltered. Besides, this
1769 * way there's no checking against rectangles that will be nuked
1770 * due to coalescing, so we have to examine fewer rectangles.
1772 miSetExtents (regD
);
1776 bool REGION::XXorRegion(Region sra
, Region srb
, Region dr
)
1778 Region tra
= XCreateRegion();
1780 wxCHECK_MSG( tra
, false, wxT("region not created") );
1782 Region trb
= XCreateRegion();
1784 wxCHECK_MSG( trb
, false, wxT("region not created") );
1786 (void) XSubtractRegion(sra
,srb
,tra
);
1787 (void) XSubtractRegion(srb
,sra
,trb
);
1788 (void) XUnionRegion(tra
,trb
,dr
);
1789 XDestroyRegion(tra
);
1790 XDestroyRegion(trb
);
1795 * Check to see if the region is empty. Assumes a region is passed
1798 bool REGION::XEmptyRegion(Region r
)
1800 if( r
->numRects
== 0 ) return true;
1805 * Check to see if two regions are equal
1807 bool REGION::XEqualRegion(Region r1
, Region r2
)
1811 if( r1
->numRects
!= r2
->numRects
) return false;
1812 else if( r1
->numRects
== 0 ) return true;
1813 else if ( r1
->extents
.x1
!= r2
->extents
.x1
) return false;
1814 else if ( r1
->extents
.x2
!= r2
->extents
.x2
) return false;
1815 else if ( r1
->extents
.y1
!= r2
->extents
.y1
) return false;
1816 else if ( r1
->extents
.y2
!= r2
->extents
.y2
) return false;
1817 else for( i
=0; i
< r1
->numRects
; i
++ ) {
1818 if ( r1
->rects
[i
].x1
!= r2
->rects
[i
].x1
) return false;
1819 else if ( r1
->rects
[i
].x2
!= r2
->rects
[i
].x2
) return false;
1820 else if ( r1
->rects
[i
].y1
!= r2
->rects
[i
].y1
) return false;
1821 else if ( r1
->rects
[i
].y2
!= r2
->rects
[i
].y2
) return false;
1826 bool REGION::XPointInRegion(Region pRegion
, int x
, int y
)
1830 if (pRegion
->numRects
== 0)
1832 if (!INBOX(pRegion
->extents
, x
, y
))
1834 for (i
=0; i
<pRegion
->numRects
; i
++)
1836 if (INBOX (pRegion
->rects
[i
], x
, y
))
1842 wxRegionContain
REGION::XRectInRegion(register Region region
,
1844 unsigned int rwidth
,
1845 unsigned int rheight
)
1847 register BoxPtr pbox
;
1848 register BoxPtr pboxEnd
;
1850 register BoxPtr prect
= &rect
;
1851 int partIn
, partOut
;
1855 prect
->x2
= rwidth
+ rx
;
1856 prect
->y2
= rheight
+ ry
;
1858 /* this is (just) a useful optimization */
1859 if ((region
->numRects
== 0) || !EXTENTCHECK(®ion
->extents
, prect
))
1860 return(wxOutRegion
);
1865 /* can stop when both partOut and partIn are true, or we reach prect->y2 */
1866 for (pbox
= region
->rects
, pboxEnd
= pbox
+ region
->numRects
;
1872 continue; /* getting up to speed or skipping remainder of band */
1876 partOut
= true; /* missed part of rectangle above */
1877 if (partIn
|| (pbox
->y1
>= prect
->y2
))
1879 ry
= pbox
->y1
; /* x guaranteed to be == prect->x1 */
1883 continue; /* not far enough over yet */
1887 partOut
= true; /* missed part of rectangle to left */
1892 if (pbox
->x1
< prect
->x2
)
1894 partIn
= true; /* definitely overlap */
1899 if (pbox
->x2
>= prect
->x2
)
1901 ry
= pbox
->y2
; /* finished with this band */
1902 if (ry
>= prect
->y2
)
1904 rx
= prect
->x1
; /* reset x out to left again */
1908 * Because boxes in a band are maximal width, if the first box
1909 * to overlap the rectangle doesn't completely cover it in that
1910 * band, the rectangle must be partially out, since some of it
1911 * will be uncovered in that band. partIn will have been set true
1919 return(partIn
? ((ry
< prect
->y2
) ? wxPartRegion
: wxInRegion
) :