Sphere.cpp 6.8 KB

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