]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/math.qh
1a707a435cfa6a28e7efc92521598fd036b74d97
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / math.qh
1 #ifndef MATH_H
2 #define MATH_H
3
4 void mean_accumulate(entity e, .float a, .float c, float mean, float value, float weight)
5 {
6     if (weight == 0)
7         return;
8     if (mean == 0)
9         e.(a) *= pow(value, weight);
10     else
11         e.(a) += pow(value, mean) * weight;
12     e.(c) += weight;
13 }
14
15 float mean_evaluate(entity e, .float a, .float c, float mean)
16 {
17     if (e.(c) == 0)
18         return 0;
19     if (mean == 0)
20         return pow(e.(a), 1.0 / e.(c));
21     else
22         return pow(e.(a) / e.(c), 1.0 / mean);
23 }
24
25 #define MEAN_ACCUMULATE(prefix,v,w) mean_accumulate(self,prefix##_accumulator,prefix##_count,prefix##_mean,v,w)
26 #define MEAN_EVALUATE(prefix) mean_evaluate(self,prefix##_accumulator,prefix##_count,prefix##_mean)
27 #define MEAN_DECLARE(prefix,m) float prefix##_mean = m; .float prefix##_count, prefix##_accumulator
28
29 /*
30 ==================
31 crandom
32
33 Returns a random number between -1.0 and 1.0
34 ==================
35 */
36 float crandom()
37 {
38     return 2 * (random() - 0.5);
39 }
40
41
42 /*
43 ==================
44 Angc used for animations
45 ==================
46 */
47
48
49 float angc(float a1, float a2)
50 {
51     while (a1 > 180) a1 -= 360;
52     while (a1 < -179) a1 += 360;
53     while (a2 > 180) a2 -= 360;
54     while (a2 < -179) a2 += 360;
55     float a = a1 - a2;
56     while (a > 180) a -= 360;
57     while (a < -179) a += 360;
58     return a;
59 }
60
61 float fsnap(float val,float fsize)
62 {
63     return rint(val / fsize) * fsize;
64 }
65
66 vector vsnap(vector point,float fsize)
67 {
68     vector vret;
69
70     vret.x = rint(point.x / fsize) * fsize;
71     vret.y = rint(point.y / fsize) * fsize;
72     vret.z = ceil(point.z / fsize) * fsize;
73
74     return vret;
75 }
76
77 vector lerpv(float t0, vector v0, float t1, vector v1, float t)
78 {
79     return v0 + (v1 - v0) * ((t - t0) / (t1 - t0));
80 }
81
82 vector bezier_quadratic_getpoint(vector a, vector b, vector c, float t)
83 {
84     return
85         (c - 2 * b + a) * (t * t) +
86         (b - a) * (2 * t) +
87         a;
88 }
89
90 vector bezier_quadratic_getderivative(vector a, vector b, vector c, float t)
91 {
92     return
93         (c - 2 * b + a) * (2 * t) +
94         (b - a) * 2;
95 }
96
97 float cubic_speedfunc(float startspeedfactor, float endspeedfactor, float x)
98 {
99     return
100         (((     startspeedfactor + endspeedfactor - 2
101         ) * x - 2 * startspeedfactor - endspeedfactor + 3
102         ) * x + startspeedfactor
103         ) * x;
104 }
105
106 bool cubic_speedfunc_is_sane(float startspeedfactor, float endspeedfactor)
107 {
108     if (startspeedfactor < 0 || endspeedfactor < 0)
109         return false;
110
111     /*
112     // if this is the case, the possible zeros of the first derivative are outside
113     // 0..1
114     We can calculate this condition as condition
115     if(se <= 3)
116         return true;
117     */
118
119     // better, see below:
120     if (startspeedfactor <= 3 && endspeedfactor <= 3)
121         return true;
122
123     // if this is the case, the first derivative has no zeros at all
124     float se = startspeedfactor + endspeedfactor;
125     float s_e = startspeedfactor - endspeedfactor;
126     if (3 * (se - 4) * (se - 4) + s_e * s_e <= 12) // an ellipse
127         return true;
128
129     // Now let s <= 3, s <= 3, s+e >= 3 (triangle) then we get se <= 6 (top right corner).
130     // we also get s_e <= 6 - se
131     // 3 * (se - 4)^2 + (6 - se)^2
132     // is quadratic, has value 12 at 3 and 6, and value < 12 in between.
133     // Therefore, above "better" check works!
134
135     return false;
136
137     // known good cases:
138     // (0, [0..3])
139     // (0.5, [0..3.8])
140     // (1, [0..4])
141     // (1.5, [0..3.9])
142     // (2, [0..3.7])
143     // (2.5, [0..3.4])
144     // (3, [0..3])
145     // (3.5, [0.2..2.3])
146     // (4, 1)
147
148     /*
149        On another note:
150        inflection point is always at (2s + e - 3) / (3s + 3e - 6).
151
152        s + e - 2 == 0: no inflection
153
154        s + e > 2:
155        0 < inflection < 1 if:
156        0 < 2s + e - 3 < 3s + 3e - 6
157        2s + e > 3 and 2e + s > 3
158
159        s + e < 2:
160        0 < inflection < 1 if:
161        0 > 2s + e - 3 > 3s + 3e - 6
162        2s + e < 3 and 2e + s < 3
163
164        Therefore: there is an inflection point iff:
165        e outside (3 - s)/2 .. 3 - s*2
166
167        in other words, if (s,e) in triangle (1,1)(0,3)(0,1.5) or in triangle (1,1)(3,0)(1.5,0)
168     */
169 }
170
171 /** continuous function mapping all reals into -1..1 */
172 float float2range11(float f)
173 {
174     return f / (fabs(f) + 1);
175 }
176
177 /** continuous function mapping all reals into 0..1 */
178 float float2range01(float f)
179 {
180     return 0.5 + 0.5 * float2range11(f);
181 }
182
183 float median(float a, float b, float c)
184 {
185     return (a < c) ? bound(a, b, c) : bound(c, b, a);
186 }
187
188 float almost_equals(float a, float b)
189 {
190     float eps = (max(a, -a) + max(b, -b)) * 0.001;
191     return a - b < eps && b - a < eps;
192 }
193
194 float almost_in_bounds(float a, float b, float c)
195 {
196     float eps = (max(a, -a) + max(c, -c)) * 0.001;
197     if (a > c)
198         eps = -eps;
199     return b == median(a - eps, b, c + eps);
200 }
201
202 float power2of(float e)
203 {
204     return pow(2, e);
205 }
206
207 float log2of(float x)
208 {
209     // NOTE: generated code
210     if (x > 2048)
211         if (x > 131072)
212             if (x > 1048576)
213                 if (x > 4194304)
214                     return 23;
215                 else
216                     if (x > 2097152)
217                         return 22;
218                     else
219                         return 21;
220             else
221                 if (x > 524288)
222                     return 20;
223                 else
224                     if (x > 262144)
225                         return 19;
226                     else
227                         return 18;
228         else
229             if (x > 16384)
230                 if (x > 65536)
231                     return 17;
232                 else
233                     if (x > 32768)
234                         return 16;
235                     else
236                         return 15;
237             else
238                 if (x > 8192)
239                     return 14;
240                 else
241                     if (x > 4096)
242                         return 13;
243                     else
244                         return 12;
245     else
246         if (x > 32)
247             if (x > 256)
248                 if (x > 1024)
249                     return 11;
250                 else
251                     if (x > 512)
252                         return 10;
253                     else
254                         return 9;
255             else
256                 if (x > 128)
257                     return 8;
258                 else
259                     if (x > 64)
260                         return 7;
261                     else
262                         return 6;
263         else
264             if (x > 4)
265                 if (x > 16)
266                     return 5;
267                 else
268                     if (x > 8)
269                         return 4;
270                     else
271                         return 3;
272             else
273                 if (x > 2)
274                     return 2;
275                 else
276                     if (x > 1)
277                         return 1;
278                     else
279                         return 0;
280 }
281
282
283 #endif