generated from karl/cpp-template
Compare commits
No commits in common. "21c0e9c6091fc0876ea01d66c9deb8faa49a3236" and "bf96bbf26f6a91a8b18ac883eb0199ebabe75919" have entirely different histories.
21c0e9c609
...
bf96bbf26f
@ -1,5 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
// Forward declarations
|
||||
@ -26,7 +24,7 @@ struct Vector {
|
||||
}
|
||||
|
||||
Vector cross(const Vector &other) {
|
||||
return Vector(c[1] * other[2] - c[2] * other[1], c[2] * other[0] - c[0] * other[2],
|
||||
return Vector(c[1] * other[2] - c[2] - other[1], c[2] * other[0] - c[0] * other[2],
|
||||
c[0] * other[1] - c[1] * other[0]);
|
||||
}
|
||||
|
||||
|
80
kdtree.h
80
kdtree.h
@ -1,5 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
#include "geometry.h"
|
||||
#include <algorithm>
|
||||
#include <glm/glm.hpp>
|
||||
@ -13,7 +11,7 @@ class KDTree {
|
||||
|
||||
~KDTree() = default; // TODO: Delete all allocated Nodes
|
||||
|
||||
Triangle *intersect_ray(Ray ray) { return intersect_ray_recurse(ray, root, 1000.0, 0); }
|
||||
Triangle *intersect_ray(Ray ray) { return intersect_ray_recurse(ray, root, 1000.0); }
|
||||
|
||||
std::string to_string() {
|
||||
std::string str = "";
|
||||
@ -21,16 +19,6 @@ class KDTree {
|
||||
return str;
|
||||
}
|
||||
|
||||
std::vector<Vector> get_lines() {
|
||||
std::vector<Vector> lines;
|
||||
float mins[3] = {-1000, -1000, -1000};
|
||||
float maxs[3] = {1000, 1000, 1000};
|
||||
|
||||
get_lines_recursive(lines, root, mins, maxs);
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
private:
|
||||
Node *root;
|
||||
|
||||
@ -87,7 +75,7 @@ class KDTree {
|
||||
build(right_of_median, depth + 1));
|
||||
}
|
||||
|
||||
Triangle *intersect_ray_recurse(Ray ray, Node *node, float max_distance, int depth) {
|
||||
Triangle *intersect_ray_recurse(Ray ray, Node *node, float max_distance) {
|
||||
// Exit condition: There was no collision
|
||||
if (node == nullptr) { return nullptr; }
|
||||
|
||||
@ -96,6 +84,9 @@ class KDTree {
|
||||
ray.origin[node->axis] > node->point->pos[node->axis] ? node->right : node->left;
|
||||
Node *far = near == node->right ? node->left : node->right;
|
||||
|
||||
std::cout << "Checking " << node->point->pos[0] << ", " << node->point->pos[1] << ", "
|
||||
<< node->point->pos[2] << std::endl;
|
||||
|
||||
// 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
|
||||
@ -107,7 +98,7 @@ class KDTree {
|
||||
? (node->point->pos[node->axis] - ray.origin[node->axis]) /
|
||||
ray.direction[node->axis]
|
||||
: max_distance;
|
||||
Triangle *near_result = intersect_ray_recurse(ray, near, t, depth + 1);
|
||||
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
|
||||
// collision.
|
||||
@ -126,7 +117,7 @@ class KDTree {
|
||||
// For this, calculate a new ray origin and continue towards that direction, but with
|
||||
// the new origin (we can leave behind what we already checked)
|
||||
return intersect_ray_recurse(Ray(ray.origin + ray.direction * t, ray.direction), far,
|
||||
max_distance - t, depth + 1);
|
||||
max_distance - t);
|
||||
}
|
||||
|
||||
// If nothing worked, return a nullptr
|
||||
@ -145,61 +136,4 @@ class KDTree {
|
||||
to_string_recurse(str, node->left, depth + 1);
|
||||
to_string_recurse(str, node->right, depth + 1);
|
||||
}
|
||||
|
||||
void get_lines_recursive(std::vector<Vector> &lines, Node *node, float mins[3], float maxs[3]) {
|
||||
if (node == nullptr) return;
|
||||
|
||||
int axis = node->axis;
|
||||
|
||||
// Add lines for this node
|
||||
// We add a whole bounding box, so we need 8 points -> 12 lines
|
||||
lines.emplace_back(Vector(mins[0], mins[1], mins[2]));
|
||||
lines.emplace_back(Vector(maxs[0], mins[1], mins[2]));
|
||||
|
||||
lines.emplace_back(Vector(maxs[0], mins[1], mins[2]));
|
||||
lines.emplace_back(Vector(maxs[0], maxs[1], mins[2]));
|
||||
|
||||
lines.emplace_back(Vector(maxs[0], maxs[1], mins[2]));
|
||||
lines.emplace_back(Vector(mins[0], maxs[1], mins[2]));
|
||||
|
||||
lines.emplace_back(Vector(mins[0], maxs[1], mins[2]));
|
||||
lines.emplace_back(Vector(mins[0], mins[1], mins[2]));
|
||||
|
||||
lines.emplace_back(Vector(mins[0], mins[1], maxs[2]));
|
||||
lines.emplace_back(Vector(maxs[0], mins[1], maxs[2]));
|
||||
|
||||
lines.emplace_back(Vector(maxs[0], mins[1], maxs[2]));
|
||||
lines.emplace_back(Vector(maxs[0], maxs[1], maxs[2]));
|
||||
|
||||
lines.emplace_back(Vector(maxs[0], maxs[1], maxs[2]));
|
||||
lines.emplace_back(Vector(mins[0], maxs[1], maxs[2]));
|
||||
|
||||
lines.emplace_back(Vector(mins[0], maxs[1], maxs[2]));
|
||||
lines.emplace_back(Vector(mins[0], mins[1], maxs[2]));
|
||||
|
||||
lines.emplace_back(Vector(mins[0], mins[1], mins[2]));
|
||||
lines.emplace_back(Vector(mins[0], mins[1], maxs[2]));
|
||||
|
||||
lines.emplace_back(Vector(maxs[0], mins[1], mins[2]));
|
||||
lines.emplace_back(Vector(maxs[0], mins[1], maxs[2]));
|
||||
|
||||
lines.emplace_back(Vector(mins[0], maxs[1], mins[2]));
|
||||
lines.emplace_back(Vector(mins[0], maxs[1], maxs[2]));
|
||||
|
||||
lines.emplace_back(Vector(maxs[0], maxs[1], mins[2]));
|
||||
lines.emplace_back(Vector(maxs[0], maxs[1], maxs[2]));
|
||||
|
||||
// TODO: We don't actually have to add all as some are always going to overlap with the
|
||||
// previous lines...
|
||||
|
||||
// Recurse on the left
|
||||
float new_maxs[3] = {maxs[0], maxs[1], maxs[2]}; // Copy initially
|
||||
new_maxs[axis] = node->point->pos[axis];
|
||||
get_lines_recursive(lines, node->left, mins, new_maxs);
|
||||
|
||||
// Recurse on the right
|
||||
float new_mins[3] = {mins[0], mins[1], mins[2]}; // Copy initially
|
||||
new_mins[axis] = node->point->pos[axis];
|
||||
get_lines_recursive(lines, node->right, new_mins, maxs);
|
||||
}
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user