Sphere.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //
  2. // Copyright (c) 2008-2017 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. Clear();
  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. Clear();
  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. Clear();
  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 (radius_ < 0.0f)
  93. {
  94. center_ = sphere.center_;
  95. radius_ = sphere.radius_;
  96. return;
  97. }
  98. Vector3 offset = sphere.center_ - center_;
  99. float dist = offset.Length();
  100. // If sphere fits inside, do nothing
  101. if (dist + sphere.radius_ < radius_)
  102. return;
  103. // If we fit inside the other sphere, become it
  104. if (dist + radius_ < sphere.radius_)
  105. {
  106. center_ = sphere.center_;
  107. radius_ = sphere.radius_;
  108. }
  109. else
  110. {
  111. Vector3 NormalizedOffset = offset / dist;
  112. Vector3 min = center_ - radius_ * NormalizedOffset;
  113. Vector3 max = sphere.center_ + sphere.radius_ * NormalizedOffset;
  114. center_ = (min + max) * 0.5f;
  115. radius_ = (max - center_).Length();
  116. }
  117. }
  118. Intersection Sphere::IsInside(const BoundingBox& box) const
  119. {
  120. float radiusSquared = radius_ * radius_;
  121. float distSquared = 0;
  122. float temp;
  123. Vector3 min = box.min_;
  124. Vector3 max = box.max_;
  125. if (center_.x_ < min.x_)
  126. {
  127. temp = center_.x_ - min.x_;
  128. distSquared += temp * temp;
  129. }
  130. else if (center_.x_ > max.x_)
  131. {
  132. temp = center_.x_ - max.x_;
  133. distSquared += temp * temp;
  134. }
  135. if (center_.y_ < min.y_)
  136. {
  137. temp = center_.y_ - min.y_;
  138. distSquared += temp * temp;
  139. }
  140. else if (center_.y_ > max.y_)
  141. {
  142. temp = center_.y_ - max.y_;
  143. distSquared += temp * temp;
  144. }
  145. if (center_.z_ < min.z_)
  146. {
  147. temp = center_.z_ - min.z_;
  148. distSquared += temp * temp;
  149. }
  150. else if (center_.z_ > max.z_)
  151. {
  152. temp = center_.z_ - max.z_;
  153. distSquared += temp * temp;
  154. }
  155. if (distSquared >= radiusSquared)
  156. return OUTSIDE;
  157. min -= center_;
  158. max -= center_;
  159. Vector3 tempVec = min; // - - -
  160. if (tempVec.LengthSquared() >= radiusSquared)
  161. return INTERSECTS;
  162. tempVec.x_ = max.x_; // + - -
  163. if (tempVec.LengthSquared() >= radiusSquared)
  164. return INTERSECTS;
  165. tempVec.y_ = max.y_; // + + -
  166. if (tempVec.LengthSquared() >= radiusSquared)
  167. return INTERSECTS;
  168. tempVec.x_ = min.x_; // - + -
  169. if (tempVec.LengthSquared() >= radiusSquared)
  170. return INTERSECTS;
  171. tempVec.z_ = max.z_; // - + +
  172. if (tempVec.LengthSquared() >= radiusSquared)
  173. return INTERSECTS;
  174. tempVec.y_ = min.y_; // - - +
  175. if (tempVec.LengthSquared() >= radiusSquared)
  176. return INTERSECTS;
  177. tempVec.x_ = max.x_; // + - +
  178. if (tempVec.LengthSquared() >= radiusSquared)
  179. return INTERSECTS;
  180. tempVec.y_ = max.y_; // + + +
  181. if (tempVec.LengthSquared() >= radiusSquared)
  182. return INTERSECTS;
  183. return INSIDE;
  184. }
  185. Intersection Sphere::IsInsideFast(const BoundingBox& box) const
  186. {
  187. float radiusSquared = radius_ * radius_;
  188. float distSquared = 0;
  189. float temp;
  190. Vector3 min = box.min_;
  191. Vector3 max = box.max_;
  192. if (center_.x_ < min.x_)
  193. {
  194. temp = center_.x_ - min.x_;
  195. distSquared += temp * temp;
  196. }
  197. else if (center_.x_ > max.x_)
  198. {
  199. temp = center_.x_ - max.x_;
  200. distSquared += temp * temp;
  201. }
  202. if (center_.y_ < min.y_)
  203. {
  204. temp = center_.y_ - min.y_;
  205. distSquared += temp * temp;
  206. }
  207. else if (center_.y_ > max.y_)
  208. {
  209. temp = center_.y_ - max.y_;
  210. distSquared += temp * temp;
  211. }
  212. if (center_.z_ < min.z_)
  213. {
  214. temp = center_.z_ - min.z_;
  215. distSquared += temp * temp;
  216. }
  217. else if (center_.z_ > max.z_)
  218. {
  219. temp = center_.z_ - max.z_;
  220. distSquared += temp * temp;
  221. }
  222. if (distSquared >= radiusSquared)
  223. return OUTSIDE;
  224. else
  225. return INSIDE;
  226. }
  227. Vector3 Sphere::GetLocalPoint(float theta, float phi) const
  228. {
  229. return Vector3(
  230. radius_ * Sin(theta) * Sin(phi),
  231. radius_ * Cos(phi),
  232. radius_ * Cos(theta) * Sin(phi)
  233. );
  234. }
  235. }