aabb2d.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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_aabb2ds_h
  8. #define cglms_aabb2ds_h
  9. #include "../common.h"
  10. #include "../types-struct.h"
  11. #include "../aabb2d.h"
  12. #include "vec2.h"
  13. #include "vec4.h"
  14. #include "mat4.h"
  15. /* api definition */
  16. #define glms_aabb2d_(NAME) CGLM_STRUCTAPI(aabb2d, NAME)
  17. /*!
  18. * @brief apply transform to Axis-Aligned Bounding Box
  19. *
  20. * @param[in] aabb bounding box
  21. * @param[in] m transform matrix
  22. * @param[out] dest transformed bounding box
  23. */
  24. CGLM_INLINE
  25. void
  26. glms_aabb2d_(transform)(vec2s aabb[2], mat3s m, vec2s dest[2]) {
  27. vec2 rawAabb[2];
  28. vec2 rawDest[2];
  29. glms_vec2_(unpack)(rawAabb, aabb, 2);
  30. glm_aabb2d_transform(rawAabb, m.raw, rawDest);
  31. glms_vec2_(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] aabb1 bounding box 1
  40. * @param[in] aabb2 bounding box 2
  41. * @param[out] dest merged bounding box
  42. */
  43. CGLM_INLINE
  44. void
  45. glms_aabb2d_(merge)(vec2s aabb1[2], vec2s aabb2[2], vec2s dest[2]) {
  46. vec2 rawAabb1[2];
  47. vec2 rawAabb2[2];
  48. vec2 rawDest[2];
  49. glms_vec2_(unpack)(rawAabb1, aabb1, 2);
  50. glms_vec2_(unpack)(rawAabb2, aabb2, 2);
  51. glm_aabb2d_merge(rawAabb1, rawAabb2, rawDest);
  52. glms_vec2_(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] aabb bounding box 1
  62. * @param[in] cropAabb crop box
  63. * @param[out] dest cropped bounding box
  64. */
  65. CGLM_INLINE
  66. void
  67. glms_aabb2d_(crop)(vec2s aabb[2], vec2s cropAabb[2], vec2s dest[2]) {
  68. vec2 rawAabb[2];
  69. vec2 rawCropAabb[2];
  70. vec2 rawDest[2];
  71. glms_vec2_(unpack)(rawAabb, aabb, 2);
  72. glms_vec2_(unpack)(rawCropAabb, cropAabb, 2);
  73. glm_aabb2d_crop(rawAabb, rawCropAabb, rawDest);
  74. glms_vec2_(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] aabb bounding box
  84. * @param[in] cropAabb crop box
  85. * @param[in] clampAabb minimum box
  86. * @param[out] dest cropped bounding box
  87. */
  88. CGLM_INLINE
  89. void
  90. glms_aabb2d_(crop_until)(vec2s aabb[2],
  91. vec2s cropAabb[2],
  92. vec2s clampAabb[2],
  93. vec2s dest[2]) {
  94. glms_aabb2d_(crop)(aabb, cropAabb, dest);
  95. glms_aabb2d_(merge)(clampAabb, dest, dest);
  96. }
  97. /*!
  98. * @brief invalidate AABB min and max values
  99. *
  100. * @param[in, out] aabb bounding box
  101. */
  102. CGLM_INLINE
  103. void
  104. glms_aabb2d_(invalidate)(vec2s box[2]) {
  105. box[0] = glms_vec2_(fill)(FLT_MAX);
  106. box[1] = glms_vec2_(fill)(-FLT_MAX);
  107. }
  108. /*!
  109. * @brief check if AABB is valid or not
  110. *
  111. * @param[in] aabb bounding box
  112. */
  113. CGLM_INLINE
  114. bool
  115. glms_aabb2d_(isvalid)(vec2s aabb[2]) {
  116. vec2 rawAabb[2];
  117. glms_vec2_(unpack)(rawAabb, aabb, 2);
  118. return glm_aabb2d_isvalid(rawAabb);
  119. }
  120. /*!
  121. * @brief distance between of min and max
  122. *
  123. * @param[in] aabb bounding box
  124. */
  125. CGLM_INLINE
  126. float
  127. glms_aabb2d_(diag)(vec2s aabb[2]) {
  128. vec2 rawAabb[2];
  129. glms_vec2_(unpack)(rawAabb, aabb, 2);
  130. return glm_aabb2d_diag(rawAabb);
  131. }
  132. /*!
  133. * @brief size of aabb
  134. *
  135. * @param[in] aabb bounding aabb
  136. * @param[out] dest size
  137. */
  138. CGLM_INLINE
  139. vec2s
  140. glms_aabb2d_(sizev)(vec2s aabb[2]) {
  141. vec2s size;
  142. vec2 rawAabb[2];
  143. glms_vec2_(unpack)(rawAabb, aabb, 2);
  144. glm_aabb2d_sizev(rawAabb, size.raw);
  145. return size;
  146. }
  147. /*!
  148. * @brief radius of sphere which surrounds AABB
  149. *
  150. * @param[in] aabb bounding box
  151. */
  152. CGLM_INLINE
  153. float
  154. glms_aabb2d_(radius)(vec2s aabb[2]) {
  155. return glms_aabb2d_(size)(aabb) * 0.5f;
  156. }
  157. /*!
  158. * @brief computes center point of AABB
  159. *
  160. * @param[in] aabb bounding box
  161. * @returns center of bounding box
  162. */
  163. CGLM_INLINE
  164. vec2s
  165. glms_aabb2d_(center)(vec2s aabb[2]) {
  166. return glms_vec2_(center)(aabb[0], aabb[1]);
  167. }
  168. /*!
  169. * @brief check if two AABB intersects
  170. *
  171. * @param[in] aabb bounding box
  172. * @param[in] other other bounding box
  173. */
  174. CGLM_INLINE
  175. bool
  176. glms_aabb2d_(aabb)(vec2s aabb[2], vec2s other[2]) {
  177. vec2 rawAabb[2];
  178. vec2 rawOther[2];
  179. glms_vec2_(unpack)(rawAabb, aabb, 2);
  180. glms_vec2_(unpack)(rawOther, other, 2);
  181. return glm_aabb2d_aabb(rawAabb, rawOther);
  182. }
  183. /*!
  184. * @brief check if AABB intersects with a circle
  185. *
  186. * https://github.com/erich666/GraphicsGems/blob/master/gems/BoxSphere.c
  187. * Solid Box - Solid Sphere test.
  188. *
  189. * @param[in] aabb solid bounding box
  190. * @param[in] s solid sphere
  191. */
  192. CGLM_INLINE
  193. bool
  194. glms_aabb2d_(circle)(vec2s aabb[2], vec3s c) {
  195. vec2 rawAabb[2];
  196. glms_vec2_(unpack)(rawAabb, aabb, 2);
  197. return glm_aabb2d_circle(rawAabb, c.raw);
  198. }
  199. /*!
  200. * @brief check if point is inside of AABB
  201. *
  202. * @param[in] aabb bounding box
  203. * @param[in] point point
  204. */
  205. CGLM_INLINE
  206. bool
  207. glms_aabb2d_(point)(vec2s aabb[2], vec2s point) {
  208. vec2 rawAabb[2];
  209. glms_vec2_(unpack)(rawAabb, aabb, 2);
  210. return glm_aabb2d_point(rawAabb, point.raw);
  211. }
  212. /*!
  213. * @brief check if AABB contains other AABB
  214. *
  215. * @param[in] box bounding box
  216. * @param[in] other other bounding box
  217. */
  218. CGLM_INLINE
  219. bool
  220. glms_aabb2d_(contains)(vec2s aabb[2], vec2s other[2]) {
  221. vec2 rawAabb[2];
  222. vec2 rawOther[2];
  223. glms_vec2_(unpack)(rawAabb, aabb, 2);
  224. glms_vec2_(unpack)(rawOther, other, 2);
  225. return glm_aabb2d_contains(rawAabb, rawOther);
  226. }
  227. #endif /* cglms_aabb2ds_h */