aabb.h 11 KB

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