vary r_drawparticles_drawdistance and r_drawdecals_drawdistance for
[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 "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
207         d = DotProduct(forward, right);
208         VectorMA(right, -d, forward, right);
209         VectorNormalize(right);
210         CrossProduct(right, forward, up);
211 }
212
213 void VectorVectorsDouble(const double *forward, double *right, double *up)
214 {
215         double d;
216
217         right[0] = forward[2];
218         right[1] = -forward[0];
219         right[2] = forward[1];
220
221         d = DotProduct(forward, right);
222         VectorMA(right, -d, forward, right);
223         VectorNormalize(right);
224         CrossProduct(right, forward, up);
225 }
226
227 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees )
228 {
229         float t0, t1;
230         float angle, c, s;
231         vec3_t vr, vu, vf;
232
233         angle = DEG2RAD(degrees);
234         c = cos(angle);
235         s = sin(angle);
236         VectorCopy(dir, vf);
237         VectorVectors(vf, vr, vu);
238
239         t0 = vr[0] *  c + vu[0] * -s;
240         t1 = vr[0] *  s + vu[0] *  c;
241         dst[0] = (t0 * vr[0] + t1 * vu[0] + vf[0] * vf[0]) * point[0]
242                + (t0 * vr[1] + t1 * vu[1] + vf[0] * vf[1]) * point[1]
243                + (t0 * vr[2] + t1 * vu[2] + vf[0] * vf[2]) * point[2];
244
245         t0 = vr[1] *  c + vu[1] * -s;
246         t1 = vr[1] *  s + vu[1] *  c;
247         dst[1] = (t0 * vr[0] + t1 * vu[0] + vf[1] * vf[0]) * point[0]
248                + (t0 * vr[1] + t1 * vu[1] + vf[1] * vf[1]) * point[1]
249                + (t0 * vr[2] + t1 * vu[2] + vf[1] * vf[2]) * point[2];
250
251         t0 = vr[2] *  c + vu[2] * -s;
252         t1 = vr[2] *  s + vu[2] *  c;
253         dst[2] = (t0 * vr[0] + t1 * vu[0] + vf[2] * vf[0]) * point[0]
254                + (t0 * vr[1] + t1 * vu[1] + vf[2] * vf[1]) * point[1]
255                + (t0 * vr[2] + t1 * vu[2] + vf[2] * vf[2]) * point[2];
256 }
257
258 /*-----------------------------------------------------------------*/
259
260 // returns the smallest integer greater than or equal to "value", or 0 if "value" is too big
261 unsigned int CeilPowerOf2(unsigned int value)
262 {
263         unsigned int ceilvalue;
264
265         if (value > (1U << (sizeof(int) * 8 - 1)))
266                 return 0;
267
268         ceilvalue = 1;
269         while (ceilvalue < value)
270                 ceilvalue <<= 1;
271
272         return ceilvalue;
273 }
274
275
276 /*-----------------------------------------------------------------*/
277
278
279 void PlaneClassify(mplane_t *p)
280 {
281         // for optimized plane comparisons
282         if (p->normal[0] == 1)
283                 p->type = 0;
284         else if (p->normal[1] == 1)
285                 p->type = 1;
286         else if (p->normal[2] == 1)
287                 p->type = 2;
288         else
289                 p->type = 3;
290         // for BoxOnPlaneSide
291         p->signbits = 0;
292         if (p->normal[0] < 0) // 1
293                 p->signbits |= 1;
294         if (p->normal[1] < 0) // 2
295                 p->signbits |= 2;
296         if (p->normal[2] < 0) // 4
297                 p->signbits |= 4;
298 }
299
300 int BoxOnPlaneSide(const vec3_t emins, const vec3_t emaxs, const mplane_t *p)
301 {
302         if (p->type < 3)
303                 return ((emaxs[p->type] >= p->dist) | ((emins[p->type] < p->dist) << 1));
304         switch(p->signbits)
305         {
306         default:
307         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));
308         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));
309         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));
310         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));
311         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));
312         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));
313         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));
314         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));
315         }
316 }
317
318 #if 0
319 int BoxOnPlaneSide_Separate(const vec3_t emins, const vec3_t emaxs, const vec3_t normal, const vec_t dist)
320 {
321         switch((normal[0] < 0) | ((normal[1] < 0) << 1) | ((normal[2] < 0) << 2))
322         {
323         default:
324         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));
325         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));
326         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));
327         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));
328         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));
329         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));
330         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));
331         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));
332         }
333 }
334 #endif
335
336 void BoxPlaneCorners(const vec3_t emins, const vec3_t emaxs, const mplane_t *p, vec3_t outnear, vec3_t outfar)
337 {
338         if (p->type < 3)
339         {
340                 outnear[0] = outnear[1] = outnear[2] = outfar[0] = outfar[1] = outfar[2] = 0;
341                 outnear[p->type] = emins[p->type];
342                 outfar[p->type] = emaxs[p->type];
343                 return;
344         }
345         switch(p->signbits)
346         {
347         default:
348         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;
349         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;
350         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;
351         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;
352         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;
353         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;
354         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;
355         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;
356         }
357 }
358
359 void BoxPlaneCorners_Separate(const vec3_t emins, const vec3_t emaxs, const vec3_t normal, vec3_t outnear, vec3_t outfar)
360 {
361         switch((normal[0] < 0) | ((normal[1] < 0) << 1) | ((normal[2] < 0) << 2))
362         {
363         default:
364         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;
365         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;
366         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;
367         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;
368         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;
369         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;
370         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;
371         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;
372         }
373 }
374
375 void BoxPlaneCornerDistances(const vec3_t emins, const vec3_t emaxs, const mplane_t *p, vec_t *outneardist, vec_t *outfardist)
376 {
377         if (p->type < 3)
378         {
379                 *outneardist = emins[p->type] - p->dist;
380                 *outfardist = emaxs[p->type] - p->dist;
381                 return;
382         }
383         switch(p->signbits)
384         {
385         default:
386         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;
387         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;
388         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;
389         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;
390         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;
391         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;
392         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;
393         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;
394         }
395 }
396
397 void BoxPlaneCornerDistances_Separate(const vec3_t emins, const vec3_t emaxs, const vec3_t normal, vec_t *outneardist, vec_t *outfardist)
398 {
399         switch((normal[0] < 0) | ((normal[1] < 0) << 1) | ((normal[2] < 0) << 2))
400         {
401         default:
402         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;
403         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;
404         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;
405         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;
406         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;
407         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;
408         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;
409         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;
410         }
411 }
412
413 void AngleVectors (const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
414 {
415         double angle, sr, sp, sy, cr, cp, cy;
416
417         angle = angles[YAW] * (M_PI*2 / 360);
418         sy = sin(angle);
419         cy = cos(angle);
420         angle = angles[PITCH] * (M_PI*2 / 360);
421         sp = sin(angle);
422         cp = cos(angle);
423         if (forward)
424         {
425                 forward[0] = cp*cy;
426                 forward[1] = cp*sy;
427                 forward[2] = -sp;
428         }
429         if (right || up)
430         {
431                 if (angles[ROLL])
432                 {
433                         angle = angles[ROLL] * (M_PI*2 / 360);
434                         sr = sin(angle);
435                         cr = cos(angle);
436                         if (right)
437                         {
438                                 right[0] = -1*(sr*sp*cy+cr*-sy);
439                                 right[1] = -1*(sr*sp*sy+cr*cy);
440                                 right[2] = -1*(sr*cp);
441                         }
442                         if (up)
443                         {
444                                 up[0] = (cr*sp*cy+-sr*-sy);
445                                 up[1] = (cr*sp*sy+-sr*cy);
446                                 up[2] = cr*cp;
447                         }
448                 }
449                 else
450                 {
451                         if (right)
452                         {
453                                 right[0] = sy;
454                                 right[1] = -cy;
455                                 right[2] = 0;
456                         }
457                         if (up)
458                         {
459                                 up[0] = (sp*cy);
460                                 up[1] = (sp*sy);
461                                 up[2] = cp;
462                         }
463                 }
464         }
465 }
466
467 void AngleVectorsFLU (const vec3_t angles, vec3_t forward, vec3_t left, vec3_t up)
468 {
469         double angle, sr, sp, sy, cr, cp, cy;
470
471         angle = angles[YAW] * (M_PI*2 / 360);
472         sy = sin(angle);
473         cy = cos(angle);
474         angle = angles[PITCH] * (M_PI*2 / 360);
475         sp = sin(angle);
476         cp = cos(angle);
477         if (forward)
478         {
479                 forward[0] = cp*cy;
480                 forward[1] = cp*sy;
481                 forward[2] = -sp;
482         }
483         if (left || up)
484         {
485                 if (angles[ROLL])
486                 {
487                         angle = angles[ROLL] * (M_PI*2 / 360);
488                         sr = sin(angle);
489                         cr = cos(angle);
490                         if (left)
491                         {
492                                 left[0] = sr*sp*cy+cr*-sy;
493                                 left[1] = sr*sp*sy+cr*cy;
494                                 left[2] = sr*cp;
495                         }
496                         if (up)
497                         {
498                                 up[0] = cr*sp*cy+-sr*-sy;
499                                 up[1] = cr*sp*sy+-sr*cy;
500                                 up[2] = cr*cp;
501                         }
502                 }
503                 else
504                 {
505                         if (left)
506                         {
507                                 left[0] = -sy;
508                                 left[1] = cy;
509                                 left[2] = 0;
510                         }
511                         if (up)
512                         {
513                                 up[0] = sp*cy;
514                                 up[1] = sp*sy;
515                                 up[2] = cp;
516                         }
517                 }
518         }
519 }
520
521 // LordHavoc: calculates pitch/yaw/roll angles from forward and up vectors
522 void AnglesFromVectors (vec3_t angles, const vec3_t forward, const vec3_t up, qboolean flippitch)
523 {
524         if (forward[0] == 0 && forward[1] == 0)
525         {
526                 angles[PITCH] = forward[2] > 0 ? -M_PI * 0.5 : M_PI * 0.5;
527                 angles[YAW] = up ? atan2(-up[1], -up[0]) : 0;
528                 angles[ROLL] = 0;
529         }
530         else
531         {
532                 angles[YAW] = atan2(forward[1], forward[0]);
533                 angles[PITCH] = -atan2(forward[2], sqrt(forward[0]*forward[0] + forward[1]*forward[1]));
534                 if (up)
535                 {
536                         vec_t cp = cos(angles[PITCH]), sp = sin(angles[PITCH]);
537                         vec_t cy = cos(angles[YAW]), sy = sin(angles[YAW]);
538                         vec3_t tleft, tup;
539                         tleft[0] = -sy;
540                         tleft[1] = cy;
541                         tleft[2] = 0;
542                         tup[0] = sp*cy;
543                         tup[1] = sp*sy;
544                         tup[2] = cp;
545                         angles[ROLL] = -atan2(DotProduct(up, tleft), DotProduct(up, tup));
546                 }
547                 else
548                         angles[ROLL] = 0;
549         }
550
551         // now convert radians to degrees, and make all values positive
552         VectorScale(angles, 180.0 / M_PI, angles);
553         if (flippitch)
554                 angles[PITCH] *= -1;
555         if (angles[PITCH] < 0) angles[PITCH] += 360;
556         if (angles[YAW] < 0) angles[YAW] += 360;
557         if (angles[ROLL] < 0) angles[ROLL] += 360;
558
559 #if 0
560 {
561         // debugging code
562         vec3_t tforward, tleft, tup, nforward, nup;
563         VectorCopy(forward, nforward);
564         VectorNormalize(nforward);
565         if (up)
566         {
567                 VectorCopy(up, nup);
568                 VectorNormalize(nup);
569                 AngleVectors(angles, tforward, tleft, tup);
570                 if (VectorDistance(tforward, nforward) > 0.01 || VectorDistance(tup, nup) > 0.01)
571                 {
572                         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]);
573                         Con_Printf("^3But that is '%f %f %f', '%f %f %f'\n", tforward[0], tforward[1], tforward[2], tup[0], tup[1], tup[2]);
574                 }
575         }
576         else
577         {
578                 AngleVectors(angles, tforward, tleft, tup);
579                 if (VectorDistance(tforward, nforward) > 0.01)
580                 {
581                         Con_Printf("vectoangles('%f %f %f') = %f %f %f\n", nforward[0], nforward[1], nforward[2], angles[0], angles[1], angles[2]);
582                         Con_Printf("^3But that is '%f %f %f'\n", tforward[0], tforward[1], tforward[2]);
583                 }
584         }
585 }
586 #endif
587 }
588
589 #if 0
590 void AngleMatrix (const vec3_t angles, const vec3_t translate, vec_t matrix[][4])
591 {
592         double angle, sr, sp, sy, cr, cp, cy;
593
594         angle = angles[YAW] * (M_PI*2 / 360);
595         sy = sin(angle);
596         cy = cos(angle);
597         angle = angles[PITCH] * (M_PI*2 / 360);
598         sp = sin(angle);
599         cp = cos(angle);
600         angle = angles[ROLL] * (M_PI*2 / 360);
601         sr = sin(angle);
602         cr = cos(angle);
603         matrix[0][0] = cp*cy;
604         matrix[0][1] = sr*sp*cy+cr*-sy;
605         matrix[0][2] = cr*sp*cy+-sr*-sy;
606         matrix[0][3] = translate[0];
607         matrix[1][0] = cp*sy;
608         matrix[1][1] = sr*sp*sy+cr*cy;
609         matrix[1][2] = cr*sp*sy+-sr*cy;
610         matrix[1][3] = translate[1];
611         matrix[2][0] = -sp;
612         matrix[2][1] = sr*cp;
613         matrix[2][2] = cr*cp;
614         matrix[2][3] = translate[2];
615 }
616 #endif
617
618
619 // LordHavoc: renamed this to Length, and made the normal one a #define
620 float VectorNormalizeLength (vec3_t v)
621 {
622         float length, ilength;
623
624         length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
625         length = sqrt (length);
626
627         if (length)
628         {
629                 ilength = 1/length;
630                 v[0] *= ilength;
631                 v[1] *= ilength;
632                 v[2] *= ilength;
633         }
634
635         return length;
636
637 }
638
639
640 /*
641 ================
642 R_ConcatRotations
643 ================
644 */
645 void R_ConcatRotations (const float in1[3*3], const float in2[3*3], float out[3*3])
646 {
647         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];
648         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];
649         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];
650         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];
651         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];
652         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];
653         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];
654         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];
655         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];
656 }
657
658
659 /*
660 ================
661 R_ConcatTransforms
662 ================
663 */
664 void R_ConcatTransforms (const float in1[3*4], const float in2[3*4], float out[3*4])
665 {
666         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];
667         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];
668         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];
669         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];
670         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];
671         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];
672         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];
673         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];
674         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];
675         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];
676         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];
677         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];
678 }
679
680 float RadiusFromBounds (const vec3_t mins, const vec3_t maxs)
681 {
682         vec3_t m1, m2;
683         VectorMultiply(mins, mins, m1);
684         VectorMultiply(maxs, maxs, m2);
685         return sqrt(max(m1[0], m2[0]) + max(m1[1], m2[1]) + max(m1[2], m2[2]));
686 }
687
688 float RadiusFromBoundsAndOrigin (const vec3_t mins, const vec3_t maxs, const vec3_t origin)
689 {
690         vec3_t m1, m2;
691         VectorSubtract(mins, origin, m1);VectorMultiply(m1, m1, m1);
692         VectorSubtract(maxs, origin, m2);VectorMultiply(m2, m2, m2);
693         return sqrt(max(m1[0], m2[0]) + max(m1[1], m2[1]) + max(m1[2], m2[2]));
694 }
695
696 void Mathlib_Init(void)
697 {
698         int a;
699
700         // LordHavoc: setup 1.0f / N table for quick recipricols of integers
701         ixtable[0] = 0;
702         for (a = 1;a < 4096;a++)
703                 ixtable[a] = 1.0f / a;
704 }
705
706 #include "matrixlib.h"
707
708 void Matrix4x4_Print(const matrix4x4_t *in)
709 {
710         Con_Printf("%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n"
711         , in->m[0][0], in->m[0][1], in->m[0][2], in->m[0][3]
712         , in->m[1][0], in->m[1][1], in->m[1][2], in->m[1][3]
713         , in->m[2][0], in->m[2][1], in->m[2][2], in->m[2][3]
714         , in->m[3][0], in->m[3][1], in->m[3][2], in->m[3][3]);
715 }
716
717 int Math_atov(const char *s, vec3_t out)
718 {
719         int i;
720         VectorClear(out);
721         if (*s == '\'')
722                 s++;
723         for (i = 0;i < 3;i++)
724         {
725                 while (*s == ' ' || *s == '\t')
726                         s++;
727                 out[i] = atof (s);
728                 if (out[i] == 0 && *s != '-' && *s != '+' && (*s < '0' || *s > '9'))
729                         break; // not a number
730                 while (*s && *s != ' ' && *s !='\t' && *s != '\'')
731                         s++;
732                 if (*s == '\'')
733                         break;
734         }
735         return i;
736 }
737
738 void BoxFromPoints(vec3_t mins, vec3_t maxs, int numpoints, vec_t *point3f)
739 {
740         int i;
741         VectorCopy(point3f, mins);
742         VectorCopy(point3f, maxs);
743         for (i = 1, point3f += 3;i < numpoints;i++, point3f += 3)
744         {
745                 mins[0] = min(mins[0], point3f[0]);maxs[0] = max(maxs[0], point3f[0]);
746                 mins[1] = min(mins[1], point3f[1]);maxs[1] = max(maxs[1], point3f[1]);
747                 mins[2] = min(mins[2], point3f[2]);maxs[2] = max(maxs[2], point3f[2]);
748         }
749 }
750