Beispiel #1
0
    static public Slice2D Slice(Polygon polygon, List <Vector2f> slice)
    {
        Slice2D result = Slice2D.Create();

        if (slice.Count < 2)
        {
            return(result);
        }

        // Normalize into clockwise
        polygon.Normalize();

        if (Slicer2D.complexSliceType != Slicer2D.SliceType.Regular)
        {
            result = SlicePolygonInside(polygon, slice);
            if (result.polygons.Count > 0)
            {
                return(result);
            }
        }

        // Optimization (holes?)
        // if (polygon.SliceIntersectPoly (slice) == false)
        //	return(result);

        List <List <Vector2f> > slices = new List <List <Vector2f> >();

        bool entered = polygon.PointInPoly(slice.First());

        List <Vector2f> currentSlice = new List <Vector2f> ();

        foreach (Pair2f pair in Pair2f.GetList(slice, false))
        {
            List <Vector2f> stackList = polygon.GetListSliceIntersectPoly(pair);
            stackList = VectorList2f.GetListSortedToPoint(stackList, pair.A);

            foreach (Vector2f id in stackList)
            {
                if (entered == true)
                {
                    currentSlice.Add(id);
                    slices.Add(currentSlice);
                }
                else
                {
                    currentSlice = new List <Vector2f> ();
                    currentSlice.Add(id);
                }
                entered = !entered;
            }

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

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

        foreach (List <Vector2f> id in slices)
        {
            if (id.Count > 1)
            {
                foreach (Vector2f p in id)
                {
                    result.AddCollision(p);
                }

                // Sclice line points generated from intersections list
                Vector2f vec0 = id.First();
                vec0.Push(Vector2f.Atan2(vec0, id[1]), precision);

                Vector2f vec1 = id.Last();
                vec1.Push(Vector2f.Atan2(vec1, id[id.Count - 2]), precision);

                // For each in polygons list attempt convex split
                List <Polygon> temp = new List <Polygon>(result.polygons);               // necessary?
                foreach (Polygon poly in temp)
                {
                    Slice2D resultList = SingleSlice(poly, id);

                    if (resultList.polygons.Count > 0)
                    {
                        foreach (Polygon i in resultList.polygons)
                        {
                            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);
    }
Beispiel #2
0
    // Linear Slice
    static public Slice2D Slice(Polygon polygon, Pair2f slice)
    {
        Slice2D result = Slice2D.Create();

        // Normalize into clockwise
        polygon.Normalize();

        // Getting the list of intersections
        List <Vector2f> intersections = polygon.GetListSliceIntersectPoly(slice);

        // Sorting intersections from one point
        intersections = VectorList2f.GetListSortedToPoint(intersections, slice.A);

        if (intersections.Count < 2)
        {
            return(result);
        }

        List <Pair2f> collisionList = new List <Pair2f>();

        // Dividing intersections into single slices - This method doesn't look like very reliable!!!
        // Optimize this (polygon.PointInPoly) line // Fix this nonsense!!!
        foreach (Pair2f p in Pair2f.GetList(intersections, false))
        {
            if (polygon.PointInPoly(new Vector2f((p.B.GetX() + p.A.GetX()) / 2, (p.B.GetY() + p.A.GetY()) / 2)) == true)
            {
                collisionList.Add(p);
                intersections.Remove(p.A);
                intersections.Remove(p.B);
            }
        }

        result.AddPolygon(polygon);

        // Slice line points generated from intersections list
        foreach (Pair2f id in collisionList)
        {
            result.AddCollision(id.A);
            result.AddCollision(id.B);

            Vector2f vec0 = new Vector2f(id.A);
            Vector2f vec1 = new Vector2f(id.B);

            float rot = Vector2f.Atan2(vec0, vec1);

            // Slightly pushing slice line so it intersect in all cases
            vec0.Push(rot, precision);
            vec1.Push(rot, -precision);

            // For each in polygons list attempt convex split
            foreach (Polygon poly in (new List <Polygon>(result.polygons)))
            {
                Slice2D resultList = SingleSlice(poly, new Pair2f(vec0, vec1));

                if (resultList.polygons.Count > 0)
                {
                    foreach (Polygon i in resultList.polygons)
                    {
                        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);
    }
    // Slice From Point
    static public Slice2D SliceFromPoint(Polygon polygon, Vector2f point, float rotation)
    {
        Slice2D result = Slice2D.Create();

        // Normalize into clockwise
        polygon.Normalize();

        Vector2f sliceA = new Vector2f(point);
        Vector2f sliceB = new Vector2f(point);

        sliceA.Push(rotation, 1e+10f / 2);
        sliceB.Push(rotation, -1e+10f / 2);

        if (polygon.PointInPoly(point) == false)
        {
            return(result);
        }

        // Getting the list of intersections
        List <Vector2f> intersectionsA = polygon.GetListSliceIntersectPoly(new Pair2f(point, sliceA));
        List <Vector2f> intersectionsB = polygon.GetListSliceIntersectPoly(new Pair2f(point, sliceB));

        // Sorting intersections from one point
        if (intersectionsA.Count > 0 && intersectionsB.Count > 0)
        {
            sliceA = VectorList2f.GetListSortedToPoint(intersectionsA, point) [0];
            sliceB = VectorList2f.GetListSortedToPoint(intersectionsB, point) [0];
        }
        else
        {
            return(result);
        }

        List <Pair2f> collisionList = new List <Pair2f>();

        collisionList.Add(new Pair2f(sliceA, sliceB));

        result.AddPolygon(polygon);

        foreach (Pair2f id in collisionList)
        {
            // Sclice line points generated from intersections list
            Vector2f vec0 = new Vector2f(id.A);
            Vector2f vec1 = new Vector2f(id.B);

            float rot = Vector2f.Atan2(vec0, vec1);

            // Slightly pushing slice line so it intersect in all cases
            vec0.Push(rot, LinearSlicer.precision);
            vec1.Push(rot, -LinearSlicer.precision);

            // For each in polygons list attempt convex split
            List <Polygon> temp = new List <Polygon>(result.polygons);           // necessary?
            foreach (Polygon poly in temp)
            {
                // NO, that's the problem
                Slice2D resultList = LinearSlicer.Slice(poly, new Pair2f(vec0, vec1));

                if (resultList.polygons.Count > 0)
                {
                    foreach (Polygon i in resultList.polygons)
                    {
                        result.AddPolygon(i);
                    }

                    // If it's possible to perform splice, remove parent polygon from result list
                    result.RemovePolygon(poly);
                }
            }
        }
        result.RemovePolygon(polygon);
        return(result);
    }