aabb.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "assert.h"
  25. #include "math_types.h"
  26. #include "matrix4x4.h"
  27. #include "sphere.h"
  28. #include "types.h"
  29. #include "vector3.h"
  30. namespace crown
  31. {
  32. /// Functions to manipulate AABB.
  33. ///
  34. /// @ingroup Math
  35. namespace aabb
  36. {
  37. void reset(AABB& b);
  38. /// Returns the center of the box @a b.
  39. Vector3 center(const AABB& b);
  40. /// Returns the radius of the box @a b.
  41. float radius(const AABB& b);
  42. /// Returns the volume of the box @a b.
  43. float volume(const AABB& b);
  44. /// Adds @a num @a points to the box @a b, expanding its bounds if necessary.
  45. void add_points(AABB& b, uint32_t num, const Vector3* points);
  46. /// Adds @a num @a boxes to the box @a b, expanding its bounds if necessary.
  47. void add_boxes(AABB& b, uint32_t num, const AABB* boxes);
  48. /// Returns whether point @a p is contained in the box @a b.
  49. bool contains_point(const AABB& b, const Vector3& p);
  50. /// Returns the @a index -th vertex of the box.
  51. Vector3 vertex(const AABB& b, uint32_t index);
  52. /// Returns the box enclosing @a b transformed by @a m.
  53. AABB transformed(const AABB& b, const Matrix4x4& m);
  54. /// Returns the eight vertices of the box @a b.
  55. void to_vertices(const AABB& b, Vector3 v[8]);
  56. /// Returns the sphere enclosing the box @a b.
  57. Sphere to_sphere(const AABB& b);
  58. }
  59. namespace aabb
  60. {
  61. //-----------------------------------------------------------------------------
  62. inline void reset(AABB& b)
  63. {
  64. b.min = vector3::ZERO;
  65. b.max = vector3::ZERO;
  66. }
  67. //-----------------------------------------------------------------------------
  68. inline Vector3 center(const AABB& b)
  69. {
  70. return (b.min + b.max) * 0.5;
  71. }
  72. //-----------------------------------------------------------------------------
  73. inline float radius(const AABB& b)
  74. {
  75. return vector3::length(b.max - (b.min + b.max) * 0.5);
  76. }
  77. //-----------------------------------------------------------------------------
  78. inline float volume(const AABB& b)
  79. {
  80. return (b.max.x - b.min.x) * (b.max.y - b.min.y) * (b.max.z - b.min.z);
  81. }
  82. //-----------------------------------------------------------------------------
  83. inline void add_points(AABB& b, uint32_t num, const Vector3* points)
  84. {
  85. for (uint32_t i = 0; i < num; i++)
  86. {
  87. const Vector3& p = points[i];
  88. if (p.x < b.min.x) b.min.x = p.x;
  89. if (p.y < b.min.y) b.min.y = p.y;
  90. if (p.z < b.min.z) b.min.z = p.z;
  91. if (p.x > b.max.x) b.max.x = p.x;
  92. if (p.y > b.max.y) b.max.y = p.y;
  93. if (p.z > b.max.z) b.max.z = p.z;
  94. }
  95. }
  96. //-----------------------------------------------------------------------------
  97. inline void add_boxes(AABB& b, uint32_t num, const AABB* boxes)
  98. {
  99. for (uint32_t i = 0; i < num; i++)
  100. {
  101. const AABB& box = boxes[i];
  102. if (box.min.x < b.min.x) b.min.x = box.min.x;
  103. if (box.min.y < b.min.y) b.min.y = box.min.y;
  104. if (box.min.z < b.min.z) b.min.z = box.min.z;
  105. if (box.max.x > b.max.x) b.max.x = box.max.x;
  106. if (box.max.y > b.max.y) b.max.y = box.max.y;
  107. if (box.max.z > b.max.z) b.max.z = box.max.z;
  108. }
  109. }
  110. //-----------------------------------------------------------------------------
  111. inline bool contains_point(const AABB& b, const Vector3& p)
  112. {
  113. return (p.x > b.min.x &&
  114. p.y > b.min.y &&
  115. p.z > b.min.z &&
  116. p.x < b.max.x &&
  117. p.y < b.max.y &&
  118. p.z < b.max.z);
  119. }
  120. //-----------------------------------------------------------------------------
  121. inline Vector3 vertex(const AABB& b, uint32_t index)
  122. {
  123. CE_ASSERT(index < 8, "Index must be < 8");
  124. switch (index)
  125. {
  126. case 0: return Vector3(b.min.x, b.min.y, b.min.z);
  127. case 1: return Vector3(b.max.x, b.min.y, b.min.z);
  128. case 2: return Vector3(b.max.x, b.min.y, b.max.z);
  129. case 3: return Vector3(b.min.x, b.min.y, b.max.z);
  130. case 4: return Vector3(b.min.x, b.max.y, b.min.z);
  131. case 5: return Vector3(b.max.x, b.max.y, b.min.z);
  132. case 6: return Vector3(b.max.x, b.max.y, b.max.z);
  133. case 7: return Vector3(b.min.x, b.max.y, b.max.z);
  134. }
  135. }
  136. //-----------------------------------------------------------------------------
  137. inline AABB transformed(const AABB& b, const Matrix4x4& m)
  138. {
  139. Vector3 vertices[8];
  140. to_vertices(b, vertices);
  141. vertices[0] = m * vertices[0];
  142. vertices[1] = m * vertices[1];
  143. vertices[2] = m * vertices[2];
  144. vertices[3] = m * vertices[3];
  145. vertices[4] = m * vertices[4];
  146. vertices[5] = m * vertices[5];
  147. vertices[6] = m * vertices[6];
  148. vertices[7] = m * vertices[7];
  149. AABB res;
  150. reset(res);
  151. add_points(res, 8, vertices);
  152. return res;
  153. }
  154. //-----------------------------------------------------------------------------
  155. inline void to_vertices(const AABB& b, Vector3 v[8])
  156. {
  157. // 7 ---- 6
  158. // | |
  159. // | | <--- Top face
  160. // 4 ---- 5
  161. //
  162. // 3 ---- 2
  163. // | |
  164. // | | <--- Bottom face
  165. // 0 ---- 1
  166. v[0].x = b.min.x;
  167. v[0].y = b.min.y;
  168. v[0].z = b.max.z;
  169. v[1].x = b.max.x;
  170. v[1].y = b.min.y;
  171. v[1].z = b.max.z;
  172. v[2].x = b.max.x;
  173. v[2].y = b.min.y;
  174. v[2].z = b.min.z;
  175. v[3].x = b.min.x;
  176. v[3].y = b.min.y;
  177. v[3].z = b.min.z;
  178. v[4].x = b.min.x;
  179. v[4].y = b.max.y;
  180. v[4].z = b.max.z;
  181. v[5].x = b.max.x;
  182. v[5].y = b.max.y;
  183. v[5].z = b.max.z;
  184. v[6].x = b.max.x;
  185. v[6].y = b.max.y;
  186. v[6].z = b.min.z;
  187. v[7].x = b.min.x;
  188. v[7].y = b.max.y;
  189. v[7].z = b.min.z;
  190. }
  191. //-----------------------------------------------------------------------------
  192. inline Sphere to_sphere(const AABB& b)
  193. {
  194. return Sphere(center(b), radius(b));
  195. }
  196. } // namespace aabb
  197. //-----------------------------------------------------------------------------
  198. inline AABB::AABB()
  199. {
  200. // Do not initialize
  201. }
  202. //-----------------------------------------------------------------------------
  203. inline AABB::AABB(const Vector3& min, const Vector3& max)
  204. : min(min), max(max)
  205. {
  206. }
  207. } // namespace crown