public static void CutPaths(ref MapPoint[] points, ref PathSegment[] segments)
        {
            List <PathSegment> list          = new List <PathSegment>();
            List <MapPoint>    list2         = new List <MapPoint>();
            PolygonCutter      polygonCutter = new PolygonCutter();
            int num = 0;

            PathSegment[] array = segments;
            for (int i = 0; i < array.Length; i++)
            {
                PathSegment pathSegment = array[i];
                if (pathSegment.Length > 0)
                {
                    polygonCutter.Load(points, num, pathSegment.Length, PolygonClosingPole.North);
                    foreach (PolygonPart path in polygonCutter.GetPaths())
                    {
                        PathSegment item = default(PathSegment);
                        item.Type   = pathSegment.Type;
                        item.Length = path.Points.Count;
                        list.Add(item);
                        list2.AddRange(path.Points);
                    }
                }
                num += pathSegment.Length;
            }
            segments = list.ToArray();
            points   = list2.ToArray();
        }
        public static void CutShapes(ref MapPoint[] points, ref ShapeSegment[] segments)
        {
            List <ShapeSegment> list          = new List <ShapeSegment>();
            List <MapPoint>     list2         = new List <MapPoint>();
            PolygonCutter       polygonCutter = new PolygonCutter();
            int num = 0;

            ShapeSegment[] array = segments;
            for (int i = 0; i < array.Length; i++)
            {
                ShapeSegment segment = array[i];
                if (segment.Length > 0)
                {
                    bool flag = false;
                    List <ShapeSegment> list3 = default(List <ShapeSegment>);
                    List <MapPoint>     list4 = default(List <MapPoint>);
                    polygonCutter.ProcessShapeSegment(segment, (IList <MapPoint>)points, num, PolygonClosingPole.North, out list3, out list4, out flag);
                    if (flag)
                    {
                        List <ShapeSegment> list5 = default(List <ShapeSegment>);
                        List <MapPoint>     list6 = default(List <MapPoint>);
                        polygonCutter.ProcessShapeSegment(segment, (IList <MapPoint>)points, num, PolygonClosingPole.South, out list5, out list6, out flag);
                        ShapeSegment[] array2 = list3.ToArray();
                        MapPoint[]     array3 = list4.ToArray();
                        GeoUtils.CalculateSignedArea(ref array3, ref array2);
                        double num2 = 0.0;
                        for (int j = 0; j < array2.Length; j++)
                        {
                            num2 += Math.Abs(array2[j].PolygonSignedArea);
                        }
                        ShapeSegment[] array4 = list5.ToArray();
                        MapPoint[]     array5 = list6.ToArray();
                        GeoUtils.CalculateSignedArea(ref array5, ref array4);
                        double num3 = 0.0;
                        for (int k = 0; k < array4.Length; k++)
                        {
                            num3 += Math.Abs(array4[k].PolygonSignedArea);
                        }
                        if (num2 < num3)
                        {
                            list.AddRange(list3);
                            list2.AddRange(list4);
                        }
                        else
                        {
                            list.AddRange(list5);
                            list2.AddRange(list6);
                        }
                    }
                    else
                    {
                        list.AddRange(list3);
                        list2.AddRange(list4);
                    }
                }
                num += segment.Length;
            }
            segments = list.ToArray();
            points   = list2.ToArray();
        }
        public static void CutShapes(ref MapPoint[] points, ref ShapeSegment[] segments)
        {
            List <ShapeSegment> list          = new List <ShapeSegment>();
            List <MapPoint>     list2         = new List <MapPoint>();
            PolygonCutter       polygonCutter = new PolygonCutter();
            int num = 0;

            ShapeSegment[] array = segments;
            for (int i = 0; i < array.Length; i++)
            {
                ShapeSegment segment = array[i];
                if (segment.Length > 0)
                {
                    bool isClosedAtPole = false;
                    polygonCutter.ProcessShapeSegment(segment, points, num, PolygonClosingPole.North, out List <ShapeSegment> segments2, out List <MapPoint> segmentPoints, out isClosedAtPole);
                    if (isClosedAtPole)
                    {
                        polygonCutter.ProcessShapeSegment(segment, points, num, PolygonClosingPole.South, out List <ShapeSegment> segments3, out List <MapPoint> segmentPoints2, out isClosedAtPole);
                        ShapeSegment[] segments4 = segments2.ToArray();
                        MapPoint[]     points2   = segmentPoints.ToArray();
                        CalculateSignedArea(ref points2, ref segments4);
                        double num2 = 0.0;
                        for (int j = 0; j < segments4.Length; j++)
                        {
                            num2 += Math.Abs(segments4[j].PolygonSignedArea);
                        }
                        ShapeSegment[] segments5 = segments3.ToArray();
                        MapPoint[]     points3   = segmentPoints2.ToArray();
                        CalculateSignedArea(ref points3, ref segments5);
                        double num3 = 0.0;
                        for (int k = 0; k < segments5.Length; k++)
                        {
                            num3 += Math.Abs(segments5[k].PolygonSignedArea);
                        }
                        if (num2 < num3)
                        {
                            list.AddRange(segments2);
                            list2.AddRange(segmentPoints);
                        }
                        else
                        {
                            list.AddRange(segments3);
                            list2.AddRange(segmentPoints2);
                        }
                    }
                    else
                    {
                        list.AddRange(segments2);
                        list2.AddRange(segmentPoints);
                    }
                }
                num += segment.Length;
            }
            segments = list.ToArray();
            points   = list2.ToArray();
        }
Exemple #4
0
    public IEnumerator Cut(MPath path)
    {
        while (buildingGraph)
        {
            yield return(new WaitForSeconds(0.01f));
        }

        DebugTime("Total Cut");
        Vector3 normal = path.GetNormal(0);

        path.InsertPositionAt(0, path.dotFrom.transform.position);
        path.InsertNormalAt(0, normal);
        path.AddPosition(path.dotTo.transform.position);
        path.AddNormal(normal);

        DebugTime("Identify Cuts");
        mesh = manifold.gameObject.GetComponent <MeshFilter> ().mesh;
        var vertices = mesh.vertices.ToList();

        meshTriangles = mesh.triangles.ToList();
        meshNormals   = mesh.normals.ToList();

        // Misc.DebugSphere (vertices[1299], Color.blue, "1299");
        // Misc.DebugSphere (vertices[1334], Color.blue, "1334");
        // Misc.DebugSphere (vertices[1335], Color.blue, "1335");
        // DebugAtTriangle (2596, Color.white, "2596");
        // DebugAtTriangle (2597, Color.white, "2597");
        var cutPointIdentifier = new CutPointIdentifier(g, manifold);

        cutPointIdentifier.IdentifyCuts(path);

        var cutPoints     = cutPointIdentifier.cutPoints;
        var cutEdges      = cutPointIdentifier.cutEdges;
        var cutPointType  = cutPointIdentifier.cutPointType;
        var cutTriangles  = cutPointIdentifier.cutTriangles;
        var cutAlongEdges = cutPointIdentifier.cutAlongEdges;
        var cutNodes      = cutPointIdentifier.cutNodes;

        DebugTime("Identify Cuts");
        Debug.Log("Identification done, resulting in " + cutPoints.Count + " cut Points.");

        List <HashSet <int> > bounds;
        List <HashSet <int> > meshVertexPres;

        GetNewMeshVertices(vertices, cutNodes, cutAlongEdges, cutEdges, out bounds, out meshVertexPres);
        Debug.Log("GetBounds done, resulting in " + bounds.Count + " bounds.");
        int numComponents = meshVertexPres.Count;

        DebugTime("Set up Polygons");
        var polyAtTriangle = new Dictionary <int, List <MPolygon> > ();

        foreach (var i in cutTriangles.Keys)
        {
            var index = cutTriangles[i];

            polyAtTriangle[index] = new List <MPolygon> ();
            var list = new MPolygon();
            list.Add(meshTriangles[index * 3]);
            list.Add(meshTriangles[index * 3 + 1]);
            list.Add(meshTriangles[index * 3 + 2]);
            polyAtTriangle[index].Add(list);
        }
        DebugTime("Set up Polygons");
        // foreach (var item in cutTriangles.Keys) {
        //     DebugAtTriangle (cutTriangles[item], Color.red, item + "");
        // }
        DebugTime("Cut up Polygons");
        oldVertexNumber = vertices.Count;
        var            polygonCutter       = new PolygonCutter(meshTriangles, meshNormals, vertices, polyAtTriangle);
        var            sidesOfCut          = polygonCutter.Cut(cutPoints, cutEdges, cutNodes, cutTriangles);
        List <Vector3> normals             = polygonCutter.normals;
        var            cutPointsToVertices = polygonCutter.cutPointVertices;

        DebugTime("Cut up Polygons");
        // for (int i = 0; i < cutPoints.Count; i++) {
        //     Vector3 cp = cutPoints[i];
        //     Misc.DebugSphere (cp, Color.red, i + "cp");
        // }

        DebugTime("Triangulate");
        var to = new TriangleOrdering(vertices, polyAtTriangle, bounds, cutNodes, cutPointsToVertices, sidesOfCut);
        Pair <HashSet <int> > sideBounds;
        var polygons = to.orderTriangles(out sideBounds);

        DebugTime("Triangulate");
        Debug.Log("Triangulation done, resulting in " + polygons.Count + " lists of polygons.");

        // foreach (var item in sidesOfCut.left) {
        //     DebugAtPolygon (vertices, item, Color.green);
        // }
        // foreach (var item in sidesOfCut.right) {
        //     DebugAtPolygon (vertices, item, Color.red);
        // }

        var newMeshVertexPres = new List <HashSet <int> > ();

        newMeshVertexPres.Add(new HashSet <int> ());
        newMeshVertexPres.Add(new HashSet <int> ());
        foreach (var bound in sideBounds.left)
        {
            newMeshVertexPres[0].UnionWith(meshVertexPres[bound]);
        }
        foreach (var bound in sideBounds.right)
        {
            newMeshVertexPres[1].UnionWith(meshVertexPres[bound]);
        }
        numComponents = 2;
        if (newMeshVertexPres[0].SetEquals(newMeshVertexPres[1]))
        {
            numComponents = 1;
            polygons[0].AddRange(polygons[1]);
        }
        DebugTime("GetTriangles");
        List <List <int> >     meshts;
        List <List <Vector3> > meshvs, meshns;

        SetUpNewMeshes(vertices, cutTriangles, newMeshVertexPres, out meshts, out meshvs, out meshns);
        DebugTime("Set Normals and Triangles");

        DebugTime("Set up manifolds");
        SetUpManifolds(vertices, meshts, meshvs, meshns, normals, polygons, cutPoints, numComponents);
        SetSwitchedManifolds();
        DebugTime("Set up manifolds");  //SLOW
        DebugTime("Total Cut");
    }