Exemple #1
0
        private bool approximate(TNode current, double[] position,
                                 KDTreeNodeCollection <TNode> list, int maxLeaves, ref int visited)
        {
            // Compute distance from this node to the point
            double d = distance.Distance(position, current.Position);

            list.Add(current, d);

            if (++visited > maxLeaves)
            {
                return(true);
            }


            // Check for leafs on the opposite sides of
            // the subtrees to nearest possible neighbors.

            // Prepare for recursion. The following null checks
            // will be used to avoid function calls if possible

            double value  = position[current.Axis];
            double median = current.Position[current.Axis];
            double u      = value - median;

            if (u <= 0)
            {
                if (current.Left != null)
                {
                    if (approximate(current.Left, position, list, maxLeaves, ref visited))
                    {
                        return(true);
                    }
                }

                if (current.Right != null && Math.Abs(u) <= list.Maximum)
                {
                    if (approximate(current.Right, position, list, maxLeaves, ref visited))
                    {
                        return(true);
                    }
                }
            }
            else
            {
                if (current.Right != null)
                {
                    approximate(current.Right, position, list, maxLeaves, ref visited);
                }

                if (current.Left != null && Math.Abs(u) <= list.Maximum)
                {
                    if (approximate(current.Left, position, list, maxLeaves, ref visited))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Exemple #2
0
        /// <summary>
        ///   Retrieves the nearest points to a given point within a given radius.
        /// </summary>
        ///
        /// <param name="position">The queried point.</param>
        /// <param name="radius">The search radius.</param>
        /// <param name="maximum">The maximum number of neighbors to retrieve.</param>
        ///
        /// <returns>A list of neighbor points, ordered by distance.</returns>
        ///
        public ICollection <NodeDistance <TNode> > Nearest(double[] position, double radius, int maximum)
        {
            if (maximum == 0)
            {
                var list = new List <NodeDistance <TNode> >();

                if (Root != null)
                {
                    nearest(Root, position, radius, list);
                }

                return(list);
            }
            else
            {
                var list = new KDTreeNodeCollection <TNode>(maximum);

                if (Root != null)
                {
                    nearest(Root, position, radius, list);
                }

                return(list);
            }
        }
Exemple #3
0
        /// <summary>
        ///   Retrieves a fixed point of nearest points to a given point.
        /// </summary>
        ///
        /// <param name="position">The queried point.</param>
        /// <param name="neighbors">The number of neighbors to retrieve.</param>
        ///
        /// <returns>A list of neighbor points, ordered by distance.</returns>
        ///
        public KDTreeNodeCollection <TNode> Nearest(double[] position, int neighbors)
        {
            var list = new KDTreeNodeCollection <TNode>(size: neighbors);

            if (Root != null)
            {
                nearest(Root, position, list);
            }

            return(list);
        }
Exemple #4
0
        /// <summary>
        ///   Retrieves a fixed point of nearest points to a given point.
        /// </summary>
        ///
        /// <param name="position">The queried point.</param>
        /// <param name="neighbors">The number of neighbors to retrieve.</param>
        /// <param name="maxLeaves">The maximum number of leaf nodes that can
        /// be visited before the search finishes with an approximate answer.</param>
        ///
        /// <returns>A list of neighbor points, ordered by distance.</returns>
        ///
        public KDTreeNodeCollection <TNode> ApproximateNearest(double[] position, int neighbors, int maxLeaves)
        {
            var list = new KDTreeNodeCollection <TNode>(size: neighbors);

            if (Root != null)
            {
                int visited = 0;
                approximate(Root, position, list, maxLeaves, ref visited);
            }

            return(list);
        }
Exemple #5
0
        /// <summary>
        ///   k-nearest neighbors search.
        /// </summary>
        ///
        private void nearest(TNode current, double[] position, KDTreeNodeCollection <TNode> list)
        {
            // Compute distance from this node to the point
            double d = distance.Distance(position, current.Position);


            list.Add(current, d);


            // Check for leafs on the opposite sides of
            // the subtrees to nearest possible neighbors.

            // Prepare for recursion. The following null checks
            // will be used to avoid function calls if possible

            double value  = position[current.Axis];
            double median = current.Position[current.Axis];
            double u      = value - median;

            if (u <= 0)
            {
                if (current.Left != null)
                {
                    nearest(current.Left, position, list);
                }

                if (current.Right != null && Math.Abs(u) <= list.Maximum)
                {
                    nearest(current.Right, position, list);
                }
            }
            else
            {
                if (current.Right != null)
                {
                    nearest(current.Right, position, list);
                }

                if (current.Left != null && Math.Abs(u) <= list.Maximum)
                {
                    nearest(current.Left, position, list);
                }
            }
        }