-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKdTree.inl.hpp
520 lines (459 loc) · 17.9 KB
/
KdTree.inl.hpp
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
namespace Tvl {
/*enum class KdChild {
LEFT = 0,
RIGHT = 1,
};
template <typename TNode, typename TMetric>
void KdTree<TNode, TMetric>::buildImpl(IScheduler& scheduler, ArrayView<const Vector> points) {
VERBOSE_LOG
static_assert(sizeof(LeafNode<TNode>) == sizeof(InnerNode<TNode>), "Sizes of nodes must match");
// clean the current tree
const Size currentCnt = nodes.size();
this->init();
for (const auto& i : iterateWithIndex(points)) {
entireBox.extend(i.value());
idxs.push(i.index());
}
if (SPH_UNLIKELY(points.empty())) {
return;
}
const Size nodeCnt = max(2 * points.size() / config.leafSize + 1, currentCnt);
nodes.resize(nodeCnt);
SharedPtr<ITask> rootTask = scheduler.submit([this, &scheduler, points] {
this->buildTree(scheduler, ROOT_PARENT_NODE, KdChild(-1), 0, points.size(), entireBox, 0, 0);
});
rootTask->wait();
// shrink nodes to only the constructed ones
nodes.resize(nodeCounter);
ASSERT(this->sanityCheck(), this->sanityCheck().error());
}
template <typename TNode, typename TMetric>
void KdTree<TNode, TMetric>::buildTree(IScheduler& scheduler,
const Size parent,
const KdChild child,
const Size from,
const Size to,
const Box& box,
const Size slidingCnt,
const Size depth) {
Box box1, box2;
Vector boxSize = box.size();
// split by the dimension of largest extent
Size splitIdx = argMax(boxSize);
bool slidingMidpoint = false;
bool degeneratedBox = false;
if (to - from <= config.leafSize) {
// enough points to fit inside one leaf
this->addLeaf(parent, child, from, to);
return;
} else {
// check for singularity of dimensions
for (Size dim = 0; dim < 3; ++dim) {
if (this->isSingular(from, to, splitIdx)) {
boxSize[splitIdx] = 0.f;
// find new largest dimension
splitIdx = argMax(boxSize);
if (boxSize == Vector(0._f)) {
// too many overlapping points, just split until they fit within a leaf,
// the code can handle this case, but it smells with an error ...
ASSERT(false, "Too many overlapping points, something is probably wrong ...");
degeneratedBox = true;
break;
}
} else {
break;
}
}
// split around center of the box
Float splitPosition = box.center()[splitIdx];
std::make_signed_t<Size> n1 = from, n2 = to - 1; // use ints for easier for loop ending with 0
if (slidingCnt <= 5 && !degeneratedBox) {
for (;; std::swap(idxs[n1], idxs[n2])) {
for (; n1 < int(to) && this->values[idxs[n1]][splitIdx] <= splitPosition; ++n1)
;
for (; n2 >= int(from) && this->values[idxs[n2]][splitIdx] >= splitPosition; --n2)
;
if (n1 >= n2) {
break;
}
}
if (n1 == int(from)) {
Size idx = from;
splitPosition = this->values[idxs[from]][splitIdx];
for (Size i = from + 1; i < to; ++i) {
const Float x1 = this->values[idxs[i]][splitIdx];
if (x1 < splitPosition) {
idx = i;
splitPosition = x1;
}
}
std::swap(idxs[from], idxs[idx]);
n1++;
slidingMidpoint = true;
} else if (n1 == int(to)) {
Size idx = from;
splitPosition = this->values[idxs[from]][splitIdx];
for (Size i = from + 1; i < to; ++i) {
const Float x2 = this->values[idxs[i]][splitIdx];
if (x2 > splitPosition) {
idx = i;
splitPosition = x2;
}
}
std::swap(idxs[to - 1], idxs[idx]);
n1--;
slidingMidpoint = true;
}
tie(box1, box2) = box.split(splitIdx, splitPosition);
} else {
n1 = (from + to) >> 1;
// do quick select to sort elements around the midpoint
Iterator<Size> iter = idxs.begin();
if (!degeneratedBox) {
std::nth_element(iter + from, iter + n1, iter + to, [this, splitIdx](Size i1, Size i2) {
return this->values[i1][splitIdx] < this->values[i2][splitIdx];
});
}
tie(box1, box2) = box.split(splitIdx, this->values[idxs[n1]][splitIdx]);
}
// sanity check
ASSERT(this->checkBoxes(from, to, n1, box1, box2));
// add inner node and connect it to the parent
const Size index = this->addInner(parent, child, splitPosition, splitIdx);
// recurse to left and right subtree
const Size nextSlidingCnt = slidingMidpoint ? slidingCnt + 1 : 0;
auto processRightSubTree = [this, &scheduler, index, to, n1, box2, nextSlidingCnt, depth] {
this->buildTree(scheduler, index, KdChild::RIGHT, n1, to, box2, nextSlidingCnt, depth + 1);
};
if (depth < config.maxParallelDepth) {
// ad hoc decision - split the build only for few topmost nodes, there is no point in splitting
// the work for child node in the bottom, it would only overburden the ThreadPool.
scheduler.submit(processRightSubTree);
} else {
// otherwise simply process both subtrees in the same thread
processRightSubTree();
}
this->buildTree(scheduler, index, KdChild::LEFT, from, n1, box1, nextSlidingCnt, depth + 1);
}
}
template <typename TNode, typename TMetric>
void KdTree<TNode, TMetric>::addLeaf(const Size parent, const KdChild child, const Size from, const Size to) {
const Size index = nodeCounter++;
if (index >= nodes.size()) {
// needs more nodes than estimated; allocate up to 2x more than necessary to avoid frequent
// reallocations
nodesMutex.lock();
nodes.resize(max(2 * index, nodes.size()));
nodesMutex.unlock();
}
nodesMutex.lock_shared();
auto releaseLock = finally([this] { nodesMutex.unlock_shared(); });
LeafNode<TNode>& node = (LeafNode<TNode>&)nodes[index];
node.type = KdNode::Type::LEAF;
ASSERT(node.isLeaf());
#ifdef SPH_DEBUG
node.from = node.to = -1;
#endif
node.from = from;
node.to = to;
// find the bounding box of the leaf
Box box;
for (Size i = from; i < to; ++i) {
box.extend(this->values[idxs[i]]);
}
node.box = box;
if (parent == ROOT_PARENT_NODE) {
return;
}
InnerNode<TNode>& parentNode = (InnerNode<TNode>&)nodes[parent];
ASSERT(!parentNode.isLeaf());
if (child == KdChild::LEFT) {
// left child
parentNode.left = index;
} else {
ASSERT(child == KdChild::RIGHT);
// right child
parentNode.right = index;
}
}
template <typename TNode, typename TMetric>
Size KdTree<TNode, TMetric>::addInner(const Size parent,
const KdChild child,
const Float splitPosition,
const Size splitIdx) {
static_assert(int(KdNode::Type::X) == 0 && int(KdNode::Type::Y) == 1 && int(KdNode::Type::Z) == 2,
"Invalid values of KdNode::Type enum");
const Size index = nodeCounter++;
if (index >= nodes.size()) {
// needs more nodes than estimated; allocate up to 2x more than necessary to avoid frequent
// reallocations
nodesMutex.lock();
nodes.resize(max(2 * index, nodes.size()));
nodesMutex.unlock();
}
nodesMutex.lock_shared();
auto releaseLock = finally([this] { nodesMutex.unlock_shared(); });
InnerNode<TNode>& node = (InnerNode<TNode>&)nodes[index];
node.type = KdNode::Type(splitIdx);
ASSERT(!node.isLeaf());
#ifdef SPH_DEBUG
node.left = node.right = -1;
node.box = Box(); // will be computed later
#endif
node.splitPosition = float(splitPosition);
if (parent == ROOT_PARENT_NODE) {
// no need to set up parents
return index;
}
InnerNode<TNode>& parentNode = (InnerNode<TNode>&)nodes[parent];
if (child == KdChild::LEFT) {
// left child
ASSERT(parentNode.left == Size(-1));
parentNode.left = index;
} else {
ASSERT(child == KdChild::RIGHT);
// right child
ASSERT(parentNode.right == Size(-1));
parentNode.right = index;
}
return index;
}
template <typename TNode, typename TMetric>
void KdTree<TNode, TMetric>::init() {
entireBox = Box();
idxs.clear();
nodes.clear();
nodeCounter = 0;
}
template <typename TNode, typename TMetric>
bool KdTree<TNode, TMetric>::isSingular(const Size from, const Size to, const Size splitIdx) const {
for (Size i = from; i < to; ++i) {
if (this->values[idxs[i]][splitIdx] != this->values[idxs[to - 1]][splitIdx]) {
return false;
}
}
return true;
}
template <typename TNode, typename TMetric>
bool KdTree<TNode, TMetric>::checkBoxes(const Size from,
const Size to,
const Size mid,
const Box& box1,
const Box& box2) const {
for (Size i = from; i < to; ++i) {
if (i < mid && !box1.contains(this->values[idxs[i]])) {
return false;
}
if (i >= mid && !box2.contains(this->values[idxs[i]])) {
return false;
}
}
return true;
}
/// \brief Object used during traversal.
///
/// Holds an index of the node and squared distance of the bounding box.
struct ProcessedNode {
/// Index into the nodeStack array. We cannot use pointers because the array might get reallocated.
Size idx;
Vector sizeSqr;
Float distanceSqr;
};
/// \brief Cached stack to avoid reallocation
///
/// It is thread_local to allow using KdTree from multiple threads
extern thread_local Array<ProcessedNode> nodeStack;
template <typename TNode, typename TMetric>
template <bool FindAll>
Size KdTree<TNode, TMetric>::find(const Vector& r0,
const Size index,
const Float radius,
Array<NeighbourRecord>& neighbours) const {
ASSERT(neighbours.empty());
const Float radiusSqr = sqr(radius);
const Vector maxDistSqr = sqr(max(Vector(0._f), entireBox.lower() - r0, r0 - entireBox.upper()));
// L1 norm
const Float l1 = l1Norm(maxDistSqr);
ProcessedNode node{ 0, maxDistSqr, l1 };
ASSERT(nodeStack.empty()); // not sure if there can be some nodes from previous search ...
TMetric metric;
while (node.distanceSqr < radiusSqr) {
if (nodes[node.idx].isLeaf()) {
// for leaf just add all
const LeafNode<TNode>& leaf = (const LeafNode<TNode>&)nodes[node.idx];
if (leaf.size() > 0) {
const Float leafDistSqr =
metric(max(Vector(0._f), leaf.box.lower() - r0, r0 - leaf.box.upper()));
if (leafDistSqr < radiusSqr) {
// leaf intersects the sphere
for (Size i = leaf.from; i < leaf.to; ++i) {
const Size actIndex = idxs[i];
const Float distSqr = metric(this->values[actIndex] - r0);
if (distSqr < radiusSqr && (FindAll || this->rank[actIndex] < this->rank[index])) {
/// \todo order part
neighbours.push(NeighbourRecord{ actIndex, distSqr });
}
}
}
}
if (nodeStack.empty()) {
break;
}
node = nodeStack.pop();
} else {
// inner node
const InnerNode<TNode>& inner = (InnerNode<TNode>&)nodes[node.idx];
const Size splitDimension = Size(inner.type);
ASSERT(splitDimension < 3);
const Float splitPosition = inner.splitPosition;
if (r0[splitDimension] < splitPosition) {
// process left subtree, put right on stack
ProcessedNode right = node;
node.idx = inner.left;
const Float dx = splitPosition - r0[splitDimension];
right.distanceSqr += sqr(dx) - right.sizeSqr[splitDimension];
right.sizeSqr[splitDimension] = sqr(dx);
if (right.distanceSqr < radiusSqr) {
const InnerNode<TNode>& next = (const InnerNode<TNode>&)nodes[right.idx];
right.idx = next.right;
nodeStack.push(right);
}
} else {
// process right subtree, put left on stack
ProcessedNode left = node;
node.idx = inner.right;
const Float dx = splitPosition - r0[splitDimension];
left.distanceSqr += sqr(dx) - left.sizeSqr[splitDimension];
left.sizeSqr[splitDimension] = sqr(dx);
if (left.distanceSqr < radiusSqr) {
const InnerNode<TNode>& next = (const InnerNode<TNode>&)nodes[left.idx];
left.idx = next.left;
nodeStack.push(left);
}
}
}
}
return neighbours.size();
}
template <typename TNode, typename TMetric>
Outcome KdTree<TNode, TMetric>::sanityCheck() const {
if (this->values.size() != idxs.size()) {
return makeFailed("Number of values does not match the number of indices");
}
// check bounding box
for (const Vector& v : this->values) {
if (!entireBox.contains(v)) {
return makeFailed("Points are not strictly within the bounding box");
}
}
// check node connectivity
Size counter = 0;
std::set<Size> indices;
Function<Outcome(const Size idx)> countNodes = [this, &indices, &counter, &countNodes](
const Size idx) -> Outcome {
// count this
counter++;
// check index validity
if (idx >= nodes.size()) {
return makeFailed("Invalid index found: ", idx, " (", nodes.size(), ")");
}
// if inner node, count children
if (!nodes[idx].isLeaf()) {
const InnerNode<TNode>& inner = (const InnerNode<TNode>&)nodes[idx];
return countNodes(inner.left) && countNodes(inner.right);
} else {
// check that all points fit inside the bounding box of the leaf
const LeafNode<TNode>& leaf = (const LeafNode<TNode>&)nodes[idx];
if (leaf.to == leaf.from) {
// empty leaf?
return makeFailed("Empty leaf: ", leaf.to);
}
for (Size i = leaf.from; i < leaf.to; ++i) {
if (!leaf.box.contains(this->values[idxs[i]])) {
return makeFailed("Leaf points do not fit inside the bounding box");
}
if (indices.find(i) != indices.end()) {
// child referenced twice?
return makeFailed("Index repeated: ", i);
}
indices.insert(i);
}
}
return SUCCESS;
};
const Outcome result = countNodes(0);
if (!result) {
return result;
}
// we should count exactly nodes.size()
if (counter != nodes.size()) {
return makeFailed("Unexpected number of nodes: ", counter, " == ", nodes.size());
}
// each index should have been inserted exactly once
Size i = 0;
for (Size idx : indices) {
// std::set is sorted, so we can check sequentially
if (idx != i) {
return makeFailed("Invalid index: ", idx, " == ", i);
}
++i;
}
return SUCCESS;
}
template <IterateDirection Dir, typename TNode, typename TMetric, typename TFunctor>
void iterateTree(KdTree<TNode, TMetric>& tree,
IScheduler& scheduler,
const TFunctor& functor,
const Size nodeIdx,
const Size depthLimit) {
TNode& node = tree.getNode(nodeIdx);
if (Dir == IterateDirection::TOP_DOWN) {
if (node.isLeaf()) {
functor(node, nullptr, nullptr);
} else {
InnerNode<TNode>& inner = reinterpret_cast<InnerNode<TNode>&>(node);
if (!functor(inner, &tree.getNode(inner.left), &tree.getNode(inner.right))) {
return;
}
}
}
SharedPtr<ITask> task;
if (!node.isLeaf()) {
InnerNode<TNode>& inner = reinterpret_cast<InnerNode<TNode>&>(node);
const Size newDepth = depthLimit == 0 ? 0 : depthLimit - 1;
auto iterateRightSubtree = [&tree, &scheduler, &functor, &inner, newDepth] {
iterateTree<Dir>(tree, scheduler, functor, inner.right, newDepth);
};
if (newDepth > 0) {
task = scheduler.submit(iterateRightSubtree);
} else {
iterateRightSubtree();
}
iterateTree<Dir>(tree, scheduler, functor, inner.left, newDepth);
}
if (task) {
task->wait();
}
if (Dir == IterateDirection::BOTTOM_UP) {
if (node.isLeaf()) {
functor(node, nullptr, nullptr);
} else {
InnerNode<TNode>& inner = reinterpret_cast<InnerNode<TNode>&>(node);
functor(inner, &tree.getNode(inner.left), &tree.getNode(inner.right));
}
}
}
/// \copydoc iterateTree
template <IterateDirection Dir, typename TNode, typename TMetric, typename TFunctor>
void iterateTree(const KdTree<TNode, TMetric>& tree,
IScheduler& scheduler,
const TFunctor& functor,
const Size nodeIdx,
const Size depthLimit) {
// use non-const overload using const_cast, but call the functor with const reference
auto actFunctor = [&functor](TNode& node, TNode* left, TNode* right)
INL { return functor(asConst(node), left, right); };
iterateTree<Dir>(const_cast<KdTree<TNode, TMetric>&>(tree), scheduler, actFunctor, nodeIdx, depthLimit);
}
*/
} // namespace Tvl