public void InitializeVertices() { /** create empty list of visited vertices */ m_VisitedVertices = new List <TreePathVertice>(); /** create list of all vertices which we will remove from as we visit */ m_UnvisitedVertices = new List <TreePathVertice>(_vertices.Values); /** * set all initial values for each vertice... * origin distance to zero, and all others to infinity * state and source */ foreach (KeyValuePair <String, TreePathVertice> kvp in _vertices) { TreePathVertice vertice = kvp.Value; if (vertice == _origin) { DebugLogText(LogLevel.DEBUG, "Set origin vertice to {0} ({1})", _origin, _origin.Position); vertice.Distance = 0; } else { DebugLogText(LogLevel.DEBUG, "Set other vertice at {0}, ({1})", vertice, vertice.Position); vertice.Distance = Double.PositiveInfinity; } vertice.State = TreePathVertice.VerticeState.Unvisited; vertice.Source = null; } }
public void TryAddNeighbor(TreePathVertice neighbor) { if (m_Neighbors.ContainsKey(neighbor.HashName) == false) { m_Neighbors.Add(neighbor.HashName, neighbor); } }
public void TryReplaceNeighbor(TreePathVertice oldNeighbor, TreePathVertice newNeighbor) { if (m_Neighbors.ContainsKey(oldNeighbor.HashName)) { m_Neighbors.Remove(oldNeighbor.HashName); } TryAddNeighbor(newNeighbor); }
PointD PossiblyCombinePoint(PointD point, Double distance) { PointD ret = point; foreach (KeyValuePair <String, TreePathVertice> kvp in _vertices) { TreePathVertice vertice = kvp.Value; if (point.Equals(vertice.Position) == false && FlatGeo.Distance(point, vertice.Position) < distance) { ret = vertice.Position; DebugLogText(LogLevel.DEBUG, "Combining {0} into {1}", point, ret); break; } } return(ret); }
public PointDList GetPath() { /** now work backwards along source paths */ PointDList path = new PointDList(); TreePathVertice vertice = _destination; TreePathVertice lastInserted; do { path.Insert(0, vertice.Position); lastInserted = vertice; vertice = vertice.Source; }while(lastInserted != _origin); return(path); }
public bool Cycle() { /** get the vertice with the lowest distance from the origin */ TreePathVertice current = null; foreach (TreePathVertice vertice in m_UnvisitedVertices) { if (current == null || vertice.Distance <= current.Distance) { current = vertice; } } DebugLogText(LogLevel.DEBUG, "Cycle set current vertice to {0}", current); if (current.Distance != Double.PositiveInfinity) { /** go through each neighbor of the current */ foreach (KeyValuePair <String, TreePathVertice> kvp in current.Neighbors) { TreePathVertice neighbor = kvp.Value; if (neighbor.State == TreePathVertice.VerticeState.Unvisited) { Double nDistance = FlatGeo.Distance(current.Position, neighbor.Position); if (current.Distance + nDistance < neighbor.Distance) { neighbor.Distance = current.Distance + nDistance; neighbor.Source = current; DebugLogText(LogLevel.DEBUG, "--- set neighbor {0} to distance of {1:0.00}", neighbor, GetNativeDistance(neighbor.Distance)); } } } /** move this node from visited to unvisited */ current.State = TreePathVertice.VerticeState.Visited; DebugLogText(LogLevel.DEBUG, "Set vertice {0} to Visited", current); m_UnvisitedVertices.Remove(current); m_VisitedVertices.Add(current); } else { m_UnvisitedVertices.Remove(current); DebugLogText(LogLevel.ERROR, "VERTICE {0} has no solution!", current); } return(m_UnvisitedVertices.Count > 0); }
protected virtual void Initialize() { if (DebugLogging && Log == null) { DebugLogText(LogLevel.DEBUG, "**************** New Spanning Tree {0} lines", m_LinesToConstructor.Count); } DumpLines("To constructor", m_LinesToConstructor); _vertices = new Dictionary <String, TreePathVertice>(); m_Lines = new LineList(); m_OriginalLines = new LineList(); foreach (Line line in m_LinesToConstructor) { m_OriginalLines.Add(line); m_Lines.Add(line); /** create objects for each end if they do not already exist */ TreePathVertice v1, v2; if (_vertices.TryGetValue(line.P1.HashName, out v1) == false) { v1 = new TreePathVertice(line.P1 as PointD, TreePathVertice.VerticeType.Standard); v1.Name = String.Format("{0} pt1", line.Name); _vertices.Add(v1.HashName, v1); } if (_vertices.TryGetValue(line.P2.HashName, out v2) == false) { v2 = new TreePathVertice(line.P2 as PointD, TreePathVertice.VerticeType.Standard); v2.Name = String.Format("{0} pt2", line.Name); _vertices.Add(v2.HashName, v2); } /** add them as neighbors to each other */ v1.TryAddNeighbor(v2); v2.TryAddNeighbor(v1); } DumpLines("m_Lines", m_Lines); DumpVertices(); }
public new GeoPointList GetPath() { /** now work backwards along source paths */ GeoPointList path = new GeoPointList(); TreePathVertice vertice = m_Destination; TreePathVertice lastInserted; do { if (vertice == null) { throw new Exception("Can't compute path"); } path.Insert(0, _coordinateMap.GetGeoPoint(vertice.Position)); lastInserted = vertice; vertice = vertice.Source; }while(lastInserted != m_Origin); return(path); }
protected TreePathVertice AddAdHocVertice(PointD point, TreePathVertice.VerticeType type) { TreePathVertice V1, V2; if (_vertices.TryGetValue(point.HashName, out V1) == false) { /** * Having ensured that our vertice does not already exist, * we are either adding an origin or destination. * * we need to add the vertice itself (V1), as well as an additional one (V2) on the closest * line segment (L) to the new vertice. * * The vertices at the end of (L) will have each other as neighbors. Since we are * essentially splitting (L) into two segments, */ /** clear any that exist of the given type */ ClearAdHocVertices(type); /** create our new vertice */ V1 = new TreePathVertice(point, type); _vertices.Add(V1.HashName, V1); /** find closest point on the closest line for a second vertice */ Double closestDistance; Line L; PointD closestPoint = m_Lines.ClosestPointTo(point, out L, out closestDistance); DebugLogText(LogLevel.DEBUG, "Added ad-hoc vertice of type {0} at point {1} closest to line [[{2}]] at {3} pixels", type, point, L, closestDistance); if (closestPoint != null) { /** if the closest vertice doesn't exist yet, we will create it, and split the line L */ if (_vertices.TryGetValue(closestPoint.HashName, out V2) == false) { /** get the vertices on the line we are about to split */ TreePathVertice LV1, LV2; if (_vertices.TryGetValue(L.P1.HashName, out LV1) == false || _vertices.TryGetValue(L.P2.HashName, out LV2) == false) { throw new Exception("Vertices on original lines should exist"); } /** create our new vertice on the split line */ V2 = new TreePathVertice(closestPoint, type); _vertices.Add(V2.HashName, V2); /** fix up neighbors */ LV1.TryReplaceNeighbor(LV2, V2); LV2.TryReplaceNeighbor(LV1, V2); V2.TryAddNeighbor(LV1); V2.TryAddNeighbor(LV2); V2.TryAddNeighbor(V1); V1.TryAddNeighbor(V2); /** replace L with two new lines L1 and L2 */ Line l1 = new Line(L.P1, closestPoint); Line l2 = new Line(closestPoint, L.P2); m_Lines.Remove(L); m_Lines.Add(l1); m_Lines.Add(l2); } else { V1.TryAddNeighbor(V2); V2.TryAddNeighbor(V1); } } } // DumpVertices(); return(V1); }