36e858dee589dca6c48541c8730375e5a6340db1
[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 <math.h>
23 #include "quakedef.h"
24
25 vec3_t vec3_origin = {0,0,0};
26 float ixtable[4096];
27
28 /*-----------------------------------------------------------------*/
29
30 float m_bytenormals[NUMVERTEXNORMALS][3] =
31 {
32 {-0.525731, 0.000000, 0.850651}, {-0.442863, 0.238856, 0.864188},
33 {-0.295242, 0.000000, 0.955423}, {-0.309017, 0.500000, 0.809017},
34 {-0.162460, 0.262866, 0.951056}, {0.000000, 0.000000, 1.000000},
35 {0.000000, 0.850651, 0.525731}, {-0.147621, 0.716567, 0.681718},
36 {0.147621, 0.716567, 0.681718}, {0.000000, 0.525731, 0.850651},
37 {0.309017, 0.500000, 0.809017}, {0.525731, 0.000000, 0.850651},
38 {0.295242, 0.000000, 0.955423}, {0.442863, 0.238856, 0.864188},
39 {0.162460, 0.262866, 0.951056}, {-0.681718, 0.147621, 0.716567},
40 {-0.809017, 0.309017, 0.500000}, {-0.587785, 0.425325, 0.688191},
41 {-0.850651, 0.525731, 0.000000}, {-0.864188, 0.442863, 0.238856},
42 {-0.716567, 0.681718, 0.147621}, {-0.688191, 0.587785, 0.425325},
43 {-0.500000, 0.809017, 0.309017}, {-0.238856, 0.864188, 0.442863},
44 {-0.425325, 0.688191, 0.587785}, {-0.716567, 0.681718, -0.147621},
45 {-0.500000, 0.809017, -0.309017}, {-0.525731, 0.850651, 0.000000},
46 {0.000000, 0.850651, -0.525731}, {-0.238856, 0.864188, -0.442863},
47 {0.000000, 0.955423, -0.295242}, {-0.262866, 0.951056, -0.162460},
48 {0.000000, 1.000000, 0.000000}, {0.000000, 0.955423, 0.295242},
49 {-0.262866, 0.951056, 0.162460}, {0.238856, 0.864188, 0.442863},
50 {0.262866, 0.951056, 0.162460}, {0.500000, 0.809017, 0.309017},
51 {0.238856, 0.864188, -0.442863}, {0.262866, 0.951056, -0.162460},
52 {0.500000, 0.809017, -0.309017}, {0.850651, 0.525731, 0.000000},
53 {0.716567, 0.681718, 0.147621}, {0.716567, 0.681718, -0.147621},
54 {0.525731, 0.850651, 0.000000}, {0.425325, 0.688191, 0.587785},
55 {0.864188, 0.442863, 0.238856}, {0.688191, 0.587785, 0.425325},
56 {0.809017, 0.309017, 0.500000}, {0.681718, 0.147621, 0.716567},
57 {0.587785, 0.425325, 0.688191}, {0.955423, 0.295242, 0.000000},
58 {1.000000, 0.000000, 0.000000}, {0.951056, 0.162460, 0.262866},
59 {0.850651, -0.525731, 0.000000}, {0.955423, -0.295242, 0.000000},
60 {0.864188, -0.442863, 0.238856}, {0.951056, -0.162460, 0.262866},
61 {0.809017, -0.309017, 0.500000}, {0.681718, -0.147621, 0.716567},
62 {0.850651, 0.000000, 0.525731}, {0.864188, 0.442863, -0.238856},
63 {0.809017, 0.309017, -0.500000}, {0.951056, 0.162460, -0.262866},
64 {0.525731, 0.000000, -0.850651}, {0.681718, 0.147621, -0.716567},
65 {0.681718, -0.147621, -0.716567}, {0.850651, 0.000000, -0.525731},
66 {0.809017, -0.309017, -0.500000}, {0.864188, -0.442863, -0.238856},
67 {0.951056, -0.162460, -0.262866}, {0.147621, 0.716567, -0.681718},
68 {0.309017, 0.500000, -0.809017}, {0.425325, 0.688191, -0.587785},
69 {0.442863, 0.238856, -0.864188}, {0.587785, 0.425325, -0.688191},
70 {0.688191, 0.587785, -0.425325}, {-0.147621, 0.716567, -0.681718},
71 {-0.309017, 0.500000, -0.809017}, {0.000000, 0.525731, -0.850651},
72 {-0.525731, 0.000000, -0.850651}, {-0.442863, 0.238856, -0.864188},
73 {-0.295242, 0.000000, -0.955423}, {-0.162460, 0.262866, -0.951056},
74 {0.000000, 0.000000, -1.000000}, {0.295242, 0.000000, -0.955423},
75 {0.162460, 0.262866, -0.951056}, {-0.442863, -0.238856, -0.864188},
76 {-0.309017, -0.500000, -0.809017}, {-0.162460, -0.262866, -0.951056},
77 {0.000000, -0.850651, -0.525731}, {-0.147621, -0.716567, -0.681718},
78 {0.147621, -0.716567, -0.681718}, {0.000000, -0.525731, -0.850651},
79 {0.309017, -0.500000, -0.809017}, {0.442863, -0.238856, -0.864188},
80 {0.162460, -0.262866, -0.951056}, {0.238856, -0.864188, -0.442863},
81 {0.500000, -0.809017, -0.309017}, {0.425325, -0.688191, -0.587785},
82 {0.716567, -0.681718, -0.147621}, {0.688191, -0.587785, -0.425325},
83 {0.587785, -0.425325, -0.688191}, {0.000000, -0.955423, -0.295242},
84 {0.000000, -1.000000, 0.000000}, {0.262866, -0.951056, -0.162460},
85 {0.000000, -0.850651, 0.525731}, {0.000000, -0.955423, 0.295242},
86 {0.238856, -0.864188, 0.442863}, {0.262866, -0.951056, 0.162460},
87 {0.500000, -0.809017, 0.309017}, {0.716567, -0.681718, 0.147621},
88 {0.525731, -0.850651, 0.000000}, {-0.238856, -0.864188, -0.442863},
89 {-0.500000, -0.809017, -0.309017}, {-0.262866, -0.951056, -0.162460},
90 {-0.850651, -0.525731, 0.000000}, {-0.716567, -0.681718, -0.147621},
91 {-0.716567, -0.681718, 0.147621}, {-0.525731, -0.850651, 0.000000},
92 {-0.500000, -0.809017, 0.309017}, {-0.238856, -0.864188, 0.442863},
93 {-0.262866, -0.951056, 0.162460}, {-0.864188, -0.442863, 0.238856},
94 {-0.809017, -0.309017, 0.500000}, {-0.688191, -0.587785, 0.425325},
95 {-0.681718, -0.147621, 0.716567}, {-0.442863, -0.238856, 0.864188},
96 {-0.587785, -0.425325, 0.688191}, {-0.309017, -0.500000, 0.809017},
97 {-0.147621, -0.716567, 0.681718}, {-0.425325, -0.688191, 0.587785},
98 {-0.162460, -0.262866, 0.951056}, {0.442863, -0.238856, 0.864188},
99 {0.162460, -0.262866, 0.951056}, {0.309017, -0.500000, 0.809017},
100 {0.147621, -0.716567, 0.681718}, {0.000000, -0.525731, 0.850651},
101 {0.425325, -0.688191, 0.587785}, {0.587785, -0.425325, 0.688191},
102 {0.688191, -0.587785, 0.425325}, {-0.955423, 0.295242, 0.000000},
103 {-0.951056, 0.162460, 0.262866}, {-1.000000, 0.000000, 0.000000},
104 {-0.850651, 0.000000, 0.525731}, {-0.955423, -0.295242, 0.000000},
105 {-0.951056, -0.162460, 0.262866}, {-0.864188, 0.442863, -0.238856},
106 {-0.951056, 0.162460, -0.262866}, {-0.809017, 0.309017, -0.500000},
107 {-0.864188, -0.442863, -0.238856}, {-0.951056, -0.162460, -0.262866},
108 {-0.809017, -0.309017, -0.500000}, {-0.681718, 0.147621, -0.716567},
109 {-0.681718, -0.147621, -0.716567}, {-0.850651, 0.000000, -0.525731},
110 {-0.688191, 0.587785, -0.425325}, {-0.587785, 0.425325, -0.688191},
111 {-0.425325, 0.688191, -0.587785}, {-0.425325, -0.688191, -0.587785},
112 {-0.587785, -0.425325, -0.688191}, {-0.688191, -0.587785, -0.425325},
113 };
114
115 #if 0
116 unsigned char NormalToByte(const vec3_t n)
117 {
118         int i, best;
119         float bestdistance, distance;
120
121         best = 0;
122         bestdistance = DotProduct (n, m_bytenormals[0]);
123         for (i = 1;i < NUMVERTEXNORMALS;i++)
124         {
125                 distance = DotProduct (n, m_bytenormals[i]);
126                 if (distance > bestdistance)
127                 {
128                         bestdistance = distance;
129                         best = i;
130                 }
131         }
132         return best;
133 }
134
135 // note: uses byte partly to force unsigned for the validity check
136 void ByteToNormal(unsigned char num, vec3_t n)
137 {
138         if (num < NUMVERTEXNORMALS)
139                 VectorCopy(m_bytenormals[num], n);
140         else
141                 VectorClear(n); // FIXME: complain?
142 }
143
144 // assumes "src" is normalized
145 void PerpendicularVector( vec3_t dst, const vec3_t src )
146 {
147         // LordHavoc: optimized to death and beyond
148         int pos;
149         float minelem;
150
151         if (src[0])
152         {
153                 dst[0] = 0;
154                 if (src[1])
155                 {
156                         dst[1] = 0;
157                         if (src[2])
158                         {
159                                 dst[2] = 0;
160                                 pos = 0;
161                                 minelem = fabs(src[0]);
162                                 if (fabs(src[1]) < minelem)
163                                 {
164                                         pos = 1;
165                                         minelem = fabs(src[1]);
166                                 }
167                                 if (fabs(src[2]) < minelem)
168                                         pos = 2;
169
170                                 dst[pos] = 1;
171                                 dst[0] -= src[pos] * src[0];
172                                 dst[1] -= src[pos] * src[1];
173                                 dst[2] -= src[pos] * src[2];
174
175                                 // normalize the result
176                                 VectorNormalize(dst);
177                         }
178                         else
179                                 dst[2] = 1;
180                 }
181                 else
182                 {
183                         dst[1] = 1;
184                         dst[2] = 0;
185                 }
186         }
187         else
188         {
189                 dst[0] = 1;
190                 dst[1] = 0;
191                 dst[2] = 0;
192         }
193 }
194 #endif
195
196
197 // LordHavoc: like AngleVectors, but taking a forward vector instead of angles, useful!
198 void VectorVectors(const vec3_t forward, vec3_t right, vec3_t up)
199 {
200         float d;
201
202         right[0] = forward[2];
203         right[1] = -forward[0];
204         right[2] = forward[1];
205
206         d = DotProduct(forward, right);
207         VectorMA(right, -d, forward, right);
208         VectorNormalize(right);
209         CrossProduct(right, forward, up);
210 }
211
212 void VectorVectorsDouble(const double *forward, double *right, double *up)
213 {
214         double d;
215
216         right[0] = forward[2];
217         right[1] = -forward[0];
218         right[2] = forward[1];
219
220         d = DotProduct(forward, right);
221         VectorMA(right, -d, forward, right);
222         VectorNormalize(right);
223         CrossProduct(right, forward, up);
224 }
225
226 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees )
227 {
228         float t0, t1;
229         float angle, c, s;
230         vec3_t vr, vu, vf;
231
232         angle = DEG2RAD(degrees);
233         c = cos(angle);
234         s = sin(angle);
235         VectorCopy(dir, vf);
236         VectorVectors(vf, vr, vu);
237
238         t0 = vr[0] *  c + vu[0] * -s;
239         t1 = vr[0] *  s + vu[0] *  c;
240         dst[0] = (t0 * vr[0] + t1 * vu[0] + vf[0] * vf[0]) * point[0]
241                + (t0 * vr[1] + t1 * vu[1] + vf[0] * vf[1]) * point[1]
242                + (t0 * vr[2] + t1 * vu[2] + vf[0] * vf[2]) * point[2];
243
244         t0 = vr[1] *  c + vu[1] * -s;
245         t1 = vr[1] *  s + vu[1] *  c;
246         dst[1] = (t0 * vr[0] + t1 * vu[0] + vf[1] * vf[0]) * point[0]
247                + (t0 * vr[1] + t1 * vu[1] + vf[1] * vf[1]) * point[1]
248                + (t0 * vr[2] + t1 * vu[2] + vf[1] * vf[2]) * point[2];
249
250         t0 = vr[2] *  c + vu[2] * -s;
251         t1 = vr[2] *  s + vu[2] *  c;
252         dst[2] = (t0 * vr[0] + t1 * vu[0] + vf[2] * vf[0]) * point[0]
253                + (t0 * vr[1] + t1 * vu[1] + vf[2] * vf[1]) * point[1]
254                + (t0 * vr[2] + t1 * vu[2] + vf[2] * vf[2]) * point[2];
255 }
256
257 /*-----------------------------------------------------------------*/
258
259
260 void PlaneClassify(mplane_t *p)
261 {
262         // for optimized plane comparisons
263         if (p->normal[0] == 1)
264                 p->type = 0;
265         else if (p->normal[1] == 1)
266                 p->type = 1;
267         else if (p->normal[2] == 1)
268                 p->type = 2;
269         else
270                 p->type = 3;
271         // for BoxOnPlaneSide
272         p->signbits = 0;
273         if (p->normal[0] < 0) // 1
274                 p->signbits |= 1;
275         if (p->normal[1] < 0) // 2
276                 p->signbits |= 2;
277         if (p->normal[2] < 0) // 4
278                 p->signbits |= 4;
279 }
280
281 int BoxOnPlaneSide(const vec3_t emins, const vec3_t emaxs, const mplane_t *p)
282 {
283         if (p->type < 3)
284                 return ((emaxs[p->type] >= p->dist) | ((emins[p->type] < p->dist) << 1));
285         switch(p->signbits)
286         {
287         default:
288         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));
289         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));
290         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));
291         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));
292         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));
293         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));
294         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));
295         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));
296         }
297 }
298
299 #if 0
300 int BoxOnPlaneSide_Separate(const vec3_t emins, const vec3_t emaxs, const vec3_t normal, const vec_t dist)
301 {
302         switch((normal[0] < 0) | ((normal[1] < 0) << 1) | ((normal[2] < 0) << 2))
303         {
304         default:
305         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));
306         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));
307         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));
308         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));
309         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));
310         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));
311         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));
312         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));
313         }
314 }
315 #endif
316
317 void BoxPlaneCorners(const vec3_t emins, const vec3_t emaxs, const mplane_t *p, vec3_t outnear, vec3_t outfar)
318 {
319         if (p->type < 3)
320         {
321                 outnear[0] = outnear[1] = outnear[2] = outfar[0] = outfar[1] = outfar[2] = 0;
322                 outnear[p->type] = emins[p->type];
323                 outfar[p->type] = emaxs[p->type];
324                 return;
325         }
326         switch(p->signbits)
327         {
328         default:
329         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;
330         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;
331         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;
332         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;
333         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;
334         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;
335         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;
336         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;
337         }
338 }
339
340 void BoxPlaneCorners_Separate(const vec3_t emins, const vec3_t emaxs, const vec3_t normal, vec3_t outnear, vec3_t outfar)
341 {
342         switch((normal[0] < 0) | ((normal[1] < 0) << 1) | ((normal[2] < 0) << 2))
343         {
344         default:
345         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;
346         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;
347         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;
348         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;
349         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;
350         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;
351         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;
352         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;
353         }
354 }
355
356 void BoxPlaneCornerDistances(const vec3_t emins, const vec3_t emaxs, const mplane_t *p, vec_t *outneardist, vec_t *outfardist)
357 {
358         if (p->type < 3)
359         {
360                 *outneardist = emins[p->type] - p->dist;
361                 *outfardist = emaxs[p->type] - p->dist;
362                 return;
363         }
364         switch(p->signbits)
365         {
366         default:
367         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;
368         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;
369         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;
370         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;
371         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;
372         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;
373         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;
374         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;
375         }
376 }
377
378 void BoxPlaneCornerDistances_Separate(const vec3_t emins, const vec3_t emaxs, const vec3_t normal, vec_t *outneardist, vec_t *outfardist)
379 {
380         switch((normal[0] < 0) | ((normal[1] < 0) << 1) | ((normal[2] < 0) << 2))
381         {
382         default:
383         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;
384         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;
385         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;
386         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;
387         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;
388         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;
389         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;
390         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;
391         }
392 }
393
394 void AngleVectors (const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
395 {
396         double angle, sr, sp, sy, cr, cp, cy;
397
398         angle = angles[YAW] * (M_PI*2 / 360);
399         sy = sin(angle);
400         cy = cos(angle);
401         angle = angles[PITCH] * (M_PI*2 / 360);
402         sp = sin(angle);
403         cp = cos(angle);
404         if (forward)
405         {
406                 forward[0] = cp*cy;
407                 forward[1] = cp*sy;
408                 forward[2] = -sp;
409         }
410         if (right || up)
411         {
412                 if (angles[ROLL])
413                 {
414                         angle = angles[ROLL] * (M_PI*2 / 360);
415                         sr = sin(angle);
416                         cr = cos(angle);
417                         if (right)
418                         {
419                                 right[0] = -1*(sr*sp*cy+cr*-sy);
420                                 right[1] = -1*(sr*sp*sy+cr*cy);
421                                 right[2] = -1*(sr*cp);
422                         }
423                         if (up)
424                         {
425                                 up[0] = (cr*sp*cy+-sr*-sy);
426                                 up[1] = (cr*sp*sy+-sr*cy);
427                                 up[2] = cr*cp;
428                         }
429                 }
430                 else
431                 {
432                         if (right)
433                         {
434                                 right[0] = sy;
435                                 right[1] = -cy;
436                                 right[2] = 0;
437                         }
438                         if (up)
439                         {
440                                 up[0] = (sp*cy);
441                                 up[1] = (sp*sy);
442                                 up[2] = cp;
443                         }
444                 }
445         }
446 }
447
448 void AngleVectorsFLU (const vec3_t angles, vec3_t forward, vec3_t left, vec3_t up)
449 {
450         double angle, sr, sp, sy, cr, cp, cy;
451
452         angle = angles[YAW] * (M_PI*2 / 360);
453         sy = sin(angle);
454         cy = cos(angle);
455         angle = angles[PITCH] * (M_PI*2 / 360);
456         sp = sin(angle);
457         cp = cos(angle);
458         if (forward)
459         {
460                 forward[0] = cp*cy;
461                 forward[1] = cp*sy;
462                 forward[2] = -sp;
463         }
464         if (left || up)
465         {
466                 if (angles[ROLL])
467                 {
468                         angle = angles[ROLL] * (M_PI*2 / 360);
469                         sr = sin(angle);
470                         cr = cos(angle);
471                         if (left)
472                         {
473                                 left[0] = sr*sp*cy+cr*-sy;
474                                 left[1] = sr*sp*sy+cr*cy;
475                                 left[2] = sr*cp;
476                         }
477                         if (up)
478                         {
479                                 up[0] = cr*sp*cy+-sr*-sy;
480                                 up[1] = cr*sp*sy+-sr*cy;
481                                 up[2] = cr*cp;
482                         }
483                 }
484                 else
485                 {
486                         if (left)
487                         {
488                                 left[0] = -sy;
489                                 left[1] = cy;
490                                 left[2] = 0;
491                         }
492                         if (up)
493                         {
494                                 up[0] = sp*cy;
495                                 up[1] = sp*sy;
496                                 up[2] = cp;
497                         }
498                 }
499         }
500 }
501
502 #if 0
503 void AngleMatrix (const vec3_t angles, const vec3_t translate, vec_t matrix[][4])
504 {
505         double angle, sr, sp, sy, cr, cp, cy;
506
507         angle = angles[YAW] * (M_PI*2 / 360);
508         sy = sin(angle);
509         cy = cos(angle);
510         angle = angles[PITCH] * (M_PI*2 / 360);
511         sp = sin(angle);
512         cp = cos(angle);
513         angle = angles[ROLL] * (M_PI*2 / 360);
514         sr = sin(angle);
515         cr = cos(angle);
516         matrix[0][0] = cp*cy;
517         matrix[0][1] = sr*sp*cy+cr*-sy;
518         matrix[0][2] = cr*sp*cy+-sr*-sy;
519         matrix[0][3] = translate[0];
520         matrix[1][0] = cp*sy;
521         matrix[1][1] = sr*sp*sy+cr*cy;
522         matrix[1][2] = cr*sp*sy+-sr*cy;
523         matrix[1][3] = translate[1];
524         matrix[2][0] = -sp;
525         matrix[2][1] = sr*cp;
526         matrix[2][2] = cr*cp;
527         matrix[2][3] = translate[2];
528 }
529 #endif
530
531
532 // LordHavoc: renamed this to Length, and made the normal one a #define
533 float VectorNormalizeLength (vec3_t v)
534 {
535         float length, ilength;
536
537         length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
538         length = sqrt (length);
539
540         if (length)
541         {
542                 ilength = 1/length;
543                 v[0] *= ilength;
544                 v[1] *= ilength;
545                 v[2] *= ilength;
546         }
547
548         return length;
549
550 }
551
552
553 /*
554 ================
555 R_ConcatRotations
556 ================
557 */
558 void R_ConcatRotations (const float in1[3*3], const float in2[3*3], float out[3*3])
559 {
560         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];
561         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];
562         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];
563         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];
564         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];
565         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];
566         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];
567         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];
568         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];
569 }
570
571
572 /*
573 ================
574 R_ConcatTransforms
575 ================
576 */
577 void R_ConcatTransforms (const float in1[3*4], const float in2[3*4], float out[3*4])
578 {
579         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];
580         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];
581         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];
582         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];
583         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];
584         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];
585         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];
586         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];
587         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];
588         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];
589         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];
590         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];
591 }
592
593 float RadiusFromBounds (const vec3_t mins, const vec3_t maxs)
594 {
595         vec3_t m1, m2;
596         VectorMultiply(mins, mins, m1);
597         VectorMultiply(maxs, maxs, m2);
598         return sqrt(max(m1[0], m2[0]) + max(m1[1], m2[1]) + max(m1[2], m2[2]));
599 }
600
601 float RadiusFromBoundsAndOrigin (const vec3_t mins, const vec3_t maxs, const vec3_t origin)
602 {
603         vec3_t m1, m2;
604         VectorSubtract(mins, origin, m1);VectorMultiply(m1, m1, m1);
605         VectorSubtract(maxs, origin, m2);VectorMultiply(m2, m2, m2);
606         return sqrt(max(m1[0], m2[0]) + max(m1[1], m2[1]) + max(m1[2], m2[2]));
607 }
608
609 void Mathlib_Init(void)
610 {
611         int a;
612
613         // LordHavoc: setup 1.0f / N table for quick recipricols of integers
614         ixtable[0] = 0;
615         for (a = 1;a < 4096;a++)
616                 ixtable[a] = 1.0f / a;
617 }
618
619 #include "matrixlib.h"
620
621 void Matrix4x4_Print(const matrix4x4_t *in)
622 {
623         Con_Printf("%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n"
624         , in->m[0][0], in->m[0][1], in->m[0][2], in->m[0][3]
625         , in->m[1][0], in->m[1][1], in->m[1][2], in->m[1][3]
626         , in->m[2][0], in->m[2][1], in->m[2][2], in->m[2][3]
627         , in->m[3][0], in->m[3][1], in->m[3][2], in->m[3][3]);
628 }
629
630 int Math_atov(const char *s, vec3_t out)
631 {
632         int i;
633         VectorClear(out);
634         if (*s == '\'')
635                 s++;
636         for (i = 0;i < 3;i++)
637         {
638                 while (*s == ' ' || *s == '\t')
639                         s++;
640                 out[i] = atof (s);
641                 if (out[i] == 0 && *s != '-' && *s != '+' && (*s < '0' || *s > '9'))
642                         break; // not a number
643                 while (*s && *s != ' ' && *s !='\t' && *s != '\'')
644                         s++;
645                 if (*s == '\'')
646                         break;
647         }
648         return i;
649 }
650
651 void BoxFromPoints(vec3_t mins, vec3_t maxs, int numpoints, vec_t *point3f)
652 {
653         int i;
654         VectorCopy(point3f, mins);
655         VectorCopy(point3f, maxs);
656         for (i = 1, point3f += 3;i < numpoints;i++, point3f += 3)
657         {
658                 mins[0] = min(mins[0], point3f[0]);maxs[0] = max(maxs[0], point3f[0]);
659                 mins[1] = min(mins[1], point3f[1]);maxs[1] = max(maxs[1], point3f[1]);
660                 mins[2] = min(mins[2], point3f[2]);maxs[2] = max(maxs[2], point3f[2]);
661         }
662 }
663