BoundingBox.cpp 7.4 KB

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