// Linear Slice static public Slice2D Slice(Polygon2D polygon, Pair2D slice) { Slice2D result = Slice2D.Create(null, slice); if (slice == null) { return(result); } // Normalize into clockwise polygon.Normalize(); // Optimization // Skip slicing polygons that are not overlapping with slice Rect sliceRect = Math2D.GetBounds(slice); Rect polygonRect = polygon.GetBounds(); if (sliceRect.Overlaps(polygonRect) == false) { return(result); } result = MultipleSlice(polygon, slice); return(result); }
// Complex Slice static public Slice2D Slice(Polygon2D polygon, List <Vector2D> slice) { Slice2D result = Slice2D.Create(null, slice); if (slice == null) { return(result); } if (slice.Count < 2) { return(result); } // Normalize into clockwise polygon.Normalize(); // Optimization // Skip slicing polygons that are not overlapping with slice Rect sliceRect = Math2D.GetBounds(slice); Rect polygonRect = polygon.GetBounds(); if (sliceRect.Overlaps(polygonRect) == false) { return(result); } // If possible - slice out area if (Slicer2D.complexSliceType != Slicer2D.SliceType.Regular) { result = ComplexSlicerExtended.SlicePolygonInside(polygon, slice); if (result.GetPolygons().Count > 0) { return(result); } } result = MultipleSlice(polygon, slice); return(result); }
public Rect GetBounds() { return(Math2D.GetBounds(pointsList)); }
// Linear Slice static public Slice2D Slice(Polygon2D polygon, Pair2D slice) { Slice2D result = Slice2D.Create(null, slice); // Optimize: Cancel slicing for polygons that are not overlapping Rect sliceRect = Math2D.GetBounds(slice); Rect polygonRect = polygon.GetBounds(); if (sliceRect.Overlaps(polygonRect) == false) { return(result); } // Normalize into clockwise polygon.Normalize(); // Getting the list of intersections List <Vector2D> intersections = polygon.GetListLineIntersectPoly(slice); // Single collision cannot make a proper slice if (intersections.Count < 2) { return(result); } // Sorting intersections from one point intersections = Vector2DList.GetListSortedToPoint(intersections, slice.A); List <Pair2D> collisionList = new List <Pair2D>(); // Dividing intersections into single slices // Optimize this (polygon.PointInPoly) line // This method doesn't look like very reliable!!! foreach (Pair2D p in Pair2D.GetList(intersections, false)) { if (polygon.PointInPoly(new Vector2D((p.B.x + p.A.x) / 2, (p.B.y + p.A.y) / 2)) == true) { collisionList.Add(p); intersections.Remove(p.A); intersections.Remove(p.B); } } result.AddPolygon(polygon); // Slice line points generated from intersections list foreach (Pair2D id in collisionList) { result.AddCollision(id.A); result.AddCollision(id.B); Vector2D vec0 = new Vector2D(id.A); Vector2D vec1 = new Vector2D(id.B); double rot = Vector2D.Atan2(vec0, vec1); // Slightly pushing slice line so it intersect in all cases vec0.Push(rot, precision); vec1.Push(rot, -precision); Pair2D line = new Pair2D(vec0, vec1); // For each in polygons list attempt convex split foreach (Polygon2D poly in (new List <Polygon2D>(result.GetPolygons()))) { Slice2D resultList = SingleSlice(poly, line); if (resultList.slices.Count > 0) { foreach (List <Vector2D> i in resultList.slices) { result.AddSlice(i); } } if (resultList.GetPolygons().Count > 0) { result.AddSlice(line); foreach (Polygon2D i in resultList.GetPolygons()) { result.AddPolygon(i); } // If it's possible to perform splice, remove currently sliced polygon from result list result.RemovePolygon(poly); } } } result.RemovePolygon(polygon); return(result); }
static public Slice2D Slice(Polygon2D polygon, List <Vector2D> slice) { Slice2D result = Slice2D.Create(null, slice); if (slice.Count < 2) { return(result); } // Normalize into clockwise polygon.Normalize(); // Optimize slicing for polygons that are far away Rect sliceRect = Math2D.GetBounds(slice); Rect polygonRect = polygon.GetBounds(); if (sliceRect.Overlaps(polygonRect) == false) { return(result); } // Change if (Slicer2D.complexSliceType != Slicer2D.SliceType.Regular) { result = SlicePolygonInside(polygon, slice); if (result.GetPolygons().Count > 0) { return(result); } } // Optimization (holes?) // if (polygon.SliceIntersectPoly (slice) == false) // return(result); List <List <Vector2D> > slices = new List <List <Vector2D> >(); bool entered = polygon.PointInPoly(slice.First()); List <Vector2D> currentSlice = new List <Vector2D> (); 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) { currentSlice.Add(id); slices.Add(currentSlice); } else { currentSlice = new List <Vector2D> (); currentSlice.Add(id); } entered = !entered; } if (entered == true) { currentSlice.Add(pair.B); } } // Adjusting split lines before performing convex split result.AddPolygon(polygon); foreach (List <Vector2D> id in slices) { if (id.Count > 1) { foreach (Vector2D p in id) { result.AddCollision(p); } // Sclice line points generated from intersections list Vector2D vec0 = id.First(); vec0.Push(Vector2D.Atan2(vec0, id[1]), precision); Vector2D vec1 = id.Last(); vec1.Push(Vector2D.Atan2(vec1, id[id.Count - 2]), precision); // For each in polygons list attempt convex split List <Polygon2D> temp = new List <Polygon2D>(result.GetPolygons()); // necessary? foreach (Polygon2D poly in temp) { Slice2D resultList = SingleSlice(poly, id); if (resultList.slices.Count > 0) { foreach (List <Vector2D> i in resultList.slices) { result.AddSlice(i); } } if (resultList.GetPolygons().Count > 0) { foreach (Polygon2D i in resultList.GetPolygons()) { result.AddPolygon(i); } // If it's possible to perform convex split, remove parent polygon from result list result.RemovePolygon(poly); } } } } result.RemovePolygon(polygon); return(result); }
// Complex Merge static public Merge2D Merge(Polygon2D polygon, List <Vector2D> slice) { Merge2D result = Merge2D.Create(slice); if (slice.Count < 2) { return(result); } // Normalize into clockwise polygon.Normalize(); Rect sliceRect = Math2D.GetBounds(slice); Rect polygonRect = polygon.GetBounds(); if (sliceRect.Overlaps(polygonRect) == false) { return(result); } List <List <Vector2D> > slices = GetSplitSlices(polygon, slice); // Adjusting split lines before performing convex split result.AddPolygon(polygon); foreach (List <Vector2D> id in slices) { if (id.Count < 1) { continue; } foreach (Vector2D p in id) { result.AddCollision(p); } // Sclice line points generated from intersections list Vector2D vec0 = id.First(); vec0.Push(Vector2D.Atan2(vec0, id[1]), precision); // ERROR Vector2D vec1 = id.Last(); vec1.Push(Vector2D.Atan2(vec1, id[id.Count - 2]), precision); // For each in polygons list attempt convex split List <Polygon2D> temp = new List <Polygon2D>(result.polygons); // necessary? foreach (Polygon2D poly in temp) { Merge2D resultList = SingleMerge(poly, id); if (resultList.slices.Count > 0) { foreach (List <Vector2D> i in resultList.slices) { result.AddSlice(i); } } if (resultList.polygons.Count > 0) { foreach (Polygon2D i in resultList.polygons) { result.AddPolygon(i); } // If it's possible to perform convex split, remove parent polygon from result list result.polygons.Remove(poly); } } } return(result); }