2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* bsp_tree.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2021-01-01 20:13:46 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "bsp_tree.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
|
|
|
|
#include "core/error_macros.h"
|
|
|
|
#include "core/print_string.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-11-17 03:09:00 +01:00
|
|
|
void BSP_Tree::from_aabb(const AABB &p_aabb) {
|
2014-02-10 02:10:30 +01:00
|
|
|
planes.clear();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
Vector3 n;
|
2017-03-05 16:44:50 +01:00
|
|
|
n[i] = 1;
|
2017-06-06 20:33:51 +02:00
|
|
|
planes.push_back(Plane(n, p_aabb.position[i] + p_aabb.size[i]));
|
|
|
|
planes.push_back(Plane(-n, -p_aabb.position[i]));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
nodes.clear();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < 6; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
Node n;
|
2017-03-05 16:44:50 +01:00
|
|
|
n.plane = i;
|
|
|
|
n.under = (i == 0) ? UNDER_LEAF : i - 1;
|
|
|
|
n.over = OVER_LEAF;
|
2014-02-10 02:10:30 +01:00
|
|
|
nodes.push_back(n);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
aabb = p_aabb;
|
|
|
|
error_radius = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector<BSP_Tree::Node> BSP_Tree::get_nodes() const {
|
|
|
|
return nodes;
|
|
|
|
}
|
|
|
|
Vector<Plane> BSP_Tree::get_planes() const {
|
|
|
|
return planes;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-11-17 03:09:00 +01:00
|
|
|
AABB BSP_Tree::get_aabb() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return aabb;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
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];
|
2014-02-10 02:10:30 +01:00
|
|
|
const Plane &p = planes[node->plane];
|
|
|
|
|
|
|
|
Vector3 min(
|
2017-03-05 16:44:50 +01:00
|
|
|
(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;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
real_t dist_min = p.distance_to(min);
|
|
|
|
real_t dist_max = p.distance_to(max);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if ((dist_min * dist_max) < CMP_EPSILON) { //intersection, test point by point
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int under_count = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
//sort points, so the are under first, over last
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < p_indices_count; i++) {
|
|
|
|
int index = p_indices[i];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-05-05 12:44:11 +02:00
|
|
|
for (int j = index; j < p_indices_count - 1; j++) {
|
2017-03-05 16:44:50 +01:00
|
|
|
p_indices[j] = p_indices[j + 1];
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p_indices[p_indices_count - 1] = index;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
under_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int total = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (under_count > 0) {
|
|
|
|
if (node->under == UNDER_LEAF) {
|
|
|
|
total += under_count;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
total += _get_points_inside(node->under, p_points, p_indices, p_center, p_half_extents, under_count);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (under_count != p_indices_count) {
|
|
|
|
if (node->over == OVER_LEAF) {
|
2014-02-10 02:10:30 +01:00
|
|
|
//total+=0 //if they are over an OVER_LEAF, they are outside the model
|
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
total += _get_points_inside(node->over, p_points, &p_indices[under_count], p_center, p_half_extents, p_indices_count - under_count);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (dist_min > 0) { //all points over plane
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (node->over == OVER_LEAF) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return 0; // all these points are not visible
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return _get_points_inside(node->over, p_points, p_indices, p_center, p_half_extents, p_indices_count);
|
2019-07-01 12:59:42 +02:00
|
|
|
} else { //all points behind plane
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (node->under == UNDER_LEAF) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return p_indices_count; // all these points are visible
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
return _get_points_inside(node->under, p_points, p_indices, p_center, p_half_extents, p_indices_count);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int BSP_Tree::get_points_inside(const Vector3 *p_points, int p_point_count) const {
|
2021-05-05 12:44:11 +02:00
|
|
|
if (nodes.size() == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return 0;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
#if 1
|
2017-03-05 16:44:50 +01:00
|
|
|
//this version is easier to debug, and and MUCH faster in real world cases
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int pass_count = 0;
|
2017-03-05 16:44:50 +01:00
|
|
|
const Node *nodesptr = &nodes[0];
|
|
|
|
const Plane *planesptr = &planes[0];
|
|
|
|
int node_count = nodes.size();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-05-05 12:44:11 +02:00
|
|
|
if (node_count == 0) { // no nodes!
|
2014-02-10 02:10:30 +01:00
|
|
|
return 0;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < p_point_count; i++) {
|
|
|
|
const Vector3 &point = p_points[i];
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!aabb.has_point(point)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int idx = node_count - 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool pass = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (true) {
|
|
|
|
if (idx == OVER_LEAF) {
|
|
|
|
pass = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (idx == UNDER_LEAF) {
|
|
|
|
pass = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
2018-10-03 16:13:34 +02:00
|
|
|
int plane_count = planes.size();
|
|
|
|
uint16_t plane = nodesptr[idx].plane;
|
2019-12-10 05:13:02 +01:00
|
|
|
ERR_FAIL_UNSIGNED_INDEX_V(plane, plane_count, 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
idx = planesptr[nodesptr[idx].plane].is_point_over(point) ? nodes[idx].over : nodes[idx].under;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
|
2019-12-10 05:13:02 +01:00
|
|
|
ERR_FAIL_COND_V(idx < MAX_NODES && idx >= node_count, 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-05-05 12:44:11 +02:00
|
|
|
if (pass) {
|
2014-02-10 02:10:30 +01:00
|
|
|
pass_count++;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return pass_count;
|
|
|
|
|
|
|
|
#else
|
2017-03-05 16:44:50 +01:00
|
|
|
//this version scales better but it's slower for real world cases
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int *indices = (int *)alloca(p_point_count * sizeof(int));
|
2014-02-10 02:10:30 +01:00
|
|
|
AABB bounds;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < p_point_count; i++) {
|
|
|
|
indices[i] = i;
|
|
|
|
if (i == 0)
|
|
|
|
bounds.pos = p_points[i];
|
2014-02-10 02:10:30 +01:00
|
|
|
else
|
|
|
|
bounds.expand_to(p_points[i]);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
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);
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool BSP_Tree::point_is_inside(const Vector3 &p_point) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!aabb.has_point(p_point)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int node_count = nodes.size();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2021-05-05 12:44:11 +02:00
|
|
|
if (node_count == 0) { // no nodes!
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const Node *nodesptr = &nodes[0];
|
|
|
|
const Plane *planesptr = &planes[0];
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int idx = node_count - 1;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (true) {
|
|
|
|
if (idx == OVER_LEAF) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
if (idx == UNDER_LEAF) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return true;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#ifdef DEBUG_ENABLED
|
2018-03-26 01:36:34 +02:00
|
|
|
int plane_count = planes.size();
|
|
|
|
uint16_t plane = nodesptr[idx].plane;
|
2019-07-20 08:09:57 +02:00
|
|
|
ERR_FAIL_UNSIGNED_INDEX_V(plane, plane_count, false);
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
2018-03-26 01:36:34 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool over = planesptr[nodesptr[idx].plane].is_point_over(p_point);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
idx = over ? nodes[idx].over : nodes[idx].under;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#ifdef DEBUG_ENABLED
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(idx < MAX_NODES && idx >= node_count, false);
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static int _bsp_find_best_half_plane(const Face3 *p_faces, const Vector<int> &p_indices, real_t p_tolerance) {
|
2014-02-10 02:10:30 +01:00
|
|
|
int ic = p_indices.size();
|
2017-03-05 16:44:50 +01:00
|
|
|
const int *indices = p_indices.ptr();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int best_plane = -1;
|
2017-01-14 21:35:39 +01:00
|
|
|
real_t best_plane_cost = 1e20;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// Loop to find the polygon that best divides the set.
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < ic; i++) {
|
|
|
|
const Face3 &f = p_faces[indices[i]];
|
2014-02-10 02:10:30 +01:00
|
|
|
Plane p = f.get_plane();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int num_over = 0, num_under = 0, num_spanning = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = 0; j < ic; j++) {
|
2021-05-05 12:44:11 +02:00
|
|
|
if (i == j) {
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const Face3 &g = p_faces[indices[j]];
|
|
|
|
int over = 0, under = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int k = 0; k < 3; k++) {
|
2017-01-14 21:35:39 +01:00
|
|
|
real_t d = p.distance_to(g.vertex[j]);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (Math::abs(d) > p_tolerance) {
|
2021-05-05 12:44:11 +02:00
|
|
|
if (d > 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
over++;
|
2021-05-05 12:44:11 +02:00
|
|
|
} else {
|
2014-02-10 02:10:30 +01:00
|
|
|
under++;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 12:44:11 +02:00
|
|
|
if (over && under) {
|
2014-02-10 02:10:30 +01:00
|
|
|
num_spanning++;
|
2021-05-05 12:44:11 +02:00
|
|
|
} else if (over) {
|
2014-02-10 02:10:30 +01:00
|
|
|
num_over++;
|
2021-05-05 12:44:11 +02:00
|
|
|
} else {
|
2014-02-10 02:10:30 +01:00
|
|
|
num_under++;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
//real_t split_cost = num_spanning / (real_t) face_count;
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t relation = Math::abs(num_over - num_under) / (real_t)ic;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
// 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
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
real_t plane_cost = /*split_cost +*/ relation;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
//printf("plane %i, %i over, %i under, %i spanning, cost is %g\n",i,num_over,num_under,num_spanning,plane_cost);
|
2017-03-05 16:44:50 +01:00
|
|
|
if (plane_cost < best_plane_cost) {
|
|
|
|
best_plane = i;
|
|
|
|
best_plane_cost = plane_cost;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return best_plane;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
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);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// should not reach here
|
2021-09-12 08:47:33 +02:00
|
|
|
ERR_FAIL_COND_V(p_indices.size() == 0, -1);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int ic = p_indices.size();
|
2017-03-05 16:44:50 +01:00
|
|
|
const int *indices = p_indices.ptr();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int divisor_idx = _bsp_find_best_half_plane(p_faces, p_indices, p_tolerance);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// returned error
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(divisor_idx < 0, -1);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Vector<int> faces_over;
|
|
|
|
Vector<int> faces_under;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Plane divisor_plane = p_faces[indices[divisor_idx]].get_plane();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < ic; i++) {
|
2021-05-05 12:44:11 +02:00
|
|
|
if (i == divisor_idx) {
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const Face3 &f = p_faces[indices[i]];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-14 12:26:56 +01:00
|
|
|
/*
|
2019-10-02 22:31:09 +02:00
|
|
|
if (f.get_plane().is_equal_approx(divisor_plane))
|
2017-01-14 12:26:56 +01:00
|
|
|
continue;
|
|
|
|
*/
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int over_count = 0;
|
|
|
|
int under_count = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = 0; j < 3; j++) {
|
2017-01-14 21:35:39 +01:00
|
|
|
real_t d = divisor_plane.distance_to(f.vertex[j]);
|
2017-03-05 16:44:50 +01:00
|
|
|
if (Math::abs(d) > p_tolerance) {
|
2021-05-05 12:44:11 +02:00
|
|
|
if (d > 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
over_count++;
|
2021-05-05 12:44:11 +02:00
|
|
|
} else {
|
2014-02-10 02:10:30 +01:00
|
|
|
under_count++;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 12:44:11 +02:00
|
|
|
if (over_count) {
|
2017-03-05 16:44:50 +01:00
|
|
|
faces_over.push_back(indices[i]);
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
|
|
|
if (under_count) {
|
2017-03-05 16:44:50 +01:00
|
|
|
faces_under.push_back(indices[i]);
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uint16_t over_idx = BSP_Tree::OVER_LEAF, under_idx = BSP_Tree::UNDER_LEAF;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (faces_over.size() > 0) { //have facess above?
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int idx = _bsp_create_node(p_faces, faces_over, p_planes, p_nodes, p_tolerance);
|
2021-05-05 12:44:11 +02:00
|
|
|
if (idx >= 0) {
|
2017-03-05 16:44:50 +01:00
|
|
|
over_idx = idx;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (faces_under.size() > 0) { //have facess above?
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int idx = _bsp_create_node(p_faces, faces_under, p_planes, p_nodes, p_tolerance);
|
2021-05-05 12:44:11 +02:00
|
|
|
if (idx >= 0) {
|
2017-03-05 16:44:50 +01:00
|
|
|
under_idx = idx;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the node */
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
// find existing divisor plane
|
2017-03-05 16:44:50 +01:00
|
|
|
int divisor_plane_idx = -1;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < p_planes.size(); i++) {
|
2019-10-02 22:31:09 +02:00
|
|
|
if (p_planes[i].is_equal_approx(divisor_plane)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
divisor_plane_idx = i;
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
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);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
BSP_Tree::Node node;
|
2017-03-05 16:44:50 +01:00
|
|
|
node.plane = divisor_plane_idx;
|
|
|
|
node.under = under_idx;
|
|
|
|
node.over = over_idx;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
p_nodes.push_back(node);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return p_nodes.size() - 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
BSP_Tree::operator Variant() const {
|
|
|
|
Dictionary d;
|
2017-03-05 16:44:50 +01:00
|
|
|
d["error_radius"] = error_radius;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
Vector<real_t> plane_values;
|
2017-03-05 16:44:50 +01:00
|
|
|
plane_values.resize(planes.size() * 4);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < planes.size(); i++) {
|
2018-07-25 03:11:03 +02:00
|
|
|
plane_values.write[i * 4 + 0] = planes[i].normal.x;
|
|
|
|
plane_values.write[i * 4 + 1] = planes[i].normal.y;
|
|
|
|
plane_values.write[i * 4 + 2] = planes[i].normal.z;
|
|
|
|
plane_values.write[i * 4 + 3] = planes[i].d;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
d["planes"] = plane_values;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<int> dst_nodes;
|
2017-03-05 16:44:50 +01:00
|
|
|
dst_nodes.resize(nodes.size() * 3);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
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);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
d["nodes"] = dst_nodes;
|
2014-02-10 02:10:30 +01:00
|
|
|
d["aabb"] = aabb;
|
|
|
|
|
|
|
|
return Variant(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
BSP_Tree::BSP_Tree() {
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
BSP_Tree::BSP_Tree(const Variant &p_variant) {
|
|
|
|
Dictionary d = p_variant;
|
2014-02-10 02:10:30 +01:00
|
|
|
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"));
|
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<int> src_nodes = d["nodes"];
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(src_nodes.size() % 3);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
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);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (plane_count) {
|
2017-01-14 21:35:39 +01:00
|
|
|
PoolVector<real_t>::Read r = src_planes.read();
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < plane_count / 4; i++) {
|
2018-07-25 03:11:03 +02:00
|
|
|
planes.write[i].normal.x = r[i * 4 + 0];
|
|
|
|
planes.write[i].normal.y = r[i * 4 + 1];
|
|
|
|
planes.write[i].normal.z = r[i * 4 + 2];
|
|
|
|
planes.write[i].d = r[i * 4 + 3];
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
planes = d["planes"];
|
|
|
|
}
|
|
|
|
|
|
|
|
error_radius = d["error"];
|
|
|
|
aabb = d["aabb"];
|
|
|
|
|
2017-01-14 12:26:56 +01:00
|
|
|
//int node_count = src_nodes.size();
|
2017-03-05 16:44:50 +01:00
|
|
|
nodes.resize(src_nodes.size() / 3);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<int>::Read r = src_nodes.read();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < nodes.size(); i++) {
|
2018-07-25 03:11:03 +02:00
|
|
|
nodes.write[i].over = r[i * 3 + 0];
|
|
|
|
nodes.write[i].under = r[i * 3 + 1];
|
|
|
|
nodes.write[i].plane = r[i * 3 + 2];
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
BSP_Tree::BSP_Tree(const PoolVector<Face3> &p_faces, real_t p_error_radius) {
|
2014-02-10 02:10:30 +01:00
|
|
|
// compute aabb
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int face_count = p_faces.size();
|
|
|
|
PoolVector<Face3>::Read faces_r = p_faces.read();
|
2014-02-10 02:10:30 +01:00
|
|
|
const Face3 *facesptr = faces_r.ptr();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool first = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Vector<int> indices;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < face_count; i++) {
|
|
|
|
const Face3 &f = facesptr[i];
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2021-05-05 12:44:11 +02:00
|
|
|
if (f.is_degenerate()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = 0; j < 3; j++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
if (first) {
|
2017-06-06 20:33:51 +02:00
|
|
|
aabb.position = f.vertex[0];
|
2017-03-05 16:44:50 +01:00
|
|
|
first = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
|
|
|
aabb.expand_to(f.vertex[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
indices.push_back(i);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(aabb.has_no_area());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int top = _bsp_create_node(faces_r.ptr(), indices, planes, nodes, aabb.get_longest_axis_size() * 0.0001);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (top < 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
nodes.clear();
|
|
|
|
planes.clear();
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(top < 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
error_radius = p_error_radius;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-12-06 21:36:34 +01:00
|
|
|
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) {
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BSP_Tree::~BSP_Tree() {
|
|
|
|
}
|