bounds.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #pragma once
  15. #include "math-defs.h"
  16. #include "vec3.h"
  17. /*
  18. * Axis Aligned Bounding Box
  19. */
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. #define BOUNDS_MAX_X 1
  24. #define BOUNDS_MAX_Y 2
  25. #define BOUNDS_MAX_Z 4
  26. #define BOUNDS_OUTSIDE 1
  27. #define BOUNDS_INSIDE 2
  28. #define BOUNDS_PARTIAL 3
  29. struct bounds {
  30. struct vec3 min, max;
  31. };
  32. static inline void bounds_zero(struct bounds *dst)
  33. {
  34. vec3_zero(&dst->min);
  35. vec3_zero(&dst->max);
  36. }
  37. static inline void bounds_copy(struct bounds *dst, const struct bounds *b)
  38. {
  39. vec3_copy(&dst->min, &b->min);
  40. vec3_copy(&dst->max, &b->max);
  41. }
  42. EXPORT void bounds_move(struct bounds *dst, const struct bounds *b,
  43. const struct vec3 *v);
  44. EXPORT void bounds_scale(struct bounds *dst, const struct bounds *b,
  45. const struct vec3 *v);
  46. EXPORT void bounds_merge(struct bounds *dst, const struct bounds *b1,
  47. const struct bounds *b2);
  48. EXPORT void bounds_merge_point(struct bounds *dst, const struct bounds *b,
  49. const struct vec3 *v);
  50. EXPORT void bounds_get_point(struct vec3 *dst, const struct bounds *b,
  51. unsigned int i);
  52. EXPORT void bounds_get_center(struct vec3 *dst, const struct bounds *b);
  53. /**
  54. * Note: transforms as OBB, then converts back to AABB, which can result in
  55. * the actual size becoming larger than it originally was.
  56. */
  57. EXPORT void bounds_transform(struct bounds *dst, const struct bounds *b,
  58. const struct matrix4 *m);
  59. EXPORT void bounds_transform3x4(struct bounds *dst, const struct bounds *b,
  60. const struct matrix3 *m);
  61. EXPORT bool bounds_intersection_ray(const struct bounds *b,
  62. const struct vec3 *orig, const struct vec3 *dir, float *t);
  63. EXPORT bool bounds_intersection_line(const struct bounds *b,
  64. const struct vec3 *p1, const struct vec3 *p2, float *t);
  65. EXPORT bool bounds_plane_test(const struct bounds *b, const struct plane *p);
  66. EXPORT bool bounds_under_plane(const struct bounds *b,
  67. const struct plane *p);
  68. static inline bool bounds_inside(const struct bounds *b,
  69. const struct bounds *test)
  70. {
  71. return test->min.x >= b->min.x &&
  72. test->min.y >= b->min.y &&
  73. test->min.z >= b->min.z &&
  74. test->max.x <= b->max.x &&
  75. test->max.y <= b->max.y &&
  76. test->max.z <= b->max.z;
  77. }
  78. static inline bool bounds_vec3_inside(const struct bounds *b,
  79. const struct vec3 *v)
  80. {
  81. return v->x >= (b->min.x-EPSILON) &&
  82. v->x <= (b->max.x+EPSILON) &&
  83. v->y >= (b->min.y-EPSILON) &&
  84. v->y <= (b->max.y+EPSILON) &&
  85. v->z >= (b->min.z-EPSILON) &&
  86. v->z <= (b->max.z+EPSILON);
  87. }
  88. EXPORT bool bounds_intersects(const struct bounds *b,
  89. const struct bounds *test, float epsilon);
  90. EXPORT bool bounds_intersects_obb(const struct bounds *b,
  91. const struct bounds *test, const struct matrix4 *m,
  92. float epsilon);
  93. EXPORT bool bounds_intersects_obb3x4(const struct bounds *b,
  94. const struct bounds *test, const struct matrix3 *m,
  95. float epsilon);
  96. static inline bool bounds_intersects_ray(const struct bounds *b,
  97. const struct vec3 *orig, const struct vec3 *dir)
  98. {
  99. float t;
  100. return bounds_intersection_ray(b, orig, dir, &t);
  101. }
  102. static inline bool bounds_intersects_line(const struct bounds *b,
  103. const struct vec3 *p1, const struct vec3 *p2)
  104. {
  105. float t;
  106. return bounds_intersection_line(b, p1, p2, &t);
  107. }
  108. EXPORT float bounds_min_dist(const struct bounds *b, const struct plane *p);
  109. #ifdef __cplusplus
  110. }
  111. #endif