bsp_tree.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 "aabb.h"
  33. #include "dvector.h"
  34. #include "face3.h"
  35. #include "method_ptrcall.h"
  36. #include "plane.h"
  37. #include "variant.h"
  38. #include "vector.h"
  39. /**
  40. @author Juan Linietsky <reduzio@gmail.com>
  41. */
  42. class BSP_Tree {
  43. public:
  44. enum {
  45. UNDER_LEAF = 0xFFFF,
  46. OVER_LEAF = 0xFFFE,
  47. MAX_NODES = 0xFFFE,
  48. MAX_PLANES = (1 << 16)
  49. };
  50. struct Node {
  51. uint16_t plane;
  52. uint16_t under;
  53. uint16_t over;
  54. };
  55. private:
  56. // thanks to the properties of Vector,
  57. // this class can be assigned and passed around between threads
  58. // with no cost.
  59. Vector<Node> nodes;
  60. Vector<Plane> planes;
  61. AABB aabb;
  62. float error_radius;
  63. 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;
  64. template <class T>
  65. bool _test_convex(const Node *p_nodes, const Plane *p_planes, int p_current, const T &p_convex) const;
  66. public:
  67. bool is_empty() const { return nodes.size() == 0; }
  68. Vector<Node> get_nodes() const;
  69. Vector<Plane> get_planes() const;
  70. AABB get_aabb() const;
  71. bool point_is_inside(const Vector3 &p_point) const;
  72. int get_points_inside(const Vector3 *p_points, int p_point_count) const;
  73. template <class T>
  74. bool convex_is_inside(const T &p_convex) const;
  75. operator Variant() const;
  76. void from_aabb(const AABB &p_aabb);
  77. BSP_Tree();
  78. BSP_Tree(const Variant &p_variant);
  79. BSP_Tree(const DVector<Face3> &p_faces, float p_error_radius = 0);
  80. BSP_Tree(const Vector<Node> &p_nodes, const Vector<Plane> &p_planes, const AABB &p_aabb, float p_error_radius = 0);
  81. ~BSP_Tree();
  82. };
  83. template <class T>
  84. bool BSP_Tree::_test_convex(const Node *p_nodes, const Plane *p_planes, int p_current, const T &p_convex) const {
  85. if (p_current == UNDER_LEAF)
  86. return true;
  87. else if (p_current == OVER_LEAF)
  88. return false;
  89. bool collided = false;
  90. const Node &n = p_nodes[p_current];
  91. const Plane &p = p_planes[n.plane];
  92. float min, max;
  93. p_convex.project_range(p.normal, min, max);
  94. bool go_under = min < p.d;
  95. bool go_over = max >= p.d;
  96. if (go_under && _test_convex(p_nodes, p_planes, n.under, p_convex))
  97. collided = true;
  98. if (go_over && _test_convex(p_nodes, p_planes, n.over, p_convex))
  99. collided = true;
  100. return collided;
  101. }
  102. template <class T>
  103. bool BSP_Tree::convex_is_inside(const T &p_convex) const {
  104. int node_count = nodes.size();
  105. if (node_count == 0)
  106. return false;
  107. const Node *nodes = &this->nodes[0];
  108. const Plane *planes = &this->planes[0];
  109. return _test_convex(nodes, planes, node_count - 1, p_convex);
  110. }
  111. #ifdef PTRCALL_ENABLED
  112. template <>
  113. struct PtrToArg<BSP_Tree> {
  114. _FORCE_INLINE_ static BSP_Tree convert(const void *p_ptr) {
  115. BSP_Tree s(Variant(*reinterpret_cast<const Dictionary *>(p_ptr)));
  116. return s;
  117. }
  118. _FORCE_INLINE_ static void encode(BSP_Tree p_val, void *p_ptr) {
  119. Dictionary *d = reinterpret_cast<Dictionary *>(p_ptr);
  120. *d = Variant(p_val);
  121. }
  122. };
  123. template <>
  124. struct PtrToArg<const BSP_Tree &> {
  125. _FORCE_INLINE_ static BSP_Tree convert(const void *p_ptr) {
  126. BSP_Tree s(Variant(*reinterpret_cast<const Dictionary *>(p_ptr)));
  127. return s;
  128. }
  129. };
  130. #endif
  131. #endif