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