Sphere.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __BV_SPHERE_H__
  21. #define __BV_SPHERE_H__
  22. /*
  23. ===============================================================================
  24. Sphere
  25. ===============================================================================
  26. */
  27. class idSphere {
  28. public:
  29. idSphere();
  30. explicit idSphere( const idVec3 &point );
  31. explicit idSphere( const idVec3 &point, const float r );
  32. float operator[]( const int index ) const;
  33. float & operator[]( const int index );
  34. idSphere operator+( const idVec3 &t ) const; // returns tranlated sphere
  35. idSphere & operator+=( const idVec3 &t ); // translate the sphere
  36. idSphere operator+( const idSphere &s ) const;
  37. idSphere & operator+=( const idSphere &s );
  38. bool Compare( const idSphere &a ) const; // exact compare, no epsilon
  39. bool Compare( const idSphere &a, const float epsilon ) const; // compare with epsilon
  40. bool operator==( const idSphere &a ) const; // exact compare, no epsilon
  41. bool operator!=( const idSphere &a ) const; // exact compare, no epsilon
  42. void Clear(); // inside out sphere
  43. void Zero(); // single point at origin
  44. void SetOrigin( const idVec3 &o ); // set origin of sphere
  45. void SetRadius( const float r ); // set square radius
  46. const idVec3 & GetOrigin() const; // returns origin of sphere
  47. float GetRadius() const; // returns sphere radius
  48. bool IsCleared() const; // returns true if sphere is inside out
  49. bool AddPoint( const idVec3 &p ); // add the point, returns true if the sphere expanded
  50. bool AddSphere( const idSphere &s ); // add the sphere, returns true if the sphere expanded
  51. idSphere Expand( const float d ) const; // return bounds expanded in all directions with the given value
  52. idSphere & ExpandSelf( const float d ); // expand bounds in all directions with the given value
  53. idSphere Translate( const idVec3 &translation ) const;
  54. idSphere & TranslateSelf( const idVec3 &translation );
  55. float PlaneDistance( const idPlane &plane ) const;
  56. int PlaneSide( const idPlane &plane, const float epsilon = ON_EPSILON ) const;
  57. bool ContainsPoint( const idVec3 &p ) const; // includes touching
  58. bool IntersectsSphere( const idSphere &s ) const; // includes touching
  59. bool LineIntersection( const idVec3 &start, const idVec3 &end ) const;
  60. // intersection points are (start + dir * scale1) and (start + dir * scale2)
  61. bool RayIntersection( const idVec3 &start, const idVec3 &dir, float &scale1, float &scale2 ) const;
  62. // Tight sphere for a point set.
  63. void FromPoints( const idVec3 *points, const int numPoints );
  64. // Most tight sphere for a translation.
  65. void FromPointTranslation( const idVec3 &point, const idVec3 &translation );
  66. void FromSphereTranslation( const idSphere &sphere, const idVec3 &start, const idVec3 &translation );
  67. // Most tight sphere for a rotation.
  68. void FromPointRotation( const idVec3 &point, const idRotation &rotation );
  69. void FromSphereRotation( const idSphere &sphere, const idVec3 &start, const idRotation &rotation );
  70. void AxisProjection( const idVec3 &dir, float &min, float &max ) const;
  71. private:
  72. idVec3 origin;
  73. float radius;
  74. };
  75. extern idSphere sphere_zero;
  76. ID_INLINE idSphere::idSphere() {
  77. }
  78. ID_INLINE idSphere::idSphere( const idVec3 &point ) {
  79. origin = point;
  80. radius = 0.0f;
  81. }
  82. ID_INLINE idSphere::idSphere( const idVec3 &point, const float r ) {
  83. origin = point;
  84. radius = r;
  85. }
  86. ID_INLINE float idSphere::operator[]( const int index ) const {
  87. return ((float *) &origin)[index];
  88. }
  89. ID_INLINE float &idSphere::operator[]( const int index ) {
  90. return ((float *) &origin)[index];
  91. }
  92. ID_INLINE idSphere idSphere::operator+( const idVec3 &t ) const {
  93. return idSphere( origin + t, radius );
  94. }
  95. ID_INLINE idSphere &idSphere::operator+=( const idVec3 &t ) {
  96. origin += t;
  97. return *this;
  98. }
  99. ID_INLINE bool idSphere::Compare( const idSphere &a ) const {
  100. return ( origin.Compare( a.origin ) && radius == a.radius );
  101. }
  102. ID_INLINE bool idSphere::Compare( const idSphere &a, const float epsilon ) const {
  103. return ( origin.Compare( a.origin, epsilon ) && idMath::Fabs( radius - a.radius ) <= epsilon );
  104. }
  105. ID_INLINE bool idSphere::operator==( const idSphere &a ) const {
  106. return Compare( a );
  107. }
  108. ID_INLINE bool idSphere::operator!=( const idSphere &a ) const {
  109. return !Compare( a );
  110. }
  111. ID_INLINE void idSphere::Clear() {
  112. origin.Zero();
  113. radius = -1.0f;
  114. }
  115. ID_INLINE void idSphere::Zero() {
  116. origin.Zero();
  117. radius = 0.0f;
  118. }
  119. ID_INLINE void idSphere::SetOrigin( const idVec3 &o ) {
  120. origin = o;
  121. }
  122. ID_INLINE void idSphere::SetRadius( const float r ) {
  123. radius = r;
  124. }
  125. ID_INLINE const idVec3 &idSphere::GetOrigin() const {
  126. return origin;
  127. }
  128. ID_INLINE float idSphere::GetRadius() const {
  129. return radius;
  130. }
  131. ID_INLINE bool idSphere::IsCleared() const {
  132. return ( radius < 0.0f );
  133. }
  134. ID_INLINE bool idSphere::AddPoint( const idVec3 &p ) {
  135. if ( radius < 0.0f ) {
  136. origin = p;
  137. radius = 0.0f;
  138. return true;
  139. }
  140. else {
  141. float r = ( p - origin ).LengthSqr();
  142. if ( r > radius * radius ) {
  143. r = idMath::Sqrt( r );
  144. origin += ( p - origin ) * 0.5f * (1.0f - radius / r );
  145. radius += 0.5f * ( r - radius );
  146. return true;
  147. }
  148. return false;
  149. }
  150. }
  151. ID_INLINE bool idSphere::AddSphere( const idSphere &s ) {
  152. if ( radius < 0.0f ) {
  153. origin = s.origin;
  154. radius = s.radius;
  155. return true;
  156. }
  157. else {
  158. float r = ( s.origin - origin ).LengthSqr();
  159. if ( r > ( radius + s.radius ) * ( radius + s.radius ) ) {
  160. r = idMath::Sqrt( r );
  161. origin += ( s.origin - origin ) * 0.5f * (1.0f - radius / ( r + s.radius ) );
  162. radius += 0.5f * ( ( r + s.radius ) - radius );
  163. return true;
  164. }
  165. return false;
  166. }
  167. }
  168. ID_INLINE idSphere idSphere::Expand( const float d ) const {
  169. return idSphere( origin, radius + d );
  170. }
  171. ID_INLINE idSphere &idSphere::ExpandSelf( const float d ) {
  172. radius += d;
  173. return *this;
  174. }
  175. ID_INLINE idSphere idSphere::Translate( const idVec3 &translation ) const {
  176. return idSphere( origin + translation, radius );
  177. }
  178. ID_INLINE idSphere &idSphere::TranslateSelf( const idVec3 &translation ) {
  179. origin += translation;
  180. return *this;
  181. }
  182. ID_INLINE bool idSphere::ContainsPoint( const idVec3 &p ) const {
  183. if ( ( p - origin ).LengthSqr() > radius * radius ) {
  184. return false;
  185. }
  186. return true;
  187. }
  188. ID_INLINE bool idSphere::IntersectsSphere( const idSphere &s ) const {
  189. float r = s.radius + radius;
  190. if ( ( s.origin - origin ).LengthSqr() > r * r ) {
  191. return false;
  192. }
  193. return true;
  194. }
  195. ID_INLINE void idSphere::FromPointTranslation( const idVec3 &point, const idVec3 &translation ) {
  196. origin = point + 0.5f * translation;
  197. radius = idMath::Sqrt( 0.5f * translation.LengthSqr() );
  198. }
  199. ID_INLINE void idSphere::FromSphereTranslation( const idSphere &sphere, const idVec3 &start, const idVec3 &translation ) {
  200. origin = start + sphere.origin + 0.5f * translation;
  201. radius = idMath::Sqrt( 0.5f * translation.LengthSqr() ) + sphere.radius;
  202. }
  203. ID_INLINE void idSphere::FromPointRotation( const idVec3 &point, const idRotation &rotation ) {
  204. idVec3 end = rotation * point;
  205. origin = ( point + end ) * 0.5f;
  206. radius = idMath::Sqrt( 0.5f * ( end - point ).LengthSqr() );
  207. }
  208. ID_INLINE void idSphere::FromSphereRotation( const idSphere &sphere, const idVec3 &start, const idRotation &rotation ) {
  209. idVec3 end = rotation * sphere.origin;
  210. origin = start + ( sphere.origin + end ) * 0.5f;
  211. radius = idMath::Sqrt( 0.5f * ( end - sphere.origin ).LengthSqr() ) + sphere.radius;
  212. }
  213. ID_INLINE void idSphere::AxisProjection( const idVec3 &dir, float &min, float &max ) const {
  214. float d;
  215. d = dir * origin;
  216. min = d - radius;
  217. max = d + radius;
  218. }
  219. #endif /* !__BV_SPHERE_H__ */