Ejemplo n.º 1
0
        private void GenDelaunayTreeDecks(List <Vector3> points)
        {
            GameObject DeckNode = new GameObject("Deck Node");

            // Magic and beautiful Delaunay triangulation
            List <Triangle> triangulation = Delaunay.TriangulateByFlippingEdges(points);

            // Area filter setup
            double[] check_range = MathFilter.AreaSpan(triangulation, MaxLength, MinAngle, RelativeArea);

            foreach (Triangle tri in triangulation)
            {
                // Length constraint. if 0, no constraint
                if (MathFilter.LengthFilter(tri, MaxLength))
                {
                    List <double> TriAngles = MathFilter.AngleDegrees(tri);

                    // Angle constraint
                    if (TriAngles.TrueForAll(angle => angle >= MinAngle))
                    {
                        double area = MathFilter.TriangleArea(tri);

                        // Area constraint
                        if (check_range[0] <= area && area <= check_range[1])
                        {
                            Vector3 P1 = tri.v1.position;
                            Vector3 P2 = tri.v2.position;
                            Vector3 P3 = tri.v3.position;

                            // Brett deck
                            GameObject TreeDeck      = Instantiate(DeckObject);
                            Treehouse  TreehouseComp = TreeDeck.AddComponent <Treehouse>();
                            TreeDeck.AddComponent <Deck>();

                            // Deck component needs 3 GameObjects. In the future this
                            // may become 3 Vector3s.
                            GameObject newTreeNode1 = Instantiate(DeckNode, P1, rot);
                            GameObject newTreeNode2 = Instantiate(DeckNode, P2, rot);
                            GameObject newTreeNode3 = Instantiate(DeckNode, P3, rot);

                            List <GameObject> TreesTri = new List <GameObject>();
                            TreesTri.Add(newTreeNode1);
                            TreesTri.Add(newTreeNode2);
                            TreesTri.Add(newTreeNode3);

                            TreehouseComp.Trees = TreesTri;
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
    public static List <VoronoiCell> GenerateVoronoiDiagram(List <Vector3> sites)
    {
        //First generate the delaunay triangulation
        List <Triangle> triangles = Delaunay.TriangulateByFlippingEdges(sites);


        //Generate the voronoi diagram

        //Step 1. For every delaunay edge, compute a voronoi edge
        //The voronoi edge is the edge connecting the circumcenters of two neighboring delaunay triangles
        List <VoronoiEdge> voronoiEdges = new List <VoronoiEdge>();

        for (int i = 0; i < triangles.Count; i++)
        {
            Triangle t = triangles[i];

            //Each triangle consists of these edges
            HalfEdge e1 = t.halfEdge;
            HalfEdge e2 = e1.nextEdge;
            HalfEdge e3 = e2.nextEdge;

            //Calculate the circumcenter for this triangle
            Vector3 v1 = e1.v.position;
            Vector3 v2 = e2.v.position;
            Vector3 v3 = e3.v.position;

            //The circumcenter is the center of a circle where the triangles corners is on the circumference of that circle
            //The .XZ() is an extension method that removes the y value of a vector3 so it becomes a vector2
            Vector2 center2D = Geometry.CalculateCircleCenter(v1.XZ(), v2.XZ(), v3.XZ());

            //The circumcenter is also known as a voronoi vertex, which is a position in the diagram where we are equally
            //close to the surrounding sites
            Vector3 voronoiVertex = new Vector3(center2D.x, 0f, center2D.y);

            TryAddVoronoiEdgeFromTriangleEdge(e1, voronoiVertex, voronoiEdges);
            TryAddVoronoiEdgeFromTriangleEdge(e2, voronoiVertex, voronoiEdges);
            TryAddVoronoiEdgeFromTriangleEdge(e3, voronoiVertex, voronoiEdges);
        }


        //Step 2. Find the voronoi cells where each cell is a list of all edges belonging to a site
        List <VoronoiCell> voronoiCells = new List <VoronoiCell>();

        for (int i = 0; i < voronoiEdges.Count; i++)
        {
            VoronoiEdge e = voronoiEdges[i];

            //Find the position in the list of all cells that includes this site
            int cellPos = TryFindCellPos(e, voronoiCells);

            //No cell was found so we need to create a new cell
            if (cellPos == -1)
            {
                VoronoiCell newCell = new VoronoiCell(e.sitePos);

                voronoiCells.Add(newCell);

                newCell.edges.Add(e);
            }
            else
            {
                voronoiCells[cellPos].edges.Add(e);
            }
        }


        return(voronoiCells);
    }