-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOctree.cpp
More file actions
228 lines (215 loc) · 5.42 KB
/
Copy pathOctree.cpp
File metadata and controls
228 lines (215 loc) · 5.42 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "Octree.h"
#include "IOctreeElement.h"
#include <algorithm>
#undef min
#undef max
namespace sh::game
{
SH_GAME_API Octree::Octree(const render::AABB& aabb, std::size_t capacity, uint32_t depth) :
capacity(capacity), depth(depth),
aabb(aabb), root(this)
{
}
SH_GAME_API Octree::Octree(Octree&& other) noexcept :
capacity(other.capacity), depth(other.depth),
aabb(std::move(other.aabb)),
childs(std::move(other.childs)), objs(std::move(other.objs)),
root(other.root == &other ? this : other.root),
parent(other.parent),
maxDepth(other.maxDepth)
{
const auto fixTreeLinks = [&](auto&& self, Octree& node, Octree* treeRoot, Octree* treeParent) -> void
{
node.root = treeRoot;
node.parent = treeParent;
for (auto& child : node.childs)
{
if (!child)
continue;
self(self, *child, treeRoot, &node);
}
};
for (auto& child : childs)
{
if (!child)
continue;
fixTreeLinks(fixTreeLinks, *child, root, this);
}
}
SH_GAME_API Octree::~Octree()
{
}
SH_GAME_API void Octree::Clear()
{
for (auto& child : childs)
child.reset();
objs.clear();
}
SH_GAME_API auto Octree::Query(const glm::vec3& pos) -> Octree*
{
if (IsLeaf())
{
if (aabb.Contains(pos))
return this;
}
else
{
for (int i = 0; i < 8; ++i)
{
if (!childs[i]->aabb.Contains(pos))
continue;
return childs[i]->Query(pos);
}
}
return nullptr;
}
SH_GAME_API auto Octree::Query(IOctreeElement& obj) -> std::vector<Octree*>
{
std::vector<Octree*> resultVec{};
Query(obj, resultVec);
return resultVec;
}
SH_GAME_API auto Octree::Query(const render::AABB& aabb) -> std::vector<IOctreeElement*>
{
std::vector<IOctreeElement*> resultVec{};
Query(aabb, resultVec);
return resultVec;
}
SH_GAME_API void Octree::Query(const IOctreeElement& obj, std::vector<Octree*>& vec)
{
if (IsLeaf())
{
if (obj.Intersect(aabb))
return vec.push_back(this);
}
else
{
for (int i = 0; i < 8; ++i)
{
if (!obj.Intersect(childs[i]->aabb))
continue;
childs[i]->Query(obj, vec);
}
}
}
SH_GAME_API void Octree::Query(const render::AABB& aabb, std::vector<IOctreeElement*>& vec)
{
if (!aabb.Intersects(this->aabb))
return;
for (auto element : objs)
{
if (element->Intersect(aabb))
vec.push_back(element);
}
if (!IsLeaf())
{
for (int i = 0; i < 8; ++i)
{
if (!aabb.Intersects(childs[i]->aabb))
continue;
childs[i]->Query(aabb, vec);
}
}
}
SH_GAME_API auto Octree::Insert(IOctreeElement& obj) -> bool
{
if (!obj.Intersect(aabb))
return false;
if (IsLeaf())
{
if (objs.size() < capacity)
{
if (std::find(objs.begin(), objs.end(), &obj) == objs.end())
objs.push_back(&obj);
return true;
}
else
{
if (depth == maxDepth)
{
if (std::find(objs.begin(), objs.end(), &obj) == objs.end())
objs.push_back(&obj);
return true;
}
Subdivide();
std::vector<IOctreeElement*> prevObjs{};
prevObjs.swap(objs);
for (IOctreeElement* elem : prevObjs)
{
if (!InsertIntoChildren(*elem))
objs.push_back(elem);
}
if (InsertIntoChildren(obj))
return true;
if (std::find(objs.begin(), objs.end(), &obj) == objs.end())
objs.push_back(&obj);
return true;
}
}
if (InsertIntoChildren(obj))
return true;
if (std::find(objs.begin(), objs.end(), &obj) == objs.end())
objs.push_back(&obj);
return true;
}
SH_GAME_API bool Octree::Erase(IOctreeElement& obj)
{
const auto eraseRecursive = [&](auto&& self, Octree& node) -> bool
{
const auto prevSize = node.objs.size();
node.objs.erase(
std::remove(node.objs.begin(), node.objs.end(), &obj),
node.objs.end()
);
bool erased = node.objs.size() != prevSize;
for (auto& child : node.childs)
{
if (!child)
continue;
erased |= self(self, *child);
}
return erased;
};
return eraseRecursive(eraseRecursive, GetRoot());
}
void Octree::Subdivide()
{
if (childs[0])
return;
render::AABB childaabb[8];
Vec3 mid = aabb.GetCenter();
childaabb[0] = render::AABB{ aabb.GetMin(), mid };
childaabb[1] = render::AABB{ Vec3{ mid.x, aabb.GetMin().y, aabb.GetMin().z }, Vec3{ aabb.GetMax().x, mid.y, mid.z } };
childaabb[2] = render::AABB{ Vec3{ aabb.GetMin().x, mid.y, aabb.GetMin().z }, Vec3{ mid.x, aabb.GetMax().y, mid.z } };
childaabb[3] = render::AABB{ Vec3{ mid.x, mid.y, aabb.GetMin().z }, Vec3{ aabb.GetMax().x, aabb.GetMax().y, mid.z } };
childaabb[4] = render::AABB{ Vec3{ aabb.GetMin().x, aabb.GetMin().y, mid.z }, Vec3{ mid.x, mid.y, aabb.GetMax().z } };
childaabb[5] = render::AABB{ Vec3{ mid.x, aabb.GetMin().y, mid.z }, Vec3{ aabb.GetMax().x, mid.y, aabb.GetMax().z } };
childaabb[6] = render::AABB{ Vec3{ aabb.GetMin().x, mid.y, mid.z }, Vec3{ mid.x, aabb.GetMax().y, aabb.GetMax().z } };
childaabb[7] = render::AABB{ mid, aabb.GetMax() };
for (int i = 0; i < 8; ++i)
{
childs[i] = std::make_unique<Octree>(childaabb[i], this->capacity, depth + 1);
childs[i]->parent = this;
childs[i]->root = root;
}
}
auto Octree::InsertIntoChildren(IOctreeElement& obj) -> bool
{
if (childs[0] == nullptr)
return false;
int intersectCount = 0;
int childIdx = -1;
for (int i = 0; i < 8; ++i)
{
if (!obj.Intersect(childs[i]->aabb))
continue;
++intersectCount;
childIdx = i;
if (intersectCount > 1)
return false;
}
if (intersectCount != 1)
return false;
return childs[childIdx]->Insert(obj);
}
}//namespace