public void Preprocessing(IPositionSet positionSet) { positionSet.InitToTraverseSet(); while (positionSet.NextPosition()) { Insert(positionSet.GetPosition()); } }
public static void printPositionSet(IPositionSet ps) { ps.InitToTraverseSet(); while (ps.NextPosition()) { IPosition p = ps.GetPosition(); System.Console.Out.WriteLine(p.GetX() + "," + p.GetY() + ","); } }
public PositionSet_Transformed(IPositionSet positionSet, Transform transform) { this.positionSet = positionSet; this.transform = transform; positionSet.InitToTraverseSet(); positionSet.NextPosition(); if (positionSet.GetPosition() is IPosition_Edit) { isPositionEdit = true; } }
private void AddChildPositionSetToList(int parentPartLevelSequence, IPosition parentPart) { childPositionSetOfPart = m2mStructure.GetChildPositionSetByParentPart(parentPartLevelSequence, (IPart)parentPart); //得到凸包上的点的子分块集合 childPositionSetOfPart.InitToTraverseSet(); while (childPositionSetOfPart.NextPosition()) { tempChildPositionList.Add(childPositionSetOfPart.GetPosition()); } }
//计算多边形的重心 public static IPosition GetGravityCenter(IPositionSet ps) { //double xsum = 0, ysum = 0; //IPosition[] pa = (IPosition[])ps.ToArray(); //int n = pa.Length; //int nn = (pa[0] == pa[n - 1]) ? n : n + 1; //for (int i = 0; i < nn - 1; i++) //{ // double tvalue = pa[i].GetX() * pa[(i + 1) % n].GetY() - pa[i].GetY() * pa[(i + 1) % n].GetX(); // xsum += (pa[i].GetX() + pa[(i + 1) % n].GetX()) * tvalue; // ysum += (pa[i].GetY() + pa[(i + 1) % n].GetY()) * tvalue; //} //double area = GetArea(ps); //double cx = xsum / (6 * area); //double cy = ysum / (6 * area); //return new Position_Point((float)cx, (float)cy); ps.InitToTraverseSet(); ps.NextPosition(); float maxX = ps.GetPosition().GetX(); float minX = ps.GetPosition().GetX(); float maxY = ps.GetPosition().GetY(); float minY = ps.GetPosition().GetY(); while (ps.NextPosition()) { float x = ps.GetPosition().GetX(); float y = ps.GetPosition().GetY(); if (x > maxX) { maxX = x; } else if (x < minX) { minX = x; } if (y > maxY) { maxY = y; } else if (y < minY) { minY = y; } } return(new Position_Point((minX + maxX) / 2f, (minY + maxY) / 2f)); }
private void button1_Click_1(object sender, EventArgs e) { //取得测试数据 float rx = float.Parse(txtRX.Text); float ry = float.Parse(txtRY.Text); float sx = float.Parse(txtSX.Text); float sy = float.Parse(txtSY.Text); float ex = float.Parse(txtEX.Text); float ey = float.Parse(txtEY.Text); float itv = float.Parse(txtItv.Text); //求解 Position_Point rp = new Position_Point(rx, ry); Position_Point sp = new Position_Point(sx, sy); Position_Point ep = new Position_Point(ex, ey); IPositionSet ps = BlockLine.getBlockLine(rp, itv, sp, ep); //作图 pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height); Graphics g = Graphics.FromImage(pictureBox1.Image); g.Clear(Color.White); //网格线 for (float x = rx - itv; x >= 0; x -= itv) { g.DrawLine(Pens.Black, x, 0, x, pictureBox1.Height); } for (float y = ry - itv; y >= 0; y -= itv) { g.DrawLine(Pens.Black, 0, y, pictureBox1.Width, y); } for (float x = rx; x < pictureBox1.Width; x += itv) { g.DrawLine(Pens.Black, x, 0, x, pictureBox1.Height); } for (float y = ry; y < pictureBox1.Width; y += itv) { g.DrawLine(Pens.Black, 0, y, pictureBox1.Width, y); } //经过的网格 ps.InitToTraverseSet(); while (ps.NextPosition()) { IPosition p = ps.GetPosition(); float x = p.GetX(); float y = p.GetY(); g.FillRectangle(Brushes.Gray, x - itv / 2 + 1, y - itv / 2 + 1, itv - 1, itv - 1); } //起点和终点 g.FillEllipse(Brushes.Black, sx - 2, sy - 2, 4, 4); g.FillEllipse(Brushes.Black, ex - 2, ey - 2, 4, 4); g.DrawLine(Pens.Red, sx, sy, ex, ey); }
public PositionSet_Cloned(IPositionSet positionSet) { List <IPosition> positionList = new List <IPosition>(); positionSet.InitToTraverseSet(); while (positionSet.NextPosition()) { positionList.Add(new Position_Point(positionSet.GetPosition().GetX(), positionSet.GetPosition().GetY())); } this.positionSetEdit = new PositionSetEdit_ImplementByICollectionTemplate(positionList); this.positionSet = positionSetEdit; }
//根据点序列来作图 public void draw() { //生成报表第一行 reportTable = "\t"; IPositionSet ps0 = psList[0]; ps0.InitToTraverseSet(); while (ps0.NextPosition()) { IPosition p = ps0.GetPosition(); reportTable += p.GetX() + "\t"; } reportTable += "\r\n"; //作图并生成报表 for (int i = 0; i < nameList.Count; i++) { reportTable += nameList[i] + "\t"; List <double> xData = new List <double>(); List <double> yData = new List <double>(); IPositionSet ps = psList[i]; ps.InitToTraverseSet(); while (ps.NextPosition()) { if (bLogX) { xData.Add(Math.Log10(ps.GetPosition().GetX())); } else { xData.Add(ps.GetPosition().GetX()); } yData.Add(ps.GetPosition().GetY()); reportTable += ps.GetPosition().GetY() + "\t"; } reportTable += "\r\n"; form.addCurve(nameList[i], xData.ToArray(), yData.ToArray()); } FileStream fs = new FileStream(reportFileName, System.IO.FileMode.Create, FileAccess.ReadWrite); StreamWriter sw = new StreamWriter(fs, System.Text.UnicodeEncoding.Unicode); sw.Write(reportTable); sw.Close(); fs.Close(); form.Show(); }
private void OnGetLinePositionSetInSpecificLevel(ILevel level, int levelSequence, IPositionSet positionSet) { lock (layers) { if (linePartSetLayer == null) { linePartSetLayer = new Layer_M2MPartSetInSpecificLevel(level, positionSet); linePartSetLayer.MainColor = Settings.Default.LinePositionSetInSpecificLevelColor; linePartSetLayer.Alpha = 50; //linePartSetLayer.LineColor = Color.Red; linePartSetLayer.Active = true; layers.Add(linePartSetLayer); positionSetSet = new PositionSetSet(); BottonLevelPositionSetLayer = new Layer_PositionSet_Point(positionSetSet); BottonLevelPositionSetLayer.Point.IsDrawPointBorder = true; BottonLevelPositionSetLayer.Point.PointRadius = 2; BottonLevelPositionSetLayer.Point.PointColor = Settings.Default.BottonLevelPositionSetColor; layers.Add(BottonLevelPositionSetLayer); } else { linePartSetLayer.SpringLayerRepresentationChangedEvent(linePartSetLayer); } positionSetSet.Clear(); positionSet.InitToTraverseSet(); while (positionSet.NextPosition()) { IPart tempPart = level.GetPartRefByPartIndex((int)positionSet.GetPosition().GetX(), (int)positionSet.GetPosition().GetY()); if (tempPart != null) { positionSetSet.AddPositionSet(m2mStructure.GetBottonLevelPositionSetByAncestorPart(( tempPart), levelSequence)); } } BottonLevelPositionSetLayer.SpringLayerRepresentationChangedEvent(BottonLevelPositionSetLayer); } flowControlerForm.BeginInvoke(Update); flowControlerForm.SuspendAndRecordWorkerThread(); }
override public void InitToTraverseSet() { if (true) { if (AncestorLevelSequence == DescendantLevelSequence - 1) { DescendentPositionSetOfChildPart = M2MS.GetChildPositionSetByParentPart(AncestorLevelSequence, AncestorPart); DescendentPositionSetOfChildPart.InitToTraverseSet(); } else if (AncestorLevelSequence > DescendantLevelSequence - 1) { throw new Exception("AncestorLevelSequence > DescendantLevelSequence - 1"); } else { childPositionSet = M2MS.GetChildPositionSetByParentPart(AncestorLevelSequence, AncestorPart); childPositionSet.InitToTraverseSet(); if (childPositionSet.NextPosition()) { childPart = (IPart)childPositionSet.GetPosition(); //if (true) { DescendentPositionSetOfChildPart = new PositionSet_DescendentPosition(DescendantLevelSequence, (IPart)childPositionSet.GetPosition(), AncestorLevelSequence + 1, M2MS); DescendentPositionSetOfChildPart.InitToTraverseSet(); } //else //{ // state = State.ChildPartSubPointNumIsOne; // state = State.None; //} } } } }
public void Preprocessing2(IPositionSet positionSet) { this.positionSet = positionSet; completeThreadNum = 0; ThreadPool.SetMaxThreads(4, 4); positionSet.InitToTraverseSet(); for (int i = 0; i < ThreadNum; i++) { ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc)); } while (completeThreadNum < ThreadNum) { Thread.CurrentThread.Join(1); } }
public static List <IPosition_Connected_Edit> convertPositionSetToList(IPositionSet set, int width, int height, bool bounded) { List <IPosition_Connected_Edit> all = new List <IPosition_Connected_Edit>(); IPosition position; float x, y; //为将地图范围以内的点坐标创建节点 set.InitToTraverseSet(); while (set.NextPosition()) { position = set.GetPosition(); x = position.GetX(); y = position.GetY(); if (!bounded || (x <= width - 1 && y <= height - 1)) { all.Add(new Position_Connected_Edit(x, y)); } } return(all); }
private void button6_Click(object sender, EventArgs e) { QuickHull qn = new QuickHull(); IPositionSet ps = qn.ConvexHull(getPoints2((Bitmap)pictureBox1.Image)); Graphics g = Graphics.FromImage(pictureBox1.Image); IPosition p1 = ps.GetPosition(); IPosition p2 = ps.GetPosition(); while (p2 != null) { g.DrawLine(new Pen(Color.Green), new PointF(p1.GetX(), p1.GetY()), new PointF(p2.GetX(), p2.GetY())); p1 = p2; p2 = ps.GetPosition(); } ps.InitToTraverseSet(); p2 = ps.GetPosition(); g.DrawLine(new Pen(Color.Green), new PointF(p1.GetX(), p1.GetY()), new PointF(p2.GetX(), p2.GetY())); pictureBox1.Refresh(); }
public static List <IPosition_Connected_Edit> convertPositionSetToList_Integer(IPositionSet set, int width, int height, bool bounded) { List <IPosition_Connected_Edit> all = new List <IPosition_Connected_Edit>(); IPosition position; int x, y; //将点坐标取整,为将地图范围以内的点坐标创建节点 set.InitToTraverseSet(); while (set.NextPosition()) { position = set.GetPosition(); x = (int)position.GetX(); y = (int)position.GetY(); if (!bounded || (x < width && y < height)) { all.Add(new Position_Connected_Edit(x, y)); } } return(all); }
public IPosition GetTheNearestPointInPart() { IPositionSet BottonLevelPositionSet = M2MS.GetBottonLevelPositionSetByAncestorPart(currentPart, AncestorLevelSequence); BottonLevelPositionSet.InitToTraverseSet(); while (BottonLevelPositionSet.NextPosition()) { CompareToThePoint(BottonLevelPositionSet.GetPosition()); } //currentPart.OnGetOnePoint = this.CompareToThePoint; //((Part)currentPart).OnGetOnePoint += CompareToThePoint; //((Part)currentPart).TravelAllPointInPart(); //((Part)currentPart).OnGetOnePoint -= CompareToThePoint; return(nearestPointToComparePoint); }
protected RectangleF GetMaxRect() { RectangleF rect = new RectangleF(0f, 0f, 0f, 0f); float minX = float.MaxValue; float minY = float.MaxValue; float maxX = float.MinValue; float maxY = float.MinValue; positionSet.InitToTraverseSet(); while (positionSet.NextPosition()) { IPosition point = positionSet.GetPosition(); float x = point.GetX() * this.PositionSetScaleX + this.PositionSetTranslationX; if (minX > x) { minX = x; } else if (maxX < x) { maxX = x; } float y = point.GetY() * this.PositionSetScaleY + this.PositionSetTranslationY; if (minY > y) { minY = y; } else if (maxY < y) { maxY = y; } } return(new RectangleF(minX - 10, minY - 10, maxX - minX + 20, maxY - minY + 20)); }
public bool Collide(IPositionSet objPositionSet) { IPosition start = new Position_Point(); IPosition end = new Position_Point(); IPosition first = new Position_Point(); ILevel currentLevel = new Level(); for (int i = 1; i < m2mStructure.GetLevelNum(); i++) { currentLevel = m2mStructure.GetLevel(i); objPositionSet.InitToTraverseSet(); //判断第一点所在的分块是否相交 if (objPositionSet.NextPosition()) { start = new Position_Point(currentLevel.ConvertRealValueToRelativeValueX(objPositionSet.GetPosition().GetX()), currentLevel.ConvertRealValueToRelativeValueY(objPositionSet.GetPosition().GetY())); first = start; if (currentLevel.GetPartRefByPoint(start) != null) { if (i < m2mStructure.GetLevelNum() - 1) { goto NextLevel; } else { tempIntersectPositionSet.AddPosition(new Position_Point(currentLevel.ConvertRelativeValueToPartSequenceX(start.GetX()), currentLevel.ConvertRelativeValueToPartSequenceY(start.GetY()))); } } } while (objPositionSet.NextPosition()) { //判断每条边所在的分块是否相交 end = new Position_Point(currentLevel.ConvertRealValueToRelativeValueX(objPositionSet.GetPosition().GetX()), currentLevel.ConvertRealValueToRelativeValueY(objPositionSet.GetPosition().GetY())); IPositionSet positionSetTemp = writeLineInGridEngine.WriteLineInGrid(currentLevel.GetGridWidth(), currentLevel.GetGridHeight(), start, end); if (positionSetTemp != null) { positionSetTemp.InitToTraverseSet(); while (positionSetTemp.NextPosition()) { if (currentLevel.GetPartRefByPoint(positionSetTemp.GetPosition()) != null) { if (i < m2mStructure.GetLevelNum() - 1) { goto NextLevel; } else { tempIntersectPositionSet.AddPosition(new Position_Point(currentLevel.ConvertRelativeValueToPartSequenceX(positionSetTemp.GetPosition().GetX()), currentLevel.ConvertRelativeValueToPartSequenceY(positionSetTemp.GetPosition().GetY()))); } } } } //判断每条边的终点所在的分块是否相交 start = end; if (currentLevel.GetPartRefByPoint(start) != null) { if (i < m2mStructure.GetLevelNum() - 1) { goto NextLevel; } else { tempIntersectPositionSet.AddPosition(new Position_Point(currentLevel.ConvertRelativeValueToPartSequenceX(start.GetX()), currentLevel.ConvertRelativeValueToPartSequenceY(start.GetY()))); } } } //判断最后一条边所在的分块是否相交 IPositionSet positionSetTemp2 = writeLineInGridEngine.WriteLineInGrid(currentLevel.GetGridWidth(), currentLevel.GetGridHeight(), end, first); if (positionSetTemp2 != null) { positionSetTemp2.InitToTraverseSet(); while (positionSetTemp2.NextPosition()) { if (currentLevel.GetPartRefByPoint(positionSetTemp2.GetPosition()) != null) { if (i < m2mStructure.GetLevelNum() - 1) { goto NextLevel; } else { tempIntersectPositionSet.AddPosition(new Position_Point(currentLevel.ConvertRelativeValueToPartSequenceX(positionSetTemp2.GetPosition().GetX()), currentLevel.ConvertRelativeValueToPartSequenceY(positionSetTemp2.GetPosition().GetY()))); } } } } if (i == m2mStructure.GetLevelNum() - 1 && tempIntersectPositionSet.GetNum() > 0) { goto NextLevel; } #region code for algorithm demo if (GetNoCollision != null) { GetNoCollision(objPositionSet); Debug.WriteLine(i); } #endregion return(false);//free of collision NextLevel :; } #region code for algorithm demo if (GetCollision != null) { GetCollision(objPositionSet); } if (GetIntersectPart != null) { GetIntersectPart(currentLevel, tempIntersectPositionSet); } #endregion tempIntersectPositionSet.Clear(); return(true); }
override public bool NextPosition() { //if (state == State.ChildPartSubPointNumIsOne) //{ // returnPosition = childPart.GetRandomOneFormDescendantPoint(); // if (childPositionSet.NextPosition()) // { // childPart = (IPart)childPositionSet.GetPosition(); // if (childPart.GetBottomLevelPointNum() != 1) // { // DescendentPositionSetOfChildPart = // new PositionSet_DescendentPosition(DescendantLevelSequence, // childPart, // AncestorLevelSequence + 1, M2MS); // DescendentPositionSetOfChildPart.InitToTraverseSet(); // state = State.None; // } // else // { // state = State.ChildPartSubPointNumIsOne; // state = State.None; // } // } // else // { // DescendentPositionSetOfChildPart = null; // state = State.None; // } // return true; //} if (DescendentPositionSetOfChildPart == null) { //if (state == State.CurrentPartSubPointNumIsOne) //{ // state = State.None; // returnPosition = AncestorPart.GetRandomOneFormDescendantPoint(); // return true; //} return(false); } if (DescendentPositionSetOfChildPart.NextPosition()) { returnPosition = DescendentPositionSetOfChildPart.GetPosition(); return(true); } else { if (childPositionSet == null) { return(false); } if (childPositionSet.NextPosition()) { childPart = (IPart)childPositionSet.GetPosition(); if (true) { if (AncestorLevelSequence >= DescendantLevelSequence - 2) { DescendentPositionSetOfChildPart = M2MS.GetChildPositionSetByParentPart(AncestorLevelSequence, childPart); DescendentPositionSetOfChildPart.InitToTraverseSet(); if (DescendentPositionSetOfChildPart.NextPosition()) { returnPosition = DescendentPositionSetOfChildPart.GetPosition(); return(true); } else { return(false); } } else { DescendentPositionSetOfChildPart = new PositionSet_DescendentPosition(DescendantLevelSequence, childPart, AncestorLevelSequence + 1, M2MS); DescendentPositionSetOfChildPart.InitToTraverseSet(); if (DescendentPositionSetOfChildPart.NextPosition()) { returnPosition = DescendentPositionSetOfChildPart.GetPosition(); return(true); } else { return(false); } } } } else { return(false); } } }
//求散点集的凸包 public IPositionSet ConvexHull(IPositionSet ps) { IPosition p1 = null; IPosition p2 = null; PositionSetEdit_ImplementByICollectionTemplate rest = new PositionSetEdit_ImplementByICollectionTemplate(); //找到最低点 /* * p1 = pts[0]; * for (int i = 1; i < pts.Length; i++) * { * if (pts[i].Y < p1.Y || ( pts[i].Y == p1.Y && pts[i].X < p1.X) ) * { * rest.Add(p1); * p1 = pts[i]; * } * else * rest.Add(pts[i]); * } * */ ps.InitToTraverseSet(); if (ps.NextPosition()) { p1 = ps.GetPosition(); } while (ps.NextPosition()) { p2 = ps.GetPosition(); if (p2.GetY() < p1.GetY() || (p2.GetY() == p1.GetY() && p2.GetX() < p1.GetX())) { rest.AddPosition(p1); p1 = p2; } else { rest.AddPosition(p2); } } rest.InitToTraverseSet(); //找到与最低点成角最小的点 /* * int p2_i = 0; * double p2_angle = getAngle(rest[0], p1); * for (int i = 1; i < rest.Count; i++) * { * double angle_t = getAngle(rest[i], p1); * if (angle_t < p2_angle || (angle_t == p2_angle && getDistance(p1, rest[i]) > getDistance(p1, rest[p2_i])) ) * { * p2_i = i; * p2_angle = angle_t; * } * } * p2 = rest[p2_i]; * rest.RemoveAt(p2_i); * */ ps = rest; rest = new PositionSetEdit_ImplementByICollectionTemplate(); ps.InitToTraverseSet(); if (ps.NextPosition()) { p2 = ps.GetPosition(); } double p2_angle = getAngle(p2, p1); while (ps.NextPosition()) { IPosition pt = ps.GetPosition(); double angle_t = getAngle(pt, p1); if (angle_t < p2_angle || (angle_t == p2_angle && getDistance(p1, pt) > getDistance(p1, p2))) { rest.AddPosition(p2); p2 = pt; p2_angle = angle_t; } else { rest.AddPosition(pt); } } //求解剩余点 IPositionSet subResult = solveHelp(p1, p2, rest); //组合成凸包 PositionSetEdit_ImplementByICollectionTemplate result = new PositionSetEdit_ImplementByICollectionTemplate(); result.AddPosition(p1); /* * foreach (Point p in subResult) * result.Add(p); * */ subResult.InitToTraverseSet(); while (subResult.NextPosition()) { result.AddPosition(subResult.GetPosition()); } result.AddPosition(p2); return(result); }
private void BFSInOnePoint(Queue <PositionAndPositon> PositionAndPositonQueue, bool isPart, List <IPart_Connected> SubPartList, IPart_Multi currentMultiPart, float minSequenceX, float maxSequenceX, float minSequenceY, float maxSequenceY, IPosition_Connected currentPosition_Connected) { //如果没有被搜索 if (GetParentPart(currentPosition_Connected, isPart) == null) { IPart_Connected currentPart_Connected = currentMultiPart.CreateSubPart(); currentPart_Connected.SetAttachment(new Tag_M2M_Part()); currentPart_Connected.SetX(currentMultiPart.GetX()); currentPart_Connected.SetY(currentMultiPart.GetY()); bool isNeedToAddToSubPartList;// = true; SubPartList.Add(currentPart_Connected); currentPart_Connected.AddToSubPositionList(currentPosition_Connected); SetParentPart(currentPosition_Connected, currentPart_Connected, isPart); //以当前点为起始点做墨滴搜索 Queue <IPosition_Connected> OpenTable = new Queue <IPosition_Connected>(); OpenTable.Enqueue(currentPosition_Connected); //如果Open表还有元素则继续取出 while (OpenTable.Count > 0) { IPosition_Connected currentCenterPosition_Connected = OpenTable.Dequeue(); IPositionSet_Connected_Adjacency currentPositionAdjacencyPositionSet = currentCenterPosition_Connected.GetAdjacencyPositionSet(); currentPositionAdjacencyPositionSet.InitToTraverseSet(); while (currentPositionAdjacencyPositionSet.NextPosition()) { IPosition_Connected adjPosition_Connected = currentPositionAdjacencyPositionSet.GetPosition_Connected(); float pX = adjPosition_Connected.GetX(); float pY = adjPosition_Connected.GetY(); //如果分块在当前分块之内 if (pX >= minSequenceX && pX < maxSequenceX && pY >= minSequenceY && pY < maxSequenceY) { IPart_Connected parentPart = GetParentPart(adjPosition_Connected, isPart); //如果该分块还没被搜索 if (parentPart == null) { currentPart_Connected.AddToSubPositionList(adjPosition_Connected); SetParentPart(adjPosition_Connected, currentPart_Connected, isPart); //放进Open表 OpenTable.Enqueue(adjPosition_Connected); } //如果父分块不为null,则表明该分块之前已经被搜索过,刚搜索到的分块与原分块连通 else if (parentPart != currentPart_Connected) { isNeedToAddToSubPartList = false; //把原来的父分块设为新的父分块 IPositionSet tempPositionSet = parentPart.GetTrueChildPositionSet(); tempPositionSet.InitToTraverseSet(); while (tempPositionSet.NextPosition()) { IPosition_Connected temp = (IPosition_Connected)tempPositionSet.GetPosition(); SetParentPart(temp, currentPart_Connected, isPart); currentPart_Connected.AddToSubPositionList(temp); } SubPartList.Remove(parentPart); } } //如果不在当前分块里面 else { //如果在当前分块外的点还没被搜索(即还没被制定其父分块)则把其放进partAndPositonQueue IPart_Connected currentParentPart = GetParentPart(adjPosition_Connected, isPart); PositionAndPositonQueue.Enqueue(new PositionAndPositon(currentCenterPosition_Connected, adjPosition_Connected)); //if (currentParentPart == null) //{ // PositionAndPositonQueue.Enqueue(new PositionAndPositon(currentCenterPosition_Connected, adjPosition_Connected)); //} ////如果在当前分块外的点已经分配父分块,则建立两父分块间的链接关系。 //else //{ // currentPart_Connected.GetAdjacencyPositionSetEdit().AddAdjacency(currentParentPart, CalculateTheDistanceBetweenTwoPosition(currentPart_Connected, currentParentPart)); //} } } } } }
public static void lanch(IConvexHullEngine chEngine, int pointCount, float min, float max) { //求解凸包并作图 //IPositionSet ps = testData(); IPositionSet ps = new RandomPositionSet(pointCount, min, max); //System.Console.Out.WriteLine("position set:"); //printPositionSet(ps); IPositionSet cps = chEngine.ConvexHull(ps); //System.Console.Out.WriteLine("Jarvis Match:"); //printPositionSet(cps); PainterDialog.Clear(); PainterDialog.DrawPositionSet(ps); PainterDialog.DrawConvexHull(cps); PainterDialog.Show(); AnalyzeReport report = new AnalyzeReport(); //测试点集引用 cps.InitToTraverseSet(); ps.InitToTraverseSet(); bool correct = true; while (correct && cps.NextPosition()) { IPosition cp = cps.GetPosition(); bool find = false; ps.InitToTraverseSet(); while (!find && ps.NextPosition()) { IPosition p = ps.GetPosition(); if (cp == p) { find = true; } } if (!find) { correct = false; } } report.content += "引用测试:" + (correct ? "正确" : "错误") + "\n"; //测试凸包 if (correct) { report.content += "凸包正确性测试:\n"; IPositionSet cps_ref = (new QuickHull()).ConvexHull(ps); // System.Console.Out.WriteLine("Quick Hull:"); // printPositionSet(cps_ref); IPosition[] cpa = (IPosition[])(cps.ToArray()); IPosition[] cpa_ref = (IPosition[])(cps_ref.ToArray()); if (cpa.Length != cpa_ref.Length) { report.content += "数目不等"; correct = false; } if (correct) { int n = cpa.Length; int m = 0; int p = 0; for (; p < n; p++) { if (cpa[0] == cpa_ref[p]) { break; } } if (p == n) { correct = false; report.content += "发生错误!"; } if (correct) { for (int i = 0; i < n; i++) { if (cpa[i] == cpa_ref[(p + i + n) % n]) { m++; } } int tm = 0; for (int i = 0; i < n; i++) { if (cpa[i] == cpa_ref[(p - i + n) % n]) { tm++; } } if (tm > m) { m = tm; } report.content += "正确率:" + m.ToString() + "/" + n.ToString(); if (m == n) { report.content += "正确!"; } else { report.content += "不正确!"; } } } } report.Show(); }
public void TraversalEveryLevelAndBuild(IM2MStructure m2mStructure) { IPart rootPart = m2mStructure.GetLevel(0).GetPartRefByPartIndex(0, 0); IPositionSet bottonLevelPositionSet = m2mStructure.GetBottonLevelPositionSetByAncestorPart(rootPart, 0); bottonLevelPositionSet.InitToTraverseSet(); while (bottonLevelPositionSet.NextPosition()) { ((IPosition_Connected)(bottonLevelPositionSet.GetPosition())).SetAttachment(new Tag_M2M_Position()); } //遍历每一层 for (int levelSequence = m2mStructure.GetLevelNum() - 1; levelSequence >= 0; levelSequence--) { Queue <PositionAndPositon> partAndPositonQueue = new Queue <PositionAndPositon>(); bool isPart = true; ILevel currentLevel = m2mStructure.GetLevel(levelSequence); IPositionSet currrentLevelPositionSet = null; //如果处于最底层 if (levelSequence == m2mStructure.GetLevelNum() - 1) { isPart = false; } if (levelSequence == 0) { IPositionSetEdit temp = new Position_Implement.PositionSetEdit_ImplementByICollectionTemplate(); temp.AddPosition(rootPart); currrentLevelPositionSet = temp; } else { currrentLevelPositionSet = m2mStructure.GetDescendentPositionSetByAncestorPart(levelSequence, rootPart, 0); } currrentLevelPositionSet.InitToTraverseSet(); //遍历每一个分块 while (currrentLevelPositionSet.NextPosition()) { List <IPart_Connected> SubPartList = new List <IPart_Connected>(); IPart_Multi currentMultiPart = (IPart_Multi)(currrentLevelPositionSet.GetPosition()); //计算part的边界 float minSequenceX; float maxSequenceX; float minSequenceY; float maxSequenceY; if (isPart) { float unitNumInLength = (int)(m2mStructure.GetLevel(levelSequence + 1).GetUnitNumInHeight() / currentLevel.GetUnitNumInHeight()); minSequenceX = currentMultiPart.GetX() * unitNumInLength; maxSequenceX = minSequenceX + unitNumInLength; minSequenceY = currentMultiPart.GetY() * unitNumInLength; maxSequenceY = minSequenceY + unitNumInLength; } else { minSequenceX = currentLevel.ConvertPartSequenceXToRealValue(currentMultiPart.GetX()); maxSequenceX = minSequenceX + currentLevel.GetGridWidth(); minSequenceY = currentLevel.ConvertPartSequenceYToRealValue(currentMultiPart.GetY()); maxSequenceY = minSequenceY + currentLevel.GetGridHeight(); } IPositionSet childPositionSetOfCurrentPart = m2mStructure.GetChildPositionSetByParentPart(levelSequence, currentMultiPart); childPositionSetOfCurrentPart.InitToTraverseSet(); //遍历每一个子分块或点 while (childPositionSetOfCurrentPart.NextPosition()) { if (isPart) { IPart_Multi childPartMulti = (IPart_Multi)childPositionSetOfCurrentPart.GetPosition(); foreach (IPosition_Connected currentPosition_Connected in childPartMulti.GetSubPartSet()) { BFSInOnePoint(partAndPositonQueue, isPart, SubPartList, currentMultiPart, minSequenceX, maxSequenceX, minSequenceY, maxSequenceY, currentPosition_Connected); } } else { IPosition_Connected currentPosition_Connected = (IPosition_Connected)childPositionSetOfCurrentPart.GetPosition(); BFSInOnePoint(partAndPositonQueue, isPart, SubPartList, currentMultiPart, minSequenceX, maxSequenceX, minSequenceY, maxSequenceY, currentPosition_Connected); } } //遍历每一个子分块结束 currentMultiPart.SetSubPartSet(SubPartList); } //遍历完某层的每个分块 //处理还没建立链接的点和分块对 foreach (PositionAndPositon pap in partAndPositonQueue) { IPosition_Connected position_Connected = pap.position; IPart_Connected currentParentPart = GetParentPart(position_Connected, isPart); IPart_Connected currentParentPart2 = GetParentPart(pap.positionInExistPart, isPart); float distance = CalculateTheDistanceBetweenTwoPosition(currentParentPart2, currentParentPart); IPositionSet_Connected_AdjacencyEdit positionSet_Connected_Adjacency = currentParentPart2.GetAdjacencyPositionSetEdit(); bool isContain = false; positionSet_Connected_Adjacency.InitToTraverseSet(); while (positionSet_Connected_Adjacency.NextPosition()) { if (positionSet_Connected_Adjacency.GetPosition() == currentParentPart) { isContain = true; break; } } if (isContain) { continue; } else { positionSet_Connected_Adjacency.AddAdjacency(currentParentPart, distance); } positionSet_Connected_Adjacency = currentParentPart.GetAdjacencyPositionSetEdit(); isContain = false; positionSet_Connected_Adjacency.InitToTraverseSet(); while (positionSet_Connected_Adjacency.NextPosition()) { if (positionSet_Connected_Adjacency.GetPosition() == currentParentPart2) { isContain = true; break; } } if (isContain) { continue; } else { positionSet_Connected_Adjacency.AddAdjacency(currentParentPart2, distance); } } #region code for algorithm demo if (GetPartSetInSpecificLevel != null) { GetPartSetInSpecificLevel(m2mStructure.GetLevel(levelSequence), levelSequence, m2mStructure.GetDescendentPositionSetByAncestorPart(levelSequence, rootPart, 0)); } #endregion } //遍历每一层结束 }
override public void InitToTraverseSet() { positionSet.InitToTraverseSet(); }
//求散点子集的凸包 private IPositionSet solveHelp(IPosition p1, IPosition p2, IPositionSet ps) { //求离p1,p2最远的点 ps.InitToTraverseSet(); if (!ps.NextPosition()) { return(ps); } IPosition p3 = ps.GetPosition(); double p3_h = getH(p1, p2, p3); PositionSetEdit_ImplementByICollectionTemplate rest = new PositionSetEdit_ImplementByICollectionTemplate(); while (ps.NextPosition()) { IPosition pt = ps.GetPosition(); double pt_h = getH(p1, p2, pt); if (pt_h > p3_h) { rest.AddPosition(p3); p3 = pt; p3_h = pt_h; } else { rest.AddPosition(pt); } } //把散点分成几部分 PositionSetEdit_ImplementByICollectionTemplate leftpart = new PositionSetEdit_ImplementByICollectionTemplate(); PositionSetEdit_ImplementByICollectionTemplate rightpart = new PositionSetEdit_ImplementByICollectionTemplate(); PositionSetEdit_ImplementByICollectionTemplate p1set = new PositionSetEdit_ImplementByICollectionTemplate(); PositionSetEdit_ImplementByICollectionTemplate p2set = new PositionSetEdit_ImplementByICollectionTemplate(); ps = rest; IPosition p11 = new Position_Point(2 * p2.GetX() - p1.GetX(), 2 * p2.GetY() - p1.GetY()); //p1关于p2的对称点 double leftangle = getAngle(p1, p2, p3); double rightangle = getAngle(p2, p11, p3); ps.InitToTraverseSet(); while (ps.NextPosition()) { IPosition p = ps.GetPosition(); if (p.GetX() == p1.GetX() && p.GetY() == p1.GetY()) { p1set.AddPosition(p); } else if (p.GetX() == p2.GetX() && p.GetY() == p2.GetY()) { p2set.AddPosition(p); } else if (p.GetX() == p3.GetX() && p.GetY() == p3.GetY()) { leftpart.AddPosition(p); } else { double a = getAngle(p1, p2, p); if (a > leftangle) { leftpart.AddPosition(p); } a = getAngle(p2, p11, p); if (a < rightangle) { rightpart.AddPosition(p); } } } //对子集求解 IPositionSet subResult1 = solveHelp(p1, p3, leftpart); IPositionSet subResult2 = solveHelp(p3, p2, rightpart); //合并各部分解 PositionSetEdit_ImplementByICollectionTemplate result = new PositionSetEdit_ImplementByICollectionTemplate(); p1set.InitToTraverseSet(); while (p1set.NextPosition()) { result.AddPosition(p1set.GetPosition()); } subResult1.InitToTraverseSet(); while (subResult1.NextPosition()) { result.AddPosition(subResult1.GetPosition()); } result.AddPosition(p3); subResult2.InitToTraverseSet(); while (subResult2.NextPosition()) { result.AddPosition(subResult2.GetPosition()); } p2set.InitToTraverseSet(); while (p2set.NextPosition()) { result.AddPosition(p2set.GetPosition()); } return(result); }
private void TestBotton_Click(object sender, EventArgs e) { PainterDialog painterDialog = new PainterDialog(); RandomPositionSet_Connected_Config config = new RandomPositionSet_Connected_Config(); new ConfiguratedByForm(config); IPositionSet_ConnectedEdit pSet = config.Produce(); painterDialog.DrawPositionSet_Connected(pSet); M2MSCreater_ForGeneralM2MStruture m2m_Creater_ForGeneralM2MStruture = new M2MSCreater_ForGeneralM2MStruture(); m2m_Creater_ForGeneralM2MStruture.PartType = typeof(Part_Multi); m2m_Creater_ForGeneralM2MStruture.SetPointInPartFactor(10); m2m_Creater_ForGeneralM2MStruture.SetUnitNumInGridLength(4); IM2MStructure m2mStructure = m2m_Creater_ForGeneralM2MStruture.CreateAutomatically(pSet); m2mStructure.Preprocessing(pSet); painterDialog.DrawM2MStructure(m2mStructure); BuildPartSetConnectionForM2MStructure buildPartSetConnectionForM2MStructure = new BuildPartSetConnectionForM2MStructure(); buildPartSetConnectionForM2MStructure.GetPartSetInSpecificLevel += delegate(ILevel level, int levelSequence, IPositionSet positionSet) { List <IPosition_Connected> position_ConnectedList = new List <IPosition_Connected>(); positionSet.InitToTraverseSet(); while (positionSet.NextPosition()) { IPart_Multi partMulti = (IPart_Multi)positionSet.GetPosition(); IEnumerable <IPart_Connected> part_ConnectedEnumerable = partMulti.GetSubPartSet(); foreach (IPart_Connected part in part_ConnectedEnumerable) { position_ConnectedList.Add((IPosition_Connected)part); } } PositionSet_Connected positionSet_Connected = new PositionSet_Connected(position_ConnectedList); Layer_PositionSet_Connected layer_PositionSet_Connected = new Layer_PositionSet_Connected(positionSet_Connected); layer_PositionSet_Connected.SetPositionSetTransformByM2MLevel(level); painterDialog.Layers.Add(layer_PositionSet_Connected); painterDialog.Show(); }; buildPartSetConnectionForM2MStructure.TraversalEveryLevelAndBuild(m2mStructure); 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) { position_ConnectedList.Add((IPosition_Connected)part); } } else { position_ConnectedList.Add((IPosition_Connected)positionSet.GetPosition()); } } PositionSet_Connected positionSet_Connected = new PositionSet_Connected(position_ConnectedList); Layer_PositionSet_Connected layer_PartSet_Connected = new Layer_PositionSet_Connected(positionSet_Connected); //layer_PartSet_Connected.MainColor //layer_PartSet_Connected.Active = true; layer_PartSet_Connected.SetPositionSetTransformByM2MLevel(level); painterDialog.Layers.Add(layer_PartSet_Connected); } painterDialog.Show(); //painterDialog.Show(); }
public IPositionSet ConvexHull(IPositionSet ps) { List <IPosition> result = new List <IPosition>(); List <IPosition> posList = new List <IPosition>(); IPosition p0 = null, p1 = null, p3; int index = 0; //先转换成List ps.InitToTraverseSet(); while (ps.NextPosition()) { posList.Add(ps.GetPosition()); } //找出第1个点(最低点) index = 0; for (int i = 1; i < posList.Count; i++) { p0 = posList[index]; p1 = posList[i]; if (p1.GetY() < p0.GetY() || (p1.GetY() == p0.GetY() && p1.GetX() < p0.GetX())) { index = i; } } p0 = p1 = posList[index]; removePosition(posList, result, index); //找到第2个点 /* * index = 0; * preAngle = getAngle(p1, posList[index]); * for (int i = 1; i < posList.Count; i++) * { * p2 = posList[i]; * curAngle = getAngle(p1, p2); * if (curAngle < preAngle || (curAngle == preAngle && getDistance(p1, p2) > getDistance(p1, posList[index]))) * { * index = i; * preAngle = curAngle; * } * } * p2 = posList[index]; * removePosition(posList, result, index); */ posList.Add(p0); //重新加入起点,好让能构成环让算法结束 //找到其他点 while (true) { index = 0; //preAngle = getAngle(p2, posList[index], p1); if (posList.Count <= 1) { break; } for (int i = 1; i < posList.Count; i++) { p3 = posList[i]; //curAngle = getAngle(p2, p3, p1); double dir = getDirection(p1, posList[index], p3); if (dir < 0 || (dir == 0 && getDistance(p1, p3) > getDistance(p1, posList[index]))) //if (curAngle > preAngle || (curAngle == preAngle && getDistance(p2, p3) > getDistance(p2, posList[index]))) { index = i; //preAngle = curAngle; } } p3 = posList[index]; if (p3 == p0) { break; } removePosition(posList, result, index); p1 = p3; } return(new PositionSetEdit_ImplementByICollectionTemplate(result)); }
public IPositionSet QueryConvexHull() { #region code for algorithm demo if (GetChildPositionSetInSpecificLevelOfConvexHull != null) { GetChildPositionSetInSpecificLevelOfConvexHull(m2mStructure.GetLevel(1), 1, m2mStructure.GetChildPositionSetByParentPart(0, m2mStructure.GetLevel(0).GetPartRefByPartIndex(0, 0))); } #endregion IPositionSet PositionSetInConvexHull = convexHullEngine.ConvexHull( m2mStructure.GetChildPositionSetByParentPart(0, m2mStructure.GetLevel(0).GetPartRefByPartIndex(0, 0))); for (int i = 1; i < m2mStructure.GetLevelNum(); i++) { ILevel currentLevel = m2mStructure.GetLevel(i); #region code for algorithm demo if (GetConvexHullPositionSetInSpecificLevel != null) { GetConvexHullPositionSetInSpecificLevel(currentLevel, i, PositionSetInConvexHull); } #endregion tempChildPositionList = new List <IPosition>(); tempChildPositionList.Capacity = 4 * PositionSetInConvexHull.GetNum(); PositionSetInConvexHull.InitToTraverseSet(); IPosition hullStartPoint = null; IPosition temp1 = null; IPosition temp2 = null; if (PositionSetInConvexHull.NextPosition()) { temp1 = PositionSetInConvexHull.GetPosition(); hullStartPoint = temp1; AddChildPositionSetToList(i, temp1); #region code for algorithm demo if (GetLinePositionSetInSpecificLevel != null) { linePositionSetInSpecificLevel = new PositionSetEdit_ImplementByICollectionTemplate(); linePositionSetInSpecificLevel.AddPosition(new Position_Point(temp1.GetX(), temp1.GetY())); } #endregion } #region code for algorithm demo if (GetRepresentativeHullInSpecificLevel != null) { representativeHull = new PositionSetEdit_ImplementByICollectionTemplate(); } #endregion bool isWaitingLastPoint = true; //遍历该层凸包的每个分块,以确定下一层的候选分块 while (true) { if (isWaitingLastPoint) { if (PositionSetInConvexHull.NextPosition() == false) { isWaitingLastPoint = false; temp2 = hullStartPoint; } else { temp2 = PositionSetInConvexHull.GetPosition(); AddChildPositionSetToList(i, temp2); } } else { break; } IPosition start = new Position_Point(currentLevel.ConvertRealValueToRelativeValueX(((IPart)temp1).GetRandomOneFormDescendantPoint().GetX()), currentLevel.ConvertRealValueToRelativeValueY(((IPart)temp1).GetRandomOneFormDescendantPoint().GetY())); IPosition end = new Position_Point(currentLevel.ConvertRealValueToRelativeValueX(((IPart)temp2).GetRandomOneFormDescendantPoint().GetX()), currentLevel.ConvertRealValueToRelativeValueY(((IPart)temp2).GetRandomOneFormDescendantPoint().GetY())); IPositionSet tempPositionSet = writeLineInGridEngine.WriteLineInGrid(currentLevel.GetGridWidth(), currentLevel.GetGridHeight(), start, end); #region code for algorithm demo if (GetRepresentativeHullInSpecificLevel != null) { representativeHull.AddPosition(start); representativeHull.AddPosition(end); GetRepresentativeHullInSpecificLevel(m2mStructure.GetLevel(i), i, representativeHull); } #endregion tempPositionSet.InitToTraverseSet(); while (tempPositionSet.NextPosition()) { IPosition tempPosition = tempPositionSet.GetPosition(); IPart tempPart = currentLevel.GetPartRefByPartIndex(currentLevel.ConvertRelativeValueToPartSequenceX(tempPosition.GetX()), currentLevel.ConvertRelativeValueToPartSequenceY(tempPosition.GetY())); #region code for algorithm demo if (GetLinePositionSetInSpecificLevel != null) { linePositionSetInSpecificLevel.AddPosition(new Position_Point(currentLevel.ConvertRelativeValueToPartSequenceX(tempPosition.GetX()) , currentLevel.ConvertRelativeValueToPartSequenceY(tempPosition.GetY()))); } #endregion if (tempPart != null) { AddChildPositionSetToList(i, tempPart); } } #region code for algorithm demo if (GetLinePositionSetInSpecificLevel != null) { if (isWaitingLastPoint) { linePositionSetInSpecificLevel.AddPosition(new Position_Point(temp2.GetX(), temp2.GetY())); } } #endregion #region code for algorithm demo if (GetLinePositionSetInSpecificLevel != null) { GetLinePositionSetInSpecificLevel(m2mStructure.GetLevel(i), i, linePositionSetInSpecificLevel); } #endregion temp1 = temp2; } IPositionSet ChildPositionSetInConvexHull = new PositionSetEdit_ImplementByICollectionTemplate(tempChildPositionList); #region code for algorithm demo if (GetChildPositionSetInSpecificLevelOfConvexHull != null) { if (i < m2mStructure.GetLevelNum() - 1) { GetChildPositionSetInSpecificLevelOfConvexHull(m2mStructure.GetLevel(i + 1), i + 1, ChildPositionSetInConvexHull); } } #endregion PositionSetInConvexHull = convexHullEngine.ConvexHull(ChildPositionSetInConvexHull); } #region code for algorithm demo if (GetRealConvexHull != null) { GetRealConvexHull(PositionSetInConvexHull); } #endregion return(PositionSetInConvexHull); }
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 M2MStructure_General CreateAutomatically(IPositionSet positionSet, int level) { int PointNum = 0; float leftestX = 0; float rightestX = 0; float lowestY = 0; float highestY = 0; positionSet.InitToTraverseSet(); if (positionSet.NextPosition()) { leftestX = positionSet.GetPosition().GetX(); rightestX = positionSet.GetPosition().GetX(); lowestY = positionSet.GetPosition().GetY(); highestY = positionSet.GetPosition().GetY(); PointNum++; } while (positionSet.NextPosition()) { if (positionSet.GetPosition().GetX() > rightestX) { rightestX = positionSet.GetPosition().GetX(); } else if (positionSet.GetPosition().GetX() < leftestX) { leftestX = positionSet.GetPosition().GetX(); } if (positionSet.GetPosition().GetY() > highestY) { highestY = positionSet.GetPosition().GetY(); } else if (positionSet.GetPosition().GetY() < lowestY) { lowestY = positionSet.GetPosition().GetY(); } PointNum++; } if (PointNum == 0) { throw new Exception("PointNum == 0"); } float mapWidth = (rightestX - leftestX) * set_Bias; float mapHeight = (highestY - lowestY) * set_Bias; int MicPartNumInMacPart = set_UnitNumInGridLength * set_UnitNumInGridLength; float maxGridLength; if (mapWidth > mapHeight) { maxGridLength = mapHeight / set_UnitNumInGridLength + float.Epsilon; } else { maxGridLength = mapWidth / set_UnitNumInGridLength + float.Epsilon; } Init(leftestX, lowestY, mapWidth, mapHeight, maxGridLength, set_UnitNumInGridLength, level); return(Create()); }
public void PreProcess(PositionSetEditSet pointList) { #region code for algorithm demo if (GetPositionSetToGetConvexHull != null) { GetPositionSetToGetConvexHull(pointList); } #endregion //请在这里改变不同的m2mStructure实现。 M2MSCreater_ForGeneralM2MStruture m2m_Creater_ForGeneralM2MStruture = new M2MSCreater_ForGeneralM2MStruture(); m2mStructure = m2m_Creater_ForGeneralM2MStruture.CreateAutomatically(pointList, levelNum); PositionSetEdit_ImplementByICollectionTemplate positionSet = new PositionSetEdit_ImplementByICollectionTemplate(); ILevel bottomLevel = m2mStructure.GetLevel(m2mStructure.GetLevelNum() - 1); for (int i = 0; i < pointList.GetPositionSetNum(); i++) { IPosition start = new Position_Point(); IPosition end = new Position_Point(); IPosition first = new Position_Point(); IPositionSet positionListTemp = pointList.GetNthPositionSet(i); positionListTemp.InitToTraverseSet(); if (positionListTemp.NextPosition()) { start = new Position_Point(bottomLevel.ConvertRealValueToRelativeValueX(positionListTemp.GetPosition().GetX()), bottomLevel.ConvertRealValueToRelativeValueY(positionListTemp.GetPosition().GetY())); first = start; positionSet.AddPosition(start); } while (positionListTemp.NextPosition()) { end = new Position_Point(bottomLevel.ConvertRealValueToRelativeValueX(positionListTemp.GetPosition().GetX()), bottomLevel.ConvertRealValueToRelativeValueY(positionListTemp.GetPosition().GetY())); IPositionSet positionSetTemp = writeLineInGridEngine.WriteLineInGrid(bottomLevel.GetGridWidth(), bottomLevel.GetGridHeight(), start, end); if (positionSetTemp != null) { positionSetTemp.InitToTraverseSet(); while (positionSetTemp.NextPosition()) { positionSet.AddPosition(positionSetTemp.GetPosition()); } } start = end; positionSet.AddPosition(start); } IPositionSet positionSetTemp2 = writeLineInGridEngine.WriteLineInGrid(bottomLevel.GetGridWidth(), bottomLevel.GetGridHeight(), end, first); if (positionSetTemp2 != null) { positionSetTemp2.InitToTraverseSet(); while (positionSetTemp2.NextPosition()) { positionSet.AddPosition(positionSetTemp2.GetPosition()); } } } m2mStructure.Preprocessing(positionSet); #region code for algorithm demo if (GetM2MStructure != null) { GetM2MStructure(m2mStructure); } #endregion }