Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions KDTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,27 @@ pointIndexArr KDTree::nearest_pointIndices(point_t const& pt,
return output;
}

std::pair<std::vector<double>, std::vector<size_t>> KDTree::nearest_indices_dists(
point_t const& pt, size_t const& num_nearest) {

size_t level = 0;
std::list<std::pair<KDNodePtr, double>> k_buffer{};
k_buffer.emplace_back(root_, dist2(static_cast<point_t>(*root_), pt));
knearest_(root_, // beginning of tree
pt, // point we are querying
level, // start from level 0
num_nearest, // number of nearest neighbours to return in k_buffer
k_buffer); // list of k nearest neigbours (to be filled)
std::vector<size_t> output_ids;
std::vector<double> output_dists;
std::for_each(k_buffer.begin(), k_buffer.end(),
[&output_ids, &output_dists](auto const& nodeptr_dist) {
output_ids.push_back(nodeptr_dist.first->index);
output_dists.push_back(nodeptr_dist.second);
});
return {output_dists, output_ids};
}

pointVec KDTree::nearest_points(point_t const& pt, size_t const& num_nearest) {
auto const k_nearest{nearest_pointIndices(pt, num_nearest)};
pointVec k_nearest_points(k_nearest.size());
Expand Down
10 changes: 10 additions & 0 deletions KDTree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ class KDTree {
pointIndexArr nearest_pointIndices(point_t const& pt,
size_t const& num_nearest);

/// Gets index of points closest to the given input point and their
/// respective distance from it.
///
/// @param pt input point.
/// @param num_nearest Number of nearest points to return.
/// @returns a vector containing the point distances and a vector
/// containing point indices.
std::pair<std::vector<double>, std::vector<size_t>>
nearest_indices_dists(point_t const& pt, size_t const& num_nearest);

/// Get the nearest set of points to the given input point.
///
/// @param pt input point.
Expand Down