@Test public void splitData_three() { List<double[]> points = createPoints(2, 1,2 , 3,5 , -3,4); AxisSplitterMedian<double[]> alg = new AxisSplitterMedian<>(distance,new DummyRule(0)); alg.splitData(points,null,left,null,right,null); // The first point is selected to be the median assertEquals(1,left.size()); assertEquals(1,right.size()); assertEquals(-3,left.get(0)[0],1e-8); assertEquals(3,right.get(0)[0],1e-8); }
/** * Makes sure a branch is created correctly given the results from the splitter */ @Test public void computeBranch_dontTrack() { DummySplitter splitter = createSplitter(1,1,false); splitter.splitAxis = 1; splitter.splitPoint = new double[]{2,3}; KdTreeConstructor<double[]> alg = new KdTreeConstructor<>(new KdTreeMemory(),splitter); KdTree.Node n = alg.computeBranch(new ArrayList<>(),null); assertSame(n.point, splitter.splitPoint); assertEquals(n.split, splitter.splitAxis); assertEquals(-2, n.index); // default value in splitter assertTrue(n.left.isLeaf()); assertTrue(n.right.isLeaf()); assertSame(n.left.point, splitter.left.get(0)); assertEquals(-1, n.left.index); assertSame(n.right.point, splitter.right.get(0)); assertEquals(-1, n.right.index); }
/** * Basic tests to see if it can handle different sized input lists. */ @Test public void construct() { KdTreeConstructor<double[]> alg = new KdTreeConstructor<>(new KdTreeMemory(),createSplitter(1,1,false)); // test an empty list List<double[]> points = new ArrayList<>(); KdTree tree = alg.construct(points,false); assertEquals(2, tree.N); assertNull(tree.root); // add a point points.add( new double[]{1,2}); tree = alg.construct(points,false); assertEquals(2, tree.N); assertSame(tree.root.point, points.get(0)); assertTrue(tree.root.isLeaf()); // add another point. These input points are ignored by the dummy splitter points.add( new double[]{1,2,4,5}); tree = alg.construct(points,false); assertEquals(2, tree.N); assertTrue(tree.root.left.isLeaf()); assertTrue(tree.root.right.isLeaf()); }
@Test public void splitData_one() { List<double[]> points = createPoints(2, 1,2); AxisSplitterMedian<double[]> alg = new AxisSplitterMedian<>(distance,new DummyRule(0)); alg.splitData(points,null,left,null,right,null); // the median point is not included and will become the node's point assertEquals(0,left.size()); assertEquals(0,right.size()); }
/** * Same test as above but with associated data */ @Test public void computeBranch_trackIndexes() { DummySplitter splitter = createSplitter(1,1,true); splitter.splitAxis = 1; splitter.splitPoint = new double[]{2,3}; splitter.splitIndex = 2; KdTreeConstructor<double[]> alg = new KdTreeConstructor<>(new KdTreeMemory(),splitter); KdTree.Node n = alg.computeBranch(new ArrayList<>(),new GrowQueue_I32()); assertSame(n.point, splitter.splitPoint); assertEquals(n.split, splitter.splitAxis); assertEquals(n.index, splitter.splitIndex); assertTrue(n.left.isLeaf()); assertTrue(n.right.isLeaf()); assertSame(n.left.point, splitter.left.get(0)); assertEquals(n.left.index, splitter.leftIndex.get(0)); assertSame(n.right.point, splitter.right.get(0)); assertEquals(n.right.index, splitter.rightIndex.get(0)); }
@Test public void splitData_two() { List<double[]> points = createPoints(2, 1,2 , 3,5); AxisSplitterMedian<double[]> alg = new AxisSplitterMedian<>(distance,new DummyRule(0)); alg.splitData(points,null,left,null,right,null); // The second point is selected to be the median assertEquals(1,left.size()); assertEquals(0, right.size()); assertEquals(1,left.get(0)[0],1e-8); }
@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)); }
/** * Make two of the points identical and see if things blow up */ @Test public void identical_points() { List<double[]> points = createPoints(2, 1,2, 1.1,4 , 1,2); AxisSplitterMedian<double[]> alg = new AxisSplitterMedian<>(distance,new DummyRule(0)); alg.splitData(points,null,left,null,right,null); // sorted order should be (1,2) (1,2) (1.1,4) assertEquals(1, left.size()); assertEquals(1, right.size()); assertEquals(1,alg.getSplitPoint()[0],1e-8); assertEquals(1,left.get(0)[0],1e-8); assertEquals(1.1,right.get(0)[0],1e-8); }
@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)); }
@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)); }
/** * Make sure the split point is returned */ @Test public void splitData_split_point() { List<double[]> points = createPoints(2, 1,2 , 3,5 , -3,4); AxisSplitterMedian<double[]> alg = new AxisSplitterMedian<>(distance,new DummyRule(1)); alg.splitData(points,null,left,null,right,null); assertEquals(1,alg.getSplitAxis()); assertEquals(-3,alg.getSplitPoint()[0],1e-8); // assertEquals(2, alg.getSplitIndex()); }
@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 splitData_withData() { List<double[]> points = createPoints(2, 1,2 , 3,5 , -3,4); GrowQueue_I32 data = new GrowQueue_I32(); for( int i = 0; i < points.size(); i++ ) data.add(i); AxisSplitterMedian<double[]> alg = new AxisSplitterMedian<>(distance,new DummyRule(1)); alg.splitData(points,data,left,leftData,right,rightData); assertEquals(1,left.size()); assertEquals(1,right.size()); assertEquals(1,leftData.size()); assertEquals(1,rightData.size()); assertEquals(1,alg.getSplitAxis(),1e-8); assertEquals(-3,alg.getSplitPoint()[0],1e-8); assertTrue(data.get(2) == alg.getSplitIndex()); assertTrue(data.get(0) == leftData.get(0)); assertTrue(data.get(1) == rightData.get(0)); }
/** * 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)); }
/** * 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); }
/** * 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); }