From 44f0699ab6a8f670748a33a6fa3ea6df2d4e474b Mon Sep 17 00:00:00 2001 From: karl Date: Mon, 28 Dec 2020 16:28:19 +0100 Subject: [PATCH] Restructure intersect_ray_recurse The implementation should be finished aside from the actual ray-triangle-intersection. --- kdtree.h | 65 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/kdtree.h b/kdtree.h index 5918cf9..252648b 100644 --- a/kdtree.h +++ b/kdtree.h @@ -65,7 +65,7 @@ class KDTree { ~KDTree() = default; // TODO: Delete all allocated Nodes - Point *intersect_ray(Ray ray) { return intersect_ray_recurse(ray, root); } + Point *intersect_ray(Ray ray) { return intersect_ray_recurse(ray, root, 1000.0); } std::string to_string() { std::string str = ""; @@ -129,7 +129,7 @@ class KDTree { build(right_of_median, depth + 1)); } - Point *intersect_ray_recurse(Ray ray, Node *node) { + Point *intersect_ray_recurse(Ray ray, Node *node, float max_distance) { // Exit condition: There was no collision if (node == nullptr) { return nullptr; } @@ -142,40 +142,43 @@ class KDTree { << node->point->coordinates[1] << ", " << node->point->coordinates[2] << ", " << std::endl; - // Intersect ray with the point's splitting plane + // Check for collisions in this order (stopping if an intersection is found): + // 1. In the nearer section + // 2. With the point in this current node + // 3. In the further section - // Are they parallel? If so, recurse only to the nearer side - if (ray.direction[node->axis] == 0.0) { - return intersect_ray_recurse(ray, near); - } else { - // They are not parallel, so check where the intersection occurs - float t = (node->point->coordinates[node->axis] - ray.origin[node->axis]) / - ray.direction[node->axis]; + // If the axes are not parallel, our max_distance decreases, since we've already covered + // some area. `t` represents the distance from this node to the splitting plane. + float t = ray.direction[node->axis] != 0.0 + ? (node->point->coordinates[node->axis] - ray.origin[node->axis]) / + ray.direction[node->axis] + : max_distance; + Point *near_result = intersect_ray_recurse(ray, near, t); - if (t >= 0.0) { - // t is positive, so we need to recurse to both children if the nearer one does not - // result in a collision - Point *first_attempt = intersect_ray_recurse(ray, near); + // If the nearer segment had a collision, we're done! We're only interested in the closest + // collision. + if (near_result != nullptr) { return near_result; } - if (first_attempt != nullptr) { - return first_attempt; - } else { - // The first attempt did not work, so recurse to the other side too. - // For this, calculate a new ray origin ... - float new_origin[3]{ray.origin[0] + t * ray.direction[0], - ray.origin[1] + t * ray.direction[1], - ray.origin[2] + t * ray.direction[2]}; + // No collision in the nearer side, so check for a collision directly here + Point *collision_here = nullptr; + // TODO: Ray-triangle-intersection here - // ... and continue towards that direction, but with the new origin (we can - // leave behind what we already checked) - return intersect_ray_recurse(Ray(new_origin, ray.direction), far); - } - } else { - // We only have to check the nearer one, as the other side can't be reached by the - // ray - return intersect_ray_recurse(ray, near); - } + // 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 + if (ray.direction[node->axis] != 0.0 && t >= 0.0) { + // It does make sense to check the far node. + // For this, calculate a new ray origin ... + float new_origin[3]{ray.origin[0] + t * ray.direction[0], + ray.origin[1] + t * ray.direction[1], + ray.origin[2] + t * ray.direction[2]}; + + // ... and continue towards that direction, but with the new origin (we can + // leave behind what we already checked) + return intersect_ray_recurse(Ray(new_origin, ray.direction), far, max_distance - t); } + + // If nothing worked, return a nullptr + return nullptr; } void to_string_recurse(std::string &str, Node *node, int depth) {