Пример #1
0
        public void SortHilbertTest7()
        {
            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 Graph<Edge>();
            for (var idx = 0; idx < locations.Count; idx++)
            {
                graph.AddVertex((float)locations[idx].Item1.Latitude,
                    (float)locations[idx].Item1.Longitude);
            }

            // add edges.
            Func<uint, uint, uint> buildTagsId = (v1, v2) => { return v1 + (v2 * 16); };
            graph.AddEdge(1, 2, new Edge() { Tags = buildTagsId(1, 2) });
            graph.AddEdge(2, 3, new Edge() { Tags = buildTagsId(2, 3) });
            graph.AddEdge(3, 4, new Edge() { Tags = buildTagsId(3, 4) });
            graph.AddEdge(4, 5, new Edge() { Tags = buildTagsId(4, 5) });
            graph.AddEdge(5, 6, new Edge() { Tags = buildTagsId(5, 6) });
            graph.AddEdge(6, 7, new Edge() { Tags = buildTagsId(6, 7) });
            graph.AddEdge(7, 8, new Edge() { Tags = buildTagsId(7, 8) });
            graph.AddEdge(8, 9, new Edge() { Tags = buildTagsId(8, 9) });
            graph.AddEdge(9, 10, new Edge() { Tags = buildTagsId(9, 10) });
            graph.AddEdge(10, 11, new Edge() { Tags = buildTagsId(10, 11) });
            graph.AddEdge(11, 12, new Edge() { Tags = buildTagsId(11, 12) });
            graph.AddEdge(12, 13, new Edge() { Tags = buildTagsId(12, 13) });
            graph.AddEdge(13, 14, new Edge() { Tags = buildTagsId(13, 14) });
            graph.AddEdge(14, 15, new Edge() { Tags = buildTagsId(14, 15) });

            // add 10 random edges.
            for (var i = 0; i < 10; i++)
            {
                var from = Convert.ToUInt32(OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(15) + 1);
                var to = Convert.ToUInt32(OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(14) + 1);
                if(from <= to)
                {
                    to = to + 1;
                }
                graph.AddEdge(from, to, new Edge() { Tags = buildTagsId(from, to) });
            }

            // sort vertices in-place.
            graph.SortHilbert();
            graph.Compress();

            // 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 convert = 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);

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

            for (uint vertex = 1; vertex <= graph.VertexCount; vertex++)
            {
                var edges = graph.GetEdges(vertex);
                var originalVertex = convert[vertex];
                foreach (var edge in edges)
                {
                    var originalNeighbour = convert[edges.Neighbour];
                    if (edge.EdgeData.Forward)
                    { // edge is forward.
                        Assert.AreEqual(buildTagsId(originalVertex, originalNeighbour), edge.EdgeData.Tags);
                    }
                    else
                    { // edge is backward.
                        Assert.AreEqual(buildTagsId(originalNeighbour, originalVertex), edge.EdgeData.Tags);
                    }
                }
            }
        }
Пример #2
0
        public void SearchHilbertTest5()
        {
            var n = GraphExtensions.DefaultHilbertSteps;

            // build graph.
            var graph = new Graph<Edge>();
            var vertex1 = graph.AddVertex(-90, -180);
            var vertex2 = graph.AddVertex(-90, -60);
            var vertex3 = graph.AddVertex(-30, -60);
            var vertex4 = graph.AddVertex(-30, -180);
            var vertex5 = graph.AddVertex(30, -180);
            var vertex6 = graph.AddVertex(90, -180);
            var vertex7 = graph.AddVertex(90, -60);
            var vertex8 = graph.AddVertex(30, -60);
            var vertex9 = graph.AddVertex(30, 60);
            var vertex10 = graph.AddVertex(90, 60);
            var vertex11 = graph.AddVertex(90, 180);
            var vertex12 = graph.AddVertex(30, 180);
            var vertex13 = graph.AddVertex(-30, 180);
            var vertex14 = graph.AddVertex(-30, 60);
            var vertex15 = graph.AddVertex(-90, 60);
            var vertex16 = graph.AddVertex(-90, 180);

            // search.
            var found = graph.SearchHilbert(30, 60, 0.001f);
            Assert.IsNotNull(found);
            Assert.AreEqual(1, found.Count);
            Assert.AreEqual(vertex9, found[0]);

            found = graph.SearchHilbert(30, -180, 0.001f);
            Assert.IsNotNull(found);
            Assert.AreEqual(1, found.Count);
            Assert.AreEqual(vertex5, found[0]);

            found = graph.SearchHilbert(30, 180, 0.001f);
            Assert.IsNotNull(found);
            Assert.AreEqual(1, found.Count);
            Assert.AreEqual(vertex12, found[0]);

            found = graph.SearchHilbert(-30, -60, 0.001f);
            Assert.IsNotNull(found);
            Assert.AreEqual(1, found.Count);
            Assert.AreEqual(vertex3, found[0]);

            // build graph.
            graph = new Graph<Edge>();
            vertex1 = graph.AddVertex(0, 0);
            vertex2 = graph.AddVertex(0.00001f, 0.00001f);
            vertex3 = graph.AddVertex(0.00002f, 0.00002f);
            vertex4 = graph.AddVertex(0.00003f, 0.00003f);
            vertex5 = graph.AddVertex(0.00004f, 0.00004f);
            vertex6 = graph.AddVertex(0.00005f, 0.00005f);

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

            found = graph.SearchHilbert(0, 0, 0.001f);
            Assert.IsNotNull(found);
            Assert.AreEqual(6, found.Count);
        }
Пример #3
0
        public void SortHilbertTestDefaultStepsWithEdges()
        {
            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 Graph<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);
                }
            }
        }
Пример #4
0
        public void SearchHilbertTest4()
        {
            var n = GraphExtensions.DefaultHilbertSteps;

            // build graph.
            var graph = new Graph<Edge>();
            var vertex1 = graph.AddVertex(-90, -180);
            var vertex2 = graph.AddVertex(-90, -60);
            var vertex3 = graph.AddVertex(-30, -60);
            var vertex4 = graph.AddVertex(-30, -180);
            var vertex5 = graph.AddVertex(30, -180);
            var vertex6 = graph.AddVertex(90, -180);
            var vertex7 = graph.AddVertex(90, -60);
            var vertex8 = graph.AddVertex(30, -60);
            var vertex9 = graph.AddVertex(30, 60);
            var vertex10 = graph.AddVertex(90, 60);
            var vertex11 = graph.AddVertex(90, 180);
            var vertex12 = graph.AddVertex(30, 180);
            var vertex13 = graph.AddVertex(-30, 180);
            var vertex14 = graph.AddVertex(-30, 60);
            var vertex15 = graph.AddVertex(-90, 60);
            var vertex16 = graph.AddVertex(-90, 180);

            // search vertex.
            uint vertex;
            int count;

            Assert.IsTrue(graph.SearchHilbert(HilbertCurve.HilbertDistance(-90, -180, n), n, 1, 16, out vertex, out count));
            Assert.AreEqual(vertex1, vertex);
            Assert.AreEqual(1, count);

            Assert.IsTrue(graph.SearchHilbert(HilbertCurve.HilbertDistance(-90, -60, n), n, 1, 16, out vertex, out count));
            Assert.AreEqual(vertex2, vertex);
            Assert.AreEqual(1, count);

            Assert.IsTrue(graph.SearchHilbert(HilbertCurve.HilbertDistance(-30, -60, n), n, 1, 16, out vertex, out count));
            Assert.AreEqual(vertex3, vertex);
            Assert.AreEqual(1, count);

            Assert.IsTrue(graph.SearchHilbert(HilbertCurve.HilbertDistance(-30, -180, n), n, 1, 16, out vertex, out count));
            Assert.AreEqual(vertex4, vertex);
            Assert.AreEqual(1, count);

            Assert.IsTrue(graph.SearchHilbert(HilbertCurve.HilbertDistance(30, -180, n), n, 1, 16, out vertex, out count));
            Assert.AreEqual(vertex5, vertex);
            Assert.AreEqual(1, count);

            Assert.IsTrue(graph.SearchHilbert(HilbertCurve.HilbertDistance(90, -180, n), n, 1, 16, out vertex, out count));
            Assert.AreEqual(vertex6, vertex);
            Assert.AreEqual(1, count);

            Assert.IsTrue(graph.SearchHilbert(HilbertCurve.HilbertDistance(90, -60, n), n, 1, 16, out vertex, out count));
            Assert.AreEqual(vertex7, vertex);
            Assert.AreEqual(1, count);

            Assert.IsTrue(graph.SearchHilbert(HilbertCurve.HilbertDistance(30, -60, n), n, 1, 16, out vertex, out count));
            Assert.AreEqual(vertex8, vertex);
            Assert.AreEqual(1, count);

            Assert.IsTrue(graph.SearchHilbert(HilbertCurve.HilbertDistance(30, 60, n), n, 1, 16, out vertex, out count));
            Assert.AreEqual(vertex9, vertex);
            Assert.AreEqual(1, count);

            Assert.IsTrue(graph.SearchHilbert(HilbertCurve.HilbertDistance(90, 60, n), n, 1, 16, out vertex, out count));
            Assert.AreEqual(vertex10, vertex);
            Assert.AreEqual(1, count);

            Assert.IsTrue(graph.SearchHilbert(HilbertCurve.HilbertDistance(90, 180, n), n, 1, 16, out vertex, out count));
            Assert.AreEqual(vertex11, vertex);
            Assert.AreEqual(1, count);

            Assert.IsTrue(graph.SearchHilbert(HilbertCurve.HilbertDistance(30, 180, n), n, 1, 16, out vertex, out count));
            Assert.AreEqual(vertex12, vertex);
            Assert.AreEqual(1, count);

            Assert.IsTrue(graph.SearchHilbert(HilbertCurve.HilbertDistance(-30, 180, n), n, 1, 16, out vertex, out count));
            Assert.AreEqual(vertex13, vertex);
            Assert.AreEqual(1, count);

            Assert.IsTrue(graph.SearchHilbert(HilbertCurve.HilbertDistance(-30, 60, n), n, 1, 16, out vertex, out count));
            Assert.AreEqual(vertex14, vertex);
            Assert.AreEqual(1, count);

            Assert.IsTrue(graph.SearchHilbert(HilbertCurve.HilbertDistance(-90, 60, n), n, 1, 16, out vertex, out count));
            Assert.AreEqual(vertex15, vertex);
            Assert.AreEqual(1, count);

            Assert.IsTrue(graph.SearchHilbert(HilbertCurve.HilbertDistance(-90, 180, n), n, 1, 16, out vertex, out count));
            Assert.AreEqual(vertex16, vertex);
            Assert.AreEqual(1, count);
            
            Assert.IsTrue(graph.SearchHilbert(HilbertCurve.HilbertDistance(30, 60, n) + 1, n, 1, 16, out vertex, out count));
            Assert.AreEqual(vertex9, vertex);
            Assert.AreEqual(0, count);

            Assert.IsTrue(graph.SearchHilbert(HilbertCurve.HilbertDistance(90, -180, n) + 1, n, 1, 16, out vertex, out count));
            Assert.AreEqual(vertex6, vertex);
            Assert.AreEqual(0, count);

            Assert.IsTrue(graph.SearchHilbert(HilbertCurve.HilbertDistance(-30, 60, n) + 1, n, 1, 16, out vertex, out count));
            Assert.AreEqual(vertex14, vertex);
            Assert.AreEqual(0, count);

            // build graph.
            graph = new Graph<Edge>();
            vertex1 = graph.AddVertex(0, 0);
            vertex2 = graph.AddVertex(0.00001f, 0.00001f);
            vertex3 = graph.AddVertex(0.00002f, 0.00002f);
            vertex4 = graph.AddVertex(0.00003f, 0.00003f);
            vertex5 = graph.AddVertex(0.00004f, 0.00004f);
            vertex6 = graph.AddVertex(0.00005f, 0.00005f);

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

            Assert.IsTrue(graph.SearchHilbert(HilbertCurve.HilbertDistance(0, 0, n), n, vertex1, vertex6, 
                out vertex, out count));
            Assert.AreEqual(vertex1, vertex);
            Assert.AreEqual(6, count);

            Assert.IsTrue(graph.SearchHilbert(HilbertCurve.HilbertDistance(0, 0, n), n, vertex3, vertex6, 
                out vertex, out count));
            Assert.AreEqual(vertex1, vertex);
            Assert.AreEqual(6, count);

            Assert.IsTrue(graph.SearchHilbert(HilbertCurve.HilbertDistance(0, 0, n), n, vertex1, vertex3, 
                out vertex, out count));
            Assert.AreEqual(vertex1, vertex);
            Assert.AreEqual(6, count);

            Assert.IsTrue(graph.SearchHilbert(HilbertCurve.HilbertDistance(0, 0, n), n, vertex3, vertex3, 
                out vertex, out count));
            Assert.AreEqual(vertex1, vertex);
            Assert.AreEqual(6, count);
        }
Пример #5
0
        public void SortHilbertTestStepsDefault()
        {
            var n = GraphExtensions.DefaultHilbertSteps;

            // 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 Graph<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);

            // 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);
            }
        }