/**
 * Utility class to find the nearest point given an ArrayList GsSpotPairs
 *
 * 
 * @author Nico Stuurman
 * @copyright UCSF, Dec. 2012
 */
package edu.valelab.GaussianFit;
import ags.utils.KdTree;
import ags.utils.KdTree.Entry;
import ags.utils.KdTree.SqrEuclid;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;



/**
 * Class that finds the closest by point in a point collection given a single point
 * 
 * The method findKDWSE uses a kd tree approach based written by Rednaxela
 * 
 * 
 * @author nico
 */
public class NearestPointGsSpotPair {
   private ArrayList<GsSpotPair> theList_;
   private final double maxDistance_;
   private final double maxDistanceSquared_;
   private KdTree<Integer> we_;
   
   public NearestPointGsSpotPair(ArrayList<GsSpotPair> unsorted, double maxDistance) {
      theList_ = unsorted;
      maxDistance_ = maxDistance;
      maxDistanceSquared_ = maxDistance * maxDistance;
   }
   
   /**
    * method to find the nearest point in the collection of Points
    * Uses Squared Euclidian distance method from Rednaxela
    * 
    * @param input - point for which we want to find the nearest neighbor
    * @return point found or null when it was farther away than the cutoff set 
    * in the constructor
    */
   public GsSpotPair findKDWSE(Point2D.Double input) {
      // construct a new KD tree if needed
      if (we_ == null) {
         we_ = new SqrEuclid<Integer>(2, 50 * theList_.size());
         for (int i = 0; i < theList_.size(); i++) {
            Point2D.Double p = theList_.get(i).getfp();
            double[] point = {p.x, p.y};
            we_.addPoint(point, i);
         }
      }
      double[] testPoint = {input.x, input.y};
      List<Entry<Integer>> result = we_.nearestNeighbor(testPoint, 1, false);
      
      Integer index = result.get(0).value;
      double distance = result.get(0).distance;
      
      GsSpotPair ret =  (GsSpotPair) theList_.get(index).copy();
      
      if (distance < maxDistanceSquared_)
         return ret;
      
      return null;
   }
   
   /**
    * Brute force method to find the nearest point in the collection of Points
    * 
    * @param input - point for which we want to find the nearest neighbor
    * @return point found or null when it was farther away than the cutoff set 
    * in the constructor
    */
   public GsSpotPair findBF(Point2D.Double input) {
      GsSpotPair closestPoint = theList_.get(0);
      double minDist2 = Double.MAX_VALUE;
      Iterator it = theList_.iterator();
      while (it.hasNext()) {
         GsSpotPair p = (GsSpotPair) it.next();
         double dist2 = NearestPoint2D.distance2(input, p.getfp());
         if (dist2 < minDist2) {
            minDist2 = dist2;
            closestPoint = p;
         }
      }
      
      double dist = Math.sqrt(NearestPoint2D.distance2(input, 
              closestPoint.getfp()));
      if (dist < maxDistance_)
         return closestPoint;
      return null;
   }
   

   

       
}


@@ -1826,24 +1826,76 @@
 se);%0A      %0A
+      if (result != null && !result.isEmpty()) %7B%0A   
       Intege
@@ -1925,16 +1925,19 @@
 .value;%0A
+   
       do
@@ -1976,26 +1976,23 @@
 stance;%0A
+%0A
       
-%0A   
    GsSpo
@@ -2002,17 +2002,16 @@
 ir ret =
- 
  (GsSpot
@@ -2044,26 +2044,23 @@
 copy();%0A
+%0A
       
-%0A   
    if (d
@@ -2089,16 +2089,18 @@
 quared_)
+ %7B
 %0A       
@@ -2097,24 +2097,27 @@
  %7B%0A         
+   
 return ret;%0A
@@ -2116,16 +2116,35 @@
 rn ret;%0A
+         %7D%0A      %7D%0A
       %0A 

Fix potential Thread crash