Plane.cpp 4.1 KB

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