2017-03-01 01:46:03 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2017-03-07 23:34:08 +00:00
|
|
|
#include "vectors.h"
|
2017-03-07 14:58:22 +00:00
|
|
|
|
2017-03-08 11:40:45 +00:00
|
|
|
// Node in a binary AABB tree
|
|
|
|
struct AABBTreeNode
|
2017-03-01 01:46:03 +00:00
|
|
|
{
|
2017-03-08 11:40:45 +00:00
|
|
|
AABBTreeNode(const FVector2 &aabb_min, const FVector2 &aabb_max, int line_index) : aabb_left(aabb_min.X), aabb_top(aabb_min.Y), aabb_right(aabb_max.X), aabb_bottom(aabb_max.Y), left_node(-1), right_node(-1), line_index(line_index) { }
|
|
|
|
AABBTreeNode(const FVector2 &aabb_min, const FVector2 &aabb_max, int left, int right) : aabb_left(aabb_min.X), aabb_top(aabb_min.Y), aabb_right(aabb_max.X), aabb_bottom(aabb_max.Y), left_node(left), right_node(right), line_index(-1) { }
|
2017-03-07 14:58:22 +00:00
|
|
|
|
2017-03-08 11:40:45 +00:00
|
|
|
// Axis aligned bounding box for the node
|
2017-03-07 14:58:22 +00:00
|
|
|
float aabb_left, aabb_top;
|
|
|
|
float aabb_right, aabb_bottom;
|
2017-03-08 11:40:45 +00:00
|
|
|
|
2017-03-08 12:31:19 +00:00
|
|
|
// Child node indices
|
2017-03-08 11:40:45 +00:00
|
|
|
int left_node;
|
|
|
|
int right_node;
|
|
|
|
|
|
|
|
// AABBTreeLine index if it is a leaf node. Index is -1 if it is not.
|
2017-03-07 14:58:22 +00:00
|
|
|
int line_index;
|
2017-03-08 11:40:45 +00:00
|
|
|
|
|
|
|
// Padding to keep 16-byte length (this structure is uploaded to the GPU)
|
2017-03-07 14:58:22 +00:00
|
|
|
int padding;
|
2017-03-01 01:46:03 +00:00
|
|
|
};
|
|
|
|
|
2017-03-08 11:40:45 +00:00
|
|
|
// Line segment for leaf nodes in an AABB tree
|
|
|
|
struct AABBTreeLine
|
2017-03-01 01:46:03 +00:00
|
|
|
{
|
|
|
|
float x, y;
|
|
|
|
float dx, dy;
|
2017-03-07 14:58:22 +00:00
|
|
|
};
|
|
|
|
|
2017-03-08 11:40:45 +00:00
|
|
|
// Axis aligned bounding box tree used for ray testing lines.
|
|
|
|
class LevelAABBTree
|
2017-03-07 14:58:22 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-03-08 11:40:45 +00:00
|
|
|
// Constructs a tree for the current level
|
|
|
|
LevelAABBTree();
|
2017-03-07 14:58:22 +00:00
|
|
|
|
2017-03-08 11:40:45 +00:00
|
|
|
// Nodes in the AABB tree. Last node is the root node.
|
|
|
|
TArray<AABBTreeNode> nodes;
|
2017-03-07 14:58:22 +00:00
|
|
|
|
2017-03-08 11:40:45 +00:00
|
|
|
// Line segments for the leaf nodes in the tree.
|
|
|
|
TArray<AABBTreeLine> lines;
|
|
|
|
|
2017-03-08 12:31:19 +00:00
|
|
|
// Shoot a ray from ray_start to ray_end and return the closest hit as a fractional value between 0 and 1. Returns 1 if no line was hit.
|
2017-03-07 23:34:08 +00:00
|
|
|
double RayTest(const DVector3 &ray_start, const DVector3 &ray_end);
|
|
|
|
|
2017-03-07 14:58:22 +00:00
|
|
|
private:
|
2017-03-08 11:40:45 +00:00
|
|
|
// Test if a ray overlaps an AABB node or not
|
|
|
|
bool OverlapRayAABB(const DVector2 &ray_start2d, const DVector2 &ray_end2d, const AABBTreeNode &node);
|
|
|
|
|
|
|
|
// Intersection test between a ray and a line segment
|
2017-03-07 23:34:08 +00:00
|
|
|
double IntersectRayLine(const DVector2 &ray_start, const DVector2 &ray_end, int line_index, const DVector2 &raydelta, double rayd, double raydist2);
|
|
|
|
|
2017-03-08 11:40:45 +00:00
|
|
|
// Generate a tree node and its children recursively
|
|
|
|
int GenerateTreeNode(int *lines, int num_lines, const FVector2 *centroids, int *work_buffer);
|
2017-03-01 01:46:03 +00:00
|
|
|
};
|