DIRECTORY Imager, IO, Rope, RatNums, EuclideanGraphs; ConvexEuclideanGraphs: CEDAR DEFINITIONS = BEGIN OPEN RN: RatNums, EG: EuclideanGraphs; ConvexEuclideanGraph: TYPE = EG.EuclideanGraph; Vertex: TYPE = EG.Vertex; VertexList: TYPE = EG.VertexList; Adjacency: TYPE = EG.Adjacency; AdjacencyList: TYPE = EG.AdjacencyList; Point: TYPE = EG.Point; PointList: TYPE = EG.PointList; CEGEdgeData: TYPE = REF CEGEdgeDataRec; -- This definition repeated in ConvexEuclideanGraphs.mesa CEGEdgeDataRec: TYPE = RECORD[ exteriorEdge: BOOLEAN, -- true if region to the left of edge to this point is external to the current outline, i.e. if the region on whose boundary this edge lies is the exterior of the current outline. intersectionPolygonEdge: BOOLEAN _ FALSE, -- mark an edge that lies along the intersection polygon formed by the input polygon and the outline of the current figure clientData: REF ]; Verbosity: TYPE = REF VerbosityRec; VerbosityRec: TYPE = RECORD[ in, out: IO.STREAM ]; ConvexHull: PROC [L: PointList] RETURNS [Hull: PointList]; ThreeOrMoreVertices: PROC [w: EG.EuclideanGraph] RETURNS [BOOL]; AddEdge: PROC [root, adjacentVertex: Vertex, exterior: BOOL _ FALSE, IPEdge: BOOL _ FALSE, clientData: REF _ NIL, visitedValue: BOOL _ FALSE]; InsertVertexInEdges: PROC [v, w: Vertex, p: Point, vertexData: REF _ NIL] RETURNS [newVertex: Vertex]; JoinVertices: PROC [start, end: Vertex, startToEndData, endToStartData: REF _ NIL, startToEndIPEdge, endToStartIPEdge: BOOL _ FALSE, startToEndExterior, endToStartExterior: BOOL _ FALSE, startToEndVisited, endToStartVisited: BOOL _ FALSE]; StepVertex: PROC[v1, w1: Vertex, outline: BOOL] RETURNS [ v2, w2: Vertex]; NextVertex: PROC[v, w: Vertex, outline: BOOL] RETURNS [ z: Vertex]; PreviousVertex: PROC[v, w: Vertex, outline: BOOL] RETURNS [z: Vertex]; NextOutlineVertex: PROC[v, w: Vertex] RETURNS [ z: Vertex]; NextPolygonVertex: PROC[ v, w: Vertex] RETURNS [ Vertex]; SpecialPreviousOutlineVertex: PROC [w: Vertex] RETURNS [v:Vertex]; PreviousOutlineVertex: PROC [v, w: Vertex] RETURNS [z: Vertex]; PreviousPolygonVertex: PROC [v, w: Vertex] RETURNS [z:Vertex]; GetEdgeClientData: PROC [startToEnd: Adjacency] RETURNS [data: REF ANY]; SetEdgeClientData: PROC [startToEnd: Adjacency, data: REF ANY]; SetPolygonClientData: PROC [start, end: Vertex, data: REF, setVisited: BOOL _ FALSE]; GetEdgeExterior: PROC [startToEnd: Adjacency] RETURNS [value: BOOL]; SetEdgeExterior: PROC [startToEnd: Adjacency, value: BOOL]; GetEdgeIPEdge: PROC [startToEnd: Adjacency] RETURNS [value: BOOL]; SetEdgeIPEdge: PROC [startToEnd: Adjacency, value: BOOL]; SetPolygonIPEdges: PROC [start, end: Vertex, value: BOOL]; ClearGraphIPEdges: PROC [v, w: Vertex]; IPVertexToEdge: PROC [intersectionPolyVertex: Vertex] RETURNS [intersectionPolyNext: Vertex]; IPVertex: PROC [v: Vertex] RETURNS [BOOL]; ClearEdgeVisitedFields: PROC [v, w: Vertex]; ClearGraphEdgeFields: PROC [v: Vertex]; SetOutwardOutlineEdgeFields: PROC [start, end: Vertex, exteriorEdgeValue, intersectionPolygonEdgeValue: BOOL, clientDataValue: REF _ NIL]; SetPolygonFields: PROC [start, end: Vertex, exteriorEdgeValue, intersectionPolygonEdgeValue: BOOL _ FALSE, clientDataValue: REF _ NIL]; CenterOfMass: PROC[ v, w : Vertex, outline: BOOL] RETURNS [Point]; CountPolygonVertices: PROC [start, end: Vertex, outline: BOOL _ FALSE] RETURNS [numVertices: CARDINAL]; AddVertexToPolygon: PROC [entryPrior, entryNext: Vertex, coordinates: Point, clientData: REF _ NIL] RETURNS [exitPrior, exitNext: Vertex]; PointSectorRelation: TYPE = {EQUALCOM, RIGHTSPOKEINTERIOR, EQUALRIGHTVERTEX, LEFTSPOKEINTERIOR, EQUALLEFTVERTEX, POLYGONEDGEINTERIOR, SECTORINTERIOR, RIGHTRAYINTERIOR, LEFTRAYINTERIOR, SUBTENDINTERIOR, OUTSIDESUBTEND}; PointSectorTest: PROC [rightvertex, leftvertex, centerOfMass, test: Point] RETURNS[PointSectorRelation]; FindSubtendForPoint: PROC[v, w: Vertex, centerOfMass, test: Point, outline: BOOL ] RETURNS [status: PointSectorRelation, t, u: Vertex]; VertexExternalToPolygon: PROC [firstCOM: Point, firstStart, firstEnd, secondStart, secondEnd: Vertex] RETURNS [found: BOOL, newFirstStart, newFirstEnd, newSecondStart, newSecondEnd: Vertex _ NIL]; ConvexPolygon: PROC[v, w : Vertex, outline: BOOL ] RETURNS [BOOL]; ConvexPolygonCompareStatus: TYPE = { Encloses, External, Intersection }; ConvexPolygonCompare: PROC [innerCOM: Point, innerStart, innerEnd: Vertex, innerOutline: BOOL, outerStart, outerEnd: Vertex, checkExternal: BOOL _ TRUE] RETURNS [status: ConvexPolygonCompareStatus, innerPrevious, innerCommon, innerNext, outerPrevious, outerCommon, outerNext: Vertex _ NIL]; FindInternalPolygonForPoint: PROC [v, w: Vertex, test: Point] RETURNS [pointSectorStatus: PointSectorRelation, y, z: Vertex]; SearchInternalPolygonsForPoint: PROC [v, w: Vertex, test: Point] RETURNS [found: BOOL, y, z: Vertex]; ConvexPolygonOnOutline: PROC[ prew2, w2, prew1, w1: Vertex] RETURNS [BOOL]; GrowToConvexOutline: PROC[w2, v, w1: Vertex, verbosity: Verbosity _ NIL] RETURNS [v3, w3: Vertex]; ConvexifyOutline: PROC [outlineStart, outlineEnd: Vertex, verbosity: Verbosity _ NIL] RETURNS [newOutlineStart, newOutlineEnd: Vertex]; RegionList: TYPE = LIST OF Region; Region: TYPE = REF RegionRec; RegionRec: TYPE = RECORD [ outline: Imager.Trajectory, -- should be a convex polygon. clientData: REF, -- of (outline - holes) holes: RegionList _ NIL -- note that holes can themselves contain holes. ]; InternalPolygons: PROC [v, w: Vertex] RETURNS [RegionList]; IsAProc: TYPE = PROC [clientData: REF] RETURNS [BOOL]; -- definition of region boundaries MaximalRegions: PROC [outlineVertex: Vertex, isA: IsAProc, verbosity: Verbosity _ NIL] RETURNS [outRegionList: RegionList]; RegionBuilder: PROC [outlineStart, outlineEnd: Vertex, setOutlineVerticesVisited: BOOL, vertexVisitedValue: BOOLEAN, isA: IsAProc, verbosity: Verbosity _ NIL] RETURNS [region: Region]; RopeFromClientData: TYPE = EG.RopeFromRefProc; ClientDataFromRope: TYPE = EG.RefFromRopeProc; DumpGraph: PROC [v: ConvexEuclideanGraph, ropeFromClientData: RopeFromClientData, filename: Rope.ROPE]; GraphFromFile: PROC [filename: Rope.ROPE, clientDataFromRope: ClientDataFromRope, MessageStream: IO.STREAM] RETURNS [v: ConvexEuclideanGraph, numberVertices: CARDINAL]; END. /0ConvexEuclideanGraphs.mesa Last Edited by: Arnon, February 26, 1985 2:21:53 pm PST Routines for manipulation of Convex EuclideanGraphs, i.e. EuclideanGraphs which consists of a convex polygon (the "outline (polygon)") decomposed into convex polygons (the "internal polygons"). Included are routines for manipulation of convex polygons represented as (Convex) EuclideanGraphs. It is assumed that edge data in the underlying EG.EuclideanGraph is of type CEGEdgeData. In particular, any creator of a CEG is assumed to allocate, for each edge, a (non-NIL) CEGEdgeData for the edge to point to. There is no manipulation of vertex data. It is assumed that client data for the area outside the outline of a Convex EuclideanGraphs is NIL. Convex EuclideanGraphs edge data consists of two parts: (a) geometrical information for maintainance of the graph outline (the exteriorEdge field), and (b) state information, consisting of the state itself (leftData field), and info for state updating (intersectionPolygonEdge field). States here are colors. Procedures for merging Convex EuclideanGraphs are left to the client, since although the geometrical semantics of such a merge is client-independent, the desired manipulation of client data probably varies from client to client. This interface does attempt to provide useful geometrical primitives from which merge operators can be constructed. Some reasons why the outline of a Convex EG.EuclideanGraph is useful: can do point location with respect to wedges of the current outline, can terminate depth first searches of the internal polygons of the current figure. ************** Hull Computation ************** Convex Hull of up to 30 points. Graham's algorithm is used (Info. Proc. Letters, I, 1972, 132-133). Modification: there is no need to do his Step 4, i.e. to check for points with equal polar coordinates. If there are exactly two such points, they will be removed in the course of step 5. Not clear what may happen if there are three or more such points. firstVertex is thought of as being on the polygonal outline of a figure. The new vertex [x,y] is added so as to come after the vertex which precedes firstVertex (call it vhandlelast), and before firstVertex, in the counterclockwise ordering of vertices. firstVertex is returned, so that subsequently added vertices will always be inserted just before firstVertex. ************** Graph Structure ************** Suffices to test whether an arbitrary vertex is adjacent to at least two distinct vertices. Join vertices with two directed edges with specified data. The possible prior existence of geometrically identical edges between the two vertices is the client's business. We need a version in ConvexEuclideanGraphs in order to create new CEGDataRec's for each new edge (i.e. using the ConvexEuclideanGraph version of AddEdge). ************** Graph Traversal ************** w is a vertex on the outline of a ConvexEuclideanGraph. By checking the exteriorEdge fields of the adjacencies of w, we find the vertex z which precedes w in the counterclockwise ordering of vertices of a polygon. This routine is "special" because it requires only one input vertex, instead of the two endpoints of an edge, and it uses exteriorEdge fields, which in general is not a safe thing to do. Assuming that [v, w] is an edge on the outline of some ConvexEuclideanGraph containing at least two vertices, find the vertex z which precedes v in the counterclockwise ordering of vertices of a polygon (counterclockwise order defined with respect to the INTERIOR of the figure on whose outline [v, w] lies). Assuming that w is a vertex on the outline of some ConvexEuclideanGraph containing at least two vertices, find the vertex v which precedes w in the counterclockwise ordering of vertices. ************** Edge Fields ************** [start, End] is an edge. Obtain its client data. [start, End] is an edge. Set its client data. [start, end] is a (counterclockwise) directed edge of an internal polygon. Its client data is set. If setVisited, then the visited fields of its edges are set TRUE. [start, End] is an edge. Obtain the value of its exterior field. [start, End] is an edge. Set the value of its exterior field. [start, End] is an edge. Obtain the value of its intersectionPolygonEdge field. [start, End] is an edge. The value of its intersectionPolygonEdge field is set. [start, end] is a (counterclockwise) directed edge of an internal polygon. The value of the intersectionPolygonEdge fields is set. [intersectionPolyStart, intersectionPolyEnd] is a (directed) edge in the current (i.e. combined) geometry which is contained in the intersection polygon (of current figure and input polygon), and such that the interior of the intersection polygon lies to its left. The leftRegionVisited and intersectionPolygonEdge fields of all edges of all polygons interior to the intersection polygon are cleared. The algorithm uses Depth First Search. Search the adjacencies of intersectionPolyVertex until an intersection polygon edge is found True iff some edge outgoing from v is an Intersection Polygon Edge. [v,w] is a counterclockwise oriented edge of a polygon contained in the outline of the current figure. Clear the visited fields of all edges of all polygons interior to a ConvexEuclideanGraph. Clears IPEdge and visited fields of all edges in the Graph pointed to by v. [start, end] is a (counterclockwise) directed edge of an outline. The fields of all "outward-facing" edges of the outline are set to the specified values. [start, end] is a (counterclockwise) directed edge of an (internal) polygon. The fields of all edges of the polygon are set to the specified values. ************** Convex (internal) polygon operations ************** [v,w] is a counterclockwise oriented edge on the polygon [start, end] is an edge of a polygon with at least two vertices. entryPrior = entryNext = NIL for a previously empty polygon. entryPrior = NIL, entryNext#NIL for a polygon with one previous vertex. clientData is the interior clientData for the polygon being created. If previously empty polygon, then at exit exitPrior = NIL, exitNext is new vertex. Otherwise, at exit, exitPrior is new vertex and exitNext = entryNext. Determine if the test point lies in the sector of a polygon determined by leftvertex, rightvertex, centerOfMass A sector is defined as the (interior of the) triangle having as vertices the center of mass of p, the ith vertex of p (rightvertex as you face out from the center of mass) and the i+1st vertex of p (leftvertex). Note that for a sector of a convex polygon, Angle(leftvertex, centerOfMass, rightvertex) < 180 degrees. [v,w] is a counterclockwise oriented edge on the polygon. [t, u] is a counterclockwise oriented edge on the polygon such that test lies in the closure of the subtend defined by [t, u] and the polygon's center of mass. If test = centerOfMass, then status = EQUALCOM and [t, u] _ [v, w]. [firstStart, firstEnd] is a (counterclockwise) directed edge of an internal polygon of a Convex EG.EuclideanGraph, as is [secondStart, secondEnd]. firstCOM is center of mass of first. If some vertex of second lies external to first, then found _ TRUE, and [newFirstStart, newFirstEnd] is a (counterclockwise) directed edge of first in whose (closed) wedge the exterior point newSecondStart lies; newSecondEnd is the vertex of second which follows newSecondStart. If no points of second are exterior to first, then found = FALSE, and the other output parameters are undefined. [v,w] is a counterclockwise oriented edge on the polygon, must have at least 2 (3?) vertices. [innerStart, innerEnd] is a counterclockwise oriented edge on the "inner" polygon, whose center of mass is innerCOM. innerOutline specifies whether inner polygon is to be outline or polygon stepped. [outerStart, outerEnd] is a counterclockwise oriented edge on the "outer" polygon, such that outerStart lies in the subtend of the wedge of the sector of the inner polygon defined by [innerStart, innerEnd], but not in (the closure of) the wedge itself. Outer polygon is always polygon stepped. We trace the outer polygon, stepping the inner polygon to follow, until an intersection of the outer with the inner polygon is found, or we determine that there is no intersection (i.e. we've gone all the way around the outer polygon without finding one). As we trace, if checkExternal true, we simultaneously collect information to be able to decide whether the outer polygon is external to or encloses the inner, in case there is no intersection. If an intersection is found, then innerCommon and outerCommon are distinct vertices of the two polygons whose coordinates are the intersection point, innerStart and outerStart the respective preceding vertices, and innerEnd and outerEnd the respective following vertices. If no intersection is found, all are undefined. ************** Graph Searching ************** [v, w] is a (counterclockwise) directed edge of an internal polygon of a Convex EG.EuclideanGraph within whose outline test is known to lie. Either [y, z] is a (counterclockwise) directed edge of the unique internal polygon in whose interior test lies, such that test lies in the closed wedge of this polygon defined by [y, z], or [y, z] is a (counterclockwise) directed edge of an internal polygon such that test lies in the closed edge [y, z]. [v, w] is a (counterclockwise) directed edge of an internal polygon of a Convex EG.EuclideanGraph. If test is found within the closure of the current structure (i.e. in its interior or on its outline), then found = TRUE, and either [y, z] is a (counterclockwise) directed edge of the unique internal polygon in whose interior test lies, such that test lies in the closed wedge of this polygon defined by [y, z], or [y, z] is a (counterclockwise) directed edge of an internal polygon such that test lies in the closed edge [y, z]. If test is not found within the closure of the current structure, then found = FALSE, y = v, and z = w. ************** Graph Outline ************** We assume that the vertices are given to us in counterclockwise order (with respect to figure interior) along the outline of the figure. [outlineStart, outlineEnd] is an edge of a closed EG.EuclideanGraph, i.e. one which has an outline. An outline may loop back and touch itself (at a single vertex), but may not cross itself. This edge is assumed to be counterclockwise-oriented with respect to the interior of the outline. We walk around the outline, calling GrowToConvexOutline to add new edges so that at the end we have a Convex EG.EuclideanGraph, i.e. a convex outline and convex internal polygons. Note a previous way of finding args for GrowToConvexOutline: IF lensInExterior THEN { New determination of arguments for GrowToConvexOutline - search the adjacencies of nextIntersectionVertex until we find a consecutive pair x,y such that [x,nextIntersectionVertex].leftRegionExterior and [nextIntersectionVertex,y].leftRegionExterior. Some kind of searching like this is necessary, because we can no longer necessarily have our hands on the arguments for GrowToConvexOutline at the end of a lens. [x, y] _ FindExternalAcuteAngle[nextIntersectionVertex]; [newCombinedStart, newCombinedEnd] _ GrowToConvexOutline[y, nextIntersectionVertex, x]; ************** Region Extraction ************** [v, w] is a counterclockwise oriented edge of an internal polygon of a ConvexEuclideanGraph. Extract all the internal polygons, and their respective clientData's, from the graph. Uses Depth First Search. The graph must have at least three vertices. [v,w] is a counterclockwise oriented edge of an internal polygon of a ConvexEuclideanGraph. **************** Input and Output **************** This is for edge client data. Any non-blank character ok. IF data=NIL THEN RETURN["NIL"] ELSE RETURN[ ... -- all RopeFromData's should do this This is for edge client data. Remember to catch "NIL" Dump a EuclideanGraph to a file. Does Depth First Search. m Κ ˜šœ™J™7J™—šΟk ˜ Jšœ˜Jšœ˜Jšœ˜Jšœ˜Jšœ˜Jšœ˜—head2šœœ ˜(J˜—š œœœœ œ˜.J˜—šœΑ™ΑJ™—Jšœœœ˜/Jšœœœ˜Jšœ œœ ˜!Jšœ œœ ˜Jšœœœ˜'Jšœœœ˜Jšœ œœ ˜J™Jšœb™bJ™Jš œ/œœ,œ5œ"™ΦJšœ‰œ™J™Jšœ7™7Jšœ_™_šœ™J™—Jšœ œœœΟc9˜bšœœœ˜Jšœœž³˜ΚJšœœœžz˜€Jšœ ˜Jšœ˜—J™JšœΫ™ΫJ™šœ)œ™EJšœE™EJšœR™RJ™—Jšœ œœ˜#šœœœ˜Jšœ œ˜Jšœ˜—Icode™™0J˜—šΟn œœœ˜:Lšœ™L™ΔJšœλ™λJ™—J™™.J™—š Ÿœœœœœ˜@Lšœ\™\L˜—šŸœœ*œœ œœœœœœ˜ŽL™«L˜—š Ÿœœ&œœœ˜fLšœš™šL˜—LšŸ œœ6œœ&œœ*œœ(œœ˜οJ™™/J™—šŸ œœœœ˜JJ˜—šŸ œœœœ˜CJ˜—šŸœœœœ ˜FJ˜—šŸœœœ˜;J˜—šŸœœœ ˜9J˜—šŸœœ œ ˜BJšœΦ™ΦJšœΊ™ΊJ˜—šŸœœœ ˜?Jšœ΄™΄J™—šŸœœœ ˜>JšœΊ™ΊJ™—J™™+J˜—š Ÿœœœœœ˜HLšœ0™0J™—šŸœœœœ˜?Lšœ-™-J™—š Ÿœœœœœ˜ULšœ₯™₯L™—šŸœœœ œ˜DLšœ@™@J™—šŸœœ œ˜;Lšœ=™=J™—šŸ œœœ œ˜BLšœO™OJ™—šŸ œœ œ˜9LšœO™OJ™—šŸœœœ˜:Lšœ‚™‚L™—šŸœœ˜'J•commentTRUEšœ·™·J™—šŸœœ"œ ˜]Lšœ\™\L˜—šŸœœ œœ˜*L™CL˜—šŸœœ˜,JšœΑ™ΑJ˜—šŸœœ ˜'J™KJ˜—š ŸœœGœœœ˜ŠLšœš™šL™—š ŸœœGœœœœ˜‡Lšœ”™”L™—L™J™CšŸ œœœœ ˜BJ–TRUE™8J™—š Ÿœœœœœœ˜gL™@—š ŸœœAœœœ˜ŠJšœœ/œ—™εJ˜—šœœœœœœœœœœœœœ˜ΪJ˜—šŸœœ6œ˜hJš žJœ&žaœžœ žEœ ž™ΓJšœg™gJ™—šŸœœ3œœ-˜‡J–TRUEšœœ™žJ˜—š ŸœœIœ œEœ˜ΔLšœ`œέ™ΏL™—š Ÿ œœœœœ˜B–TRUE™]L˜——šœœ(˜HLšœ™—šŸœœ?œ/œœœ}œ˜’Jšœν™νJšœΐ™ΐJšœΐ™ΐJ™—L˜™.J™L˜—šŸœœœ8˜}LšœPœλ™½L™—šŸœœœ œ˜eLšœPœ¨™ϊL™—J˜J™,šŸœœ œœ˜KJ–TRUE™ˆJ™J™—šŸœœ+œœ˜bJ˜—šŸœœ;œœ*˜‡Jšœ2œΪœD™Τšœ<™<šœœ™Jšœœ™œJšœ8™8JšœW™W—J™—J˜—J™™1J˜—Jšœ œœœ˜"Jšœœœ ˜šœ œœ˜Jšœž˜:Jšœ œž˜(Jšœœž0˜HJšœ˜J˜—šŸœœœ˜;Jšœ³™³JšœF™FJ™—š Ÿœœœœœœž"˜YL˜—J™šŸœœ>œœ˜{Jšœ\™\J™—š Ÿ œœ?œœ'œœ˜ΈJ™—J™™3J™—šœœœ˜.L™9Lš œœœœœœž$™TL™—šœœœ˜.Lšœ6™6L™—šŸ œœRœ˜gJšœ9™9L™—šŸ œœœ9œœœ+œ˜¨J˜—L™L™šœ˜J™——…—tQΓ