1 /***********************************************************
3 Copyright 1987, 1998 The Open Group
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
25 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
29 Permission to use, copy, modify, and distribute this software and its
30 documentation for any purpose and without fee is hereby granted,
31 provided that the above copyright notice appear in all copies and that
32 both that copyright notice and this permission notice appear in
33 supporting documentation, and that the name of Digital not be
34 used in advertising or publicity pertaining to distribution of the
35 software without specific, written prior permission.
37 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
38 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
39 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
40 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
41 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
42 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
45 ******************************************************************/
46 #ifdef HAVE_DIX_CONFIG_H
47 #include <dix-config.h>
50 #include "regionstr.h"
52 #include "miscanfill.h"
54 #include "misc.h" /* MAXINT */
59 * Written by Brian Kelleher; Oct. 1985
61 * This module contains all of the utility functions
62 * needed to scan convert a polygon.
69 * Insert the given edge into the edge table.
70 * First we must find the correct bucket in the
71 * Edge table, then find the right slot in the
72 * bucket. Finally, we can insert it.
76 miInsertEdgeInET(EdgeTable
* ET
, EdgeTableEntry
* ETE
, int scanline
,
77 ScanLineListBlock
** SLLBlock
, int *iSLLBlock
)
79 EdgeTableEntry
*start
, *prev
;
80 ScanLineList
*pSLL
, *pPrevSLL
;
81 ScanLineListBlock
*tmpSLLBlock
;
84 * find the right bucket to put the edge into
86 pPrevSLL
= &ET
->scanlines
;
87 pSLL
= pPrevSLL
->next
;
88 while (pSLL
&& (pSLL
->scanline
< scanline
)) {
94 * reassign pSLL (pointer to ScanLineList) if necessary
96 if ((!pSLL
) || (pSLL
->scanline
> scanline
)) {
97 if (*iSLLBlock
> SLLSPERBLOCK
- 1) {
98 tmpSLLBlock
= malloc(sizeof(ScanLineListBlock
));
101 (*SLLBlock
)->next
= tmpSLLBlock
;
102 tmpSLLBlock
->next
= NULL
;
103 *SLLBlock
= tmpSLLBlock
;
106 pSLL
= &((*SLLBlock
)->SLLs
[(*iSLLBlock
)++]);
108 pSLL
->next
= pPrevSLL
->next
;
109 pSLL
->edgelist
= NULL
;
110 pPrevSLL
->next
= pSLL
;
112 pSLL
->scanline
= scanline
;
115 * now insert the edge in the right bucket
118 start
= pSLL
->edgelist
;
119 while (start
&& (start
->bres
.minor
< ETE
->bres
.minor
)) {
128 pSLL
->edgelist
= ETE
;
135 * This routine creates the edge table for
136 * scan converting polygons.
137 * The Edge Table (ET) looks like:
141 * | ymax | ScanLineLists
142 * |scanline|-->------------>-------------->...
143 * -------- |scanline| |scanline|
144 * |edgelist| |edgelist|
145 * --------- ---------
149 * list of ETEs list of ETEs
151 * where ETE is an EdgeTableEntry data structure,
152 * and there is one ScanLineList per scanline at
153 * which an edge is initially entered.
158 miCreateETandAET(int count
, DDXPointPtr pts
, EdgeTable
* ET
,
159 EdgeTableEntry
* AET
, EdgeTableEntry
* pETEs
,
160 ScanLineListBlock
* pSLLBlock
)
162 DDXPointPtr top
, bottom
;
163 DDXPointPtr PrevPt
, CurrPt
;
172 * initialize the Active Edge Table
176 AET
->nextWETE
= NULL
;
177 AET
->bres
.minor
= MININT
;
180 * initialize the Edge Table.
182 ET
->scanlines
.next
= NULL
;
185 pSLLBlock
->next
= NULL
;
187 PrevPt
= &pts
[count
- 1];
190 * for each vertex in the array of points.
191 * In this loop we are dealing with two vertices at
192 * a time -- these make up one edge of the polygon.
198 * find out which point is above and which is below.
200 if (PrevPt
->y
> CurrPt
->y
) {
201 bottom
= PrevPt
, top
= CurrPt
;
202 pETEs
->ClockWise
= 0;
205 bottom
= CurrPt
, top
= PrevPt
;
206 pETEs
->ClockWise
= 1;
210 * don't add horizontal edges to the Edge table.
212 if (bottom
->y
!= top
->y
) {
213 pETEs
->ymax
= bottom
->y
- 1; /* -1 so we don't get last scanline */
216 * initialize integer edge algorithm
218 dy
= bottom
->y
- top
->y
;
219 BRESINITPGONSTRUCT(dy
, top
->x
, bottom
->x
, pETEs
->bres
);
221 if (!miInsertEdgeInET(ET
, pETEs
, top
->y
, &pSLLBlock
, &iSLLBlock
)) {
222 miFreeStorage(pSLLBlock
->next
);
226 ET
->ymax
= max(ET
->ymax
, PrevPt
->y
);
227 ET
->ymin
= min(ET
->ymin
, PrevPt
->y
);
239 * This routine moves EdgeTableEntries from the
240 * EdgeTable into the Active Edge Table,
241 * leaving them sorted by smaller x coordinate.
246 miloadAET(EdgeTableEntry
* AET
, EdgeTableEntry
* ETEs
)
248 EdgeTableEntry
*pPrevAET
;
254 while (AET
&& (AET
->bres
.minor
< ETEs
->bres
.minor
)) {
262 ETEs
->back
= pPrevAET
;
263 pPrevAET
->next
= ETEs
;
273 * This routine links the AET by the
274 * nextWETE (winding EdgeTableEntry) link for
275 * use by the winding number rule. The final
276 * Active Edge Table (AET) might look something
280 * ---------- --------- ---------
281 * |ymax | |ymax | |ymax |
282 * | ... | |... | |... |
283 * |next |->|next |->|next |->...
284 * |nextWETE| |nextWETE| |nextWETE|
285 * --------- --------- ^--------
287 * V-------------------> V---> ...
291 micomputeWAET(EdgeTableEntry
* AET
)
293 EdgeTableEntry
*pWETE
;
297 AET
->nextWETE
= NULL
;
306 if ((!inside
&& !isInside
) || (inside
&& isInside
)) {
307 pWETE
->nextWETE
= AET
;
313 pWETE
->nextWETE
= NULL
;
319 * Just a simple insertion sort using
320 * pointers and back pointers to sort the Active
326 miInsertionSort(EdgeTableEntry
* AET
)
328 EdgeTableEntry
*pETEchase
;
329 EdgeTableEntry
*pETEinsert
;
330 EdgeTableEntry
*pETEchaseBackTMP
;
337 while (pETEchase
->back
->bres
.minor
> AET
->bres
.minor
)
338 pETEchase
= pETEchase
->back
;
341 if (pETEchase
!= pETEinsert
) {
342 pETEchaseBackTMP
= pETEchase
->back
;
343 pETEinsert
->back
->next
= AET
;
345 AET
->back
= pETEinsert
->back
;
346 pETEinsert
->next
= pETEchase
;
347 pETEchase
->back
->next
= pETEinsert
;
348 pETEchase
->back
= pETEinsert
;
349 pETEinsert
->back
= pETEchaseBackTMP
;
360 miFreeStorage(ScanLineListBlock
* pSLLBlock
)
362 ScanLineListBlock
*tmpSLLBlock
;
365 tmpSLLBlock
= pSLLBlock
->next
;
367 pSLLBlock
= tmpSLLBlock
;