public void TriangulatePartial()
        {
            var sortedVertices = new[]
            {
                new Vertex(1, 1),       // 0
                new Vertex(1, 3),
                new Vertex(1.5f, 3),    // 2
                new Vertex(2, 2),
                new Vertex(2, 4),       // 4
                new Vertex(2.5f, 1),
                new Vertex(2.5f, 2),    // 6
                new Vertex(2.5f, 3),
                new Vertex(3.5f, 2.5f), // 8
                new Vertex(3.5f, 1),
                new Vertex(4, 1.5f),    // 10
                new Vertex(4, 3.5f),
                new Vertex(4, 4),       // 12
            };

            var polygon = Polygon.Build(sortedVertices)
                          .AddVertices(5, 0, 6, 3, 1, 4, 12, 2, 7, 11, 8, 10, 9)
                          .Close();

            Assert.AreEqual("5 0 6 3 1 4 12 2 7 11 8 10 9", string.Join(" ", polygon.SubPolygonVertices(0)));

            var triangluator = new PolygonTriangulator(polygon);
            var partialEdges = triangluator.GetEdgesAfterPartialTrapezoidation(4).ToArray();

            CollectionAssert.AreEqual(new[] { "1>4", "2<12", "2>7", "3<6", "0>6", "0<5" }, partialEdges);
        }
        public void EdgesWithTwoTouchingPolygons()
        {
            var builder = PlanePolygonBuilder.CreatePolygonBuilder();

            builder.AddEdge(new Vector3(1.363289f, 0.325342f, 0), new Vector3(0.532633f, -0.758319f, 0));
            builder.AddEdge(new Vector3(1.874113f, 0.991756f, 0), new Vector3(1.363289f, 0.325342f, 0));
            builder.AddEdge(new Vector3(-0.884818f, 1.403969f, 0), new Vector3(-0.616898f, 1.753494f, 0));
            builder.AddEdge(new Vector3(-1.009216f, 1.241681f, 0), new Vector3(-0.884818f, 1.403969f, 0));
            builder.AddEdge(new Vector3(-0.256794f, 0.265680f, 0), new Vector3(-1.009216f, 1.241681f, 0));
            builder.AddEdge(new Vector3(0.532633f, -0.758319f, 0), new Vector3(-0.256793f, 0.265680f, 0));
            builder.AddEdge(new Vector3(0.989307f, 2.139478f, 0), new Vector3(1.874113f, 0.991756f, 0));
            builder.AddEdge(new Vector3(-0.616898f, 1.753494f, 0), new Vector3(-0.145745f, 1.866717f, 0));
            builder.AddEdge(new Vector3(-0.145745f, 1.866717f, 0), new Vector3(0.779111f, 2.088966f, 0));
            builder.AddEdge(new Vector3(0.779111f, 2.088966f, 0), new Vector3(0.989307f, 2.139478f, 0));
            builder.AddEdge(new Vector3(0.951598f, 1.471772f, 0), new Vector3(0.935504f, 1.772997f, 0));
            builder.AddEdge(new Vector3(1.010642f, 0.366718f, 0), new Vector3(0.951598f, 1.471772f, 0));
            builder.AddEdge(new Vector3(0.935504f, 1.772997f, 0), new Vector3(-0.145745f, 1.866717f, 0));
            builder.AddEdge(new Vector3(-0.094393f, 0.905628f, 0), new Vector3(-0.070607f, 0.460440f, 0));
            builder.AddEdge(new Vector3(-0.145745f, 1.866717f, 0), new Vector3(-0.094393f, 0.905628f, 0));
            builder.AddEdge(new Vector3(0.449942f, 0.415320f, 0), new Vector3(1.010642f, 0.366718f, 0));
            builder.AddEdge(new Vector3(-0.070607f, 0.460440f, 0), new Vector3(0.449942f, 0.415320f, 0));

            var polygon = builder.BuildPolygon().Polygon;

            Assert.AreEqual(1, polygon.SubPolygonIds.Count());

            var triangulator = new PolygonTriangulator(polygon);
            var splits       = triangulator.GetSplits();

            Assert.IsNotNull(splits);

            var triangles = triangulator.BuildTriangles();

            Assert.IsNotNull(triangles);
        }
        public void SplitAtJoin2()
        {
            var sortedVertices = new[]
            {
                new Vertex(0.0f, 0.0f), // 0 0
                new Vertex(1.0f, 0.5f), // 6 1
                new Vertex(1.5f, 1.0f), // 7 2
                new Vertex(2.0f, 0.0f), // 4 3
                new Vertex(3.0f, 0.5f), // 5 4
                new Vertex(3.5f, 0.0f), // 3 5
                new Vertex(4.0f, 4.0f), // 1 6
                new Vertex(5.0f, 0.0f), // 2 7
            };

            var polygon1 = Polygon.Build(sortedVertices)
                           .AddVertices(3, 0, 6, 7, 5)
                           .ClosePartialPolygon()
                           .AddVertices(4, 2, 1)
                           .Close();

            var triangleCollector = PolygonTriangulator.CreateTriangleCollector();

            var triangluator = new PolygonTriangulator(polygon1);
            var splits       = string.Join(" ", triangluator.GetSplits().OrderBy(x => x.Item1).ThenBy(x => x.Item2).Select(x => $"{x.Item1}-{x.Item2}"));

            Assert.AreEqual("0-1 1-3 3-4 4-5 5-6", splits);

            var specialSplits = new[] { (4, 5), (3, 4), (0, 1), (1, 3), (5, 6) }
        public void InnerPolygonTopLeft()
        {
            var sortedVertices = new[]
            {
                new Vertex(0, 0),
                new Vertex(1, 2),
                new Vertex(1, 3),  // 2
                new Vertex(2, 2),
                new Vertex(3, 3),  // 4
                new Vertex(4, 2),
                new Vertex(5, 2),
                new Vertex(5, 3),  // 7
                new Vertex(6, 1),
            };

            var polygon = Polygon.Build(sortedVertices)
                          .AddVertices(0, 2, 4, 7, 8)
                          .ClosePartialPolygon()
                          .AddVertices(4, 1, 3)
                          .Close(4);

            var triangluator = new PolygonTriangulator(polygon);
            var splits       = string.Join(" ", triangluator.GetSplits().OrderBy(x => x.Item1).ThenBy(x => x.Item2).Select(x => $"{x.Item1}-{x.Item2}"));

            Assert.AreEqual("0-1 1-2", splits);
        }
        public void FusionWithClosingAboveTransition()
        {
            var vertices = new[]
            {
                new Vertex(-2.04168500f, 0.05391948f),
                new Vertex(-1.73525800f, -0.39220070f),
                new Vertex(-1.59090200f, 0.81340110f),
                new Vertex(-1.28913800f, -1.04169800f),
                new Vertex(-1.18307500f, 1.50051100f),
                new Vertex(-1.16461800f, -0.62563610f),
                new Vertex(-1.13713300f, 0.22282370f),
                new Vertex(-1.10918100f, 1.08569700f),
                new Vertex(-0.98826190f, -1.09449200f),
                new Vertex(-0.96441320f, 1.46214300f),
                new Vertex(-0.96191410f, 1.11436200f),
                new Vertex(-0.94439380f, -0.58277110f),
                new Vertex(-0.38140370f, -1.20097700f),
                new Vertex(-0.24038320f, -1.22572200f),
                new Vertex(-0.07887594f, -0.41430300f),
                new Vertex(-0.02343902f, 1.29703000f),
                new Vertex(0.27925600f, -0.35023050f),
                new Vertex(0.44308910f, 1.21516900f),
                new Vertex(0.85362760f, 0.61747490f),
            };

            var polygon = Polygon.Build(vertices)
                          .AddVertices(11, 14, 15, 17, 18, 16, 13, 12, 8, 3, 1, 0, 2, 4, 9, 15, 10, 7, 6, 5)
                          .ClosePartialPolygon()
                          .Close(15);

            var triangluator = new PolygonTriangulator(polygon);
            var splits       = string.Join(" ", triangluator.GetSplits().OrderBy(x => x.Item1).ThenBy(x => x.Item2).Select(x => $"{x.Item1}-{x.Item2}"));

            Assert.AreEqual("1-2 2-3 3-4 4-5 5-8 7-9 8-11 9-10 11-12 13-14 15-16 16-17", splits);
        }
    protected override void OnPopulateMesh(VertexHelper vh)
    {
        Vector2[] points = null;
        Mesh      mesh   = new Mesh();

        var rect = rectTransform.rect;

        Collider2dPointsGetter.GetCircleCoordinates(rect.center, CalcRadius(), ref points, Proximity);
        //Collider2dPointsGetter.GetCircleCoordinates(rectTransform.anchoredPosition, 1f, ref points, Proximity);

        float ppu = (Camera.main.orthographicSize * 2) / Screen.height * 300;

        PolygonTriangulator.TriangulateAsLine(points, mesh, ppu, true);

        vh.Clear();

        for (int i = 0; i < mesh.vertices.Length; i++)
        {
            vh.AddVert(mesh.vertices[i], new Color32(1, 1, 1, 1), new Vector2(0.5f, 0.5f));
        }

        for (int i = 0; i < mesh.triangles.Length; i += 3)
        {
            vh.AddTriangle(mesh.triangles[i], mesh.triangles[i + 1], mesh.triangles[i + 2]);
        }
    }
        public void TriangulateForm1()
        {
            var sortedVertices = new[]
            {
                new Vertex(1, 1),       // 0
                new Vertex(1, 3),
                new Vertex(1.5f, 3),    // 2
                new Vertex(2, 2),
                new Vertex(2, 4),       // 4
                new Vertex(2.5f, 1),
                new Vertex(2.5f, 2),    // 6
                new Vertex(2.5f, 3),
                new Vertex(3.5f, 2.5f), // 8
                new Vertex(3.5f, 1),
                new Vertex(4, 1.5f),    // 10
                new Vertex(4, 3.5f),
                new Vertex(4, 4),       // 12
            };

            var polygon = Polygon.Build(sortedVertices)
                          .AddVertices(5, 0, 6, 3, 1, 4, 12, 2, 7, 11, 8, 10, 9)
                          .Close();

            Assert.AreEqual("5 0 6 3 1 4 12 2 7 11 8 10 9", string.Join(" ", polygon.SubPolygonVertices(0)));

            var triangluator = new PolygonTriangulator(polygon);
            var splits       = string.Join(" ", triangluator.GetSplits().OrderBy(x => x.Item1).ThenBy(x => x.Item2).Select(x => $"{x.Item1}-{x.Item2}"));

            Assert.AreEqual("1-2 2-3 2-4 5-6 6-7 7-8 8-9", splits);

            var triangles = triangluator.BuildTriangles();

            Assert.IsTrue(VerifyTriangle(triangles, 0, 6, 5));
            Assert.IsTrue(VerifyTriangle(triangles, 1, 2, 3));
            Assert.IsTrue(VerifyTriangle(triangles, 1, 4, 2));
            Assert.IsTrue(VerifyTriangle(triangles, 2, 7, 3));
            Assert.IsTrue(VerifyTriangle(triangles, 2, 4, 12));
            Assert.IsTrue(VerifyTriangle(triangles, 3, 7, 6));
            Assert.IsTrue(VerifyTriangle(triangles, 5, 6, 8));
            Assert.IsTrue(VerifyTriangle(triangles, 5, 8, 9));
            Assert.IsTrue(VerifyTriangle(triangles, 6, 7, 8));
            Assert.IsTrue(VerifyTriangle(triangles, 7, 11, 8));
            Assert.IsTrue(VerifyTriangle(triangles, 8, 10, 9));
            Assert.AreEqual(11 * 3, triangles.Length);
        }
示例#8
0
    public void SpawnCustomObject(Vector2[] points, Vector2[] colliderPoints, Color color)
    {
        Debug.Log("spawning custom object");

        GameObject go     = new GameObject();
        MeshFilter filter = go.AddComponent <MeshFilter>();

        filter.mesh = ExtrudeMeshFromPoints(points);

        go.name = "Junk";
        go.AddComponent <Rigidbody>();
        MeshRenderer r = go.AddComponent <MeshRenderer>();

        r.material = customObjectMaterial;
        int isConvex = PolygonTriangulator.IsConvex(asList(colliderPoints));

        if (isConvex == 1)
        {
            Debug.Log("CONVEX");
            MeshCollider col = go.AddComponent <MeshCollider>();
            col.sharedMesh = ExtrudeMeshFromPoints(colliderPoints);
            col.convex     = true;
        }
        else if (isConvex == -1)
        {
            Debug.Log("CONCAVE");
            attachCollisionObjects(go, colliderPoints);
        }
        else
        {
            Debug.Log("WTF");
        }

        go.AddComponent <Constructable> ();

        TurnIntoPlayerMadeObject(go, color, 0);
    }
        public string[] BuildTrapezoidationDebug()
        {
            var lines          = new List <string>();
            var trapezoidation = new PolygonTriangulator(this.Polygon);
            int limit          = 0;

            foreach (var vertexInfo in this.Polygon.OrderedVertices)
            {
                try
                {
                    lines.Add($"{vertexInfo.Prev}>{vertexInfo.Id}>{vertexInfo.Next}");
                    lines.AddRange(trapezoidation.GetEdgesAfterPartialTrapezoidation(++limit));
                    lines.Add(string.Empty);
                }
                catch (Exception e)
                {
                    lines.Add(string.Empty);
                    lines.Add(e.ToString());
                    break;
                }
            }

            return(lines.ToArray());
        }