Sphere.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #pragma hdrstop
  21. #include "../precompiled.h"
  22. idSphere sphere_zero( vec3_zero, 0.0f );
  23. /*
  24. ================
  25. idSphere::PlaneDistance
  26. ================
  27. */
  28. float idSphere::PlaneDistance( const idPlane &plane ) const {
  29. float d;
  30. d = plane.Distance( origin );
  31. if ( d > radius ) {
  32. return d - radius;
  33. }
  34. if ( d < -radius ) {
  35. return d + radius;
  36. }
  37. return 0.0f;
  38. }
  39. /*
  40. ================
  41. idSphere::PlaneSide
  42. ================
  43. */
  44. int idSphere::PlaneSide( const idPlane &plane, const float epsilon ) const {
  45. float d;
  46. d = plane.Distance( origin );
  47. if ( d > radius + epsilon ) {
  48. return PLANESIDE_FRONT;
  49. }
  50. if ( d < -radius - epsilon ) {
  51. return PLANESIDE_BACK;
  52. }
  53. return PLANESIDE_CROSS;
  54. }
  55. /*
  56. ============
  57. idSphere::LineIntersection
  58. Returns true if the line intersects the sphere between the start and end point.
  59. ============
  60. */
  61. bool idSphere::LineIntersection( const idVec3 &start, const idVec3 &end ) const {
  62. idVec3 r, s, e;
  63. float a;
  64. s = start - origin;
  65. e = end - origin;
  66. r = e - s;
  67. a = -s * r;
  68. if ( a <= 0 ) {
  69. return ( s * s < radius * radius );
  70. }
  71. else if ( a >= r * r ) {
  72. return ( e * e < radius * radius );
  73. }
  74. else {
  75. r = s + ( a / ( r * r ) ) * r;
  76. return ( r * r < radius * radius );
  77. }
  78. }
  79. /*
  80. ============
  81. idSphere::RayIntersection
  82. Returns true if the ray intersects the sphere.
  83. The ray can intersect the sphere in both directions from the start point.
  84. If start is inside the sphere then scale1 < 0 and scale2 > 0.
  85. ============
  86. */
  87. bool idSphere::RayIntersection( const idVec3 &start, const idVec3 &dir, float &scale1, float &scale2 ) const {
  88. double a, b, c, d, sqrtd;
  89. idVec3 p;
  90. p = start - origin;
  91. a = dir * dir;
  92. b = dir * p;
  93. c = p * p - radius * radius;
  94. d = b * b - c * a;
  95. if ( d < 0.0f ) {
  96. return false;
  97. }
  98. sqrtd = idMath::Sqrt( d );
  99. a = 1.0f / a;
  100. scale1 = ( -b + sqrtd ) * a;
  101. scale2 = ( -b - sqrtd ) * a;
  102. return true;
  103. }
  104. /*
  105. ============
  106. idSphere::FromPoints
  107. Tight sphere for a point set.
  108. ============
  109. */
  110. void idSphere::FromPoints( const idVec3 *points, const int numPoints ) {
  111. int i;
  112. float radiusSqr, dist;
  113. idVec3 mins, maxs;
  114. SIMDProcessor->MinMax( mins, maxs, points, numPoints );
  115. origin = ( mins + maxs ) * 0.5f;
  116. radiusSqr = 0.0f;
  117. for ( i = 0; i < numPoints; i++ ) {
  118. dist = ( points[i] - origin ).LengthSqr();
  119. if ( dist > radiusSqr ) {
  120. radiusSqr = dist;
  121. }
  122. }
  123. radius = idMath::Sqrt( radiusSqr );
  124. }