private void checkSubtree(KDNode<T> node, Coordinate query, NNState state) { if (node == null) return; final float dist = distance(query, node._point); if (dist < state.bestDist) { state.best = node._point; state.bestDist = dist; } if (state.bestDist == 0) return; final float d = node._point.getOrdinate(node._discriminate).floatValue() - query.getOrdinate(node._discriminate).floatValue(); if (d * d > state.bestDist) { // check subtree final double ordinate1 = query.getOrdinate(node._discriminate).doubleValue(); final double ordinate2 = node._point.getOrdinate(node._discriminate).doubleValue(); if (ordinate1 > ordinate2) checkSubtree(node._right, query, state); else checkSubtree(node._left, query, state); } else { checkSubtree(node._left, query, state); checkSubtree(node._right, query, state); } }
private void checkSubtree(KDNode<T> node, Coordinate query, NNState state) { if (node == null) return; final float dist = distance(query, node._point); if (dist < state.bestDist) { state.best = node._point; state.bestDist = dist; } if (state.bestDist == 0) return; final float d = node._point.getOrdinate(node._discriminate).floatValue() - query.getOrdinate(node._discriminate).floatValue(); if (d * d > state.bestDist) { // check subtree final double ordinate1 = query.getOrdinate(node._discriminate).doubleValue(); final double ordinate2 = node._point.getOrdinate(node._discriminate).doubleValue(); if (ordinate1 > ordinate2) checkSubtree(node._right, query, state); else checkSubtree(node._left, query, state); } else { checkSubtree(node._left, query, state); checkSubtree(node._right, query, state); } }
/** * Find the nearest neighbour. Only one neighbour will be returned - if * multiple neighbours share the same location, or are equidistant, then * this might not be the one you expect. * * @param query * query coordinate * @return nearest neighbour */ @Override public T nearestNeighbour(Coordinate query) { final Stack<KDNode<T>> stack = walkdown(query); final NNState state = new NNState(); state.best = stack.peek()._point; state.bestDist = distance(query, state.best); if (state.bestDist == 0) return state.best; while (!stack.isEmpty()) { final KDNode<T> current = stack.pop(); checkSubtree(current, query, state); } return state.best; }
/** * Find the nearest neighbour. Only one neighbour will be returned - if * multiple neighbours share the same location, or are equidistant, then * this might not be the one you expect. * * @param query * query coordinate * @return nearest neighbour */ @Override public T nearestNeighbour(Coordinate query) { final Stack<KDNode<T>> stack = walkdown(query); final NNState state = new NNState(); state.best = stack.peek()._point; state.bestDist = distance(query, state.best); if (state.bestDist == 0) return state.best; while (!stack.isEmpty()) { final KDNode<T> current = stack.pop(); checkSubtree(current, query, state); } return state.best; }