generated from karl/cpp-template
Compare commits
3 Commits
6cb085dcd6
...
bf96bbf26f
Author | SHA1 | Date | |
---|---|---|---|
bf96bbf26f | |||
d36181393f | |||
d966beeb15 |
2
Makefile
2
Makefile
@ -4,7 +4,7 @@ CXXFLAGS = -Wall -O3
|
|||||||
kdtree: main.o
|
kdtree: main.o
|
||||||
$(CXX) $(CXXFLAGS) -o kdtree.out main.o
|
$(CXX) $(CXXFLAGS) -o kdtree.out main.o
|
||||||
|
|
||||||
main.o: main.cpp kdtree.h
|
main.o: main.cpp kdtree.h geometry.h
|
||||||
$(CXX) $(CXXFLAGS) -c main.cpp
|
$(CXX) $(CXXFLAGS) -c main.cpp
|
||||||
|
|
||||||
clean :
|
clean :
|
||||||
|
63
geometry.h
63
geometry.h
@ -1,25 +1,7 @@
|
|||||||
|
#include <vector>
|
||||||
|
|
||||||
// Forward declarations
|
// Forward declarations
|
||||||
struct Point;
|
struct Triangle;
|
||||||
|
|
||||||
struct Node {
|
|
||||||
Node(int axis, Point *point, Node *left, Node *right)
|
|
||||||
: axis(axis), point(point), left(left), right(right) {}
|
|
||||||
|
|
||||||
int axis;
|
|
||||||
|
|
||||||
Point *point;
|
|
||||||
|
|
||||||
Node *left;
|
|
||||||
Node *right;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Triangle {
|
|
||||||
Triangle(Point *p1, Point *p2, Point *p3) : p1(p1), p2(p2), p3(p3) {}
|
|
||||||
|
|
||||||
Point *p1;
|
|
||||||
Point *p2;
|
|
||||||
Point *p3;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Vector {
|
struct Vector {
|
||||||
Vector(float coordinates[3]) : c(coordinates) {}
|
Vector(float coordinates[3]) : c(coordinates) {}
|
||||||
@ -42,24 +24,47 @@ struct Vector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Vector cross(const Vector &other) {
|
Vector cross(const Vector &other) {
|
||||||
return Vector(c[2] * other[3] - c[3] - other[2], c[3] * other[1] - c[1] * other[3],
|
return Vector(c[1] * other[2] - c[2] - other[1], c[2] * other[0] - c[0] * other[2],
|
||||||
c[1] * other[2] - c[2] * other[1]);
|
c[0] * other[1] - c[1] * other[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
float dot(const Vector &other) { return c[1] * other[1] + c[2] * other[2] + c[3] * other[3]; }
|
float dot(const Vector &other) { return c[0] * other[0] + c[1] * other[1] + c[2] * other[2]; }
|
||||||
|
|
||||||
float *c;
|
float *c;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
Point(float coordinates[3], Triangle *triangle)
|
Point(Vector pos, Triangle *triangle) : pos(pos), triangle(triangle) {}
|
||||||
: pos(Vector(coordinates)), triangle(triangle) {}
|
|
||||||
|
|
||||||
Vector pos;
|
Vector pos;
|
||||||
|
|
||||||
Triangle *triangle;
|
Triangle *triangle;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct Triangle {
|
||||||
|
Triangle(Vector p1, Vector p2, Vector p3) : p1(p1), p2(p2), p3(p3) {}
|
||||||
|
|
||||||
|
std::vector<Point *> create_point_objects() {
|
||||||
|
return std::vector<Point *>{new Point(p1, this), new Point(p2, this), new Point(p3, this)};
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector p1;
|
||||||
|
Vector p2;
|
||||||
|
Vector p3;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Node {
|
||||||
|
Node(int axis, Point *point, Node *left, Node *right)
|
||||||
|
: axis(axis), point(point), left(left), right(right) {}
|
||||||
|
|
||||||
|
int axis;
|
||||||
|
|
||||||
|
Point *point;
|
||||||
|
|
||||||
|
Node *left;
|
||||||
|
Node *right;
|
||||||
|
};
|
||||||
|
|
||||||
struct Ray {
|
struct Ray {
|
||||||
Ray(Vector origin, Vector direction) : origin(origin), direction(direction) {}
|
Ray(Vector origin, Vector direction) : origin(origin), direction(direction) {}
|
||||||
|
|
||||||
@ -72,9 +77,9 @@ struct Ray {
|
|||||||
// https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
|
// https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
|
||||||
const float EPSILON = 0.0000001;
|
const float EPSILON = 0.0000001;
|
||||||
|
|
||||||
Vector p1 = triangle->p1->pos;
|
Vector p1 = triangle->p1;
|
||||||
Vector p2 = triangle->p2->pos;
|
Vector p2 = triangle->p2;
|
||||||
Vector p3 = triangle->p3->pos;
|
Vector p3 = triangle->p3;
|
||||||
|
|
||||||
Vector edge1 = p2 - p1;
|
Vector edge1 = p2 - p1;
|
||||||
Vector edge2 = p3 - p1;
|
Vector edge2 = p3 - p1;
|
||||||
|
14
kdtree.h
14
kdtree.h
@ -11,7 +11,7 @@ class KDTree {
|
|||||||
|
|
||||||
~KDTree() = default; // TODO: Delete all allocated Nodes
|
~KDTree() = default; // TODO: Delete all allocated Nodes
|
||||||
|
|
||||||
Point *intersect_ray(Ray ray) { return intersect_ray_recurse(ray, root, 1000.0); }
|
Triangle *intersect_ray(Ray ray) { return intersect_ray_recurse(ray, root, 1000.0); }
|
||||||
|
|
||||||
std::string to_string() {
|
std::string to_string() {
|
||||||
std::string str = "";
|
std::string str = "";
|
||||||
@ -75,7 +75,7 @@ class KDTree {
|
|||||||
build(right_of_median, depth + 1));
|
build(right_of_median, depth + 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
Point *intersect_ray_recurse(Ray ray, Node *node, float max_distance) {
|
Triangle *intersect_ray_recurse(Ray ray, Node *node, float max_distance) {
|
||||||
// Exit condition: There was no collision
|
// Exit condition: There was no collision
|
||||||
if (node == nullptr) { return nullptr; }
|
if (node == nullptr) { return nullptr; }
|
||||||
|
|
||||||
@ -85,7 +85,7 @@ class KDTree {
|
|||||||
Node *far = near == node->right ? node->left : node->right;
|
Node *far = near == node->right ? node->left : node->right;
|
||||||
|
|
||||||
std::cout << "Checking " << node->point->pos[0] << ", " << node->point->pos[1] << ", "
|
std::cout << "Checking " << node->point->pos[0] << ", " << node->point->pos[1] << ", "
|
||||||
<< node->point->pos[2] << ", " << std::endl;
|
<< node->point->pos[2] << std::endl;
|
||||||
|
|
||||||
// Check for collisions in this order (stopping if an intersection is found):
|
// Check for collisions in this order (stopping if an intersection is found):
|
||||||
// 1. In the nearer section
|
// 1. In the nearer section
|
||||||
@ -98,16 +98,16 @@ class KDTree {
|
|||||||
? (node->point->pos[node->axis] - ray.origin[node->axis]) /
|
? (node->point->pos[node->axis] - ray.origin[node->axis]) /
|
||||||
ray.direction[node->axis]
|
ray.direction[node->axis]
|
||||||
: max_distance;
|
: max_distance;
|
||||||
Point *near_result = intersect_ray_recurse(ray, near, t);
|
Triangle *near_result = intersect_ray_recurse(ray, near, t);
|
||||||
|
|
||||||
// If the nearer segment had a collision, we're done! We're only interested in the closest
|
// If the nearer segment had a collision, we're done! We're only interested in the closest
|
||||||
// collision.
|
// collision.
|
||||||
if (near_result != nullptr) { return near_result; }
|
if (near_result != nullptr) { return near_result; }
|
||||||
|
|
||||||
// No collision in the nearer side, so check for a collision directly here
|
// No collision in the nearer side, so check for a collision directly here
|
||||||
if (node->point->triangle && ray.intersects_triangle(node->point->triangle)) {
|
if (ray.intersects_triangle(node->point->triangle)) {
|
||||||
// We do have a collision here, so we're done and can return this point!
|
// We do have a collision here, so we're done and can return this point!
|
||||||
return node->point;
|
return node->point->triangle;
|
||||||
}
|
}
|
||||||
|
|
||||||
// No collision here either. Does it make sense to also check the far node?
|
// No collision here either. Does it make sense to also check the far node?
|
||||||
@ -129,7 +129,7 @@ class KDTree {
|
|||||||
|
|
||||||
Point *point = node->point;
|
Point *point = node->point;
|
||||||
|
|
||||||
str += std::string(depth, '-') + std::to_string(point->pos[0]) + ", " +
|
str += std::string(depth * 2, ' ') + std::to_string(point->pos[0]) + ", " +
|
||||||
std::to_string(point->pos[1]) + ", " + std::to_string(point->pos[2]) +
|
std::to_string(point->pos[1]) + ", " + std::to_string(point->pos[2]) +
|
||||||
" with axis " + std::to_string(node->axis) + "\n";
|
" with axis " + std::to_string(node->axis) + "\n";
|
||||||
|
|
||||||
|
22
main.cpp
22
main.cpp
@ -2,20 +2,24 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::cout << "Hello World!" << std::endl;
|
// Testing triangles
|
||||||
|
Triangle t1 = Triangle(Vector(0.0, 0.0, 0.0), Vector(2.0, 0.0, 0.0), Vector(0.0, 2.0, 0.0));
|
||||||
|
Triangle t2 = Triangle(Vector(0.5, 0.0, 0.0), Vector(1.0, 1.0, 0.0), Vector(1.0, 1.0, 1.0));
|
||||||
|
|
||||||
// Test points
|
// Create list of all points from triangles
|
||||||
std::vector<Point *> points{new Point(new float[3]{0.0, 0.0, 0.0}, nullptr),
|
std::vector<Point *> points = t1.create_point_objects();
|
||||||
new Point(new float[3]{0.0, 1.0, 0.0}, nullptr),
|
std::vector<Point *> other_points = t2.create_point_objects();
|
||||||
new Point(new float[3]{0.0, 2.0, 3.0}, nullptr),
|
points.insert(points.end(), other_points.begin(), other_points.end());
|
||||||
new Point(new float[3]{1.0, 0.0, 4.0}, nullptr),
|
|
||||||
new Point(new float[3]{1.0, -1.0, 8.0}, nullptr)};
|
|
||||||
|
|
||||||
|
// Create and print the KDTree resulting from these points
|
||||||
KDTree tree = KDTree(points);
|
KDTree tree = KDTree(points);
|
||||||
|
|
||||||
std::cout << tree.to_string();
|
std::cout << tree.to_string();
|
||||||
|
|
||||||
tree.intersect_ray(Ray(new float[3]{0.0, 0.0, 5.0}, new float[3]{0.0, 0.0, -1.0}));
|
// Intersection check
|
||||||
|
Triangle *intersection =
|
||||||
|
tree.intersect_ray(Ray(new float[3]{0.5, 0.5, -1.0}, new float[3]{0.0, 0.1, 1.0}));
|
||||||
|
|
||||||
|
if (intersection != nullptr) { std::cout << "Hit!" << std::endl; }
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user