Example #1
0
        /// <summary>
        /// Adds an edge.
        /// </summary>
        /// <param name="forward"></param>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="tags"></param>
        protected virtual bool AddRoadEdge(TagsCollectionBase tags, bool forward, uint from, uint to)
        {
            float         latitude;
            float         longitude;
            GeoCoordinate fromCoordinate = null;

            if (_dynamicGraph.GetVertex(from, out latitude, out longitude))
            { //
                fromCoordinate = new GeoCoordinate(latitude, longitude);
            }
            GeoCoordinate toCoordinate = null;

            if (_dynamicGraph.GetVertex(to, out latitude, out longitude))
            { //
                toCoordinate = new GeoCoordinate(latitude, longitude);
            }

            if (fromCoordinate != null && toCoordinate != null)
            { // calculate the edge data.
                TEdgeData edgeData = this.CalculateEdgeData(_interpreter.EdgeInterpreter, _tagsIndex, tags, forward, fromCoordinate, toCoordinate);

                _dynamicGraph.AddArc(from, to, edgeData, _edgeComparer);
            }
            return(false);
        }
Example #2
0
        /// <summary>
        /// Contracts the given vertex.
        /// </summary>
        /// <param name="vertex"></param>
        public void Contract(uint vertex)
        {
            if (_contracted.Length > vertex && _contracted[vertex])
            {
                throw new Exception("Is already contracted!");
            }

            // keep the neighbours.
            HashSet <KeyValuePair <uint, CHEdgeData> > neighbours =
                new HashSet <KeyValuePair <uint, CHEdgeData> >();

            // get all information from the source.
            KeyValuePair <uint, CHEdgeData>[] edges = _target.GetArcs(vertex);

            // remove all informative edges.
            edges = edges.RemoveInformativeEdges();

            // report the before contraction event.
            this.OnBeforeContraction(vertex, edges);

            // remove the edges from the neighbours to the target.
            foreach (KeyValuePair <uint, CHEdgeData> edge in edges)
            { // remove the edge.
                _target.DeleteArc(edge.Key, vertex);

                // keep the neighbour.
                if (_keepDirectNeighbours && !edge.Value.HasContractedVertex)
                { // edge does represent a neighbour relation.
                    neighbours.Add(
                        new KeyValuePair <uint, CHEdgeData>(edge.Key, edge.Value.ConvertToInformative()));
                }
            }

            // loop over each combination of edges just once.
            for (int x = 1; x < edges.Length; x++)
            { // loop over all elements first.
                KeyValuePair <uint, CHEdgeData> xEdge = edges[x];
                if (xEdge.Value.IsInformative)
                {
                    continue;
                }

                for (int y = 0; y < x; y++)
                { // loop over all elements.
                    KeyValuePair <uint, CHEdgeData> yEdge = edges[y];
                    if (yEdge.Value.IsInformative)
                    {
                        continue;
                    }

                    // calculate the total weight.
                    float weight = xEdge.Value.Weight + yEdge.Value.Weight;

                    // add the combinations of these edges.
                    if (((xEdge.Value.Backward && yEdge.Value.Forward) ||
                         (yEdge.Value.Backward && xEdge.Value.Forward)) &&
                        (xEdge.Key != yEdge.Key))
                    { // there is a connection from x to y and there is no witness path.
                        bool witnessXToY = _witnessCalculator.Exists(_target, xEdge.Key,
                                                                     yEdge.Key, vertex, weight, 100);
                        bool witnessYToX = _witnessCalculator.Exists(_target, yEdge.Key,
                                                                     xEdge.Key, vertex, weight, 100);

                        // create x-to-y data and edge.
                        CHEdgeData dataXToY = new CHEdgeData();
                        bool       forward  = (xEdge.Value.Backward && yEdge.Value.Forward) &&
                                              !witnessXToY;
                        bool backward = (yEdge.Value.Backward && xEdge.Value.Forward) &&
                                        !witnessYToX;
                        dataXToY.SetDirection(forward, backward, true);
                        dataXToY.Weight             = weight;
                        dataXToY.ContractedVertexId = vertex;
                        if ((dataXToY.Forward || dataXToY.Backward) ||
                            !_target.HasArc(xEdge.Key, yEdge.Key))
                        { // add the edge if there is usefull info or if there needs to be a neighbour relationship.
                            _target.AddArc(xEdge.Key, yEdge.Key, dataXToY, _comparer);
                        }

                        // create y-to-x data and edge.
                        CHEdgeData dataYToX = new CHEdgeData();
                        forward = (yEdge.Value.Backward && xEdge.Value.Forward) &&
                                  !witnessYToX;
                        backward = (xEdge.Value.Backward && yEdge.Value.Forward) &&
                                   !witnessXToY;
                        dataYToX.SetDirection(forward, backward, true);
                        dataYToX.Weight             = weight;
                        dataYToX.ContractedVertexId = vertex;
                        if ((dataYToX.Forward || dataYToX.Backward) ||
                            !_target.HasArc(yEdge.Key, xEdge.Key))
                        { // add the edge if there is usefull info or if there needs to be a neighbour relationship.
                            _target.AddArc(yEdge.Key, xEdge.Key, dataYToX, _comparer);
                        }
                    }
                }
            }

            // mark the vertex as contracted.
            this.MarkContracted(vertex);

            // notify a contracted neighbour.
            _calculator.NotifyContracted(vertex);

            // add contracted neighbour edges again.
            if (_keepDirectNeighbours)
            {
                foreach (KeyValuePair <uint, CHEdgeData> neighbour in neighbours)
                {
                    _target.AddArc(neighbour.Key, vertex, neighbour.Value, null);
                }
            }

            // report the after contraction event.
            this.OnAfterContraction(vertex, edges);
        }