BoundingBox.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. #pragma once
  24. #include "Rect.h"
  25. #include "Vector3.h"
  26. class Frustum;
  27. class Matrix3;
  28. class Matrix4;
  29. class Matrix3x4;
  30. class Ray;
  31. class Sphere;
  32. /// Three-dimensional axis-aligned bounding box
  33. class BoundingBox
  34. {
  35. public:
  36. /// Construct with zero size
  37. BoundingBox() :
  38. min_(Vector3::ZERO),
  39. max_(Vector3::ZERO),
  40. defined_(false)
  41. {
  42. }
  43. /// Copy-construct from another bounding box
  44. BoundingBox(const BoundingBox& box) :
  45. min_(box.min_),
  46. max_(box.max_),
  47. defined_(box.defined_)
  48. {
  49. }
  50. /// Construct from a rect, with the Z dimension left zero
  51. BoundingBox(const Rect& rect) :
  52. min_(Vector3(rect.min_, 0.0f)),
  53. max_(Vector3(rect.max_, 0.0f)),
  54. defined_(true)
  55. {
  56. }
  57. /// Construct from minimum and maximum vectors
  58. BoundingBox(const Vector3& min, const Vector3& max) :
  59. min_(min),
  60. max_(max),
  61. defined_(true)
  62. {
  63. }
  64. /// Construct from minimum and maximum floats (all dimensions same)
  65. BoundingBox(float min, float max) :
  66. min_(Vector3(min, min, min)),
  67. max_(Vector3(max, max, max)),
  68. defined_(true)
  69. {
  70. }
  71. /// Assign from another bounding box
  72. BoundingBox& operator = (const BoundingBox& rhs)
  73. {
  74. min_ = rhs.min_;
  75. max_ = rhs.max_;
  76. defined_ = rhs.defined_;
  77. return *this;
  78. }
  79. /// Assign from a Rect, with the Z dimension left zero
  80. BoundingBox& operator = (const Rect& rhs)
  81. {
  82. min_ = Vector3(rhs.min_, 0.0f);
  83. max_ = Vector3(rhs.max_, 0.0f);
  84. defined_ = true;
  85. return *this;
  86. }
  87. /// Test for equality with another bounding box
  88. bool operator == (const BoundingBox& rhs) const
  89. {
  90. return ((min_ == rhs.min_) && (max_ == rhs.max_));
  91. }
  92. /// Test for inequality with another bounding box
  93. bool operator != (const BoundingBox& rhs) const
  94. {
  95. return ((min_ != rhs.min_) || (max_ != rhs.max_));
  96. }
  97. /// Define from minimum and maximum vectors
  98. void Define(const Vector3& min, const Vector3& max)
  99. {
  100. min_ = min;
  101. max_ = max;
  102. defined_ = true;
  103. }
  104. /// Define from a point
  105. void Define(const Vector3& point)
  106. {
  107. min_ = max_ = point;
  108. defined_ = true;
  109. }
  110. /// Define from minimum and maximum floats (all dimensions same)
  111. void Define(float min, float max)
  112. {
  113. min_ = Vector3(min, min, min);
  114. max_ = Vector3(max, max, max);
  115. defined_ = true;
  116. }
  117. /// Merge a point
  118. void Merge(const Vector3& point)
  119. {
  120. if (!defined_)
  121. {
  122. min_ = max_ = point;
  123. defined_ = true;
  124. return;
  125. }
  126. if (point.x_ < min_.x_) min_.x_ = point.x_;
  127. if (point.y_ < min_.y_) min_.y_ = point.y_;
  128. if (point.z_ < min_.z_) min_.z_ = point.z_;
  129. if (point.x_ > max_.x_) max_.x_ = point.x_;
  130. if (point.y_ > max_.y_) max_.y_ = point.y_;
  131. if (point.z_ > max_.z_) max_.z_ = point.z_;
  132. }
  133. /// Merge another bounding box
  134. void Merge(const BoundingBox& box)
  135. {
  136. if (!defined_)
  137. {
  138. min_ = box.min_;
  139. max_ = box.max_;
  140. defined_ = true;
  141. return;
  142. }
  143. if (box.min_.x_ < min_.x_) min_.x_ = box.min_.x_;
  144. if (box.min_.y_ < min_.y_) min_.y_ = box.min_.y_;
  145. if (box.min_.z_ < min_.z_) min_.z_ = box.min_.z_;
  146. if (box.max_.x_ > max_.x_) max_.x_ = box.max_.x_;
  147. if (box.max_.y_ > max_.y_) max_.y_ = box.max_.y_;
  148. if (box.max_.z_ > max_.z_) max_.z_ = box.max_.z_;
  149. }
  150. /// Define from an array of vertices
  151. void Define(const Vector3* vertices, unsigned count);
  152. /// Define from a frustum
  153. void Define(const Frustum& frustum);
  154. /// Define from a sphere
  155. void Define(const Sphere& sphere);
  156. /// Merge an array of vertices
  157. void Merge(const Vector3* vertices, unsigned count);
  158. /// Merge a frustum
  159. void Merge(const Frustum& frustum);
  160. /// Merge a sphere
  161. void Merge(const Sphere& sphere);
  162. /// Intersect with another bounding box
  163. void Intersect(const BoundingBox& box);
  164. /// Transform with a 3x3 matrix
  165. void Transform(const Matrix3& transform);
  166. /// Transform with a 4x3 matrix
  167. void Transform(const Matrix3x4& transform);
  168. /// Return center
  169. Vector3 Center() const { return (max_ + min_) * 0.5f; }
  170. /// Return size
  171. Vector3 Size() const { return max_ - min_; }
  172. /// Return half-size
  173. Vector3 HalfSize() const { return (max_ - min_) * 0.5f; }
  174. /// Return transformed by a 3x3 matrix
  175. BoundingBox Transformed(const Matrix3& transform) const;
  176. /// Return transformed by a 4x3 matrix
  177. BoundingBox Transformed(const Matrix3x4& transform) const;
  178. /// Return projected by a 4x4 projection matrix
  179. Rect Projected(const Matrix4& projection) const;
  180. /// Test if a point is inside
  181. Intersection IsInside(const Vector3& point) const
  182. {
  183. if ((point.x_ < min_.x_) || (point.x_ > max_.x_))
  184. return OUTSIDE;
  185. if ((point.y_ < min_.y_) || (point.y_ > max_.y_))
  186. return OUTSIDE;
  187. if ((point.z_ < min_.z_) || (point.z_ > max_.z_))
  188. return OUTSIDE;
  189. return INSIDE;
  190. }
  191. /// Test if another bounding box is inside, outside or intersects
  192. Intersection IsInside(const BoundingBox& box) const
  193. {
  194. if ((box.max_.x_ < min_.x_) || (box.min_.x_ > max_.x_))
  195. return OUTSIDE;
  196. if ((box.max_.y_ < min_.y_) || (box.min_.y_ > max_.y_))
  197. return OUTSIDE;
  198. if ((box.max_.z_ < min_.z_) || (box.min_.z_ > max_.z_))
  199. return OUTSIDE;
  200. if ((box.min_.x_ < min_.x_) || (box.max_.x_ > max_.x_))
  201. return INTERSECTS;
  202. if ((box.min_.y_ < min_.y_) || (box.max_.y_ > max_.y_))
  203. return INTERSECTS;
  204. if ((box.min_.z_ < min_.z_) || (box.max_.z_ > max_.z_))
  205. return INTERSECTS;
  206. return INSIDE;
  207. }
  208. /// Test if another bounding box is (partially) inside or outside
  209. Intersection IsInsideFast(const BoundingBox& box) const
  210. {
  211. if ((box.max_.x_ < min_.x_) || (box.min_.x_ > max_.x_))
  212. return OUTSIDE;
  213. if ((box.max_.y_ < min_.y_) || (box.min_.y_ > max_.y_))
  214. return OUTSIDE;
  215. if ((box.max_.z_ < min_.z_) || (box.min_.z_ > max_.z_))
  216. return OUTSIDE;
  217. return INSIDE;
  218. }
  219. /// Test if a sphere is inside, outside or intersects
  220. Intersection IsInside(const Sphere& sphere) const;
  221. /// Test if a sphere is (partially) inside or outside
  222. Intersection IsInsideFast(const Sphere& sphere) const;
  223. /// Return ray hit distance, or infinity if no hit
  224. float Distance(const Ray& ray) const;
  225. /// Minimum vector
  226. Vector3 min_;
  227. /// Maximum vector
  228. Vector3 max_;
  229. /// Defined flag
  230. bool defined_;
  231. };