📄 hints.c
字号:
/*:h2 id=subpath.Navigation Through Edge Lists For continuity checking purposes, we need to navigate through edgelists by the "subpath" chains and answer questions about edges. Thesubpath chain links together edges that were part of the same subpath(no intervening move segments) when the interior of the path wascalculated. Here we use the term "edge" to mean every edge listthat was created in between changes of direction. The subpath chains are singly-linked circular chains. For the convenienceof building them, they direction of the list (from edge to edge) is thereverse of the order in which they were built. Within any single edge,the subpath chain goes from top-to-bottom. (There might be a violationof this because of the way the user started the first chain; see:hdref refid=fixsubp..). :h3.ISTOP() and ISBOTTOM() - Flag Bits for Edge Lists at the Top andBottom of Their SubPaths*/ #define ISTOP(flag) ((flag)&0x20)#define ISBOTTOM(flag) ((flag)&0x10)/*:h3.ISLEFT() - Flag Bit for Left Edges*/ #define ISLEFT(flag) ((flag)&0x08) /*:h3.XofY() - Macro to Find X Value at Given Y This macro can only be used if it is known that the Y is within thegiven edgelist's ymin and ymax.*/ #define XofY(edge, y) edge->xvalues[y - edge->ymin] /*:h3.findXofY() - Like XofY(), Except not Restricted If the Y is out of bounds of the given edgelist, this macro willcall SearchXofY to search the edge's subpath chain for the correctY range. If the Y value is off the edge, MINPEL is returned.*/#define findXofY(edge, y) ((y < edge->ymin || y >= edge->ymax) ? SearchXofY(edge, y) : XofY(edge, y)) /*:h4.SearchXofY() - Routine Called by FindXofY() for Difficult Cases The concept of this routine is to follow the subpath chain to find theedge just below (i.e., next in chain) or just above (i.e., immediatelybefore in chain. It is assumed that the Y value is no more than oneoff of the edge's range; XofY() could be replace by FindXofY() tocall ourselves recursively if this were not true.*/ static pel SearchXofY(edge, y) register struct edgelist *edge; /* represents edge */ register pel y; /* 'y' value to find edge for */{ register struct edgelist *e; /* loop variable */ if (y < edge->ymin) { if (ISTOP(edge->flag)) return(MINPEL); for (e = edge->subpath; e->subpath != edge; e = e->subpath) { ; } if (e->ymax == edge->ymin) return(XofY(e, y)); } else if (y >= edge->ymax) { if (ISBOTTOM(edge->flag)) return(MINPEL); e = edge->subpath; if (e->ymin == edge->ymax) return(XofY(e, y)); } else return(XofY(edge, y)); abort("bad subpath chain"); /*NOTREACHED*/}/*:h3.ISBREAK() Macro - Tests if an Edge List is at a "Break" The subpath chains are organized top to bottom. When the bottom ofa given edge is reached, the subpath chain points to the top of thenext edge. We call this a "break" in the chain. The following macrois the simple test for the break condition:*/ #define ISBREAK(top,bot) (top->ymax != bot->ymin) /*:h3.ImpliedHorizontalLine() - Tests for Horizontal Connectivity This function returns true if two edges are connected horizontally.They are connected horizontally if they are consecutive in the subpath,and either we are at the bottom and the first edge is going down or weare at the top and the first edge is going up.*/ #define BLACKABOVE -1#define BLACKBELOW +1#define NONE 0 static int ImpliedHorizontalLine(e1, e2, y) register struct edgelist *e1,*e2; /* two edges to check */ register int y; /* y where they might be connected */{ register struct edgelist *e3,*e4; if (ISDOWN(e1->flag) == ISDOWN(e2->flag)) return(NONE); /* can't be consecutive unless different directions *//*Now we check for consecutiveness: Can we get from 'e1' to 'e2' withonly one intervening break? Can we get from 'e2' to 'e1' with only oneintervening break? 'e3' will be as far as we can get after 'e1'; 'e4'will be has far as we can get after 'e2':*/ for (e3 = e1; !ISBREAK(e3, e3->subpath); e3 = e3->subpath) { ; } for (e3 = e3->subpath; e3 != e2; e3 = e3->subpath) if (ISBREAK(e3, e3->subpath)) break; for (e4 = e2; !ISBREAK(e4, e4->subpath); e4 = e4->subpath) { ; } for (e4 = e4->subpath; e4 != e1; e4 = e4->subpath) if (ISBREAK(e4, e4->subpath)) break;/*If the edges are mutually consecutive, we must have horizontal linesboth top and bottom:*/ if (e3 == e2 && e4 == e1) return(TRUE);/*If the edges are not consecutive either way, no horizontal lines arepossible:*/ if (e3 != e2 && e4 != e1) return(NONE);/*Now let's swap 'e1' and 'e2' if necessary to enforce the rule that 'e2'follows 'e1'. Remember that subpath chains go in the opposite directionfrom the way the subpaths were built; this led to the simplest waydo build them.*/ if (e4 != e1) { e2 = e1; e1 = e3; /* remember e3 == e2, this just swaps 'e1' and 'e2' */ }/*Now we have everything to return the answer:*/ if (ISTOP(e1->flag) && y == e1->ymin) return(ISDOWN(e2->flag)); else if (ISBOTTOM(e1->flag) && y == e1->ymax) return(!ISDOWN(e2->flag)); else abort("ImpliedHorizontalLine: why ask?"); /*NOTREACHED*/} /*:h3 id=fixsubp.FixSubPaths() - Must be Called to Organize Subpath Chains The region-building code in Interior(), in particular splitedge(),maintains the rule that sub-paths are linked top-to-bottom exceptat breaks. However, it is possible that there may be a "false break"because the user started the subpath in the middle of an edge (andwent in the "wrong" direction from there, up instead of down). Thisroutine finds and fixes false breaks. Also, this routine sets the ISTOP and ISBOTTOM flags in the edge lists.*/ static void FixSubPaths(R) register struct region *R; /* anchor of region */{ register struct edgelist *e; /* fast loop variable */ register struct edgelist *edge; /* current edge in region */ register struct edgelist *next; /* next in subpath after 'edge' */ register struct edgelist *break1; /* first break after 'next' */ register struct edgelist *break2; /* last break before 'edge' */ register struct edgelist *prev; /* previous edge for fixing links */ int left = TRUE; for (edge = R->anchor; edge != NULL; edge = edge->link) { if (left) edge->flag |= ISLEFT(ON); left = !left; next = edge->subpath; if (!ISBREAK(edge, next)) continue; if (edge->ymax < next->ymin) abort("disjoint subpath?");/*'edge' now contains an edgelist at the bottom of an edge, and 'next'contains the next subsequent edgelist in the subpath, which must be atthe top. We refer to this a "break" in the subpath.*/ next->flag |= ISTOP(ON); edge->flag |= ISBOTTOM(ON); if (ISDOWN(edge->flag) != ISDOWN(next->flag)) continue;/*We are now in the unusual case; both edges are going in the samedirection so this must be a "false break" due to the way that the usercreated the path. We'll have to fix it.*/ for (break1 = next; !ISBREAK(break1, break1->subpath); break1 = break1->subpath) { ; } for (e = break1->subpath; e != edge; e = e->subpath) if (ISBREAK(e, e->subpath)) break2 = e;/*Now we've set up 'break1' and 'break2'. I've found the followingdiagram invaluable. 'break1' is the first break after 'next'. 'break2'is the LAST break before 'edge'.&drawing. next +------+ +---->+------+ +--->| >-----+ | | >-----+ | | | | | | | | | +-------------+ | +-------------+ | | |break1| | | | | | +->| >-------+ +->| >-----+ | | | | | | | | | +-------------+ | +------+ | | | | +----------------+ | | | | | +------+ | +->| >-----+ | +->| >-----+ | | | | | | | | | +-------------+ | +-------------+ | | | | | | |edge | | | |break2| | +->| >-----+ | +->| >-----+ | | | | | | | | | | | | | | | | | | | | | | | | | +------+ | | +------+ | | | | | +---------------+ +---------------+ &edrawing.We want to fix this situation by having 'edge' point to where 'break1'now points, and having 'break1' point to where 'break2' now points.Finally, 'break2' should point to 'next'. Also, we observe that'break1' can't be a bottom, and is also not a top unless it is the sameas 'next':*/ edge->subpath = break1->subpath; break1->subpath = break2->subpath; if (ISBREAK(break1, break1->subpath)) abort("unable to fix subpath break?"); break2->subpath = next; break1->flag &= ~ISBOTTOM(ON); if (break1 != next) break1->flag &= ~ISTOP(ON); }/*This region might contain "ambiguous" edges; edges exactly equal toedge->link. Due to the random dynamics of where they get sorted intothe list, they can yield false crossings, where the edges appearto cross. This confuses our continuity logic no end. Since we canswap them without changing the region, we do.*/ for (edge = R->anchor, prev = NULL; VALIDEDGE(edge); prev = edge, edge = prev->link) { if (! ISAMBIGUOUS(edge->flag)) continue; next = edge->subpath; while (ISAMBIGUOUS(next->flag) && next != edge) next = next->subpath;/*We've finally found a non-ambiguous edge; we make sure it is left/rightcompatible with 'edge':*/ if ( (ISLEFT(edge->flag) == ISLEFT(next->flag) && ISDOWN(edge->flag) == ISDOWN(next->flag) ) || (ISLEFT(edge->flag) != ISLEFT(next->flag) && ISDOWN(edge->flag) != ISDOWN(next->flag) ) ) continue; /*Incompatible, we will swap 'edge' and the following edge in the list.You may think that there must be a next edge in this swath. So did I.No! If there is a totally ambiguous inner loop, for example, we couldget all the way to the outside without resolving ambiguity.*/ next = edge->link; /* note new meaning of 'next' */ if (next == NULL || edge->ymin != next->ymin) continue; if (prev == NULL) R->anchor = next; else prev->link = next; edge->link = next->link;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -