Exemple #1
0
        static public List <SlicerSplit> GetSplitSlices(Polygon2D polygon, List <Vector2D> slice)
        {
            bool entered = polygon.PointInPoly(slice.First());

            List <SlicerSplit> slices       = new List <SlicerSplit>();
            SlicerSplit        currentSlice = new SlicerSplit();

            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 SlicerSplit();
                        currentSlice.points.Add(id);
                    }
                    entered = !entered;
                }

                if (entered == true)
                {
                    currentSlice.points.Add(pair.B);
                }
            }

            return(slices);
        }
Exemple #2
0
        static private Slice2D MultipleSlice(Polygon2D polygon, List <Vector2D> slice)
        {
            Slice2D result = Slice2D.Create(null, slice);


            List <SlicerSplit> slices = SlicerSplit.GetSplitSlices(polygon, slice);

            if (slices.Count < 1)
            {
                return(result);
            }

            // Adjusting split lines before performing convex split
            result.AddPolygon(polygon);

            foreach (SlicerSplit 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);
        }