aabb.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #pragma once
  2. #ifndef GUL_AABB_H
  3. #define GUL_AABB_H
  4. #include <cstdint>
  5. #include <stdexcept>
  6. #include <glm/glm.hpp>
  7. namespace gul
  8. {
  9. template< typename _T, size_t _dim>
  10. class aabb_t
  11. {
  12. public:
  13. using value_type = _T;
  14. using vec_type = glm::vec< static_cast<int>(_dim), value_type, glm::defaultp>;// array_type;
  15. /// Lower bound of AABB in each dimension.
  16. vec_type lowerBound;
  17. /// Upper bound of AABB in each dimension.
  18. vec_type upperBound;
  19. aabb_t()
  20. {
  21. }
  22. static constexpr size_t dimensions()
  23. {
  24. return _dim;
  25. }
  26. aabb_t(const value_type& _halfExtents) : aabb_t(-vec_type(_halfExtents), vec_type(_halfExtents))
  27. {
  28. }
  29. aabb_t(const vec_type& _halfExtents) : aabb_t(-_halfExtents, _halfExtents)
  30. {
  31. }
  32. aabb_t(const vec_type& lowerBound_,
  33. const vec_type& upperBound_) : lowerBound(lowerBound_),
  34. upperBound(upperBound_)
  35. {
  36. // Validate that the upper bounds exceed the lower bounds.
  37. const int32_t dim = static_cast<int32_t>(dimensions());
  38. for (int32_t i=0;i < dim ;i++)
  39. {
  40. // Validate the bound.
  41. if (lowerBound[i] > upperBound[i])
  42. {
  43. throw std::invalid_argument("[ERROR]: AABB lower bound is greater than the upper bound!");
  44. }
  45. }
  46. }
  47. value_type computeSurfaceArea() const
  48. {
  49. // Sum of "area" of all the sides.
  50. value_type sum = 0;
  51. // General formula for one side: hold one dimension constant
  52. // and multiply by all the other ones.
  53. const int32_t dim = static_cast<int32_t>(dimensions());
  54. for (int32_t d1 = 0; d1 < dim; d1++)
  55. {
  56. // "Area" of current side.
  57. value_type product = 1;
  58. for (int32_t d2 = 0; d2 < dim; d2++)
  59. {
  60. if (d1 == d2)
  61. continue;
  62. value_type dx = upperBound[d2] - lowerBound[d2];
  63. product *= dx;
  64. }
  65. // Update the sum.
  66. sum += product;
  67. }
  68. return 2 * sum;
  69. }
  70. void translate(vec_type const & v)
  71. {
  72. lowerBound += v;
  73. upperBound += v;
  74. }
  75. // static aabb_t merge(const aabb_t & aabb1, const aabb_t & aabb2)
  76. // {
  77. // aabb_t out;
  78. // for (uint32_t i=0;i<dimensions();i++)
  79. // {
  80. // out.lowerBound[i] = std::min(aabb1.lowerBound[i], aabb2.lowerBound[i]);
  81. // out.upperBound[i] = std::max(aabb1.upperBound[i], aabb2.upperBound[i]);
  82. // }
  83. // return out;
  84. // }
  85. bool contains(const aabb_t & aabb) const
  86. {
  87. int d = static_cast<int>(dimensions());
  88. for (int i=0;i<d;i++)
  89. {
  90. if (aabb.lowerBound[i] < lowerBound[i]) return false;
  91. if (aabb.upperBound[i] > upperBound[i]) return false;
  92. }
  93. return true;
  94. }
  95. bool contains(const vec_type & p) const
  96. {
  97. for (uint32_t i=0;i<p.length();i++)
  98. {
  99. if (p[i] < lowerBound[i]) return false;
  100. if (p[i] > upperBound[i]) return false;
  101. }
  102. return true;
  103. }
  104. void expand(const vec_type & p)
  105. {
  106. lowerBound = glm::min(lowerBound, p);
  107. upperBound = glm::max(upperBound, p);
  108. }
  109. void expand(const aabb_t & p)
  110. {
  111. expand( p.lowerBound );
  112. expand( p.upperBound);
  113. }
  114. void scale(vec_type const & p)
  115. {
  116. auto W = (upperBound-lowerBound) * 0.5f;
  117. auto M = computeCentre();
  118. W *= p;
  119. upperBound = M + W;
  120. lowerBound = M - W;
  121. }
  122. bool overlaps(const aabb_t & aabb, bool touchIsOverlap) const
  123. {
  124. bool rv = true;
  125. const int32_t dim = static_cast<int32_t>(dimensions());
  126. if (touchIsOverlap)
  127. {
  128. for (int32_t i = 0; i < dim ; ++i)
  129. {
  130. if (aabb.upperBound[i] < lowerBound[i] || aabb.lowerBound[i] > upperBound[i])
  131. {
  132. rv = false;
  133. break;
  134. }
  135. }
  136. }
  137. else
  138. {
  139. for (int32_t i = 0; i < dim; ++i)
  140. {
  141. if (aabb.upperBound[i] <= lowerBound[i] || aabb.lowerBound[i] >= upperBound[i])
  142. {
  143. rv = false;
  144. break;
  145. }
  146. }
  147. }
  148. return rv;
  149. }
  150. vec_type computeCentre() const
  151. {
  152. vec_type position;
  153. int32_t dim = static_cast<int32_t>(dimensions());
  154. for ( int32_t i=0;i<dim;i++)
  155. position[i] = static_cast<value_type>(0.5) * (lowerBound[i] + upperBound[i]);
  156. return position;
  157. }
  158. aabb_t<_T, _dim> transform(const glm::mat4 & M) const
  159. {
  160. glm::vec4 p[] =
  161. {
  162. M*glm::vec4(lowerBound.x, lowerBound.y, lowerBound.z,1.0f),
  163. M*glm::vec4(lowerBound.x, lowerBound.y, upperBound.z,1.0f),
  164. M*glm::vec4(lowerBound.x, upperBound.y, lowerBound.z,1.0f),
  165. M*glm::vec4(lowerBound.x, upperBound.y, upperBound.z,1.0f),
  166. M*glm::vec4(upperBound.x, lowerBound.y, lowerBound.z,1.0f),
  167. M*glm::vec4(upperBound.x, lowerBound.y, upperBound.z,1.0f),
  168. M*glm::vec4(upperBound.x, upperBound.y, lowerBound.z,1.0f),
  169. M*glm::vec4(upperBound.x, upperBound.y, upperBound.z,1.0f)
  170. };
  171. aabb_t<_T, _dim> out;
  172. out.lowerBound = p[0];
  173. out.upperBound = p[0];
  174. for(int i=0 ; i < 7 ; i++)
  175. {
  176. out.lowerBound = glm::min( out.lowerBound, vec_type(p[i]) );
  177. out.upperBound = glm::max( out.upperBound, vec_type(p[i]) );
  178. }
  179. return out;
  180. }
  181. };
  182. template<typename _T, size_t _dim>
  183. inline aabb_t<_T,_dim> merge(const aabb_t<_T,_dim> & aabb1, const aabb_t<_T,_dim> & aabb2)
  184. {
  185. aabb_t<_T,_dim> out;
  186. constexpr int d = static_cast<int>(_dim);
  187. for (int i=0;i<d;i++)
  188. {
  189. out.lowerBound[i] = std::min(aabb1.lowerBound[i], aabb2.lowerBound[i]);
  190. out.upperBound[i] = std::max(aabb1.upperBound[i], aabb2.upperBound[i]);
  191. }
  192. return out;
  193. }
  194. template<typename _T, size_t _dim>
  195. inline aabb_t<_T,_dim> contains(const aabb_t<_T,_dim> & a1, const aabb_t<_T,_dim> & a2)
  196. {
  197. for (uint32_t i=0;i<_dim;i++)
  198. {
  199. if (a2.lowerBound[i] < a1.lowerBound[i]) return false;
  200. if (a2.upperBound[i] > a1.upperBound[i]) return false;
  201. }
  202. return true;
  203. }
  204. template<typename _T, size_t _dim>
  205. inline aabb_t<_T,_dim> overlaps(const aabb_t<_T,_dim> & a1, const aabb_t<_T,_dim> & a2, bool touchIsOverlap)
  206. {
  207. bool rv = true;
  208. if (touchIsOverlap)
  209. {
  210. for (uint32_t i = 0; i < _dim ; ++i)
  211. {
  212. if (a2.upperBound[i] < a1.lowerBound[i] || a2.lowerBound[i] > a1.upperBound[i])
  213. {
  214. rv = false;
  215. break;
  216. }
  217. }
  218. }
  219. else
  220. {
  221. for (uint32_t i = 0; i < _dim; ++i)
  222. {
  223. if (a2.upperBound[i] <= a1.lowerBound[i] || a2.lowerBound[i] >= a1.upperBound[i])
  224. {
  225. rv = false;
  226. break;
  227. }
  228. }
  229. }
  230. return rv;
  231. }
  232. using AABB = aabb_t<float, 3>;
  233. using bb3f = aabb_t<float,3>;
  234. using bb2f = aabb_t<float,2>;
  235. template<typename T, size_t _dim>
  236. bool intersects(aabb_t<T,_dim> const & A, aabb_t<T,_dim> const & B)
  237. {
  238. return A.overlaps(B,true);
  239. }
  240. }
  241. #endif