/// <summary>
        /// Searches for the closest points in a hyper-sphere around the given center.
        /// </summary>
        /// <param name="center">The center of the hyper-sphere</param>
        /// <param name="radius">The radius of the hyper-sphere</param>
        /// <param name="neighboors">The number of neighbors to return.</param>
        /// <returns>The specified number of closest points in the hyper-sphere</returns>
        public Tuple <TDimension[], TNode>[] RadialSearch(TDimension[] center, float radius, int neighboors = -1)
        {
            var nearestNeighbors = new BoundedPriorityList <int, float>(this.Count);

            if (neighboors == -1)
            {
                this.SearchForNearestNeighbors(
                    0,
                    center,
                    HyperRect <TDimension> .Infinite(this.Dimensions, this.MaxValue, this.MinValue),
                    0,
                    nearestNeighbors,
                    radius);
            }
            else
            {
                this.SearchForNearestNeighbors(
                    0,
                    center,
                    HyperRect <TDimension> .Infinite(this.Dimensions, this.MaxValue, this.MinValue),
                    0,
                    nearestNeighbors,
                    radius);
            }

            return(nearestNeighbors.ToResultSet(this));
        }
        /// <summary>
        /// Finds the nearest neighbors in the <see cref="KDTreeEricReginaGeneric{TDimension,TNode}"/> of the given <paramref name="point"/>.
        /// </summary>
        /// <param name="point">The point whose neighbors we search for.</param>
        /// <param name="neighbors">The number of neighbors to look for.</param>
        /// <returns>The</returns>
        public Tuple <TDimension[], TNode>[] NearestNeighbors(TDimension[] point, int neighbors)
        {
            var nearestNeighborList = new BoundedPriorityList <int, float>(neighbors, true);
            var rect = HyperRect <TDimension> .Infinite(this.Dimensions, this.MaxValue, this.MinValue);

            this.SearchForNearestNeighbors(0, point, rect, 0, nearestNeighborList, float.MaxValue);

            return(nearestNeighborList.ToResultSet(this));
        }
Пример #3
0
        public static Tuple <TPoint[], TNode>[] LinearRadialSearch <TPoint, TNode>(TPoint[][] points, TNode[] nodes, TPoint[] target, Func <TPoint[], TPoint[], float> metric, float radius)
        {
            var pointsInRadius = new BoundedPriorityList <int, double>(points.Length, true);

            for (int i = 0; i < points.Length; i++)
            {
                var currentDist = metric(target, points[i]);
                if (radius >= currentDist)
                {
                    pointsInRadius.Add(i, currentDist);
                }
            }

            return(pointsInRadius.Select(idx => new Tuple <TPoint[], TNode>(points[idx], nodes[idx])).ToArray());
        }
Пример #4
0
        public static T[][] LinearRadialSearch <T>(T[][] data, T[] point, Func <T[], T[], float> metric, float radius)
        {
            var pointsInRadius = new BoundedPriorityList <T[], double>(data.Length, true);

            for (int i = 0; i < data.Length; i++)
            {
                var currentDist = metric(point, data[i]);
                if (radius >= currentDist)
                {
                    pointsInRadius.Add(data[i], currentDist);
                }
            }

            return(pointsInRadius.ToArray());
        }
        public static Tuple <TDimension[], TNode>[] ToResultSet <TPriority, TDimension, TNode>(
            this BoundedPriorityList <int, TPriority> list,
            KDTreeEricReginaGeneric <TDimension, TNode> tree)
            where TDimension : IComparable <TDimension>
            where TPriority : IComparable <TPriority>
        {
            var array = new Tuple <TDimension[], TNode> [list.Count];

            for (var i = 0; i < list.Count; i++)
            {
                array[i] = new Tuple <TDimension[], TNode>(
                    tree.InternalPointArray[list[i]],
                    tree.InternalNodeArray[list[i]]);
            }

            return(array);
        }
        /// <summary>
        /// A top-down recursive method to find the nearest neighbors of a given point.
        /// </summary>
        /// <param name="nodeIndex">The index of the node for the current recursion branch.</param>
        /// <param name="target">The point whose neighbors we are trying to find.</param>
        /// <param name="rect">The <see cref="HyperRect{T}"/> containing the possible nearest neighbors.</param>
        /// <param name="dimension">The current splitting dimension for this recursion branch.</param>
        /// <param name="nearestNeighbors">The <see cref="BoundedPriorityList{TElement,TPriority}"/> containing the nearest neighbors already discovered.</param>
        /// <param name="maxSearchRadiusSquared">The squared radius of the current largest distance to search from the <paramref name="target"/></param>
        private void SearchForNearestNeighbors(
            int nodeIndex,
            TDimension[] target,
            HyperRect <TDimension> rect,
            int dimension,
            BoundedPriorityList <int, float> nearestNeighbors,
            float maxSearchRadiusSquared)
        {
            if (this.InternalPointArray.Length <= nodeIndex || nodeIndex < 0 ||
                this.InternalPointArray[nodeIndex] == null)
            {
                return;
            }

            // Work out the current dimension
            var dim = dimension % this.Dimensions;

            // Split our hyper-rectangle into 2 sub rectangles along the current
            // node's point on the current dimension
            var leftRect = rect.Clone();

            leftRect.MaxPoint[dim] = this.InternalPointArray[nodeIndex][dim];

            var rightRect = rect.Clone();

            rightRect.MinPoint[dim] = this.InternalPointArray[nodeIndex][dim];

            // Determine which side the target resides in
            var compare = target[dim].CompareTo(this.InternalPointArray[nodeIndex][dim]);

            var nearerRect  = compare <= 0 ? leftRect : rightRect;
            var furtherRect = compare <= 0 ? rightRect : leftRect;

            var nearerNode  = compare <= 0 ? LeftChildIndex(nodeIndex) : RightChildIndex(nodeIndex);
            var furtherNode = compare <= 0 ? RightChildIndex(nodeIndex) : LeftChildIndex(nodeIndex);

            // Move down into the nearer branch
            this.SearchForNearestNeighbors(
                nearerNode,
                target,
                nearerRect,
                dimension + 1,
                nearestNeighbors,
                maxSearchRadiusSquared);

            // Walk down into the further branch but only if our capacity hasn't been reached
            // OR if there's a region in the further rectangle that's closer to the target than our
            // current furtherest nearest neighbor
            var closestPointInFurtherRect = furtherRect.GetClosestPoint(target);
            var distanceSquaredToTarget   = this.Metric(closestPointInFurtherRect, target);

            if (distanceSquaredToTarget.CompareTo(maxSearchRadiusSquared) <= 0)
            {
                if (nearestNeighbors.IsFull)
                {
                    if (distanceSquaredToTarget.CompareTo(nearestNeighbors.MaxPriority) < 0)
                    {
                        this.SearchForNearestNeighbors(
                            furtherNode,
                            target,
                            furtherRect,
                            dimension + 1,
                            nearestNeighbors,
                            maxSearchRadiusSquared);
                    }
                }
                else
                {
                    this.SearchForNearestNeighbors(
                        furtherNode,
                        target,
                        furtherRect,
                        dimension + 1,
                        nearestNeighbors,
                        maxSearchRadiusSquared);
                }
            }

            // Try to add the current node to our nearest neighbors list
            distanceSquaredToTarget = this.Metric(this.InternalPointArray[nodeIndex], target);
            if (distanceSquaredToTarget.CompareTo(maxSearchRadiusSquared) <= 0)
            {
                nearestNeighbors.Add(nodeIndex, distanceSquaredToTarget);
            }
        }