generated from karl/cpp-template
Restructure intersect_ray_recurse
The implementation should be finished aside from the actual ray-triangle-intersection.
This commit is contained in:
parent
71841766f1
commit
44f0699ab6
53
kdtree.h
53
kdtree.h
@ -65,7 +65,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); }
|
Point *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 = "";
|
||||||
@ -129,7 +129,7 @@ class KDTree {
|
|||||||
build(right_of_median, depth + 1));
|
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
|
// Exit condition: There was no collision
|
||||||
if (node == nullptr) { return nullptr; }
|
if (node == nullptr) { return nullptr; }
|
||||||
|
|
||||||
@ -142,25 +142,31 @@ class KDTree {
|
|||||||
<< node->point->coordinates[1] << ", " << node->point->coordinates[2] << ", "
|
<< node->point->coordinates[1] << ", " << node->point->coordinates[2] << ", "
|
||||||
<< std::endl;
|
<< 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 the axes are not parallel, our max_distance decreases, since we've already covered
|
||||||
if (ray.direction[node->axis] == 0.0) {
|
// some area. `t` represents the distance from this node to the splitting plane.
|
||||||
return intersect_ray_recurse(ray, near);
|
float t = ray.direction[node->axis] != 0.0
|
||||||
} else {
|
? (node->point->coordinates[node->axis] - ray.origin[node->axis]) /
|
||||||
// They are not parallel, so check where the intersection occurs
|
ray.direction[node->axis]
|
||||||
float t = (node->point->coordinates[node->axis] - ray.origin[node->axis]) /
|
: max_distance;
|
||||||
ray.direction[node->axis];
|
Point *near_result = intersect_ray_recurse(ray, near, t);
|
||||||
|
|
||||||
if (t >= 0.0) {
|
// If the nearer segment had a collision, we're done! We're only interested in the closest
|
||||||
// t is positive, so we need to recurse to both children if the nearer one does not
|
// collision.
|
||||||
// result in a collision
|
if (near_result != nullptr) { return near_result; }
|
||||||
Point *first_attempt = intersect_ray_recurse(ray, near);
|
|
||||||
|
|
||||||
if (first_attempt != nullptr) {
|
// No collision in the nearer side, so check for a collision directly here
|
||||||
return first_attempt;
|
Point *collision_here = nullptr;
|
||||||
} else {
|
// TODO: Ray-triangle-intersection here
|
||||||
// The first attempt did not work, so recurse to the other side too.
|
|
||||||
|
// 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 ...
|
// For this, calculate a new ray origin ...
|
||||||
float new_origin[3]{ray.origin[0] + t * ray.direction[0],
|
float new_origin[3]{ray.origin[0] + t * ray.direction[0],
|
||||||
ray.origin[1] + t * ray.direction[1],
|
ray.origin[1] + t * ray.direction[1],
|
||||||
@ -168,14 +174,11 @@ class KDTree {
|
|||||||
|
|
||||||
// ... and continue towards that direction, but with the new origin (we can
|
// ... and continue towards that direction, but with the new origin (we can
|
||||||
// leave behind what we already checked)
|
// leave behind what we already checked)
|
||||||
return intersect_ray_recurse(Ray(new_origin, ray.direction), far);
|
return intersect_ray_recurse(Ray(new_origin, ray.direction), far, max_distance - t);
|
||||||
}
|
|
||||||
} 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If nothing worked, return a nullptr
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void to_string_recurse(std::string &str, Node *node, int depth) {
|
void to_string_recurse(std::string &str, Node *node, int depth) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user