diff --git a/geometry.h b/geometry.h new file mode 100644 index 0000000..c1d0499 --- /dev/null +++ b/geometry.h @@ -0,0 +1,57 @@ +// Forward declarations +struct Point; + +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 Point { + Point(float coordinates[3], Triangle *triangle) + : coordinates(coordinates), triangle(triangle) {} + + Point operator+(const Point &other) const { + return Point(new float[3]{coordinates[0] + other.coordinates[0], + coordinates[1] + other.coordinates[1], + coordinates[2] + other.coordinates[2]}, + nullptr); + } + + Point operator*(float scalar) const { + return Point( + new float[3]{coordinates[0] * scalar, coordinates[1] * scalar, coordinates[2] * scalar}, + nullptr); + } + + float *coordinates; + + Triangle *triangle; +}; + +struct Ray { + Ray(float origin[3], float direction[3]) : origin(origin), direction(direction) {} + + float *origin; + + float *direction; + + bool intersects_triangle(Triangle *triangle) { + // TODO + return false; + } +}; \ No newline at end of file diff --git a/kdtree.h b/kdtree.h index 252648b..135ae21 100644 --- a/kdtree.h +++ b/kdtree.h @@ -1,64 +1,10 @@ +#include "geometry.h" #include #include #include #include #include -// Forward declarations -struct Node; -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 Point { - Point(float coordinates[3], Triangle *triangle) - : coordinates(coordinates), triangle(triangle) {} - - Point operator+(const Point &other) const { - return Point(new float[3]{coordinates[0] + other.coordinates[0], - coordinates[1] + other.coordinates[1], - coordinates[2] + other.coordinates[2]}, - nullptr); - } - - Point operator*(float scalar) const { - return Point( - new float[3]{coordinates[0] * scalar, coordinates[1] * scalar, coordinates[2] * scalar}, - nullptr); - } - - float *coordinates; - - Triangle *triangle; -}; - -struct Ray { - Ray(float origin[3], float direction[3]) : origin(origin), direction(direction) {} - - float *origin; - - float *direction; -}; - class KDTree { public: KDTree(std::vector points) { root = build(points, 0); } @@ -160,8 +106,10 @@ class KDTree { if (near_result != nullptr) { return near_result; } // No collision in the nearer side, so check for a collision directly here - Point *collision_here = nullptr; - // TODO: Ray-triangle-intersection here + if (ray.intersects_triangle(node->point->triangle)) { + // We do have a collision here, so we're done and can return this point! + return node->point; + } // No collision here either. Does it make sense to also check the far node? // Only if the axes are not parallel and if that area is not behind us