public KdTree requestTree( int N ) { if( unusedTrees.isEmpty() ) return new KdTree(N); KdTree tree = unusedTrees.remove( unusedTrees.size()-1); tree.N = N; return tree; }
public KdTree requestTree( int N ) { if( unusedTrees.isEmpty() ) return new KdTree(N); KdTree tree = unusedTrees.remove( unusedTrees.size()-1); tree.N = N; return tree; }
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; }
@Test public void recycleGraph_nullRoot() { KdTreeMemory alg = new KdTreeMemory(); KdTree tree = new KdTree(); tree.root = null; alg.recycleGraph(tree); assertEquals(0,alg.open.size()); assertEquals(1,alg.unusedTrees.size()); assertEquals(0,alg.unusedNodes.size()); }
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; }
/** * The tree is empty and it should always fail */ @Test public void findClosest_empty() { KdTreeSearch1<double[]> alg = createAlg(); alg.setTree( new KdTree(2) ); KdTree.Node found = alg.findNeighbor(new double[]{11, 8}); assertNull(found); }
@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()); }
/** * The tree is empty and it should always fail */ @Test public void findClosest_empty() { KdTreeSearchN<double[]> alg = createAlg(); alg.setTree( new KdTree(2) ); found.reset(); alg.findNeighbor(new double[]{11, 8}, 2, found); assertEquals(0, found.size()); }
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; }
/** * 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); }
/** * 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); }
/** * 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); }
/** * 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); }
/** * 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); }
/** * 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); }