BoundingBox.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #ifndef MATH_BOUNDINGBOX_H
  24. #define MATH_BOUNDINGBOX_H
  25. #include "Rect.h"
  26. #include "Vector3.h"
  27. #include <vector>
  28. class Frustum;
  29. class Matrix3;
  30. class Matrix4;
  31. class Matrix4x3;
  32. class Ray;
  33. class Sphere;
  34. //! A three-dimensional axis-aligned bounding box
  35. class BoundingBox
  36. {
  37. public:
  38. //! Construct with zero size
  39. BoundingBox() :
  40. mMin(Vector3::sZero),
  41. mMax(Vector3::sZero),
  42. mDefined(false)
  43. {
  44. }
  45. //! Copy-construct from another bounding box
  46. BoundingBox(const BoundingBox& box) :
  47. mMin(box.mMin),
  48. mMax(box.mMax),
  49. mDefined(box.mDefined)
  50. {
  51. }
  52. //! Construct from a Rect, with the Z dimension left zero
  53. BoundingBox(const Rect& rect) :
  54. mMin(Vector3(rect.mMin, 0.0f)),
  55. mMax(Vector3(rect.mMax, 0.0f)),
  56. mDefined(true)
  57. {
  58. }
  59. //! Construct from minimum and maximum vectors
  60. BoundingBox(const Vector3& min, const Vector3& max) :
  61. mMin(min),
  62. mMax(max),
  63. mDefined(true)
  64. {
  65. }
  66. //! Construct from minimum and maximum floats (all dimensions same)
  67. BoundingBox(float min, float max) :
  68. mMin(Vector3(min, min, min)),
  69. mMax(Vector3(max, max, max)),
  70. mDefined(true)
  71. {
  72. }
  73. //! Assign from another bounding box
  74. BoundingBox& operator = (const BoundingBox& rhs)
  75. {
  76. mMin = rhs.mMin;
  77. mMax = rhs.mMax;
  78. mDefined = rhs.mDefined;
  79. return *this;
  80. }
  81. //! Assign from a Rect, with the Z dimension left zero
  82. BoundingBox& operator = (const Rect& rhs)
  83. {
  84. mMin = Vector3(rhs.mMin, 0.0f);
  85. mMax = Vector3(rhs.mMax, 0.0f);
  86. mDefined = true;
  87. return *this;
  88. }
  89. //! Test for equality with another bounding box
  90. bool operator == (const BoundingBox& rhs) const
  91. {
  92. return ((mMin == rhs.mMin) && (mMax == rhs.mMax));
  93. }
  94. //! Test for inequality with another bounding box
  95. bool operator != (const BoundingBox& rhs) const
  96. {
  97. return ((mMin != rhs.mMin) || (mMax != rhs.mMax));
  98. }
  99. //! Define from minimum and maximum vectors
  100. void define(const Vector3& min, const Vector3& max)
  101. {
  102. mMin = min;
  103. mMax = max;
  104. mDefined = true;
  105. }
  106. //! Define from a point
  107. void define(const Vector3& point)
  108. {
  109. mMin = mMax = point;
  110. mDefined = true;
  111. }
  112. //! Define from minimum and maximum floats (all dimensions same)
  113. void define(float min, float max)
  114. {
  115. mMin = Vector3(min, min, min);
  116. mMax = Vector3(max, max, max);
  117. mDefined = true;
  118. }
  119. //! Merge a point
  120. void merge(const Vector3& point)
  121. {
  122. if (!mDefined)
  123. {
  124. mMin = mMax = point;
  125. mDefined = true;
  126. return;
  127. }
  128. if (point.mX < mMin.mX) mMin.mX = point.mX;
  129. if (point.mY < mMin.mY) mMin.mY = point.mY;
  130. if (point.mZ < mMin.mZ) mMin.mZ = point.mZ;
  131. if (point.mX > mMax.mX) mMax.mX = point.mX;
  132. if (point.mY > mMax.mY) mMax.mY = point.mY;
  133. if (point.mZ > mMax.mZ) mMax.mZ = point.mZ;
  134. }
  135. //! Merge another bounding box
  136. void merge(const BoundingBox& box)
  137. {
  138. if (!mDefined)
  139. {
  140. mMin = box.mMin;
  141. mMax = box.mMax;
  142. mDefined = true;
  143. return;
  144. }
  145. if (box.mMin.mX < mMin.mX) mMin.mX = box.mMin.mX;
  146. if (box.mMin.mY < mMin.mY) mMin.mY = box.mMin.mY;
  147. if (box.mMin.mZ < mMin.mZ) mMin.mZ = box.mMin.mZ;
  148. if (box.mMax.mX > mMax.mX) mMax.mX = box.mMax.mX;
  149. if (box.mMax.mY > mMax.mY) mMax.mY = box.mMax.mY;
  150. if (box.mMax.mZ > mMax.mZ) mMax.mZ = box.mMax.mZ;
  151. }
  152. //! Define from a vector of vertices
  153. void define(const std::vector<Vector3>& vertices);
  154. //! Define from an array of vertices
  155. void define(const Vector3* vertices, unsigned count);
  156. //! Define from a frustum
  157. void define(const Frustum& frustum);
  158. //! Define from a sphere
  159. void define(const Sphere& sphere);
  160. //! Merge a vector of vertices
  161. void merge(const std::vector<Vector3>& vertices);
  162. //! Merge an array of vertices
  163. void merge(const Vector3* vertices, unsigned count);
  164. //! Merge a frustum
  165. void merge(const Frustum& frustum);
  166. //! Merge a sphere
  167. void merge(const Sphere& sphere);
  168. //! Intersect with another bounding box
  169. void intersect(const BoundingBox& box);
  170. //! Transform with a 3x3 matrix
  171. void transform(const Matrix3& transform);
  172. //! Transform with a 4x3 matrix
  173. void transform(const Matrix4x3& transform);
  174. //! Return center
  175. Vector3 getCenter() const { return (mMax + mMin) * 0.5f; }
  176. //! Return size
  177. Vector3 getSize() const { return mMax - mMin; }
  178. //! Return half-size
  179. Vector3 getHalfSize() const { return (mMax - mMin) * 0.5f; }
  180. //! Return transformed by a 3x3 matrix
  181. BoundingBox getTransformed(const Matrix3& transform) const;
  182. //! Return transformed by a 4x3 matrix
  183. BoundingBox getTransformed(const Matrix4x3& transform) const;
  184. //! Return projected by a 4x4 projection matrix
  185. Rect getProjected(const Matrix4& projection) const;
  186. //! Test if a point is inside
  187. Intersection isInside(const Vector3& point) const
  188. {
  189. if ((point.mX < mMin.mX) || (point.mX > mMax.mX))
  190. return OUTSIDE;
  191. if ((point.mY < mMin.mY) || (point.mY > mMax.mY))
  192. return OUTSIDE;
  193. if ((point.mZ < mMin.mZ) || (point.mZ > mMax.mZ))
  194. return OUTSIDE;
  195. return INSIDE;
  196. }
  197. //! Test if another bounding box is inside, outside or intersects
  198. Intersection isInside(const BoundingBox& box) const
  199. {
  200. if ((box.mMax.mX < mMin.mX) || (box.mMin.mX > mMax.mX))
  201. return OUTSIDE;
  202. if ((box.mMax.mY < mMin.mY) || (box.mMin.mY > mMax.mY))
  203. return OUTSIDE;
  204. if ((box.mMax.mZ < mMin.mZ) || (box.mMin.mZ > mMax.mZ))
  205. return OUTSIDE;
  206. if ((box.mMin.mX < mMin.mX) || (box.mMax.mX > mMax.mX))
  207. return INTERSECTS;
  208. if ((box.mMin.mY < mMin.mY) || (box.mMax.mY > mMax.mY))
  209. return INTERSECTS;
  210. if ((box.mMin.mZ < mMin.mZ) || (box.mMax.mZ > mMax.mZ))
  211. return INTERSECTS;
  212. return INSIDE;
  213. }
  214. //! Test if another bounding box is (partially) inside or outside
  215. Intersection isInsideFast(const BoundingBox& box) const
  216. {
  217. if ((box.mMax.mX < mMin.mX) || (box.mMin.mX > mMax.mX))
  218. return OUTSIDE;
  219. if ((box.mMax.mY < mMin.mY) || (box.mMin.mY > mMax.mY))
  220. return OUTSIDE;
  221. if ((box.mMax.mZ < mMin.mZ) || (box.mMin.mZ > mMax.mZ))
  222. return OUTSIDE;
  223. return INSIDE;
  224. }
  225. //! Test if a sphere is inside, outside or intersects
  226. Intersection isInside(const Sphere& sphere) const;
  227. //! Test if a sphere is (partially) inside or outside
  228. Intersection isInsideFast(const Sphere& sphere) const;
  229. //! Return ray hit distance, or infinity if no hit
  230. float getDistance(const Ray& ray) const;
  231. //! Minimum vector
  232. Vector3 mMin;
  233. //! Maximum vector
  234. Vector3 mMax;
  235. //! Defined flag
  236. bool mDefined;
  237. };
  238. #endif // MATH_BOUNDINGBOX_H