/** * 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; } } }
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); }
/** * Select the maximum variance as the split */ private void computeAxisVariance(List<P> points) { int numPoints = points.size(); for( int i = 0; i < N; i++ ) { mean[i] = 0; var[i] = 0; } // compute the mean for( int i = 0; i < numPoints; i++ ) { P p = points.get(i); for( int j = 0; j < N; j++ ) { mean[j] += distance.valueAt(p,j); } } for( int i = 0; i < N; i++ ) { mean[i] /= numPoints; } // compute the variance * N for( int i = 0; i < numPoints; i++ ) { P p = points.get(i); for( int j = 0; j < N; j++ ) { double d = mean[j] - distance.valueAt(p,j); var[j] += d*d; } } }
double distSq = distance.distance((P)node.point,target); if( distSq <= bestDistanceSq ) { if( closest == null || distSq < bestDistanceSq ) { double distSq = distance.distance((P)node.point,target); if( distSq <= bestDistanceSq ) { if( closest == null || distSq < bestDistanceSq ) { double splitValue = distance.valueAt((P)node.point,node.split); double targetAtSplit = distance.valueAt(target,node.split); if( targetAtSplit <= splitValue ) { nearer = node.left;
double distSq = distance.distance((P)node.point,target); if( distSq <= bestDistanceSq ) { if( closest == null || distSq < bestDistanceSq ) { double distSq = distance.distance((P)node.point,target); if( distSq <= bestDistanceSq ) { if( closest == null || distSq < bestDistanceSq ) { double splitValue = distance.valueAt((P)node.point,node.split); double targetAtSplit = distance.valueAt(target,node.split); if( targetAtSplit <= splitValue ) { nearer = node.left;
/** * 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; } } }
/** * Select the maximum variance as the split */ private void computeAxisVariance(List<P> points) { int numPoints = points.size(); for( int i = 0; i < N; i++ ) { mean[i] = 0; var[i] = 0; } // compute the mean for( int i = 0; i < numPoints; i++ ) { P p = points.get(i); for( int j = 0; j < N; j++ ) { mean[j] += distance.valueAt(p,j); } } for( int i = 0; i < N; i++ ) { mean[i] /= numPoints; } // compute the variance * N for( int i = 0; i < numPoints; i++ ) { P p = points.get(i); for( int j = 0; j < N; j++ ) { double d = mean[j] - distance.valueAt(p,j); var[j] += d*d; } } }
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); }
/** * Finds the index of the point which has the smallest Euclidean distance to 'p' and is {@code <} maxDistance * away. * * @param p A point. * @param maxDistance The maximum distance (Euclidean squared) the neighbor can be. * @return Index of the closest point. */ public int findClosest( P p , double maxDistance ) { int best = -1; bestDistance = maxDistance; for( int i = 0; i < points.size(); i++ ) { P c = points.get(i); double distanceC = distance.distance(p,c); if( distanceC <= bestDistance ) { bestDistance = distanceC; best = i; } } return best; }
/** * Uses quick-select to find the median value */ private void quickSelect(List<P> points, int splitAxis, int medianNum) { int numPoints = points.size(); if( tmp.length < numPoints ) { tmp = new double[numPoints]; indexes = new int[ numPoints ]; } for( int i = 0; i < numPoints; i++ ) { tmp[i] = distance.valueAt(points.get(i),splitAxis); } QuickSelect.selectIndex(tmp, medianNum, numPoints, indexes); } }
/** * Finds the index of the point which has the smallest Euclidean distance to 'p' and is {@code <} maxDistance * away. * * @param p A point. * @param maxDistance The maximum distance (Euclidean squared) the neighbor can be. * @return Index of the closest point. */ public int findClosest( P p , double maxDistance ) { int best = -1; bestDistance = maxDistance; for( int i = 0; i < points.size(); i++ ) { P c = points.get(i); double distanceC = distance.distance(p,c); if( distanceC <= bestDistance ) { bestDistance = distanceC; best = i; } } return best; }
/** * Uses quick-select to find the median value */ private void quickSelect(List<P> points, int splitAxis, int medianNum) { int numPoints = points.size(); if( tmp.length < numPoints ) { tmp = new double[numPoints]; indexes = new int[ numPoints ]; } for( int i = 0; i < numPoints; i++ ) { tmp[i] = distance.valueAt(points.get(i),splitAxis); } QuickSelect.selectIndex(tmp, medianNum, numPoints, indexes); } }
@Override protected void checkBestDistance(KdTree.Node node, double[] target) { double distanceSq = distance.distance((double[])node.point,target); if( distanceSq <= bestDistanceSq ) { bestDistanceSq = distanceSq; bestNode = node; } }
/** * Traverse a node down to a leaf. Unexplored branches are added to the priority queue. */ protected void searchNode(P target, KdTree.Node n) { while( n != null) { checkBestDistance(n, target); if( n.isLeaf() ) break; // select the most promising branch to investigate first KdTree.Node nearer,further; double splitValue = distance.valueAt( (P)n.point , n.split ); if( distance.valueAt( target , n.split ) <= splitValue ) { nearer = n.left; further = n.right; } else { nearer = n.right; further = n.left; } // See if it is possible for 'further' to contain a better node double dx = splitValue - distance.valueAt(target, n.split ); if( further != null && canImprove(dx*dx) ) { addToQueue(dx*dx, further, target ); } n = nearer; } }
P c = points.get(i); double distanceC = distance.distance(p,c);
/** * Traverse a node down to a leaf. Unexplored branches are added to the priority queue. */ protected void searchNode(P target, KdTree.Node n) { while( n != null) { checkBestDistance(n, target); if( n.isLeaf() ) break; // select the most promising branch to investigate first KdTree.Node nearer,further; double splitValue = distance.valueAt( (P)n.point , n.split ); if( distance.valueAt( target , n.split ) <= splitValue ) { nearer = n.left; further = n.right; } else { nearer = n.right; further = n.left; } // See if it is possible for 'further' to contain a better node double dx = splitValue - distance.valueAt(target, n.split ); if( further != null && canImprove(dx*dx) ) { addToQueue(dx*dx, further, target ); } n = nearer; } }
double distSq = distance.distance((P)node.point,target);
P c = points.get(i); double distanceC = distance.distance(p,c);