]>
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
;
229 virtual ~wxRegionRefData()
236 wxRegionRefData(const REGION
&);
239 // ========================================================================
241 // ========================================================================
242 //IMPLEMENT_DYNAMIC_CLASS(wxRegionGeneric, wxGDIObject)
244 #define M_REGIONDATA ((wxRegionRefData *)m_refData)
245 #define M_REGIONDATA_OF(rgn) ((wxRegionRefData *)(rgn.m_refData))
247 // ----------------------------------------------------------------------------
248 // wxRegionGeneric construction
249 // ----------------------------------------------------------------------------
251 wxRegionGeneric::wxRegionGeneric()
255 wxRegionGeneric::~wxRegionGeneric()
259 wxRegionGeneric::wxRegionGeneric(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
261 m_refData
= new wxRegionRefData(wxRect(x
,y
,w
,h
));
264 wxRegionGeneric::wxRegionGeneric(const wxRect
& rect
)
266 m_refData
= new wxRegionRefData(rect
);
269 wxRegionGeneric::wxRegionGeneric(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
271 m_refData
= new wxRegionRefData(topLeft
, bottomRight
);
274 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::DoIsEqual(const wxRegion
& region
) const
291 return REGION::XEqualRegion(M_REGIONDATA
,M_REGIONDATA_OF(region
));
294 bool wxRegionGeneric::DoGetBox(wxCoord
& x
, wxCoord
& y
, wxCoord
&w
, wxCoord
&h
) const
300 REGION::XClipBox(M_REGIONDATA
,&rect
);
308 // ----------------------------------------------------------------------------
309 // wxRegionGeneric operations
310 // ----------------------------------------------------------------------------
312 bool wxRegionGeneric::DoUnionWithRect(const wxRect
& rect
)
314 if ( rect
.IsEmpty() )
322 return REGION::XUnionRegion(®ion
,M_REGIONDATA
,M_REGIONDATA
);
325 bool wxRegionGeneric::DoUnionWithRegion(const wxRegion
& region
)
328 return REGION::XUnionRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
331 bool wxRegionGeneric::DoIntersect(const wxRegion
& region
)
334 return REGION::XIntersectRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
337 bool wxRegionGeneric::DoSubtract(const wxRegion
& region
)
339 if ( region
.IsEmpty() )
345 return REGION::XSubtractRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
348 bool wxRegionGeneric::DoXor(const wxRegion
& region
)
351 return REGION::XXorRegion(M_REGIONDATA_OF(region
),M_REGIONDATA
,M_REGIONDATA
);
354 bool wxRegionGeneric::DoOffset(wxCoord x
, wxCoord y
)
357 return REGION::XOffsetRegion(M_REGIONDATA
, x
, y
);
360 // ----------------------------------------------------------------------------
361 // wxRegionGeneric comparison
362 // ----------------------------------------------------------------------------
364 bool wxRegionGeneric::IsEmpty() const
367 return REGION::XEmptyRegion(M_REGIONDATA
);
370 // Does the region contain the point (x,y)?
371 wxRegionContain
wxRegionGeneric::DoContainsPoint(wxCoord x
, wxCoord y
) const
374 return REGION::XPointInRegion(M_REGIONDATA
,x
,y
) ? wxInRegion
: wxOutRegion
;
377 // Does the region contain the rectangle rect?
378 wxRegionContain
wxRegionGeneric::DoContainsRect(const wxRect
& rect
) const
381 return REGION::XRectInRegion(M_REGIONDATA
,rect
.x
,rect
.y
,rect
.width
,rect
.height
);
384 // ========================================================================
385 // wxRegionIteratorGeneric
386 // ========================================================================
387 //IMPLEMENT_DYNAMIC_CLASS(wxRegionIteratorGeneric,wxObject)
389 wxRegionIteratorGeneric::wxRegionIteratorGeneric()
394 wxRegionIteratorGeneric::wxRegionIteratorGeneric(const wxRegionGeneric
& region
)
400 wxRegionIteratorGeneric::wxRegionIteratorGeneric(const wxRegionIteratorGeneric
& iterator
)
401 : m_region(iterator
.m_region
)
403 m_current
= iterator
.m_current
;
406 void wxRegionIteratorGeneric::Reset(const wxRegionGeneric
& region
)
412 bool wxRegionIteratorGeneric::HaveRects() const
414 return M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
417 wxRegionIteratorGeneric
& wxRegionIteratorGeneric::operator++()
423 wxRegionIteratorGeneric
wxRegionIteratorGeneric::operator++(int)
425 wxRegionIteratorGeneric
copy(*this);
430 wxRect
wxRegionIteratorGeneric::GetRect() const
432 wxASSERT(m_region
.m_refData
);
433 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
443 long wxRegionIteratorGeneric::GetX() const
445 wxASSERT(m_region
.m_refData
);
446 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
451 long wxRegionIteratorGeneric::GetY() const
453 wxASSERT(m_region
.m_refData
);
454 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
459 long wxRegionIteratorGeneric::GetW() const
461 wxASSERT(m_region
.m_refData
);
462 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
464 return box
->x2
- box
->x1
;
467 long wxRegionIteratorGeneric::GetH() const
469 wxASSERT(m_region
.m_refData
);
470 const Box
*box
= M_REGIONDATA_OF(m_region
)->GetBox(m_current
);
472 return box
->y2
- box
->y1
;
475 wxRegionIteratorGeneric::~wxRegionIteratorGeneric()
480 // ========================================================================
481 // The guts (from X.org)
482 // ========================================================================
484 /************************************************************************
486 Copyright 1987, 1988, 1998 The Open Group
488 Permission to use, copy, modify, distribute, and sell this software and its
489 documentation for any purpose is hereby granted without fee, provided that
490 the above copyright notice appear in all copies and that both that
491 copyright notice and this permission notice appear in supporting
494 The above copyright notice and this permission notice shall be included in
495 all copies or substantial portions of the Software.
497 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
498 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
499 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
500 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
501 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
502 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
504 Except as contained in this notice, the name of The Open Group shall not be
505 used in advertising or otherwise to promote the sale, use or other dealings
506 in this Software without prior written authorization from The Open Group.
509 Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
513 Permission to use, copy, modify, and distribute this software and its
514 documentation for any purpose and without fee is hereby granted,
515 provided that the above copyright notice appear in all copies and that
516 both that copyright notice and this permission notice appear in
517 supporting documentation, and that the name of Digital not be
518 used in advertising or publicity pertaining to distribution of the
519 software without specific, written prior permission.
521 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
522 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
523 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
524 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
525 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
526 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
529 ************************************************************************/
531 /* 1 if two BOXs overlap.
532 * 0 if two BOXs do not overlap.
533 * Remember, x2 and y2 are not in the region
535 #define EXTENTCHECK(r1, r2) \
536 ((r1)->x2 > (r2)->x1 && \
537 (r1)->x1 < (r2)->x2 && \
538 (r1)->y2 > (r2)->y1 && \
542 * Check to see if there is enough memory in the present region.
544 #define MEMCHECK(reg, rect, firstrect){\
545 if ((reg)->numRects >= ((reg)->size - 1)){\
546 (firstrect) = (BOX *) realloc \
547 ((char *)(firstrect), (unsigned) (2 * (sizeof(BOX)) * ((reg)->size)));\
548 if ((firstrect) == 0)\
551 (rect) = &(firstrect)[(reg)->numRects];\
555 #define EMPTY_REGION(pReg) pReg->numRects = 0
557 #define REGION_NOT_EMPTY(pReg) pReg->numRects
559 #define INBOX(r, x, y) \
560 ( ( ((r).x2 > x)) && \
561 ( ((r).x1 <= x)) && \
566 * The functions in this file implement the Region abstraction, similar to one
567 * used in the X11 sample server. A Region is simply an area, as the name
568 * implies, and is implemented as a "y-x-banded" array of rectangles. To
569 * explain: Each Region is made up of a certain number of rectangles sorted
570 * by y coordinate first, and then by x coordinate.
572 * Furthermore, the rectangles are banded such that every rectangle with a
573 * given upper-left y coordinate (y1) will have the same lower-right y
574 * coordinate (y2) and vice versa. If a rectangle has scanlines in a band, it
575 * will span the entire vertical distance of the band. This means that some
576 * areas that could be merged into a taller rectangle will be represented as
577 * several shorter rectangles to account for shorter rectangles to its left
578 * or right but within its "vertical scope".
580 * An added constraint on the rectangles is that they must cover as much
581 * horizontal area as possible. E.g. no two rectangles in a band are allowed
584 * Whenever possible, bands will be merged together to cover a greater vertical
585 * distance (and thus reduce the number of rectangles). Two bands can be merged
586 * only if the bottom of one touches the top of the other and they have
587 * rectangles in the same places (of the same width, of course). This maintains
588 * the y-x-banding that's so nice to have...
591 /* Create a new empty region */
592 Region
REGION::XCreateRegion(void)
594 Region temp
= new REGION
;
597 return (Region
) NULL
;
599 temp
->rects
= ( BOX
* )malloc( (unsigned) sizeof( BOX
));
604 return (Region
) NULL
;
607 temp
->extents
.x1
= 0;
608 temp
->extents
.y1
= 0;
609 temp
->extents
.x2
= 0;
610 temp
->extents
.y2
= 0;
615 bool REGION::XClipBox(Region r
, wxRect
*rect
)
617 rect
->x
= r
->extents
.x1
;
618 rect
->y
= r
->extents
.y1
;
619 rect
->width
= r
->extents
.x2
- r
->extents
.x1
;
620 rect
->height
= r
->extents
.y2
- r
->extents
.y1
;
625 *-----------------------------------------------------------------------
627 * Reset the extents of a region to what they should be. Called by
628 * miSubtract and miIntersect b/c they can't figure it out along the
629 * way or do so easily, as miUnion can.
635 * The region's 'extents' structure is overwritten.
637 *-----------------------------------------------------------------------
640 miSetExtents (Region pReg
)
642 register BoxPtr pBox
,
646 if (pReg
->numRects
== 0)
648 pReg
->extents
.x1
= 0;
649 pReg
->extents
.y1
= 0;
650 pReg
->extents
.x2
= 0;
651 pReg
->extents
.y2
= 0;
655 pExtents
= &pReg
->extents
;
657 pBoxEnd
= &pBox
[pReg
->numRects
- 1];
660 * Since pBox is the first rectangle in the region, it must have the
661 * smallest y1 and since pBoxEnd is the last rectangle in the region,
662 * it must have the largest y2, because of banding. Initialize x1 and
663 * x2 from pBox and pBoxEnd, resp., as good things to initialize them
666 pExtents
->x1
= pBox
->x1
;
667 pExtents
->y1
= pBox
->y1
;
668 pExtents
->x2
= pBoxEnd
->x2
;
669 pExtents
->y2
= pBoxEnd
->y2
;
671 assert(pExtents
->y1
< pExtents
->y2
);
672 while (pBox
<= pBoxEnd
)
674 if (pBox
->x1
< pExtents
->x1
)
676 pExtents
->x1
= pBox
->x1
;
678 if (pBox
->x2
> pExtents
->x2
)
680 pExtents
->x2
= pBox
->x2
;
684 assert(pExtents
->x1
< pExtents
->x2
);
691 free( (char *) r
->rects
);
696 /* TranslateRegion(pRegion, x, y)
703 register Region pRegion
,
710 pbox
= pRegion
->rects
;
711 nbox
= pRegion
->numRects
;
721 pRegion
->extents
.x1
+= x
;
722 pRegion
->extents
.x2
+= x
;
723 pRegion
->extents
.y1
+= y
;
724 pRegion
->extents
.y2
+= y
;
728 /*======================================================================
729 * Region Intersection
730 *====================================================================*/
732 *-----------------------------------------------------------------------
734 * Handle an overlapping band for miIntersect.
740 * Rectangles may be added to the region.
742 *-----------------------------------------------------------------------
747 register Region pReg
,
757 register BoxPtr pNextRect
;
759 pNextRect
= &pReg
->rects
[pReg
->numRects
];
761 while ((r1
!= r1End
) && (r2
!= r2End
))
763 x1
= wxMax(r1
->x1
,r2
->x1
);
764 x2
= wxMin(r1
->x2
,r2
->x2
);
767 * If there's any overlap between the two rectangles, add that
768 * overlap to the new region.
769 * There's no need to check for subsumption because the only way
770 * such a need could arise is if some region has two rectangles
771 * right next to each other. Since that should never happen...
777 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
784 assert(pReg
->numRects
<= pReg
->size
);
788 * Need to advance the pointers. Shift the one that extends
789 * to the right the least, since the other still has a chance to
790 * overlap with that region's next rectangle, if you see what I mean.
796 else if (r2
->x2
< r1
->x2
)
812 Region reg2
, /* source regions */
813 register Region newReg
) /* destination Region */
815 /* check for trivial reject */
816 if ( (!(reg1
->numRects
)) || (!(reg2
->numRects
)) ||
817 (!EXTENTCHECK(®1
->extents
, ®2
->extents
)))
818 newReg
->numRects
= 0;
820 miRegionOp (newReg
, reg1
, reg2
,
821 miIntersectO
, NULL
, NULL
);
824 * Can't alter newReg's extents before we call miRegionOp because
825 * it might be one of the source regions and miRegionOp depends
826 * on the extents of those regions being the same. Besides, this
827 * way there's no checking against rectangles that will be nuked
828 * due to coalescing, so we have to examine fewer rectangles.
830 miSetExtents(newReg
);
836 register Region dstrgn
,
840 if (dstrgn
!= rgn
) /* don't want to copy to itself */
842 if (dstrgn
->size
< rgn
->numRects
)
846 BOX
*prevRects
= dstrgn
->rects
;
848 dstrgn
->rects
= (BOX
*)
849 realloc((char *) dstrgn
->rects
,
850 (unsigned) rgn
->numRects
* (sizeof(BOX
)));
857 dstrgn
->size
= rgn
->numRects
;
859 dstrgn
->numRects
= rgn
->numRects
;
860 dstrgn
->extents
.x1
= rgn
->extents
.x1
;
861 dstrgn
->extents
.y1
= rgn
->extents
.y1
;
862 dstrgn
->extents
.x2
= rgn
->extents
.x2
;
863 dstrgn
->extents
.y2
= rgn
->extents
.y2
;
865 memcpy((char *) dstrgn
->rects
, (char *) rgn
->rects
,
866 (int) (rgn
->numRects
* sizeof(BOX
)));
870 /*======================================================================
871 * Generic Region Operator
872 *====================================================================*/
875 *-----------------------------------------------------------------------
877 * Attempt to merge the boxes in the current band with those in the
878 * previous one. Used only by miRegionOp.
881 * The new index for the previous band.
884 * If coalescing takes place:
885 * - rectangles in the previous band will have their y2 fields
887 * - pReg->numRects will be decreased.
889 *-----------------------------------------------------------------------
894 register Region pReg
, /* Region to coalesce */
895 int prevStart
, /* Index of start of previous band */
896 int curStart
) /* Index of start of current band */
898 register BoxPtr pPrevBox
; /* Current box in previous band */
899 register BoxPtr pCurBox
; /* Current box in current band */
900 register BoxPtr pRegEnd
; /* End of region */
901 int curNumRects
; /* Number of rectangles in current
903 int prevNumRects
; /* Number of rectangles in previous
905 int bandY1
; /* Y1 coordinate for current band */
907 pRegEnd
= &pReg
->rects
[pReg
->numRects
];
909 pPrevBox
= &pReg
->rects
[prevStart
];
910 prevNumRects
= curStart
- prevStart
;
913 * Figure out how many rectangles are in the current band. Have to do
914 * this because multiple bands could have been added in miRegionOp
915 * at the end when one region has been exhausted.
917 pCurBox
= &pReg
->rects
[curStart
];
918 bandY1
= pCurBox
->y1
;
919 for (curNumRects
= 0;
920 (pCurBox
!= pRegEnd
) && (pCurBox
->y1
== bandY1
);
926 if (pCurBox
!= pRegEnd
)
929 * If more than one band was added, we have to find the start
930 * of the last band added so the next coalescing job can start
931 * at the right place... (given when multiple bands are added,
932 * this may be pointless -- see above).
935 while (pRegEnd
[-1].y1
== pRegEnd
->y1
)
939 curStart
= pRegEnd
- pReg
->rects
;
940 pRegEnd
= pReg
->rects
+ pReg
->numRects
;
943 if ((curNumRects
== prevNumRects
) && (curNumRects
!= 0))
945 pCurBox
-= curNumRects
;
947 * The bands may only be coalesced if the bottom of the previous
948 * matches the top scanline of the current.
950 if (pPrevBox
->y2
== pCurBox
->y1
)
953 * Make sure the bands have boxes in the same places. This
954 * assumes that boxes have been added in such a way that they
955 * cover the most area possible. I.e. two boxes in a band must
956 * have some horizontal space between them.
960 if ((pPrevBox
->x1
!= pCurBox
->x1
) ||
961 (pPrevBox
->x2
!= pCurBox
->x2
))
964 * The bands don't line up so they can't be coalesced.
971 } while (prevNumRects
!= 0);
973 pReg
->numRects
-= curNumRects
;
974 pCurBox
-= curNumRects
;
975 pPrevBox
-= curNumRects
;
978 * The bands may be merged, so set the bottom y of each box
979 * in the previous band to that of the corresponding box in
984 pPrevBox
->y2
= pCurBox
->y2
;
988 } while (curNumRects
!= 0);
991 * If only one band was added to the region, we have to backup
992 * curStart to the start of the previous band.
994 * If more than one band was added to the region, copy the
995 * other bands down. The assumption here is that the other bands
996 * came from the same region as the current one and no further
997 * coalescing can be done on them since it's all been done
998 * already... curStart is already in the right place.
1000 if (pCurBox
== pRegEnd
)
1002 curStart
= prevStart
;
1008 *pPrevBox
++ = *pCurBox
++;
1009 } while (pCurBox
!= pRegEnd
);
1018 *-----------------------------------------------------------------------
1020 * Apply an operation to two regions. Called by miUnion, miInverse,
1021 * miSubtract, miIntersect...
1027 * The new region is overwritten.
1030 * The idea behind this function is to view the two regions as sets.
1031 * Together they cover a rectangle of area that this function divides
1032 * into horizontal bands where points are covered only by one region
1033 * or by both. For the first case, the nonOverlapFunc is called with
1034 * each the band and the band's upper and lower extents. For the
1035 * second, the overlapFunc is called to process the entire band. It
1036 * is responsible for clipping the rectangles in the band, though
1037 * this function provides the boundaries.
1038 * At the end of each band, the new region is coalesced, if possible,
1039 * to reduce the number of rectangles in the region.
1041 *-----------------------------------------------------------------------
1046 register Region newReg
, /* Place to store result */
1047 Region reg1
, /* First region in operation */
1048 Region reg2
, /* 2d region in operation */
1050 register Region pReg
,
1056 wxCoord y2
), /* Function to call for over-
1058 int (*nonOverlap1Func
)(
1059 register Region pReg
,
1062 register wxCoord y1
,
1063 register wxCoord y2
), /* Function to call for non-
1064 * overlapping bands in region
1066 int (*nonOverlap2Func
)(
1067 register Region pReg
,
1070 register wxCoord y1
,
1071 register wxCoord y2
)) /* Function to call for non-
1072 * overlapping bands in region
1075 register BoxPtr r1
; /* Pointer into first region */
1076 register BoxPtr r2
; /* Pointer into 2d region */
1077 BoxPtr r1End
; /* End of 1st region */
1078 BoxPtr r2End
; /* End of 2d region */
1079 register wxCoord ybot
; /* Bottom of intersection */
1080 register wxCoord ytop
; /* Top of intersection */
1081 BoxPtr oldRects
; /* Old rects for newReg */
1082 int prevBand
; /* Index of start of
1083 * previous band in newReg */
1084 int curBand
; /* Index of start of current
1086 register BoxPtr r1BandEnd
; /* End of current band in r1 */
1087 register BoxPtr r2BandEnd
; /* End of current band in r2 */
1088 wxCoord top
; /* Top of non-overlapping
1090 wxCoord bot
; /* Bottom of non-overlapping
1095 * set r1, r2, r1End and r2End appropriately, preserve the important
1096 * parts of the destination region until the end in case it's one of
1097 * the two source regions, then mark the "new" region empty, allocating
1098 * another array of rectangles for it to use.
1102 r1End
= r1
+ reg1
->numRects
;
1103 r2End
= r2
+ reg2
->numRects
;
1105 oldRects
= newReg
->rects
;
1107 EMPTY_REGION(newReg
);
1110 * Allocate a reasonable number of rectangles for the new region. The idea
1111 * is to allocate enough so the individual functions don't need to
1112 * reallocate and copy the array, which is time consuming, yet we don't
1113 * have to worry about using too much memory. I hope to be able to
1114 * nuke the realloc() at the end of this function eventually.
1116 newReg
->size
= wxMax(reg1
->numRects
,reg2
->numRects
) * 2;
1118 newReg
->rects
= (BoxPtr
)malloc((unsigned) (sizeof(BoxRec
) * newReg
->size
));
1127 * Initialize ybot and ytop.
1128 * In the upcoming loop, ybot and ytop serve different functions depending
1129 * on whether the band being handled is an overlapping or non-overlapping
1131 * In the case of a non-overlapping band (only one of the regions
1132 * has points in the band), ybot is the bottom of the most recent
1133 * intersection and thus clips the top of the rectangles in that band.
1134 * ytop is the top of the next intersection between the two regions and
1135 * serves to clip the bottom of the rectangles in the current band.
1136 * For an overlapping band (where the two regions intersect), ytop clips
1137 * the top of the rectangles of both regions and ybot clips the bottoms.
1139 if (reg1
->extents
.y1
< reg2
->extents
.y1
)
1140 ybot
= reg1
->extents
.y1
;
1142 ybot
= reg2
->extents
.y1
;
1145 * prevBand serves to mark the start of the previous band so rectangles
1146 * can be coalesced into larger rectangles. qv. miCoalesce, above.
1147 * In the beginning, there is no previous band, so prevBand == curBand
1148 * (curBand is set later on, of course, but the first band will always
1149 * start at index 0). prevBand and curBand must be indices because of
1150 * the possible expansion, and resultant moving, of the new region's
1151 * array of rectangles.
1157 curBand
= newReg
->numRects
;
1160 * This algorithm proceeds one source-band (as opposed to a
1161 * destination band, which is determined by where the two regions
1162 * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
1163 * rectangle after the last one in the current band for their
1164 * respective regions.
1167 while ((r1BandEnd
!= r1End
) && (r1BandEnd
->y1
== r1
->y1
))
1173 while ((r2BandEnd
!= r2End
) && (r2BandEnd
->y1
== r2
->y1
))
1179 * First handle the band that doesn't intersect, if any.
1181 * Note that attention is restricted to one band in the
1182 * non-intersecting region at once, so if a region has n
1183 * bands between the current position and the next place it overlaps
1184 * the other, this entire loop will be passed through n times.
1186 if (r1
->y1
< r2
->y1
)
1188 top
= wxMax(r1
->y1
,ybot
);
1189 bot
= wxMin(r1
->y2
,r2
->y1
);
1191 if ((top
!= bot
) && (nonOverlap1Func
!= NULL
))
1193 (* nonOverlap1Func
) (newReg
, r1
, r1BandEnd
, top
, bot
);
1198 else if (r2
->y1
< r1
->y1
)
1200 top
= wxMax(r2
->y1
,ybot
);
1201 bot
= wxMin(r2
->y2
,r1
->y1
);
1203 if ((top
!= bot
) && (nonOverlap2Func
!= NULL
))
1205 (* nonOverlap2Func
) (newReg
, r2
, r2BandEnd
, top
, bot
);
1216 * If any rectangles got added to the region, try and coalesce them
1217 * with rectangles from the previous band. Note we could just do
1218 * this test in miCoalesce, but some machines incur a not
1219 * inconsiderable cost for function calls, so...
1221 if (newReg
->numRects
!= curBand
)
1223 prevBand
= miCoalesce (newReg
, prevBand
, curBand
);
1227 * Now see if we've hit an intersecting band. The two bands only
1228 * intersect if ybot > ytop
1230 ybot
= wxMin(r1
->y2
, r2
->y2
);
1231 curBand
= newReg
->numRects
;
1234 (* overlapFunc
) (newReg
, r1
, r1BandEnd
, r2
, r2BandEnd
, ytop
, ybot
);
1238 if (newReg
->numRects
!= curBand
)
1240 prevBand
= miCoalesce (newReg
, prevBand
, curBand
);
1244 * If we've finished with a band (y2 == ybot) we skip forward
1245 * in the region to the next band.
1255 } while ((r1
!= r1End
) && (r2
!= r2End
));
1258 * Deal with whichever region still has rectangles left.
1260 curBand
= newReg
->numRects
;
1263 if (nonOverlap1Func
!= NULL
)
1268 while ((r1BandEnd
< r1End
) && (r1BandEnd
->y1
== r1
->y1
))
1272 (* nonOverlap1Func
) (newReg
, r1
, r1BandEnd
,
1273 wxMax(r1
->y1
,ybot
), r1
->y2
);
1275 } while (r1
!= r1End
);
1278 else if ((r2
!= r2End
) && (nonOverlap2Func
!= NULL
))
1283 while ((r2BandEnd
< r2End
) && (r2BandEnd
->y1
== r2
->y1
))
1287 (* nonOverlap2Func
) (newReg
, r2
, r2BandEnd
,
1288 wxMax(r2
->y1
,ybot
), r2
->y2
);
1290 } while (r2
!= r2End
);
1293 if (newReg
->numRects
!= curBand
)
1295 (void) miCoalesce (newReg
, prevBand
, curBand
);
1299 * A bit of cleanup. To keep regions from growing without bound,
1300 * we shrink the array of rectangles to match the new number of
1301 * rectangles in the region. This never goes to 0, however...
1303 * Only do this stuff if the number of rectangles allocated is more than
1304 * twice the number of rectangles in the region (a simple optimization...).
1306 if (newReg
->numRects
< (newReg
->size
>> 1))
1308 if (REGION_NOT_EMPTY(newReg
))
1310 BoxPtr prev_rects
= newReg
->rects
;
1311 newReg
->size
= newReg
->numRects
;
1312 newReg
->rects
= (BoxPtr
) realloc ((char *) newReg
->rects
,
1313 (unsigned) (sizeof(BoxRec
) * newReg
->size
));
1314 if (! newReg
->rects
)
1315 newReg
->rects
= prev_rects
;
1320 * No point in doing the extra work involved in an realloc if
1321 * the region is empty
1324 free((char *) newReg
->rects
);
1325 newReg
->rects
= (BoxPtr
) malloc(sizeof(BoxRec
));
1328 free ((char *) oldRects
);
1332 /*======================================================================
1334 *====================================================================*/
1337 *-----------------------------------------------------------------------
1339 * Handle a non-overlapping band for the union operation. Just
1340 * Adds the rectangles into the region. Doesn't have to check for
1341 * subsumption or anything.
1347 * pReg->numRects is incremented and the final rectangles overwritten
1348 * with the rectangles we're passed.
1350 *-----------------------------------------------------------------------
1355 register Region pReg
,
1358 register wxCoord y1
,
1359 register wxCoord y2
)
1361 register BoxPtr pNextRect
;
1363 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1369 assert(r
->x1
< r
->x2
);
1370 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1371 pNextRect
->x1
= r
->x1
;
1373 pNextRect
->x2
= r
->x2
;
1375 pReg
->numRects
+= 1;
1378 assert(pReg
->numRects
<=pReg
->size
);
1381 return 0; /* lint */
1386 *-----------------------------------------------------------------------
1388 * Handle an overlapping band for the union operation. Picks the
1389 * left-most rectangle each time and merges it into the region.
1395 * Rectangles are overwritten in pReg->rects and pReg->numRects will
1398 *-----------------------------------------------------------------------
1404 register Region pReg
,
1409 register wxCoord y1
,
1410 register wxCoord y2
)
1412 register BoxPtr pNextRect
;
1414 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1416 #define MERGERECT(r) \
1417 if ((pReg->numRects != 0) && \
1418 (pNextRect[-1].y1 == y1) && \
1419 (pNextRect[-1].y2 == y2) && \
1420 (pNextRect[-1].x2 >= r->x1)) \
1422 if (pNextRect[-1].x2 < r->x2) \
1424 pNextRect[-1].x2 = r->x2; \
1425 assert(pNextRect[-1].x1<pNextRect[-1].x2); \
1430 MEMCHECK(pReg, pNextRect, pReg->rects); \
1431 pNextRect->y1 = y1; \
1432 pNextRect->y2 = y2; \
1433 pNextRect->x1 = r->x1; \
1434 pNextRect->x2 = r->x2; \
1435 pReg->numRects += 1; \
1438 assert(pReg->numRects<=pReg->size);\
1442 while ((r1
!= r1End
) && (r2
!= r2End
))
1444 if (r1
->x1
< r2
->x1
)
1459 } while (r1
!= r1End
);
1461 else while (r2
!= r2End
)
1465 return 0; /* lint */
1471 Region reg2
, /* source regions */
1472 Region newReg
) /* destination Region */
1474 /* checks all the simple cases */
1477 * Region 1 and 2 are the same or region 1 is empty
1479 if ( (reg1
== reg2
) || (!(reg1
->numRects
)) )
1482 miRegionCopy(newReg
, reg2
);
1487 * if nothing to union (region 2 empty)
1489 if (!(reg2
->numRects
))
1492 miRegionCopy(newReg
, reg1
);
1497 * Region 1 completely subsumes region 2
1499 if ((reg1
->numRects
== 1) &&
1500 (reg1
->extents
.x1
<= reg2
->extents
.x1
) &&
1501 (reg1
->extents
.y1
<= reg2
->extents
.y1
) &&
1502 (reg1
->extents
.x2
>= reg2
->extents
.x2
) &&
1503 (reg1
->extents
.y2
>= reg2
->extents
.y2
))
1506 miRegionCopy(newReg
, reg1
);
1511 * Region 2 completely subsumes region 1
1513 if ((reg2
->numRects
== 1) &&
1514 (reg2
->extents
.x1
<= reg1
->extents
.x1
) &&
1515 (reg2
->extents
.y1
<= reg1
->extents
.y1
) &&
1516 (reg2
->extents
.x2
>= reg1
->extents
.x2
) &&
1517 (reg2
->extents
.y2
>= reg1
->extents
.y2
))
1520 miRegionCopy(newReg
, reg2
);
1524 miRegionOp (newReg
, reg1
, reg2
, miUnionO
,
1525 miUnionNonO
, miUnionNonO
);
1527 newReg
->extents
.x1
= wxMin(reg1
->extents
.x1
, reg2
->extents
.x1
);
1528 newReg
->extents
.y1
= wxMin(reg1
->extents
.y1
, reg2
->extents
.y1
);
1529 newReg
->extents
.x2
= wxMax(reg1
->extents
.x2
, reg2
->extents
.x2
);
1530 newReg
->extents
.y2
= wxMax(reg1
->extents
.y2
, reg2
->extents
.y2
);
1535 /*======================================================================
1536 * Region Subtraction
1537 *====================================================================*/
1540 *-----------------------------------------------------------------------
1542 * Deal with non-overlapping band for subtraction. Any parts from
1543 * region 2 we discard. Anything from region 1 we add to the region.
1549 * pReg may be affected.
1551 *-----------------------------------------------------------------------
1556 register Region pReg
,
1559 register wxCoord y1
,
1560 register wxCoord y2
)
1562 register BoxPtr pNextRect
;
1564 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1570 assert(r
->x1
<r
->x2
);
1571 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1572 pNextRect
->x1
= r
->x1
;
1574 pNextRect
->x2
= r
->x2
;
1576 pReg
->numRects
+= 1;
1579 assert(pReg
->numRects
<= pReg
->size
);
1583 return 0; /* lint */
1587 *-----------------------------------------------------------------------
1589 * Overlapping band subtraction. x1 is the left-most point not yet
1596 * pReg may have rectangles added to it.
1598 *-----------------------------------------------------------------------
1603 register Region pReg
,
1608 register wxCoord y1
,
1609 register wxCoord y2
)
1611 register BoxPtr pNextRect
;
1617 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1619 while ((r1
!= r1End
) && (r2
!= r2End
))
1624 * Subtrahend missed the boat: go to next subtrahend.
1628 else if (r2
->x1
<= x1
)
1631 * Subtrahend preceeds minuend: nuke left edge of minuend.
1637 * Minuend completely covered: advance to next minuend and
1638 * reset left fence to edge of new minuend.
1647 * Subtrahend now used up since it doesn't extend beyond
1653 else if (r2
->x1
< r1
->x2
)
1656 * Left part of subtrahend covers part of minuend: add uncovered
1657 * part of minuend to region and skip to next subtrahend.
1660 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1663 pNextRect
->x2
= r2
->x1
;
1665 pReg
->numRects
+= 1;
1668 assert(pReg
->numRects
<=pReg
->size
);
1674 * Minuend used up: advance to new...
1683 * Subtrahend used up
1691 * Minuend used up: add any remaining piece before advancing.
1695 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1698 pNextRect
->x2
= r1
->x2
;
1700 pReg
->numRects
+= 1;
1702 assert(pReg
->numRects
<=pReg
->size
);
1711 * Add remaining minuend rectangles to region.
1716 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1719 pNextRect
->x2
= r1
->x2
;
1721 pReg
->numRects
+= 1;
1724 assert(pReg
->numRects
<=pReg
->size
);
1732 return 0; /* lint */
1736 *-----------------------------------------------------------------------
1738 * Subtract regS from regM and leave the result in regD.
1739 * S stands for subtrahend, M for minuend and D for difference.
1745 * regD is overwritten.
1747 *-----------------------------------------------------------------------
1750 bool REGION::XSubtractRegion(Region regM
, Region regS
, register Region regD
)
1752 /* check for trivial reject */
1753 if ( (!(regM
->numRects
)) || (!(regS
->numRects
)) ||
1754 (!EXTENTCHECK(®M
->extents
, ®S
->extents
)) )
1756 miRegionCopy(regD
, regM
);
1760 miRegionOp (regD
, regM
, regS
, miSubtractO
,
1761 miSubtractNonO1
, NULL
);
1764 * Can't alter newReg's extents before we call miRegionOp because
1765 * it might be one of the source regions and miRegionOp depends
1766 * on the extents of those regions being the unaltered. Besides, this
1767 * way there's no checking against rectangles that will be nuked
1768 * due to coalescing, so we have to examine fewer rectangles.
1770 miSetExtents (regD
);
1774 bool REGION::XXorRegion(Region sra
, Region srb
, Region dr
)
1776 Region tra
= XCreateRegion();
1778 wxCHECK_MSG( tra
, false, wxT("region not created") );
1780 Region trb
= XCreateRegion();
1782 wxCHECK_MSG( trb
, false, wxT("region not created") );
1784 (void) XSubtractRegion(sra
,srb
,tra
);
1785 (void) XSubtractRegion(srb
,sra
,trb
);
1786 (void) XUnionRegion(tra
,trb
,dr
);
1787 XDestroyRegion(tra
);
1788 XDestroyRegion(trb
);
1793 * Check to see if the region is empty. Assumes a region is passed
1796 bool REGION::XEmptyRegion(Region r
)
1798 if( r
->numRects
== 0 ) return true;
1803 * Check to see if two regions are equal
1805 bool REGION::XEqualRegion(Region r1
, Region r2
)
1809 if( r1
->numRects
!= r2
->numRects
) return false;
1810 else if( r1
->numRects
== 0 ) return true;
1811 else if ( r1
->extents
.x1
!= r2
->extents
.x1
) return false;
1812 else if ( r1
->extents
.x2
!= r2
->extents
.x2
) return false;
1813 else if ( r1
->extents
.y1
!= r2
->extents
.y1
) return false;
1814 else if ( r1
->extents
.y2
!= r2
->extents
.y2
) return false;
1815 else for( i
=0; i
< r1
->numRects
; i
++ ) {
1816 if ( r1
->rects
[i
].x1
!= r2
->rects
[i
].x1
) return false;
1817 else if ( r1
->rects
[i
].x2
!= r2
->rects
[i
].x2
) return false;
1818 else if ( r1
->rects
[i
].y1
!= r2
->rects
[i
].y1
) return false;
1819 else if ( r1
->rects
[i
].y2
!= r2
->rects
[i
].y2
) return false;
1824 bool REGION::XPointInRegion(Region pRegion
, int x
, int y
)
1828 if (pRegion
->numRects
== 0)
1830 if (!INBOX(pRegion
->extents
, x
, y
))
1832 for (i
=0; i
<pRegion
->numRects
; i
++)
1834 if (INBOX (pRegion
->rects
[i
], x
, y
))
1840 wxRegionContain
REGION::XRectInRegion(register Region region
,
1842 unsigned int rwidth
,
1843 unsigned int rheight
)
1845 register BoxPtr pbox
;
1846 register BoxPtr pboxEnd
;
1848 register BoxPtr prect
= &rect
;
1849 int partIn
, partOut
;
1853 prect
->x2
= rwidth
+ rx
;
1854 prect
->y2
= rheight
+ ry
;
1856 /* this is (just) a useful optimization */
1857 if ((region
->numRects
== 0) || !EXTENTCHECK(®ion
->extents
, prect
))
1858 return(wxOutRegion
);
1863 /* can stop when both partOut and partIn are true, or we reach prect->y2 */
1864 for (pbox
= region
->rects
, pboxEnd
= pbox
+ region
->numRects
;
1870 continue; /* getting up to speed or skipping remainder of band */
1874 partOut
= true; /* missed part of rectangle above */
1875 if (partIn
|| (pbox
->y1
>= prect
->y2
))
1877 ry
= pbox
->y1
; /* x guaranteed to be == prect->x1 */
1881 continue; /* not far enough over yet */
1885 partOut
= true; /* missed part of rectangle to left */
1890 if (pbox
->x1
< prect
->x2
)
1892 partIn
= true; /* definitely overlap */
1897 if (pbox
->x2
>= prect
->x2
)
1899 ry
= pbox
->y2
; /* finished with this band */
1900 if (ry
>= prect
->y2
)
1902 rx
= prect
->x1
; /* reset x out to left again */
1906 * Because boxes in a band are maximal width, if the first box
1907 * to overlap the rectangle doesn't completely cover it in that
1908 * band, the rectangle must be partially out, since some of it
1909 * will be uncovered in that band. partIn will have been set true
1917 return(partIn
? ((ry
< prect
->y2
) ? wxPartRegion
: wxInRegion
) :