static public List <ComplexSlicerSplit> GetSplitSlices(Polygon2D polygon, List <Vector2D> slice) { bool entered = polygon.PointInPoly(slice.First()); List <ComplexSlicerSplit> slices = new List <ComplexSlicerSplit>(); ComplexSlicerSplit currentSlice = new ComplexSlicerSplit(); Pair2D pair = Pair2D.Zero(); for (int i = 0; i < slice.Count - 1; i++) { pair.A = slice[i]; pair.B = slice[i + 1]; List <Vector2D> stackList = polygon.GetListLineIntersectPoly(pair); stackList = Vector2DList.GetListSortedToPoint(stackList, pair.A); foreach (Vector2D id in stackList) { if (entered == true) { Vector2D last = null; if (currentSlice.points.Count > 0) { last = (Vector2D)currentSlice.points.Last(); } // Goes through a same point if (last != null && last.ToVector2().Equals(id.ToVector2()) == true) { Debug.LogWarning("Slicer2D: Slicing through the point"); currentSlice.type = Type.SingleVertexCollision; continue; } currentSlice.points.Add(id); slices.Add(currentSlice); } else { currentSlice = new ComplexSlicerSplit(); currentSlice.points.Add(id); } entered = !entered; } if (entered == true) { currentSlice.points.Add(pair.B); } } return(slices); }
static private Slice2D MultipleSlice(Polygon2D polygon, List <Vector2D> slice) { Slice2D result = Slice2D.Create(null, slice); List <ComplexSlicerSplit> slices = ComplexSlicerSplit.GetSplitSlices(polygon, slice); if (slices.Count < 1) { return(result); } // Adjusting split lines before performing convex split result.AddPolygon(polygon); foreach (ComplexSlicerSplit id in slices) { if (id.points.Count > 1) { foreach (Vector2D p in id.points) { result.AddCollision(p); } // Sclice line points generated from intersections list Vector2D vec0 = id.points.First(); vec0.Push(Vector2D.Atan2(vec0, id.points[1]), precision); Vector2D vec1 = id.points.Last(); vec1.Push(Vector2D.Atan2(vec1, id.points[id.points.Count - 2]), precision); // For each in polygons list attempt convex split List <Polygon2D> resultPolygons = new List <Polygon2D>(result.GetPolygons()); // necessary? foreach (Polygon2D poly in resultPolygons) { Slice2D resultList = SingleSlice(poly, id); if (resultList.GetPolygons().Count > 0) { if (resultList.slices.Count > 0) { foreach (List <Vector2D> resultSlice in resultList.slices) { result.AddSlice(resultSlice); } } foreach (Polygon2D resultPoly in resultList.GetPolygons()) { result.AddPolygon(resultPoly); } // If it's possible to perform convex split, remove parent polygon from result list result.RemovePolygon(poly); } } } } //Debug.Log("1 " + slices[0].Count + " " + slices[0][0].ToVector2() + " " + slices[0][1].ToVector2()); //Debug.Log("2 " + slices[1].Count + " " + slices[1][0].ToVector2() + " " + slices[1][1].ToVector2()); // if exist then remove slice was not performed result.RemovePolygon(polygon); return(result); }
static private Slice2D SingleSlice(Polygon2D polygon, ComplexSlicerSplit split) { Slice2D result = Slice2D.Create(null, split.points); // Change if (Slicer2D.complexSliceType == Slicer2D.SliceType.Regular) { if (Math2D.SliceIntersectItself(split.points)) { if (Slicer2D.Debug.enabled) { Debug.LogWarning("Slicer2D: Slice Intersect Itself In Regular Mode"); } return(result); } } // Start and End of slice should be outside the polygon if (polygon.PointInPoly(split.points.First()) == true || polygon.PointInPoly(split.points.Last()) == true) { return(result); } List <Vector2D> slice = new List <Vector2D> (split.points); if (split.type == ComplexSlicerSplit.Type.SingleVertexCollision) { Debug.Log("Single Vertex Collision"); return(result); } ComplexCollision collisionSlice = new ComplexCollision(polygon, slice); if (collisionSlice.error) { if (Slicer2D.Debug.enabled) { Debug.LogWarning("Slicer2D: Complex Collision Error"); } return(result); } List <Polygon2D> intersectHoles = polygon.GetListSliceIntersectHoles(slice); switch (intersectHoles.Count) { case 0: if (collisionSlice.collisionCount == 2) { return(SliceWithoutHoles.Slice(polygon, slice, collisionSlice)); } break; case 1: return(SliceWithOneHole.Slice(polygon, slice, collisionSlice)); case 2: return(SliceWithTwoHoles.Slice(polygon, slice, collisionSlice)); default: break; } return(result); }