This commit is contained in:
CookieKastanie 2022-02-28 23:46:29 +01:00
parent 73b432d0c8
commit 2425729a72
2 changed files with 5 additions and 4 deletions

View File

@ -1,5 +1,6 @@
#include "kd_tree.h" #include "kd_tree.h"
#include <algorithm> #include <algorithm>
#include <limits>
KdTree::Point::Point(): x{0}, y{0}, z{0} {} KdTree::Point::Point(): x{0}, y{0}, z{0} {}
KdTree::Point::Point(double x, double y, double z): x{x}, y{y}, z{z} {} KdTree::Point::Point(double x, double y, double z): x{x}, y{y}, z{z} {}
@ -19,7 +20,7 @@ double KdTree::Point::dist2(Point const &other) const {
return x_ * x_ + y_ * y_ + z_ * z_; return x_ * x_ + y_ * y_ + z_ * z_;
} }
ostream& operator<<(ostream& os, KdTree::Point const & point) { ostream& operator<<(ostream &os, KdTree::Point const &point) {
os << "[" << point.x << ", " << point.y << ", " << point.z << "]"; os << "[" << point.x << ", " << point.y << ", " << point.z << "]";
return os; return os;
} }
@ -40,7 +41,7 @@ void KdTree::fill(std::vector<Tuple> &points) {
KdTree::Node *KdTree::fillRec(std::size_t begin, std::size_t end, int axis) { KdTree::Node *KdTree::fillRec(std::size_t begin, std::size_t end, int axis) {
if(end <= begin) return nullptr; if(end <= begin) return nullptr;
size_t n = begin + (end - begin) / 2; std::size_t n = begin + (end - begin) / 2;
auto i = nodes.begin(); auto i = nodes.begin();
std::nth_element(i + begin, i + n, i + end, [&](Node &p1, Node &p2) { std::nth_element(i + begin, i + n, i + end, [&](Node &p1, Node &p2) {
return p1.position[axis] < p2.position[axis]; return p1.position[axis] < p2.position[axis];
@ -54,7 +55,7 @@ KdTree::Node *KdTree::fillRec(std::size_t begin, std::size_t end, int axis) {
} }
KdTree::Point KdTree::query(Point const &point) { KdTree::Point KdTree::query(Point const &point) {
bestDist = 99999.; bestDist = std::numeric_limits<double>::max();
bestNode = nullptr; bestNode = nullptr;
queryRec(root, point, 0); queryRec(root, point, 0);
return bestNode->position; return bestNode->position;

View File

@ -20,7 +20,7 @@ public:
double operator[] (std::size_t i) const; double operator[] (std::size_t i) const;
double dist2(Point const &other) const; double dist2(Point const &other) const;
friend ostream& operator<<(ostream& os, Point const & point); friend ostream& operator<<(ostream &os, Point const &point);
}; };
using Tuple = std::pair<Point, vtkIdType>; using Tuple = std::pair<Point, vtkIdType>;