]>
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/generic/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 */
639 if (! (temp
= new REGION
))
640 return (Region
) NULL
;
641 if (! (temp
->rects
= ( BOX
* )malloc( (unsigned) sizeof( BOX
)))) {
643 return (Region
) NULL
;
646 temp
->extents
.x1
= 0;
647 temp
->extents
.y1
= 0;
648 temp
->extents
.x2
= 0;
649 temp
->extents
.y2
= 0;
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 if (! (dstrgn
->rects
= (BOX
*)
891 realloc((char *) dstrgn
->rects
,
892 (unsigned) rgn
->numRects
* (sizeof(BOX
)))))
898 dstrgn
->size
= rgn
->numRects
;
900 dstrgn
->numRects
= rgn
->numRects
;
901 dstrgn
->extents
.x1
= rgn
->extents
.x1
;
902 dstrgn
->extents
.y1
= rgn
->extents
.y1
;
903 dstrgn
->extents
.x2
= rgn
->extents
.x2
;
904 dstrgn
->extents
.y2
= rgn
->extents
.y2
;
906 memcpy((char *) dstrgn
->rects
, (char *) rgn
->rects
,
907 (int) (rgn
->numRects
* sizeof(BOX
)));
911 /*======================================================================
912 * Generic Region Operator
913 *====================================================================*/
916 *-----------------------------------------------------------------------
918 * Attempt to merge the boxes in the current band with those in the
919 * previous one. Used only by miRegionOp.
922 * The new index for the previous band.
925 * If coalescing takes place:
926 * - rectangles in the previous band will have their y2 fields
928 * - pReg->numRects will be decreased.
930 *-----------------------------------------------------------------------
935 register Region pReg
, /* Region to coalesce */
936 int prevStart
, /* Index of start of previous band */
937 int curStart
) /* Index of start of current band */
939 register BoxPtr pPrevBox
; /* Current box in previous band */
940 register BoxPtr pCurBox
; /* Current box in current band */
941 register BoxPtr pRegEnd
; /* End of region */
942 int curNumRects
; /* Number of rectangles in current
944 int prevNumRects
; /* Number of rectangles in previous
946 int bandY1
; /* Y1 coordinate for current band */
948 pRegEnd
= &pReg
->rects
[pReg
->numRects
];
950 pPrevBox
= &pReg
->rects
[prevStart
];
951 prevNumRects
= curStart
- prevStart
;
954 * Figure out how many rectangles are in the current band. Have to do
955 * this because multiple bands could have been added in miRegionOp
956 * at the end when one region has been exhausted.
958 pCurBox
= &pReg
->rects
[curStart
];
959 bandY1
= pCurBox
->y1
;
960 for (curNumRects
= 0;
961 (pCurBox
!= pRegEnd
) && (pCurBox
->y1
== bandY1
);
967 if (pCurBox
!= pRegEnd
)
970 * If more than one band was added, we have to find the start
971 * of the last band added so the next coalescing job can start
972 * at the right place... (given when multiple bands are added,
973 * this may be pointless -- see above).
976 while (pRegEnd
[-1].y1
== pRegEnd
->y1
)
980 curStart
= pRegEnd
- pReg
->rects
;
981 pRegEnd
= pReg
->rects
+ pReg
->numRects
;
984 if ((curNumRects
== prevNumRects
) && (curNumRects
!= 0))
986 pCurBox
-= curNumRects
;
988 * The bands may only be coalesced if the bottom of the previous
989 * matches the top scanline of the current.
991 if (pPrevBox
->y2
== pCurBox
->y1
)
994 * Make sure the bands have boxes in the same places. This
995 * assumes that boxes have been added in such a way that they
996 * cover the most area possible. I.e. two boxes in a band must
997 * have some horizontal space between them.
1001 if ((pPrevBox
->x1
!= pCurBox
->x1
) ||
1002 (pPrevBox
->x2
!= pCurBox
->x2
))
1005 * The bands don't line up so they can't be coalesced.
1012 } while (prevNumRects
!= 0);
1014 pReg
->numRects
-= curNumRects
;
1015 pCurBox
-= curNumRects
;
1016 pPrevBox
-= curNumRects
;
1019 * The bands may be merged, so set the bottom y of each box
1020 * in the previous band to that of the corresponding box in
1025 pPrevBox
->y2
= pCurBox
->y2
;
1029 } while (curNumRects
!= 0);
1032 * If only one band was added to the region, we have to backup
1033 * curStart to the start of the previous band.
1035 * If more than one band was added to the region, copy the
1036 * other bands down. The assumption here is that the other bands
1037 * came from the same region as the current one and no further
1038 * coalescing can be done on them since it's all been done
1039 * already... curStart is already in the right place.
1041 if (pCurBox
== pRegEnd
)
1043 curStart
= prevStart
;
1049 *pPrevBox
++ = *pCurBox
++;
1050 } while (pCurBox
!= pRegEnd
);
1059 *-----------------------------------------------------------------------
1061 * Apply an operation to two regions. Called by miUnion, miInverse,
1062 * miSubtract, miIntersect...
1068 * The new region is overwritten.
1071 * The idea behind this function is to view the two regions as sets.
1072 * Together they cover a rectangle of area that this function divides
1073 * into horizontal bands where points are covered only by one region
1074 * or by both. For the first case, the nonOverlapFunc is called with
1075 * each the band and the band's upper and lower extents. For the
1076 * second, the overlapFunc is called to process the entire band. It
1077 * is responsible for clipping the rectangles in the band, though
1078 * this function provides the boundaries.
1079 * At the end of each band, the new region is coalesced, if possible,
1080 * to reduce the number of rectangles in the region.
1082 *-----------------------------------------------------------------------
1087 register Region newReg
, /* Place to store result */
1088 Region reg1
, /* First region in operation */
1089 Region reg2
, /* 2d region in operation */
1091 register Region pReg
,
1097 wxCoord y2
), /* Function to call for over-
1099 int (*nonOverlap1Func
)(
1100 register Region pReg
,
1103 register wxCoord y1
,
1104 register wxCoord y2
), /* Function to call for non-
1105 * overlapping bands in region
1107 int (*nonOverlap2Func
)(
1108 register Region pReg
,
1111 register wxCoord y1
,
1112 register wxCoord y2
)) /* Function to call for non-
1113 * overlapping bands in region
1116 register BoxPtr r1
; /* Pointer into first region */
1117 register BoxPtr r2
; /* Pointer into 2d region */
1118 BoxPtr r1End
; /* End of 1st region */
1119 BoxPtr r2End
; /* End of 2d region */
1120 register wxCoord ybot
; /* Bottom of intersection */
1121 register wxCoord ytop
; /* Top of intersection */
1122 BoxPtr oldRects
; /* Old rects for newReg */
1123 int prevBand
; /* Index of start of
1124 * previous band in newReg */
1125 int curBand
; /* Index of start of current
1127 register BoxPtr r1BandEnd
; /* End of current band in r1 */
1128 register BoxPtr r2BandEnd
; /* End of current band in r2 */
1129 wxCoord top
; /* Top of non-overlapping
1131 wxCoord bot
; /* Bottom of non-overlapping
1136 * set r1, r2, r1End and r2End appropriately, preserve the important
1137 * parts of the destination region until the end in case it's one of
1138 * the two source regions, then mark the "new" region empty, allocating
1139 * another array of rectangles for it to use.
1143 r1End
= r1
+ reg1
->numRects
;
1144 r2End
= r2
+ reg2
->numRects
;
1146 oldRects
= newReg
->rects
;
1148 EMPTY_REGION(newReg
);
1151 * Allocate a reasonable number of rectangles for the new region. The idea
1152 * is to allocate enough so the individual functions don't need to
1153 * reallocate and copy the array, which is time consuming, yet we don't
1154 * have to worry about using too much memory. I hope to be able to
1155 * nuke the realloc() at the end of this function eventually.
1157 newReg
->size
= wxMax(reg1
->numRects
,reg2
->numRects
) * 2;
1159 if (! (newReg
->rects
= (BoxPtr
)
1160 malloc ((unsigned) (sizeof(BoxRec
) * newReg
->size
)))) {
1166 * Initialize ybot and ytop.
1167 * In the upcoming loop, ybot and ytop serve different functions depending
1168 * on whether the band being handled is an overlapping or non-overlapping
1170 * In the case of a non-overlapping band (only one of the regions
1171 * has points in the band), ybot is the bottom of the most recent
1172 * intersection and thus clips the top of the rectangles in that band.
1173 * ytop is the top of the next intersection between the two regions and
1174 * serves to clip the bottom of the rectangles in the current band.
1175 * For an overlapping band (where the two regions intersect), ytop clips
1176 * the top of the rectangles of both regions and ybot clips the bottoms.
1178 if (reg1
->extents
.y1
< reg2
->extents
.y1
)
1179 ybot
= reg1
->extents
.y1
;
1181 ybot
= reg2
->extents
.y1
;
1184 * prevBand serves to mark the start of the previous band so rectangles
1185 * can be coalesced into larger rectangles. qv. miCoalesce, above.
1186 * In the beginning, there is no previous band, so prevBand == curBand
1187 * (curBand is set later on, of course, but the first band will always
1188 * start at index 0). prevBand and curBand must be indices because of
1189 * the possible expansion, and resultant moving, of the new region's
1190 * array of rectangles.
1196 curBand
= newReg
->numRects
;
1199 * This algorithm proceeds one source-band (as opposed to a
1200 * destination band, which is determined by where the two regions
1201 * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
1202 * rectangle after the last one in the current band for their
1203 * respective regions.
1206 while ((r1BandEnd
!= r1End
) && (r1BandEnd
->y1
== r1
->y1
))
1212 while ((r2BandEnd
!= r2End
) && (r2BandEnd
->y1
== r2
->y1
))
1218 * First handle the band that doesn't intersect, if any.
1220 * Note that attention is restricted to one band in the
1221 * non-intersecting region at once, so if a region has n
1222 * bands between the current position and the next place it overlaps
1223 * the other, this entire loop will be passed through n times.
1225 if (r1
->y1
< r2
->y1
)
1227 top
= wxMax(r1
->y1
,ybot
);
1228 bot
= wxMin(r1
->y2
,r2
->y1
);
1230 if ((top
!= bot
) && (nonOverlap1Func
!= NULL
))
1232 (* nonOverlap1Func
) (newReg
, r1
, r1BandEnd
, top
, bot
);
1237 else if (r2
->y1
< r1
->y1
)
1239 top
= wxMax(r2
->y1
,ybot
);
1240 bot
= wxMin(r2
->y2
,r1
->y1
);
1242 if ((top
!= bot
) && (nonOverlap2Func
!= NULL
))
1244 (* nonOverlap2Func
) (newReg
, r2
, r2BandEnd
, top
, bot
);
1255 * If any rectangles got added to the region, try and coalesce them
1256 * with rectangles from the previous band. Note we could just do
1257 * this test in miCoalesce, but some machines incur a not
1258 * inconsiderable cost for function calls, so...
1260 if (newReg
->numRects
!= curBand
)
1262 prevBand
= miCoalesce (newReg
, prevBand
, curBand
);
1266 * Now see if we've hit an intersecting band. The two bands only
1267 * intersect if ybot > ytop
1269 ybot
= wxMin(r1
->y2
, r2
->y2
);
1270 curBand
= newReg
->numRects
;
1273 (* overlapFunc
) (newReg
, r1
, r1BandEnd
, r2
, r2BandEnd
, ytop
, ybot
);
1277 if (newReg
->numRects
!= curBand
)
1279 prevBand
= miCoalesce (newReg
, prevBand
, curBand
);
1283 * If we've finished with a band (y2 == ybot) we skip forward
1284 * in the region to the next band.
1294 } while ((r1
!= r1End
) && (r2
!= r2End
));
1297 * Deal with whichever region still has rectangles left.
1299 curBand
= newReg
->numRects
;
1302 if (nonOverlap1Func
!= NULL
)
1307 while ((r1BandEnd
< r1End
) && (r1BandEnd
->y1
== r1
->y1
))
1311 (* nonOverlap1Func
) (newReg
, r1
, r1BandEnd
,
1312 wxMax(r1
->y1
,ybot
), r1
->y2
);
1314 } while (r1
!= r1End
);
1317 else if ((r2
!= r2End
) && (nonOverlap2Func
!= NULL
))
1322 while ((r2BandEnd
< r2End
) && (r2BandEnd
->y1
== r2
->y1
))
1326 (* nonOverlap2Func
) (newReg
, r2
, r2BandEnd
,
1327 wxMax(r2
->y1
,ybot
), r2
->y2
);
1329 } while (r2
!= r2End
);
1332 if (newReg
->numRects
!= curBand
)
1334 (void) miCoalesce (newReg
, prevBand
, curBand
);
1338 * A bit of cleanup. To keep regions from growing without bound,
1339 * we shrink the array of rectangles to match the new number of
1340 * rectangles in the region. This never goes to 0, however...
1342 * Only do this stuff if the number of rectangles allocated is more than
1343 * twice the number of rectangles in the region (a simple optimization...).
1345 if (newReg
->numRects
< (newReg
->size
>> 1))
1347 if (REGION_NOT_EMPTY(newReg
))
1349 BoxPtr prev_rects
= newReg
->rects
;
1350 newReg
->size
= newReg
->numRects
;
1351 newReg
->rects
= (BoxPtr
) realloc ((char *) newReg
->rects
,
1352 (unsigned) (sizeof(BoxRec
) * newReg
->size
));
1353 if (! newReg
->rects
)
1354 newReg
->rects
= prev_rects
;
1359 * No point in doing the extra work involved in an realloc if
1360 * the region is empty
1363 free((char *) newReg
->rects
);
1364 newReg
->rects
= (BoxPtr
) malloc(sizeof(BoxRec
));
1367 free ((char *) oldRects
);
1371 /*======================================================================
1373 *====================================================================*/
1376 *-----------------------------------------------------------------------
1378 * Handle a non-overlapping band for the union operation. Just
1379 * Adds the rectangles into the region. Doesn't have to check for
1380 * subsumption or anything.
1386 * pReg->numRects is incremented and the final rectangles overwritten
1387 * with the rectangles we're passed.
1389 *-----------------------------------------------------------------------
1394 register Region pReg
,
1397 register wxCoord y1
,
1398 register wxCoord y2
)
1400 register BoxPtr pNextRect
;
1402 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1408 assert(r
->x1
< r
->x2
);
1409 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1410 pNextRect
->x1
= r
->x1
;
1412 pNextRect
->x2
= r
->x2
;
1414 pReg
->numRects
+= 1;
1417 assert(pReg
->numRects
<=pReg
->size
);
1420 return 0; /* lint */
1425 *-----------------------------------------------------------------------
1427 * Handle an overlapping band for the union operation. Picks the
1428 * left-most rectangle each time and merges it into the region.
1434 * Rectangles are overwritten in pReg->rects and pReg->numRects will
1437 *-----------------------------------------------------------------------
1443 register Region pReg
,
1448 register wxCoord y1
,
1449 register wxCoord y2
)
1451 register BoxPtr pNextRect
;
1453 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1455 #define MERGERECT(r) \
1456 if ((pReg->numRects != 0) && \
1457 (pNextRect[-1].y1 == y1) && \
1458 (pNextRect[-1].y2 == y2) && \
1459 (pNextRect[-1].x2 >= r->x1)) \
1461 if (pNextRect[-1].x2 < r->x2) \
1463 pNextRect[-1].x2 = r->x2; \
1464 assert(pNextRect[-1].x1<pNextRect[-1].x2); \
1469 MEMCHECK(pReg, pNextRect, pReg->rects); \
1470 pNextRect->y1 = y1; \
1471 pNextRect->y2 = y2; \
1472 pNextRect->x1 = r->x1; \
1473 pNextRect->x2 = r->x2; \
1474 pReg->numRects += 1; \
1477 assert(pReg->numRects<=pReg->size);\
1481 while ((r1
!= r1End
) && (r2
!= r2End
))
1483 if (r1
->x1
< r2
->x1
)
1498 } while (r1
!= r1End
);
1500 else while (r2
!= r2End
)
1504 return 0; /* lint */
1510 Region reg2
, /* source regions */
1511 Region newReg
) /* destination Region */
1513 /* checks all the simple cases */
1516 * Region 1 and 2 are the same or region 1 is empty
1518 if ( (reg1
== reg2
) || (!(reg1
->numRects
)) )
1521 miRegionCopy(newReg
, reg2
);
1526 * if nothing to union (region 2 empty)
1528 if (!(reg2
->numRects
))
1531 miRegionCopy(newReg
, reg1
);
1536 * Region 1 completely subsumes region 2
1538 if ((reg1
->numRects
== 1) &&
1539 (reg1
->extents
.x1
<= reg2
->extents
.x1
) &&
1540 (reg1
->extents
.y1
<= reg2
->extents
.y1
) &&
1541 (reg1
->extents
.x2
>= reg2
->extents
.x2
) &&
1542 (reg1
->extents
.y2
>= reg2
->extents
.y2
))
1545 miRegionCopy(newReg
, reg1
);
1550 * Region 2 completely subsumes region 1
1552 if ((reg2
->numRects
== 1) &&
1553 (reg2
->extents
.x1
<= reg1
->extents
.x1
) &&
1554 (reg2
->extents
.y1
<= reg1
->extents
.y1
) &&
1555 (reg2
->extents
.x2
>= reg1
->extents
.x2
) &&
1556 (reg2
->extents
.y2
>= reg1
->extents
.y2
))
1559 miRegionCopy(newReg
, reg2
);
1563 miRegionOp (newReg
, reg1
, reg2
, miUnionO
,
1564 miUnionNonO
, miUnionNonO
);
1566 newReg
->extents
.x1
= wxMin(reg1
->extents
.x1
, reg2
->extents
.x1
);
1567 newReg
->extents
.y1
= wxMin(reg1
->extents
.y1
, reg2
->extents
.y1
);
1568 newReg
->extents
.x2
= wxMax(reg1
->extents
.x2
, reg2
->extents
.x2
);
1569 newReg
->extents
.y2
= wxMax(reg1
->extents
.y2
, reg2
->extents
.y2
);
1574 /*======================================================================
1575 * Region Subtraction
1576 *====================================================================*/
1579 *-----------------------------------------------------------------------
1581 * Deal with non-overlapping band for subtraction. Any parts from
1582 * region 2 we discard. Anything from region 1 we add to the region.
1588 * pReg may be affected.
1590 *-----------------------------------------------------------------------
1595 register Region pReg
,
1598 register wxCoord y1
,
1599 register wxCoord y2
)
1601 register BoxPtr pNextRect
;
1603 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1609 assert(r
->x1
<r
->x2
);
1610 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1611 pNextRect
->x1
= r
->x1
;
1613 pNextRect
->x2
= r
->x2
;
1615 pReg
->numRects
+= 1;
1618 assert(pReg
->numRects
<= pReg
->size
);
1622 return 0; /* lint */
1626 *-----------------------------------------------------------------------
1628 * Overlapping band subtraction. x1 is the left-most point not yet
1635 * pReg may have rectangles added to it.
1637 *-----------------------------------------------------------------------
1642 register Region pReg
,
1647 register wxCoord y1
,
1648 register wxCoord y2
)
1650 register BoxPtr pNextRect
;
1656 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1658 while ((r1
!= r1End
) && (r2
!= r2End
))
1663 * Subtrahend missed the boat: go to next subtrahend.
1667 else if (r2
->x1
<= x1
)
1670 * Subtrahend preceeds minuend: nuke left edge of minuend.
1676 * Minuend completely covered: advance to next minuend and
1677 * reset left fence to edge of new minuend.
1686 * Subtrahend now used up since it doesn't extend beyond
1692 else if (r2
->x1
< r1
->x2
)
1695 * Left part of subtrahend covers part of minuend: add uncovered
1696 * part of minuend to region and skip to next subtrahend.
1699 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1702 pNextRect
->x2
= r2
->x1
;
1704 pReg
->numRects
+= 1;
1707 assert(pReg
->numRects
<=pReg
->size
);
1713 * Minuend used up: advance to new...
1722 * Subtrahend used up
1730 * Minuend used up: add any remaining piece before advancing.
1734 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1737 pNextRect
->x2
= r1
->x2
;
1739 pReg
->numRects
+= 1;
1741 assert(pReg
->numRects
<=pReg
->size
);
1750 * Add remaining minuend rectangles to region.
1755 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1758 pNextRect
->x2
= r1
->x2
;
1760 pReg
->numRects
+= 1;
1763 assert(pReg
->numRects
<=pReg
->size
);
1771 return 0; /* lint */
1775 *-----------------------------------------------------------------------
1777 * Subtract regS from regM and leave the result in regD.
1778 * S stands for subtrahend, M for minuend and D for difference.
1784 * regD is overwritten.
1786 *-----------------------------------------------------------------------
1793 register Region regD
)
1795 /* check for trivial reject */
1796 if ( (!(regM
->numRects
)) || (!(regS
->numRects
)) ||
1797 (!EXTENTCHECK(®M
->extents
, ®S
->extents
)) )
1799 miRegionCopy(regD
, regM
);
1803 miRegionOp (regD
, regM
, regS
, miSubtractO
,
1804 miSubtractNonO1
, NULL
);
1807 * Can't alter newReg's extents before we call miRegionOp because
1808 * it might be one of the source regions and miRegionOp depends
1809 * on the extents of those regions being the unaltered. Besides, this
1810 * way there's no checking against rectangles that will be nuked
1811 * due to coalescing, so we have to examine fewer rectangles.
1813 miSetExtents (regD
);
1818 XXorRegion(Region sra
, Region srb
, Region dr
)
1822 if ((! (tra
= XCreateRegion())) || (! (trb
= XCreateRegion())))
1824 (void) XSubtractRegion(sra
,srb
,tra
);
1825 (void) XSubtractRegion(srb
,sra
,trb
);
1826 (void) XUnionRegion(tra
,trb
,dr
);
1827 XDestroyRegion(tra
);
1828 XDestroyRegion(trb
);
1833 * Check to see if the region is empty. Assumes a region is passed
1840 if( r
->numRects
== 0 ) return true;
1845 * Check to see if two regions are equal
1848 XEqualRegion(Region r1
, Region r2
)
1852 if( r1
->numRects
!= r2
->numRects
) return false;
1853 else if( r1
->numRects
== 0 ) return true;
1854 else if ( r1
->extents
.x1
!= r2
->extents
.x1
) return false;
1855 else if ( r1
->extents
.x2
!= r2
->extents
.x2
) return false;
1856 else if ( r1
->extents
.y1
!= r2
->extents
.y1
) return false;
1857 else if ( r1
->extents
.y2
!= r2
->extents
.y2
) return false;
1858 else for( i
=0; i
< r1
->numRects
; i
++ ) {
1859 if ( r1
->rects
[i
].x1
!= r2
->rects
[i
].x1
) return false;
1860 else if ( r1
->rects
[i
].x2
!= r2
->rects
[i
].x2
) return false;
1861 else if ( r1
->rects
[i
].y1
!= r2
->rects
[i
].y1
) return false;
1862 else if ( r1
->rects
[i
].y2
!= r2
->rects
[i
].y2
) return false;
1874 if (pRegion
->numRects
== 0)
1876 if (!INBOX(pRegion
->extents
, x
, y
))
1878 for (i
=0; i
<pRegion
->numRects
; i
++)
1880 if (INBOX (pRegion
->rects
[i
], x
, y
))
1886 wxRegionContain
REGION::
1888 register Region region
,
1890 unsigned int rwidth
, unsigned int rheight
)
1892 register BoxPtr pbox
;
1893 register BoxPtr pboxEnd
;
1895 register BoxPtr prect
= &rect
;
1896 int partIn
, partOut
;
1900 prect
->x2
= rwidth
+ rx
;
1901 prect
->y2
= rheight
+ ry
;
1903 /* this is (just) a useful optimization */
1904 if ((region
->numRects
== 0) || !EXTENTCHECK(®ion
->extents
, prect
))
1905 return(wxOutRegion
);
1910 /* can stop when both partOut and partIn are true, or we reach prect->y2 */
1911 for (pbox
= region
->rects
, pboxEnd
= pbox
+ region
->numRects
;
1917 continue; /* getting up to speed or skipping remainder of band */
1921 partOut
= true; /* missed part of rectangle above */
1922 if (partIn
|| (pbox
->y1
>= prect
->y2
))
1924 ry
= pbox
->y1
; /* x guaranteed to be == prect->x1 */
1928 continue; /* not far enough over yet */
1932 partOut
= true; /* missed part of rectangle to left */
1937 if (pbox
->x1
< prect
->x2
)
1939 partIn
= true; /* definitely overlap */
1944 if (pbox
->x2
>= prect
->x2
)
1946 ry
= pbox
->y2
; /* finished with this band */
1947 if (ry
>= prect
->y2
)
1949 rx
= prect
->x1
; /* reset x out to left again */
1953 * Because boxes in a band are maximal width, if the first box
1954 * to overlap the rectangle doesn't completely cover it in that
1955 * band, the rectangle must be partially out, since some of it
1956 * will be uncovered in that band. partIn will have been set true
1964 return(partIn
? ((ry
< prect
->y2
) ? wxPartRegion
: wxInRegion
) :