static int CheckForRightSplice( GLUtesselator *tess, ActiveRegion *regUp ) /* * Check the upper and lower edge of "regUp", to make sure that the * eUp->Org is above eLo, or eLo->Org is below eUp (depending on which * origin is leftmost). * * The main purpose is to splice right-going edges with the same * dest vertex and nearly identical slopes (ie. we can't distinguish * the slopes numerically). However the splicing can also help us * to recover from numerical errors. For example, suppose at one * point we checked eUp and eLo, and decided that eUp->Org is barely * above eLo. Then later, we split eLo into two edges (eg. from * a splice operation like this one). This can change the result of * our test so that now eUp->Org is incident to eLo, or barely below it. * We must correct this condition to maintain the dictionary invariants. * * One possibility is to check these edges for intersection again * (ie. CheckForIntersect). This is what we do if possible. However * CheckForIntersect requires that tess->event lies between eUp and eLo, * so that it has something to fall back on when the intersection * calculation gives us an unusable answer. So, for those cases where * we can't check for intersection, this routine fixes the problem * by just splicing the offending vertex into the other edge. * This is a guaranteed solution, no matter how degenerate things get. * Basically this is a combinatorial solution to a numerical problem. */ { ActiveRegion *regLo = RegionBelow(regUp); GLUhalfEdge *eUp = regUp->eUp; GLUhalfEdge *eLo = regLo->eUp; if( VertLeq( eUp->Org, eLo->Org )) { if( EdgeSign( eLo->Dst, eUp->Org, eLo->Org ) > 0 ) return FALSE; /* eUp->Org appears to be below eLo */ if( ! VertEq( eUp->Org, eLo->Org )) { /* Splice eUp->Org into eLo */ if ( __gl_meshSplitEdge( eLo->Sym ) == NULL) longjmp(tess->env,1); if ( !__gl_meshSplice( eUp, eLo->Oprev ) ) longjmp(tess->env,1); regUp->dirty = regLo->dirty = TRUE; } else if( eUp->Org != eLo->Org ) { /* merge the two vertices, discarding eUp->Org */ pqDelete( tess->pq, eUp->Org->pqHandle ); /* __gl_pqSortDelete */ SpliceMergeVertices( tess, eLo->Oprev, eUp ); } } else { if( EdgeSign( eUp->Dst, eLo->Org, eUp->Org ) < 0 ) return FALSE; /* eLo->Org appears to be above eUp, so splice eLo->Org into eUp */ regUp->dirty = TRUE; void* valid_ptr_check = RegionAbove(regUp);//->dirty if ( valid_ptr_check ) { RegionAbove(regUp)->dirty = TRUE; } if (__gl_meshSplitEdge( eUp->Sym ) == NULL) longjmp(tess->env,1); if ( !__gl_meshSplice( eLo->Oprev, eUp ) ) longjmp(tess->env,1); } return TRUE; }
static int CheckForLeftSplice( GLUtesselator *tess, ActiveRegion *regUp ) /* * Check the upper and lower edge of "regUp", to make sure that the * eUp->Dst is above eLo, or eLo->Dst is below eUp (depending on which * destination is rightmost). * * Theoretically, this should always be true. However, splitting an edge * into two pieces can change the results of previous tests. For example, * suppose at one point we checked eUp and eLo, and decided that eUp->Dst * is barely above eLo. Then later, we split eLo into two edges (eg. from * a splice operation like this one). This can change the result of * the test so that now eUp->Dst is incident to eLo, or barely below it. * We must correct this condition to maintain the dictionary invariants * (otherwise new edges might get inserted in the wrong place in the * dictionary, and bad stuff will happen). * * We fix the problem by just splicing the offending vertex into the * other edge. */ { ActiveRegion *regLo = RegionBelow(regUp); GLUhalfEdge *eUp = regUp->eUp; GLUhalfEdge *eLo = regLo->eUp; GLUhalfEdge *e; assert( ! VertEq( eUp->Dst, eLo->Dst )); if( VertLeq( eUp->Dst, eLo->Dst )) { if( EdgeSign( eUp->Dst, eLo->Dst, eUp->Org ) < 0 ) return FALSE; /* eLo->Dst is above eUp, so splice eLo->Dst into eUp */ if ( RegionAbove(regUp) ) { RegionAbove(regUp)->dirty = TRUE; } regUp->dirty = TRUE; e = __gl_meshSplitEdge( eUp ); if (e == NULL) longjmp(tess->env,1); if ( !__gl_meshSplice( eLo->Sym, e ) ) longjmp(tess->env,1); e->Lface->inside = regUp->inside; } else { if( EdgeSign( eLo->Dst, eUp->Dst, eLo->Org ) > 0 ) return FALSE; /* eUp->Dst is below eLo, so splice eUp->Dst into eLo */ regUp->dirty = regLo->dirty = TRUE; e = __gl_meshSplitEdge( eLo ); if (e == NULL) longjmp(tess->env,1); if ( !__gl_meshSplice( eUp->Lnext, eLo->Sym ) ) longjmp(tess->env,1); e->Rface->inside = regUp->inside; } return TRUE; }
static ActiveRegion *TopRightRegion( ActiveRegion *reg ) { TESSvertex *dst = reg->eUp->Dst; /* Find the region above the uppermost edge with the same destination */ do { reg = RegionAbove( reg ); } while( reg->eUp->Dst == dst ); return reg; }
static ActiveRegion *TopLeftRegion( TESStesselator *tess, ActiveRegion *reg ) { TESSvertex *org = reg->eUp->Org; TESShalfEdge *e; /* Find the region above the uppermost edge with the same origin */ do { reg = RegionAbove( reg ); } while( reg->eUp->Org == org ); /* If the edge above was a temporary edge introduced by ConnectRightVertex, * now is the time to fix it. */ if( reg->fixUpperEdge ) { e = tessMeshConnect( tess->mesh, RegionBelow(reg)->eUp->Sym, reg->eUp->Lnext ); if (e == NULL) return NULL; if ( !FixUpperEdge( tess, reg, e ) ) return NULL; reg = RegionAbove( reg ); } return reg; }
static void WalkDirtyRegions( TESStesselator *tess, ActiveRegion *regUp ) /* * When the upper or lower edge of any region changes, the region is * marked "dirty". This routine walks through all the dirty regions * and makes sure that the dictionary invariants are satisfied * (see the comments at the beginning of this file). Of course * new dirty regions can be created as we make changes to restore * the invariants. */ { ActiveRegion *regLo = RegionBelow(regUp); TESShalfEdge *eUp, *eLo; for( ;; ) { /* Find the lowest dirty region (we walk from the bottom up). */ while( regLo->dirty ) { regUp = regLo; regLo = RegionBelow(regLo); } if( ! regUp->dirty ) { regLo = regUp; regUp = RegionAbove( regUp ); if( regUp == NULL || ! regUp->dirty ) { /* We've walked all the dirty regions */ return; } } regUp->dirty = FALSE; eUp = regUp->eUp; eLo = regLo->eUp; if( eUp->Dst != eLo->Dst ) { /* Check that the edge ordering is obeyed at the Dst vertices. */ if( CheckForLeftSplice( tess, regUp )) { /* If the upper or lower edge was marked fixUpperEdge, then * we no longer need it (since these edges are needed only for * vertices which otherwise have no right-going edges). */ if( regLo->fixUpperEdge ) { DeleteRegion( tess, regLo ); if ( !tessMeshDelete( tess->mesh, eLo ) ) longjmp(tess->env,1); regLo = RegionBelow( regUp ); eLo = regLo->eUp; } else if( regUp->fixUpperEdge ) { DeleteRegion( tess, regUp ); if ( !tessMeshDelete( tess->mesh, eUp ) ) longjmp(tess->env,1); regUp = RegionAbove( regLo ); eUp = regUp->eUp; } } } if( eUp->Org != eLo->Org ) { if( eUp->Dst != eLo->Dst && ! regUp->fixUpperEdge && ! regLo->fixUpperEdge && (eUp->Dst == tess->event || eLo->Dst == tess->event) ) { /* When all else fails in CheckForIntersect(), it uses tess->event * as the intersection location. To make this possible, it requires * that tess->event lie between the upper and lower edges, and also * that neither of these is marked fixUpperEdge (since in the worst * case it might splice one of these edges into tess->event, and * violate the invariant that fixable edges are the only right-going * edge from their associated vertex). */ if( CheckForIntersect( tess, regUp )) { /* WalkDirtyRegions() was called recursively; we're done */ return; } } else { /* Even though we can't use CheckForIntersect(), the Org vertices * may violate the dictionary edge ordering. Check and correct this. */ (void) CheckForRightSplice( tess, regUp ); } } if( eUp->Org == eLo->Org && eUp->Dst == eLo->Dst ) { /* A degenerate loop consisting of only two edges -- delete it. */ AddWinding( eLo, eUp ); DeleteRegion( tess, regUp ); if ( !tessMeshDelete( tess->mesh, eUp ) ) longjmp(tess->env,1); regUp = RegionAbove( regLo ); } } }
static int CheckForIntersect( TESStesselator *tess, ActiveRegion *regUp ) /* * Check the upper and lower edges of the given region to see if * they intersect. If so, create the intersection and add it * to the data structures. * * Returns TRUE if adding the new intersection resulted in a recursive * call to AddRightEdges(); in this case all "dirty" regions have been * checked for intersections, and possibly regUp has been deleted. */ { ActiveRegion *regLo = RegionBelow(regUp); TESShalfEdge *eUp = regUp->eUp; TESShalfEdge *eLo = regLo->eUp; TESSvertex *orgUp = eUp->Org; TESSvertex *orgLo = eLo->Org; TESSvertex *dstUp = eUp->Dst; TESSvertex *dstLo = eLo->Dst; TESSreal tMinUp, tMaxLo; TESSvertex isect, *orgMin; TESShalfEdge *e; assert( ! VertEq( dstLo, dstUp )); assert( EdgeSign( dstUp, tess->event, orgUp ) <= 0 ); assert( EdgeSign( dstLo, tess->event, orgLo ) >= 0 ); assert( orgUp != tess->event && orgLo != tess->event ); assert( ! regUp->fixUpperEdge && ! regLo->fixUpperEdge ); if( orgUp == orgLo ) return FALSE; /* right endpoints are the same */ tMinUp = MIN( orgUp->t, dstUp->t ); tMaxLo = MAX( orgLo->t, dstLo->t ); if( tMinUp > tMaxLo ) return FALSE; /* t ranges do not overlap */ if( VertLeq( orgUp, orgLo )) { if( EdgeSign( dstLo, orgUp, orgLo ) > 0 ) return FALSE; } else { if( EdgeSign( dstUp, orgLo, orgUp ) < 0 ) return FALSE; } /* At this point the edges intersect, at least marginally */ DebugEvent( tess ); tesedgeIntersect( dstUp, orgUp, dstLo, orgLo, &isect ); /* The following properties are guaranteed: */ assert( MIN( orgUp->t, dstUp->t ) <= isect.t ); assert( isect.t <= MAX( orgLo->t, dstLo->t )); assert( MIN( dstLo->s, dstUp->s ) <= isect.s ); assert( isect.s <= MAX( orgLo->s, orgUp->s )); if( VertLeq( &isect, tess->event )) { /* The intersection point lies slightly to the left of the sweep line, * so move it until it''s slightly to the right of the sweep line. * (If we had perfect numerical precision, this would never happen * in the first place). The easiest and safest thing to do is * replace the intersection by tess->event. */ isect.s = tess->event->s; isect.t = tess->event->t; } /* Similarly, if the computed intersection lies to the right of the * rightmost origin (which should rarely happen), it can cause * unbelievable inefficiency on sufficiently degenerate inputs. * (If you have the test program, try running test54.d with the * "X zoom" option turned on). */ orgMin = VertLeq( orgUp, orgLo ) ? orgUp : orgLo; if( VertLeq( orgMin, &isect )) { isect.s = orgMin->s; isect.t = orgMin->t; } if( VertEq( &isect, orgUp ) || VertEq( &isect, orgLo )) { /* Easy case -- intersection at one of the right endpoints */ (void) CheckForRightSplice( tess, regUp ); return FALSE; } if( (! VertEq( dstUp, tess->event ) && EdgeSign( dstUp, tess->event, &isect ) >= 0) || (! VertEq( dstLo, tess->event ) && EdgeSign( dstLo, tess->event, &isect ) <= 0 )) { /* Very unusual -- the new upper or lower edge would pass on the * wrong side of the sweep event, or through it. This can happen * due to very small numerical errors in the intersection calculation. */ if( dstLo == tess->event ) { /* Splice dstLo into eUp, and process the new region(s) */ if (tessMeshSplitEdge( tess->mesh, eUp->Sym ) == NULL) longjmp(tess->env,1); if ( !tessMeshSplice( tess->mesh, eLo->Sym, eUp ) ) longjmp(tess->env,1); regUp = TopLeftRegion( tess, regUp ); if (regUp == NULL) longjmp(tess->env,1); eUp = RegionBelow(regUp)->eUp; FinishLeftRegions( tess, RegionBelow(regUp), regLo ); AddRightEdges( tess, regUp, eUp->Oprev, eUp, eUp, TRUE ); return TRUE; } if( dstUp == tess->event ) { /* Splice dstUp into eLo, and process the new region(s) */ if (tessMeshSplitEdge( tess->mesh, eLo->Sym ) == NULL) longjmp(tess->env,1); if ( !tessMeshSplice( tess->mesh, eUp->Lnext, eLo->Oprev ) ) longjmp(tess->env,1); regLo = regUp; regUp = TopRightRegion( regUp ); e = RegionBelow(regUp)->eUp->Rprev; regLo->eUp = eLo->Oprev; eLo = FinishLeftRegions( tess, regLo, NULL ); AddRightEdges( tess, regUp, eLo->Onext, eUp->Rprev, e, TRUE ); return TRUE; } /* Special case: called from ConnectRightVertex. If either * edge passes on the wrong side of tess->event, split it * (and wait for ConnectRightVertex to splice it appropriately). */ if( EdgeSign( dstUp, tess->event, &isect ) >= 0 ) { RegionAbove(regUp)->dirty = regUp->dirty = TRUE; if (tessMeshSplitEdge( tess->mesh, eUp->Sym ) == NULL) longjmp(tess->env,1); eUp->Org->s = tess->event->s; eUp->Org->t = tess->event->t; } if( EdgeSign( dstLo, tess->event, &isect ) <= 0 ) { regUp->dirty = regLo->dirty = TRUE; if (tessMeshSplitEdge( tess->mesh, eLo->Sym ) == NULL) longjmp(tess->env,1); eLo->Org->s = tess->event->s; eLo->Org->t = tess->event->t; } /* leave the rest for ConnectRightVertex */ return FALSE; } /* General case -- split both edges, splice into new vertex. * When we do the splice operation, the order of the arguments is * arbitrary as far as correctness goes. However, when the operation * creates a new face, the work done is proportional to the size of * the new face. We expect the faces in the processed part of * the mesh (ie. eUp->Lface) to be smaller than the faces in the * unprocessed original contours (which will be eLo->Oprev->Lface). */ if (tessMeshSplitEdge( tess->mesh, eUp->Sym ) == NULL) longjmp(tess->env,1); if (tessMeshSplitEdge( tess->mesh, eLo->Sym ) == NULL) longjmp(tess->env,1); if ( !tessMeshSplice( tess->mesh, eLo->Oprev, eUp ) ) longjmp(tess->env,1); eUp->Org->s = isect.s; eUp->Org->t = isect.t; eUp->Org->pqHandle = pqInsert( &tess->alloc, tess->pq, eUp->Org ); if (eUp->Org->pqHandle == INV_HANDLE) { pqDeletePriorityQ( &tess->alloc, tess->pq ); tess->pq = NULL; longjmp(tess->env,1); } GetIntersectData( tess, eUp->Org, orgUp, dstUp, orgLo, dstLo ); RegionAbove(regUp)->dirty = regUp->dirty = regLo->dirty = TRUE; return FALSE; }
static void ComputeWinding( TESStesselator *tess, ActiveRegion *reg ) { reg->windingNumber = RegionAbove(reg)->windingNumber + reg->eUp->winding; reg->inside = IsWindingInside( tess, reg->windingNumber ); }