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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
|
/*************************************************************************/
/* bsp_tree.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "bsp_tree.h"
#include "error_macros.h"
#include "print_string.h"
void BSP_Tree::from_aabb(const AABB &p_aabb) {
planes.clear();
for (int i = 0; i < 3; i++) {
Vector3 n;
n[i] = 1;
planes.push_back(Plane(n, p_aabb.position[i] + p_aabb.size[i]));
planes.push_back(Plane(-n, -p_aabb.position[i]));
}
nodes.clear();
for (int i = 0; i < 6; i++) {
Node n;
n.plane = i;
n.under = (i == 0) ? UNDER_LEAF : i - 1;
n.over = OVER_LEAF;
nodes.push_back(n);
}
aabb = p_aabb;
error_radius = 0;
}
Vector<BSP_Tree::Node> BSP_Tree::get_nodes() const {
return nodes;
}
Vector<Plane> BSP_Tree::get_planes() const {
return planes;
}
AABB BSP_Tree::get_aabb() const {
return aabb;
}
int BSP_Tree::_get_points_inside(int p_node, const Vector3 *p_points, int *p_indices, const Vector3 &p_center, const Vector3 &p_half_extents, int p_indices_count) const {
const Node *node = &nodes[p_node];
const Plane &p = planes[node->plane];
Vector3 min(
(p.normal.x > 0) ? -p_half_extents.x : p_half_extents.x,
(p.normal.y > 0) ? -p_half_extents.y : p_half_extents.y,
(p.normal.z > 0) ? -p_half_extents.z : p_half_extents.z);
Vector3 max = -min;
max += p_center;
min += p_center;
real_t dist_min = p.distance_to(min);
real_t dist_max = p.distance_to(max);
if ((dist_min * dist_max) < CMP_EPSILON) { //intersection, test point by point
int under_count = 0;
//sort points, so the are under first, over last
for (int i = 0; i < p_indices_count; i++) {
int index = p_indices[i];
if (p.is_point_over(p_points[index])) {
// kind of slow (but cache friendly), should try something else,
// but this is a corner case most of the time
for (int j = index; j < p_indices_count - 1; j++)
p_indices[j] = p_indices[j + 1];
p_indices[p_indices_count - 1] = index;
} else {
under_count++;
}
}
int total = 0;
if (under_count > 0) {
if (node->under == UNDER_LEAF) {
total += under_count;
} else {
total += _get_points_inside(node->under, p_points, p_indices, p_center, p_half_extents, under_count);
}
}
if (under_count != p_indices_count) {
if (node->over == OVER_LEAF) {
//total+=0 //if they are over an OVER_LEAF, they are outside the model
} else {
total += _get_points_inside(node->over, p_points, &p_indices[under_count], p_center, p_half_extents, p_indices_count - under_count);
}
}
return total;
} else if (dist_min > 0) { //all points over plane
if (node->over == OVER_LEAF) {
return 0; // all these points are not visible
}
return _get_points_inside(node->over, p_points, p_indices, p_center, p_half_extents, p_indices_count);
} else if (dist_min <= 0) { //all points behind plane
if (node->under == UNDER_LEAF) {
return p_indices_count; // all these points are visible
}
return _get_points_inside(node->under, p_points, p_indices, p_center, p_half_extents, p_indices_count);
}
return 0;
}
int BSP_Tree::get_points_inside(const Vector3 *p_points, int p_point_count) const {
if (nodes.size() == 0)
return 0;
#if 1
//this version is easier to debug, and and MUCH faster in real world cases
int pass_count = 0;
const Node *nodesptr = &nodes[0];
const Plane *planesptr = &planes[0];
int plane_count = planes.size();
int node_count = nodes.size();
if (node_count == 0) // no nodes!
return 0;
for (int i = 0; i < p_point_count; i++) {
const Vector3 &point = p_points[i];
if (!aabb.has_point(point)) {
continue;
}
int idx = node_count - 1;
bool pass = false;
while (true) {
if (idx == OVER_LEAF) {
pass = false;
break;
} else if (idx == UNDER_LEAF) {
pass = true;
break;
}
uint16_t plane = nodesptr[idx].plane;
#ifdef DEBUG_ENABLED
ERR_FAIL_INDEX_V(plane, plane_count, false);
#endif
idx = planesptr[nodesptr[idx].plane].is_point_over(point) ? nodes[idx].over : nodes[idx].under;
#ifdef DEBUG_ENABLED
ERR_FAIL_COND_V(idx < MAX_NODES && idx >= node_count, false);
#endif
}
if (pass)
pass_count++;
}
return pass_count;
#else
//this version scales better but it's slower for real world cases
int *indices = (int *)alloca(p_point_count * sizeof(int));
AABB bounds;
for (int i = 0; i < p_point_count; i++) {
indices[i] = i;
if (i == 0)
bounds.pos = p_points[i];
else
bounds.expand_to(p_points[i]);
}
Vector3 half_extents = bounds.size / 2.0;
return _get_points_inside(nodes.size() + 1, p_points, indices, bounds.pos + half_extents, half_extents, p_point_count);
#endif
}
bool BSP_Tree::point_is_inside(const Vector3 &p_point) const {
if (!aabb.has_point(p_point)) {
return false;
}
int node_count = nodes.size();
if (node_count == 0) // no nodes!
return false;
const Node *nodesptr = &nodes[0];
const Plane *planesptr = &planes[0];
int plane_count = planes.size();
int idx = node_count - 1;
int steps = 0;
while (true) {
if (idx == OVER_LEAF) {
return false;
}
if (idx == UNDER_LEAF) {
return true;
}
uint16_t plane = nodesptr[idx].plane;
#ifdef DEBUG_ENABLED
ERR_FAIL_INDEX_V(plane, plane_count, false);
#endif
bool over = planesptr[nodesptr[idx].plane].is_point_over(p_point);
idx = over ? nodes[idx].over : nodes[idx].under;
#ifdef DEBUG_ENABLED
ERR_FAIL_COND_V(idx < MAX_NODES && idx >= node_count, false);
#endif
steps++;
}
return false;
}
static int _bsp_find_best_half_plane(const Face3 *p_faces, const Vector<int> &p_indices, real_t p_tolerance) {
int ic = p_indices.size();
const int *indices = p_indices.ptr();
int best_plane = -1;
real_t best_plane_cost = 1e20;
// Loop to find the polygon that best divides the set.
for (int i = 0; i < ic; i++) {
const Face3 &f = p_faces[indices[i]];
Plane p = f.get_plane();
int num_over = 0, num_under = 0, num_spanning = 0;
for (int j = 0; j < ic; j++) {
if (i == j)
continue;
const Face3 &g = p_faces[indices[j]];
int over = 0, under = 0;
for (int k = 0; k < 3; k++) {
real_t d = p.distance_to(g.vertex[j]);
if (Math::abs(d) > p_tolerance) {
if (d > 0)
over++;
else
under++;
}
}
if (over && under)
num_spanning++;
else if (over)
num_over++;
else
num_under++;
}
//real_t split_cost = num_spanning / (real_t) face_count;
real_t relation = Math::abs(num_over - num_under) / (real_t)ic;
// being honest, i never found a way to add split cost to the mix in a meaninguful way
// in this engine, also, will likely be ignored anyway
real_t plane_cost = /*split_cost +*/ relation;
//printf("plane %i, %i over, %i under, %i spanning, cost is %g\n",i,num_over,num_under,num_spanning,plane_cost);
if (plane_cost < best_plane_cost) {
best_plane = i;
best_plane_cost = plane_cost;
}
}
return best_plane;
}
static int _bsp_create_node(const Face3 *p_faces, const Vector<int> &p_indices, Vector<Plane> &p_planes, Vector<BSP_Tree::Node> &p_nodes, real_t p_tolerance) {
ERR_FAIL_COND_V(p_nodes.size() == BSP_Tree::MAX_NODES, -1);
// should not reach here
ERR_FAIL_COND_V(p_indices.size() == 0, -1)
int ic = p_indices.size();
const int *indices = p_indices.ptr();
int divisor_idx = _bsp_find_best_half_plane(p_faces, p_indices, p_tolerance);
// returned error
ERR_FAIL_COND_V(divisor_idx < 0, -1);
Vector<int> faces_over;
Vector<int> faces_under;
Plane divisor_plane = p_faces[indices[divisor_idx]].get_plane();
for (int i = 0; i < ic; i++) {
if (i == divisor_idx)
continue;
const Face3 &f = p_faces[indices[i]];
/*
if (f.get_plane().is_almost_like(divisor_plane))
continue;
*/
int over_count = 0;
int under_count = 0;
for (int j = 0; j < 3; j++) {
real_t d = divisor_plane.distance_to(f.vertex[j]);
if (Math::abs(d) > p_tolerance) {
if (d > 0)
over_count++;
else
under_count++;
}
}
if (over_count)
faces_over.push_back(indices[i]);
if (under_count)
faces_under.push_back(indices[i]);
}
uint16_t over_idx = BSP_Tree::OVER_LEAF, under_idx = BSP_Tree::UNDER_LEAF;
if (faces_over.size() > 0) { //have facess above?
int idx = _bsp_create_node(p_faces, faces_over, p_planes, p_nodes, p_tolerance);
if (idx >= 0)
over_idx = idx;
}
if (faces_under.size() > 0) { //have facess above?
int idx = _bsp_create_node(p_faces, faces_under, p_planes, p_nodes, p_tolerance);
if (idx >= 0)
under_idx = idx;
}
/* Create the node */
// find existing divisor plane
int divisor_plane_idx = -1;
for (int i = 0; i < p_planes.size(); i++) {
if (p_planes[i].is_almost_like(divisor_plane)) {
divisor_plane_idx = i;
break;
}
}
if (divisor_plane_idx == -1) {
ERR_FAIL_COND_V(p_planes.size() == BSP_Tree::MAX_PLANES, -1);
divisor_plane_idx = p_planes.size();
p_planes.push_back(divisor_plane);
}
BSP_Tree::Node node;
node.plane = divisor_plane_idx;
node.under = under_idx;
node.over = over_idx;
p_nodes.push_back(node);
return p_nodes.size() - 1;
}
BSP_Tree::operator Variant() const {
Dictionary d;
d["error_radius"] = error_radius;
Vector<real_t> plane_values;
plane_values.resize(planes.size() * 4);
for (int i = 0; i < planes.size(); i++) {
plane_values[i * 4 + 0] = planes[i].normal.x;
plane_values[i * 4 + 1] = planes[i].normal.y;
plane_values[i * 4 + 2] = planes[i].normal.z;
plane_values[i * 4 + 3] = planes[i].d;
}
d["planes"] = plane_values;
PoolVector<int> dst_nodes;
dst_nodes.resize(nodes.size() * 3);
for (int i = 0; i < nodes.size(); i++) {
dst_nodes.set(i * 3 + 0, nodes[i].over);
dst_nodes.set(i * 3 + 1, nodes[i].under);
dst_nodes.set(i * 3 + 2, nodes[i].plane);
}
d["nodes"] = dst_nodes;
d["aabb"] = aabb;
return Variant(d);
}
BSP_Tree::BSP_Tree() {
}
BSP_Tree::BSP_Tree(const Variant &p_variant) {
Dictionary d = p_variant;
ERR_FAIL_COND(!d.has("nodes"));
ERR_FAIL_COND(!d.has("planes"));
ERR_FAIL_COND(!d.has("aabb"));
ERR_FAIL_COND(!d.has("error_radius"));
PoolVector<int> src_nodes = d["nodes"];
ERR_FAIL_COND(src_nodes.size() % 3);
if (d["planes"].get_type() == Variant::POOL_REAL_ARRAY) {
PoolVector<real_t> src_planes = d["planes"];
int plane_count = src_planes.size();
ERR_FAIL_COND(plane_count % 4);
planes.resize(plane_count / 4);
if (plane_count) {
PoolVector<real_t>::Read r = src_planes.read();
for (int i = 0; i < plane_count / 4; i++) {
planes[i].normal.x = r[i * 4 + 0];
planes[i].normal.y = r[i * 4 + 1];
planes[i].normal.z = r[i * 4 + 2];
planes[i].d = r[i * 4 + 3];
}
}
} else {
planes = d["planes"];
}
error_radius = d["error"];
aabb = d["aabb"];
//int node_count = src_nodes.size();
nodes.resize(src_nodes.size() / 3);
PoolVector<int>::Read r = src_nodes.read();
for (int i = 0; i < nodes.size(); i++) {
nodes[i].over = r[i * 3 + 0];
nodes[i].under = r[i * 3 + 1];
nodes[i].plane = r[i * 3 + 2];
}
}
BSP_Tree::BSP_Tree(const PoolVector<Face3> &p_faces, real_t p_error_radius) {
// compute aabb
int face_count = p_faces.size();
PoolVector<Face3>::Read faces_r = p_faces.read();
const Face3 *facesptr = faces_r.ptr();
bool first = true;
Vector<int> indices;
for (int i = 0; i < face_count; i++) {
const Face3 &f = facesptr[i];
if (f.is_degenerate())
continue;
for (int j = 0; j < 3; j++) {
if (first) {
aabb.position = f.vertex[0];
first = false;
} else {
aabb.expand_to(f.vertex[j]);
}
}
indices.push_back(i);
}
ERR_FAIL_COND(aabb.has_no_area());
int top = _bsp_create_node(faces_r.ptr(), indices, planes, nodes, aabb.get_longest_axis_size() * 0.0001);
if (top < 0) {
nodes.clear();
planes.clear();
ERR_FAIL_COND(top < 0);
}
error_radius = p_error_radius;
}
BSP_Tree::BSP_Tree(const Vector<Node> &p_nodes, const Vector<Plane> &p_planes, const AABB &p_aabb, real_t p_error_radius) :
nodes(p_nodes),
planes(p_planes),
aabb(p_aabb),
error_radius(p_error_radius) {
}
BSP_Tree::~BSP_Tree() {
}
|