Sphere.cpp 6.8 KB

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