bsp_tree.h 4.5 KB

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