bsp_tree.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*************************************************************************/
  2. /* bsp_tree.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef BSP_TREE_H
  31. #define BSP_TREE_H
  32. #include "core/math/aabb.h"
  33. #include "core/math/face3.h"
  34. #include "core/math/plane.h"
  35. #include "core/method_ptrcall.h"
  36. #include "core/pool_vector.h"
  37. #include "core/variant.h"
  38. #include "core/vector.h"
  39. class BSP_Tree {
  40. public:
  41. enum {
  42. UNDER_LEAF = 0xFFFF,
  43. OVER_LEAF = 0xFFFE,
  44. MAX_NODES = 0xFFFE,
  45. MAX_PLANES = (1 << 16)
  46. };
  47. struct Node {
  48. uint16_t plane;
  49. uint16_t under;
  50. uint16_t over;
  51. };
  52. private:
  53. // thanks to the properties of Vector,
  54. // this class can be assigned and passed around between threads
  55. // with no cost.
  56. Vector<Node> nodes;
  57. Vector<Plane> planes;
  58. AABB aabb;
  59. real_t error_radius;
  60. 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;
  61. template <class T>
  62. bool _test_convex(const Node *p_nodes, const Plane *p_planes, int p_current, const T &p_convex) const;
  63. public:
  64. bool is_empty() const { return nodes.size() == 0; }
  65. Vector<Node> get_nodes() const;
  66. Vector<Plane> get_planes() const;
  67. AABB get_aabb() const;
  68. bool point_is_inside(const Vector3 &p_point) const;
  69. int get_points_inside(const Vector3 *p_points, int p_point_count) const;
  70. template <class T>
  71. bool convex_is_inside(const T &p_convex) const;
  72. operator Variant() const;
  73. void from_aabb(const AABB &p_aabb);
  74. BSP_Tree();
  75. BSP_Tree(const Variant &p_variant);
  76. BSP_Tree(const PoolVector<Face3> &p_faces, real_t p_error_radius = 0);
  77. BSP_Tree(const Vector<Node> &p_nodes, const Vector<Plane> &p_planes, const AABB &p_aabb, real_t p_error_radius = 0);
  78. ~BSP_Tree();
  79. };
  80. template <class T>
  81. bool BSP_Tree::_test_convex(const Node *p_nodes, const Plane *p_planes, int p_current, const T &p_convex) const {
  82. if (p_current == UNDER_LEAF)
  83. return true;
  84. else if (p_current == OVER_LEAF)
  85. return false;
  86. bool collided = false;
  87. const Node &n = p_nodes[p_current];
  88. const Plane &p = p_planes[n.plane];
  89. real_t min, max;
  90. p_convex.project_range(p.normal, min, max);
  91. bool go_under = min < p.d;
  92. bool go_over = max >= p.d;
  93. if (go_under && _test_convex(p_nodes, p_planes, n.under, p_convex))
  94. collided = true;
  95. if (go_over && _test_convex(p_nodes, p_planes, n.over, p_convex))
  96. collided = true;
  97. return collided;
  98. }
  99. template <class T>
  100. bool BSP_Tree::convex_is_inside(const T &p_convex) const {
  101. int node_count = nodes.size();
  102. if (node_count == 0)
  103. return false;
  104. const Node *nodes = &this->nodes[0];
  105. const Plane *planes = &this->planes[0];
  106. return _test_convex(nodes, planes, node_count - 1, p_convex);
  107. }
  108. #ifdef PTRCALL_ENABLED
  109. template <>
  110. struct PtrToArg<BSP_Tree> {
  111. _FORCE_INLINE_ static BSP_Tree convert(const void *p_ptr) {
  112. BSP_Tree s(Variant(*reinterpret_cast<const Dictionary *>(p_ptr)));
  113. return s;
  114. }
  115. _FORCE_INLINE_ static void encode(BSP_Tree p_val, void *p_ptr) {
  116. Dictionary *d = reinterpret_cast<Dictionary *>(p_ptr);
  117. *d = Variant(p_val);
  118. }
  119. };
  120. template <>
  121. struct PtrToArg<const BSP_Tree &> {
  122. _FORCE_INLINE_ static BSP_Tree convert(const void *p_ptr) {
  123. BSP_Tree s(Variant(*reinterpret_cast<const Dictionary *>(p_ptr)));
  124. return s;
  125. }
  126. };
  127. #endif
  128. #endif