1
0

box.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 cglms_boxs_h
  8. #define cglms_boxs_h
  9. #include "../common.h"
  10. #include "../types-struct.h"
  11. #include "../box.h"
  12. #include "vec3.h"
  13. #include "vec4.h"
  14. #include "mat4.h"
  15. /* api definition */
  16. #define glms_aabb_(NAME) CGLM_STRUCTAPI(aabb, NAME)
  17. /*!
  18. * @brief apply transform to Axis-Aligned Bounding Box
  19. *
  20. * @param[in] box bounding box
  21. * @param[in] m transform matrix
  22. * @param[out] dest transformed bounding box
  23. */
  24. CGLM_INLINE
  25. void
  26. glms_aabb_(transform)(vec3s box[2], mat4s m, vec3s dest[2]) {
  27. vec3 rawBox[2];
  28. vec3 rawDest[2];
  29. glms_vec3_(unpack)(rawBox, box, 2);
  30. glm_aabb_transform(rawBox, m.raw, rawDest);
  31. glms_vec3_(pack)(dest, rawDest, 2);
  32. }
  33. /*!
  34. * @brief merges two AABB bounding box and creates new one
  35. *
  36. * two box must be in same space, if one of box is in different space then
  37. * you should consider to convert it's space by glm_box_space
  38. *
  39. * @param[in] box1 bounding box 1
  40. * @param[in] box2 bounding box 2
  41. * @param[out] dest merged bounding box
  42. */
  43. CGLM_INLINE
  44. void
  45. glms_aabb_(merge)(vec3s box1[2], vec3s box2[2], vec3s dest[2]) {
  46. vec3 rawBox1[2];
  47. vec3 rawBox2[2];
  48. vec3 rawDest[2];
  49. glms_vec3_(unpack)(rawBox1, box1, 2);
  50. glms_vec3_(unpack)(rawBox2, box2, 2);
  51. glm_aabb_merge(rawBox1, rawBox2, rawDest);
  52. glms_vec3_(pack)(dest, rawDest, 2);
  53. }
  54. /*!
  55. * @brief crops a bounding box with another one.
  56. *
  57. * this could be useful for getting a bbox which fits with view frustum and
  58. * object bounding boxes. In this case you crop view frustum box with objects
  59. * box
  60. *
  61. * @param[in] box bounding box 1
  62. * @param[in] cropBox crop box
  63. * @param[out] dest cropped bounding box
  64. */
  65. CGLM_INLINE
  66. void
  67. glms_aabb_(crop)(vec3s box[2], vec3s cropBox[2], vec3s dest[2]) {
  68. vec3 rawBox[2];
  69. vec3 rawCropBox[2];
  70. vec3 rawDest[2];
  71. glms_vec3_(unpack)(rawBox, box, 2);
  72. glms_vec3_(unpack)(rawCropBox, cropBox, 2);
  73. glm_aabb_crop(rawBox, rawCropBox, rawDest);
  74. glms_vec3_(pack)(dest, rawDest, 2);
  75. }
  76. /*!
  77. * @brief crops a bounding box with another one.
  78. *
  79. * this could be useful for getting a bbox which fits with view frustum and
  80. * object bounding boxes. In this case you crop view frustum box with objects
  81. * box
  82. *
  83. * @param[in] box bounding box
  84. * @param[in] cropBox crop box
  85. * @param[in] clampBox minimum box
  86. * @param[out] dest cropped bounding box
  87. */
  88. CGLM_INLINE
  89. void
  90. glms_aabb_(crop_until)(vec3s box[2],
  91. vec3s cropBox[2],
  92. vec3s clampBox[2],
  93. vec3s dest[2]) {
  94. glms_aabb_(crop)(box, cropBox, dest);
  95. glms_aabb_(merge)(clampBox, dest, dest);
  96. }
  97. /*!
  98. * @brief check if AABB intersects with frustum planes
  99. *
  100. * this could be useful for frustum culling using AABB.
  101. *
  102. * OPTIMIZATION HINT:
  103. * if planes order is similar to LEFT, RIGHT, BOTTOM, TOP, NEAR, FAR
  104. * then this method should run even faster because it would only use two
  105. * planes if object is not inside the two planes
  106. * fortunately cglm extracts planes as this order! just pass what you got!
  107. *
  108. * @param[in] box bounding box
  109. * @param[in] planes frustum planes
  110. */
  111. CGLM_INLINE
  112. bool
  113. glms_aabb_(frustum)(vec3s box[2], vec4s planes[6]) {
  114. vec3 rawBox[2];
  115. vec4 rawPlanes[6];
  116. glms_vec3_(unpack)(rawBox, box, 2);
  117. glms_vec4_(unpack)(rawPlanes, planes, 6);
  118. return glm_aabb_frustum(rawBox, rawPlanes);
  119. }
  120. /*!
  121. * @brief invalidate AABB min and max values
  122. *
  123. * @param[in, out] box bounding box
  124. */
  125. CGLM_INLINE
  126. void
  127. glms_aabb_(invalidate)(vec3s box[2]) {
  128. box[0] = glms_vec3_(broadcast)(FLT_MAX);
  129. box[1] = glms_vec3_(broadcast)(-FLT_MAX);
  130. }
  131. /*!
  132. * @brief check if AABB is valid or not
  133. *
  134. * @param[in] box bounding box
  135. */
  136. CGLM_INLINE
  137. bool
  138. glms_aabb_(isvalid)(vec3s box[2]) {
  139. vec3 rawBox[2];
  140. glms_vec3_(unpack)(rawBox, box, 2);
  141. return glm_aabb_isvalid(rawBox);
  142. }
  143. /*!
  144. * @brief distance between of min and max
  145. *
  146. * @param[in] box bounding box
  147. */
  148. CGLM_INLINE
  149. float
  150. glms_aabb_(size)(vec3s box[2]) {
  151. return glm_vec3_distance(box[0].raw, box[1].raw);
  152. }
  153. /*!
  154. * @brief radius of sphere which surrounds AABB
  155. *
  156. * @param[in] box bounding box
  157. */
  158. CGLM_INLINE
  159. float
  160. glms_aabb_(radius)(vec3s box[2]) {
  161. return glms_aabb_(size)(box) * 0.5f;
  162. }
  163. /*!
  164. * @brief computes center point of AABB
  165. *
  166. * @param[in] box bounding box
  167. * @returns center of bounding box
  168. */
  169. CGLM_INLINE
  170. vec3s
  171. glms_aabb_(center)(vec3s box[2]) {
  172. return glms_vec3_(center)(box[0], box[1]);
  173. }
  174. /*!
  175. * @brief check if two AABB intersects
  176. *
  177. * @param[in] box bounding box
  178. * @param[in] other other bounding box
  179. */
  180. CGLM_INLINE
  181. bool
  182. glms_aabb_(aabb)(vec3s box[2], vec3s other[2]) {
  183. vec3 rawBox[2];
  184. vec3 rawOther[2];
  185. glms_vec3_(unpack)(rawBox, box, 2);
  186. glms_vec3_(unpack)(rawOther, other, 2);
  187. return glm_aabb_aabb(rawBox, rawOther);
  188. }
  189. /*!
  190. * @brief check if AABB intersects with sphere
  191. *
  192. * https://github.com/erich666/GraphicsGems/blob/master/gems/BoxSphere.c
  193. * Solid Box - Solid Sphere test.
  194. *
  195. * @param[in] box solid bounding box
  196. * @param[in] s solid sphere
  197. */
  198. CGLM_INLINE
  199. bool
  200. glms_aabb_(sphere)(vec3s box[2], vec4s s) {
  201. vec3 rawBox[2];
  202. glms_vec3_(unpack)(rawBox, box, 2);
  203. return glm_aabb_sphere(rawBox, s.raw);
  204. }
  205. /*!
  206. * @brief check if point is inside of AABB
  207. *
  208. * @param[in] box bounding box
  209. * @param[in] point point
  210. */
  211. CGLM_INLINE
  212. bool
  213. glms_aabb_(point)(vec3s box[2], vec3s point) {
  214. vec3 rawBox[2];
  215. glms_vec3_(unpack)(rawBox, box, 2);
  216. return glm_aabb_point(rawBox, point.raw);
  217. }
  218. /*!
  219. * @brief check if AABB contains other AABB
  220. *
  221. * @param[in] box bounding box
  222. * @param[in] other other bounding box
  223. */
  224. CGLM_INLINE
  225. bool
  226. glms_aabb_(contains)(vec3s box[2], vec3s other[2]) {
  227. vec3 rawBox[2];
  228. vec3 rawOther[2];
  229. glms_vec3_(unpack)(rawBox, box, 2);
  230. glms_vec3_(unpack)(rawOther, other, 2);
  231. return glm_aabb_contains(rawBox, rawOther);
  232. }
  233. #endif /* cglms_boxs_h */