-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkdtree.h
More file actions
456 lines (374 loc) · 11.8 KB
/
Copy pathkdtree.h
File metadata and controls
456 lines (374 loc) · 11.8 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
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
///////////////////////////////////////////////////////////////////////////////
// unvell Common Graphics Module (libugm.a)
// Common classes for cross-platform C++ 2D/3D graphics application.
//
// MIT License
// Copyright 2016-2019 Jingwood, unvell.com, all rights reserved.
///////////////////////////////////////////////////////////////////////////////
#pragma once
#ifndef __KDTREE_H__
#define __KDTREE_H__
#include <vector>
#include <functional>
#include <algorithm>
#include <limits>
#include "types2d.h"
#include "types3d.h"
#include "functions.h"
namespace ugm {
template<typename T>
class KDNode {
public:
BoundingBox bbox;
KDNode* left = NULL;
KDNode* right = NULL;
std::vector<T> items;
KDNode() {
}
~KDNode() {
reset();
}
inline BoundingBox boundingBoxFromItems(T* items, size_t itemCount) {
BoundingBox bbox = items[0]->bbox;
for (size_t i = 1; i < itemCount; i++) {
bbox.expandTo(items[i]->bbox);
}
bbox.finalize();
return bbox;
}
void reset() {
if (this->left != NULL) {
delete this->left;
this->left = NULL;
}
if (this->right != NULL) {
delete this->right;
this->right = NULL;
}
this->items.clear();
}
void build(T* items, size_t itemCount, int depth = 0) {
if (itemCount <= 2 || depth >= 32) {
this->items.assign(items, items + itemCount);
this->bbox = boundingBoxFromItems(items, itemCount);
return;
}
// 全体バウンディングボックス
BoundingBox nodeBox = boundingBoxFromItems(items, itemCount);
// 最良の分割コスト
float bestCost = std::numeric_limits<float>::max();
int bestAxis = -1;
float bestSplit = 0.0f;
// 各軸ごとに評価
for (int axis = 0; axis < 3; ++axis) {
// ソート用構造体
std::vector<std::pair<float, T>> sorted;
for (size_t i = 0; i < itemCount; ++i) {
float center = items[i]->bbox.origin[axis];
sorted.emplace_back(center, items[i]);
}
std::sort(sorted.begin(), sorted.end(),
[](const auto& a, const auto& b) { return a.first < b.first; });
std::vector<BoundingBox> leftBoxes(itemCount);
std::vector<BoundingBox> rightBoxes(itemCount);
BoundingBox leftBox, rightBox;
for (size_t i = 0; i < itemCount; ++i) {
leftBox.expandTo(sorted[i].second->bbox);
leftBoxes[i] = leftBox;
}
for (int i = (int)itemCount - 1; i >= 0; --i) {
rightBox.expandTo(sorted[i].second->bbox);
rightBoxes[i] = rightBox;
}
// 各分割点のコストを評価
for (size_t i = 1; i < itemCount; ++i) {
float SA = nodeBox.surfaceArea();
float SAL = leftBoxes[i - 1].surfaceArea();
float SAR = rightBoxes[i].surfaceArea();
int NL = (int)i;
int NR = (int)(itemCount - i);
float cost = (SAL / SA) * NL + (SAR / SA) * NR;
if (cost < bestCost) {
bestCost = cost;
bestAxis = axis;
bestSplit = 0.5f * (sorted[i - 1].first + sorted[i].first);
}
}
}
// 分割失敗 → リーフ
if (bestAxis == -1) {
this->items.assign(items, items + itemCount);
this->bbox = nodeBox;
return;
}
// 左右に振り分け
std::vector<T> ltris, rtris;
for (size_t i = 0; i < itemCount; ++i) {
const T& item = items[i];
if (item->bbox.origin[bestAxis] < bestSplit) {
ltris.push_back(item);
} else {
rtris.push_back(item);
}
}
// リーフ条件
if (ltris.size() == itemCount || rtris.size() == itemCount) {
this->items.assign(items, items + itemCount);
this->bbox = nodeBox;
return;
}
// 再帰構築
this->left = new KDNode();
this->left->build(ltris.data(), ltris.size(), depth + 1);
this->right = new KDNode();
this->right->build(rtris.data(), rtris.size(), depth + 1);
this->bbox = nodeBox;
}
// bool iterate(const Ray& ray, std::function<bool(T)> iterator) const {
// for (const auto& t : this->items) {
// const bool res = iterator(t);
// if (!res) return false;
// }
//
// if (this->left != NULL && this->left->bbox.intersects(ray)) {
// const bool res = this->left->iterate(ray, iterator);
// if (!res) return false;
// }
//
// if (this->right != NULL && this->right->bbox.intersects(ray)) {
// const bool res = this->right->iterate(ray, iterator);
// if (!res) return false;
// }
//
// return true;
// }
bool iterate(const Ray& ray, std::function<bool(T)> iterator) const {
// 現在のノード内のアイテムに対して処理
for (const auto& t : this->items) {
if (iterator(t)) return true; // true で終了
}
// 左右ノードの距離計算
float tminLeft = 0.0f, tmaxLeft = 0.0f;
float tminRight = 0.0f, tmaxRight = 0.0f;
bool hitLeft = this->left && this->left->bbox.intersects(ray, tminLeft, tmaxLeft);
bool hitRight = this->right && this->right->bbox.intersects(ray, tminRight, tmaxRight);
if (hitLeft && hitRight) {
// 距離の近い方から探索
if (tminLeft < tminRight) {
if (this->left->iterate(ray, iterator)) return true;
if (this->right->iterate(ray, iterator)) return true;
} else {
if (this->right->iterate(ray, iterator)) return true;
if (this->left->iterate(ray, iterator)) return true;
}
}
else if (hitLeft) {
if (this->left->iterate(ray, iterator)) return true;
}
else if (hitRight) {
if (this->right->iterate(ray, iterator)) return true;
}
return false; // 最後まで終了しなければ false
}
};
template<typename T>
class KDNode2D {
public:
BBox2D bbox;
KDNode2D* left = NULL;
KDNode2D* right = NULL;
std::vector<T> items;
KDNode2D() {
}
~KDNode2D() {
reset();
}
inline BBox2D boundingBoxFromItems(T* items, size_t itemCount) {
BBox2D bbox = items[0].bbox;
for (size_t i = 1; i < itemCount; i++) {
bbox.expandTo(items[i].bbox);
}
return bbox;
}
void reset() {
if (this->left != NULL) {
delete this->left;
this->left = NULL;
}
if (this->right != NULL) {
delete this->right;
this->right = NULL;
}
this->items.clear();
}
void build(T* items, size_t itemCount, int depth = 0) {
if (itemCount <= 0) {
return;
}
else if (itemCount <= 1) {
this->bbox = items[0].bbox;
this->items.push_back(items[0]);
return;
}
BBox2D bbox = boundingBoxFromItems(items, itemCount);
if (itemCount <= 3) {
this->bbox = bbox;
for (size_t i = 0; i < itemCount; i++) {
this->items.push_back(items[i]);
}
return;
}
KDNode2D* left = NULL;
KDNode2D* right = NULL;
BBox2D lbox, rbox;
std::vector<T> ltris, rtris;
const vec2 boxSize = bbox.getSize();
const vec2 splitPoint = bbox.min + boxSize * 0.5f;
if (boxSize.x > boxSize.y) {
lbox = BBox2D(bbox.min, vec2(splitPoint.x, bbox.max.y));
rbox = BBox2D(vec2(splitPoint.x, bbox.min.y), bbox.max);
} else {
lbox = BBox2D(bbox.min, vec2(bbox.max.x, splitPoint.y));
rbox = BBox2D(vec2(bbox.min.x, splitPoint.y), bbox.max);
}
for (size_t i = 0; i < itemCount; i++/*, pitem++*/) {
const T& item = items[i];
const auto& bbox = item.bbox;
if (lbox.contains(bbox)) {
ltris.push_back(item);
}
else if (rbox.contains(bbox)) {
rtris.push_back(item);
}
else {
this->items.push_back(item);
}
}
if (ltris.size() > 0) {
left = new KDNode2D();
left->build(ltris.data(), ltris.size(), depth + 1);
this->left = left;
}
if (rtris.size() > 0) {
right = new KDNode2D();
right->build(rtris.data(), rtris.size(), depth + 1);
this->right = right;
}
if (this->items.size() > 0) {
bbox = boundingBoxFromItems(this->items.data(), this->items.size());
if (left != NULL) bbox.expandTo(left->bbox);
if (right != NULL) bbox.expandTo(right->bbox);
this->bbox = bbox;
}
else {
if (left != NULL && right == NULL) {
this->bbox = left->bbox;
}
else if (left == NULL && right != NULL) {
this->bbox = right->bbox;
}
else {
this->bbox = BBox2D(left->bbox.min, right->bbox.max);
}
}
}
void split(int depth = 0, int maxDepth = 5) {
if (depth > maxDepth) return;
const vec2 boxSize = this->bbox.getSize();
const vec2 splitPoint = this->bbox.min + boxSize * 0.5f;
if (this->left == NULL) this->left = new KDNode2D();
if (this->right == NULL) this->right = new KDNode2D();
if (boxSize.x > boxSize.y) {
this->left->bbox = BBox2D(bbox.min, vec2(splitPoint.x, bbox.max.y));
this->right->bbox = BBox2D(vec2(splitPoint.x, bbox.min.y), bbox.max);
} else {
this->left->bbox = BBox2D(bbox.min, vec2(bbox.max.x, splitPoint.y));
this->left->bbox = BBox2D(vec2(bbox.min.x, splitPoint.y), bbox.max);
}
this->left->split(depth + 1, maxDepth);
this->right->split(depth + 1, maxDepth);
}
void addItem(T& item) {
this->items.push_back(item);
if (this->left != NULL && this->left->bbox.contains(item.bbox)) {
this->left->addItem(item);
return;
} else if (this->right != NULL && this->right->bbox.contains(item.bbox)) {
this->right->addItem(item);
return;
}
if (this->items.size() < 5) {
this->items.push_back(item);
this->bbox.expandTo(item.bbox);
return;
}
}
bool iterate(const vec2& p, std::function<bool(T&)> iterator) const {
for (const auto& t : this->items) {
if (t.bbox.contains(p)) {
const bool res = iterator(t);
if (!res) return false;
}
}
if (this->left != NULL && this->left->bbox.contains(p)) {
const bool res = this->left->iterate(p, iterator);
if (!res) return false;
}
if (this->right != NULL && this->right->bbox.contains(p)) {
const bool res = this->right->iterate(p, iterator);
if (!res) return false;
}
return true;
}
bool iterate(const BBox2D& box, std::function<bool(T&)> iterator) const {
for (const auto& i : this->items) {
if (i.bbox.intersects(bbox)) {
const bool res = iterator(box);
if (!res) return false;
}
}
if (this->left != NULL && this->left->bbox.contains(box)) {
const bool res = this->left->iterate(box, iterator);
if (!res) return false;
}
if (this->right != NULL && this->right->bbox.contains(box)) {
const bool res = this->right->iterate(box, iterator);
if (!res) return false;
}
return true;
}
bool hitAny(const vec2& p) const {
for (const auto& t : this->items) {
if (t.bbox.contains(p) && pointInTriangle2D(p, t)) {
return true;
}
}
if (this->left != NULL && this->left->bbox.contains(p)) {
const bool res = this->left->hitAny(p);
if (res) return true;
}
if (this->right != NULL && this->right->bbox.contains(p)) {
const bool res = this->right->hitAny(p);
if (res) return true;
}
return false;
}
bool hitAny(const BBox2D& box) const {
for (const auto& t : this->items) {
if (t.bbox.intersects(box)) {
return true;
}
}
if (this->left != NULL && this->left->bbox.contains(box)) {
const bool res = this->left->hitAny(box);
if (res) return true;
}
if (this->right != NULL && this->right->bbox.contains(box)) {
const bool res = this->right->hitAny(box);
if (res) return true;
}
return false;
}
};
}
#endif /* __KDTREE_H__ */