]> icculus.org git repositories - divverent/darkplaces.git/blob - mathlib.c
cgame.c:60: warning: implicit declaration of function `memset', fixed
[divverent/darkplaces.git] / mathlib.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // mathlib.c -- math primitives
21
22 #include <math.h>
23 #include "quakedef.h"
24
25 void Sys_Error (char *error, ...);
26
27 vec3_t vec3_origin = {0,0,0};
28 float ixtable[4096];
29
30 /*-----------------------------------------------------------------*/
31
32 float m_bytenormals[NUMVERTEXNORMALS][3] =
33 {
34 {-0.525731, 0.000000, 0.850651}, {-0.442863, 0.238856, 0.864188}, 
35 {-0.295242, 0.000000, 0.955423}, {-0.309017, 0.500000, 0.809017}, 
36 {-0.162460, 0.262866, 0.951056}, {0.000000, 0.000000, 1.000000}, 
37 {0.000000, 0.850651, 0.525731}, {-0.147621, 0.716567, 0.681718}, 
38 {0.147621, 0.716567, 0.681718}, {0.000000, 0.525731, 0.850651}, 
39 {0.309017, 0.500000, 0.809017}, {0.525731, 0.000000, 0.850651}, 
40 {0.295242, 0.000000, 0.955423}, {0.442863, 0.238856, 0.864188}, 
41 {0.162460, 0.262866, 0.951056}, {-0.681718, 0.147621, 0.716567}, 
42 {-0.809017, 0.309017, 0.500000}, {-0.587785, 0.425325, 0.688191}, 
43 {-0.850651, 0.525731, 0.000000}, {-0.864188, 0.442863, 0.238856},
44 {-0.716567, 0.681718, 0.147621}, {-0.688191, 0.587785, 0.425325}, 
45 {-0.500000, 0.809017, 0.309017}, {-0.238856, 0.864188, 0.442863}, 
46 {-0.425325, 0.688191, 0.587785}, {-0.716567, 0.681718, -0.147621}, 
47 {-0.500000, 0.809017, -0.309017}, {-0.525731, 0.850651, 0.000000}, 
48 {0.000000, 0.850651, -0.525731}, {-0.238856, 0.864188, -0.442863}, 
49 {0.000000, 0.955423, -0.295242}, {-0.262866, 0.951056, -0.162460}, 
50 {0.000000, 1.000000, 0.000000}, {0.000000, 0.955423, 0.295242}, 
51 {-0.262866, 0.951056, 0.162460}, {0.238856, 0.864188, 0.442863}, 
52 {0.262866, 0.951056, 0.162460}, {0.500000, 0.809017, 0.309017}, 
53 {0.238856, 0.864188, -0.442863}, {0.262866, 0.951056, -0.162460}, 
54 {0.500000, 0.809017, -0.309017}, {0.850651, 0.525731, 0.000000}, 
55 {0.716567, 0.681718, 0.147621}, {0.716567, 0.681718, -0.147621}, 
56 {0.525731, 0.850651, 0.000000}, {0.425325, 0.688191, 0.587785}, 
57 {0.864188, 0.442863, 0.238856}, {0.688191, 0.587785, 0.425325}, 
58 {0.809017, 0.309017, 0.500000}, {0.681718, 0.147621, 0.716567}, 
59 {0.587785, 0.425325, 0.688191}, {0.955423, 0.295242, 0.000000}, 
60 {1.000000, 0.000000, 0.000000}, {0.951056, 0.162460, 0.262866},
61 {0.850651, -0.525731, 0.000000}, {0.955423, -0.295242, 0.000000}, 
62 {0.864188, -0.442863, 0.238856}, {0.951056, -0.162460, 0.262866}, 
63 {0.809017, -0.309017, 0.500000}, {0.681718, -0.147621, 0.716567},
64 {0.850651, 0.000000, 0.525731}, {0.864188, 0.442863, -0.238856},
65 {0.809017, 0.309017, -0.500000}, {0.951056, 0.162460, -0.262866}, 
66 {0.525731, 0.000000, -0.850651}, {0.681718, 0.147621, -0.716567}, 
67 {0.681718, -0.147621, -0.716567}, {0.850651, 0.000000, -0.525731},
68 {0.809017, -0.309017, -0.500000}, {0.864188, -0.442863, -0.238856}, 
69 {0.951056, -0.162460, -0.262866}, {0.147621, 0.716567, -0.681718}, 
70 {0.309017, 0.500000, -0.809017}, {0.425325, 0.688191, -0.587785}, 
71 {0.442863, 0.238856, -0.864188}, {0.587785, 0.425325, -0.688191}, 
72 {0.688191, 0.587785, -0.425325}, {-0.147621, 0.716567, -0.681718}, 
73 {-0.309017, 0.500000, -0.809017}, {0.000000, 0.525731, -0.850651}, 
74 {-0.525731, 0.000000, -0.850651}, {-0.442863, 0.238856, -0.864188}, 
75 {-0.295242, 0.000000, -0.955423}, {-0.162460, 0.262866, -0.951056}, 
76 {0.000000, 0.000000, -1.000000}, {0.295242, 0.000000, -0.955423}, 
77 {0.162460, 0.262866, -0.951056}, {-0.442863, -0.238856, -0.864188}, 
78 {-0.309017, -0.500000, -0.809017}, {-0.162460, -0.262866, -0.951056}, 
79 {0.000000, -0.850651, -0.525731}, {-0.147621, -0.716567, -0.681718}, 
80 {0.147621, -0.716567, -0.681718}, {0.000000, -0.525731, -0.850651}, 
81 {0.309017, -0.500000, -0.809017}, {0.442863, -0.238856, -0.864188}, 
82 {0.162460, -0.262866, -0.951056}, {0.238856, -0.864188, -0.442863}, 
83 {0.500000, -0.809017, -0.309017}, {0.425325, -0.688191, -0.587785}, 
84 {0.716567, -0.681718, -0.147621}, {0.688191, -0.587785, -0.425325}, 
85 {0.587785, -0.425325, -0.688191}, {0.000000, -0.955423, -0.295242},
86 {0.000000, -1.000000, 0.000000}, {0.262866, -0.951056, -0.162460}, 
87 {0.000000, -0.850651, 0.525731}, {0.000000, -0.955423, 0.295242}, 
88 {0.238856, -0.864188, 0.442863}, {0.262866, -0.951056, 0.162460}, 
89 {0.500000, -0.809017, 0.309017}, {0.716567, -0.681718, 0.147621}, 
90 {0.525731, -0.850651, 0.000000}, {-0.238856, -0.864188, -0.442863}, 
91 {-0.500000, -0.809017, -0.309017}, {-0.262866, -0.951056, -0.162460}, 
92 {-0.850651, -0.525731, 0.000000}, {-0.716567, -0.681718, -0.147621},
93 {-0.716567, -0.681718, 0.147621}, {-0.525731, -0.850651, 0.000000}, 
94 {-0.500000, -0.809017, 0.309017}, {-0.238856, -0.864188, 0.442863},
95 {-0.262866, -0.951056, 0.162460}, {-0.864188, -0.442863, 0.238856}, 
96 {-0.809017, -0.309017, 0.500000}, {-0.688191, -0.587785, 0.425325},
97 {-0.681718, -0.147621, 0.716567}, {-0.442863, -0.238856, 0.864188}, 
98 {-0.587785, -0.425325, 0.688191}, {-0.309017, -0.500000, 0.809017}, 
99 {-0.147621, -0.716567, 0.681718}, {-0.425325, -0.688191, 0.587785}, 
100 {-0.162460, -0.262866, 0.951056}, {0.442863, -0.238856, 0.864188}, 
101 {0.162460, -0.262866, 0.951056}, {0.309017, -0.500000, 0.809017}, 
102 {0.147621, -0.716567, 0.681718}, {0.000000, -0.525731, 0.850651}, 
103 {0.425325, -0.688191, 0.587785}, {0.587785, -0.425325, 0.688191}, 
104 {0.688191, -0.587785, 0.425325}, {-0.955423, 0.295242, 0.000000}, 
105 {-0.951056, 0.162460, 0.262866}, {-1.000000, 0.000000, 0.000000}, 
106 {-0.850651, 0.000000, 0.525731}, {-0.955423, -0.295242, 0.000000}, 
107 {-0.951056, -0.162460, 0.262866}, {-0.864188, 0.442863, -0.238856}, 
108 {-0.951056, 0.162460, -0.262866}, {-0.809017, 0.309017, -0.500000}, 
109 {-0.864188, -0.442863, -0.238856}, {-0.951056, -0.162460, -0.262866},
110 {-0.809017, -0.309017, -0.500000}, {-0.681718, 0.147621, -0.716567}, 
111 {-0.681718, -0.147621, -0.716567}, {-0.850651, 0.000000, -0.525731}, 
112 {-0.688191, 0.587785, -0.425325}, {-0.587785, 0.425325, -0.688191}, 
113 {-0.425325, 0.688191, -0.587785}, {-0.425325, -0.688191, -0.587785}, 
114 {-0.587785, -0.425325, -0.688191}, {-0.688191, -0.587785, -0.425325}, 
115 };
116
117 byte NormalToByte(vec3_t n)
118 {
119         int i, best;
120         float bestdistance, distance;
121
122         best = 0;
123         bestdistance = DotProduct (n, m_bytenormals[0]);
124         for (i = 1;i < NUMVERTEXNORMALS;i++)
125         {
126                 distance = DotProduct (n, m_bytenormals[i]);
127                 if (distance > bestdistance)
128                 {
129                         bestdistance = distance;
130                         best = i;
131                 }
132         }
133         return best;
134 }
135
136 // note: uses byte partly to force unsigned for the validity check
137 void ByteToNormal(byte num, vec3_t n)
138 {
139         if (num < NUMVERTEXNORMALS)
140                 VectorCopy(m_bytenormals[num], n);
141         else
142                 VectorClear(n); // FIXME: complain?
143 }
144
145 float Q_RSqrt(float number)
146 {
147         float y;
148
149         if (number == 0.0f)
150                 return 0.0f;
151
152         *((long *)&y) = 0x5f3759df - ((* (long *) &number) >> 1);
153         return y * (1.5f - (number * 0.5f * y * y));
154 }
155
156 void _VectorNormalizeFast(vec3_t v)
157 {
158         float y, number;
159
160         number = DotProduct(v, v);
161
162         if (number != 0.0)
163         {
164                 *((long *)&y) = 0x5f3759df - ((* (long *) &number) >> 1);
165                 y = y * (1.5f - (number * 0.5f * y * y));
166
167                 VectorScale(v, y, v);
168         }
169 }
170
171 #if 0
172 // LordHavoc: no longer used at all
173 void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal )
174 {
175 #if 0
176         // LordHavoc: the old way...
177         float d;
178         vec3_t n;
179         float inv_denom;
180
181         inv_denom = 1.0F / DotProduct( normal, normal );
182
183         d = DotProduct( normal, p ) * inv_denom;
184
185         n[0] = normal[0] * inv_denom;
186         n[1] = normal[1] * inv_denom;
187         n[2] = normal[2] * inv_denom;
188
189         dst[0] = p[0] - d * n[0];
190         dst[1] = p[1] - d * n[1];
191         dst[2] = p[2] - d * n[2];
192 #else
193         // LordHavoc: optimized to death and beyond
194         float d;
195
196         // LordHavoc: the normal is a unit vector by definition,
197         //            therefore inv_denom was pointless.
198         d = DotProduct(normal, p);
199         dst[0] = p[0] - d * normal[0];
200         dst[1] = p[1] - d * normal[1];
201         dst[2] = p[2] - d * normal[2];
202 #endif
203 }
204 #endif
205
206 // assumes "src" is normalized
207 void PerpendicularVector( vec3_t dst, const vec3_t src )
208 {
209 #if 0
210         // LordHavoc: the old way...
211         int     pos;
212         int i;
213         float minelem, d;
214         vec3_t tempvec;
215
216         // find the smallest magnitude axially aligned vector
217         minelem = 1.0F;
218         for ( pos = 0, i = 0; i < 3; i++ )
219         {
220                 if ( fabs( src[i] ) < minelem )
221                 {
222                         pos = i;
223                         minelem = fabs( src[i] );
224                 }
225         }
226         VectorClear(tempvec);
227         tempvec[pos] = 1.0F;
228
229         // project the point onto the plane defined by src
230         ProjectPointOnPlane( dst, tempvec, src );
231
232         // normalize the result
233         VectorNormalize(dst);
234 #else
235         // LordHavoc: optimized to death and beyond
236         int     pos;
237         float minelem;
238
239         if (src[0])
240         {
241                 dst[0] = 0;
242                 if (src[1])
243                 {
244                         dst[1] = 0;
245                         if (src[2])
246                         {
247                                 dst[2] = 0;
248                                 pos = 0;
249                                 minelem = fabs(src[0]);
250                                 if (fabs(src[1]) < minelem)
251                                 {
252                                         pos = 1;
253                                         minelem = fabs(src[1]);
254                                 }
255                                 if (fabs(src[2]) < minelem)
256                                         pos = 2;
257
258                                 dst[pos] = 1;
259                                 dst[0] -= src[pos] * src[0];
260                                 dst[1] -= src[pos] * src[1];
261                                 dst[2] -= src[pos] * src[2];
262
263                                 // normalize the result
264                                 VectorNormalize(dst);
265                         }
266                         else
267                                 dst[2] = 1;
268                 }
269                 else
270                 {
271                         dst[1] = 1;
272                         dst[2] = 0;
273                 }
274         }
275         else
276         {
277                 dst[0] = 1;
278                 dst[1] = 0;
279                 dst[2] = 0;
280         }
281 #endif
282 }
283
284
285 // LordHavoc: like AngleVectors, but taking a forward vector instead of angles, useful!
286 void VectorVectors(const vec3_t forward, vec3_t right, vec3_t up)
287 {
288         float d;
289
290         right[0] = forward[2];
291         right[1] = -forward[0];
292         right[2] = forward[1];
293
294         d = DotProduct(forward, right);
295         right[0] -= d * forward[0];
296         right[1] -= d * forward[1];
297         right[2] -= d * forward[2];
298         VectorNormalizeFast(right);
299         CrossProduct(right, forward, up);
300 }
301
302 void VectorVectorsDouble(const double *forward, double *right, double *up)
303 {
304         double d;
305
306         right[0] = forward[2];
307         right[1] = -forward[0];
308         right[2] = forward[1];
309
310         d = DotProduct(forward, right);
311         right[0] -= d * forward[0];
312         right[1] -= d * forward[1];
313         right[2] -= d * forward[2];
314         VectorNormalize(right);
315         CrossProduct(right, forward, up);
316 }
317
318 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees )
319 {
320 #if 0
321         // LordHavoc: the old way, cryptic brute force...
322         float   m[3][3];
323         float   im[3][3];
324         float   zrot[3][3];
325         float   tmpmat[3][3];
326         float   rot[3][3];
327         int     i;
328         vec3_t vr, vup, vf;
329
330         vf[0] = dir[0];
331         vf[1] = dir[1];
332         vf[2] = dir[2];
333
334         PerpendicularVector( vr, dir );
335         CrossProduct( vr, vf, vup );
336
337         m[0][0] = vr[0];
338         m[1][0] = vr[1];
339         m[2][0] = vr[2];
340
341         m[0][1] = vup[0];
342         m[1][1] = vup[1];
343         m[2][1] = vup[2];
344
345         m[0][2] = vf[0];
346         m[1][2] = vf[1];
347         m[2][2] = vf[2];
348
349         memcpy( im, m, sizeof( im ) );
350
351         im[0][1] = m[1][0];
352         im[0][2] = m[2][0];
353         im[1][0] = m[0][1];
354         im[1][2] = m[2][1];
355         im[2][0] = m[0][2];
356         im[2][1] = m[1][2];
357
358         memset( zrot, 0, sizeof( zrot ) );
359         zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F;
360
361         zrot[0][0] = cos( DEG2RAD( degrees ) );
362         zrot[0][1] = sin( DEG2RAD( degrees ) );
363         zrot[1][0] = -sin( DEG2RAD( degrees ) );
364         zrot[1][1] = cos( DEG2RAD( degrees ) );
365
366         R_ConcatRotations( m, zrot, tmpmat );
367         R_ConcatRotations( tmpmat, im, rot );
368
369         for ( i = 0; i < 3; i++ )
370                 dst[i] = rot[i][0] * point[0] + rot[i][1] * point[1] + rot[i][2] * point[2];
371 #elif 0
372         // LordHavoc: on the path to unintelligible code...
373 //      float   m[3][3];
374 //      float   im[3][3];
375 //      float   zrot[3][3];
376         float   tmpmat[3][3];
377 //      float   rot[3][3];
378         float   angle, c, s;
379 //      int     i;
380         vec3_t vr, vu, vf;
381
382         angle = DEG2RAD(degrees);
383
384         c = cos(angle);
385         s = sin(angle);
386
387         vf[0] = dir[0];
388         vf[1] = dir[1];
389         vf[2] = dir[2];
390
391         PerpendicularVector(vr, dir);
392         CrossProduct(vr, vf, vu);
393
394 //      m   [0][0] = vr[0];m   [0][1] = vu[0];m   [0][2] = vf[0];
395 //      m   [1][0] = vr[1];m   [1][1] = vu[1];m   [1][2] = vf[1];
396 //      m   [2][0] = vr[2];m   [2][1] = vu[2];m   [2][2] = vf[2];
397 //      im  [0][0] = vr[0];im  [0][1] = vr[1];im  [0][2] = vr[2];
398 //      im  [1][0] = vu[0];im  [1][1] = vu[1];im  [1][2] = vu[2];
399 //      im  [2][0] = vf[0];im  [2][1] = vf[1];im  [2][2] = vf[2];
400 //      zrot[0][0] =     c;zrot[0][1] =     s;zrot[0][2] =     0;
401 //      zrot[1][0] =    -s;zrot[1][1] =     c;zrot[1][2] =     0;
402 //      zrot[2][0] =     0;zrot[2][1] =     0;zrot[2][2] =     1;
403
404 //      tmpmat[0][0] = m[0][0] * zrot[0][0] + m[0][1] * zrot[1][0] + m[0][2] * zrot[2][0];
405 //      tmpmat[0][1] = m[0][0] * zrot[0][1] + m[0][1] * zrot[1][1] + m[0][2] * zrot[2][1];
406 //      tmpmat[0][2] = m[0][0] * zrot[0][2] + m[0][1] * zrot[1][2] + m[0][2] * zrot[2][2];
407 //      tmpmat[1][0] = m[1][0] * zrot[0][0] + m[1][1] * zrot[1][0] + m[1][2] * zrot[2][0];
408 //      tmpmat[1][1] = m[1][0] * zrot[0][1] + m[1][1] * zrot[1][1] + m[1][2] * zrot[2][1];
409 //      tmpmat[1][2] = m[1][0] * zrot[0][2] + m[1][1] * zrot[1][2] + m[1][2] * zrot[2][2];
410 //      tmpmat[2][0] = m[2][0] * zrot[0][0] + m[2][1] * zrot[1][0] + m[2][2] * zrot[2][0];
411 //      tmpmat[2][1] = m[2][0] * zrot[0][1] + m[2][1] * zrot[1][1] + m[2][2] * zrot[2][1];
412 //      tmpmat[2][2] = m[2][0] * zrot[0][2] + m[2][1] * zrot[1][2] + m[2][2] * zrot[2][2];
413
414         tmpmat[0][0] = vr[0] *  c + vu[0] * -s;
415         tmpmat[0][1] = vr[0] *  s + vu[0] *  c;
416 //      tmpmat[0][2] =                           vf[0];
417         tmpmat[1][0] = vr[1] *  c + vu[1] * -s;
418         tmpmat[1][1] = vr[1] *  s + vu[1] *  c;
419 //      tmpmat[1][2] =                           vf[1];
420         tmpmat[2][0] = vr[2] *  c + vu[2] * -s;
421         tmpmat[2][1] = vr[2] *  s + vu[2] *  c;
422 //      tmpmat[2][2] =                           vf[2];
423
424 //      rot[0][0] = tmpmat[0][0] * vr[0] + tmpmat[0][1] * vu[0] + tmpmat[0][2] * vf[0];
425 //      rot[0][1] = tmpmat[0][0] * vr[1] + tmpmat[0][1] * vu[1] + tmpmat[0][2] * vf[1];
426 //      rot[0][2] = tmpmat[0][0] * vr[2] + tmpmat[0][1] * vu[2] + tmpmat[0][2] * vf[2];
427 //      rot[1][0] = tmpmat[1][0] * vr[0] + tmpmat[1][1] * vu[0] + tmpmat[1][2] * vf[0];
428 //      rot[1][1] = tmpmat[1][0] * vr[1] + tmpmat[1][1] * vu[1] + tmpmat[1][2] * vf[1];
429 //      rot[1][2] = tmpmat[1][0] * vr[2] + tmpmat[1][1] * vu[2] + tmpmat[1][2] * vf[2];
430 //      rot[2][0] = tmpmat[2][0] * vr[0] + tmpmat[2][1] * vu[0] + tmpmat[2][2] * vf[0];
431 //      rot[2][1] = tmpmat[2][0] * vr[1] + tmpmat[2][1] * vu[1] + tmpmat[2][2] * vf[1];
432 //      rot[2][2] = tmpmat[2][0] * vr[2] + tmpmat[2][1] * vu[2] + tmpmat[2][2] * vf[2];
433
434 //      rot[0][0] = tmpmat[0][0] * vr[0] + tmpmat[0][1] * vu[0] + vf[0] * vf[0];
435 //      rot[0][1] = tmpmat[0][0] * vr[1] + tmpmat[0][1] * vu[1] + vf[0] * vf[1];
436 //      rot[0][2] = tmpmat[0][0] * vr[2] + tmpmat[0][1] * vu[2] + vf[0] * vf[2];
437 //      rot[1][0] = tmpmat[1][0] * vr[0] + tmpmat[1][1] * vu[0] + vf[1] * vf[0];
438 //      rot[1][1] = tmpmat[1][0] * vr[1] + tmpmat[1][1] * vu[1] + vf[1] * vf[1];
439 //      rot[1][2] = tmpmat[1][0] * vr[2] + tmpmat[1][1] * vu[2] + vf[1] * vf[2];
440 //      rot[2][0] = tmpmat[2][0] * vr[0] + tmpmat[2][1] * vu[0] + vf[2] * vf[0];
441 //      rot[2][1] = tmpmat[2][0] * vr[1] + tmpmat[2][1] * vu[1] + vf[2] * vf[1];
442 //      rot[2][2] = tmpmat[2][0] * vr[2] + tmpmat[2][1] * vu[2] + vf[2] * vf[2];
443
444 //      dst[0] = rot[0][0] * point[0] + rot[0][1] * point[1] + rot[0][2] * point[2];
445 //      dst[1] = rot[1][0] * point[0] + rot[1][1] * point[1] + rot[1][2] * point[2];
446 //      dst[2] = rot[2][0] * point[0] + rot[2][1] * point[1] + rot[2][2] * point[2];
447
448         dst[0] = (tmpmat[0][0] * vr[0] + tmpmat[0][1] * vu[0] + vf[0] * vf[0]) * point[0]
449                + (tmpmat[0][0] * vr[1] + tmpmat[0][1] * vu[1] + vf[0] * vf[1]) * point[1]
450                + (tmpmat[0][0] * vr[2] + tmpmat[0][1] * vu[2] + vf[0] * vf[2]) * point[2];
451         dst[1] = (tmpmat[1][0] * vr[0] + tmpmat[1][1] * vu[0] + vf[1] * vf[0]) * point[0]
452                + (tmpmat[1][0] * vr[1] + tmpmat[1][1] * vu[1] + vf[1] * vf[1]) * point[1]
453                + (tmpmat[1][0] * vr[2] + tmpmat[1][1] * vu[2] + vf[1] * vf[2]) * point[2];
454         dst[2] = (tmpmat[2][0] * vr[0] + tmpmat[2][1] * vu[0] + vf[2] * vf[0]) * point[0]
455                + (tmpmat[2][0] * vr[1] + tmpmat[2][1] * vu[1] + vf[2] * vf[1]) * point[1]
456                + (tmpmat[2][0] * vr[2] + tmpmat[2][1] * vu[2] + vf[2] * vf[2]) * point[2];
457 #else
458         // LordHavoc: optimized to death and beyond, cryptic in an entirely new way
459         float   t0, t1;
460         float   angle, c, s;
461         vec3_t  vr, vu, vf;
462
463         angle = DEG2RAD(degrees);
464
465         c = cos(angle);
466         s = sin(angle);
467
468         vf[0] = dir[0];
469         vf[1] = dir[1];
470         vf[2] = dir[2];
471
472 //      PerpendicularVector(vr, dir);
473 //      CrossProduct(vr, vf, vu);
474         VectorVectors(vf, vr, vu);
475
476         t0 = vr[0] *  c + vu[0] * -s;
477         t1 = vr[0] *  s + vu[0] *  c;
478         dst[0] = (t0 * vr[0] + t1 * vu[0] + vf[0] * vf[0]) * point[0]
479                + (t0 * vr[1] + t1 * vu[1] + vf[0] * vf[1]) * point[1]
480                + (t0 * vr[2] + t1 * vu[2] + vf[0] * vf[2]) * point[2];
481
482         t0 = vr[1] *  c + vu[1] * -s;
483         t1 = vr[1] *  s + vu[1] *  c;
484         dst[1] = (t0 * vr[0] + t1 * vu[0] + vf[1] * vf[0]) * point[0]
485                + (t0 * vr[1] + t1 * vu[1] + vf[1] * vf[1]) * point[1]
486                + (t0 * vr[2] + t1 * vu[2] + vf[1] * vf[2]) * point[2];
487
488         t0 = vr[2] *  c + vu[2] * -s;
489         t1 = vr[2] *  s + vu[2] *  c;
490         dst[2] = (t0 * vr[0] + t1 * vu[0] + vf[2] * vf[0]) * point[0]
491                + (t0 * vr[1] + t1 * vu[1] + vf[2] * vf[1]) * point[1]
492                + (t0 * vr[2] + t1 * vu[2] + vf[2] * vf[2]) * point[2];
493 #endif
494 }
495
496 /*-----------------------------------------------------------------*/
497
498
499 // LordHavoc note 1:
500 // BoxOnPlaneSide did a switch on a 'signbits' value and had optimized
501 // assembly in an attempt to accelerate it further, very inefficient
502 // considering that signbits of the frustum planes only changed each
503 // frame, and the world planes changed only at load time.
504 // So, to optimize it further I took the obvious route of storing a function
505 // pointer in the plane struct itself, and shrunk each of the individual
506 // cases to a single return statement.
507 // LordHavoc note 2:
508 // realized axial cases would be a nice speedup for world geometry, although
509 // never useful for the frustum planes.
510 int BoxOnPlaneSideX (vec3_t emins, vec3_t emaxs, mplane_t *p) {return p->dist <= emins[0] ? 1 : (p->dist >= emaxs[0] ? 2 : 3);}
511 int BoxOnPlaneSideY (vec3_t emins, vec3_t emaxs, mplane_t *p) {return p->dist <= emins[1] ? 1 : (p->dist >= emaxs[1] ? 2 : 3);}
512 int BoxOnPlaneSideZ (vec3_t emins, vec3_t emaxs, mplane_t *p) {return p->dist <= emins[2] ? 1 : (p->dist >= emaxs[2] ? 2 : 3);}
513 int BoxOnPlaneSide0 (vec3_t emins, vec3_t emaxs, mplane_t *p) {return (((p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2]) >= p->dist) | (((p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2]) < p->dist) << 1));}
514 int BoxOnPlaneSide1 (vec3_t emins, vec3_t emaxs, mplane_t *p) {return (((p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2]) >= p->dist) | (((p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2]) < p->dist) << 1));}
515 int BoxOnPlaneSide2 (vec3_t emins, vec3_t emaxs, mplane_t *p) {return (((p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2]) >= p->dist) | (((p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2]) < p->dist) << 1));}
516 int BoxOnPlaneSide3 (vec3_t emins, vec3_t emaxs, mplane_t *p) {return (((p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2]) >= p->dist) | (((p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2]) < p->dist) << 1));}
517 int BoxOnPlaneSide4 (vec3_t emins, vec3_t emaxs, mplane_t *p) {return (((p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2]) >= p->dist) | (((p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2]) < p->dist) << 1));}
518 int BoxOnPlaneSide5 (vec3_t emins, vec3_t emaxs, mplane_t *p) {return (((p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2]) >= p->dist) | (((p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2]) < p->dist) << 1));}
519 int BoxOnPlaneSide6 (vec3_t emins, vec3_t emaxs, mplane_t *p) {return (((p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2]) >= p->dist) | (((p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2]) < p->dist) << 1));}
520 int BoxOnPlaneSide7 (vec3_t emins, vec3_t emaxs, mplane_t *p) {return (((p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2]) >= p->dist) | (((p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2]) < p->dist) << 1));}
521
522 void BoxOnPlaneSideClassify(mplane_t *p)
523 {
524         switch(p->type)
525         {
526         case 0: // x axis
527                 p->BoxOnPlaneSideFunc = BoxOnPlaneSideX;
528                 break;
529         case 1: // y axis
530                 p->BoxOnPlaneSideFunc = BoxOnPlaneSideY;
531                 break;
532         case 2: // z axis
533                 p->BoxOnPlaneSideFunc = BoxOnPlaneSideZ;
534                 break;
535         default:
536                 if (p->normal[2] < 0) // 4
537                 {
538                         if (p->normal[1] < 0) // 2
539                         {
540                                 if (p->normal[0] < 0) // 1
541                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide7;
542                                 else
543                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide6;
544                         }
545                         else
546                         {
547                                 if (p->normal[0] < 0) // 1
548                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide5;
549                                 else
550                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide4;
551                         }
552                 }
553                 else
554                 {
555                         if (p->normal[1] < 0) // 2
556                         {
557                                 if (p->normal[0] < 0) // 1
558                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide3;
559                                 else
560                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide2;
561                         }
562                         else
563                         {
564                                 if (p->normal[0] < 0) // 1
565                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide1;
566                                 else
567                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide0;
568                         }
569                 }
570                 break;
571         }
572 }
573
574 void PlaneClassify(mplane_t *p)
575 {
576         if (p->normal[0] == 1)
577                 p->type = 0;
578         else if (p->normal[1] == 1)
579                 p->type = 1;
580         else if (p->normal[2] == 1)
581                 p->type = 2;
582         else
583                 p->type = 3;
584         BoxOnPlaneSideClassify(p);
585 }
586
587 void AngleVectors (vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
588 {
589         double angle, sr, sp, sy, cr, cp, cy;
590
591         angle = angles[YAW] * (M_PI*2 / 360);
592         sy = sin(angle);
593         cy = cos(angle);
594         angle = angles[PITCH] * (M_PI*2 / 360);
595         sp = sin(angle);
596         cp = cos(angle);
597         if (forward)
598         {
599                 forward[0] = cp*cy;
600                 forward[1] = cp*sy;
601                 forward[2] = -sp;
602         }
603         if (right || up)
604         {
605                 angle = angles[ROLL] * (M_PI*2 / 360);
606                 sr = sin(angle);
607                 cr = cos(angle);
608                 if (right)
609                 {
610                         right[0] = -1*(sr*sp*cy+cr*-sy);
611                         right[1] = -1*(sr*sp*sy+cr*cy);
612                         right[2] = -1*(sr*cp);
613                 }
614                 if (up)
615                 {
616                         up[0] = (cr*sp*cy+-sr*-sy);
617                         up[1] = (cr*sp*sy+-sr*cy);
618                         up[2] = cr*cp;
619                 }
620         }
621 }
622
623 void AngleVectorsFLU (vec3_t angles, vec3_t forward, vec3_t left, vec3_t up)
624 {
625         double angle, sr, sp, sy, cr, cp, cy;
626
627         angle = angles[YAW] * (M_PI*2 / 360);
628         sy = sin(angle);
629         cy = cos(angle);
630         angle = angles[PITCH] * (M_PI*2 / 360);
631         sp = sin(angle);
632         cp = cos(angle);
633         if (forward)
634         {
635                 forward[0] = cp*cy;
636                 forward[1] = cp*sy;
637                 forward[2] = -sp;
638         }
639         if (left || up)
640         {
641                 angle = angles[ROLL] * (M_PI*2 / 360);
642                 sr = sin(angle);
643                 cr = cos(angle);
644                 if (left)
645                 {
646                         left[0] = sr*sp*cy+cr*-sy;
647                         left[1] = sr*sp*sy+cr*cy;
648                         left[2] = sr*cp;
649                 }
650                 if (up)
651                 {
652                         up[0] = cr*sp*cy+-sr*-sy;
653                         up[1] = cr*sp*sy+-sr*cy;
654                         up[2] = cr*cp;
655                 }
656         }
657 }
658
659 void AngleMatrix (vec3_t angles, vec3_t translate, vec_t matrix[][4])
660 {
661         double angle, sr, sp, sy, cr, cp, cy;
662
663         angle = angles[YAW] * (M_PI*2 / 360);
664         sy = sin(angle);
665         cy = cos(angle);
666         angle = angles[PITCH] * (M_PI*2 / 360);
667         sp = sin(angle);
668         cp = cos(angle);
669         angle = angles[ROLL] * (M_PI*2 / 360);
670         sr = sin(angle);
671         cr = cos(angle);
672         matrix[0][0] = cp*cy;
673         matrix[0][1] = sr*sp*cy+cr*-sy;
674         matrix[0][2] = cr*sp*cy+-sr*-sy;
675         matrix[0][3] = translate[0];
676         matrix[1][0] = cp*sy;
677         matrix[1][1] = sr*sp*sy+cr*cy;
678         matrix[1][2] = cr*sp*sy+-sr*cy;
679         matrix[1][3] = translate[1];
680         matrix[2][0] = -sp;
681         matrix[2][1] = sr*cp;
682         matrix[2][2] = cr*cp;
683         matrix[2][3] = translate[2];
684 }
685
686 void VectorMASlow (vec3_t veca, float scale, vec3_t vecb, vec3_t vecc)
687 {
688         vecc[0] = veca[0] + scale*vecb[0];
689         vecc[1] = veca[1] + scale*vecb[1];
690         vecc[2] = veca[2] + scale*vecb[2];
691 }
692
693
694 vec_t _DotProduct (vec3_t v1, vec3_t v2)
695 {
696         return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
697 }
698
699 void _VectorSubtract (vec3_t veca, vec3_t vecb, vec3_t out)
700 {
701         out[0] = veca[0]-vecb[0];
702         out[1] = veca[1]-vecb[1];
703         out[2] = veca[2]-vecb[2];
704 }
705
706 void _VectorAdd (vec3_t veca, vec3_t vecb, vec3_t out)
707 {
708         out[0] = veca[0]+vecb[0];
709         out[1] = veca[1]+vecb[1];
710         out[2] = veca[2]+vecb[2];
711 }
712
713 void _VectorCopy (vec3_t in, vec3_t out)
714 {
715         out[0] = in[0];
716         out[1] = in[1];
717         out[2] = in[2];
718 }
719
720 // LordHavoc: changed CrossProduct to a #define
721 /*
722 void CrossProduct (vec3_t v1, vec3_t v2, vec3_t cross)
723 {
724         cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
725         cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
726         cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
727 }
728 */
729
730 double sqrt(double x);
731
732 vec_t Length(vec3_t v)
733 {
734         int             i;
735         float   length;
736
737         length = 0;
738         for (i=0 ; i< 3 ; i++)
739                 length += v[i]*v[i];
740         length = sqrt (length);         // FIXME
741
742         return length;
743 }
744
745 /*
746 // LordHavoc: fixme: do more research on gcc assembly so that qftol_minushalf and result will not be considered unused
747 static double qftol_minushalf = -0.5;
748
749 int qftol(double v)
750 {
751         int result;
752 #ifdef _MSC_VER
753         __asm
754         {
755                 fld             v
756                 fadd    qftol_minushalf
757                 fistp   result
758         }
759 #else // gcc hopefully
760         asm("fldl v\n\tfaddl qftol_minushalf\n\tfistpl result");
761 #endif
762         return result;
763 }
764 */
765
766 // LordHavoc: renamed these to Length, and made the normal ones #define
767 float VectorNormalizeLength (vec3_t v)
768 {
769         float   length, ilength;
770
771         length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
772         length = sqrt (length);         // FIXME
773
774         if (length)
775         {
776                 ilength = 1/length;
777                 v[0] *= ilength;
778                 v[1] *= ilength;
779                 v[2] *= ilength;
780         }
781                 
782         return length;
783
784 }
785
786 float VectorNormalizeLength2 (vec3_t v, vec3_t dest) // LordHavoc: added to allow copying while doing the calculation...
787 {
788         float   length, ilength;
789
790         length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
791         length = sqrt (length);         // FIXME
792
793         if (length)
794         {
795                 ilength = 1/length;
796                 dest[0] = v[0] * ilength;
797                 dest[1] = v[1] * ilength;
798                 dest[2] = v[2] * ilength;
799         }
800         else
801                 dest[0] = dest[1] = dest[2] = 0;
802                 
803         return length;
804
805 }
806
807 void _VectorInverse (vec3_t v)
808 {
809         v[0] = -v[0];
810         v[1] = -v[1];
811         v[2] = -v[2];
812 }
813
814 void _VectorScale (vec3_t in, vec_t scale, vec3_t out)
815 {
816         out[0] = in[0]*scale;
817         out[1] = in[1]*scale;
818         out[2] = in[2]*scale;
819 }
820
821
822 int Q_log2(int val)
823 {
824         int answer=0;
825         while (val>>=1)
826                 answer++;
827         return answer;
828 }
829
830
831 /*
832 ================
833 R_ConcatRotations
834 ================
835 */
836 void R_ConcatRotations (float in1[3][3], float in2[3][3], float out[3][3])
837 {
838         out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] + in1[0][2] * in2[2][0];
839         out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] + in1[0][2] * in2[2][1];
840         out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] + in1[0][2] * in2[2][2];
841         out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] + in1[1][2] * in2[2][0];
842         out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] + in1[1][2] * in2[2][1];
843         out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] + in1[1][2] * in2[2][2];
844         out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] + in1[2][2] * in2[2][0];
845         out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] + in1[2][2] * in2[2][1];
846         out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] + in1[2][2] * in2[2][2];
847 }
848
849
850 /*
851 ================
852 R_ConcatTransforms
853 ================
854 */
855 void R_ConcatTransforms (float in1[3][4], float in2[3][4], float out[3][4])
856 {
857         out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] + in1[0][2] * in2[2][0];
858         out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] + in1[0][2] * in2[2][1];
859         out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] + in1[0][2] * in2[2][2];
860         out[0][3] = in1[0][0] * in2[0][3] + in1[0][1] * in2[1][3] + in1[0][2] * in2[2][3] + in1[0][3];
861         out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] + in1[1][2] * in2[2][0];
862         out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] + in1[1][2] * in2[2][1];
863         out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] + in1[1][2] * in2[2][2];
864         out[1][3] = in1[1][0] * in2[0][3] + in1[1][1] * in2[1][3] + in1[1][2] * in2[2][3] + in1[1][3];
865         out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] + in1[2][2] * in2[2][0];
866         out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] + in1[2][2] * in2[2][1];
867         out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] + in1[2][2] * in2[2][2];
868         out[2][3] = in1[2][0] * in2[0][3] + in1[2][1] * in2[1][3] + in1[2][2] * in2[2][3] + in1[2][3];
869 }
870
871
872 /*
873 ===================
874 FloorDivMod
875
876 Returns mathematically correct (floor-based) quotient and remainder for
877 numer and denom, both of which should contain no fractional part. The
878 quotient must fit in 32 bits.
879 ====================
880 */
881
882 void FloorDivMod (double numer, double denom, int *quotient,
883                 int *rem)
884 {
885         int             q, r;
886         double  x;
887
888 #ifndef PARANOID
889         if (denom <= 0.0)
890                 Sys_Error ("FloorDivMod: bad denominator %d\n", denom);
891
892 //      if ((floor(numer) != numer) || (floor(denom) != denom))
893 //              Sys_Error ("FloorDivMod: non-integer numer or denom %f %f\n",
894 //                              numer, denom);
895 #endif
896
897         if (numer >= 0.0)
898         {
899
900                 x = floor(numer / denom);
901                 q = (int)x;
902                 r = (int)floor(numer - (x * denom));
903         }
904         else
905         {
906         //
907         // perform operations with positive values, and fix mod to make floor-based
908         //
909                 x = floor(-numer / denom);
910                 q = -(int)x;
911                 r = (int)floor(-numer - (x * denom));
912                 if (r != 0)
913                 {
914                         q--;
915                         r = (int)denom - r;
916                 }
917         }
918
919         *quotient = q;
920         *rem = r;
921 }
922
923
924 /*
925 ===================
926 GreatestCommonDivisor
927 ====================
928 */
929 int GreatestCommonDivisor (int i1, int i2)
930 {
931         if (i1 > i2)
932         {
933                 if (i2 == 0)
934                         return (i1);
935                 return GreatestCommonDivisor (i2, i1 % i2);
936         }
937         else
938         {
939                 if (i1 == 0)
940                         return (i2);
941                 return GreatestCommonDivisor (i1, i2 % i1);
942         }
943 }
944
945
946 void Mathlib_Init(void)
947 {
948         int a;
949
950         // LordHavoc: setup 1.0f / N table for quick recipricols of integers
951         ixtable[0] = 0;
952         for (a = 1;a < 4096;a++)
953                 ixtable[a] = 1.0f / a;
954 }