generated from karl/cpp-template
Compare commits
3 Commits
80d6a27eef
...
ab2adf4173
Author | SHA1 | Date | |
---|---|---|---|
ab2adf4173 | |||
2105e2619e | |||
2ce6b06e54 |
6
Makefile
6
Makefile
@ -1,10 +1,10 @@
|
|||||||
CXX = g++
|
CXX = g++
|
||||||
CXXFLAGS = -Wall -O3
|
CXXFLAGS = -Wall -O3
|
||||||
|
|
||||||
program: main.o
|
kdtree: main.o
|
||||||
$(CXX) $(CXXFLAGS) -o program.out main.o
|
$(CXX) $(CXXFLAGS) -o kdtree.out main.o
|
||||||
|
|
||||||
main.o: main.cpp
|
main.o: main.cpp kdtree.h
|
||||||
$(CXX) $(CXXFLAGS) -c main.cpp
|
$(CXX) $(CXXFLAGS) -c main.cpp
|
||||||
|
|
||||||
clean :
|
clean :
|
||||||
|
7
kdtree.h
7
kdtree.h
@ -74,7 +74,7 @@ class KDTree {
|
|||||||
// Get extent along this axis
|
// Get extent along this axis
|
||||||
auto comparator = get_point_comparator(it_axis);
|
auto comparator = get_point_comparator(it_axis);
|
||||||
|
|
||||||
Point *min = *std::max_element(points.begin(), points.end(), comparator);
|
Point *min = *std::min_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);
|
||||||
|
|
||||||
float extent = max->coordinates[it_axis] - min->coordinates[it_axis];
|
float extent = max->coordinates[it_axis] - min->coordinates[it_axis];
|
||||||
@ -98,7 +98,7 @@ class KDTree {
|
|||||||
|
|
||||||
// TODO: This copies. Can we split the vector into two without copying?
|
// 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 *> left_of_median(points.begin(), points.begin() + middle);
|
||||||
std::vector<Point *> right_of_median(points.begin() + middle, points.end());
|
std::vector<Point *> right_of_median(points.begin() + middle + 1, points.end());
|
||||||
|
|
||||||
// Create node, recursively call to construct subtree
|
// Create node, recursively call to construct subtree
|
||||||
return new Node(axis, median, build(left_of_median, depth + 1),
|
return new Node(axis, median, build(left_of_median, depth + 1),
|
||||||
@ -112,7 +112,8 @@ class KDTree {
|
|||||||
|
|
||||||
str += std::string(depth, '-') + std::to_string(point->coordinates[0]) + ", " +
|
str += std::string(depth, '-') + std::to_string(point->coordinates[0]) + ", " +
|
||||||
std::to_string(point->coordinates[1]) + ", " +
|
std::to_string(point->coordinates[1]) + ", " +
|
||||||
std::to_string(point->coordinates[2]) + "\n";
|
std::to_string(point->coordinates[2]) + " with axis " + std::to_string(node->axis) +
|
||||||
|
"\n";
|
||||||
|
|
||||||
to_string_recurse(str, node->left, depth + 1);
|
to_string_recurse(str, node->left, depth + 1);
|
||||||
to_string_recurse(str, node->right, depth + 1);
|
to_string_recurse(str, node->right, depth + 1);
|
||||||
|
11
main.cpp
11
main.cpp
@ -1,7 +1,18 @@
|
|||||||
|
#include "kdtree.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::cout << "Hello World!" << std::endl;
|
std::cout << "Hello World!" << std::endl;
|
||||||
|
|
||||||
|
// Test points
|
||||||
|
std::vector<Point *> points{new Point(new float[3]{0.0, 0.0, 0.0}, nullptr),
|
||||||
|
new Point(new float[3]{0.0, 1.0, 0.0}, nullptr),
|
||||||
|
new Point(new float[3]{0.0, 2.0, 3.0}, nullptr),
|
||||||
|
new Point(new float[3]{1.0, 0.0, 4.0}, nullptr)};
|
||||||
|
|
||||||
|
KDTree tree = KDTree(points);
|
||||||
|
|
||||||
|
std::cout << tree.to_string();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user