Sphere.h 6.3 KB

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