]> icculus.org git repositories - divverent/darkplaces.git/blob - mathlib.c
added support for arguire's strange protocols that expand
[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 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 // returns the smallest integer greater than or equal to "value", or 0 if "value" is too big
260 unsigned int CeilPowerOf2(unsigned int value)
261 {
262         unsigned int ceilvalue;
263
264         if (value > (1U << (sizeof(int) * 8 - 1)))
265                 return 0;
266
267         ceilvalue = 1;
268         while (ceilvalue < value)
269                 ceilvalue <<= 1;
270
271         return ceilvalue;
272 }
273
274
275 /*-----------------------------------------------------------------*/
276
277
278 void PlaneClassify(mplane_t *p)
279 {
280         // for optimized plane comparisons
281         if (p->normal[0] == 1)
282                 p->type = 0;
283         else if (p->normal[1] == 1)
284                 p->type = 1;
285         else if (p->normal[2] == 1)
286                 p->type = 2;
287         else
288                 p->type = 3;
289         // for BoxOnPlaneSide
290         p->signbits = 0;
291         if (p->normal[0] < 0) // 1
292                 p->signbits |= 1;
293         if (p->normal[1] < 0) // 2
294                 p->signbits |= 2;
295         if (p->normal[2] < 0) // 4
296                 p->signbits |= 4;
297 }
298
299 int BoxOnPlaneSide(const vec3_t emins, const vec3_t emaxs, const mplane_t *p)
300 {
301         if (p->type < 3)
302                 return ((emaxs[p->type] >= p->dist) | ((emins[p->type] < p->dist) << 1));
303         switch(p->signbits)
304         {
305         default:
306         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));
307         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));
308         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));
309         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));
310         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));
311         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));
312         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));
313         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));
314         }
315 }
316
317 #if 0
318 int BoxOnPlaneSide_Separate(const vec3_t emins, const vec3_t emaxs, const vec3_t normal, const vec_t dist)
319 {
320         switch((normal[0] < 0) | ((normal[1] < 0) << 1) | ((normal[2] < 0) << 2))
321         {
322         default:
323         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));
324         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));
325         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));
326         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));
327         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));
328         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));
329         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));
330         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));
331         }
332 }
333 #endif
334
335 void BoxPlaneCorners(const vec3_t emins, const vec3_t emaxs, const mplane_t *p, vec3_t outnear, vec3_t outfar)
336 {
337         if (p->type < 3)
338         {
339                 outnear[0] = outnear[1] = outnear[2] = outfar[0] = outfar[1] = outfar[2] = 0;
340                 outnear[p->type] = emins[p->type];
341                 outfar[p->type] = emaxs[p->type];
342                 return;
343         }
344         switch(p->signbits)
345         {
346         default:
347         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;
348         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;
349         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;
350         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;
351         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;
352         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;
353         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;
354         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;
355         }
356 }
357
358 void BoxPlaneCorners_Separate(const vec3_t emins, const vec3_t emaxs, const vec3_t normal, vec3_t outnear, vec3_t outfar)
359 {
360         switch((normal[0] < 0) | ((normal[1] < 0) << 1) | ((normal[2] < 0) << 2))
361         {
362         default:
363         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;
364         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;
365         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;
366         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;
367         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;
368         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;
369         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;
370         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;
371         }
372 }
373
374 void BoxPlaneCornerDistances(const vec3_t emins, const vec3_t emaxs, const mplane_t *p, vec_t *outneardist, vec_t *outfardist)
375 {
376         if (p->type < 3)
377         {
378                 *outneardist = emins[p->type] - p->dist;
379                 *outfardist = emaxs[p->type] - p->dist;
380                 return;
381         }
382         switch(p->signbits)
383         {
384         default:
385         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;
386         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;
387         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;
388         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;
389         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;
390         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;
391         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;
392         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;
393         }
394 }
395
396 void BoxPlaneCornerDistances_Separate(const vec3_t emins, const vec3_t emaxs, const vec3_t normal, vec_t *outneardist, vec_t *outfardist)
397 {
398         switch((normal[0] < 0) | ((normal[1] < 0) << 1) | ((normal[2] < 0) << 2))
399         {
400         default:
401         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;
402         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;
403         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;
404         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;
405         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;
406         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;
407         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;
408         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;
409         }
410 }
411
412 void AngleVectors (const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
413 {
414         double angle, sr, sp, sy, cr, cp, cy;
415
416         angle = angles[YAW] * (M_PI*2 / 360);
417         sy = sin(angle);
418         cy = cos(angle);
419         angle = angles[PITCH] * (M_PI*2 / 360);
420         sp = sin(angle);
421         cp = cos(angle);
422         if (forward)
423         {
424                 forward[0] = cp*cy;
425                 forward[1] = cp*sy;
426                 forward[2] = -sp;
427         }
428         if (right || up)
429         {
430                 if (angles[ROLL])
431                 {
432                         angle = angles[ROLL] * (M_PI*2 / 360);
433                         sr = sin(angle);
434                         cr = cos(angle);
435                         if (right)
436                         {
437                                 right[0] = -1*(sr*sp*cy+cr*-sy);
438                                 right[1] = -1*(sr*sp*sy+cr*cy);
439                                 right[2] = -1*(sr*cp);
440                         }
441                         if (up)
442                         {
443                                 up[0] = (cr*sp*cy+-sr*-sy);
444                                 up[1] = (cr*sp*sy+-sr*cy);
445                                 up[2] = cr*cp;
446                         }
447                 }
448                 else
449                 {
450                         if (right)
451                         {
452                                 right[0] = sy;
453                                 right[1] = -cy;
454                                 right[2] = 0;
455                         }
456                         if (up)
457                         {
458                                 up[0] = (sp*cy);
459                                 up[1] = (sp*sy);
460                                 up[2] = cp;
461                         }
462                 }
463         }
464 }
465
466 void AngleVectorsFLU (const vec3_t angles, vec3_t forward, vec3_t left, vec3_t up)
467 {
468         double angle, sr, sp, sy, cr, cp, cy;
469
470         angle = angles[YAW] * (M_PI*2 / 360);
471         sy = sin(angle);
472         cy = cos(angle);
473         angle = angles[PITCH] * (M_PI*2 / 360);
474         sp = sin(angle);
475         cp = cos(angle);
476         if (forward)
477         {
478                 forward[0] = cp*cy;
479                 forward[1] = cp*sy;
480                 forward[2] = -sp;
481         }
482         if (left || up)
483         {
484                 if (angles[ROLL])
485                 {
486                         angle = angles[ROLL] * (M_PI*2 / 360);
487                         sr = sin(angle);
488                         cr = cos(angle);
489                         if (left)
490                         {
491                                 left[0] = sr*sp*cy+cr*-sy;
492                                 left[1] = sr*sp*sy+cr*cy;
493                                 left[2] = sr*cp;
494                         }
495                         if (up)
496                         {
497                                 up[0] = cr*sp*cy+-sr*-sy;
498                                 up[1] = cr*sp*sy+-sr*cy;
499                                 up[2] = cr*cp;
500                         }
501                 }
502                 else
503                 {
504                         if (left)
505                         {
506                                 left[0] = -sy;
507                                 left[1] = cy;
508                                 left[2] = 0;
509                         }
510                         if (up)
511                         {
512                                 up[0] = sp*cy;
513                                 up[1] = sp*sy;
514                                 up[2] = cp;
515                         }
516                 }
517         }
518 }
519
520 #if 0
521 void AngleMatrix (const vec3_t angles, const vec3_t translate, vec_t matrix[][4])
522 {
523         double angle, sr, sp, sy, cr, cp, cy;
524
525         angle = angles[YAW] * (M_PI*2 / 360);
526         sy = sin(angle);
527         cy = cos(angle);
528         angle = angles[PITCH] * (M_PI*2 / 360);
529         sp = sin(angle);
530         cp = cos(angle);
531         angle = angles[ROLL] * (M_PI*2 / 360);
532         sr = sin(angle);
533         cr = cos(angle);
534         matrix[0][0] = cp*cy;
535         matrix[0][1] = sr*sp*cy+cr*-sy;
536         matrix[0][2] = cr*sp*cy+-sr*-sy;
537         matrix[0][3] = translate[0];
538         matrix[1][0] = cp*sy;
539         matrix[1][1] = sr*sp*sy+cr*cy;
540         matrix[1][2] = cr*sp*sy+-sr*cy;
541         matrix[1][3] = translate[1];
542         matrix[2][0] = -sp;
543         matrix[2][1] = sr*cp;
544         matrix[2][2] = cr*cp;
545         matrix[2][3] = translate[2];
546 }
547 #endif
548
549
550 // LordHavoc: renamed this to Length, and made the normal one a #define
551 float VectorNormalizeLength (vec3_t v)
552 {
553         float length, ilength;
554
555         length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
556         length = sqrt (length);
557
558         if (length)
559         {
560                 ilength = 1/length;
561                 v[0] *= ilength;
562                 v[1] *= ilength;
563                 v[2] *= ilength;
564         }
565
566         return length;
567
568 }
569
570
571 /*
572 ================
573 R_ConcatRotations
574 ================
575 */
576 void R_ConcatRotations (const float in1[3*3], const float in2[3*3], float out[3*3])
577 {
578         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];
579         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];
580         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];
581         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];
582         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];
583         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];
584         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];
585         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];
586         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];
587 }
588
589
590 /*
591 ================
592 R_ConcatTransforms
593 ================
594 */
595 void R_ConcatTransforms (const float in1[3*4], const float in2[3*4], float out[3*4])
596 {
597         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];
598         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];
599         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];
600         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];
601         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];
602         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];
603         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];
604         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];
605         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];
606         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];
607         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];
608         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];
609 }
610
611 float RadiusFromBounds (const vec3_t mins, const vec3_t maxs)
612 {
613         vec3_t m1, m2;
614         VectorMultiply(mins, mins, m1);
615         VectorMultiply(maxs, maxs, m2);
616         return sqrt(max(m1[0], m2[0]) + max(m1[1], m2[1]) + max(m1[2], m2[2]));
617 }
618
619 float RadiusFromBoundsAndOrigin (const vec3_t mins, const vec3_t maxs, const vec3_t origin)
620 {
621         vec3_t m1, m2;
622         VectorSubtract(mins, origin, m1);VectorMultiply(m1, m1, m1);
623         VectorSubtract(maxs, origin, m2);VectorMultiply(m2, m2, m2);
624         return sqrt(max(m1[0], m2[0]) + max(m1[1], m2[1]) + max(m1[2], m2[2]));
625 }
626
627 void Mathlib_Init(void)
628 {
629         int a;
630
631         // LordHavoc: setup 1.0f / N table for quick recipricols of integers
632         ixtable[0] = 0;
633         for (a = 1;a < 4096;a++)
634                 ixtable[a] = 1.0f / a;
635 }
636
637 #include "matrixlib.h"
638
639 void Matrix4x4_Print(const matrix4x4_t *in)
640 {
641         Con_Printf("%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n"
642         , in->m[0][0], in->m[0][1], in->m[0][2], in->m[0][3]
643         , in->m[1][0], in->m[1][1], in->m[1][2], in->m[1][3]
644         , in->m[2][0], in->m[2][1], in->m[2][2], in->m[2][3]
645         , in->m[3][0], in->m[3][1], in->m[3][2], in->m[3][3]);
646 }
647
648 int Math_atov(const char *s, vec3_t out)
649 {
650         int i;
651         VectorClear(out);
652         if (*s == '\'')
653                 s++;
654         for (i = 0;i < 3;i++)
655         {
656                 while (*s == ' ' || *s == '\t')
657                         s++;
658                 out[i] = atof (s);
659                 if (out[i] == 0 && *s != '-' && *s != '+' && (*s < '0' || *s > '9'))
660                         break; // not a number
661                 while (*s && *s != ' ' && *s !='\t' && *s != '\'')
662                         s++;
663                 if (*s == '\'')
664                         break;
665         }
666         return i;
667 }
668
669 void BoxFromPoints(vec3_t mins, vec3_t maxs, int numpoints, vec_t *point3f)
670 {
671         int i;
672         VectorCopy(point3f, mins);
673         VectorCopy(point3f, maxs);
674         for (i = 1, point3f += 3;i < numpoints;i++, point3f += 3)
675         {
676                 mins[0] = min(mins[0], point3f[0]);maxs[0] = max(maxs[0], point3f[0]);
677                 mins[1] = min(mins[1], point3f[1]);maxs[1] = max(maxs[1], point3f[1]);
678                 mins[2] = min(mins[2], point3f[2]);maxs[2] = max(maxs[2], point3f[2]);
679         }
680 }
681