示例#1
0
 public int Cut(PolygonSide a, PolygonSide b, out List <MPolygon> newPolys, int vertexCount)
 {
     if (!a.SameAs(b))
     {
         var newV1 = vertexCount;
         var newV2 = vertexCount + 1;
         var part1 = new List <int> ();
         var part2 = new List <int> ();
         var part3 = new List <int> ();
         if (contains(a.Reversed()))
         {
             a.Reverse();
         }
         if (contains(b.Reversed()))
         {
             b.Reverse();
         }
         List <int> side1 = GetSideWalk(a.right, b.left);
         side1.Add(newV1);
         side1.Add(newV2);
         List <int> side2 = GetSideWalk(b.right, a.left);
         side2.Add(newV2);
         side2.Add(newV1);
         newPolys = new List <MPolygon> ();
         newPolys.Add(new MPolygon(side1));
         newPolys.Add(new MPolygon(side2));
         return(2);
     }
     newPolys = null;
     return(-1);
 }
示例#2
0
 private bool isLHS(MTriangle mTriangle, PolygonSide pairEntry, int vertexExit)
 {
     foreach (var side in mTriangle.GetSides())
     {
         if (pairEntry.SameAs(side))
         {
             if (vertexExit == side.left)
             {
                 return(false);
             }
             else if (vertexExit == side.right)
             {
                 return(true);
             }
         }
     }
     return(false);
 }
示例#3
0
    internal Pair <List <MPolygon> > Cut(List <Vector3> cutPoints, Dictionary <int, PolygonSide> cutEdges, Dictionary <int, int> cutNodes, Dictionary <int, int> cutTriangles)
    {
        var lhs = new List <MPolygon> ();
        var rhs = new List <MPolygon> ();

        for (int i = 1; i < cutPoints.Count + 1; i++)
        {
            PolygonSide pairEntry   = null;
            PolygonSide pairExit    = null;
            int         vertexEntry = -1;
            int         vertexExit  = -1;

            int bIndex     = i - 1;
            var pointEntry = cutPoints[bIndex];
            if (cutEdges.ContainsKey(bIndex))
            {
                pairEntry = cutEdges[bIndex];
            }
            else if (cutNodes.ContainsKey(bIndex))
            {
                vertexEntry = cutNodes[bIndex];
                // Debug.Log ("Vertex entry not null at " + vertexEntry);
            }

            int tIndex    = i == cutPoints.Count ? 0 : i;
            var pointExit = cutPoints[tIndex];
            if (cutEdges.ContainsKey(tIndex))
            {
                pairExit = cutEdges[tIndex];
            }
            else if (cutNodes.ContainsKey(tIndex))
            {
                vertexExit = cutNodes[tIndex];
                // Debug.Log ("Vertex exit not null at " + vertexExit);
            }
            List <MPolygon> newPolys = null;
            var             triangle = -1;
            MPolygon        remove   = null;

            if (pairEntry != null && vertexExit != -1)
            {
                // Debug.Log ("Cut from " + pairEntry + " to " + vertexExit + " at triangle " + tIndex);
                triangle = cutTriangles[tIndex];
                var cutPointIndices = CutUpPolygons(triangle, pairEntry, pointEntry, vertexExit, pointExit, out newPolys, out remove);
                if (cutPointIndices.Count == 1)
                {
                    cutPointVertices[cutPointIndices[0]] = tIndex;
                    lhs.Add(newPolys[1]);
                    rhs.Add(newPolys[0]);
                    if (newPolys[0].Contains(1370))
                    {
                        Debug.Log("p-v Adding " + newPolys[0] + " to rhs and " + newPolys[1] + " to lhs");
                    }
                }
                else
                {
                    // Debug.Log ("Same entry and exit at p-v " + pairEntry + " to " + vertexExit + " with " + cutTriangles[bIndex] + " and " + cutTriangles[tIndex] + " " + isLHS (GetTriangle (triangle), pairEntry, vertexExit));
                    // if (isLHS (GetTriangle (triangle), pairEntry, vertexExit)) {
                    //     lhs.AddRange (polyAtTriangle[cutTriangles[tIndex]]);
                    //     foreach (var item in polyAtTriangle[cutTriangles[tIndex]]) {
                    //         if (item.Contains (1299)) {
                    //             Debug.Log ("p-v Adding " + item);
                    //         }
                    //     }
                    // } else {
                    //     rhs.AddRange (polyAtTriangle[cutTriangles[tIndex]]);
                    // }
                }
            }
            else if (vertexEntry != -1 && pairExit != null)
            {
                // Debug.Log ("Cut from " + vertexEntry + " to " + pairExit);
                triangle = cutTriangles[tIndex];
                var cutPointIndices = CutUpPolygons(triangle, pairExit, pointExit, vertexEntry, pointEntry, out newPolys, out remove);
                if (cutPointIndices.Count == 1)
                {
                    cutPointVertices[cutPointIndices[0]] = tIndex;
                    lhs.Add(newPolys[0]);
                    rhs.Add(newPolys[1]);
                    if (newPolys[1].Contains(1370))
                    {
                        Debug.Log("v-p Adding " + newPolys[1] + " to rhs and " + newPolys[0] + " to lhs");
                    }
                }
                else
                {
                    // Debug.Log ("Same entry and exit at v-p " + pairExit + " to " + vertexEntry + " with " + cutTriangles[bIndex] + " and " + cutTriangles[tIndex] + " " + isLHS (GetTriangle (triangle), pairExit, vertexEntry));
                    // if (isLHS (GetTriangle (triangle), pairExit, vertexEntry)) {
                    //     lhs.AddRange (polyAtTriangle[cutTriangles[tIndex]]);
                    //     foreach (var item in polyAtTriangle[cutTriangles[tIndex]]) {
                    //         if (item.Contains (1299)) {
                    //             Debug.Log ("v-p Adding " + item);
                    //         }
                    //     }
                    // } else {
                    //     rhs.AddRange (polyAtTriangle[cutTriangles[tIndex]]);
                    // }
                }
            }
            else if (vertexEntry != -1 && vertexExit != -1)
            {
                //Nothing
            }
            else if (pairEntry != null && pairExit != null)
            {
                if (!pairEntry.SameAs(pairExit))
                {
                    triangle = cutTriangles[tIndex];
                    var cutPointIndices = CutUpPolygons(triangle, pairEntry, pairExit, pointEntry, pointExit, out newPolys, out remove);
                    if (cutPointIndices.Count == 2)
                    {
                        cutPointVertices[cutPointIndices[0]] = tIndex;
                        cutPointVertices[cutPointIndices[1]] = bIndex;
                        lhs.Add(newPolys[0]);
                        rhs.Add(newPolys[1]);
                        if (newPolys[1].Contains(1335))
                        {
                            Debug.Log("p-p Adding " + newPolys[1] + " to rhs and " + newPolys[0] + " to lhs");
                        }
                    }
                }
                else
                {
                    // Debug.Log ("Same entry and exit at " + pairEntry + " to " + pairExit + " with " + cutTriangles[bIndex] + " and " + cutTriangles[tIndex]);
                    // Misc.DebugSphere (vertices[pairEntry.left], Color.white, "Entry " + pairEntry + " left");
                    // Misc.DebugSphere (vertices[pairEntry.right], Color.white, "Entry " + pairEntry + " right");
                    // rhs.AddRange (polyAtTriangle[cutTriangles[bIndex]]);
                    // foreach (var item in polyAtTriangle[cutTriangles[bIndex]]) {
                    //     if (item.Contains (1297)) {
                    //         Debug.Log ("e-e Adding " + item + " to rhs");
                    //     }
                    // }
                    // lhs.AddRange (polyAtTriangle[cutTriangles[tIndex]]);
                }
            }

            if (newPolys != null)
            {
                polyAtTriangle[triangle].Remove(remove);
                lhs.Remove(remove);
                rhs.Remove(remove);
                polyAtTriangle[triangle].AddRange(newPolys);
            }
        }
        return(new Pair <List <MPolygon> > (lhs, rhs));
    }