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