BoundingBox.h 8.5 KB

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