Sphere.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 Sphere::Define(const Vector3* vertices, unsigned count)
  29. {
  30. if (!count)
  31. return;
  32. defined_ = false;
  33. Merge(vertices, count);
  34. }
  35. void Sphere::Define(const BoundingBox& box)
  36. {
  37. const Vector3& min = box.min_;
  38. const Vector3& max = box.max_;
  39. defined_ = false;
  40. Merge(min);
  41. Merge(Vector3(max.x_, min.y_, min.z_));
  42. Merge(Vector3(min.x_, max.y_, min.z_));
  43. Merge(Vector3(max.x_, max.y_, min.z_));
  44. Merge(Vector3(min.x_, min.y_, max.z_));
  45. Merge(Vector3(max.x_, min.y_, max.z_));
  46. Merge(Vector3(min.x_, max.y_, max.z_));
  47. Merge(max);
  48. }
  49. void Sphere::Define(const Frustum& frustum)
  50. {
  51. Define(frustum.vertices_, NUM_FRUSTUM_VERTICES);
  52. }
  53. void Sphere::Define(const Polyhedron& poly)
  54. {
  55. defined_ = false;
  56. Merge(poly);
  57. }
  58. void Sphere::Merge(const Vector3* vertices, unsigned count)
  59. {
  60. while (count--)
  61. {
  62. Merge(*vertices);
  63. ++vertices;
  64. }
  65. }
  66. void Sphere::Merge(const BoundingBox& box)
  67. {
  68. const Vector3& min = box.min_;
  69. const Vector3& max = box.max_;
  70. Merge(min);
  71. Merge(Vector3(max.x_, min.y_, min.z_));
  72. Merge(Vector3(min.x_, max.y_, min.z_));
  73. Merge(Vector3(max.x_, max.y_, min.z_));
  74. Merge(Vector3(min.x_, min.y_, max.z_));
  75. Merge(Vector3(max.x_, min.y_, max.z_));
  76. Merge(Vector3(min.x_, max.y_, max.z_));
  77. Merge(max);
  78. }
  79. void Sphere::Merge(const Frustum& frustum)
  80. {
  81. const Vector3* vertices = frustum.vertices_;
  82. Merge(vertices, NUM_FRUSTUM_VERTICES);
  83. }
  84. void Sphere::Merge(const Polyhedron& poly)
  85. {
  86. for (unsigned i = 0; i < poly.faces_.Size(); ++i)
  87. {
  88. const PODVector<Vector3>& face = poly.faces_[i];
  89. if (!face.Empty())
  90. Merge(&face[0], face.Size());
  91. }
  92. }
  93. void Sphere::Merge(const Sphere& sphere)
  94. {
  95. if (!defined_)
  96. {
  97. center_ = sphere.center_;
  98. radius_ = sphere.radius_;
  99. defined_ = true;
  100. return;
  101. }
  102. Vector3 offset = sphere.center_ - center_;
  103. float dist = offset.Length();
  104. // If sphere fits inside, do nothing
  105. if (dist + sphere.radius_ < radius_)
  106. return;
  107. // If we fit inside the other sphere, become it
  108. if (dist + radius_ < sphere.radius_)
  109. {
  110. center_ = sphere.center_;
  111. radius_ = sphere.radius_;
  112. }
  113. else
  114. {
  115. Vector3 NormalizedOffset = offset / dist;
  116. Vector3 min = center_ - radius_ * NormalizedOffset;
  117. Vector3 max = sphere.center_ + sphere.radius_ * NormalizedOffset;
  118. center_ = (min + max) * 0.5f;
  119. radius_ = (max - center_).Length();
  120. }
  121. }
  122. Intersection Sphere::IsInside(const BoundingBox& box) const
  123. {
  124. float radiusSquared = radius_ * radius_;
  125. float distSquared = 0;
  126. float temp;
  127. Vector3 min = box.min_;
  128. Vector3 max = box.max_;
  129. if (center_.x_ < min.x_)
  130. {
  131. temp = center_.x_ - min.x_;
  132. distSquared += temp * temp;
  133. }
  134. else if (center_.x_ > max.x_)
  135. {
  136. temp = center_.x_ - max.x_;
  137. distSquared += temp * temp;
  138. }
  139. if (center_.y_ < min.y_)
  140. {
  141. temp = center_.y_ - min.y_;
  142. distSquared += temp * temp;
  143. }
  144. else if (center_.y_ > max.y_)
  145. {
  146. temp = center_.y_ - max.y_;
  147. distSquared += temp * temp;
  148. }
  149. if (center_.z_ < min.z_)
  150. {
  151. temp = center_.z_ - min.z_;
  152. distSquared += temp * temp;
  153. }
  154. else if (center_.z_ > max.z_)
  155. {
  156. temp = center_.z_ - max.z_;
  157. distSquared += temp * temp;
  158. }
  159. if (distSquared >= radiusSquared)
  160. return OUTSIDE;
  161. min -= center_;
  162. max -= center_;
  163. Vector3 tempVec = min; // - - -
  164. if (tempVec.LengthSquared() >= radiusSquared)
  165. return INTERSECTS;
  166. tempVec.x_ = max.x_; // + - -
  167. if (tempVec.LengthSquared() >= radiusSquared)
  168. return INTERSECTS;
  169. tempVec.y_ = max.y_; // + + -
  170. if (tempVec.LengthSquared() >= radiusSquared)
  171. return INTERSECTS;
  172. tempVec.x_ = min.x_; // - + -
  173. if (tempVec.LengthSquared() >= radiusSquared)
  174. return INTERSECTS;
  175. tempVec.z_ = max.z_; // - + +
  176. if (tempVec.LengthSquared() >= radiusSquared)
  177. return INTERSECTS;
  178. tempVec.y_ = min.y_; // - - +
  179. if (tempVec.LengthSquared() >= radiusSquared)
  180. return INTERSECTS;
  181. tempVec.x_ = max.x_; // + - +
  182. if (tempVec.LengthSquared() >= radiusSquared)
  183. return INTERSECTS;
  184. tempVec.y_ = max.y_; // + + +
  185. if (tempVec.LengthSquared() >= radiusSquared)
  186. return INTERSECTS;
  187. return INSIDE;
  188. }
  189. Intersection Sphere::IsInsideFast(const BoundingBox& box) const
  190. {
  191. float radiusSquared = radius_ * radius_;
  192. float distSquared = 0;
  193. float temp;
  194. Vector3 min = box.min_;
  195. Vector3 max = box.max_;
  196. if (center_.x_ < min.x_)
  197. {
  198. temp = center_.x_ - min.x_;
  199. distSquared += temp * temp;
  200. }
  201. else if (center_.x_ > max.x_)
  202. {
  203. temp = center_.x_ - max.x_;
  204. distSquared += temp * temp;
  205. }
  206. if (center_.y_ < min.y_)
  207. {
  208. temp = center_.y_ - min.y_;
  209. distSquared += temp * temp;
  210. }
  211. else if (center_.y_ > max.y_)
  212. {
  213. temp = center_.y_ - max.y_;
  214. distSquared += temp * temp;
  215. }
  216. if (center_.z_ < min.z_)
  217. {
  218. temp = center_.z_ - min.z_;
  219. distSquared += temp * temp;
  220. }
  221. else if (center_.z_ > max.z_)
  222. {
  223. temp = center_.z_ - max.z_;
  224. distSquared += temp * temp;
  225. }
  226. if (distSquared >= radiusSquared)
  227. return OUTSIDE;
  228. else
  229. return INSIDE;
  230. }
  231. }