public IPositionSet ApproximateSearchKNearestNeighbor2(IPosition targetPoint, int k)
        {
            int levelSequence;

            int BottonlevelSequence = m2mStructure.GetLevelNum() - 1;

            IPart currentPart = m2mStructure.GetLevel(1).GetPartRefByPoint(targetPoint);

            if (currentPart == null)
            {
                return(m2mStructure.GetBottonLevelPositionSetByAncestorPart(m2mStructure.GetLevel(0).GetPartRefByPoint(targetPoint), 0));
            }

            for (levelSequence = 2; levelSequence < BottonlevelSequence; levelSequence++)
            {
                ILevel currentLevel = m2mStructure.GetLevel(levelSequence);

                currentPart = currentLevel.GetPartRefByPoint(targetPoint);

                if ((currentPart == null) || (currentPart.GetBottomLevelPointNum() <= k))
                {
                    return(m2mStructure.GetBottonLevelPositionSetByAncestorPart(currentPart, levelSequence));
                }
            }

            currentPart = m2mStructure.GetLevel(BottonlevelSequence).GetPartRefByPoint(targetPoint);

            return(m2mStructure.GetBottonLevelPositionSetByAncestorPart(currentPart, BottonlevelSequence));
        }
Example #2
0
        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();
        }
            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);
            }
        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
            }
            //遍历每一层结束
        }
Example #5
0
        public override void Draw(Graphics graphics)
        {
            if (Visible)
            {
                SetDrawerGraphic(graphics);

                List <IPosition_Connected> PartMiddlePointList = new List <IPosition_Connected>();

                List <PointF[]> PointArrayList = new List <PointF[]>();

                ILevel       level       = m2mStructure.GetLevel(levelSequence);
                IPart        rootPart    = m2mStructure.GetLevel(0).GetPartRefByPartIndex(0, 0);
                IPositionSet positionSet = m2mStructure.GetDescendentPositionSetByAncestorPart(levelSequence, rootPart, 0);

                Dictionary <IPosition, IPosition_Connected_Edit> partToPositionDictionary = new Dictionary <IPosition, IPosition_Connected_Edit>();

                positionSet.InitToTraverseSet();
                while (positionSet.NextPosition())
                {
                    Position_Connected_Edit PartDeputyPosition_Connected = new Position_Connected_Edit();
                    partToPositionDictionary.Add(positionSet.GetPosition(), PartDeputyPosition_Connected);
                }

                positionSet.InitToTraverseSet();

                while (positionSet.NextPosition())
                {
                    IPart_Connected currentPart = (IPart_Connected)positionSet.GetPosition();

                    IPositionSet bottomLevelPositionSet = m2mStructure.GetBottonLevelPositionSetByAncestorPart(currentPart, levelSequence);

                    IPosition tempPoint = PositionSetAttribute.GetGravityCenter(bottomLevelPositionSet);

                    IPosition_Connected_Edit PartDeputyPosition_Connected;
                    partToPositionDictionary.TryGetValue(positionSet.GetPosition(), out PartDeputyPosition_Connected);
                    PartDeputyPosition_Connected.SetX(tempPoint.GetX());
                    PartDeputyPosition_Connected.SetY(tempPoint.GetY());


                    currentPart.GetAdjacencyPositionSet().InitToTraverseSet();

                    while (currentPart.GetAdjacencyPositionSet().NextPosition())
                    {
                        IPosition_Connected_Edit adjDeputy;
                        partToPositionDictionary.TryGetValue(currentPart.GetAdjacencyPositionSet().GetPosition(), out adjDeputy);

                        PartDeputyPosition_Connected.GetAdjacencyPositionSetEdit().AddAdjacency(
                            adjDeputy, currentPart.GetAdjacencyPositionSet().GetDistanceToAdjacency());
                    }

                    PartMiddlePointList.Add(PartDeputyPosition_Connected);

                    IPositionSet ConvexHullPointSet = null;

                    if (bottomLevelPositionSet.GetNum() > 1)
                    {
                        ConvexHullPointSet = new QuickHull().ConvexHull(bottomLevelPositionSet);
                    }
                    else
                    {
                        ConvexHullPointSet = bottomLevelPositionSet;
                    }

                    PointF[] tempArray = new PointF[ConvexHullPointSet.GetNum()];

                    int sequence = 0;
                    ConvexHullPointSet.InitToTraverseSet();
                    while (ConvexHullPointSet.NextPosition())
                    {
                        tempArray[sequence].X = ConvertRealXToScreenX(ConvexHullPointSet.GetPosition().GetX());
                        tempArray[sequence].Y = ConvertRealYToScreenY(ConvexHullPointSet.GetPosition().GetY());
                        sequence++;
                    }

                    PointArrayList.Add(tempArray);
                }

                Pen   pen   = new Pen(partColor, partBorderWidth);
                Brush brush = new SolidBrush(Color.FromArgb(alpha, partColor));

                foreach (PointF[] pointArray in PointArrayList)
                {
                    if (pointArray.Length >= 2)
                    {
                        graphics.DrawPolygon(pen, pointArray);
                        graphics.FillPolygon(brush, pointArray);
                    }
                }

                this.positionSet = new PositionSet_ConnectedEdit(PartMiddlePointList);

                positionSetDrawerPump.ResetPump();
                positionSetDrawerPump.DrawCoordinate(pointCoordinateDrawer);
                positionSetDrawerPump.DrawPoint(pointDrawer);
                positionSetDrawerPump.DrawConnection(connectionDrawer);
                positionSetDrawerPump.Run();
            }
        }