private static TItem[] NearestNeighbour(BoundablePair <TItem> initBndPair, double maxDistance) { double distanceLowerBound = maxDistance; BoundablePair <TItem> minPair = null; // initialize internal structures var priQ = new PriorityQueue <BoundablePair <TItem> >(); // initialize queue priQ.Add(initBndPair); while (!priQ.IsEmpty() && distanceLowerBound > 0.0) { // pop head of queue and expand one side of pair var bndPair = priQ.Poll(); double currentDistance = bndPair.Distance; //bndPair.GetDistance(); /** * If the distance for the first node in the queue * is >= the current minimum distance, all other nodes * in the queue must also have a greater distance. * So the current minDistance must be the true minimum, * and we are done. */ if (currentDistance >= distanceLowerBound) { break; } /** * If the pair members are leaves * then their distance is the exact lower bound. * Update the distanceLowerBound to reflect this * (which must be smaller, due to the test * immediately prior to this). */ if (bndPair.IsLeaves) { // assert: currentDistance < minimumDistanceFound distanceLowerBound = currentDistance; minPair = bndPair; } else { // testing - does allowing a tolerance improve speed? // Ans: by only about 10% - not enough to matter /* * double maxDist = bndPair.getMaximumDistance(); * if (maxDist * .99 < lastComputedDistance) * return; * //*/ /** * Otherwise, expand one side of the pair, * (the choice of which side to expand is heuristically determined) * and insert the new expanded pairs into the queue */ bndPair.ExpandToQueue(priQ, distanceLowerBound); } } if (minPair != null) { // done - return items with min distance return new[] { ((ItemBoundable <Envelope, TItem>)minPair.GetBoundable(0)).Item, ((ItemBoundable <Envelope, TItem>)minPair.GetBoundable(1)).Item } } ; return(null); }
private static TItem[] NearestNeighbour(BoundablePair <TItem> initBndPair) { double distanceLowerBound = double.PositiveInfinity; BoundablePair <TItem> minPair = null; // initialize search queue var priQ = new PriorityQueue <BoundablePair <TItem> >(); priQ.Add(initBndPair); while (!priQ.IsEmpty() && distanceLowerBound > 0.0) { // pop head of queue and expand one side of pair var bndPair = priQ.Poll(); double pairDistance = bndPair.Distance; /** * If the distance for the first node in the queue * is >= the current minimum distance, all other nodes * in the queue must also have a greater distance. * So the current minDistance must be the true minimum, * and we are done. */ if (pairDistance >= distanceLowerBound) { break; } /** * If the pair members are leaves * then their distance is the exact lower bound. * Update the distanceLowerBound to reflect this * (which must be smaller, due to the test * immediately prior to this). */ if (bndPair.IsLeaves) { // assert: currentDistance < minimumDistanceFound distanceLowerBound = pairDistance; minPair = bndPair; } else { /** * Otherwise, expand one side of the pair, * (the choice of which side to expand is heuristically determined) * and insert the new expanded pairs into the queue */ bndPair.ExpandToQueue(priQ, distanceLowerBound); } } if (minPair != null) { // done - return items with min distance return new[] { ((ItemBoundable <Envelope, TItem>)minPair.GetBoundable(0)).Item, ((ItemBoundable <Envelope, TItem>)minPair.GetBoundable(1)).Item } } ; return(null); }