Sphere.cpp 6.8 KB

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