1 /*
2 ===========================================================================
3
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6
7 This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").
8
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.
13
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.
18
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/>.
21
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.
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 #include "sys/platform.h"
30 #include "idlib/Str.h"
31
32 #include "idlib/math/Plane.h"
33
34 idPlane plane_origin( 0.0f, 0.0f, 0.0f, 0.0f );
35
36 /*
37 ================
38 idPlane::Type
39 ================
40 */
Type(void) const41 int idPlane::Type( void ) const {
42 if ( Normal()[0] == 0.0f ) {
43 if ( Normal()[1] == 0.0f ) {
44 return Normal()[2] > 0.0f ? PLANETYPE_Z : PLANETYPE_NEGZ;
45 }
46 else if ( Normal()[2] == 0.0f ) {
47 return Normal()[1] > 0.0f ? PLANETYPE_Y : PLANETYPE_NEGY;
48 }
49 else {
50 return PLANETYPE_ZEROX;
51 }
52 }
53 else if ( Normal()[1] == 0.0f ) {
54 if ( Normal()[2] == 0.0f ) {
55 return Normal()[0] > 0.0f ? PLANETYPE_X : PLANETYPE_NEGX;
56 }
57 else {
58 return PLANETYPE_ZEROY;
59 }
60 }
61 else if ( Normal()[2] == 0.0f ) {
62 return PLANETYPE_ZEROZ;
63 }
64 else {
65 return PLANETYPE_NONAXIAL;
66 }
67 }
68
69 /*
70 ================
71 idPlane::HeightFit
72 ================
73 */
HeightFit(const idVec3 * points,const int numPoints)74 bool idPlane::HeightFit( const idVec3 *points, const int numPoints ) {
75 int i;
76 float sumXX = 0.0f, sumXY = 0.0f, sumXZ = 0.0f;
77 float sumYY = 0.0f, sumYZ = 0.0f;
78 idVec3 sum, average, dir;
79
80 if ( numPoints == 1 ) {
81 a = 0.0f;
82 b = 0.0f;
83 c = 1.0f;
84 d = -points[0].z;
85 return true;
86 }
87 if ( numPoints == 2 ) {
88 dir = points[1] - points[0];
89 Normal() = dir.Cross( idVec3( 0, 0, 1 ) ).Cross( dir );
90 Normalize();
91 d = -( Normal() * points[0] );
92 return true;
93 }
94
95 sum.Zero();
96 for ( i = 0; i < numPoints; i++) {
97 sum += points[i];
98 }
99 average = sum / numPoints;
100
101 for ( i = 0; i < numPoints; i++ ) {
102 dir = points[i] - average;
103 sumXX += dir.x * dir.x;
104 sumXY += dir.x * dir.y;
105 sumXZ += dir.x * dir.z;
106 sumYY += dir.y * dir.y;
107 sumYZ += dir.y * dir.z;
108 }
109
110 idMat2 m( sumXX, sumXY, sumXY, sumYY );
111 if ( !m.InverseSelf() ) {
112 return false;
113 }
114
115 a = - sumXZ * m[0][0] - sumYZ * m[0][1];
116 b = - sumXZ * m[1][0] - sumYZ * m[1][1];
117 c = 1.0f;
118 Normalize();
119 d = -( a * average.x + b * average.y + c * average.z );
120 return true;
121 }
122
123 /*
124 ================
125 idPlane::PlaneIntersection
126 ================
127 */
PlaneIntersection(const idPlane & plane,idVec3 & start,idVec3 & dir) const128 bool idPlane::PlaneIntersection( const idPlane &plane, idVec3 &start, idVec3 &dir ) const {
129 double n00, n01, n11, det, invDet, f0, f1;
130
131 n00 = Normal().LengthSqr();
132 n01 = Normal() * plane.Normal();
133 n11 = plane.Normal().LengthSqr();
134 det = n00 * n11 - n01 * n01;
135
136 if ( idMath::Fabs(det) < 1e-6f ) {
137 return false;
138 }
139
140 invDet = 1.0f / det;
141 f0 = ( n01 * plane.d - n11 * d ) * invDet;
142 f1 = ( n01 * d - n00 * plane.d ) * invDet;
143
144 dir = Normal().Cross( plane.Normal() );
145 start = f0 * Normal() + f1 * plane.Normal();
146 return true;
147 }
148
149 /*
150 =============
151 idPlane::ToString
152 =============
153 */
ToString(int precision) const154 const char *idPlane::ToString( int precision ) const {
155 return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
156 }
157