SpiecsEngine
 
Loading...
Searching...
No Matches

◆ nearest_neighbour_search()

template<uint32_t K>
auto scl::kd_tree< K >::nearest_neighbour_search ( const item & point,
const item & condition ) const -> item
inline

Search for a nearest point in the kd_tree. Traverse the tree as in a normal search to find the leaf node closest to the query point. Unwind the recursion, considering at each step whether there could be any points on the other side of the splitting plane that are closer to the query point than the current best. If there could be, recurse down the other branch, adding any closer points found to the current best. The final best point is the nearest neighbor.

Parameters
[in]pointSearched point in k d.
[in]conditionallowed distance for neighbours.
Returns
Returns nearest point.

Definition at line 629 of file KDTree.h.

634 {
635 std::vector<kd_tree<K>::item> rangePoints;
636 range_search_recursive(m_Root, point, condition, rangePoints, 0);
637
638 if (rangePoints.size() == 0) return {};
639
640 int nearIndex = -1;
641 float nearRate = 1E11;
642 for (int i = 0; i < rangePoints.size(); i++)
643 {
644 float rate = 0;
645 for (int j = 0; j < K; j++)
646 {
647 rate += std::abs(rangePoints[i][j] - point[j]);
648 }
649 if (rate <= nearRate)
650 {
651 nearRate = rate;
652 nearIndex = i;
653 }
654 }
655
656 return rangePoints[nearIndex];
657 }
Node * m_Root
Pointer to the root node of the tree.
Definition KDTree.h:76
void range_search_recursive(Node *node, const item &point, const item &condition, std::vector< item > &rangePoints, int depth) const
Recursive function to search for all nearest points in the kd_tree.
Definition KDTree.h:463