-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoctree_v1.h
194 lines (168 loc) · 5.77 KB
/
octree_v1.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
VERLET PHYSISC
author: Paolo Bonomi
Real-Time Graphics Programming's Project - 2019/2020
*/
#pragma once
#include <glm/glm.hpp>
#include <vector>
using std::vector;
using std::abs;
using glm::dot;
typedef glm::vec3 vec3;
//In order to use the Octree Spatial Data Structure it's necessary
//to extend the OItem abstract class and implements its methods.
class OItem
{ public:
virtual bool isMember(vec3 node_pos, float node_side_size) = 0;
};
template <class T> class Octree
{
public:
enum NodePosition
{
topRightBack, //000 0
topRightFront, //001 1
topLeftBack, //010 2
topLeftFront, //011 3
bottomRightBack, //100 4
bottomRightFront, //101 5
bottomLeftBack, //110 6
bottomLeftFront, //111 7
};
class OctreeNode;
OctreeNode * root;
int m_depth;
public:
Octree(vec3 &position, const float &size, int &depth)
{
root = new OctreeNode(position, size, NULL, -1);
m_depth = depth;
}
void updateTree(vector<T*> items)
{
root->clear();
root->update(items, m_depth);
}
void getLeafsWithObj(vector<OctreeNode*> *result)
{
if(root->m_isLeaf){
if(root->m_items.size() > 1)
result->push_back(root);
return;
}
root->getLeafsWithObj(result);
}
void getLeafs(vector<OctreeNode*> *nodes)
{
if(root->m_isLeaf){
nodes->push_back(root);
return;
}
root->getLeafs(nodes);
}
class OctreeNode
{
public:
vector<T*> m_items;
vector<OctreeNode*> m_subNodes;
float m_side_size;
vec3 m_pos;
OctreeNode * m_parent;
int m_id;
bool m_isLeaf = true;
OctreeNode(){}
OctreeNode(const vec3 &position, const float &size, OctreeNode * parent, int id)
{
m_side_size = size;
m_pos = position;
m_parent = parent;
m_id = id;
}
//we dont take in cosideration the case were the rigidbody are outside the root of the octree
void update(vector<T*> items, const int &depth)
{
if(depth == 0)
{
this->m_items = items;
}
else
{
vector<T*> temp;
vec3 newPos;
for(int i = 0; i < 8; i++)
{ // for each subnode
//find the center of the new subnode
newPos = this->m_pos;
newPos.x = ((i & 2) == 2) ? newPos.x + this->m_side_size*0.25f : newPos.x - this->m_side_size*0.25f;
newPos.y = ((i & 4) == 4) ? newPos.y - this->m_side_size*0.25f : newPos.y + this->m_side_size*0.25f;
newPos.z = ((i & 1) == 1) ? newPos.z + this->m_side_size*0.25f : newPos.z - this->m_side_size*0.25f;
//check thought all items if they are member with the node i
//if its so we add the item reference to the temp list
for(int j = 0; j < items.size(); j++)
if(items.at(j)->isMember(newPos, m_side_size*0.5f)) temp.push_back(items.at(j));
//if only one item is memeber with the subnode. Create the node, add the item,
//but no further recursion is nedeed
if(temp.size() == 1)
{
this->m_isLeaf = false;
this->m_subNodes.push_back(new OctreeNode(newPos, this->m_side_size*0.5f, this, i));
vector<T*>().swap(temp);
//and we pass to the next subnode
continue;
}
//if more then one item is member with the subnode -> create the subnode
//and recursively update the branch
if(temp.size() > 1)
{
this->m_isLeaf = false;
this->m_subNodes.push_back(new OctreeNode(newPos, this->m_side_size*0.5f, this, i));
this->m_subNodes.back()->update(temp, depth-1);
//we empty the temp vector
vector<T*>().swap(temp);
//and we pass to the next subdnode
continue;
}
vector<T*>().swap(temp);
//if we are here temp.size() is equal to 0, we pass to the next node.
}
}
}
bool isLeaf()
{
return m_isLeaf;
}
void getLeafsWithObj(vector<OctreeNode*> *result)
{
for(int i = 0; i < this->m_subNodes.size(); i++)
{
if(this->m_subNodes.at(i)->isLeaf())
{
if(this->m_subNodes.at(i)->m_items.size() > 1)
result->push_back(this->m_subNodes.at(i));
}
else
this->m_subNodes.at(i)->getLeafsWithObj(result);
}
}
void getLeafs(vector<OctreeNode*> *nodes)
{
for(int i = 0; i < this->m_subNodes.size(); i++)
{
if(this->m_subNodes.at(i)->isLeaf())
nodes->push_back(this->m_subNodes.at(i));
else
this->m_subNodes.at(i)->getLeafs(nodes);
}
}
void clear()
{
for(int i = 0; i < m_subNodes.size(); i++)
{
m_subNodes.at(i)->clear();
}
vector<OctreeNode*>().swap(m_subNodes);
vector<T*>().swap(m_items);
}
};
};