bounds.c 6.2 KB

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