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 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);
        }
Пример #3
0
        private void triangulateButton_Click(object sender, EventArgs e)
        {
            this.vertexText.Text = string.Empty;
            var lines     = new List <string>();
            var collector = PolygonTriangulator.CreateTriangleCollector();

            try
            {
                var triangulator = new PolygonTriangulator(this.controller.Polygon);
                var splits       = triangulator.GetSplits();
                lines.Add("Splits");
                lines.AddRange(splits.Select(x => $"{x.Item1} - {x.Item2}"));
                lines.Add("");

                var monotones = Polygon.Split(this.controller.Polygon, splits, PolygonTriangulator.CreateTriangleCollector());
                lines.Add("Monotones");
                lines.AddRange(monotones.SubPolygonIds.Select(x => string.Join(" ", monotones.SubPolygonVertices(x))));
                lines.Add("");

                triangulator.BuildTriangles(collector);
            }
            catch (Exception ex)
            {
                this.vertexText.Text = ex.ToString();
            }

            var triangles = collector.Triangles;

            lines.Add("Triangles");
            for (int i = 0; i < triangles.Length; i += 3)
            {
                lines.Add($"{triangles[i + 0]} {triangles[i + 1]} {triangles[i + 2]} ");
            }

            this.vertexText.Text += string.Join(Environment.NewLine, lines);
            this.polygonPanel.AutoScale();
        }