sweep.c 47 KB

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