Sphere.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #pragma once
  24. #include "Vector3.h"
  25. class BoundingBox;
  26. class Polyhedron;
  27. class Frustum;
  28. /// %Sphere in three-dimensional space.
  29. class Sphere
  30. {
  31. public:
  32. /// Construct undefined.
  33. Sphere() :
  34. center_(Vector3::ZERO),
  35. radius_(0.0f),
  36. defined_(false)
  37. {
  38. }
  39. /// Copy-construct from another sphere.
  40. Sphere(const Sphere& sphere) :
  41. center_(sphere.center_),
  42. radius_(sphere.radius_),
  43. defined_(sphere.defined_)
  44. {
  45. }
  46. /// Construct from center and radius.
  47. Sphere(const Vector3& center, float radius) :
  48. center_(center),
  49. radius_(radius),
  50. defined_(true)
  51. {
  52. }
  53. /// Construct from an array of vertices.
  54. Sphere(const Vector3* vertices, unsigned count) :
  55. defined_(false)
  56. {
  57. Define(vertices, count);
  58. }
  59. /// Construct from a bounding box.
  60. Sphere(const BoundingBox& box) :
  61. defined_(false)
  62. {
  63. Define(box);
  64. }
  65. /// Construct from a frustum.
  66. Sphere(const Frustum& frustum) :
  67. defined_(false)
  68. {
  69. Define(frustum);
  70. }
  71. /// Construct from a polyhedron.
  72. Sphere(const Polyhedron& poly) :
  73. defined_(false)
  74. {
  75. Define(poly);
  76. }
  77. /// Assign from another sphere.
  78. Sphere& operator = (const Sphere& rhs)
  79. {
  80. center_ = rhs.center_;
  81. radius_ = rhs.radius_;
  82. defined_ = rhs.defined_;
  83. return *this;
  84. }
  85. /// Test for equality with another sphere.
  86. bool operator == (const Sphere& rhs) const { return center_ == rhs.center_ && radius_ == rhs.radius_; }
  87. /// Test for inequality with another sphere.
  88. bool operator != (const Sphere& rhs) const { return center_ != rhs.center_ || radius_ != rhs.radius_; }
  89. /// Define from another sphere.
  90. void Define(const Sphere& sphere)
  91. {
  92. Define(sphere.center_, sphere.radius_);
  93. }
  94. /// Define from center and radius.
  95. void Define(const Vector3& center, float radius)
  96. {
  97. center_ = center;
  98. radius_ = radius;
  99. defined_ = true;
  100. }
  101. /// Define from an array of vertices.
  102. void Define(const Vector3* vertices, unsigned count);
  103. /// Define from a bounding box.
  104. void Define(const BoundingBox& box);
  105. /// Define from a frustum.
  106. void Define(const Frustum& frustum);
  107. /// Define from a polyhedron.
  108. void Define(const Polyhedron& poly);
  109. /// Merge a point.
  110. void Merge(const Vector3& point)
  111. {
  112. if (!defined_)
  113. {
  114. center_ = point;
  115. radius_ = 0.0f;
  116. defined_ = true;
  117. return;
  118. }
  119. Vector3 offset = point - center_;
  120. float dist = offset.Length();
  121. if (dist > radius_)
  122. {
  123. float half = (dist - radius_) * 0.5f;
  124. radius_ += half;
  125. center_ += (half / dist) * offset;
  126. }
  127. }
  128. /// Merge an array of vertices.
  129. void Merge(const Vector3* vertices, unsigned count);
  130. /// Merge a bounding box.
  131. void Merge(const BoundingBox& box);
  132. /// Merge a frustum.
  133. void Merge(const Frustum& frustum);
  134. /// Merge a polyhedron.
  135. void Merge(const Polyhedron& poly);
  136. /// Merge a sphere.
  137. void Merge(const Sphere& sphere);
  138. /// Clear to undefined state.
  139. void Clear()
  140. {
  141. center_ = Vector3::ZERO;
  142. radius_ = 0.0f;
  143. defined_ = false;
  144. }
  145. /// Test if a point is inside.
  146. Intersection IsInside(const Vector3& point) const
  147. {
  148. float distSquared = (point - center_).LengthSquared();
  149. if (distSquared < radius_ * radius_)
  150. return INSIDE;
  151. else
  152. return OUTSIDE;
  153. }
  154. /// Test if another sphere is inside, outside or intersects.
  155. Intersection IsInside(const Sphere& sphere) const
  156. {
  157. float dist = (sphere.center_ - center_).Length();
  158. if (dist >= sphere.radius_ + radius_)
  159. return OUTSIDE;
  160. else if (dist + sphere.radius_ < radius_)
  161. return INSIDE;
  162. else
  163. return INTERSECTS;
  164. }
  165. /// Test if another sphere is (partially) inside or outside.
  166. Intersection IsInsideFast(const Sphere& sphere) const
  167. {
  168. float distSquared = (sphere.center_ - center_).LengthSquared();
  169. float combined = sphere.radius_ + radius_;
  170. if (distSquared >= combined * combined)
  171. return OUTSIDE;
  172. else
  173. return INSIDE;
  174. }
  175. /// Test if a bounding box is inside, outside or intersects.
  176. Intersection IsInside(const BoundingBox& box) const;
  177. /// Test if a bounding box is (partially) inside or outside.
  178. Intersection IsInsideFast(const BoundingBox& box) const;
  179. /// Return distance of a point to the surface, or 0 if inside.
  180. float Distance(const Vector3& point) const { return Max((point - center_).Length() - radius_, 0.0f); }
  181. /// Return distance of a point to the surface, or 0 if inside, using fast square root.
  182. float DistanceFast(const Vector3& point) const { return Max((point - center_).LengthFast() - radius_, 0.0f); }
  183. /// Sphere center.
  184. Vector3 center_;
  185. /// Sphere radius.
  186. float radius_;
  187. /// Defined flag.
  188. bool defined_;
  189. };