//------------------------------------------------------------------------------ public bool AddPaths(List <List <ClipperIntPoint> > ppg, ClipperPolyType polyType, bool closed) { bool result = false; for (int i = 0; i < ppg.Count; ++i) { if (AddPath(ppg[i], polyType, closed)) { result = true; } } return(result); }
//------------------------------------------------------------------------------ private void InitEdge2(ClipperTEdge e, ClipperPolyType polyType) { if (e.Curr.Y >= e.Next.Curr.Y) { e.Bot = e.Curr; e.Top = e.Next.Curr; } else { e.Top = e.Curr; e.Bot = e.Next.Curr; } SetDx(e); e.PolyTyp = polyType; }
//------------------------------------------------------------------------------ public bool AddPath(List <ClipperIntPoint> pg, ClipperPolyType polyType, bool Closed) { if (!Closed && polyType == ClipperPolyType.Clip) { throw new ClipperException("AddPath: Open paths must be subject."); } int highI = (int)pg.Count - 1; if (Closed) { while (highI > 0 && (pg[highI] == pg[0])) { --highI; } } while (highI > 0 && (pg[highI] == pg[highI - 1])) { --highI; } if ((Closed && highI < 2) || (!Closed && highI < 1)) { return(false); } //create a new edge array ... List <ClipperTEdge> edges = new List <ClipperTEdge>(highI + 1); for (int i = 0; i <= highI; i++) { edges.Add(new ClipperTEdge()); } bool IsFlat = true; //1. Basic (first) edge initialization ... edges[1].Curr = pg[1]; RangeTest(pg[0], ref m_UseFullRange); RangeTest(pg[highI], ref m_UseFullRange); InitEdge(edges[0], edges[1], edges[highI], pg[0]); InitEdge(edges[highI], edges[0], edges[highI - 1], pg[highI]); for (int i = highI - 1; i >= 1; --i) { RangeTest(pg[i], ref m_UseFullRange); InitEdge(edges[i], edges[i + 1], edges[i - 1], pg[i]); } ClipperTEdge eStart = edges[0]; //2. Remove duplicate vertices, and (when closed) collinear edges ... ClipperTEdge E = eStart, eLoopStop = eStart; for (; ;) { //nb: allows matching start and end points when not Closed ... if (E.Curr == E.Next.Curr && (Closed || E.Next != eStart)) { if (E == E.Next) { break; } if (E == eStart) { eStart = E.Next; } E = RemoveEdge(E); eLoopStop = E; continue; } if (E.Prev == E.Next) { break; //only two vertices } else if (Closed && SlopesEqual(E.Prev.Curr, E.Curr, E.Next.Curr, m_UseFullRange) && (!PreserveCollinear || !Pt2IsBetweenPt1AndPt3(E.Prev.Curr, E.Curr, E.Next.Curr))) { //Collinear edges are allowed for open paths but in closed paths //the default is to merge adjacent collinear edges into a single edge. //However, if the PreserveCollinear property is enabled, only overlapping //collinear edges (ie spikes) will be removed from closed paths. if (E == eStart) { eStart = E.Next; } E = RemoveEdge(E); E = E.Prev; eLoopStop = E; continue; } E = E.Next; if ((E == eLoopStop) || (!Closed && E.Next == eStart)) { break; } } if ((!Closed && (E == E.Next)) || (Closed && (E.Prev == E.Next))) { return(false); } if (!Closed) { m_HasOpenPaths = true; eStart.Prev.OutIdx = Skip; } //3. Do second stage of edge initialization ... E = eStart; do { InitEdge2(E, polyType); E = E.Next; if (IsFlat && E.Curr.Y != eStart.Curr.Y) { IsFlat = false; } }while (E != eStart); //4. Finally, add edge bounds to LocalMinima list ... //Totally flat paths must be handled differently when adding them //to LocalMinima list to avoid endless loops etc ... if (IsFlat) { if (Closed) { return(false); } E.Prev.OutIdx = Skip; ClipperLocalMinima locMin = new ClipperLocalMinima { Next = null, Y = E.Bot.Y, LeftBound = null, RightBound = E }; locMin.RightBound.Side = ClipperEdgeSide.Right; locMin.RightBound.WindDelta = 0; for (; ;) { if (E.Bot.X != E.Prev.Top.X) { ReverseHorizontal(E); } if (E.Next.OutIdx == Skip) { break; } E.NextInLML = E.Next; E = E.Next; } InsertLocalMinima(locMin); m_edges.Add(edges); return(true); } m_edges.Add(edges); bool leftBoundIsForward; ClipperTEdge EMin = null; //workaround to avoid an endless loop in the while loop below when //open paths have matching start and end points ... if (E.Prev.Bot == E.Prev.Top) { E = E.Next; } for (; ;) { E = FindNextLocMin(E); if (E == EMin) { break; } else if (EMin == null) { EMin = E; } //E and E.Prev now share a local minima (left aligned if horizontal). //Compare their slopes to find which starts which bound ... ClipperLocalMinima locMin = new ClipperLocalMinima { Next = null, Y = E.Bot.Y }; if (E.Dx < E.Prev.Dx) { locMin.LeftBound = E.Prev; locMin.RightBound = E; leftBoundIsForward = false; //Q.nextInLML = Q.prev } else { locMin.LeftBound = E; locMin.RightBound = E.Prev; leftBoundIsForward = true; //Q.nextInLML = Q.next } locMin.LeftBound.Side = ClipperEdgeSide.Left; locMin.RightBound.Side = ClipperEdgeSide.Right; if (!Closed) { locMin.LeftBound.WindDelta = 0; } else if (locMin.LeftBound.Next == locMin.RightBound) { locMin.LeftBound.WindDelta = -1; } else { locMin.LeftBound.WindDelta = 1; } locMin.RightBound.WindDelta = -locMin.LeftBound.WindDelta; E = ProcessBound(locMin.LeftBound, leftBoundIsForward); if (E.OutIdx == Skip) { E = ProcessBound(E, leftBoundIsForward); } ClipperTEdge E2 = ProcessBound(locMin.RightBound, !leftBoundIsForward); if (E2.OutIdx == Skip) { E2 = ProcessBound(E2, !leftBoundIsForward); } if (locMin.LeftBound.OutIdx == Skip) { locMin.LeftBound = null; } else if (locMin.RightBound.OutIdx == Skip) { locMin.RightBound = null; } InsertLocalMinima(locMin); if (!leftBoundIsForward) { E = E2; } } return(true); }