generated from karl/cpp-template
Fix clang-format
That earlier change introduced an error because YAML sucks
This commit is contained in:
parent
e7f18acc7d
commit
ece164a660
@ -9,4 +9,5 @@ AllowShortLoopsOnASingleLine: 'false'
|
|||||||
AlwaysBreakBeforeMultilineStrings: 'true'
|
AlwaysBreakBeforeMultilineStrings: 'true'
|
||||||
IndentWidth: '4'
|
IndentWidth: '4'
|
||||||
ColumnLimit: 100
|
ColumnLimit: 100
|
||||||
...
|
|
||||||
|
...
|
||||||
|
131
kdtree.h
131
kdtree.h
@ -8,97 +8,94 @@ struct Point;
|
|||||||
struct Triangle;
|
struct Triangle;
|
||||||
|
|
||||||
struct Node {
|
struct Node {
|
||||||
Node(int axis, Point *point, Node *left, Node *right)
|
Node(int axis, Point *point, Node *left, Node *right)
|
||||||
: axis(axis), point(point), left(left), right(right) {}
|
: axis(axis), point(point), left(left), right(right) {}
|
||||||
|
|
||||||
int axis;
|
int axis;
|
||||||
|
|
||||||
Point *point;
|
Point *point;
|
||||||
|
|
||||||
Node *left;
|
Node *left;
|
||||||
Node *right;
|
Node *right;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Triangle {
|
struct Triangle {
|
||||||
Triangle(Point *p1, Point *p2, Point *p3) : p1(p1), p2(p2), p3(p3) {}
|
Triangle(Point *p1, Point *p2, Point *p3) : p1(p1), p2(p2), p3(p3) {}
|
||||||
|
|
||||||
Point *p1;
|
Point *p1;
|
||||||
Point *p2;
|
Point *p2;
|
||||||
Point *p3;
|
Point *p3;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
Point(float coordinates[3], Triangle *triangle)
|
Point(float coordinates[3], Triangle *triangle)
|
||||||
: coordinates(coordinates), triangle(triangle) {}
|
: coordinates(coordinates), triangle(triangle) {}
|
||||||
|
|
||||||
float *coordinates;
|
float *coordinates;
|
||||||
|
|
||||||
Triangle *triangle;
|
Triangle *triangle;
|
||||||
};
|
};
|
||||||
|
|
||||||
class KDTree {
|
class KDTree {
|
||||||
public:
|
public:
|
||||||
KDTree(std::vector<Point *> points) { root = build(points, 0); }
|
KDTree(std::vector<Point *> points) { root = build(points, 0); }
|
||||||
|
|
||||||
~KDTree() = default;
|
~KDTree() = default;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Node *root;
|
Node *root;
|
||||||
|
|
||||||
int MAX_DEPTH = 500;
|
int MAX_DEPTH = 500;
|
||||||
|
|
||||||
// Returns a comparator lambda for assessing which of the two points has a
|
// Returns a comparator lambda for assessing which of the two points has a
|
||||||
// greater coordinate in the given axis.
|
// greater coordinate in the given axis.
|
||||||
auto get_point_comparator(int axis) {
|
auto get_point_comparator(int axis) {
|
||||||
return [axis](Point *p1, Point *p2) {
|
return [axis](Point *p1, Point *p2) {
|
||||||
return p1->coordinates[axis] < p2->coordinates[axis];
|
return p1->coordinates[axis] < p2->coordinates[axis];
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
Node *build(std::vector<Point *> points, int depth) {
|
|
||||||
// Exit conditions
|
|
||||||
if (points.empty() || depth > MAX_DEPTH) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Select axis by choosing the one with maximal extent
|
Node *build(std::vector<Point *> points, int depth) {
|
||||||
float max_extent = 0;
|
// Exit conditions
|
||||||
int axis = 0;
|
if (points.empty() || depth > MAX_DEPTH) { return nullptr; }
|
||||||
|
|
||||||
for (int it_axis = 0; it_axis < 3; it_axis++) {
|
// Select axis by choosing the one with maximal extent
|
||||||
// Get extent along this axis
|
float max_extent = 0;
|
||||||
auto comparator = get_point_comparator(it_axis);
|
int axis = 0;
|
||||||
|
|
||||||
Point *min = *std::max_element(points.begin(), points.end(), comparator);
|
for (int it_axis = 0; it_axis < 3; it_axis++) {
|
||||||
Point *max = *std::max_element(points.begin(), points.end(), comparator);
|
// Get extent along this axis
|
||||||
|
auto comparator = get_point_comparator(it_axis);
|
||||||
|
|
||||||
float extent = max->coordinates[it_axis] - min->coordinates[it_axis];
|
Point *min = *std::max_element(points.begin(), points.end(), comparator);
|
||||||
|
Point *max = *std::max_element(points.begin(), points.end(), comparator);
|
||||||
|
|
||||||
// Is it greater than max_extent?
|
float extent = max->coordinates[it_axis] - min->coordinates[it_axis];
|
||||||
if (extent > max_extent) {
|
|
||||||
// If so, make this the splitting axis
|
// Is it greater than max_extent?
|
||||||
max_extent = extent;
|
if (extent > max_extent) {
|
||||||
axis = it_axis;
|
// If so, make this the splitting axis
|
||||||
}
|
max_extent = extent;
|
||||||
|
axis = it_axis;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Choose the median as the pivot and sort the points into
|
||||||
|
// left-of-median and right-of-median using nth_element
|
||||||
|
int middle = points.size() / 2;
|
||||||
|
|
||||||
|
std::nth_element(points.begin(), points.begin() + middle, points.end(),
|
||||||
|
get_point_comparator(axis));
|
||||||
|
|
||||||
|
Point *median;
|
||||||
|
float pivot;
|
||||||
|
|
||||||
|
// TODO: This copies. Can we split the vector into two without copying?
|
||||||
|
std::vector<Point *> left_of_median(points.begin(), points.begin() + middle);
|
||||||
|
std::vector<Point *> right_of_median(points.begin() + middle, points.end());
|
||||||
|
|
||||||
|
// Create node, recursively call to construct subtree
|
||||||
|
return new Node(axis, median, build(left_of_median, depth + 1),
|
||||||
|
build(right_of_median, depth + 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Choose the median as the pivot and sort the points into
|
|
||||||
// left-of-median and right-of-median using nth_element
|
|
||||||
int middle = points.size() / 2;
|
|
||||||
|
|
||||||
std::nth_element(points.begin(), points.begin() + middle, points.end(),
|
|
||||||
get_point_comparator(axis));
|
|
||||||
|
|
||||||
Point *median;
|
|
||||||
float pivot;
|
|
||||||
|
|
||||||
// TODO: This copies. Can we split the vector into two without copying?
|
|
||||||
std::vector<Point *> left_of_median(points.begin(),
|
|
||||||
points.begin() + middle);
|
|
||||||
std::vector<Point *> right_of_median(points.begin() + middle, points.end());
|
|
||||||
|
|
||||||
// Create node, recursively call to construct subtree
|
|
||||||
return new Node(axis, median, build(left_of_median, depth + 1),
|
|
||||||
build(right_of_median, depth + 1));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user