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