Sphere.h 6.3 KB

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