Box.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 "Types.h"
  26. #include "Matrix4x4.h"
  27. #include "Vector3.h"
  28. #include "Sphere.h"
  29. namespace crown
  30. {
  31. /// Axially aligned bounding box.
  32. ///
  33. /// Used mainly for collision detection and intersection tests.
  34. class Box
  35. {
  36. public:
  37. /// Does nothing for efficiency.
  38. Box();
  39. /// Constructs from @a min and @a max.
  40. Box(const Vector3& min, const Vector3& max);
  41. Box(const Box& box);
  42. const Vector3& min() const;
  43. const Vector3& max() const;
  44. void set_min(const Vector3& min);
  45. void set_max(const Vector3& max);
  46. Vector3 center() const;
  47. float radius() const;
  48. float volume() const;
  49. /// Adds @a count @a points expanding if necessary.
  50. void add_points(const Vector3* points, uint32_t count);
  51. /// Adds @a count @a boxes expanding if necessay.
  52. void add_boxes(const Box* boxes, uint32_t count);
  53. /// Returns whether point @a p is contained in the box.
  54. bool contains_point(const Vector3& p) const;
  55. /// Returns the @a index -th vertex of the box.
  56. Vector3 vertex(uint32_t index) const;
  57. /// Returns the box trasformed according to @a mat matrix into @a result.
  58. void transformed(const Matrix4x4& mat, Box& result) const;
  59. /// Returns the eight vertices of the box.
  60. void to_vertices(Vector3 v[8]) const;
  61. /// Returns as a sphere.
  62. Sphere to_sphere() const;
  63. /// Sets min and max to zero.
  64. void zero();
  65. private:
  66. Vector3 m_min;
  67. Vector3 m_max;
  68. };
  69. //-----------------------------------------------------------------------------
  70. inline Box::Box()
  71. {
  72. }
  73. //-----------------------------------------------------------------------------
  74. inline Box::Box(const Box& box) : m_min(box.m_min), m_max(box.m_max)
  75. {
  76. }
  77. //-----------------------------------------------------------------------------
  78. inline Box::Box(const Vector3& min, const Vector3& max) : m_min(min), m_max(max)
  79. {
  80. }
  81. //-----------------------------------------------------------------------------
  82. inline const Vector3& Box::min() const
  83. {
  84. return m_min;
  85. }
  86. //-----------------------------------------------------------------------------
  87. inline void Box::set_min(const Vector3& min)
  88. {
  89. m_min = min;
  90. }
  91. //-----------------------------------------------------------------------------
  92. inline const Vector3& Box::max() const
  93. {
  94. return m_max;
  95. }
  96. //-----------------------------------------------------------------------------
  97. inline void Box::set_max(const Vector3& max)
  98. {
  99. m_max = max;
  100. }
  101. //-----------------------------------------------------------------------------
  102. inline void Box::add_points(const Vector3* points, uint32_t count)
  103. {
  104. for (uint32_t i = 0; i < count; i++)
  105. {
  106. const Vector3& p = points[i];
  107. if (p.x < m_min.x)
  108. {
  109. m_min.x = p.x;
  110. }
  111. if (p.y < m_min.y)
  112. {
  113. m_min.y = p.y;
  114. }
  115. if (p.z < m_min.z)
  116. {
  117. m_min.z = p.z;
  118. }
  119. if (p.x > m_max.x)
  120. {
  121. m_max.x = p.x;
  122. }
  123. if (p.y > m_max.y)
  124. {
  125. m_max.y = p.y;
  126. }
  127. if (p.z > m_max.z)
  128. {
  129. m_max.z = p.z;
  130. }
  131. }
  132. }
  133. //-----------------------------------------------------------------------------
  134. inline void Box::add_boxes(const Box* boxes, uint32_t count)
  135. {
  136. for (uint32_t i = 0; i < count; i++)
  137. {
  138. const Box& box = boxes[i];
  139. if (box.m_min.x < m_min.x)
  140. {
  141. m_min.x = box.m_min.x;
  142. }
  143. if (box.m_min.y < m_min.y)
  144. {
  145. m_min.y = box.m_min.y;
  146. }
  147. if (box.m_min.z < m_min.z)
  148. {
  149. m_min.z = box.m_min.z;
  150. }
  151. if (box.m_max.x > m_max.x)
  152. {
  153. m_max.x = box.m_max.x;
  154. }
  155. if (box.m_max.y > m_max.y)
  156. {
  157. m_max.y = box.m_max.y;
  158. }
  159. if (box.m_max.z > m_max.z)
  160. {
  161. m_max.z = box.m_max.z;
  162. }
  163. }
  164. }
  165. //-----------------------------------------------------------------------------
  166. inline bool Box::contains_point(const Vector3& p) const
  167. {
  168. return (p.x > m_min.x && p.y > m_min.y && p.z > m_min.z &&
  169. p.x < m_max.x && p.y < m_max.y && p.z < m_max.z);
  170. }
  171. //-----------------------------------------------------------------------------
  172. inline Vector3 Box::center() const
  173. {
  174. return (m_min + m_max) * 0.5;
  175. }
  176. //-----------------------------------------------------------------------------
  177. inline float Box::radius() const
  178. {
  179. return (m_max - (m_min + m_max) * 0.5).length();
  180. }
  181. //-----------------------------------------------------------------------------
  182. inline void Box::to_vertices(Vector3 v[8]) const
  183. {
  184. // 7 ---- 6
  185. // | |
  186. // | | <--- Top face
  187. // 4 ---- 5
  188. //
  189. // 3 ---- 2
  190. // | |
  191. // | | <--- Bottom face
  192. // 0 ---- 1
  193. v[0].x = m_min.x;
  194. v[0].y = m_min.y;
  195. v[0].z = m_max.z;
  196. v[1].x = m_max.x;
  197. v[1].y = m_min.y;
  198. v[1].z = m_max.z;
  199. v[2].x = m_max.x;
  200. v[2].y = m_min.y;
  201. v[2].z = m_min.z;
  202. v[3].x = m_min.x;
  203. v[3].y = m_min.y;
  204. v[3].z = m_min.z;
  205. v[4].x = m_min.x;
  206. v[4].y = m_max.y;
  207. v[4].z = m_max.z;
  208. v[5].x = m_max.x;
  209. v[5].y = m_max.y;
  210. v[5].z = m_max.z;
  211. v[6].x = m_max.x;
  212. v[6].y = m_max.y;
  213. v[6].z = m_min.z;
  214. v[7].x = m_min.x;
  215. v[7].y = m_max.y;
  216. v[7].z = m_min.z;
  217. }
  218. //-----------------------------------------------------------------------------
  219. inline Vector3 Box::vertex(uint32_t index) const
  220. {
  221. CE_ASSERT(index < 8, "Index must be < 8");
  222. switch (index)
  223. {
  224. case 0:
  225. return Vector3(m_min.x, m_min.y, m_min.z);
  226. case 1:
  227. return Vector3(m_max.x, m_min.y, m_min.z);
  228. case 2:
  229. return Vector3(m_max.x, m_min.y, m_max.z);
  230. case 3:
  231. return Vector3(m_min.x, m_min.y, m_max.z);
  232. case 4:
  233. return Vector3(m_min.x, m_max.y, m_min.z);
  234. case 5:
  235. return Vector3(m_max.x, m_max.y, m_min.z);
  236. case 6:
  237. return Vector3(m_max.x, m_max.y, m_max.z);
  238. case 7:
  239. return Vector3(m_min.x, m_max.y, m_max.z);
  240. }
  241. }
  242. //-----------------------------------------------------------------------------
  243. inline void Box::transformed(const Matrix4x4& mat, Box& result) const
  244. {
  245. Vector3 vertices[8];
  246. to_vertices(vertices);
  247. result.m_min = mat * vertices[0];
  248. result.m_max = mat * vertices[0];
  249. vertices[1] = mat * vertices[1];
  250. vertices[2] = mat * vertices[2];
  251. vertices[3] = mat * vertices[3];
  252. vertices[4] = mat * vertices[4];
  253. vertices[5] = mat * vertices[5];
  254. vertices[6] = mat * vertices[6];
  255. vertices[7] = mat * vertices[7];
  256. result.add_points(&vertices[1], 7);
  257. }
  258. //-----------------------------------------------------------------------------
  259. inline float Box::volume() const
  260. {
  261. return (m_max.x - m_min.x) * (m_max.y - m_min.y) * (m_max.z - m_min.z);
  262. }
  263. //-----------------------------------------------------------------------------
  264. inline void Box::zero()
  265. {
  266. m_min.zero();
  267. m_max.zero();
  268. }
  269. //-----------------------------------------------------------------------------
  270. inline Sphere Box::to_sphere() const
  271. {
  272. return Sphere(center(), radius());
  273. }
  274. } // namespace crown