BoundingBox.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Frustum.h"
  25. void BoundingBox::Define(const Vector3* vertices, unsigned count)
  26. {
  27. if (!count)
  28. return;
  29. defined_ = false;
  30. Merge(vertices, count);
  31. }
  32. void BoundingBox::Define(const Frustum& frustum)
  33. {
  34. Define(frustum.vertices_, NUM_FRUSTUM_VERTICES);
  35. }
  36. void BoundingBox::Define(const Sphere& sphere)
  37. {
  38. const Vector3& center = sphere.center_;
  39. float radius = sphere.radius_;
  40. min_ = center + Vector3(-radius, -radius, -radius);
  41. max_ = center + Vector3(radius, radius, radius);
  42. defined_ = true;
  43. }
  44. void BoundingBox::Merge(const Vector3* vertices, unsigned count)
  45. {
  46. while (count--)
  47. {
  48. Merge(*vertices);
  49. ++vertices;
  50. }
  51. }
  52. void BoundingBox::Merge(const Frustum& frustum)
  53. {
  54. Merge(frustum.vertices_, NUM_FRUSTUM_VERTICES);
  55. }
  56. void BoundingBox::Merge(const Sphere& sphere)
  57. {
  58. const Vector3& center = sphere.center_;
  59. float radius = sphere.radius_;
  60. Merge(center + Vector3(radius, radius, radius));
  61. Merge(center + Vector3(-radius, -radius, -radius));
  62. }
  63. void BoundingBox::Intersect(const BoundingBox& box)
  64. {
  65. if (box.min_.x_ > min_.x_)
  66. min_.x_ = box.min_.x_;
  67. if (box.max_.x_ < max_.x_)
  68. max_.x_ = box.max_.x_;
  69. if (box.min_.y_ > min_.y_)
  70. min_.y_ = box.min_.y_;
  71. if (box.max_.y_ < max_.y_)
  72. max_.y_ = box.max_.y_;
  73. if (box.min_.z_ > min_.z_)
  74. min_.z_ = box.min_.z_;
  75. if (box.max_.z_ < max_.z_)
  76. max_.z_ = box.max_.z_;
  77. float temp;
  78. if (min_.x_ > max_.x_)
  79. {
  80. temp = min_.x_;
  81. min_.x_ = max_.x_;
  82. max_.x_ = temp;
  83. }
  84. if (min_.y_ > max_.y_)
  85. {
  86. temp = min_.y_;
  87. min_.y_ = max_.y_;
  88. max_.y_ = temp;
  89. }
  90. if (min_.z_ > max_.z_)
  91. {
  92. temp = min_.z_;
  93. min_.z_ = max_.z_;
  94. max_.z_ = temp;
  95. }
  96. }
  97. void BoundingBox::Transform(const Matrix3& transform)
  98. {
  99. Vector3 newCenter = transform * Center();
  100. Vector3 oldEdge = Size() * 0.5;
  101. Vector3 newEdge = Vector3(
  102. fabsf(transform.m00_) * oldEdge.x_ + fabsf(transform.m01_) * oldEdge.y_ + fabsf(transform.m02_) * oldEdge.z_,
  103. fabsf(transform.m10_) * oldEdge.x_ + fabsf(transform.m11_) * oldEdge.y_ + fabsf(transform.m12_) * oldEdge.z_,
  104. fabsf(transform.m20_) * oldEdge.x_ + fabsf(transform.m21_) * oldEdge.y_ + fabsf(transform.m22_) * oldEdge.z_
  105. );
  106. min_ = newCenter - newEdge;
  107. max_ = newCenter + newEdge;
  108. }
  109. void BoundingBox::Transform(const Matrix3x4& transform)
  110. {
  111. Vector3 newCenter = transform * Center();
  112. Vector3 oldEdge = Size() * 0.5;
  113. Vector3 newEdge = Vector3(
  114. fabsf(transform.m00_) * oldEdge.x_ + fabsf(transform.m01_) * oldEdge.y_ + fabsf(transform.m02_) * oldEdge.z_,
  115. fabsf(transform.m10_) * oldEdge.x_ + fabsf(transform.m11_) * oldEdge.y_ + fabsf(transform.m12_) * oldEdge.z_,
  116. fabsf(transform.m20_) * oldEdge.x_ + fabsf(transform.m21_) * oldEdge.y_ + fabsf(transform.m22_) * oldEdge.z_
  117. );
  118. min_ = newCenter - newEdge;
  119. max_ = newCenter + newEdge;
  120. }
  121. BoundingBox BoundingBox::Transformed(const Matrix3& transform) const
  122. {
  123. Vector3 newCenter = transform * Center();
  124. Vector3 oldEdge = Size() * 0.5;
  125. Vector3 newEdge = Vector3(
  126. fabsf(transform.m00_) * oldEdge.x_ + fabsf(transform.m01_) * oldEdge.y_ + fabsf(transform.m02_) * oldEdge.z_,
  127. fabsf(transform.m10_) * oldEdge.x_ + fabsf(transform.m11_) * oldEdge.y_ + fabsf(transform.m12_) * oldEdge.z_,
  128. fabsf(transform.m20_) * oldEdge.x_ + fabsf(transform.m21_) * oldEdge.y_ + fabsf(transform.m22_) * oldEdge.z_
  129. );
  130. return BoundingBox(newCenter - newEdge, newCenter + newEdge);
  131. }
  132. BoundingBox BoundingBox::Transformed(const Matrix3x4& transform) const
  133. {
  134. Vector3 newCenter = transform * Center();
  135. Vector3 oldEdge = Size() * 0.5f;
  136. Vector3 newEdge = Vector3(
  137. fabsf(transform.m00_) * oldEdge.x_ + fabsf(transform.m01_) * oldEdge.y_ + fabsf(transform.m02_) * oldEdge.z_,
  138. fabsf(transform.m10_) * oldEdge.x_ + fabsf(transform.m11_) * oldEdge.y_ + fabsf(transform.m12_) * oldEdge.z_,
  139. fabsf(transform.m20_) * oldEdge.x_ + fabsf(transform.m21_) * oldEdge.y_ + fabsf(transform.m22_) * oldEdge.z_
  140. );
  141. return BoundingBox(newCenter - newEdge, newCenter + newEdge);
  142. }
  143. Rect BoundingBox::Projected(const Matrix4& projection) const
  144. {
  145. Vector3 projMin = min_;
  146. Vector3 projMax = max_;
  147. if (projMin.z_ < M_MIN_NEARCLIP)
  148. projMin.z_ = M_MIN_NEARCLIP;
  149. if (projMax.z_ < M_MIN_NEARCLIP)
  150. projMax.z_ = M_MIN_NEARCLIP;
  151. Vector3 vertices[8];
  152. vertices[0] = projMin;
  153. vertices[1] = Vector3(projMax.x_, projMin.y_, projMin.z_);
  154. vertices[2] = Vector3(projMin.x_, projMax.y_, projMin.z_);
  155. vertices[3] = Vector3(projMax.x_, projMax.y_, projMin.z_);
  156. vertices[4] = Vector3(projMin.x_, projMin.y_, projMax.z_);
  157. vertices[5] = Vector3(projMax.x_, projMin.y_, projMax.z_);
  158. vertices[6] = Vector3(projMin.x_, projMax.y_, projMax.z_);
  159. vertices[7] = projMax;
  160. Rect rect;
  161. for (unsigned i = 0; i < 8; ++i)
  162. {
  163. Vector3 projected = projection * vertices[i];
  164. rect.Merge(Vector2(projected.x_, projected.y_));
  165. }
  166. return rect;
  167. }
  168. Intersection BoundingBox::IsInside(const Sphere& sphere) const
  169. {
  170. float distSquared = 0;
  171. float temp;
  172. const Vector3& center = sphere.center_;
  173. if (center.x_ < min_.x_)
  174. {
  175. temp = center.x_ - min_.x_;
  176. distSquared += temp * temp;
  177. }
  178. else if (center.x_ > max_.x_)
  179. {
  180. temp = center.x_ - max_.x_;
  181. distSquared += temp * temp;
  182. }
  183. if (center.y_ < min_.y_)
  184. {
  185. temp = center.y_ - min_.y_;
  186. distSquared += temp * temp;
  187. }
  188. else if (center.y_ > max_.y_)
  189. {
  190. temp = center.y_ - max_.y_;
  191. distSquared += temp * temp;
  192. }
  193. if (center.z_ < min_.z_)
  194. {
  195. temp = center.z_ - min_.z_;
  196. distSquared += temp * temp;
  197. }
  198. else if (center.z_ > max_.z_)
  199. {
  200. temp = center.z_ - max_.z_;
  201. distSquared += temp * temp;
  202. }
  203. float radius = sphere.radius_;
  204. if (distSquared >= radius * radius)
  205. return OUTSIDE;
  206. else if (center.x_ - radius < min_.x_ || center.x_ + radius > max_.x_ || center.y_ - radius < min_.y_ ||
  207. center.y_ + radius > max_.y_ || center.z_ - radius < min_.z_ || center.z_ + radius > max_.z_)
  208. return INTERSECTS;
  209. else
  210. return INSIDE;
  211. }
  212. Intersection BoundingBox::IsInsideFast(const Sphere& sphere) const
  213. {
  214. float distSquared = 0;
  215. float temp;
  216. const Vector3& center = sphere.center_;
  217. if (center.x_ < min_.x_)
  218. {
  219. temp = center.x_ - min_.x_;
  220. distSquared += temp * temp;
  221. }
  222. else if (center.x_ > max_.x_)
  223. {
  224. temp = center.x_ - max_.x_;
  225. distSquared += temp * temp;
  226. }
  227. if (center.y_ < min_.y_)
  228. {
  229. temp = center.y_ - min_.y_;
  230. distSquared += temp * temp;
  231. }
  232. else if (center.y_ > max_.y_)
  233. {
  234. temp = center.y_ - max_.y_;
  235. distSquared += temp * temp;
  236. }
  237. if (center.z_ < min_.z_)
  238. {
  239. temp = center.z_ - min_.z_;
  240. distSquared += temp * temp;
  241. }
  242. else if (center.z_ > max_.z_)
  243. {
  244. temp = center.z_ - max_.z_;
  245. distSquared += temp * temp;
  246. }
  247. float radius = sphere.radius_;
  248. if (distSquared >= radius * radius)
  249. return OUTSIDE;
  250. else
  251. return INSIDE;
  252. }