private void button6_Click(object sender, RoutedEventArgs e) { if (set == null) { return; } IPosition_Connected a = null, b = null; foreach (IPosition_Connected p in set) { if (a == null) { a = p; } b = p; } SearchPathEngine search = new AStar(new ManhattanDistanceEvaluator()); search.InitEngineForMap(set); IPosition_ConnectedSet path = search.SearchPath(a, b); if (path != null) { foreach (IPosition_Connected p in path) { p.SetTagIndex(MapModel.PATH_ID); } } group.Children.Clear(); mapModel.generateMap_from_IPosition_ConnectedSet(group, set); }
//每次寻径之前根据起点、终点信息做初始化 bool Init(IPosition_Connected end, IPosition_Connected start) { //判断起点和终点是否在地图上,并初始化起点的标签 Tag tag = (Tag)end.GetAttachment(); //生成这次寻径使用的time stamp time_stamp = TimeStamp.getNextTimeStamp(time_stamp); if (tag != null) { tag.g = 0; tag.parent = null; tag.closed = false; tag.timeStamp = time_stamp; } else { return(false); } if (start.GetAttachment() == null) { return(false); } return(true); }
public void TraversePositionSet_ConnectedAndSpringEvent(IPositionSet ps) { if (OnGetConnection != null) { IPosition_Connected[] ary = (IPosition_Connected[])ps.ToArray(); if (ary != null) { foreach (IPosition_Connected p in ary) { float x = layer.ConvertPositionSetXToScreenX(p.GetX()); float y = layer.ConvertPositionSetYToScreenY(p.GetY()); IPositionSet_Connected_Adjacency adj_set = p.GetAdjacencyPositionSet(); adj_set.InitToTraverseSet(); while (adj_set.NextPosition()) { IPosition_Connected adj = adj_set.GetPosition_Connected(); float adj_x = layer.ConvertPositionSetXToScreenX(adj.GetX()); float adj_y = layer.ConvertPositionSetYToScreenY(adj.GetY()); OnGetConnection(x, y, adj_x, adj_y); } } } } TraversePositionSetAndSpringEvent(ps); }
protected string SearchForPath(ISearchPathEngine searchEngine, bool countTimeEnable) { start = map.GetStartPosition(); end = map.GetEndPosition(); searchPathEngine = searchEngine; if (start != null && end != null) { searchEngine.InitEngineForMap(map.GetPositionSet_Connected()); path = searchEngine.SearchPath(start, end); if (path != null) { double length = 0, dx, dy; for (int i = 1; i < path.Count; i++) { dx = path[i].GetX() - path[i - 1].GetX(); dy = path[i].GetY() - path[i - 1].GetY(); length += Math.Sqrt(dx * dx + dy * dy); } if (countTimeEnable) { return(string.Format("Time:{0} milliseconds.\nPath length:{1}", timeCounter.CountTimeForRepeatableMethod(SearchForPath_countTime), length)); } else { return(string.Format("Path length:{1}", length)); } } } return("There is not any path between the start point and the end point!"); }
protected void DrawPathNode(IPosition_Connected p) { using (Graphics g = panel1.CreateGraphics()) { g.DrawEllipse(pathNodePen, (int)p.GetX() * blockSize, (int)p.GetY() * blockSize, blockSize, blockSize); } }
public Tag(IPosition_Connected parent, float g) { this.parent = parent; this.f = float.MaxValue; this.g = g; this.closed = false; this.timeStamp = 0; }
public Tag(IPosition_Connected parent, float f, float g, bool closed) { this.parent = parent; this.f = f; this.g = g; this.closed = closed; this.timeStamp = 0; }
public void Clear() { this.parent = null; this.f = float.MaxValue; this.g = float.MaxValue; this.closed = false; this.timeStamp = 0; }
public Array ToArray() { IPosition_Connected[] a = new IPosition_Connected[pairList.Count]; for (int i = 0; i < pairList.Count; i++) { a[i] = pairList[i].position; } return(a); }
private void toolStripButton8_Click(object sender, EventArgs e) { IPosition_Connected_Edit p, adj; StringBuilder sb = new StringBuilder(); sb.AppendLine("Position:\tAdjacency:"); for (int i = 0; i < mapWidth; i++) { for (int j = 0; j < mapHeight; j++) { if (map.Exists(i, j)) { p = map.GetPosition(i, j); sb.Append(p.ToString()); sb.Append("\t"); IPositionSet_Connected_AdjacencyEdit adjSet = p.GetAdjacencyPositionSetEdit(); adjSet.InitToTraverseSet(); while (adjSet.NextPosition()) { adj = adjSet.GetPosition_Connected_Edit(); sb.Append(adj.ToString() + ":" + adjSet.GetDistanceToAdjacency().ToString() + "\t"); } sb.AppendLine(); } } } IPosition_Connected start = map.GetStartPosition(); IPosition_Connected end = map.GetEndPosition(); if (start != null) { sb.Append("Start position:" + start.ToString()); } if (end != null) { sb.Append("End position:" + end.ToString()); } sb.AppendLine(SearchForPath(astar, false)); if (path != null) { sb.AppendLine(); sb.AppendLine("Path length:" + path.Count); foreach (IPosition_Connected iter in path) { sb.Append(iter.ToString() + iter.GetAttachment().ToString() + "\t"); } } infoForm.SetText(sb.ToString()); infoForm.Show(); infoForm.Activate(); if (colorThePathToolStripMenuItem.Checked) { drawMap(); } }
public void RemoveAdjacency(IPosition_Connected adjacency) { for (int i = 0; i < pairList.Count; i++) { if (pairList[i].position == adjacency) { pairList.RemoveAt(i); break; } } }
IPart_Connected GetParentPart(IPosition_Connected position_Connected, bool isPart) { if (isPart) { return(((IPart_Connected)position_Connected).GetParentPart()); } else { return(((Tag_M2M_Position)position_Connected.GetAttachment()).parentPart); } }
void SetParentPart(IPosition_Connected position_Connected, IPart_Connected parentPart, bool isPart) { if (isPart) { ((IPart_Connected)position_Connected).SetParentPart(parentPart); } else { ((Tag_M2M_Position)position_Connected.GetAttachment()).parentPart = parentPart; total++; } }
public override int Compare(IPosition_Connected p1, IPosition_Connected p2) { Tag t1 = list[p1.GetTagIndex()]; Tag t2 = list[p2.GetTagIndex()]; int r = diff(t1.f, t2.f); if (r != 0) { return(r); } return(diff(t1.g, t2.g)); }
public void RemovePosition_Connected(IPosition_Connected p) { IPositionSet_Connected_AdjacencyEdit adjSet = ((IPosition_Connected_Edit)p).GetAdjacencyPositionSetEdit(); adjSet.InitToTraverseSet(); while (adjSet.NextPosition()) { IPosition_Connected_Edit adj = adjSet.GetPosition_Connected_Edit(); adj.GetAdjacencyPositionSetEdit().RemoveAdjacency(p); adjSet.RemoveAdjacency(adj); } list.Remove(p); }
//每次寻径之前根据起点、终点信息做初始化 protected void Init(IPosition_Connected start, IPosition_Connected end) { //判断起点和终点是否在地图上,并初始化起点的标签 //Tag tag = (Tag)start.GetAttachment(); //生成这次寻径使用的time stamp time_stamp = TimeStamp.getNextTimeStamp(time_stamp); Tag tag = list[start.GetTagIndex()]; tag.Clear(); tag.timeStamp = time_stamp; }
//从终点根据标签中记录的父节点找到起点,生成路径 protected void GetPath(List <IPosition_Connected> path, IPosition_Connected p, IPosition_Connected end) { if (p == end) { path.Add(end); Tag p_tag = list[end.GetTagIndex()]; while (p_tag.parent != null) { path.Add(p_tag.parent); p_tag = list[p_tag.parent.GetTagIndex()]; } path.Reverse(); } }
bool Init(IPosition_Connected start, IPosition_Connected end) { IPosition_Connected p; Tag tag; //初始化 positionSet.InitToTraverseSet(); while (positionSet.NextPosition()) { p = positionSet.GetPosition_Connected(); if (dict.TryGetValue(p, out tag)) { tag.Clear(); } else { dict.Add(p, new Tag()); open.add(p); } } //判断起点和终点是否在地图上,并初始化起点的标签 if (dict.TryGetValue(start, out tag)) { tag.g = 0; tag.parent = null; } else { return(false); } if (!dict.TryGetValue(end, out tag)) { return(false); } //更新起点在候选队列中的位置 open.remove(start); open.add(start); return(true); }
public void TestMethod1() { // // TODO: Add test logic here // RandomMaze_IPosition_Connected_Config config = new RandomMaze_IPosition_Connected_Config(); config.Width = 3; config.Height = 3; config.Depth = 3; IPosition_ConnectedSet set = config.Produce(); IPosition_Connected a = null, b = null; foreach (IPosition_Connected p in set) { if (a == null) { a = p; } b = p; } AStar search = new AStar(); search.InitEngineForMap(set); IPosition_ConnectedSet path = search.SearchPath(a, b); if (path != null) { foreach (IPosition_Connected p in path) { Console.WriteLine("{0},{1}", p.GetValue(0), p.GetValue(1)); } } }
//interface IPositionSet_Connected_AdjacencyEdit public void AddAdjacency(IPosition_Connected adjacency, float distance) { pairList.Add(new Position_Distance(adjacency, distance)); }
//没有路径则返回null public override ICollection <IPosition_Connected> SearchPath(IPosition_Connected start, IPosition_Connected end) { List <IPosition_Connected> path = new List <IPosition_Connected>(); if (start == end) { path.Add(start); return(path); } //初始化 Init(start, end); IPosition_Connected p = null, p_adj; Tag p_tag, p_adj_tag; Real newG; open.clear(); //将起点加入open表 open.add(start); //当open表非空时 while (open.getSize() > 0) { //获得下一个最近的点 p = open.removeFirst(); //到达终点则结束 if (p == end) { break; } p_tag = list[p.GetTagIndex()]; p_tag.closed = true; IEnumerable <IAdjacency> adj_set = p.GetAdjacencyOut(); foreach (IAdjacency adjacency in adj_set) { p_adj = adjacency.GetPosition_Connected(); p_adj_tag = list[p_adj.GetTagIndex()]; //如果未被搜索 if (p_adj_tag.timeStamp != time_stamp) { newG = p_tag.g + adjacency.GetDistance(); p_adj_tag.parent = p; p_adj_tag.g = newG; p_adj_tag.closed = false; p_adj_tag.timeStamp = time_stamp; open.add(p_adj); } else { if (!p_adj_tag.closed) { newG = p_tag.g + adjacency.GetDistance(); if (newG < p_adj_tag.g) { p_adj_tag.parent = p; p_adj_tag.g = newG; open.update(p_adj); } } } } } GetPath(path, p, end); return(path); }
public abstract ICollection <IPosition_Connected> SearchPath(IPosition_Connected start, IPosition_Connected end);
public Adjacency(IPosition_Connected pisition, Real distance) { p = pisition; d = distance; }
//没有路径则返回null public List <IPosition_Connected> SearchPath(IPosition_Connected start, IPosition_Connected end) { path.Clear(); if (end == start) { path.Add(end); return(path); } //初始化 if (!Init(end, start)) { return(null); } IPosition_Connected p = null, p_adj; IPositionSet_Connected_Adjacency adj_set; Tag p_tag, p_adj_tag; float newG; //int count = 0; //if (debug) // Console.WriteLine("SearchPath:"); open.clear(); //将起点加入open表 open.add(end); //当open表非空时 while (open.getSize() > 0) { //获得下一个最近的点 p = open.removeFirst(); //到达终点则结束 if (p == start) { pathLength = ((Tag)start.GetAttachment()).g; break; } p_tag = (Tag)p.GetAttachment(); p_tag.closed = true; adj_set = p.GetAdjacencyPositionSet(); adj_set.InitToTraverseSet(); //if (debug) //{ // count++; // Console.Write(p.ToString() + "\t"); //} while (adj_set.NextPosition()) { p_adj = adj_set.GetPosition_Connected(); p_adj_tag = (Tag)p_adj.GetAttachment(); //如果未被搜索 if (p_adj_tag.timeStamp != time_stamp) { newG = p_tag.g + adj_set.GetDistanceToAdjacency(); p_adj_tag.parent = p; p_adj_tag.g = newG; p_adj_tag.closed = false; p_adj_tag.timeStamp = time_stamp; open.add(p_adj); } else { if (!p_adj_tag.closed) { newG = p_tag.g + adj_set.GetDistanceToAdjacency(); if (newG < p_adj_tag.g) { p_adj_tag.parent = p; p_adj_tag.g = newG; open.update(p_adj); } } } //if (debug) // Console.Write(p_adj.ToString() + ":" + p_adj.GetAttachment().ToString() + "\t"); } //if (debug) // Console.WriteLine(); } //if (debug) //{ // Console.Write("position count:"); // Console.WriteLine(count); //} //从终点根据标签中记录的父节点找到起点,生成路径 if (p == start) { path.Add(start); p_tag = (Tag)start.GetAttachment(); while (p_tag.parent != null) { path.Add(p_tag.parent); p_tag = (Tag)p_tag.parent.GetAttachment(); } //path.Reverse(); return(path); } else { return(null); } }
private void pathFindingToolStripMenuItem_Click(object sender, EventArgs e) { IPositionSet_Connected positionSet_Connected = (IPositionSet_Connected)positionSetContainer.GetPositionSet(); Dijkstra dijkstra = new Dijkstra(); AStar astar = new AStar(); M2M_PF m2m_PF = new M2M_PF(); IM2MStructure m2mStructure = null; List <IPosition_Connected> m2mPath = null; List <IPosition_Connected> path = null; //计算算法运行时间用 //IPosition_Connected start = null; //IPosition_Connected end = null; ISearchPathEngine searchPathEngine1 = m2m_PF; ISearchPathEngine searchPathEngine2 = dijkstra; m2m_PF.GetM2MStructure += delegate(IM2MStructure m2mS) { m2mStructure = m2mS; }; m2m_PF.GetM2MStructureInPreprocess += delegate(IM2MStructure m2mS) { m2mStructure = m2mS; }; List <ushort> timeStampList = new List <ushort>(); m2m_PF.GetTimeStamp += delegate(ushort timeStamp) { timeStampList.Add(timeStamp); }; searchPathEngine1.InitEngineForMap(positionSet_Connected); searchPathEngine2.InitEngineForMap(positionSet_Connected); //以下代码必须在UI线程中调用,即不能在另开的线程中调用 LayersExOptDlg layers = new LayersExOptDlg(); LayersPainterForm layersPainterForm = new LayersPainterForm(layers); LayersPaintedControl layersPaintedControl = layersPainterForm.LayersPaintedControl; LayersEditedControl layersEditedControl = new LayersEditedControl(); layersEditedControl.Dock = DockStyle.Top; layersEditedControl.LayersPaintedControl = layersPaintedControl; layersPainterForm.Controls.Add(layersEditedControl); layersPainterForm.Show(); //打开一个Worker线程来进行算法流程的演示(否则会阻塞UI线程以至于演示不能进行) IAsyncResult result = new dDemoProcess(delegate { Layer_PositionSet_Connected layer_PositionSet_Connected = new Layer_PositionSet_Connected(positionSet_Connected); layer_PositionSet_Connected.Point.PointColor = Color.Yellow; layer_PositionSet_Connected.Connection.LineColor = Color.Blue; layer_PositionSet_Connected.Connection.LineWidth = 0.6f; layer_PositionSet_Connected.Point.PointRadius = 1.2f; layers.Add(layer_PositionSet_Connected); layer_PositionSet_Connected.SpringLayerRepresentationChangedEvent(layer_PositionSet_Connected); for (int levelSequence = 1; levelSequence < m2mStructure.GetLevelNum(); levelSequence++) { Layer_PartSet_Connected layer_PartSet_Connected = new Layer_PartSet_Connected(m2mStructure, levelSequence); layer_PartSet_Connected.Visible = false; layers.Add(layer_PartSet_Connected); } while (true) { IPosition_Connected start = (IPosition_Connected)layersEditedControl.GetMouseDoubleChickedNearestPositionInCurrentPositionSet(positionSet_Connected); PositionSet_ConnectedEdit startPointSet = new PositionSet_ConnectedEdit(); startPointSet.AddPosition_Connected(start); Layer_PositionSet_Point layerStartPoint = new Layer_PositionSet_Point(startPointSet); layerStartPoint.Active = true; layerStartPoint.Point.IsDrawPointBorder = true; layerStartPoint.Point.PointRadius = 5; layerStartPoint.Point.PointColor = Color.PaleGreen; layers.Add(layerStartPoint); layerStartPoint.SpringLayerRepresentationChangedEvent(layerStartPoint); IPosition_Connected end = (IPosition_Connected)layersEditedControl.GetMouseDoubleChickedNearestPositionInCurrentPositionSet(positionSet_Connected); PositionSet_ConnectedEdit endPointSet = new PositionSet_ConnectedEdit(); endPointSet.AddPosition_Connected(end); Layer_PositionSet_Point layerEndPoint = new Layer_PositionSet_Point(endPointSet); layerEndPoint.Active = true; layerEndPoint.Point.IsDrawPointBorder = true; layerEndPoint.Point.PointRadius = 5; layerEndPoint.Point.PointColor = Color.Cyan; layers.Add(layerEndPoint); layerEndPoint.SpringLayerRepresentationChangedEvent(layerEndPoint); CountTimeTool.TimeCounter timeCounter = new CountTimeTool.TimeCounter(); searchPathEngine2.InitEngineForMap(positionSet_Connected); double time2 = timeCounter.CountTimeForRepeatableMethod(delegate { searchPathEngine2.SearchPath(start, end); }); path = searchPathEngine2.SearchPath(start, end); float pathLength2 = ((Dijkstra)searchPathEngine2).GetPathLength(); Console.WriteLine("Dijkstra consume time: " + time2 + " path Length = " + pathLength2); searchPathEngine1.InitEngineForMap(positionSet_Connected); //清空timeStampList以记录寻径过程各层的timeStamp double time1 = timeCounter.CountTimeForRepeatableMethod(delegate { searchPathEngine1.SearchPath(start, end); }); timeStampList.Clear(); m2mPath = searchPathEngine1.SearchPath(start, end); float pathLength1 = ((M2M_PF)searchPathEngine1).GetPathLength(); Console.WriteLine("M2M_PF consume time: " + time1 + " path Length = " + pathLength1); //searchPathEngine1.SearchPath(start, end); if (searchPathEngine1 is M2M_PF && m2mStructure != null) { for (int levelSequence = 1; levelSequence < m2mStructure.GetLevelNum(); levelSequence++) { ILevel level = m2mStructure.GetLevel(levelSequence); IPart rootPart = m2mStructure.GetLevel(0).GetPartRefByPartIndex(0, 0); IPositionSet positionSet = m2mStructure.GetDescendentPositionSetByAncestorPart(levelSequence, rootPart, 0); List <IPosition_Connected> position_ConnectedList = new List <IPosition_Connected>(); positionSet.InitToTraverseSet(); while (positionSet.NextPosition()) { if (positionSet.GetPosition() is IPart_Multi) { IPart_Multi partMulti = (IPart_Multi)positionSet.GetPosition(); IEnumerable <IPart_Connected> part_ConnectedEnumerable = partMulti.GetSubPartSet(); foreach (IPart_Connected part in part_ConnectedEnumerable) { IPosition_Connected tempPartConnected = (IPosition_Connected)part; if (((Tag_M2M_Part)tempPartConnected.GetAttachment()).isNeedToSearch == true) { if (timeStampList[levelSequence - 1] == ((Tag_M2M_Part)tempPartConnected.GetAttachment()).timeStamp) { position_ConnectedList.Add(tempPartConnected); } } } } else { IPosition_Connected tempPartConnected = (IPosition_Connected)positionSet.GetPosition(); if (((Tag_M2M_Part)tempPartConnected.GetAttachment()).isNeedToSearch == true) { if (timeStampList[levelSequence - 1] == ((Tag_M2M_Part)tempPartConnected.GetAttachment()).timeStamp) { position_ConnectedList.Add(tempPartConnected); } } } } IPositionSet partSet = new PositionSet_Connected(position_ConnectedList); Layer_M2MPartSetInSpecificLevel layer_M2MPartSetInSpecificLevel = new Layer_M2MPartSetInSpecificLevel(m2mStructure.GetLevel(levelSequence), partSet); layer_M2MPartSetInSpecificLevel.Active = true; layer_M2MPartSetInSpecificLevel.Alpha = 100; //layer_M2MPartSetInSpecificLevel. layers.Add(layer_M2MPartSetInSpecificLevel); //PositionSet_Connected partSet_Connected = new PositionSet_Connected(position_ConnectedList); //Layer_PositionSet_Connected layer_PartSet_Connected = new Layer_PositionSet_Connected(partSet_Connected); ////layer_PartSet_Connected.MainColor ////layer_PartSet_Connected.Active = true; //layer_PartSet_Connected.SetPositionSetTransformByM2MLevel(level); //layers.Add(layer_PartSet_Connected); } } if (path != null) { Layer_PositionSet_Path layer = new Layer_PositionSet_Path(new PositionSet_Connected(path)); layer.Active = true; layer.PathLine.LineColor = Color.Black; layer.PathLine.LineWidth = 2; layer.PathPoint.PointRadius = 2; layer.PathPoint.IsDrawPointBorder = true; //layer.EditAble = true; //layer.Point.PointColor = Color.Yellow; //layer.Point.PointRadius = 2; //layer.Point.IsDrawPointBorder = true; layers.Add(layer); layer.SpringLayerRepresentationChangedEvent(layer); } else { this.BeginInvoke(new dShow(MessageBox.Show), new object[] { "There is no path between two node" }); } if (m2mPath != null) { Layer_PositionSet_Path layer = new Layer_PositionSet_Path(new PositionSet_Connected(m2mPath)); layer.Active = true; layer.PathLine.LineColor = Color.Red; layer.PathLine.LineWidth = 2; layer.PathPoint.PointRadius = 2; layer.PathPoint.IsDrawPointBorder = true; //layer.EditAble = true; //layer.Point.PointColor = Color.Yellow; //layer.Point.PointRadius = 2; //layer.Point.IsDrawPointBorder = true; layers.Add(layer); layer.SpringLayerRepresentationChangedEvent(layer); } else { this.BeginInvoke(new dShow(MessageBox.Show), new object[] { "There is no path between two node by M2M_PF" }); } } }).BeginInvoke(null, null); }
public void AddPosition_Connected(IPosition_Connected p) { list.Add(p); }
//没有路径则返回null public List <IPosition_Connected> SearchPath(IPosition_Connected start, IPosition_Connected end) { IPart_Connected startParentPart = ((Tag_M2M_Position)(start.GetAttachment())).parentPart; IPart_Connected endParentPart = ((Tag_M2M_Position)(end.GetAttachment())).parentPart; ushort lastLevelTimeStamp = TimeStamp.getRandomTimeStamp(); ushort endTime_stamp = TimeStamp.getRandomTimeStamp(); ushort startTime_stamp = TimeStamp.getRandomTimeStamp(); //从最上层的下一层开始进行搜索空间收缩 for (int levelSequence = 1; levelSequence < m2mStructure.GetLevelNum(); levelSequence++) { IPart_Connected currentLevelAncestorPartOfStartPosition = startParentPart; IPart_Connected currentLevelAncestorPartOfEndPosition = endParentPart; for (int i = 0; i < m2mStructure.GetLevelNum() - levelSequence - 1; i++) { currentLevelAncestorPartOfStartPosition = (IPart_Connected)currentLevelAncestorPartOfStartPosition.GetParentPart(); currentLevelAncestorPartOfEndPosition = (IPart_Connected)currentLevelAncestorPartOfEndPosition.GetParentPart(); } Tag_M2M_Part StartPartTag = (Tag_M2M_Part)currentLevelAncestorPartOfStartPosition.GetAttachment(); StartPartTag.isNeedToSearch = true; Tag_M2M_Part EndPartTag = (Tag_M2M_Part)currentLevelAncestorPartOfEndPosition.GetAttachment(); EndPartTag.isNeedToSearch = true; bool isTopLevel = false; if (levelSequence == 1) { isTopLevel = true; } //在该层中标记需要搜索的分块 { //归入搜索空间的路径的最长长度 float PathLengthBound = float.MaxValue; endTime_stamp = TimeStamp.getNextTimeStamp(startTime_stamp); EndPartTag.timeStamp = endTime_stamp; EndPartTag.ge = 0; open.clear(); ((M2M_Part_TagComparer)com).state = 0; //将起点加入open表 open.add(currentLevelAncestorPartOfEndPosition); //当open表非空时 while (open.getSize() > 0) { //获得下一个最近的点 IPart_Connected currentPosition_Connected = (IPart_Connected)open.removeFirst(); Tag_M2M_Part TagOfCurrentPart_Connected = (Tag_M2M_Part)currentPosition_Connected.GetAttachment(); //当open表里面所有的点的g值都超出路径最大长度范围,则结束循环 if (TagOfCurrentPart_Connected.ge >= PathLengthBound) { break; } //到达终点则记录路径的长度,并计算出搜索范围 if (currentPosition_Connected == currentLevelAncestorPartOfStartPosition) { if (PathLengthBound == float.MaxValue) { PathLengthBound = CalculatePathLengthBound(m2mStructure.GetLevelNum(), levelSequence, TagOfCurrentPart_Connected.ge); } } TagOfCurrentPart_Connected.isClose = true; IPositionSet_Connected_Adjacency CurrentPartAdjSet = currentPosition_Connected.GetAdjacencyPositionSet(); CurrentPartAdjSet.InitToTraverseSet(); while (CurrentPartAdjSet.NextPosition()) { IPart_Connected adjPosition = (IPart_Connected)CurrentPartAdjSet.GetPosition_Connected(); Tag_M2M_Part tagOfAdjPosition = (Tag_M2M_Part)adjPosition.GetAttachment(); bool isInSearchingBound = false; if (isTopLevel) { isInSearchingBound = true; } else { Tag_M2M_Part parenPartTag = (Tag_M2M_Part)adjPosition.GetParentPart().GetAttachment(); if ((parenPartTag.timeStamp == lastLevelTimeStamp) && (parenPartTag.isNeedToSearch == true)) { isInSearchingBound = true; } } //如果属于搜索空间 if (isInSearchingBound) { //如果未被搜索 if (tagOfAdjPosition.timeStamp != endTime_stamp) { float newG = TagOfCurrentPart_Connected.ge + CurrentPartAdjSet.GetDistanceToAdjacency(); if (newG < PathLengthBound) { tagOfAdjPosition.ge = newG; tagOfAdjPosition.isClose = false; tagOfAdjPosition.timeStamp = endTime_stamp; open.add(adjPosition); } } else { if (!tagOfAdjPosition.isClose) { float newG = TagOfCurrentPart_Connected.ge + CurrentPartAdjSet.GetDistanceToAdjacency(); if (newG < tagOfAdjPosition.ge) { tagOfAdjPosition.ge = newG; open.update(adjPosition); } } } } } } startTime_stamp = TimeStamp.getNextTimeStamp(endTime_stamp); StartPartTag.timeStamp = startTime_stamp; StartPartTag.gs = 0; open.clear(); ((M2M_Part_TagComparer)com).state = 1; //将起点加入open表 open.add(currentLevelAncestorPartOfStartPosition); //当open表非空时 while (open.getSize() > 0) { //获得下一个最近的点 IPart_Connected currentPosition_Connected = (IPart_Connected)open.removeFirst(); Tag_M2M_Part TagOfCurrentPart_Connected = (Tag_M2M_Part)currentPosition_Connected.GetAttachment(); //当open表里面所有的点的g值都超出路径最大长度范围,则结束循环 if (TagOfCurrentPart_Connected.gs >= PathLengthBound) { break; } TagOfCurrentPart_Connected.isClose = true; IPositionSet_Connected_Adjacency CurrentPartAdjSet = currentPosition_Connected.GetAdjacencyPositionSet(); CurrentPartAdjSet.InitToTraverseSet(); while (CurrentPartAdjSet.NextPosition()) { IPart_Connected adjPosition = (IPart_Connected)CurrentPartAdjSet.GetPosition_Connected(); Tag_M2M_Part tagOfAdjPosition = (Tag_M2M_Part)adjPosition.GetAttachment(); bool isInSearchingBound = false; if (isTopLevel) { isInSearchingBound = true; } else { Tag_M2M_Part parenPartTag = (Tag_M2M_Part)adjPosition.GetParentPart().GetAttachment(); if ((parenPartTag.timeStamp == lastLevelTimeStamp) && (parenPartTag.isNeedToSearch == true)) { isInSearchingBound = true; } } //如果属于搜索空间 if (isInSearchingBound) { //如果未被搜索 if (tagOfAdjPosition.timeStamp != startTime_stamp) { float newG = TagOfCurrentPart_Connected.gs + CurrentPartAdjSet.GetDistanceToAdjacency(); if (newG < PathLengthBound) { tagOfAdjPosition.gs = newG; tagOfAdjPosition.isClose = false; //如果在上面的搜索过程中被搜索过 if (tagOfAdjPosition.timeStamp == endTime_stamp) { //如果两次搜索的g值之和小于路径的最大长度则标记该分块为下一层的待搜索空间 if (tagOfAdjPosition.ge + newG < PathLengthBound) { tagOfAdjPosition.isNeedToSearch = true; } else { tagOfAdjPosition.isNeedToSearch = false; } } else { //如果在上面的搜索过程中没有被搜索过,则把它的ge值设大,使得以后改变g值的时候不会因为两个g值之和小于PathLengthBound而把该点标记为待搜索点 tagOfAdjPosition.ge = PathLengthBound + 1; tagOfAdjPosition.isNeedToSearch = false; } tagOfAdjPosition.timeStamp = startTime_stamp; open.add(adjPosition); } } else { if (!tagOfAdjPosition.isClose) { float newG = TagOfCurrentPart_Connected.gs + CurrentPartAdjSet.GetDistanceToAdjacency(); if (newG < tagOfAdjPosition.gs) { if (tagOfAdjPosition.isNeedToSearch == false) { //如果两次搜索的g值之和小于路径的最大长度则标记该分块为下一层的待搜索空间 if (tagOfAdjPosition.ge + newG < PathLengthBound) { tagOfAdjPosition.isNeedToSearch = true; } } tagOfAdjPosition.gs = newG; open.update(adjPosition); } } } } } } lastLevelTimeStamp = startTime_stamp; if (GetTimeStamp != null) { GetTimeStamp(lastLevelTimeStamp); } } } if (GetM2MStructure != null) { GetM2MStructure(m2mStructure); } //最底层寻找真实路径 path.Clear(); ((M2M_Part_TagComparer)com).state = 2; if (end == start) { path.Add(end); return(path); } //判断起点和终点是否在地图上,并初始化起点的标签 Tag_M2M_Position tag = (Tag_M2M_Position)end.GetAttachment(); //生成这次寻径使用的time stamp time_stamp = TimeStamp.getNextTimeStamp(lastLevelTimeStamp); if (tag != null) { tag.g = 0; tag.parent = null; tag.isClose = false; tag.timeStamp = time_stamp; } IPosition_Connected p = null, p_adj; IPositionSet_Connected_Adjacency adj_set; Tag_M2M_Position p_tag, p_adj_tag; open.clear(); //将起点加入open表 open.add(end); //当open表非空时 while (open.getSize() > 0) { float newG; //获得下一个最近的点 p = open.removeFirst(); //到达终点则结束 if (p == start) { pathLength = ((Tag_M2M_Position)start.GetAttachment()).g; break; } p_tag = (Tag_M2M_Position)p.GetAttachment(); p_tag.isClose = true; adj_set = p.GetAdjacencyPositionSet(); adj_set.InitToTraverseSet(); while (adj_set.NextPosition()) { p_adj = adj_set.GetPosition_Connected(); p_adj_tag = (Tag_M2M_Position)p_adj.GetAttachment(); //如果需要搜索 Tag_M2M_Part parentPartTag = (Tag_M2M_Part)p_adj_tag.parentPart.GetAttachment(); if (parentPartTag.isNeedToSearch && parentPartTag.timeStamp == lastLevelTimeStamp) { //如果未被搜索 if (p_adj_tag.timeStamp != time_stamp) { newG = p_tag.g + adj_set.GetDistanceToAdjacency(); p_adj_tag.parent = p; p_adj_tag.g = newG; p_adj_tag.isClose = false; p_adj_tag.timeStamp = time_stamp; open.add(p_adj); } else { if (!p_adj_tag.isClose) { newG = p_tag.g + adj_set.GetDistanceToAdjacency(); if (newG < p_adj_tag.g) { p_adj_tag.parent = p; p_adj_tag.g = newG; open.update(p_adj); } } } } } } //从终点根据标签中记录的父节点找到起点,生成路径 if (p == start) { path.Add(start); p_tag = (Tag_M2M_Position)start.GetAttachment(); while (p_tag.parent != null) { path.Add(p_tag.parent); p_tag = (Tag_M2M_Position)p_tag.parent.GetAttachment(); } //path.Reverse(); return(path); } else { return(null); } }
private void panel1_MouseClick(object sender, MouseEventArgs e) { int x = e.X / blockSize, y = e.Y / blockSize; if (nodeToolStripButton.Checked) { if (x >= 0 && x < mapWidth && y >= 0 && y < mapHeight) { if (!map.Exists(x, y)) { map.AddPosition(x, y); drawNode_refreshTags(x, y); } } return; } if (connectionToolStripButton.Checked) { if (connecting) { connecting = false; if (map.Exists(x, y) && map.Exists(x1, y1) && (x != x1 || y != y1)) { map.AddConnection(x1, y1, x, y, (float)Math.Sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1))); ClearPath(); drawMap(); } else { refreshBlock(x1, y1); } } else { if (map.Exists(x, y)) { x1 = x; y1 = y; connecting = true; } } return; } if (doubleConnectionToolStripButton.Checked) { if (connecting) { connecting = false; if (map.Exists(x, y) && map.Exists(x1, y1) && (x != x1 || y != y1)) { map.AddDoubleConnection(x1, y1, x, y, (float)Math.Sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1))); ClearPath(); drawMap(); } else { refreshBlock(x1, y1); } } else { if (map.Exists(x, y)) { x1 = x; y1 = y; connecting = true; } } return; } if (eraserToolStripButton.Checked) { if (map.Exists(x, y)) { IPosition_Connected p = map.GetPosition(x, y); if (p == map.GetStartPosition()) { map.ClearStartPosition(); } if (p == map.GetEndPosition()) { map.ClearEndPosition(); } map.RemovePosition(x, y); ClearPath(); drawMap(); } return; } if (startPositionToolStripButton.Checked) { if (map.Exists(x, y)) { map.SetStartPosition(x, y); ClearPath(); refreshTags(); } return; } if (endPositionToolStripButton.Checked) { if (map.Exists(x, y)) { map.SetEndPosition(x, y); ClearPath(); refreshTags(); } return; } }
public Position_Distance(IPosition_Connected position, float distance) { this.position = position; this.distance = distance; }
protected void drawConnection(Pen pen, IPosition_Connected p1, IPosition_Connected p2) { drawConnection(pen, p1.GetX(), p1.GetY(), p2.GetX(), p2.GetY()); }