Exemplo n.º 1
0
        public void TestVoronoi()
        {
            var Coords = new Coordinate[5];

            Coords[0] = new Coordinate(200, 200);
            Coords[1] = new Coordinate(400, 400);
            Coords[2] = new Coordinate(600, 600);
            Coords[3] = new Coordinate(800, 300);
            Coords[4] = new Coordinate(430, 200);

            var voronoiDiagram = new VoronoiDiagramBuilder();

            voronoiDiagram.SetSites(Coords);

            var factory          = new GeometryFactory();
            var resultingDiagram = voronoiDiagram.GetDiagram(factory);

            int i = 0;

            foreach (var voronoiCell in resultingDiagram.Geometries)
            {
                Console.WriteLine("Voronoi {0}: Contains {1}? {2}", i++, voronoiCell.UserData,
                                  voronoiCell.Contains(factory.CreatePoint((Coordinate)voronoiCell.UserData)));
            }
        }
        public void TestVoronoi()
        {
            var bmp = new Bitmap(1000, 650);
            var gr  = Graphics.FromImage(bmp);

            gr.SmoothingMode      = SmoothingMode.AntiAlias;
            gr.CompositingQuality = CompositingQuality.HighQuality;

            var blackPen = new Pen(Color.Black, 3);

            var Coords = new Coordinate[5];

            Coords[0] = new Coordinate(200, 200);
            Coords[1] = new Coordinate(400, 400);
            Coords[2] = new Coordinate(600, 600);
            Coords[3] = new Coordinate(800, 300);
            Coords[4] = new Coordinate(430, 200);

            var voronoiDiagram = new VoronoiDiagramBuilder();

            voronoiDiagram.SetSites(Coords);

            var factory          = new GeometryFactory();
            var resultingDiagram = voronoiDiagram.GetDiagram(factory);

            // draw cells
            var i = 0;

            foreach (var voronoiCell in resultingDiagram.Geometries)
            {
                var points = new List <System.Drawing.Point>();
                foreach (var coordinate in voronoiCell.Coordinates)
                {
                    points.Add(new System.Drawing.Point((int)coordinate.X, (int)coordinate.Y));
                }

                gr.DrawPolygon(blackPen, points.ToArray());

                Console.WriteLine("Voronoi {0}: Contains {1}? {2}", i++, voronoiCell.UserData,
                                  voronoiCell.Contains(factory.CreatePoint((Coordinate)voronoiCell.UserData)));
            }

            // draw sites
            var blueVioletBrush = new SolidBrush(Color.BlueViolet);

            foreach (Coordinate C in Coords)
            {
                gr.FillEllipse(blueVioletBrush, (float)C.X, (float)C.Y, 20, 20);
            }

            var path = new Uri(Path.Combine(Path.GetTempPath(), Path.ChangeExtension(Path.GetTempFileName(), "png")));

            bmp.Save(path.LocalPath);
            Console.WriteLine(path.AbsoluteUri);

            blueVioletBrush.Dispose();
            blackPen.Dispose();
            bmp.Dispose();
            gr.Dispose();
        }
Exemplo n.º 3
0
        public Geometry VoronoiDiagram(Geometry sitesGeom, Envelope extent)
        {
            var builder = new VoronoiDiagramBuilder();

            builder.SetSites(sitesGeom);
            builder.ClipEnvelope = extent;
            builder.Tolerance    = 0.0001;
            Geometry diagram = builder.GetDiagram(sitesGeom.Factory);

            return(diagram);
        }
Exemplo n.º 4
0
        public static Geometry voronoiDiagram(Geometry geom, Geometry g2)
        {
            var builder = new VoronoiDiagramBuilder();

            builder.SetSites(geom);
            if (g2 != null)
            {
                builder.ClipEnvelope = g2.EnvelopeInternal;
            }
            builder.Tolerance = TRIANGULATION_TOLERANCE;
            Geometry diagram = builder.GetDiagram(geom.Factory);

            return(diagram);
        }
Exemplo n.º 5
0
        public static Geometry VoronoiDiagram(Geometry geom, Geometry g2)
        {
            var builder = new VoronoiDiagramBuilder();

            builder.SetSites(geom);
            if (g2 != null)
            {
                builder.ClipEnvelope = g2.EnvelopeInternal;
            }
            builder.Tolerance = TriangulationTolerance;
            Geometry diagram = builder.GetDiagram(geom.Factory);

            return(diagram);
        }
Exemplo n.º 6
0
        public void TestVoronoiDiagramBuilder()
        {
            const string wkt =
                @"POLYGON ((561758.3399999999 4815264.26, 561758.2199999997 4815264.24, 561758.0899999999 4815264.25, 561757.9699999997 4815264.279999999, 561757.8499999996 4815264.34, 561757.7599999999 4815264.42, 561757.6799999997 4815264.52, 561757.6299999999 4815264.640000001, 561757.5999999996 4815264.76, 561757.5999999996 4815264.890000001, 561757.6299999999 4815265.02, 561757.6799999997 4815265.130000001, 561757.7599999999 4815265.230000001, 561757.8600000005 4815265.310000001, 561758.6100000005 4815265.724000001, 561759.3600000005 4815266.138000001, 561760.1100000005 4815266.552000001, 561760.8600000005 4815266.966000001, 561761.6100000005 4815267.380000001, 561761.75 4815267.41, 561761.9000000004 4815267.4, 561762.04 4815267.359999999, 561762.1699999999 4815267.289999999, 561762.2699999996 4815267.1899999995, 561762.3600000005 4815267.07, 561762.4100000003 4815266.930000001, 561762.4299999997 4815266.789999999, 561762.4199999999 4815266.65, 561762.3799999999 4815266.51, 561762.2999999998 4815266.380000001, 561761.5319999999 4815265.966000001, 561760.764 4815265.552000001, 561759.9959999999 4815265.138000001, 561759.228 4815264.724000001, 561758.4600000001 4815264.310000001, 561758.3399999999 4815264.26))";

            var reader = new WKTReader();
            var geom   = reader.Read(wkt);

            Assert.IsTrue(geom.IsValid);
            Assert.IsTrue(geom.IsSimple);

            //var dtb = new DelaunayTriangulationBuilder();
            //dtb.SetSites(geom);
            QuadEdgeSubdivision qes = null;
            //Assert.DoesNotThrow( () => qes = dtb.GetSubdivision());
            //Assert.IsNotNull(qes);

            //foreach (var t in qes.GetVoronoiCellPolygons(GeometryFactory.Default))
            //    Console.WriteLine(t.AsText());

            //Console.WriteLine();

            GeometryCollection diag = null;
            //Assert.DoesNotThrow(() => diag = qes.GetVoronoiDiagram(GeometryFactory.Default));
            //Assert.IsNotNull(diag);

            var vdb = new VoronoiDiagramBuilder();

            vdb.SetSites(geom);
            qes = null;
            Assert.DoesNotThrow(() => qes = vdb.GetSubdivision());
            Assert.IsNotNull(qes);

            foreach (var t in qes.GetVoronoiCellPolygons(GeometryFactory.Default))
            {
                Console.WriteLine(t.AsText());
            }

            Console.WriteLine();

            diag = null;
            Assert.DoesNotThrow(() => diag = vdb.GetDiagram(GeometryFactory.Default));
            Assert.IsNotNull(diag);
        }
Exemplo n.º 7
0
        public static Geometry voronoiDiagramWithData(Geometry geom, Geometry g2)
        {
            GeometryDataUtil.setComponentDataToIndex(geom);

            var mapper = new VertexTaggedGeometryDataMapper();

            mapper.LoadSourceGeometries(geom);

            var builder = new VoronoiDiagramBuilder();

            builder.SetSites(mapper.Coordinates);
            if (g2 != null)
            {
                builder.ClipEnvelope = g2.EnvelopeInternal;
            }
            builder.Tolerance = TRIANGULATION_TOLERANCE;
            Geometry diagram = builder.GetDiagram(geom.Factory);

            mapper.TransferData(diagram);
            return(diagram);
        }
        public static IGeometry VoronoiDiagramWithData(IGeometry geom, IGeometry g2)
        {
            GeometryDataUtil.SetComponentDataToIndex(geom);

            VertexTaggedGeometryDataMapper mapper = new VertexTaggedGeometryDataMapper();

            mapper.LoadSourceGeometries(geom);

            VoronoiDiagramBuilder builder = new VoronoiDiagramBuilder();

            builder.SetSites(mapper.Coordinates);
            if (g2 != null)
            {
                builder.ClipEnvelope = g2.EnvelopeInternal;
            }
            builder.Tolerance = TriangulationTolerance;
            IGeometry diagram = builder.GetDiagram(geom.Factory);

            mapper.TransferData(diagram);
            return(diagram);
        }
Exemplo n.º 9
0
        public void Generate(int pointCount)
        {
            //Generate random points
            HashSet <Coordinate> hashSet = new HashSet <Coordinate>();
            {
                Random rand = new Random(seed);
                while (hashSet.Count < pointCount)
                {
                    double x = rand.NextDouble() * 2 - 1;
                    double y = rand.NextDouble() * 2 - 1;
                    if (x < -0.99 || y < -0.99 || x > 0.99 || y > 0.99)
                    {
                        continue;
                    }
                    hashSet.Add(new Coordinate(x, y));
                }
            }
            //Optimize points
            {
                Coordinate[] points = hashSet.ToArray();
                for (int i = 0; i < 2; i++)
                {
                    VoronoiDiagramBuilder builder = new VoronoiDiagramBuilder();
                    builder.SetSites(points);
                    VoronoiDiagram = builder.GetDiagram(new GeometryFactory());
                    for (int j = 0; j < points.Length; j++)
                    {
                        Polygon poly = VoronoiDiagram[j] as Polygon;
                        points[j] = new Coordinate(poly.Centroid.X, poly.Centroid.Y);
                    }
                }
            }
            //Build graph
            PlanarGraph graph;
            {
                VoronoiDiagram = ClipGeometryCollection(VoronoiDiagram, new Envelope(-1, 1, -1, 1));
                graph          = new PlanarGraph(new OverlayNodeFactory());
                List <Edge> edges = new List <Edge>();
                for (int i = 0; i < VoronoiDiagram.Count; i++)
                {
                    Polygon      poly   = VoronoiDiagram[i] as Polygon;
                    Coordinate[] coords = poly.Coordinates;
                    for (int j = 1; j < coords.Length; j++)
                    {
                        edges.Add(new Edge(new[] { coords[j - 1], coords[j] }, new Label(Location.Boundary)));
                    }
                }
                graph.AddEdges(edges);
            }
            //Convert graph
            Dictionary <Node, MapNode> nodeDict;

            {
                Dictionary <MapPolygon, HashSet <MapPolygon> > polys = new Dictionary <MapPolygon, HashSet <MapPolygon> >();
                nodeDict = new Dictionary <Node, MapNode>();
                Dictionary <MapNode, Tuple <HashSet <MapPolygon>, HashSet <MapEdge> > > dats =
                    new Dictionary <MapNode, Tuple <HashSet <MapPolygon>, HashSet <MapEdge> > >();
                for (int i = 0; i < VoronoiDiagram.Count; i++)
                {
                    List <MapNode> nodes = new List <MapNode>();
                    MapPolygon     poly  = new MapPolygon
                    {
                        CentroidX = VoronoiDiagram[i].Centroid.X,
                        CentroidY = VoronoiDiagram[i].Centroid.Y,
                        Polygon   = VoronoiDiagram[i] as Polygon
                    };
                    foreach (Coordinate j in VoronoiDiagram[i].Coordinates.Skip(1))
                    {
                        Node    n = graph.Find(j);
                        MapNode mapNode;
                        if (!nodeDict.TryGetValue(n, out mapNode))
                        {
                            mapNode = new MapNode {
                                X = j.X, Y = j.Y
                            };
                            dats[mapNode] =
                                new Tuple <HashSet <MapPolygon>, HashSet <MapEdge> >(new HashSet <MapPolygon> {
                                poly
                            },
                                                                                     new HashSet <MapEdge>());
                        }
                        else
                        {
                            dats[mapNode].Item1.Add(poly);
                        }
                        nodes.Add(nodeDict[n] = mapNode);
                    }
                    poly.Nodes = nodes.ToArray();
                    polys.Add(poly, new HashSet <MapPolygon>());
                }
                foreach (KeyValuePair <Node, MapNode> i in nodeDict)
                {
                    foreach (MapPolygon j in dats[i.Value].Item1)
                    {
                        foreach (MapPolygon k in dats[i.Value].Item1)
                        {
                            if (j != k)
                            {
                                polys[j].Add(k);
                                polys[k].Add(j);
                            }
                        }
                    }
                    foreach (EdgeEnd j in i.Key.Edges)
                    {
                        MapNode from = nodeDict[graph.Find(j.Coordinate)];
                        MapNode to   = nodeDict[graph.Find(j.DirectedCoordinate)];
                        dats[from].Item2.Add(new MapEdge {
                            From = from, To = to
                        });
                    }
                }
                int ftrh = dats.Count(_ => _.Value.Item2.Count == 0);
                foreach (KeyValuePair <MapNode, Tuple <HashSet <MapPolygon>, HashSet <MapEdge> > > i in dats)
                {
                    i.Key.Edges = i.Value.Item2.ToArray();
                }
                KeyValuePair <MapPolygon, HashSet <MapPolygon> >[] x = polys.ToArray();
                for (int i = 0; i < x.Length; i++)
                {
                    x[i].Key.Neighbour = x[i].Value.ToArray();
                    x[i].Key.Id        = i;
                }
                Polygons = x.Select(_ => _.Key).ToArray();
            }
            //Generate map
            DetermineLandmass();
            FindOceans();
            ComputeDistances();
            RedistributeElevation(nodeDict.Values);
            FindLakesAndCoasts();
        }