BoundingBox.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //
  2. // Copyright (c) 2008-2015 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. #include "../Precompiled.h"
  23. #include "../Math/Frustum.h"
  24. #include "../Math/Polyhedron.h"
  25. #include "../DebugNew.h"
  26. namespace Urho3D
  27. {
  28. void BoundingBox::Define(const Vector3* vertices, unsigned count)
  29. {
  30. Clear();
  31. if (!count)
  32. return;
  33. Merge(vertices, count);
  34. }
  35. void BoundingBox::Define(const Frustum& frustum)
  36. {
  37. Clear();
  38. Define(frustum.vertices_, NUM_FRUSTUM_VERTICES);
  39. }
  40. void BoundingBox::Define(const Polyhedron& poly)
  41. {
  42. Clear();
  43. Merge(poly);
  44. }
  45. void BoundingBox::Define(const Sphere& sphere)
  46. {
  47. const Vector3& center = sphere.center_;
  48. float radius = sphere.radius_;
  49. min_ = center + Vector3(-radius, -radius, -radius);
  50. max_ = center + Vector3(radius, radius, radius);
  51. }
  52. void BoundingBox::Merge(const Vector3* vertices, unsigned count)
  53. {
  54. while (count--)
  55. Merge(*vertices++);
  56. }
  57. void BoundingBox::Merge(const Frustum& frustum)
  58. {
  59. Merge(frustum.vertices_, NUM_FRUSTUM_VERTICES);
  60. }
  61. void BoundingBox::Merge(const Polyhedron& poly)
  62. {
  63. for (unsigned i = 0; i < poly.faces_.Size(); ++i)
  64. {
  65. const PODVector<Vector3>& face = poly.faces_[i];
  66. if (!face.Empty())
  67. Merge(&face[0], face.Size());
  68. }
  69. }
  70. void BoundingBox::Merge(const Sphere& sphere)
  71. {
  72. const Vector3& center = sphere.center_;
  73. float radius = sphere.radius_;
  74. Merge(center + Vector3(radius, radius, radius));
  75. Merge(center + Vector3(-radius, -radius, -radius));
  76. }
  77. void BoundingBox::Clip(const BoundingBox& box)
  78. {
  79. if (box.min_.x_ > min_.x_)
  80. min_.x_ = box.min_.x_;
  81. if (box.max_.x_ < max_.x_)
  82. max_.x_ = box.max_.x_;
  83. if (box.min_.y_ > min_.y_)
  84. min_.y_ = box.min_.y_;
  85. if (box.max_.y_ < max_.y_)
  86. max_.y_ = box.max_.y_;
  87. if (box.min_.z_ > min_.z_)
  88. min_.z_ = box.min_.z_;
  89. if (box.max_.z_ < max_.z_)
  90. max_.z_ = box.max_.z_;
  91. if (min_.x_ > max_.x_ || min_.y_ > max_.y_ || min_.z_ > max_.z_)
  92. {
  93. min_ = Vector3(M_INFINITY, M_INFINITY, M_INFINITY);
  94. max_ = Vector3(-M_INFINITY, -M_INFINITY, -M_INFINITY);
  95. }
  96. }
  97. void BoundingBox::Transform(const Matrix3& transform)
  98. {
  99. *this = Transformed(Matrix3x4(transform));
  100. }
  101. void BoundingBox::Transform(const Matrix3x4& transform)
  102. {
  103. *this = Transformed(transform);
  104. }
  105. BoundingBox BoundingBox::Transformed(const Matrix3& transform) const
  106. {
  107. return Transformed(Matrix3x4(transform));
  108. }
  109. BoundingBox BoundingBox::Transformed(const Matrix3x4& transform) const
  110. {
  111. #ifdef URHO3D_SSE
  112. __m128 minPt = _mm_set_ps(1.f, min_.z_, min_.y_, min_.x_);
  113. __m128 maxPt = _mm_set_ps(1.f, max_.z_, max_.y_, max_.x_);
  114. __m128 centerPoint = _mm_mul_ps(_mm_add_ps(minPt, maxPt), _mm_set1_ps(0.5f));
  115. __m128 halfSize = _mm_sub_ps(centerPoint, minPt);
  116. __m128 m0 = _mm_loadu_ps(&transform.m00_);
  117. __m128 m1 = _mm_loadu_ps(&transform.m10_);
  118. __m128 m2 = _mm_loadu_ps(&transform.m20_);
  119. __m128 r0 = _mm_mul_ps(m0, centerPoint);
  120. __m128 r1 = _mm_mul_ps(m1, centerPoint);
  121. __m128 r2 = _mm_mul_ps(m2, centerPoint);
  122. __m128 r3 = _mm_setzero_ps();
  123. _MM_TRANSPOSE4_PS(r0, r1, r2, r3);
  124. __m128 newCenter = _mm_add_ps(_mm_add_ps(r0, r1), _mm_add_ps(r2, r3));
  125. __m128 absMask = _mm_castsi128_ps(_mm_set1_epi32(0x7FFFFFFF));
  126. __m128 x = _mm_and_ps(absMask, _mm_mul_ps(m0, halfSize));
  127. __m128 y = _mm_and_ps(absMask, _mm_mul_ps(m1, halfSize));
  128. __m128 z = _mm_and_ps(absMask, _mm_mul_ps(m2, halfSize));
  129. __m128 w = _mm_setzero_ps();
  130. _MM_TRANSPOSE4_PS(x, y, z, w);
  131. __m128 newDir = _mm_add_ps(_mm_add_ps(x, y), _mm_add_ps(z, w));
  132. __m128 boxMin = _mm_sub_ps(newCenter, newDir);
  133. __m128 boxMax = _mm_add_ps(newCenter, newDir);
  134. return BoundingBox(
  135. Vector3(_mm_cvtss_f32(boxMin),
  136. _mm_cvtss_f32(_mm_shuffle_ps(boxMin, boxMin, _MM_SHUFFLE(1, 1, 1, 1))),
  137. _mm_cvtss_f32(_mm_movehl_ps(boxMin, boxMin))),
  138. Vector3(_mm_cvtss_f32(boxMax),
  139. _mm_cvtss_f32(_mm_shuffle_ps(boxMax, boxMax, _MM_SHUFFLE(1, 1, 1, 1))),
  140. _mm_cvtss_f32(_mm_movehl_ps(boxMax, boxMax))));
  141. #else
  142. Vector3 newCenter = transform * Center();
  143. Vector3 oldEdge = Size() * 0.5f;
  144. Vector3 newEdge = Vector3(
  145. Abs(transform.m00_) * oldEdge.x_ + Abs(transform.m01_) * oldEdge.y_ + Abs(transform.m02_) * oldEdge.z_,
  146. Abs(transform.m10_) * oldEdge.x_ + Abs(transform.m11_) * oldEdge.y_ + Abs(transform.m12_) * oldEdge.z_,
  147. Abs(transform.m20_) * oldEdge.x_ + Abs(transform.m21_) * oldEdge.y_ + Abs(transform.m22_) * oldEdge.z_
  148. );
  149. return BoundingBox(newCenter - newEdge, newCenter + newEdge);
  150. #endif
  151. }
  152. Rect BoundingBox::Projected(const Matrix4& projection) const
  153. {
  154. Vector3 projMin = min_;
  155. Vector3 projMax = max_;
  156. if (projMin.z_ < M_MIN_NEARCLIP)
  157. projMin.z_ = M_MIN_NEARCLIP;
  158. if (projMax.z_ < M_MIN_NEARCLIP)
  159. projMax.z_ = M_MIN_NEARCLIP;
  160. Vector3 vertices[8];
  161. vertices[0] = projMin;
  162. vertices[1] = Vector3(projMax.x_, projMin.y_, projMin.z_);
  163. vertices[2] = Vector3(projMin.x_, projMax.y_, projMin.z_);
  164. vertices[3] = Vector3(projMax.x_, projMax.y_, projMin.z_);
  165. vertices[4] = Vector3(projMin.x_, projMin.y_, projMax.z_);
  166. vertices[5] = Vector3(projMax.x_, projMin.y_, projMax.z_);
  167. vertices[6] = Vector3(projMin.x_, projMax.y_, projMax.z_);
  168. vertices[7] = projMax;
  169. Rect rect;
  170. for (unsigned i = 0; i < 8; ++i)
  171. {
  172. Vector3 projected = projection * vertices[i];
  173. rect.Merge(Vector2(projected.x_, projected.y_));
  174. }
  175. return rect;
  176. }
  177. Intersection BoundingBox::IsInside(const Sphere& sphere) const
  178. {
  179. float distSquared = 0;
  180. float temp;
  181. const Vector3& center = sphere.center_;
  182. if (center.x_ < min_.x_)
  183. {
  184. temp = center.x_ - min_.x_;
  185. distSquared += temp * temp;
  186. }
  187. else if (center.x_ > max_.x_)
  188. {
  189. temp = center.x_ - max_.x_;
  190. distSquared += temp * temp;
  191. }
  192. if (center.y_ < min_.y_)
  193. {
  194. temp = center.y_ - min_.y_;
  195. distSquared += temp * temp;
  196. }
  197. else if (center.y_ > max_.y_)
  198. {
  199. temp = center.y_ - max_.y_;
  200. distSquared += temp * temp;
  201. }
  202. if (center.z_ < min_.z_)
  203. {
  204. temp = center.z_ - min_.z_;
  205. distSquared += temp * temp;
  206. }
  207. else if (center.z_ > max_.z_)
  208. {
  209. temp = center.z_ - max_.z_;
  210. distSquared += temp * temp;
  211. }
  212. float radius = sphere.radius_;
  213. if (distSquared >= radius * radius)
  214. return OUTSIDE;
  215. else if (center.x_ - radius < min_.x_ || center.x_ + radius > max_.x_ || center.y_ - radius < min_.y_ ||
  216. center.y_ + radius > max_.y_ || center.z_ - radius < min_.z_ || center.z_ + radius > max_.z_)
  217. return INTERSECTS;
  218. else
  219. return INSIDE;
  220. }
  221. Intersection BoundingBox::IsInsideFast(const Sphere& sphere) const
  222. {
  223. float distSquared = 0;
  224. float temp;
  225. const Vector3& center = sphere.center_;
  226. if (center.x_ < min_.x_)
  227. {
  228. temp = center.x_ - min_.x_;
  229. distSquared += temp * temp;
  230. }
  231. else if (center.x_ > max_.x_)
  232. {
  233. temp = center.x_ - max_.x_;
  234. distSquared += temp * temp;
  235. }
  236. if (center.y_ < min_.y_)
  237. {
  238. temp = center.y_ - min_.y_;
  239. distSquared += temp * temp;
  240. }
  241. else if (center.y_ > max_.y_)
  242. {
  243. temp = center.y_ - max_.y_;
  244. distSquared += temp * temp;
  245. }
  246. if (center.z_ < min_.z_)
  247. {
  248. temp = center.z_ - min_.z_;
  249. distSquared += temp * temp;
  250. }
  251. else if (center.z_ > max_.z_)
  252. {
  253. temp = center.z_ - max_.z_;
  254. distSquared += temp * temp;
  255. }
  256. float radius = sphere.radius_;
  257. if (distSquared >= radius * radius)
  258. return OUTSIDE;
  259. else
  260. return INSIDE;
  261. }
  262. String BoundingBox::ToString() const
  263. {
  264. return min_.ToString() + " - " + max_.ToString();
  265. }
  266. }