private List <Vector3> CreateRoad(Bounds bounds, int pointCount, Vector2[] clipPolygon)
        {
            // get offset, eg when biome mask is used for clipping
            float xmin = bounds.center.x - bounds.extents.x;
            float zmin = bounds.center.z - bounds.extents.z;

            // get 0/0-based bounds for graph processing
            Bounds graphBounds = new Bounds(bounds.size / 2, bounds.size);

            DelaunayVoronoiGraph graph = new DelaunayVoronoiGraph();

            // algorithm is 0-based
            graph.GeneratePoints(0, bounds.size.x, bounds.size.z); // initialize with the dimensions which are used in the algorithm

            // add random points
            for (int i = 0; i < pointCount; i++)
            {
                // get random point starting at [0/0]
                Vector2 vector = PolygonUtils.GetRandomPointXZ(graphBounds);

                graph.AddPoint(vector.x, vector.y);
            }

            // create the graph using the points
            graph.CreateGraph();

            int cellIndex = Random.Range(0, pointCount - 1);

            // normalize clip polygon, shift to [0/0]
            Vector2[] offsetClipPolygon = clipPolygon.Select(item => new Vector2(item.x - xmin, item.y - zmin)).ToArray();

            // get cell, clip it at the clip polygon
            Cell cell = graph.GetVoronoiCell(cellIndex, offsetClipPolygon);

            if (cell == null)
            {
                return(null);
            }

            // consider biome mask shift: shift points away from 0/0 if necessary
            Vector3 position = new Vector3(cell.Centroid.x + xmin, 0, cell.Centroid.y + zmin); // TODO: recalculate, might have changed because of clipping

            // consider biome mask shift: shift points away from 0/0 if necessary
            List <Vector3> nodes = cell.Vertices.Select(item => new Vector3(item.x + xmin, 0, item.y + zmin)).ToList();

            // apply random shape if requested
            if (editor.extension.shapeSettings.randomShape)
            {
                nodes = ShapeCreator.CreateRandomShape(nodes,                                             //
                                                       editor.extension.shapeSettings.RandomConvexity,    //
                                                       editor.extension.shapeSettings.keepOriginalPoints, //
                                                       editor.extension.shapeSettings.RandomPointsCount,  //
                                                       editor.extension.shapeSettings.randomAngle,        //
                                                       editor.extension.shapeSettings.douglasPeuckerReductionTolerance);
            }

            return(nodes);
        }
示例#2
0
        /// <summary>
        /// Create the graph using the points list
        /// </summary>
        private void CreateGraph()
        {
            // clear the canvas
            Graph.Children.Clear();

            graph.CreateGraph();

            List <InteractiveDelaunayVoronoi.Triangle> triangulation = graph.GetDelaunayTriangles();
            List <InteractiveDelaunayVoronoi.Edge>     voronoiEdges  = graph.GetVoronoiEdges();

            #region visualization

            if (cbDrawDelaunay.IsChecked.GetValueOrDefault())
            {
                DrawTriangulation(triangulation);
            }

            if (cbDrawPoints.IsChecked.GetValueOrDefault())
            {
                DrawPoints(graph.GetAllVectors());
            }

            if (cbDrawCircumCenters.IsChecked.GetValueOrDefault())
            {
                DrawCircumCenters(graph.GetCircumCenterPoints());
            }

            if (cbDrawCircumCircles.IsChecked.GetValueOrDefault())
            {
                DrawCircumCircles(triangulation);
            }

            if (cbDrawVoronoiFillCurrent.IsChecked.GetValueOrDefault())
            {
                DrawVoronoiFillCurrent();
            }

            if (cbDrawVoronoiFillAll.IsChecked.GetValueOrDefault())
            {
                DrawVoronoiFillAll();
            }

            if (cbDrawVoronoi.IsChecked.GetValueOrDefault())
            {
                DrawVoronoi(voronoiEdges);
            }

            if (cbDrawClipVoronoi.IsChecked.GetValueOrDefault())
            {
                DrawAllClippedPolygons();
            }

            if (cbDrawMeanVector.IsChecked.GetValueOrDefault())
            {
                DrawMeanVector();
            }

            if (cbDrawRandomShape.IsChecked.GetValueOrDefault())
            {
                DrawRandomShape();
            }
            #endregion visualization
        }