3#ifndef TRAM_SDK_TEMPLATES_OCTREE_H
4#define TRAM_SDK_TEMPLATES_OCTREE_H
25 return nodes.index(new_leaf);
38 for (
int i = 0 ; i < search.
found ; i++) {
51 uint32_t
AddLeaf(T type,
float x,
float y,
float z){
52 return Insert({x, y, z}, type);
60 return Find(result, {x, y, z});
76 Node*
octants[8] = {
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr};
93 for (
int i = 0; i < 8; i++)
if (
octants[i])
return true;
110 Node* existing_leaf = octant;
112 if (glm::distance(existing_leaf->
point, leaf->
point) == 0.0f) {
113 std::cout <<
"Point " << leaf->
point.x <<
" " << leaf->
point.y <<
" " << leaf->
point.z <<
" already in tree!" << std::endl;
117 octant =
NewNode(octant_type, parent);
119 Insert(octant, existing_leaf);
130 if (!node->
parent)
return;
133 for (
int i = 0; i < 8; i++) {
146 float distance[4] = {INFINITY, INFINITY, INFINITY, INFINITY};
158 for (
int i = 0 ; i < 8; i++) {
159 if (i != first_octant)
FindNearest(search, parent_node, i);
166 if (!search_node)
return;
168 if (search_node->
IsNode()) {
170 if (search->
found >= 4) {
177 if (search->
found < 4) {
178 float distance = glm::distance(search_node->
point, search->
point);
190 float distance = glm::distance(search_node->
point, search->
point);
200 for (
int i = 0; i < 4; i++) {
265 if (point.y < mid.y) {
266 if (point.x < mid.x) {
267 if (point.z < mid.z) {
273 if (point.z < mid.z) {
280 if (point.x < mid.x) {
281 if (point.z < mid.z) {
287 if (point.z < mid.z) {
323 for (
int i = 0 ; i < 8; i++) {
void Insert(Node *parent, Node *leaf)
Definition: octree.h:102
void Draw()
Definition: octree.h:45
Pool< Node > nodes
Definition: octree.h:98
Node * root
Definition: octree.h:100
void Draw(Node *node)
Definition: octree.h:297
void FindNearest(NearestSearch *search, Node *parent_node, int octant)
Definition: octree.h:163
Node * NewNode(Octant octant, Node *parent)
Definition: octree.h:210
void Remove(Node *node)
Definition: octree.h:128
size_t Find(T *array, vec3 point)
Definition: octree.h:32
Octant
Definition: octree.h:64
@ OCTANT_BOTTOM_RIGHT_FRONT
Definition: octree.h:72
@ OCTANT_BOTTOM_LEFT_BACK
Definition: octree.h:69
@ OCTANT_TOP_RIGHT_FRONT
Definition: octree.h:68
@ OCTANT_TOP_RIGHT_BACK
Definition: octree.h:67
@ OCTANT_TOP_LEFT_BACK
Definition: octree.h:65
@ OCTANT_BOTTOM_LEFT_FRONT
Definition: octree.h:70
@ OCTANT_BOTTOM_RIGHT_BACK
Definition: octree.h:71
@ OCTANT_TOP_LEFT_FRONT
Definition: octree.h:66
void Remove(uint32_t node)
Definition: octree.h:28
Octant GetOctant(vec3 mid, vec3 point)
Definition: octree.h:264
Octree(std::string name, size_t size)
Definition: octree.h:16
void RemoveLeaf(uint32_t leaf_id)
Definition: octree.h:55
uint32_t AddLeaf(T type, float x, float y, float z)
Definition: octree.h:51
size_t FindNearest(T result[], float x, float y, float z)
Definition: octree.h:59
void FindNearest(NearestSearch *search, Node *parent_node)
Definition: octree.h:153
uint32_t Insert(vec3 point, T data)
Definition: octree.h:22
glm::vec3 vec3
Definition: math.h:11
int found
Definition: octree.h:150
Node * nearest[4]
Definition: octree.h:145
float distance[4]
Definition: octree.h:146
float farthest_distance
Definition: octree.h:148
int farthest_index
Definition: octree.h:147
vec3 point
Definition: octree.h:149
Node * parent
Definition: octree.h:78
bool IsNode()
Definition: octree.h:92
float half_extent
Definition: octree.h:82
vec3 point
Definition: octree.h:84
Node * octants[8]
Definition: octree.h:76
vec3 mid_point
Definition: octree.h:80
bool IsLeaf()
Definition: octree.h:88
T data
Definition: octree.h:86