/** * 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; } } }
/** * 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; } } }
/** * 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); } }
/** * 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); } }
/** * 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; } }
/** * 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; } }