-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOctree.cpp
369 lines (318 loc) · 8.72 KB
/
Octree.cpp
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//////
//
// Includes
//
// Local includes
#include <vector>
#include <map>
#include <stdexcept>
// Local includes
#include "Ray.h"
#include "HitTest/vector.h"
#include "HitTest/hittest.h"
// Implemented header
#include "Octree.h"
//////
//
// Default namespaces
//
// Implemented namespaces
using namespace htest;
//////
//
// Helper functions
//
template <class flt_type>
SAABox<flt_type> toSAABox(const AABox<flt_type> &aabox)
{
SAABox<flt_type> ret;
ret.pmin.x = aabox.pmin.x(); ret.pmin.y = aabox.pmin.y(); ret.pmin.z = aabox.pmin.z();
ret.pmax.x = aabox.pmax.x(); ret.pmax.y = aabox.pmax.y(); ret.pmax.z = aabox.pmax.z();
ret.updateSecondaryData();
return std::move(ret);
}
template <class flt_type>
AABox<flt_type> toAABox(const SAABox<flt_type> &aabox)
{
AABox<flt_type> ret;
ret.pmin.x() = aabox.pmin.x; ret.pmin.y() = aabox.pmin.y; ret.pmin.z() = aabox.pmin.z;
ret.pmax.x() = aabox.pmax.x; ret.pmax.y() = aabox.pmax.y; ret.pmax.z() = aabox.pmax.z;
return std::move(ret);
}
//////
//
// Interface pre-implementation
//
// IOctreeClient
//
template <class flt_type>
IOctreeClient<flt_type>::~IOctreeClient() {}
//////
//
// Class implementation
//
// Octree
//
template <class flt_type>
Octree<flt_type>::Octree(
const IOctreeClient<real> &client, unsigned maxDepth, unsigned targetOccupancy
)
: client(client), maxDepth(maxDepth), targetOccupancy(targetOccupancy),
children{nullptr}
{
}
template <class flt_type>
Octree<flt_type>::Octree(
const IOctreeClient<real> &client, const IdArray &triangles,
const AABox<real> &meshExtend, unsigned maxDepth, unsigned targetOccupancy
)
: client(client), maxDepth(maxDepth), targetOccupancy(targetOccupancy),
children{nullptr}
{
build(triangles, meshExtend);
}
template <class flt_type>
Octree<flt_type>::~Octree()
{
clear();
}
template <class flt_type>
void Octree<flt_type>::calculateChildBoxes(void)
{
Vec3 midpoint = box.centroid();
// Lower-left-front
child_box[0].pmin = box.pmin;
//-
child_box[0].pmax = midpoint;
// Lower-right-front
child_box[1].pmin.x() = midpoint.x();
child_box[1].pmin.y() = box.pmin.y();
child_box[1].pmin.z() = box.pmin.z();
//-
child_box[1].pmax.x() = box.pmax.x();
child_box[1].pmax.y() = midpoint.y();
child_box[1].pmax.z() = midpoint.z();
// Upper-left-front
child_box[2].pmin.x() = box.pmin.x();
child_box[2].pmin.y() = midpoint.y();
child_box[2].pmin.z() = box.pmin.z();
//-
child_box[2].pmax.x() = midpoint.x();
child_box[2].pmax.y() = box.pmax.y();
child_box[2].pmax.z() = midpoint.z();
// Upper-right-front
child_box[3].pmin.x() = midpoint.x();
child_box[3].pmin.y() = midpoint.y();
child_box[3].pmin.z() = box.pmin.z();
//-
child_box[3].pmax.x() = box.pmax.x();
child_box[3].pmax.y() = box.pmax.y();
child_box[3].pmax.z() = midpoint.z();
// Lower-left-back
child_box[4].pmin.x() = box.pmin.x();
child_box[4].pmin.y() = box.pmin.y();
child_box[4].pmin.z() = midpoint.z();
//-
child_box[4].pmax.x() = midpoint.x();
child_box[4].pmax.y() = midpoint.y();
child_box[4].pmax.z() = box.pmax.z();
// Lower-right-back
child_box[5].pmin.x() = midpoint.x();
child_box[5].pmin.y() = box.pmin.y();
child_box[5].pmin.z() = midpoint.z();
//-
child_box[5].pmax.x() = box.pmax.x();
child_box[5].pmax.y() = midpoint.y();
child_box[5].pmax.z() = box.pmax.z();
// Upper-left-back
child_box[6].pmin.x() = box.pmin.x();
child_box[6].pmin.y() = midpoint.y();
child_box[6].pmin.z() = midpoint.z();
//-
child_box[6].pmax.x() = midpoint.x();
child_box[6].pmax.y() = box.pmax.y();
child_box[6].pmax.z() = box.pmax.z();
// Upper-right-back
child_box[7].pmin = midpoint;
//-
child_box[7].pmax = box.pmax;
}
template <class flt_type>
void Octree<flt_type>::insertTriangle (
unsigned id, const Vec3 &p0, const Vec3 &p1, const Vec3 &p2, const Vec3 &normal
)
{
// Common init: convert box formats for HitTest API conformance
SAABox<real> node = std::move(toSAABox(box));
SAABox<real> child_node [8] = {
std::move(toSAABox(child_box[0])), std::move(toSAABox(child_box[1])),
std::move(toSAABox(child_box[2])), std::move(toSAABox(child_box[3])),
std::move(toSAABox(child_box[4])), std::move(toSAABox(child_box[5])),
std::move(toSAABox(child_box[6])), std::move(toSAABox(child_box[7]))
};
if (isEmpty())
{
// First ever triangle in the tree
if (!_<real>::boxTriTest(node, p0.data(), p1.data(), p2.data(), normal.data()))
// Sanity check
throw std::runtime_error("[Octree] !!!INTERNAL ERROR!!!");
occupants.push_back(id);
return;
}
IdArray adds;
if (occupants.size() > 0)
{
// Node is currently (still) a leaf
if (occupants.size() < targetOccupancy || maxDepth < 2)
{
// Leaf not yet full
occupants.push_back(id);
return;
}
// Leaf now full, subdivide
adds = std::move(occupants);
}
adds.push_back(id);
// Insert triangles
for (unsigned i=0; i<adds.size(); i++)
{
Vec3 _p0, _p1, _p2, _normal;
client.queryTriangle(&_p0, &_p1, &_p2, &_normal, adds[i]);
for (unsigned j=0; j<8; j++)
{
if (_<real>::boxTriTest(
child_node[j], _p0.data(), _p1.data(), _p2.data(), _normal.data()
))
{
if (!children[j])
{
children[j] = new Octree<real>(client, maxDepth-1, targetOccupancy);
children[j]->box = std::move(toAABox(child_node[j]));
children[j]->calculateChildBoxes();
}
children[j]->insertTriangle(adds[i], _p0, _p1, _p2, _normal);
}
}
}
}
template <class flt_type>
void Octree<flt_type>::build(const IdArray &triangles, const AABox<real> &meshExtend)
{
// Check that not already built
if (box.valid())
throw std::move(
std::runtime_error("[Octree] Cannot call build() on already built tree!")
);
// Determine root node size
Vec3 halfsize(std::move(meshExtend.extent()));
if (halfsize.x() >= halfsize.y() && halfsize.x() >= halfsize.z())
halfsize.z() = halfsize.y() = halfsize.x() = real(0.5)*halfsize.x();
else if (halfsize.y() >= halfsize.x() && halfsize.y() >= halfsize.z())
halfsize.z() = halfsize.y() = halfsize.x() = real(0.5)*halfsize.y();
else
halfsize.z() = halfsize.y() = halfsize.x() = real(0.5)*halfsize.z();
// Determine root node axis-aligned box
Vec3 center = std::move(meshExtend.centroid());
box.pmin = center - halfsize;
box.pmax = center + halfsize;
// Sanity check
if (!box.valid())
throw std::move(
std::runtime_error("[Octree] Mesh bounding box must not be degenerate!")
);
// Build up child boxes
calculateChildBoxes();
// Add triangles
for (unsigned i=0; i<triangles.size(); i++)
{
Vec3 p0, p1, p2, normal;
client.queryTriangle(&p0, &p1, &p2, &normal, triangles[i]);
insertTriangle(triangles[i], p0, p1, p2, normal);
}
}
template <class flt_type>
bool Octree<flt_type>::queryRay (RayIdMap *IDs, const Ray<real> &ray)
const
{
// Common init
SAABox<real> node = std::move(toSAABox(box));
Vec3 orig(
ray.origin.x() / ray.origin.w(),
ray.origin.y() / ray.origin.w(),
ray.origin.z() / ray.origin.w()
), isect;
real t = Constants<real>::inf;
// Check if ray even hits this sub tree
if (_<real>::boxRayIntersection(isect.data(), &t, node, orig.data(), ray.direction.data()))
{
// Convenience shorthand
RayIdMap &ids = *IDs;
// Check if leaf node
if (!occupants.empty())
{
RayIdMap::iterator at = ids.find(t);
if (at != ids.end())
{
at->second.insert(
at->second.end(), occupants.begin(), occupants.end()
);
}
else
ids.insert(std::move(std::make_pair(t, occupants)));
return true;
}
// Query child nodes
bool leaf_hit = false;
for (unsigned i=0; i<8; i++)
if (children[i])
leaf_hit = children[i]->queryRay(IDs, ray) || leaf_hit;
return leaf_hit;
}
return false;
}
template <class flt_type>
void Octree<flt_type>::clear (void)
{
if (occupants.empty())
{
if (children[0]) delete children[0];
if (children[1]) delete children[1];
if (children[2]) delete children[2];
if (children[3]) delete children[3];
if (children[4]) delete children[4];
if (children[5]) delete children[5];
if (children[6]) delete children[6];
if (children[7]) delete children[7];
}
else
occupants.clear();
}
template <class flt_type>
bool Octree<flt_type>::isEmpty (void)
{
return
// In case of non-leaf node
!(children[0] || children[1] || children[2] || children[3] || children[4] ||
children[5] || children[6] || children[7] ||
// In case of leaf
!occupants.empty());
}
template <class flt_type>
bool Octree<flt_type>::isNotEmpty(void)
{
return
// In case of non-leaf node
(children[0] || children[1] || children[2] || children[3] || children[4] ||
children[5] || children[6] || children[7] ||
// In case of leaf
occupants.empty());
}
//////
//
// Explicit template instantiations
//
// Only floating point variants are intended
template APISPEC IOctreeClient<float>;
template APISPEC IOctreeClient<double>;
template APISPEC Octree<float>;
template APISPEC Octree<double>;