aabb.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*************************************************************************/
  2. /* aabb.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 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 intersects_inclusive(const AABB& p_aabb) const; /// Both AABBs (or their faces) overlap
  56. _FORCE_INLINE_ bool encloses(const AABB & p_aabb) const; /// p_aabb is completely inside this
  57. AABB merge(const AABB& p_with) const;
  58. void merge_with(const AABB& p_aabb); ///merge with another AABB
  59. AABB intersection(const AABB& p_aabb) const; ///get box where two intersect, empty if no intersection occurs
  60. bool intersects_segment(const Vector3& p_from, const Vector3& p_to,Vector3* r_clip=NULL,Vector3* r_normal=NULL) const;
  61. bool intersects_ray(const Vector3& p_from, const Vector3& p_dir,Vector3* r_clip=NULL,Vector3* r_normal=NULL) const;
  62. _FORCE_INLINE_ bool smits_intersect_ray(const Vector3 &from,const Vector3& p_dir, float t0, float t1) const;
  63. _FORCE_INLINE_ bool intersects_convex_shape(const Plane *p_plane, int p_plane_count) const;
  64. bool intersects_plane(const Plane &p_plane) const;
  65. _FORCE_INLINE_ bool has_point(const Vector3& p_point) const;
  66. _FORCE_INLINE_ Vector3 get_support(const Vector3& p_normal) const;
  67. Vector3 get_longest_axis() const;
  68. int get_longest_axis_index() const;
  69. _FORCE_INLINE_ real_t get_longest_axis_size() const;
  70. Vector3 get_shortest_axis() const;
  71. int get_shortest_axis_index() const;
  72. _FORCE_INLINE_ real_t get_shortest_axis_size() const;
  73. AABB grow(real_t p_by) const;
  74. _FORCE_INLINE_ void grow_by(real_t p_amount);
  75. void get_edge(int p_edge,Vector3& r_from,Vector3& r_to) const;
  76. _FORCE_INLINE_ Vector3 get_endpoint(int p_point) const;
  77. AABB expand(const Vector3& p_vector) const;
  78. _FORCE_INLINE_ void project_range_in_plane(const Plane& p_plane,float &r_min,float& r_max) const;
  79. _FORCE_INLINE_ void expand_to(const Vector3& p_vector); /** expand to contain a point if necesary */
  80. operator String() const;
  81. _FORCE_INLINE_ AABB() {}
  82. inline AABB(const Vector3 &p_pos,const Vector3& p_size) { pos=p_pos; size=p_size; }
  83. };
  84. inline bool AABB::intersects(const AABB& p_aabb) const {
  85. if ( pos.x >= (p_aabb.pos.x + p_aabb.size.x) )
  86. return false;
  87. if ( (pos.x+size.x) <= p_aabb.pos.x )
  88. return false;
  89. if ( pos.y >= (p_aabb.pos.y + p_aabb.size.y) )
  90. return false;
  91. if ( (pos.y+size.y) <= p_aabb.pos.y )
  92. return false;
  93. if ( pos.z >= (p_aabb.pos.z + p_aabb.size.z) )
  94. return false;
  95. if ( (pos.z+size.z) <= p_aabb.pos.z )
  96. return false;
  97. return true;
  98. }
  99. inline bool AABB::intersects_inclusive(const AABB& p_aabb) const {
  100. if ( pos.x > (p_aabb.pos.x + p_aabb.size.x) )
  101. return false;
  102. if ( (pos.x+size.x) < p_aabb.pos.x )
  103. return false;
  104. if ( pos.y > (p_aabb.pos.y + p_aabb.size.y) )
  105. return false;
  106. if ( (pos.y+size.y) < p_aabb.pos.y )
  107. return false;
  108. if ( pos.z > (p_aabb.pos.z + p_aabb.size.z) )
  109. return false;
  110. if ( (pos.z+size.z) < p_aabb.pos.z )
  111. return false;
  112. return true;
  113. }
  114. inline bool AABB::encloses(const AABB & p_aabb) const {
  115. Vector3 src_min=pos;
  116. Vector3 src_max=pos+size;
  117. Vector3 dst_min=p_aabb.pos;
  118. Vector3 dst_max=p_aabb.pos+p_aabb.size;
  119. return (
  120. (src_min.x <= dst_min.x) &&
  121. (src_max.x > dst_max.x) &&
  122. (src_min.y <= dst_min.y) &&
  123. (src_max.y > dst_max.y) &&
  124. (src_min.z <= dst_min.z) &&
  125. (src_max.z > dst_max.z) );
  126. }
  127. Vector3 AABB::get_support(const Vector3& p_normal) const {
  128. Vector3 half_extents = size * 0.5;
  129. Vector3 ofs = pos + half_extents;
  130. return Vector3(
  131. (p_normal.x>0) ? -half_extents.x : half_extents.x,
  132. (p_normal.y>0) ? -half_extents.y : half_extents.y,
  133. (p_normal.z>0) ? -half_extents.z : half_extents.z
  134. )+ofs;
  135. }
  136. Vector3 AABB::get_endpoint(int p_point) const {
  137. switch(p_point) {
  138. case 0: return Vector3( pos.x , pos.y , pos.z );
  139. case 1: return Vector3( pos.x , pos.y , pos.z+size.z );
  140. case 2: return Vector3( pos.x , pos.y+size.y , pos.z );
  141. case 3: return Vector3( pos.x , pos.y+size.y , pos.z+size.z );
  142. case 4: return Vector3( pos.x+size.x , pos.y , pos.z );
  143. case 5: return Vector3( pos.x+size.x , pos.y , pos.z+size.z );
  144. case 6: return Vector3( pos.x+size.x , pos.y+size.y , pos.z );
  145. case 7: return Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z );
  146. };
  147. ERR_FAIL_V(Vector3());
  148. }
  149. bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count) const {
  150. #if 1
  151. Vector3 half_extents = size * 0.5;
  152. Vector3 ofs = pos + half_extents;
  153. for(int i=0;i<p_plane_count;i++) {
  154. const Plane &p=p_planes[i];
  155. Vector3 point(
  156. (p.normal.x>0) ? -half_extents.x : half_extents.x,
  157. (p.normal.y>0) ? -half_extents.y : half_extents.y,
  158. (p.normal.z>0) ? -half_extents.z : half_extents.z
  159. );
  160. point+=ofs;
  161. if (p.is_point_over(point))
  162. return false;
  163. }
  164. return true;
  165. #else
  166. //cache all points to check against!
  167. // #warning should be easy to optimize, just use the same as when taking the support and use only that point
  168. Vector3 points[8] = {
  169. Vector3( pos.x , pos.y , pos.z ),
  170. Vector3( pos.x , pos.y , pos.z+size.z ),
  171. Vector3( pos.x , pos.y+size.y , pos.z ),
  172. Vector3( pos.x , pos.y+size.y , pos.z+size.z ),
  173. Vector3( pos.x+size.x , pos.y , pos.z ),
  174. Vector3( pos.x+size.x , pos.y , pos.z+size.z ),
  175. Vector3( pos.x+size.x , pos.y+size.y , pos.z ),
  176. Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z ),
  177. };
  178. for (int i=0;i<p_plane_count;i++) { //for each plane
  179. const Plane & plane=p_planes[i];
  180. bool all_points_over=true;
  181. //test if it has all points over!
  182. for (int j=0;j<8;j++) {
  183. if (!plane.is_point_over( points[j] )) {
  184. all_points_over=false;
  185. break;
  186. }
  187. }
  188. if (all_points_over) {
  189. return false;
  190. }
  191. }
  192. return true;
  193. #endif
  194. }
  195. bool AABB::has_point(const Vector3& p_point) const {
  196. if (p_point.x<pos.x)
  197. return false;
  198. if (p_point.y<pos.y)
  199. return false;
  200. if (p_point.z<pos.z)
  201. return false;
  202. if (p_point.x>pos.x+size.x)
  203. return false;
  204. if (p_point.y>pos.y+size.y)
  205. return false;
  206. if (p_point.z>pos.z+size.z)
  207. return false;
  208. return true;
  209. }
  210. inline void AABB::expand_to(const Vector3& p_vector) {
  211. Vector3 begin=pos;
  212. Vector3 end=pos+size;
  213. if (p_vector.x<begin.x)
  214. begin.x=p_vector.x;
  215. if (p_vector.y<begin.y)
  216. begin.y=p_vector.y;
  217. if (p_vector.z<begin.z)
  218. begin.z=p_vector.z;
  219. if (p_vector.x>end.x)
  220. end.x=p_vector.x;
  221. if (p_vector.y>end.y)
  222. end.y=p_vector.y;
  223. if (p_vector.z>end.z)
  224. end.z=p_vector.z;
  225. pos=begin;
  226. size=end-begin;
  227. }
  228. void AABB::project_range_in_plane(const Plane& p_plane,float &r_min,float& r_max) const {
  229. Vector3 half_extents( size.x * 0.5, size.y * 0.5, size.z * 0.5 );
  230. Vector3 center( pos.x + half_extents.x, pos.y + half_extents.y, pos.z + half_extents.z );
  231. float length = p_plane.normal.abs().dot(half_extents);
  232. float distance = p_plane.distance_to( center );
  233. r_min = distance - length;
  234. r_max = distance + length;
  235. }
  236. inline real_t AABB::get_longest_axis_size() const {
  237. real_t max_size=size.x;
  238. if (size.y > max_size ) {
  239. max_size=size.y;
  240. }
  241. if (size.z > max_size ) {
  242. max_size=size.z;
  243. }
  244. return max_size;
  245. }
  246. inline real_t AABB::get_shortest_axis_size() const {
  247. real_t max_size=size.x;
  248. if (size.y < max_size ) {
  249. max_size=size.y;
  250. }
  251. if (size.z < max_size ) {
  252. max_size=size.z;
  253. }
  254. return max_size;
  255. }
  256. bool AABB::smits_intersect_ray(const Vector3 &from,const Vector3& dir, float t0, float t1) const {
  257. float divx=1.0/dir.x;
  258. float divy=1.0/dir.y;
  259. float divz=1.0/dir.z;
  260. Vector3 upbound=pos+size;
  261. float tmin, tmax, tymin, tymax, tzmin, tzmax;
  262. if (dir.x >= 0) {
  263. tmin = (pos.x - from.x) * divx;
  264. tmax = (upbound.x - from.x) * divx;
  265. }
  266. else {
  267. tmin = (upbound.x - from.x) * divx;
  268. tmax = (pos.x - from.x) * divx;
  269. }
  270. if (dir.y >= 0) {
  271. tymin = (pos.y - from.y) * divy;
  272. tymax = (upbound.y - from.y) * divy;
  273. }
  274. else {
  275. tymin = (upbound.y - from.y) * divy;
  276. tymax = (pos.y - from.y) * divy;
  277. }
  278. if ( (tmin > tymax) || (tymin > tmax) )
  279. return false;
  280. if (tymin > tmin)
  281. tmin = tymin;
  282. if (tymax < tmax)
  283. tmax = tymax;
  284. if (dir.z >= 0) {
  285. tzmin = (pos.z - from.z) * divz;
  286. tzmax = (upbound.z - from.z) * divz;
  287. }
  288. else {
  289. tzmin = (upbound.z - from.z) * divz;
  290. tzmax = (pos.z - from.z) * divz;
  291. }
  292. if ( (tmin > tzmax) || (tzmin > tmax) )
  293. return false;
  294. if (tzmin > tmin)
  295. tmin = tzmin;
  296. if (tzmax < tmax)
  297. tmax = tzmax;
  298. return ( (tmin < t1) && (tmax > t0) );
  299. }
  300. void AABB::grow_by(real_t p_amount) {
  301. pos.x-=p_amount;
  302. pos.y-=p_amount;
  303. pos.z-=p_amount;
  304. size.x+=2.0*p_amount;
  305. size.y+=2.0*p_amount;
  306. size.z+=2.0*p_amount;
  307. }
  308. typedef AABB Rect3;
  309. #endif // AABB_H