]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/regiong.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/region.cpp
3 // Purpose: generic wxRegion class
4 // Author: David Elliott
8 // Copyright: (c) 2004 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/generic/region.h"
15 // ========================================================================
16 // Classes to interface with X.org code
17 // ========================================================================
21 wxCoord x1
, x2
, y1
, y2
;
22 } Box
, BOX
, BoxRec
, *BoxPtr
;
24 typedef struct REGION
*Region
;
29 // Default constructor initializes nothing
31 REGION(const wxRect
& rect
)
37 extents
.x2
= rect
.x
+ rect
.width
;
38 extents
.y2
= rect
.y
+ rect
.height
;
41 BoxPtr
GetBox(int i
) { if(i
<numRects
) return rects
+i
; else return NULL
; }
47 static bool XOffsetRegion(
48 register Region pRegion
,
51 static bool XIntersectRegion(
53 Region reg2
, /* source regions */
54 register Region newReg
); /* destination Region */
55 static bool XUnionRegion(
57 Region reg2
, /* source regions */
58 Region newReg
); /* destination Region */
59 static bool XSubtractRegion(
62 register Region regD
);
63 static bool XXorRegion(Region sra
, Region srb
, Region dr
);
64 static bool XEmptyRegion(
66 static bool XEqualRegion(Region r1
, Region r2
);
67 static bool XPointInRegion(
70 static wxRegionContain
XRectInRegion(
71 register Region region
,
73 unsigned int rwidth
, unsigned int rheight
);
76 static Region
XCreateRegion(void);
77 static void miSetExtents (
79 static bool XDestroyRegion(Region r
);
80 static int miIntersectO (
88 static void miRegionCopy(
89 register Region dstrgn
,
91 static int miCoalesce(
92 register Region pReg
, /* Region to coalesce */
93 int prevStart
, /* Index of start of previous band */
94 int curStart
); /* Index of start of current band */
95 static void miRegionOp(
96 register Region newReg
, /* Place to store result */
97 Region reg1
, /* First region in operation */
98 Region reg2
, /* 2d region in operation */
100 register Region pReg
,
106 wxCoord y2
), /* Function to call for over-
108 int (*nonOverlap1Func
)(
109 register Region pReg
,
113 register wxCoord y2
), /* Function to call for non-
114 * overlapping bands in region
116 int (*nonOverlap2Func
)(
117 register Region pReg
,
121 register wxCoord y2
)); /* Function to call for non-
122 * overlapping bands in region
124 static int miUnionNonO (
125 register Region pReg
,
129 register wxCoord y2
);
130 static int miUnionO (
131 register Region pReg
,
137 register wxCoord y2
);
138 static int miSubtractNonO1 (
139 register Region pReg
,
143 register wxCoord y2
);
144 static int miSubtractO (
145 register Region pReg
,
151 register wxCoord y2
);
159 // ========================================================================
161 // ========================================================================
162 class wxRegionRefData
: public wxObjectRefData
, public REGION
169 rects
= ( BOX
* )malloc( (unsigned) sizeof( BOX
));
175 wxRegionRefData(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
178 { rects
= (BOX
*)malloc(sizeof(BOX
));
181 extents
.x1
= topLeft
.x
;
182 extents
.y1
= topLeft
.y
;
183 extents
.x2
= bottomRight
.x
;
184 extents
.y2
= bottomRight
.y
;
187 wxRegionRefData(const wxRect
& rect
)
190 { rects
= (BOX
*)malloc(sizeof(BOX
));
193 wxRegionRefData(const wxRegionRefData
& refData
)
198 numRects
= refData
.numRects
;
199 rects
= (Box
*)malloc(numRects
*sizeof(Box
));
200 extents
= refData
.extents
;
208 wxRegionRefData(const REGION
&);
211 // ========================================================================
213 // ========================================================================
214 //IMPLEMENT_DYNAMIC_CLASS(wxRegionGeneric, wxGDIObject);
216 #define M_REGIONDATA ((wxRegionRefData *)m_refData)
217 #define M_REGIONDATA_OF(rgn) ((wxRegionRefData *)(rgn.m_refData))
219 // ----------------------------------------------------------------------------
220 // wxRegionGeneric construction
221 // ----------------------------------------------------------------------------
223 wxRegionGeneric::wxRegionGeneric()
227 wxRegionGeneric::~wxRegionGeneric()
231 wxRegionGeneric::wxRegionGeneric(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
233 m_refData
= new wxRegionRefData(wxRect(x
,y
,w
,h
));
236 wxRegionGeneric::wxRegionGeneric(const wxRect
& rect
)
238 m_refData
= new wxRegionRefData(rect
);
241 wxRegionGeneric::wxRegionGeneric(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
243 m_refData
= new wxRegionRefData(topLeft
, bottomRight
);
246 void wxRegionGeneric::Clear()
251 wxObjectRefData
*wxRegionGeneric::CreateRefData() const
253 return new wxRegionRefData
;
256 wxObjectRefData
*wxRegionGeneric::CloneRefData(const wxObjectRefData
*data
) const
258 return new wxRegionRefData(*(wxRegionRefData
*)data
);
261 bool wxRegionGeneric::operator== (const wxRegionGeneric
& region
)
263 wxASSERT(m_refData
&& region
.m_refData
);
264 return REGION::XEqualRegion(M_REGIONDATA
,M_REGIONDATA_OF(region
));
267 wxRect
wxRegionGeneric::GetBox() const
271 REGION::XClipBox(M_REGIONDATA
,&rect
);
275 void wxRegionGeneric::GetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
&w
, wxCoord
&h
) const
279 REGION::XClipBox(M_REGIONDATA
,&rect
);
286 // ----------------------------------------------------------------------------
287 // wxRegionGeneric operations
288 // ----------------------------------------------------------------------------
290 bool wxRegionGeneric::Union(const wxRect
& rect
)
291 /* XUnionRectWithRegion */
293 if (!rect
.width
|| !rect
.height
)
298 return REGION::XUnionRegion(®ion
,M_REGIONDATA
,M_REGIONDATA
);
301 bool wxRegionGeneric::Union(const wxRegionGeneric
& region
)
304 return REGION::XUnionRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
307 bool wxRegionGeneric::Intersect(const wxRect
& rect
)
309 if (!rect
.width
|| !rect
.height
)
314 return REGION::XIntersectRegion(®ion
,M_REGIONDATA
,M_REGIONDATA
);
317 bool wxRegionGeneric::Intersect(const wxRegionGeneric
& region
)
320 return REGION::XIntersectRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
323 bool wxRegionGeneric::Subtract(const wxRect
& rect
)
325 if (!rect
.width
|| !rect
.height
)
330 return REGION::XSubtractRegion(®ion
,M_REGIONDATA
,M_REGIONDATA
);
333 bool wxRegionGeneric::Subtract(const wxRegionGeneric
& region
)
335 return REGION::XSubtractRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
338 bool wxRegionGeneric::Xor(const wxRect
& rect
)
340 if (!rect
.width
|| !rect
.height
)
345 return REGION::XXorRegion(®ion
,M_REGIONDATA
,M_REGIONDATA
);
348 bool wxRegionGeneric::Xor(const wxRegionGeneric
& region
)
351 return REGION::XXorRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
354 bool wxRegionGeneric::Offset(wxCoord x
, wxCoord y
)
357 return REGION::XOffsetRegion(M_REGIONDATA
, x
, y
);
360 // ----------------------------------------------------------------------------
361 // wxRegionGeneric comparison
362 // ----------------------------------------------------------------------------
364 bool wxRegionGeneric::Empty() const
367 return REGION::XEmptyRegion(M_REGIONDATA
);
370 // Does the region contain the point (x,y)?
371 wxRegionContain
wxRegionGeneric::Contains(long x
, long y
) const
374 return REGION::XPointInRegion(M_REGIONDATA
,x
,y
)?wxInRegion
:wxOutRegion
;
377 // Does the region contain the point pt?
378 wxRegionContain
wxRegionGeneric::Contains(const wxPoint
& pt
) const
381 return REGION::XPointInRegion(M_REGIONDATA
,pt
.x
,pt
.y
)?wxInRegion
:wxOutRegion
;
384 // Does the region contain the rectangle (x, y, w, h)?
385 wxRegionContain
wxRegionGeneric::Contains(long x
, long y
, long w
, long h
) const
388 return REGION::XRectInRegion(M_REGIONDATA
,x
,y
,w
,h
);
391 // Does the region contain the rectangle rect?
392 wxRegionContain
wxRegionGeneric::Contains(const wxRect
& rect
) const
395 return REGION::XRectInRegion(M_REGIONDATA
,rect
.x
,rect
.y
,rect
.width
,rect
.height
);
398 // ========================================================================
399 // wxRegionIteratorGeneric
400 // ========================================================================
401 //IMPLEMENT_DYNAMIC_CLASS(wxRegionIteratorGeneric,wxObject);
403 wxRegionIteratorGeneric::wxRegionIteratorGeneric()
408 wxRegionIteratorGeneric::wxRegionIteratorGeneric(const wxRegionGeneric
& region
)
414 wxRegionIteratorGeneric::wxRegionIteratorGeneric(const wxRegionIteratorGeneric
& iterator
)
415 : m_region(iterator
.m_region
)
417 m_current
= iterator
.m_current
;
420 void wxRegionIteratorGeneric::Reset(const wxRegionGeneric
& region
)
426 bool wxRegionIteratorGeneric::HaveRects() const
428 return M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
431 wxRegionIteratorGeneric
& wxRegionIteratorGeneric::operator++()
437 wxRegionIteratorGeneric
wxRegionIteratorGeneric::operator++(int)
439 wxRegionIteratorGeneric
copy(*this);
444 wxRect
wxRegionIteratorGeneric::GetRect() const
447 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
457 long wxRegionIteratorGeneric::GetX() const
460 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
465 long wxRegionIteratorGeneric::GetY() const
468 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
473 long wxRegionIteratorGeneric::GetW() const
476 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
478 return box
->x2
- box
->x1
;
481 long wxRegionIteratorGeneric::GetH() const
484 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
486 return box
->y2
- box
->y1
;
489 wxRegionIteratorGeneric::~wxRegionIteratorGeneric()
494 // ========================================================================
495 // The guts (from X.org)
496 // ========================================================================
498 /************************************************************************
500 Copyright 1987, 1988, 1998 The Open Group
502 Permission to use, copy, modify, distribute, and sell this software and its
503 documentation for any purpose is hereby granted without fee, provided that
504 the above copyright notice appear in all copies and that both that
505 copyright notice and this permission notice appear in supporting
508 The above copyright notice and this permission notice shall be included in
509 all copies or substantial portions of the Software.
511 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
512 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
513 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
514 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
515 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
516 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
518 Except as contained in this notice, the name of The Open Group shall not be
519 used in advertising or otherwise to promote the sale, use or other dealings
520 in this Software without prior written authorization from The Open Group.
523 Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
527 Permission to use, copy, modify, and distribute this software and its
528 documentation for any purpose and without fee is hereby granted,
529 provided that the above copyright notice appear in all copies and that
530 both that copyright notice and this permission notice appear in
531 supporting documentation, and that the name of Digital not be
532 used in advertising or publicity pertaining to distribution of the
533 software without specific, written prior permission.
535 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
536 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
537 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
538 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
539 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
540 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
543 ************************************************************************/
545 /* 1 if two BOXs overlap.
546 * 0 if two BOXs do not overlap.
547 * Remember, x2 and y2 are not in the region
549 #define EXTENTCHECK(r1, r2) \
550 ((r1)->x2 > (r2)->x1 && \
551 (r1)->x1 < (r2)->x2 && \
552 (r1)->y2 > (r2)->y1 && \
556 * Check to see if there is enough memory in the present region.
558 #define MEMCHECK(reg, rect, firstrect){\
559 if ((reg)->numRects >= ((reg)->size - 1)){\
560 (firstrect) = (BOX *) realloc \
561 ((char *)(firstrect), (unsigned) (2 * (sizeof(BOX)) * ((reg)->size)));\
562 if ((firstrect) == 0)\
565 (rect) = &(firstrect)[(reg)->numRects];\
569 #define EMPTY_REGION(pReg) pReg->numRects = 0
571 #define REGION_NOT_EMPTY(pReg) pReg->numRects
573 #define INBOX(r, x, y) \
574 ( ( ((r).x2 > x)) && \
575 ( ((r).x1 <= x)) && \
580 * The functions in this file implement the Region abstraction, similar to one
581 * used in the X11 sample server. A Region is simply an area, as the name
582 * implies, and is implemented as a "y-x-banded" array of rectangles. To
583 * explain: Each Region is made up of a certain number of rectangles sorted
584 * by y coordinate first, and then by x coordinate.
586 * Furthermore, the rectangles are banded such that every rectangle with a
587 * given upper-left y coordinate (y1) will have the same lower-right y
588 * coordinate (y2) and vice versa. If a rectangle has scanlines in a band, it
589 * will span the entire vertical distance of the band. This means that some
590 * areas that could be merged into a taller rectangle will be represented as
591 * several shorter rectangles to account for shorter rectangles to its left
592 * or right but within its "vertical scope".
594 * An added constraint on the rectangles is that they must cover as much
595 * horizontal area as possible. E.g. no two rectangles in a band are allowed
598 * Whenever possible, bands will be merged together to cover a greater vertical
599 * distance (and thus reduce the number of rectangles). Two bands can be merged
600 * only if the bottom of one touches the top of the other and they have
601 * rectangles in the same places (of the same width, of course). This maintains
602 * the y-x-banding that's so nice to have...
605 /* Create a new empty region */
611 if (! (temp
= new REGION
))
612 return (Region
) NULL
;
613 if (! (temp
->rects
= ( BOX
* )malloc( (unsigned) sizeof( BOX
)))) {
615 return (Region
) NULL
;
618 temp
->extents
.x1
= 0;
619 temp
->extents
.y1
= 0;
620 temp
->extents
.x2
= 0;
621 temp
->extents
.y2
= 0;
631 rect
->x
= r
->extents
.x1
;
632 rect
->y
= r
->extents
.y1
;
633 rect
->width
= r
->extents
.x2
- r
->extents
.x1
;
634 rect
->height
= r
->extents
.y2
- r
->extents
.y1
;
639 *-----------------------------------------------------------------------
641 * Reset the extents of a region to what they should be. Called by
642 * miSubtract and miIntersect b/c they can't figure it out along the
643 * way or do so easily, as miUnion can.
649 * The region's 'extents' structure is overwritten.
651 *-----------------------------------------------------------------------
657 register BoxPtr pBox
,
661 if (pReg
->numRects
== 0)
663 pReg
->extents
.x1
= 0;
664 pReg
->extents
.y1
= 0;
665 pReg
->extents
.x2
= 0;
666 pReg
->extents
.y2
= 0;
670 pExtents
= &pReg
->extents
;
672 pBoxEnd
= &pBox
[pReg
->numRects
- 1];
675 * Since pBox is the first rectangle in the region, it must have the
676 * smallest y1 and since pBoxEnd is the last rectangle in the region,
677 * it must have the largest y2, because of banding. Initialize x1 and
678 * x2 from pBox and pBoxEnd, resp., as good things to initialize them
681 pExtents
->x1
= pBox
->x1
;
682 pExtents
->y1
= pBox
->y1
;
683 pExtents
->x2
= pBoxEnd
->x2
;
684 pExtents
->y2
= pBoxEnd
->y2
;
686 assert(pExtents
->y1
< pExtents
->y2
);
687 while (pBox
<= pBoxEnd
)
689 if (pBox
->x1
< pExtents
->x1
)
691 pExtents
->x1
= pBox
->x1
;
693 if (pBox
->x2
> pExtents
->x2
)
695 pExtents
->x2
= pBox
->x2
;
699 assert(pExtents
->x1
< pExtents
->x2
);
706 free( (char *) r
->rects
);
711 /* TranslateRegion(pRegion, x, y)
718 register Region pRegion
,
725 pbox
= pRegion
->rects
;
726 nbox
= pRegion
->numRects
;
736 pRegion
->extents
.x1
+= x
;
737 pRegion
->extents
.x2
+= x
;
738 pRegion
->extents
.y1
+= y
;
739 pRegion
->extents
.y2
+= y
;
743 /*======================================================================
744 * Region Intersection
745 *====================================================================*/
747 *-----------------------------------------------------------------------
749 * Handle an overlapping band for miIntersect.
755 * Rectangles may be added to the region.
757 *-----------------------------------------------------------------------
762 register Region pReg
,
772 register BoxPtr pNextRect
;
774 pNextRect
= &pReg
->rects
[pReg
->numRects
];
776 while ((r1
!= r1End
) && (r2
!= r2End
))
778 x1
= wxMax(r1
->x1
,r2
->x1
);
779 x2
= wxMin(r1
->x2
,r2
->x2
);
782 * If there's any overlap between the two rectangles, add that
783 * overlap to the new region.
784 * There's no need to check for subsumption because the only way
785 * such a need could arise is if some region has two rectangles
786 * right next to each other. Since that should never happen...
792 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
799 assert(pReg
->numRects
<= pReg
->size
);
803 * Need to advance the pointers. Shift the one that extends
804 * to the right the least, since the other still has a chance to
805 * overlap with that region's next rectangle, if you see what I mean.
811 else if (r2
->x2
< r1
->x2
)
827 Region reg2
, /* source regions */
828 register Region newReg
) /* destination Region */
830 /* check for trivial reject */
831 if ( (!(reg1
->numRects
)) || (!(reg2
->numRects
)) ||
832 (!EXTENTCHECK(®1
->extents
, ®2
->extents
)))
833 newReg
->numRects
= 0;
835 miRegionOp (newReg
, reg1
, reg2
,
836 miIntersectO
, NULL
, NULL
);
839 * Can't alter newReg's extents before we call miRegionOp because
840 * it might be one of the source regions and miRegionOp depends
841 * on the extents of those regions being the same. Besides, this
842 * way there's no checking against rectangles that will be nuked
843 * due to coalescing, so we have to examine fewer rectangles.
845 miSetExtents(newReg
);
851 register Region dstrgn
,
855 if (dstrgn
!= rgn
) /* don't want to copy to itself */
857 if (dstrgn
->size
< rgn
->numRects
)
861 BOX
*prevRects
= dstrgn
->rects
;
863 if (! (dstrgn
->rects
= (BOX
*)
864 realloc((char *) dstrgn
->rects
,
865 (unsigned) rgn
->numRects
* (sizeof(BOX
))))) {
870 dstrgn
->size
= rgn
->numRects
;
872 dstrgn
->numRects
= rgn
->numRects
;
873 dstrgn
->extents
.x1
= rgn
->extents
.x1
;
874 dstrgn
->extents
.y1
= rgn
->extents
.y1
;
875 dstrgn
->extents
.x2
= rgn
->extents
.x2
;
876 dstrgn
->extents
.y2
= rgn
->extents
.y2
;
878 memcpy((char *) dstrgn
->rects
, (char *) rgn
->rects
,
879 (int) (rgn
->numRects
* sizeof(BOX
)));
883 /*======================================================================
884 * Generic Region Operator
885 *====================================================================*/
888 *-----------------------------------------------------------------------
890 * Attempt to merge the boxes in the current band with those in the
891 * previous one. Used only by miRegionOp.
894 * The new index for the previous band.
897 * If coalescing takes place:
898 * - rectangles in the previous band will have their y2 fields
900 * - pReg->numRects will be decreased.
902 *-----------------------------------------------------------------------
907 register Region pReg
, /* Region to coalesce */
908 int prevStart
, /* Index of start of previous band */
909 int curStart
) /* Index of start of current band */
911 register BoxPtr pPrevBox
; /* Current box in previous band */
912 register BoxPtr pCurBox
; /* Current box in current band */
913 register BoxPtr pRegEnd
; /* End of region */
914 int curNumRects
; /* Number of rectangles in current
916 int prevNumRects
; /* Number of rectangles in previous
918 int bandY1
; /* Y1 coordinate for current band */
920 pRegEnd
= &pReg
->rects
[pReg
->numRects
];
922 pPrevBox
= &pReg
->rects
[prevStart
];
923 prevNumRects
= curStart
- prevStart
;
926 * Figure out how many rectangles are in the current band. Have to do
927 * this because multiple bands could have been added in miRegionOp
928 * at the end when one region has been exhausted.
930 pCurBox
= &pReg
->rects
[curStart
];
931 bandY1
= pCurBox
->y1
;
932 for (curNumRects
= 0;
933 (pCurBox
!= pRegEnd
) && (pCurBox
->y1
== bandY1
);
939 if (pCurBox
!= pRegEnd
)
942 * If more than one band was added, we have to find the start
943 * of the last band added so the next coalescing job can start
944 * at the right place... (given when multiple bands are added,
945 * this may be pointless -- see above).
948 while (pRegEnd
[-1].y1
== pRegEnd
->y1
)
952 curStart
= pRegEnd
- pReg
->rects
;
953 pRegEnd
= pReg
->rects
+ pReg
->numRects
;
956 if ((curNumRects
== prevNumRects
) && (curNumRects
!= 0)) {
957 pCurBox
-= curNumRects
;
959 * The bands may only be coalesced if the bottom of the previous
960 * matches the top scanline of the current.
962 if (pPrevBox
->y2
== pCurBox
->y1
)
965 * Make sure the bands have boxes in the same places. This
966 * assumes that boxes have been added in such a way that they
967 * cover the most area possible. I.e. two boxes in a band must
968 * have some horizontal space between them.
972 if ((pPrevBox
->x1
!= pCurBox
->x1
) ||
973 (pPrevBox
->x2
!= pCurBox
->x2
))
976 * The bands don't line up so they can't be coalesced.
983 } while (prevNumRects
!= 0);
985 pReg
->numRects
-= curNumRects
;
986 pCurBox
-= curNumRects
;
987 pPrevBox
-= curNumRects
;
990 * The bands may be merged, so set the bottom y of each box
991 * in the previous band to that of the corresponding box in
996 pPrevBox
->y2
= pCurBox
->y2
;
1000 } while (curNumRects
!= 0);
1003 * If only one band was added to the region, we have to backup
1004 * curStart to the start of the previous band.
1006 * If more than one band was added to the region, copy the
1007 * other bands down. The assumption here is that the other bands
1008 * came from the same region as the current one and no further
1009 * coalescing can be done on them since it's all been done
1010 * already... curStart is already in the right place.
1012 if (pCurBox
== pRegEnd
)
1014 curStart
= prevStart
;
1020 *pPrevBox
++ = *pCurBox
++;
1021 } while (pCurBox
!= pRegEnd
);
1030 *-----------------------------------------------------------------------
1032 * Apply an operation to two regions. Called by miUnion, miInverse,
1033 * miSubtract, miIntersect...
1039 * The new region is overwritten.
1042 * The idea behind this function is to view the two regions as sets.
1043 * Together they cover a rectangle of area that this function divides
1044 * into horizontal bands where points are covered only by one region
1045 * or by both. For the first case, the nonOverlapFunc is called with
1046 * each the band and the band's upper and lower extents. For the
1047 * second, the overlapFunc is called to process the entire band. It
1048 * is responsible for clipping the rectangles in the band, though
1049 * this function provides the boundaries.
1050 * At the end of each band, the new region is coalesced, if possible,
1051 * to reduce the number of rectangles in the region.
1053 *-----------------------------------------------------------------------
1058 register Region newReg
, /* Place to store result */
1059 Region reg1
, /* First region in operation */
1060 Region reg2
, /* 2d region in operation */
1062 register Region pReg
,
1068 wxCoord y2
), /* Function to call for over-
1070 int (*nonOverlap1Func
)(
1071 register Region pReg
,
1074 register wxCoord y1
,
1075 register wxCoord y2
), /* Function to call for non-
1076 * overlapping bands in region
1078 int (*nonOverlap2Func
)(
1079 register Region pReg
,
1082 register wxCoord y1
,
1083 register wxCoord y2
)) /* Function to call for non-
1084 * overlapping bands in region
1087 register BoxPtr r1
; /* Pointer into first region */
1088 register BoxPtr r2
; /* Pointer into 2d region */
1089 BoxPtr r1End
; /* End of 1st region */
1090 BoxPtr r2End
; /* End of 2d region */
1091 register wxCoord ybot
; /* Bottom of intersection */
1092 register wxCoord ytop
; /* Top of intersection */
1093 BoxPtr oldRects
; /* Old rects for newReg */
1094 int prevBand
; /* Index of start of
1095 * previous band in newReg */
1096 int curBand
; /* Index of start of current
1098 register BoxPtr r1BandEnd
; /* End of current band in r1 */
1099 register BoxPtr r2BandEnd
; /* End of current band in r2 */
1100 wxCoord top
; /* Top of non-overlapping
1102 wxCoord bot
; /* Bottom of non-overlapping
1107 * set r1, r2, r1End and r2End appropriately, preserve the important
1108 * parts of the destination region until the end in case it's one of
1109 * the two source regions, then mark the "new" region empty, allocating
1110 * another array of rectangles for it to use.
1114 r1End
= r1
+ reg1
->numRects
;
1115 r2End
= r2
+ reg2
->numRects
;
1117 oldRects
= newReg
->rects
;
1119 EMPTY_REGION(newReg
);
1122 * Allocate a reasonable number of rectangles for the new region. The idea
1123 * is to allocate enough so the individual functions don't need to
1124 * reallocate and copy the array, which is time consuming, yet we don't
1125 * have to worry about using too much memory. I hope to be able to
1126 * nuke the realloc() at the end of this function eventually.
1128 newReg
->size
= wxMax(reg1
->numRects
,reg2
->numRects
) * 2;
1130 if (! (newReg
->rects
= (BoxPtr
)
1131 malloc ((unsigned) (sizeof(BoxRec
) * newReg
->size
)))) {
1137 * Initialize ybot and ytop.
1138 * In the upcoming loop, ybot and ytop serve different functions depending
1139 * on whether the band being handled is an overlapping or non-overlapping
1141 * In the case of a non-overlapping band (only one of the regions
1142 * has points in the band), ybot is the bottom of the most recent
1143 * intersection and thus clips the top of the rectangles in that band.
1144 * ytop is the top of the next intersection between the two regions and
1145 * serves to clip the bottom of the rectangles in the current band.
1146 * For an overlapping band (where the two regions intersect), ytop clips
1147 * the top of the rectangles of both regions and ybot clips the bottoms.
1149 if (reg1
->extents
.y1
< reg2
->extents
.y1
)
1150 ybot
= reg1
->extents
.y1
;
1152 ybot
= reg2
->extents
.y1
;
1155 * prevBand serves to mark the start of the previous band so rectangles
1156 * can be coalesced into larger rectangles. qv. miCoalesce, above.
1157 * In the beginning, there is no previous band, so prevBand == curBand
1158 * (curBand is set later on, of course, but the first band will always
1159 * start at index 0). prevBand and curBand must be indices because of
1160 * the possible expansion, and resultant moving, of the new region's
1161 * array of rectangles.
1167 curBand
= newReg
->numRects
;
1170 * This algorithm proceeds one source-band (as opposed to a
1171 * destination band, which is determined by where the two regions
1172 * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
1173 * rectangle after the last one in the current band for their
1174 * respective regions.
1177 while ((r1BandEnd
!= r1End
) && (r1BandEnd
->y1
== r1
->y1
))
1183 while ((r2BandEnd
!= r2End
) && (r2BandEnd
->y1
== r2
->y1
))
1189 * First handle the band that doesn't intersect, if any.
1191 * Note that attention is restricted to one band in the
1192 * non-intersecting region at once, so if a region has n
1193 * bands between the current position and the next place it overlaps
1194 * the other, this entire loop will be passed through n times.
1196 if (r1
->y1
< r2
->y1
)
1198 top
= wxMax(r1
->y1
,ybot
);
1199 bot
= wxMin(r1
->y2
,r2
->y1
);
1201 if ((top
!= bot
) && (nonOverlap1Func
!= NULL
))
1203 (* nonOverlap1Func
) (newReg
, r1
, r1BandEnd
, top
, bot
);
1208 else if (r2
->y1
< r1
->y1
)
1210 top
= wxMax(r2
->y1
,ybot
);
1211 bot
= wxMin(r2
->y2
,r1
->y1
);
1213 if ((top
!= bot
) && (nonOverlap2Func
!= NULL
))
1215 (* nonOverlap2Func
) (newReg
, r2
, r2BandEnd
, top
, bot
);
1226 * If any rectangles got added to the region, try and coalesce them
1227 * with rectangles from the previous band. Note we could just do
1228 * this test in miCoalesce, but some machines incur a not
1229 * inconsiderable cost for function calls, so...
1231 if (newReg
->numRects
!= curBand
)
1233 prevBand
= miCoalesce (newReg
, prevBand
, curBand
);
1237 * Now see if we've hit an intersecting band. The two bands only
1238 * intersect if ybot > ytop
1240 ybot
= wxMin(r1
->y2
, r2
->y2
);
1241 curBand
= newReg
->numRects
;
1244 (* overlapFunc
) (newReg
, r1
, r1BandEnd
, r2
, r2BandEnd
, ytop
, ybot
);
1248 if (newReg
->numRects
!= curBand
)
1250 prevBand
= miCoalesce (newReg
, prevBand
, curBand
);
1254 * If we've finished with a band (y2 == ybot) we skip forward
1255 * in the region to the next band.
1265 } while ((r1
!= r1End
) && (r2
!= r2End
));
1268 * Deal with whichever region still has rectangles left.
1270 curBand
= newReg
->numRects
;
1273 if (nonOverlap1Func
!= NULL
)
1278 while ((r1BandEnd
< r1End
) && (r1BandEnd
->y1
== r1
->y1
))
1282 (* nonOverlap1Func
) (newReg
, r1
, r1BandEnd
,
1283 wxMax(r1
->y1
,ybot
), r1
->y2
);
1285 } while (r1
!= r1End
);
1288 else if ((r2
!= r2End
) && (nonOverlap2Func
!= NULL
))
1293 while ((r2BandEnd
< r2End
) && (r2BandEnd
->y1
== r2
->y1
))
1297 (* nonOverlap2Func
) (newReg
, r2
, r2BandEnd
,
1298 wxMax(r2
->y1
,ybot
), r2
->y2
);
1300 } while (r2
!= r2End
);
1303 if (newReg
->numRects
!= curBand
)
1305 (void) miCoalesce (newReg
, prevBand
, curBand
);
1309 * A bit of cleanup. To keep regions from growing without bound,
1310 * we shrink the array of rectangles to match the new number of
1311 * rectangles in the region. This never goes to 0, however...
1313 * Only do this stuff if the number of rectangles allocated is more than
1314 * twice the number of rectangles in the region (a simple optimization...).
1316 if (newReg
->numRects
< (newReg
->size
>> 1))
1318 if (REGION_NOT_EMPTY(newReg
))
1320 BoxPtr prev_rects
= newReg
->rects
;
1321 newReg
->size
= newReg
->numRects
;
1322 newReg
->rects
= (BoxPtr
) realloc ((char *) newReg
->rects
,
1323 (unsigned) (sizeof(BoxRec
) * newReg
->size
));
1324 if (! newReg
->rects
)
1325 newReg
->rects
= prev_rects
;
1330 * No point in doing the extra work involved in an realloc if
1331 * the region is empty
1334 free((char *) newReg
->rects
);
1335 newReg
->rects
= (BoxPtr
) malloc(sizeof(BoxRec
));
1338 free ((char *) oldRects
);
1342 /*======================================================================
1344 *====================================================================*/
1347 *-----------------------------------------------------------------------
1349 * Handle a non-overlapping band for the union operation. Just
1350 * Adds the rectangles into the region. Doesn't have to check for
1351 * subsumption or anything.
1357 * pReg->numRects is incremented and the final rectangles overwritten
1358 * with the rectangles we're passed.
1360 *-----------------------------------------------------------------------
1365 register Region pReg
,
1368 register wxCoord y1
,
1369 register wxCoord y2
)
1371 register BoxPtr pNextRect
;
1373 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1379 assert(r
->x1
< r
->x2
);
1380 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1381 pNextRect
->x1
= r
->x1
;
1383 pNextRect
->x2
= r
->x2
;
1385 pReg
->numRects
+= 1;
1388 assert(pReg
->numRects
<=pReg
->size
);
1391 return 0; /* lint */
1396 *-----------------------------------------------------------------------
1398 * Handle an overlapping band for the union operation. Picks the
1399 * left-most rectangle each time and merges it into the region.
1405 * Rectangles are overwritten in pReg->rects and pReg->numRects will
1408 *-----------------------------------------------------------------------
1414 register Region pReg
,
1419 register wxCoord y1
,
1420 register wxCoord y2
)
1422 register BoxPtr pNextRect
;
1424 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1426 #define MERGERECT(r) \
1427 if ((pReg->numRects != 0) && \
1428 (pNextRect[-1].y1 == y1) && \
1429 (pNextRect[-1].y2 == y2) && \
1430 (pNextRect[-1].x2 >= r->x1)) \
1432 if (pNextRect[-1].x2 < r->x2) \
1434 pNextRect[-1].x2 = r->x2; \
1435 assert(pNextRect[-1].x1<pNextRect[-1].x2); \
1440 MEMCHECK(pReg, pNextRect, pReg->rects); \
1441 pNextRect->y1 = y1; \
1442 pNextRect->y2 = y2; \
1443 pNextRect->x1 = r->x1; \
1444 pNextRect->x2 = r->x2; \
1445 pReg->numRects += 1; \
1448 assert(pReg->numRects<=pReg->size);\
1452 while ((r1
!= r1End
) && (r2
!= r2End
))
1454 if (r1
->x1
< r2
->x1
)
1469 } while (r1
!= r1End
);
1471 else while (r2
!= r2End
)
1475 return 0; /* lint */
1481 Region reg2
, /* source regions */
1482 Region newReg
) /* destination Region */
1484 /* checks all the simple cases */
1487 * Region 1 and 2 are the same or region 1 is empty
1489 if ( (reg1
== reg2
) || (!(reg1
->numRects
)) )
1492 miRegionCopy(newReg
, reg2
);
1497 * if nothing to union (region 2 empty)
1499 if (!(reg2
->numRects
))
1502 miRegionCopy(newReg
, reg1
);
1507 * Region 1 completely subsumes region 2
1509 if ((reg1
->numRects
== 1) &&
1510 (reg1
->extents
.x1
<= reg2
->extents
.x1
) &&
1511 (reg1
->extents
.y1
<= reg2
->extents
.y1
) &&
1512 (reg1
->extents
.x2
>= reg2
->extents
.x2
) &&
1513 (reg1
->extents
.y2
>= reg2
->extents
.y2
))
1516 miRegionCopy(newReg
, reg1
);
1521 * Region 2 completely subsumes region 1
1523 if ((reg2
->numRects
== 1) &&
1524 (reg2
->extents
.x1
<= reg1
->extents
.x1
) &&
1525 (reg2
->extents
.y1
<= reg1
->extents
.y1
) &&
1526 (reg2
->extents
.x2
>= reg1
->extents
.x2
) &&
1527 (reg2
->extents
.y2
>= reg1
->extents
.y2
))
1530 miRegionCopy(newReg
, reg2
);
1534 miRegionOp (newReg
, reg1
, reg2
, miUnionO
,
1535 miUnionNonO
, miUnionNonO
);
1537 newReg
->extents
.x1
= wxMin(reg1
->extents
.x1
, reg2
->extents
.x1
);
1538 newReg
->extents
.y1
= wxMin(reg1
->extents
.y1
, reg2
->extents
.y1
);
1539 newReg
->extents
.x2
= wxMax(reg1
->extents
.x2
, reg2
->extents
.x2
);
1540 newReg
->extents
.y2
= wxMax(reg1
->extents
.y2
, reg2
->extents
.y2
);
1545 /*======================================================================
1546 * Region Subtraction
1547 *====================================================================*/
1550 *-----------------------------------------------------------------------
1552 * Deal with non-overlapping band for subtraction. Any parts from
1553 * region 2 we discard. Anything from region 1 we add to the region.
1559 * pReg may be affected.
1561 *-----------------------------------------------------------------------
1566 register Region pReg
,
1569 register wxCoord y1
,
1570 register wxCoord y2
)
1572 register BoxPtr pNextRect
;
1574 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1580 assert(r
->x1
<r
->x2
);
1581 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1582 pNextRect
->x1
= r
->x1
;
1584 pNextRect
->x2
= r
->x2
;
1586 pReg
->numRects
+= 1;
1589 assert(pReg
->numRects
<= pReg
->size
);
1593 return 0; /* lint */
1597 *-----------------------------------------------------------------------
1599 * Overlapping band subtraction. x1 is the left-most point not yet
1606 * pReg may have rectangles added to it.
1608 *-----------------------------------------------------------------------
1613 register Region pReg
,
1618 register wxCoord y1
,
1619 register wxCoord y2
)
1621 register BoxPtr pNextRect
;
1627 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1629 while ((r1
!= r1End
) && (r2
!= r2End
))
1634 * Subtrahend missed the boat: go to next subtrahend.
1638 else if (r2
->x1
<= x1
)
1641 * Subtrahend preceeds minuend: nuke left edge of minuend.
1647 * Minuend completely covered: advance to next minuend and
1648 * reset left fence to edge of new minuend.
1657 * Subtrahend now used up since it doesn't extend beyond
1663 else if (r2
->x1
< r1
->x2
)
1666 * Left part of subtrahend covers part of minuend: add uncovered
1667 * part of minuend to region and skip to next subtrahend.
1670 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1673 pNextRect
->x2
= r2
->x1
;
1675 pReg
->numRects
+= 1;
1678 assert(pReg
->numRects
<=pReg
->size
);
1684 * Minuend used up: advance to new...
1693 * Subtrahend used up
1701 * Minuend used up: add any remaining piece before advancing.
1705 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1708 pNextRect
->x2
= r1
->x2
;
1710 pReg
->numRects
+= 1;
1712 assert(pReg
->numRects
<=pReg
->size
);
1721 * Add remaining minuend rectangles to region.
1726 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1729 pNextRect
->x2
= r1
->x2
;
1731 pReg
->numRects
+= 1;
1734 assert(pReg
->numRects
<=pReg
->size
);
1742 return 0; /* lint */
1746 *-----------------------------------------------------------------------
1748 * Subtract regS from regM and leave the result in regD.
1749 * S stands for subtrahend, M for minuend and D for difference.
1755 * regD is overwritten.
1757 *-----------------------------------------------------------------------
1764 register Region regD
)
1766 /* check for trivial reject */
1767 if ( (!(regM
->numRects
)) || (!(regS
->numRects
)) ||
1768 (!EXTENTCHECK(®M
->extents
, ®S
->extents
)) )
1770 miRegionCopy(regD
, regM
);
1774 miRegionOp (regD
, regM
, regS
, miSubtractO
,
1775 miSubtractNonO1
, NULL
);
1778 * Can't alter newReg's extents before we call miRegionOp because
1779 * it might be one of the source regions and miRegionOp depends
1780 * on the extents of those regions being the unaltered. Besides, this
1781 * way there's no checking against rectangles that will be nuked
1782 * due to coalescing, so we have to examine fewer rectangles.
1784 miSetExtents (regD
);
1789 XXorRegion(Region sra
, Region srb
, Region dr
)
1793 if ((! (tra
= XCreateRegion())) || (! (trb
= XCreateRegion())))
1795 (void) XSubtractRegion(sra
,srb
,tra
);
1796 (void) XSubtractRegion(srb
,sra
,trb
);
1797 (void) XUnionRegion(tra
,trb
,dr
);
1798 XDestroyRegion(tra
);
1799 XDestroyRegion(trb
);
1804 * Check to see if the region is empty. Assumes a region is passed
1811 if( r
->numRects
== 0 ) return true;
1816 * Check to see if two regions are equal
1819 XEqualRegion(Region r1
, Region r2
)
1823 if( r1
->numRects
!= r2
->numRects
) return false;
1824 else if( r1
->numRects
== 0 ) return true;
1825 else if ( r1
->extents
.x1
!= r2
->extents
.x1
) return false;
1826 else if ( r1
->extents
.x2
!= r2
->extents
.x2
) return false;
1827 else if ( r1
->extents
.y1
!= r2
->extents
.y1
) return false;
1828 else if ( r1
->extents
.y2
!= r2
->extents
.y2
) return false;
1829 else for( i
=0; i
< r1
->numRects
; i
++ ) {
1830 if ( r1
->rects
[i
].x1
!= r2
->rects
[i
].x1
) return false;
1831 else if ( r1
->rects
[i
].x2
!= r2
->rects
[i
].x2
) return false;
1832 else if ( r1
->rects
[i
].y1
!= r2
->rects
[i
].y1
) return false;
1833 else if ( r1
->rects
[i
].y2
!= r2
->rects
[i
].y2
) return false;
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::
1859 register Region region
,
1861 unsigned int rwidth
, unsigned int rheight
)
1863 register BoxPtr pbox
;
1864 register BoxPtr pboxEnd
;
1866 register BoxPtr prect
= &rect
;
1867 int partIn
, partOut
;
1871 prect
->x2
= rwidth
+ rx
;
1872 prect
->y2
= rheight
+ ry
;
1874 /* this is (just) a useful optimization */
1875 if ((region
->numRects
== 0) || !EXTENTCHECK(®ion
->extents
, prect
))
1876 return(wxOutRegion
);
1881 /* can stop when both partOut and partIn are TRUE, or we reach prect->y2 */
1882 for (pbox
= region
->rects
, pboxEnd
= pbox
+ region
->numRects
;
1888 continue; /* getting up to speed or skipping remainder of band */
1892 partOut
= TRUE
; /* missed part of rectangle above */
1893 if (partIn
|| (pbox
->y1
>= prect
->y2
))
1895 ry
= pbox
->y1
; /* x guaranteed to be == prect->x1 */
1899 continue; /* not far enough over yet */
1903 partOut
= TRUE
; /* missed part of rectangle to left */
1908 if (pbox
->x1
< prect
->x2
)
1910 partIn
= TRUE
; /* definitely overlap */
1915 if (pbox
->x2
>= prect
->x2
)
1917 ry
= pbox
->y2
; /* finished with this band */
1918 if (ry
>= prect
->y2
)
1920 rx
= prect
->x1
; /* reset x out to left again */
1924 * Because boxes in a band are maximal width, if the first box
1925 * to overlap the rectangle doesn't completely cover it in that
1926 * band, the rectangle must be partially out, since some of it
1927 * will be uncovered in that band. partIn will have been set true
1935 return(partIn
? ((ry
< prect
->y2
) ? wxPartRegion
: wxInRegion
) :