]> git.xonotic.org Git - xonotic/darkplaces.git/blob - mathlib.c
add a note about a VectorVectors bug
[xonotic/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 "quakedef.h"
23
24 #include <math.h>
25
26 vec3_t vec3_origin = {0,0,0};
27 float ixtable[4096];
28
29 /*-----------------------------------------------------------------*/
30
31 float m_bytenormals[NUMVERTEXNORMALS][3] =
32 {
33 {-0.525731, 0.000000, 0.850651}, {-0.442863, 0.238856, 0.864188},
34 {-0.295242, 0.000000, 0.955423}, {-0.309017, 0.500000, 0.809017},
35 {-0.162460, 0.262866, 0.951056}, {0.000000, 0.000000, 1.000000},
36 {0.000000, 0.850651, 0.525731}, {-0.147621, 0.716567, 0.681718},
37 {0.147621, 0.716567, 0.681718}, {0.000000, 0.525731, 0.850651},
38 {0.309017, 0.500000, 0.809017}, {0.525731, 0.000000, 0.850651},
39 {0.295242, 0.000000, 0.955423}, {0.442863, 0.238856, 0.864188},
40 {0.162460, 0.262866, 0.951056}, {-0.681718, 0.147621, 0.716567},
41 {-0.809017, 0.309017, 0.500000}, {-0.587785, 0.425325, 0.688191},
42 {-0.850651, 0.525731, 0.000000}, {-0.864188, 0.442863, 0.238856},
43 {-0.716567, 0.681718, 0.147621}, {-0.688191, 0.587785, 0.425325},
44 {-0.500000, 0.809017, 0.309017}, {-0.238856, 0.864188, 0.442863},
45 {-0.425325, 0.688191, 0.587785}, {-0.716567, 0.681718, -0.147621},
46 {-0.500000, 0.809017, -0.309017}, {-0.525731, 0.850651, 0.000000},
47 {0.000000, 0.850651, -0.525731}, {-0.238856, 0.864188, -0.442863},
48 {0.000000, 0.955423, -0.295242}, {-0.262866, 0.951056, -0.162460},
49 {0.000000, 1.000000, 0.000000}, {0.000000, 0.955423, 0.295242},
50 {-0.262866, 0.951056, 0.162460}, {0.238856, 0.864188, 0.442863},
51 {0.262866, 0.951056, 0.162460}, {0.500000, 0.809017, 0.309017},
52 {0.238856, 0.864188, -0.442863}, {0.262866, 0.951056, -0.162460},
53 {0.500000, 0.809017, -0.309017}, {0.850651, 0.525731, 0.000000},
54 {0.716567, 0.681718, 0.147621}, {0.716567, 0.681718, -0.147621},
55 {0.525731, 0.850651, 0.000000}, {0.425325, 0.688191, 0.587785},
56 {0.864188, 0.442863, 0.238856}, {0.688191, 0.587785, 0.425325},
57 {0.809017, 0.309017, 0.500000}, {0.681718, 0.147621, 0.716567},
58 {0.587785, 0.425325, 0.688191}, {0.955423, 0.295242, 0.000000},
59 {1.000000, 0.000000, 0.000000}, {0.951056, 0.162460, 0.262866},
60 {0.850651, -0.525731, 0.000000}, {0.955423, -0.295242, 0.000000},
61 {0.864188, -0.442863, 0.238856}, {0.951056, -0.162460, 0.262866},
62 {0.809017, -0.309017, 0.500000}, {0.681718, -0.147621, 0.716567},
63 {0.850651, 0.000000, 0.525731}, {0.864188, 0.442863, -0.238856},
64 {0.809017, 0.309017, -0.500000}, {0.951056, 0.162460, -0.262866},
65 {0.525731, 0.000000, -0.850651}, {0.681718, 0.147621, -0.716567},
66 {0.681718, -0.147621, -0.716567}, {0.850651, 0.000000, -0.525731},
67 {0.809017, -0.309017, -0.500000}, {0.864188, -0.442863, -0.238856},
68 {0.951056, -0.162460, -0.262866}, {0.147621, 0.716567, -0.681718},
69 {0.309017, 0.500000, -0.809017}, {0.425325, 0.688191, -0.587785},
70 {0.442863, 0.238856, -0.864188}, {0.587785, 0.425325, -0.688191},
71 {0.688191, 0.587785, -0.425325}, {-0.147621, 0.716567, -0.681718},
72 {-0.309017, 0.500000, -0.809017}, {0.000000, 0.525731, -0.850651},
73 {-0.525731, 0.000000, -0.850651}, {-0.442863, 0.238856, -0.864188},
74 {-0.295242, 0.000000, -0.955423}, {-0.162460, 0.262866, -0.951056},
75 {0.000000, 0.000000, -1.000000}, {0.295242, 0.000000, -0.955423},
76 {0.162460, 0.262866, -0.951056}, {-0.442863, -0.238856, -0.864188},
77 {-0.309017, -0.500000, -0.809017}, {-0.162460, -0.262866, -0.951056},
78 {0.000000, -0.850651, -0.525731}, {-0.147621, -0.716567, -0.681718},
79 {0.147621, -0.716567, -0.681718}, {0.000000, -0.525731, -0.850651},
80 {0.309017, -0.500000, -0.809017}, {0.442863, -0.238856, -0.864188},
81 {0.162460, -0.262866, -0.951056}, {0.238856, -0.864188, -0.442863},
82 {0.500000, -0.809017, -0.309017}, {0.425325, -0.688191, -0.587785},
83 {0.716567, -0.681718, -0.147621}, {0.688191, -0.587785, -0.425325},
84 {0.587785, -0.425325, -0.688191}, {0.000000, -0.955423, -0.295242},
85 {0.000000, -1.000000, 0.000000}, {0.262866, -0.951056, -0.162460},
86 {0.000000, -0.850651, 0.525731}, {0.000000, -0.955423, 0.295242},
87 {0.238856, -0.864188, 0.442863}, {0.262866, -0.951056, 0.162460},
88 {0.500000, -0.809017, 0.309017}, {0.716567, -0.681718, 0.147621},
89 {0.525731, -0.850651, 0.000000}, {-0.238856, -0.864188, -0.442863},
90 {-0.500000, -0.809017, -0.309017}, {-0.262866, -0.951056, -0.162460},
91 {-0.850651, -0.525731, 0.000000}, {-0.716567, -0.681718, -0.147621},
92 {-0.716567, -0.681718, 0.147621}, {-0.525731, -0.850651, 0.000000},
93 {-0.500000, -0.809017, 0.309017}, {-0.238856, -0.864188, 0.442863},
94 {-0.262866, -0.951056, 0.162460}, {-0.864188, -0.442863, 0.238856},
95 {-0.809017, -0.309017, 0.500000}, {-0.688191, -0.587785, 0.425325},
96 {-0.681718, -0.147621, 0.716567}, {-0.442863, -0.238856, 0.864188},
97 {-0.587785, -0.425325, 0.688191}, {-0.309017, -0.500000, 0.809017},
98 {-0.147621, -0.716567, 0.681718}, {-0.425325, -0.688191, 0.587785},
99 {-0.162460, -0.262866, 0.951056}, {0.442863, -0.238856, 0.864188},
100 {0.162460, -0.262866, 0.951056}, {0.309017, -0.500000, 0.809017},
101 {0.147621, -0.716567, 0.681718}, {0.000000, -0.525731, 0.850651},
102 {0.425325, -0.688191, 0.587785}, {0.587785, -0.425325, 0.688191},
103 {0.688191, -0.587785, 0.425325}, {-0.955423, 0.295242, 0.000000},
104 {-0.951056, 0.162460, 0.262866}, {-1.000000, 0.000000, 0.000000},
105 {-0.850651, 0.000000, 0.525731}, {-0.955423, -0.295242, 0.000000},
106 {-0.951056, -0.162460, 0.262866}, {-0.864188, 0.442863, -0.238856},
107 {-0.951056, 0.162460, -0.262866}, {-0.809017, 0.309017, -0.500000},
108 {-0.864188, -0.442863, -0.238856}, {-0.951056, -0.162460, -0.262866},
109 {-0.809017, -0.309017, -0.500000}, {-0.681718, 0.147621, -0.716567},
110 {-0.681718, -0.147621, -0.716567}, {-0.850651, 0.000000, -0.525731},
111 {-0.688191, 0.587785, -0.425325}, {-0.587785, 0.425325, -0.688191},
112 {-0.425325, 0.688191, -0.587785}, {-0.425325, -0.688191, -0.587785},
113 {-0.587785, -0.425325, -0.688191}, {-0.688191, -0.587785, -0.425325},
114 };
115
116 #if 0
117 unsigned char NormalToByte(const 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(unsigned char 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 // assumes "src" is normalized
146 void PerpendicularVector( vec3_t dst, const vec3_t src )
147 {
148         // LordHavoc: optimized to death and beyond
149         int pos;
150         float minelem;
151
152         if (src[0])
153         {
154                 dst[0] = 0;
155                 if (src[1])
156                 {
157                         dst[1] = 0;
158                         if (src[2])
159                         {
160                                 dst[2] = 0;
161                                 pos = 0;
162                                 minelem = fabs(src[0]);
163                                 if (fabs(src[1]) < minelem)
164                                 {
165                                         pos = 1;
166                                         minelem = fabs(src[1]);
167                                 }
168                                 if (fabs(src[2]) < minelem)
169                                         pos = 2;
170
171                                 dst[pos] = 1;
172                                 dst[0] -= src[pos] * src[0];
173                                 dst[1] -= src[pos] * src[1];
174                                 dst[2] -= src[pos] * src[2];
175
176                                 // normalize the result
177                                 VectorNormalize(dst);
178                         }
179                         else
180                                 dst[2] = 1;
181                 }
182                 else
183                 {
184                         dst[1] = 1;
185                         dst[2] = 0;
186                 }
187         }
188         else
189         {
190                 dst[0] = 1;
191                 dst[1] = 0;
192                 dst[2] = 0;
193         }
194 }
195 #endif
196
197
198 // LordHavoc: like AngleVectors, but taking a forward vector instead of angles, useful!
199 void VectorVectors(const vec3_t forward, vec3_t right, vec3_t up)
200 {
201         float d;
202
203         right[0] = forward[2];
204         right[1] = -forward[0];
205         right[2] = forward[1];
206         // BUG!
207         //   assume forward = {sqrt(1/3), sqrt(1/3), -sqrt(1/3)}
208         //   then right will be {-sqrt(1/3), -sqrt(1/3), sqrt(1/3)}
209         //   PROBLEM?
210
211         d = DotProduct(forward, right);
212         VectorMA(right, -d, forward, right);
213         VectorNormalize(right);
214         CrossProduct(right, forward, up);
215         VectorNormalize(up); // CrossProduct in this case returns 'up thats length is not 1
216 }
217
218 void VectorVectorsDouble(const double *forward, double *right, double *up)
219 {
220         double d;
221
222         right[0] = forward[2];
223         right[1] = -forward[0];
224         right[2] = forward[1];
225
226         d = DotProduct(forward, right);
227         VectorMA(right, -d, forward, right);
228         VectorNormalize(right);
229         CrossProduct(right, forward, up);
230 }
231
232 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees )
233 {
234         float t0, t1;
235         float angle, c, s;
236         vec3_t vr, vu, vf;
237
238         angle = DEG2RAD(degrees);
239         c = cos(angle);
240         s = sin(angle);
241         VectorCopy(dir, vf);
242         VectorVectors(vf, vr, vu);
243
244         t0 = vr[0] *  c + vu[0] * -s;
245         t1 = vr[0] *  s + vu[0] *  c;
246         dst[0] = (t0 * vr[0] + t1 * vu[0] + vf[0] * vf[0]) * point[0]
247                + (t0 * vr[1] + t1 * vu[1] + vf[0] * vf[1]) * point[1]
248                + (t0 * vr[2] + t1 * vu[2] + vf[0] * vf[2]) * point[2];
249
250         t0 = vr[1] *  c + vu[1] * -s;
251         t1 = vr[1] *  s + vu[1] *  c;
252         dst[1] = (t0 * vr[0] + t1 * vu[0] + vf[1] * vf[0]) * point[0]
253                + (t0 * vr[1] + t1 * vu[1] + vf[1] * vf[1]) * point[1]
254                + (t0 * vr[2] + t1 * vu[2] + vf[1] * vf[2]) * point[2];
255
256         t0 = vr[2] *  c + vu[2] * -s;
257         t1 = vr[2] *  s + vu[2] *  c;
258         dst[2] = (t0 * vr[0] + t1 * vu[0] + vf[2] * vf[0]) * point[0]
259                + (t0 * vr[1] + t1 * vu[1] + vf[2] * vf[1]) * point[1]
260                + (t0 * vr[2] + t1 * vu[2] + vf[2] * vf[2]) * point[2];
261 }
262
263 /*-----------------------------------------------------------------*/
264
265 // returns the smallest integer greater than or equal to "value", or 0 if "value" is too big
266 unsigned int CeilPowerOf2(unsigned int value)
267 {
268         unsigned int ceilvalue;
269
270         if (value > (1U << (sizeof(int) * 8 - 1)))
271                 return 0;
272
273         ceilvalue = 1;
274         while (ceilvalue < value)
275                 ceilvalue <<= 1;
276
277         return ceilvalue;
278 }
279
280
281 /*-----------------------------------------------------------------*/
282
283
284 void PlaneClassify(mplane_t *p)
285 {
286         // for optimized plane comparisons
287         if (p->normal[0] == 1)
288                 p->type = 0;
289         else if (p->normal[1] == 1)
290                 p->type = 1;
291         else if (p->normal[2] == 1)
292                 p->type = 2;
293         else
294                 p->type = 3;
295         // for BoxOnPlaneSide
296         p->signbits = 0;
297         if (p->normal[0] < 0) // 1
298                 p->signbits |= 1;
299         if (p->normal[1] < 0) // 2
300                 p->signbits |= 2;
301         if (p->normal[2] < 0) // 4
302                 p->signbits |= 4;
303 }
304
305 int BoxOnPlaneSide(const vec3_t emins, const vec3_t emaxs, const mplane_t *p)
306 {
307         if (p->type < 3)
308                 return ((emaxs[p->type] >= p->dist) | ((emins[p->type] < p->dist) << 1));
309         switch(p->signbits)
310         {
311         default:
312         case 0: 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));
313         case 1: 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));
314         case 2: 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));
315         case 3: 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));
316         case 4: 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));
317         case 5: 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));
318         case 6: 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));
319         case 7: 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));
320         }
321 }
322
323 #if 0
324 int BoxOnPlaneSide_Separate(const vec3_t emins, const vec3_t emaxs, const vec3_t normal, const vec_t dist)
325 {
326         switch((normal[0] < 0) | ((normal[1] < 0) << 1) | ((normal[2] < 0) << 2))
327         {
328         default:
329         case 0: return (((normal[0] * emaxs[0] + normal[1] * emaxs[1] + normal[2] * emaxs[2]) >= dist) | (((normal[0] * emins[0] + normal[1] * emins[1] + normal[2] * emins[2]) < dist) << 1));
330         case 1: return (((normal[0] * emins[0] + normal[1] * emaxs[1] + normal[2] * emaxs[2]) >= dist) | (((normal[0] * emaxs[0] + normal[1] * emins[1] + normal[2] * emins[2]) < dist) << 1));
331         case 2: return (((normal[0] * emaxs[0] + normal[1] * emins[1] + normal[2] * emaxs[2]) >= dist) | (((normal[0] * emins[0] + normal[1] * emaxs[1] + normal[2] * emins[2]) < dist) << 1));
332         case 3: return (((normal[0] * emins[0] + normal[1] * emins[1] + normal[2] * emaxs[2]) >= dist) | (((normal[0] * emaxs[0] + normal[1] * emaxs[1] + normal[2] * emins[2]) < dist) << 1));
333         case 4: return (((normal[0] * emaxs[0] + normal[1] * emaxs[1] + normal[2] * emins[2]) >= dist) | (((normal[0] * emins[0] + normal[1] * emins[1] + normal[2] * emaxs[2]) < dist) << 1));
334         case 5: return (((normal[0] * emins[0] + normal[1] * emaxs[1] + normal[2] * emins[2]) >= dist) | (((normal[0] * emaxs[0] + normal[1] * emins[1] + normal[2] * emaxs[2]) < dist) << 1));
335         case 6: return (((normal[0] * emaxs[0] + normal[1] * emins[1] + normal[2] * emins[2]) >= dist) | (((normal[0] * emins[0] + normal[1] * emaxs[1] + normal[2] * emaxs[2]) < dist) << 1));
336         case 7: return (((normal[0] * emins[0] + normal[1] * emins[1] + normal[2] * emins[2]) >= dist) | (((normal[0] * emaxs[0] + normal[1] * emaxs[1] + normal[2] * emaxs[2]) < dist) << 1));
337         }
338 }
339 #endif
340
341 void BoxPlaneCorners(const vec3_t emins, const vec3_t emaxs, const mplane_t *p, vec3_t outnear, vec3_t outfar)
342 {
343         if (p->type < 3)
344         {
345                 outnear[0] = outnear[1] = outnear[2] = outfar[0] = outfar[1] = outfar[2] = 0;
346                 outnear[p->type] = emins[p->type];
347                 outfar[p->type] = emaxs[p->type];
348                 return;
349         }
350         switch(p->signbits)
351         {
352         default:
353         case 0: outnear[0] = emaxs[0];outnear[1] = emaxs[1];outnear[2] = emaxs[2];outfar[0] = emins[0];outfar[1] = emins[1];outfar[2] = emins[2];break;
354         case 1: outnear[0] = emins[0];outnear[1] = emaxs[1];outnear[2] = emaxs[2];outfar[0] = emaxs[0];outfar[1] = emins[1];outfar[2] = emins[2];break;
355         case 2: outnear[0] = emaxs[0];outnear[1] = emins[1];outnear[2] = emaxs[2];outfar[0] = emins[0];outfar[1] = emaxs[1];outfar[2] = emins[2];break;
356         case 3: outnear[0] = emins[0];outnear[1] = emins[1];outnear[2] = emaxs[2];outfar[0] = emaxs[0];outfar[1] = emaxs[1];outfar[2] = emins[2];break;
357         case 4: outnear[0] = emaxs[0];outnear[1] = emaxs[1];outnear[2] = emins[2];outfar[0] = emins[0];outfar[1] = emins[1];outfar[2] = emaxs[2];break;
358         case 5: outnear[0] = emins[0];outnear[1] = emaxs[1];outnear[2] = emins[2];outfar[0] = emaxs[0];outfar[1] = emins[1];outfar[2] = emaxs[2];break;
359         case 6: outnear[0] = emaxs[0];outnear[1] = emins[1];outnear[2] = emins[2];outfar[0] = emins[0];outfar[1] = emaxs[1];outfar[2] = emaxs[2];break;
360         case 7: outnear[0] = emins[0];outnear[1] = emins[1];outnear[2] = emins[2];outfar[0] = emaxs[0];outfar[1] = emaxs[1];outfar[2] = emaxs[2];break;
361         }
362 }
363
364 void BoxPlaneCorners_Separate(const vec3_t emins, const vec3_t emaxs, const vec3_t normal, vec3_t outnear, vec3_t outfar)
365 {
366         switch((normal[0] < 0) | ((normal[1] < 0) << 1) | ((normal[2] < 0) << 2))
367         {
368         default:
369         case 0: outnear[0] = emaxs[0];outnear[1] = emaxs[1];outnear[2] = emaxs[2];outfar[0] = emins[0];outfar[1] = emins[1];outfar[2] = emins[2];break;
370         case 1: outnear[0] = emins[0];outnear[1] = emaxs[1];outnear[2] = emaxs[2];outfar[0] = emaxs[0];outfar[1] = emins[1];outfar[2] = emins[2];break;
371         case 2: outnear[0] = emaxs[0];outnear[1] = emins[1];outnear[2] = emaxs[2];outfar[0] = emins[0];outfar[1] = emaxs[1];outfar[2] = emins[2];break;
372         case 3: outnear[0] = emins[0];outnear[1] = emins[1];outnear[2] = emaxs[2];outfar[0] = emaxs[0];outfar[1] = emaxs[1];outfar[2] = emins[2];break;
373         case 4: outnear[0] = emaxs[0];outnear[1] = emaxs[1];outnear[2] = emins[2];outfar[0] = emins[0];outfar[1] = emins[1];outfar[2] = emaxs[2];break;
374         case 5: outnear[0] = emins[0];outnear[1] = emaxs[1];outnear[2] = emins[2];outfar[0] = emaxs[0];outfar[1] = emins[1];outfar[2] = emaxs[2];break;
375         case 6: outnear[0] = emaxs[0];outnear[1] = emins[1];outnear[2] = emins[2];outfar[0] = emins[0];outfar[1] = emaxs[1];outfar[2] = emaxs[2];break;
376         case 7: outnear[0] = emins[0];outnear[1] = emins[1];outnear[2] = emins[2];outfar[0] = emaxs[0];outfar[1] = emaxs[1];outfar[2] = emaxs[2];break;
377         }
378 }
379
380 void BoxPlaneCornerDistances(const vec3_t emins, const vec3_t emaxs, const mplane_t *p, vec_t *outneardist, vec_t *outfardist)
381 {
382         if (p->type < 3)
383         {
384                 *outneardist = emins[p->type] - p->dist;
385                 *outfardist = emaxs[p->type] - p->dist;
386                 return;
387         }
388         switch(p->signbits)
389         {
390         default:
391         case 0: *outneardist = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2] - p->dist;*outfardist = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2] - p->dist;break;
392         case 1: *outneardist = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2] - p->dist;*outfardist = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2] - p->dist;break;
393         case 2: *outneardist = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2] - p->dist;*outfardist = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2] - p->dist;break;
394         case 3: *outneardist = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2] - p->dist;*outfardist = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2] - p->dist;break;
395         case 4: *outneardist = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2] - p->dist;*outfardist = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2] - p->dist;break;
396         case 5: *outneardist = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2] - p->dist;*outfardist = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2] - p->dist;break;
397         case 6: *outneardist = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2] - p->dist;*outfardist = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2] - p->dist;break;
398         case 7: *outneardist = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2] - p->dist;*outfardist = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2] - p->dist;break;
399         }
400 }
401
402 void BoxPlaneCornerDistances_Separate(const vec3_t emins, const vec3_t emaxs, const vec3_t normal, vec_t *outneardist, vec_t *outfardist)
403 {
404         switch((normal[0] < 0) | ((normal[1] < 0) << 1) | ((normal[2] < 0) << 2))
405         {
406         default:
407         case 0: *outneardist = normal[0] * emaxs[0] + normal[1] * emaxs[1] + normal[2] * emaxs[2];*outfardist = normal[0] * emins[0] + normal[1] * emins[1] + normal[2] * emins[2];break;
408         case 1: *outneardist = normal[0] * emins[0] + normal[1] * emaxs[1] + normal[2] * emaxs[2];*outfardist = normal[0] * emaxs[0] + normal[1] * emins[1] + normal[2] * emins[2];break;
409         case 2: *outneardist = normal[0] * emaxs[0] + normal[1] * emins[1] + normal[2] * emaxs[2];*outfardist = normal[0] * emins[0] + normal[1] * emaxs[1] + normal[2] * emins[2];break;
410         case 3: *outneardist = normal[0] * emins[0] + normal[1] * emins[1] + normal[2] * emaxs[2];*outfardist = normal[0] * emaxs[0] + normal[1] * emaxs[1] + normal[2] * emins[2];break;
411         case 4: *outneardist = normal[0] * emaxs[0] + normal[1] * emaxs[1] + normal[2] * emins[2];*outfardist = normal[0] * emins[0] + normal[1] * emins[1] + normal[2] * emaxs[2];break;
412         case 5: *outneardist = normal[0] * emins[0] + normal[1] * emaxs[1] + normal[2] * emins[2];*outfardist = normal[0] * emaxs[0] + normal[1] * emins[1] + normal[2] * emaxs[2];break;
413         case 6: *outneardist = normal[0] * emaxs[0] + normal[1] * emins[1] + normal[2] * emins[2];*outfardist = normal[0] * emins[0] + normal[1] * emaxs[1] + normal[2] * emaxs[2];break;
414         case 7: *outneardist = normal[0] * emins[0] + normal[1] * emins[1] + normal[2] * emins[2];*outfardist = normal[0] * emaxs[0] + normal[1] * emaxs[1] + normal[2] * emaxs[2];break;
415         }
416 }
417
418 void AngleVectors (const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
419 {
420         double angle, sr, sp, sy, cr, cp, cy;
421
422         angle = angles[YAW] * (M_PI*2 / 360);
423         sy = sin(angle);
424         cy = cos(angle);
425         angle = angles[PITCH] * (M_PI*2 / 360);
426         sp = sin(angle);
427         cp = cos(angle);
428         if (forward)
429         {
430                 forward[0] = cp*cy;
431                 forward[1] = cp*sy;
432                 forward[2] = -sp;
433         }
434         if (right || up)
435         {
436                 if (angles[ROLL])
437                 {
438                         angle = angles[ROLL] * (M_PI*2 / 360);
439                         sr = sin(angle);
440                         cr = cos(angle);
441                         if (right)
442                         {
443                                 right[0] = -1*(sr*sp*cy+cr*-sy);
444                                 right[1] = -1*(sr*sp*sy+cr*cy);
445                                 right[2] = -1*(sr*cp);
446                         }
447                         if (up)
448                         {
449                                 up[0] = (cr*sp*cy+-sr*-sy);
450                                 up[1] = (cr*sp*sy+-sr*cy);
451                                 up[2] = cr*cp;
452                         }
453                 }
454                 else
455                 {
456                         if (right)
457                         {
458                                 right[0] = sy;
459                                 right[1] = -cy;
460                                 right[2] = 0;
461                         }
462                         if (up)
463                         {
464                                 up[0] = (sp*cy);
465                                 up[1] = (sp*sy);
466                                 up[2] = cp;
467                         }
468                 }
469         }
470 }
471
472 void AngleVectorsFLU (const vec3_t angles, vec3_t forward, vec3_t left, vec3_t up)
473 {
474         double angle, sr, sp, sy, cr, cp, cy;
475
476         angle = angles[YAW] * (M_PI*2 / 360);
477         sy = sin(angle);
478         cy = cos(angle);
479         angle = angles[PITCH] * (M_PI*2 / 360);
480         sp = sin(angle);
481         cp = cos(angle);
482         if (forward)
483         {
484                 forward[0] = cp*cy;
485                 forward[1] = cp*sy;
486                 forward[2] = -sp;
487         }
488         if (left || up)
489         {
490                 if (angles[ROLL])
491                 {
492                         angle = angles[ROLL] * (M_PI*2 / 360);
493                         sr = sin(angle);
494                         cr = cos(angle);
495                         if (left)
496                         {
497                                 left[0] = sr*sp*cy+cr*-sy;
498                                 left[1] = sr*sp*sy+cr*cy;
499                                 left[2] = sr*cp;
500                         }
501                         if (up)
502                         {
503                                 up[0] = cr*sp*cy+-sr*-sy;
504                                 up[1] = cr*sp*sy+-sr*cy;
505                                 up[2] = cr*cp;
506                         }
507                 }
508                 else
509                 {
510                         if (left)
511                         {
512                                 left[0] = -sy;
513                                 left[1] = cy;
514                                 left[2] = 0;
515                         }
516                         if (up)
517                         {
518                                 up[0] = sp*cy;
519                                 up[1] = sp*sy;
520                                 up[2] = cp;
521                         }
522                 }
523         }
524 }
525
526 // LordHavoc: calculates pitch/yaw/roll angles from forward and up vectors
527 void AnglesFromVectors (vec3_t angles, const vec3_t forward, const vec3_t up, qboolean flippitch)
528 {
529         if (forward[0] == 0 && forward[1] == 0)
530         {
531                 if(forward[2] > 0)
532                 {
533                         angles[PITCH] = -M_PI * 0.5;
534                         angles[YAW] = up ? atan2(-up[1], -up[0]) : 0;
535                 }
536                 else
537                 {
538                         angles[PITCH] = M_PI * 0.5;
539                         angles[YAW] = up ? atan2(up[1], up[0]) : 0;
540                 }
541                 angles[ROLL] = 0;
542         }
543         else
544         {
545                 angles[YAW] = atan2(forward[1], forward[0]);
546                 angles[PITCH] = -atan2(forward[2], sqrt(forward[0]*forward[0] + forward[1]*forward[1]));
547                 if (up)
548                 {
549                         vec_t cp = cos(angles[PITCH]), sp = sin(angles[PITCH]);
550                         vec_t cy = cos(angles[YAW]), sy = sin(angles[YAW]);
551                         vec3_t tleft, tup;
552                         tleft[0] = -sy;
553                         tleft[1] = cy;
554                         tleft[2] = 0;
555                         tup[0] = sp*cy;
556                         tup[1] = sp*sy;
557                         tup[2] = cp;
558                         angles[ROLL] = -atan2(DotProduct(up, tleft), DotProduct(up, tup));
559                 }
560                 else
561                         angles[ROLL] = 0;
562         }
563
564         // now convert radians to degrees, and make all values positive
565         VectorScale(angles, 180.0 / M_PI, angles);
566         if (flippitch)
567                 angles[PITCH] *= -1;
568         if (angles[PITCH] < 0) angles[PITCH] += 360;
569         if (angles[YAW] < 0) angles[YAW] += 360;
570         if (angles[ROLL] < 0) angles[ROLL] += 360;
571
572 #if 0
573 {
574         // debugging code
575         vec3_t tforward, tleft, tup, nforward, nup;
576         VectorCopy(forward, nforward);
577         VectorNormalize(nforward);
578         if (up)
579         {
580                 VectorCopy(up, nup);
581                 VectorNormalize(nup);
582                 AngleVectors(angles, tforward, tleft, tup);
583                 if (VectorDistance(tforward, nforward) > 0.01 || VectorDistance(tup, nup) > 0.01)
584                 {
585                         Con_Printf("vectoangles('%f %f %f', '%f %f %f') = %f %f %f\n", nforward[0], nforward[1], nforward[2], nup[0], nup[1], nup[2], angles[0], angles[1], angles[2]);
586                         Con_Printf("^3But that is '%f %f %f', '%f %f %f'\n", tforward[0], tforward[1], tforward[2], tup[0], tup[1], tup[2]);
587                 }
588         }
589         else
590         {
591                 AngleVectors(angles, tforward, tleft, tup);
592                 if (VectorDistance(tforward, nforward) > 0.01)
593                 {
594                         Con_Printf("vectoangles('%f %f %f') = %f %f %f\n", nforward[0], nforward[1], nforward[2], angles[0], angles[1], angles[2]);
595                         Con_Printf("^3But that is '%f %f %f'\n", tforward[0], tforward[1], tforward[2]);
596                 }
597         }
598 }
599 #endif
600 }
601
602 #if 0
603 void AngleMatrix (const vec3_t angles, const vec3_t translate, vec_t matrix[][4])
604 {
605         double angle, sr, sp, sy, cr, cp, cy;
606
607         angle = angles[YAW] * (M_PI*2 / 360);
608         sy = sin(angle);
609         cy = cos(angle);
610         angle = angles[PITCH] * (M_PI*2 / 360);
611         sp = sin(angle);
612         cp = cos(angle);
613         angle = angles[ROLL] * (M_PI*2 / 360);
614         sr = sin(angle);
615         cr = cos(angle);
616         matrix[0][0] = cp*cy;
617         matrix[0][1] = sr*sp*cy+cr*-sy;
618         matrix[0][2] = cr*sp*cy+-sr*-sy;
619         matrix[0][3] = translate[0];
620         matrix[1][0] = cp*sy;
621         matrix[1][1] = sr*sp*sy+cr*cy;
622         matrix[1][2] = cr*sp*sy+-sr*cy;
623         matrix[1][3] = translate[1];
624         matrix[2][0] = -sp;
625         matrix[2][1] = sr*cp;
626         matrix[2][2] = cr*cp;
627         matrix[2][3] = translate[2];
628 }
629 #endif
630
631
632 // LordHavoc: renamed this to Length, and made the normal one a #define
633 float VectorNormalizeLength (vec3_t v)
634 {
635         float length, ilength;
636
637         length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
638         length = sqrt (length);
639
640         if (length)
641         {
642                 ilength = 1/length;
643                 v[0] *= ilength;
644                 v[1] *= ilength;
645                 v[2] *= ilength;
646         }
647
648         return length;
649
650 }
651
652
653 /*
654 ================
655 R_ConcatRotations
656 ================
657 */
658 void R_ConcatRotations (const float in1[3*3], const float in2[3*3], float out[3*3])
659 {
660         out[0*3+0] = in1[0*3+0] * in2[0*3+0] + in1[0*3+1] * in2[1*3+0] + in1[0*3+2] * in2[2*3+0];
661         out[0*3+1] = in1[0*3+0] * in2[0*3+1] + in1[0*3+1] * in2[1*3+1] + in1[0*3+2] * in2[2*3+1];
662         out[0*3+2] = in1[0*3+0] * in2[0*3+2] + in1[0*3+1] * in2[1*3+2] + in1[0*3+2] * in2[2*3+2];
663         out[1*3+0] = in1[1*3+0] * in2[0*3+0] + in1[1*3+1] * in2[1*3+0] + in1[1*3+2] * in2[2*3+0];
664         out[1*3+1] = in1[1*3+0] * in2[0*3+1] + in1[1*3+1] * in2[1*3+1] + in1[1*3+2] * in2[2*3+1];
665         out[1*3+2] = in1[1*3+0] * in2[0*3+2] + in1[1*3+1] * in2[1*3+2] + in1[1*3+2] * in2[2*3+2];
666         out[2*3+0] = in1[2*3+0] * in2[0*3+0] + in1[2*3+1] * in2[1*3+0] + in1[2*3+2] * in2[2*3+0];
667         out[2*3+1] = in1[2*3+0] * in2[0*3+1] + in1[2*3+1] * in2[1*3+1] + in1[2*3+2] * in2[2*3+1];
668         out[2*3+2] = in1[2*3+0] * in2[0*3+2] + in1[2*3+1] * in2[1*3+2] + in1[2*3+2] * in2[2*3+2];
669 }
670
671
672 /*
673 ================
674 R_ConcatTransforms
675 ================
676 */
677 void R_ConcatTransforms (const float in1[3*4], const float in2[3*4], float out[3*4])
678 {
679         out[0*4+0] = in1[0*4+0] * in2[0*4+0] + in1[0*4+1] * in2[1*4+0] + in1[0*4+2] * in2[2*4+0];
680         out[0*4+1] = in1[0*4+0] * in2[0*4+1] + in1[0*4+1] * in2[1*4+1] + in1[0*4+2] * in2[2*4+1];
681         out[0*4+2] = in1[0*4+0] * in2[0*4+2] + in1[0*4+1] * in2[1*4+2] + in1[0*4+2] * in2[2*4+2];
682         out[0*4+3] = in1[0*4+0] * in2[0*4+3] + in1[0*4+1] * in2[1*4+3] + in1[0*4+2] * in2[2*4+3] + in1[0*4+3];
683         out[1*4+0] = in1[1*4+0] * in2[0*4+0] + in1[1*4+1] * in2[1*4+0] + in1[1*4+2] * in2[2*4+0];
684         out[1*4+1] = in1[1*4+0] * in2[0*4+1] + in1[1*4+1] * in2[1*4+1] + in1[1*4+2] * in2[2*4+1];
685         out[1*4+2] = in1[1*4+0] * in2[0*4+2] + in1[1*4+1] * in2[1*4+2] + in1[1*4+2] * in2[2*4+2];
686         out[1*4+3] = in1[1*4+0] * in2[0*4+3] + in1[1*4+1] * in2[1*4+3] + in1[1*4+2] * in2[2*4+3] + in1[1*4+3];
687         out[2*4+0] = in1[2*4+0] * in2[0*4+0] + in1[2*4+1] * in2[1*4+0] + in1[2*4+2] * in2[2*4+0];
688         out[2*4+1] = in1[2*4+0] * in2[0*4+1] + in1[2*4+1] * in2[1*4+1] + in1[2*4+2] * in2[2*4+1];
689         out[2*4+2] = in1[2*4+0] * in2[0*4+2] + in1[2*4+1] * in2[1*4+2] + in1[2*4+2] * in2[2*4+2];
690         out[2*4+3] = in1[2*4+0] * in2[0*4+3] + in1[2*4+1] * in2[1*4+3] + in1[2*4+2] * in2[2*4+3] + in1[2*4+3];
691 }
692
693 float RadiusFromBounds (const vec3_t mins, const vec3_t maxs)
694 {
695         vec3_t m1, m2;
696         VectorMultiply(mins, mins, m1);
697         VectorMultiply(maxs, maxs, m2);
698         return sqrt(max(m1[0], m2[0]) + max(m1[1], m2[1]) + max(m1[2], m2[2]));
699 }
700
701 float RadiusFromBoundsAndOrigin (const vec3_t mins, const vec3_t maxs, const vec3_t origin)
702 {
703         vec3_t m1, m2;
704         VectorSubtract(mins, origin, m1);VectorMultiply(m1, m1, m1);
705         VectorSubtract(maxs, origin, m2);VectorMultiply(m2, m2, m2);
706         return sqrt(max(m1[0], m2[0]) + max(m1[1], m2[1]) + max(m1[2], m2[2]));
707 }
708
709 void Mathlib_Init(void)
710 {
711         int a;
712
713         // LordHavoc: setup 1.0f / N table for quick recipricols of integers
714         ixtable[0] = 0;
715         for (a = 1;a < 4096;a++)
716                 ixtable[a] = 1.0f / a;
717 }
718
719 #include "matrixlib.h"
720
721 void Matrix4x4_Print(const matrix4x4_t *in)
722 {
723         Con_Printf("%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n"
724         , in->m[0][0], in->m[0][1], in->m[0][2], in->m[0][3]
725         , in->m[1][0], in->m[1][1], in->m[1][2], in->m[1][3]
726         , in->m[2][0], in->m[2][1], in->m[2][2], in->m[2][3]
727         , in->m[3][0], in->m[3][1], in->m[3][2], in->m[3][3]);
728 }
729
730 int Math_atov(const char *s, vec3_t out)
731 {
732         int i;
733         VectorClear(out);
734         if (*s == '\'')
735                 s++;
736         for (i = 0;i < 3;i++)
737         {
738                 while (*s == ' ' || *s == '\t')
739                         s++;
740                 out[i] = atof (s);
741                 if (out[i] == 0 && *s != '-' && *s != '+' && (*s < '0' || *s > '9'))
742                         break; // not a number
743                 while (*s && *s != ' ' && *s !='\t' && *s != '\'')
744                         s++;
745                 if (*s == '\'')
746                         break;
747         }
748         return i;
749 }
750
751 void BoxFromPoints(vec3_t mins, vec3_t maxs, int numpoints, vec_t *point3f)
752 {
753         int i;
754         VectorCopy(point3f, mins);
755         VectorCopy(point3f, maxs);
756         for (i = 1, point3f += 3;i < numpoints;i++, point3f += 3)
757         {
758                 mins[0] = min(mins[0], point3f[0]);maxs[0] = max(maxs[0], point3f[0]);
759                 mins[1] = min(mins[1], point3f[1]);maxs[1] = max(maxs[1], point3f[1]);
760                 mins[2] = min(mins[2], point3f[2]);maxs[2] = max(maxs[2], point3f[2]);
761         }
762 }
763
764 // LordHavoc: this has to be done right or you get severe precision breakdown
765 int LoopingFrameNumberFromDouble(double t, int loopframes)
766 {
767         if (loopframes)
768                 return (int)(t - floor(t/loopframes)*loopframes);
769         else
770                 return (int)t;
771 }
772