doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Vector.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 idVec2 vec2_origin( 0.0f, 0.0f );
33 idVec3 vec3_origin( 0.0f, 0.0f, 0.0f );
34 idVec4 vec4_origin( 0.0f, 0.0f, 0.0f, 0.0f );
35 idVec5 vec5_origin( 0.0f, 0.0f, 0.0f, 0.0f, 0.0f );
36 idVec6 vec6_origin( 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f );
38 
39 
40 //===============================================================
41 //
42 // idVec2
43 //
44 //===============================================================
45 
46 /*
47 =============
48 idVec2::ToString
49 =============
50 */
51 const char *idVec2::ToString( int precision ) const {
52  return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
53 }
54 
55 /*
56 =============
57 Lerp
58 
59 Linearly inperpolates one vector to another.
60 =============
61 */
62 void idVec2::Lerp( const idVec2 &v1, const idVec2 &v2, const float l ) {
63  if ( l <= 0.0f ) {
64  (*this) = v1;
65  } else if ( l >= 1.0f ) {
66  (*this) = v2;
67  } else {
68  (*this) = v1 + l * ( v2 - v1 );
69  }
70 }
71 
72 
73 //===============================================================
74 //
75 // idVec3
76 //
77 //===============================================================
78 
79 /*
80 =============
81 idVec3::ToYaw
82 =============
83 */
84 float idVec3::ToYaw( void ) const {
85  float yaw;
86 
87  if ( ( y == 0.0f ) && ( x == 0.0f ) ) {
88  yaw = 0.0f;
89  } else {
90  yaw = RAD2DEG( atan2( y, x ) );
91  if ( yaw < 0.0f ) {
92  yaw += 360.0f;
93  }
94  }
95 
96  return yaw;
97 }
98 
99 /*
100 =============
101 idVec3::ToPitch
102 =============
103 */
104 float idVec3::ToPitch( void ) const {
105  float forward;
106  float pitch;
107 
108  if ( ( x == 0.0f ) && ( y == 0.0f ) ) {
109  if ( z > 0.0f ) {
110  pitch = 90.0f;
111  } else {
112  pitch = 270.0f;
113  }
114  } else {
115  forward = ( float )idMath::Sqrt( x * x + y * y );
116  pitch = RAD2DEG( atan2( z, forward ) );
117  if ( pitch < 0.0f ) {
118  pitch += 360.0f;
119  }
120  }
121 
122  return pitch;
123 }
124 
125 /*
126 =============
127 idVec3::ToAngles
128 =============
129 */
130 idAngles idVec3::ToAngles( void ) const {
131  float forward;
132  float yaw;
133  float pitch;
134 
135  if ( ( x == 0.0f ) && ( y == 0.0f ) ) {
136  yaw = 0.0f;
137  if ( z > 0.0f ) {
138  pitch = 90.0f;
139  } else {
140  pitch = 270.0f;
141  }
142  } else {
143  yaw = RAD2DEG( atan2( y, x ) );
144  if ( yaw < 0.0f ) {
145  yaw += 360.0f;
146  }
147 
148  forward = ( float )idMath::Sqrt( x * x + y * y );
149  pitch = RAD2DEG( atan2( z, forward ) );
150  if ( pitch < 0.0f ) {
151  pitch += 360.0f;
152  }
153  }
154 
155  return idAngles( -pitch, yaw, 0.0f );
156 }
157 
158 /*
159 =============
160 idVec3::ToPolar
161 =============
162 */
163 idPolar3 idVec3::ToPolar( void ) const {
164  float forward;
165  float yaw;
166  float pitch;
167 
168  if ( ( x == 0.0f ) && ( y == 0.0f ) ) {
169  yaw = 0.0f;
170  if ( z > 0.0f ) {
171  pitch = 90.0f;
172  } else {
173  pitch = 270.0f;
174  }
175  } else {
176  yaw = RAD2DEG( atan2( y, x ) );
177  if ( yaw < 0.0f ) {
178  yaw += 360.0f;
179  }
180 
181  forward = ( float )idMath::Sqrt( x * x + y * y );
182  pitch = RAD2DEG( atan2( z, forward ) );
183  if ( pitch < 0.0f ) {
184  pitch += 360.0f;
185  }
186  }
187  return idPolar3( idMath::Sqrt( x * x + y * y + z * z ), yaw, -pitch );
188 }
189 
190 /*
191 =============
192 idVec3::ToMat3
193 =============
194 */
195 idMat3 idVec3::ToMat3( void ) const {
196  idMat3 mat;
197  float d;
198 
199  mat[0] = *this;
200  d = x * x + y * y;
201  if ( !d ) {
202  mat[1][0] = 1.0f;
203  mat[1][1] = 0.0f;
204  mat[1][2] = 0.0f;
205  } else {
206  d = idMath::InvSqrt( d );
207  mat[1][0] = -y * d;
208  mat[1][1] = x * d;
209  mat[1][2] = 0.0f;
210  }
211  mat[2] = Cross( mat[1] );
212 
213  return mat;
214 }
215 
216 /*
217 =============
218 idVec3::ToString
219 =============
220 */
221 const char *idVec3::ToString( int precision ) const {
222  return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
223 }
224 
225 /*
226 =============
227 Lerp
228 
229 Linearly inperpolates one vector to another.
230 =============
231 */
232 void idVec3::Lerp( const idVec3 &v1, const idVec3 &v2, const float l ) {
233  if ( l <= 0.0f ) {
234  (*this) = v1;
235  } else if ( l >= 1.0f ) {
236  (*this) = v2;
237  } else {
238  (*this) = v1 + l * ( v2 - v1 );
239  }
240 }
241 
242 /*
243 =============
244 SLerp
245 
246 Spherical linear interpolation from v1 to v2.
247 Vectors are expected to be normalized.
248 =============
249 */
250 #define LERP_DELTA 1e-6
251 
252 void idVec3::SLerp( const idVec3 &v1, const idVec3 &v2, const float t ) {
253  float omega, cosom, sinom, scale0, scale1;
254 
255  if ( t <= 0.0f ) {
256  (*this) = v1;
257  return;
258  } else if ( t >= 1.0f ) {
259  (*this) = v2;
260  return;
261  }
262 
263  cosom = v1 * v2;
264  if ( ( 1.0f - cosom ) > LERP_DELTA ) {
265  omega = acos( cosom );
266  sinom = sin( omega );
267  scale0 = sin( ( 1.0f - t ) * omega ) / sinom;
268  scale1 = sin( t * omega ) / sinom;
269  } else {
270  scale0 = 1.0f - t;
271  scale1 = t;
272  }
273 
274  (*this) = ( v1 * scale0 + v2 * scale1 );
275 }
276 
277 /*
278 =============
279 ProjectSelfOntoSphere
280 
281 Projects the z component onto a sphere.
282 =============
283 */
284 void idVec3::ProjectSelfOntoSphere( const float radius ) {
285  float rsqr = radius * radius;
286  float len = Length();
287  if ( len < rsqr * 0.5f ) {
288  z = sqrt( rsqr - len );
289  } else {
290  z = rsqr / ( 2.0f * sqrt( len ) );
291  }
292 }
293 
294 
295 
296 //===============================================================
297 //
298 // idVec4
299 //
300 //===============================================================
301 
302 /*
303 =============
304 idVec4::ToString
305 =============
306 */
307 const char *idVec4::ToString( int precision ) const {
308  return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
309 }
310 
311 /*
312 =============
313 Lerp
314 
315 Linearly inperpolates one vector to another.
316 =============
317 */
318 void idVec4::Lerp( const idVec4 &v1, const idVec4 &v2, const float l ) {
319  if ( l <= 0.0f ) {
320  (*this) = v1;
321  } else if ( l >= 1.0f ) {
322  (*this) = v2;
323  } else {
324  (*this) = v1 + l * ( v2 - v1 );
325  }
326 }
327 
328 
329 //===============================================================
330 //
331 // idVec5
332 //
333 //===============================================================
334 
335 /*
336 =============
337 idVec5::ToString
338 =============
339 */
340 const char *idVec5::ToString( int precision ) const {
341  return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
342 }
343 
344 /*
345 =============
346 idVec5::Lerp
347 =============
348 */
349 void idVec5::Lerp( const idVec5 &v1, const idVec5 &v2, const float l ) {
350  if ( l <= 0.0f ) {
351  (*this) = v1;
352  } else if ( l >= 1.0f ) {
353  (*this) = v2;
354  } else {
355  x = v1.x + l * ( v2.x - v1.x );
356  y = v1.y + l * ( v2.y - v1.y );
357  z = v1.z + l * ( v2.z - v1.z );
358  s = v1.s + l * ( v2.s - v1.s );
359  t = v1.t + l * ( v2.t - v1.t );
360  }
361 }
362 
363 
364 //===============================================================
365 //
366 // idVec6
367 //
368 //===============================================================
369 
370 /*
371 =============
372 idVec6::ToString
373 =============
374 */
375 const char *idVec6::ToString( int precision ) const {
376  return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
377 }
378 
379 
380 //===============================================================
381 //
382 // idVecX
383 //
384 //===============================================================
385 
386 float idVecX::temp[VECX_MAX_TEMP+4];
387 float * idVecX::tempPtr = (float *) ( ( (int) idVecX::temp + 15 ) & ~15 );
388 int idVecX::tempIndex = 0;
389 
390 /*
391 =============
392 idVecX::ToString
393 =============
394 */
395 const char *idVecX::ToString( int precision ) const {
396  return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
397 }
static const float INFINITY
Definition: Math.h:218
idMat3 ToMat3(void) const
Definition: Vector.cpp:195
idVec4 vec4_origin(0.0f, 0.0f, 0.0f, 0.0f)
static int tempIndex
Definition: Vector.h:1499
float t
Definition: Vector.h:1072
const float * ToFloatPtr(void) const
Definition: Vector.h:719
int GetDimension(void) const
Definition: Vector.h:297
idVec6 vec6_origin(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)
GLenum GLint GLint y
Definition: glext.h:2849
const char * ToString(int precision=2) const
Definition: Vector.cpp:375
Definition: Vector.h:316
case const float
Definition: Callbacks.cpp:62
static const char * FloatArrayToString(const float *array, const int length, const int precision)
Definition: Str.cpp:418
static float Sqrt(float x)
Definition: Math.h:302
void Lerp(const idVec2 &v1, const idVec2 &v2, const float l)
Definition: Vector.cpp:62
GLdouble s
Definition: glext.h:2935
GLenum GLsizei len
Definition: glext.h:3472
#define LERP_DELTA
Definition: Vector.cpp:250
idVec3 Cross(const idVec3 &a) const
Definition: Vector.h:619
GLenum GLint x
Definition: glext.h:2849
const char * ToString(int precision=2) const
Definition: Vector.cpp:51
const float * ToFloatPtr(void) const
Definition: Vector.h:1410
float z
Definition: Vector.h:1070
list l
Definition: prepare.py:17
GLfloat GLfloat GLfloat v2
Definition: glext.h:3608
Definition: Vector.h:52
idAngles ToAngles(void) const
Definition: Vector.cpp:130
Definition: Vector.h:808
float Length(void) const
Definition: Vector.h:631
float ToYaw(void) const
Definition: Vector.cpp:84
idVec3 vec3_origin(0.0f, 0.0f, 0.0f)
static float temp[VECX_MAX_TEMP+4]
Definition: Vector.h:1497
static float * tempPtr
Definition: Vector.h:1498
const char * ToString(int precision=2) const
Definition: Vector.cpp:395
const float * ToFloatPtr(void) const
Definition: Vector.h:1910
float y
Definition: Vector.h:319
const char * ToString(int precision=2) const
Definition: Vector.cpp:307
static float InvSqrt(float x)
Definition: Math.h:268
float ToPitch(void) const
Definition: Vector.cpp:104
const float * ToFloatPtr(void) const
Definition: Vector.h:1051
GLfloat GLfloat v1
Definition: glext.h:3607
const char * ToString(int precision=2) const
Definition: Vector.cpp:221
int GetDimension(void) const
Definition: Vector.h:1886
idVec5 vec5_origin(0.0f, 0.0f, 0.0f, 0.0f, 0.0f)
const float * ToFloatPtr(void) const
Definition: Vector.h:1143
float y
Definition: Vector.h:1069
idVec2 vec2_origin(0.0f, 0.0f)
Definition: Matrix.h:333
void ProjectSelfOntoSphere(const float radius)
Definition: Vector.cpp:284
tuple f
Definition: idal.py:89
void Lerp(const idVec4 &v1, const idVec4 &v2, const float l)
Definition: Vector.cpp:318
const float * ToFloatPtr(void) const
Definition: Vector.h:301
#define RAD2DEG(a)
Definition: Math.h:57
idVec6 vec6_infinity(idMath::INFINITY, idMath::INFINITY, idMath::INFINITY, idMath::INFINITY, idMath::INFINITY, idMath::INFINITY)
float x
Definition: Vector.h:1068
void Lerp(const idVec5 &v1, const idVec5 &v2, const float l)
Definition: Vector.cpp:349
void Lerp(const idVec3 &v1, const idVec3 &v2, const float l)
Definition: Vector.cpp:232
int GetDimension(void) const
Definition: Vector.h:1031
void SLerp(const idVec3 &v1, const idVec3 &v2, const float l)
Definition: Vector.cpp:252
int GetDimension(void) const
Definition: Vector.h:1398
int GetDimension(void) const
Definition: Vector.h:707
GLdouble GLdouble z
Definition: glext.h:3067
float s
Definition: Vector.h:1071
const char * ToString(int precision=2) const
Definition: Vector.cpp:340
#define VECX_MAX_TEMP
Definition: Vector.h:1429
idPolar3 ToPolar(void) const
Definition: Vector.cpp:163
int GetDimension(void) const
Definition: Vector.h:1131
GLdouble GLdouble t
Definition: glext.h:2943