BoundingBox.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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. #include "Polyhedron.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. {
  56. Merge(*vertices);
  57. ++vertices;
  58. }
  59. }
  60. void BoundingBox::Merge(const Frustum& frustum)
  61. {
  62. Merge(frustum.vertices_, NUM_FRUSTUM_VERTICES);
  63. }
  64. void BoundingBox::Merge(const Polyhedron& poly)
  65. {
  66. for (unsigned i = 0; i < poly.faces_.Size(); ++i)
  67. {
  68. const PODVector<Vector3>& face = poly.faces_[i];
  69. if (!face.Empty())
  70. Merge(&face[0], face.Size());
  71. }
  72. }
  73. void BoundingBox::Merge(const Sphere& sphere)
  74. {
  75. const Vector3& center = sphere.center_;
  76. float radius = sphere.radius_;
  77. Merge(center + Vector3(radius, radius, radius));
  78. Merge(center + Vector3(-radius, -radius, -radius));
  79. }
  80. void BoundingBox::Clip(const BoundingBox& box)
  81. {
  82. if (box.min_.x_ > min_.x_)
  83. min_.x_ = box.min_.x_;
  84. if (box.max_.x_ < max_.x_)
  85. max_.x_ = box.max_.x_;
  86. if (box.min_.y_ > min_.y_)
  87. min_.y_ = box.min_.y_;
  88. if (box.max_.y_ < max_.y_)
  89. max_.y_ = box.max_.y_;
  90. if (box.min_.z_ > min_.z_)
  91. min_.z_ = box.min_.z_;
  92. if (box.max_.z_ < max_.z_)
  93. max_.z_ = box.max_.z_;
  94. float temp;
  95. if (min_.x_ > max_.x_)
  96. {
  97. temp = min_.x_;
  98. min_.x_ = max_.x_;
  99. max_.x_ = temp;
  100. }
  101. if (min_.y_ > max_.y_)
  102. {
  103. temp = min_.y_;
  104. min_.y_ = max_.y_;
  105. max_.y_ = temp;
  106. }
  107. if (min_.z_ > max_.z_)
  108. {
  109. temp = min_.z_;
  110. min_.z_ = max_.z_;
  111. max_.z_ = temp;
  112. }
  113. }
  114. void BoundingBox::Transform(const Matrix3& transform)
  115. {
  116. Vector3 newCenter = transform * Center();
  117. Vector3 oldEdge = Size() * 0.5f;
  118. Vector3 newEdge = Vector3(
  119. Abs(transform.m00_) * oldEdge.x_ + Abs(transform.m01_) * oldEdge.y_ + Abs(transform.m02_) * oldEdge.z_,
  120. Abs(transform.m10_) * oldEdge.x_ + Abs(transform.m11_) * oldEdge.y_ + Abs(transform.m12_) * oldEdge.z_,
  121. Abs(transform.m20_) * oldEdge.x_ + Abs(transform.m21_) * oldEdge.y_ + Abs(transform.m22_) * oldEdge.z_
  122. );
  123. min_ = newCenter - newEdge;
  124. max_ = newCenter + newEdge;
  125. }
  126. void BoundingBox::Transform(const Matrix3x4& transform)
  127. {
  128. Vector3 newCenter = transform * Center();
  129. Vector3 oldEdge = Size() * 0.5f;
  130. Vector3 newEdge = Vector3(
  131. Abs(transform.m00_) * oldEdge.x_ + Abs(transform.m01_) * oldEdge.y_ + Abs(transform.m02_) * oldEdge.z_,
  132. Abs(transform.m10_) * oldEdge.x_ + Abs(transform.m11_) * oldEdge.y_ + Abs(transform.m12_) * oldEdge.z_,
  133. Abs(transform.m20_) * oldEdge.x_ + Abs(transform.m21_) * oldEdge.y_ + Abs(transform.m22_) * oldEdge.z_
  134. );
  135. min_ = newCenter - newEdge;
  136. max_ = newCenter + newEdge;
  137. }
  138. BoundingBox BoundingBox::Transformed(const Matrix3& transform) const
  139. {
  140. Vector3 newCenter = transform * Center();
  141. Vector3 oldEdge = Size() * 0.5f;
  142. Vector3 newEdge = Vector3(
  143. Abs(transform.m00_) * oldEdge.x_ + Abs(transform.m01_) * oldEdge.y_ + Abs(transform.m02_) * oldEdge.z_,
  144. Abs(transform.m10_) * oldEdge.x_ + Abs(transform.m11_) * oldEdge.y_ + Abs(transform.m12_) * oldEdge.z_,
  145. Abs(transform.m20_) * oldEdge.x_ + Abs(transform.m21_) * oldEdge.y_ + Abs(transform.m22_) * oldEdge.z_
  146. );
  147. return BoundingBox(newCenter - newEdge, newCenter + newEdge);
  148. }
  149. BoundingBox BoundingBox::Transformed(const Matrix3x4& transform) const
  150. {
  151. Vector3 newCenter = transform * Center();
  152. Vector3 oldEdge = Size() * 0.5f;
  153. Vector3 newEdge = Vector3(
  154. Abs(transform.m00_) * oldEdge.x_ + Abs(transform.m01_) * oldEdge.y_ + Abs(transform.m02_) * oldEdge.z_,
  155. Abs(transform.m10_) * oldEdge.x_ + Abs(transform.m11_) * oldEdge.y_ + Abs(transform.m12_) * oldEdge.z_,
  156. Abs(transform.m20_) * oldEdge.x_ + Abs(transform.m21_) * oldEdge.y_ + Abs(transform.m22_) * oldEdge.z_
  157. );
  158. return BoundingBox(newCenter - newEdge, newCenter + newEdge);
  159. }
  160. Rect BoundingBox::Projected(const Matrix4& projection) const
  161. {
  162. Vector3 projMin = min_;
  163. Vector3 projMax = max_;
  164. if (projMin.z_ < M_MIN_NEARCLIP)
  165. projMin.z_ = M_MIN_NEARCLIP;
  166. if (projMax.z_ < M_MIN_NEARCLIP)
  167. projMax.z_ = M_MIN_NEARCLIP;
  168. Vector3 vertices[8];
  169. vertices[0] = projMin;
  170. vertices[1] = Vector3(projMax.x_, projMin.y_, projMin.z_);
  171. vertices[2] = Vector3(projMin.x_, projMax.y_, projMin.z_);
  172. vertices[3] = Vector3(projMax.x_, projMax.y_, projMin.z_);
  173. vertices[4] = Vector3(projMin.x_, projMin.y_, projMax.z_);
  174. vertices[5] = Vector3(projMax.x_, projMin.y_, projMax.z_);
  175. vertices[6] = Vector3(projMin.x_, projMax.y_, projMax.z_);
  176. vertices[7] = projMax;
  177. Rect rect;
  178. for (unsigned i = 0; i < 8; ++i)
  179. {
  180. Vector3 projected = projection * vertices[i];
  181. rect.Merge(Vector2(projected.x_, projected.y_));
  182. }
  183. return rect;
  184. }
  185. Intersection BoundingBox::IsInside(const Sphere& sphere) const
  186. {
  187. float distSquared = 0;
  188. float temp;
  189. const Vector3& center = sphere.center_;
  190. if (center.x_ < min_.x_)
  191. {
  192. temp = center.x_ - min_.x_;
  193. distSquared += temp * temp;
  194. }
  195. else if (center.x_ > max_.x_)
  196. {
  197. temp = center.x_ - max_.x_;
  198. distSquared += temp * temp;
  199. }
  200. if (center.y_ < min_.y_)
  201. {
  202. temp = center.y_ - min_.y_;
  203. distSquared += temp * temp;
  204. }
  205. else if (center.y_ > max_.y_)
  206. {
  207. temp = center.y_ - max_.y_;
  208. distSquared += temp * temp;
  209. }
  210. if (center.z_ < min_.z_)
  211. {
  212. temp = center.z_ - min_.z_;
  213. distSquared += temp * temp;
  214. }
  215. else if (center.z_ > max_.z_)
  216. {
  217. temp = center.z_ - max_.z_;
  218. distSquared += temp * temp;
  219. }
  220. float radius = sphere.radius_;
  221. if (distSquared >= radius * radius)
  222. return OUTSIDE;
  223. else if (center.x_ - radius < min_.x_ || center.x_ + radius > max_.x_ || center.y_ - radius < min_.y_ ||
  224. center.y_ + radius > max_.y_ || center.z_ - radius < min_.z_ || center.z_ + radius > max_.z_)
  225. return INTERSECTS;
  226. else
  227. return INSIDE;
  228. }
  229. Intersection BoundingBox::IsInsideFast(const Sphere& sphere) const
  230. {
  231. float distSquared = 0;
  232. float temp;
  233. const Vector3& center = sphere.center_;
  234. if (center.x_ < min_.x_)
  235. {
  236. temp = center.x_ - min_.x_;
  237. distSquared += temp * temp;
  238. }
  239. else if (center.x_ > max_.x_)
  240. {
  241. temp = center.x_ - max_.x_;
  242. distSquared += temp * temp;
  243. }
  244. if (center.y_ < min_.y_)
  245. {
  246. temp = center.y_ - min_.y_;
  247. distSquared += temp * temp;
  248. }
  249. else if (center.y_ > max_.y_)
  250. {
  251. temp = center.y_ - max_.y_;
  252. distSquared += temp * temp;
  253. }
  254. if (center.z_ < min_.z_)
  255. {
  256. temp = center.z_ - min_.z_;
  257. distSquared += temp * temp;
  258. }
  259. else if (center.z_ > max_.z_)
  260. {
  261. temp = center.z_ - max_.z_;
  262. distSquared += temp * temp;
  263. }
  264. float radius = sphere.radius_;
  265. if (distSquared >= radius * radius)
  266. return OUTSIDE;
  267. else
  268. return INSIDE;
  269. }
  270. }