public void TestVertexCountAndTrim()
        {
            var graph = new GeometricGraph(1, 100);

            // add edge.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 10 }, null);

            // trim.
            graph.Trim();
            Assert.AreEqual(2, graph.VertexCount);
            Assert.AreEqual(1, graph.EdgeCount);

            graph = new GeometricGraph(1, 100);

            // add edge.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddVertex(11001, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 10 }, null);
            graph.AddEdge(0, 11001, new uint[] { 10 }, null);

            // trim.
            graph.Trim();
            Assert.AreEqual(11002, graph.VertexCount);
            Assert.AreEqual(2, graph.EdgeCount);

            graph = new GeometricGraph(1, 100);

            // trim.
            graph.Trim(); // keep minimum one vertex.
            Assert.AreEqual(0, graph.VertexCount);
        }
        public void TestEdgeCount()
        {
            var graph = new GeometricGraph(1, 100);

            // add edge.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 10 }, null);
            Assert.AreEqual(1, graph.EdgeCount);

            graph = new GeometricGraph(1, 100);

            // add edge.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddVertex(11001, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 10 }, null);
            graph.AddEdge(0, 11001, new uint[] { 10 }, null);
            Assert.AreEqual(2, graph.EdgeCount);

            graph.AddEdge(0, 11001, new uint[] { 20 }, null);
            Assert.AreEqual(3, graph.EdgeCount);

            Assert.AreEqual(2, graph.RemoveEdges(0, 11001));
            Assert.AreEqual(1, graph.EdgeCount);

            Assert.AreEqual(1, graph.RemoveEdges(0, 1));
            Assert.AreEqual(0, graph.EdgeCount);
        }
        public void TestSetVertex()
        {
            var graph = new GeometricGraph(1, 100);

            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 1, 1);
            graph.AddVertex(2, 2, 2);

            float latitude, longitude;

            Assert.IsTrue(graph.GetVertex(0, out latitude, out longitude));
            Assert.AreEqual(0, latitude);
            Assert.AreEqual(0, longitude);
            Assert.AreEqual(0, graph.GetVertex(0).Latitude);
            Assert.AreEqual(0, graph.GetVertex(0).Longitude);
            Assert.IsTrue(graph.GetVertex(1, out latitude, out longitude));
            Assert.AreEqual(1, latitude);
            Assert.AreEqual(1, longitude);
            Assert.AreEqual(1, graph.GetVertex(1).Latitude);
            Assert.AreEqual(1, graph.GetVertex(1).Longitude);
            Assert.IsTrue(graph.GetVertex(2, out latitude, out longitude));
            Assert.AreEqual(2, latitude);
            Assert.AreEqual(2, longitude);
            Assert.AreEqual(2, graph.GetVertex(2).Latitude);
            Assert.AreEqual(2, graph.GetVertex(2).Longitude);

            Assert.IsFalse(graph.GetVertex(10000, out latitude, out longitude));
        }
        public void TestRemoveEdges()
        {
            var graph = new GeometricGraph(1, 100);

            // add and remove edge.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 10 }, null);
            Assert.AreEqual(1, graph.RemoveEdges(0));
            Assert.AreEqual(0, graph.RemoveEdges(1));

            // verify all edges.
            var edges = graph.GetEdgeEnumerator(0);

            Assert.AreEqual(0, edges.Count());

            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(0, edges.Count());

            graph = new GeometricGraph(1, 100);

            // add and remove edges.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddVertex(2, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 10 }, null);
            graph.AddEdge(0, 2, new uint[] { 10 }, null);
            Assert.AreEqual(2, graph.RemoveEdges(0));

            // verify all edges.
            edges = graph.GetEdgeEnumerator(0);
            Assert.AreEqual(0, edges.Count());
            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(0, edges.Count());
            edges = graph.GetEdgeEnumerator(2);
            Assert.AreEqual(0, edges.Count());

            graph = new GeometricGraph(1, 100);

            // add and remove edges.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddVertex(2, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 10 }, null);
            graph.AddEdge(0, 2, new uint[] { 20 }, null);
            graph.AddEdge(1, 2, new uint[] { 30 }, null);
            Assert.AreEqual(2, graph.RemoveEdges(0));

            // verify all edges.
            edges = graph.GetEdgeEnumerator(0);
            Assert.AreEqual(0, edges.Count());
            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(1, edges.Count());
            edges = graph.GetEdgeEnumerator(2);
            Assert.AreEqual(1, edges.Count());
        }
        public void TestGetShape()
        {
            var graph = new GeometricGraph(2, 100);

            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 1, 1);
            graph.AddVertex(2, 0, 1);
            graph.AddVertex(3, 0, 2);
            var edge1 = graph.AddEdge(0, 1, new uint[] { 0, 10 },
                                      new Coordinate(0.25f, 0.25f),
                                      new Coordinate(0.50f, 0.50f),
                                      new Coordinate(0.75f, 0.75f));

            var shape = graph.GetShape(graph.GetEdge(edge1));

            Assert.IsNotNull(shape);
            Assert.AreEqual(5, shape.Count);
            Assert.AreEqual(0, shape[0].Latitude);
            Assert.AreEqual(0, shape[0].Longitude);
            Assert.AreEqual(0.25, shape[1].Latitude);
            Assert.AreEqual(0.25, shape[1].Longitude);
            Assert.AreEqual(0.5, shape[2].Latitude);
            Assert.AreEqual(0.5, shape[2].Longitude);
            Assert.AreEqual(0.75, shape[3].Latitude);
            Assert.AreEqual(0.75, shape[3].Longitude);
            Assert.AreEqual(1, shape[4].Latitude);
            Assert.AreEqual(1, shape[4].Longitude);
            shape = graph.GetShape(graph.GetEdgeEnumerator(0).First());
            Assert.IsNotNull(shape);
            Assert.AreEqual(5, shape.Count);
            Assert.AreEqual(0, shape[0].Latitude);
            Assert.AreEqual(0, shape[0].Longitude);
            Assert.AreEqual(0.25, shape[1].Latitude);
            Assert.AreEqual(0.25, shape[1].Longitude);
            Assert.AreEqual(0.5, shape[2].Latitude);
            Assert.AreEqual(0.5, shape[2].Longitude);
            Assert.AreEqual(0.75, shape[3].Latitude);
            Assert.AreEqual(0.75, shape[3].Longitude);
            Assert.AreEqual(1, shape[4].Latitude);
            Assert.AreEqual(1, shape[4].Longitude);
            shape = graph.GetShape(graph.GetEdgeEnumerator(1).First(x => x.To == 0));
            Assert.IsNotNull(shape);
            Assert.AreEqual(5, shape.Count);
            Assert.AreEqual(1, shape[0].Latitude);
            Assert.AreEqual(1, shape[0].Longitude);
            Assert.AreEqual(0.75, shape[1].Latitude);
            Assert.AreEqual(0.75, shape[1].Longitude);
            Assert.AreEqual(0.5, shape[2].Latitude);
            Assert.AreEqual(0.5, shape[2].Longitude);
            Assert.AreEqual(0.25, shape[3].Latitude);
            Assert.AreEqual(0.25, shape[3].Longitude);
            Assert.AreEqual(0, shape[4].Latitude);
            Assert.AreEqual(0, shape[4].Longitude);
        }
Exemple #6
0
        public void SearchClosestVertexTest()
        {
            var graph = new GeometricGraph(1);

            graph.AddVertex(0, 1, 1);
            graph.AddVertex(1, 2, 2);

            graph.Sort();

            Assert.AreEqual(0, graph.SearchClosest(1, 1, 1, 1));
            Assert.AreEqual(1, graph.SearchClosest(2, 2, 1, 1));
            Assert.AreEqual(Constants.NO_VERTEX, graph.SearchClosest(3, 3, .5f, .5f));
        }
        public void TestArgumentExcptions()
        {
            // create graph with one vertex and start adding 2.
            var graph = new GeometricGraph(1, 2);

            // make sure to add 1 and 2.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 1, 1);
            graph.AddEdge(0, 1, new uint[] { 1 }, null);

            Assert.Catch <ArgumentOutOfRangeException>(() =>
            {
                graph.GetEdgeEnumerator(2);
            });
        }
Exemple #8
0
        public void SortHilbertTestSteps4()
        {
            var n = 4;

            // build locations.
            var locations = new List <Coordinate>();

            locations.Add(new Coordinate(-90, -180));
            locations.Add(new Coordinate(-90, -60));
            locations.Add(new Coordinate(-90, 60));
            locations.Add(new Coordinate(-90, 180));
            locations.Add(new Coordinate(-30, -180));
            locations.Add(new Coordinate(-30, -60));
            locations.Add(new Coordinate(-30, 60));
            locations.Add(new Coordinate(-30, 180));
            locations.Add(new Coordinate(30, -180));
            locations.Add(new Coordinate(30, -60));
            locations.Add(new Coordinate(30, 60));
            locations.Add(new Coordinate(30, 180));
            locations.Add(new Coordinate(90, -180));
            locations.Add(new Coordinate(90, -60));
            locations.Add(new Coordinate(90, 60));
            locations.Add(new Coordinate(90, 180));

            // build graph.
            var graph = new GeometricGraph(1);

            for (var vertex = 0; vertex < locations.Count; vertex++)
            {
                graph.AddVertex((uint)vertex, locations[vertex].Latitude,
                                locations[vertex].Longitude);
            }

            // build a sorted version in-place.
            graph.Sort(n);

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

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

            // confirm sort.
            for (uint vertex = 0; vertex < graph.VertexCount; vertex++)
            {
                float latitude, longitude;
                graph.GetVertex(vertex, out latitude, out longitude);
                Assert.AreEqual(latitude, locations[(int)vertex].Latitude);
                Assert.AreEqual(longitude, locations[(int)vertex].Longitude);
            }
        }
Exemple #9
0
        public void TestTwoEdgesWithIsBetter()
        {
            var vertex0 = new Coordinate(51.26779566943717f, 4.801347255706787f);
            var vertex1 = new Coordinate(51.26689735000000f, 4.801347255706787f);
            var vertex2 = new Coordinate(51.26689735000000f, 4.801347255706787f);

            var graph = new GeometricGraph(1);

            graph.AddVertex(0, vertex0.Latitude, vertex0.Longitude);
            graph.AddVertex(1, vertex1.Latitude, vertex1.Longitude);
            graph.AddVertex(2, vertex2.Latitude, vertex2.Longitude);
            graph.AddEdge(0, 1, new uint[] { 0 }, null);
            var expectedEdgeId = graph.AddEdge(0, 2, new uint[] { 1 }, null);

            // resolve on vertex0.
            var location = vertex0;
            var resolver = new ResolveAlgorithm(graph, location.Latitude, location.Longitude, Constants.DefaultMaxEdgeDistance, 50f,
                                                (edge) => { return(true); }, (edge) => { return(edge.Data[0] == 1); });

            resolver.Run();

            var result = resolver.Result;

            Assert.IsNotNull(result);
            Assert.AreEqual(expectedEdgeId, result.EdgeId);

            // resolve on vertex1.
            location = vertex1;
            resolver = new ResolveAlgorithm(graph, location.Latitude, location.Longitude, Constants.DefaultMaxEdgeDistance, 50f,
                                            (edge) => { return(true); }, (edge) => { return(edge.Data[0] == 1); });
            resolver.Run();

            result = resolver.Result;
            Assert.IsNotNull(result);
            Assert.AreEqual(expectedEdgeId, result.EdgeId);

            // resolve right in between.
            location = new Coordinate((vertex0.Latitude + vertex1.Latitude) / 2,
                                      (vertex0.Longitude + vertex1.Longitude) / 2);
            resolver = new ResolveAlgorithm(graph, location.Latitude, location.Longitude, Constants.DefaultMaxEdgeDistance, 50f,
                                            (edge) => { return(true); }, (edge) => { return(edge.Data[0] == 1); });
            resolver.Run();

            result = resolver.Result;
            Assert.IsNotNull(result);
            Assert.AreEqual(expectedEdgeId, result.EdgeId);
        }
Exemple #10
0
        public void TestSerialize()
        {
            var graph = new GeometricGraph(1, 100);

            // add one edge.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 10 }, null);

            // serialize.
            graph.Compress();
            var expectedSize = graph.SizeInBytes;

            using (var stream = new System.IO.MemoryStream())
            {
                Assert.AreEqual(expectedSize, graph.Serialize(stream));
            }

            graph = new GeometricGraph(1, 100);

            // add one edge.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddVertex(2, 0, 0);
            graph.AddVertex(3, 0, 0);
            graph.AddVertex(4, 0, 0);
            graph.AddVertex(5, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 10 }, null);
            graph.AddEdge(0, 2, new uint[] { 20 }, null);
            graph.AddEdge(0, 3, new uint[] { 30 }, null);
            graph.AddEdge(0, 4, new uint[] { 40 }, null);
            graph.AddEdge(5, 1, new uint[] { 50 }, null);
            graph.AddEdge(5, 2, new uint[] { 60 }, null);
            graph.AddEdge(5, 3, new uint[] { 70 }, null);
            graph.AddEdge(5, 4, new uint[] { 80 }, null);

            // serialize.
            graph.Compress();
            expectedSize = graph.SizeInBytes;
            using (var stream = new System.IO.MemoryStream())
            {
                Assert.AreEqual(expectedSize, graph.Serialize(stream));
            }
        }
Exemple #11
0
        public void TestRemoveEdge()
        {
            var graph = new GeometricGraph(1, 100);

            // add and remove edge.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 1 }, null);
            Assert.IsTrue(graph.RemoveEdges(0, 1) != 0);

            graph = new GeometricGraph(1, 100);

            // add and remove edge.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            var edge = graph.AddEdge(0, 1, new uint[] { 10 }, null);

            Assert.IsTrue(graph.RemoveEdge(edge));
        }
Exemple #12
0
        public void TestOneEdge()
        {
            var vertex0 = new Coordinate(51.26779566943717f, 4.801347255706787f);
            var vertex1 = new Coordinate(51.26689735000000f, 4.801347255706787f);

            var graph = new GeometricGraph(1);

            graph.AddVertex(0, (float)vertex0.Latitude, (float)vertex0.Longitude);
            graph.AddVertex(1, (float)vertex1.Latitude, (float)vertex1.Longitude);
            graph.AddEdge(0, 1, new uint[] { 0 }, null);

            // resolve on vertex0.
            var location = vertex0;
            var resolver = new ResolveAlgorithm(graph, (float)location.Latitude, (float)location.Longitude,
                                                Constants.DefaultMaxEdgeDistance, 50f,
                                                (edge) => { return(true); });

            resolver.Run();

            var result = resolver.Result;

            Assert.IsNotNull(result);

            // resolve on vertex1.
            location = vertex1;
            resolver = new ResolveAlgorithm(graph, (float)location.Latitude, (float)location.Longitude, Constants.DefaultMaxEdgeDistance, 50f,
                                            (edge) => { return(true); });
            resolver.Run();

            result = resolver.Result;
            Assert.IsNotNull(result);

            // resolve right in between.
            location = new Coordinate((vertex0.Latitude + vertex1.Latitude) / 2,
                                      (vertex0.Longitude + vertex1.Longitude) / 2);
            resolver = new ResolveAlgorithm(graph, (float)location.Latitude, (float)location.Longitude, Constants.DefaultMaxEdgeDistance, 50f,
                                            (edge) => { return(true); });
            resolver.Run();

            result = resolver.Result;
            Assert.IsNotNull(result);
        }
Exemple #13
0
        public void SearchCloseVerticesTest()
        {
            var DefaultHilbertSteps = (int)System.Math.Pow(2, 15);

            // build locations.
            var locations = new List <Coordinate>();

            locations.Add(new Coordinate(53.06905f, 29.40519f));
            locations.Add(new Coordinate(53.1183f, 29.31932f));
            locations.Add(new Coordinate(53.08611f, 29.37143f));
            locations.Add(new Coordinate(53.05757f, 29.44653f));

            // build graph.
            var graph  = new GeometricGraph(1);
            int vertex = 0;

            for (vertex = 0; vertex < locations.Count; vertex++)
            {
                graph.AddVertex((uint)vertex, locations[vertex].Latitude,
                                locations[vertex].Longitude);
            }

            // build a sorted version in-place.
            graph.Sort(DefaultHilbertSteps);
            var closelist1 = graph.Search(53.0695f, 29.40594f, 0.1f);

            Assert.AreEqual(4, closelist1.Count);

            // add another vertex.
            graph.AddVertex((uint)vertex, 52.7362f, 29.4935f);
            graph.Sort(DefaultHilbertSteps);

            // build a sorted version in-place.
            var closelist2 = graph.Search(53.0695f, 29.40594f, 0.1f);

            Assert.IsTrue(closelist2.Count >= 3, "some close vertex missed");
        }
Exemple #14
0
        public void TestCompress()
        {
            var graph = new GeometricGraph(1, 100);

            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            var edgeId = graph.AddEdge(0, 1, new uint[] { 10 }, new Coordinate()
            {
                Latitude  = 1,
                Longitude = 1
            });

            graph.Compress();

            var edge = graph.GetEdge(edgeId);

            Assert.IsNotNull(edge);
            Assert.AreEqual(0, edge.From);
            Assert.AreEqual(1, edge.To);
            Assert.AreEqual(edgeId, edge.Id);
            Assert.AreEqual(1, edge.Shape.Count);
            Assert.AreEqual(1, edge.Shape.First().Latitude);
            Assert.AreEqual(1, edge.Shape.First().Longitude);
        }
Exemple #15
0
        public void TestGetEdgeEnumerator()
        {
            var graph = new GeometricGraph(1, 100);

            // add edges.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddVertex(2, 0, 0);
            graph.AddVertex(3, 0, 0);
            graph.AddVertex(4, 0, 0);
            graph.AddVertex(5, 0, 0);
            var edge1 = graph.AddEdge(0, 1, new uint[] { 10 }, null);
            var edge2 = graph.AddEdge(1, 2, new uint[] { 20 }, null);
            var edge3 = graph.AddEdge(1, 3, new uint[] { 30 }, null);
            var edge4 = graph.AddEdge(3, 4, new uint[] { 40 }, null);
            var edge5 = graph.AddEdge(4, 1, new uint[] { 50 }, null);
            var edge6 = graph.AddEdge(5, 1, new uint[] { 60 }, null);

            // get empty edge enumerator.
            var edges = graph.GetEdgeEnumerator();

            Assert.IsFalse(edges.HasData);

            // move to vertices and test result.
            Assert.IsTrue(edges.MoveTo(0));
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(10, edges.First().Data[0]);
            Assert.AreEqual(1, edges.First().To);

            Assert.IsTrue(edges.MoveTo(1));
            Assert.AreEqual(5, edges.Count());
            Assert.IsTrue(edges.Any(x => x.To == 0));
            Assert.AreEqual(true, edges.First(x => x.To == 0).DataInverted);
            Assert.AreEqual(10, edges.First(x => x.To == 0).Data[0]);
            Assert.AreEqual(edge1, edges.First(x => x.To == 0).Id);
            Assert.IsTrue(edges.Any(x => x.To == 2));
            Assert.AreEqual(20, edges.First(x => x.To == 2).Data[0]);
            Assert.AreEqual(edge2, edges.First(x => x.To == 2).Id);
            Assert.IsTrue(edges.Any(x => x.To == 3));
            Assert.AreEqual(30, edges.First(x => x.To == 3).Data[0]);
            Assert.AreEqual(edge3, edges.First(x => x.To == 3).Id);
            Assert.IsTrue(edges.Any(x => x.To == 4));
            Assert.AreEqual(true, edges.First(x => x.To == 4).DataInverted);
            Assert.AreEqual(50, edges.First(x => x.To == 4).Data[0]);
            Assert.AreEqual(edge5, edges.First(x => x.To == 4).Id);
            Assert.IsTrue(edges.Any(x => x.To == 5));
            Assert.AreEqual(true, edges.First(x => x.To == 5).DataInverted);
            Assert.AreEqual(60, edges.First(x => x.To == 5).Data[0]);
            Assert.AreEqual(edge6, edges.First(x => x.To == 5).Id);

            Assert.IsTrue(edges.MoveTo(2));
            Assert.AreEqual(1, edges.Count());
            Assert.IsTrue(edges.Any(x => x.To == 1));
            Assert.AreEqual(true, edges.First(x => x.To == 1).DataInverted);
            Assert.AreEqual(20, edges.First(x => x.To == 1).Data[0]);
            Assert.AreEqual(edge2, edges.First(x => x.To == 1).Id);

            Assert.IsTrue(edges.MoveTo(3));
            Assert.AreEqual(2, edges.Count());
            Assert.IsTrue(edges.Any(x => x.To == 1));
            Assert.AreEqual(true, edges.First(x => x.To == 1).DataInverted);
            Assert.AreEqual(30, edges.First(x => x.To == 1).Data[0]);
            Assert.AreEqual(edge3, edges.First(x => x.To == 1).Id);
            Assert.IsTrue(edges.Any(x => x.To == 4));
            Assert.AreEqual(40, edges.First(x => x.To == 4).Data[0]);
            Assert.AreEqual(edge4, edges.First(x => x.To == 4).Id);

            Assert.IsTrue(edges.MoveTo(4));
            Assert.AreEqual(2, edges.Count());
            Assert.IsTrue(edges.Any(x => x.To == 3));
            Assert.AreEqual(true, edges.First(x => x.To == 3).DataInverted);
            Assert.AreEqual(40, edges.First(x => x.To == 3).Data[0]);
            Assert.AreEqual(edge4, edges.First(x => x.To == 3).Id);
            Assert.IsTrue(edges.Any(x => x.To == 1));
            Assert.AreEqual(50, edges.First(x => x.To == 1).Data[0]);
            Assert.AreEqual(edge5, edges.First(x => x.To == 1).Id);

            Assert.IsTrue(edges.MoveTo(5));
            Assert.AreEqual(1, edges.Count());
            Assert.IsTrue(edges.Any(x => x.To == 1));
            Assert.AreEqual(60, edges.First(x => x.To == 1).Data[0]);
            Assert.AreEqual(edge6, edges.First(x => x.To == 1).Id);
        }
Exemple #16
0
        public void TestSwitch()
        {
            var graph = new GeometricGraph(1, 100);

            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 1 }, null);

            graph.Switch(0, 1);

            // verify all edges.
            var edges = graph.GetEdgeEnumerator(0);

            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(1, edges.First().To);
            Assert.AreEqual(true, edges.First().DataInverted);
            Assert.AreEqual(1, edges.First().Data[0]);
            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(0, edges.First().To);
            Assert.AreEqual(1, edges.First().Data[0]);

            graph = new GeometricGraph(1, 100);
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddVertex(2, 0, 0);
            graph.AddVertex(3, 0, 0);
            graph.AddVertex(4, 0, 0);
            graph.AddVertex(5, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 1 }, null);
            graph.AddEdge(0, 2, new uint[] { 2 }, null);
            graph.AddEdge(0, 3, new uint[] { 3 }, null);
            graph.AddEdge(0, 4, new uint[] { 4 }, null);
            graph.AddEdge(5, 1, new uint[] { 5 }, null);
            graph.AddEdge(5, 2, new uint[] { 6 }, null);
            graph.AddEdge(5, 3, new uint[] { 7 }, null);
            graph.AddEdge(5, 4, new uint[] { 8 }, null);

            graph.Switch(0, 1);

            // verify all edges.
            edges = graph.GetEdgeEnumerator(0);
            Assert.AreEqual(2, edges.Count());
            Assert.AreEqual(true, edges.First(x => x.To == 1).DataInverted);
            Assert.AreEqual(1, edges.First(x => x.To == 1).Data[0]);
            Assert.AreEqual(true, edges.First(x => x.To == 5).DataInverted);
            Assert.AreEqual(5, edges.First(x => x.To == 5).Data[0]);
            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(4, edges.Count());
            Assert.AreEqual(1, edges.First(x => x.To == 0).Data[0]);
            Assert.AreEqual(2, edges.First(x => x.To == 2).Data[0]);
            Assert.AreEqual(3, edges.First(x => x.To == 3).Data[0]);
            Assert.AreEqual(4, edges.First(x => x.To == 4).Data[0]);
            edges = graph.GetEdgeEnumerator(2);
            Assert.AreEqual(2, edges.Count());
            Assert.AreEqual(true, edges.First(x => x.To == 1).DataInverted);
            Assert.AreEqual(2, edges.First(x => x.To == 1).Data[0]);
            Assert.AreEqual(true, edges.First(x => x.To == 5).DataInverted);
            Assert.AreEqual(6, edges.First(x => x.To == 5).Data[0]);
            edges = graph.GetEdgeEnumerator(3);
            Assert.AreEqual(2, edges.Count());
            Assert.AreEqual(true, edges.First(x => x.To == 1).DataInverted);
            Assert.AreEqual(3, edges.First(x => x.To == 1).Data[0]);
            Assert.AreEqual(true, edges.First(x => x.To == 5).DataInverted);
            Assert.AreEqual(7, edges.First(x => x.To == 5).Data[0]);
            edges = graph.GetEdgeEnumerator(4);
            Assert.AreEqual(2, edges.Count());
            Assert.AreEqual(true, edges.First(x => x.To == 1).DataInverted);
            Assert.AreEqual(4, edges.First(x => x.To == 1).Data[0]);
            Assert.AreEqual(true, edges.First(x => x.To == 5).DataInverted);
            Assert.AreEqual(8, edges.First(x => x.To == 5).Data[0]);

            graph.Switch(0, 1);

            // verify all edges.
            edges = graph.GetEdgeEnumerator(0);
            Assert.AreEqual(4, edges.Count());
            Assert.AreEqual(1, edges.First(x => x.To == 1).Data[0]);
            Assert.AreEqual(2, edges.First(x => x.To == 2).Data[0]);
            Assert.AreEqual(3, edges.First(x => x.To == 3).Data[0]);
            Assert.AreEqual(4, edges.First(x => x.To == 4).Data[0]);
            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(2, edges.Count());
            Assert.AreEqual(true, edges.First(x => x.To == 0).DataInverted);
            Assert.AreEqual(1, edges.First(x => x.To == 0).Data[0]);
            Assert.AreEqual(true, edges.First(x => x.To == 5).DataInverted);
            Assert.AreEqual(5, edges.First(x => x.To == 5).Data[0]);
            edges = graph.GetEdgeEnumerator(2);
            Assert.AreEqual(2, edges.Count());
            Assert.AreEqual(true, edges.First(x => x.To == 0).DataInverted);
            Assert.AreEqual(2, edges.First(x => x.To == 0).Data[0]);
            Assert.AreEqual(true, edges.First(x => x.To == 5).DataInverted);
            Assert.AreEqual(6, edges.First(x => x.To == 5).Data[0]);
            edges = graph.GetEdgeEnumerator(3);
            Assert.AreEqual(2, edges.Count());
            Assert.AreEqual(true, edges.First(x => x.To == 0).DataInverted);
            Assert.AreEqual(3, edges.First(x => x.To == 0).Data[0]);
            Assert.AreEqual(true, edges.First(x => x.To == 5).DataInverted);
            Assert.AreEqual(7, edges.First(x => x.To == 5).Data[0]);
            edges = graph.GetEdgeEnumerator(4);
            Assert.AreEqual(2, edges.Count());
            Assert.AreEqual(true, edges.First(x => x.To == 0).DataInverted);
            Assert.AreEqual(4, edges.First(x => x.To == 0).Data[0]);
            Assert.AreEqual(true, edges.First(x => x.To == 5).DataInverted);
            Assert.AreEqual(8, edges.First(x => x.To == 5).Data[0]);
        }
Exemple #17
0
        public void TestDeserialize()
        {
            var graph = new GeometricGraph(1, 100);

            // add one edge.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 1 }, null);

            // serialize.
            using (var stream = new System.IO.MemoryStream())
            {
                var size = graph.Serialize(stream);
                stream.Seek(0, System.IO.SeekOrigin.Begin);

                var deserializedGraph = GeometricGraph.Deserialize(stream, GeometricGraphProfile.Default);
                Assert.AreEqual(size, stream.Position);

                Assert.AreEqual(2, deserializedGraph.VertexCount);
                Assert.AreEqual(1, deserializedGraph.EdgeCount);

                // verify all edges.
                var edges = deserializedGraph.GetEdgeEnumerator(0);
                Assert.AreEqual(1, edges.Count());
                Assert.AreEqual(1, edges.First().Data[0]);
                Assert.AreEqual(1, edges.First().To);

                edges = deserializedGraph.GetEdgeEnumerator(1);
                Assert.AreEqual(1, edges.Count());
                Assert.AreEqual(true, edges.First().DataInverted);
                Assert.AreEqual(1, edges.First().Data[0]);
                Assert.AreEqual(0, edges.First().To);
            }

            graph = new GeometricGraph(1, 100);

            // add one edge.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 0, 0);
            graph.AddVertex(2, 0, 0);
            graph.AddVertex(3, 0, 0);
            graph.AddVertex(4, 0, 0);
            graph.AddVertex(5, 0, 0);
            graph.AddEdge(0, 1, new uint[] { 1 }, null);
            graph.AddEdge(0, 2, new uint[] { 2 }, null);
            graph.AddEdge(0, 3, new uint[] { 3 }, null);
            graph.AddEdge(0, 4, new uint[] { 4 }, null);
            graph.AddEdge(5, 1, new uint[] { 5 }, null);
            graph.AddEdge(5, 2, new uint[] { 6 }, null);
            graph.AddEdge(5, 3, new uint[] { 7 }, null);
            graph.AddEdge(5, 4, new uint[] { 8 }, null);

            // serialize.
            using (var stream = new System.IO.MemoryStream())
            {
                var size = graph.Serialize(stream);
                stream.Seek(0, System.IO.SeekOrigin.Begin);

                var deserializedGraph = GeometricGraph.Deserialize(stream, GeometricGraphProfile.Default);
                Assert.AreEqual(size, stream.Position);

                Assert.AreEqual(6, deserializedGraph.VertexCount);
                Assert.AreEqual(8, deserializedGraph.EdgeCount);
            }
        }
Exemple #18
0
        public void SearchVerticesTest()
        {
            var graph = new GeometricGraph(1);

            graph.AddVertex(0, .00f, .00f);
            graph.AddVertex(1, .02f, .00f);
            graph.AddVertex(2, .04f, .00f);
            graph.AddVertex(3, .06f, .00f);
            graph.AddVertex(4, .08f, .00f);
            graph.AddVertex(5, .00f, .02f);
            graph.AddVertex(6, .02f, .02f);
            graph.AddVertex(7, .04f, .02f);
            graph.AddVertex(8, .06f, .02f);
            graph.AddVertex(9, .08f, .02f);
            graph.AddVertex(10, .00f, .04f);
            graph.AddVertex(11, .02f, .04f);
            graph.AddVertex(12, .04f, .04f);
            graph.AddVertex(13, .06f, .04f);
            graph.AddVertex(14, .08f, .04f);
            graph.AddVertex(15, .00f, .06f);
            graph.AddVertex(16, .02f, .06f);
            graph.AddVertex(17, .04f, .06f);
            graph.AddVertex(18, .06f, .06f);
            graph.AddVertex(19, .08f, .06f);
            graph.AddVertex(20, .00f, .08f);
            graph.AddVertex(21, .02f, .08f);
            graph.AddVertex(22, .04f, .08f);
            graph.AddVertex(23, .06f, .08f);
            graph.AddVertex(24, .08f, .08f);

            graph.Sort();

            // test 0.009, 0.009, 0.019, 0.019
            var vertices = graph.Search(0.009f, 0.009f, 0.029f, 0.029f);

            Assert.AreEqual(1, vertices.Count);
            Assert.IsTrue(vertices.Contains(3));
            var location = graph.GetVertex(3);

            Assert.AreEqual(.02f, location.Latitude);
            Assert.AreEqual(.02f, location.Longitude);

            // test 0.009, 0.009, 0.099, 0.019
            vertices = graph.Search(0.009f, 0.009f, 0.099f, 0.029f);
            Assert.AreEqual(4, vertices.Count);
            Assert.IsTrue(vertices.Contains(3));
            location = graph.GetVertex(3);
            Assert.AreEqual(.02f, location.Latitude);
            Assert.AreEqual(.02f, location.Longitude);
            Assert.IsTrue(vertices.Contains(13));
            location = graph.GetVertex(13);
            Assert.AreEqual(.04f, location.Latitude);
            Assert.AreEqual(.02f, location.Longitude);
            Assert.IsTrue(vertices.Contains(16));
            location = graph.GetVertex(16);
            Assert.AreEqual(.06f, location.Latitude);
            Assert.AreEqual(.02f, location.Longitude);
            Assert.IsTrue(vertices.Contains(19));
            location = graph.GetVertex(19);
            Assert.AreEqual(.08f, location.Latitude);
            Assert.AreEqual(.02f, location.Longitude);

            // test -0.001, -0.001, 0.09, 0.09
            vertices = graph.Search(-0.001f, -0.001f, 0.09f, 0.09f);
            Assert.AreEqual(25, vertices.Count);
            Assert.IsTrue(vertices.Contains(0));
            location = graph.GetVertex(0);
            Assert.AreEqual(.0f, location.Latitude);
            Assert.AreEqual(.0f, location.Longitude);
            Assert.IsTrue(vertices.Contains(1));
            location = graph.GetVertex(1);
            Assert.AreEqual(.0f, location.Latitude);
            Assert.AreEqual(.02f, location.Longitude);
            Assert.IsTrue(vertices.Contains(2));
            location = graph.GetVertex(2);
            Assert.AreEqual(.02f, location.Latitude);
            Assert.AreEqual(.0f, location.Longitude);
            Assert.IsTrue(vertices.Contains(3));
            location = graph.GetVertex(3);
            Assert.AreEqual(.02f, location.Latitude);
            Assert.AreEqual(.02f, location.Longitude);
            Assert.IsTrue(vertices.Contains(4));
            location = graph.GetVertex(4);
            Assert.AreEqual(.02f, location.Latitude);
            Assert.AreEqual(.04f, location.Longitude);
            Assert.IsTrue(vertices.Contains(5));
            location = graph.GetVertex(5);
            Assert.AreEqual(.00f, location.Latitude);
            Assert.AreEqual(.04f, location.Longitude);
            Assert.IsTrue(vertices.Contains(6));
            location = graph.GetVertex(6);
            Assert.AreEqual(.00f, location.Latitude);
            Assert.AreEqual(.06f, location.Longitude);
            Assert.IsTrue(vertices.Contains(7));
            location = graph.GetVertex(7);
            Assert.AreEqual(.00f, location.Latitude);
            Assert.AreEqual(.08f, location.Longitude);
            Assert.IsTrue(vertices.Contains(8));
            location = graph.GetVertex(8);
            Assert.AreEqual(.02f, location.Latitude);
            Assert.AreEqual(.08f, location.Longitude);
            Assert.IsTrue(vertices.Contains(9));
            location = graph.GetVertex(9);
            Assert.AreEqual(.02f, location.Latitude);
            Assert.AreEqual(.06f, location.Longitude);
            Assert.IsTrue(vertices.Contains(10));
            location = graph.GetVertex(10);
            Assert.AreEqual(.04f, location.Latitude);
            Assert.AreEqual(.08f, location.Longitude);
            Assert.IsTrue(vertices.Contains(11));
            location = graph.GetVertex(11);
            Assert.AreEqual(.04f, location.Latitude);
            Assert.AreEqual(.06f, location.Longitude);
            Assert.IsTrue(vertices.Contains(12));
            location = graph.GetVertex(12);
            Assert.AreEqual(.04f, location.Latitude);
            Assert.AreEqual(.04f, location.Longitude);
            Assert.IsTrue(vertices.Contains(13));
            location = graph.GetVertex(13);
            Assert.AreEqual(.04f, location.Latitude);
            Assert.AreEqual(.02f, location.Longitude);
            Assert.IsTrue(vertices.Contains(14));
            location = graph.GetVertex(14);
            Assert.AreEqual(.04f, location.Latitude);
            Assert.AreEqual(.00f, location.Longitude);
            Assert.IsTrue(vertices.Contains(15));
            location = graph.GetVertex(15);
            Assert.AreEqual(.06f, location.Latitude);
            Assert.AreEqual(.04f, location.Longitude);
            Assert.IsTrue(vertices.Contains(16));
            location = graph.GetVertex(16);
            Assert.AreEqual(.06f, location.Latitude);
            Assert.AreEqual(.02f, location.Longitude);
            Assert.IsTrue(vertices.Contains(17));
            location = graph.GetVertex(17);
            Assert.AreEqual(.06f, location.Latitude);
            Assert.AreEqual(.00f, location.Longitude);
            Assert.IsTrue(vertices.Contains(18));
            location = graph.GetVertex(18);
            Assert.AreEqual(.08f, location.Latitude);
            Assert.AreEqual(.00f, location.Longitude);
            Assert.IsTrue(vertices.Contains(19));
            location = graph.GetVertex(19);
            Assert.AreEqual(.08f, location.Latitude);
            Assert.AreEqual(.02f, location.Longitude);
            Assert.IsTrue(vertices.Contains(20));
            location = graph.GetVertex(20);
            Assert.AreEqual(.08f, location.Latitude);
            Assert.AreEqual(.04f, location.Longitude);
            Assert.IsTrue(vertices.Contains(21));
            location = graph.GetVertex(21);
            Assert.AreEqual(.08f, location.Latitude);
            Assert.AreEqual(.06f, location.Longitude);
            Assert.IsTrue(vertices.Contains(22));
            location = graph.GetVertex(22);
            Assert.AreEqual(.08f, location.Latitude);
            Assert.AreEqual(.08f, location.Longitude);
            Assert.IsTrue(vertices.Contains(23));
            location = graph.GetVertex(23);
            Assert.AreEqual(.06f, location.Latitude);
            Assert.AreEqual(.08f, location.Longitude);
            Assert.IsTrue(vertices.Contains(24));
            location = graph.GetVertex(24);
            Assert.AreEqual(.06f, location.Latitude);
            Assert.AreEqual(.06f, location.Longitude);
        }
Exemple #19
0
        public void TestAddEdgeShape()
        {
            var graph = new GeometricGraph(2, 100);

            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 1, 1);
            graph.AddVertex(2, 0, 1);
            graph.AddVertex(3, 0, 2);
            graph.AddEdge(0, 1, new uint[] { 0, 10 }, new Coordinate(0.25f, 0.25f),
                          new Coordinate(0.5f, 0.5f),
                          new Coordinate(0.75f, 0.75f));
            graph.AddEdge(1, 2, new uint[] { 10, 20 }, new Coordinate(0.75f, 1),
                          new Coordinate(0.5f, 1),
                          new Coordinate(0.25f, 1));
            graph.AddEdge(2, 3, new uint[] { 20, 30 }, new Coordinate(0, 1.25f),
                          new Coordinate(0, 1.5f),
                          new Coordinate(0, 1.75f));

            // verify all edges.
            var edges = graph.GetEdgeEnumerator(0);

            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(1, edges.First().To);
            Assert.IsNotNull(edges.First().Shape);
            var shape = new List <Coordinate>(edges.First().Shape);

            Assert.AreEqual(0.25, shape[0].Latitude);
            Assert.AreEqual(0.25, shape[0].Longitude);
            Assert.AreEqual(0.5, shape[1].Latitude);
            Assert.AreEqual(0.5, shape[1].Longitude);
            Assert.AreEqual(0.75, shape[2].Latitude);
            Assert.AreEqual(0.75, shape[2].Longitude);
            shape = new List <Coordinate>(edges.First().Shape.Reverse());
            Assert.AreEqual(0.25, shape[2].Latitude);
            Assert.AreEqual(0.25, shape[2].Longitude);
            Assert.AreEqual(0.5, shape[1].Latitude);
            Assert.AreEqual(0.5, shape[1].Longitude);
            Assert.AreEqual(0.75, shape[0].Latitude);
            Assert.AreEqual(0.75, shape[0].Longitude);

            edges = graph.GetEdgeEnumerator(1);
            shape = new List <Coordinate>(edges.First(x => x.To == 0).Shape);
            Assert.AreEqual(0.25, shape[0].Latitude);
            Assert.AreEqual(0.25, shape[0].Longitude);
            Assert.AreEqual(0.5, shape[1].Latitude);
            Assert.AreEqual(0.5, shape[1].Longitude);
            Assert.AreEqual(0.75, shape[2].Latitude);
            Assert.AreEqual(0.75, shape[2].Longitude);
            shape = new List <Coordinate>(edges.First(x => x.To == 0).Shape.Reverse());
            Assert.AreEqual(0.25, shape[2].Latitude);
            Assert.AreEqual(0.25, shape[2].Longitude);
            Assert.AreEqual(0.5, shape[1].Latitude);
            Assert.AreEqual(0.5, shape[1].Longitude);
            Assert.AreEqual(0.75, shape[0].Latitude);
            Assert.AreEqual(0.75, shape[0].Longitude);
            shape = new List <Coordinate>(edges.First(x => x.To == 2).Shape);
            Assert.AreEqual(0.75, shape[0].Latitude);
            Assert.AreEqual(1, shape[0].Longitude);
            Assert.AreEqual(0.5, shape[1].Latitude);
            Assert.AreEqual(1, shape[1].Longitude);
            Assert.AreEqual(0.25, shape[2].Latitude);
            Assert.AreEqual(1, shape[2].Longitude);
            shape = new List <Coordinate>(edges.First(x => x.To == 2).Shape.Reverse());
            Assert.AreEqual(0.75, shape[2].Latitude);
            Assert.AreEqual(1, shape[2].Longitude);
            Assert.AreEqual(0.5, shape[1].Latitude);
            Assert.AreEqual(1, shape[1].Longitude);
            Assert.AreEqual(0.25, shape[0].Latitude);
            Assert.AreEqual(1, shape[0].Longitude);

            edges = graph.GetEdgeEnumerator(2);
            shape = new List <Coordinate>(edges.First(x => x.To == 1).Shape);
            Assert.AreEqual(0.75, shape[0].Latitude);
            Assert.AreEqual(1, shape[0].Longitude);
            Assert.AreEqual(0.5, shape[1].Latitude);
            Assert.AreEqual(1, shape[1].Longitude);
            Assert.AreEqual(0.25, shape[2].Latitude);
            Assert.AreEqual(1, shape[2].Longitude);
            shape = new List <Coordinate>(edges.First(x => x.To == 1).Shape.Reverse());
            Assert.AreEqual(0.75, shape[2].Latitude);
            Assert.AreEqual(1, shape[2].Longitude);
            Assert.AreEqual(0.5, shape[1].Latitude);
            Assert.AreEqual(1, shape[1].Longitude);
            Assert.AreEqual(0.25, shape[0].Latitude);
            Assert.AreEqual(1, shape[0].Longitude);
            shape = new List <Coordinate>(edges.First(x => x.To == 3).Shape);
            Assert.AreEqual(0, shape[0].Latitude);
            Assert.AreEqual(1.25, shape[0].Longitude);
            Assert.AreEqual(0, shape[1].Latitude);
            Assert.AreEqual(1.5, shape[1].Longitude);
            Assert.AreEqual(0, shape[2].Latitude);
            Assert.AreEqual(1.75, shape[2].Longitude);
            shape = new List <Coordinate>(edges.First(x => x.To == 3).Shape.Reverse());
            Assert.AreEqual(0, shape[2].Latitude);
            Assert.AreEqual(1.25, shape[2].Longitude);
            Assert.AreEqual(0, shape[1].Latitude);
            Assert.AreEqual(1.5, shape[1].Longitude);
            Assert.AreEqual(0, shape[0].Latitude);
            Assert.AreEqual(1.75, shape[0].Longitude);

            edges = graph.GetEdgeEnumerator(3);
            shape = new List <Coordinate>(edges.First().Shape);
            Assert.AreEqual(0, shape[0].Latitude);
            Assert.AreEqual(1.25, shape[0].Longitude);
            Assert.AreEqual(0, shape[1].Latitude);
            Assert.AreEqual(1.5, shape[1].Longitude);
            Assert.AreEqual(0, shape[2].Latitude);
            Assert.AreEqual(1.75, shape[2].Longitude);
            shape = new List <Coordinate>(edges.First().Shape.Reverse());
            Assert.AreEqual(0, shape[2].Latitude);
            Assert.AreEqual(1.25, shape[2].Longitude);
            Assert.AreEqual(0, shape[1].Latitude);
            Assert.AreEqual(1.5, shape[1].Longitude);
            Assert.AreEqual(0, shape[0].Latitude);
            Assert.AreEqual(1.75, shape[0].Longitude);
        }
Exemple #20
0
        public void TestAddEdge()
        {
            var graph = new GeometricGraph(1, 100);

            // add edge.
            graph.AddVertex(0, 0, 0);
            graph.AddVertex(1, 1, 1);
            var edgeId1 = graph.AddEdge(0, 1, new uint[] { 10 }, null);

            Assert.AreEqual(0, edgeId1);

            // verify all edges.
            var edges = graph.GetEdgeEnumerator(0);

            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(10, edges.First().Data[0]);
            Assert.AreEqual(edgeId1, edges.First().Id);
            Assert.AreEqual(0, edges.First().From);
            Assert.AreEqual(1, edges.First().To);

            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(true, edges.First().DataInverted);
            Assert.AreEqual(10, edges.First().Data[0]);
            Assert.AreEqual(edgeId1, edges.First().Id);
            Assert.AreEqual(1, edges.First().From);
            Assert.AreEqual(0, edges.First().To);

            // add another edge.
            graph.AddVertex(2, 2, 2);
            var edgeId2 = graph.AddEdge(1, 2, new uint[] { 20 }, null);

            // verify all edges.
            edges = graph.GetEdgeEnumerator(0);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(10, edges.First().Data[0]);
            Assert.AreEqual(edgeId1, edges.First().Id);
            Assert.AreEqual(0, edges.First().From);
            Assert.AreEqual(1, edges.First().To);

            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(2, edges.Count());
            Assert.IsTrue(edges.Any(x => x.To == 0));
            Assert.AreEqual(true, edges.First(x => x.To == 0).DataInverted);
            Assert.AreEqual(10, edges.First(x => x.To == 0).Data[0]);
            Assert.AreEqual(edgeId1, edges.First(x => x.To == 0).Id);
            Assert.AreEqual(1, edges.First(x => x.To == 0).From);
            Assert.IsTrue(edges.Any(x => x.To == 2));
            Assert.AreEqual(20, edges.First(x => x.To == 2).Data[0]);
            Assert.AreEqual(edgeId2, edges.First(x => x.To == 2).Id);
            Assert.AreEqual(1, edges.First(x => x.To == 2).From);

            edges = graph.GetEdgeEnumerator(2);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(true, edges.First().DataInverted);
            Assert.AreEqual(20, edges.First().Data[0]);
            Assert.AreEqual(edgeId2, edges.First().Id);
            Assert.AreEqual(2, edges.First().From);
            Assert.AreEqual(1, edges.First().To);

            // add another edge.
            graph.AddVertex(3, 3, 3);
            var edgeId3 = graph.AddEdge(1, 3, new uint[] { 30 }, null);

            // verify all edges.
            edges = graph.GetEdgeEnumerator(0);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(10, edges.First().Data[0]);
            Assert.AreEqual(edgeId1, edges.First().Id);
            Assert.AreEqual(0, edges.First().From);
            Assert.AreEqual(1, edges.First().To);

            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(3, edges.Count());
            Assert.IsTrue(edges.Any(x => x.To == 0));
            Assert.AreEqual(true, edges.First(x => x.To == 0).DataInverted);
            Assert.AreEqual(10, edges.First(x => x.To == 0).Data[0]);
            Assert.AreEqual(edgeId1, edges.First(x => x.To == 0).Id);
            Assert.AreEqual(1, edges.First(x => x.To == 0).From);
            Assert.IsTrue(edges.Any(x => x.To == 2));
            Assert.AreEqual(20, edges.First(x => x.To == 2).Data[0]);
            Assert.AreEqual(edgeId2, edges.First(x => x.To == 2).Id);
            Assert.AreEqual(1, edges.First(x => x.To == 2).From);
            Assert.IsTrue(edges.Any(x => x.To == 3));
            Assert.AreEqual(30, edges.First(x => x.To == 3).Data[0]);
            Assert.AreEqual(edgeId3, edges.First(x => x.To == 3).Id);
            Assert.AreEqual(1, edges.First(x => x.To == 3).From);

            edges = graph.GetEdgeEnumerator(2);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(edgeId2, edges.First().Id);
            Assert.AreEqual(true, edges.First().DataInverted);
            Assert.AreEqual(20, edges.First().Data[0]);
            Assert.AreEqual(2, edges.First().From);
            Assert.AreEqual(1, edges.First().To);

            edges = graph.GetEdgeEnumerator(3);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(true, edges.First().DataInverted);
            Assert.AreEqual(30, edges.First().Data[0]);
            Assert.AreEqual(edgeId3, edges.First().Id);
            Assert.AreEqual(3, edges.First().From);
            Assert.AreEqual(1, edges.First().To);

            // add another edge but in reverse.
            var edgeId4 = graph.AddEdge(3, 1, new uint[] { 31 }, null);

            // verify all edges.
            edges = graph.GetEdgeEnumerator(0);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(10, edges.First().Data[0]);
            Assert.AreEqual(edgeId1, edges.First().Id);
            Assert.AreEqual(0, edges.First().From);
            Assert.AreEqual(1, edges.First().To);

            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(4, edges.Count());
            Assert.IsTrue(edges.Any(x => x.To == 0));
            Assert.AreEqual(true, edges.First(x => x.To == 0).DataInverted);
            Assert.AreEqual(10, edges.First(x => x.To == 0).Data[0]);
            Assert.AreEqual(edgeId1, edges.First(x => x.To == 0).Id);
            Assert.AreEqual(1, edges.First(x => x.To == 0).From);
            Assert.IsTrue(edges.Any(x => x.To == 2));
            Assert.AreEqual(20, edges.First(x => x.To == 2).Data[0]);
            Assert.AreEqual(edgeId2, edges.First(x => x.To == 2).Id);
            Assert.AreEqual(1, edges.First(x => x.To == 2).From);
            Assert.IsTrue(edges.Any(x => x.To == 3 && x.Data[0] == 30));
            Assert.AreEqual(false, edges.First(x => x.To == 3 && x.Data[0] == 30).DataInverted);
            Assert.AreEqual(30, edges.First(x => x.To == 3 && x.Data[0] == 30).Data[0]);
            Assert.AreEqual(edgeId3, edges.First(x => x.To == 3 && x.Data[0] == 30).Id);
            Assert.AreEqual(1, edges.First(x => x.To == 3 && x.Data[0] == 30).From);
            Assert.IsTrue(edges.Any(x => x.To == 3 && x.Data[0] == 31));
            Assert.AreEqual(true, edges.First(x => x.To == 3 && x.Data[0] == 31).DataInverted);
            Assert.AreEqual(31, edges.First(x => x.To == 3 && x.Data[0] == 31).Data[0]);
            Assert.AreEqual(edgeId4, edges.First(x => x.To == 3 && x.Data[0] == 31).Id);
            Assert.AreEqual(1, edges.First(x => x.To == 3 && x.Data[0] == 31).From);

            edges = graph.GetEdgeEnumerator(2);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(edgeId2, edges.First().Id);
            Assert.AreEqual(true, edges.First().DataInverted);
            Assert.AreEqual(20, edges.First().Data[0]);
            Assert.AreEqual(2, edges.First().From);
            Assert.AreEqual(1, edges.First().To);

            edges = graph.GetEdgeEnumerator(3);
            Assert.AreEqual(2, edges.Count());
            Assert.AreEqual(30, edges.First(x => x.To == 1 && x.Data[0] == 30).Data[0]);
            Assert.AreEqual(edgeId3, edges.First(x => x.To == 1 && x.Data[0] == 30).Id);
            Assert.AreEqual(3, edges.First(x => x.To == 1 && x.Data[0] == 30).From);
            Assert.AreEqual(1, edges.First(x => x.To == 1 && x.Data[0] == 30).To);
            Assert.AreEqual(31, edges.First(x => x.To == 1 && x.Data[0] == 31).Data[0]);
            Assert.AreEqual(edgeId4, edges.First(x => x.To == 1 && x.Data[0] == 31).Id);
            Assert.AreEqual(3, edges.First(x => x.To == 1 && x.Data[0] == 31).From);
            Assert.AreEqual(1, edges.First(x => x.To == 1 && x.Data[0] == 31).To);

            // add another edge and start a new island.
            uint vertex4 = 4;
            uint vertex5 = 5;

            graph.AddVertex(vertex4, 4, 4);
            graph.AddVertex(vertex5, 5, 5);
            var edge5Id = graph.AddEdge(vertex4, vertex5, new uint[] { 40 }, null);

            // verify all edges.
            edges = graph.GetEdgeEnumerator(0);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(10, edges.First().Data[0]);
            Assert.AreEqual(edgeId1, edges.First().Id);
            Assert.AreEqual(0, edges.First().From);
            Assert.AreEqual(1, edges.First().To);

            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(4, edges.Count());
            Assert.IsTrue(edges.Any(x => x.To == 0));
            Assert.AreEqual(true, edges.First(x => x.To == 0).DataInverted);
            Assert.AreEqual(10, edges.First(x => x.To == 0).Data[0]);
            Assert.AreEqual(edgeId1, edges.First(x => x.To == 0).Id);
            Assert.AreEqual(1, edges.First(x => x.To == 0).From);
            Assert.IsTrue(edges.Any(x => x.To == 2));
            Assert.AreEqual(20, edges.First(x => x.To == 2).Data[0]);
            Assert.AreEqual(edgeId2, edges.First(x => x.To == 2).Id);
            Assert.AreEqual(1, edges.First(x => x.To == 2).From);
            Assert.IsTrue(edges.Any(x => x.To == 3 && x.Data[0] == 30));
            Assert.AreEqual(false, edges.First(x => x.To == 3 && x.Data[0] == 30).DataInverted);
            Assert.AreEqual(30, edges.First(x => x.To == 3 && x.Data[0] == 30).Data[0]);
            Assert.AreEqual(edgeId3, edges.First(x => x.To == 3 && x.Data[0] == 30).Id);
            Assert.AreEqual(1, edges.First(x => x.To == 3 && x.Data[0] == 30).From);
            Assert.IsTrue(edges.Any(x => x.To == 3 && x.Data[0] == 31));
            Assert.AreEqual(true, edges.First(x => x.To == 3 && x.Data[0] == 31).DataInverted);
            Assert.AreEqual(31, edges.First(x => x.To == 3 && x.Data[0] == 31).Data[0]);
            Assert.AreEqual(edgeId4, edges.First(x => x.To == 3 && x.Data[0] == 31).Id);
            Assert.AreEqual(1, edges.First(x => x.To == 3 && x.Data[0] == 31).From);

            edges = graph.GetEdgeEnumerator(2);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(edgeId2, edges.First().Id);
            Assert.AreEqual(true, edges.First().DataInverted);
            Assert.AreEqual(20, edges.First().Data[0]);
            Assert.AreEqual(2, edges.First().From);
            Assert.AreEqual(1, edges.First().To);

            edges = graph.GetEdgeEnumerator(3);
            Assert.AreEqual(2, edges.Count());
            Assert.AreEqual(30, edges.First(x => x.To == 1 && x.Data[0] == 30).Data[0]);
            Assert.AreEqual(edgeId3, edges.First(x => x.To == 1 && x.Data[0] == 30).Id);
            Assert.AreEqual(3, edges.First(x => x.To == 1 && x.Data[0] == 30).From);
            Assert.AreEqual(1, edges.First(x => x.To == 1 && x.Data[0] == 30).To);
            Assert.AreEqual(31, edges.First(x => x.To == 1 && x.Data[0] == 31).Data[0]);
            Assert.AreEqual(edgeId4, edges.First(x => x.To == 1 && x.Data[0] == 31).Id);
            Assert.AreEqual(3, edges.First(x => x.To == 1 && x.Data[0] == 31).From);
            Assert.AreEqual(1, edges.First(x => x.To == 1 && x.Data[0] == 31).To);

            edges = graph.GetEdgeEnumerator(vertex4);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(40, edges.First().Data[0]);
            Assert.AreEqual(edge5Id, edges.First().Id);
            Assert.AreEqual(vertex4, edges.First().From);
            Assert.AreEqual(vertex5, edges.First().To);

            edges = graph.GetEdgeEnumerator(vertex5);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(true, edges.First().DataInverted);
            Assert.AreEqual(40, edges.First().Data[0]);
            Assert.AreEqual(edge5Id, edges.First().Id);
            Assert.AreEqual(vertex5, edges.First().From);
            Assert.AreEqual(vertex4, edges.First().To);

            // connect the islands.
            var edgeId5 = graph.AddEdge(vertex5, 3, new uint[] { 50 }, null);

            // verify all edges.
            edges = graph.GetEdgeEnumerator(0);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(10, edges.First().Data[0]);
            Assert.AreEqual(edgeId1, edges.First().Id);
            Assert.AreEqual(0, edges.First().From);
            Assert.AreEqual(1, edges.First().To);

            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(4, edges.Count());
            Assert.IsTrue(edges.Any(x => x.To == 0));
            Assert.AreEqual(true, edges.First(x => x.To == 0).DataInverted);
            Assert.AreEqual(10, edges.First(x => x.To == 0).Data[0]);
            Assert.AreEqual(edgeId1, edges.First(x => x.To == 0).Id);
            Assert.AreEqual(1, edges.First(x => x.To == 0).From);
            Assert.IsTrue(edges.Any(x => x.To == 2));
            Assert.AreEqual(20, edges.First(x => x.To == 2).Data[0]);
            Assert.AreEqual(edgeId2, edges.First(x => x.To == 2).Id);
            Assert.AreEqual(1, edges.First(x => x.To == 2).From);
            Assert.IsTrue(edges.Any(x => x.To == 3 && x.Data[0] == 30));
            Assert.AreEqual(false, edges.First(x => x.To == 3 && x.Data[0] == 30).DataInverted);
            Assert.AreEqual(30, edges.First(x => x.To == 3 && x.Data[0] == 30).Data[0]);
            Assert.AreEqual(edgeId3, edges.First(x => x.To == 3 && x.Data[0] == 30).Id);
            Assert.AreEqual(1, edges.First(x => x.To == 3 && x.Data[0] == 30).From);
            Assert.IsTrue(edges.Any(x => x.To == 3 && x.Data[0] == 31));
            Assert.AreEqual(true, edges.First(x => x.To == 3 && x.Data[0] == 31).DataInverted);
            Assert.AreEqual(31, edges.First(x => x.To == 3 && x.Data[0] == 31).Data[0]);
            Assert.AreEqual(edgeId4, edges.First(x => x.To == 3 && x.Data[0] == 31).Id);
            Assert.AreEqual(1, edges.First(x => x.To == 3 && x.Data[0] == 31).From);

            edges = graph.GetEdgeEnumerator(2);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(edgeId2, edges.First().Id);
            Assert.AreEqual(true, edges.First().DataInverted);
            Assert.AreEqual(20, edges.First().Data[0]);
            Assert.AreEqual(2, edges.First().From);
            Assert.AreEqual(1, edges.First().To);

            edges = graph.GetEdgeEnumerator(3);
            Assert.AreEqual(3, edges.Count());
            Assert.AreEqual(30, edges.First(x => x.To == 1 && x.Data[0] == 30).Data[0]);
            Assert.AreEqual(edgeId3, edges.First(x => x.To == 1 && x.Data[0] == 30).Id);
            Assert.AreEqual(3, edges.First(x => x.To == 1 && x.Data[0] == 30).From);
            Assert.AreEqual(1, edges.First(x => x.To == 1 && x.Data[0] == 30).To);
            Assert.AreEqual(31, edges.First(x => x.To == 1 && x.Data[0] == 31).Data[0]);
            Assert.AreEqual(edgeId4, edges.First(x => x.To == 1 && x.Data[0] == 31).Id);
            Assert.AreEqual(3, edges.First(x => x.To == 1 && x.Data[0] == 31).From);
            Assert.AreEqual(1, edges.First(x => x.To == 1 && x.Data[0] == 31).To);
            Assert.IsTrue(edges.Any(x => x.To == vertex5));
            Assert.AreEqual(true, edges.First(x => x.To == vertex5).DataInverted);
            Assert.AreEqual(50, edges.First(x => x.To == vertex5).Data[0]);
            Assert.AreEqual(edgeId5, edges.First(x => x.To == vertex5).Id);
            Assert.AreEqual(3, edges.First(x => x.To == vertex5).From);

            edges = graph.GetEdgeEnumerator(vertex4);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(40, edges.First().Data[0]);
            Assert.AreEqual(edge5Id, edges.First().Id);
            Assert.AreEqual(vertex4, edges.First().From);
            Assert.AreEqual(vertex5, edges.First().To);

            edges = graph.GetEdgeEnumerator(vertex5);
            Assert.AreEqual(2, edges.Count());
            Assert.IsTrue(edges.Any(x => x.To == vertex4));
            Assert.AreEqual(true, edges.First(x => x.To == vertex4).DataInverted);
            Assert.AreEqual(40, edges.First(x => x.To == vertex4).Data[0]);
            Assert.AreEqual(edge5Id, edges.First(x => x.To == vertex4).Id);
            Assert.AreEqual(vertex5, edges.First(x => x.To == vertex4).From);
            Assert.IsTrue(edges.Any(x => x.To == 3));
            Assert.AreEqual(50, edges.First(x => x.To == 3).Data[0]);
            Assert.AreEqual(edgeId5, edges.First(x => x.To == 3).Id);
            Assert.AreEqual(vertex5, edges.First(x => x.To == 3).From);
        }
Exemple #21
0
        public void TestOneEdgeWithShape()
        {
            var vertex0 = new Coordinate(51.26779566943717f, 4.801347255706787f);
            var shape0  = new Coordinate(51.26763791763357f, 4.801728129386902f);
            var shape1  = new Coordinate(51.267070677950585f, 4.801749587059021f);
            var vertex1 = new Coordinate(51.26689735000000f, 4.801347255706787f);

            var graph = new GeometricGraph(1);

            graph.AddVertex(0, vertex0.Latitude, vertex0.Longitude);
            graph.AddVertex(1, vertex1.Latitude, vertex1.Longitude);
            graph.AddEdge(0, 1, new uint[] { 0 }, shape0, shape1);

            // resolve on vertex0.
            var location = vertex0;
            var resolver = new ResolveAlgorithm(graph, location.Latitude, location.Longitude, Constants.DefaultMaxEdgeDistance, 50f,
                                                (edge) => { return(true); });

            resolver.Run();

            var result = resolver.Result;

            Assert.IsNotNull(result);

            // resolve on vertex1.
            location = vertex1;
            resolver = new ResolveAlgorithm(graph, location.Latitude, location.Longitude, Constants.DefaultMaxEdgeDistance, 50f,
                                            (edge) => { return(true); });
            resolver.Run();

            result = resolver.Result;
            Assert.IsNotNull(result);

            // resolve on shape0.
            location = shape0;
            resolver = new ResolveAlgorithm(graph, location.Latitude, location.Longitude, Constants.DefaultMaxEdgeDistance, 50f,
                                            (edge) => { return(true); });
            resolver.Run();

            result = resolver.Result;
            Assert.IsNotNull(result);

            // resolve on shape1.
            location = shape1;
            resolver = new ResolveAlgorithm(graph, location.Latitude, location.Longitude, Constants.DefaultMaxEdgeDistance, 50f,
                                            (edge) => { return(true); });
            resolver.Run();

            result = resolver.Result;
            Assert.IsNotNull(result);

            // resolve on shape0 a bit to the top-right.
            location = new Coordinate(51.26771847181371f, 4.801915884017944f);
            resolver = new ResolveAlgorithm(graph, location.Latitude, location.Longitude, Constants.DefaultMaxEdgeDistance, 50f,
                                            (edge) => { return(true); });
            resolver.Run();

            result = resolver.Result;
            Assert.IsNotNull(result);

            // resolve on shape1 a bit to the bottom-right.
            location = new Coordinate(51.266986766160414f, 4.8019373416900635f);
            resolver = new ResolveAlgorithm(graph, location.Latitude, location.Longitude, Constants.DefaultMaxEdgeDistance, 50f,
                                            (edge) => { return(true); });
            resolver.Run();

            result = resolver.Result;
            Assert.IsNotNull(result);
        }
Exemple #22
0
 /// <summary>
 /// Adds a new vertex.
 /// </summary>
 public void AddVertex(uint vertex, float latitude, float longitude)
 {
     _graph.AddVertex(vertex, latitude, longitude);
 }