Sphere.h 5.4 KB

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