@Override public boolean findNearest(P point, double maxDistance, NnData<P> result) { if( maxDistance < 0 ) maxDistance = Double.MAX_VALUE; int index = alg.findClosest(point,maxDistance); if( index >= 0 ) { result.point = points.get(index); result.distance = alg.getBestDistance(); result.index = index; return true; } else { return false; } }
@Override public boolean findNearest(P point, double maxDistance, NnData<P> result) { if( maxDistance < 0 ) maxDistance = Double.MAX_VALUE; int index = alg.findClosest(point,maxDistance); if( index >= 0 ) { result.point = points.get(index); result.distance = alg.getBestDistance(); result.index = index; return true; } else { return false; } }
@Test public void findClosest_zero() { List<double[]> list = new ArrayList<double[]>(); ExhaustiveNeighbor<double[]> alg = new ExhaustiveNeighbor<>(distance); alg.setPoints(list); assertEquals(alg.findClosest(new double[]{1, 2}, 10), -1); }
@Test public void findClosest_one() { List<double[]> list = TestKdTreeConstructor.createPoints(2, 1,2); ExhaustiveNeighbor<double[]> alg = new ExhaustiveNeighbor<>(distance); alg.setPoints(list); assertEquals(0, alg.findClosest(new double[]{1, 2.1}, 10)); assertNotEquals(0, alg.findClosest(new double[]{1, 200}, 10)); }
@Test public void findClosest_three() { List<double[]> list = TestKdTreeConstructor.createPoints(2, 1,2, 3,4, 6,7); ExhaustiveNeighbor<double[]> alg = new ExhaustiveNeighbor<>(distance); alg.setPoints(list); assertEquals(1, alg.findClosest(new double[]{3.1, 3.9}, 10)); }
@Test public void findNearest_compareToNaive() { for( int numPoints = 10; numPoints <= 100; numPoints += 10 ) { // System.out.println("numPoints = "+numPoints); List<double[]> points = new ArrayList<double[]>(); for( int i = 0; i < numPoints; i++ ) points.add(randPoint(2)); alg.setPoints(points,false); double[] where = randPoint(2); assertTrue(alg.findNearest(where, 10, found)); ExhaustiveNeighbor<double[]> exhaustive = new ExhaustiveNeighbor<>(distance); exhaustive.setPoints(points); double[] expected = points.get( exhaustive.findClosest(where,1000) ); assertSame(expected, found.point); } }
@Test public void findClosest_two() { List<double[]> list = TestKdTreeConstructor.createPoints(2, 1,2, 3,4); ExhaustiveNeighbor<double[]> alg = new ExhaustiveNeighbor<>(distance); alg.setPoints(list); assertEquals(0, alg.findClosest(new double[]{1, 2.1}, 10)); }