BoundingBox.h 10 KB

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