]>
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 wxObjectRefData
,
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
;
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()
279 wxObjectRefData
*wxRegionGeneric::CreateRefData() const
281 return new wxRegionRefData
;
284 wxObjectRefData
*wxRegionGeneric::CloneRefData(const wxObjectRefData
*data
) const
286 return new wxRegionRefData(*(wxRegionRefData
*)data
);
289 bool wxRegionGeneric::operator== (const wxRegionGeneric
& region
) const
291 wxASSERT(m_refData
&& region
.m_refData
);
292 return REGION::XEqualRegion(M_REGIONDATA
,M_REGIONDATA_OF(region
));
295 wxRect
wxRegionGeneric::GetBox() const
299 REGION::XClipBox(M_REGIONDATA
,&rect
);
303 void wxRegionGeneric::GetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
&w
, wxCoord
&h
) const
307 REGION::XClipBox(M_REGIONDATA
,&rect
);
314 // ----------------------------------------------------------------------------
315 // wxRegionGeneric operations
316 // ----------------------------------------------------------------------------
318 bool wxRegionGeneric::Union(const wxRect
& rect
)
319 /* XUnionRectWithRegion */
321 if (!rect
.width
|| !rect
.height
)
326 return REGION::XUnionRegion(®ion
,M_REGIONDATA
,M_REGIONDATA
);
329 bool wxRegionGeneric::Union(const wxRegionGeneric
& region
)
332 return REGION::XUnionRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
335 bool wxRegionGeneric::Intersect(const wxRect
& rect
)
337 if (!rect
.width
|| !rect
.height
)
342 return REGION::XIntersectRegion(®ion
,M_REGIONDATA
,M_REGIONDATA
);
345 bool wxRegionGeneric::Intersect(const wxRegionGeneric
& region
)
348 return REGION::XIntersectRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
351 bool wxRegionGeneric::Subtract(const wxRect
& rect
)
353 if (!rect
.width
|| !rect
.height
)
358 return REGION::XSubtractRegion(®ion
,M_REGIONDATA
,M_REGIONDATA
);
361 bool wxRegionGeneric::Subtract(const wxRegionGeneric
& region
)
363 return REGION::XSubtractRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
366 bool wxRegionGeneric::Xor(const wxRect
& rect
)
368 if (!rect
.width
|| !rect
.height
)
373 return REGION::XXorRegion(®ion
,M_REGIONDATA
,M_REGIONDATA
);
376 bool wxRegionGeneric::Xor(const wxRegionGeneric
& region
)
379 return REGION::XXorRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
382 bool wxRegionGeneric::Offset(wxCoord x
, wxCoord y
)
385 return REGION::XOffsetRegion(M_REGIONDATA
, x
, y
);
388 // ----------------------------------------------------------------------------
389 // wxRegionGeneric comparison
390 // ----------------------------------------------------------------------------
392 bool wxRegionGeneric::Empty() const
395 return REGION::XEmptyRegion(M_REGIONDATA
);
398 // Does the region contain the point (x,y)?
399 wxRegionContain
wxRegionGeneric::Contains(long x
, long y
) const
402 return REGION::XPointInRegion(M_REGIONDATA
,x
,y
)?wxInRegion
:wxOutRegion
;
405 // Does the region contain the point pt?
406 wxRegionContain
wxRegionGeneric::Contains(const wxPoint
& pt
) const
409 return REGION::XPointInRegion(M_REGIONDATA
,pt
.x
,pt
.y
)?wxInRegion
:wxOutRegion
;
412 // Does the region contain the rectangle (x, y, w, h)?
413 wxRegionContain
wxRegionGeneric::Contains(long x
, long y
, long w
, long h
) const
416 return REGION::XRectInRegion(M_REGIONDATA
,x
,y
,w
,h
);
419 // Does the region contain the rectangle rect?
420 wxRegionContain
wxRegionGeneric::Contains(const wxRect
& rect
) const
423 return REGION::XRectInRegion(M_REGIONDATA
,rect
.x
,rect
.y
,rect
.width
,rect
.height
);
426 // ========================================================================
427 // wxRegionIteratorGeneric
428 // ========================================================================
429 //IMPLEMENT_DYNAMIC_CLASS(wxRegionIteratorGeneric,wxObject)
431 wxRegionIteratorGeneric::wxRegionIteratorGeneric()
436 wxRegionIteratorGeneric::wxRegionIteratorGeneric(const wxRegionGeneric
& region
)
442 wxRegionIteratorGeneric::wxRegionIteratorGeneric(const wxRegionIteratorGeneric
& iterator
)
443 : m_region(iterator
.m_region
)
445 m_current
= iterator
.m_current
;
448 void wxRegionIteratorGeneric::Reset(const wxRegionGeneric
& region
)
454 bool wxRegionIteratorGeneric::HaveRects() const
456 return M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
459 wxRegionIteratorGeneric
& wxRegionIteratorGeneric::operator++()
465 wxRegionIteratorGeneric
wxRegionIteratorGeneric::operator++(int)
467 wxRegionIteratorGeneric
copy(*this);
472 wxRect
wxRegionIteratorGeneric::GetRect() const
474 wxASSERT(m_region
.m_refData
);
475 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
485 long wxRegionIteratorGeneric::GetX() const
487 wxASSERT(m_region
.m_refData
);
488 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
493 long wxRegionIteratorGeneric::GetY() const
495 wxASSERT(m_region
.m_refData
);
496 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
501 long wxRegionIteratorGeneric::GetW() const
503 wxASSERT(m_region
.m_refData
);
504 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
506 return box
->x2
- box
->x1
;
509 long wxRegionIteratorGeneric::GetH() const
511 wxASSERT(m_region
.m_refData
);
512 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
514 return box
->y2
- box
->y1
;
517 wxRegionIteratorGeneric::~wxRegionIteratorGeneric()
522 // ========================================================================
523 // The guts (from X.org)
524 // ========================================================================
526 /************************************************************************
528 Copyright 1987, 1988, 1998 The Open Group
530 Permission to use, copy, modify, distribute, and sell this software and its
531 documentation for any purpose is hereby granted without fee, provided that
532 the above copyright notice appear in all copies and that both that
533 copyright notice and this permission notice appear in supporting
536 The above copyright notice and this permission notice shall be included in
537 all copies or substantial portions of the Software.
539 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
540 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
541 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
542 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
543 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
544 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
546 Except as contained in this notice, the name of The Open Group shall not be
547 used in advertising or otherwise to promote the sale, use or other dealings
548 in this Software without prior written authorization from The Open Group.
551 Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
555 Permission to use, copy, modify, and distribute this software and its
556 documentation for any purpose and without fee is hereby granted,
557 provided that the above copyright notice appear in all copies and that
558 both that copyright notice and this permission notice appear in
559 supporting documentation, and that the name of Digital not be
560 used in advertising or publicity pertaining to distribution of the
561 software without specific, written prior permission.
563 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
564 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
565 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
566 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
567 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
568 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
571 ************************************************************************/
573 /* 1 if two BOXs overlap.
574 * 0 if two BOXs do not overlap.
575 * Remember, x2 and y2 are not in the region
577 #define EXTENTCHECK(r1, r2) \
578 ((r1)->x2 > (r2)->x1 && \
579 (r1)->x1 < (r2)->x2 && \
580 (r1)->y2 > (r2)->y1 && \
584 * Check to see if there is enough memory in the present region.
586 #define MEMCHECK(reg, rect, firstrect){\
587 if ((reg)->numRects >= ((reg)->size - 1)){\
588 (firstrect) = (BOX *) realloc \
589 ((char *)(firstrect), (unsigned) (2 * (sizeof(BOX)) * ((reg)->size)));\
590 if ((firstrect) == 0)\
593 (rect) = &(firstrect)[(reg)->numRects];\
597 #define EMPTY_REGION(pReg) pReg->numRects = 0
599 #define REGION_NOT_EMPTY(pReg) pReg->numRects
601 #define INBOX(r, x, y) \
602 ( ( ((r).x2 > x)) && \
603 ( ((r).x1 <= x)) && \
608 * The functions in this file implement the Region abstraction, similar to one
609 * used in the X11 sample server. A Region is simply an area, as the name
610 * implies, and is implemented as a "y-x-banded" array of rectangles. To
611 * explain: Each Region is made up of a certain number of rectangles sorted
612 * by y coordinate first, and then by x coordinate.
614 * Furthermore, the rectangles are banded such that every rectangle with a
615 * given upper-left y coordinate (y1) will have the same lower-right y
616 * coordinate (y2) and vice versa. If a rectangle has scanlines in a band, it
617 * will span the entire vertical distance of the band. This means that some
618 * areas that could be merged into a taller rectangle will be represented as
619 * several shorter rectangles to account for shorter rectangles to its left
620 * or right but within its "vertical scope".
622 * An added constraint on the rectangles is that they must cover as much
623 * horizontal area as possible. E.g. no two rectangles in a band are allowed
626 * Whenever possible, bands will be merged together to cover a greater vertical
627 * distance (and thus reduce the number of rectangles). Two bands can be merged
628 * only if the bottom of one touches the top of the other and they have
629 * rectangles in the same places (of the same width, of course). This maintains
630 * the y-x-banding that's so nice to have...
633 /* Create a new empty region */
634 Region
REGION::XCreateRegion(void)
636 Region temp
= new REGION
;
639 return (Region
) NULL
;
641 temp
->rects
= ( BOX
* )malloc( (unsigned) sizeof( BOX
));
646 return (Region
) NULL
;
649 temp
->extents
.x1
= 0;
650 temp
->extents
.y1
= 0;
651 temp
->extents
.x2
= 0;
652 temp
->extents
.y2
= 0;
657 bool REGION::XClipBox(Region r
, wxRect
*rect
)
659 rect
->x
= r
->extents
.x1
;
660 rect
->y
= r
->extents
.y1
;
661 rect
->width
= r
->extents
.x2
- r
->extents
.x1
;
662 rect
->height
= r
->extents
.y2
- r
->extents
.y1
;
667 *-----------------------------------------------------------------------
669 * Reset the extents of a region to what they should be. Called by
670 * miSubtract and miIntersect b/c they can't figure it out along the
671 * way or do so easily, as miUnion can.
677 * The region's 'extents' structure is overwritten.
679 *-----------------------------------------------------------------------
682 miSetExtents (Region pReg
)
684 register BoxPtr pBox
,
688 if (pReg
->numRects
== 0)
690 pReg
->extents
.x1
= 0;
691 pReg
->extents
.y1
= 0;
692 pReg
->extents
.x2
= 0;
693 pReg
->extents
.y2
= 0;
697 pExtents
= &pReg
->extents
;
699 pBoxEnd
= &pBox
[pReg
->numRects
- 1];
702 * Since pBox is the first rectangle in the region, it must have the
703 * smallest y1 and since pBoxEnd is the last rectangle in the region,
704 * it must have the largest y2, because of banding. Initialize x1 and
705 * x2 from pBox and pBoxEnd, resp., as good things to initialize them
708 pExtents
->x1
= pBox
->x1
;
709 pExtents
->y1
= pBox
->y1
;
710 pExtents
->x2
= pBoxEnd
->x2
;
711 pExtents
->y2
= pBoxEnd
->y2
;
713 assert(pExtents
->y1
< pExtents
->y2
);
714 while (pBox
<= pBoxEnd
)
716 if (pBox
->x1
< pExtents
->x1
)
718 pExtents
->x1
= pBox
->x1
;
720 if (pBox
->x2
> pExtents
->x2
)
722 pExtents
->x2
= pBox
->x2
;
726 assert(pExtents
->x1
< pExtents
->x2
);
733 free( (char *) r
->rects
);
738 /* TranslateRegion(pRegion, x, y)
745 register Region pRegion
,
752 pbox
= pRegion
->rects
;
753 nbox
= pRegion
->numRects
;
763 pRegion
->extents
.x1
+= x
;
764 pRegion
->extents
.x2
+= x
;
765 pRegion
->extents
.y1
+= y
;
766 pRegion
->extents
.y2
+= y
;
770 /*======================================================================
771 * Region Intersection
772 *====================================================================*/
774 *-----------------------------------------------------------------------
776 * Handle an overlapping band for miIntersect.
782 * Rectangles may be added to the region.
784 *-----------------------------------------------------------------------
789 register Region pReg
,
799 register BoxPtr pNextRect
;
801 pNextRect
= &pReg
->rects
[pReg
->numRects
];
803 while ((r1
!= r1End
) && (r2
!= r2End
))
805 x1
= wxMax(r1
->x1
,r2
->x1
);
806 x2
= wxMin(r1
->x2
,r2
->x2
);
809 * If there's any overlap between the two rectangles, add that
810 * overlap to the new region.
811 * There's no need to check for subsumption because the only way
812 * such a need could arise is if some region has two rectangles
813 * right next to each other. Since that should never happen...
819 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
826 assert(pReg
->numRects
<= pReg
->size
);
830 * Need to advance the pointers. Shift the one that extends
831 * to the right the least, since the other still has a chance to
832 * overlap with that region's next rectangle, if you see what I mean.
838 else if (r2
->x2
< r1
->x2
)
854 Region reg2
, /* source regions */
855 register Region newReg
) /* destination Region */
857 /* check for trivial reject */
858 if ( (!(reg1
->numRects
)) || (!(reg2
->numRects
)) ||
859 (!EXTENTCHECK(®1
->extents
, ®2
->extents
)))
860 newReg
->numRects
= 0;
862 miRegionOp (newReg
, reg1
, reg2
,
863 miIntersectO
, NULL
, NULL
);
866 * Can't alter newReg's extents before we call miRegionOp because
867 * it might be one of the source regions and miRegionOp depends
868 * on the extents of those regions being the same. Besides, this
869 * way there's no checking against rectangles that will be nuked
870 * due to coalescing, so we have to examine fewer rectangles.
872 miSetExtents(newReg
);
878 register Region dstrgn
,
882 if (dstrgn
!= rgn
) /* don't want to copy to itself */
884 if (dstrgn
->size
< rgn
->numRects
)
888 BOX
*prevRects
= dstrgn
->rects
;
890 dstrgn
->rects
= (BOX
*)
891 realloc((char *) dstrgn
->rects
,
892 (unsigned) rgn
->numRects
* (sizeof(BOX
)));
899 dstrgn
->size
= rgn
->numRects
;
901 dstrgn
->numRects
= rgn
->numRects
;
902 dstrgn
->extents
.x1
= rgn
->extents
.x1
;
903 dstrgn
->extents
.y1
= rgn
->extents
.y1
;
904 dstrgn
->extents
.x2
= rgn
->extents
.x2
;
905 dstrgn
->extents
.y2
= rgn
->extents
.y2
;
907 memcpy((char *) dstrgn
->rects
, (char *) rgn
->rects
,
908 (int) (rgn
->numRects
* sizeof(BOX
)));
912 /*======================================================================
913 * Generic Region Operator
914 *====================================================================*/
917 *-----------------------------------------------------------------------
919 * Attempt to merge the boxes in the current band with those in the
920 * previous one. Used only by miRegionOp.
923 * The new index for the previous band.
926 * If coalescing takes place:
927 * - rectangles in the previous band will have their y2 fields
929 * - pReg->numRects will be decreased.
931 *-----------------------------------------------------------------------
936 register Region pReg
, /* Region to coalesce */
937 int prevStart
, /* Index of start of previous band */
938 int curStart
) /* Index of start of current band */
940 register BoxPtr pPrevBox
; /* Current box in previous band */
941 register BoxPtr pCurBox
; /* Current box in current band */
942 register BoxPtr pRegEnd
; /* End of region */
943 int curNumRects
; /* Number of rectangles in current
945 int prevNumRects
; /* Number of rectangles in previous
947 int bandY1
; /* Y1 coordinate for current band */
949 pRegEnd
= &pReg
->rects
[pReg
->numRects
];
951 pPrevBox
= &pReg
->rects
[prevStart
];
952 prevNumRects
= curStart
- prevStart
;
955 * Figure out how many rectangles are in the current band. Have to do
956 * this because multiple bands could have been added in miRegionOp
957 * at the end when one region has been exhausted.
959 pCurBox
= &pReg
->rects
[curStart
];
960 bandY1
= pCurBox
->y1
;
961 for (curNumRects
= 0;
962 (pCurBox
!= pRegEnd
) && (pCurBox
->y1
== bandY1
);
968 if (pCurBox
!= pRegEnd
)
971 * If more than one band was added, we have to find the start
972 * of the last band added so the next coalescing job can start
973 * at the right place... (given when multiple bands are added,
974 * this may be pointless -- see above).
977 while (pRegEnd
[-1].y1
== pRegEnd
->y1
)
981 curStart
= pRegEnd
- pReg
->rects
;
982 pRegEnd
= pReg
->rects
+ pReg
->numRects
;
985 if ((curNumRects
== prevNumRects
) && (curNumRects
!= 0))
987 pCurBox
-= curNumRects
;
989 * The bands may only be coalesced if the bottom of the previous
990 * matches the top scanline of the current.
992 if (pPrevBox
->y2
== pCurBox
->y1
)
995 * Make sure the bands have boxes in the same places. This
996 * assumes that boxes have been added in such a way that they
997 * cover the most area possible. I.e. two boxes in a band must
998 * have some horizontal space between them.
1002 if ((pPrevBox
->x1
!= pCurBox
->x1
) ||
1003 (pPrevBox
->x2
!= pCurBox
->x2
))
1006 * The bands don't line up so they can't be coalesced.
1013 } while (prevNumRects
!= 0);
1015 pReg
->numRects
-= curNumRects
;
1016 pCurBox
-= curNumRects
;
1017 pPrevBox
-= curNumRects
;
1020 * The bands may be merged, so set the bottom y of each box
1021 * in the previous band to that of the corresponding box in
1026 pPrevBox
->y2
= pCurBox
->y2
;
1030 } while (curNumRects
!= 0);
1033 * If only one band was added to the region, we have to backup
1034 * curStart to the start of the previous band.
1036 * If more than one band was added to the region, copy the
1037 * other bands down. The assumption here is that the other bands
1038 * came from the same region as the current one and no further
1039 * coalescing can be done on them since it's all been done
1040 * already... curStart is already in the right place.
1042 if (pCurBox
== pRegEnd
)
1044 curStart
= prevStart
;
1050 *pPrevBox
++ = *pCurBox
++;
1051 } while (pCurBox
!= pRegEnd
);
1060 *-----------------------------------------------------------------------
1062 * Apply an operation to two regions. Called by miUnion, miInverse,
1063 * miSubtract, miIntersect...
1069 * The new region is overwritten.
1072 * The idea behind this function is to view the two regions as sets.
1073 * Together they cover a rectangle of area that this function divides
1074 * into horizontal bands where points are covered only by one region
1075 * or by both. For the first case, the nonOverlapFunc is called with
1076 * each the band and the band's upper and lower extents. For the
1077 * second, the overlapFunc is called to process the entire band. It
1078 * is responsible for clipping the rectangles in the band, though
1079 * this function provides the boundaries.
1080 * At the end of each band, the new region is coalesced, if possible,
1081 * to reduce the number of rectangles in the region.
1083 *-----------------------------------------------------------------------
1088 register Region newReg
, /* Place to store result */
1089 Region reg1
, /* First region in operation */
1090 Region reg2
, /* 2d region in operation */
1092 register Region pReg
,
1098 wxCoord y2
), /* Function to call for over-
1100 int (*nonOverlap1Func
)(
1101 register Region pReg
,
1104 register wxCoord y1
,
1105 register wxCoord y2
), /* Function to call for non-
1106 * overlapping bands in region
1108 int (*nonOverlap2Func
)(
1109 register Region pReg
,
1112 register wxCoord y1
,
1113 register wxCoord y2
)) /* Function to call for non-
1114 * overlapping bands in region
1117 register BoxPtr r1
; /* Pointer into first region */
1118 register BoxPtr r2
; /* Pointer into 2d region */
1119 BoxPtr r1End
; /* End of 1st region */
1120 BoxPtr r2End
; /* End of 2d region */
1121 register wxCoord ybot
; /* Bottom of intersection */
1122 register wxCoord ytop
; /* Top of intersection */
1123 BoxPtr oldRects
; /* Old rects for newReg */
1124 int prevBand
; /* Index of start of
1125 * previous band in newReg */
1126 int curBand
; /* Index of start of current
1128 register BoxPtr r1BandEnd
; /* End of current band in r1 */
1129 register BoxPtr r2BandEnd
; /* End of current band in r2 */
1130 wxCoord top
; /* Top of non-overlapping
1132 wxCoord bot
; /* Bottom of non-overlapping
1137 * set r1, r2, r1End and r2End appropriately, preserve the important
1138 * parts of the destination region until the end in case it's one of
1139 * the two source regions, then mark the "new" region empty, allocating
1140 * another array of rectangles for it to use.
1144 r1End
= r1
+ reg1
->numRects
;
1145 r2End
= r2
+ reg2
->numRects
;
1147 oldRects
= newReg
->rects
;
1149 EMPTY_REGION(newReg
);
1152 * Allocate a reasonable number of rectangles for the new region. The idea
1153 * is to allocate enough so the individual functions don't need to
1154 * reallocate and copy the array, which is time consuming, yet we don't
1155 * have to worry about using too much memory. I hope to be able to
1156 * nuke the realloc() at the end of this function eventually.
1158 newReg
->size
= wxMax(reg1
->numRects
,reg2
->numRects
) * 2;
1160 newReg
->rects
= (BoxPtr
)malloc((unsigned) (sizeof(BoxRec
) * newReg
->size
));
1169 * Initialize ybot and ytop.
1170 * In the upcoming loop, ybot and ytop serve different functions depending
1171 * on whether the band being handled is an overlapping or non-overlapping
1173 * In the case of a non-overlapping band (only one of the regions
1174 * has points in the band), ybot is the bottom of the most recent
1175 * intersection and thus clips the top of the rectangles in that band.
1176 * ytop is the top of the next intersection between the two regions and
1177 * serves to clip the bottom of the rectangles in the current band.
1178 * For an overlapping band (where the two regions intersect), ytop clips
1179 * the top of the rectangles of both regions and ybot clips the bottoms.
1181 if (reg1
->extents
.y1
< reg2
->extents
.y1
)
1182 ybot
= reg1
->extents
.y1
;
1184 ybot
= reg2
->extents
.y1
;
1187 * prevBand serves to mark the start of the previous band so rectangles
1188 * can be coalesced into larger rectangles. qv. miCoalesce, above.
1189 * In the beginning, there is no previous band, so prevBand == curBand
1190 * (curBand is set later on, of course, but the first band will always
1191 * start at index 0). prevBand and curBand must be indices because of
1192 * the possible expansion, and resultant moving, of the new region's
1193 * array of rectangles.
1199 curBand
= newReg
->numRects
;
1202 * This algorithm proceeds one source-band (as opposed to a
1203 * destination band, which is determined by where the two regions
1204 * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
1205 * rectangle after the last one in the current band for their
1206 * respective regions.
1209 while ((r1BandEnd
!= r1End
) && (r1BandEnd
->y1
== r1
->y1
))
1215 while ((r2BandEnd
!= r2End
) && (r2BandEnd
->y1
== r2
->y1
))
1221 * First handle the band that doesn't intersect, if any.
1223 * Note that attention is restricted to one band in the
1224 * non-intersecting region at once, so if a region has n
1225 * bands between the current position and the next place it overlaps
1226 * the other, this entire loop will be passed through n times.
1228 if (r1
->y1
< r2
->y1
)
1230 top
= wxMax(r1
->y1
,ybot
);
1231 bot
= wxMin(r1
->y2
,r2
->y1
);
1233 if ((top
!= bot
) && (nonOverlap1Func
!= NULL
))
1235 (* nonOverlap1Func
) (newReg
, r1
, r1BandEnd
, top
, bot
);
1240 else if (r2
->y1
< r1
->y1
)
1242 top
= wxMax(r2
->y1
,ybot
);
1243 bot
= wxMin(r2
->y2
,r1
->y1
);
1245 if ((top
!= bot
) && (nonOverlap2Func
!= NULL
))
1247 (* nonOverlap2Func
) (newReg
, r2
, r2BandEnd
, top
, bot
);
1258 * If any rectangles got added to the region, try and coalesce them
1259 * with rectangles from the previous band. Note we could just do
1260 * this test in miCoalesce, but some machines incur a not
1261 * inconsiderable cost for function calls, so...
1263 if (newReg
->numRects
!= curBand
)
1265 prevBand
= miCoalesce (newReg
, prevBand
, curBand
);
1269 * Now see if we've hit an intersecting band. The two bands only
1270 * intersect if ybot > ytop
1272 ybot
= wxMin(r1
->y2
, r2
->y2
);
1273 curBand
= newReg
->numRects
;
1276 (* overlapFunc
) (newReg
, r1
, r1BandEnd
, r2
, r2BandEnd
, ytop
, ybot
);
1280 if (newReg
->numRects
!= curBand
)
1282 prevBand
= miCoalesce (newReg
, prevBand
, curBand
);
1286 * If we've finished with a band (y2 == ybot) we skip forward
1287 * in the region to the next band.
1297 } while ((r1
!= r1End
) && (r2
!= r2End
));
1300 * Deal with whichever region still has rectangles left.
1302 curBand
= newReg
->numRects
;
1305 if (nonOverlap1Func
!= NULL
)
1310 while ((r1BandEnd
< r1End
) && (r1BandEnd
->y1
== r1
->y1
))
1314 (* nonOverlap1Func
) (newReg
, r1
, r1BandEnd
,
1315 wxMax(r1
->y1
,ybot
), r1
->y2
);
1317 } while (r1
!= r1End
);
1320 else if ((r2
!= r2End
) && (nonOverlap2Func
!= NULL
))
1325 while ((r2BandEnd
< r2End
) && (r2BandEnd
->y1
== r2
->y1
))
1329 (* nonOverlap2Func
) (newReg
, r2
, r2BandEnd
,
1330 wxMax(r2
->y1
,ybot
), r2
->y2
);
1332 } while (r2
!= r2End
);
1335 if (newReg
->numRects
!= curBand
)
1337 (void) miCoalesce (newReg
, prevBand
, curBand
);
1341 * A bit of cleanup. To keep regions from growing without bound,
1342 * we shrink the array of rectangles to match the new number of
1343 * rectangles in the region. This never goes to 0, however...
1345 * Only do this stuff if the number of rectangles allocated is more than
1346 * twice the number of rectangles in the region (a simple optimization...).
1348 if (newReg
->numRects
< (newReg
->size
>> 1))
1350 if (REGION_NOT_EMPTY(newReg
))
1352 BoxPtr prev_rects
= newReg
->rects
;
1353 newReg
->size
= newReg
->numRects
;
1354 newReg
->rects
= (BoxPtr
) realloc ((char *) newReg
->rects
,
1355 (unsigned) (sizeof(BoxRec
) * newReg
->size
));
1356 if (! newReg
->rects
)
1357 newReg
->rects
= prev_rects
;
1362 * No point in doing the extra work involved in an realloc if
1363 * the region is empty
1366 free((char *) newReg
->rects
);
1367 newReg
->rects
= (BoxPtr
) malloc(sizeof(BoxRec
));
1370 free ((char *) oldRects
);
1374 /*======================================================================
1376 *====================================================================*/
1379 *-----------------------------------------------------------------------
1381 * Handle a non-overlapping band for the union operation. Just
1382 * Adds the rectangles into the region. Doesn't have to check for
1383 * subsumption or anything.
1389 * pReg->numRects is incremented and the final rectangles overwritten
1390 * with the rectangles we're passed.
1392 *-----------------------------------------------------------------------
1397 register Region pReg
,
1400 register wxCoord y1
,
1401 register wxCoord y2
)
1403 register BoxPtr pNextRect
;
1405 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1411 assert(r
->x1
< r
->x2
);
1412 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1413 pNextRect
->x1
= r
->x1
;
1415 pNextRect
->x2
= r
->x2
;
1417 pReg
->numRects
+= 1;
1420 assert(pReg
->numRects
<=pReg
->size
);
1423 return 0; /* lint */
1428 *-----------------------------------------------------------------------
1430 * Handle an overlapping band for the union operation. Picks the
1431 * left-most rectangle each time and merges it into the region.
1437 * Rectangles are overwritten in pReg->rects and pReg->numRects will
1440 *-----------------------------------------------------------------------
1446 register Region pReg
,
1451 register wxCoord y1
,
1452 register wxCoord y2
)
1454 register BoxPtr pNextRect
;
1456 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1458 #define MERGERECT(r) \
1459 if ((pReg->numRects != 0) && \
1460 (pNextRect[-1].y1 == y1) && \
1461 (pNextRect[-1].y2 == y2) && \
1462 (pNextRect[-1].x2 >= r->x1)) \
1464 if (pNextRect[-1].x2 < r->x2) \
1466 pNextRect[-1].x2 = r->x2; \
1467 assert(pNextRect[-1].x1<pNextRect[-1].x2); \
1472 MEMCHECK(pReg, pNextRect, pReg->rects); \
1473 pNextRect->y1 = y1; \
1474 pNextRect->y2 = y2; \
1475 pNextRect->x1 = r->x1; \
1476 pNextRect->x2 = r->x2; \
1477 pReg->numRects += 1; \
1480 assert(pReg->numRects<=pReg->size);\
1484 while ((r1
!= r1End
) && (r2
!= r2End
))
1486 if (r1
->x1
< r2
->x1
)
1501 } while (r1
!= r1End
);
1503 else while (r2
!= r2End
)
1507 return 0; /* lint */
1513 Region reg2
, /* source regions */
1514 Region newReg
) /* destination Region */
1516 /* checks all the simple cases */
1519 * Region 1 and 2 are the same or region 1 is empty
1521 if ( (reg1
== reg2
) || (!(reg1
->numRects
)) )
1524 miRegionCopy(newReg
, reg2
);
1529 * if nothing to union (region 2 empty)
1531 if (!(reg2
->numRects
))
1534 miRegionCopy(newReg
, reg1
);
1539 * Region 1 completely subsumes region 2
1541 if ((reg1
->numRects
== 1) &&
1542 (reg1
->extents
.x1
<= reg2
->extents
.x1
) &&
1543 (reg1
->extents
.y1
<= reg2
->extents
.y1
) &&
1544 (reg1
->extents
.x2
>= reg2
->extents
.x2
) &&
1545 (reg1
->extents
.y2
>= reg2
->extents
.y2
))
1548 miRegionCopy(newReg
, reg1
);
1553 * Region 2 completely subsumes region 1
1555 if ((reg2
->numRects
== 1) &&
1556 (reg2
->extents
.x1
<= reg1
->extents
.x1
) &&
1557 (reg2
->extents
.y1
<= reg1
->extents
.y1
) &&
1558 (reg2
->extents
.x2
>= reg1
->extents
.x2
) &&
1559 (reg2
->extents
.y2
>= reg1
->extents
.y2
))
1562 miRegionCopy(newReg
, reg2
);
1566 miRegionOp (newReg
, reg1
, reg2
, miUnionO
,
1567 miUnionNonO
, miUnionNonO
);
1569 newReg
->extents
.x1
= wxMin(reg1
->extents
.x1
, reg2
->extents
.x1
);
1570 newReg
->extents
.y1
= wxMin(reg1
->extents
.y1
, reg2
->extents
.y1
);
1571 newReg
->extents
.x2
= wxMax(reg1
->extents
.x2
, reg2
->extents
.x2
);
1572 newReg
->extents
.y2
= wxMax(reg1
->extents
.y2
, reg2
->extents
.y2
);
1577 /*======================================================================
1578 * Region Subtraction
1579 *====================================================================*/
1582 *-----------------------------------------------------------------------
1584 * Deal with non-overlapping band for subtraction. Any parts from
1585 * region 2 we discard. Anything from region 1 we add to the region.
1591 * pReg may be affected.
1593 *-----------------------------------------------------------------------
1598 register Region pReg
,
1601 register wxCoord y1
,
1602 register wxCoord y2
)
1604 register BoxPtr pNextRect
;
1606 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1612 assert(r
->x1
<r
->x2
);
1613 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1614 pNextRect
->x1
= r
->x1
;
1616 pNextRect
->x2
= r
->x2
;
1618 pReg
->numRects
+= 1;
1621 assert(pReg
->numRects
<= pReg
->size
);
1625 return 0; /* lint */
1629 *-----------------------------------------------------------------------
1631 * Overlapping band subtraction. x1 is the left-most point not yet
1638 * pReg may have rectangles added to it.
1640 *-----------------------------------------------------------------------
1645 register Region pReg
,
1650 register wxCoord y1
,
1651 register wxCoord y2
)
1653 register BoxPtr pNextRect
;
1659 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1661 while ((r1
!= r1End
) && (r2
!= r2End
))
1666 * Subtrahend missed the boat: go to next subtrahend.
1670 else if (r2
->x1
<= x1
)
1673 * Subtrahend preceeds minuend: nuke left edge of minuend.
1679 * Minuend completely covered: advance to next minuend and
1680 * reset left fence to edge of new minuend.
1689 * Subtrahend now used up since it doesn't extend beyond
1695 else if (r2
->x1
< r1
->x2
)
1698 * Left part of subtrahend covers part of minuend: add uncovered
1699 * part of minuend to region and skip to next subtrahend.
1702 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1705 pNextRect
->x2
= r2
->x1
;
1707 pReg
->numRects
+= 1;
1710 assert(pReg
->numRects
<=pReg
->size
);
1716 * Minuend used up: advance to new...
1725 * Subtrahend used up
1733 * Minuend used up: add any remaining piece before advancing.
1737 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1740 pNextRect
->x2
= r1
->x2
;
1742 pReg
->numRects
+= 1;
1744 assert(pReg
->numRects
<=pReg
->size
);
1753 * Add remaining minuend rectangles to region.
1758 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1761 pNextRect
->x2
= r1
->x2
;
1763 pReg
->numRects
+= 1;
1766 assert(pReg
->numRects
<=pReg
->size
);
1774 return 0; /* lint */
1778 *-----------------------------------------------------------------------
1780 * Subtract regS from regM and leave the result in regD.
1781 * S stands for subtrahend, M for minuend and D for difference.
1787 * regD is overwritten.
1789 *-----------------------------------------------------------------------
1792 bool REGION::XSubtractRegion(Region regM
, Region regS
, register Region regD
)
1794 /* check for trivial reject */
1795 if ( (!(regM
->numRects
)) || (!(regS
->numRects
)) ||
1796 (!EXTENTCHECK(®M
->extents
, ®S
->extents
)) )
1798 miRegionCopy(regD
, regM
);
1802 miRegionOp (regD
, regM
, regS
, miSubtractO
,
1803 miSubtractNonO1
, NULL
);
1806 * Can't alter newReg's extents before we call miRegionOp because
1807 * it might be one of the source regions and miRegionOp depends
1808 * on the extents of those regions being the unaltered. Besides, this
1809 * way there's no checking against rectangles that will be nuked
1810 * due to coalescing, so we have to examine fewer rectangles.
1812 miSetExtents (regD
);
1816 bool REGION::XXorRegion(Region sra
, Region srb
, Region dr
)
1818 Region tra
= XCreateRegion();
1820 wxCHECK_MSG( tra
, false, wxT("region not created") );
1822 Region trb
= XCreateRegion();
1824 wxCHECK_MSG( trb
, false, wxT("region not created") );
1826 (void) XSubtractRegion(sra
,srb
,tra
);
1827 (void) XSubtractRegion(srb
,sra
,trb
);
1828 (void) XUnionRegion(tra
,trb
,dr
);
1829 XDestroyRegion(tra
);
1830 XDestroyRegion(trb
);
1835 * Check to see if the region is empty. Assumes a region is passed
1838 bool REGION::XEmptyRegion(Region r
)
1840 if( r
->numRects
== 0 ) return true;
1845 * Check to see if two regions are equal
1847 bool REGION::XEqualRegion(Region r1
, Region r2
)
1851 if( r1
->numRects
!= r2
->numRects
) return false;
1852 else if( r1
->numRects
== 0 ) return true;
1853 else if ( r1
->extents
.x1
!= r2
->extents
.x1
) return false;
1854 else if ( r1
->extents
.x2
!= r2
->extents
.x2
) return false;
1855 else if ( r1
->extents
.y1
!= r2
->extents
.y1
) return false;
1856 else if ( r1
->extents
.y2
!= r2
->extents
.y2
) return false;
1857 else for( i
=0; i
< r1
->numRects
; i
++ ) {
1858 if ( r1
->rects
[i
].x1
!= r2
->rects
[i
].x1
) return false;
1859 else if ( r1
->rects
[i
].x2
!= r2
->rects
[i
].x2
) return false;
1860 else if ( r1
->rects
[i
].y1
!= r2
->rects
[i
].y1
) return false;
1861 else if ( r1
->rects
[i
].y2
!= r2
->rects
[i
].y2
) return false;
1866 bool REGION::XPointInRegion(Region pRegion
, int x
, int y
)
1870 if (pRegion
->numRects
== 0)
1872 if (!INBOX(pRegion
->extents
, x
, y
))
1874 for (i
=0; i
<pRegion
->numRects
; i
++)
1876 if (INBOX (pRegion
->rects
[i
], x
, y
))
1882 wxRegionContain
REGION::XRectInRegion(register Region region
,
1884 unsigned int rwidth
,
1885 unsigned int rheight
)
1887 register BoxPtr pbox
;
1888 register BoxPtr pboxEnd
;
1890 register BoxPtr prect
= &rect
;
1891 int partIn
, partOut
;
1895 prect
->x2
= rwidth
+ rx
;
1896 prect
->y2
= rheight
+ ry
;
1898 /* this is (just) a useful optimization */
1899 if ((region
->numRects
== 0) || !EXTENTCHECK(®ion
->extents
, prect
))
1900 return(wxOutRegion
);
1905 /* can stop when both partOut and partIn are true, or we reach prect->y2 */
1906 for (pbox
= region
->rects
, pboxEnd
= pbox
+ region
->numRects
;
1912 continue; /* getting up to speed or skipping remainder of band */
1916 partOut
= true; /* missed part of rectangle above */
1917 if (partIn
|| (pbox
->y1
>= prect
->y2
))
1919 ry
= pbox
->y1
; /* x guaranteed to be == prect->x1 */
1923 continue; /* not far enough over yet */
1927 partOut
= true; /* missed part of rectangle to left */
1932 if (pbox
->x1
< prect
->x2
)
1934 partIn
= true; /* definitely overlap */
1939 if (pbox
->x2
>= prect
->x2
)
1941 ry
= pbox
->y2
; /* finished with this band */
1942 if (ry
>= prect
->y2
)
1944 rx
= prect
->x1
; /* reset x out to left again */
1948 * Because boxes in a band are maximal width, if the first box
1949 * to overlap the rectangle doesn't completely cover it in that
1950 * band, the rectangle must be partially out, since some of it
1951 * will be uncovered in that band. partIn will have been set true
1959 return(partIn
? ((ry
< prect
->y2
) ? wxPartRegion
: wxInRegion
) :