Plane.cpp (4210B)
1 /* 2 =========================================================================== 3 4 Doom 3 BFG Edition GPL Source Code 5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 6 7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). 8 9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation, either version 3 of the License, or 12 (at your option) any later version. 13 14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>. 21 22 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. 23 24 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. 25 26 =========================================================================== 27 */ 28 29 #pragma hdrstop 30 #include "../precompiled.h" 31 32 idPlane plane_origin( 0.0f, 0.0f, 0.0f, 0.0f ); 33 34 /* 35 ================ 36 idPlane::Type 37 ================ 38 */ 39 int idPlane::Type() const { 40 if ( Normal()[0] == 0.0f ) { 41 if ( Normal()[1] == 0.0f ) { 42 return Normal()[2] > 0.0f ? PLANETYPE_Z : PLANETYPE_NEGZ; 43 } 44 else if ( Normal()[2] == 0.0f ) { 45 return Normal()[1] > 0.0f ? PLANETYPE_Y : PLANETYPE_NEGY; 46 } 47 else { 48 return PLANETYPE_ZEROX; 49 } 50 } 51 else if ( Normal()[1] == 0.0f ) { 52 if ( Normal()[2] == 0.0f ) { 53 return Normal()[0] > 0.0f ? PLANETYPE_X : PLANETYPE_NEGX; 54 } 55 else { 56 return PLANETYPE_ZEROY; 57 } 58 } 59 else if ( Normal()[2] == 0.0f ) { 60 return PLANETYPE_ZEROZ; 61 } 62 else { 63 return PLANETYPE_NONAXIAL; 64 } 65 } 66 67 /* 68 ================ 69 idPlane::HeightFit 70 ================ 71 */ 72 bool idPlane::HeightFit( const idVec3 *points, const int numPoints ) { 73 int i; 74 float sumXX = 0.0f, sumXY = 0.0f, sumXZ = 0.0f; 75 float sumYY = 0.0f, sumYZ = 0.0f; 76 idVec3 sum, average, dir; 77 78 if ( numPoints == 1 ) { 79 a = 0.0f; 80 b = 0.0f; 81 c = 1.0f; 82 d = -points[0].z; 83 return true; 84 } 85 if ( numPoints == 2 ) { 86 dir = points[1] - points[0]; 87 Normal() = dir.Cross( idVec3( 0, 0, 1 ) ).Cross( dir ); 88 Normalize(); 89 d = -( Normal() * points[0] ); 90 return true; 91 } 92 93 sum.Zero(); 94 for ( i = 0; i < numPoints; i++) { 95 sum += points[i]; 96 } 97 average = sum / numPoints; 98 99 for ( i = 0; i < numPoints; i++ ) { 100 dir = points[i] - average; 101 sumXX += dir.x * dir.x; 102 sumXY += dir.x * dir.y; 103 sumXZ += dir.x * dir.z; 104 sumYY += dir.y * dir.y; 105 sumYZ += dir.y * dir.z; 106 } 107 108 idMat2 m( sumXX, sumXY, sumXY, sumYY ); 109 if ( !m.InverseSelf() ) { 110 return false; 111 } 112 113 a = - sumXZ * m[0][0] - sumYZ * m[0][1]; 114 b = - sumXZ * m[1][0] - sumYZ * m[1][1]; 115 c = 1.0f; 116 Normalize(); 117 d = -( a * average.x + b * average.y + c * average.z ); 118 return true; 119 } 120 121 /* 122 ================ 123 idPlane::PlaneIntersection 124 ================ 125 */ 126 bool idPlane::PlaneIntersection( const idPlane &plane, idVec3 &start, idVec3 &dir ) const { 127 double n00, n01, n11, det, invDet, f0, f1; 128 129 n00 = Normal().LengthSqr(); 130 n01 = Normal() * plane.Normal(); 131 n11 = plane.Normal().LengthSqr(); 132 det = n00 * n11 - n01 * n01; 133 134 if ( idMath::Fabs(det) < 1e-6f ) { 135 return false; 136 } 137 138 invDet = 1.0f / det; 139 f0 = ( n01 * plane.d - n11 * d ) * invDet; 140 f1 = ( n01 * d - n00 * plane.d ) * invDet; 141 142 dir = Normal().Cross( plane.Normal() ); 143 start = f0 * Normal() + f1 * plane.Normal(); 144 return true; 145 } 146 147 /* 148 ============= 149 idPlane::ToString 150 ============= 151 */ 152 const char *idPlane::ToString( int precision ) const { 153 return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision ); 154 }