/** * Creates canonical K-D Tree by selecting the maximum variance axis and splitting the points at the median. * */ public KdTreeConstructor( KdTreeDistance<P> distance ) { this(new KdTreeMemory<>(), new AxisSplitterMedian<>(distance, new AxisSplitRuleMax())); }
@Override public void setPoints(List<P> points, boolean trackIndicies) { if( tree != null ) memory.recycleGraph(tree); tree = constructor.construct(points,trackIndicies); search.setTree(tree); searchN.setTree(tree); }
public AxisSplitterMedian(KdTreeDistance<P> distance, AxisSplitRule splitRule ) { this.distance = distance; this.splitRule = splitRule; this.N = distance.length(); this.mean = new double[N]; this.var = new double[N]; splitRule.setDimension(N); }
/** * 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()); }
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; }
public KdTreeNearestNeighbor(KdTreeSearch1<P> search, KdTreeSearchN<P> searchN, AxisSplitter<P> splitter) { this.search = search; this.searchN = searchN; this.splitter = splitter; constructor = new KdTreeConstructor<>(memory,splitter); }
/** * Request a leaf node be returned. All data parameters will be automatically assigned appropriate * values for a leaf. */ public KdTree.Node requestNode(P point , int index ) { KdTree.Node n = requestNode(); n.point = point; n.index = index; n.split = -1; return n; }
@Override public void setPoints(List<P> points, boolean trackIndicies) { alg.setPoints(points); this.points = points; }
/** * Defaults to selecting the split axis with maximum variance */ public AxisSplitterMedian(KdTreeDistance<P> distance) { this(distance,new AxisSplitRuleMax()); }
/** * Checks to see if the current node's point is the closet point found so far */ @Override protected void checkBestDistance(KdTree.Node node, P target) { double distanceSq = distance.distance((P)node.point,target); if( distanceSq <= bestDistanceSq ) { if( bestNode == null || distanceSq < bestDistanceSq ) { bestDistanceSq = distanceSq; bestNode = node; } } }
/** * {@link VpTree Vantage point} tree implementation for nearest neighbor search. Slower than KD-Tree on * random data, but faster than it for some pathological cases. * * @see VpTree * * @param randSeed Random seed * @return {@link NearestNeighbor} implementation */ public static NearestNeighbor<double[]> vptree( long randSeed ) { return new VpTree(randSeed); } }
@Override public void setPoints(List<P> points, boolean trackIndicies) { if( tree != null ) memory.recycleGraph(tree); tree = constructor.construct(points,trackIndicies); search.setTree(tree); searchN.setTree(tree); }
/** * Creates canonical K-D Tree by selecting the maximum variance axis and splitting the points at the median. * */ public KdTreeConstructor( KdTreeDistance<P> distance ) { this(new KdTreeMemory<>(), new AxisSplitterMedian<>(distance, new AxisSplitRuleMax())); }
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; }
public AxisSplitterMedian(KdTreeDistance<P> distance, AxisSplitRule splitRule ) { this.distance = distance; this.splitRule = splitRule; this.N = distance.length(); this.mean = new double[N]; this.var = new double[N]; splitRule.setDimension(N); }
public KdTreeNearestNeighbor(KdTreeSearch1<P> search, KdTreeSearchN<P> searchN, AxisSplitter<P> splitter) { this.search = search; this.searchN = searchN; this.splitter = splitter; constructor = new KdTreeConstructor<>(memory,splitter); }
/** * Request a leaf node be returned. All data parameters will be automatically assigned appropriate * values for a leaf. */ public KdTree.Node requestNode(P point , int index ) { KdTree.Node n = requestNode(); n.point = point; n.index = index; n.split = -1; return n; }
@Override public void setPoints(List<P> points, boolean trackIndicies) { alg.setPoints(points); this.points = points; }
/** * Defaults to selecting the split axis with maximum variance */ public AxisSplitterMedian(KdTreeDistance<P> distance) { this(distance,new AxisSplitRuleMax()); }
@Override protected void checkBestDistance(KdTree.Node node, double[] target) { double distanceSq = distance.distance((double[])node.point,target); if( distanceSq <= bestDistanceSq ) { bestDistanceSq = distanceSq; bestNode = node; } }