#ifndef KD_TREE_H #define KD_TREE_H #include #include #include // https://rosettacode.org/wiki/K-d_tree#C.2B.2B class KdTree { public: KdTree() = default; struct Point { Point(); Point(double x, double y, double z); Point(double *position); double x, y, z; double operator[] (std::size_t i) const; double dist2(Point const &other) const; friend ostream& operator<<(ostream& os, Point const & point); }; using Tuple = std::pair; void fill(std::vector &points); Point query(Point const &point); Point query(double *point); private: struct Node { Point position; vtkIdType index; Node *leftChild; Node *rightChild; }; std::vector nodes; Node *root; double bestDist; Node *bestNode; Node *fillRec(std::size_t begin, std::size_t end, int axis); void queryRec(Node *node, Point const &point, int axis); }; #endif