/** * Returns a new node. All object references can be assumed to be null. * @return */ public KdTree.Node requestNode() { if( unusedNodes.isEmpty() ) return new KdTree.Node(); return unusedNodes.remove( unusedNodes.size()-1); }
/** * Returns a new node. All object references can be assumed to be null. * @return */ public KdTree.Node requestNode() { if( unusedNodes.isEmpty() ) return new KdTree.Node(); return unusedNodes.remove( unusedNodes.size()-1); }
@Test public void recycleGraph() { KdTreeMemory alg = new KdTreeMemory(); KdTree tree = new KdTree(); tree.root = new KdTree.Node(); tree.root.left = new KdTree.Node(); tree.root.right = new KdTree.Node(); tree.root.left.left = new KdTree.Node(); tree.root.left.right = new KdTree.Node(); alg.recycleGraph(tree); assertEquals(0,alg.open.size()); assertEquals(1,alg.unusedTrees.size()); assertEquals(5,alg.unusedNodes.size()); }
@Test public void computeChild() { KdTreeConstructor<double[]> alg = new KdTreeConstructor<>(distance); List<double[]> points = new ArrayList<>(); GrowQueue_I32 data = new GrowQueue_I32(); // empty lists should be null KdTree.Node n = new KdTree.Node(); n.point = new double[2]; n.index = 1; KdTree.Node found = alg.computeChild(points,data); assertNull(found); // add a point points.add( new double[2] ); data.add(2); found = alg.computeChild(points,data); assertTrue(found.isLeaf()); assertSame(found.point, points.get(0)); assertEquals(found.index, data.get(0)); // for all the other cases it will create a branch. testing that will require a bit more work... }
/** * See if max distance is being respected */ @Test public void findClosest_maxDistance() { KdTree tree = new KdTree(2); tree.root = new KdTree.Node(new double[]{1,2}); KdTreeSearchN<double[]> alg = createAlg(); alg.setTree( tree ); alg.setMaxDistance(2); found.reset(); alg.findNeighbor(new double[]{11, 8}, 1, found); assertEquals(0, found.size); found.reset(); alg.findNeighbor(new double[]{1, 1.5}, 1, found); assertEquals(1, found.size); assertTrue(found.data[0].node == tree.root); }
/** * The tree is a leaf and should always return the same result */ @Test public void findClosest_leaf() { KdTree tree = new KdTree(2); tree.root = new KdTree.Node(new double[]{1,2}); KdTreeSearchN<double[]> alg =createAlg(); alg.setTree( tree ); found.reset(); alg.findNeighbor(new double[]{11, 8}, 2, found); assertEquals(1,found.size); assertTrue(found.data[0].node == tree.root); found.reset(); alg.findNeighbor(new double[]{2, 5}, 2, found); assertEquals(1,found.size); assertTrue(found.data[0].node == tree.root); }
/** * Provide multiple trees for input and see if it finds the best one */ @Test public void multiTreeSearch() { KdTree forest[] = new KdTree[2]; forest[0] = StandardKdTreeSearch1Tests.createTreeA(); forest[1] = new KdTree(2); forest[1].root = new KdTree.Node(new double[]{12,2}); KdTreeSearch1Bbf<double[]> alg = new KdTreeSearch1Bbf<>(new KdTreeEuclideanSq_F64(2),200); alg.setTrees(forest); KdTree.Node found = alg.findNeighbor(new double[]{12, 3}); // make sure it searched some nodes besides the root ones assertTrue(alg.numNodesSearched>0); // the best node should be the root node in the second forest assertSame(found, forest[1].root); }
public static KdTree createTreeA() { KdTree tree = new KdTree(2); tree.root = new KdTree.Node(new double[]{1,2}); tree.root.split = 1; tree.root.left = new KdTree.Node(new double[]{-0.21,1}); tree.root.left.split = 0; tree.root.left.left = new KdTree.Node(new double[]{-2,0.5}); tree.root.left.left.split = -1; tree.root.left.right = new KdTree.Node(new double[]{2,0.8}); tree.root.left.right.split = -1; tree.root.right = new KdTree.Node(new double[]{10,5}); tree.root.right.split = 0; tree.root.right.left = new KdTree.Node(new double[]{10,5}); // duplicate tree.root.right.left.split = -1; tree.root.right.right = new KdTree.Node(new double[]{12,10}); tree.root.right.right.split = -1; return tree; }
/** * Provide multiple trees for input and see if it finds the best one */ @Test public void multiTreeSearch() { KdTree forest[] = new KdTree[2]; forest[0] = StandardKdTreeSearch1Tests.createTreeA(); forest[1] = new KdTree(2); forest[1].root = new KdTree.Node(new double[]{12,2}); BBF alg = new BBF(200,3); alg.setTrees(forest); KdTree.Node found = alg.findClosest(new double[]{12,3}); // make sure it searched some nodes besides the root ones assertTrue(alg.numNodesSearched>0); // the best node should be the root node in the second forest assertTrue(found==forest[1].root); }
/** * The tree is a leaf and should always return the same result */ @Test public void findClosest_leaf() { KdTree tree = new KdTree(2); tree.root = new KdTree.Node(new double[]{1,2}); KdTreeSearch1<double[]> alg =createAlg(); alg.setTree( tree ); KdTree.Node found = alg.findNeighbor(new double[]{11, 8}); assertSame(found, tree.root); found = alg.findNeighbor(new double[]{2, 5}); assertSame(found, tree.root); }
/** * See if max distance is being respected */ @Test public void findClosest_maxDistance() { KdTree tree = new KdTree(2); tree.root = new KdTree.Node(new double[]{1,2}); KdTreeSearch1<double[]> alg = createAlg(); alg.setTree( tree ); alg.setMaxDistance(2); KdTree.Node found = alg.findNeighbor(new double[]{11, 8}); assertNull(found); found = alg.findNeighbor(new double[]{1, 1.5}); assertSame(found, tree.root); }
@Test public void requestNode_leaf() { // create a node with values that need to be changed KdTree.Node n = new KdTree.Node(); n.point = new double[2]; n.split = 123; n.index = 3; KdTreeMemory alg = new KdTreeMemory(); alg.unusedNodes.add(n); KdTree.Node m = alg.requestNode(new double[]{1,2},4); assertTrue(m==n); assertTrue(((double[])m.point)[0]==1); assertEquals(m.index,4); assertTrue(m.split==-1); }
@Test public void isLeaf() { KdTree.Node n = new KdTree.Node(); n.split = -1; assertTrue(n.isLeaf()); n.split = 1; assertFalse(n.isLeaf()); }
public static KdTree createTreeDuplicates() { KdTree tree = new KdTree(2); tree.root = new KdTree.Node(new double[]{1,2}); tree.root.split = 1; tree.root.left = new KdTree.Node(new double[]{1,2}); tree.root.left.split = 0; tree.root.left.left = new KdTree.Node(new double[]{1,2}); tree.root.left.left.split = -1; tree.root.left.right = null; tree.root.right = new KdTree.Node(new double[]{1,2}); tree.root.right.split = -1; return tree; }
@Test public void recycle() { KdTreeMemory alg = new KdTreeMemory(); KdTree.Node n = new KdTree.Node(); n.point = new double[2]; n.left = n; n.right = n; alg.recycle(n); assertTrue(n.point == null); assertTrue(n.left == null); assertTrue(n.right == null); assertEquals(1,alg.unusedNodes.size()); } @Test
public static KdTree createTreeWithNull() { KdTree tree = new KdTree(2); tree.root = new KdTree.Node(new double[]{1,2}); tree.root.split = 1; tree.root.left = new KdTree.Node(new double[]{-0.2,1}); tree.root.left.split = -1; tree.root.right = null; return tree; }