3#ifndef TRAM_SDK_TEMPLATES_AABB_H
4#define TRAM_SDK_TEMPLATES_AABB_H
32 new_node->
value = value;
69 if (sibling_parent->
left == sibling) {
70 sibling_parent->
left = new_parent;
71 }
else if (sibling_parent->
right == sibling) {
72 sibling_parent->
right = new_parent;
77 new_parent->
parent = sibling_parent;
79 new_parent->
left = new_node;
80 new_parent->
right = sibling;
82 sibling->
parent = new_parent;
83 new_node->
parent = new_parent;
98 if (parent->
left != node && parent->
right != node) {
102 if (parent ==
root) {
103 if (parent->
left == node) {
104 parent->
left =
nullptr;
107 parent->
min = sibling->
min;
108 parent->
max = sibling->
max;
111 parent->
right =
nullptr;
114 parent->
min = sibling->
min;
115 parent->
max = sibling->
max;
128 if (grandparent->
left == parent) {
129 grandparent->
left = sibling;
131 grandparent->
right = sibling;
134 sibling->
parent = grandparent;
157 if (is_node_intersect) {
159 result.push_back(node->
value);
170 if (!root_intersects) {
174 float nearest_dist = INFINITY;
175 uint32_t nearest_index = -1;
179 return nearest_index;
185 float leaf_distance = filter(ray_pos, ray_dir, node->
value);
187 if (leaf_distance < nearest_dist) {
188 nearest_dist = leaf_distance;
189 nearest_index = node->
value;
195 float left_distance = INFINITY;
196 float right_distance = INFINITY;
201 if (left_distance < right_distance) {
203 if (left_distance < nearest_dist && left_distance < distance_limit) {
207 if (right_distance < nearest_dist && right_distance < distance_limit) {
213 if (right_distance < nearest_dist && right_distance < distance_limit) {
217 if (left_distance < nearest_dist && left_distance < distance_limit) {
233 callback(node->
value);
255 if (node->
IsLeaf())
return;
260 if (!left_child || !right_child) {
261 assert(node ==
root);
263 if (!left_child && right_child) {
264 node->
min = right_child->
min;
265 node->
max = right_child->
max;
266 }
else if (left_child && !right_child) {
267 node->
min = left_child->
min;
268 node->
max = left_child->
max;
270 node->
min = {0.0f, 0.0f, 0.0f};
271 node->
max = {0.0f, 0.0f, 0.0f};
281 if (node->
parent !=
nullptr) {
285 assert(node->
parent != node);
296 if (node->
left ==
nullptr) {
308 float left_merge_volume =
AABBVolume(left_merge_min, left_merge_max);
309 float right_merge_volume =
AABBVolume(right_merge_min, right_merge_max);
314 if (left_merge_volume < right_merge_volume) {
333 assert((
long long)node > 100);
349 a.x < b.x ? a.x : b.x,
350 a.y < b.y ? a.y : b.y,
351 a.z < b.z ? a.z : b.z
357 a.x > b.x ? a.x : b.x,
358 a.y > b.y ? a.y : b.y,
359 a.z > b.z ? a.z : b.z
364 return min.x <= other_max.x && max.x >= other_min.x &&
365 min.y <= other_max.y && max.y >= other_min.y &&
366 min.z <= other_max.z && max.z >= other_min.z;
370 return (max.x - min.x) * (max.y - min.y) * (max.z - min.z);
374 float x = max.x - min.x;
375 float y = max.y - min.y;
376 float z = max.z - min.z;
378 assert(max.x >= min.x);
379 assert(max.y >= min.y);
380 assert(max.z >= min.z);
382 return 2 * ((x * y) + (x * z) + (y * z));
386 vec3 t1 = (min - ray_pos) / ray_dir;
387 vec3 t2 = (max - ray_pos) / ray_dir;
389 vec3 t1min = glm::min(t1, t2);
390 vec3 t2max = glm::max(t1, t2);
392 float tnear = glm::max(glm::max(t1min.x, t1min.y), t1min.z);
393 float tfar = glm::min(glm::min(t2max.x, t2max.y), t2max.z);
395 return tfar >= tnear;
399 vec3 t1 = (min - ray_pos) / ray_dir;
400 vec3 t2 = (max - ray_pos) / ray_dir;
402 vec3 t1min = glm::min(t1, t2);
403 vec3 t2max = glm::max(t1, t2);
405 float tnear = glm::max(glm::max(t1min.x, t1min.y), t1min.z);
406 float tfar = glm::min(glm::min(t2max.x, t2max.y), t2max.z);
408 return tfar >= tnear ? tnear : INFINITY;
428 Node*
root =
new Node {
nullptr,
nullptr,
nullptr, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}};
Node * InsertLeaf(uint32_t value, vec3 min, vec3 max)
Definition: aabb.h:29
static bool AABBIntersect(vec3 ray_pos, vec3 ray_dir, vec3 min, vec3 max)
Definition: aabb.h:385
~AABBTree()
Definition: aabb.h:19
void FindIntersection(vec3 ray_pos, vec3 ray_dir, Node *node, std::vector< uint32_t > &result) const
Definition: aabb.h:154
void UpdateParentAABB(Node *node)
Definition: aabb.h:251
void ValidateTree(Node *node)
Definition: aabb.h:322
void FindAABBIntersection(Node *node, vec3 min, vec3 max, auto callback)
Definition: aabb.h:230
AABBTree()
Definition: aabb.h:18
vec3 GetAABBMax()
Definition: aabb.h:25
void FindIntersectionRecursive(vec3 ray_pos, vec3 ray_dir, float &nearest_dist, uint32_t &nearest_index, float distance_limit, Node *node, auto filter) const
Definition: aabb.h:183
Node * root
Definition: aabb.h:428
static float AABBVolume(vec3 min, vec3 max)
Definition: aabb.h:369
void FindAABBIntersection(vec3 min, vec3 max, auto callback)
Definition: aabb.h:225
static vec3 MergeAABBMax(vec3 a, vec3 b)
Definition: aabb.h:355
static float AABBDistance(vec3 ray_pos, vec3 ray_dir, vec3 min, vec3 max)
Definition: aabb.h:398
void RemoveLeaf(Node *node)
Definition: aabb.h:92
void RemoveHierarchy(Node *node)
Definition: aabb.h:144
static vec3 MergeAABBMin(vec3 a, vec3 b)
Definition: aabb.h:347
Node * FindSibling(vec3 min, vec3 max, Node *node)
Definition: aabb.h:289
vec3 GetAABBMin()
Definition: aabb.h:24
uint32_t FindIntersection(vec3 ray_pos, vec3 ray_dir, float distance_limit, auto filter) const
Definition: aabb.h:167
void ValidateTree(Node *node, size_t num)
Definition: aabb.h:331
static float AABBSurface(vec3 min, vec3 max)
Definition: aabb.h:373
static bool AABBOverlap(vec3 min, vec3 max, vec3 other_min, vec3 other_max)
Definition: aabb.h:363
glm::vec3 vec3
Definition: math.h:11
Node * left
Definition: aabb.h:417
Node * right
Definition: aabb.h:421
bool IsLeaf() const
Definition: aabb.h:412
Node * parent
Definition: aabb.h:422
vec3 max
Definition: aabb.h:425
vec3 min
Definition: aabb.h:424
uint32_t value
Definition: aabb.h:418
void Print() const
Definition: aabb.h:414