1 /*
2 ===========================================================================
3 Copyright (C) 1999 - 2005, Id Software, Inc.
4 Copyright (C) 2000 - 2013, Raven Software, Inc.
5 Copyright (C) 2001 - 2013, Activision, Inc.
6 Copyright (C) 2013 - 2015, OpenJK contributors
7
8 This file is part of the OpenJK source code.
9
10 OpenJK is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License version 2 as
12 published by the Free Software Foundation.
13
14 This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
21 ===========================================================================
22 */
23
24 #include "q_math.h"
25 #include <assert.h>
26 #include <float.h>
27 #include <math.h>
28 #include <stdlib.h>
29
30
31 ///////////////////////////////////////////////////////////////////////////
32 //
33 // DIRECTION ENCODING
34 //
35 ///////////////////////////////////////////////////////////////////////////
36 #define NUMVERTEXNORMALS 162
37 static const vec3_t bytedirs[NUMVERTEXNORMALS] =
38 {
39 {-0.525731f, 0.000000f, 0.850651f}, {-0.442863f, 0.238856f, 0.864188f},
40 {-0.295242f, 0.000000f, 0.955423f}, {-0.309017f, 0.500000f, 0.809017f},
41 {-0.162460f, 0.262866f, 0.951056f}, {0.000000f, 0.000000f, 1.000000f},
42 {0.000000f, 0.850651f, 0.525731f}, {-0.147621f, 0.716567f, 0.681718f},
43 {0.147621f, 0.716567f, 0.681718f}, {0.000000f, 0.525731f, 0.850651f},
44 {0.309017f, 0.500000f, 0.809017f}, {0.525731f, 0.000000f, 0.850651f},
45 {0.295242f, 0.000000f, 0.955423f}, {0.442863f, 0.238856f, 0.864188f},
46 {0.162460f, 0.262866f, 0.951056f}, {-0.681718f, 0.147621f, 0.716567f},
47 {-0.809017f, 0.309017f, 0.500000f},{-0.587785f, 0.425325f, 0.688191f},
48 {-0.850651f, 0.525731f, 0.000000f},{-0.864188f, 0.442863f, 0.238856f},
49 {-0.716567f, 0.681718f, 0.147621f},{-0.688191f, 0.587785f, 0.425325f},
50 {-0.500000f, 0.809017f, 0.309017f}, {-0.238856f, 0.864188f, 0.442863f},
51 {-0.425325f, 0.688191f, 0.587785f}, {-0.716567f, 0.681718f, -0.147621f},
52 {-0.500000f, 0.809017f, -0.309017f}, {-0.525731f, 0.850651f, 0.000000f},
53 {0.000000f, 0.850651f, -0.525731f}, {-0.238856f, 0.864188f, -0.442863f},
54 {0.000000f, 0.955423f, -0.295242f}, {-0.262866f, 0.951056f, -0.162460f},
55 {0.000000f, 1.000000f, 0.000000f}, {0.000000f, 0.955423f, 0.295242f},
56 {-0.262866f, 0.951056f, 0.162460f}, {0.238856f, 0.864188f, 0.442863f},
57 {0.262866f, 0.951056f, 0.162460f}, {0.500000f, 0.809017f, 0.309017f},
58 {0.238856f, 0.864188f, -0.442863f},{0.262866f, 0.951056f, -0.162460f},
59 {0.500000f, 0.809017f, -0.309017f},{0.850651f, 0.525731f, 0.000000f},
60 {0.716567f, 0.681718f, 0.147621f}, {0.716567f, 0.681718f, -0.147621f},
61 {0.525731f, 0.850651f, 0.000000f}, {0.425325f, 0.688191f, 0.587785f},
62 {0.864188f, 0.442863f, 0.238856f}, {0.688191f, 0.587785f, 0.425325f},
63 {0.809017f, 0.309017f, 0.500000f}, {0.681718f, 0.147621f, 0.716567f},
64 {0.587785f, 0.425325f, 0.688191f}, {0.955423f, 0.295242f, 0.000000f},
65 {1.000000f, 0.000000f, 0.000000f}, {0.951056f, 0.162460f, 0.262866f},
66 {0.850651f, -0.525731f, 0.000000f},{0.955423f, -0.295242f, 0.000000f},
67 {0.864188f, -0.442863f, 0.238856f}, {0.951056f, -0.162460f, 0.262866f},
68 {0.809017f, -0.309017f, 0.500000f}, {0.681718f, -0.147621f, 0.716567f},
69 {0.850651f, 0.000000f, 0.525731f}, {0.864188f, 0.442863f, -0.238856f},
70 {0.809017f, 0.309017f, -0.500000f}, {0.951056f, 0.162460f, -0.262866f},
71 {0.525731f, 0.000000f, -0.850651f}, {0.681718f, 0.147621f, -0.716567f},
72 {0.681718f, -0.147621f, -0.716567f},{0.850651f, 0.000000f, -0.525731f},
73 {0.809017f, -0.309017f, -0.500000f}, {0.864188f, -0.442863f, -0.238856f},
74 {0.951056f, -0.162460f, -0.262866f}, {0.147621f, 0.716567f, -0.681718f},
75 {0.309017f, 0.500000f, -0.809017f}, {0.425325f, 0.688191f, -0.587785f},
76 {0.442863f, 0.238856f, -0.864188f}, {0.587785f, 0.425325f, -0.688191f},
77 {0.688191f, 0.587785f, -0.425325f}, {-0.147621f, 0.716567f, -0.681718f},
78 {-0.309017f, 0.500000f, -0.809017f}, {0.000000f, 0.525731f, -0.850651f},
79 {-0.525731f, 0.000000f, -0.850651f}, {-0.442863f, 0.238856f, -0.864188f},
80 {-0.295242f, 0.000000f, -0.955423f}, {-0.162460f, 0.262866f, -0.951056f},
81 {0.000000f, 0.000000f, -1.000000f}, {0.295242f, 0.000000f, -0.955423f},
82 {0.162460f, 0.262866f, -0.951056f}, {-0.442863f, -0.238856f, -0.864188f},
83 {-0.309017f, -0.500000f, -0.809017f}, {-0.162460f, -0.262866f, -0.951056f},
84 {0.000000f, -0.850651f, -0.525731f}, {-0.147621f, -0.716567f, -0.681718f},
85 {0.147621f, -0.716567f, -0.681718f}, {0.000000f, -0.525731f, -0.850651f},
86 {0.309017f, -0.500000f, -0.809017f}, {0.442863f, -0.238856f, -0.864188f},
87 {0.162460f, -0.262866f, -0.951056f}, {0.238856f, -0.864188f, -0.442863f},
88 {0.500000f, -0.809017f, -0.309017f}, {0.425325f, -0.688191f, -0.587785f},
89 {0.716567f, -0.681718f, -0.147621f}, {0.688191f, -0.587785f, -0.425325f},
90 {0.587785f, -0.425325f, -0.688191f}, {0.000000f, -0.955423f, -0.295242f},
91 {0.000000f, -1.000000f, 0.000000f}, {0.262866f, -0.951056f, -0.162460f},
92 {0.000000f, -0.850651f, 0.525731f}, {0.000000f, -0.955423f, 0.295242f},
93 {0.238856f, -0.864188f, 0.442863f}, {0.262866f, -0.951056f, 0.162460f},
94 {0.500000f, -0.809017f, 0.309017f}, {0.716567f, -0.681718f, 0.147621f},
95 {0.525731f, -0.850651f, 0.000000f}, {-0.238856f, -0.864188f, -0.442863f},
96 {-0.500000f, -0.809017f, -0.309017f}, {-0.262866f, -0.951056f, -0.162460f},
97 {-0.850651f, -0.525731f, 0.000000f}, {-0.716567f, -0.681718f, -0.147621f},
98 {-0.716567f, -0.681718f, 0.147621f}, {-0.525731f, -0.850651f, 0.000000f},
99 {-0.500000f, -0.809017f, 0.309017f}, {-0.238856f, -0.864188f, 0.442863f},
100 {-0.262866f, -0.951056f, 0.162460f}, {-0.864188f, -0.442863f, 0.238856f},
101 {-0.809017f, -0.309017f, 0.500000f}, {-0.688191f, -0.587785f, 0.425325f},
102 {-0.681718f, -0.147621f, 0.716567f}, {-0.442863f, -0.238856f, 0.864188f},
103 {-0.587785f, -0.425325f, 0.688191f}, {-0.309017f, -0.500000f, 0.809017f},
104 {-0.147621f, -0.716567f, 0.681718f}, {-0.425325f, -0.688191f, 0.587785f},
105 {-0.162460f, -0.262866f, 0.951056f}, {0.442863f, -0.238856f, 0.864188f},
106 {0.162460f, -0.262866f, 0.951056f}, {0.309017f, -0.500000f, 0.809017f},
107 {0.147621f, -0.716567f, 0.681718f}, {0.000000f, -0.525731f, 0.850651f},
108 {0.425325f, -0.688191f, 0.587785f}, {0.587785f, -0.425325f, 0.688191f},
109 {0.688191f, -0.587785f, 0.425325f}, {-0.955423f, 0.295242f, 0.000000f},
110 {-0.951056f, 0.162460f, 0.262866f}, {-1.000000f, 0.000000f, 0.000000f},
111 {-0.850651f, 0.000000f, 0.525731f}, {-0.955423f, -0.295242f, 0.000000f},
112 {-0.951056f, -0.162460f, 0.262866f}, {-0.864188f, 0.442863f, -0.238856f},
113 {-0.951056f, 0.162460f, -0.262866f}, {-0.809017f, 0.309017f, -0.500000f},
114 {-0.864188f, -0.442863f, -0.238856f}, {-0.951056f, -0.162460f, -0.262866f},
115 {-0.809017f, -0.309017f, -0.500000f}, {-0.681718f, 0.147621f, -0.716567f},
116 {-0.681718f, -0.147621f, -0.716567f}, {-0.850651f, 0.000000f, -0.525731f},
117 {-0.688191f, 0.587785f, -0.425325f}, {-0.587785f, 0.425325f, -0.688191f},
118 {-0.425325f, 0.688191f, -0.587785f}, {-0.425325f, -0.688191f, -0.587785f},
119 {-0.587785f, -0.425325f, -0.688191f}, {-0.688191f, -0.587785f, -0.425325f}
120 };
121
122 // this isn't a real cheap function to call!
DirToByte(vec3_t dir)123 int DirToByte( vec3_t dir )
124 {
125 int i, best;
126 float d, bestd;
127
128 if ( !dir ) {
129 return 0;
130 }
131
132 bestd = 0;
133 best = 0;
134 for (i=0 ; i<NUMVERTEXNORMALS ; i++)
135 {
136 d = DotProduct(dir, bytedirs[i]);
137 if (d > bestd)
138 {
139 bestd = d;
140 best = i;
141 }
142 }
143
144 return best;
145 }
146
ByteToDir(int b,vec3_t dir)147 void ByteToDir( int b, vec3_t dir )
148 {
149 if ( b < 0 || b >= NUMVERTEXNORMALS ) {
150 VectorCopy( vec3_origin, dir );
151 return;
152 }
153 VectorCopy(bytedirs[b], dir);
154 }
155
156 /*
157 ** NormalToLatLong
158 **
159 ** We use two byte encoded normals in some space critical applications.
160 ** Lat = 0 at (1,0,0) to 360 (-1,0,0), encoded in 8-bit sine table format
161 ** Lng = 0 at (0,0,1) to 180 (0,0,-1), encoded in 8-bit sine table format
162 **
163 */
164 //rwwRMG - added
NormalToLatLong(const vec3_t normal,byte bytes[2])165 void NormalToLatLong( const vec3_t normal, byte bytes[2] )
166 {
167 // check for singularities
168 if (!normal[0] && !normal[1])
169 {
170 if ( normal[2] > 0.0f )
171 {
172 bytes[0] = 0;
173 bytes[1] = 0; // lat = 0, long = 0
174 }
175 else
176 {
177 bytes[0] = 128;
178 bytes[1] = 0; // lat = 0, long = 128
179 }
180 }
181 else
182 {
183 int a, b;
184
185 a = (int)(RAD2DEG( (float)atan2( normal[1], normal[0] ) ) * (255.0f / 360.0f ));
186 a &= 0xff;
187
188 b = (int)(RAD2DEG( (float)acos( normal[2] ) ) * ( 255.0f / 360.0f ));
189 b &= 0xff;
190
191 bytes[0] = b; // longitude
192 bytes[1] = a; // lattitude
193 }
194 }
195
196 ///////////////////////////////////////////////////////////////////////////
197 //
198 // RANDOM NUMBER GENERATION
199 //
200 ///////////////////////////////////////////////////////////////////////////
Q_rand(int * seed)201 int Q_rand( int *seed )
202 {
203 *seed = (69069 * *seed + 1);
204 return *seed;
205 }
206
Q_random(int * seed)207 float Q_random( int *seed )
208 {
209 return (Q_rand(seed) & 0xffff) / (float)0x10000;
210 }
211
Q_crandom(int * seed)212 float Q_crandom( int *seed )
213 {
214 return 2.0f * (Q_random(seed) - 0.5f);
215 }
216
217 // This is the VC libc version of rand() without multiple seeds per thread or 12 levels
218 // of subroutine calls.
219 // Both calls have been designed to minimise the inherent number of float <--> int
220 // conversions and the additional math required to get the desired value.
221 // eg the typical tint = (rand() * 255) / 32768
222 // becomes tint = irand(0, 255)
223 static uint32_t holdrand = 0x89abcdef;
224
Rand_Init(int seed)225 void Rand_Init( int seed )
226 {
227 holdrand = seed;
228 }
229
230 // Returns a float min <= x < max (exclusive; will get max - 0.00001; but never max)
flrand(float min,float max)231 float flrand(float min, float max)
232 {
233 float result;
234
235 holdrand = (holdrand * 214013L) + 2531011L;
236 result = (float)(holdrand >> 17); // 0 - 32767 range
237 result = ((result * (max - min)) / (float)QRAND_MAX) + min;
238
239 return(result);
240 }
241
Q_flrand(float min,float max)242 float Q_flrand( float min, float max )
243 {
244 return flrand(min, max);
245 }
246
247 // Returns an integer min <= x <= max (ie inclusive)
irand(int min,int max)248 int irand( int min, int max )
249 {
250 int result;
251
252 assert((max - min) < QRAND_MAX);
253
254 max++;
255 holdrand = (holdrand * 214013L) + 2531011L;
256 result = holdrand >> 17;
257 result = ((result * (max - min)) >> 15) + min;
258
259 return result;
260 }
261
Q_irand(int value1,int value2)262 int Q_irand( int value1, int value2 )
263 {
264 return irand(value1, value2);
265 }
266
267 /*
268 erandom
269
270 This function produces a random number with a exponential
271 distribution and the specified mean value.
272 */
erandom(float mean)273 float erandom( float mean )
274 {
275 float r;
276
277 do {
278 r = Q_flrand(0.0f, 1.0f);
279 } while ( r == 0.0 );
280
281 return -mean * logf( r );
282 }
283
284
285 ///////////////////////////////////////////////////////////////////////////
286 //
287 // MATH UTILITIES
288 //
289 ///////////////////////////////////////////////////////////////////////////
ClampChar(int i)290 signed char ClampChar( int i )
291 {
292 if ( i < -128 ) {
293 return -128;
294 }
295 if ( i > 127 ) {
296 return 127;
297 }
298 return i;
299 }
300
ClampShort(int i)301 signed short ClampShort( int i )
302 {
303 if ( i < -32768 ) {
304 return -32768;
305 }
306 if ( i > 0x7fff ) {
307 return 0x7fff;
308 }
309 return i;
310 }
311
Com_Clampi(int min,int max,int value)312 int Com_Clampi( int min, int max, int value )
313 {
314 if ( value < min )
315 {
316 return min;
317 }
318 if ( value > max )
319 {
320 return max;
321 }
322 return value;
323 }
324
Com_Clamp(float min,float max,float value)325 float Com_Clamp( float min, float max, float value ) {
326 if ( value < min ) {
327 return min;
328 }
329 if ( value > max ) {
330 return max;
331 }
332 return value;
333 }
334
Com_AbsClampi(int min,int max,int value)335 int Com_AbsClampi( int min, int max, int value )
336 {
337 if( value < 0 )
338 {
339 return Com_Clampi( -max, -min, value );
340 }
341 else
342 {
343 return Com_Clampi( min, max, value );
344 }
345 }
346
Com_AbsClamp(float min,float max,float value)347 float Com_AbsClamp( float min, float max, float value )
348 {
349 if( value < 0.0f )
350 {
351 return Com_Clamp( -max, -min, value );
352 }
353 else
354 {
355 return Com_Clamp( min, max, value );
356 }
357 }
358
359
Q_rsqrt(float number)360 float Q_rsqrt( float number )
361 {
362 byteAlias_t t;
363 float x2, y;
364 const float threehalfs = 1.5F;
365
366 x2 = number * 0.5F;
367 y = number;
368 t.f = number;
369 t.i = 0x5f3759df - ( t.i >> 1 ); // what the fuck?
370 y = t.f;
371 y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
372 // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
373
374 assert( !Q_isnan(y) );
375 return y;
376 }
377
Q_fabs(float f)378 float Q_fabs( float f )
379 {
380 byteAlias_t fi;
381 fi.f = f;
382 fi.i &= 0x7FFFFFFF;
383 return fi.f;
384 }
385
386 /*
387 =====================
388 Q_acos
389
390 the msvc acos doesn't always return a value between -PI and PI:
391
392 int i;
393 i = 1065353246;
394 acos(*(float*) &i) == -1.#IND0
395
396 This should go in q_math but it is too late to add new traps
397 to game and ui
398 =====================
399 */
Q_acos(float c)400 float Q_acos(float c) {
401 float angle;
402
403 angle = acosf(c);
404
405 if (angle > M_PI) {
406 return (float)M_PI;
407 }
408 if (angle < -M_PI) {
409 return (float)M_PI;
410 }
411 return angle;
412 }
413
Q_asin(float c)414 float Q_asin(float c)
415 {
416 float angle;
417
418 angle = asinf(c);
419
420 if (angle > M_PI) {
421 return (float)M_PI;
422 }
423 if (angle < -M_PI) {
424 return (float)M_PI;
425 }
426 return angle;
427 }
428
Q_powf(float x,int y)429 float Q_powf ( float x, int y )
430 {
431 float r = x;
432 for ( y--; y>0; y-- )
433 r *= x;
434 return r;
435 }
436
Q_isnan(float f)437 qboolean Q_isnan (float f)
438 {
439 #ifdef _MSC_VER
440 return (qboolean)(_isnan (f) != 0);
441 #else
442 return (qboolean)(isnan (f) != 0);
443 #endif
444 }
445
Q_log2(int val)446 int Q_log2( int val )
447 {
448 int answer;
449
450 answer = 0;
451 while ( ( val>>=1 ) != 0 ) {
452 answer++;
453 }
454 return answer;
455 }
456
LerpAngle(float from,float to,float frac)457 float LerpAngle(float from, float to, float frac)
458 {
459 float a;
460
461 if ( to - from > 180 ) {
462 to -= 360;
463 }
464 if ( to - from < -180 ) {
465 to += 360;
466 }
467 a = from + frac * (to - from);
468
469 return a;
470 }
471
472 /*
473 =================
474 AngleSubtract
475
476 Always returns a value from -180 to 180
477 =================
478 */
AngleSubtract(float a1,float a2)479 float AngleSubtract( float a1, float a2 ) {
480 float a;
481
482 a = a1 - a2;
483 a=fmodf(a,360);//chop it down quickly, then level it out
484 while ( a > 180 ) {
485 a -= 360;
486 }
487 while ( a < -180 ) {
488 a += 360;
489 }
490 return a;
491 }
492
AnglesSubtract(vec3_t v1,vec3_t v2,vec3_t v3)493 void AnglesSubtract( vec3_t v1, vec3_t v2, vec3_t v3 ) {
494 v3[0] = AngleSubtract( v1[0], v2[0] );
495 v3[1] = AngleSubtract( v1[1], v2[1] );
496 v3[2] = AngleSubtract( v1[2], v2[2] );
497 }
498
AngleMod(float a)499 float AngleMod(float a) {
500 a = (360.0f/65536) * ((int)(a*(65536/360.0f)) & 65535);
501 return a;
502 }
503
504 /*
505 =================
506 AngleNormalize360
507
508 returns angle normalized to the range [0 <= angle < 360]
509 =================
510 */
AngleNormalize360(float angle)511 float AngleNormalize360 ( float angle ) {
512 return (360.0f / 65536) * ((int)(angle * (65536 / 360.0f)) & 65535);
513 }
514
515 /*
516 =================
517 AngleNormalize180
518
519 returns angle normalized to the range [-180 < angle <= 180]
520 =================
521 */
AngleNormalize180(float angle)522 float AngleNormalize180 ( float angle ) {
523 angle = AngleNormalize360( angle );
524 if ( angle > 180.0 ) {
525 angle -= 360.0;
526 }
527 return angle;
528 }
529
530 /*
531 =================
532 AngleDelta
533
534 returns the normalized delta from angle1 to angle2
535 =================
536 */
AngleDelta(float angle1,float angle2)537 float AngleDelta ( float angle1, float angle2 ) {
538 return AngleNormalize180( angle1 - angle2 );
539 }
540
541
542 ///////////////////////////////////////////////////////////////////////////
543 //
544 // GEOMETRIC UTILITIES
545 //
546 ///////////////////////////////////////////////////////////////////////////
547 /*
548 =====================
549 PlaneFromPoints
550
551 Returns false if the triangle is degenrate.
552 The normal will point out of the clock for clockwise ordered points
553 =====================
554 */
PlaneFromPoints(vec4_t plane,const vec3_t a,const vec3_t b,const vec3_t c)555 qboolean PlaneFromPoints( vec4_t plane, const vec3_t a, const vec3_t b, const vec3_t c ) {
556 vec3_t d1, d2;
557
558 VectorSubtract( b, a, d1 );
559 VectorSubtract( c, a, d2 );
560 CrossProduct( d2, d1, plane );
561 if ( VectorNormalize( plane ) == 0 ) {
562 return qfalse;
563 }
564
565 plane[3] = DotProduct( a, plane );
566 return qtrue;
567 }
568
569 /*
570 ===============
571 RotatePointAroundVector
572
573 From q3mme
574 ===============
575 */
RotatePointAroundVector(vec3_t dst,const vec3_t dir,const vec3_t point,float degrees)576 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees ) {
577 float m[3][3];
578 float c, s, t;
579
580 degrees = -DEG2RAD( degrees );
581 s = sinf( degrees );
582 c = cosf( degrees );
583 t = 1 - c;
584
585 m[0][0] = t*dir[0]*dir[0] + c;
586 m[0][1] = t*dir[0]*dir[1] + s*dir[2];
587 m[0][2] = t*dir[0]*dir[2] - s*dir[1];
588
589 m[1][0] = t*dir[0]*dir[1] - s*dir[2];
590 m[1][1] = t*dir[1]*dir[1] + c;
591 m[1][2] = t*dir[1]*dir[2] + s*dir[0];
592
593 m[2][0] = t*dir[0]*dir[2] + s*dir[1];
594 m[2][1] = t*dir[1]*dir[2] - s*dir[0];
595 m[2][2] = t*dir[2]*dir[2] + c;
596 VectorRotate( point, m, dst );
597 }
598
RotateAroundDirection(matrix3_t axis,float yaw)599 void RotateAroundDirection( matrix3_t axis, float yaw ) {
600
601 // create an arbitrary axis[1]
602 PerpendicularVector( axis[1], axis[0] );
603
604 // rotate it around axis[0] by yaw
605 if ( yaw ) {
606 vec3_t temp;
607
608 VectorCopy( axis[1], temp );
609 RotatePointAroundVector( axis[1], axis[0], temp, yaw );
610 }
611
612 // cross to get axis[2]
613 CrossProduct( axis[0], axis[1], axis[2] );
614 }
615
vectoangles(const vec3_t value1,vec3_t angles)616 void vectoangles( const vec3_t value1, vec3_t angles ) {
617 float forward;
618 float yaw, pitch;
619
620 if ( value1[1] == 0 && value1[0] == 0 ) {
621 yaw = 0;
622 if ( value1[2] > 0 ) {
623 pitch = 90;
624 }
625 else {
626 pitch = 270;
627 }
628 }
629 else {
630 if ( value1[0] ) {
631 yaw = ( atan2f ( value1[1], value1[0] ) * 180 / M_PI );
632 }
633 else if ( value1[1] > 0 ) {
634 yaw = 90;
635 }
636 else {
637 yaw = 270;
638 }
639 if ( yaw < 0 ) {
640 yaw += 360;
641 }
642
643 forward = sqrtf ( value1[0]*value1[0] + value1[1]*value1[1] );
644 pitch = ( atan2f(value1[2], forward) * 180 / M_PI );
645 if ( pitch < 0 ) {
646 pitch += 360;
647 }
648 }
649
650 angles[PITCH] = -pitch;
651 angles[YAW] = yaw;
652 angles[ROLL] = 0;
653 }
654
GetYawForDirection(const vec3_t p1,const vec3_t p2)655 vec_t GetYawForDirection( const vec3_t p1, const vec3_t p2 ) {
656 vec3_t v, angles;
657
658 VectorSubtract( p2, p1, v );
659 vectoangles( v, angles );
660
661 return angles[YAW];
662 }
663
GetAnglesForDirection(const vec3_t p1,const vec3_t p2,vec3_t out)664 void GetAnglesForDirection( const vec3_t p1, const vec3_t p2, vec3_t out ) {
665 vec3_t v;
666
667 VectorSubtract( p2, p1, v );
668 vectoangles( v, out );
669 }
670
ProjectPointOnPlane(vec3_t dst,const vec3_t p,const vec3_t normal)671 void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal )
672 {
673 float d;
674 vec3_t n;
675 float inv_denom;
676
677 inv_denom = DotProduct( normal, normal );
678 assert( Q_fabs(inv_denom) != 0.0f );
679 inv_denom = 1.0f / inv_denom;
680
681 d = DotProduct( normal, p ) * inv_denom;
682
683 n[0] = normal[0] * inv_denom;
684 n[1] = normal[1] * inv_denom;
685 n[2] = normal[2] * inv_denom;
686
687 dst[0] = p[0] - d * n[0];
688 dst[1] = p[1] - d * n[1];
689 dst[2] = p[2] - d * n[2];
690 }
691
G_FindClosestPointOnLineSegment(const vec3_t start,const vec3_t end,const vec3_t from,vec3_t result)692 qboolean G_FindClosestPointOnLineSegment( const vec3_t start, const vec3_t end, const vec3_t from, vec3_t result )
693 {
694 vec3_t vecStart2From, vecStart2End, vecEnd2Start, vecEnd2From;
695 float distEnd2From, distEnd2Result, theta, cos_theta, dot;
696
697 //Find the perpendicular vector to vec from start to end
698 VectorSubtract( from, start, vecStart2From);
699 VectorSubtract( end, start, vecStart2End);
700
701 dot = DotProductNormalize( vecStart2From, vecStart2End );
702
703 if ( dot <= 0 )
704 {
705 //The perpendicular would be beyond or through the start point
706 VectorCopy( start, result );
707 return qfalse;
708 }
709
710 if ( dot == 1 )
711 {
712 //parallel, closer of 2 points will be the target
713 if( (VectorLengthSquared( vecStart2From )) < (VectorLengthSquared( vecStart2End )) )
714 {
715 VectorCopy( from, result );
716 }
717 else
718 {
719 VectorCopy( end, result );
720 }
721 return qfalse;
722 }
723
724 //Try other end
725 VectorSubtract( from, end, vecEnd2From);
726 VectorSubtract( start, end, vecEnd2Start);
727
728 dot = DotProductNormalize( vecEnd2From, vecEnd2Start );
729
730 if ( dot <= 0 )
731 {//The perpendicular would be beyond or through the start point
732 VectorCopy( end, result );
733 return qfalse;
734 }
735
736 if ( dot == 1 )
737 {//parallel, closer of 2 points will be the target
738 if( (VectorLengthSquared( vecEnd2From )) < (VectorLengthSquared( vecEnd2Start )))
739 {
740 VectorCopy( from, result );
741 }
742 else
743 {
744 VectorCopy( end, result );
745 }
746 return qfalse;
747 }
748
749 // /|
750 // c / |
751 // / |a
752 // theta /)__|
753 // b
754 //cos(theta) = b / c
755 //solve for b
756 //b = cos(theta) * c
757
758 //angle between vecs end2from and end2start, should be between 0 and 90
759 theta = 90 * (1 - dot);//theta
760
761 //Get length of side from End2Result using sine of theta
762 distEnd2From = VectorLength( vecEnd2From );//c
763 cos_theta = cosf(DEG2RAD(theta));//cos(theta)
764 distEnd2Result = cos_theta * distEnd2From;//b
765
766 //Extrapolate to find result
767 VectorNormalize( vecEnd2Start );
768 VectorMA( end, distEnd2Result, vecEnd2Start, result );
769
770 //perpendicular intersection is between the 2 endpoints
771 return qtrue;
772 }
773
G_PointDistFromLineSegment(const vec3_t start,const vec3_t end,const vec3_t from)774 float G_PointDistFromLineSegment( const vec3_t start, const vec3_t end, const vec3_t from )
775 {
776 vec3_t vecStart2From, vecStart2End, vecEnd2Start, vecEnd2From, intersection;
777 float distEnd2From, distStart2From, distEnd2Result, theta, cos_theta, dot;
778
779 //Find the perpendicular vector to vec from start to end
780 VectorSubtract( from, start, vecStart2From);
781 VectorSubtract( end, start, vecStart2End);
782 VectorSubtract( from, end, vecEnd2From);
783 VectorSubtract( start, end, vecEnd2Start);
784
785 dot = DotProductNormalize( vecStart2From, vecStart2End );
786
787 distStart2From = Distance( start, from );
788 distEnd2From = Distance( end, from );
789
790 if ( dot <= 0 )
791 {
792 //The perpendicular would be beyond or through the start point
793 return distStart2From;
794 }
795
796 if ( dot == 1 )
797 {
798 //parallel, closer of 2 points will be the target
799 return ((distStart2From<distEnd2From)?distStart2From:distEnd2From);
800 }
801
802 //Try other end
803
804 dot = DotProductNormalize( vecEnd2From, vecEnd2Start );
805
806 if ( dot <= 0 )
807 {//The perpendicular would be beyond or through the end point
808 return distEnd2From;
809 }
810
811 if ( dot == 1 )
812 {//parallel, closer of 2 points will be the target
813 return ((distStart2From<distEnd2From)?distStart2From:distEnd2From);
814 }
815
816 // /|
817 // c / |
818 // / |a
819 // theta /)__|
820 // b
821 //cos(theta) = b / c
822 //solve for b
823 //b = cos(theta) * c
824
825 //angle between vecs end2from and end2start, should be between 0 and 90
826 theta = 90 * (1 - dot);//theta
827
828 //Get length of side from End2Result using sine of theta
829 cos_theta = cosf(DEG2RAD(theta));//cos(theta)
830 distEnd2Result = cos_theta * distEnd2From;//b
831
832 //Extrapolate to find result
833 VectorNormalize( vecEnd2Start );
834 VectorMA( end, distEnd2Result, vecEnd2Start, intersection );
835
836 //perpendicular intersection is between the 2 endpoints, return dist to it from from
837 return Distance( intersection, from );
838 }
839
MatrixMultiply(float in1[3][3],float in2[3][3],float out[3][3])840 void MatrixMultiply(float in1[3][3], float in2[3][3], float out[3][3]) {
841 out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
842 in1[0][2] * in2[2][0];
843 out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
844 in1[0][2] * in2[2][1];
845 out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
846 in1[0][2] * in2[2][2];
847 out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
848 in1[1][2] * in2[2][0];
849 out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
850 in1[1][2] * in2[2][1];
851 out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
852 in1[1][2] * in2[2][2];
853 out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
854 in1[2][2] * in2[2][0];
855 out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
856 in1[2][2] * in2[2][1];
857 out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
858 in1[2][2] * in2[2][2];
859 }
860
861
862 ///////////////////////////////////////////////////////////////////////////
863 //
864 // BOUNDING BOX
865 //
866 ///////////////////////////////////////////////////////////////////////////
RadiusFromBounds(const vec3_t mins,const vec3_t maxs)867 float RadiusFromBounds( const vec3_t mins, const vec3_t maxs ) {
868 int i;
869 vec3_t corner;
870 float a, b;
871
872 for (i=0 ; i<3 ; i++) {
873 a = fabsf( mins[i] );
874 b = fabsf( maxs[i] );
875 corner[i] = a > b ? a : b;
876 }
877
878 return VectorLength (corner);
879 }
880
ClearBounds(vec3_t mins,vec3_t maxs)881 void ClearBounds( vec3_t mins, vec3_t maxs ) {
882 mins[0] = mins[1] = mins[2] = 100000;
883 maxs[0] = maxs[1] = maxs[2] = -100000;
884 }
885
AddPointToBounds(const vec3_t v,vec3_t mins,vec3_t maxs)886 void AddPointToBounds( const vec3_t v, vec3_t mins, vec3_t maxs ) {
887 if ( v[0] < mins[0] ) {
888 mins[0] = v[0];
889 }
890 if ( v[0] > maxs[0]) {
891 maxs[0] = v[0];
892 }
893
894 if ( v[1] < mins[1] ) {
895 mins[1] = v[1];
896 }
897 if ( v[1] > maxs[1]) {
898 maxs[1] = v[1];
899 }
900
901 if ( v[2] < mins[2] ) {
902 mins[2] = v[2];
903 }
904 if ( v[2] > maxs[2]) {
905 maxs[2] = v[2];
906 }
907 }
908
909
910 ///////////////////////////////////////////////////////////////////////////
911 //
912 // PLANE
913 //
914 ///////////////////////////////////////////////////////////////////////////
SetPlaneSignbits(cplane_t * out)915 void SetPlaneSignbits( cplane_t *out )
916 {
917 int bits, j;
918
919 // for fast box on planeside test
920 bits = 0;
921 for (j=0 ; j<3 ; j++) {
922 if (out->normal[j] < 0) {
923 bits |= 1<<j;
924 }
925 }
926 out->signbits = bits;
927 }
928
PlaneTypeForNormal(vec3_t normal)929 int PlaneTypeForNormal( vec3_t normal )
930 {
931 if ( normal[0] == 1.0 )
932 return PLANE_X;
933 if ( normal[1] == 1.0 )
934 return PLANE_Y;
935 if ( normal[2] == 1.0 )
936 return PLANE_Z;
937
938 return PLANE_NON_AXIAL;
939 }
940
941 /*
942 ==================
943 BoxOnPlaneSide
944
945 Returns 1, 2, or 1 + 2
946 ==================
947 */
BoxOnPlaneSide(vec3_t emins,vec3_t emaxs,cplane_t * p)948 int BoxOnPlaneSide(vec3_t emins, vec3_t emaxs, cplane_t *p)
949 {
950 float dist[2];
951 int sides, b, i;
952
953 // fast axial cases
954 if (p->type < 3)
955 {
956 if (p->dist <= emins[p->type])
957 return 1;
958 if (p->dist >= emaxs[p->type])
959 return 2;
960 return 3;
961 }
962
963 // general case
964 dist[0] = dist[1] = 0;
965 if (p->signbits < 8) // >= 8: default case is original code (dist[0]=dist[1]=0)
966 {
967 for (i=0 ; i<3 ; i++)
968 {
969 b = (p->signbits >> i) & 1;
970 dist[ b] += p->normal[i]*emaxs[i];
971 dist[!b] += p->normal[i]*emins[i];
972 }
973 }
974
975 sides = 0;
976 if (dist[0] >= p->dist)
977 sides = 1;
978 if (dist[1] < p->dist)
979 sides |= 2;
980
981 return sides;
982 }
983
984
985 ///////////////////////////////////////////////////////////////////////////
986 //
987 // AXIS
988 //
989 ///////////////////////////////////////////////////////////////////////////
990 matrix3_t axisDefault = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
991
AxisClear(matrix3_t axis)992 void AxisClear( matrix3_t axis ) {
993 axis[0][0] = 1;
994 axis[0][1] = 0;
995 axis[0][2] = 0;
996 axis[1][0] = 0;
997 axis[1][1] = 1;
998 axis[1][2] = 0;
999 axis[2][0] = 0;
1000 axis[2][1] = 0;
1001 axis[2][2] = 1;
1002 }
1003
AxisCopy(matrix3_t in,matrix3_t out)1004 void AxisCopy( matrix3_t in, matrix3_t out ) {
1005 VectorCopy( in[0], out[0] );
1006 VectorCopy( in[1], out[1] );
1007 VectorCopy( in[2], out[2] );
1008 }
1009
AnglesToAxis(const vec3_t angles,matrix3_t axis)1010 void AnglesToAxis( const vec3_t angles, matrix3_t axis ) {
1011 vec3_t right;
1012
1013 // angle vectors returns "right" instead of "y axis"
1014 AngleVectors( angles, axis[0], right, axis[2] );
1015 VectorSubtract( vec3_origin, right, axis[1] );
1016 }
1017
1018
1019 ///////////////////////////////////////////////////////////////////////////
1020 //
1021 // VEC2
1022 //
1023 ///////////////////////////////////////////////////////////////////////////
1024 vec2_t vec2_zero = {0,0};
1025
VectorAdd2(const vec2_t vec1,const vec2_t vec2,vec2_t vecOut)1026 void VectorAdd2( const vec2_t vec1, const vec2_t vec2, vec2_t vecOut )
1027 {
1028 vecOut[0] = vec1[0]+vec2[0];
1029 vecOut[1] = vec1[1]+vec2[1];
1030 }
1031
VectorSubtract2(const vec2_t vec1,const vec2_t vec2,vec2_t vecOut)1032 void VectorSubtract2( const vec2_t vec1, const vec2_t vec2, vec2_t vecOut )
1033 {
1034 vecOut[0] = vec1[0]-vec2[0];
1035 vecOut[1] = vec1[1]-vec2[1];
1036 }
1037
VectorScale2(const vec2_t vecIn,float scale,vec2_t vecOut)1038 void VectorScale2( const vec2_t vecIn, float scale, vec2_t vecOut )
1039 {
1040 vecOut[0] = vecIn[0]*scale;
1041 vecOut[1] = vecIn[1]*scale;
1042 }
1043
VectorMA2(const vec2_t vec1,float scale,const vec2_t vec2,vec2_t vecOut)1044 void VectorMA2( const vec2_t vec1, float scale, const vec2_t vec2, vec2_t vecOut )
1045 {
1046 vecOut[0] = vec1[0] + scale*vec2[0];
1047 vecOut[1] = vec1[1] + scale*vec2[1];
1048 }
1049
VectorSet2(vec2_t vec,float x,float y)1050 void VectorSet2( vec2_t vec, float x, float y )
1051 {
1052 vec[0]=x; vec[1]=y;
1053 }
1054
VectorClear2(vec2_t vec)1055 void VectorClear2( vec2_t vec )
1056 {
1057 vec[0] = vec[1] = 0.0f;
1058 }
1059
VectorCopy2(const vec2_t vecIn,vec2_t vecOut)1060 void VectorCopy2( const vec2_t vecIn, vec2_t vecOut )
1061 {
1062 vecOut[0] = vecIn[0];
1063 vecOut[1] = vecIn[1];
1064 }
1065
1066
1067 ///////////////////////////////////////////////////////////////////////////
1068 //
1069 // VEC3
1070 //
1071 ///////////////////////////////////////////////////////////////////////////
1072 vec3_t vec3_origin = {0,0,0};
1073
VectorAdd(const vec3_t vec1,const vec3_t vec2,vec3_t vecOut)1074 void VectorAdd( const vec3_t vec1, const vec3_t vec2, vec3_t vecOut )
1075 {
1076 vecOut[0] = vec1[0]+vec2[0];
1077 vecOut[1] = vec1[1]+vec2[1];
1078 vecOut[2] = vec1[2]+vec2[2];
1079 }
1080
VectorSubtract(const vec3_t vec1,const vec3_t vec2,vec3_t vecOut)1081 void VectorSubtract( const vec3_t vec1, const vec3_t vec2, vec3_t vecOut )
1082 {
1083 vecOut[0] = vec1[0]-vec2[0];
1084 vecOut[1] = vec1[1]-vec2[1];
1085 vecOut[2] = vec1[2]-vec2[2];
1086 }
1087
VectorScale(const vec3_t vecIn,float scale,vec3_t vecOut)1088 void VectorScale( const vec3_t vecIn, float scale, vec3_t vecOut )
1089 {
1090 vecOut[0] = vecIn[0]*scale;
1091 vecOut[1] = vecIn[1]*scale;
1092 vecOut[2] = vecIn[2]*scale;
1093 }
1094
VectorMA(const vec3_t vec1,float scale,const vec3_t vec2,vec3_t vecOut)1095 void VectorMA( const vec3_t vec1, float scale, const vec3_t vec2, vec3_t vecOut )
1096 {
1097 vecOut[0] = vec1[0] + scale*vec2[0];
1098 vecOut[1] = vec1[1] + scale*vec2[1];
1099 vecOut[2] = vec1[2] + scale*vec2[2];
1100 }
1101
VectorSet(vec3_t vec,float x,float y,float z)1102 void VectorSet( vec3_t vec, float x, float y, float z )
1103 {
1104 vec[0]=x; vec[1]=y; vec[2]=z;
1105 }
1106
VectorClear(vec3_t vec)1107 void VectorClear( vec3_t vec )
1108 {
1109 vec[0] = vec[1] = vec[2] = 0.0f;
1110 }
1111
VectorCopy(const vec3_t vecIn,vec3_t vecOut)1112 void VectorCopy( const vec3_t vecIn, vec3_t vecOut )
1113 {
1114 vecOut[0] = vecIn[0];
1115 vecOut[1] = vecIn[1];
1116 vecOut[2] = vecIn[2];
1117 }
1118
VectorLength(const vec3_t vec)1119 float VectorLength( const vec3_t vec )
1120 {
1121 return (float)sqrt( vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2] );
1122 }
1123
VectorLengthSquared(const vec3_t vec)1124 float VectorLengthSquared( const vec3_t vec )
1125 {
1126 return (vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]);
1127 }
1128
Distance(const vec3_t p1,const vec3_t p2)1129 float Distance( const vec3_t p1, const vec3_t p2 )
1130 {
1131 vec3_t v;
1132
1133 VectorSubtract( p2, p1, v );
1134 return VectorLength( v );
1135 }
1136
DistanceSquared(const vec3_t p1,const vec3_t p2)1137 float DistanceSquared( const vec3_t p1, const vec3_t p2 )
1138 {
1139 vec3_t v;
1140
1141 VectorSubtract( p2, p1, v );
1142 return v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
1143 }
1144
1145 // fast vector normalize routine that does not check to make sure
1146 // that length != 0, nor does it return length, uses rsqrt approximation
VectorNormalizeFast(vec3_t vec)1147 void VectorNormalizeFast( vec3_t vec )
1148 {
1149 float ilength;
1150
1151 ilength = Q_rsqrt( DotProduct( vec, vec ) );
1152
1153 vec[0] *= ilength;
1154 vec[1] *= ilength;
1155 vec[2] *= ilength;
1156 }
1157
VectorNormalize(vec3_t vec)1158 float VectorNormalize( vec3_t vec )
1159 {
1160 float length, ilength;
1161
1162 length = vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2];
1163 length = sqrtf( length );
1164
1165 if ( length ) {
1166 ilength = 1/length;
1167 vec[0] *= ilength;
1168 vec[1] *= ilength;
1169 vec[2] *= ilength;
1170 }
1171
1172 return length;
1173 }
1174
VectorNormalize2(const vec3_t vec,vec3_t vecOut)1175 float VectorNormalize2( const vec3_t vec, vec3_t vecOut )
1176 {
1177 float length, ilength;
1178
1179 length = vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2];
1180 length = sqrtf( length );
1181
1182 if ( length ) {
1183 ilength = 1/length;
1184 vecOut[0] = vec[0]*ilength;
1185 vecOut[1] = vec[1]*ilength;
1186 vecOut[2] = vec[2]*ilength;
1187 }
1188 else
1189 VectorClear( vecOut );
1190
1191 return length;
1192 }
1193
VectorAdvance(const vec3_t veca,const float scale,const vec3_t vecb,vec3_t vecc)1194 void VectorAdvance( const vec3_t veca, const float scale, const vec3_t vecb, vec3_t vecc)
1195 {
1196 vecc[0] = veca[0] + (scale * (vecb[0] - veca[0]));
1197 vecc[1] = veca[1] + (scale * (vecb[1] - veca[1]));
1198 vecc[2] = veca[2] + (scale * (vecb[2] - veca[2]));
1199 }
1200
VectorInc(vec3_t vec)1201 void VectorInc( vec3_t vec ) {
1202 vec[0] += 1.0f; vec[1] += 1.0f; vec[2] += 1.0f;
1203 }
1204
VectorDec(vec3_t vec)1205 void VectorDec( vec3_t vec ) {
1206 vec[0] -= 1.0f; vec[1] -= 1.0f; vec[2] -= 1.0f;
1207 }
1208
VectorInverse(vec3_t vec)1209 void VectorInverse( vec3_t vec ) {
1210 vec[0] = -vec[0]; vec[1] = -vec[1]; vec[2] = -vec[2];
1211 }
1212
CrossProduct(const vec3_t vec1,const vec3_t vec2,vec3_t vecOut)1213 void CrossProduct( const vec3_t vec1, const vec3_t vec2, vec3_t vecOut ) {
1214 vecOut[0] = vec1[1]*vec2[2] - vec1[2]*vec2[1];
1215 vecOut[1] = vec1[2]*vec2[0] - vec1[0]*vec2[2];
1216 vecOut[2] = vec1[0]*vec2[1] - vec1[1]*vec2[0];
1217 }
1218
DotProduct(const vec3_t vec1,const vec3_t vec2)1219 float DotProduct( const vec3_t vec1, const vec3_t vec2 ) {
1220 return vec1[0]*vec2[0] + vec1[1]*vec2[1] + vec1[2]*vec2[2];
1221 }
1222
VectorCompare(const vec3_t vec1,const vec3_t vec2)1223 qboolean VectorCompare( const vec3_t vec1, const vec3_t vec2 )
1224 {
1225 return (qboolean)(vec1[0] == vec2[0] && vec1[1] == vec2[1] && vec1[2] == vec2[2]);
1226 }
1227
VectorCompare2(const vec3_t v1,const vec3_t v2)1228 qboolean VectorCompare2( const vec3_t v1, const vec3_t v2 )
1229 {
1230 if ( v1[0] > (v2[0] + 0.0001f) || v1[0] < (v2[0] - 0.0001f) ||
1231 v1[1] > (v2[1] + 0.0001f) || v1[1] < (v2[1] + 0.0001f) ||
1232 v1[2] > (v2[2] + 0.0001f) || v1[2] < (v2[2] + 0.0001f) )
1233 {
1234 return qfalse;
1235 }
1236
1237 return qtrue;
1238 }
1239
SnapVector(float * v)1240 void SnapVector( float *v )
1241 {
1242 #if defined(_MSC_VER) && !defined(idx64)
1243 // pitiful attempt to reduce _ftol2 calls -rww
1244 static int i;
1245 static float f;
1246
1247 f = *v;
1248 __asm fld f
1249 __asm fistp i
1250 *v = (float)i;
1251 v++;
1252 f = *v;
1253 __asm fld f
1254 __asm fistp i
1255 *v = (float)i;
1256 v++;
1257 f = *v;
1258 __asm fld f
1259 __asm fistp i
1260 *v = (float)i;
1261 #else // mac, linux, mingw
1262 v[0] = (int)v[0];
1263 v[1] = (int)v[1];
1264 v[2] = (int)v[2];
1265 #endif
1266 }
1267
DistanceHorizontal(const vec3_t p1,const vec3_t p2)1268 float DistanceHorizontal( const vec3_t p1, const vec3_t p2 )
1269 {
1270 vec3_t v;
1271
1272 VectorSubtract( p2, p1, v );
1273 return sqrtf( v[0]*v[0] + v[1]*v[1] ); //Leave off the z component
1274 }
1275
DistanceHorizontalSquared(const vec3_t p1,const vec3_t p2)1276 float DistanceHorizontalSquared( const vec3_t p1, const vec3_t p2 )
1277 {
1278 vec3_t v;
1279
1280 VectorSubtract( p2, p1, v );
1281 return v[0]*v[0] + v[1]*v[1]; //Leave off the z component
1282 }
1283
1284 /*
1285 ================
1286 MakeNormalVectors
1287
1288 Given a normalized forward vector, create two
1289 other perpendicular vectors
1290 ================
1291 */
MakeNormalVectors(const vec3_t forward,vec3_t right,vec3_t up)1292 void MakeNormalVectors( const vec3_t forward, vec3_t right, vec3_t up) {
1293 float d;
1294
1295 // this rotate and negate guarantees a vector
1296 // not colinear with the original
1297 right[1] = -forward[0];
1298 right[2] = forward[1];
1299 right[0] = forward[2];
1300
1301 d = DotProduct(right, forward);
1302 VectorMA(right, -d, forward, right);
1303 VectorNormalize (right);
1304 CrossProduct (right, forward, up);
1305 }
1306
VectorRotate(const vec3_t in,matrix3_t matrix,vec3_t out)1307 void VectorRotate( const vec3_t in, matrix3_t matrix, vec3_t out )
1308 {
1309 out[0] = DotProduct( in, matrix[0] );
1310 out[1] = DotProduct( in, matrix[1] );
1311 out[2] = DotProduct( in, matrix[2] );
1312 }
1313
AngleVectors(const vec3_t angles,vec3_t forward,vec3_t right,vec3_t up)1314 void AngleVectors( const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up) {
1315 float angle;
1316 static float sr, sp, sy, cr, cp, cy;
1317 // static to help MS compiler fp bugs
1318
1319 angle = angles[YAW] * (M_PI*2 / 360);
1320 sy = sinf(angle);
1321 cy = cosf(angle);
1322 angle = angles[PITCH] * (M_PI*2 / 360);
1323 sp = sinf(angle);
1324 cp = cosf(angle);
1325 angle = angles[ROLL] * (M_PI*2 / 360);
1326 sr = sinf(angle);
1327 cr = cosf(angle);
1328
1329 if (forward)
1330 {
1331 forward[0] = cp*cy;
1332 forward[1] = cp*sy;
1333 forward[2] = -sp;
1334 }
1335 if (right)
1336 {
1337 right[0] = (-1*sr*sp*cy+-1*cr*-sy);
1338 right[1] = (-1*sr*sp*sy+-1*cr*cy);
1339 right[2] = -1*sr*cp;
1340 }
1341 if (up)
1342 {
1343 up[0] = (cr*sp*cy+-sr*-sy);
1344 up[1] = (cr*sp*sy+-sr*cy);
1345 up[2] = cr*cp;
1346 }
1347 }
1348
1349 /*
1350 ** assumes "src" is normalized
1351 */
PerpendicularVector(vec3_t dst,const vec3_t src)1352 void PerpendicularVector( vec3_t dst, const vec3_t src )
1353 {
1354 int pos;
1355 int i;
1356 float minelem = 1.0F;
1357 vec3_t tempvec;
1358
1359 /*
1360 ** find the smallest magnitude axially aligned vector
1361 */
1362 for ( pos = 0, i = 0; i < 3; i++ )
1363 {
1364 if ( fabs( src[i] ) < minelem )
1365 {
1366 pos = i;
1367 minelem = fabsf( src[i] );
1368 }
1369 }
1370 tempvec[0] = tempvec[1] = tempvec[2] = 0.0F;
1371 tempvec[pos] = 1.0F;
1372
1373 /*
1374 ** project the point onto the plane defined by src
1375 */
1376 ProjectPointOnPlane( dst, tempvec, src );
1377
1378 /*
1379 ** normalize the result
1380 */
1381 VectorNormalize( dst );
1382 }
1383
DotProductNormalize(const vec3_t inVec1,const vec3_t inVec2)1384 float DotProductNormalize( const vec3_t inVec1, const vec3_t inVec2 )
1385 {
1386 vec3_t v1, v2;
1387
1388 VectorNormalize2( inVec1, v1 );
1389 VectorNormalize2( inVec2, v2 );
1390
1391 return DotProduct(v1, v2);
1392 }
1393
1394
1395 ///////////////////////////////////////////////////////////////////////////
1396 //
1397 // VEC4
1398 //
1399 ///////////////////////////////////////////////////////////////////////////
VectorScale4(const vec4_t vecIn,float scale,vec4_t vecOut)1400 void VectorScale4( const vec4_t vecIn, float scale, vec4_t vecOut )
1401 {
1402 vecOut[0] = vecIn[0]*scale;
1403 vecOut[1] = vecIn[1]*scale;
1404 vecOut[2] = vecIn[2]*scale;
1405 vecOut[3] = vecIn[3]*scale;
1406 }
1407
VectorCopy4(const vec4_t vecIn,vec4_t vecOut)1408 void VectorCopy4( const vec4_t vecIn, vec4_t vecOut )
1409 {
1410 vecOut[0] = vecIn[0];
1411 vecOut[1] = vecIn[1];
1412 vecOut[2] = vecIn[2];
1413 vecOut[3] = vecIn[3];
1414 }
1415
VectorSet4(vec4_t vec,float x,float y,float z,float w)1416 void VectorSet4( vec4_t vec, float x, float y, float z, float w )
1417 {
1418 vec[0]=x; vec[1]=y; vec[2]=z; vec[3]=w;
1419 }
1420
VectorClear4(vec4_t vec)1421 void VectorClear4( vec4_t vec )
1422 {
1423 vec[0] = vec[1] = vec[2] = vec[3] = 0;
1424 }
1425
1426 ///////////////////////////////////////////////////////////////////////////
1427 //
1428 // VEC5
1429 //
1430 ///////////////////////////////////////////////////////////////////////////
VectorSet5(vec5_t vec,float x,float y,float z,float w,float u)1431 void VectorSet5( vec5_t vec, float x, float y, float z, float w, float u ) {
1432 vec[0]=x; vec[1]=y; vec[2]=z; vec[3]=w; vec[4]=u;
1433 }
1434