예제 #1
0
        /// <summary>
        /// Nearest neighbor search method to find the single nearest neighbor at a given point.
        /// Translated from https://github.com/gvd/kdtree/blob/master/kdtree.h
        /// </summary>
        /// <param name="query">The nearest neighbor search query point.</param>
        /// <returns>The closest node to the parameter query is returned.</returns>
        //public KDNode NNSearch(Point query)
        //{
        //	MinPriorityQueue<Tuple<double, KDNode>> pq = new MinPriorityQueue<Tuple<double, KDNode>>();

        //	//Tuple<double, KDNode> best = new Tuple<double, KDNode>(1.79769e+308, root);

        //	pq.Enqueue(new Tuple<double, KDNode>(0.0, root));

        //	do
        //	{
        //		var current = pq.Dequeue();
        //              //if (current.Item1 >= best.Item1)
        //              //{
        //              //	if (EuclideanDistance(query, current.Item2.Data) > EuclideanDistance(query, best.Item2.Data))
        //              //		return best.Item2;
        //              //	else
        //              //		return current.Item2;
        //              //}
        //              List<KDNode> a = new List<KDNode>
        //              {

        //              };
        //              var currentNode = current.Item2;
        //		//double d = EuclideanDistance(query, currentNode.Data);
        //		//double dx = Subtract(query, currentNode.Data, currentNode.Depth);
        //		if ()
        //		{
        //			best = new Tuple<double, KDNode>(d, currentNode);
        //		}

        //		KDNode near, far;


        //                  near = currentNode.Left;
        //		else
        //                  near = currentNode.Right;


        //                  far = currentNode.Right;
        //		else
        //                  far = currentNode.Left;

        //		if (near != null)
        //                  pq.Enqueue(new Tuple<double, KDNode>(0, near));
        //		if (far != null)
        //                  pq.Enqueue(new Tuple<double, KDNode>(0, far));

        //	} while (pq.Count() != 0);

        //	return best.Item2;
        //}

        public List <KDNode> LocSearch(Point query)
        {
            MinPriorityQueue <Tuple <double, KDNode> > pq = new MinPriorityQueue <Tuple <double, KDNode> >();

            Point p = new Point(0, 0);

            List <KDNode> a = new List <KDNode>
            {
            };

            pq.Enqueue(new Tuple <double, KDNode>(0.0, root));

            do
            {
                var current = pq.Dequeue();
                // current.Item2.Data;
                var currentNode = current.Item2;
                if (query.x < currentNode.Data.x && currentNode.Data.x < query.x && query.y < currentNode.Data.y)
                {
                    a.Add(currentNode);
                }
                double d  = EuclideanDistance(query, currentNode.Data);
                double dx = Subtract(query, currentNode.Data, currentNode.Depth);

                KDNode near;

                if (dx <= 0)
                {
                    near = currentNode.Left;
                }
                else
                {
                    near = currentNode.Right;
                }

                //if (dx <= 0)
                //    far = currentNode.Right;
                //else
                //    far = currentNode.Left;

                if (near != null)
                {
                    pq.Enqueue(new Tuple <double, KDNode>(0, near));
                }
            } while (pq.Count() != 0);

            return(a);
        }
예제 #2
0
        public List <String> LocSearch(Point query1, Point query2)
        {
            MinPriorityQueue <Tuple <double, KDNode> > pq = new MinPriorityQueue <Tuple <double, KDNode> >();



            List <String> a = new List <String>();

            pq.Enqueue(new Tuple <double, KDNode>(0.0, root));

            do
            {
                var current = pq.Dequeue();

                var currentNode = current.Item2;
                if (query1.x > query2.x && query1.y > query2.y)
                {
                    if (query1.x >= currentNode.Data.x && query2.x <= currentNode.Data.x && query1.y >= currentNode.Data.y && query2.y <= currentNode.Data.y)
                    {
                        a.Add(currentNode.Data.x.ToString());
                        a.Add(currentNode.Data.y.ToString());
                    }
                }
                else if (query1.x < query2.x && query1.y < query2.y)
                {
                    if (query1.x <= currentNode.Data.x && query2.x >= currentNode.Data.x && query1.y <= currentNode.Data.y && query2.y >= currentNode.Data.y)
                    {
                        a.Add(currentNode.Data.x.ToString());
                        a.Add(currentNode.Data.y.ToString());
                    }
                }
                else if (query1.x < query2.x && query1.y > query2.y)
                {
                    if (query1.x <= currentNode.Data.x && query2.x >= currentNode.Data.x && query1.y >= currentNode.Data.y && query2.y <= currentNode.Data.y)
                    {
                        a.Add(currentNode.Data.x.ToString());
                        a.Add(currentNode.Data.y.ToString());
                    }
                }
                else if (query1.x > query2.x && query1.y < query2.y)
                {
                    if (query1.x >= currentNode.Data.x && query2.x <= currentNode.Data.x && query1.y <= currentNode.Data.y && query2.y >= currentNode.Data.y)
                    {
                        a.Add(currentNode.Data.x.ToString());
                        a.Add(currentNode.Data.y.ToString());
                    }
                }



                KDNode near, far;


                near = currentNode.Right;


                far = currentNode.Left;

                if (near != null)
                {
                    pq.Enqueue(new Tuple <double, KDNode>(0, near));
                }
                if (far != null)
                {
                    pq.Enqueue(new Tuple <double, KDNode>(0, far));
                }
            } while (pq.Count() != 0);

            return(a);
        }