sweep.c 47 KB

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