Esempio n. 1
0
        private void nearest(KDTreeNode <T> current, double[] position, KDTreeNodeCollection <T> list)
        {
            // Compute distance from this node to the point
            double d = distance(position, current.Position);

            if (current.IsLeaf)
            {
                // Base: node is leaf
                list.Add(current, d);
            }
            else
            {
                // 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);
                    }

                    list.Add(current, d);

                    if (current.Right != null)
                    {
                        if ((u * u) < list.Distance.Max)
                        {
                            nearest(current.Right, position, list);
                        }
                    }
                }
                else
                {
                    if (current.Right != null)
                    {
                        nearest(current.Right, position, list);
                    }

                    list.Add(current, d);

                    if (current.Left != null)
                    {
                        if ((u * u) < list.Distance.Max)
                        {
                            nearest(current.Left, position, list);
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        private bool approximate(KDTreeNode <T> current, double[] position,
                                 KDTreeNodeCollection <T> list, int maxLeaves, ref int visited)
        {
            // Compute distance from this node to the point
            double d = 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);
        }