#ifndef KDTREE_H1#define KDTREE_H23#include "precomp.hpp"45namespace cv6{7namespace ml8{910/*!11Fast Nearest Neighbor Search Class.1213The class implements D. Lowe BBF (Best-Bin-First) algorithm for the last14approximate (or accurate) nearest neighbor search in multi-dimensional spaces.1516First, a set of vectors is passed to KDTree::KDTree() constructor17or KDTree::build() method, where it is reordered.1819Then arbitrary vectors can be passed to KDTree::findNearest() methods, which20find the K nearest neighbors among the vectors from the initial set.21The user can balance between the speed and accuracy of the search by varying Emax22parameter, which is the number of leaves that the algorithm checks.23The larger parameter values yield more accurate results at the expense of lower processing speed.2425\code26KDTree T(points, false);27const int K = 3, Emax = INT_MAX;28int idx[K];29float dist[K];30T.findNearest(query_vec, K, Emax, idx, 0, dist);31CV_Assert(dist[0] <= dist[1] && dist[1] <= dist[2]);32\endcode33*/34class CV_EXPORTS_W KDTree35{36public:37/*!38The node of the search tree.39*/40struct Node41{42Node() : idx(-1), left(-1), right(-1), boundary(0.f) {}43Node(int _idx, int _left, int _right, float _boundary)44: idx(_idx), left(_left), right(_right), boundary(_boundary) {}4546//! split dimension; >=0 for nodes (dim), < 0 for leaves (index of the point)47int idx;48//! node indices of the left and the right branches49int left, right;50//! go to the left if query_vec[node.idx]<=node.boundary, otherwise go to the right51float boundary;52};5354//! the default constructor55CV_WRAP KDTree();56//! the full constructor that builds the search tree57CV_WRAP KDTree(InputArray points, bool copyAndReorderPoints = false);58//! the full constructor that builds the search tree59CV_WRAP KDTree(InputArray points, InputArray _labels,60bool copyAndReorderPoints = false);61//! builds the search tree62CV_WRAP void build(InputArray points, bool copyAndReorderPoints = false);63//! builds the search tree64CV_WRAP void build(InputArray points, InputArray labels,65bool copyAndReorderPoints = false);66//! finds the K nearest neighbors of "vec" while looking at Emax (at most) leaves67CV_WRAP int findNearest(InputArray vec, int K, int Emax,68OutputArray neighborsIdx,69OutputArray neighbors = noArray(),70OutputArray dist = noArray(),71OutputArray labels = noArray()) const;72//! finds all the points from the initial set that belong to the specified box73CV_WRAP void findOrthoRange(InputArray minBounds,74InputArray maxBounds,75OutputArray neighborsIdx,76OutputArray neighbors = noArray(),77OutputArray labels = noArray()) const;78//! returns vectors with the specified indices79CV_WRAP void getPoints(InputArray idx, OutputArray pts,80OutputArray labels = noArray()) const;81//! return a vector with the specified index82const float* getPoint(int ptidx, int* label = 0) const;83//! returns the search space dimensionality84CV_WRAP int dims() const;8586std::vector<Node> nodes; //!< all the tree nodes87CV_PROP Mat points; //!< all the points. It can be a reordered copy of the input vector set or the original vector set.88CV_PROP std::vector<int> labels; //!< the parallel array of labels.89CV_PROP int maxDepth; //!< maximum depth of the search tree. Do not modify it90CV_PROP_RW int normType; //!< type of the distance (cv::NORM_L1 or cv::NORM_L2) used for search. Initially set to cv::NORM_L2, but you can modify it91};9293}94}9596#endif979899