generated from karl/cpp-template
Move point comparator lambda to function
This avoids some code duplication
This commit is contained in:
parent
0d8d6d7f6b
commit
e7f18acc7d
18
kdtree.h
18
kdtree.h
@ -47,6 +47,14 @@ private:
|
|||||||
|
|
||||||
int MAX_DEPTH = 500;
|
int MAX_DEPTH = 500;
|
||||||
|
|
||||||
|
// Returns a comparator lambda for assessing which of the two points has a
|
||||||
|
// greater coordinate in the given axis.
|
||||||
|
auto get_point_comparator(int axis) {
|
||||||
|
return [axis](Point *p1, Point *p2) {
|
||||||
|
return p1->coordinates[axis] < p2->coordinates[axis];
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
Node *build(std::vector<Point *> points, int depth) {
|
Node *build(std::vector<Point *> points, int depth) {
|
||||||
// Exit conditions
|
// Exit conditions
|
||||||
if (points.empty() || depth > MAX_DEPTH) {
|
if (points.empty() || depth > MAX_DEPTH) {
|
||||||
@ -59,9 +67,7 @@ private:
|
|||||||
|
|
||||||
for (int it_axis = 0; it_axis < 3; it_axis++) {
|
for (int it_axis = 0; it_axis < 3; it_axis++) {
|
||||||
// Get extent along this axis
|
// Get extent along this axis
|
||||||
auto comparator = [it_axis](Point *p1, Point *p2) {
|
auto comparator = get_point_comparator(it_axis);
|
||||||
return p1->coordinates[it_axis] < p2->coordinates[it_axis];
|
|
||||||
};
|
|
||||||
|
|
||||||
Point *min = *std::max_element(points.begin(), points.end(), comparator);
|
Point *min = *std::max_element(points.begin(), points.end(), comparator);
|
||||||
Point *max = *std::max_element(points.begin(), points.end(), comparator);
|
Point *max = *std::max_element(points.begin(), points.end(), comparator);
|
||||||
@ -80,12 +86,8 @@ private:
|
|||||||
// left-of-median and right-of-median using nth_element
|
// left-of-median and right-of-median using nth_element
|
||||||
int middle = points.size() / 2;
|
int middle = points.size() / 2;
|
||||||
|
|
||||||
// TODO: Code duplication from the comparator in the earlier axis assessment
|
|
||||||
// loop
|
|
||||||
std::nth_element(points.begin(), points.begin() + middle, points.end(),
|
std::nth_element(points.begin(), points.begin() + middle, points.end(),
|
||||||
[axis](Point *p1, Point *p2) {
|
get_point_comparator(axis));
|
||||||
return p1->coordinates[axis] < p2->coordinates[axis];
|
|
||||||
});
|
|
||||||
|
|
||||||
Point *median;
|
Point *median;
|
||||||
float pivot;
|
float pivot;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user