box.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (c), Recep Aslantas.
  3. *
  4. * MIT License (MIT), http://opensource.org/licenses/MIT
  5. * Full license can be found in the LICENSE file
  6. */
  7. #ifndef cglm_box_h
  8. #define cglm_box_h
  9. #include "common.h"
  10. #include "vec3.h"
  11. #include "vec4.h"
  12. #include "util.h"
  13. /*!
  14. * @brief apply transform to Axis-Aligned Bounding Box
  15. *
  16. * @param[in] box bounding box
  17. * @param[in] m transform matrix
  18. * @param[out] dest transformed bounding box
  19. */
  20. CGLM_INLINE
  21. void
  22. glm_aabb_transform(vec3 box[2], mat4 m, vec3 dest[2]) {
  23. vec3 v[2], xa, xb, ya, yb, za, zb;
  24. glm_vec3_scale(m[0], box[0][0], xa);
  25. glm_vec3_scale(m[0], box[1][0], xb);
  26. glm_vec3_scale(m[1], box[0][1], ya);
  27. glm_vec3_scale(m[1], box[1][1], yb);
  28. glm_vec3_scale(m[2], box[0][2], za);
  29. glm_vec3_scale(m[2], box[1][2], zb);
  30. /* translation + min(xa, xb) + min(ya, yb) + min(za, zb) */
  31. glm_vec3(m[3], v[0]);
  32. glm_vec3_minadd(xa, xb, v[0]);
  33. glm_vec3_minadd(ya, yb, v[0]);
  34. glm_vec3_minadd(za, zb, v[0]);
  35. /* translation + max(xa, xb) + max(ya, yb) + max(za, zb) */
  36. glm_vec3(m[3], v[1]);
  37. glm_vec3_maxadd(xa, xb, v[1]);
  38. glm_vec3_maxadd(ya, yb, v[1]);
  39. glm_vec3_maxadd(za, zb, v[1]);
  40. glm_vec3_copy(v[0], dest[0]);
  41. glm_vec3_copy(v[1], dest[1]);
  42. }
  43. /*!
  44. * @brief merges two AABB bounding box and creates new one
  45. *
  46. * two box must be in same space, if one of box is in different space then
  47. * you should consider to convert it's space by glm_box_space
  48. *
  49. * @param[in] box1 bounding box 1
  50. * @param[in] box2 bounding box 2
  51. * @param[out] dest merged bounding box
  52. */
  53. CGLM_INLINE
  54. void
  55. glm_aabb_merge(vec3 box1[2], vec3 box2[2], vec3 dest[2]) {
  56. dest[0][0] = glm_min(box1[0][0], box2[0][0]);
  57. dest[0][1] = glm_min(box1[0][1], box2[0][1]);
  58. dest[0][2] = glm_min(box1[0][2], box2[0][2]);
  59. dest[1][0] = glm_max(box1[1][0], box2[1][0]);
  60. dest[1][1] = glm_max(box1[1][1], box2[1][1]);
  61. dest[1][2] = glm_max(box1[1][2], box2[1][2]);
  62. }
  63. /*!
  64. * @brief crops a bounding box with another one.
  65. *
  66. * this could be useful for gettng a bbox which fits with view frustum and
  67. * object bounding boxes. In this case you crop view frustum box with objects
  68. * box
  69. *
  70. * @param[in] box bounding box 1
  71. * @param[in] cropBox crop box
  72. * @param[out] dest cropped bounding box
  73. */
  74. CGLM_INLINE
  75. void
  76. glm_aabb_crop(vec3 box[2], vec3 cropBox[2], vec3 dest[2]) {
  77. dest[0][0] = glm_max(box[0][0], cropBox[0][0]);
  78. dest[0][1] = glm_max(box[0][1], cropBox[0][1]);
  79. dest[0][2] = glm_max(box[0][2], cropBox[0][2]);
  80. dest[1][0] = glm_min(box[1][0], cropBox[1][0]);
  81. dest[1][1] = glm_min(box[1][1], cropBox[1][1]);
  82. dest[1][2] = glm_min(box[1][2], cropBox[1][2]);
  83. }
  84. /*!
  85. * @brief crops a bounding box with another one.
  86. *
  87. * this could be useful for gettng a bbox which fits with view frustum and
  88. * object bounding boxes. In this case you crop view frustum box with objects
  89. * box
  90. *
  91. * @param[in] box bounding box
  92. * @param[in] cropBox crop box
  93. * @param[in] clampBox miniumum box
  94. * @param[out] dest cropped bounding box
  95. */
  96. CGLM_INLINE
  97. void
  98. glm_aabb_crop_until(vec3 box[2],
  99. vec3 cropBox[2],
  100. vec3 clampBox[2],
  101. vec3 dest[2]) {
  102. glm_aabb_crop(box, cropBox, dest);
  103. glm_aabb_merge(clampBox, dest, dest);
  104. }
  105. /*!
  106. * @brief check if AABB intersects with frustum planes
  107. *
  108. * this could be useful for frustum culling using AABB.
  109. *
  110. * OPTIMIZATION HINT:
  111. * if planes order is similar to LEFT, RIGHT, BOTTOM, TOP, NEAR, FAR
  112. * then this method should run even faster because it would only use two
  113. * planes if object is not inside the two planes
  114. * fortunately cglm extracts planes as this order! just pass what you got!
  115. *
  116. * @param[in] box bounding box
  117. * @param[in] planes frustum planes
  118. */
  119. CGLM_INLINE
  120. bool
  121. glm_aabb_frustum(vec3 box[2], vec4 planes[6]) {
  122. float *p, dp;
  123. int i;
  124. for (i = 0; i < 6; i++) {
  125. p = planes[i];
  126. dp = p[0] * box[p[0] > 0.0f][0]
  127. + p[1] * box[p[1] > 0.0f][1]
  128. + p[2] * box[p[2] > 0.0f][2];
  129. if (dp < -p[3])
  130. return false;
  131. }
  132. return true;
  133. }
  134. /*!
  135. * @brief invalidate AABB min and max values
  136. *
  137. * @param[in, out] box bounding box
  138. */
  139. CGLM_INLINE
  140. void
  141. glm_aabb_invalidate(vec3 box[2]) {
  142. glm_vec3_broadcast(FLT_MAX, box[0]);
  143. glm_vec3_broadcast(-FLT_MAX, box[1]);
  144. }
  145. /*!
  146. * @brief check if AABB is valid or not
  147. *
  148. * @param[in] box bounding box
  149. */
  150. CGLM_INLINE
  151. bool
  152. glm_aabb_isvalid(vec3 box[2]) {
  153. return glm_vec3_max(box[0]) != FLT_MAX
  154. && glm_vec3_min(box[1]) != -FLT_MAX;
  155. }
  156. /*!
  157. * @brief distance between of min and max
  158. *
  159. * @param[in] box bounding box
  160. */
  161. CGLM_INLINE
  162. float
  163. glm_aabb_size(vec3 box[2]) {
  164. return glm_vec3_distance(box[0], box[1]);
  165. }
  166. /*!
  167. * @brief radius of sphere which surrounds AABB
  168. *
  169. * @param[in] box bounding box
  170. */
  171. CGLM_INLINE
  172. float
  173. glm_aabb_radius(vec3 box[2]) {
  174. return glm_aabb_size(box) * 0.5f;
  175. }
  176. /*!
  177. * @brief computes center point of AABB
  178. *
  179. * @param[in] box bounding box
  180. * @param[out] dest center of bounding box
  181. */
  182. CGLM_INLINE
  183. void
  184. glm_aabb_center(vec3 box[2], vec3 dest) {
  185. glm_vec3_center(box[0], box[1], dest);
  186. }
  187. /*!
  188. * @brief check if two AABB intersects
  189. *
  190. * @param[in] box bounding box
  191. * @param[in] other other bounding box
  192. */
  193. CGLM_INLINE
  194. bool
  195. glm_aabb_aabb(vec3 box[2], vec3 other[2]) {
  196. return (box[0][0] <= other[1][0] && box[1][0] >= other[0][0])
  197. && (box[0][1] <= other[1][1] && box[1][1] >= other[0][1])
  198. && (box[0][2] <= other[1][2] && box[1][2] >= other[0][2]);
  199. }
  200. /*!
  201. * @brief check if AABB intersects with sphere
  202. *
  203. * https://github.com/erich666/GraphicsGems/blob/master/gems/BoxSphere.c
  204. * Solid Box - Solid Sphere test.
  205. *
  206. * Sphere Representation in cglm: [center.x, center.y, center.z, radii]
  207. *
  208. * @param[in] box solid bounding box
  209. * @param[in] s solid sphere
  210. */
  211. CGLM_INLINE
  212. bool
  213. glm_aabb_sphere(vec3 box[2], vec4 s) {
  214. float dmin;
  215. int a, b, c;
  216. a = (s[0] < box[0][0]) + (s[0] > box[1][0]);
  217. b = (s[1] < box[0][1]) + (s[1] > box[1][1]);
  218. c = (s[2] < box[0][2]) + (s[2] > box[1][2]);
  219. dmin = glm_pow2((s[0] - box[!(a - 1)][0]) * (a != 0))
  220. + glm_pow2((s[1] - box[!(b - 1)][1]) * (b != 0))
  221. + glm_pow2((s[2] - box[!(c - 1)][2]) * (c != 0));
  222. return dmin <= glm_pow2(s[3]);
  223. }
  224. /*!
  225. * @brief check if point is inside of AABB
  226. *
  227. * @param[in] box bounding box
  228. * @param[in] point point
  229. */
  230. CGLM_INLINE
  231. bool
  232. glm_aabb_point(vec3 box[2], vec3 point) {
  233. return (point[0] >= box[0][0] && point[0] <= box[1][0])
  234. && (point[1] >= box[0][1] && point[1] <= box[1][1])
  235. && (point[2] >= box[0][2] && point[2] <= box[1][2]);
  236. }
  237. /*!
  238. * @brief check if AABB contains other AABB
  239. *
  240. * @param[in] box bounding box
  241. * @param[in] other other bounding box
  242. */
  243. CGLM_INLINE
  244. bool
  245. glm_aabb_contains(vec3 box[2], vec3 other[2]) {
  246. return (box[0][0] <= other[0][0] && box[1][0] >= other[1][0])
  247. && (box[0][1] <= other[0][1] && box[1][1] >= other[1][1])
  248. && (box[0][2] <= other[0][2] && box[1][2] >= other[1][2]);
  249. }
  250. #endif /* cglm_box_h */