bounds.c 7.4 KB

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