BoundingBox.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Math/Rect.h"
  5. #include "../Math/Vector3.h"
  6. #ifdef URHO3D_SSE
  7. #include <xmmintrin.h>
  8. #endif
  9. namespace Urho3D
  10. {
  11. class Polyhedron;
  12. class Frustum;
  13. class Matrix3;
  14. class Matrix4;
  15. class Matrix3x4;
  16. class Sphere;
  17. /// Three-dimensional axis-aligned bounding box.
  18. /// @allfloats
  19. class URHO3D_API BoundingBox
  20. {
  21. public:
  22. /// Construct with zero size.
  23. BoundingBox() noexcept :
  24. min_(M_INFINITY, M_INFINITY, M_INFINITY),
  25. max_(-M_INFINITY, -M_INFINITY, -M_INFINITY)
  26. {
  27. }
  28. /// Copy-construct from another bounding box.
  29. BoundingBox(const BoundingBox& box) noexcept :
  30. min_(box.min_),
  31. max_(box.max_)
  32. {
  33. }
  34. /// Construct from a rect, with the Z dimension left zero.
  35. explicit BoundingBox(const Rect& rect) noexcept :
  36. min_(Vector3(rect.min_, 0.0f)),
  37. max_(Vector3(rect.max_, 0.0f))
  38. {
  39. }
  40. /// Construct from minimum and maximum vectors.
  41. BoundingBox(const Vector3& min, const Vector3& max) noexcept :
  42. min_(min),
  43. max_(max)
  44. {
  45. }
  46. /// Construct from minimum and maximum floats (all dimensions same).
  47. BoundingBox(float min, float max) noexcept :
  48. min_(Vector3(min, min, min)),
  49. max_(Vector3(max, max, max))
  50. {
  51. }
  52. #ifdef URHO3D_SSE
  53. /// @nobind
  54. BoundingBox(__m128 min, __m128 max) noexcept
  55. {
  56. _mm_storeu_ps(&min_.x_, min);
  57. _mm_storeu_ps(&max_.x_, max);
  58. }
  59. #endif
  60. /// Construct from an array of vertices.
  61. BoundingBox(const Vector3* vertices, unsigned count) :
  62. min_(M_INFINITY, M_INFINITY, M_INFINITY),
  63. max_(-M_INFINITY, -M_INFINITY, -M_INFINITY)
  64. {
  65. Define(vertices, count);
  66. }
  67. /// Construct from a frustum.
  68. explicit BoundingBox(const Frustum& frustum) :
  69. min_(M_INFINITY, M_INFINITY, M_INFINITY),
  70. max_(-M_INFINITY, -M_INFINITY, -M_INFINITY)
  71. {
  72. Define(frustum);
  73. }
  74. /// Construct from a polyhedron.
  75. explicit BoundingBox(const Polyhedron& poly) :
  76. min_(M_INFINITY, M_INFINITY, M_INFINITY),
  77. max_(-M_INFINITY, -M_INFINITY, -M_INFINITY)
  78. {
  79. Define(poly);
  80. }
  81. /// Construct from a sphere.
  82. explicit BoundingBox(const Sphere& sphere) :
  83. min_(M_INFINITY, M_INFINITY, M_INFINITY),
  84. max_(-M_INFINITY, -M_INFINITY, -M_INFINITY)
  85. {
  86. Define(sphere);
  87. }
  88. /// Assign from another bounding box.
  89. BoundingBox& operator =(const BoundingBox& rhs) noexcept
  90. {
  91. min_ = rhs.min_;
  92. max_ = rhs.max_;
  93. return *this;
  94. }
  95. /// Assign from a Rect, with the Z dimension left zero.
  96. BoundingBox& operator =(const Rect& rhs) noexcept
  97. {
  98. min_ = Vector3(rhs.min_, 0.0f);
  99. max_ = Vector3(rhs.max_, 0.0f);
  100. return *this;
  101. }
  102. /// Test for equality with another bounding box.
  103. bool operator ==(const BoundingBox& rhs) const { return (min_ == rhs.min_ && max_ == rhs.max_); }
  104. /// Test for inequality with another bounding box.
  105. bool operator !=(const BoundingBox& rhs) const { return (min_ != rhs.min_ || max_ != rhs.max_); }
  106. /// Define from another bounding box.
  107. void Define(const BoundingBox& box)
  108. {
  109. Define(box.min_, box.max_);
  110. }
  111. /// Define from a Rect.
  112. void Define(const Rect& rect)
  113. {
  114. Define(Vector3(rect.min_, 0.0f), Vector3(rect.max_, 0.0f));
  115. }
  116. /// Define from minimum and maximum vectors.
  117. void Define(const Vector3& min, const Vector3& max)
  118. {
  119. min_ = min;
  120. max_ = max;
  121. }
  122. /// Define from minimum and maximum floats (all dimensions same).
  123. void Define(float min, float max)
  124. {
  125. min_ = Vector3(min, min, min);
  126. max_ = Vector3(max, max, max);
  127. }
  128. /// Define from a point.
  129. void Define(const Vector3& point)
  130. {
  131. min_ = max_ = point;
  132. }
  133. /// Merge a point.
  134. void Merge(const Vector3& point)
  135. {
  136. #ifdef URHO3D_SSE
  137. __m128 vec = _mm_set_ps(1.f, point.z_, point.y_, point.x_);
  138. _mm_storeu_ps(&min_.x_, _mm_min_ps(_mm_loadu_ps(&min_.x_), vec));
  139. _mm_storeu_ps(&max_.x_, _mm_max_ps(_mm_loadu_ps(&max_.x_), vec));
  140. #else
  141. if (point.x_ < min_.x_)
  142. min_.x_ = point.x_;
  143. if (point.y_ < min_.y_)
  144. min_.y_ = point.y_;
  145. if (point.z_ < min_.z_)
  146. min_.z_ = point.z_;
  147. if (point.x_ > max_.x_)
  148. max_.x_ = point.x_;
  149. if (point.y_ > max_.y_)
  150. max_.y_ = point.y_;
  151. if (point.z_ > max_.z_)
  152. max_.z_ = point.z_;
  153. #endif
  154. }
  155. /// Merge another bounding box.
  156. void Merge(const BoundingBox& box)
  157. {
  158. #ifdef URHO3D_SSE
  159. _mm_storeu_ps(&min_.x_, _mm_min_ps(_mm_loadu_ps(&min_.x_), _mm_loadu_ps(&box.min_.x_)));
  160. _mm_storeu_ps(&max_.x_, _mm_max_ps(_mm_loadu_ps(&max_.x_), _mm_loadu_ps(&box.max_.x_)));
  161. #else
  162. if (box.min_.x_ < min_.x_)
  163. min_.x_ = box.min_.x_;
  164. if (box.min_.y_ < min_.y_)
  165. min_.y_ = box.min_.y_;
  166. if (box.min_.z_ < min_.z_)
  167. min_.z_ = box.min_.z_;
  168. if (box.max_.x_ > max_.x_)
  169. max_.x_ = box.max_.x_;
  170. if (box.max_.y_ > max_.y_)
  171. max_.y_ = box.max_.y_;
  172. if (box.max_.z_ > max_.z_)
  173. max_.z_ = box.max_.z_;
  174. #endif
  175. }
  176. /// Define from an array of vertices.
  177. void Define(const Vector3* vertices, unsigned count);
  178. /// Define from a frustum.
  179. void Define(const Frustum& frustum);
  180. /// Define from a polyhedron.
  181. void Define(const Polyhedron& poly);
  182. /// Define from a sphere.
  183. void Define(const Sphere& sphere);
  184. /// Merge an array of vertices.
  185. void Merge(const Vector3* vertices, unsigned count);
  186. /// Merge a frustum.
  187. void Merge(const Frustum& frustum);
  188. /// Merge a polyhedron.
  189. void Merge(const Polyhedron& poly);
  190. /// Merge a sphere.
  191. void Merge(const Sphere& sphere);
  192. /// Clip with another bounding box. The box can become degenerate (undefined) as a result.
  193. void Clip(const BoundingBox& box);
  194. /// Transform with a 3x3 matrix.
  195. void Transform(const Matrix3& transform);
  196. /// Transform with a 3x4 matrix.
  197. void Transform(const Matrix3x4& transform);
  198. /// Clear to undefined state.
  199. void Clear()
  200. {
  201. #ifdef URHO3D_SSE
  202. _mm_storeu_ps(&min_.x_, _mm_set1_ps(M_INFINITY));
  203. _mm_storeu_ps(&max_.x_, _mm_set1_ps(-M_INFINITY));
  204. #else
  205. min_ = Vector3(M_INFINITY, M_INFINITY, M_INFINITY);
  206. max_ = Vector3(-M_INFINITY, -M_INFINITY, -M_INFINITY);
  207. #endif
  208. }
  209. /// Return true if this bounding box is defined via a previous call to Define() or Merge().
  210. bool Defined() const
  211. {
  212. return min_.x_ != M_INFINITY;
  213. }
  214. /// Return center.
  215. /// @property
  216. Vector3 Center() const { return (max_ + min_) * 0.5f; }
  217. /// Return size.
  218. /// @property
  219. Vector3 Size() const { return max_ - min_; }
  220. /// Return half-size.
  221. /// @property
  222. Vector3 HalfSize() const { return (max_ - min_) * 0.5f; }
  223. /// Return transformed by a 3x3 matrix.
  224. BoundingBox Transformed(const Matrix3& transform) const;
  225. /// Return transformed by a 3x4 matrix.
  226. BoundingBox Transformed(const Matrix3x4& transform) const;
  227. /// Return projected by a 4x4 projection matrix.
  228. Rect Projected(const Matrix4& projection) const;
  229. /// Return distance to point.
  230. float DistanceToPoint(const Vector3& point) 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. /// Return as string.
  266. String ToString() const;
  267. /// Minimum vector.
  268. Vector3 min_;
  269. float dummyMin_{}; // This is never used, but exists to pad the min_ value to four floats.
  270. /// Maximum vector.
  271. Vector3 max_;
  272. float dummyMax_{}; // This is never used, but exists to pad the max_ value to four floats.
  273. };
  274. }