Exemplo n.º 1
0
        private Node BBFFindNearest(Node tree, Train target)
        {
            Node nearest = new Node();        //最近邻节点

            nearest = tree;

            float priority = -1; //优先级

            //计算优先级
            priority = this.CalPriority(nearest, target, tree.split);
            this.InsertPriorityList(nearest, priority); //将根节点加入优先队列中

            Node   topNode     = null;                  //优先级最高的节点
            Node   currentNode = null;
            double dist        = 0;

            while (priorityList.Count > 0)
            {
                topNode = priorityList[0].node; //优先队列中的第一个总是优先级最高的
                this.RmovePriority(topNode);

                while (topNode != null)
                {
                    //当前节点不是叶子节点
                    if (topNode.leftNode != null || topNode.righNode != null)
                    {
                        int split = topNode.split;
                        if (split == 0)
                        {
                            if (target.positionX <= topNode.point.positionX)
                            {
                                //current = topNode.point;
                                //若右节点不为空,将右子树节点添加到优先队列中
                                if (topNode.righNode != null)
                                {
                                    priority = this.CalPriority(topNode.righNode, target, split);
                                    this.InsertPriorityList(topNode.righNode, priority);
                                }

                                topNode = topNode.leftNode;
                            }
                            else
                            {
                                //current = topNode.point;
                                //将左子树节点添加到优先级队列中
                                if (topNode.leftNode != null)
                                {
                                    priority = this.CalPriority(topNode.leftNode, target, split);
                                    this.InsertPriorityList(topNode.leftNode, priority);
                                }

                                topNode = topNode.righNode;
                            }

                            currentNode = topNode;
                        }
                        else if (split == 1)
                        {
                            if (target.positionY <= topNode.point.positionY)
                            {
                                //current = topNode.point;
                                //将右子树节点添加到优先队列中
                                if (topNode.righNode != null)
                                {
                                    priority = this.CalPriority(topNode.righNode, target, split);
                                    this.InsertPriorityList(topNode.righNode, priority);
                                }

                                topNode = topNode.leftNode;
                            }
                            else
                            {
                                //current = topNode.point;
                                //将左子树节点添加到优先级队列中
                                if (topNode.leftNode != null)
                                {
                                    priority = this.CalPriority(topNode.leftNode, target, split);
                                    this.InsertPriorityList(topNode.leftNode, priority);
                                }

                                topNode = topNode.righNode;
                            }

                            currentNode = topNode;
                        }
                        else
                        {
                            if (target.positionZ <= topNode.point.positionZ)
                            {
                                //current = topNode.point;
                                //将右子树节点添加到优先队列中
                                if (topNode.righNode != null)
                                {
                                    priority = this.CalPriority(topNode.righNode, target, split);
                                    this.InsertPriorityList(topNode.righNode, priority);
                                }

                                topNode = topNode.leftNode;
                            }
                            else
                            {
                                //current = topNode.point;
                                //将左子树节点添加到优先级队列中
                                if (topNode.leftNode != null)
                                {
                                    priority = this.CalPriority(topNode.leftNode, target, split);
                                    this.InsertPriorityList(topNode.leftNode, priority);
                                }

                                topNode = topNode.righNode;
                            }
                            currentNode = topNode;
                        }
                    }
                    else
                    {
                        currentNode = topNode;
                        topNode     = null; //叶子节点
                    }

                    if (currentNode != null && Distance(nearest.point, target) > Distance(currentNode.point, target))
                    {
                        nearest = currentNode;
                        dist    = Distance(currentNode.point, target);
                    }
                }
            }
            return(nearest);
        }
Exemplo n.º 2
0
 //寻找K个近邻的节点
 private void BBFKNNFind(Node tree, Train target, int k = 3)
 {
 }
Exemplo n.º 3
0
        private Node KDTreeFindNearest(Node tree, Train target)
        {
            double      dist       = double.MaxValue;   //最近邻和目标点之间的距离
            Node        nearest    = null;              //最近邻节点
            Node        searchNode = tree;              //根节点
            List <Node> searchPath = new List <Node>(); //搜索路径

            #region 检索搜索路径
            while (searchNode != null)
            {
                searchPath.Add(searchNode); //添加当前搜索节点

                if (searchNode.split == 0)
                {
                    //X方向轴分割
                    if (target.positionX <= searchNode.point.positionX)
                    {
                        searchNode = searchNode.leftNode;
                    }
                    else
                    {
                        searchNode = searchNode.righNode;
                    }
                }
                else if (searchNode.split == 1)
                {
                    //Y方向轴分割
                    if (target.positionY <= searchNode.point.positionY)
                    {
                        searchNode = searchNode.leftNode;
                    }
                    else
                    {
                        searchNode = searchNode.righNode;
                    }
                }
                else
                {
                    if (target.positionZ <= searchNode.point.positionZ)
                    {
                        searchNode = searchNode.leftNode;
                    }
                    else
                    {
                        searchNode = searchNode.righNode;
                    }
                }
            }
            #endregion

            nearest = searchPath[searchPath.Count - 1]; //获取搜索路径最后一个节点
            searchPath.Remove(nearest);                 //从搜索路径中删除假定的最近邻节点

            //计算两个节点之间的距离
            dist = this.Distance(nearest.point, target);

            #region 回溯路径搜索

            Node backNode;
            int  split = -1;
            while (searchPath.Count > 0)
            {
                backNode = searchPath[searchPath.Count - 1];    //取搜索路径最后一个
                if (backNode.leftNode == null && backNode.righNode == null)
                {
                    //该节点为叶子节点
                    if (Distance(nearest.point, target) > Distance(backNode.point, target))
                    {
                        nearest = backNode;
                        dist    = Distance(backNode.point, target);
                    }
                }
                else
                {
                    split = backNode.split;     //取出当前的分割方向轴
                    if (split == 0)
                    {
                        if (Distance(backNode.point, target) < dist)
                        {
                            nearest = backNode;
                            dist    = Distance(backNode.point, target);
                        }

                        //确定是否进入子空间搜索
                        float disTmp = Math.Abs(target.positionX - backNode.point.positionX);
                        if (disTmp < dist)
                        {
                            //判断目标点是在左子空间还是右子空间
                            if (target.positionX > backNode.point.positionX)
                            {
                                //目标点在右子空间,则需要进入左子空间进行搜索
                                searchNode = backNode.leftNode;
                            }
                            else
                            {
                                searchNode = backNode.righNode;
                            }

                            searchPath.Add(searchNode);     //添加搜索节点
                        }
                    }
                    else if (split == 1)
                    {
                        if (Distance(backNode.point, target) < dist)
                        {
                            nearest = backNode;
                            dist    = Distance(backNode.point, target);
                        }

                        //确定是否进入子空间搜索
                        float disTmp = Math.Abs(target.positionY - backNode.point.positionY);
                        if (disTmp < dist)
                        {
                            //判断目标点是在左子空间还是右子空间
                            if (target.positionY > backNode.point.positionY)
                            {
                                //目标点在右子空间,则需要进入左子空间进行搜索
                                searchNode = backNode.leftNode;
                            }
                            else
                            {
                                searchNode = backNode.righNode;
                            }

                            searchPath.Add(searchNode);     //添加搜索节点
                        }
                    }
                    else
                    {
                        if (Distance(backNode.point, target) < dist)
                        {
                            nearest = backNode;
                            dist    = Distance(backNode.point, target);
                        }

                        //确定是否进入子空间搜索
                        float disTmp = Math.Abs(target.positionZ - backNode.point.positionZ);
                        if (disTmp < dist)
                        {
                            //判断目标点是在左子空间还是右子空间
                            if (target.positionZ > backNode.point.positionZ)
                            {
                                //目标点在右子空间,则需要进入左子空间进行搜索
                                searchNode = backNode.leftNode;
                            }
                            else
                            {
                                searchNode = backNode.righNode;
                            }

                            searchPath.Add(searchNode);     //添加搜索节点
                        }
                    }
                }

                searchPath.Remove(backNode);
            }

            #endregion

            return(nearest);     //返回最近邻节点
        }