]> git.lizzy.rs Git - minetest.git/blob - src/util/numeric.h
6f82a18c1b4d858019e29d8f5a9c0da064b6a3a1
[minetest.git] / src / util / numeric.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 #include "basic_macros.h"
23 #include "irrlichttypes.h"
24 #include "irr_v2d.h"
25 #include "irr_v3d.h"
26 #include "irr_aabb3d.h"
27 #include <matrix4.h>
28
29 #define rangelim(d, min, max) ((d) < (min) ? (min) : ((d) > (max) ? (max) : (d)))
30 #define myfloor(x) ((x) < 0.0 ? (int)(x) - 1 : (int)(x))
31 // The naive swap performs better than the xor version
32 #define SWAP(t, x, y) do { \
33         t temp = x; \
34         x = y; \
35         y = temp; \
36 } while (0)
37
38
39 inline s16 getContainerPos(s16 p, s16 d)
40 {
41         return (p >= 0 ? p : p - d + 1) / d;
42 }
43
44 inline v2s16 getContainerPos(v2s16 p, s16 d)
45 {
46         return v2s16(
47                 getContainerPos(p.X, d),
48                 getContainerPos(p.Y, d)
49         );
50 }
51
52 inline v3s16 getContainerPos(v3s16 p, s16 d)
53 {
54         return v3s16(
55                 getContainerPos(p.X, d),
56                 getContainerPos(p.Y, d),
57                 getContainerPos(p.Z, d)
58         );
59 }
60
61 inline v2s16 getContainerPos(v2s16 p, v2s16 d)
62 {
63         return v2s16(
64                 getContainerPos(p.X, d.X),
65                 getContainerPos(p.Y, d.Y)
66         );
67 }
68
69 inline v3s16 getContainerPos(v3s16 p, v3s16 d)
70 {
71         return v3s16(
72                 getContainerPos(p.X, d.X),
73                 getContainerPos(p.Y, d.Y),
74                 getContainerPos(p.Z, d.Z)
75         );
76 }
77
78 inline void getContainerPosWithOffset(s16 p, s16 d, s16 &container, s16 &offset)
79 {
80         container = (p >= 0 ? p : p - d + 1) / d;
81         offset = p & (d - 1);
82 }
83
84 inline void getContainerPosWithOffset(const v2s16 &p, s16 d, v2s16 &container, v2s16 &offset)
85 {
86         getContainerPosWithOffset(p.X, d, container.X, offset.X);
87         getContainerPosWithOffset(p.Y, d, container.Y, offset.Y);
88 }
89
90 inline void getContainerPosWithOffset(const v3s16 &p, s16 d, v3s16 &container, v3s16 &offset)
91 {
92         getContainerPosWithOffset(p.X, d, container.X, offset.X);
93         getContainerPosWithOffset(p.Y, d, container.Y, offset.Y);
94         getContainerPosWithOffset(p.Z, d, container.Z, offset.Z);
95 }
96
97
98 inline bool isInArea(v3s16 p, s16 d)
99 {
100         return (
101                 p.X >= 0 && p.X < d &&
102                 p.Y >= 0 && p.Y < d &&
103                 p.Z >= 0 && p.Z < d
104         );
105 }
106
107 inline bool isInArea(v2s16 p, s16 d)
108 {
109         return (
110                 p.X >= 0 && p.X < d &&
111                 p.Y >= 0 && p.Y < d
112         );
113 }
114
115 inline bool isInArea(v3s16 p, v3s16 d)
116 {
117         return (
118                 p.X >= 0 && p.X < d.X &&
119                 p.Y >= 0 && p.Y < d.Y &&
120                 p.Z >= 0 && p.Z < d.Z
121         );
122 }
123
124 inline void sortBoxVerticies(v3s16 &p1, v3s16 &p2) {
125         if (p1.X > p2.X)
126                 SWAP(s16, p1.X, p2.X);
127         if (p1.Y > p2.Y)
128                 SWAP(s16, p1.Y, p2.Y);
129         if (p1.Z > p2.Z)
130                 SWAP(s16, p1.Z, p2.Z);
131 }
132
133 inline v3s16 componentwise_min(const v3s16 &a, const v3s16 &b)
134 {
135         return v3s16(MYMIN(a.X, b.X), MYMIN(a.Y, b.Y), MYMIN(a.Z, b.Z));
136 }
137
138 inline v3s16 componentwise_max(const v3s16 &a, const v3s16 &b)
139 {
140         return v3s16(MYMAX(a.X, b.X), MYMAX(a.Y, b.Y), MYMAX(a.Z, b.Z));
141 }
142
143
144 /** Returns \p f wrapped to the range [-360, 360]
145  *
146  *  See test.cpp for example cases.
147  *
148  *  \note This is also used in cases where degrees wrapped to the range [0, 360]
149  *  is innapropriate (e.g. pitch needs negative values)
150  *
151  *  \internal functionally equivalent -- although precision may vary slightly --
152  *  to fmodf((f), 360.0f) however empirical tests indicate that this approach is
153  *  faster.
154  */
155 inline float modulo360f(float f)
156 {
157         int sign;
158         int whole;
159         float fraction;
160
161         if (f < 0) {
162                 f = -f;
163                 sign = -1;
164         } else {
165                 sign = 1;
166         }
167
168         whole = f;
169
170         fraction = f - whole;
171         whole %= 360;
172
173         return sign * (whole + fraction);
174 }
175
176
177 /** Returns \p f wrapped to the range [0, 360]
178   */
179 inline float wrapDegrees_0_360(float f)
180 {
181         float value = modulo360f(f);
182         return value < 0 ? value + 360 : value;
183 }
184
185
186 /** Returns \p v3f wrapped to the range [0, 360]
187   */
188 inline v3f wrapDegrees_0_360_v3f(v3f v)
189 {
190         v3f value_v3f;
191         value_v3f.X = modulo360f(v.X);
192         value_v3f.Y = modulo360f(v.Y);
193         value_v3f.Z = modulo360f(v.Z);
194
195         // Now that values are wrapped, use to get values for certain ranges
196         value_v3f.X = value_v3f.X < 0 ? value_v3f.X + 360 : value_v3f.X;
197         value_v3f.Y = value_v3f.Y < 0 ? value_v3f.Y + 360 : value_v3f.Y;
198         value_v3f.Z = value_v3f.Z < 0 ? value_v3f.Z + 360 : value_v3f.Z;
199         return value_v3f;
200 }
201
202
203 /** Returns \p f wrapped to the range [-180, 180]
204   */
205 inline float wrapDegrees_180(float f)
206 {
207         float value = modulo360f(f + 180);
208         if (value < 0)
209                 value += 360;
210         return value - 180;
211 }
212
213 /*
214         Pseudo-random (VC++ rand() sucks)
215 */
216 #define MYRAND_RANGE 0xffffffff
217 u32 myrand();
218 void mysrand(unsigned int seed);
219 void myrand_bytes(void *out, size_t len);
220 int myrand_range(int min, int max);
221
222 /*
223         Miscellaneous functions
224 */
225
226 inline u32 get_bits(u32 x, u32 pos, u32 len)
227 {
228         u32 mask = (1 << len) - 1;
229         return (x >> pos) & mask;
230 }
231
232 inline void set_bits(u32 *x, u32 pos, u32 len, u32 val)
233 {
234         u32 mask = (1 << len) - 1;
235         *x &= ~(mask << pos);
236         *x |= (val & mask) << pos;
237 }
238
239 inline u32 calc_parity(u32 v)
240 {
241         v ^= v >> 16;
242         v ^= v >> 8;
243         v ^= v >> 4;
244         v &= 0xf;
245         return (0x6996 >> v) & 1;
246 }
247
248 u64 murmur_hash_64_ua(const void *key, int len, unsigned int seed);
249
250 bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
251                 f32 camera_fov, f32 range, f32 *distance_ptr=NULL);
252
253 s16 adjustDist(s16 dist, float zoom_fov);
254
255 /*
256         Returns nearest 32-bit integer for given floating point number.
257         <cmath> and <math.h> in VC++ don't provide round().
258 */
259 inline s32 myround(f32 f)
260 {
261         return (s32)(f < 0.f ? (f - 0.5f) : (f + 0.5f));
262 }
263
264 inline constexpr f32 sqr(f32 f)
265 {
266         return f * f;
267 }
268
269 /*
270         Returns integer position of node in given floating point position
271 */
272 inline v3s16 floatToInt(v3f p, f32 d)
273 {
274         return v3s16(
275                 (p.X + (p.X > 0 ? d / 2 : -d / 2)) / d,
276                 (p.Y + (p.Y > 0 ? d / 2 : -d / 2)) / d,
277                 (p.Z + (p.Z > 0 ? d / 2 : -d / 2)) / d);
278 }
279
280 /*
281         Returns integer position of node in given double precision position
282  */
283 inline v3s16 doubleToInt(v3d p, double d)
284 {
285         return v3s16(
286                 (p.X + (p.X > 0 ? d / 2 : -d / 2)) / d,
287                 (p.Y + (p.Y > 0 ? d / 2 : -d / 2)) / d,
288                 (p.Z + (p.Z > 0 ? d / 2 : -d / 2)) / d);
289 }
290
291 /*
292         Returns floating point position of node in given integer position
293 */
294 inline v3f intToFloat(v3s16 p, f32 d)
295 {
296         return v3f(
297                 (f32)p.X * d,
298                 (f32)p.Y * d,
299                 (f32)p.Z * d
300         );
301 }
302
303 // Random helper. Usually d=BS
304 inline aabb3f getNodeBox(v3s16 p, float d)
305 {
306         return aabb3f(
307                 (float)p.X * d - 0.5f * d,
308                 (float)p.Y * d - 0.5f * d,
309                 (float)p.Z * d - 0.5f * d,
310                 (float)p.X * d + 0.5f * d,
311                 (float)p.Y * d + 0.5f * d,
312                 (float)p.Z * d + 0.5f * d
313         );
314 }
315
316
317 class IntervalLimiter
318 {
319 public:
320         IntervalLimiter() = default;
321
322         /*
323                 dtime: time from last call to this method
324                 wanted_interval: interval wanted
325                 return value:
326                         true: action should be skipped
327                         false: action should be done
328         */
329         bool step(float dtime, float wanted_interval)
330         {
331                 m_accumulator += dtime;
332                 if (m_accumulator < wanted_interval)
333                         return false;
334                 m_accumulator -= wanted_interval;
335                 return true;
336         }
337
338 private:
339         float m_accumulator = 0.0f;
340 };
341
342
343 /*
344         Splits a list into "pages". For example, the list [1,2,3,4,5] split
345         into two pages would be [1,2,3],[4,5]. This function computes the
346         minimum and maximum indices of a single page.
347
348         length: Length of the list that should be split
349         page: Page number, 1 <= page <= pagecount
350         pagecount: The number of pages, >= 1
351         minindex: Receives the minimum index (inclusive).
352         maxindex: Receives the maximum index (exclusive).
353
354         Ensures 0 <= minindex <= maxindex <= length.
355 */
356 inline void paging(u32 length, u32 page, u32 pagecount, u32 &minindex, u32 &maxindex)
357 {
358         if (length < 1 || pagecount < 1 || page < 1 || page > pagecount) {
359                 // Special cases or invalid parameters
360                 minindex = maxindex = 0;
361         } else if(pagecount <= length) {
362                 // Less pages than entries in the list:
363                 // Each page contains at least one entry
364                 minindex = (length * (page-1) + (pagecount-1)) / pagecount;
365                 maxindex = (length * page + (pagecount-1)) / pagecount;
366         } else {
367                 // More pages than entries in the list:
368                 // Make sure the empty pages are at the end
369                 if (page < length) {
370                         minindex = page-1;
371                         maxindex = page;
372                 } else {
373                         minindex = 0;
374                         maxindex = 0;
375                 }
376         }
377 }
378
379 inline float cycle_shift(float value, float by = 0, float max = 1)
380 {
381     if (value + by < 0)   return value + by + max;
382     if (value + by > max) return value + by - max;
383     return value + by;
384 }
385
386 inline bool is_power_of_two(u32 n)
387 {
388         return n != 0 && (n & (n - 1)) == 0;
389 }
390
391 // Compute next-higher power of 2 efficiently, e.g. for power-of-2 texture sizes.
392 // Public Domain: https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
393 inline u32 npot2(u32 orig) {
394         orig--;
395         orig |= orig >> 1;
396         orig |= orig >> 2;
397         orig |= orig >> 4;
398         orig |= orig >> 8;
399         orig |= orig >> 16;
400         return orig + 1;
401 }
402
403 // Gradual steps towards the target value in a wrapped (circular) system
404 // using the shorter of both ways
405 template<typename T>
406 inline void wrappedApproachShortest(T &current, const T target, const T stepsize,
407         const T maximum)
408 {
409         T delta = target - current;
410         if (delta < 0)
411                 delta += maximum;
412
413         if (delta > stepsize && maximum - delta > stepsize) {
414                 current += (delta < maximum / 2) ? stepsize : -stepsize;
415                 if (current >= maximum)
416                         current -= maximum;
417         } else {
418                 current = target;
419         }
420 }
421
422 void setPitchYawRollRad(core::matrix4 &m, const v3f &rot);
423
424 inline void setPitchYawRoll(core::matrix4 &m, const v3f &rot)
425 {
426         setPitchYawRollRad(m, rot * core::DEGTORAD64);
427 }
428
429 v3f getPitchYawRollRad(const core::matrix4 &m);
430
431 inline v3f getPitchYawRoll(const core::matrix4 &m)
432 {
433         return getPitchYawRollRad(m) * core::RADTODEG64;
434 }