BoundingBox.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 Atomic
  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_)
  92. Swap(min_.x_, max_.x_);
  93. if (min_.y_ > max_.y_)
  94. Swap(min_.y_, max_.y_);
  95. if (min_.z_ > max_.z_)
  96. Swap(min_.z_, max_.z_);
  97. }
  98. void BoundingBox::Transform(const Matrix3& transform)
  99. {
  100. *this = Transformed(Matrix3x4(transform));
  101. }
  102. void BoundingBox::Transform(const Matrix3x4& transform)
  103. {
  104. *this = Transformed(transform);
  105. }
  106. BoundingBox BoundingBox::Transformed(const Matrix3& transform) const
  107. {
  108. return Transformed(Matrix3x4(transform));
  109. }
  110. BoundingBox BoundingBox::Transformed(const Matrix3x4& transform) const
  111. {
  112. Vector3 newCenter = transform * Center();
  113. Vector3 oldEdge = Size() * 0.5f;
  114. Vector3 newEdge = Vector3(
  115. Abs(transform.m00_) * oldEdge.x_ + Abs(transform.m01_) * oldEdge.y_ + Abs(transform.m02_) * oldEdge.z_,
  116. Abs(transform.m10_) * oldEdge.x_ + Abs(transform.m11_) * oldEdge.y_ + Abs(transform.m12_) * oldEdge.z_,
  117. Abs(transform.m20_) * oldEdge.x_ + Abs(transform.m21_) * oldEdge.y_ + Abs(transform.m22_) * oldEdge.z_
  118. );
  119. return BoundingBox(newCenter - newEdge, newCenter + newEdge);
  120. }
  121. Rect BoundingBox::Projected(const Matrix4& projection) const
  122. {
  123. Vector3 projMin = min_;
  124. Vector3 projMax = max_;
  125. if (projMin.z_ < M_MIN_NEARCLIP)
  126. projMin.z_ = M_MIN_NEARCLIP;
  127. if (projMax.z_ < M_MIN_NEARCLIP)
  128. projMax.z_ = M_MIN_NEARCLIP;
  129. Vector3 vertices[8];
  130. vertices[0] = projMin;
  131. vertices[1] = Vector3(projMax.x_, projMin.y_, projMin.z_);
  132. vertices[2] = Vector3(projMin.x_, projMax.y_, projMin.z_);
  133. vertices[3] = Vector3(projMax.x_, projMax.y_, projMin.z_);
  134. vertices[4] = Vector3(projMin.x_, projMin.y_, projMax.z_);
  135. vertices[5] = Vector3(projMax.x_, projMin.y_, projMax.z_);
  136. vertices[6] = Vector3(projMin.x_, projMax.y_, projMax.z_);
  137. vertices[7] = projMax;
  138. Rect rect;
  139. for (unsigned i = 0; i < 8; ++i)
  140. {
  141. Vector3 projected = projection * vertices[i];
  142. rect.Merge(Vector2(projected.x_, projected.y_));
  143. }
  144. return rect;
  145. }
  146. Intersection BoundingBox::IsInside(const Sphere& sphere) const
  147. {
  148. float distSquared = 0;
  149. float temp;
  150. const Vector3& center = sphere.center_;
  151. if (center.x_ < min_.x_)
  152. {
  153. temp = center.x_ - min_.x_;
  154. distSquared += temp * temp;
  155. }
  156. else if (center.x_ > max_.x_)
  157. {
  158. temp = center.x_ - max_.x_;
  159. distSquared += temp * temp;
  160. }
  161. if (center.y_ < min_.y_)
  162. {
  163. temp = center.y_ - min_.y_;
  164. distSquared += temp * temp;
  165. }
  166. else if (center.y_ > max_.y_)
  167. {
  168. temp = center.y_ - max_.y_;
  169. distSquared += temp * temp;
  170. }
  171. if (center.z_ < min_.z_)
  172. {
  173. temp = center.z_ - min_.z_;
  174. distSquared += temp * temp;
  175. }
  176. else if (center.z_ > max_.z_)
  177. {
  178. temp = center.z_ - max_.z_;
  179. distSquared += temp * temp;
  180. }
  181. float radius = sphere.radius_;
  182. if (distSquared >= radius * radius)
  183. return OUTSIDE;
  184. else if (center.x_ - radius < min_.x_ || center.x_ + radius > max_.x_ || center.y_ - radius < min_.y_ ||
  185. center.y_ + radius > max_.y_ || center.z_ - radius < min_.z_ || center.z_ + radius > max_.z_)
  186. return INTERSECTS;
  187. else
  188. return INSIDE;
  189. }
  190. Intersection BoundingBox::IsInsideFast(const Sphere& sphere) const
  191. {
  192. float distSquared = 0;
  193. float temp;
  194. const Vector3& center = sphere.center_;
  195. if (center.x_ < min_.x_)
  196. {
  197. temp = center.x_ - min_.x_;
  198. distSquared += temp * temp;
  199. }
  200. else if (center.x_ > max_.x_)
  201. {
  202. temp = center.x_ - max_.x_;
  203. distSquared += temp * temp;
  204. }
  205. if (center.y_ < min_.y_)
  206. {
  207. temp = center.y_ - min_.y_;
  208. distSquared += temp * temp;
  209. }
  210. else if (center.y_ > max_.y_)
  211. {
  212. temp = center.y_ - max_.y_;
  213. distSquared += temp * temp;
  214. }
  215. if (center.z_ < min_.z_)
  216. {
  217. temp = center.z_ - min_.z_;
  218. distSquared += temp * temp;
  219. }
  220. else if (center.z_ > max_.z_)
  221. {
  222. temp = center.z_ - max_.z_;
  223. distSquared += temp * temp;
  224. }
  225. float radius = sphere.radius_;
  226. if (distSquared >= radius * radius)
  227. return OUTSIDE;
  228. else
  229. return INSIDE;
  230. }
  231. String BoundingBox::ToString() const
  232. {
  233. return min_.ToString() + " - " + max_.ToString();
  234. }
  235. }