private void KnnNodeSearch( MNode <int> node, T queryObject, BoundablePriorityList <MNodeEntry <int>, double> nearestNeighboorList, BoundablePriorityList <MNode <int>, double> priorityQueue) { foreach (var entry in node.Entries) { var maxPriority = nearestNeighboorList.MaxPriority; if (node.IsInternalNode) { if ((node.ParentEntry == null) || (Math.Abs( this.Metric(this.internalArray[node.ParentEntry.Value], queryObject) - entry.DistanceFromParent) <= entry.CoveringRadius + maxPriority)) { var distanceFromQueryObject = this.Metric(this.internalArray[entry.Value], queryObject); var dMin = Math.Max(distanceFromQueryObject - entry.CoveringRadius, 0); if (dMin < maxPriority) { priorityQueue.Add(entry.ChildNode, dMin); var dMax = distanceFromQueryObject + entry.CoveringRadius; if (dMax < maxPriority) { nearestNeighboorList.Add(null, dMax); maxPriority = Math.Max(dMax, maxPriority); priorityQueue.RemoveAllPriorities(p => p > maxPriority); } } } } else if (Math.Abs(this.Metric(this.internalArray[node.ParentEntry.Value], queryObject) - entry.DistanceFromParent) < maxPriority) { var distanceFromQueryObject = this.Metric(this.internalArray[entry.Value], queryObject); if (distanceFromQueryObject < maxPriority) { nearestNeighboorList.Add(entry, distanceFromQueryObject); nearestNeighboorList.RemoveAllElements(e => e == null); maxPriority = Math.Max(distanceFromQueryObject, maxPriority); priorityQueue.RemoveAllPriorities(p => p > maxPriority); } } } }
/// <summary> /// A top-down recursive method to find the nearest neighbors of a given point. /// </summary> /// <param name="node">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="maxSearchRadius">The radius of the current largest distance to search from the <paramref name="target"/></param> private void SearchForNearestNeighbors( KDNode node, double[] target, HyperRect <double> rect, int dimension, BoundablePriorityList <int, double> nearestNeighbors, double maxSearchRadius) { if (node == null) { return; } // Work out the current dimension var dim = dimension % this.Dimensions; // Get the coordinate of the current node. var coordinate = this.PointSelector(this.InternalList[node.ElementIndex]); // 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] = coordinate[dim]; var rightRect = rect.Clone(); rightRect.MinPoint[dim] = coordinate[dim]; // Determine which side the target resides in var comparison = target[dim] <= coordinate[dim]; var nearerRect = comparison ? leftRect : rightRect; var furtherRect = comparison ? rightRect : leftRect; var nearerNode = comparison ? node.Left : node.Right; var furtherNode = comparison ? node.Right : node.Left; // Move down into the nearer branch this.SearchForNearestNeighbors( nearerNode, target, nearerRect, dimension + 1, nearestNeighbors, maxSearchRadius); // 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 distanceToTarget = this.Metric(closestPointInFurtherRect, target); if (distanceToTarget.CompareTo(maxSearchRadius) <= 0) { if (nearestNeighbors.IsFull) { if (distanceToTarget.CompareTo(nearestNeighbors.MaxPriority) < 0) { this.SearchForNearestNeighbors( furtherNode, target, furtherRect, dimension + 1, nearestNeighbors, maxSearchRadius); } } else { this.SearchForNearestNeighbors( furtherNode, target, furtherRect, dimension + 1, nearestNeighbors, maxSearchRadius); } } // Try to add the current node to our nearest neighbors list distanceToTarget = this.Metric(coordinate, target); if (distanceToTarget.CompareTo(maxSearchRadius) <= 0) { nearestNeighbors.Add(node.ElementIndex, distanceToTarget); } }