bounds.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <string.h>
  15. #include "bounds.h"
  16. #include "matrix3.h"
  17. #include "matrix4.h"
  18. #include "plane.h"
  19. void bounds_move(struct bounds *dst, const struct bounds *b,
  20. const struct vec3 *v)
  21. {
  22. vec3_add(&dst->min, &b->min, v);
  23. vec3_add(&dst->max, &b->max, v);
  24. }
  25. void bounds_scale(struct bounds *dst, const struct bounds *b,
  26. const struct vec3 *v)
  27. {
  28. vec3_mul(&dst->min, &b->min, v);
  29. vec3_mul(&dst->max, &b->max, v);
  30. }
  31. void bounds_merge(struct bounds *dst, const struct bounds *b1,
  32. const struct bounds *b2)
  33. {
  34. vec3_min(&dst->min, &b1->min, &b2->min);
  35. vec3_max(&dst->max, &b1->max, &b2->max);
  36. }
  37. void bounds_merge_point(struct bounds *dst, const struct bounds *b,
  38. const struct vec3 *v)
  39. {
  40. vec3_min(&dst->min, &b->min, v);
  41. vec3_max(&dst->max, &b->max, v);
  42. }
  43. void bounds_get_point(struct vec3 *dst, const struct bounds *b, unsigned int i)
  44. {
  45. if (i > 8)
  46. return;
  47. /*
  48. * Note:
  49. * 0 = min.x,min.y,min.z
  50. * 1 = min.x,min.y,MAX.z
  51. * 2 = min.x,MAX.y,min.z
  52. * 3 = min.x,MAX.y,MAX.z
  53. * 4 = MAX.x,min.y,min.z
  54. * 5 = MAX.x,min.y,MAX.z
  55. * 6 = MAX.x,MAX.y,min.z
  56. * 7 = MAX.x,MAX.y,MAX.z
  57. */
  58. if (i > 3) {
  59. dst->x = b->max.x;
  60. i -= 4;
  61. } else {
  62. dst->x = b->min.x;
  63. }
  64. if (i > 1) {
  65. dst->y = b->max.y;
  66. i -= 2;
  67. } else {
  68. dst->y = b->min.y;
  69. }
  70. dst->z = (i == 1) ? b->max.z : b->min.z;
  71. }
  72. void bounds_get_center(struct vec3 *dst, const struct bounds *b)
  73. {
  74. vec3_sub(dst, &b->max, &b->min);
  75. vec3_mulf(dst, dst, 0.5f);
  76. vec3_add(dst, dst, &b->min);
  77. }
  78. void bounds_transform(struct bounds *dst, const struct bounds *b,
  79. const struct matrix4 *m)
  80. {
  81. struct bounds temp;
  82. bool b_init = false;
  83. int i;
  84. memset(&temp, 0, sizeof(temp));
  85. for (i = 0; i < 8; i++) {
  86. struct vec3 p;
  87. bounds_get_point(&p, b, i);
  88. vec3_transform(&p, &p, m);
  89. if (!b_init) {
  90. vec3_copy(&temp.min, &p);
  91. vec3_copy(&temp.max, &p);
  92. b_init = true;
  93. } else {
  94. if (p.x < temp.min.x)
  95. temp.min.x = p.x;
  96. else if (p.x > temp.max.x)
  97. temp.max.x = p.x;
  98. if (p.y < temp.min.y)
  99. temp.min.y = p.y;
  100. else if (p.y > temp.max.y)
  101. temp.max.y = p.y;
  102. if (p.z < temp.min.z)
  103. temp.min.z = p.z;
  104. else if (p.z > temp.max.z)
  105. temp.max.z = p.z;
  106. }
  107. }
  108. bounds_copy(dst, &temp);
  109. }
  110. void bounds_transform3x4(struct bounds *dst, const struct bounds *b,
  111. const struct matrix3 *m)
  112. {
  113. struct bounds temp;
  114. bool b_init = false;
  115. int i;
  116. memset(&temp, 0, sizeof(temp));
  117. for (i = 0; i < 8; i++) {
  118. struct vec3 p;
  119. bounds_get_point(&p, b, i);
  120. vec3_transform3x4(&p, &p, m);
  121. if (!b_init) {
  122. vec3_copy(&temp.min, &p);
  123. vec3_copy(&temp.max, &p);
  124. b_init = true;
  125. } else {
  126. if (p.x < temp.min.x)
  127. temp.min.x = p.x;
  128. else if (p.x > temp.max.x)
  129. temp.max.x = p.x;
  130. if (p.y < temp.min.y)
  131. temp.min.y = p.y;
  132. else if (p.y > temp.max.y)
  133. temp.max.y = p.y;
  134. if (p.z < temp.min.z)
  135. temp.min.z = p.z;
  136. else if (p.z > temp.max.z)
  137. temp.max.z = p.z;
  138. }
  139. }
  140. bounds_copy(dst, &temp);
  141. }
  142. bool bounds_intersection_ray(const struct bounds *b, const struct vec3 *orig,
  143. const struct vec3 *dir, float *t)
  144. {
  145. float t_max = M_INFINITE;
  146. float t_min = -M_INFINITE;
  147. struct vec3 center, max_offset, box_offset;
  148. int i;
  149. bounds_get_center(&center, b);
  150. vec3_sub(&max_offset, &b->max, &center);
  151. vec3_sub(&box_offset, &center, orig);
  152. for (i = 0; i < 3; i++) {
  153. float e = box_offset.ptr[i];
  154. float f = dir->ptr[i];
  155. if (fabsf(f) > 0.0f) {
  156. float fi = 1.0f / f;
  157. float t1 = (e + max_offset.ptr[i]) * fi;
  158. float t2 = (e - max_offset.ptr[i]) * fi;
  159. if (t1 > t2) {
  160. if (t2 > t_min)
  161. t_min = t2;
  162. if (t1 < t_max)
  163. t_max = t1;
  164. } else {
  165. if (t1 > t_min)
  166. t_min = t1;
  167. if (t2 < t_max)
  168. t_max = t2;
  169. }
  170. if (t_min > t_max)
  171. return false;
  172. if (t_max < 0.0f)
  173. return false;
  174. } else if ((-e - max_offset.ptr[i]) > 0.0f ||
  175. (-e + max_offset.ptr[i]) < 0.0f) {
  176. return false;
  177. }
  178. }
  179. *t = (t_min > 0.0f) ? t_min : t_max;
  180. return true;
  181. }
  182. bool bounds_intersection_line(const struct bounds *b, const struct vec3 *p1,
  183. const struct vec3 *p2, float *t)
  184. {
  185. struct vec3 dir;
  186. float length;
  187. vec3_sub(&dir, p2, p1);
  188. length = vec3_len(&dir);
  189. if (length <= TINY_EPSILON)
  190. return false;
  191. vec3_mulf(&dir, &dir, 1.0f / length);
  192. if (!bounds_intersection_ray(b, p1, &dir, t))
  193. return false;
  194. *t /= length;
  195. return true;
  196. }
  197. bool bounds_plane_test(const struct bounds *b, const struct plane *p)
  198. {
  199. struct vec3 vmin, vmax;
  200. int i;
  201. for (i = 0; i < 3; i++) {
  202. if (p->dir.ptr[i] >= 0.0f) {
  203. vmin.ptr[i] = b->min.ptr[i];
  204. vmax.ptr[i] = b->max.ptr[i];
  205. } else {
  206. vmin.ptr[i] = b->max.ptr[i];
  207. vmax.ptr[i] = b->min.ptr[i];
  208. }
  209. }
  210. if (vec3_plane_dist(&vmin, p) > 0.0f)
  211. return BOUNDS_OUTSIDE;
  212. if (vec3_plane_dist(&vmax, p) >= 0.0f)
  213. return BOUNDS_PARTIAL;
  214. return BOUNDS_INSIDE;
  215. }
  216. bool bounds_under_plane(const struct bounds *b, const struct plane *p)
  217. {
  218. struct vec3 vmin;
  219. vmin.x = (p->dir.x < 0.0f) ? b->max.x : b->min.x;
  220. vmin.y = (p->dir.y < 0.0f) ? b->max.y : b->min.y;
  221. vmin.z = (p->dir.z < 0.0f) ? b->max.z : b->min.z;
  222. return (vec3_dot(&vmin, &p->dir) <= p->dist);
  223. }
  224. bool bounds_intersects(const struct bounds *b, const struct bounds *test,
  225. float epsilon)
  226. {
  227. return ((b->min.x - test->max.x) <= epsilon) &&
  228. ((test->min.x - b->max.x) <= epsilon) &&
  229. ((b->min.y - test->max.y) <= epsilon) &&
  230. ((test->min.y - b->max.y) <= epsilon) &&
  231. ((b->min.z - test->max.z) <= epsilon) &&
  232. ((test->min.z - b->max.z) <= epsilon);
  233. }
  234. bool bounds_intersects_obb(const struct bounds *b, const struct bounds *test,
  235. const struct matrix4 *m, float epsilon)
  236. {
  237. struct bounds b_tr, test_tr;
  238. struct matrix4 m_inv;
  239. matrix4_inv(&m_inv, m);
  240. bounds_transform(&b_tr, b, m);
  241. bounds_transform(&test_tr, test, &m_inv);
  242. return bounds_intersects(b, &test_tr, epsilon) &&
  243. bounds_intersects(&b_tr, test, epsilon);
  244. }
  245. bool bounds_intersects_obb3x4(const struct bounds *b, const struct bounds *test,
  246. const struct matrix3 *m, float epsilon)
  247. {
  248. struct bounds b_tr, test_tr;
  249. struct matrix3 m_inv;
  250. matrix3_transpose(&m_inv, m);
  251. bounds_transform3x4(&b_tr, b, m);
  252. bounds_transform3x4(&test_tr, test, &m_inv);
  253. return bounds_intersects(b, &test_tr, epsilon) &&
  254. bounds_intersects(&b_tr, test, epsilon);
  255. }
  256. static inline float vec3or_offset_len(const struct bounds *b,
  257. const struct vec3 *v)
  258. {
  259. struct vec3 temp1, temp2;
  260. vec3_sub(&temp1, &b->max, &b->min);
  261. vec3_abs(&temp2, v);
  262. return vec3_dot(&temp1, &temp2);
  263. }
  264. float bounds_min_dist(const struct bounds *b, const struct plane *p)
  265. {
  266. struct vec3 center;
  267. float vec_len = vec3or_offset_len(b, &p->dir) * 0.5f;
  268. float center_dist;
  269. bounds_get_center(&center, b);
  270. center_dist = vec3_plane_dist(&center, p);
  271. return p->dist + center_dist - vec_len;
  272. }