public WrapExhaustiveNeighbor(KdTreeDistance<P> distance ) { alg = new ExhaustiveNeighbor(distance); }
@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 void setPoints(List<P> points, boolean trackIndicies) { alg.setPoints(points); this.points = points; }
@Test public void findClosestN_zero() { List<double[]> list = new ArrayList<double[]>(); ExhaustiveNeighbor<double[]> alg = new ExhaustiveNeighbor<>(distance); alg.setPoints(list); GrowQueue_I32 outputIndex = new GrowQueue_I32(); GrowQueue_F64 outputDistance = new GrowQueue_F64(); alg.findClosestN(new double[]{1, 2}, 10, 5, outputIndex, outputDistance); assertEquals(0,outputIndex.size); assertEquals(0,outputDistance.size); }
@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); }
@Override public void findNearest(P point, double maxDistance, int numNeighbors, FastQueue<NnData<P>> results) { results.reset(); if( maxDistance < 0 ) maxDistance = Double.MAX_VALUE; outputIndex.reset(); outputDistance.reset(); alg.findClosestN(point,maxDistance,numNeighbors,outputIndex,outputDistance); for( int i = 0; i < outputIndex.size; i++ ) { int index = outputIndex.get(i); NnData<P> r = results.grow(); r.distance = outputDistance.get(i); r.point = points.get(index); r.index = index; } } }
@Test public void findClosestN_standard() { List<double[]> list = TestKdTreeConstructor.createPoints(2, 1,2, 3,4 , 4,5, 6,7 , 8,9 ); ExhaustiveNeighbor<double[]> alg = new ExhaustiveNeighbor<>(distance); alg.setPoints(list); GrowQueue_I32 outputIndex = new GrowQueue_I32(); GrowQueue_F64 outputDistance = new GrowQueue_F64(); alg.findClosestN(new double[]{4.1, 4.9}, 10, 3, outputIndex, outputDistance); assertEquals(3,outputIndex.size); assertEquals(3,outputDistance.size); checkContains(1,outputIndex); checkContains(2,outputIndex); checkContains(3,outputIndex); }
@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)); }
@Override public void findNearest(P point, double maxDistance, int numNeighbors, FastQueue<NnData<P>> results) { results.reset(); if( maxDistance < 0 ) maxDistance = Double.MAX_VALUE; outputIndex.reset(); outputDistance.reset(); alg.findClosestN(point,maxDistance,numNeighbors,outputIndex,outputDistance); for( int i = 0; i < outputIndex.size; i++ ) { int index = outputIndex.get(i); NnData<P> r = results.grow(); r.distance = outputDistance.get(i); r.point = points.get(index); r.index = index; } } }
/** * Make sure it works after multiple calls */ @Test public void findClosestN_multiple_calls() { List<double[]> list = TestKdTreeConstructor.createPoints(2, 1,2, 3,4 , 4,5, 6,7 , 8,9 ); ExhaustiveNeighbor<double[]> alg = new ExhaustiveNeighbor<>(distance); alg.setPoints(list); GrowQueue_I32 outputIndex = new GrowQueue_I32(); GrowQueue_F64 outputDistance = new GrowQueue_F64(); alg.findClosestN(new double[]{4.1, 4.9}, 10, 3, outputIndex, outputDistance); outputIndex.reset(); outputDistance.reset(); alg.findClosestN(new double[]{4.1, 4.9}, 10, 3, outputIndex, outputDistance); assertEquals(3,outputIndex.size); assertEquals(3,outputDistance.size); checkContains(1,outputIndex); checkContains(2,outputIndex); checkContains(3,outputIndex); }
@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)); }
@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 void setPoints(List<P> points, boolean trackIndicies) { alg.setPoints(points); this.points = points; }
public WrapExhaustiveNeighbor(KdTreeDistance<P> distance ) { alg = new ExhaustiveNeighbor(distance); }
/** * Request more inliers than there are */ @Test public void findClosestN_toomany() { List<double[]> list = TestKdTreeConstructor.createPoints(2, 1,2, 3,4); ExhaustiveNeighbor<double[]> alg = new ExhaustiveNeighbor<>(distance); alg.setPoints(list); GrowQueue_I32 outputIndex = new GrowQueue_I32(); GrowQueue_F64 outputDistance = new GrowQueue_F64(); alg.findClosestN(new double[]{1, 2}, 10, 5, outputIndex, outputDistance); assertEquals(2,outputIndex.size); assertEquals(2,outputDistance.size); assertEquals(0,outputIndex.get(0)); assertEquals(1,outputIndex.get(1)); }
@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)); }
/** * Request more inliers than there are within the allowed distance */ @Test public void findClosestN_toomany_distance() { List<double[]> list = TestKdTreeConstructor.createPoints(2, 1,2, 3,4); ExhaustiveNeighbor<double[]> alg = new ExhaustiveNeighbor<>(distance); alg.setPoints(list); GrowQueue_I32 outputIndex = new GrowQueue_I32(); GrowQueue_F64 outputDistance = new GrowQueue_F64(); alg.findClosestN(new double[]{1, 2}, 0.1, 5, outputIndex, outputDistance); assertEquals(1,outputIndex.size); assertEquals(1,outputDistance.size); assertEquals(0,outputIndex.get(0)); assertEquals(0,outputDistance.get(0),1e-8); }
@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); } }
ExhaustiveNeighbor<double[]> exhaustive = new ExhaustiveNeighbor<>(distance); exhaustive.setPoints(points); exhaustive.findClosestN(where, 10.0, numNeighbors, outputIndex,outputDistance);