2 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3 * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice including the dates of first publication and
13 * either this permission notice or a reference to
14 * http://oss.sgi.com/projects/FreeB/
15 * shall be included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
22 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * Except as contained in this notice, the name of Silicon Graphics, Inc.
26 * shall not be used in advertising or otherwise to promote the sale, use or
27 * other dealings in this Software without prior written authorization from
28 * Silicon Graphics, Inc.
31 ** Author: Eric Veach, July 1994.
38 #include <setjmp.h> /* longjmp */
39 #include <limits.h> /* LONG_MAX */
45 #include "priorityq.h"
52 #ifdef FOR_TRITE_TEST_PROGRAM
53 extern void DebugEvent( GLUtesselator *tess );
55 #define DebugEvent( tess )
59 * Invariants for the Edge Dictionary.
60 * - each pair of adjacent edges e2=Succ(e1) satisfies EdgeLeq(e1,e2)
61 * at any valid location of the sweep event
62 * - if EdgeLeq(e2,e1) as well (at any valid sweep event), then e1 and e2
63 * share a common endpoint
64 * - for each e, e->Dst has been processed, but not e->Org
65 * - each edge e satisfies VertLeq(e->Dst,event) && VertLeq(event,e->Org)
66 * where "event" is the current sweep line event.
67 * - no edge e has zero length
69 * Invariants for the Mesh (the processed portion).
70 * - the portion of the mesh left of the sweep line is a planar graph,
71 * ie. there is *some* way to embed it in the plane
72 * - no processed edge has zero length
73 * - no two processed vertices have identical coordinates
74 * - each "inside" region is monotone, ie. can be broken into two chains
75 * of monotonically increasing vertices according to VertLeq(v1,v2)
76 * - a non-invariant: these chains may intersect (very slightly)
78 * Invariants for the Sweep.
79 * - if none of the edges incident to the event vertex have an activeRegion
80 * (ie. none of these edges are in the edge dictionary), then the vertex
81 * has only right-going edges.
82 * - if an edge is marked "fixUpperEdge" (it is a temporary edge introduced
83 * by ConnectRightVertex), then it is the only right-going edge from
84 * its associated vertex. (This says that these edges exist only
85 * when it is necessary.)
90 #define MAX(x,y) ((x) >= (y) ? (x) : (y))
91 #define MIN(x,y) ((x) <= (y) ? (x) : (y))
93 /* When we merge two edges into one, we need to compute the combined
94 * winding of the new edge.
96 #define AddWinding(eDst,eSrc) (eDst->winding += eSrc->winding, \
97 eDst->Sym->winding += eSrc->Sym->winding)
99 static void SweepEvent( GLUtesselator *tess, GLUvertex *vEvent );
100 static void WalkDirtyRegions( GLUtesselator *tess, ActiveRegion *regUp );
101 static int CheckForRightSplice( GLUtesselator *tess, ActiveRegion *regUp );
103 static int EdgeLeq( GLUtesselator *tess, ActiveRegion *reg1,
106 * Both edges must be directed from right to left (this is the canonical
107 * direction for the upper edge of each region).
109 * The strategy is to evaluate a "t" value for each edge at the
110 * current sweep line position, given by tess->event. The calculations
111 * are designed to be very stable, but of course they are not perfect.
113 * Special case: if both edge destinations are at the sweep event,
114 * we sort the edges by slope (they would otherwise compare equally).
117 GLUvertex *event = tess->event;
118 GLUhalfEdge *e1, *e2;
124 if( e1->Dst == event ) {
125 if( e2->Dst == event ) {
126 /* Two edges right of the sweep line which meet at the sweep event.
127 * Sort them by slope.
129 if( VertLeq( e1->Org, e2->Org )) {
130 return EdgeSign( e2->Dst, e1->Org, e2->Org ) <= 0;
132 return EdgeSign( e1->Dst, e2->Org, e1->Org ) >= 0;
134 return EdgeSign( e2->Dst, event, e2->Org ) <= 0;
136 if( e2->Dst == event ) {
137 return EdgeSign( e1->Dst, event, e1->Org ) >= 0;
140 /* General case - compute signed distance *from* e1, e2 to event */
141 t1 = EdgeEval( e1->Dst, event, e1->Org );
142 t2 = EdgeEval( e2->Dst, event, e2->Org );
147 static void DeleteRegion( GLUtesselator *tess, ActiveRegion *reg )
149 if( reg->fixUpperEdge ) {
150 /* It was created with zero winding number, so it better be
151 * deleted with zero winding number (ie. it better not get merged
154 assert( reg->eUp->winding == 0 );
156 reg->eUp->activeRegion = NULL;
157 dictDelete( tess->dict, reg->nodeUp ); /* __gl_dictListDelete */
162 static int FixUpperEdge( ActiveRegion *reg, GLUhalfEdge *newEdge )
164 * Replace an upper edge which needs fixing (see ConnectRightVertex).
167 assert( reg->fixUpperEdge );
168 if ( !__gl_meshDelete( reg->eUp ) ) return 0;
169 reg->fixUpperEdge = FALSE;
171 newEdge->activeRegion = reg;
176 static ActiveRegion *TopLeftRegion( ActiveRegion *reg )
178 GLUvertex *org = reg->eUp->Org;
181 /* Find the region above the uppermost edge with the same origin */
183 reg = RegionAbove( reg );
184 } while( reg->eUp->Org == org );
186 /* If the edge above was a temporary edge introduced by ConnectRightVertex,
187 * now is the time to fix it.
189 if( reg->fixUpperEdge ) {
190 e = __gl_meshConnect( RegionBelow(reg)->eUp->Sym, reg->eUp->Lnext );
191 if (e == NULL) return NULL;
192 if ( !FixUpperEdge( reg, e ) ) return NULL;
193 reg = RegionAbove( reg );
198 static ActiveRegion *TopRightRegion( ActiveRegion *reg )
200 GLUvertex *dst = reg->eUp->Dst;
202 /* Find the region above the uppermost edge with the same destination */
204 reg = RegionAbove( reg );
205 } while( reg->eUp->Dst == dst );
209 static ActiveRegion *AddRegionBelow( GLUtesselator *tess,
210 ActiveRegion *regAbove,
211 GLUhalfEdge *eNewUp )
213 * Add a new active region to the sweep line, *somewhere* below "regAbove"
214 * (according to where the new edge belongs in the sweep-line dictionary).
215 * The upper edge of the new region will be "eNewUp".
216 * Winding number and "inside" flag are not updated.
219 ActiveRegion *regNew = (ActiveRegion *)memAlloc( sizeof( ActiveRegion ));
220 if (regNew == NULL) longjmp(tess->env,1);
222 regNew->eUp = eNewUp;
223 /* __gl_dictListInsertBefore */
224 regNew->nodeUp = dictInsertBefore( tess->dict, regAbove->nodeUp, regNew );
225 if (regNew->nodeUp == NULL) longjmp(tess->env,1);
226 regNew->fixUpperEdge = FALSE;
227 regNew->sentinel = FALSE;
228 regNew->dirty = FALSE;
230 eNewUp->activeRegion = regNew;
234 static GLboolean IsWindingInside( GLUtesselator *tess, int n )
236 switch( tess->windingRule ) {
237 case GLU_TESS_WINDING_ODD:
239 case GLU_TESS_WINDING_NONZERO:
241 case GLU_TESS_WINDING_POSITIVE:
243 case GLU_TESS_WINDING_NEGATIVE:
245 case GLU_TESS_WINDING_ABS_GEQ_TWO:
246 return (n >= 2) || (n <= -2);
251 return GL_FALSE; /* avoid compiler complaints */
255 static void ComputeWinding( GLUtesselator *tess, ActiveRegion *reg )
257 reg->windingNumber = RegionAbove(reg)->windingNumber + reg->eUp->winding;
258 reg->inside = IsWindingInside( tess, reg->windingNumber );
262 static void FinishRegion( GLUtesselator *tess, ActiveRegion *reg )
264 * Delete a region from the sweep line. This happens when the upper
265 * and lower chains of a region meet (at a vertex on the sweep line).
266 * The "inside" flag is copied to the appropriate mesh face (we could
267 * not do this before -- since the structure of the mesh is always
268 * changing, this face may not have even existed until now).
271 GLUhalfEdge *e = reg->eUp;
272 GLUface *f = e->Lface;
274 f->inside = reg->inside;
275 f->anEdge = e; /* optimization for __gl_meshTessellateMonoRegion() */
276 DeleteRegion( tess, reg );
280 static GLUhalfEdge *FinishLeftRegions( GLUtesselator *tess,
281 ActiveRegion *regFirst, ActiveRegion *regLast )
283 * We are given a vertex with one or more left-going edges. All affected
284 * edges should be in the edge dictionary. Starting at regFirst->eUp,
285 * we walk down deleting all regions where both edges have the same
286 * origin vOrg. At the same time we copy the "inside" flag from the
287 * active region to the face, since at this point each face will belong
288 * to at most one region (this was not necessarily true until this point
289 * in the sweep). The walk stops at the region above regLast; if regLast
290 * is NULL we walk as far as possible. At the same time we relink the
291 * mesh if necessary, so that the ordering of edges around vOrg is the
292 * same as in the dictionary.
295 ActiveRegion *reg, *regPrev;
296 GLUhalfEdge *e, *ePrev;
299 ePrev = regFirst->eUp;
300 while( regPrev != regLast ) {
301 regPrev->fixUpperEdge = FALSE; /* placement was OK */
302 reg = RegionBelow( regPrev );
304 if( e->Org != ePrev->Org ) {
305 if( ! reg->fixUpperEdge ) {
306 /* Remove the last left-going edge. Even though there are no further
307 * edges in the dictionary with this origin, there may be further
308 * such edges in the mesh (if we are adding left edges to a vertex
309 * that has already been processed). Thus it is important to call
310 * FinishRegion rather than just DeleteRegion.
312 FinishRegion( tess, regPrev );
315 /* If the edge below was a temporary edge introduced by
316 * ConnectRightVertex, now is the time to fix it.
318 e = __gl_meshConnect( ePrev->Lprev, e->Sym );
319 if (e == NULL) longjmp(tess->env,1);
320 if ( !FixUpperEdge( reg, e ) ) longjmp(tess->env,1);
323 /* Relink edges so that ePrev->Onext == e */
324 if( ePrev->Onext != e ) {
325 if ( !__gl_meshSplice( e->Oprev, e ) ) longjmp(tess->env,1);
326 if ( !__gl_meshSplice( ePrev, e ) ) longjmp(tess->env,1);
328 FinishRegion( tess, regPrev ); /* may change reg->eUp */
336 static void AddRightEdges( GLUtesselator *tess, ActiveRegion *regUp,
337 GLUhalfEdge *eFirst, GLUhalfEdge *eLast, GLUhalfEdge *eTopLeft,
340 * Purpose: insert right-going edges into the edge dictionary, and update
341 * winding numbers and mesh connectivity appropriately. All right-going
342 * edges share a common origin vOrg. Edges are inserted CCW starting at
343 * eFirst; the last edge inserted is eLast->Oprev. If vOrg has any
344 * left-going edges already processed, then eTopLeft must be the edge
345 * such that an imaginary upward vertical segment from vOrg would be
346 * contained between eTopLeft->Oprev and eTopLeft; otherwise eTopLeft
350 ActiveRegion *reg, *regPrev;
351 GLUhalfEdge *e, *ePrev;
352 int firstTime = TRUE;
354 /* Insert the new right-going edges in the dictionary */
357 assert( VertLeq( e->Org, e->Dst ));
358 AddRegionBelow( tess, regUp, e->Sym );
360 } while ( e != eLast );
362 /* Walk *all* right-going edges from e->Org, in the dictionary order,
363 * updating the winding numbers of each region, and re-linking the mesh
364 * edges to match the dictionary ordering (if necessary).
366 if( eTopLeft == NULL ) {
367 eTopLeft = RegionBelow( regUp )->eUp->Rprev;
372 reg = RegionBelow( regPrev );
374 if( e->Org != ePrev->Org ) break;
376 if( e->Onext != ePrev ) {
377 /* Unlink e from its current position, and relink below ePrev */
378 if ( !__gl_meshSplice( e->Oprev, e ) ) longjmp(tess->env,1);
379 if ( !__gl_meshSplice( ePrev->Oprev, e ) ) longjmp(tess->env,1);
381 /* Compute the winding number and "inside" flag for the new regions */
382 reg->windingNumber = regPrev->windingNumber - e->winding;
383 reg->inside = IsWindingInside( tess, reg->windingNumber );
385 /* Check for two outgoing edges with same slope -- process these
386 * before any intersection tests (see example in __gl_computeInterior).
388 regPrev->dirty = TRUE;
389 if( ! firstTime && CheckForRightSplice( tess, regPrev )) {
390 AddWinding( e, ePrev );
391 DeleteRegion( tess, regPrev );
392 if ( !__gl_meshDelete( ePrev ) ) longjmp(tess->env,1);
398 regPrev->dirty = TRUE;
399 assert( regPrev->windingNumber - e->winding == reg->windingNumber );
402 /* Check for intersections between newly adjacent edges. */
403 WalkDirtyRegions( tess, regPrev );
408 static void CallCombine( GLUtesselator *tess, GLUvertex *isect,
409 void *data[4], GLfloat weights[4], int needed )
413 /* Copy coord data in case the callback changes it. */
414 coords[0] = isect->coords[0];
415 coords[1] = isect->coords[1];
416 coords[2] = isect->coords[2];
419 CALL_COMBINE_OR_COMBINE_DATA( coords, data, weights, &isect->data );
420 if( isect->data == NULL ) {
422 isect->data = data[0];
423 } else if( ! tess->fatalError ) {
424 /* The only way fatal error is when two edges are found to intersect,
425 * but the user has not provided the callback necessary to handle
426 * generated intersection points.
428 CALL_ERROR_OR_ERROR_DATA( GLU_TESS_NEED_COMBINE_CALLBACK );
429 tess->fatalError = TRUE;
434 static void SpliceMergeVertices( GLUtesselator *tess, GLUhalfEdge *e1,
437 * Two vertices with idential coordinates are combined into one.
438 * e1->Org is kept, while e2->Org is discarded.
441 void *data[4] = { NULL, NULL, NULL, NULL };
442 GLfloat weights[4] = { 0.5, 0.5, 0.0, 0.0 };
444 data[0] = e1->Org->data;
445 data[1] = e2->Org->data;
446 CallCombine( tess, e1->Org, data, weights, FALSE );
447 if ( !__gl_meshSplice( e1, e2 ) ) longjmp(tess->env,1);
450 static void VertexWeights( GLUvertex *isect, GLUvertex *org, GLUvertex *dst,
453 * Find some weights which describe how the intersection vertex is
454 * a linear combination of "org" and "dest". Each of the two edges
455 * which generated "isect" is allocated 50% of the weight; each edge
456 * splits the weight between its org and dst according to the
457 * relative distance to "isect".
460 GLdouble t1 = VertL1dist( org, isect );
461 GLdouble t2 = VertL1dist( dst, isect );
463 weights[0] = 0.5 * t2 / (t1 + t2);
464 weights[1] = 0.5 * t1 / (t1 + t2);
465 isect->coords[0] += weights[0]*org->coords[0] + weights[1]*dst->coords[0];
466 isect->coords[1] += weights[0]*org->coords[1] + weights[1]*dst->coords[1];
467 isect->coords[2] += weights[0]*org->coords[2] + weights[1]*dst->coords[2];
471 static void GetIntersectData( GLUtesselator *tess, GLUvertex *isect,
472 GLUvertex *orgUp, GLUvertex *dstUp,
473 GLUvertex *orgLo, GLUvertex *dstLo )
475 * We've computed a new intersection point, now we need a "data" pointer
476 * from the user so that we can refer to this new vertex in the
477 * rendering callbacks.
483 data[0] = orgUp->data;
484 data[1] = dstUp->data;
485 data[2] = orgLo->data;
486 data[3] = dstLo->data;
488 isect->coords[0] = isect->coords[1] = isect->coords[2] = 0;
489 VertexWeights( isect, orgUp, dstUp, &weights[0] );
490 VertexWeights( isect, orgLo, dstLo, &weights[2] );
492 CallCombine( tess, isect, data, weights, TRUE );
495 static int CheckForRightSplice( GLUtesselator *tess, ActiveRegion *regUp )
497 * Check the upper and lower edge of "regUp", to make sure that the
498 * eUp->Org is above eLo, or eLo->Org is below eUp (depending on which
499 * origin is leftmost).
501 * The main purpose is to splice right-going edges with the same
502 * dest vertex and nearly identical slopes (ie. we can't distinguish
503 * the slopes numerically). However the splicing can also help us
504 * to recover from numerical errors. For example, suppose at one
505 * point we checked eUp and eLo, and decided that eUp->Org is barely
506 * above eLo. Then later, we split eLo into two edges (eg. from
507 * a splice operation like this one). This can change the result of
508 * our test so that now eUp->Org is incident to eLo, or barely below it.
509 * We must correct this condition to maintain the dictionary invariants.
511 * One possibility is to check these edges for intersection again
512 * (ie. CheckForIntersect). This is what we do if possible. However
513 * CheckForIntersect requires that tess->event lies between eUp and eLo,
514 * so that it has something to fall back on when the intersection
515 * calculation gives us an unusable answer. So, for those cases where
516 * we can't check for intersection, this routine fixes the problem
517 * by just splicing the offending vertex into the other edge.
518 * This is a guaranteed solution, no matter how degenerate things get.
519 * Basically this is a combinatorial solution to a numerical problem.
522 ActiveRegion *regLo = RegionBelow(regUp);
523 GLUhalfEdge *eUp = regUp->eUp;
524 GLUhalfEdge *eLo = regLo->eUp;
526 if( VertLeq( eUp->Org, eLo->Org )) {
527 if( EdgeSign( eLo->Dst, eUp->Org, eLo->Org ) > 0 ) return FALSE;
529 /* eUp->Org appears to be below eLo */
530 if( ! VertEq( eUp->Org, eLo->Org )) {
531 /* Splice eUp->Org into eLo */
532 if ( __gl_meshSplitEdge( eLo->Sym ) == NULL) longjmp(tess->env,1);
533 if ( !__gl_meshSplice( eUp, eLo->Oprev ) ) longjmp(tess->env,1);
534 regUp->dirty = regLo->dirty = TRUE;
536 } else if( eUp->Org != eLo->Org ) {
537 /* merge the two vertices, discarding eUp->Org */
538 pqDelete( tess->pq, eUp->Org->pqHandle ); /* __gl_pqSortDelete */
539 SpliceMergeVertices( tess, eLo->Oprev, eUp );
542 if( EdgeSign( eUp->Dst, eLo->Org, eUp->Org ) < 0 ) return FALSE;
544 /* eLo->Org appears to be above eUp, so splice eLo->Org into eUp */
545 RegionAbove(regUp)->dirty = regUp->dirty = TRUE;
546 if (__gl_meshSplitEdge( eUp->Sym ) == NULL) longjmp(tess->env,1);
547 if ( !__gl_meshSplice( eLo->Oprev, eUp ) ) longjmp(tess->env,1);
552 static int CheckForLeftSplice( GLUtesselator *tess, ActiveRegion *regUp )
554 * Check the upper and lower edge of "regUp", to make sure that the
555 * eUp->Dst is above eLo, or eLo->Dst is below eUp (depending on which
556 * destination is rightmost).
558 * Theoretically, this should always be true. However, splitting an edge
559 * into two pieces can change the results of previous tests. For example,
560 * suppose at one point we checked eUp and eLo, and decided that eUp->Dst
561 * is barely above eLo. Then later, we split eLo into two edges (eg. from
562 * a splice operation like this one). This can change the result of
563 * the test so that now eUp->Dst is incident to eLo, or barely below it.
564 * We must correct this condition to maintain the dictionary invariants
565 * (otherwise new edges might get inserted in the wrong place in the
566 * dictionary, and bad stuff will happen).
568 * We fix the problem by just splicing the offending vertex into the
572 ActiveRegion *regLo = RegionBelow(regUp);
573 GLUhalfEdge *eUp = regUp->eUp;
574 GLUhalfEdge *eLo = regLo->eUp;
577 assert( ! VertEq( eUp->Dst, eLo->Dst ));
579 if( VertLeq( eUp->Dst, eLo->Dst )) {
580 if( EdgeSign( eUp->Dst, eLo->Dst, eUp->Org ) < 0 ) return FALSE;
582 /* eLo->Dst is above eUp, so splice eLo->Dst into eUp */
583 RegionAbove(regUp)->dirty = regUp->dirty = TRUE;
584 e = __gl_meshSplitEdge( eUp );
585 if (e == NULL) longjmp(tess->env,1);
586 if ( !__gl_meshSplice( eLo->Sym, e ) ) longjmp(tess->env,1);
587 e->Lface->inside = regUp->inside;
589 if( EdgeSign( eLo->Dst, eUp->Dst, eLo->Org ) > 0 ) return FALSE;
591 /* eUp->Dst is below eLo, so splice eUp->Dst into eLo */
592 regUp->dirty = regLo->dirty = TRUE;
593 e = __gl_meshSplitEdge( eLo );
594 if (e == NULL) longjmp(tess->env,1);
595 if ( !__gl_meshSplice( eUp->Lnext, eLo->Sym ) ) longjmp(tess->env,1);
596 e->Rface->inside = regUp->inside;
602 static int CheckForIntersect( GLUtesselator *tess, ActiveRegion *regUp )
604 * Check the upper and lower edges of the given region to see if
605 * they intersect. If so, create the intersection and add it
606 * to the data structures.
608 * Returns TRUE if adding the new intersection resulted in a recursive
609 * call to AddRightEdges(); in this case all "dirty" regions have been
610 * checked for intersections, and possibly regUp has been deleted.
613 ActiveRegion *regLo = RegionBelow(regUp);
614 GLUhalfEdge *eUp = regUp->eUp;
615 GLUhalfEdge *eLo = regLo->eUp;
616 GLUvertex *orgUp = eUp->Org;
617 GLUvertex *orgLo = eLo->Org;
618 GLUvertex *dstUp = eUp->Dst;
619 GLUvertex *dstLo = eLo->Dst;
620 GLdouble tMinUp, tMaxLo;
621 GLUvertex isect, *orgMin;
624 assert( ! VertEq( dstLo, dstUp ));
625 assert( EdgeSign( dstUp, tess->event, orgUp ) <= 0 );
626 assert( EdgeSign( dstLo, tess->event, orgLo ) >= 0 );
627 assert( orgUp != tess->event && orgLo != tess->event );
628 assert( ! regUp->fixUpperEdge && ! regLo->fixUpperEdge );
630 if( orgUp == orgLo ) return FALSE; /* right endpoints are the same */
632 tMinUp = MIN( orgUp->t, dstUp->t );
633 tMaxLo = MAX( orgLo->t, dstLo->t );
634 if( tMinUp > tMaxLo ) return FALSE; /* t ranges do not overlap */
636 if( VertLeq( orgUp, orgLo )) {
637 if( EdgeSign( dstLo, orgUp, orgLo ) > 0 ) return FALSE;
639 if( EdgeSign( dstUp, orgLo, orgUp ) < 0 ) return FALSE;
642 /* At this point the edges intersect, at least marginally */
645 __gl_edgeIntersect( dstUp, orgUp, dstLo, orgLo, &isect );
646 /* The following properties are guaranteed: */
647 assert( MIN( orgUp->t, dstUp->t ) <= isect.t );
648 assert( isect.t <= MAX( orgLo->t, dstLo->t ));
649 assert( MIN( dstLo->s, dstUp->s ) <= isect.s );
650 assert( isect.s <= MAX( orgLo->s, orgUp->s ));
652 if( VertLeq( &isect, tess->event )) {
653 /* The intersection point lies slightly to the left of the sweep line,
654 * so move it until it''s slightly to the right of the sweep line.
655 * (If we had perfect numerical precision, this would never happen
656 * in the first place). The easiest and safest thing to do is
657 * replace the intersection by tess->event.
659 isect.s = tess->event->s;
660 isect.t = tess->event->t;
662 /* Similarly, if the computed intersection lies to the right of the
663 * rightmost origin (which should rarely happen), it can cause
664 * unbelievable inefficiency on sufficiently degenerate inputs.
665 * (If you have the test program, try running test54.d with the
666 * "X zoom" option turned on).
668 orgMin = VertLeq( orgUp, orgLo ) ? orgUp : orgLo;
669 if( VertLeq( orgMin, &isect )) {
674 if( VertEq( &isect, orgUp ) || VertEq( &isect, orgLo )) {
675 /* Easy case -- intersection at one of the right endpoints */
676 (void) CheckForRightSplice( tess, regUp );
680 if( (! VertEq( dstUp, tess->event )
681 && EdgeSign( dstUp, tess->event, &isect ) >= 0)
682 || (! VertEq( dstLo, tess->event )
683 && EdgeSign( dstLo, tess->event, &isect ) <= 0 ))
685 /* Very unusual -- the new upper or lower edge would pass on the
686 * wrong side of the sweep event, or through it. This can happen
687 * due to very small numerical errors in the intersection calculation.
689 if( dstLo == tess->event ) {
690 /* Splice dstLo into eUp, and process the new region(s) */
691 if (__gl_meshSplitEdge( eUp->Sym ) == NULL) longjmp(tess->env,1);
692 if ( !__gl_meshSplice( eLo->Sym, eUp ) ) longjmp(tess->env,1);
693 regUp = TopLeftRegion( regUp );
694 if (regUp == NULL) longjmp(tess->env,1);
695 eUp = RegionBelow(regUp)->eUp;
696 FinishLeftRegions( tess, RegionBelow(regUp), regLo );
697 AddRightEdges( tess, regUp, eUp->Oprev, eUp, eUp, TRUE );
700 if( dstUp == tess->event ) {
701 /* Splice dstUp into eLo, and process the new region(s) */
702 if (__gl_meshSplitEdge( eLo->Sym ) == NULL) longjmp(tess->env,1);
703 if ( !__gl_meshSplice( eUp->Lnext, eLo->Oprev ) ) longjmp(tess->env,1);
705 regUp = TopRightRegion( regUp );
706 e = RegionBelow(regUp)->eUp->Rprev;
707 regLo->eUp = eLo->Oprev;
708 eLo = FinishLeftRegions( tess, regLo, NULL );
709 AddRightEdges( tess, regUp, eLo->Onext, eUp->Rprev, e, TRUE );
712 /* Special case: called from ConnectRightVertex. If either
713 * edge passes on the wrong side of tess->event, split it
714 * (and wait for ConnectRightVertex to splice it appropriately).
716 if( EdgeSign( dstUp, tess->event, &isect ) >= 0 ) {
717 RegionAbove(regUp)->dirty = regUp->dirty = TRUE;
718 if (__gl_meshSplitEdge( eUp->Sym ) == NULL) longjmp(tess->env,1);
719 eUp->Org->s = tess->event->s;
720 eUp->Org->t = tess->event->t;
722 if( EdgeSign( dstLo, tess->event, &isect ) <= 0 ) {
723 regUp->dirty = regLo->dirty = TRUE;
724 if (__gl_meshSplitEdge( eLo->Sym ) == NULL) longjmp(tess->env,1);
725 eLo->Org->s = tess->event->s;
726 eLo->Org->t = tess->event->t;
728 /* leave the rest for ConnectRightVertex */
732 /* General case -- split both edges, splice into new vertex.
733 * When we do the splice operation, the order of the arguments is
734 * arbitrary as far as correctness goes. However, when the operation
735 * creates a new face, the work done is proportional to the size of
736 * the new face. We expect the faces in the processed part of
737 * the mesh (ie. eUp->Lface) to be smaller than the faces in the
738 * unprocessed original contours (which will be eLo->Oprev->Lface).
740 if (__gl_meshSplitEdge( eUp->Sym ) == NULL) longjmp(tess->env,1);
741 if (__gl_meshSplitEdge( eLo->Sym ) == NULL) longjmp(tess->env,1);
742 if ( !__gl_meshSplice( eLo->Oprev, eUp ) ) longjmp(tess->env,1);
743 eUp->Org->s = isect.s;
744 eUp->Org->t = isect.t;
745 eUp->Org->pqHandle = pqInsert( tess->pq, eUp->Org ); /* __gl_pqSortInsert */
746 if (eUp->Org->pqHandle == LONG_MAX) {
747 pqDeletePriorityQ(tess->pq); /* __gl_pqSortDeletePriorityQ */
749 longjmp(tess->env,1);
751 GetIntersectData( tess, eUp->Org, orgUp, dstUp, orgLo, dstLo );
752 RegionAbove(regUp)->dirty = regUp->dirty = regLo->dirty = TRUE;
756 static void WalkDirtyRegions( GLUtesselator *tess, ActiveRegion *regUp )
758 * When the upper or lower edge of any region changes, the region is
759 * marked "dirty". This routine walks through all the dirty regions
760 * and makes sure that the dictionary invariants are satisfied
761 * (see the comments at the beginning of this file). Of course
762 * new dirty regions can be created as we make changes to restore
766 ActiveRegion *regLo = RegionBelow(regUp);
767 GLUhalfEdge *eUp, *eLo;
770 /* Find the lowest dirty region (we walk from the bottom up). */
771 while( regLo->dirty ) {
773 regLo = RegionBelow(regLo);
775 if( ! regUp->dirty ) {
777 regUp = RegionAbove( regUp );
778 if( regUp == NULL || ! regUp->dirty ) {
779 /* We've walked all the dirty regions */
783 regUp->dirty = FALSE;
787 if( eUp->Dst != eLo->Dst ) {
788 /* Check that the edge ordering is obeyed at the Dst vertices. */
789 if( CheckForLeftSplice( tess, regUp )) {
791 /* If the upper or lower edge was marked fixUpperEdge, then
792 * we no longer need it (since these edges are needed only for
793 * vertices which otherwise have no right-going edges).
795 if( regLo->fixUpperEdge ) {
796 DeleteRegion( tess, regLo );
797 if ( !__gl_meshDelete( eLo ) ) longjmp(tess->env,1);
798 regLo = RegionBelow( regUp );
800 } else if( regUp->fixUpperEdge ) {
801 DeleteRegion( tess, regUp );
802 if ( !__gl_meshDelete( eUp ) ) longjmp(tess->env,1);
803 regUp = RegionAbove( regLo );
808 if( eUp->Org != eLo->Org ) {
809 if( eUp->Dst != eLo->Dst
810 && ! regUp->fixUpperEdge && ! regLo->fixUpperEdge
811 && (eUp->Dst == tess->event || eLo->Dst == tess->event) )
813 /* When all else fails in CheckForIntersect(), it uses tess->event
814 * as the intersection location. To make this possible, it requires
815 * that tess->event lie between the upper and lower edges, and also
816 * that neither of these is marked fixUpperEdge (since in the worst
817 * case it might splice one of these edges into tess->event, and
818 * violate the invariant that fixable edges are the only right-going
819 * edge from their associated vertex).
821 if( CheckForIntersect( tess, regUp )) {
822 /* WalkDirtyRegions() was called recursively; we're done */
826 /* Even though we can't use CheckForIntersect(), the Org vertices
827 * may violate the dictionary edge ordering. Check and correct this.
829 (void) CheckForRightSplice( tess, regUp );
832 if( eUp->Org == eLo->Org && eUp->Dst == eLo->Dst ) {
833 /* A degenerate loop consisting of only two edges -- delete it. */
834 AddWinding( eLo, eUp );
835 DeleteRegion( tess, regUp );
836 if ( !__gl_meshDelete( eUp ) ) longjmp(tess->env,1);
837 regUp = RegionAbove( regLo );
843 static void ConnectRightVertex( GLUtesselator *tess, ActiveRegion *regUp,
844 GLUhalfEdge *eBottomLeft )
846 * Purpose: connect a "right" vertex vEvent (one where all edges go left)
847 * to the unprocessed portion of the mesh. Since there are no right-going
848 * edges, two regions (one above vEvent and one below) are being merged
849 * into one. "regUp" is the upper of these two regions.
851 * There are two reasons for doing this (adding a right-going edge):
852 * - if the two regions being merged are "inside", we must add an edge
853 * to keep them separated (the combined region would not be monotone).
854 * - in any case, we must leave some record of vEvent in the dictionary,
855 * so that we can merge vEvent with features that we have not seen yet.
856 * For example, maybe there is a vertical edge which passes just to
857 * the right of vEvent; we would like to splice vEvent into this edge.
859 * However, we don't want to connect vEvent to just any vertex. We don''t
860 * want the new edge to cross any other edges; otherwise we will create
861 * intersection vertices even when the input data had no self-intersections.
862 * (This is a bad thing; if the user's input data has no intersections,
863 * we don't want to generate any false intersections ourselves.)
865 * Our eventual goal is to connect vEvent to the leftmost unprocessed
866 * vertex of the combined region (the union of regUp and regLo).
867 * But because of unseen vertices with all right-going edges, and also
868 * new vertices which may be created by edge intersections, we don''t
869 * know where that leftmost unprocessed vertex is. In the meantime, we
870 * connect vEvent to the closest vertex of either chain, and mark the region
871 * as "fixUpperEdge". This flag says to delete and reconnect this edge
872 * to the next processed vertex on the boundary of the combined region.
873 * Quite possibly the vertex we connected to will turn out to be the
874 * closest one, in which case we won''t need to make any changes.
878 GLUhalfEdge *eTopLeft = eBottomLeft->Onext;
879 ActiveRegion *regLo = RegionBelow(regUp);
880 GLUhalfEdge *eUp = regUp->eUp;
881 GLUhalfEdge *eLo = regLo->eUp;
882 int degenerate = FALSE;
884 if( eUp->Dst != eLo->Dst ) {
885 (void) CheckForIntersect( tess, regUp );
888 /* Possible new degeneracies: upper or lower edge of regUp may pass
889 * through vEvent, or may coincide with new intersection vertex
891 if( VertEq( eUp->Org, tess->event )) {
892 if ( !__gl_meshSplice( eTopLeft->Oprev, eUp ) ) longjmp(tess->env,1);
893 regUp = TopLeftRegion( regUp );
894 if (regUp == NULL) longjmp(tess->env,1);
895 eTopLeft = RegionBelow( regUp )->eUp;
896 FinishLeftRegions( tess, RegionBelow(regUp), regLo );
899 if( VertEq( eLo->Org, tess->event )) {
900 if ( !__gl_meshSplice( eBottomLeft, eLo->Oprev ) ) longjmp(tess->env,1);
901 eBottomLeft = FinishLeftRegions( tess, regLo, NULL );
905 AddRightEdges( tess, regUp, eBottomLeft->Onext, eTopLeft, eTopLeft, TRUE );
909 /* Non-degenerate situation -- need to add a temporary, fixable edge.
910 * Connect to the closer of eLo->Org, eUp->Org.
912 if( VertLeq( eLo->Org, eUp->Org )) {
917 eNew = __gl_meshConnect( eBottomLeft->Lprev, eNew );
918 if (eNew == NULL) longjmp(tess->env,1);
920 /* Prevent cleanup, otherwise eNew might disappear before we've even
921 * had a chance to mark it as a temporary edge.
923 AddRightEdges( tess, regUp, eNew, eNew->Onext, eNew->Onext, FALSE );
924 eNew->Sym->activeRegion->fixUpperEdge = TRUE;
925 WalkDirtyRegions( tess, regUp );
928 /* Because vertices at exactly the same location are merged together
929 * before we process the sweep event, some degenerate cases can't occur.
930 * However if someone eventually makes the modifications required to
931 * merge features which are close together, the cases below marked
932 * TOLERANCE_NONZERO will be useful. They were debugged before the
933 * code to merge identical vertices in the main loop was added.
935 #define TOLERANCE_NONZERO FALSE
937 static void ConnectLeftDegenerate( GLUtesselator *tess,
938 ActiveRegion *regUp, GLUvertex *vEvent )
940 * The event vertex lies exacty on an already-processed edge or vertex.
941 * Adding the new vertex involves splicing it into the already-processed
945 GLUhalfEdge *e, *eTopLeft, *eTopRight, *eLast;
949 if( VertEq( e->Org, vEvent )) {
950 /* e->Org is an unprocessed vertex - just combine them, and wait
951 * for e->Org to be pulled from the queue
953 assert( TOLERANCE_NONZERO );
954 SpliceMergeVertices( tess, e, vEvent->anEdge );
958 if( ! VertEq( e->Dst, vEvent )) {
959 /* General case -- splice vEvent into edge e which passes through it */
960 if (__gl_meshSplitEdge( e->Sym ) == NULL) longjmp(tess->env,1);
961 if( regUp->fixUpperEdge ) {
962 /* This edge was fixable -- delete unused portion of original edge */
963 if ( !__gl_meshDelete( e->Onext ) ) longjmp(tess->env,1);
964 regUp->fixUpperEdge = FALSE;
966 if ( !__gl_meshSplice( vEvent->anEdge, e ) ) longjmp(tess->env,1);
967 SweepEvent( tess, vEvent ); /* recurse */
971 /* vEvent coincides with e->Dst, which has already been processed.
972 * Splice in the additional right-going edges.
974 assert( TOLERANCE_NONZERO );
975 regUp = TopRightRegion( regUp );
976 reg = RegionBelow( regUp );
977 eTopRight = reg->eUp->Sym;
978 eTopLeft = eLast = eTopRight->Onext;
979 if( reg->fixUpperEdge ) {
980 /* Here e->Dst has only a single fixable edge going right.
981 * We can delete it since now we have some real right-going edges.
983 assert( eTopLeft != eTopRight ); /* there are some left edges too */
984 DeleteRegion( tess, reg );
985 if ( !__gl_meshDelete( eTopRight ) ) longjmp(tess->env,1);
986 eTopRight = eTopLeft->Oprev;
988 if ( !__gl_meshSplice( vEvent->anEdge, eTopRight ) ) longjmp(tess->env,1);
989 if( ! EdgeGoesLeft( eTopLeft )) {
990 /* e->Dst had no left-going edges -- indicate this to AddRightEdges() */
993 AddRightEdges( tess, regUp, eTopRight->Onext, eLast, eTopLeft, TRUE );
997 static void ConnectLeftVertex( GLUtesselator *tess, GLUvertex *vEvent )
999 * Purpose: connect a "left" vertex (one where both edges go right)
1000 * to the processed portion of the mesh. Let R be the active region
1001 * containing vEvent, and let U and L be the upper and lower edge
1002 * chains of R. There are two possibilities:
1004 * - the normal case: split R into two regions, by connecting vEvent to
1005 * the rightmost vertex of U or L lying to the left of the sweep line
1007 * - the degenerate case: if vEvent is close enough to U or L, we
1008 * merge vEvent into that edge chain. The subcases are:
1009 * - merging with the rightmost vertex of U or L
1010 * - merging with the active edge of U or L
1011 * - merging with an already-processed portion of U or L
1014 ActiveRegion *regUp, *regLo, *reg;
1015 GLUhalfEdge *eUp, *eLo, *eNew;
1018 /* assert( vEvent->anEdge->Onext->Onext == vEvent->anEdge ); */
1020 /* Get a pointer to the active region containing vEvent */
1021 tmp.eUp = vEvent->anEdge->Sym;
1022 /* __GL_DICTLISTKEY */ /* __gl_dictListSearch */
1023 regUp = (ActiveRegion *)dictKey( dictSearch( tess->dict, &tmp ));
1024 regLo = RegionBelow( regUp );
1028 /* Try merging with U or L first */
1029 if( EdgeSign( eUp->Dst, vEvent, eUp->Org ) == 0 ) {
1030 ConnectLeftDegenerate( tess, regUp, vEvent );
1034 /* Connect vEvent to rightmost processed vertex of either chain.
1035 * e->Dst is the vertex that we will connect to vEvent.
1037 reg = VertLeq( eLo->Dst, eUp->Dst ) ? regUp : regLo;
1039 if( regUp->inside || reg->fixUpperEdge) {
1040 if( reg == regUp ) {
1041 eNew = __gl_meshConnect( vEvent->anEdge->Sym, eUp->Lnext );
1042 if (eNew == NULL) longjmp(tess->env,1);
1044 GLUhalfEdge *tempHalfEdge= __gl_meshConnect( eLo->Dnext, vEvent->anEdge);
1045 if (tempHalfEdge == NULL) longjmp(tess->env,1);
1047 eNew = tempHalfEdge->Sym;
1049 if( reg->fixUpperEdge ) {
1050 if ( !FixUpperEdge( reg, eNew ) ) longjmp(tess->env,1);
1052 ComputeWinding( tess, AddRegionBelow( tess, regUp, eNew ));
1054 SweepEvent( tess, vEvent );
1056 /* The new vertex is in a region which does not belong to the polygon.
1057 * We don''t need to connect this vertex to the rest of the mesh.
1059 AddRightEdges( tess, regUp, vEvent->anEdge, vEvent->anEdge, NULL, TRUE );
1064 static void SweepEvent( GLUtesselator *tess, GLUvertex *vEvent )
1066 * Does everything necessary when the sweep line crosses a vertex.
1067 * Updates the mesh and the edge dictionary.
1070 ActiveRegion *regUp, *reg;
1071 GLUhalfEdge *e, *eTopLeft, *eBottomLeft;
1073 tess->event = vEvent; /* for access in EdgeLeq() */
1076 /* Check if this vertex is the right endpoint of an edge that is
1077 * already in the dictionary. In this case we don't need to waste
1078 * time searching for the location to insert new edges.
1081 while( e->activeRegion == NULL ) {
1083 if( e == vEvent->anEdge ) {
1084 /* All edges go right -- not incident to any processed edges */
1085 ConnectLeftVertex( tess, vEvent );
1090 /* Processing consists of two phases: first we "finish" all the
1091 * active regions where both the upper and lower edges terminate
1092 * at vEvent (ie. vEvent is closing off these regions).
1093 * We mark these faces "inside" or "outside" the polygon according
1094 * to their winding number, and delete the edges from the dictionary.
1095 * This takes care of all the left-going edges from vEvent.
1097 regUp = TopLeftRegion( e->activeRegion );
1098 if (regUp == NULL) longjmp(tess->env,1);
1099 reg = RegionBelow( regUp );
1100 eTopLeft = reg->eUp;
1101 eBottomLeft = FinishLeftRegions( tess, reg, NULL );
1103 /* Next we process all the right-going edges from vEvent. This
1104 * involves adding the edges to the dictionary, and creating the
1105 * associated "active regions" which record information about the
1106 * regions between adjacent dictionary edges.
1108 if( eBottomLeft->Onext == eTopLeft ) {
1109 /* No right-going edges -- add a temporary "fixable" edge */
1110 ConnectRightVertex( tess, regUp, eBottomLeft );
1112 AddRightEdges( tess, regUp, eBottomLeft->Onext, eTopLeft, eTopLeft, TRUE );
1117 /* Make the sentinel coordinates big enough that they will never be
1118 * merged with real input features. (Even with the largest possible
1119 * input contour and the maximum tolerance of 1.0, no merging will be
1120 * done with coordinates larger than 3 * GLU_TESS_MAX_COORD).
1122 #define SENTINEL_COORD (4 * GLU_TESS_MAX_COORD)
1124 static void AddSentinel( GLUtesselator *tess, GLdouble t )
1126 * We add two sentinel edges above and below all other edges,
1127 * to avoid special cases at the top and bottom.
1131 ActiveRegion *reg = (ActiveRegion *)memAlloc( sizeof( ActiveRegion ));
1132 if (reg == NULL) longjmp(tess->env,1);
1134 e = __gl_meshMakeEdge( tess->mesh );
1135 if (e == NULL) longjmp(tess->env,1);
1137 e->Org->s = SENTINEL_COORD;
1139 e->Dst->s = -SENTINEL_COORD;
1141 tess->event = e->Dst; /* initialize it */
1144 reg->windingNumber = 0;
1145 reg->inside = FALSE;
1146 reg->fixUpperEdge = FALSE;
1147 reg->sentinel = TRUE;
1149 reg->nodeUp = dictInsert( tess->dict, reg ); /* __gl_dictListInsertBefore */
1150 if (reg->nodeUp == NULL) longjmp(tess->env,1);
1154 static void InitEdgeDict( GLUtesselator *tess )
1156 * We maintain an ordering of edge intersections with the sweep line.
1157 * This order is maintained in a dynamic dictionary.
1160 /* __gl_dictListNewDict */
1161 tess->dict = dictNewDict( tess, (int (*)(void *, DictKey, DictKey)) EdgeLeq );
1162 if (tess->dict == NULL) longjmp(tess->env,1);
1164 AddSentinel( tess, -SENTINEL_COORD );
1165 AddSentinel( tess, SENTINEL_COORD );
1169 static void DoneEdgeDict( GLUtesselator *tess )
1176 /* __GL_DICTLISTKEY */ /* __GL_DICTLISTMIN */
1177 while( (reg = (ActiveRegion *)dictKey( dictMin( tess->dict ))) != NULL ) {
1179 * At the end of all processing, the dictionary should contain
1180 * only the two sentinel edges, plus at most one "fixable" edge
1181 * created by ConnectRightVertex().
1183 if( ! reg->sentinel ) {
1184 assert( reg->fixUpperEdge );
1185 assert( ++fixedEdges == 1 );
1187 assert( reg->windingNumber == 0 );
1188 DeleteRegion( tess, reg );
1189 /* __gl_meshDelete( reg->eUp );*/
1191 dictDeleteDict( tess->dict ); /* __gl_dictListDeleteDict */
1195 static void RemoveDegenerateEdges( GLUtesselator *tess )
1197 * Remove zero-length edges, and contours with fewer than 3 vertices.
1200 GLUhalfEdge *e, *eNext, *eLnext;
1201 GLUhalfEdge *eHead = &tess->mesh->eHead;
1204 for( e = eHead->next; e != eHead; e = eNext ) {
1208 if( VertEq( e->Org, e->Dst ) && e->Lnext->Lnext != e ) {
1209 /* Zero-length edge, contour has at least 3 edges */
1211 SpliceMergeVertices( tess, eLnext, e ); /* deletes e->Org */
1212 if ( !__gl_meshDelete( e ) ) longjmp(tess->env,1); /* e is a self-loop */
1216 if( eLnext->Lnext == e ) {
1217 /* Degenerate contour (one or two edges) */
1220 if( eLnext == eNext || eLnext == eNext->Sym ) { eNext = eNext->next; }
1221 if ( !__gl_meshDelete( eLnext ) ) longjmp(tess->env,1);
1223 if( e == eNext || e == eNext->Sym ) { eNext = eNext->next; }
1224 if ( !__gl_meshDelete( e ) ) longjmp(tess->env,1);
1229 static int InitPriorityQ( GLUtesselator *tess )
1231 * Insert all vertices into the priority queue which determines the
1232 * order in which vertices cross the sweep line.
1236 GLUvertex *v, *vHead;
1238 /* __gl_pqSortNewPriorityQ */
1239 pq = tess->pq = pqNewPriorityQ( (int (*)(PQkey, PQkey)) __gl_vertLeq );
1240 if (pq == NULL) return 0;
1242 vHead = &tess->mesh->vHead;
1243 for( v = vHead->next; v != vHead; v = v->next ) {
1244 v->pqHandle = pqInsert( pq, v ); /* __gl_pqSortInsert */
1245 if (v->pqHandle == LONG_MAX) break;
1247 if (v != vHead || !pqInit( pq ) ) { /* __gl_pqSortInit */
1248 pqDeletePriorityQ(tess->pq); /* __gl_pqSortDeletePriorityQ */
1257 static void DonePriorityQ( GLUtesselator *tess )
1259 pqDeletePriorityQ( tess->pq ); /* __gl_pqSortDeletePriorityQ */
1263 static int RemoveDegenerateFaces( GLUmesh *mesh )
1265 * Delete any degenerate faces with only two edges. WalkDirtyRegions()
1266 * will catch almost all of these, but it won't catch degenerate faces
1267 * produced by splice operations on already-processed edges.
1268 * The two places this can happen are in FinishLeftRegions(), when
1269 * we splice in a "temporary" edge produced by ConnectRightVertex(),
1270 * and in CheckForLeftSplice(), where we splice already-processed
1271 * edges to ensure that our dictionary invariants are not violated
1272 * by numerical errors.
1274 * In both these cases it is *very* dangerous to delete the offending
1275 * edge at the time, since one of the routines further up the stack
1276 * will sometimes be keeping a pointer to that edge.
1283 for( f = mesh->fHead.next; f != &mesh->fHead; f = fNext ) {
1286 assert( e->Lnext != e );
1288 if( e->Lnext->Lnext == e ) {
1289 /* A face with only two edges */
1290 AddWinding( e->Onext, e );
1291 if ( !__gl_meshDelete( e ) ) return 0;
1297 int __gl_computeInterior( GLUtesselator *tess )
1299 * __gl_computeInterior( tess ) computes the planar arrangement specified
1300 * by the given contours, and further subdivides this arrangement
1301 * into regions. Each region is marked "inside" if it belongs
1302 * to the polygon, according to the rule given by tess->windingRule.
1303 * Each interior region is guaranteed be monotone.
1306 GLUvertex *v, *vNext;
1308 tess->fatalError = FALSE;
1310 /* Each vertex defines an event for our sweep line. Start by inserting
1311 * all the vertices in a priority queue. Events are processed in
1312 * lexicographic order, ie.
1314 * e1 < e2 iff e1.x < e2.x || (e1.x == e2.x && e1.y < e2.y)
1316 RemoveDegenerateEdges( tess );
1317 if ( !InitPriorityQ( tess ) ) return 0; /* if error */
1318 InitEdgeDict( tess );
1320 /* __gl_pqSortExtractMin */
1321 while( (v = (GLUvertex *)pqExtractMin( tess->pq )) != NULL ) {
1323 vNext = (GLUvertex *)pqMinimum( tess->pq ); /* __gl_pqSortMinimum */
1324 if( vNext == NULL || ! VertEq( vNext, v )) break;
1326 /* Merge together all vertices at exactly the same location.
1327 * This is more efficient than processing them one at a time,
1328 * simplifies the code (see ConnectLeftDegenerate), and is also
1329 * important for correct handling of certain degenerate cases.
1330 * For example, suppose there are two identical edges A and B
1331 * that belong to different contours (so without this code they would
1332 * be processed by separate sweep events). Suppose another edge C
1333 * crosses A and B from above. When A is processed, we split it
1334 * at its intersection point with C. However this also splits C,
1335 * so when we insert B we may compute a slightly different
1336 * intersection point. This might leave two edges with a small
1337 * gap between them. This kind of error is especially obvious
1338 * when using boundary extraction (GLU_TESS_BOUNDARY_ONLY).
1340 vNext = (GLUvertex *)pqExtractMin( tess->pq ); /* __gl_pqSortExtractMin*/
1341 SpliceMergeVertices( tess, v->anEdge, vNext->anEdge );
1343 SweepEvent( tess, v );
1346 /* Set tess->event for debugging purposes */
1347 /* __GL_DICTLISTKEY */ /* __GL_DICTLISTMIN */
1348 tess->event = ((ActiveRegion *) dictKey( dictMin( tess->dict )))->eUp->Org;
1350 DoneEdgeDict( tess );
1351 DonePriorityQ( tess );
1353 if ( !RemoveDegenerateFaces( tess->mesh ) ) return 0;
1354 __gl_meshCheckMesh( tess->mesh );