BoundingBox.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. //
  2. // Copyright (c) 2008-2017 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. #ifdef ATOMIC_SSE
  26. #include <xmmintrin.h>
  27. #endif
  28. namespace Atomic
  29. {
  30. class Polyhedron;
  31. class Frustum;
  32. class Matrix3;
  33. class Matrix4;
  34. class Matrix3x4;
  35. class Sphere;
  36. /// Three-dimensional axis-aligned bounding box.
  37. class ATOMIC_API BoundingBox
  38. {
  39. public:
  40. /// Construct with zero size.
  41. BoundingBox() :
  42. min_(M_INFINITY, M_INFINITY, M_INFINITY),
  43. max_(-M_INFINITY, -M_INFINITY, -M_INFINITY)
  44. {
  45. }
  46. /// Copy-construct from another bounding box.
  47. BoundingBox(const BoundingBox& box) :
  48. min_(box.min_),
  49. max_(box.max_)
  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. {
  57. }
  58. /// Construct from minimum and maximum vectors.
  59. BoundingBox(const Vector3& min, const Vector3& max) :
  60. min_(min),
  61. max_(max)
  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. {
  69. }
  70. #ifdef ATOMIC_SSE
  71. BoundingBox(__m128 min, __m128 max)
  72. {
  73. _mm_storeu_ps(&min_.x_, min);
  74. _mm_storeu_ps(&max_.x_, max);
  75. }
  76. #endif
  77. /// Construct from an array of vertices.
  78. BoundingBox(const Vector3* vertices, unsigned count) :
  79. min_(M_INFINITY, M_INFINITY, M_INFINITY),
  80. max_(-M_INFINITY, -M_INFINITY, -M_INFINITY)
  81. {
  82. Define(vertices, count);
  83. }
  84. /// Construct from a frustum.
  85. BoundingBox(const Frustum& frustum) :
  86. min_(M_INFINITY, M_INFINITY, M_INFINITY),
  87. max_(-M_INFINITY, -M_INFINITY, -M_INFINITY)
  88. {
  89. Define(frustum);
  90. }
  91. /// Construct from a polyhedron.
  92. BoundingBox(const Polyhedron& poly) :
  93. min_(M_INFINITY, M_INFINITY, M_INFINITY),
  94. max_(-M_INFINITY, -M_INFINITY, -M_INFINITY)
  95. {
  96. Define(poly);
  97. }
  98. /// Construct from a sphere.
  99. BoundingBox(const Sphere& sphere) :
  100. min_(M_INFINITY, M_INFINITY, M_INFINITY),
  101. max_(-M_INFINITY, -M_INFINITY, -M_INFINITY)
  102. {
  103. Define(sphere);
  104. }
  105. /// Assign from another bounding box.
  106. BoundingBox& operator =(const BoundingBox& rhs)
  107. {
  108. min_ = rhs.min_;
  109. max_ = rhs.max_;
  110. return *this;
  111. }
  112. /// Assign from a Rect, with the Z dimension left zero.
  113. BoundingBox& operator =(const Rect& rhs)
  114. {
  115. min_ = Vector3(rhs.min_, 0.0f);
  116. max_ = Vector3(rhs.max_, 0.0f);
  117. return *this;
  118. }
  119. /// Test for equality with another bounding box.
  120. bool operator ==(const BoundingBox& rhs) const { return (min_ == rhs.min_ && max_ == rhs.max_); }
  121. /// Test for inequality with another bounding box.
  122. bool operator !=(const BoundingBox& rhs) const { return (min_ != rhs.min_ || max_ != rhs.max_); }
  123. /// Define from another bounding box.
  124. void Define(const BoundingBox& box)
  125. {
  126. Define(box.min_, box.max_);
  127. }
  128. /// Define from a Rect.
  129. void Define(const Rect& rect)
  130. {
  131. Define(Vector3(rect.min_, 0.0f), Vector3(rect.max_, 0.0f));
  132. }
  133. /// Define from minimum and maximum vectors.
  134. void Define(const Vector3& min, const Vector3& max)
  135. {
  136. min_ = min;
  137. max_ = max;
  138. }
  139. /// Define from minimum and maximum floats (all dimensions same.)
  140. void Define(float min, float max)
  141. {
  142. min_ = Vector3(min, min, min);
  143. max_ = Vector3(max, max, max);
  144. }
  145. /// Define from a point.
  146. void Define(const Vector3& point)
  147. {
  148. min_ = max_ = point;
  149. }
  150. /// Merge a point.
  151. void Merge(const Vector3& point)
  152. {
  153. #ifdef ATOMIC_SSE
  154. __m128 vec = _mm_set_ps(1.f, point.z_, point.y_, point.x_);
  155. _mm_storeu_ps(&min_.x_, _mm_min_ps(_mm_loadu_ps(&min_.x_), vec));
  156. _mm_storeu_ps(&max_.x_, _mm_max_ps(_mm_loadu_ps(&max_.x_), vec));
  157. #else
  158. if (point.x_ < min_.x_)
  159. min_.x_ = point.x_;
  160. if (point.y_ < min_.y_)
  161. min_.y_ = point.y_;
  162. if (point.z_ < min_.z_)
  163. min_.z_ = point.z_;
  164. if (point.x_ > max_.x_)
  165. max_.x_ = point.x_;
  166. if (point.y_ > max_.y_)
  167. max_.y_ = point.y_;
  168. if (point.z_ > max_.z_)
  169. max_.z_ = point.z_;
  170. #endif
  171. }
  172. /// Merge another bounding box.
  173. void Merge(const BoundingBox& box)
  174. {
  175. #ifdef ATOMIC_SSE
  176. _mm_storeu_ps(&min_.x_, _mm_min_ps(_mm_loadu_ps(&min_.x_), _mm_loadu_ps(&box.min_.x_)));
  177. _mm_storeu_ps(&max_.x_, _mm_max_ps(_mm_loadu_ps(&max_.x_), _mm_loadu_ps(&box.max_.x_)));
  178. #else
  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. #endif
  192. }
  193. /// Define from an array of vertices.
  194. void Define(const Vector3* vertices, unsigned count);
  195. /// Define from a frustum.
  196. void Define(const Frustum& frustum);
  197. /// Define from a polyhedron.
  198. void Define(const Polyhedron& poly);
  199. /// Define from a sphere.
  200. void Define(const Sphere& sphere);
  201. /// Merge an array of vertices.
  202. void Merge(const Vector3* vertices, unsigned count);
  203. /// Merge a frustum.
  204. void Merge(const Frustum& frustum);
  205. /// Merge a polyhedron.
  206. void Merge(const Polyhedron& poly);
  207. /// Merge a sphere.
  208. void Merge(const Sphere& sphere);
  209. /// Clip with another bounding box. The box can become degenerate (undefined) as a result.
  210. void Clip(const BoundingBox& box);
  211. /// Transform with a 3x3 matrix.
  212. void Transform(const Matrix3& transform);
  213. /// Transform with a 3x4 matrix.
  214. void Transform(const Matrix3x4& transform);
  215. /// Clear to undefined state.
  216. void Clear()
  217. {
  218. #ifdef ATOMIC_SSE
  219. _mm_storeu_ps(&min_.x_, _mm_set1_ps(M_INFINITY));
  220. _mm_storeu_ps(&max_.x_, _mm_set1_ps(-M_INFINITY));
  221. #else
  222. min_ = Vector3(M_INFINITY, M_INFINITY, M_INFINITY);
  223. max_ = Vector3(-M_INFINITY, -M_INFINITY, -M_INFINITY);
  224. #endif
  225. }
  226. /// Return true if this bounding box is defined via a previous call to Define() or Merge().
  227. bool Defined() const
  228. {
  229. return min_.x_ != M_INFINITY;
  230. }
  231. /// Return center.
  232. Vector3 Center() const { return (max_ + min_) * 0.5f; }
  233. /// Return size.
  234. Vector3 Size() const { return max_ - min_; }
  235. /// Return half-size.
  236. Vector3 HalfSize() const { return (max_ - min_) * 0.5f; }
  237. /// Return transformed by a 3x3 matrix.
  238. BoundingBox Transformed(const Matrix3& transform) const;
  239. /// Return transformed by a 3x4 matrix.
  240. BoundingBox Transformed(const Matrix3x4& transform) const;
  241. /// Return projected by a 4x4 projection matrix.
  242. Rect Projected(const Matrix4& projection) const;
  243. /// Test if a point is inside.
  244. Intersection IsInside(const Vector3& point) const
  245. {
  246. if (point.x_ < min_.x_ || point.x_ > max_.x_ || point.y_ < min_.y_ || point.y_ > max_.y_ ||
  247. point.z_ < min_.z_ || point.z_ > max_.z_)
  248. return OUTSIDE;
  249. else
  250. return INSIDE;
  251. }
  252. /// Test if another bounding box is inside, outside or intersects.
  253. Intersection IsInside(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 if (box.min_.x_ < min_.x_ || box.max_.x_ > max_.x_ || box.min_.y_ < min_.y_ || box.max_.y_ > max_.y_ ||
  259. box.min_.z_ < min_.z_ || box.max_.z_ > max_.z_)
  260. return INTERSECTS;
  261. else
  262. return INSIDE;
  263. }
  264. /// Test if another bounding box is (partially) inside or outside.
  265. Intersection IsInsideFast(const BoundingBox& box) const
  266. {
  267. if (box.max_.x_ < min_.x_ || box.min_.x_ > max_.x_ || box.max_.y_ < min_.y_ || box.min_.y_ > max_.y_ ||
  268. box.max_.z_ < min_.z_ || box.min_.z_ > max_.z_)
  269. return OUTSIDE;
  270. else
  271. return INSIDE;
  272. }
  273. /// Test if a sphere is inside, outside or intersects.
  274. Intersection IsInside(const Sphere& sphere) const;
  275. /// Test if a sphere is (partially) inside or outside.
  276. Intersection IsInsideFast(const Sphere& sphere) const;
  277. /// Return as string.
  278. String ToString() const;
  279. /// Minimum vector.
  280. Vector3 min_;
  281. float dummyMin_; // This is never used, but exists to pad the min_ value to four floats.
  282. /// Maximum vector.
  283. Vector3 max_;
  284. float dummyMax_; // This is never used, but exists to pad the max_ value to four floats.
  285. // ATOMIC BEGIN
  286. float data_[6];
  287. BoundingBox(float* data) :
  288. min_(Vector3(data[0], data[1], data[2])),
  289. max_(Vector3(data[3], data[4], data[5]))
  290. {
  291. }
  292. float* Data() const
  293. {
  294. float* data = const_cast<BoundingBox*>(this)->data_;
  295. data[0] = min_.x_;
  296. data[1] = min_.y_;
  297. data[2] = min_.z_;
  298. data[3] = max_.x_;
  299. data[4] = max_.y_;
  300. data[5] = max_.z_;
  301. return data;
  302. }
  303. // ATOMIC END
  304. };
  305. }