Sphere.h 6.3 KB

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