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 wxRegionGeneric::wxRegionGeneric(const wxBitmap
& bmp
)
276 wxFAIL_MSG("NOT IMPLEMENTED: wxRegionGeneric::wxRegionGeneric(const wxBitmap& bmp)");
279 wxRegionGeneric::wxRegionGeneric(size_t n
, const wxPoint
*points
, wxPolygonFillMode fillStyle
)
281 wxFAIL_MSG("NOT IMPLEMENTED: wxRegionGeneric::wxRegionGeneric(size_t n, const wxPoint *points, wxPolygonFillMode fillStyle)");
284 wxRegionGeneric::wxRegionGeneric(const wxBitmap
& bmp
, const wxColour
& transp
, int tolerance
)
286 wxFAIL_MSG("NOT IMPLEMENTED: wxRegionGeneric::wxRegionGeneric(const wxBitmap& bmp, const wxColour& transp, int tolerance)");
289 void wxRegionGeneric::Clear()
293 m_refData
= new wxRegionRefData(wxRect(0,0,0,0));
296 wxGDIRefData
*wxRegionGeneric::CreateGDIRefData() const
298 return new wxRegionRefData
;
301 wxGDIRefData
*wxRegionGeneric::CloneGDIRefData(const wxGDIRefData
*data
) const
303 return new wxRegionRefData(*(wxRegionRefData
*)data
);
306 bool wxRegionGeneric::DoIsEqual(const wxRegion
& region
) const
308 return REGION::XEqualRegion(M_REGIONDATA
,M_REGIONDATA_OF(region
));
311 bool wxRegionGeneric::DoGetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
&w
, wxCoord
&h
) const
317 REGION::XClipBox(M_REGIONDATA
,&rect
);
325 // ----------------------------------------------------------------------------
326 // wxRegionGeneric operations
327 // ----------------------------------------------------------------------------
329 bool wxRegionGeneric::DoUnionWithRect(const wxRect
& rect
)
331 if ( rect
.IsEmpty() )
339 return REGION::XUnionRegion(®ion
,M_REGIONDATA
,M_REGIONDATA
);
342 bool wxRegionGeneric::DoUnionWithRegion(const wxRegion
& region
)
345 return REGION::XUnionRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
348 bool wxRegionGeneric::DoIntersect(const wxRegion
& region
)
351 return REGION::XIntersectRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
354 bool wxRegionGeneric::DoSubtract(const wxRegion
& region
)
356 if ( region
.IsEmpty() )
362 return REGION::XSubtractRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
365 bool wxRegionGeneric::DoXor(const wxRegion
& region
)
368 return REGION::XXorRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
371 bool wxRegionGeneric::DoOffset(wxCoord x
, wxCoord y
)
374 return REGION::XOffsetRegion(M_REGIONDATA
, x
, y
);
377 // ----------------------------------------------------------------------------
378 // wxRegionGeneric comparison
379 // ----------------------------------------------------------------------------
381 bool wxRegionGeneric::IsEmpty() const
384 return REGION::XEmptyRegion(M_REGIONDATA
);
387 // Does the region contain the point (x,y)?
388 wxRegionContain
wxRegionGeneric::DoContainsPoint(wxCoord x
, wxCoord y
) const
391 return REGION::XPointInRegion(M_REGIONDATA
,x
,y
) ? wxInRegion
: wxOutRegion
;
394 // Does the region contain the rectangle rect?
395 wxRegionContain
wxRegionGeneric::DoContainsRect(const wxRect
& rect
) const
398 return REGION::XRectInRegion(M_REGIONDATA
,rect
.x
,rect
.y
,rect
.width
,rect
.height
);
401 // ========================================================================
402 // wxRegionIteratorGeneric
403 // ========================================================================
404 //IMPLEMENT_DYNAMIC_CLASS(wxRegionIteratorGeneric,wxObject)
406 wxRegionIteratorGeneric::wxRegionIteratorGeneric()
411 wxRegionIteratorGeneric::wxRegionIteratorGeneric(const wxRegionGeneric
& region
)
417 wxRegionIteratorGeneric::wxRegionIteratorGeneric(const wxRegionIteratorGeneric
& iterator
)
418 : m_region(iterator
.m_region
)
420 m_current
= iterator
.m_current
;
423 void wxRegionIteratorGeneric::Reset(const wxRegionGeneric
& region
)
429 bool wxRegionIteratorGeneric::HaveRects() const
431 return M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
434 wxRegionIteratorGeneric
& wxRegionIteratorGeneric::operator++()
440 wxRegionIteratorGeneric
wxRegionIteratorGeneric::operator++(int)
442 wxRegionIteratorGeneric
copy(*this);
447 wxRect
wxRegionIteratorGeneric::GetRect() const
449 wxASSERT(m_region
.m_refData
);
450 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
460 long wxRegionIteratorGeneric::GetX() const
462 wxASSERT(m_region
.m_refData
);
463 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
468 long wxRegionIteratorGeneric::GetY() const
470 wxASSERT(m_region
.m_refData
);
471 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
476 long wxRegionIteratorGeneric::GetW() const
478 wxASSERT(m_region
.m_refData
);
479 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
481 return box
->x2
- box
->x1
;
484 long wxRegionIteratorGeneric::GetH() const
486 wxASSERT(m_region
.m_refData
);
487 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
489 return box
->y2
- box
->y1
;
492 wxRegionIteratorGeneric::~wxRegionIteratorGeneric()
497 // ========================================================================
498 // The guts (from X.org)
499 // ========================================================================
501 /************************************************************************
503 Copyright 1987, 1988, 1998 The Open Group
505 Permission to use, copy, modify, distribute, and sell this software and its
506 documentation for any purpose is hereby granted without fee, provided that
507 the above copyright notice appear in all copies and that both that
508 copyright notice and this permission notice appear in supporting
511 The above copyright notice and this permission notice shall be included in
512 all copies or substantial portions of the Software.
514 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
515 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
516 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
517 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
518 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
519 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
521 Except as contained in this notice, the name of The Open Group shall not be
522 used in advertising or otherwise to promote the sale, use or other dealings
523 in this Software without prior written authorization from The Open Group.
526 Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
530 Permission to use, copy, modify, and distribute this software and its
531 documentation for any purpose and without fee is hereby granted,
532 provided that the above copyright notice appear in all copies and that
533 both that copyright notice and this permission notice appear in
534 supporting documentation, and that the name of Digital not be
535 used in advertising or publicity pertaining to distribution of the
536 software without specific, written prior permission.
538 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
539 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
540 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
541 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
542 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
543 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
546 ************************************************************************/
548 /* 1 if two BOXs overlap.
549 * 0 if two BOXs do not overlap.
550 * Remember, x2 and y2 are not in the region
552 #define EXTENTCHECK(r1, r2) \
553 ((r1)->x2 > (r2)->x1 && \
554 (r1)->x1 < (r2)->x2 && \
555 (r1)->y2 > (r2)->y1 && \
559 * Check to see if there is enough memory in the present region.
561 #define MEMCHECK(reg, rect, firstrect){\
562 if ((reg)->numRects >= ((reg)->size - 1)){\
563 (firstrect) = (BOX *) realloc \
564 ((char *)(firstrect), (unsigned) (2 * (sizeof(BOX)) * ((reg)->size)));\
565 if ((firstrect) == 0)\
568 (rect) = &(firstrect)[(reg)->numRects];\
572 #define EMPTY_REGION(pReg) pReg->numRects = 0
574 #define REGION_NOT_EMPTY(pReg) pReg->numRects
576 #define INBOX(r, x, y) \
577 ( ( ((r).x2 > x)) && \
578 ( ((r).x1 <= x)) && \
583 * The functions in this file implement the Region abstraction, similar to one
584 * used in the X11 sample server. A Region is simply an area, as the name
585 * implies, and is implemented as a "y-x-banded" array of rectangles. To
586 * explain: Each Region is made up of a certain number of rectangles sorted
587 * by y coordinate first, and then by x coordinate.
589 * Furthermore, the rectangles are banded such that every rectangle with a
590 * given upper-left y coordinate (y1) will have the same lower-right y
591 * coordinate (y2) and vice versa. If a rectangle has scanlines in a band, it
592 * will span the entire vertical distance of the band. This means that some
593 * areas that could be merged into a taller rectangle will be represented as
594 * several shorter rectangles to account for shorter rectangles to its left
595 * or right but within its "vertical scope".
597 * An added constraint on the rectangles is that they must cover as much
598 * horizontal area as possible. E.g. no two rectangles in a band are allowed
601 * Whenever possible, bands will be merged together to cover a greater vertical
602 * distance (and thus reduce the number of rectangles). Two bands can be merged
603 * only if the bottom of one touches the top of the other and they have
604 * rectangles in the same places (of the same width, of course). This maintains
605 * the y-x-banding that's so nice to have...
608 /* Create a new empty region */
609 Region
REGION::XCreateRegion(void)
611 Region temp
= new REGION
;
614 return (Region
) NULL
;
616 temp
->rects
= ( BOX
* )malloc( (unsigned) sizeof( BOX
));
621 return (Region
) NULL
;
624 temp
->extents
.x1
= 0;
625 temp
->extents
.y1
= 0;
626 temp
->extents
.x2
= 0;
627 temp
->extents
.y2
= 0;
632 bool REGION::XClipBox(Region r
, wxRect
*rect
)
634 rect
->x
= r
->extents
.x1
;
635 rect
->y
= r
->extents
.y1
;
636 rect
->width
= r
->extents
.x2
- r
->extents
.x1
;
637 rect
->height
= r
->extents
.y2
- r
->extents
.y1
;
642 *-----------------------------------------------------------------------
644 * Reset the extents of a region to what they should be. Called by
645 * miSubtract and miIntersect b/c they can't figure it out along the
646 * way or do so easily, as miUnion can.
652 * The region's 'extents' structure is overwritten.
654 *-----------------------------------------------------------------------
657 miSetExtents (Region pReg
)
659 register BoxPtr pBox
,
663 if (pReg
->numRects
== 0)
665 pReg
->extents
.x1
= 0;
666 pReg
->extents
.y1
= 0;
667 pReg
->extents
.x2
= 0;
668 pReg
->extents
.y2
= 0;
672 pExtents
= &pReg
->extents
;
674 pBoxEnd
= &pBox
[pReg
->numRects
- 1];
677 * Since pBox is the first rectangle in the region, it must have the
678 * smallest y1 and since pBoxEnd is the last rectangle in the region,
679 * it must have the largest y2, because of banding. Initialize x1 and
680 * x2 from pBox and pBoxEnd, resp., as good things to initialize them
683 pExtents
->x1
= pBox
->x1
;
684 pExtents
->y1
= pBox
->y1
;
685 pExtents
->x2
= pBoxEnd
->x2
;
686 pExtents
->y2
= pBoxEnd
->y2
;
688 assert(pExtents
->y1
< pExtents
->y2
);
689 while (pBox
<= pBoxEnd
)
691 if (pBox
->x1
< pExtents
->x1
)
693 pExtents
->x1
= pBox
->x1
;
695 if (pBox
->x2
> pExtents
->x2
)
697 pExtents
->x2
= pBox
->x2
;
701 assert(pExtents
->x1
< pExtents
->x2
);
708 free( (char *) r
->rects
);
713 /* TranslateRegion(pRegion, x, y)
720 register Region pRegion
,
727 pbox
= pRegion
->rects
;
728 nbox
= pRegion
->numRects
;
738 pRegion
->extents
.x1
+= x
;
739 pRegion
->extents
.x2
+= x
;
740 pRegion
->extents
.y1
+= y
;
741 pRegion
->extents
.y2
+= y
;
745 /*======================================================================
746 * Region Intersection
747 *====================================================================*/
749 *-----------------------------------------------------------------------
751 * Handle an overlapping band for miIntersect.
757 * Rectangles may be added to the region.
759 *-----------------------------------------------------------------------
764 register Region pReg
,
774 register BoxPtr pNextRect
;
776 pNextRect
= &pReg
->rects
[pReg
->numRects
];
778 while ((r1
!= r1End
) && (r2
!= r2End
))
780 x1
= wxMax(r1
->x1
,r2
->x1
);
781 x2
= wxMin(r1
->x2
,r2
->x2
);
784 * If there's any overlap between the two rectangles, add that
785 * overlap to the new region.
786 * There's no need to check for subsumption because the only way
787 * such a need could arise is if some region has two rectangles
788 * right next to each other. Since that should never happen...
794 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
801 assert(pReg
->numRects
<= pReg
->size
);
805 * Need to advance the pointers. Shift the one that extends
806 * to the right the least, since the other still has a chance to
807 * overlap with that region's next rectangle, if you see what I mean.
813 else if (r2
->x2
< r1
->x2
)
829 Region reg2
, /* source regions */
830 register Region newReg
) /* destination Region */
832 /* check for trivial reject */
833 if ( (!(reg1
->numRects
)) || (!(reg2
->numRects
)) ||
834 (!EXTENTCHECK(®1
->extents
, ®2
->extents
)))
835 newReg
->numRects
= 0;
837 miRegionOp (newReg
, reg1
, reg2
,
838 miIntersectO
, NULL
, NULL
);
841 * Can't alter newReg's extents before we call miRegionOp because
842 * it might be one of the source regions and miRegionOp depends
843 * on the extents of those regions being the same. Besides, this
844 * way there's no checking against rectangles that will be nuked
845 * due to coalescing, so we have to examine fewer rectangles.
847 miSetExtents(newReg
);
853 register Region dstrgn
,
857 if (dstrgn
!= rgn
) /* don't want to copy to itself */
859 if (dstrgn
->size
< rgn
->numRects
)
863 BOX
*prevRects
= dstrgn
->rects
;
865 dstrgn
->rects
= (BOX
*)
866 realloc((char *) dstrgn
->rects
,
867 (unsigned) rgn
->numRects
* (sizeof(BOX
)));
874 dstrgn
->size
= rgn
->numRects
;
876 dstrgn
->numRects
= rgn
->numRects
;
877 dstrgn
->extents
.x1
= rgn
->extents
.x1
;
878 dstrgn
->extents
.y1
= rgn
->extents
.y1
;
879 dstrgn
->extents
.x2
= rgn
->extents
.x2
;
880 dstrgn
->extents
.y2
= rgn
->extents
.y2
;
882 memcpy((char *) dstrgn
->rects
, (char *) rgn
->rects
,
883 (int) (rgn
->numRects
* sizeof(BOX
)));
887 /*======================================================================
888 * Generic Region Operator
889 *====================================================================*/
892 *-----------------------------------------------------------------------
894 * Attempt to merge the boxes in the current band with those in the
895 * previous one. Used only by miRegionOp.
898 * The new index for the previous band.
901 * If coalescing takes place:
902 * - rectangles in the previous band will have their y2 fields
904 * - pReg->numRects will be decreased.
906 *-----------------------------------------------------------------------
911 register Region pReg
, /* Region to coalesce */
912 int prevStart
, /* Index of start of previous band */
913 int curStart
) /* Index of start of current band */
915 register BoxPtr pPrevBox
; /* Current box in previous band */
916 register BoxPtr pCurBox
; /* Current box in current band */
917 register BoxPtr pRegEnd
; /* End of region */
918 int curNumRects
; /* Number of rectangles in current
920 int prevNumRects
; /* Number of rectangles in previous
922 int bandY1
; /* Y1 coordinate for current band */
924 pRegEnd
= &pReg
->rects
[pReg
->numRects
];
926 pPrevBox
= &pReg
->rects
[prevStart
];
927 prevNumRects
= curStart
- prevStart
;
930 * Figure out how many rectangles are in the current band. Have to do
931 * this because multiple bands could have been added in miRegionOp
932 * at the end when one region has been exhausted.
934 pCurBox
= &pReg
->rects
[curStart
];
935 bandY1
= pCurBox
->y1
;
936 for (curNumRects
= 0;
937 (pCurBox
!= pRegEnd
) && (pCurBox
->y1
== bandY1
);
943 if (pCurBox
!= pRegEnd
)
946 * If more than one band was added, we have to find the start
947 * of the last band added so the next coalescing job can start
948 * at the right place... (given when multiple bands are added,
949 * this may be pointless -- see above).
952 while (pRegEnd
[-1].y1
== pRegEnd
->y1
)
956 curStart
= pRegEnd
- pReg
->rects
;
957 pRegEnd
= pReg
->rects
+ pReg
->numRects
;
960 if ((curNumRects
== prevNumRects
) && (curNumRects
!= 0))
962 pCurBox
-= curNumRects
;
964 * The bands may only be coalesced if the bottom of the previous
965 * matches the top scanline of the current.
967 if (pPrevBox
->y2
== pCurBox
->y1
)
970 * Make sure the bands have boxes in the same places. This
971 * assumes that boxes have been added in such a way that they
972 * cover the most area possible. I.e. two boxes in a band must
973 * have some horizontal space between them.
977 if ((pPrevBox
->x1
!= pCurBox
->x1
) ||
978 (pPrevBox
->x2
!= pCurBox
->x2
))
981 * The bands don't line up so they can't be coalesced.
988 } while (prevNumRects
!= 0);
990 pReg
->numRects
-= curNumRects
;
991 pCurBox
-= curNumRects
;
992 pPrevBox
-= curNumRects
;
995 * The bands may be merged, so set the bottom y of each box
996 * in the previous band to that of the corresponding box in
1001 pPrevBox
->y2
= pCurBox
->y2
;
1005 } while (curNumRects
!= 0);
1008 * If only one band was added to the region, we have to backup
1009 * curStart to the start of the previous band.
1011 * If more than one band was added to the region, copy the
1012 * other bands down. The assumption here is that the other bands
1013 * came from the same region as the current one and no further
1014 * coalescing can be done on them since it's all been done
1015 * already... curStart is already in the right place.
1017 if (pCurBox
== pRegEnd
)
1019 curStart
= prevStart
;
1025 *pPrevBox
++ = *pCurBox
++;
1026 } while (pCurBox
!= pRegEnd
);
1035 *-----------------------------------------------------------------------
1037 * Apply an operation to two regions. Called by miUnion, miInverse,
1038 * miSubtract, miIntersect...
1044 * The new region is overwritten.
1047 * The idea behind this function is to view the two regions as sets.
1048 * Together they cover a rectangle of area that this function divides
1049 * into horizontal bands where points are covered only by one region
1050 * or by both. For the first case, the nonOverlapFunc is called with
1051 * each the band and the band's upper and lower extents. For the
1052 * second, the overlapFunc is called to process the entire band. It
1053 * is responsible for clipping the rectangles in the band, though
1054 * this function provides the boundaries.
1055 * At the end of each band, the new region is coalesced, if possible,
1056 * to reduce the number of rectangles in the region.
1058 *-----------------------------------------------------------------------
1063 register Region newReg
, /* Place to store result */
1064 Region reg1
, /* First region in operation */
1065 Region reg2
, /* 2d region in operation */
1067 register Region pReg
,
1073 wxCoord y2
), /* Function to call for over-
1075 int (*nonOverlap1Func
)(
1076 register Region pReg
,
1079 register wxCoord y1
,
1080 register wxCoord y2
), /* Function to call for non-
1081 * overlapping bands in region
1083 int (*nonOverlap2Func
)(
1084 register Region pReg
,
1087 register wxCoord y1
,
1088 register wxCoord y2
)) /* Function to call for non-
1089 * overlapping bands in region
1092 register BoxPtr r1
; /* Pointer into first region */
1093 register BoxPtr r2
; /* Pointer into 2d region */
1094 BoxPtr r1End
; /* End of 1st region */
1095 BoxPtr r2End
; /* End of 2d region */
1096 register wxCoord ybot
; /* Bottom of intersection */
1097 register wxCoord ytop
; /* Top of intersection */
1098 BoxPtr oldRects
; /* Old rects for newReg */
1099 int prevBand
; /* Index of start of
1100 * previous band in newReg */
1101 int curBand
; /* Index of start of current
1103 register BoxPtr r1BandEnd
; /* End of current band in r1 */
1104 register BoxPtr r2BandEnd
; /* End of current band in r2 */
1105 wxCoord top
; /* Top of non-overlapping
1107 wxCoord bot
; /* Bottom of non-overlapping
1112 * set r1, r2, r1End and r2End appropriately, preserve the important
1113 * parts of the destination region until the end in case it's one of
1114 * the two source regions, then mark the "new" region empty, allocating
1115 * another array of rectangles for it to use.
1119 r1End
= r1
+ reg1
->numRects
;
1120 r2End
= r2
+ reg2
->numRects
;
1122 oldRects
= newReg
->rects
;
1124 EMPTY_REGION(newReg
);
1127 * Allocate a reasonable number of rectangles for the new region. The idea
1128 * is to allocate enough so the individual functions don't need to
1129 * reallocate and copy the array, which is time consuming, yet we don't
1130 * have to worry about using too much memory. I hope to be able to
1131 * nuke the realloc() at the end of this function eventually.
1133 newReg
->size
= wxMax(reg1
->numRects
,reg2
->numRects
) * 2;
1135 newReg
->rects
= (BoxPtr
)malloc((unsigned) (sizeof(BoxRec
) * newReg
->size
));
1144 * Initialize ybot and ytop.
1145 * In the upcoming loop, ybot and ytop serve different functions depending
1146 * on whether the band being handled is an overlapping or non-overlapping
1148 * In the case of a non-overlapping band (only one of the regions
1149 * has points in the band), ybot is the bottom of the most recent
1150 * intersection and thus clips the top of the rectangles in that band.
1151 * ytop is the top of the next intersection between the two regions and
1152 * serves to clip the bottom of the rectangles in the current band.
1153 * For an overlapping band (where the two regions intersect), ytop clips
1154 * the top of the rectangles of both regions and ybot clips the bottoms.
1156 if (reg1
->extents
.y1
< reg2
->extents
.y1
)
1157 ybot
= reg1
->extents
.y1
;
1159 ybot
= reg2
->extents
.y1
;
1162 * prevBand serves to mark the start of the previous band so rectangles
1163 * can be coalesced into larger rectangles. qv. miCoalesce, above.
1164 * In the beginning, there is no previous band, so prevBand == curBand
1165 * (curBand is set later on, of course, but the first band will always
1166 * start at index 0). prevBand and curBand must be indices because of
1167 * the possible expansion, and resultant moving, of the new region's
1168 * array of rectangles.
1174 curBand
= newReg
->numRects
;
1177 * This algorithm proceeds one source-band (as opposed to a
1178 * destination band, which is determined by where the two regions
1179 * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
1180 * rectangle after the last one in the current band for their
1181 * respective regions.
1184 while ((r1BandEnd
!= r1End
) && (r1BandEnd
->y1
== r1
->y1
))
1190 while ((r2BandEnd
!= r2End
) && (r2BandEnd
->y1
== r2
->y1
))
1196 * First handle the band that doesn't intersect, if any.
1198 * Note that attention is restricted to one band in the
1199 * non-intersecting region at once, so if a region has n
1200 * bands between the current position and the next place it overlaps
1201 * the other, this entire loop will be passed through n times.
1203 if (r1
->y1
< r2
->y1
)
1205 top
= wxMax(r1
->y1
,ybot
);
1206 bot
= wxMin(r1
->y2
,r2
->y1
);
1208 if ((top
!= bot
) && (nonOverlap1Func
!= NULL
))
1210 (* nonOverlap1Func
) (newReg
, r1
, r1BandEnd
, top
, bot
);
1215 else if (r2
->y1
< r1
->y1
)
1217 top
= wxMax(r2
->y1
,ybot
);
1218 bot
= wxMin(r2
->y2
,r1
->y1
);
1220 if ((top
!= bot
) && (nonOverlap2Func
!= NULL
))
1222 (* nonOverlap2Func
) (newReg
, r2
, r2BandEnd
, top
, bot
);
1233 * If any rectangles got added to the region, try and coalesce them
1234 * with rectangles from the previous band. Note we could just do
1235 * this test in miCoalesce, but some machines incur a not
1236 * inconsiderable cost for function calls, so...
1238 if (newReg
->numRects
!= curBand
)
1240 prevBand
= miCoalesce (newReg
, prevBand
, curBand
);
1244 * Now see if we've hit an intersecting band. The two bands only
1245 * intersect if ybot > ytop
1247 ybot
= wxMin(r1
->y2
, r2
->y2
);
1248 curBand
= newReg
->numRects
;
1251 (* overlapFunc
) (newReg
, r1
, r1BandEnd
, r2
, r2BandEnd
, ytop
, ybot
);
1255 if (newReg
->numRects
!= curBand
)
1257 prevBand
= miCoalesce (newReg
, prevBand
, curBand
);
1261 * If we've finished with a band (y2 == ybot) we skip forward
1262 * in the region to the next band.
1272 } while ((r1
!= r1End
) && (r2
!= r2End
));
1275 * Deal with whichever region still has rectangles left.
1277 curBand
= newReg
->numRects
;
1280 if (nonOverlap1Func
!= NULL
)
1285 while ((r1BandEnd
< r1End
) && (r1BandEnd
->y1
== r1
->y1
))
1289 (* nonOverlap1Func
) (newReg
, r1
, r1BandEnd
,
1290 wxMax(r1
->y1
,ybot
), r1
->y2
);
1292 } while (r1
!= r1End
);
1295 else if ((r2
!= r2End
) && (nonOverlap2Func
!= NULL
))
1300 while ((r2BandEnd
< r2End
) && (r2BandEnd
->y1
== r2
->y1
))
1304 (* nonOverlap2Func
) (newReg
, r2
, r2BandEnd
,
1305 wxMax(r2
->y1
,ybot
), r2
->y2
);
1307 } while (r2
!= r2End
);
1310 if (newReg
->numRects
!= curBand
)
1312 (void) miCoalesce (newReg
, prevBand
, curBand
);
1316 * A bit of cleanup. To keep regions from growing without bound,
1317 * we shrink the array of rectangles to match the new number of
1318 * rectangles in the region. This never goes to 0, however...
1320 * Only do this stuff if the number of rectangles allocated is more than
1321 * twice the number of rectangles in the region (a simple optimization...).
1323 if (newReg
->numRects
< (newReg
->size
>> 1))
1325 if (REGION_NOT_EMPTY(newReg
))
1327 BoxPtr prev_rects
= newReg
->rects
;
1328 newReg
->size
= newReg
->numRects
;
1329 newReg
->rects
= (BoxPtr
) realloc ((char *) newReg
->rects
,
1330 (unsigned) (sizeof(BoxRec
) * newReg
->size
));
1331 if (! newReg
->rects
)
1332 newReg
->rects
= prev_rects
;
1337 * No point in doing the extra work involved in an realloc if
1338 * the region is empty
1341 free((char *) newReg
->rects
);
1342 newReg
->rects
= (BoxPtr
) malloc(sizeof(BoxRec
));
1345 free ((char *) oldRects
);
1349 /*======================================================================
1351 *====================================================================*/
1354 *-----------------------------------------------------------------------
1356 * Handle a non-overlapping band for the union operation. Just
1357 * Adds the rectangles into the region. Doesn't have to check for
1358 * subsumption or anything.
1364 * pReg->numRects is incremented and the final rectangles overwritten
1365 * with the rectangles we're passed.
1367 *-----------------------------------------------------------------------
1372 register Region pReg
,
1375 register wxCoord y1
,
1376 register wxCoord y2
)
1378 register BoxPtr pNextRect
;
1380 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1386 assert(r
->x1
< r
->x2
);
1387 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1388 pNextRect
->x1
= r
->x1
;
1390 pNextRect
->x2
= r
->x2
;
1392 pReg
->numRects
+= 1;
1395 assert(pReg
->numRects
<=pReg
->size
);
1398 return 0; /* lint */
1403 *-----------------------------------------------------------------------
1405 * Handle an overlapping band for the union operation. Picks the
1406 * left-most rectangle each time and merges it into the region.
1412 * Rectangles are overwritten in pReg->rects and pReg->numRects will
1415 *-----------------------------------------------------------------------
1421 register Region pReg
,
1426 register wxCoord y1
,
1427 register wxCoord y2
)
1429 register BoxPtr pNextRect
;
1431 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1433 #define MERGERECT(r) \
1434 if ((pReg->numRects != 0) && \
1435 (pNextRect[-1].y1 == y1) && \
1436 (pNextRect[-1].y2 == y2) && \
1437 (pNextRect[-1].x2 >= r->x1)) \
1439 if (pNextRect[-1].x2 < r->x2) \
1441 pNextRect[-1].x2 = r->x2; \
1442 assert(pNextRect[-1].x1<pNextRect[-1].x2); \
1447 MEMCHECK(pReg, pNextRect, pReg->rects); \
1448 pNextRect->y1 = y1; \
1449 pNextRect->y2 = y2; \
1450 pNextRect->x1 = r->x1; \
1451 pNextRect->x2 = r->x2; \
1452 pReg->numRects += 1; \
1455 assert(pReg->numRects<=pReg->size);\
1459 while ((r1
!= r1End
) && (r2
!= r2End
))
1461 if (r1
->x1
< r2
->x1
)
1476 } while (r1
!= r1End
);
1478 else while (r2
!= r2End
)
1482 return 0; /* lint */
1488 Region reg2
, /* source regions */
1489 Region newReg
) /* destination Region */
1491 /* checks all the simple cases */
1494 * Region 1 and 2 are the same or region 1 is empty
1496 if ( (reg1
== reg2
) || (!(reg1
->numRects
)) )
1499 miRegionCopy(newReg
, reg2
);
1504 * if nothing to union (region 2 empty)
1506 if (!(reg2
->numRects
))
1509 miRegionCopy(newReg
, reg1
);
1514 * Region 1 completely subsumes region 2
1516 if ((reg1
->numRects
== 1) &&
1517 (reg1
->extents
.x1
<= reg2
->extents
.x1
) &&
1518 (reg1
->extents
.y1
<= reg2
->extents
.y1
) &&
1519 (reg1
->extents
.x2
>= reg2
->extents
.x2
) &&
1520 (reg1
->extents
.y2
>= reg2
->extents
.y2
))
1523 miRegionCopy(newReg
, reg1
);
1528 * Region 2 completely subsumes region 1
1530 if ((reg2
->numRects
== 1) &&
1531 (reg2
->extents
.x1
<= reg1
->extents
.x1
) &&
1532 (reg2
->extents
.y1
<= reg1
->extents
.y1
) &&
1533 (reg2
->extents
.x2
>= reg1
->extents
.x2
) &&
1534 (reg2
->extents
.y2
>= reg1
->extents
.y2
))
1537 miRegionCopy(newReg
, reg2
);
1541 miRegionOp (newReg
, reg1
, reg2
, miUnionO
,
1542 miUnionNonO
, miUnionNonO
);
1544 newReg
->extents
.x1
= wxMin(reg1
->extents
.x1
, reg2
->extents
.x1
);
1545 newReg
->extents
.y1
= wxMin(reg1
->extents
.y1
, reg2
->extents
.y1
);
1546 newReg
->extents
.x2
= wxMax(reg1
->extents
.x2
, reg2
->extents
.x2
);
1547 newReg
->extents
.y2
= wxMax(reg1
->extents
.y2
, reg2
->extents
.y2
);
1552 /*======================================================================
1553 * Region Subtraction
1554 *====================================================================*/
1557 *-----------------------------------------------------------------------
1559 * Deal with non-overlapping band for subtraction. Any parts from
1560 * region 2 we discard. Anything from region 1 we add to the region.
1566 * pReg may be affected.
1568 *-----------------------------------------------------------------------
1573 register Region pReg
,
1576 register wxCoord y1
,
1577 register wxCoord y2
)
1579 register BoxPtr pNextRect
;
1581 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1587 assert(r
->x1
<r
->x2
);
1588 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1589 pNextRect
->x1
= r
->x1
;
1591 pNextRect
->x2
= r
->x2
;
1593 pReg
->numRects
+= 1;
1596 assert(pReg
->numRects
<= pReg
->size
);
1600 return 0; /* lint */
1604 *-----------------------------------------------------------------------
1606 * Overlapping band subtraction. x1 is the left-most point not yet
1613 * pReg may have rectangles added to it.
1615 *-----------------------------------------------------------------------
1620 register Region pReg
,
1625 register wxCoord y1
,
1626 register wxCoord y2
)
1628 register BoxPtr pNextRect
;
1634 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1636 while ((r1
!= r1End
) && (r2
!= r2End
))
1641 * Subtrahend missed the boat: go to next subtrahend.
1645 else if (r2
->x1
<= x1
)
1648 * Subtrahend preceeds minuend: nuke left edge of minuend.
1654 * Minuend completely covered: advance to next minuend and
1655 * reset left fence to edge of new minuend.
1664 * Subtrahend now used up since it doesn't extend beyond
1670 else if (r2
->x1
< r1
->x2
)
1673 * Left part of subtrahend covers part of minuend: add uncovered
1674 * part of minuend to region and skip to next subtrahend.
1677 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1680 pNextRect
->x2
= r2
->x1
;
1682 pReg
->numRects
+= 1;
1685 assert(pReg
->numRects
<=pReg
->size
);
1691 * Minuend used up: advance to new...
1700 * Subtrahend used up
1708 * Minuend used up: add any remaining piece before advancing.
1712 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1715 pNextRect
->x2
= r1
->x2
;
1717 pReg
->numRects
+= 1;
1719 assert(pReg
->numRects
<=pReg
->size
);
1728 * Add remaining minuend rectangles to region.
1733 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1736 pNextRect
->x2
= r1
->x2
;
1738 pReg
->numRects
+= 1;
1741 assert(pReg
->numRects
<=pReg
->size
);
1749 return 0; /* lint */
1753 *-----------------------------------------------------------------------
1755 * Subtract regS from regM and leave the result in regD.
1756 * S stands for subtrahend, M for minuend and D for difference.
1762 * regD is overwritten.
1764 *-----------------------------------------------------------------------
1767 bool REGION::XSubtractRegion(Region regM
, Region regS
, register Region regD
)
1769 /* check for trivial reject */
1770 if ( (!(regM
->numRects
)) || (!(regS
->numRects
)) ||
1771 (!EXTENTCHECK(®M
->extents
, ®S
->extents
)) )
1773 miRegionCopy(regD
, regM
);
1777 miRegionOp (regD
, regM
, regS
, miSubtractO
,
1778 miSubtractNonO1
, NULL
);
1781 * Can't alter newReg's extents before we call miRegionOp because
1782 * it might be one of the source regions and miRegionOp depends
1783 * on the extents of those regions being the unaltered. Besides, this
1784 * way there's no checking against rectangles that will be nuked
1785 * due to coalescing, so we have to examine fewer rectangles.
1787 miSetExtents (regD
);
1791 bool REGION::XXorRegion(Region sra
, Region srb
, Region dr
)
1793 Region tra
= XCreateRegion();
1795 wxCHECK_MSG( tra
, false, wxT("region not created") );
1797 Region trb
= XCreateRegion();
1799 wxCHECK_MSG( trb
, false, wxT("region not created") );
1801 (void) XSubtractRegion(sra
,srb
,tra
);
1802 (void) XSubtractRegion(srb
,sra
,trb
);
1803 (void) XUnionRegion(tra
,trb
,dr
);
1804 XDestroyRegion(tra
);
1805 XDestroyRegion(trb
);
1810 * Check to see if the region is empty. Assumes a region is passed
1813 bool REGION::XEmptyRegion(Region r
)
1815 if( r
->numRects
== 0 ) return true;
1820 * Check to see if two regions are equal
1822 bool REGION::XEqualRegion(Region r1
, Region r2
)
1826 if( r1
->numRects
!= r2
->numRects
) return false;
1827 else if( r1
->numRects
== 0 ) return true;
1828 else if ( r1
->extents
.x1
!= r2
->extents
.x1
) return false;
1829 else if ( r1
->extents
.x2
!= r2
->extents
.x2
) return false;
1830 else if ( r1
->extents
.y1
!= r2
->extents
.y1
) return false;
1831 else if ( r1
->extents
.y2
!= r2
->extents
.y2
) return false;
1832 else for( i
=0; i
< r1
->numRects
; i
++ ) {
1833 if ( r1
->rects
[i
].x1
!= r2
->rects
[i
].x1
) return false;
1834 else if ( r1
->rects
[i
].x2
!= r2
->rects
[i
].x2
) return false;
1835 else if ( r1
->rects
[i
].y1
!= r2
->rects
[i
].y1
) return false;
1836 else if ( r1
->rects
[i
].y2
!= r2
->rects
[i
].y2
) return false;
1841 bool REGION::XPointInRegion(Region pRegion
, int x
, int y
)
1845 if (pRegion
->numRects
== 0)
1847 if (!INBOX(pRegion
->extents
, x
, y
))
1849 for (i
=0; i
<pRegion
->numRects
; i
++)
1851 if (INBOX (pRegion
->rects
[i
], x
, y
))
1857 wxRegionContain
REGION::XRectInRegion(register Region region
,
1859 unsigned int rwidth
,
1860 unsigned int rheight
)
1862 register BoxPtr pbox
;
1863 register BoxPtr pboxEnd
;
1865 register BoxPtr prect
= &rect
;
1866 int partIn
, partOut
;
1870 prect
->x2
= rwidth
+ rx
;
1871 prect
->y2
= rheight
+ ry
;
1873 /* this is (just) a useful optimization */
1874 if ((region
->numRects
== 0) || !EXTENTCHECK(®ion
->extents
, prect
))
1875 return(wxOutRegion
);
1880 /* can stop when both partOut and partIn are true, or we reach prect->y2 */
1881 for (pbox
= region
->rects
, pboxEnd
= pbox
+ region
->numRects
;
1887 continue; /* getting up to speed or skipping remainder of band */
1891 partOut
= true; /* missed part of rectangle above */
1892 if (partIn
|| (pbox
->y1
>= prect
->y2
))
1894 ry
= pbox
->y1
; /* x guaranteed to be == prect->x1 */
1898 continue; /* not far enough over yet */
1902 partOut
= true; /* missed part of rectangle to left */
1907 if (pbox
->x1
< prect
->x2
)
1909 partIn
= true; /* definitely overlap */
1914 if (pbox
->x2
>= prect
->x2
)
1916 ry
= pbox
->y2
; /* finished with this band */
1917 if (ry
>= prect
->y2
)
1919 rx
= prect
->x1
; /* reset x out to left again */
1923 * Because boxes in a band are maximal width, if the first box
1924 * to overlap the rectangle doesn't completely cover it in that
1925 * band, the rectangle must be partially out, since some of it
1926 * will be uncovered in that band. partIn will have been set true
1934 return(partIn
? ((ry
< prect
->y2
) ? wxPartRegion
: wxInRegion
) :