Sphere.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 an array of vertices
  71. void Define(const Vector3* vertices, unsigned count);
  72. /// Define from a boundingBox
  73. void Define(const BoundingBox& box);
  74. /// Define from a frustum
  75. void Define(const Frustum& frustum);
  76. /// Merge a point
  77. void Merge(const Vector3& point)
  78. {
  79. if (!defined_)
  80. {
  81. center_ = point;
  82. radius_ = 0.0f;
  83. defined_ = true;
  84. return;
  85. }
  86. Vector3 offset = point - center_;
  87. float dist = offset.Length();
  88. if (dist > radius_)
  89. {
  90. float half = (dist - radius_) * 0.5f;
  91. radius_ += half;
  92. center_ += (half / dist) * offset;
  93. }
  94. }
  95. /// Merge an array of vertices
  96. void Merge(const Vector3* vertices, unsigned count);
  97. /// Merge a bounding box
  98. void Merge(const BoundingBox& box);
  99. /// Merge a frustum
  100. void Merge(const Frustum& frustum);
  101. /// Merge a sphere
  102. void Merge(const Sphere& sphere);
  103. /// Test if a point is inside
  104. Intersection IsInside(const Vector3& point) const
  105. {
  106. float distSquared = (point - center_).LengthSquared();
  107. if (distSquared < radius_ * radius_)
  108. return INSIDE;
  109. else
  110. return OUTSIDE;
  111. }
  112. /// Test if another sphere is inside, outside or intersects
  113. Intersection IsInside(const Sphere& sphere) const
  114. {
  115. float dist = (sphere.center_ - center_).Length();
  116. if (dist >= sphere.radius_ + radius_)
  117. return OUTSIDE;
  118. if (dist + sphere.radius_ < radius_)
  119. return INSIDE;
  120. return INTERSECTS;
  121. }
  122. /// Test if another sphere is (partially) inside or outside
  123. Intersection IsInsideFast(const Sphere& sphere) const
  124. {
  125. float distSquared = (sphere.center_ - center_).LengthSquared();
  126. float combined = sphere.radius_ + radius_;
  127. if (distSquared >= combined * combined)
  128. return OUTSIDE;
  129. else
  130. return INSIDE;
  131. }
  132. /// Test if a bounding box is inside, outside or intersects
  133. Intersection IsInside(const BoundingBox& box) const;
  134. /// Test if a bounding box is (partially) inside or outside
  135. Intersection IsInsideFast(const BoundingBox& box) const;
  136. /// Return distance to a ray, or infinity if no intersection
  137. float Distance(const Ray& ray) const;
  138. /// Sphere center
  139. Vector3 center_;
  140. /// Sphere radius
  141. float radius_;
  142. /// Defined flag
  143. bool defined_;
  144. };