Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1293 lines
45 KiB

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