BoundingBox.h 9.2 KB

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