Debian Codegraph @

code/qcommon/q_math.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 //
23 // q_math.c -- stateless support routines that are included in each code module
24
25 // Some of the vector functions are static inline in q_shared.h. q3asm
26 // doesn't understand static functions though, so we only want them in
27 // one file. That's what this is about.
28 #ifdef Q3_VM
29 #define __Q3_VM_MATH
30 #endif
31
32 #include "q_shared.h"
33
34 vec3_t vec3_origin = {0.0f, 0.0f, 0.0f};
35 vec3_t axisDefault[3] =
36 {
37 {1.0f, 0.0f, 0.0f},
38 {0.0f, 1.0f, 0.0f},
39 {0.0f, 0.0f, 1.0f}
40 };
41
42
43 vec4_t colorBlack = {0.0f, 0.0f, 0.0f, 1.0f};
44 vec4_t colorRed = {1.0f, 0.0f, 0.0f, 1.0f};
45 vec4_t colorGreen = {0.0f, 1.0f, 0.0f, 1.0f};
46 vec4_t colorBlue = {0.0f, 0.0f, 1.0f, 1.0f};
47 vec4_t colorYellow = {1.0f, 1.0f, 0.0f, 1.0f};
48 vec4_t colorMagenta= {1.0f, 0.0f, 1.0f, 1.0f};
49 vec4_t colorCyan = {0.0f, 1.0f, 1.0f, 1.0f};
50 vec4_t colorWhite = {1.0f, 1.0f, 1.0f, 1.0f};
51 vec4_t colorLtGrey = {0.75f, 0.75f, 0.75f, 1.0f};
52 vec4_t colorMdGrey = {0.5f, 0.5f, 0.5f, 1.0f};
53 vec4_t colorDkGrey = {0.25f, 0.25f, 0.25f, 1.0f};
54
55 vec4_t g_color_table[8] =
56 {
57 {0.0f, 0.0f, 0.0f, 1.0f},
58 {1.0f, 0.0f, 0.0f, 1.0f},
59 {0.0f, 1.0f, 0.0f, 1.0f},
60 {1.0f, 1.0f, 0.0f, 1.0f},
61 {0.0f, 0.0f, 1.0f, 1.0f},
62 {0.0f, 1.0f, 1.0f, 1.0f},
63 {1.0f, 0.0f, 1.0f, 1.0f},
64 {1.0f, 1.0f, 1.0f, 1.0f},
65 };
66
67
68 vec3_t bytedirs[NUMVERTEXNORMALS] =
69 {
70 {-0.525731f, 0.000000f, 0.850651f}, {-0.442863f, 0.238856f, 0.864188f},
71 {-0.295242f, 0.000000f, 0.955423f}, {-0.309017f, 0.500000f, 0.809017f},
72 {-0.162460f, 0.262866f, 0.951056f}, {0.000000f, 0.000000f, 1.000000f},
73 {0.000000f, 0.850651f, 0.525731f}, {-0.147621f, 0.716567f, 0.681718f},
74 {0.147621f, 0.716567f, 0.681718f}, {0.000000f, 0.525731f, 0.850651f},
75 {0.309017f, 0.500000f, 0.809017f}, {0.525731f, 0.000000f, 0.850651f},
76 {0.295242f, 0.000000f, 0.955423f}, {0.442863f, 0.238856f, 0.864188f},
77 {0.162460f, 0.262866f, 0.951056f}, {-0.681718f, 0.147621f, 0.716567f},
78 {-0.809017f, 0.309017f, 0.500000f},{-0.587785f, 0.425325f, 0.688191f},
79 {-0.850651f, 0.525731f, 0.000000f},{-0.864188f, 0.442863f, 0.238856f},
80 {-0.716567f, 0.681718f, 0.147621f},{-0.688191f, 0.587785f, 0.425325f},
81 {-0.500000f, 0.809017f, 0.309017f}, {-0.238856f, 0.864188f, 0.442863f},
82 {-0.425325f, 0.688191f, 0.587785f}, {-0.716567f, 0.681718f, -0.147621f},
83 {-0.500000f, 0.809017f, -0.309017f}, {-0.525731f, 0.850651f, 0.000000f},
84 {0.000000f, 0.850651f, -0.525731f}, {-0.238856f, 0.864188f, -0.442863f},
85 {0.000000f, 0.955423f, -0.295242f}, {-0.262866f, 0.951056f, -0.162460f},
86 {0.000000f, 1.000000f, 0.000000f}, {0.000000f, 0.955423f, 0.295242f},
87 {-0.262866f, 0.951056f, 0.162460f}, {0.238856f, 0.864188f, 0.442863f},
88 {0.262866f, 0.951056f, 0.162460f}, {0.500000f, 0.809017f, 0.309017f},
89 {0.238856f, 0.864188f, -0.442863f},{0.262866f, 0.951056f, -0.162460f},
90 {0.500000f, 0.809017f, -0.309017f},{0.850651f, 0.525731f, 0.000000f},
91 {0.716567f, 0.681718f, 0.147621f}, {0.716567f, 0.681718f, -0.147621f},
92 {0.525731f, 0.850651f, 0.000000f}, {0.425325f, 0.688191f, 0.587785f},
93 {0.864188f, 0.442863f, 0.238856f}, {0.688191f, 0.587785f, 0.425325f},
94 {0.809017f, 0.309017f, 0.500000f}, {0.681718f, 0.147621f, 0.716567f},
95 {0.587785f, 0.425325f, 0.688191f}, {0.955423f, 0.295242f, 0.000000f},
96 {1.000000f, 0.000000f, 0.000000f}, {0.951056f, 0.162460f, 0.262866f},
97 {0.850651f, -0.525731f, 0.000000f},{0.955423f, -0.295242f, 0.000000f},
98 {0.864188f, -0.442863f, 0.238856f}, {0.951056f, -0.162460f, 0.262866f},
99 {0.809017f, -0.309017f, 0.500000f}, {0.681718f, -0.147621f, 0.716567f},
100 {0.850651f, 0.000000f, 0.525731f}, {0.864188f, 0.442863f, -0.238856f},
101 {0.809017f, 0.309017f, -0.500000f}, {0.951056f, 0.162460f, -0.262866f},
102 {0.525731f, 0.000000f, -0.850651f}, {0.681718f, 0.147621f, -0.716567f},
103 {0.681718f, -0.147621f, -0.716567f},{0.850651f, 0.000000f, -0.525731f},
104 {0.809017f, -0.309017f, -0.500000f}, {0.864188f, -0.442863f, -0.238856f},
105 {0.951056f, -0.162460f, -0.262866f}, {0.147621f, 0.716567f, -0.681718f},
106 {0.309017f, 0.500000f, -0.809017f}, {0.425325f, 0.688191f, -0.587785f},
107 {0.442863f, 0.238856f, -0.864188f}, {0.587785f, 0.425325f, -0.688191f},
108 {0.688191f, 0.587785f, -0.425325f}, {-0.147621f, 0.716567f, -0.681718f},
109 {-0.309017f, 0.500000f, -0.809017f}, {0.000000f, 0.525731f, -0.850651f},
110 {-0.525731f, 0.000000f, -0.850651f}, {-0.442863f, 0.238856f, -0.864188f},
111 {-0.295242f, 0.000000f, -0.955423f}, {-0.162460f, 0.262866f, -0.951056f},
112 {0.000000f, 0.000000f, -1.000000f}, {0.295242f, 0.000000f, -0.955423f},
113 {0.162460f, 0.262866f, -0.951056f}, {-0.442863f, -0.238856f, -0.864188f},
114 {-0.309017f, -0.500000f, -0.809017f}, {-0.162460f, -0.262866f, -0.951056f},
115 {0.000000f, -0.850651f, -0.525731f}, {-0.147621f, -0.716567f, -0.681718f},
116 {0.147621f, -0.716567f, -0.681718f}, {0.000000f, -0.525731f, -0.850651f},
117 {0.309017f, -0.500000f, -0.809017f}, {0.442863f, -0.238856f, -0.864188f},
118 {0.162460f, -0.262866f, -0.951056f}, {0.238856f, -0.864188f, -0.442863f},
119 {0.500000f, -0.809017f, -0.309017f}, {0.425325f, -0.688191f, -0.587785f},
120 {0.716567f, -0.681718f, -0.147621f}, {0.688191f, -0.587785f, -0.425325f},
121 {0.587785f, -0.425325f, -0.688191f}, {0.000000f, -0.955423f, -0.295242f},
122 {0.000000f, -1.000000f, 0.000000f}, {0.262866f, -0.951056f, -0.162460f},
123 {0.000000f, -0.850651f, 0.525731f}, {0.000000f, -0.955423f, 0.295242f},
124 {0.238856f, -0.864188f, 0.442863f}, {0.262866f, -0.951056f, 0.162460f},
125 {0.500000f, -0.809017f, 0.309017f}, {0.716567f, -0.681718f, 0.147621f},
126 {0.525731f, -0.850651f, 0.000000f}, {-0.238856f, -0.864188f, -0.442863f},
127 {-0.500000f, -0.809017f, -0.309017f}, {-0.262866f, -0.951056f, -0.162460f},
128 {-0.850651f, -0.525731f, 0.000000f}, {-0.716567f, -0.681718f, -0.147621f},
129 {-0.716567f, -0.681718f, 0.147621f}, {-0.525731f, -0.850651f, 0.000000f},
130 {-0.500000f, -0.809017f, 0.309017f}, {-0.238856f, -0.864188f, 0.442863f},
131 {-0.262866f, -0.951056f, 0.162460f}, {-0.864188f, -0.442863f, 0.238856f},
132 {-0.809017f, -0.309017f, 0.500000f}, {-0.688191f, -0.587785f, 0.425325f},
133 {-0.681718f, -0.147621f, 0.716567f}, {-0.442863f, -0.238856f, 0.864188f},
134 {-0.587785f, -0.425325f, 0.688191f}, {-0.309017f, -0.500000f, 0.809017f},
135 {-0.147621f, -0.716567f, 0.681718f}, {-0.425325f, -0.688191f, 0.587785f},
136 {-0.162460f, -0.262866f, 0.951056f}, {0.442863f, -0.238856f, 0.864188f},
137 {0.162460f, -0.262866f, 0.951056f}, {0.309017f, -0.500000f, 0.809017f},
138 {0.147621f, -0.716567f, 0.681718f}, {0.000000f, -0.525731f, 0.850651f},
139 {0.425325f, -0.688191f, 0.587785f}, {0.587785f, -0.425325f, 0.688191f},
140 {0.688191f, -0.587785f, 0.425325f}, {-0.955423f, 0.295242f, 0.000000f},
141 {-0.951056f, 0.162460f, 0.262866f}, {-1.000000f, 0.000000f, 0.000000f},
142 {-0.850651f, 0.000000f, 0.525731f}, {-0.955423f, -0.295242f, 0.000000f},
143 {-0.951056f, -0.162460f, 0.262866f}, {-0.864188f, 0.442863f, -0.238856f},
144 {-0.951056f, 0.162460f, -0.262866f}, {-0.809017f, 0.309017f, -0.500000f},
145 {-0.864188f, -0.442863f, -0.238856f}, {-0.951056f, -0.162460f, -0.262866f},
146 {-0.809017f, -0.309017f, -0.500000f}, {-0.681718f, 0.147621f, -0.716567f},
147 {-0.681718f, -0.147621f, -0.716567f}, {-0.850651f, 0.000000f, -0.525731f},
148 {-0.688191f, 0.587785f, -0.425325f}, {-0.587785f, 0.425325f, -0.688191f},
149 {-0.425325f, 0.688191f, -0.587785f}, {-0.425325f, -0.688191f, -0.587785f},
150 {-0.587785f, -0.425325f, -0.688191f}, {-0.688191f, -0.587785f, -0.425325f}
151 };
152
153 //==============================================================
154
155 int Q_rand( int *seed ) {
156 *seed = (69069U * *seed + 1U);
157 return *seed;
158 }
159
160 float Q_random( int *seed ) {
161 return ( Q_rand( seed ) & 0xffff ) / (float)0x10000;
162 }
163
164 float Q_crandom( int *seed ) {
165 return 2.0 * ( Q_random( seed ) - 0.5 );
166 }
167
168 //=======================================================
169
170 signed char ClampChar( int i ) {
171 if ( i < -128 ) {
172 return -128;
173 }
174 if ( i > 127 ) {
175 return 127;
176 }
177 return i;
178 }
179
180 signed short ClampShort( int i ) {
181 if ( i < -32768 ) {
182 return -32768;
183 }
184 if ( i > 0x7fff ) {
185 return 0x7fff;
186 }
187 return i;
188 }
189
190
191 // this isn't a real cheap function to call!
192 int DirToByte( vec3_t dir ) {
193 int i, best;
194 float d, bestd;
195
196 if ( !dir ) {
197 return 0;
198 }
199
200 bestd = 0;
201 best = 0;
202 for (i=0 ; i<NUMVERTEXNORMALS ; i++)
203 {
204 d = DotProduct (dir, bytedirs[i]);
205 if (d > bestd)
206 {
207 bestd = d;
208 best = i;
209 }
210 }
211
212 return best;
213 }
214
215 void ByteToDir( int b, vec3_t dir ) {
216 if ( b < 0 || b >= NUMVERTEXNORMALS ) {
217 VectorCopy( vec3_origin, dir );
218 return;
219 }
220 VectorCopy (bytedirs[b], dir);
221 }
222
223
224 unsigned ColorBytes3 (float r, float g, float b) {
225 unsigned i;
226
227 ( (byte *)&i )[0] = r * 255;
228 ( (byte *)&i )[1] = g * 255;
229 ( (byte *)&i )[2] = b * 255;
230
231 return i;
232 }
233
234 unsigned ColorBytes4 (float r, float g, float b, float a) {
235 unsigned i;
236
237 ( (byte *)&i )[0] = r * 255;
238 ( (byte *)&i )[1] = g * 255;
239 ( (byte *)&i )[2] = b * 255;
240 ( (byte *)&i )[3] = a * 255;
241
242 return i;
243 }
244
245 float NormalizeColor( const vec3_t in, vec3_t out ) {
246 float max;
247
248 max = in[0];
249 if ( in[1] > max ) {
250 max = in[1];
251 }
252 if ( in[2] > max ) {
253 max = in[2];
254 }
255
256 if ( !max ) {
257 VectorClear( out );
258 } else {
259 out[0] = in[0] / max;
260 out[1] = in[1] / max;
261 out[2] = in[2] / max;
262 }
263 return max;
264 }
265
266
267 /*
268 =====================
269 PlaneFromPoints
270
271 Returns false if the triangle is degenrate.
272 The normal will point out of the clock for clockwise ordered points
273 =====================
274 */
275 qboolean PlaneFromPoints( vec4_t plane, const vec3_t a, const vec3_t b, const vec3_t c ) {
276 vec3_t d1, d2;
277
278 VectorSubtract( b, a, d1 );
279 VectorSubtract( c, a, d2 );
280 CrossProduct( d2, d1, plane );
281 if ( VectorNormalize( plane ) == 0 ) {
282 return qfalse;
283 }
284
285 plane[3] = DotProduct( a, plane );
286 return qtrue;
287 }
288
289 /*
290 ===============
291 RotatePointAroundVector
292
293 This is not implemented very well...
294 ===============
295 */
296 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point,
297 float degrees ) {
298 float m[3][3];
299 float im[3][3];
300 float zrot[3][3];
301 float tmpmat[3][3];
302 float rot[3][3];
303 int i;
304 vec3_t vr, vup, vf;
305 float rad;
306
307 vf[0] = dir[0];
308 vf[1] = dir[1];
309 vf[2] = dir[2];
310
311 PerpendicularVector( vr, dir );
312 CrossProduct( vr, vf, vup );
313
314 m[0][0] = vr[0];
315 m[1][0] = vr[1];
316 m[2][0] = vr[2];
317
318 m[0][1] = vup[0];
319 m[1][1] = vup[1];
320 m[2][1] = vup[2];
321
322 m[0][2] = vf[0];
323 m[1][2] = vf[1];
324 m[2][2] = vf[2];
325
326 memcpy( im, m, sizeof( im ) );
327
328 im[0][1] = m[1][0];
329 im[0][2] = m[2][0];
330 im[1][0] = m[0][1];
331 im[1][2] = m[2][1];
332 im[2][0] = m[0][2];
333 im[2][1] = m[1][2];
334
335 memset( zrot, 0, sizeof( zrot ) );
336 zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F;
337
338 rad = DEG2RAD( degrees );
339 zrot[0][0] = cos( rad );
340 zrot[0][1] = sin( rad );
341 zrot[1][0] = -sin( rad );
342 zrot[1][1] = cos( rad );
343
344 MatrixMultiply( m, zrot, tmpmat );
345 MatrixMultiply( tmpmat, im, rot );
346
347 for ( i = 0; i < 3; i++ ) {
348 dst[i] = rot[i][0] * point[0] + rot[i][1] * point[1] + rot[i][2] * point[2];
349 }
350 }
351
352 /*
353 ===============
354 RotateAroundDirection
355 ===============
356 */
357 void RotateAroundDirection( vec3_t axis[3], float yaw ) {
358
359 // create an arbitrary axis[1]
360 PerpendicularVector( axis[1], axis[0] );
361
362 // rotate it around axis[0] by yaw
363 if ( yaw ) {
364 vec3_t temp;
365
366 VectorCopy( axis[1], temp );
367 RotatePointAroundVector( axis[1], axis[0], temp, yaw );
368 }
369
370 // cross to get axis[2]
371 CrossProduct( axis[0], axis[1], axis[2] );
372 }
373
374
375
376 void vectoangles( const vec3_t value1, vec3_t angles ) {
377 float forward;
378 float yaw, pitch;
379
380 if ( value1[1] == 0 && value1[0] == 0 ) {
381 yaw = 0;
382 if ( value1[2] > 0 ) {
383 pitch = 90;
384 }
385 else {
386 pitch = 270;
387 }
388 }
389 else {
390 if ( value1[0] ) {
391 yaw = ( atan2 ( value1[1], value1[0] ) * 180 / M_PI );
392 }
393 else if ( value1[1] > 0 ) {
394 yaw = 90;
395 }
396 else {
397 yaw = 270;
398 }
399 if ( yaw < 0 ) {
400 yaw += 360;
401 }
402
403 forward = sqrt ( value1[0]*value1[0] + value1[1]*value1[1] );
404 pitch = ( atan2(value1[2], forward) * 180 / M_PI );
405 if ( pitch < 0 ) {
406 pitch += 360;
407 }
408 }
409
410 angles[PITCH] = -pitch;
411 angles[YAW] = yaw;
412 angles[ROLL] = 0;
413 }
414
415
416 /*
417 =================
418 AnglesToAxis
419 =================
420 */
421 void AnglesToAxis( const vec3_t angles, vec3_t axis[3] ) {
422 vec3_t right;
423
424 // angle vectors returns "right" instead of "y axis"
425 AngleVectors( angles, axis[0], right, axis[2] );
426 VectorSubtract( vec3_origin, right, axis[1] );
427 }
428
429 void AxisClear( vec3_t axis[3] ) {
430 axis[0][0] = 1;
431 axis[0][1] = 0;
432 axis[0][2] = 0;
433 axis[1][0] = 0;
434 axis[1][1] = 1;
435 axis[1][2] = 0;
436 axis[2][0] = 0;
437 axis[2][1] = 0;
438 axis[2][2] = 1;
439 }
440
441 void AxisCopy( vec3_t in[3], vec3_t out[3] ) {
442 VectorCopy( in[0], out[0] );
443 VectorCopy( in[1], out[1] );
444 VectorCopy( in[2], out[2] );
445 }
446
447 void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal )
448 {
449 float d;
450 vec3_t n;
451 float inv_denom;
452
453 inv_denom = DotProduct( normal, normal );
454 #ifndef Q3_VM
455 assert( Q_fabs(inv_denom) != 0.0f ); // zero vectors get here
456 #endif
457 inv_denom = 1.0f / inv_denom;
458
459 d = DotProduct( normal, p ) * inv_denom;
460
461 n[0] = normal[0] * inv_denom;
462 n[1] = normal[1] * inv_denom;
463 n[2] = normal[2] * inv_denom;
464
465 dst[0] = p[0] - d * n[0];
466 dst[1] = p[1] - d * n[1];
467 dst[2] = p[2] - d * n[2];
468 }
469
470 /*
471 ================
472 MakeNormalVectors
473
474 Given a normalized forward vector, create two
475 other perpendicular vectors
476 ================
477 */
478 void MakeNormalVectors( const vec3_t forward, vec3_t right, vec3_t up) {
479 float d;
480
481 // this rotate and negate guarantees a vector
482 // not colinear with the original
483 right[1] = -forward[0];
484 right[2] = forward[1];
485 right[0] = forward[2];
486
487 d = DotProduct (right, forward);
488 VectorMA (right, -d, forward, right);
489 VectorNormalize (right);
490 CrossProduct (right, forward, up);
491 }
492
493
494 void VectorRotate( vec3_t in, vec3_t matrix[3], vec3_t out )
495 {
496 out[0] = DotProduct( in, matrix[0] );
497 out[1] = DotProduct( in, matrix[1] );
498 out[2] = DotProduct( in, matrix[2] );
499 }
500
501 //============================================================================
502
503 #if !idppc
504 /*
505 ** float q_rsqrt( float number )
506 */
507 float Q_rsqrt( float number )
508 {
509 floatint_t t;
510 float x2, y;
511 const float threehalfs = 1.5F;
512
513 x2 = number * 0.5F;
514 t.f = number;
515 t.i = 0x5f3759df - ( t.i >> 1 ); // what the fuck?
516 y = t.f;
517 y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
518 // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
519
520 return y;
521 }
522
523 float Q_fabs( float f ) {
524 floatint_t fi;
525 fi.f = f;
526 fi.i &= 0x7FFFFFFF;
527 return fi.f;
528 }
529 #endif
530
531 //============================================================
532
533 /*
534 ===============
535 LerpAngle
536
537 ===============
538 */
539 float LerpAngle (float from, float to, float frac) {
540 float a;
541
542 if ( to - from > 180 ) {
543 to -= 360;
544 }
545 if ( to - from < -180 ) {
546 to += 360;
547 }
548 a = from + frac * (to - from);
549
550 return a;
551 }
552
553
554 /*
555 =================
556 AngleSubtract
557
558 Always returns a value from -180 to 180
559 =================
560 */
561 float AngleSubtract( float a1, float a2 ) {
562 float a;
563
564 a = a1 - a2;
565 while ( a > 180 ) {
566 a -= 360;
567 }
568 while ( a < -180 ) {
569 a += 360;
570 }
571 return a;
572 }
573
574
575 void AnglesSubtract( vec3_t v1, vec3_t v2, vec3_t v3 ) {
576 v3[0] = AngleSubtract( v1[0], v2[0] );
577 v3[1] = AngleSubtract( v1[1], v2[1] );
578 v3[2] = AngleSubtract( v1[2], v2[2] );
579 }
580
581
582 float AngleMod(float a) {
583 a = (360.0/65536) * ((int)(a*(65536/360.0)) & 65535);
584 return a;
585 }
586
587
588 /*
589 =================
590 AngleNormalize360
591
592 returns angle normalized to the range [0 <= angle < 360]
593 =================
594 */
595 float AngleNormalize360 ( float angle ) {
596 return (360.0 / 65536) * ((int)(angle * (65536 / 360.0)) & 65535);
597 }
598
599
600 /*
601 =================
602 AngleNormalize180
603
604 returns angle normalized to the range [-180 < angle <= 180]
605 =================
606 */
607 float AngleNormalize180 ( float angle ) {
608 angle = AngleNormalize360( angle );
609 if ( angle > 180.0 ) {
610 angle -= 360.0;
611 }
612 return angle;
613 }
614
615
616 /*
617 =================
618 AngleDelta
619
620 returns the normalized delta from angle1 to angle2
621 =================
622 */
623 float AngleDelta ( float angle1, float angle2 ) {
624 return AngleNormalize180( angle1 - angle2 );
625 }
626
627
628 //============================================================
629
630
631 /*
632 =================
633 SetPlaneSignbits
634 =================
635 */
636 void SetPlaneSignbits (cplane_t *out) {
637 int bits, j;
638
639 // for fast box on planeside test
640 bits = 0;
641 for (j=0 ; j<3 ; j++) {
642 if (out->normal[j] < 0) {
643 bits |= 1<<j;
644 }
645 }
646 out->signbits = bits;
647 }
648
649
650 /*
651 ==================
652 BoxOnPlaneSide
653
654 Returns 1, 2, or 1 + 2
655 ==================
656 */
657 int BoxOnPlaneSide(vec3_t emins, vec3_t emaxs, struct cplane_s *p)
658 {
659 float dist[2];
660 int sides, b, i;
661
662 // fast axial cases
663 if (p->type < 3)
664 {
665 if (p->dist <= emins[p->type])
666 return 1;
667 if (p->dist >= emaxs[p->type])
668 return 2;
669 return 3;
670 }
671
672 // general case
673 dist[0] = dist[1] = 0;
674 if (p->signbits < 8) // >= 8: default case is original code (dist[0]=dist[1]=0)
675 {
676 for (i=0 ; i<3 ; i++)
677 {
678 b = (p->signbits >> i) & 1;
679 dist[ b] += p->normal[i]*emaxs[i];
680 dist[!b] += p->normal[i]*emins[i];
681 }
682 }
683
684 sides = 0;
685 if (dist[0] >= p->dist)
686 sides = 1;
687 if (dist[1] < p->dist)
688 sides |= 2;
689
690 return sides;
691 }
692
693
694 /*
695 =================
696 RadiusFromBounds
697 =================
698 */
699 float RadiusFromBounds( const vec3_t mins, const vec3_t maxs ) {
700 int i;
701 vec3_t corner;
702 float a, b;
703
704 for (i=0 ; i<3 ; i++) {
705 a = fabs( mins[i] );
706 b = fabs( maxs[i] );
707 corner[i] = a > b ? a : b;
708 }
709
710 return VectorLength (corner);
711 }
712
713
714 void ClearBounds( vec3_t mins, vec3_t maxs ) {
715 mins[0] = mins[1] = mins[2] = 99999;
716 maxs[0] = maxs[1] = maxs[2] = -99999;
717 }
718
719 void AddPointToBounds( const vec3_t v, vec3_t mins, vec3_t maxs ) {
720 if ( v[0] < mins[0] ) {
721 mins[0] = v[0];
722 }
723 if ( v[0] > maxs[0]) {
724 maxs[0] = v[0];
725 }
726
727 if ( v[1] < mins[1] ) {
728 mins[1] = v[1];
729 }
730 if ( v[1] > maxs[1]) {
731 maxs[1] = v[1];
732 }
733
734 if ( v[2] < mins[2] ) {
735 mins[2] = v[2];
736 }
737 if ( v[2] > maxs[2]) {
738 maxs[2] = v[2];
739 }
740 }
741
742 qboolean BoundsIntersect(const vec3_t mins, const vec3_t maxs,
743 const vec3_t mins2, const vec3_t maxs2)
744 {
745 if ( maxs[0] < mins2[0] ||
746 maxs[1] < mins2[1] ||
747 maxs[2] < mins2[2] ||
748 mins[0] > maxs2[0] ||
749 mins[1] > maxs2[1] ||
750 mins[2] > maxs2[2])
751 {
752 return qfalse;
753 }
754
755 return qtrue;
756 }
757
758 qboolean BoundsIntersectSphere(const vec3_t mins, const vec3_t maxs,
759 const vec3_t origin, vec_t radius)
760 {
761 if ( origin[0] - radius > maxs[0] ||
762 origin[0] + radius < mins[0] ||
763 origin[1] - radius > maxs[1] ||
764 origin[1] + radius < mins[1] ||
765 origin[2] - radius > maxs[2] ||
766 origin[2] + radius < mins[2])
767 {
768 return qfalse;
769 }
770
771 return qtrue;
772 }
773
774 qboolean BoundsIntersectPoint(const vec3_t mins, const vec3_t maxs,
775 const vec3_t origin)
776 {
777 if ( origin[0] > maxs[0] ||
778 origin[0] < mins[0] ||
779 origin[1] > maxs[1] ||
780 origin[1] < mins[1] ||
781 origin[2] > maxs[2] ||
782 origin[2] < mins[2])
783 {
784 return qfalse;
785 }
786
787 return qtrue;
788 }
789
790 vec_t VectorNormalize( vec3_t v ) {
791 // NOTE: TTimo - Apple G4 altivec source uses double?
792 float length, ilength;
793
794 length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
795
796 if ( length ) {
797 /* writing it this way allows gcc to recognize that rsqrt can be used */
798 ilength = 1/(float)sqrt (length);
799 /* sqrt(length) = length * (1 / sqrt(length)) */
800 length *= ilength;
801 v[0] *= ilength;
802 v[1] *= ilength;
803 v[2] *= ilength;
804 }
805
806 return length;
807 }
808
809 vec_t VectorNormalize2( const vec3_t v, vec3_t out) {
810 float length, ilength;
811
812 length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
813
814 if (length)
815 {
816 /* writing it this way allows gcc to recognize that rsqrt can be used */
817 ilength = 1/(float)sqrt (length);
818 /* sqrt(length) = length * (1 / sqrt(length)) */
819 length *= ilength;
820 out[0] = v[0]*ilength;
821 out[1] = v[1]*ilength;
822 out[2] = v[2]*ilength;
823 } else {
824 VectorClear( out );
825 }
826
827 return length;
828
829 }
830
831 void _VectorMA( const vec3_t veca, float scale, const vec3_t vecb, vec3_t vecc) {
832 vecc[0] = veca[0] + scale*vecb[0];
833 vecc[1] = veca[1] + scale*vecb[1];
834 vecc[2] = veca[2] + scale*vecb[2];
835 }
836
837
838 vec_t _DotProduct( const vec3_t v1, const vec3_t v2 ) {
839 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
840 }
841
842 void _VectorSubtract( const vec3_t veca, const vec3_t vecb, vec3_t out ) {
843 out[0] = veca[0]-vecb[0];
844 out[1] = veca[1]-vecb[1];
845 out[2] = veca[2]-vecb[2];
846 }
847
848 void _VectorAdd( const vec3_t veca, const vec3_t vecb, vec3_t out ) {
849 out[0] = veca[0]+vecb[0];
850 out[1] = veca[1]+vecb[1];
851 out[2] = veca[2]+vecb[2];
852 }
853
854 void _VectorCopy( const vec3_t in, vec3_t out ) {
855 out[0] = in[0];
856 out[1] = in[1];
857 out[2] = in[2];
858 }
859
860 void _VectorScale( const vec3_t in, vec_t scale, vec3_t out ) {
861 out[0] = in[0]*scale;
862 out[1] = in[1]*scale;
863 out[2] = in[2]*scale;
864 }
865
866 void Vector4Scale( const vec4_t in, vec_t scale, vec4_t out ) {
867 out[0] = in[0]*scale;
868 out[1] = in[1]*scale;
869 out[2] = in[2]*scale;
870 out[3] = in[3]*scale;
871 }
872
873
874 int Q_log2( int val ) {
875 int answer;
876
877 answer = 0;
878 while ( ( val>>=1 ) != 0 ) {
879 answer++;
880 }
881 return answer;
882 }
883
884
885
886 /*
887 =================
888 PlaneTypeForNormal
889 =================
890 */
891 /*
892 int PlaneTypeForNormal (vec3_t normal) {
893 if ( normal[0] == 1.0 )
894 return PLANE_X;
895 if ( normal[1] == 1.0 )
896 return PLANE_Y;
897 if ( normal[2] == 1.0 )
898 return PLANE_Z;
899
900 return PLANE_NON_AXIAL;
901 }
902 */
903
904
905 /*
906 ================
907 MatrixMultiply
908 ================
909 */
910 void MatrixMultiply(float in1[3][3], float in2[3][3], float out[3][3]) {
911 out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
912 in1[0][2] * in2[2][0];
913 out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
914 in1[0][2] * in2[2][1];
915 out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
916 in1[0][2] * in2[2][2];
917 out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
918 in1[1][2] * in2[2][0];
919 out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
920 in1[1][2] * in2[2][1];
921 out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
922 in1[1][2] * in2[2][2];
923 out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
924 in1[2][2] * in2[2][0];
925 out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
926 in1[2][2] * in2[2][1];
927 out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
928 in1[2][2] * in2[2][2];
929 }
930
931
932 void AngleVectors( const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up) {
933 float angle;
934 static float sr, sp, sy, cr, cp, cy;
935 // static to help MS compiler fp bugs
936
937 angle = angles[YAW] * (M_PI*2 / 360);
938 sy = sin(angle);
939 cy = cos(angle);
940 angle = angles[PITCH] * (M_PI*2 / 360);
941 sp = sin(angle);
942 cp = cos(angle);
943 angle = angles[ROLL] * (M_PI*2 / 360);
944 sr = sin(angle);
945 cr = cos(angle);
946
947 if (forward)
948 {
949 forward[0] = cp*cy;
950 forward[1] = cp*sy;
951 forward[2] = -sp;
952 }
953 if (right)
954 {
955 right[0] = (-1*sr*sp*cy+-1*cr*-sy);
956 right[1] = (-1*sr*sp*sy+-1*cr*cy);
957 right[2] = -1*sr*cp;
958 }
959 if (up)
960 {
961 up[0] = (cr*sp*cy+-sr*-sy);
962 up[1] = (cr*sp*sy+-sr*cy);
963 up[2] = cr*cp;
964 }
965 }
966
967 /*
968 ** assumes "src" is normalized
969 */
970 void PerpendicularVector( vec3_t dst, const vec3_t src )
971 {
972 int pos;
973 int i;
974 float minelem = 1.0F;
975 vec3_t tempvec;
976
977 /*
978 ** find the smallest magnitude axially aligned vector
979 */
980 for ( pos = 0, i = 0; i < 3; i++ )
981 {
982 if ( fabs( src[i] ) < minelem )
983 {
984 pos = i;
985 minelem = fabs( src[i] );
986 }
987 }
988 tempvec[0] = tempvec[1] = tempvec[2] = 0.0F;
989 tempvec[pos] = 1.0F;
990
991 /*
992 ** project the point onto the plane defined by src
993 */
994 ProjectPointOnPlane( dst, tempvec, src );
995
996 /*
997 ** normalize the result
998 */
999 VectorNormalize( dst );
1000 }
1001
1002 /*
1003 ================
1004 Q_isnan
1005
1006 Don't pass doubles to this
1007 ================
1008 */
1009 int Q_isnan( float x )
1010 {
1011 floatint_t fi;
1012
1013 fi.f = x;
1014 fi.ui &= 0x7FFFFFFF;
1015 fi.ui = 0x7F800000 - fi.ui;
1016
1017 return (int)( (unsigned int)fi.ui >> 31 );
1018 }
1019 //------------------------------------------------------------------------
1020
1021 #ifndef Q3_VM
1022 /*
1023 =====================
1024 Q_acos
1025
1026 the msvc acos doesn't always return a value between -PI and PI:
1027
1028 int i;
1029 i = 1065353246;
1030 acos(*(float*) &i) == -1.#IND0
1031
1032 =====================
1033 */
1034 float Q_acos(float c) {
1035 float angle;
1036
1037 angle = acos(c);
1038
1039 if (angle > M_PI) {
1040 return (float)M_PI;
1041 }
1042 if (angle < -M_PI) {
1043 return (float)M_PI;
1044 }
1045 return angle;
1046 }
1047 #endif