BoundingBox.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 "../Math/Frustum.h"
  23. #include "../Math/Polyhedron.h"
  24. namespace Urho3D
  25. {
  26. void BoundingBox::Define(const Vector3* vertices, unsigned count)
  27. {
  28. if (!count)
  29. return;
  30. defined_ = false;
  31. Merge(vertices, count);
  32. }
  33. void BoundingBox::Define(const Frustum& frustum)
  34. {
  35. Define(frustum.vertices_, NUM_FRUSTUM_VERTICES);
  36. }
  37. void BoundingBox::Define(const Polyhedron& poly)
  38. {
  39. defined_ = false;
  40. Merge(poly);
  41. }
  42. void BoundingBox::Define(const Sphere& sphere)
  43. {
  44. const Vector3& center = sphere.center_;
  45. float radius = sphere.radius_;
  46. min_ = center + Vector3(-radius, -radius, -radius);
  47. max_ = center + Vector3(radius, radius, radius);
  48. defined_ = true;
  49. }
  50. void BoundingBox::Merge(const Vector3* vertices, unsigned count)
  51. {
  52. while (count--)
  53. Merge(*vertices++);
  54. }
  55. void BoundingBox::Merge(const Frustum& frustum)
  56. {
  57. Merge(frustum.vertices_, NUM_FRUSTUM_VERTICES);
  58. }
  59. void BoundingBox::Merge(const Polyhedron& poly)
  60. {
  61. for (unsigned i = 0; i < poly.faces_.Size(); ++i)
  62. {
  63. const PODVector<Vector3>& face = poly.faces_[i];
  64. if (!face.Empty())
  65. Merge(&face[0], face.Size());
  66. }
  67. }
  68. void BoundingBox::Merge(const Sphere& sphere)
  69. {
  70. const Vector3& center = sphere.center_;
  71. float radius = sphere.radius_;
  72. Merge(center + Vector3(radius, radius, radius));
  73. Merge(center + Vector3(-radius, -radius, -radius));
  74. }
  75. void BoundingBox::Clip(const BoundingBox& box)
  76. {
  77. if (box.min_.x_ > min_.x_)
  78. min_.x_ = box.min_.x_;
  79. if (box.max_.x_ < max_.x_)
  80. max_.x_ = box.max_.x_;
  81. if (box.min_.y_ > min_.y_)
  82. min_.y_ = box.min_.y_;
  83. if (box.max_.y_ < max_.y_)
  84. max_.y_ = box.max_.y_;
  85. if (box.min_.z_ > min_.z_)
  86. min_.z_ = box.min_.z_;
  87. if (box.max_.z_ < max_.z_)
  88. max_.z_ = box.max_.z_;
  89. if (min_.x_ > max_.x_)
  90. Swap(min_.x_, max_.x_);
  91. if (min_.y_ > max_.y_)
  92. Swap(min_.y_, max_.y_);
  93. if (min_.z_ > max_.z_)
  94. Swap(min_.z_, max_.z_);
  95. }
  96. void BoundingBox::Transform(const Matrix3& transform)
  97. {
  98. *this = Transformed(Matrix3x4(transform));
  99. }
  100. void BoundingBox::Transform(const Matrix3x4& transform)
  101. {
  102. *this = Transformed(transform);
  103. }
  104. BoundingBox BoundingBox::Transformed(const Matrix3& transform) const
  105. {
  106. return Transformed(Matrix3x4(transform));
  107. }
  108. BoundingBox BoundingBox::Transformed(const Matrix3x4& transform) const
  109. {
  110. Vector3 newCenter = transform * Center();
  111. Vector3 oldEdge = Size() * 0.5f;
  112. Vector3 newEdge = Vector3(
  113. Abs(transform.m00_) * oldEdge.x_ + Abs(transform.m01_) * oldEdge.y_ + Abs(transform.m02_) * oldEdge.z_,
  114. Abs(transform.m10_) * oldEdge.x_ + Abs(transform.m11_) * oldEdge.y_ + Abs(transform.m12_) * oldEdge.z_,
  115. Abs(transform.m20_) * oldEdge.x_ + Abs(transform.m21_) * oldEdge.y_ + Abs(transform.m22_) * oldEdge.z_
  116. );
  117. return BoundingBox(newCenter - newEdge, newCenter + newEdge);
  118. }
  119. Rect BoundingBox::Projected(const Matrix4& projection) const
  120. {
  121. Vector3 projMin = min_;
  122. Vector3 projMax = max_;
  123. if (projMin.z_ < M_MIN_NEARCLIP)
  124. projMin.z_ = M_MIN_NEARCLIP;
  125. if (projMax.z_ < M_MIN_NEARCLIP)
  126. projMax.z_ = M_MIN_NEARCLIP;
  127. Vector3 vertices[8];
  128. vertices[0] = projMin;
  129. vertices[1] = Vector3(projMax.x_, projMin.y_, projMin.z_);
  130. vertices[2] = Vector3(projMin.x_, projMax.y_, projMin.z_);
  131. vertices[3] = Vector3(projMax.x_, projMax.y_, projMin.z_);
  132. vertices[4] = Vector3(projMin.x_, projMin.y_, projMax.z_);
  133. vertices[5] = Vector3(projMax.x_, projMin.y_, projMax.z_);
  134. vertices[6] = Vector3(projMin.x_, projMax.y_, projMax.z_);
  135. vertices[7] = projMax;
  136. Rect rect;
  137. for (unsigned i = 0; i < 8; ++i)
  138. {
  139. Vector3 projected = projection * vertices[i];
  140. rect.Merge(Vector2(projected.x_, projected.y_));
  141. }
  142. return rect;
  143. }
  144. Intersection BoundingBox::IsInside(const Sphere& sphere) const
  145. {
  146. float distSquared = 0;
  147. float temp;
  148. const Vector3& center = sphere.center_;
  149. if (center.x_ < min_.x_)
  150. {
  151. temp = center.x_ - min_.x_;
  152. distSquared += temp * temp;
  153. }
  154. else if (center.x_ > max_.x_)
  155. {
  156. temp = center.x_ - max_.x_;
  157. distSquared += temp * temp;
  158. }
  159. if (center.y_ < min_.y_)
  160. {
  161. temp = center.y_ - min_.y_;
  162. distSquared += temp * temp;
  163. }
  164. else if (center.y_ > max_.y_)
  165. {
  166. temp = center.y_ - max_.y_;
  167. distSquared += temp * temp;
  168. }
  169. if (center.z_ < min_.z_)
  170. {
  171. temp = center.z_ - min_.z_;
  172. distSquared += temp * temp;
  173. }
  174. else if (center.z_ > max_.z_)
  175. {
  176. temp = center.z_ - max_.z_;
  177. distSquared += temp * temp;
  178. }
  179. float radius = sphere.radius_;
  180. if (distSquared >= radius * radius)
  181. return OUTSIDE;
  182. else if (center.x_ - radius < min_.x_ || center.x_ + radius > max_.x_ || center.y_ - radius < min_.y_ ||
  183. center.y_ + radius > max_.y_ || center.z_ - radius < min_.z_ || center.z_ + radius > max_.z_)
  184. return INTERSECTS;
  185. else
  186. return INSIDE;
  187. }
  188. Intersection BoundingBox::IsInsideFast(const Sphere& sphere) const
  189. {
  190. float distSquared = 0;
  191. float temp;
  192. const Vector3& center = sphere.center_;
  193. if (center.x_ < min_.x_)
  194. {
  195. temp = center.x_ - min_.x_;
  196. distSquared += temp * temp;
  197. }
  198. else if (center.x_ > max_.x_)
  199. {
  200. temp = center.x_ - max_.x_;
  201. distSquared += temp * temp;
  202. }
  203. if (center.y_ < min_.y_)
  204. {
  205. temp = center.y_ - min_.y_;
  206. distSquared += temp * temp;
  207. }
  208. else if (center.y_ > max_.y_)
  209. {
  210. temp = center.y_ - max_.y_;
  211. distSquared += temp * temp;
  212. }
  213. if (center.z_ < min_.z_)
  214. {
  215. temp = center.z_ - min_.z_;
  216. distSquared += temp * temp;
  217. }
  218. else if (center.z_ > max_.z_)
  219. {
  220. temp = center.z_ - max_.z_;
  221. distSquared += temp * temp;
  222. }
  223. float radius = sphere.radius_;
  224. if (distSquared >= radius * radius)
  225. return OUTSIDE;
  226. else
  227. return INSIDE;
  228. }
  229. String BoundingBox::ToString() const
  230. {
  231. return min_.ToString() + " - " + max_.ToString();
  232. }
  233. }