BoundingBox.cpp 9.0 KB

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