BoundingBox.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 Polyhedron;
  27. class Frustum;
  28. class Matrix3;
  29. class Matrix4;
  30. class Matrix3x4;
  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. /// Construct from an array of vertices.
  72. BoundingBox(const Vector3* vertices, unsigned count) :
  73. defined_(false)
  74. {
  75. Define(vertices, count);
  76. }
  77. /// Construct from a frustum.
  78. BoundingBox(const Frustum& frustum) :
  79. defined_(false)
  80. {
  81. Define(frustum);
  82. }
  83. /// Construct from a polyhedron.
  84. BoundingBox(const Polyhedron& poly) :
  85. defined_(false)
  86. {
  87. Define(poly);
  88. }
  89. /// Construct from a sphere.
  90. BoundingBox(const Sphere& sphere) :
  91. defined_(false)
  92. {
  93. Define(sphere);
  94. }
  95. /// Assign from another bounding box.
  96. BoundingBox& operator = (const BoundingBox& rhs)
  97. {
  98. min_ = rhs.min_;
  99. max_ = rhs.max_;
  100. defined_ = rhs.defined_;
  101. return *this;
  102. }
  103. /// Assign from a Rect, with the Z dimension left zero.
  104. BoundingBox& operator = (const Rect& rhs)
  105. {
  106. min_ = Vector3(rhs.min_, 0.0f);
  107. max_ = Vector3(rhs.max_, 0.0f);
  108. defined_ = true;
  109. return *this;
  110. }
  111. /// Test for equality with another bounding box.
  112. bool operator == (const BoundingBox& rhs) const { return (min_ == rhs.min_ && max_ == rhs.max_); }
  113. /// Test for inequality with another bounding box.
  114. bool operator != (const BoundingBox& rhs) const { return (min_ != rhs.min_ || max_ != rhs.max_); }
  115. /// Define from another bounding box.
  116. void Define(const BoundingBox& box)
  117. {
  118. Define(box.min_, box.max_);
  119. }
  120. /// Define from a Rect.
  121. void Define(const Rect& rect)
  122. {
  123. Define(Vector3(rect.min_, 0.0f), Vector3(rect.max_, 0.0f));
  124. }
  125. /// Define from minimum and maximum vectors.
  126. void Define(const Vector3& min, const Vector3& max)
  127. {
  128. min_ = min;
  129. max_ = max;
  130. defined_ = true;
  131. }
  132. /// Define from minimum and maximum floats (all dimensions same.)
  133. void Define(float min, float max)
  134. {
  135. min_ = Vector3(min, min, min);
  136. max_ = Vector3(max, max, max);
  137. defined_ = true;
  138. }
  139. /// Define from a point.
  140. void Define(const Vector3& point)
  141. {
  142. min_ = max_ = point;
  143. defined_ = true;
  144. }
  145. /// Merge a point.
  146. void Merge(const Vector3& point)
  147. {
  148. if (!defined_)
  149. {
  150. min_ = max_ = point;
  151. defined_ = true;
  152. return;
  153. }
  154. if (point.x_ < min_.x_)
  155. min_.x_ = point.x_;
  156. if (point.y_ < min_.y_)
  157. min_.y_ = point.y_;
  158. if (point.z_ < min_.z_)
  159. min_.z_ = point.z_;
  160. if (point.x_ > max_.x_)
  161. max_.x_ = point.x_;
  162. if (point.y_ > max_.y_)
  163. max_.y_ = point.y_;
  164. if (point.z_ > max_.z_)
  165. max_.z_ = point.z_;
  166. }
  167. /// Merge another bounding box.
  168. void Merge(const BoundingBox& box)
  169. {
  170. if (!defined_)
  171. {
  172. min_ = box.min_;
  173. max_ = box.max_;
  174. defined_ = true;
  175. return;
  176. }
  177. if (box.min_.x_ < min_.x_)
  178. min_.x_ = box.min_.x_;
  179. if (box.min_.y_ < min_.y_)
  180. min_.y_ = box.min_.y_;
  181. if (box.min_.z_ < min_.z_)
  182. min_.z_ = box.min_.z_;
  183. if (box.max_.x_ > max_.x_)
  184. max_.x_ = box.max_.x_;
  185. if (box.max_.y_ > max_.y_)
  186. max_.y_ = box.max_.y_;
  187. if (box.max_.z_ > max_.z_)
  188. max_.z_ = box.max_.z_;
  189. }
  190. /// Define from an array of vertices.
  191. void Define(const Vector3* vertices, unsigned count);
  192. /// Define from a frustum.
  193. void Define(const Frustum& frustum);
  194. /// Define from a polyhedron.
  195. void Define(const Polyhedron& poly);
  196. /// Define from a sphere.
  197. void Define(const Sphere& sphere);
  198. /// Merge an array of vertices.
  199. void Merge(const Vector3* vertices, unsigned count);
  200. /// Merge a frustum.
  201. void Merge(const Frustum& frustum);
  202. /// Merge a polyhedron.
  203. void Merge(const Polyhedron& poly);
  204. /// Merge a sphere.
  205. void Merge(const Sphere& sphere);
  206. /// Clip with another bounding box.
  207. void Clip(const BoundingBox& box);
  208. /// Transform with a 3x3 matrix.
  209. void Transform(const Matrix3& transform);
  210. /// Transform with a 3x4 matrix.
  211. void Transform(const Matrix3x4& transform);
  212. /// Clear to undefined state.
  213. void Clear()
  214. {
  215. min_ = Vector3::ZERO;
  216. max_ = Vector3::ZERO;
  217. defined_ = false;
  218. }
  219. /// Return center.
  220. Vector3 Center() const { return (max_ + min_) * 0.5f; }
  221. /// Return size.
  222. Vector3 Size() const { return max_ - min_; }
  223. /// Return half-size.
  224. Vector3 HalfSize() const { return (max_ - min_) * 0.5f; }
  225. /// Return transformed by a 3x3 matrix.
  226. BoundingBox Transformed(const Matrix3& transform) const;
  227. /// Return transformed by a 3x4 matrix.
  228. BoundingBox Transformed(const Matrix3x4& transform) const;
  229. /// Return projected by a 4x4 projection matrix.
  230. Rect Projected(const Matrix4& projection) const;
  231. /// Test if a point is inside.
  232. Intersection IsInside(const Vector3& point) const
  233. {
  234. if (point.x_ < min_.x_ || point.x_ > max_.x_ || point.y_ < min_.y_ || point.y_ > max_.y_ ||
  235. point.z_ < min_.z_ || point.z_ > max_.z_)
  236. return OUTSIDE;
  237. else
  238. return INSIDE;
  239. }
  240. /// Test if another bounding box is inside, outside or intersects.
  241. Intersection IsInside(const BoundingBox& box) const
  242. {
  243. if (box.max_.x_ < min_.x_ || box.min_.x_ > max_.x_ || box.max_.y_ < min_.y_ || box.min_.y_ > max_.y_ ||
  244. box.max_.z_ < min_.z_ || box.min_.z_ > max_.z_)
  245. return OUTSIDE;
  246. else if (box.min_.x_ < min_.x_ || box.max_.x_ > max_.x_ || box.min_.y_ < min_.y_ || box.max_.y_ > max_.y_ ||
  247. box.min_.z_ < min_.z_ || box.max_.z_ > max_.z_)
  248. return INTERSECTS;
  249. else
  250. return INSIDE;
  251. }
  252. /// Test if another bounding box is (partially) inside or outside.
  253. Intersection IsInsideFast(const BoundingBox& box) const
  254. {
  255. if (box.max_.x_ < min_.x_ || box.min_.x_ > max_.x_ || box.max_.y_ < min_.y_ || box.min_.y_ > max_.y_ ||
  256. box.max_.z_ < min_.z_ || box.min_.z_ > max_.z_)
  257. return OUTSIDE;
  258. else
  259. return INSIDE;
  260. }
  261. /// Test if a sphere is inside, outside or intersects.
  262. Intersection IsInside(const Sphere& sphere) const;
  263. /// Test if a sphere is (partially) inside or outside.
  264. Intersection IsInsideFast(const Sphere& sphere) const;
  265. /// Minimum vector.
  266. Vector3 min_;
  267. /// Maximum vector.
  268. Vector3 max_;
  269. /// Defined flag.
  270. bool defined_;
  271. };