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

  1.  
  2. #include "../precompiled.h"
  3. #pragma hdrstop
  4.  
  5. idPlane plane_origin( 0.0f, 0.0f, 0.0f, 0.0f );
  6.  
  7. /*
  8. ================
  9. idPlane::Type
  10. ================
  11. */
  12. int idPlane::Type( void ) const {
  13.     if ( Normal()[0] == 0.0f ) {
  14.         if ( Normal()[1] == 0.0f ) {
  15.             return Normal()[2] > 0.0f ? PLANETYPE_Z : PLANETYPE_NEGZ;
  16.         }
  17.         else if ( Normal()[2] == 0.0f ) {
  18.             return Normal()[1] > 0.0f ? PLANETYPE_Y : PLANETYPE_NEGY;
  19.         }
  20.         else {
  21.             return PLANETYPE_ZEROX;
  22.         }
  23.     }
  24.     else if ( Normal()[1] == 0.0f ) {
  25.         if ( Normal()[2] == 0.0f ) {
  26.             return Normal()[0] > 0.0f ? PLANETYPE_X : PLANETYPE_NEGX;
  27.         }
  28.         else {
  29.             return PLANETYPE_ZEROY;
  30.         }
  31.     }
  32.     else if ( Normal()[2] == 0.0f ) {
  33.         return PLANETYPE_ZEROZ;
  34.     }
  35.     else {
  36.         return PLANETYPE_NONAXIAL;
  37.     }
  38. }
  39.  
  40. /*
  41. ================
  42. idPlane::HeightFit
  43. ================
  44. */
  45. bool idPlane::HeightFit( const idVec3 *points, const int numPoints ) {
  46.     int i;
  47.     float sumXX = 0.0f, sumXY = 0.0f, sumXZ = 0.0f;
  48.     float sumYY = 0.0f, sumYZ = 0.0f;
  49.     idVec3 sum, average, dir;
  50.  
  51.     if ( numPoints == 1 ) {
  52.         a = 0.0f;
  53.         b = 0.0f;
  54.         c = 1.0f;
  55.         d = -points[0].z;
  56.         return true;
  57.     }
  58.     if ( numPoints == 2 ) {
  59.         dir = points[1] - points[0];
  60.         Normal() = dir.Cross( idVec3( 0, 0, 1 ) ).Cross( dir );
  61.         Normalize();
  62.         d = -( Normal() * points[0] );
  63.         return true;
  64.     }
  65.  
  66.     sum.Zero();
  67.     for ( i = 0; i < numPoints; i++) {
  68.         sum += points[i];
  69.     }
  70.     average = sum / numPoints;
  71.  
  72.     for ( i = 0; i < numPoints; i++ ) {
  73.         dir = points[i] - average;
  74.         sumXX += dir.x * dir.x;
  75.         sumXY += dir.x * dir.y;
  76.         sumXZ += dir.x * dir.z;
  77.         sumYY += dir.y * dir.y;
  78.         sumYZ += dir.y * dir.z;
  79.     }
  80.  
  81.     idMat2 m( sumXX, sumXY, sumXY, sumYY );
  82.     if ( !m.InverseSelf() ) {
  83.         return false;
  84.     }
  85.  
  86.     a = - sumXZ * m[0][0] - sumYZ * m[0][1];
  87.     b = - sumXZ * m[1][0] - sumYZ * m[1][1];
  88.     c = 1.0f;
  89.     Normalize();
  90.     d = -( a * average.x + b * average.y + c * average.z );
  91.     return true;
  92. }
  93.  
  94. /*
  95. ================
  96. idPlane::PlaneIntersection
  97. ================
  98. */
  99. bool idPlane::PlaneIntersection( const idPlane &plane, idVec3 &start, idVec3 &dir ) const {
  100.     double n00, n01, n11, det, invDet, f0, f1;
  101.  
  102.     n00 = Normal().LengthSqr();
  103.     n01 = Normal() * plane.Normal();
  104.     n11 = plane.Normal().LengthSqr();
  105.     det = n00 * n11 - n01 * n01;
  106.  
  107.     if ( idMath::Fabs(det) < 1e-6f ) {
  108.         return false;
  109.     }
  110.  
  111.     invDet = 1.0f / det;
  112.     f0 = ( n01 * plane.d - n11 * d ) * invDet;
  113.     f1 = ( n01 * d - n00 * plane.d ) * invDet;
  114.  
  115.     dir = Normal().Cross( plane.Normal() );
  116.     start = f0 * Normal() + f1 * plane.Normal();
  117.     return true;
  118. }
  119.  
  120. /*
  121. =============
  122. idPlane::ToString
  123. =============
  124. */
  125. const char *idPlane::ToString( int precision ) const {
  126.     return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
  127. }
  128.