bounds.c 7.3 KB

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