generated from karl/cpp-template
Compare commits
No commits in common. "44f0699ab6a8f670748a33a6fa3ea6df2d4e474b" and "c72dde9b8477979b42810ce45feecb003d0a10b8" have entirely different histories.
44f0699ab6
...
c72dde9b84
70
kdtree.h
70
kdtree.h
@ -1,6 +1,5 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <iostream>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -33,19 +32,6 @@ struct Point {
|
|||||||
Point(float coordinates[3], Triangle *triangle)
|
Point(float coordinates[3], Triangle *triangle)
|
||||||
: coordinates(coordinates), 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;
|
float *coordinates;
|
||||||
|
|
||||||
Triangle *triangle;
|
Triangle *triangle;
|
||||||
@ -65,7 +51,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); }
|
Point *intersect_ray(Ray ray) { return intersect_ray_recurse(ray, root); }
|
||||||
|
|
||||||
std::string to_string() {
|
std::string to_string() {
|
||||||
std::string str = "";
|
std::string str = "";
|
||||||
@ -129,56 +115,12 @@ 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) {
|
Point *intersect_ray_recurse(Ray ray, Node *node) {
|
||||||
// Exit condition: There was no collision
|
// Intersect ray with the point's splitting plane
|
||||||
if (node == nullptr) { return nullptr; }
|
// If there is an intersection: Recurse to both children (but the nearer one first)
|
||||||
|
// Otherwise: Recurse only to the nearer one
|
||||||
|
|
||||||
// Is the left or right child node closer to this point?
|
return node->point; // TODO
|
||||||
Node *near = ray.origin[node->axis] > node->point->coordinates[node->axis] ? node->right
|
|
||||||
: node->left;
|
|
||||||
Node *far = near == node->right ? node->left : node->right;
|
|
||||||
|
|
||||||
std::cout << "Checking " << node->point->coordinates[0] << ", "
|
|
||||||
<< node->point->coordinates[1] << ", " << node->point->coordinates[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
|
|
||||||
// 3. In the further section
|
|
||||||
|
|
||||||
// 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 the nearer segment had a collision, we're done! We're only interested in the closest
|
|
||||||
// collision.
|
|
||||||
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
|
|
||||||
|
|
||||||
// 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) {
|
void to_string_recurse(std::string &str, Node *node, int depth) {
|
||||||
|
5
main.cpp
5
main.cpp
@ -8,14 +8,11 @@ int main() {
|
|||||||
std::vector<Point *> points{new Point(new float[3]{0.0, 0.0, 0.0}, nullptr),
|
std::vector<Point *> points{new Point(new float[3]{0.0, 0.0, 0.0}, nullptr),
|
||||||
new Point(new float[3]{0.0, 1.0, 0.0}, nullptr),
|
new Point(new float[3]{0.0, 1.0, 0.0}, nullptr),
|
||||||
new Point(new float[3]{0.0, 2.0, 3.0}, nullptr),
|
new Point(new float[3]{0.0, 2.0, 3.0}, nullptr),
|
||||||
new Point(new float[3]{1.0, 0.0, 4.0}, nullptr),
|
new Point(new float[3]{1.0, 0.0, 4.0}, nullptr)};
|
||||||
new Point(new float[3]{1.0, -1.0, 8.0}, nullptr)};
|
|
||||||
|
|
||||||
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}));
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user