doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Plane.cpp
Go to the documentation of this file.
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 "../precompiled.h"
30 #pragma hdrstop
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( void ) 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 }
GLsizei const GLfloat * points
Definition: glext.h:3884
const idVec3 & Normal(void) const
Definition: Plane.h:239
#define PLANETYPE_X
Definition: Plane.h:59
float z
Definition: Vector.h:320
#define PLANETYPE_NEGX
Definition: Plane.h:62
idPlane plane_origin(0.0f, 0.0f, 0.0f, 0.0f)
bool PlaneIntersection(const idPlane &plane, idVec3 &start, idVec3 &dir) const
Definition: Plane.cpp:126
const float * ToFloatPtr(void) const
Definition: Plane.h:383
Definition: Vector.h:316
static const char * FloatArrayToString(const float *array, const int length, const int precision)
Definition: Str.cpp:418
#define PLANETYPE_NONAXIAL
Definition: Plane.h:69
idVec3 Cross(const idVec3 &a) const
Definition: Vector.h:619
float x
Definition: Vector.h:318
int i
Definition: process.py:33
int Type(void) const
Definition: Plane.cpp:39
#define PLANETYPE_ZEROX
Definition: Plane.h:66
const char * ToString(int precision=2) const
Definition: Plane.cpp:152
#define PLANETYPE_Z
Definition: Plane.h:61
bool InverseSelf(void)
Definition: Matrix.cpp:47
const GLubyte * c
Definition: glext.h:4677
static float Fabs(float f)
Definition: Math.h:779
bool HeightFit(const idVec3 *points, const int numPoints)
Definition: Plane.cpp:72
float y
Definition: Vector.h:319
Definition: Plane.h:71
#define PLANETYPE_ZEROY
Definition: Plane.h:67
int GetDimension(void) const
Definition: Plane.h:371
float LengthSqr(void) const
Definition: Vector.h:635
GLubyte GLubyte GLubyte a
Definition: glext.h:4662
float Normalize(bool fixDegenerate=true)
Definition: Plane.h:247
#define PLANETYPE_NEGY
Definition: Plane.h:63
GLubyte GLubyte b
Definition: glext.h:4662
#define PLANETYPE_ZEROZ
Definition: Plane.h:68
tuple f
Definition: idal.py:89
float d
Definition: Plane.h:131
#define PLANETYPE_NEGZ
Definition: Plane.h:64
#define PLANETYPE_Y
Definition: Plane.h:60
Definition: Matrix.h:55
void Zero(void)
Definition: Vector.h:415
GLuint start
Definition: glext.h:2845