public static bool isSimple(IList <Vector3> poly) { PriorityHeap <VertexEv> evQueue = setupEvQueue(poly); SweepLine SL = new SweepLine(poly); //Debug.LogWarning("Queue size=" + evQueue.Count); while (!evQueue.Empty) { VertexEv ev = evQueue.Pop(); if (ev.type == VertexEv.vType.LEFT) { int idx = SL.Add(ev.edge); //Debug.Log("Left: " + idx + "/" + SL.Count); if (SL.intersect(idx, idx - 1)) { return(false); } if (SL.intersect(idx, idx + 1)) { return(false); } } else // right vertex { int idx = SL.Find(ev.edge); //Debug.Log("Right: " + idx + "/" + SL.Count); if (SL.intersect(idx - 1, idx + 1)) { return(false); } SL.Remove(idx); } } return(true); }
/* * List<List<Vector3>> decompose(List<Vector3> poly) { * * } */ // returns one intersection point or null if polygon is simple static IntersectionPoint oneIntersection(IList <Vector3> poly) { PriorityHeap <VertexEv> evQueue = setupEvQueue(poly); SweepLine SL = new SweepLine(poly); IntersectionPoint I; while (!evQueue.Empty) { VertexEv ev = evQueue.Pop(); if (ev.type == VertexEv.vType.LEFT) { int idx = SL.Add(ev.edge); I = SL.GetIntersectionPoint(idx, idx - 1); if (I != null) { return(I); } I = SL.GetIntersectionPoint(idx, idx + 1); if (I != null) { return(I); } } else // right vertex { int idx = SL.Find(ev.edge); I = SL.GetIntersectionPoint(idx - 1, idx + 1); if (I != null) { return(I); } SL.Remove(idx); } } return(null); }
/// <summary> /// simple_Polygon(): test if a Polygon is simple or not /// Input: Pn = a polygon with n vertices V[] /// Return: false(0) = is NOT simple /// true(1) = IS simple /// </summary> /// <param name="polygon"></param> /// <returns></returns> public bool IsSimple(Polygon polygon) { Require.ArgumentNotNull(polygon, nameof(polygon)); EventQueue eventQueue = new EventQueue(polygon); SweepLine sweepLine = new SweepLine(polygon); Event currentEvent; // the current event // This loop processes all events in the sorted queue // Events are only left or right vertices since // No new events will be added (an intersect => Done) while ((currentEvent = eventQueue.Next()) != null) { // while there are events if (currentEvent.EventType == EventType.Left) { // process a left vertex var currentSegment = sweepLine.Add(currentEvent); // add it to the sweep line if (sweepLine.Intersect(currentSegment, currentSegment.Above)) { return(false); // Pn is NOT simple } if (sweepLine.Intersect(currentSegment, currentSegment.Below)) { return(false); // Pn is NOT simple } } else { // processs a right vertex var currentSegment = sweepLine.Find(currentEvent); if (sweepLine.Intersect(currentSegment.Above, currentSegment.Below)) { return(false); // Pn is NOT simple } sweepLine.Remove(currentSegment); // remove it from the sweep line } } return(true); // Pn IS simple }
public static void BentleyOttmann(IList <Vector3> poly) { PriorityHeap <VertexEv> evQ = setupEvQueue(poly); SweepLine SL = new SweepLine(poly); Dictionary <int, IntersectionPoint> intersectionPoints = new Dictionary <int, IntersectionPoint>(); IntersectionPoint I; while (!evQ.Empty) { VertexEv ev = evQ.Pop(); if (ev.type == VertexEv.vType.LEFT) { int idx = SL.Add(ev.edge); I = SL.GetIntersectionPoint(idx, idx - 1); if (I != null) // Intersection { int ID = I.Id(poly.Count); if (!intersectionPoints.ContainsKey(ID)) { intersectionPoints[ID] = I; //evQ.Add(new VertexEv(I.v, I.Edge1, I.Edge2, VertexEv.vType.INTERSECT)); evQ.Add(new VertexEv(I)); } } I = SL.GetIntersectionPoint(idx, idx + 1); if (I != null) // intersection { int ID = I.Id(poly.Count); if (!intersectionPoints.ContainsKey(ID)) { intersectionPoints[ID] = I; evQ.Add(new VertexEv(I.P, I.Edge1, I.Edge2, VertexEv.vType.INTERSECT)); } } } else if (ev.type == VertexEv.vType.RIGHT) { int idx = SL.Find(ev.edge); I = SL.GetIntersectionPoint(idx - 1, idx + 1); if (I != null) { int ID = I.Id(poly.Count); if (!intersectionPoints.ContainsKey(ID)) { intersectionPoints[ID] = I; evQ.Add(new VertexEv(I)); } } } else // intersection point // unfinished business ... { } } }