Exemplo n.º 1
0
        private void StartDelaunayAsync(List <Vertex> vertices)
        {
            new Thread(() =>
            {
                int length = vertices.Count;
                if (vertices.Count < 3)
                {
                    return;
                }

                Debug.Log("Vertices Count : " + vertices.Count);

                Triangle tri = CreateSuperTriangle(vertices);
                Debug.Log(tri.toString());

                vertices.AddRange(tri.ToVertices());

                //            List<Edge> buff = new List<Edge>();
                //            List<Triangle> triangles = new List<Triangle>{tri};

                List <int> buff     = new List <int>();
                List <Circle> open  = new List <Circle>();
                List <Circle> close = new List <Circle>();
                open.Add(Circumcircle(vertices, length, length + 1, length + 2));
                for (int i = 0; i < vertices.Count; i++)
                {
                    if (i % 100 == 0)
                    {
                        Debug.Log((float)i / length);
                    }
                    for (int j = open.Count - 1; j >= 0; j--)
                    {
                        int dx = vertices[i].x - open[j].point.x;
                        if (dx > 0 && dx * dx > open[j].r * open[j].r)
                        {
                            close.Add(open[j]);
                            open.RemoveAt(j);
                            continue;
                        }

                        int dy = vertices[i].y - open[j].point.y;
                        if (dx * dx + dy * dy - open[j].r * open[j].r > 0)
                        {
                            continue;
                        }

                        buff.Add(open[j].i1);
                        buff.Add(open[j].i2);
                        buff.Add(open[j].i2);
                        buff.Add(open[j].i3);
                        buff.Add(open[j].i3);
                        buff.Add(open[j].i1);
                    }

                    RemoveTheSameEdges(buff);

                    for (int d = buff.Count; d > 0;)
                    {
                        open.Add(Circumcircle(vertices, buff[--d], buff[--d], i));
                    }
                }

                close.AddRange(open);

                finalTriangles = new List <Triangle>();

                for (int i = close.Count - 1; i >= 0; i--)
                {
                    if (close[i].i1 < length && close[i].i2 < length && close[i].i3 < length)
                    {
                        Triangle t = new Triangle
                        {
                            v1 = new Vertex {
                                x = vertices[close[i].i1].x, y = vertices[close[i].i1].y
                            },
                            v2 = new Vertex {
                                x = vertices[close[i].i2].x, y = vertices[close[i].i2].y
                            },
                            v3 = new Vertex {
                                x = vertices[close[i].i3].x, y = vertices[close[i].i3].y
                            }
                        };

                        finalTriangles.Add(t);
                    }
                }

                Debug.Log(finalTriangles.Count);
                isFinish = true;
            }).Start();
        }
Exemplo n.º 2
0
        public void Delaunay(Texture2D texture2D)
        {
            List <Vertex> vertices = ToVertexList(texture2D);

            //   vertices = SelectVertex(vertices);
            vertices = vertixSelect.SelectVertices(vertices, noneedgeVertices, texture2D, triangleCount);
            //    return;
            // StartDelaunay(vertices);

            //StartCoroutine(InitTexture(texture2D));
            int length = vertices.Count;

            if (vertices.Count < 3)
            {
                return;
            }

            Debug.Log("Vertices Count : " + vertices.Count);

            Triangle tri = CreateSuperTriangle(vertices);

            Debug.Log(tri.toString());

            vertices.AddRange(tri.ToVertices());
            tri.i1 = length;
            tri.i2 = length + 1;
            tri.i3 = length + 2;
            List <Triangle> triangles = new List <Triangle> {
                tri
            };
            //   triangles.Add(tri);
            List <Edge> edges = new List <Edge>();

            for (int i = 0; i < vertices.Count; i++)
            {
                edges.Clear();

                for (int j = triangles.Count - 1; j >= 0; j--)
                {
                    if (IsInCircumcircle(vertices[i], triangles[j]))
                    {
                        edges.Add(new Edge
                        {
                            v1 = triangles[j].v1,
                            v2 = triangles[j].v2,
                            i1 = triangles[j].i1,
                            i2 = triangles[j].i2
                        });
                        edges.Add(new Edge
                        {
                            v1 = triangles[j].v1,
                            v2 = triangles[j].v3,
                            i1 = triangles[j].i1,
                            i2 = triangles[j].i3
                        });
                        edges.Add(new Edge
                        {
                            v1 = triangles[j].v2,
                            v2 = triangles[j].v3,
                            i1 = triangles[j].i2,
                            i2 = triangles[j].i3
                        });

                        triangles.RemoveAt(j);
                    }
                }

                for (int j = edges.Count - 1; j >= 0; j--)
                {
                    for (int k = j - 1; k >= 0; k--)
                    {
                        if (edges[j] == edges[k])
                        {
                            edges.RemoveAt(j);
                            edges.RemoveAt(k);
                            j--;
                            break;
                        }
                    }
                }

                for (int j = 0; j < edges.Count; j++)
                {
                    Triangle tempTriangle = new Triangle
                    {
                        v1 = edges[j].v1,
                        v2 = edges[j].v2,
                        v3 = vertices[i],
                        i1 = edges[j].i1,
                        i2 = edges[j].i2,
                        i3 = i
                    };
                    triangles.Add(tempTriangle);
                }
            }



            for (int i = 0; i < triangles.Count; i++)
            {
                if (triangles[i].i1 < length && triangles[i].i2 < length && triangles[i].i3 < length)
                {
                    finalTriangles.Add(triangles[i]);
                }
            }


            Debug.Log(finalTriangles.Count);

            InitTextureSync(texture2D);
            drawTriangle.Draw(finalTriangles, source);
            img.SetNativeSize();
        }