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); }
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 } //遍历每一层结束 }