xref: /reactos/dll/opengl/glu32/src/libtess/sweep.c (revision c2c66aff)
1*c2c66affSColin Finck /*
2*c2c66affSColin Finck  * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3*c2c66affSColin Finck  * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
4*c2c66affSColin Finck  *
5*c2c66affSColin Finck  * Permission is hereby granted, free of charge, to any person obtaining a
6*c2c66affSColin Finck  * copy of this software and associated documentation files (the "Software"),
7*c2c66affSColin Finck  * to deal in the Software without restriction, including without limitation
8*c2c66affSColin Finck  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9*c2c66affSColin Finck  * and/or sell copies of the Software, and to permit persons to whom the
10*c2c66affSColin Finck  * Software is furnished to do so, subject to the following conditions:
11*c2c66affSColin Finck  *
12*c2c66affSColin Finck  * The above copyright notice including the dates of first publication and
13*c2c66affSColin Finck  * either this permission notice or a reference to
14*c2c66affSColin Finck  * http://oss.sgi.com/projects/FreeB/
15*c2c66affSColin Finck  * shall be included in all copies or substantial portions of the Software.
16*c2c66affSColin Finck  *
17*c2c66affSColin Finck  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18*c2c66affSColin Finck  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19*c2c66affSColin Finck  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20*c2c66affSColin Finck  * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21*c2c66affSColin Finck  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
22*c2c66affSColin Finck  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23*c2c66affSColin Finck  * SOFTWARE.
24*c2c66affSColin Finck  *
25*c2c66affSColin Finck  * Except as contained in this notice, the name of Silicon Graphics, Inc.
26*c2c66affSColin Finck  * shall not be used in advertising or otherwise to promote the sale, use or
27*c2c66affSColin Finck  * other dealings in this Software without prior written authorization from
28*c2c66affSColin Finck  * Silicon Graphics, Inc.
29*c2c66affSColin Finck  */
30*c2c66affSColin Finck /*
31*c2c66affSColin Finck ** Author: Eric Veach, July 1994.
32*c2c66affSColin Finck **
33*c2c66affSColin Finck */
34*c2c66affSColin Finck 
35*c2c66affSColin Finck #include "gluos.h"
36*c2c66affSColin Finck #include <assert.h>
37*c2c66affSColin Finck //#include <stddef.h>
38*c2c66affSColin Finck //#include <setjmp.h>		/* longjmp */
39*c2c66affSColin Finck //#include <limits.h>		/* LONG_MAX */
40*c2c66affSColin Finck 
41*c2c66affSColin Finck //#include "mesh.h"
42*c2c66affSColin Finck #include "geom.h"
43*c2c66affSColin Finck #include "tess.h"
44*c2c66affSColin Finck //#include "dict.h"
45*c2c66affSColin Finck //#include "priorityq.h"
46*c2c66affSColin Finck #include "memalloc.h"
47*c2c66affSColin Finck #include "sweep.h"
48*c2c66affSColin Finck 
49*c2c66affSColin Finck #ifndef TRUE
50*c2c66affSColin Finck #define TRUE 1
51*c2c66affSColin Finck #endif
52*c2c66affSColin Finck #ifndef FALSE
53*c2c66affSColin Finck #define FALSE 0
54*c2c66affSColin Finck #endif
55*c2c66affSColin Finck 
56*c2c66affSColin Finck #ifdef FOR_TRITE_TEST_PROGRAM
57*c2c66affSColin Finck extern void DebugEvent( GLUtesselator *tess );
58*c2c66affSColin Finck #else
59*c2c66affSColin Finck #define DebugEvent( tess )
60*c2c66affSColin Finck #endif
61*c2c66affSColin Finck 
62*c2c66affSColin Finck /*
63*c2c66affSColin Finck  * Invariants for the Edge Dictionary.
64*c2c66affSColin Finck  * - each pair of adjacent edges e2=Succ(e1) satisfies EdgeLeq(e1,e2)
65*c2c66affSColin Finck  *   at any valid location of the sweep event
66*c2c66affSColin Finck  * - if EdgeLeq(e2,e1) as well (at any valid sweep event), then e1 and e2
67*c2c66affSColin Finck  *   share a common endpoint
68*c2c66affSColin Finck  * - for each e, e->Dst has been processed, but not e->Org
69*c2c66affSColin Finck  * - each edge e satisfies VertLeq(e->Dst,event) && VertLeq(event,e->Org)
70*c2c66affSColin Finck  *   where "event" is the current sweep line event.
71*c2c66affSColin Finck  * - no edge e has zero length
72*c2c66affSColin Finck  *
73*c2c66affSColin Finck  * Invariants for the Mesh (the processed portion).
74*c2c66affSColin Finck  * - the portion of the mesh left of the sweep line is a planar graph,
75*c2c66affSColin Finck  *   ie. there is *some* way to embed it in the plane
76*c2c66affSColin Finck  * - no processed edge has zero length
77*c2c66affSColin Finck  * - no two processed vertices have identical coordinates
78*c2c66affSColin Finck  * - each "inside" region is monotone, ie. can be broken into two chains
79*c2c66affSColin Finck  *   of monotonically increasing vertices according to VertLeq(v1,v2)
80*c2c66affSColin Finck  *   - a non-invariant: these chains may intersect (very slightly)
81*c2c66affSColin Finck  *
82*c2c66affSColin Finck  * Invariants for the Sweep.
83*c2c66affSColin Finck  * - if none of the edges incident to the event vertex have an activeRegion
84*c2c66affSColin Finck  *   (ie. none of these edges are in the edge dictionary), then the vertex
85*c2c66affSColin Finck  *   has only right-going edges.
86*c2c66affSColin Finck  * - if an edge is marked "fixUpperEdge" (it is a temporary edge introduced
87*c2c66affSColin Finck  *   by ConnectRightVertex), then it is the only right-going edge from
88*c2c66affSColin Finck  *   its associated vertex.  (This says that these edges exist only
89*c2c66affSColin Finck  *   when it is necessary.)
90*c2c66affSColin Finck  */
91*c2c66affSColin Finck 
92*c2c66affSColin Finck #undef	MAX
93*c2c66affSColin Finck #undef	MIN
94*c2c66affSColin Finck #define MAX(x,y)	((x) >= (y) ? (x) : (y))
95*c2c66affSColin Finck #define MIN(x,y)	((x) <= (y) ? (x) : (y))
96*c2c66affSColin Finck 
97*c2c66affSColin Finck /* When we merge two edges into one, we need to compute the combined
98*c2c66affSColin Finck  * winding of the new edge.
99*c2c66affSColin Finck  */
100*c2c66affSColin Finck #define AddWinding(eDst,eSrc)	(eDst->winding += eSrc->winding, \
101*c2c66affSColin Finck                                  eDst->Sym->winding += eSrc->Sym->winding)
102*c2c66affSColin Finck 
103*c2c66affSColin Finck static void SweepEvent( GLUtesselator *tess, GLUvertex *vEvent );
104*c2c66affSColin Finck static void WalkDirtyRegions( GLUtesselator *tess, ActiveRegion *regUp );
105*c2c66affSColin Finck static int CheckForRightSplice( GLUtesselator *tess, ActiveRegion *regUp );
106*c2c66affSColin Finck 
EdgeLeq(GLUtesselator * tess,ActiveRegion * reg1,ActiveRegion * reg2)107*c2c66affSColin Finck static int EdgeLeq( GLUtesselator *tess, ActiveRegion *reg1,
108*c2c66affSColin Finck 		    ActiveRegion *reg2 )
109*c2c66affSColin Finck /*
110*c2c66affSColin Finck  * Both edges must be directed from right to left (this is the canonical
111*c2c66affSColin Finck  * direction for the upper edge of each region).
112*c2c66affSColin Finck  *
113*c2c66affSColin Finck  * The strategy is to evaluate a "t" value for each edge at the
114*c2c66affSColin Finck  * current sweep line position, given by tess->event.  The calculations
115*c2c66affSColin Finck  * are designed to be very stable, but of course they are not perfect.
116*c2c66affSColin Finck  *
117*c2c66affSColin Finck  * Special case: if both edge destinations are at the sweep event,
118*c2c66affSColin Finck  * we sort the edges by slope (they would otherwise compare equally).
119*c2c66affSColin Finck  */
120*c2c66affSColin Finck {
121*c2c66affSColin Finck   GLUvertex *event = tess->event;
122*c2c66affSColin Finck   GLUhalfEdge *e1, *e2;
123*c2c66affSColin Finck   GLdouble t1, t2;
124*c2c66affSColin Finck 
125*c2c66affSColin Finck   e1 = reg1->eUp;
126*c2c66affSColin Finck   e2 = reg2->eUp;
127*c2c66affSColin Finck 
128*c2c66affSColin Finck   if( e1->Dst == event ) {
129*c2c66affSColin Finck     if( e2->Dst == event ) {
130*c2c66affSColin Finck       /* Two edges right of the sweep line which meet at the sweep event.
131*c2c66affSColin Finck        * Sort them by slope.
132*c2c66affSColin Finck        */
133*c2c66affSColin Finck       if( VertLeq( e1->Org, e2->Org )) {
134*c2c66affSColin Finck 	return EdgeSign( e2->Dst, e1->Org, e2->Org ) <= 0;
135*c2c66affSColin Finck       }
136*c2c66affSColin Finck       return EdgeSign( e1->Dst, e2->Org, e1->Org ) >= 0;
137*c2c66affSColin Finck     }
138*c2c66affSColin Finck     return EdgeSign( e2->Dst, event, e2->Org ) <= 0;
139*c2c66affSColin Finck   }
140*c2c66affSColin Finck   if( e2->Dst == event ) {
141*c2c66affSColin Finck     return EdgeSign( e1->Dst, event, e1->Org ) >= 0;
142*c2c66affSColin Finck   }
143*c2c66affSColin Finck 
144*c2c66affSColin Finck   /* General case - compute signed distance *from* e1, e2 to event */
145*c2c66affSColin Finck   t1 = EdgeEval( e1->Dst, event, e1->Org );
146*c2c66affSColin Finck   t2 = EdgeEval( e2->Dst, event, e2->Org );
147*c2c66affSColin Finck   return (t1 >= t2);
148*c2c66affSColin Finck }
149*c2c66affSColin Finck 
150*c2c66affSColin Finck 
DeleteRegion(GLUtesselator * tess,ActiveRegion * reg)151*c2c66affSColin Finck static void DeleteRegion( GLUtesselator *tess, ActiveRegion *reg )
152*c2c66affSColin Finck {
153*c2c66affSColin Finck   if( reg->fixUpperEdge ) {
154*c2c66affSColin Finck     /* It was created with zero winding number, so it better be
155*c2c66affSColin Finck      * deleted with zero winding number (ie. it better not get merged
156*c2c66affSColin Finck      * with a real edge).
157*c2c66affSColin Finck      */
158*c2c66affSColin Finck     assert( reg->eUp->winding == 0 );
159*c2c66affSColin Finck   }
160*c2c66affSColin Finck   reg->eUp->activeRegion = NULL;
161*c2c66affSColin Finck   dictDelete( tess->dict, reg->nodeUp ); /* __gl_dictListDelete */
162*c2c66affSColin Finck   memFree( reg );
163*c2c66affSColin Finck }
164*c2c66affSColin Finck 
165*c2c66affSColin Finck 
FixUpperEdge(ActiveRegion * reg,GLUhalfEdge * newEdge)166*c2c66affSColin Finck static int FixUpperEdge( ActiveRegion *reg, GLUhalfEdge *newEdge )
167*c2c66affSColin Finck /*
168*c2c66affSColin Finck  * Replace an upper edge which needs fixing (see ConnectRightVertex).
169*c2c66affSColin Finck  */
170*c2c66affSColin Finck {
171*c2c66affSColin Finck   assert( reg->fixUpperEdge );
172*c2c66affSColin Finck   if ( !__gl_meshDelete( reg->eUp ) ) return 0;
173*c2c66affSColin Finck   reg->fixUpperEdge = FALSE;
174*c2c66affSColin Finck   reg->eUp = newEdge;
175*c2c66affSColin Finck   newEdge->activeRegion = reg;
176*c2c66affSColin Finck 
177*c2c66affSColin Finck   return 1;
178*c2c66affSColin Finck }
179*c2c66affSColin Finck 
TopLeftRegion(ActiveRegion * reg)180*c2c66affSColin Finck static ActiveRegion *TopLeftRegion( ActiveRegion *reg )
181*c2c66affSColin Finck {
182*c2c66affSColin Finck   GLUvertex *org = reg->eUp->Org;
183*c2c66affSColin Finck   GLUhalfEdge *e;
184*c2c66affSColin Finck 
185*c2c66affSColin Finck   /* Find the region above the uppermost edge with the same origin */
186*c2c66affSColin Finck   do {
187*c2c66affSColin Finck     reg = RegionAbove( reg );
188*c2c66affSColin Finck   } while( reg->eUp->Org == org );
189*c2c66affSColin Finck 
190*c2c66affSColin Finck   /* If the edge above was a temporary edge introduced by ConnectRightVertex,
191*c2c66affSColin Finck    * now is the time to fix it.
192*c2c66affSColin Finck    */
193*c2c66affSColin Finck   if( reg->fixUpperEdge ) {
194*c2c66affSColin Finck     e = __gl_meshConnect( RegionBelow(reg)->eUp->Sym, reg->eUp->Lnext );
195*c2c66affSColin Finck     if (e == NULL) return NULL;
196*c2c66affSColin Finck     if ( !FixUpperEdge( reg, e ) ) return NULL;
197*c2c66affSColin Finck     reg = RegionAbove( reg );
198*c2c66affSColin Finck   }
199*c2c66affSColin Finck   return reg;
200*c2c66affSColin Finck }
201*c2c66affSColin Finck 
TopRightRegion(ActiveRegion * reg)202*c2c66affSColin Finck static ActiveRegion *TopRightRegion( ActiveRegion *reg )
203*c2c66affSColin Finck {
204*c2c66affSColin Finck   GLUvertex *dst = reg->eUp->Dst;
205*c2c66affSColin Finck 
206*c2c66affSColin Finck   /* Find the region above the uppermost edge with the same destination */
207*c2c66affSColin Finck   do {
208*c2c66affSColin Finck     reg = RegionAbove( reg );
209*c2c66affSColin Finck   } while( reg->eUp->Dst == dst );
210*c2c66affSColin Finck   return reg;
211*c2c66affSColin Finck }
212*c2c66affSColin Finck 
AddRegionBelow(GLUtesselator * tess,ActiveRegion * regAbove,GLUhalfEdge * eNewUp)213*c2c66affSColin Finck static ActiveRegion *AddRegionBelow( GLUtesselator *tess,
214*c2c66affSColin Finck 				     ActiveRegion *regAbove,
215*c2c66affSColin Finck 				     GLUhalfEdge *eNewUp )
216*c2c66affSColin Finck /*
217*c2c66affSColin Finck  * Add a new active region to the sweep line, *somewhere* below "regAbove"
218*c2c66affSColin Finck  * (according to where the new edge belongs in the sweep-line dictionary).
219*c2c66affSColin Finck  * The upper edge of the new region will be "eNewUp".
220*c2c66affSColin Finck  * Winding number and "inside" flag are not updated.
221*c2c66affSColin Finck  */
222*c2c66affSColin Finck {
223*c2c66affSColin Finck   ActiveRegion *regNew = (ActiveRegion *)memAlloc( sizeof( ActiveRegion ));
224*c2c66affSColin Finck   if (regNew == NULL) longjmp(tess->env,1);
225*c2c66affSColin Finck 
226*c2c66affSColin Finck   regNew->eUp = eNewUp;
227*c2c66affSColin Finck   /* __gl_dictListInsertBefore */
228*c2c66affSColin Finck   regNew->nodeUp = dictInsertBefore( tess->dict, regAbove->nodeUp, regNew );
229*c2c66affSColin Finck   if (regNew->nodeUp == NULL) longjmp(tess->env,1);
230*c2c66affSColin Finck   regNew->fixUpperEdge = FALSE;
231*c2c66affSColin Finck   regNew->sentinel = FALSE;
232*c2c66affSColin Finck   regNew->dirty = FALSE;
233*c2c66affSColin Finck 
234*c2c66affSColin Finck   eNewUp->activeRegion = regNew;
235*c2c66affSColin Finck   return regNew;
236*c2c66affSColin Finck }
237*c2c66affSColin Finck 
IsWindingInside(GLUtesselator * tess,int n)238*c2c66affSColin Finck static GLboolean IsWindingInside( GLUtesselator *tess, int n )
239*c2c66affSColin Finck {
240*c2c66affSColin Finck   switch( tess->windingRule ) {
241*c2c66affSColin Finck   case GLU_TESS_WINDING_ODD:
242*c2c66affSColin Finck     return (n & 1);
243*c2c66affSColin Finck   case GLU_TESS_WINDING_NONZERO:
244*c2c66affSColin Finck     return (n != 0);
245*c2c66affSColin Finck   case GLU_TESS_WINDING_POSITIVE:
246*c2c66affSColin Finck     return (n > 0);
247*c2c66affSColin Finck   case GLU_TESS_WINDING_NEGATIVE:
248*c2c66affSColin Finck     return (n < 0);
249*c2c66affSColin Finck   case GLU_TESS_WINDING_ABS_GEQ_TWO:
250*c2c66affSColin Finck     return (n >= 2) || (n <= -2);
251*c2c66affSColin Finck   }
252*c2c66affSColin Finck   /*LINTED*/
253*c2c66affSColin Finck   assert( FALSE );
254*c2c66affSColin Finck   /*NOTREACHED*/
255*c2c66affSColin Finck   return GL_FALSE;  /* avoid compiler complaints */
256*c2c66affSColin Finck }
257*c2c66affSColin Finck 
258*c2c66affSColin Finck 
ComputeWinding(GLUtesselator * tess,ActiveRegion * reg)259*c2c66affSColin Finck static void ComputeWinding( GLUtesselator *tess, ActiveRegion *reg )
260*c2c66affSColin Finck {
261*c2c66affSColin Finck   reg->windingNumber = RegionAbove(reg)->windingNumber + reg->eUp->winding;
262*c2c66affSColin Finck   reg->inside = IsWindingInside( tess, reg->windingNumber );
263*c2c66affSColin Finck }
264*c2c66affSColin Finck 
265*c2c66affSColin Finck 
FinishRegion(GLUtesselator * tess,ActiveRegion * reg)266*c2c66affSColin Finck static void FinishRegion( GLUtesselator *tess, ActiveRegion *reg )
267*c2c66affSColin Finck /*
268*c2c66affSColin Finck  * Delete a region from the sweep line.  This happens when the upper
269*c2c66affSColin Finck  * and lower chains of a region meet (at a vertex on the sweep line).
270*c2c66affSColin Finck  * The "inside" flag is copied to the appropriate mesh face (we could
271*c2c66affSColin Finck  * not do this before -- since the structure of the mesh is always
272*c2c66affSColin Finck  * changing, this face may not have even existed until now).
273*c2c66affSColin Finck  */
274*c2c66affSColin Finck {
275*c2c66affSColin Finck   GLUhalfEdge *e = reg->eUp;
276*c2c66affSColin Finck   GLUface *f = e->Lface;
277*c2c66affSColin Finck 
278*c2c66affSColin Finck   f->inside = reg->inside;
279*c2c66affSColin Finck   f->anEdge = e;   /* optimization for __gl_meshTessellateMonoRegion() */
280*c2c66affSColin Finck   DeleteRegion( tess, reg );
281*c2c66affSColin Finck }
282*c2c66affSColin Finck 
283*c2c66affSColin Finck 
FinishLeftRegions(GLUtesselator * tess,ActiveRegion * regFirst,ActiveRegion * regLast)284*c2c66affSColin Finck static GLUhalfEdge *FinishLeftRegions( GLUtesselator *tess,
285*c2c66affSColin Finck 	       ActiveRegion *regFirst, ActiveRegion *regLast )
286*c2c66affSColin Finck /*
287*c2c66affSColin Finck  * We are given a vertex with one or more left-going edges.  All affected
288*c2c66affSColin Finck  * edges should be in the edge dictionary.  Starting at regFirst->eUp,
289*c2c66affSColin Finck  * we walk down deleting all regions where both edges have the same
290*c2c66affSColin Finck  * origin vOrg.  At the same time we copy the "inside" flag from the
291*c2c66affSColin Finck  * active region to the face, since at this point each face will belong
292*c2c66affSColin Finck  * to at most one region (this was not necessarily true until this point
293*c2c66affSColin Finck  * in the sweep).  The walk stops at the region above regLast; if regLast
294*c2c66affSColin Finck  * is NULL we walk as far as possible.	At the same time we relink the
295*c2c66affSColin Finck  * mesh if necessary, so that the ordering of edges around vOrg is the
296*c2c66affSColin Finck  * same as in the dictionary.
297*c2c66affSColin Finck  */
298*c2c66affSColin Finck {
299*c2c66affSColin Finck   ActiveRegion *reg, *regPrev;
300*c2c66affSColin Finck   GLUhalfEdge *e, *ePrev;
301*c2c66affSColin Finck 
302*c2c66affSColin Finck   regPrev = regFirst;
303*c2c66affSColin Finck   ePrev = regFirst->eUp;
304*c2c66affSColin Finck   while( regPrev != regLast ) {
305*c2c66affSColin Finck     regPrev->fixUpperEdge = FALSE;	/* placement was OK */
306*c2c66affSColin Finck     reg = RegionBelow( regPrev );
307*c2c66affSColin Finck     e = reg->eUp;
308*c2c66affSColin Finck     if( e->Org != ePrev->Org ) {
309*c2c66affSColin Finck       if( ! reg->fixUpperEdge ) {
310*c2c66affSColin Finck 	/* Remove the last left-going edge.  Even though there are no further
311*c2c66affSColin Finck 	 * edges in the dictionary with this origin, there may be further
312*c2c66affSColin Finck 	 * such edges in the mesh (if we are adding left edges to a vertex
313*c2c66affSColin Finck 	 * that has already been processed).  Thus it is important to call
314*c2c66affSColin Finck 	 * FinishRegion rather than just DeleteRegion.
315*c2c66affSColin Finck 	 */
316*c2c66affSColin Finck 	FinishRegion( tess, regPrev );
317*c2c66affSColin Finck 	break;
318*c2c66affSColin Finck       }
319*c2c66affSColin Finck       /* If the edge below was a temporary edge introduced by
320*c2c66affSColin Finck        * ConnectRightVertex, now is the time to fix it.
321*c2c66affSColin Finck        */
322*c2c66affSColin Finck       e = __gl_meshConnect( ePrev->Lprev, e->Sym );
323*c2c66affSColin Finck       if (e == NULL) longjmp(tess->env,1);
324*c2c66affSColin Finck       if ( !FixUpperEdge( reg, e ) ) longjmp(tess->env,1);
325*c2c66affSColin Finck     }
326*c2c66affSColin Finck 
327*c2c66affSColin Finck     /* Relink edges so that ePrev->Onext == e */
328*c2c66affSColin Finck     if( ePrev->Onext != e ) {
329*c2c66affSColin Finck       if ( !__gl_meshSplice( e->Oprev, e ) ) longjmp(tess->env,1);
330*c2c66affSColin Finck       if ( !__gl_meshSplice( ePrev, e ) ) longjmp(tess->env,1);
331*c2c66affSColin Finck     }
332*c2c66affSColin Finck     FinishRegion( tess, regPrev );	/* may change reg->eUp */
333*c2c66affSColin Finck     ePrev = reg->eUp;
334*c2c66affSColin Finck     regPrev = reg;
335*c2c66affSColin Finck   }
336*c2c66affSColin Finck   return ePrev;
337*c2c66affSColin Finck }
338*c2c66affSColin Finck 
339*c2c66affSColin Finck 
AddRightEdges(GLUtesselator * tess,ActiveRegion * regUp,GLUhalfEdge * eFirst,GLUhalfEdge * eLast,GLUhalfEdge * eTopLeft,GLboolean cleanUp)340*c2c66affSColin Finck static void AddRightEdges( GLUtesselator *tess, ActiveRegion *regUp,
341*c2c66affSColin Finck        GLUhalfEdge *eFirst, GLUhalfEdge *eLast, GLUhalfEdge *eTopLeft,
342*c2c66affSColin Finck        GLboolean cleanUp )
343*c2c66affSColin Finck /*
344*c2c66affSColin Finck  * Purpose: insert right-going edges into the edge dictionary, and update
345*c2c66affSColin Finck  * winding numbers and mesh connectivity appropriately.  All right-going
346*c2c66affSColin Finck  * edges share a common origin vOrg.  Edges are inserted CCW starting at
347*c2c66affSColin Finck  * eFirst; the last edge inserted is eLast->Oprev.  If vOrg has any
348*c2c66affSColin Finck  * left-going edges already processed, then eTopLeft must be the edge
349*c2c66affSColin Finck  * such that an imaginary upward vertical segment from vOrg would be
350*c2c66affSColin Finck  * contained between eTopLeft->Oprev and eTopLeft; otherwise eTopLeft
351*c2c66affSColin Finck  * should be NULL.
352*c2c66affSColin Finck  */
353*c2c66affSColin Finck {
354*c2c66affSColin Finck   ActiveRegion *reg, *regPrev;
355*c2c66affSColin Finck   GLUhalfEdge *e, *ePrev;
356*c2c66affSColin Finck   int firstTime = TRUE;
357*c2c66affSColin Finck 
358*c2c66affSColin Finck   /* Insert the new right-going edges in the dictionary */
359*c2c66affSColin Finck   e = eFirst;
360*c2c66affSColin Finck   do {
361*c2c66affSColin Finck     assert( VertLeq( e->Org, e->Dst ));
362*c2c66affSColin Finck     AddRegionBelow( tess, regUp, e->Sym );
363*c2c66affSColin Finck     e = e->Onext;
364*c2c66affSColin Finck   } while ( e != eLast );
365*c2c66affSColin Finck 
366*c2c66affSColin Finck   /* Walk *all* right-going edges from e->Org, in the dictionary order,
367*c2c66affSColin Finck    * updating the winding numbers of each region, and re-linking the mesh
368*c2c66affSColin Finck    * edges to match the dictionary ordering (if necessary).
369*c2c66affSColin Finck    */
370*c2c66affSColin Finck   if( eTopLeft == NULL ) {
371*c2c66affSColin Finck     eTopLeft = RegionBelow( regUp )->eUp->Rprev;
372*c2c66affSColin Finck   }
373*c2c66affSColin Finck   regPrev = regUp;
374*c2c66affSColin Finck   ePrev = eTopLeft;
375*c2c66affSColin Finck   for( ;; ) {
376*c2c66affSColin Finck     reg = RegionBelow( regPrev );
377*c2c66affSColin Finck     e = reg->eUp->Sym;
378*c2c66affSColin Finck     if( e->Org != ePrev->Org ) break;
379*c2c66affSColin Finck 
380*c2c66affSColin Finck     if( e->Onext != ePrev ) {
381*c2c66affSColin Finck       /* Unlink e from its current position, and relink below ePrev */
382*c2c66affSColin Finck       if ( !__gl_meshSplice( e->Oprev, e ) ) longjmp(tess->env,1);
383*c2c66affSColin Finck       if ( !__gl_meshSplice( ePrev->Oprev, e ) ) longjmp(tess->env,1);
384*c2c66affSColin Finck     }
385*c2c66affSColin Finck     /* Compute the winding number and "inside" flag for the new regions */
386*c2c66affSColin Finck     reg->windingNumber = regPrev->windingNumber - e->winding;
387*c2c66affSColin Finck     reg->inside = IsWindingInside( tess, reg->windingNumber );
388*c2c66affSColin Finck 
389*c2c66affSColin Finck     /* Check for two outgoing edges with same slope -- process these
390*c2c66affSColin Finck      * before any intersection tests (see example in __gl_computeInterior).
391*c2c66affSColin Finck      */
392*c2c66affSColin Finck     regPrev->dirty = TRUE;
393*c2c66affSColin Finck     if( ! firstTime && CheckForRightSplice( tess, regPrev )) {
394*c2c66affSColin Finck       AddWinding( e, ePrev );
395*c2c66affSColin Finck       DeleteRegion( tess, regPrev );
396*c2c66affSColin Finck       if ( !__gl_meshDelete( ePrev ) ) longjmp(tess->env,1);
397*c2c66affSColin Finck     }
398*c2c66affSColin Finck     firstTime = FALSE;
399*c2c66affSColin Finck     regPrev = reg;
400*c2c66affSColin Finck     ePrev = e;
401*c2c66affSColin Finck   }
402*c2c66affSColin Finck   regPrev->dirty = TRUE;
403*c2c66affSColin Finck   assert( regPrev->windingNumber - e->winding == reg->windingNumber );
404*c2c66affSColin Finck 
405*c2c66affSColin Finck   if( cleanUp ) {
406*c2c66affSColin Finck     /* Check for intersections between newly adjacent edges. */
407*c2c66affSColin Finck     WalkDirtyRegions( tess, regPrev );
408*c2c66affSColin Finck   }
409*c2c66affSColin Finck }
410*c2c66affSColin Finck 
411*c2c66affSColin Finck 
CallCombine(GLUtesselator * tess,GLUvertex * isect,void * data[4],GLfloat weights[4],int needed)412*c2c66affSColin Finck static void CallCombine( GLUtesselator *tess, GLUvertex *isect,
413*c2c66affSColin Finck 			 void *data[4], GLfloat weights[4], int needed )
414*c2c66affSColin Finck {
415*c2c66affSColin Finck   GLdouble coords[3];
416*c2c66affSColin Finck 
417*c2c66affSColin Finck   /* Copy coord data in case the callback changes it. */
418*c2c66affSColin Finck   coords[0] = isect->coords[0];
419*c2c66affSColin Finck   coords[1] = isect->coords[1];
420*c2c66affSColin Finck   coords[2] = isect->coords[2];
421*c2c66affSColin Finck 
422*c2c66affSColin Finck   isect->data = NULL;
423*c2c66affSColin Finck   CALL_COMBINE_OR_COMBINE_DATA( coords, data, weights, &isect->data );
424*c2c66affSColin Finck   if( isect->data == NULL ) {
425*c2c66affSColin Finck     if( ! needed ) {
426*c2c66affSColin Finck       isect->data = data[0];
427*c2c66affSColin Finck     } else if( ! tess->fatalError ) {
428*c2c66affSColin Finck       /* The only way fatal error is when two edges are found to intersect,
429*c2c66affSColin Finck        * but the user has not provided the callback necessary to handle
430*c2c66affSColin Finck        * generated intersection points.
431*c2c66affSColin Finck        */
432*c2c66affSColin Finck       CALL_ERROR_OR_ERROR_DATA( GLU_TESS_NEED_COMBINE_CALLBACK );
433*c2c66affSColin Finck       tess->fatalError = TRUE;
434*c2c66affSColin Finck     }
435*c2c66affSColin Finck   }
436*c2c66affSColin Finck }
437*c2c66affSColin Finck 
SpliceMergeVertices(GLUtesselator * tess,GLUhalfEdge * e1,GLUhalfEdge * e2)438*c2c66affSColin Finck static void SpliceMergeVertices( GLUtesselator *tess, GLUhalfEdge *e1,
439*c2c66affSColin Finck 				 GLUhalfEdge *e2 )
440*c2c66affSColin Finck /*
441*c2c66affSColin Finck  * Two vertices with idential coordinates are combined into one.
442*c2c66affSColin Finck  * e1->Org is kept, while e2->Org is discarded.
443*c2c66affSColin Finck  */
444*c2c66affSColin Finck {
445*c2c66affSColin Finck   void *data[4] = { NULL, NULL, NULL, NULL };
446*c2c66affSColin Finck   GLfloat weights[4] = { 0.5, 0.5, 0.0, 0.0 };
447*c2c66affSColin Finck 
448*c2c66affSColin Finck   data[0] = e1->Org->data;
449*c2c66affSColin Finck   data[1] = e2->Org->data;
450*c2c66affSColin Finck   CallCombine( tess, e1->Org, data, weights, FALSE );
451*c2c66affSColin Finck   if ( !__gl_meshSplice( e1, e2 ) ) longjmp(tess->env,1);
452*c2c66affSColin Finck }
453*c2c66affSColin Finck 
VertexWeights(GLUvertex * isect,GLUvertex * org,GLUvertex * dst,GLfloat * weights)454*c2c66affSColin Finck static void VertexWeights( GLUvertex *isect, GLUvertex *org, GLUvertex *dst,
455*c2c66affSColin Finck 			   GLfloat *weights )
456*c2c66affSColin Finck /*
457*c2c66affSColin Finck  * Find some weights which describe how the intersection vertex is
458*c2c66affSColin Finck  * a linear combination of "org" and "dest".  Each of the two edges
459*c2c66affSColin Finck  * which generated "isect" is allocated 50% of the weight; each edge
460*c2c66affSColin Finck  * splits the weight between its org and dst according to the
461*c2c66affSColin Finck  * relative distance to "isect".
462*c2c66affSColin Finck  */
463*c2c66affSColin Finck {
464*c2c66affSColin Finck   GLdouble t1 = VertL1dist( org, isect );
465*c2c66affSColin Finck   GLdouble t2 = VertL1dist( dst, isect );
466*c2c66affSColin Finck 
467*c2c66affSColin Finck   weights[0] = 0.5 * t2 / (t1 + t2);
468*c2c66affSColin Finck   weights[1] = 0.5 * t1 / (t1 + t2);
469*c2c66affSColin Finck   isect->coords[0] += weights[0]*org->coords[0] + weights[1]*dst->coords[0];
470*c2c66affSColin Finck   isect->coords[1] += weights[0]*org->coords[1] + weights[1]*dst->coords[1];
471*c2c66affSColin Finck   isect->coords[2] += weights[0]*org->coords[2] + weights[1]*dst->coords[2];
472*c2c66affSColin Finck }
473*c2c66affSColin Finck 
474*c2c66affSColin Finck 
GetIntersectData(GLUtesselator * tess,GLUvertex * isect,GLUvertex * orgUp,GLUvertex * dstUp,GLUvertex * orgLo,GLUvertex * dstLo)475*c2c66affSColin Finck static void GetIntersectData( GLUtesselator *tess, GLUvertex *isect,
476*c2c66affSColin Finck        GLUvertex *orgUp, GLUvertex *dstUp,
477*c2c66affSColin Finck        GLUvertex *orgLo, GLUvertex *dstLo )
478*c2c66affSColin Finck /*
479*c2c66affSColin Finck  * We've computed a new intersection point, now we need a "data" pointer
480*c2c66affSColin Finck  * from the user so that we can refer to this new vertex in the
481*c2c66affSColin Finck  * rendering callbacks.
482*c2c66affSColin Finck  */
483*c2c66affSColin Finck {
484*c2c66affSColin Finck   void *data[4];
485*c2c66affSColin Finck   GLfloat weights[4];
486*c2c66affSColin Finck 
487*c2c66affSColin Finck   data[0] = orgUp->data;
488*c2c66affSColin Finck   data[1] = dstUp->data;
489*c2c66affSColin Finck   data[2] = orgLo->data;
490*c2c66affSColin Finck   data[3] = dstLo->data;
491*c2c66affSColin Finck 
492*c2c66affSColin Finck   isect->coords[0] = isect->coords[1] = isect->coords[2] = 0;
493*c2c66affSColin Finck   VertexWeights( isect, orgUp, dstUp, &weights[0] );
494*c2c66affSColin Finck   VertexWeights( isect, orgLo, dstLo, &weights[2] );
495*c2c66affSColin Finck 
496*c2c66affSColin Finck   CallCombine( tess, isect, data, weights, TRUE );
497*c2c66affSColin Finck }
498*c2c66affSColin Finck 
CheckForRightSplice(GLUtesselator * tess,ActiveRegion * regUp)499*c2c66affSColin Finck static int CheckForRightSplice( GLUtesselator *tess, ActiveRegion *regUp )
500*c2c66affSColin Finck /*
501*c2c66affSColin Finck  * Check the upper and lower edge of "regUp", to make sure that the
502*c2c66affSColin Finck  * eUp->Org is above eLo, or eLo->Org is below eUp (depending on which
503*c2c66affSColin Finck  * origin is leftmost).
504*c2c66affSColin Finck  *
505*c2c66affSColin Finck  * The main purpose is to splice right-going edges with the same
506*c2c66affSColin Finck  * dest vertex and nearly identical slopes (ie. we can't distinguish
507*c2c66affSColin Finck  * the slopes numerically).  However the splicing can also help us
508*c2c66affSColin Finck  * to recover from numerical errors.  For example, suppose at one
509*c2c66affSColin Finck  * point we checked eUp and eLo, and decided that eUp->Org is barely
510*c2c66affSColin Finck  * above eLo.  Then later, we split eLo into two edges (eg. from
511*c2c66affSColin Finck  * a splice operation like this one).  This can change the result of
512*c2c66affSColin Finck  * our test so that now eUp->Org is incident to eLo, or barely below it.
513*c2c66affSColin Finck  * We must correct this condition to maintain the dictionary invariants.
514*c2c66affSColin Finck  *
515*c2c66affSColin Finck  * One possibility is to check these edges for intersection again
516*c2c66affSColin Finck  * (ie. CheckForIntersect).  This is what we do if possible.  However
517*c2c66affSColin Finck  * CheckForIntersect requires that tess->event lies between eUp and eLo,
518*c2c66affSColin Finck  * so that it has something to fall back on when the intersection
519*c2c66affSColin Finck  * calculation gives us an unusable answer.  So, for those cases where
520*c2c66affSColin Finck  * we can't check for intersection, this routine fixes the problem
521*c2c66affSColin Finck  * by just splicing the offending vertex into the other edge.
522*c2c66affSColin Finck  * This is a guaranteed solution, no matter how degenerate things get.
523*c2c66affSColin Finck  * Basically this is a combinatorial solution to a numerical problem.
524*c2c66affSColin Finck  */
525*c2c66affSColin Finck {
526*c2c66affSColin Finck   ActiveRegion *regLo = RegionBelow(regUp);
527*c2c66affSColin Finck   GLUhalfEdge *eUp = regUp->eUp;
528*c2c66affSColin Finck   GLUhalfEdge *eLo = regLo->eUp;
529*c2c66affSColin Finck 
530*c2c66affSColin Finck   if( VertLeq( eUp->Org, eLo->Org )) {
531*c2c66affSColin Finck     if( EdgeSign( eLo->Dst, eUp->Org, eLo->Org ) > 0 ) return FALSE;
532*c2c66affSColin Finck 
533*c2c66affSColin Finck     /* eUp->Org appears to be below eLo */
534*c2c66affSColin Finck     if( ! VertEq( eUp->Org, eLo->Org )) {
535*c2c66affSColin Finck       /* Splice eUp->Org into eLo */
536*c2c66affSColin Finck       if ( __gl_meshSplitEdge( eLo->Sym ) == NULL) longjmp(tess->env,1);
537*c2c66affSColin Finck       if ( !__gl_meshSplice( eUp, eLo->Oprev ) ) longjmp(tess->env,1);
538*c2c66affSColin Finck       regUp->dirty = regLo->dirty = TRUE;
539*c2c66affSColin Finck 
540*c2c66affSColin Finck     } else if( eUp->Org != eLo->Org ) {
541*c2c66affSColin Finck       /* merge the two vertices, discarding eUp->Org */
542*c2c66affSColin Finck       pqDelete( tess->pq, eUp->Org->pqHandle ); /* __gl_pqSortDelete */
543*c2c66affSColin Finck       SpliceMergeVertices( tess, eLo->Oprev, eUp );
544*c2c66affSColin Finck     }
545*c2c66affSColin Finck   } else {
546*c2c66affSColin Finck     if( EdgeSign( eUp->Dst, eLo->Org, eUp->Org ) < 0 ) return FALSE;
547*c2c66affSColin Finck 
548*c2c66affSColin Finck     /* eLo->Org appears to be above eUp, so splice eLo->Org into eUp */
549*c2c66affSColin Finck     RegionAbove(regUp)->dirty = regUp->dirty = TRUE;
550*c2c66affSColin Finck     if (__gl_meshSplitEdge( eUp->Sym ) == NULL) longjmp(tess->env,1);
551*c2c66affSColin Finck     if ( !__gl_meshSplice( eLo->Oprev, eUp ) ) longjmp(tess->env,1);
552*c2c66affSColin Finck   }
553*c2c66affSColin Finck   return TRUE;
554*c2c66affSColin Finck }
555*c2c66affSColin Finck 
CheckForLeftSplice(GLUtesselator * tess,ActiveRegion * regUp)556*c2c66affSColin Finck static int CheckForLeftSplice( GLUtesselator *tess, ActiveRegion *regUp )
557*c2c66affSColin Finck /*
558*c2c66affSColin Finck  * Check the upper and lower edge of "regUp", to make sure that the
559*c2c66affSColin Finck  * eUp->Dst is above eLo, or eLo->Dst is below eUp (depending on which
560*c2c66affSColin Finck  * destination is rightmost).
561*c2c66affSColin Finck  *
562*c2c66affSColin Finck  * Theoretically, this should always be true.  However, splitting an edge
563*c2c66affSColin Finck  * into two pieces can change the results of previous tests.  For example,
564*c2c66affSColin Finck  * suppose at one point we checked eUp and eLo, and decided that eUp->Dst
565*c2c66affSColin Finck  * is barely above eLo.  Then later, we split eLo into two edges (eg. from
566*c2c66affSColin Finck  * a splice operation like this one).  This can change the result of
567*c2c66affSColin Finck  * the test so that now eUp->Dst is incident to eLo, or barely below it.
568*c2c66affSColin Finck  * We must correct this condition to maintain the dictionary invariants
569*c2c66affSColin Finck  * (otherwise new edges might get inserted in the wrong place in the
570*c2c66affSColin Finck  * dictionary, and bad stuff will happen).
571*c2c66affSColin Finck  *
572*c2c66affSColin Finck  * We fix the problem by just splicing the offending vertex into the
573*c2c66affSColin Finck  * other edge.
574*c2c66affSColin Finck  */
575*c2c66affSColin Finck {
576*c2c66affSColin Finck   ActiveRegion *regLo = RegionBelow(regUp);
577*c2c66affSColin Finck   GLUhalfEdge *eUp = regUp->eUp;
578*c2c66affSColin Finck   GLUhalfEdge *eLo = regLo->eUp;
579*c2c66affSColin Finck   GLUhalfEdge *e;
580*c2c66affSColin Finck 
581*c2c66affSColin Finck   assert( ! VertEq( eUp->Dst, eLo->Dst ));
582*c2c66affSColin Finck 
583*c2c66affSColin Finck   if( VertLeq( eUp->Dst, eLo->Dst )) {
584*c2c66affSColin Finck     if( EdgeSign( eUp->Dst, eLo->Dst, eUp->Org ) < 0 ) return FALSE;
585*c2c66affSColin Finck 
586*c2c66affSColin Finck     /* eLo->Dst is above eUp, so splice eLo->Dst into eUp */
587*c2c66affSColin Finck     RegionAbove(regUp)->dirty = regUp->dirty = TRUE;
588*c2c66affSColin Finck     e = __gl_meshSplitEdge( eUp );
589*c2c66affSColin Finck     if (e == NULL) longjmp(tess->env,1);
590*c2c66affSColin Finck     if ( !__gl_meshSplice( eLo->Sym, e ) ) longjmp(tess->env,1);
591*c2c66affSColin Finck     e->Lface->inside = regUp->inside;
592*c2c66affSColin Finck   } else {
593*c2c66affSColin Finck     if( EdgeSign( eLo->Dst, eUp->Dst, eLo->Org ) > 0 ) return FALSE;
594*c2c66affSColin Finck 
595*c2c66affSColin Finck     /* eUp->Dst is below eLo, so splice eUp->Dst into eLo */
596*c2c66affSColin Finck     regUp->dirty = regLo->dirty = TRUE;
597*c2c66affSColin Finck     e = __gl_meshSplitEdge( eLo );
598*c2c66affSColin Finck     if (e == NULL) longjmp(tess->env,1);
599*c2c66affSColin Finck     if ( !__gl_meshSplice( eUp->Lnext, eLo->Sym ) ) longjmp(tess->env,1);
600*c2c66affSColin Finck     e->Rface->inside = regUp->inside;
601*c2c66affSColin Finck   }
602*c2c66affSColin Finck   return TRUE;
603*c2c66affSColin Finck }
604*c2c66affSColin Finck 
605*c2c66affSColin Finck 
CheckForIntersect(GLUtesselator * tess,ActiveRegion * regUp)606*c2c66affSColin Finck static int CheckForIntersect( GLUtesselator *tess, ActiveRegion *regUp )
607*c2c66affSColin Finck /*
608*c2c66affSColin Finck  * Check the upper and lower edges of the given region to see if
609*c2c66affSColin Finck  * they intersect.  If so, create the intersection and add it
610*c2c66affSColin Finck  * to the data structures.
611*c2c66affSColin Finck  *
612*c2c66affSColin Finck  * Returns TRUE if adding the new intersection resulted in a recursive
613*c2c66affSColin Finck  * call to AddRightEdges(); in this case all "dirty" regions have been
614*c2c66affSColin Finck  * checked for intersections, and possibly regUp has been deleted.
615*c2c66affSColin Finck  */
616*c2c66affSColin Finck {
617*c2c66affSColin Finck   ActiveRegion *regLo = RegionBelow(regUp);
618*c2c66affSColin Finck   GLUhalfEdge *eUp = regUp->eUp;
619*c2c66affSColin Finck   GLUhalfEdge *eLo = regLo->eUp;
620*c2c66affSColin Finck   GLUvertex *orgUp = eUp->Org;
621*c2c66affSColin Finck   GLUvertex *orgLo = eLo->Org;
622*c2c66affSColin Finck   GLUvertex *dstUp = eUp->Dst;
623*c2c66affSColin Finck   GLUvertex *dstLo = eLo->Dst;
624*c2c66affSColin Finck   GLdouble tMinUp, tMaxLo;
625*c2c66affSColin Finck   GLUvertex isect, *orgMin;
626*c2c66affSColin Finck   GLUhalfEdge *e;
627*c2c66affSColin Finck 
628*c2c66affSColin Finck   assert( ! VertEq( dstLo, dstUp ));
629*c2c66affSColin Finck   assert( EdgeSign( dstUp, tess->event, orgUp ) <= 0 );
630*c2c66affSColin Finck   assert( EdgeSign( dstLo, tess->event, orgLo ) >= 0 );
631*c2c66affSColin Finck   assert( orgUp != tess->event && orgLo != tess->event );
632*c2c66affSColin Finck   assert( ! regUp->fixUpperEdge && ! regLo->fixUpperEdge );
633*c2c66affSColin Finck 
634*c2c66affSColin Finck   if( orgUp == orgLo ) return FALSE;	/* right endpoints are the same */
635*c2c66affSColin Finck 
636*c2c66affSColin Finck   tMinUp = MIN( orgUp->t, dstUp->t );
637*c2c66affSColin Finck   tMaxLo = MAX( orgLo->t, dstLo->t );
638*c2c66affSColin Finck   if( tMinUp > tMaxLo ) return FALSE;	/* t ranges do not overlap */
639*c2c66affSColin Finck 
640*c2c66affSColin Finck   if( VertLeq( orgUp, orgLo )) {
641*c2c66affSColin Finck     if( EdgeSign( dstLo, orgUp, orgLo ) > 0 ) return FALSE;
642*c2c66affSColin Finck   } else {
643*c2c66affSColin Finck     if( EdgeSign( dstUp, orgLo, orgUp ) < 0 ) return FALSE;
644*c2c66affSColin Finck   }
645*c2c66affSColin Finck 
646*c2c66affSColin Finck   /* At this point the edges intersect, at least marginally */
647*c2c66affSColin Finck   DebugEvent( tess );
648*c2c66affSColin Finck 
649*c2c66affSColin Finck   __gl_edgeIntersect( dstUp, orgUp, dstLo, orgLo, &isect );
650*c2c66affSColin Finck   /* The following properties are guaranteed: */
651*c2c66affSColin Finck   assert( MIN( orgUp->t, dstUp->t ) <= isect.t );
652*c2c66affSColin Finck   assert( isect.t <= MAX( orgLo->t, dstLo->t ));
653*c2c66affSColin Finck   assert( MIN( dstLo->s, dstUp->s ) <= isect.s );
654*c2c66affSColin Finck   assert( isect.s <= MAX( orgLo->s, orgUp->s ));
655*c2c66affSColin Finck 
656*c2c66affSColin Finck   if( VertLeq( &isect, tess->event )) {
657*c2c66affSColin Finck     /* The intersection point lies slightly to the left of the sweep line,
658*c2c66affSColin Finck      * so move it until it''s slightly to the right of the sweep line.
659*c2c66affSColin Finck      * (If we had perfect numerical precision, this would never happen
660*c2c66affSColin Finck      * in the first place).  The easiest and safest thing to do is
661*c2c66affSColin Finck      * replace the intersection by tess->event.
662*c2c66affSColin Finck      */
663*c2c66affSColin Finck     isect.s = tess->event->s;
664*c2c66affSColin Finck     isect.t = tess->event->t;
665*c2c66affSColin Finck   }
666*c2c66affSColin Finck   /* Similarly, if the computed intersection lies to the right of the
667*c2c66affSColin Finck    * rightmost origin (which should rarely happen), it can cause
668*c2c66affSColin Finck    * unbelievable inefficiency on sufficiently degenerate inputs.
669*c2c66affSColin Finck    * (If you have the test program, try running test54.d with the
670*c2c66affSColin Finck    * "X zoom" option turned on).
671*c2c66affSColin Finck    */
672*c2c66affSColin Finck   orgMin = VertLeq( orgUp, orgLo ) ? orgUp : orgLo;
673*c2c66affSColin Finck   if( VertLeq( orgMin, &isect )) {
674*c2c66affSColin Finck     isect.s = orgMin->s;
675*c2c66affSColin Finck     isect.t = orgMin->t;
676*c2c66affSColin Finck   }
677*c2c66affSColin Finck 
678*c2c66affSColin Finck   if( VertEq( &isect, orgUp ) || VertEq( &isect, orgLo )) {
679*c2c66affSColin Finck     /* Easy case -- intersection at one of the right endpoints */
680*c2c66affSColin Finck     (void) CheckForRightSplice( tess, regUp );
681*c2c66affSColin Finck     return FALSE;
682*c2c66affSColin Finck   }
683*c2c66affSColin Finck 
684*c2c66affSColin Finck   if(	 (! VertEq( dstUp, tess->event )
685*c2c66affSColin Finck 	  && EdgeSign( dstUp, tess->event, &isect ) >= 0)
686*c2c66affSColin Finck       || (! VertEq( dstLo, tess->event )
687*c2c66affSColin Finck 	  && EdgeSign( dstLo, tess->event, &isect ) <= 0 ))
688*c2c66affSColin Finck   {
689*c2c66affSColin Finck     /* Very unusual -- the new upper or lower edge would pass on the
690*c2c66affSColin Finck      * wrong side of the sweep event, or through it.  This can happen
691*c2c66affSColin Finck      * due to very small numerical errors in the intersection calculation.
692*c2c66affSColin Finck      */
693*c2c66affSColin Finck     if( dstLo == tess->event ) {
694*c2c66affSColin Finck       /* Splice dstLo into eUp, and process the new region(s) */
695*c2c66affSColin Finck       if (__gl_meshSplitEdge( eUp->Sym ) == NULL) longjmp(tess->env,1);
696*c2c66affSColin Finck       if ( !__gl_meshSplice( eLo->Sym, eUp ) ) longjmp(tess->env,1);
697*c2c66affSColin Finck       regUp = TopLeftRegion( regUp );
698*c2c66affSColin Finck       if (regUp == NULL) longjmp(tess->env,1);
699*c2c66affSColin Finck       eUp = RegionBelow(regUp)->eUp;
700*c2c66affSColin Finck       FinishLeftRegions( tess, RegionBelow(regUp), regLo );
701*c2c66affSColin Finck       AddRightEdges( tess, regUp, eUp->Oprev, eUp, eUp, TRUE );
702*c2c66affSColin Finck       return TRUE;
703*c2c66affSColin Finck     }
704*c2c66affSColin Finck     if( dstUp == tess->event ) {
705*c2c66affSColin Finck       /* Splice dstUp into eLo, and process the new region(s) */
706*c2c66affSColin Finck       if (__gl_meshSplitEdge( eLo->Sym ) == NULL) longjmp(tess->env,1);
707*c2c66affSColin Finck       if ( !__gl_meshSplice( eUp->Lnext, eLo->Oprev ) ) longjmp(tess->env,1);
708*c2c66affSColin Finck       regLo = regUp;
709*c2c66affSColin Finck       regUp = TopRightRegion( regUp );
710*c2c66affSColin Finck       e = RegionBelow(regUp)->eUp->Rprev;
711*c2c66affSColin Finck       regLo->eUp = eLo->Oprev;
712*c2c66affSColin Finck       eLo = FinishLeftRegions( tess, regLo, NULL );
713*c2c66affSColin Finck       AddRightEdges( tess, regUp, eLo->Onext, eUp->Rprev, e, TRUE );
714*c2c66affSColin Finck       return TRUE;
715*c2c66affSColin Finck     }
716*c2c66affSColin Finck     /* Special case: called from ConnectRightVertex.  If either
717*c2c66affSColin Finck      * edge passes on the wrong side of tess->event, split it
718*c2c66affSColin Finck      * (and wait for ConnectRightVertex to splice it appropriately).
719*c2c66affSColin Finck      */
720*c2c66affSColin Finck     if( EdgeSign( dstUp, tess->event, &isect ) >= 0 ) {
721*c2c66affSColin Finck       RegionAbove(regUp)->dirty = regUp->dirty = TRUE;
722*c2c66affSColin Finck       if (__gl_meshSplitEdge( eUp->Sym ) == NULL) longjmp(tess->env,1);
723*c2c66affSColin Finck       eUp->Org->s = tess->event->s;
724*c2c66affSColin Finck       eUp->Org->t = tess->event->t;
725*c2c66affSColin Finck     }
726*c2c66affSColin Finck     if( EdgeSign( dstLo, tess->event, &isect ) <= 0 ) {
727*c2c66affSColin Finck       regUp->dirty = regLo->dirty = TRUE;
728*c2c66affSColin Finck       if (__gl_meshSplitEdge( eLo->Sym ) == NULL) longjmp(tess->env,1);
729*c2c66affSColin Finck       eLo->Org->s = tess->event->s;
730*c2c66affSColin Finck       eLo->Org->t = tess->event->t;
731*c2c66affSColin Finck     }
732*c2c66affSColin Finck     /* leave the rest for ConnectRightVertex */
733*c2c66affSColin Finck     return FALSE;
734*c2c66affSColin Finck   }
735*c2c66affSColin Finck 
736*c2c66affSColin Finck   /* General case -- split both edges, splice into new vertex.
737*c2c66affSColin Finck    * When we do the splice operation, the order of the arguments is
738*c2c66affSColin Finck    * arbitrary as far as correctness goes.  However, when the operation
739*c2c66affSColin Finck    * creates a new face, the work done is proportional to the size of
740*c2c66affSColin Finck    * the new face.  We expect the faces in the processed part of
741*c2c66affSColin Finck    * the mesh (ie. eUp->Lface) to be smaller than the faces in the
742*c2c66affSColin Finck    * unprocessed original contours (which will be eLo->Oprev->Lface).
743*c2c66affSColin Finck    */
744*c2c66affSColin Finck   if (__gl_meshSplitEdge( eUp->Sym ) == NULL) longjmp(tess->env,1);
745*c2c66affSColin Finck   if (__gl_meshSplitEdge( eLo->Sym ) == NULL) longjmp(tess->env,1);
746*c2c66affSColin Finck   if ( !__gl_meshSplice( eLo->Oprev, eUp ) ) longjmp(tess->env,1);
747*c2c66affSColin Finck   eUp->Org->s = isect.s;
748*c2c66affSColin Finck   eUp->Org->t = isect.t;
749*c2c66affSColin Finck   eUp->Org->pqHandle = pqInsert( tess->pq, eUp->Org ); /* __gl_pqSortInsert */
750*c2c66affSColin Finck   if (eUp->Org->pqHandle == LONG_MAX) {
751*c2c66affSColin Finck      pqDeletePriorityQ(tess->pq);	/* __gl_pqSortDeletePriorityQ */
752*c2c66affSColin Finck      tess->pq = NULL;
753*c2c66affSColin Finck      longjmp(tess->env,1);
754*c2c66affSColin Finck   }
755*c2c66affSColin Finck   GetIntersectData( tess, eUp->Org, orgUp, dstUp, orgLo, dstLo );
756*c2c66affSColin Finck   RegionAbove(regUp)->dirty = regUp->dirty = regLo->dirty = TRUE;
757*c2c66affSColin Finck   return FALSE;
758*c2c66affSColin Finck }
759*c2c66affSColin Finck 
WalkDirtyRegions(GLUtesselator * tess,ActiveRegion * regUp)760*c2c66affSColin Finck static void WalkDirtyRegions( GLUtesselator *tess, ActiveRegion *regUp )
761*c2c66affSColin Finck /*
762*c2c66affSColin Finck  * When the upper or lower edge of any region changes, the region is
763*c2c66affSColin Finck  * marked "dirty".  This routine walks through all the dirty regions
764*c2c66affSColin Finck  * and makes sure that the dictionary invariants are satisfied
765*c2c66affSColin Finck  * (see the comments at the beginning of this file).  Of course
766*c2c66affSColin Finck  * new dirty regions can be created as we make changes to restore
767*c2c66affSColin Finck  * the invariants.
768*c2c66affSColin Finck  */
769*c2c66affSColin Finck {
770*c2c66affSColin Finck   ActiveRegion *regLo = RegionBelow(regUp);
771*c2c66affSColin Finck   GLUhalfEdge *eUp, *eLo;
772*c2c66affSColin Finck 
773*c2c66affSColin Finck   for( ;; ) {
774*c2c66affSColin Finck     /* Find the lowest dirty region (we walk from the bottom up). */
775*c2c66affSColin Finck     while( regLo->dirty ) {
776*c2c66affSColin Finck       regUp = regLo;
777*c2c66affSColin Finck       regLo = RegionBelow(regLo);
778*c2c66affSColin Finck     }
779*c2c66affSColin Finck     if( ! regUp->dirty ) {
780*c2c66affSColin Finck       regLo = regUp;
781*c2c66affSColin Finck       regUp = RegionAbove( regUp );
782*c2c66affSColin Finck       if( regUp == NULL || ! regUp->dirty ) {
783*c2c66affSColin Finck 	/* We've walked all the dirty regions */
784*c2c66affSColin Finck 	return;
785*c2c66affSColin Finck       }
786*c2c66affSColin Finck     }
787*c2c66affSColin Finck     regUp->dirty = FALSE;
788*c2c66affSColin Finck     eUp = regUp->eUp;
789*c2c66affSColin Finck     eLo = regLo->eUp;
790*c2c66affSColin Finck 
791*c2c66affSColin Finck     if( eUp->Dst != eLo->Dst ) {
792*c2c66affSColin Finck       /* Check that the edge ordering is obeyed at the Dst vertices. */
793*c2c66affSColin Finck       if( CheckForLeftSplice( tess, regUp )) {
794*c2c66affSColin Finck 
795*c2c66affSColin Finck 	/* If the upper or lower edge was marked fixUpperEdge, then
796*c2c66affSColin Finck 	 * we no longer need it (since these edges are needed only for
797*c2c66affSColin Finck 	 * vertices which otherwise have no right-going edges).
798*c2c66affSColin Finck 	 */
799*c2c66affSColin Finck 	if( regLo->fixUpperEdge ) {
800*c2c66affSColin Finck 	  DeleteRegion( tess, regLo );
801*c2c66affSColin Finck 	  if ( !__gl_meshDelete( eLo ) ) longjmp(tess->env,1);
802*c2c66affSColin Finck 	  regLo = RegionBelow( regUp );
803*c2c66affSColin Finck 	  eLo = regLo->eUp;
804*c2c66affSColin Finck 	} else if( regUp->fixUpperEdge ) {
805*c2c66affSColin Finck 	  DeleteRegion( tess, regUp );
806*c2c66affSColin Finck 	  if ( !__gl_meshDelete( eUp ) ) longjmp(tess->env,1);
807*c2c66affSColin Finck 	  regUp = RegionAbove( regLo );
808*c2c66affSColin Finck 	  eUp = regUp->eUp;
809*c2c66affSColin Finck 	}
810*c2c66affSColin Finck       }
811*c2c66affSColin Finck     }
812*c2c66affSColin Finck     if( eUp->Org != eLo->Org ) {
813*c2c66affSColin Finck       if(    eUp->Dst != eLo->Dst
814*c2c66affSColin Finck 	  && ! regUp->fixUpperEdge && ! regLo->fixUpperEdge
815*c2c66affSColin Finck 	  && (eUp->Dst == tess->event || eLo->Dst == tess->event) )
816*c2c66affSColin Finck       {
817*c2c66affSColin Finck 	/* When all else fails in CheckForIntersect(), it uses tess->event
818*c2c66affSColin Finck 	 * as the intersection location.  To make this possible, it requires
819*c2c66affSColin Finck 	 * that tess->event lie between the upper and lower edges, and also
820*c2c66affSColin Finck 	 * that neither of these is marked fixUpperEdge (since in the worst
821*c2c66affSColin Finck 	 * case it might splice one of these edges into tess->event, and
822*c2c66affSColin Finck 	 * violate the invariant that fixable edges are the only right-going
823*c2c66affSColin Finck 	 * edge from their associated vertex).
824*c2c66affSColin Finck 	 */
825*c2c66affSColin Finck 	if( CheckForIntersect( tess, regUp )) {
826*c2c66affSColin Finck 	  /* WalkDirtyRegions() was called recursively; we're done */
827*c2c66affSColin Finck 	  return;
828*c2c66affSColin Finck 	}
829*c2c66affSColin Finck       } else {
830*c2c66affSColin Finck 	/* Even though we can't use CheckForIntersect(), the Org vertices
831*c2c66affSColin Finck 	 * may violate the dictionary edge ordering.  Check and correct this.
832*c2c66affSColin Finck 	 */
833*c2c66affSColin Finck 	(void) CheckForRightSplice( tess, regUp );
834*c2c66affSColin Finck       }
835*c2c66affSColin Finck     }
836*c2c66affSColin Finck     if( eUp->Org == eLo->Org && eUp->Dst == eLo->Dst ) {
837*c2c66affSColin Finck       /* A degenerate loop consisting of only two edges -- delete it. */
838*c2c66affSColin Finck       AddWinding( eLo, eUp );
839*c2c66affSColin Finck       DeleteRegion( tess, regUp );
840*c2c66affSColin Finck       if ( !__gl_meshDelete( eUp ) ) longjmp(tess->env,1);
841*c2c66affSColin Finck       regUp = RegionAbove( regLo );
842*c2c66affSColin Finck     }
843*c2c66affSColin Finck   }
844*c2c66affSColin Finck }
845*c2c66affSColin Finck 
846*c2c66affSColin Finck 
ConnectRightVertex(GLUtesselator * tess,ActiveRegion * regUp,GLUhalfEdge * eBottomLeft)847*c2c66affSColin Finck static void ConnectRightVertex( GLUtesselator *tess, ActiveRegion *regUp,
848*c2c66affSColin Finck 				GLUhalfEdge *eBottomLeft )
849*c2c66affSColin Finck /*
850*c2c66affSColin Finck  * Purpose: connect a "right" vertex vEvent (one where all edges go left)
851*c2c66affSColin Finck  * to the unprocessed portion of the mesh.  Since there are no right-going
852*c2c66affSColin Finck  * edges, two regions (one above vEvent and one below) are being merged
853*c2c66affSColin Finck  * into one.  "regUp" is the upper of these two regions.
854*c2c66affSColin Finck  *
855*c2c66affSColin Finck  * There are two reasons for doing this (adding a right-going edge):
856*c2c66affSColin Finck  *  - if the two regions being merged are "inside", we must add an edge
857*c2c66affSColin Finck  *    to keep them separated (the combined region would not be monotone).
858*c2c66affSColin Finck  *  - in any case, we must leave some record of vEvent in the dictionary,
859*c2c66affSColin Finck  *    so that we can merge vEvent with features that we have not seen yet.
860*c2c66affSColin Finck  *    For example, maybe there is a vertical edge which passes just to
861*c2c66affSColin Finck  *    the right of vEvent; we would like to splice vEvent into this edge.
862*c2c66affSColin Finck  *
863*c2c66affSColin Finck  * However, we don't want to connect vEvent to just any vertex.  We don''t
864*c2c66affSColin Finck  * want the new edge to cross any other edges; otherwise we will create
865*c2c66affSColin Finck  * intersection vertices even when the input data had no self-intersections.
866*c2c66affSColin Finck  * (This is a bad thing; if the user's input data has no intersections,
867*c2c66affSColin Finck  * we don't want to generate any false intersections ourselves.)
868*c2c66affSColin Finck  *
869*c2c66affSColin Finck  * Our eventual goal is to connect vEvent to the leftmost unprocessed
870*c2c66affSColin Finck  * vertex of the combined region (the union of regUp and regLo).
871*c2c66affSColin Finck  * But because of unseen vertices with all right-going edges, and also
872*c2c66affSColin Finck  * new vertices which may be created by edge intersections, we don''t
873*c2c66affSColin Finck  * know where that leftmost unprocessed vertex is.  In the meantime, we
874*c2c66affSColin Finck  * connect vEvent to the closest vertex of either chain, and mark the region
875*c2c66affSColin Finck  * as "fixUpperEdge".  This flag says to delete and reconnect this edge
876*c2c66affSColin Finck  * to the next processed vertex on the boundary of the combined region.
877*c2c66affSColin Finck  * Quite possibly the vertex we connected to will turn out to be the
878*c2c66affSColin Finck  * closest one, in which case we won''t need to make any changes.
879*c2c66affSColin Finck  */
880*c2c66affSColin Finck {
881*c2c66affSColin Finck   GLUhalfEdge *eNew;
882*c2c66affSColin Finck   GLUhalfEdge *eTopLeft = eBottomLeft->Onext;
883*c2c66affSColin Finck   ActiveRegion *regLo = RegionBelow(regUp);
884*c2c66affSColin Finck   GLUhalfEdge *eUp = regUp->eUp;
885*c2c66affSColin Finck   GLUhalfEdge *eLo = regLo->eUp;
886*c2c66affSColin Finck   int degenerate = FALSE;
887*c2c66affSColin Finck 
888*c2c66affSColin Finck   if( eUp->Dst != eLo->Dst ) {
889*c2c66affSColin Finck     (void) CheckForIntersect( tess, regUp );
890*c2c66affSColin Finck   }
891*c2c66affSColin Finck 
892*c2c66affSColin Finck   /* Possible new degeneracies: upper or lower edge of regUp may pass
893*c2c66affSColin Finck    * through vEvent, or may coincide with new intersection vertex
894*c2c66affSColin Finck    */
895*c2c66affSColin Finck   if( VertEq( eUp->Org, tess->event )) {
896*c2c66affSColin Finck     if ( !__gl_meshSplice( eTopLeft->Oprev, eUp ) ) longjmp(tess->env,1);
897*c2c66affSColin Finck     regUp = TopLeftRegion( regUp );
898*c2c66affSColin Finck     if (regUp == NULL) longjmp(tess->env,1);
899*c2c66affSColin Finck     eTopLeft = RegionBelow( regUp )->eUp;
900*c2c66affSColin Finck     FinishLeftRegions( tess, RegionBelow(regUp), regLo );
901*c2c66affSColin Finck     degenerate = TRUE;
902*c2c66affSColin Finck   }
903*c2c66affSColin Finck   if( VertEq( eLo->Org, tess->event )) {
904*c2c66affSColin Finck     if ( !__gl_meshSplice( eBottomLeft, eLo->Oprev ) ) longjmp(tess->env,1);
905*c2c66affSColin Finck     eBottomLeft = FinishLeftRegions( tess, regLo, NULL );
906*c2c66affSColin Finck     degenerate = TRUE;
907*c2c66affSColin Finck   }
908*c2c66affSColin Finck   if( degenerate ) {
909*c2c66affSColin Finck     AddRightEdges( tess, regUp, eBottomLeft->Onext, eTopLeft, eTopLeft, TRUE );
910*c2c66affSColin Finck     return;
911*c2c66affSColin Finck   }
912*c2c66affSColin Finck 
913*c2c66affSColin Finck   /* Non-degenerate situation -- need to add a temporary, fixable edge.
914*c2c66affSColin Finck    * Connect to the closer of eLo->Org, eUp->Org.
915*c2c66affSColin Finck    */
916*c2c66affSColin Finck   if( VertLeq( eLo->Org, eUp->Org )) {
917*c2c66affSColin Finck     eNew = eLo->Oprev;
918*c2c66affSColin Finck   } else {
919*c2c66affSColin Finck     eNew = eUp;
920*c2c66affSColin Finck   }
921*c2c66affSColin Finck   eNew = __gl_meshConnect( eBottomLeft->Lprev, eNew );
922*c2c66affSColin Finck   if (eNew == NULL) longjmp(tess->env,1);
923*c2c66affSColin Finck 
924*c2c66affSColin Finck   /* Prevent cleanup, otherwise eNew might disappear before we've even
925*c2c66affSColin Finck    * had a chance to mark it as a temporary edge.
926*c2c66affSColin Finck    */
927*c2c66affSColin Finck   AddRightEdges( tess, regUp, eNew, eNew->Onext, eNew->Onext, FALSE );
928*c2c66affSColin Finck   eNew->Sym->activeRegion->fixUpperEdge = TRUE;
929*c2c66affSColin Finck   WalkDirtyRegions( tess, regUp );
930*c2c66affSColin Finck }
931*c2c66affSColin Finck 
932*c2c66affSColin Finck /* Because vertices at exactly the same location are merged together
933*c2c66affSColin Finck  * before we process the sweep event, some degenerate cases can't occur.
934*c2c66affSColin Finck  * However if someone eventually makes the modifications required to
935*c2c66affSColin Finck  * merge features which are close together, the cases below marked
936*c2c66affSColin Finck  * TOLERANCE_NONZERO will be useful.  They were debugged before the
937*c2c66affSColin Finck  * code to merge identical vertices in the main loop was added.
938*c2c66affSColin Finck  */
939*c2c66affSColin Finck #define TOLERANCE_NONZERO	FALSE
940*c2c66affSColin Finck 
ConnectLeftDegenerate(GLUtesselator * tess,ActiveRegion * regUp,GLUvertex * vEvent)941*c2c66affSColin Finck static void ConnectLeftDegenerate( GLUtesselator *tess,
942*c2c66affSColin Finck 				   ActiveRegion *regUp, GLUvertex *vEvent )
943*c2c66affSColin Finck /*
944*c2c66affSColin Finck  * The event vertex lies exacty on an already-processed edge or vertex.
945*c2c66affSColin Finck  * Adding the new vertex involves splicing it into the already-processed
946*c2c66affSColin Finck  * part of the mesh.
947*c2c66affSColin Finck  */
948*c2c66affSColin Finck {
949*c2c66affSColin Finck   GLUhalfEdge *e, *eTopLeft, *eTopRight, *eLast;
950*c2c66affSColin Finck   ActiveRegion *reg;
951*c2c66affSColin Finck 
952*c2c66affSColin Finck   e = regUp->eUp;
953*c2c66affSColin Finck   if( VertEq( e->Org, vEvent )) {
954*c2c66affSColin Finck     /* e->Org is an unprocessed vertex - just combine them, and wait
955*c2c66affSColin Finck      * for e->Org to be pulled from the queue
956*c2c66affSColin Finck      */
957*c2c66affSColin Finck     assert( TOLERANCE_NONZERO );
958*c2c66affSColin Finck     SpliceMergeVertices( tess, e, vEvent->anEdge );
959*c2c66affSColin Finck     return;
960*c2c66affSColin Finck   }
961*c2c66affSColin Finck 
962*c2c66affSColin Finck   if( ! VertEq( e->Dst, vEvent )) {
963*c2c66affSColin Finck     /* General case -- splice vEvent into edge e which passes through it */
964*c2c66affSColin Finck     if (__gl_meshSplitEdge( e->Sym ) == NULL) longjmp(tess->env,1);
965*c2c66affSColin Finck     if( regUp->fixUpperEdge ) {
966*c2c66affSColin Finck       /* This edge was fixable -- delete unused portion of original edge */
967*c2c66affSColin Finck       if ( !__gl_meshDelete( e->Onext ) ) longjmp(tess->env,1);
968*c2c66affSColin Finck       regUp->fixUpperEdge = FALSE;
969*c2c66affSColin Finck     }
970*c2c66affSColin Finck     if ( !__gl_meshSplice( vEvent->anEdge, e ) ) longjmp(tess->env,1);
971*c2c66affSColin Finck     SweepEvent( tess, vEvent ); /* recurse */
972*c2c66affSColin Finck     return;
973*c2c66affSColin Finck   }
974*c2c66affSColin Finck 
975*c2c66affSColin Finck   /* vEvent coincides with e->Dst, which has already been processed.
976*c2c66affSColin Finck    * Splice in the additional right-going edges.
977*c2c66affSColin Finck    */
978*c2c66affSColin Finck   assert( TOLERANCE_NONZERO );
979*c2c66affSColin Finck   regUp = TopRightRegion( regUp );
980*c2c66affSColin Finck   reg = RegionBelow( regUp );
981*c2c66affSColin Finck   eTopRight = reg->eUp->Sym;
982*c2c66affSColin Finck   eTopLeft = eLast = eTopRight->Onext;
983*c2c66affSColin Finck   if( reg->fixUpperEdge ) {
984*c2c66affSColin Finck     /* Here e->Dst has only a single fixable edge going right.
985*c2c66affSColin Finck      * We can delete it since now we have some real right-going edges.
986*c2c66affSColin Finck      */
987*c2c66affSColin Finck     assert( eTopLeft != eTopRight );   /* there are some left edges too */
988*c2c66affSColin Finck     DeleteRegion( tess, reg );
989*c2c66affSColin Finck     if ( !__gl_meshDelete( eTopRight ) ) longjmp(tess->env,1);
990*c2c66affSColin Finck     eTopRight = eTopLeft->Oprev;
991*c2c66affSColin Finck   }
992*c2c66affSColin Finck   if ( !__gl_meshSplice( vEvent->anEdge, eTopRight ) ) longjmp(tess->env,1);
993*c2c66affSColin Finck   if( ! EdgeGoesLeft( eTopLeft )) {
994*c2c66affSColin Finck     /* e->Dst had no left-going edges -- indicate this to AddRightEdges() */
995*c2c66affSColin Finck     eTopLeft = NULL;
996*c2c66affSColin Finck   }
997*c2c66affSColin Finck   AddRightEdges( tess, regUp, eTopRight->Onext, eLast, eTopLeft, TRUE );
998*c2c66affSColin Finck }
999*c2c66affSColin Finck 
1000*c2c66affSColin Finck 
ConnectLeftVertex(GLUtesselator * tess,GLUvertex * vEvent)1001*c2c66affSColin Finck static void ConnectLeftVertex( GLUtesselator *tess, GLUvertex *vEvent )
1002*c2c66affSColin Finck /*
1003*c2c66affSColin Finck  * Purpose: connect a "left" vertex (one where both edges go right)
1004*c2c66affSColin Finck  * to the processed portion of the mesh.  Let R be the active region
1005*c2c66affSColin Finck  * containing vEvent, and let U and L be the upper and lower edge
1006*c2c66affSColin Finck  * chains of R.  There are two possibilities:
1007*c2c66affSColin Finck  *
1008*c2c66affSColin Finck  * - the normal case: split R into two regions, by connecting vEvent to
1009*c2c66affSColin Finck  *   the rightmost vertex of U or L lying to the left of the sweep line
1010*c2c66affSColin Finck  *
1011*c2c66affSColin Finck  * - the degenerate case: if vEvent is close enough to U or L, we
1012*c2c66affSColin Finck  *   merge vEvent into that edge chain.  The subcases are:
1013*c2c66affSColin Finck  *	- merging with the rightmost vertex of U or L
1014*c2c66affSColin Finck  *	- merging with the active edge of U or L
1015*c2c66affSColin Finck  *	- merging with an already-processed portion of U or L
1016*c2c66affSColin Finck  */
1017*c2c66affSColin Finck {
1018*c2c66affSColin Finck   ActiveRegion *regUp, *regLo, *reg;
1019*c2c66affSColin Finck   GLUhalfEdge *eUp, *eLo, *eNew;
1020*c2c66affSColin Finck   ActiveRegion tmp;
1021*c2c66affSColin Finck 
1022*c2c66affSColin Finck   /* assert( vEvent->anEdge->Onext->Onext == vEvent->anEdge ); */
1023*c2c66affSColin Finck 
1024*c2c66affSColin Finck   /* Get a pointer to the active region containing vEvent */
1025*c2c66affSColin Finck   tmp.eUp = vEvent->anEdge->Sym;
1026*c2c66affSColin Finck   /* __GL_DICTLISTKEY */ /* __gl_dictListSearch */
1027*c2c66affSColin Finck   regUp = (ActiveRegion *)dictKey( dictSearch( tess->dict, &tmp ));
1028*c2c66affSColin Finck   regLo = RegionBelow( regUp );
1029*c2c66affSColin Finck   eUp = regUp->eUp;
1030*c2c66affSColin Finck   eLo = regLo->eUp;
1031*c2c66affSColin Finck 
1032*c2c66affSColin Finck   /* Try merging with U or L first */
1033*c2c66affSColin Finck   if( EdgeSign( eUp->Dst, vEvent, eUp->Org ) == 0 ) {
1034*c2c66affSColin Finck     ConnectLeftDegenerate( tess, regUp, vEvent );
1035*c2c66affSColin Finck     return;
1036*c2c66affSColin Finck   }
1037*c2c66affSColin Finck 
1038*c2c66affSColin Finck   /* Connect vEvent to rightmost processed vertex of either chain.
1039*c2c66affSColin Finck    * e->Dst is the vertex that we will connect to vEvent.
1040*c2c66affSColin Finck    */
1041*c2c66affSColin Finck   reg = VertLeq( eLo->Dst, eUp->Dst ) ? regUp : regLo;
1042*c2c66affSColin Finck 
1043*c2c66affSColin Finck   if( regUp->inside || reg->fixUpperEdge) {
1044*c2c66affSColin Finck     if( reg == regUp ) {
1045*c2c66affSColin Finck       eNew = __gl_meshConnect( vEvent->anEdge->Sym, eUp->Lnext );
1046*c2c66affSColin Finck       if (eNew == NULL) longjmp(tess->env,1);
1047*c2c66affSColin Finck     } else {
1048*c2c66affSColin Finck       GLUhalfEdge *tempHalfEdge= __gl_meshConnect( eLo->Dnext, vEvent->anEdge);
1049*c2c66affSColin Finck       if (tempHalfEdge == NULL) longjmp(tess->env,1);
1050*c2c66affSColin Finck 
1051*c2c66affSColin Finck       eNew = tempHalfEdge->Sym;
1052*c2c66affSColin Finck     }
1053*c2c66affSColin Finck     if( reg->fixUpperEdge ) {
1054*c2c66affSColin Finck       if ( !FixUpperEdge( reg, eNew ) ) longjmp(tess->env,1);
1055*c2c66affSColin Finck     } else {
1056*c2c66affSColin Finck       ComputeWinding( tess, AddRegionBelow( tess, regUp, eNew ));
1057*c2c66affSColin Finck     }
1058*c2c66affSColin Finck     SweepEvent( tess, vEvent );
1059*c2c66affSColin Finck   } else {
1060*c2c66affSColin Finck     /* The new vertex is in a region which does not belong to the polygon.
1061*c2c66affSColin Finck      * We don''t need to connect this vertex to the rest of the mesh.
1062*c2c66affSColin Finck      */
1063*c2c66affSColin Finck     AddRightEdges( tess, regUp, vEvent->anEdge, vEvent->anEdge, NULL, TRUE );
1064*c2c66affSColin Finck   }
1065*c2c66affSColin Finck }
1066*c2c66affSColin Finck 
1067*c2c66affSColin Finck 
SweepEvent(GLUtesselator * tess,GLUvertex * vEvent)1068*c2c66affSColin Finck static void SweepEvent( GLUtesselator *tess, GLUvertex *vEvent )
1069*c2c66affSColin Finck /*
1070*c2c66affSColin Finck  * Does everything necessary when the sweep line crosses a vertex.
1071*c2c66affSColin Finck  * Updates the mesh and the edge dictionary.
1072*c2c66affSColin Finck  */
1073*c2c66affSColin Finck {
1074*c2c66affSColin Finck   ActiveRegion *regUp, *reg;
1075*c2c66affSColin Finck   GLUhalfEdge *e, *eTopLeft, *eBottomLeft;
1076*c2c66affSColin Finck 
1077*c2c66affSColin Finck   tess->event = vEvent; 	/* for access in EdgeLeq() */
1078*c2c66affSColin Finck   DebugEvent( tess );
1079*c2c66affSColin Finck 
1080*c2c66affSColin Finck   /* Check if this vertex is the right endpoint of an edge that is
1081*c2c66affSColin Finck    * already in the dictionary.  In this case we don't need to waste
1082*c2c66affSColin Finck    * time searching for the location to insert new edges.
1083*c2c66affSColin Finck    */
1084*c2c66affSColin Finck   e = vEvent->anEdge;
1085*c2c66affSColin Finck   while( e->activeRegion == NULL ) {
1086*c2c66affSColin Finck     e = e->Onext;
1087*c2c66affSColin Finck     if( e == vEvent->anEdge ) {
1088*c2c66affSColin Finck       /* All edges go right -- not incident to any processed edges */
1089*c2c66affSColin Finck       ConnectLeftVertex( tess, vEvent );
1090*c2c66affSColin Finck       return;
1091*c2c66affSColin Finck     }
1092*c2c66affSColin Finck   }
1093*c2c66affSColin Finck 
1094*c2c66affSColin Finck   /* Processing consists of two phases: first we "finish" all the
1095*c2c66affSColin Finck    * active regions where both the upper and lower edges terminate
1096*c2c66affSColin Finck    * at vEvent (ie. vEvent is closing off these regions).
1097*c2c66affSColin Finck    * We mark these faces "inside" or "outside" the polygon according
1098*c2c66affSColin Finck    * to their winding number, and delete the edges from the dictionary.
1099*c2c66affSColin Finck    * This takes care of all the left-going edges from vEvent.
1100*c2c66affSColin Finck    */
1101*c2c66affSColin Finck   regUp = TopLeftRegion( e->activeRegion );
1102*c2c66affSColin Finck   if (regUp == NULL) longjmp(tess->env,1);
1103*c2c66affSColin Finck   reg = RegionBelow( regUp );
1104*c2c66affSColin Finck   eTopLeft = reg->eUp;
1105*c2c66affSColin Finck   eBottomLeft = FinishLeftRegions( tess, reg, NULL );
1106*c2c66affSColin Finck 
1107*c2c66affSColin Finck   /* Next we process all the right-going edges from vEvent.  This
1108*c2c66affSColin Finck    * involves adding the edges to the dictionary, and creating the
1109*c2c66affSColin Finck    * associated "active regions" which record information about the
1110*c2c66affSColin Finck    * regions between adjacent dictionary edges.
1111*c2c66affSColin Finck    */
1112*c2c66affSColin Finck   if( eBottomLeft->Onext == eTopLeft ) {
1113*c2c66affSColin Finck     /* No right-going edges -- add a temporary "fixable" edge */
1114*c2c66affSColin Finck     ConnectRightVertex( tess, regUp, eBottomLeft );
1115*c2c66affSColin Finck   } else {
1116*c2c66affSColin Finck     AddRightEdges( tess, regUp, eBottomLeft->Onext, eTopLeft, eTopLeft, TRUE );
1117*c2c66affSColin Finck   }
1118*c2c66affSColin Finck }
1119*c2c66affSColin Finck 
1120*c2c66affSColin Finck 
1121*c2c66affSColin Finck /* Make the sentinel coordinates big enough that they will never be
1122*c2c66affSColin Finck  * merged with real input features.  (Even with the largest possible
1123*c2c66affSColin Finck  * input contour and the maximum tolerance of 1.0, no merging will be
1124*c2c66affSColin Finck  * done with coordinates larger than 3 * GLU_TESS_MAX_COORD).
1125*c2c66affSColin Finck  */
1126*c2c66affSColin Finck #define SENTINEL_COORD	(4 * GLU_TESS_MAX_COORD)
1127*c2c66affSColin Finck 
AddSentinel(GLUtesselator * tess,GLdouble t)1128*c2c66affSColin Finck static void AddSentinel( GLUtesselator *tess, GLdouble t )
1129*c2c66affSColin Finck /*
1130*c2c66affSColin Finck  * We add two sentinel edges above and below all other edges,
1131*c2c66affSColin Finck  * to avoid special cases at the top and bottom.
1132*c2c66affSColin Finck  */
1133*c2c66affSColin Finck {
1134*c2c66affSColin Finck   GLUhalfEdge *e;
1135*c2c66affSColin Finck   ActiveRegion *reg = (ActiveRegion *)memAlloc( sizeof( ActiveRegion ));
1136*c2c66affSColin Finck   if (reg == NULL) longjmp(tess->env,1);
1137*c2c66affSColin Finck 
1138*c2c66affSColin Finck   e = __gl_meshMakeEdge( tess->mesh );
1139*c2c66affSColin Finck   if (e == NULL) longjmp(tess->env,1);
1140*c2c66affSColin Finck 
1141*c2c66affSColin Finck   e->Org->s = SENTINEL_COORD;
1142*c2c66affSColin Finck   e->Org->t = t;
1143*c2c66affSColin Finck   e->Dst->s = -SENTINEL_COORD;
1144*c2c66affSColin Finck   e->Dst->t = t;
1145*c2c66affSColin Finck   tess->event = e->Dst; 	/* initialize it */
1146*c2c66affSColin Finck 
1147*c2c66affSColin Finck   reg->eUp = e;
1148*c2c66affSColin Finck   reg->windingNumber = 0;
1149*c2c66affSColin Finck   reg->inside = FALSE;
1150*c2c66affSColin Finck   reg->fixUpperEdge = FALSE;
1151*c2c66affSColin Finck   reg->sentinel = TRUE;
1152*c2c66affSColin Finck   reg->dirty = FALSE;
1153*c2c66affSColin Finck   reg->nodeUp = dictInsert( tess->dict, reg ); /* __gl_dictListInsertBefore */
1154*c2c66affSColin Finck   if (reg->nodeUp == NULL) longjmp(tess->env,1);
1155*c2c66affSColin Finck }
1156*c2c66affSColin Finck 
1157*c2c66affSColin Finck 
InitEdgeDict(GLUtesselator * tess)1158*c2c66affSColin Finck static void InitEdgeDict( GLUtesselator *tess )
1159*c2c66affSColin Finck /*
1160*c2c66affSColin Finck  * We maintain an ordering of edge intersections with the sweep line.
1161*c2c66affSColin Finck  * This order is maintained in a dynamic dictionary.
1162*c2c66affSColin Finck  */
1163*c2c66affSColin Finck {
1164*c2c66affSColin Finck   /* __gl_dictListNewDict */
1165*c2c66affSColin Finck   tess->dict = dictNewDict( tess, (int (*)(void *, DictKey, DictKey)) EdgeLeq );
1166*c2c66affSColin Finck   if (tess->dict == NULL) longjmp(tess->env,1);
1167*c2c66affSColin Finck 
1168*c2c66affSColin Finck   AddSentinel( tess, -SENTINEL_COORD );
1169*c2c66affSColin Finck   AddSentinel( tess, SENTINEL_COORD );
1170*c2c66affSColin Finck }
1171*c2c66affSColin Finck 
1172*c2c66affSColin Finck 
DoneEdgeDict(GLUtesselator * tess)1173*c2c66affSColin Finck static void DoneEdgeDict( GLUtesselator *tess )
1174*c2c66affSColin Finck {
1175*c2c66affSColin Finck   ActiveRegion *reg;
1176*c2c66affSColin Finck #ifndef NDEBUG
1177*c2c66affSColin Finck   int fixedEdges = 0;
1178*c2c66affSColin Finck #endif
1179*c2c66affSColin Finck 
1180*c2c66affSColin Finck   /* __GL_DICTLISTKEY */ /* __GL_DICTLISTMIN */
1181*c2c66affSColin Finck   while( (reg = (ActiveRegion *)dictKey( dictMin( tess->dict ))) != NULL ) {
1182*c2c66affSColin Finck     /*
1183*c2c66affSColin Finck      * At the end of all processing, the dictionary should contain
1184*c2c66affSColin Finck      * only the two sentinel edges, plus at most one "fixable" edge
1185*c2c66affSColin Finck      * created by ConnectRightVertex().
1186*c2c66affSColin Finck      */
1187*c2c66affSColin Finck     if( ! reg->sentinel ) {
1188*c2c66affSColin Finck       assert( reg->fixUpperEdge );
1189*c2c66affSColin Finck       assert( ++fixedEdges == 1 );
1190*c2c66affSColin Finck     }
1191*c2c66affSColin Finck     assert( reg->windingNumber == 0 );
1192*c2c66affSColin Finck     DeleteRegion( tess, reg );
1193*c2c66affSColin Finck /*    __gl_meshDelete( reg->eUp );*/
1194*c2c66affSColin Finck   }
1195*c2c66affSColin Finck   dictDeleteDict( tess->dict ); /* __gl_dictListDeleteDict */
1196*c2c66affSColin Finck }
1197*c2c66affSColin Finck 
1198*c2c66affSColin Finck 
RemoveDegenerateEdges(GLUtesselator * tess)1199*c2c66affSColin Finck static void RemoveDegenerateEdges( GLUtesselator *tess )
1200*c2c66affSColin Finck /*
1201*c2c66affSColin Finck  * Remove zero-length edges, and contours with fewer than 3 vertices.
1202*c2c66affSColin Finck  */
1203*c2c66affSColin Finck {
1204*c2c66affSColin Finck   GLUhalfEdge *e, *eNext, *eLnext;
1205*c2c66affSColin Finck   GLUhalfEdge *eHead = &tess->mesh->eHead;
1206*c2c66affSColin Finck 
1207*c2c66affSColin Finck   /*LINTED*/
1208*c2c66affSColin Finck   for( e = eHead->next; e != eHead; e = eNext ) {
1209*c2c66affSColin Finck     eNext = e->next;
1210*c2c66affSColin Finck     eLnext = e->Lnext;
1211*c2c66affSColin Finck 
1212*c2c66affSColin Finck     if( VertEq( e->Org, e->Dst ) && e->Lnext->Lnext != e ) {
1213*c2c66affSColin Finck       /* Zero-length edge, contour has at least 3 edges */
1214*c2c66affSColin Finck 
1215*c2c66affSColin Finck       SpliceMergeVertices( tess, eLnext, e );	/* deletes e->Org */
1216*c2c66affSColin Finck       if ( !__gl_meshDelete( e ) ) longjmp(tess->env,1); /* e is a self-loop */
1217*c2c66affSColin Finck       e = eLnext;
1218*c2c66affSColin Finck       eLnext = e->Lnext;
1219*c2c66affSColin Finck     }
1220*c2c66affSColin Finck     if( eLnext->Lnext == e ) {
1221*c2c66affSColin Finck       /* Degenerate contour (one or two edges) */
1222*c2c66affSColin Finck 
1223*c2c66affSColin Finck       if( eLnext != e ) {
1224*c2c66affSColin Finck 	if( eLnext == eNext || eLnext == eNext->Sym ) { eNext = eNext->next; }
1225*c2c66affSColin Finck 	if ( !__gl_meshDelete( eLnext ) ) longjmp(tess->env,1);
1226*c2c66affSColin Finck       }
1227*c2c66affSColin Finck       if( e == eNext || e == eNext->Sym ) { eNext = eNext->next; }
1228*c2c66affSColin Finck       if ( !__gl_meshDelete( e ) ) longjmp(tess->env,1);
1229*c2c66affSColin Finck     }
1230*c2c66affSColin Finck   }
1231*c2c66affSColin Finck }
1232*c2c66affSColin Finck 
InitPriorityQ(GLUtesselator * tess)1233*c2c66affSColin Finck static int InitPriorityQ( GLUtesselator *tess )
1234*c2c66affSColin Finck /*
1235*c2c66affSColin Finck  * Insert all vertices into the priority queue which determines the
1236*c2c66affSColin Finck  * order in which vertices cross the sweep line.
1237*c2c66affSColin Finck  */
1238*c2c66affSColin Finck {
1239*c2c66affSColin Finck   PriorityQ *pq;
1240*c2c66affSColin Finck   GLUvertex *v, *vHead;
1241*c2c66affSColin Finck 
1242*c2c66affSColin Finck   /* __gl_pqSortNewPriorityQ */
1243*c2c66affSColin Finck   pq = tess->pq = pqNewPriorityQ( (int (*)(PQkey, PQkey)) __gl_vertLeq );
1244*c2c66affSColin Finck   if (pq == NULL) return 0;
1245*c2c66affSColin Finck 
1246*c2c66affSColin Finck   vHead = &tess->mesh->vHead;
1247*c2c66affSColin Finck   for( v = vHead->next; v != vHead; v = v->next ) {
1248*c2c66affSColin Finck     v->pqHandle = pqInsert( pq, v ); /* __gl_pqSortInsert */
1249*c2c66affSColin Finck     if (v->pqHandle == LONG_MAX) break;
1250*c2c66affSColin Finck   }
1251*c2c66affSColin Finck   if (v != vHead || !pqInit( pq ) ) { /* __gl_pqSortInit */
1252*c2c66affSColin Finck     pqDeletePriorityQ(tess->pq);	/* __gl_pqSortDeletePriorityQ */
1253*c2c66affSColin Finck     tess->pq = NULL;
1254*c2c66affSColin Finck     return 0;
1255*c2c66affSColin Finck   }
1256*c2c66affSColin Finck 
1257*c2c66affSColin Finck   return 1;
1258*c2c66affSColin Finck }
1259*c2c66affSColin Finck 
1260*c2c66affSColin Finck 
DonePriorityQ(GLUtesselator * tess)1261*c2c66affSColin Finck static void DonePriorityQ( GLUtesselator *tess )
1262*c2c66affSColin Finck {
1263*c2c66affSColin Finck   pqDeletePriorityQ( tess->pq ); /* __gl_pqSortDeletePriorityQ */
1264*c2c66affSColin Finck }
1265*c2c66affSColin Finck 
1266*c2c66affSColin Finck 
RemoveDegenerateFaces(GLUmesh * mesh)1267*c2c66affSColin Finck static int RemoveDegenerateFaces( GLUmesh *mesh )
1268*c2c66affSColin Finck /*
1269*c2c66affSColin Finck  * Delete any degenerate faces with only two edges.  WalkDirtyRegions()
1270*c2c66affSColin Finck  * will catch almost all of these, but it won't catch degenerate faces
1271*c2c66affSColin Finck  * produced by splice operations on already-processed edges.
1272*c2c66affSColin Finck  * The two places this can happen are in FinishLeftRegions(), when
1273*c2c66affSColin Finck  * we splice in a "temporary" edge produced by ConnectRightVertex(),
1274*c2c66affSColin Finck  * and in CheckForLeftSplice(), where we splice already-processed
1275*c2c66affSColin Finck  * edges to ensure that our dictionary invariants are not violated
1276*c2c66affSColin Finck  * by numerical errors.
1277*c2c66affSColin Finck  *
1278*c2c66affSColin Finck  * In both these cases it is *very* dangerous to delete the offending
1279*c2c66affSColin Finck  * edge at the time, since one of the routines further up the stack
1280*c2c66affSColin Finck  * will sometimes be keeping a pointer to that edge.
1281*c2c66affSColin Finck  */
1282*c2c66affSColin Finck {
1283*c2c66affSColin Finck   GLUface *f, *fNext;
1284*c2c66affSColin Finck   GLUhalfEdge *e;
1285*c2c66affSColin Finck 
1286*c2c66affSColin Finck   /*LINTED*/
1287*c2c66affSColin Finck   for( f = mesh->fHead.next; f != &mesh->fHead; f = fNext ) {
1288*c2c66affSColin Finck     fNext = f->next;
1289*c2c66affSColin Finck     e = f->anEdge;
1290*c2c66affSColin Finck     assert( e->Lnext != e );
1291*c2c66affSColin Finck 
1292*c2c66affSColin Finck     if( e->Lnext->Lnext == e ) {
1293*c2c66affSColin Finck       /* A face with only two edges */
1294*c2c66affSColin Finck       AddWinding( e->Onext, e );
1295*c2c66affSColin Finck       if ( !__gl_meshDelete( e ) ) return 0;
1296*c2c66affSColin Finck     }
1297*c2c66affSColin Finck   }
1298*c2c66affSColin Finck   return 1;
1299*c2c66affSColin Finck }
1300*c2c66affSColin Finck 
__gl_computeInterior(GLUtesselator * tess)1301*c2c66affSColin Finck int __gl_computeInterior( GLUtesselator *tess )
1302*c2c66affSColin Finck /*
1303*c2c66affSColin Finck  * __gl_computeInterior( tess ) computes the planar arrangement specified
1304*c2c66affSColin Finck  * by the given contours, and further subdivides this arrangement
1305*c2c66affSColin Finck  * into regions.  Each region is marked "inside" if it belongs
1306*c2c66affSColin Finck  * to the polygon, according to the rule given by tess->windingRule.
1307*c2c66affSColin Finck  * Each interior region is guaranteed be monotone.
1308*c2c66affSColin Finck  */
1309*c2c66affSColin Finck {
1310*c2c66affSColin Finck   GLUvertex *v, *vNext;
1311*c2c66affSColin Finck 
1312*c2c66affSColin Finck   tess->fatalError = FALSE;
1313*c2c66affSColin Finck 
1314*c2c66affSColin Finck   /* Each vertex defines an event for our sweep line.  Start by inserting
1315*c2c66affSColin Finck    * all the vertices in a priority queue.  Events are processed in
1316*c2c66affSColin Finck    * lexicographic order, ie.
1317*c2c66affSColin Finck    *
1318*c2c66affSColin Finck    *	e1 < e2  iff  e1.x < e2.x || (e1.x == e2.x && e1.y < e2.y)
1319*c2c66affSColin Finck    */
1320*c2c66affSColin Finck   RemoveDegenerateEdges( tess );
1321*c2c66affSColin Finck   if ( !InitPriorityQ( tess ) ) return 0; /* if error */
1322*c2c66affSColin Finck   InitEdgeDict( tess );
1323*c2c66affSColin Finck 
1324*c2c66affSColin Finck   /* __gl_pqSortExtractMin */
1325*c2c66affSColin Finck   while( (v = (GLUvertex *)pqExtractMin( tess->pq )) != NULL ) {
1326*c2c66affSColin Finck     for( ;; ) {
1327*c2c66affSColin Finck       vNext = (GLUvertex *)pqMinimum( tess->pq ); /* __gl_pqSortMinimum */
1328*c2c66affSColin Finck       if( vNext == NULL || ! VertEq( vNext, v )) break;
1329*c2c66affSColin Finck 
1330*c2c66affSColin Finck       /* Merge together all vertices at exactly the same location.
1331*c2c66affSColin Finck        * This is more efficient than processing them one at a time,
1332*c2c66affSColin Finck        * simplifies the code (see ConnectLeftDegenerate), and is also
1333*c2c66affSColin Finck        * important for correct handling of certain degenerate cases.
1334*c2c66affSColin Finck        * For example, suppose there are two identical edges A and B
1335*c2c66affSColin Finck        * that belong to different contours (so without this code they would
1336*c2c66affSColin Finck        * be processed by separate sweep events).  Suppose another edge C
1337*c2c66affSColin Finck        * crosses A and B from above.  When A is processed, we split it
1338*c2c66affSColin Finck        * at its intersection point with C.  However this also splits C,
1339*c2c66affSColin Finck        * so when we insert B we may compute a slightly different
1340*c2c66affSColin Finck        * intersection point.  This might leave two edges with a small
1341*c2c66affSColin Finck        * gap between them.  This kind of error is especially obvious
1342*c2c66affSColin Finck        * when using boundary extraction (GLU_TESS_BOUNDARY_ONLY).
1343*c2c66affSColin Finck        */
1344*c2c66affSColin Finck       vNext = (GLUvertex *)pqExtractMin( tess->pq ); /* __gl_pqSortExtractMin*/
1345*c2c66affSColin Finck       SpliceMergeVertices( tess, v->anEdge, vNext->anEdge );
1346*c2c66affSColin Finck     }
1347*c2c66affSColin Finck     SweepEvent( tess, v );
1348*c2c66affSColin Finck   }
1349*c2c66affSColin Finck 
1350*c2c66affSColin Finck   /* Set tess->event for debugging purposes */
1351*c2c66affSColin Finck   /* __GL_DICTLISTKEY */ /* __GL_DICTLISTMIN */
1352*c2c66affSColin Finck   tess->event = ((ActiveRegion *) dictKey( dictMin( tess->dict )))->eUp->Org;
1353*c2c66affSColin Finck   DebugEvent( tess );
1354*c2c66affSColin Finck   DoneEdgeDict( tess );
1355*c2c66affSColin Finck   DonePriorityQ( tess );
1356*c2c66affSColin Finck 
1357*c2c66affSColin Finck   if ( !RemoveDegenerateFaces( tess->mesh ) ) return 0;
1358*c2c66affSColin Finck   __gl_meshCheckMesh( tess->mesh );
1359*c2c66affSColin Finck 
1360*c2c66affSColin Finck   return 1;
1361*c2c66affSColin Finck }
1362