Ejemplo n.º 1
0
        public void SortDirectedHilbertTestDefaultStepsWithEdges()
        {
            var n = GraphExtensions.DefaultHilbertSteps;

            // build locations.
            var locations = new List<Tuple<GeoCoordinate, uint>>();
            locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-90, -180), 1));
            locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-90, -60), 2));
            locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-90, 60), 3));
            locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-90, 180), 4));
            locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-30, -180), 5));
            locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-30, -60), 6));
            locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-30, 60), 7));
            locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-30, 180), 8));
            locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(30, -180), 9));
            locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(30, -60), 10));
            locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(30, 60), 11));
            locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(30, 180), 12));
            locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(90, -180), 13));
            locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(90, -60), 14));
            locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(90, 60), 15));
            locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(90, 180), 16));

            // build graph.
            var graph = new DirectedGraph<Edge>();
            for (var idx = 0; idx < locations.Count; idx++)
            {
                graph.AddVertex((float)locations[idx].Item1.Latitude,
                    (float)locations[idx].Item1.Longitude);
            }

            // add edges.
            graph.AddEdge(1, 2, new Edge() { Tags = 1 });
            graph.AddEdge(2, 3, new Edge() { Tags = 2 });
            graph.AddEdge(3, 4, new Edge() { Tags = 3 });
            graph.AddEdge(4, 5, new Edge() { Tags = 4 });
            graph.AddEdge(5, 6, new Edge() { Tags = 5 });
            graph.AddEdge(6, 7, new Edge() { Tags = 6 });
            graph.AddEdge(7, 8, new Edge() { Tags = 7 });
            graph.AddEdge(8, 9, new Edge() { Tags = 8 });
            graph.AddEdge(9, 10, new Edge() { Tags = 9 });
            graph.AddEdge(10, 11, new Edge() { Tags = 10 });
            graph.AddEdge(11, 12, new Edge() { Tags = 11 });
            graph.AddEdge(12, 13, new Edge() { Tags = 12 });
            graph.AddEdge(13, 14, new Edge() { Tags = 13 });
            graph.AddEdge(14, 15, new Edge() { Tags = 14 });

            // build a sorted version.
            graph.SortHilbert(n);

            // sort locations.
            locations.Sort((x, y) =>
            {
                return HilbertCurve.HilbertDistance((float)x.Item1.Latitude, (float)x.Item1.Longitude, n).CompareTo(
                     HilbertCurve.HilbertDistance((float)y.Item1.Latitude, (float)y.Item1.Longitude, n));
            });

            // confirm sort.
            float latitude, longitude;
            var newToOld = new Dictionary<uint, uint>();
            for (uint vertex = 1; vertex <= graph.VertexCount; vertex++)
            {
                graph.GetVertex(vertex, out latitude, out longitude);
                Assert.AreEqual(latitude, locations[(int)(vertex - 1)].Item1.Latitude);
                Assert.AreEqual(longitude, locations[(int)(vertex - 1)].Item1.Longitude);

                newToOld.Add(vertex, locations[(int)(vertex - 1)].Item2);
            }

            for (uint vertex = 1; vertex <= graph.VertexCount; vertex++)
            {
                var edges = graph.GetEdges(vertex);
                var originalVertex = newToOld[vertex];
                foreach (var edge in edges)
                {
                    var originalNeighbour = newToOld[edges.Neighbour];
                    Assert.IsTrue(originalVertex - 1 == originalNeighbour ||
                        originalVertex + 1 == originalNeighbour);
                }
            }
        }
Ejemplo n.º 2
0
        public void SortDirectedHilbertTestSteps4()
        {
            var n = 4;

            // build locations.
            var locations = new List<GeoCoordinate>();
            locations.Add(new GeoCoordinate(-90, -180));
            locations.Add(new GeoCoordinate(-90, -60));
            locations.Add(new GeoCoordinate(-90, 60));
            locations.Add(new GeoCoordinate(-90, 180));
            locations.Add(new GeoCoordinate(-30, -180));
            locations.Add(new GeoCoordinate(-30, -60));
            locations.Add(new GeoCoordinate(-30, 60));
            locations.Add(new GeoCoordinate(-30, 180));
            locations.Add(new GeoCoordinate(30, -180));
            locations.Add(new GeoCoordinate(30, -60));
            locations.Add(new GeoCoordinate(30, 60));
            locations.Add(new GeoCoordinate(30, 180));
            locations.Add(new GeoCoordinate(90, -180));
            locations.Add(new GeoCoordinate(90, -60));
            locations.Add(new GeoCoordinate(90, 60));
            locations.Add(new GeoCoordinate(90, 180));

            // build graph.
            var graph = new DirectedGraph<Edge>();
            for (var idx = 0; idx < locations.Count; idx++)
            {
                graph.AddVertex((float)locations[idx].Latitude,
                    (float)locations[idx].Longitude);
            }

            // build a sorted version.
            graph.SortHilbert(n);

            // test if sorted.
            for (uint vertex = 1; vertex < graph.VertexCount; vertex++)
            {
                Assert.IsTrue(
                    GraphExtensions.HilbertDistance(graph, n, vertex) <=
                    GraphExtensions.HilbertDistance(graph, n, vertex + 1));
            }

            // sort locations.
            locations.Sort((x, y) =>
            {
                return HilbertCurve.HilbertDistance((float)x.Latitude, (float)x.Longitude, n).CompareTo(
                     HilbertCurve.HilbertDistance((float)y.Latitude, (float)y.Longitude, n));
            });

            // confirm sort.
            for (uint vertex = 1; vertex <= graph.VertexCount; vertex++)
            {
                float latitude, longitude;
                graph.GetVertex(vertex, out latitude, out longitude);
                Assert.AreEqual(latitude, locations[(int)(vertex - 1)].Latitude);
                Assert.AreEqual(longitude, locations[(int)(vertex - 1)].Longitude);
            }
        }