2 ===========================================================================
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
9 Doom 3 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.
14 Doom 3 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.
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
22 In addition, the Doom 3 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 Source Code. If not, please request a copy in writing from id Software at the address below.
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.
26 ===========================================================================
29 #include "../precompiled.h"
32 idPlane plane_origin( 0.0f, 0.0f, 0.0f, 0.0f );
39 int idPlane::Type( void ) const {
40 if ( Normal()[0] == 0.0f ) {
41 if ( Normal()[1] == 0.0f ) {
42 return Normal()[2] > 0.0f ? PLANETYPE_Z : PLANETYPE_NEGZ;
44 else if ( Normal()[2] == 0.0f ) {
45 return Normal()[1] > 0.0f ? PLANETYPE_Y : PLANETYPE_NEGY;
48 return PLANETYPE_ZEROX;
51 else if ( Normal()[1] == 0.0f ) {
52 if ( Normal()[2] == 0.0f ) {
53 return Normal()[0] > 0.0f ? PLANETYPE_X : PLANETYPE_NEGX;
56 return PLANETYPE_ZEROY;
59 else if ( Normal()[2] == 0.0f ) {
60 return PLANETYPE_ZEROZ;
63 return PLANETYPE_NONAXIAL;
72 bool idPlane::HeightFit( const idVec3 *points, const int numPoints ) {
74 float sumXX = 0.0f, sumXY = 0.0f, sumXZ = 0.0f;
75 float sumYY = 0.0f, sumYZ = 0.0f;
76 idVec3 sum, average, dir;
78 if ( numPoints == 1 ) {
85 if ( numPoints == 2 ) {
86 dir = points[1] - points[0];
87 Normal() = dir.Cross( idVec3( 0, 0, 1 ) ).Cross( dir );
89 d = -( Normal() * points[0] );
94 for ( i = 0; i < numPoints; i++) {
97 average = sum / numPoints;
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;
108 idMat2 m( sumXX, sumXY, sumXY, sumYY );
109 if ( !m.InverseSelf() ) {
113 a = - sumXZ * m[0][0] - sumYZ * m[0][1];
114 b = - sumXZ * m[1][0] - sumYZ * m[1][1];
117 d = -( a * average.x + b * average.y + c * average.z );
123 idPlane::PlaneIntersection
126 bool idPlane::PlaneIntersection( const idPlane &plane, idVec3 &start, idVec3 &dir ) const {
127 double n00, n01, n11, det, invDet, f0, f1;
129 n00 = Normal().LengthSqr();
130 n01 = Normal() * plane.Normal();
131 n11 = plane.Normal().LengthSqr();
132 det = n00 * n11 - n01 * n01;
134 if ( idMath::Fabs(det) < 1e-6f ) {
139 f0 = ( n01 * plane.d - n11 * d ) * invDet;
140 f1 = ( n01 * d - n00 * plane.d ) * invDet;
142 dir = Normal().Cross( plane.Normal() );
143 start = f0 * Normal() + f1 * plane.Normal();
152 const char *idPlane::ToString( int precision ) const {
153 return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );