2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* bsp_tree.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* 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
|
|
|
/*************************************************************************/
|
2018-01-01 14:40:08 +01:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 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
|
|
|
#ifndef BSP_TREE_H
|
|
|
|
#define BSP_TREE_H
|
|
|
|
|
2017-11-17 03:09:00 +01:00
|
|
|
#include "aabb.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "dvector.h"
|
|
|
|
#include "face3.h"
|
|
|
|
#include "method_ptrcall.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "plane.h"
|
|
|
|
#include "variant.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "vector.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
/**
|
|
|
|
@author Juan Linietsky <reduzio@gmail.com>
|
|
|
|
*/
|
|
|
|
class BSP_Tree {
|
|
|
|
public:
|
|
|
|
enum {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
UNDER_LEAF = 0xFFFF,
|
|
|
|
OVER_LEAF = 0xFFFE,
|
|
|
|
MAX_NODES = 0xFFFE,
|
|
|
|
MAX_PLANES = (1 << 16)
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Node {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
uint16_t plane;
|
|
|
|
uint16_t under;
|
|
|
|
uint16_t over;
|
|
|
|
};
|
|
|
|
|
2016-03-09 00:00:52 +01:00
|
|
|
private:
|
|
|
|
// thanks to the properties of Vector,
|
2014-02-10 02:10:30 +01:00
|
|
|
// this class can be assigned and passed around between threads
|
|
|
|
// with no cost.
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Vector<Node> nodes;
|
|
|
|
Vector<Plane> planes;
|
2017-11-17 03:09:00 +01:00
|
|
|
AABB aabb;
|
2017-01-14 21:35:39 +01:00
|
|
|
real_t error_radius;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int _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;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class T>
|
|
|
|
bool _test_convex(const Node *p_nodes, const Plane *p_planes, int p_current, const T &p_convex) const;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
public:
|
2017-03-05 16:44:50 +01:00
|
|
|
bool is_empty() const { return nodes.size() == 0; }
|
2014-02-10 02:10:30 +01:00
|
|
|
Vector<Node> get_nodes() const;
|
|
|
|
Vector<Plane> get_planes() const;
|
2017-11-17 03:09:00 +01:00
|
|
|
AABB get_aabb() const;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool point_is_inside(const Vector3 &p_point) const;
|
|
|
|
int get_points_inside(const Vector3 *p_points, int p_point_count) const;
|
|
|
|
template <class T>
|
|
|
|
bool convex_is_inside(const T &p_convex) const;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
operator Variant() const;
|
|
|
|
|
2017-11-17 03:09:00 +01:00
|
|
|
void from_aabb(const AABB &p_aabb);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
BSP_Tree();
|
2017-03-05 16:44:50 +01:00
|
|
|
BSP_Tree(const Variant &p_variant);
|
|
|
|
BSP_Tree(const PoolVector<Face3> &p_faces, real_t p_error_radius = 0);
|
2017-11-17 03:09:00 +01:00
|
|
|
BSP_Tree(const Vector<Node> &p_nodes, const Vector<Plane> &p_planes, const AABB &p_aabb, real_t p_error_radius = 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
~BSP_Tree();
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class T>
|
|
|
|
bool BSP_Tree::_test_convex(const Node *p_nodes, const Plane *p_planes, int p_current, const T &p_convex) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_current == UNDER_LEAF)
|
2014-02-10 02:10:30 +01:00
|
|
|
return true;
|
2017-03-05 16:44:50 +01:00
|
|
|
else if (p_current == OVER_LEAF)
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool collided = false;
|
|
|
|
const Node &n = p_nodes[p_current];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const Plane &p = p_planes[n.plane];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t min, max;
|
|
|
|
p_convex.project_range(p.normal, min, max);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
bool go_under = min < p.d;
|
|
|
|
bool go_over = max >= p.d;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (go_under && _test_convex(p_nodes, p_planes, n.under, p_convex))
|
|
|
|
collided = true;
|
|
|
|
if (go_over && _test_convex(p_nodes, p_planes, n.over, p_convex))
|
|
|
|
collided = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return collided;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class T>
|
|
|
|
bool BSP_Tree::convex_is_inside(const T &p_convex) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int node_count = nodes.size();
|
2017-03-05 16:44:50 +01:00
|
|
|
if (node_count == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
2017-03-05 16:44:50 +01:00
|
|
|
const Node *nodes = &this->nodes[0];
|
|
|
|
const Plane *planes = &this->planes[0];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return _test_convex(nodes, planes, node_count - 1, p_convex);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2016-06-23 04:12:20 +02:00
|
|
|
#ifdef PTRCALL_ENABLED
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <>
|
2016-06-23 04:12:20 +02:00
|
|
|
struct PtrToArg<BSP_Tree> {
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ static BSP_Tree convert(const void *p_ptr) {
|
|
|
|
BSP_Tree s(Variant(*reinterpret_cast<const Dictionary *>(p_ptr)));
|
2016-06-23 04:12:20 +02:00
|
|
|
return s;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ static void encode(BSP_Tree p_val, void *p_ptr) {
|
|
|
|
Dictionary *d = reinterpret_cast<Dictionary *>(p_ptr);
|
|
|
|
*d = Variant(p_val);
|
2016-06-23 04:12:20 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <>
|
|
|
|
struct PtrToArg<const BSP_Tree &> {
|
|
|
|
_FORCE_INLINE_ static BSP_Tree convert(const void *p_ptr) {
|
|
|
|
BSP_Tree s(Variant(*reinterpret_cast<const Dictionary *>(p_ptr)));
|
2016-06-23 04:12:20 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|