@Override public void kNearestNeighbour(Collection<T> result, Coordinate query, int k) { final Stack<KDNode<T>> stack = walkdown(query); final PriorityQueue<NNState> state = new PriorityQueue<NNState>(k); final NNState initialState = new NNState(); initialState.best = stack.peek()._point; initialState.bestDist = distance(query, initialState.best); state.add(initialState); while (!stack.isEmpty()) { final KDNode<T> current = stack.pop(); checkSubtreeK(current, query, state, k); } @SuppressWarnings("unchecked") final NNState[] stateList = state.toArray((NNState[]) Array.newInstance(NNState.class, state.size())); Arrays.sort(stateList); for (int i = stateList.length - 1; i >= 0; i--) result.add(stateList[i].best); }
@Override public void kNearestNeighbour(Collection<T> result, Coordinate query, int k) { final Stack<KDNode<T>> stack = walkdown(query); final PriorityQueue<NNState> state = new PriorityQueue<NNState>(k); final NNState initialState = new NNState(); initialState.best = stack.peek()._point; initialState.bestDist = distance(query, initialState.best); state.add(initialState); while (!stack.isEmpty()) { final KDNode<T> current = stack.pop(); checkSubtreeK(current, query, state, k); } @SuppressWarnings("unchecked") final NNState[] stateList = state.toArray((NNState[]) Array.newInstance(NNState.class, state.size())); Arrays.sort(stateList); for (int i = stateList.length - 1; i >= 0; i--) result.add(stateList[i].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; }
/** * 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; }