4#ifndef TRAM_SDK_TEMPLATES_AABB_H
5#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);
175 if (!left_child || !right_child) {
176 assert(node ==
root);
178 if (!left_child && right_child) {
179 node->
min = right_child->
min;
180 node->
max = right_child->
max;
181 }
else if (left_child && !right_child) {
182 node->
min = left_child->
min;
183 node->
max = left_child->
max;
185 node->
min = {0.0f, 0.0f, 0.0f};
186 node->
max = {0.0f, 0.0f, 0.0f};
196 if (node->
parent !=
nullptr) {
200 assert(node->
parent != node);
211 if (node->
left ==
nullptr) {
223 float left_merge_volume =
AABBVolume(left_merge_min, left_merge_max);
224 float right_merge_volume =
AABBVolume(right_merge_min, right_merge_max);
229 if (left_merge_volume < right_merge_volume) {
248 assert((
long long)node > 100);
264 a.x < b.x ? a.x : b.x,
265 a.y < b.y ? a.y : b.y,
266 a.z < b.z ? a.z : b.z
272 a.x > b.x ? a.x : b.x,
273 a.y > b.y ? a.y : b.y,
274 a.z > b.z ? a.z : b.z
279 return (max.x - min.x) * (max.y - min.y) * (max.z - min.z);
283 float x = max.x - min.x;
284 float y = max.y - min.y;
285 float z = max.z - min.z;
287 assert(max.x >= min.x);
288 assert(max.y >= min.y);
289 assert(max.z >= min.z);
291 return 2 * ((x * y) + (x * z) + (y * z));
295 vec3 t1 = (min - ray_pos) / ray_dir;
296 vec3 t2 = (max - ray_pos) / ray_dir;
298 vec3 t1min = glm::min(t1, t2);
299 vec3 t2max = glm::max(t1, t2);
301 float tnear = glm::max(glm::max(t1min.x, t1min.y), t1min.z);
302 float tfar = glm::min(glm::min(t2max.x, t2max.y), t2max.z);
304 return tfar >= tnear;
324 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:294
~AABBTree()
Definition: aabb.h:20
void UpdateParentAABB(Node *node)
Definition: aabb.h:169
void ValidateTree(Node *node)
Definition: aabb.h:237
AABBTree()
Definition: aabb.h:19
vec3 GetAABBMax()
Definition: aabb.h:25
Node * root
Definition: aabb.h:324
static float AABBVolume(vec3 min, vec3 max)
Definition: aabb.h:278
static vec3 MergeAABBMax(vec3 a, vec3 b)
Definition: aabb.h:270
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:262
Node * FindSibling(vec3 min, vec3 max, Node *node)
Definition: aabb.h:204
vec3 GetAABBMin()
Definition: aabb.h:24
void ValidateTree(Node *node, size_t num)
Definition: aabb.h:246
void FindIntersection(vec3 ray_pos, vec3 ray_dir, Node *node, std::vector< uint32_t > &result)
Definition: aabb.h:154
static float AABBSurface(vec3 min, vec3 max)
Definition: aabb.h:282
glm::vec3 vec3
Definition: math.h:12
Node * left
Definition: aabb.h:313
Node * right
Definition: aabb.h:317
bool IsLeaf() const
Definition: aabb.h:308
Node * parent
Definition: aabb.h:318
vec3 max
Definition: aabb.h:321
vec3 min
Definition: aabb.h:320
uint32_t value
Definition: aabb.h:314
void Print() const
Definition: aabb.h:310