Sphere.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  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. class BoundingBox;
  26. class Frustum;
  27. class Ray;
  28. /// Sphere in three-dimensional space
  29. class Sphere
  30. {
  31. public:
  32. /// Construct an undefined sphere
  33. Sphere() :
  34. defined_(false)
  35. {
  36. }
  37. /// Copy-construct from another sphere
  38. Sphere(const Sphere& sphere) :
  39. center_(sphere.center_),
  40. radius_(sphere.radius_),
  41. defined_(sphere.defined_)
  42. {
  43. }
  44. /// Construct from center and radius
  45. Sphere(const Vector3& center, float radius) :
  46. center_(center),
  47. radius_(radius),
  48. defined_(true)
  49. {
  50. }
  51. /// Assign from another sphere
  52. Sphere& operator = (const Sphere& rhs)
  53. {
  54. center_ = rhs.center_;
  55. radius_ = rhs.radius_;
  56. defined_ = rhs.defined_;
  57. return *this;
  58. }
  59. /// Test for equality with another sphere
  60. bool operator == (const Sphere& rhs) const { return (center_ == rhs.center_) && (radius_ == rhs.radius_); }
  61. /// Test for inequality with another sphere
  62. bool operator != (const Sphere& rhs) const { return (center_ != rhs.center_) || (radius_ != rhs.radius_); }
  63. /// Define from center and radius
  64. void Define(const Vector3& center, float radius)
  65. {
  66. center_ = center;
  67. radius_ = radius;
  68. defined_ = true;
  69. }
  70. /// Define from a vector of vertices
  71. void Define(const std::vector<Vector3>& vertices);
  72. /// Define from an array of vertices
  73. void Define(const Vector3* vertices, unsigned count);
  74. /// Define from a boundingBox
  75. void Define(const BoundingBox& box);
  76. /// Define from a frustum
  77. void Define(const Frustum& frustum);
  78. /// Merge a point
  79. void Merge(const Vector3& point)
  80. {
  81. if (!defined_)
  82. {
  83. center_ = point;
  84. radius_ = 0.0f;
  85. defined_ = true;
  86. return;
  87. }
  88. Vector3 offset = point - center_;
  89. float dist = offset.GetLength();
  90. if (dist > radius_)
  91. {
  92. float half = (dist - radius_) * 0.5f;
  93. radius_ += half;
  94. center_ += (half / dist) * offset;
  95. }
  96. }
  97. /// Merge a vector of vertices
  98. void Merge(const std::vector<Vector3>& vertices);
  99. /// Merge an array of vertices
  100. void Merge(const Vector3* vertices, unsigned count);
  101. /// Merge a bounding box
  102. void Merge(const BoundingBox& box);
  103. /// Merge a frustum
  104. void Merge(const Frustum& frustum);
  105. /// Merge a sphere
  106. void Merge(const Sphere& sphere);
  107. /// Test if a point is inside
  108. Intersection IsInside(const Vector3& point) const
  109. {
  110. float distSquared = (point - center_).GetLengthSquared();
  111. if (distSquared < radius_ * radius_)
  112. return INSIDE;
  113. else
  114. return OUTSIDE;
  115. }
  116. /// Test if another sphere is inside, outside or intersects
  117. Intersection IsInside(const Sphere& sphere) const
  118. {
  119. float dist = (sphere.center_ - center_).GetLength();
  120. if (dist >= sphere.radius_ + radius_)
  121. return OUTSIDE;
  122. if (dist + sphere.radius_ < radius_)
  123. return INSIDE;
  124. return INTERSECTS;
  125. }
  126. /// Test if another sphere is (partially) inside or outside
  127. Intersection IsInsideFast(const Sphere& sphere) const
  128. {
  129. float distSquared = (sphere.center_ - center_).GetLengthSquared();
  130. float combined = sphere.radius_ + radius_;
  131. if (distSquared >= combined * combined)
  132. return OUTSIDE;
  133. else
  134. return INSIDE;
  135. }
  136. /// Test if a bounding box is inside, outside or intersects
  137. Intersection IsInside(const BoundingBox& box) const;
  138. /// Test if a bounding box is (partially) inside or outside
  139. Intersection IsInsideFast(const BoundingBox& box) const;
  140. /// Return distance to a ray, or infinity if no intersection
  141. float GetDistance(const Ray& ray) const;
  142. /// Sphere center
  143. Vector3 center_;
  144. /// Sphere radius
  145. float radius_;
  146. /// Defined flag
  147. bool defined_;
  148. };