bounds.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <lain@obsproject.com>
  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, const struct vec3 *v)
  20. {
  21. vec3_add(&dst->min, &b->min, v);
  22. vec3_add(&dst->max, &b->max, v);
  23. }
  24. void bounds_scale(struct bounds *dst, const struct bounds *b, const struct vec3 *v)
  25. {
  26. vec3_mul(&dst->min, &b->min, v);
  27. vec3_mul(&dst->max, &b->max, v);
  28. }
  29. void bounds_merge(struct bounds *dst, const struct bounds *b1, const struct bounds *b2)
  30. {
  31. vec3_min(&dst->min, &b1->min, &b2->min);
  32. vec3_max(&dst->max, &b1->max, &b2->max);
  33. }
  34. void bounds_merge_point(struct bounds *dst, const struct bounds *b, const struct vec3 *v)
  35. {
  36. vec3_min(&dst->min, &b->min, v);
  37. vec3_max(&dst->max, &b->max, v);
  38. }
  39. void bounds_get_point(struct vec3 *dst, const struct bounds *b, unsigned int i)
  40. {
  41. if (i > 8)
  42. return;
  43. /*
  44. * Note:
  45. * 0 = min.x,min.y,min.z
  46. * 1 = min.x,min.y,MAX.z
  47. * 2 = min.x,MAX.y,min.z
  48. * 3 = min.x,MAX.y,MAX.z
  49. * 4 = MAX.x,min.y,min.z
  50. * 5 = MAX.x,min.y,MAX.z
  51. * 6 = MAX.x,MAX.y,min.z
  52. * 7 = MAX.x,MAX.y,MAX.z
  53. */
  54. if (i > 3) {
  55. dst->x = b->max.x;
  56. i -= 4;
  57. } else {
  58. dst->x = b->min.x;
  59. }
  60. if (i > 1) {
  61. dst->y = b->max.y;
  62. i -= 2;
  63. } else {
  64. dst->y = b->min.y;
  65. }
  66. dst->z = (i == 1) ? b->max.z : b->min.z;
  67. }
  68. void bounds_get_center(struct vec3 *dst, const struct bounds *b)
  69. {
  70. vec3_sub(dst, &b->max, &b->min);
  71. vec3_mulf(dst, dst, 0.5f);
  72. vec3_add(dst, dst, &b->min);
  73. }
  74. void bounds_transform(struct bounds *dst, const struct bounds *b, const struct matrix4 *m)
  75. {
  76. struct bounds temp;
  77. bool b_init = false;
  78. int i;
  79. memset(&temp, 0, sizeof(temp));
  80. for (i = 0; i < 8; i++) {
  81. struct vec3 p;
  82. bounds_get_point(&p, b, i);
  83. vec3_transform(&p, &p, m);
  84. if (!b_init) {
  85. vec3_copy(&temp.min, &p);
  86. vec3_copy(&temp.max, &p);
  87. b_init = true;
  88. } else {
  89. if (p.x < temp.min.x)
  90. temp.min.x = p.x;
  91. else if (p.x > temp.max.x)
  92. temp.max.x = p.x;
  93. if (p.y < temp.min.y)
  94. temp.min.y = p.y;
  95. else if (p.y > temp.max.y)
  96. temp.max.y = p.y;
  97. if (p.z < temp.min.z)
  98. temp.min.z = p.z;
  99. else if (p.z > temp.max.z)
  100. temp.max.z = p.z;
  101. }
  102. }
  103. bounds_copy(dst, &temp);
  104. }
  105. void bounds_transform3x4(struct bounds *dst, const struct bounds *b, const struct matrix3 *m)
  106. {
  107. struct bounds temp;
  108. bool b_init = false;
  109. int i;
  110. memset(&temp, 0, sizeof(temp));
  111. for (i = 0; i < 8; i++) {
  112. struct vec3 p;
  113. bounds_get_point(&p, b, i);
  114. vec3_transform3x4(&p, &p, m);
  115. if (!b_init) {
  116. vec3_copy(&temp.min, &p);
  117. vec3_copy(&temp.max, &p);
  118. b_init = true;
  119. } else {
  120. if (p.x < temp.min.x)
  121. temp.min.x = p.x;
  122. else if (p.x > temp.max.x)
  123. temp.max.x = p.x;
  124. if (p.y < temp.min.y)
  125. temp.min.y = p.y;
  126. else if (p.y > temp.max.y)
  127. temp.max.y = p.y;
  128. if (p.z < temp.min.z)
  129. temp.min.z = p.z;
  130. else if (p.z > temp.max.z)
  131. temp.max.z = p.z;
  132. }
  133. }
  134. bounds_copy(dst, &temp);
  135. }
  136. bool bounds_intersection_ray(const struct bounds *b, const struct vec3 *orig, const struct vec3 *dir, float *t)
  137. {
  138. float t_max = M_INFINITE;
  139. float t_min = -M_INFINITE;
  140. struct vec3 center, max_offset, box_offset;
  141. int i;
  142. bounds_get_center(&center, b);
  143. vec3_sub(&max_offset, &b->max, &center);
  144. vec3_sub(&box_offset, &center, orig);
  145. for (i = 0; i < 3; i++) {
  146. float e = box_offset.ptr[i];
  147. float f = dir->ptr[i];
  148. if (fabsf(f) > 0.0f) {
  149. float fi = 1.0f / f;
  150. float t1 = (e + max_offset.ptr[i]) * fi;
  151. float t2 = (e - max_offset.ptr[i]) * fi;
  152. if (t1 > t2) {
  153. if (t2 > t_min)
  154. t_min = t2;
  155. if (t1 < t_max)
  156. t_max = t1;
  157. } else {
  158. if (t1 > t_min)
  159. t_min = t1;
  160. if (t2 < t_max)
  161. t_max = t2;
  162. }
  163. if (t_min > t_max)
  164. return false;
  165. if (t_max < 0.0f)
  166. return false;
  167. } else if ((-e - max_offset.ptr[i]) > 0.0f || (-e + max_offset.ptr[i]) < 0.0f) {
  168. return false;
  169. }
  170. }
  171. *t = (t_min > 0.0f) ? t_min : t_max;
  172. return true;
  173. }
  174. bool bounds_intersection_line(const struct bounds *b, const struct vec3 *p1, const struct vec3 *p2, float *t)
  175. {
  176. struct vec3 dir;
  177. float length;
  178. vec3_sub(&dir, p2, p1);
  179. length = vec3_len(&dir);
  180. if (length <= TINY_EPSILON)
  181. return false;
  182. vec3_mulf(&dir, &dir, 1.0f / length);
  183. if (!bounds_intersection_ray(b, p1, &dir, t))
  184. return false;
  185. *t /= length;
  186. return true;
  187. }
  188. bool bounds_plane_test(const struct bounds *b, const struct plane *p)
  189. {
  190. struct vec3 vmin, vmax;
  191. int i;
  192. for (i = 0; i < 3; i++) {
  193. if (p->dir.ptr[i] >= 0.0f) {
  194. vmin.ptr[i] = b->min.ptr[i];
  195. vmax.ptr[i] = b->max.ptr[i];
  196. } else {
  197. vmin.ptr[i] = b->max.ptr[i];
  198. vmax.ptr[i] = b->min.ptr[i];
  199. }
  200. }
  201. if (vec3_plane_dist(&vmin, p) > 0.0f)
  202. return BOUNDS_OUTSIDE;
  203. if (vec3_plane_dist(&vmax, p) >= 0.0f)
  204. return BOUNDS_PARTIAL;
  205. return BOUNDS_INSIDE;
  206. }
  207. bool bounds_under_plane(const struct bounds *b, const struct plane *p)
  208. {
  209. struct vec3 vmin;
  210. vmin.x = (p->dir.x < 0.0f) ? b->max.x : b->min.x;
  211. vmin.y = (p->dir.y < 0.0f) ? b->max.y : b->min.y;
  212. vmin.z = (p->dir.z < 0.0f) ? b->max.z : b->min.z;
  213. return (vec3_dot(&vmin, &p->dir) <= p->dist);
  214. }
  215. bool bounds_intersects(const struct bounds *b, const struct bounds *test, float epsilon)
  216. {
  217. return ((b->min.x - test->max.x) <= epsilon) && ((test->min.x - b->max.x) <= epsilon) &&
  218. ((b->min.y - test->max.y) <= epsilon) && ((test->min.y - b->max.y) <= epsilon) &&
  219. ((b->min.z - test->max.z) <= epsilon) && ((test->min.z - b->max.z) <= epsilon);
  220. }
  221. bool bounds_intersects_obb(const struct bounds *b, const struct bounds *test, const struct matrix4 *m, float epsilon)
  222. {
  223. struct bounds b_tr, test_tr;
  224. struct matrix4 m_inv;
  225. matrix4_inv(&m_inv, m);
  226. bounds_transform(&b_tr, b, m);
  227. bounds_transform(&test_tr, test, &m_inv);
  228. return bounds_intersects(b, &test_tr, epsilon) && bounds_intersects(&b_tr, test, epsilon);
  229. }
  230. bool bounds_intersects_obb3x4(const struct bounds *b, const struct bounds *test, const struct matrix3 *m, float epsilon)
  231. {
  232. struct bounds b_tr, test_tr;
  233. struct matrix3 m_inv;
  234. matrix3_transpose(&m_inv, m);
  235. bounds_transform3x4(&b_tr, b, m);
  236. bounds_transform3x4(&test_tr, test, &m_inv);
  237. return bounds_intersects(b, &test_tr, epsilon) && bounds_intersects(&b_tr, test, epsilon);
  238. }
  239. static inline float vec3or_offset_len(const struct bounds *b, const struct vec3 *v)
  240. {
  241. struct vec3 temp1, temp2;
  242. vec3_sub(&temp1, &b->max, &b->min);
  243. vec3_abs(&temp2, v);
  244. return vec3_dot(&temp1, &temp2);
  245. }
  246. float bounds_min_dist(const struct bounds *b, const struct plane *p)
  247. {
  248. struct vec3 center;
  249. float vec_len = vec3or_offset_len(b, &p->dir) * 0.5f;
  250. float center_dist;
  251. bounds_get_center(&center, b);
  252. center_dist = vec3_plane_dist(&center, p);
  253. return p->dist + center_dist - vec_len;
  254. }