home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / idlib / bv / Sphere.cpp < prev    next >
C/C++ Source or Header  |  2005-11-14  |  2KB  |  129 lines

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