BoundingBox.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "Rect.h"
  24. #include "Vector3.h"
  25. namespace Urho3D
  26. {
  27. class Polyhedron;
  28. class Frustum;
  29. class Matrix3;
  30. class Matrix4;
  31. class Matrix3x4;
  32. class Sphere;
  33. /// Three-dimensional axis-aligned bounding box.
  34. class URHO3D_API 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. /// Construct from an array of vertices.
  73. BoundingBox(const Vector3* vertices, unsigned count) :
  74. defined_(false)
  75. {
  76. Define(vertices, count);
  77. }
  78. /// Construct from a frustum.
  79. BoundingBox(const Frustum& frustum) :
  80. defined_(false)
  81. {
  82. Define(frustum);
  83. }
  84. /// Construct from a polyhedron.
  85. BoundingBox(const Polyhedron& poly) :
  86. defined_(false)
  87. {
  88. Define(poly);
  89. }
  90. /// Construct from a sphere.
  91. BoundingBox(const Sphere& sphere) :
  92. defined_(false)
  93. {
  94. Define(sphere);
  95. }
  96. /// Assign from another bounding box.
  97. BoundingBox& operator = (const BoundingBox& rhs)
  98. {
  99. min_ = rhs.min_;
  100. max_ = rhs.max_;
  101. defined_ = rhs.defined_;
  102. return *this;
  103. }
  104. /// Assign from a Rect, with the Z dimension left zero.
  105. BoundingBox& operator = (const Rect& rhs)
  106. {
  107. min_ = Vector3(rhs.min_, 0.0f);
  108. max_ = Vector3(rhs.max_, 0.0f);
  109. defined_ = true;
  110. return *this;
  111. }
  112. /// Test for equality with another bounding box.
  113. bool operator == (const BoundingBox& rhs) const { return (min_ == rhs.min_ && max_ == rhs.max_); }
  114. /// Test for inequality with another bounding box.
  115. bool operator != (const BoundingBox& rhs) const { return (min_ != rhs.min_ || max_ != rhs.max_); }
  116. /// Define from another bounding box.
  117. void Define(const BoundingBox& box)
  118. {
  119. Define(box.min_, box.max_);
  120. }
  121. /// Define from a Rect.
  122. void Define(const Rect& rect)
  123. {
  124. Define(Vector3(rect.min_, 0.0f), Vector3(rect.max_, 0.0f));
  125. }
  126. /// Define from minimum and maximum vectors.
  127. void Define(const Vector3& min, const Vector3& max)
  128. {
  129. min_ = min;
  130. max_ = max;
  131. defined_ = true;
  132. }
  133. /// Define from minimum and maximum floats (all dimensions same.)
  134. void Define(float min, float max)
  135. {
  136. min_ = Vector3(min, min, min);
  137. max_ = Vector3(max, max, max);
  138. defined_ = true;
  139. }
  140. /// Define from a point.
  141. void Define(const Vector3& point)
  142. {
  143. min_ = max_ = point;
  144. defined_ = true;
  145. }
  146. /// Merge a point.
  147. void Merge(const Vector3& point)
  148. {
  149. if (!defined_)
  150. {
  151. min_ = max_ = point;
  152. defined_ = true;
  153. return;
  154. }
  155. if (point.x_ < min_.x_)
  156. min_.x_ = point.x_;
  157. if (point.y_ < min_.y_)
  158. min_.y_ = point.y_;
  159. if (point.z_ < min_.z_)
  160. min_.z_ = point.z_;
  161. if (point.x_ > max_.x_)
  162. max_.x_ = point.x_;
  163. if (point.y_ > max_.y_)
  164. max_.y_ = point.y_;
  165. if (point.z_ > max_.z_)
  166. max_.z_ = point.z_;
  167. }
  168. /// Merge another bounding box.
  169. void Merge(const BoundingBox& box)
  170. {
  171. if (!defined_)
  172. {
  173. min_ = box.min_;
  174. max_ = box.max_;
  175. defined_ = true;
  176. return;
  177. }
  178. if (box.min_.x_ < min_.x_)
  179. min_.x_ = box.min_.x_;
  180. if (box.min_.y_ < min_.y_)
  181. min_.y_ = box.min_.y_;
  182. if (box.min_.z_ < min_.z_)
  183. min_.z_ = box.min_.z_;
  184. if (box.max_.x_ > max_.x_)
  185. max_.x_ = box.max_.x_;
  186. if (box.max_.y_ > max_.y_)
  187. max_.y_ = box.max_.y_;
  188. if (box.max_.z_ > max_.z_)
  189. max_.z_ = box.max_.z_;
  190. }
  191. /// Define from an array of vertices.
  192. void Define(const Vector3* vertices, unsigned count);
  193. /// Define from a frustum.
  194. void Define(const Frustum& frustum);
  195. /// Define from a polyhedron.
  196. void Define(const Polyhedron& poly);
  197. /// Define from a sphere.
  198. void Define(const Sphere& sphere);
  199. /// Merge an array of vertices.
  200. void Merge(const Vector3* vertices, unsigned count);
  201. /// Merge a frustum.
  202. void Merge(const Frustum& frustum);
  203. /// Merge a polyhedron.
  204. void Merge(const Polyhedron& poly);
  205. /// Merge a sphere.
  206. void Merge(const Sphere& sphere);
  207. /// Clip with another bounding box.
  208. void Clip(const BoundingBox& box);
  209. /// Transform with a 3x3 matrix.
  210. void Transform(const Matrix3& transform);
  211. /// Transform with a 3x4 matrix.
  212. void Transform(const Matrix3x4& transform);
  213. /// Clear to undefined state.
  214. void Clear()
  215. {
  216. min_ = Vector3::ZERO;
  217. max_ = Vector3::ZERO;
  218. defined_ = false;
  219. }
  220. /// Return center.
  221. Vector3 Center() const { return (max_ + min_) * 0.5f; }
  222. /// Return size.
  223. Vector3 Size() const { return max_ - min_; }
  224. /// Return half-size.
  225. Vector3 HalfSize() const { return (max_ - min_) * 0.5f; }
  226. /// Return transformed by a 3x3 matrix.
  227. BoundingBox Transformed(const Matrix3& transform) const;
  228. /// Return transformed by a 3x4 matrix.
  229. BoundingBox Transformed(const Matrix3x4& transform) const;
  230. /// Return projected by a 4x4 projection matrix.
  231. Rect Projected(const Matrix4& projection) const;
  232. /// Test if a point is inside.
  233. Intersection IsInside(const Vector3& point) const
  234. {
  235. if (point.x_ < min_.x_ || point.x_ > max_.x_ || point.y_ < min_.y_ || point.y_ > max_.y_ ||
  236. point.z_ < min_.z_ || point.z_ > max_.z_)
  237. return OUTSIDE;
  238. else
  239. return INSIDE;
  240. }
  241. /// Test if another bounding box is inside, outside or intersects.
  242. Intersection IsInside(const BoundingBox& box) const
  243. {
  244. if (box.max_.x_ < min_.x_ || box.min_.x_ > max_.x_ || box.max_.y_ < min_.y_ || box.min_.y_ > max_.y_ ||
  245. box.max_.z_ < min_.z_ || box.min_.z_ > max_.z_)
  246. return OUTSIDE;
  247. else if (box.min_.x_ < min_.x_ || box.max_.x_ > max_.x_ || box.min_.y_ < min_.y_ || box.max_.y_ > max_.y_ ||
  248. box.min_.z_ < min_.z_ || box.max_.z_ > max_.z_)
  249. return INTERSECTS;
  250. else
  251. return INSIDE;
  252. }
  253. /// Test if another bounding box is (partially) inside or outside.
  254. Intersection IsInsideFast(const BoundingBox& box) const
  255. {
  256. if (box.max_.x_ < min_.x_ || box.min_.x_ > max_.x_ || box.max_.y_ < min_.y_ || box.min_.y_ > max_.y_ ||
  257. box.max_.z_ < min_.z_ || box.min_.z_ > max_.z_)
  258. return OUTSIDE;
  259. else
  260. return INSIDE;
  261. }
  262. /// Test if a sphere is inside, outside or intersects.
  263. Intersection IsInside(const Sphere& sphere) const;
  264. /// Test if a sphere is (partially) inside or outside.
  265. Intersection IsInsideFast(const Sphere& sphere) const;
  266. /// Return as string.
  267. String ToString() const;
  268. /// Minimum vector.
  269. Vector3 min_;
  270. /// Maximum vector.
  271. Vector3 max_;
  272. /// Defined flag.
  273. bool defined_;
  274. };
  275. }