Sphere.h 6.3 KB

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