aabb.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*************************************************************************/
  2. /* aabb.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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 AABB_H
  30. #define AABB_H
  31. #include "vector3.h"
  32. #include "plane.h"
  33. /**
  34. * AABB / AABB (Axis Aligned Bounding Box)
  35. * This is implemented by a point (pos) and the box size
  36. */
  37. class AABB {
  38. public:
  39. Vector3 pos;
  40. Vector3 size;
  41. float get_area() const; /// get area
  42. _FORCE_INLINE_ bool has_no_area() const {
  43. return (size.x<=CMP_EPSILON || size.y<=CMP_EPSILON || size.z<=CMP_EPSILON);
  44. }
  45. _FORCE_INLINE_ bool has_no_surface() const {
  46. return (size.x<=CMP_EPSILON && size.y<=CMP_EPSILON && size.z<=CMP_EPSILON);
  47. }
  48. const Vector3& get_pos() const { return pos; }
  49. void set_pos(const Vector3& p_pos) { pos=p_pos; }
  50. const Vector3& get_size() const { return size; }
  51. void set_size(const Vector3& p_size) { size=p_size; }
  52. bool operator==(const AABB& p_rval) const;
  53. bool operator!=(const AABB& p_rval) const;
  54. _FORCE_INLINE_ bool intersects(const AABB& p_aabb) const; /// Both AABBs overlap
  55. _FORCE_INLINE_ bool encloses(const AABB & p_aabb) const; /// p_aabb is completely inside this
  56. AABB merge(const AABB& p_with) const;
  57. void merge_with(const AABB& p_aabb); ///merge with another AABB
  58. AABB intersection(const AABB& p_aabb) const; ///get box where two intersect, empty if no intersection occurs
  59. bool intersects_segment(const Vector3& p_from, const Vector3& p_to,Vector3* r_clip=NULL,Vector3* r_normal=NULL) const;
  60. bool intersects_ray(const Vector3& p_from, const Vector3& p_dir,Vector3* r_clip=NULL,Vector3* r_normal=NULL) const;
  61. _FORCE_INLINE_ bool intersects_convex_shape(const Plane *p_plane, int p_plane_count) const;
  62. bool intersects_plane(const Plane &p_plane) const;
  63. _FORCE_INLINE_ bool has_point(const Vector3& p_point) const;
  64. _FORCE_INLINE_ Vector3 get_support(const Vector3& p_normal) const;
  65. Vector3 get_longest_axis() const;
  66. int get_longest_axis_index() const;
  67. _FORCE_INLINE_ real_t get_longest_axis_size() const;
  68. Vector3 get_shortest_axis() const;
  69. int get_shortest_axis_index() const;
  70. _FORCE_INLINE_ real_t get_shortest_axis_size() const;
  71. AABB grow(real_t p_by) const;
  72. void grow_by(real_t p_amount);
  73. void get_edge(int p_edge,Vector3& r_from,Vector3& r_to) const;
  74. _FORCE_INLINE_ Vector3 get_endpoint(int p_point) const;
  75. AABB expand(const Vector3& p_vector) const;
  76. _FORCE_INLINE_ void project_range_in_plane(const Plane& p_plane,float &r_min,float& r_max) const;
  77. _FORCE_INLINE_ void expand_to(const Vector3& p_vector); /** expand to contain a point if necesary */
  78. operator String() const;
  79. _FORCE_INLINE_ AABB() {}
  80. inline AABB(const Vector3 &p_pos,const Vector3& p_size) { pos=p_pos; size=p_size; }
  81. };
  82. inline bool AABB::intersects(const AABB& p_aabb) const {
  83. if ( pos.x > (p_aabb.pos.x + p_aabb.size.x) )
  84. return false;
  85. if ( (pos.x+size.x) < p_aabb.pos.x )
  86. return false;
  87. if ( pos.y > (p_aabb.pos.y + p_aabb.size.y) )
  88. return false;
  89. if ( (pos.y+size.y) < p_aabb.pos.y )
  90. return false;
  91. if ( pos.z > (p_aabb.pos.z + p_aabb.size.z) )
  92. return false;
  93. if ( (pos.z+size.z) < p_aabb.pos.z )
  94. return false;
  95. return true;
  96. }
  97. inline bool AABB::encloses(const AABB & p_aabb) const {
  98. Vector3 src_min=pos;
  99. Vector3 src_max=pos+size;
  100. Vector3 dst_min=p_aabb.pos;
  101. Vector3 dst_max=p_aabb.pos+p_aabb.size;
  102. return (
  103. (src_min.x <= dst_min.x) &&
  104. (src_max.x > dst_max.x) &&
  105. (src_min.y <= dst_min.y) &&
  106. (src_max.y > dst_max.y) &&
  107. (src_min.z <= dst_min.z) &&
  108. (src_max.z > dst_max.z) );
  109. }
  110. Vector3 AABB::get_support(const Vector3& p_normal) const {
  111. Vector3 half_extents = size * 0.5;
  112. Vector3 ofs = pos + half_extents;
  113. return Vector3(
  114. (p_normal.x>0) ? -half_extents.x : half_extents.x,
  115. (p_normal.y>0) ? -half_extents.y : half_extents.y,
  116. (p_normal.z>0) ? -half_extents.z : half_extents.z
  117. )+ofs;
  118. }
  119. Vector3 AABB::get_endpoint(int p_point) const {
  120. switch(p_point) {
  121. case 0: return Vector3( pos.x , pos.y , pos.z );
  122. case 1: return Vector3( pos.x , pos.y , pos.z+size.z );
  123. case 2: return Vector3( pos.x , pos.y+size.y , pos.z );
  124. case 3: return Vector3( pos.x , pos.y+size.y , pos.z+size.z );
  125. case 4: return Vector3( pos.x+size.x , pos.y , pos.z );
  126. case 5: return Vector3( pos.x+size.x , pos.y , pos.z+size.z );
  127. case 6: return Vector3( pos.x+size.x , pos.y+size.y , pos.z );
  128. case 7: return Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z );
  129. };
  130. ERR_FAIL_V(Vector3());
  131. }
  132. bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count) const {
  133. #if 1
  134. Vector3 half_extents = size * 0.5;
  135. Vector3 ofs = pos + half_extents;
  136. for(int i=0;i<p_plane_count;i++) {
  137. const Plane &p=p_planes[i];
  138. Vector3 point(
  139. (p.normal.x>0) ? -half_extents.x : half_extents.x,
  140. (p.normal.y>0) ? -half_extents.y : half_extents.y,
  141. (p.normal.z>0) ? -half_extents.z : half_extents.z
  142. );
  143. point+=ofs;
  144. if (p.is_point_over(point))
  145. return false;
  146. }
  147. return true;
  148. #else
  149. //cache all points to check against!
  150. // #warning should be easy to optimize, just use the same as when taking the support and use only that point
  151. Vector3 points[8] = {
  152. Vector3( pos.x , pos.y , pos.z ),
  153. Vector3( pos.x , pos.y , pos.z+size.z ),
  154. Vector3( pos.x , pos.y+size.y , pos.z ),
  155. Vector3( pos.x , pos.y+size.y , pos.z+size.z ),
  156. Vector3( pos.x+size.x , pos.y , pos.z ),
  157. Vector3( pos.x+size.x , pos.y , pos.z+size.z ),
  158. Vector3( pos.x+size.x , pos.y+size.y , pos.z ),
  159. Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z ),
  160. };
  161. for (int i=0;i<p_plane_count;i++) { //for each plane
  162. const Plane & plane=p_planes[i];
  163. bool all_points_over=true;
  164. //test if it has all points over!
  165. for (int j=0;j<8;j++) {
  166. if (!plane.is_point_over( points[j] )) {
  167. all_points_over=false;
  168. break;
  169. }
  170. }
  171. if (all_points_over) {
  172. return false;
  173. }
  174. }
  175. return true;
  176. #endif
  177. }
  178. bool AABB::has_point(const Vector3& p_point) const {
  179. if (p_point.x<pos.x)
  180. return false;
  181. if (p_point.y<pos.y)
  182. return false;
  183. if (p_point.z<pos.z)
  184. return false;
  185. if (p_point.x>pos.x+size.x)
  186. return false;
  187. if (p_point.y>pos.y+size.y)
  188. return false;
  189. if (p_point.z>pos.z+size.z)
  190. return false;
  191. return true;
  192. }
  193. inline void AABB::expand_to(const Vector3& p_vector) {
  194. Vector3 begin=pos;
  195. Vector3 end=pos+size;
  196. if (p_vector.x<begin.x)
  197. begin.x=p_vector.x;
  198. if (p_vector.y<begin.y)
  199. begin.y=p_vector.y;
  200. if (p_vector.z<begin.z)
  201. begin.z=p_vector.z;
  202. if (p_vector.x>end.x)
  203. end.x=p_vector.x;
  204. if (p_vector.y>end.y)
  205. end.y=p_vector.y;
  206. if (p_vector.z>end.z)
  207. end.z=p_vector.z;
  208. pos=begin;
  209. size=end-begin;
  210. }
  211. void AABB::project_range_in_plane(const Plane& p_plane,float &r_min,float& r_max) const {
  212. Vector3 half_extents( size.x * 0.5, size.y * 0.5, size.z * 0.5 );
  213. Vector3 center( pos.x + half_extents.x, pos.y + half_extents.y, pos.z + half_extents.z );
  214. float length = p_plane.normal.abs().dot(half_extents);
  215. float distance = p_plane.distance_to( center );
  216. r_min = distance - length;
  217. r_max = distance + length;
  218. }
  219. inline real_t AABB::get_longest_axis_size() const {
  220. real_t max_size=size.x;
  221. if (size.y > max_size ) {
  222. max_size=size.y;
  223. }
  224. if (size.z > max_size ) {
  225. max_size=size.z;
  226. }
  227. return max_size;
  228. }
  229. inline real_t AABB::get_shortest_axis_size() const {
  230. real_t max_size=size.x;
  231. if (size.y < max_size ) {
  232. max_size=size.y;
  233. }
  234. if (size.z < max_size ) {
  235. max_size=size.z;
  236. }
  237. return max_size;
  238. }
  239. typedef AABB Rect3;
  240. #endif // AABB_H