]>
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
32 REGION(const wxRect
& rect
)
38 extents
.x2
= rect
.x
+ rect
.width
;
39 extents
.y2
= rect
.y
+ rect
.height
;
45 return i
< numRects
? rects
+ i
: NULL
;
52 static bool XOffsetRegion(
53 register Region pRegion
,
56 static bool XIntersectRegion(
58 Region reg2
, /* source regions */
59 register Region newReg
); /* destination Region */
60 static bool XUnionRegion(
62 Region reg2
, /* source regions */
63 Region newReg
); /* destination Region */
64 static bool XSubtractRegion(
67 register Region regD
);
68 static bool XXorRegion(Region sra
, Region srb
, Region dr
);
69 static bool XEmptyRegion(
71 static bool XEqualRegion(Region r1
, Region r2
);
72 static bool XPointInRegion(
75 static wxRegionContain
XRectInRegion(
76 register Region region
,
78 unsigned int rwidth
, unsigned int rheight
);
81 static Region
XCreateRegion(void);
82 static void miSetExtents (
84 static bool XDestroyRegion(Region r
);
85 static int miIntersectO (
93 static void miRegionCopy(
94 register Region dstrgn
,
96 static int miCoalesce(
97 register Region pReg
, /* Region to coalesce */
98 int prevStart
, /* Index of start of previous band */
99 int curStart
); /* Index of start of current band */
100 static void miRegionOp(
101 register Region newReg
, /* Place to store result */
102 Region reg1
, /* First region in operation */
103 Region reg2
, /* 2d region in operation */
105 register Region pReg
,
111 wxCoord y2
), /* Function to call for over-
113 int (*nonOverlap1Func
)(
114 register Region pReg
,
118 register wxCoord y2
), /* Function to call for non-
119 * overlapping bands in region
121 int (*nonOverlap2Func
)(
122 register Region pReg
,
126 register wxCoord y2
)); /* Function to call for non-
127 * overlapping bands in region
129 static int miUnionNonO (
130 register Region pReg
,
134 register wxCoord y2
);
135 static int miUnionO (
136 register Region pReg
,
142 register wxCoord y2
);
143 static int miSubtractNonO1 (
144 register Region pReg
,
148 register wxCoord y2
);
149 static int miSubtractO (
150 register Region pReg
,
156 register wxCoord y2
);
164 // ========================================================================
166 // ========================================================================
168 class wxRegionRefData
: public wxObjectRefData
,
178 rects
= ( BOX
* )malloc( (unsigned) sizeof( BOX
));
185 wxRegionRefData(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
189 rects
= (BOX
*)malloc(sizeof(BOX
));
192 extents
.x1
= topLeft
.x
;
193 extents
.y1
= topLeft
.y
;
194 extents
.x2
= bottomRight
.x
;
195 extents
.y2
= bottomRight
.y
;
199 wxRegionRefData(const wxRect
& rect
)
203 rects
= (BOX
*)malloc(sizeof(BOX
));
207 wxRegionRefData(const wxRegionRefData
& refData
)
212 numRects
= refData
.numRects
;
213 rects
= (Box
*)malloc(numRects
*sizeof(Box
));
214 memcpy(rects
, refData
.rects
, numRects
*sizeof(Box
));
215 extents
= refData
.extents
;
225 wxRegionRefData(const REGION
&);
228 // ========================================================================
230 // ========================================================================
231 //IMPLEMENT_DYNAMIC_CLASS(wxRegionGeneric, wxGDIObject);
233 #define M_REGIONDATA ((wxRegionRefData *)m_refData)
234 #define M_REGIONDATA_OF(rgn) ((wxRegionRefData *)(rgn.m_refData))
236 // ----------------------------------------------------------------------------
237 // wxRegionGeneric construction
238 // ----------------------------------------------------------------------------
240 wxRegionGeneric::wxRegionGeneric()
244 wxRegionGeneric::~wxRegionGeneric()
248 wxRegionGeneric::wxRegionGeneric(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
250 m_refData
= new wxRegionRefData(wxRect(x
,y
,w
,h
));
253 wxRegionGeneric::wxRegionGeneric(const wxRect
& rect
)
255 m_refData
= new wxRegionRefData(rect
);
258 wxRegionGeneric::wxRegionGeneric(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
260 m_refData
= new wxRegionRefData(topLeft
, bottomRight
);
263 void wxRegionGeneric::Clear()
268 wxObjectRefData
*wxRegionGeneric::CreateRefData() const
270 return new wxRegionRefData
;
273 wxObjectRefData
*wxRegionGeneric::CloneRefData(const wxObjectRefData
*data
) const
275 return new wxRegionRefData(*(wxRegionRefData
*)data
);
278 bool wxRegionGeneric::operator== (const wxRegionGeneric
& region
) const
280 wxASSERT(m_refData
&& region
.m_refData
);
281 return REGION::XEqualRegion(M_REGIONDATA
,M_REGIONDATA_OF(region
));
284 wxRect
wxRegionGeneric::GetBox() const
288 REGION::XClipBox(M_REGIONDATA
,&rect
);
292 void wxRegionGeneric::GetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
&w
, wxCoord
&h
) const
296 REGION::XClipBox(M_REGIONDATA
,&rect
);
303 // ----------------------------------------------------------------------------
304 // wxRegionGeneric operations
305 // ----------------------------------------------------------------------------
307 bool wxRegionGeneric::Union(const wxRect
& rect
)
308 /* XUnionRectWithRegion */
310 if (!rect
.width
|| !rect
.height
)
315 return REGION::XUnionRegion(®ion
,M_REGIONDATA
,M_REGIONDATA
);
318 bool wxRegionGeneric::Union(const wxRegionGeneric
& region
)
321 return REGION::XUnionRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
324 bool wxRegionGeneric::Intersect(const wxRect
& rect
)
326 if (!rect
.width
|| !rect
.height
)
331 return REGION::XIntersectRegion(®ion
,M_REGIONDATA
,M_REGIONDATA
);
334 bool wxRegionGeneric::Intersect(const wxRegionGeneric
& region
)
337 return REGION::XIntersectRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
340 bool wxRegionGeneric::Subtract(const wxRect
& rect
)
342 if (!rect
.width
|| !rect
.height
)
347 return REGION::XSubtractRegion(®ion
,M_REGIONDATA
,M_REGIONDATA
);
350 bool wxRegionGeneric::Subtract(const wxRegionGeneric
& region
)
352 return REGION::XSubtractRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
355 bool wxRegionGeneric::Xor(const wxRect
& rect
)
357 if (!rect
.width
|| !rect
.height
)
362 return REGION::XXorRegion(®ion
,M_REGIONDATA
,M_REGIONDATA
);
365 bool wxRegionGeneric::Xor(const wxRegionGeneric
& region
)
368 return REGION::XXorRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
371 bool wxRegionGeneric::Offset(wxCoord x
, wxCoord y
)
374 return REGION::XOffsetRegion(M_REGIONDATA
, x
, y
);
377 // ----------------------------------------------------------------------------
378 // wxRegionGeneric comparison
379 // ----------------------------------------------------------------------------
381 bool wxRegionGeneric::Empty() const
384 return REGION::XEmptyRegion(M_REGIONDATA
);
387 // Does the region contain the point (x,y)?
388 wxRegionContain
wxRegionGeneric::Contains(long x
, long y
) const
391 return REGION::XPointInRegion(M_REGIONDATA
,x
,y
)?wxInRegion
:wxOutRegion
;
394 // Does the region contain the point pt?
395 wxRegionContain
wxRegionGeneric::Contains(const wxPoint
& pt
) const
398 return REGION::XPointInRegion(M_REGIONDATA
,pt
.x
,pt
.y
)?wxInRegion
:wxOutRegion
;
401 // Does the region contain the rectangle (x, y, w, h)?
402 wxRegionContain
wxRegionGeneric::Contains(long x
, long y
, long w
, long h
) const
405 return REGION::XRectInRegion(M_REGIONDATA
,x
,y
,w
,h
);
408 // Does the region contain the rectangle rect?
409 wxRegionContain
wxRegionGeneric::Contains(const wxRect
& rect
) const
412 return REGION::XRectInRegion(M_REGIONDATA
,rect
.x
,rect
.y
,rect
.width
,rect
.height
);
415 // ========================================================================
416 // wxRegionIteratorGeneric
417 // ========================================================================
418 //IMPLEMENT_DYNAMIC_CLASS(wxRegionIteratorGeneric,wxObject);
420 wxRegionIteratorGeneric::wxRegionIteratorGeneric()
425 wxRegionIteratorGeneric::wxRegionIteratorGeneric(const wxRegionGeneric
& region
)
431 wxRegionIteratorGeneric::wxRegionIteratorGeneric(const wxRegionIteratorGeneric
& iterator
)
432 : m_region(iterator
.m_region
)
434 m_current
= iterator
.m_current
;
437 void wxRegionIteratorGeneric::Reset(const wxRegionGeneric
& region
)
443 bool wxRegionIteratorGeneric::HaveRects() const
445 return M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
448 wxRegionIteratorGeneric
& wxRegionIteratorGeneric::operator++()
454 wxRegionIteratorGeneric
wxRegionIteratorGeneric::operator++(int)
456 wxRegionIteratorGeneric
copy(*this);
461 wxRect
wxRegionIteratorGeneric::GetRect() const
463 wxASSERT(m_region
.m_refData
);
464 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
474 long wxRegionIteratorGeneric::GetX() const
476 wxASSERT(m_region
.m_refData
);
477 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
482 long wxRegionIteratorGeneric::GetY() const
484 wxASSERT(m_region
.m_refData
);
485 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
490 long wxRegionIteratorGeneric::GetW() const
492 wxASSERT(m_region
.m_refData
);
493 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
495 return box
->x2
- box
->x1
;
498 long wxRegionIteratorGeneric::GetH() const
500 wxASSERT(m_region
.m_refData
);
501 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
503 return box
->y2
- box
->y1
;
506 wxRegionIteratorGeneric::~wxRegionIteratorGeneric()
511 // ========================================================================
512 // The guts (from X.org)
513 // ========================================================================
515 /************************************************************************
517 Copyright 1987, 1988, 1998 The Open Group
519 Permission to use, copy, modify, distribute, and sell this software and its
520 documentation for any purpose is hereby granted without fee, provided that
521 the above copyright notice appear in all copies and that both that
522 copyright notice and this permission notice appear in supporting
525 The above copyright notice and this permission notice shall be included in
526 all copies or substantial portions of the Software.
528 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
529 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
530 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
531 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
532 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
533 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
535 Except as contained in this notice, the name of The Open Group shall not be
536 used in advertising or otherwise to promote the sale, use or other dealings
537 in this Software without prior written authorization from The Open Group.
540 Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
544 Permission to use, copy, modify, and distribute this software and its
545 documentation for any purpose and without fee is hereby granted,
546 provided that the above copyright notice appear in all copies and that
547 both that copyright notice and this permission notice appear in
548 supporting documentation, and that the name of Digital not be
549 used in advertising or publicity pertaining to distribution of the
550 software without specific, written prior permission.
552 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
553 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
554 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
555 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
556 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
557 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
560 ************************************************************************/
562 /* 1 if two BOXs overlap.
563 * 0 if two BOXs do not overlap.
564 * Remember, x2 and y2 are not in the region
566 #define EXTENTCHECK(r1, r2) \
567 ((r1)->x2 > (r2)->x1 && \
568 (r1)->x1 < (r2)->x2 && \
569 (r1)->y2 > (r2)->y1 && \
573 * Check to see if there is enough memory in the present region.
575 #define MEMCHECK(reg, rect, firstrect){\
576 if ((reg)->numRects >= ((reg)->size - 1)){\
577 (firstrect) = (BOX *) realloc \
578 ((char *)(firstrect), (unsigned) (2 * (sizeof(BOX)) * ((reg)->size)));\
579 if ((firstrect) == 0)\
582 (rect) = &(firstrect)[(reg)->numRects];\
586 #define EMPTY_REGION(pReg) pReg->numRects = 0
588 #define REGION_NOT_EMPTY(pReg) pReg->numRects
590 #define INBOX(r, x, y) \
591 ( ( ((r).x2 > x)) && \
592 ( ((r).x1 <= x)) && \
597 * The functions in this file implement the Region abstraction, similar to one
598 * used in the X11 sample server. A Region is simply an area, as the name
599 * implies, and is implemented as a "y-x-banded" array of rectangles. To
600 * explain: Each Region is made up of a certain number of rectangles sorted
601 * by y coordinate first, and then by x coordinate.
603 * Furthermore, the rectangles are banded such that every rectangle with a
604 * given upper-left y coordinate (y1) will have the same lower-right y
605 * coordinate (y2) and vice versa. If a rectangle has scanlines in a band, it
606 * will span the entire vertical distance of the band. This means that some
607 * areas that could be merged into a taller rectangle will be represented as
608 * several shorter rectangles to account for shorter rectangles to its left
609 * or right but within its "vertical scope".
611 * An added constraint on the rectangles is that they must cover as much
612 * horizontal area as possible. E.g. no two rectangles in a band are allowed
615 * Whenever possible, bands will be merged together to cover a greater vertical
616 * distance (and thus reduce the number of rectangles). Two bands can be merged
617 * only if the bottom of one touches the top of the other and they have
618 * rectangles in the same places (of the same width, of course). This maintains
619 * the y-x-banding that's so nice to have...
622 /* Create a new empty region */
628 if (! (temp
= new REGION
))
629 return (Region
) NULL
;
630 if (! (temp
->rects
= ( BOX
* )malloc( (unsigned) sizeof( BOX
)))) {
632 return (Region
) NULL
;
635 temp
->extents
.x1
= 0;
636 temp
->extents
.y1
= 0;
637 temp
->extents
.x2
= 0;
638 temp
->extents
.y2
= 0;
648 rect
->x
= r
->extents
.x1
;
649 rect
->y
= r
->extents
.y1
;
650 rect
->width
= r
->extents
.x2
- r
->extents
.x1
;
651 rect
->height
= r
->extents
.y2
- r
->extents
.y1
;
656 *-----------------------------------------------------------------------
658 * Reset the extents of a region to what they should be. Called by
659 * miSubtract and miIntersect b/c they can't figure it out along the
660 * way or do so easily, as miUnion can.
666 * The region's 'extents' structure is overwritten.
668 *-----------------------------------------------------------------------
671 miSetExtents (Region pReg
)
673 register BoxPtr pBox
,
677 if (pReg
->numRects
== 0)
679 pReg
->extents
.x1
= 0;
680 pReg
->extents
.y1
= 0;
681 pReg
->extents
.x2
= 0;
682 pReg
->extents
.y2
= 0;
686 pExtents
= &pReg
->extents
;
688 pBoxEnd
= &pBox
[pReg
->numRects
- 1];
691 * Since pBox is the first rectangle in the region, it must have the
692 * smallest y1 and since pBoxEnd is the last rectangle in the region,
693 * it must have the largest y2, because of banding. Initialize x1 and
694 * x2 from pBox and pBoxEnd, resp., as good things to initialize them
697 pExtents
->x1
= pBox
->x1
;
698 pExtents
->y1
= pBox
->y1
;
699 pExtents
->x2
= pBoxEnd
->x2
;
700 pExtents
->y2
= pBoxEnd
->y2
;
702 assert(pExtents
->y1
< pExtents
->y2
);
703 while (pBox
<= pBoxEnd
)
705 if (pBox
->x1
< pExtents
->x1
)
707 pExtents
->x1
= pBox
->x1
;
709 if (pBox
->x2
> pExtents
->x2
)
711 pExtents
->x2
= pBox
->x2
;
715 assert(pExtents
->x1
< pExtents
->x2
);
722 free( (char *) r
->rects
);
727 /* TranslateRegion(pRegion, x, y)
734 register Region pRegion
,
741 pbox
= pRegion
->rects
;
742 nbox
= pRegion
->numRects
;
752 pRegion
->extents
.x1
+= x
;
753 pRegion
->extents
.x2
+= x
;
754 pRegion
->extents
.y1
+= y
;
755 pRegion
->extents
.y2
+= y
;
759 /*======================================================================
760 * Region Intersection
761 *====================================================================*/
763 *-----------------------------------------------------------------------
765 * Handle an overlapping band for miIntersect.
771 * Rectangles may be added to the region.
773 *-----------------------------------------------------------------------
778 register Region pReg
,
788 register BoxPtr pNextRect
;
790 pNextRect
= &pReg
->rects
[pReg
->numRects
];
792 while ((r1
!= r1End
) && (r2
!= r2End
))
794 x1
= wxMax(r1
->x1
,r2
->x1
);
795 x2
= wxMin(r1
->x2
,r2
->x2
);
798 * If there's any overlap between the two rectangles, add that
799 * overlap to the new region.
800 * There's no need to check for subsumption because the only way
801 * such a need could arise is if some region has two rectangles
802 * right next to each other. Since that should never happen...
808 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
815 assert(pReg
->numRects
<= pReg
->size
);
819 * Need to advance the pointers. Shift the one that extends
820 * to the right the least, since the other still has a chance to
821 * overlap with that region's next rectangle, if you see what I mean.
827 else if (r2
->x2
< r1
->x2
)
843 Region reg2
, /* source regions */
844 register Region newReg
) /* destination Region */
846 /* check for trivial reject */
847 if ( (!(reg1
->numRects
)) || (!(reg2
->numRects
)) ||
848 (!EXTENTCHECK(®1
->extents
, ®2
->extents
)))
849 newReg
->numRects
= 0;
851 miRegionOp (newReg
, reg1
, reg2
,
852 miIntersectO
, NULL
, NULL
);
855 * Can't alter newReg's extents before we call miRegionOp because
856 * it might be one of the source regions and miRegionOp depends
857 * on the extents of those regions being the same. Besides, this
858 * way there's no checking against rectangles that will be nuked
859 * due to coalescing, so we have to examine fewer rectangles.
861 miSetExtents(newReg
);
867 register Region dstrgn
,
871 if (dstrgn
!= rgn
) /* don't want to copy to itself */
873 if (dstrgn
->size
< rgn
->numRects
)
877 BOX
*prevRects
= dstrgn
->rects
;
879 if (! (dstrgn
->rects
= (BOX
*)
880 realloc((char *) dstrgn
->rects
,
881 (unsigned) rgn
->numRects
* (sizeof(BOX
)))))
887 dstrgn
->size
= rgn
->numRects
;
889 dstrgn
->numRects
= rgn
->numRects
;
890 dstrgn
->extents
.x1
= rgn
->extents
.x1
;
891 dstrgn
->extents
.y1
= rgn
->extents
.y1
;
892 dstrgn
->extents
.x2
= rgn
->extents
.x2
;
893 dstrgn
->extents
.y2
= rgn
->extents
.y2
;
895 memcpy((char *) dstrgn
->rects
, (char *) rgn
->rects
,
896 (int) (rgn
->numRects
* sizeof(BOX
)));
900 /*======================================================================
901 * Generic Region Operator
902 *====================================================================*/
905 *-----------------------------------------------------------------------
907 * Attempt to merge the boxes in the current band with those in the
908 * previous one. Used only by miRegionOp.
911 * The new index for the previous band.
914 * If coalescing takes place:
915 * - rectangles in the previous band will have their y2 fields
917 * - pReg->numRects will be decreased.
919 *-----------------------------------------------------------------------
924 register Region pReg
, /* Region to coalesce */
925 int prevStart
, /* Index of start of previous band */
926 int curStart
) /* Index of start of current band */
928 register BoxPtr pPrevBox
; /* Current box in previous band */
929 register BoxPtr pCurBox
; /* Current box in current band */
930 register BoxPtr pRegEnd
; /* End of region */
931 int curNumRects
; /* Number of rectangles in current
933 int prevNumRects
; /* Number of rectangles in previous
935 int bandY1
; /* Y1 coordinate for current band */
937 pRegEnd
= &pReg
->rects
[pReg
->numRects
];
939 pPrevBox
= &pReg
->rects
[prevStart
];
940 prevNumRects
= curStart
- prevStart
;
943 * Figure out how many rectangles are in the current band. Have to do
944 * this because multiple bands could have been added in miRegionOp
945 * at the end when one region has been exhausted.
947 pCurBox
= &pReg
->rects
[curStart
];
948 bandY1
= pCurBox
->y1
;
949 for (curNumRects
= 0;
950 (pCurBox
!= pRegEnd
) && (pCurBox
->y1
== bandY1
);
956 if (pCurBox
!= pRegEnd
)
959 * If more than one band was added, we have to find the start
960 * of the last band added so the next coalescing job can start
961 * at the right place... (given when multiple bands are added,
962 * this may be pointless -- see above).
965 while (pRegEnd
[-1].y1
== pRegEnd
->y1
)
969 curStart
= pRegEnd
- pReg
->rects
;
970 pRegEnd
= pReg
->rects
+ pReg
->numRects
;
973 if ((curNumRects
== prevNumRects
) && (curNumRects
!= 0))
975 pCurBox
-= curNumRects
;
977 * The bands may only be coalesced if the bottom of the previous
978 * matches the top scanline of the current.
980 if (pPrevBox
->y2
== pCurBox
->y1
)
983 * Make sure the bands have boxes in the same places. This
984 * assumes that boxes have been added in such a way that they
985 * cover the most area possible. I.e. two boxes in a band must
986 * have some horizontal space between them.
990 if ((pPrevBox
->x1
!= pCurBox
->x1
) ||
991 (pPrevBox
->x2
!= pCurBox
->x2
))
994 * The bands don't line up so they can't be coalesced.
1001 } while (prevNumRects
!= 0);
1003 pReg
->numRects
-= curNumRects
;
1004 pCurBox
-= curNumRects
;
1005 pPrevBox
-= curNumRects
;
1008 * The bands may be merged, so set the bottom y of each box
1009 * in the previous band to that of the corresponding box in
1014 pPrevBox
->y2
= pCurBox
->y2
;
1018 } while (curNumRects
!= 0);
1021 * If only one band was added to the region, we have to backup
1022 * curStart to the start of the previous band.
1024 * If more than one band was added to the region, copy the
1025 * other bands down. The assumption here is that the other bands
1026 * came from the same region as the current one and no further
1027 * coalescing can be done on them since it's all been done
1028 * already... curStart is already in the right place.
1030 if (pCurBox
== pRegEnd
)
1032 curStart
= prevStart
;
1038 *pPrevBox
++ = *pCurBox
++;
1039 } while (pCurBox
!= pRegEnd
);
1048 *-----------------------------------------------------------------------
1050 * Apply an operation to two regions. Called by miUnion, miInverse,
1051 * miSubtract, miIntersect...
1057 * The new region is overwritten.
1060 * The idea behind this function is to view the two regions as sets.
1061 * Together they cover a rectangle of area that this function divides
1062 * into horizontal bands where points are covered only by one region
1063 * or by both. For the first case, the nonOverlapFunc is called with
1064 * each the band and the band's upper and lower extents. For the
1065 * second, the overlapFunc is called to process the entire band. It
1066 * is responsible for clipping the rectangles in the band, though
1067 * this function provides the boundaries.
1068 * At the end of each band, the new region is coalesced, if possible,
1069 * to reduce the number of rectangles in the region.
1071 *-----------------------------------------------------------------------
1076 register Region newReg
, /* Place to store result */
1077 Region reg1
, /* First region in operation */
1078 Region reg2
, /* 2d region in operation */
1080 register Region pReg
,
1086 wxCoord y2
), /* Function to call for over-
1088 int (*nonOverlap1Func
)(
1089 register Region pReg
,
1092 register wxCoord y1
,
1093 register wxCoord y2
), /* Function to call for non-
1094 * overlapping bands in region
1096 int (*nonOverlap2Func
)(
1097 register Region pReg
,
1100 register wxCoord y1
,
1101 register wxCoord y2
)) /* Function to call for non-
1102 * overlapping bands in region
1105 register BoxPtr r1
; /* Pointer into first region */
1106 register BoxPtr r2
; /* Pointer into 2d region */
1107 BoxPtr r1End
; /* End of 1st region */
1108 BoxPtr r2End
; /* End of 2d region */
1109 register wxCoord ybot
; /* Bottom of intersection */
1110 register wxCoord ytop
; /* Top of intersection */
1111 BoxPtr oldRects
; /* Old rects for newReg */
1112 int prevBand
; /* Index of start of
1113 * previous band in newReg */
1114 int curBand
; /* Index of start of current
1116 register BoxPtr r1BandEnd
; /* End of current band in r1 */
1117 register BoxPtr r2BandEnd
; /* End of current band in r2 */
1118 wxCoord top
; /* Top of non-overlapping
1120 wxCoord bot
; /* Bottom of non-overlapping
1125 * set r1, r2, r1End and r2End appropriately, preserve the important
1126 * parts of the destination region until the end in case it's one of
1127 * the two source regions, then mark the "new" region empty, allocating
1128 * another array of rectangles for it to use.
1132 r1End
= r1
+ reg1
->numRects
;
1133 r2End
= r2
+ reg2
->numRects
;
1135 oldRects
= newReg
->rects
;
1137 EMPTY_REGION(newReg
);
1140 * Allocate a reasonable number of rectangles for the new region. The idea
1141 * is to allocate enough so the individual functions don't need to
1142 * reallocate and copy the array, which is time consuming, yet we don't
1143 * have to worry about using too much memory. I hope to be able to
1144 * nuke the realloc() at the end of this function eventually.
1146 newReg
->size
= wxMax(reg1
->numRects
,reg2
->numRects
) * 2;
1148 if (! (newReg
->rects
= (BoxPtr
)
1149 malloc ((unsigned) (sizeof(BoxRec
) * newReg
->size
)))) {
1155 * Initialize ybot and ytop.
1156 * In the upcoming loop, ybot and ytop serve different functions depending
1157 * on whether the band being handled is an overlapping or non-overlapping
1159 * In the case of a non-overlapping band (only one of the regions
1160 * has points in the band), ybot is the bottom of the most recent
1161 * intersection and thus clips the top of the rectangles in that band.
1162 * ytop is the top of the next intersection between the two regions and
1163 * serves to clip the bottom of the rectangles in the current band.
1164 * For an overlapping band (where the two regions intersect), ytop clips
1165 * the top of the rectangles of both regions and ybot clips the bottoms.
1167 if (reg1
->extents
.y1
< reg2
->extents
.y1
)
1168 ybot
= reg1
->extents
.y1
;
1170 ybot
= reg2
->extents
.y1
;
1173 * prevBand serves to mark the start of the previous band so rectangles
1174 * can be coalesced into larger rectangles. qv. miCoalesce, above.
1175 * In the beginning, there is no previous band, so prevBand == curBand
1176 * (curBand is set later on, of course, but the first band will always
1177 * start at index 0). prevBand and curBand must be indices because of
1178 * the possible expansion, and resultant moving, of the new region's
1179 * array of rectangles.
1185 curBand
= newReg
->numRects
;
1188 * This algorithm proceeds one source-band (as opposed to a
1189 * destination band, which is determined by where the two regions
1190 * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
1191 * rectangle after the last one in the current band for their
1192 * respective regions.
1195 while ((r1BandEnd
!= r1End
) && (r1BandEnd
->y1
== r1
->y1
))
1201 while ((r2BandEnd
!= r2End
) && (r2BandEnd
->y1
== r2
->y1
))
1207 * First handle the band that doesn't intersect, if any.
1209 * Note that attention is restricted to one band in the
1210 * non-intersecting region at once, so if a region has n
1211 * bands between the current position and the next place it overlaps
1212 * the other, this entire loop will be passed through n times.
1214 if (r1
->y1
< r2
->y1
)
1216 top
= wxMax(r1
->y1
,ybot
);
1217 bot
= wxMin(r1
->y2
,r2
->y1
);
1219 if ((top
!= bot
) && (nonOverlap1Func
!= NULL
))
1221 (* nonOverlap1Func
) (newReg
, r1
, r1BandEnd
, top
, bot
);
1226 else if (r2
->y1
< r1
->y1
)
1228 top
= wxMax(r2
->y1
,ybot
);
1229 bot
= wxMin(r2
->y2
,r1
->y1
);
1231 if ((top
!= bot
) && (nonOverlap2Func
!= NULL
))
1233 (* nonOverlap2Func
) (newReg
, r2
, r2BandEnd
, top
, bot
);
1244 * If any rectangles got added to the region, try and coalesce them
1245 * with rectangles from the previous band. Note we could just do
1246 * this test in miCoalesce, but some machines incur a not
1247 * inconsiderable cost for function calls, so...
1249 if (newReg
->numRects
!= curBand
)
1251 prevBand
= miCoalesce (newReg
, prevBand
, curBand
);
1255 * Now see if we've hit an intersecting band. The two bands only
1256 * intersect if ybot > ytop
1258 ybot
= wxMin(r1
->y2
, r2
->y2
);
1259 curBand
= newReg
->numRects
;
1262 (* overlapFunc
) (newReg
, r1
, r1BandEnd
, r2
, r2BandEnd
, ytop
, ybot
);
1266 if (newReg
->numRects
!= curBand
)
1268 prevBand
= miCoalesce (newReg
, prevBand
, curBand
);
1272 * If we've finished with a band (y2 == ybot) we skip forward
1273 * in the region to the next band.
1283 } while ((r1
!= r1End
) && (r2
!= r2End
));
1286 * Deal with whichever region still has rectangles left.
1288 curBand
= newReg
->numRects
;
1291 if (nonOverlap1Func
!= NULL
)
1296 while ((r1BandEnd
< r1End
) && (r1BandEnd
->y1
== r1
->y1
))
1300 (* nonOverlap1Func
) (newReg
, r1
, r1BandEnd
,
1301 wxMax(r1
->y1
,ybot
), r1
->y2
);
1303 } while (r1
!= r1End
);
1306 else if ((r2
!= r2End
) && (nonOverlap2Func
!= NULL
))
1311 while ((r2BandEnd
< r2End
) && (r2BandEnd
->y1
== r2
->y1
))
1315 (* nonOverlap2Func
) (newReg
, r2
, r2BandEnd
,
1316 wxMax(r2
->y1
,ybot
), r2
->y2
);
1318 } while (r2
!= r2End
);
1321 if (newReg
->numRects
!= curBand
)
1323 (void) miCoalesce (newReg
, prevBand
, curBand
);
1327 * A bit of cleanup. To keep regions from growing without bound,
1328 * we shrink the array of rectangles to match the new number of
1329 * rectangles in the region. This never goes to 0, however...
1331 * Only do this stuff if the number of rectangles allocated is more than
1332 * twice the number of rectangles in the region (a simple optimization...).
1334 if (newReg
->numRects
< (newReg
->size
>> 1))
1336 if (REGION_NOT_EMPTY(newReg
))
1338 BoxPtr prev_rects
= newReg
->rects
;
1339 newReg
->size
= newReg
->numRects
;
1340 newReg
->rects
= (BoxPtr
) realloc ((char *) newReg
->rects
,
1341 (unsigned) (sizeof(BoxRec
) * newReg
->size
));
1342 if (! newReg
->rects
)
1343 newReg
->rects
= prev_rects
;
1348 * No point in doing the extra work involved in an realloc if
1349 * the region is empty
1352 free((char *) newReg
->rects
);
1353 newReg
->rects
= (BoxPtr
) malloc(sizeof(BoxRec
));
1356 free ((char *) oldRects
);
1360 /*======================================================================
1362 *====================================================================*/
1365 *-----------------------------------------------------------------------
1367 * Handle a non-overlapping band for the union operation. Just
1368 * Adds the rectangles into the region. Doesn't have to check for
1369 * subsumption or anything.
1375 * pReg->numRects is incremented and the final rectangles overwritten
1376 * with the rectangles we're passed.
1378 *-----------------------------------------------------------------------
1383 register Region pReg
,
1386 register wxCoord y1
,
1387 register wxCoord y2
)
1389 register BoxPtr pNextRect
;
1391 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1397 assert(r
->x1
< r
->x2
);
1398 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1399 pNextRect
->x1
= r
->x1
;
1401 pNextRect
->x2
= r
->x2
;
1403 pReg
->numRects
+= 1;
1406 assert(pReg
->numRects
<=pReg
->size
);
1409 return 0; /* lint */
1414 *-----------------------------------------------------------------------
1416 * Handle an overlapping band for the union operation. Picks the
1417 * left-most rectangle each time and merges it into the region.
1423 * Rectangles are overwritten in pReg->rects and pReg->numRects will
1426 *-----------------------------------------------------------------------
1432 register Region pReg
,
1437 register wxCoord y1
,
1438 register wxCoord y2
)
1440 register BoxPtr pNextRect
;
1442 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1444 #define MERGERECT(r) \
1445 if ((pReg->numRects != 0) && \
1446 (pNextRect[-1].y1 == y1) && \
1447 (pNextRect[-1].y2 == y2) && \
1448 (pNextRect[-1].x2 >= r->x1)) \
1450 if (pNextRect[-1].x2 < r->x2) \
1452 pNextRect[-1].x2 = r->x2; \
1453 assert(pNextRect[-1].x1<pNextRect[-1].x2); \
1458 MEMCHECK(pReg, pNextRect, pReg->rects); \
1459 pNextRect->y1 = y1; \
1460 pNextRect->y2 = y2; \
1461 pNextRect->x1 = r->x1; \
1462 pNextRect->x2 = r->x2; \
1463 pReg->numRects += 1; \
1466 assert(pReg->numRects<=pReg->size);\
1470 while ((r1
!= r1End
) && (r2
!= r2End
))
1472 if (r1
->x1
< r2
->x1
)
1487 } while (r1
!= r1End
);
1489 else while (r2
!= r2End
)
1493 return 0; /* lint */
1499 Region reg2
, /* source regions */
1500 Region newReg
) /* destination Region */
1502 /* checks all the simple cases */
1505 * Region 1 and 2 are the same or region 1 is empty
1507 if ( (reg1
== reg2
) || (!(reg1
->numRects
)) )
1510 miRegionCopy(newReg
, reg2
);
1515 * if nothing to union (region 2 empty)
1517 if (!(reg2
->numRects
))
1520 miRegionCopy(newReg
, reg1
);
1525 * Region 1 completely subsumes region 2
1527 if ((reg1
->numRects
== 1) &&
1528 (reg1
->extents
.x1
<= reg2
->extents
.x1
) &&
1529 (reg1
->extents
.y1
<= reg2
->extents
.y1
) &&
1530 (reg1
->extents
.x2
>= reg2
->extents
.x2
) &&
1531 (reg1
->extents
.y2
>= reg2
->extents
.y2
))
1534 miRegionCopy(newReg
, reg1
);
1539 * Region 2 completely subsumes region 1
1541 if ((reg2
->numRects
== 1) &&
1542 (reg2
->extents
.x1
<= reg1
->extents
.x1
) &&
1543 (reg2
->extents
.y1
<= reg1
->extents
.y1
) &&
1544 (reg2
->extents
.x2
>= reg1
->extents
.x2
) &&
1545 (reg2
->extents
.y2
>= reg1
->extents
.y2
))
1548 miRegionCopy(newReg
, reg2
);
1552 miRegionOp (newReg
, reg1
, reg2
, miUnionO
,
1553 miUnionNonO
, miUnionNonO
);
1555 newReg
->extents
.x1
= wxMin(reg1
->extents
.x1
, reg2
->extents
.x1
);
1556 newReg
->extents
.y1
= wxMin(reg1
->extents
.y1
, reg2
->extents
.y1
);
1557 newReg
->extents
.x2
= wxMax(reg1
->extents
.x2
, reg2
->extents
.x2
);
1558 newReg
->extents
.y2
= wxMax(reg1
->extents
.y2
, reg2
->extents
.y2
);
1563 /*======================================================================
1564 * Region Subtraction
1565 *====================================================================*/
1568 *-----------------------------------------------------------------------
1570 * Deal with non-overlapping band for subtraction. Any parts from
1571 * region 2 we discard. Anything from region 1 we add to the region.
1577 * pReg may be affected.
1579 *-----------------------------------------------------------------------
1584 register Region pReg
,
1587 register wxCoord y1
,
1588 register wxCoord y2
)
1590 register BoxPtr pNextRect
;
1592 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1598 assert(r
->x1
<r
->x2
);
1599 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1600 pNextRect
->x1
= r
->x1
;
1602 pNextRect
->x2
= r
->x2
;
1604 pReg
->numRects
+= 1;
1607 assert(pReg
->numRects
<= pReg
->size
);
1611 return 0; /* lint */
1615 *-----------------------------------------------------------------------
1617 * Overlapping band subtraction. x1 is the left-most point not yet
1624 * pReg may have rectangles added to it.
1626 *-----------------------------------------------------------------------
1631 register Region pReg
,
1636 register wxCoord y1
,
1637 register wxCoord y2
)
1639 register BoxPtr pNextRect
;
1645 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1647 while ((r1
!= r1End
) && (r2
!= r2End
))
1652 * Subtrahend missed the boat: go to next subtrahend.
1656 else if (r2
->x1
<= x1
)
1659 * Subtrahend preceeds minuend: nuke left edge of minuend.
1665 * Minuend completely covered: advance to next minuend and
1666 * reset left fence to edge of new minuend.
1675 * Subtrahend now used up since it doesn't extend beyond
1681 else if (r2
->x1
< r1
->x2
)
1684 * Left part of subtrahend covers part of minuend: add uncovered
1685 * part of minuend to region and skip to next subtrahend.
1688 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1691 pNextRect
->x2
= r2
->x1
;
1693 pReg
->numRects
+= 1;
1696 assert(pReg
->numRects
<=pReg
->size
);
1702 * Minuend used up: advance to new...
1711 * Subtrahend used up
1719 * Minuend used up: add any remaining piece before advancing.
1723 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1726 pNextRect
->x2
= r1
->x2
;
1728 pReg
->numRects
+= 1;
1730 assert(pReg
->numRects
<=pReg
->size
);
1739 * Add remaining minuend rectangles to region.
1744 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1747 pNextRect
->x2
= r1
->x2
;
1749 pReg
->numRects
+= 1;
1752 assert(pReg
->numRects
<=pReg
->size
);
1760 return 0; /* lint */
1764 *-----------------------------------------------------------------------
1766 * Subtract regS from regM and leave the result in regD.
1767 * S stands for subtrahend, M for minuend and D for difference.
1773 * regD is overwritten.
1775 *-----------------------------------------------------------------------
1782 register Region regD
)
1784 /* check for trivial reject */
1785 if ( (!(regM
->numRects
)) || (!(regS
->numRects
)) ||
1786 (!EXTENTCHECK(®M
->extents
, ®S
->extents
)) )
1788 miRegionCopy(regD
, regM
);
1792 miRegionOp (regD
, regM
, regS
, miSubtractO
,
1793 miSubtractNonO1
, NULL
);
1796 * Can't alter newReg's extents before we call miRegionOp because
1797 * it might be one of the source regions and miRegionOp depends
1798 * on the extents of those regions being the unaltered. Besides, this
1799 * way there's no checking against rectangles that will be nuked
1800 * due to coalescing, so we have to examine fewer rectangles.
1802 miSetExtents (regD
);
1807 XXorRegion(Region sra
, Region srb
, Region dr
)
1811 if ((! (tra
= XCreateRegion())) || (! (trb
= XCreateRegion())))
1813 (void) XSubtractRegion(sra
,srb
,tra
);
1814 (void) XSubtractRegion(srb
,sra
,trb
);
1815 (void) XUnionRegion(tra
,trb
,dr
);
1816 XDestroyRegion(tra
);
1817 XDestroyRegion(trb
);
1822 * Check to see if the region is empty. Assumes a region is passed
1829 if( r
->numRects
== 0 ) return true;
1834 * Check to see if two regions are equal
1837 XEqualRegion(Region r1
, Region r2
)
1841 if( r1
->numRects
!= r2
->numRects
) return false;
1842 else if( r1
->numRects
== 0 ) return true;
1843 else if ( r1
->extents
.x1
!= r2
->extents
.x1
) return false;
1844 else if ( r1
->extents
.x2
!= r2
->extents
.x2
) return false;
1845 else if ( r1
->extents
.y1
!= r2
->extents
.y1
) return false;
1846 else if ( r1
->extents
.y2
!= r2
->extents
.y2
) return false;
1847 else for( i
=0; i
< r1
->numRects
; i
++ ) {
1848 if ( r1
->rects
[i
].x1
!= r2
->rects
[i
].x1
) return false;
1849 else if ( r1
->rects
[i
].x2
!= r2
->rects
[i
].x2
) return false;
1850 else if ( r1
->rects
[i
].y1
!= r2
->rects
[i
].y1
) return false;
1851 else if ( r1
->rects
[i
].y2
!= r2
->rects
[i
].y2
) return false;
1863 if (pRegion
->numRects
== 0)
1865 if (!INBOX(pRegion
->extents
, x
, y
))
1867 for (i
=0; i
<pRegion
->numRects
; i
++)
1869 if (INBOX (pRegion
->rects
[i
], x
, y
))
1875 wxRegionContain
REGION::
1877 register Region region
,
1879 unsigned int rwidth
, unsigned int rheight
)
1881 register BoxPtr pbox
;
1882 register BoxPtr pboxEnd
;
1884 register BoxPtr prect
= &rect
;
1885 int partIn
, partOut
;
1889 prect
->x2
= rwidth
+ rx
;
1890 prect
->y2
= rheight
+ ry
;
1892 /* this is (just) a useful optimization */
1893 if ((region
->numRects
== 0) || !EXTENTCHECK(®ion
->extents
, prect
))
1894 return(wxOutRegion
);
1899 /* can stop when both partOut and partIn are true, or we reach prect->y2 */
1900 for (pbox
= region
->rects
, pboxEnd
= pbox
+ region
->numRects
;
1906 continue; /* getting up to speed or skipping remainder of band */
1910 partOut
= true; /* missed part of rectangle above */
1911 if (partIn
|| (pbox
->y1
>= prect
->y2
))
1913 ry
= pbox
->y1
; /* x guaranteed to be == prect->x1 */
1917 continue; /* not far enough over yet */
1921 partOut
= true; /* missed part of rectangle to left */
1926 if (pbox
->x1
< prect
->x2
)
1928 partIn
= true; /* definitely overlap */
1933 if (pbox
->x2
>= prect
->x2
)
1935 ry
= pbox
->y2
; /* finished with this band */
1936 if (ry
>= prect
->y2
)
1938 rx
= prect
->x1
; /* reset x out to left again */
1942 * Because boxes in a band are maximal width, if the first box
1943 * to overlap the rectangle doesn't completely cover it in that
1944 * band, the rectangle must be partially out, since some of it
1945 * will be uncovered in that band. partIn will have been set true
1953 return(partIn
? ((ry
< prect
->y2
) ? wxPartRegion
: wxInRegion
) :