Exemplo n.º 1
0
 public Triangle(IdxVertex p1, IdxVertex p2, IdxVertex p3)
 {
     this.p1 = p1;
     this.p2 = p2;
     this.p3 = p3;
     makeFacingUp();
     e1 = new Edge(p1, p2);
     e2 = new Edge(p2, p3);
     e3 = new Edge(p3, p1);
 }
Exemplo n.º 2
0
    void makeFacingUp()
    {
        var surfaceNormal = Vector3.Cross(p2.vertex - p1.vertex, p3.vertex - p1.vertex);

        if (surfaceNormal.y < 0f)
        {
            var tmp = p2;
            p2 = p3;
            p3 = tmp;
        }
    }
Exemplo n.º 3
0
    public int[] triangulate(Vector3[] vertices)
    {
        float minX = vertices[0].x;
        float minY = vertices[0].z;
        float maxX = minX;
        float maxY = minY;

        foreach (Vector3 point in vertices)
        {
            if (point.x < minX)
            {
                minX = point.x;
            }
            if (point.z < minY)
            {
                minY = point.z;
            }
            if (point.x > maxX)
            {
                maxX = point.x;
            }
            if (point.z > maxY)
            {
                maxY = point.z;
            }
        }

        float dx       = maxX - minX;
        float dy       = maxY - minY;
        float deltaMax = Mathf.Max(dx, dy);
        float midx     = (minX + maxX) / 2.0f;
        float midy     = (minY + maxY) / 2.0f;

        IdxVertex p1 = new IdxVertex(new Vector3(midx - 20 * deltaMax, 0.0f, midy - deltaMax));
        IdxVertex p2 = new IdxVertex(new Vector3(midx, 0.0f, midy + 20 * deltaMax));
        IdxVertex p3 = new IdxVertex(new Vector3(midx + 20 * deltaMax, 0.0f, midy - deltaMax));

        trianglesArray.Add(new Triangle(p1, p2, p3)); // idx ??

        for (int i = 0; i < vertices.Length; i++)
        {
            IdxVertex p = new IdxVertex(vertices[i], i);

            HashSet <Triangle>       badTrianglesArray = new HashSet <Triangle>();
            Dictionary <float, Edge> polygon           = new Dictionary <float, Edge>();

            foreach (Triangle t in trianglesArray)
            {
                if (t.circumCircleContains(p.vertex))
                {
                    badTrianglesArray.Add(t);
                    float key1 = t.e1.getKey();
                    float key2 = t.e2.getKey();
                    float key3 = t.e3.getKey();
                    if (!polygon.ContainsKey(key1))
                    {
                        polygon.Add(key1, t.e1);
                    }
                    else
                    {
                        polygon.Remove(key1);
                    }

                    if (!polygon.ContainsKey(key2))
                    {
                        polygon.Add(key2, t.e2);
                    }
                    else
                    {
                        polygon.Remove(key2);
                    }

                    if (!polygon.ContainsKey(key3))
                    {
                        polygon.Add(key3, t.e3);
                    }
                    else
                    {
                        polygon.Remove(key3);
                    }
                }
            }

            foreach (Triangle bt in badTrianglesArray)
            {
                trianglesArray.Remove(bt);
            }

            HashSet <Triangle> tempTriangles = new HashSet <Triangle>();
            foreach (var e in polygon)
            {
                tempTriangles.Add(new Triangle(e.Value.p1, e.Value.p2, p));
            }
            //tempTriangles = doFlipIfNeeded(tempTriangles);

            foreach (Triangle tmp in tempTriangles)
            {
                trianglesArray.Add(tmp);
            }

            badTrianglesArray.Clear();
            polygon.Clear();
        }

        HashSet <Triangle> badTriangles = new HashSet <Triangle>();

        foreach (Triangle t in trianglesArray)
        {
            if (t.p1.idx == -1 || t.p2.idx == -1 || t.p3.idx == -1)
            {
                badTriangles.Add(t);
            }
        }

        foreach (Triangle bt in badTriangles)
        {
            trianglesArray.Remove(bt);
        }

        // ******** TEST **********
        //test_Triangulation.test_TriangulationMesh(trianglesArray);
        //Debug.Log("TEST - FLIPS DONE: " + test_Triangulation.flipsDone);
        // ************************

        int it = 0;

        triangles = new int[trianglesArray.Count * 3];
        foreach (Triangle t in trianglesArray)
        {
            triangles[it++] = t.p1.idx;
            triangles[it++] = t.p2.idx;
            triangles[it++] = t.p3.idx;
        }
        Debug.Log(triangles.Length);
        return(triangles);
    }
Exemplo n.º 4
0
 public Edge(IdxVertex p1, IdxVertex p2)
 {
     this.p1 = p1;
     this.p2 = p2;
 }
Exemplo n.º 5
0
 public Edge(Edge e)
 {
     p1 = e.p1;
     p2 = e.p2;
 }