Sphere.h 6.2 KB

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