generated from karl/cpp-template
Move geometry structs to own file
Also, the Ray got an unimplemented `intersects_triangle` method, so the kdtree intersection function should be complete.
This commit is contained in:
parent
44f0699ab6
commit
97f8265014
57
geometry.h
Normal file
57
geometry.h
Normal file
@ -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;
|
||||
}
|
||||
};
|
62
kdtree.h
62
kdtree.h
@ -1,64 +1,10 @@
|
||||
#include "geometry.h"
|
||||
#include <algorithm>
|
||||
#include <glm/glm.hpp>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// 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<Point *> 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user