Fix a memleak pointed by @Zeno- in MeshUpdateQueue
[oweals/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 #ifndef UTIL_NUMERIC_HEADER
21 #define UTIL_NUMERIC_HEADER
22
23 #include "basic_macros.h"
24 #include "../irrlichttypes.h"
25 #include "../irr_v2d.h"
26 #include "../irr_v3d.h"
27 #include "../irr_aabb3d.h"
28 #include "../threading/mutex.h"
29
30 #define rangelim(d, min, max) ((d) < (min) ? (min) : ((d) > (max) ? (max) : (d)))
31 #define myfloor(x) ((x) < 0.0 ? (int)(x) - 1 : (int)(x))
32 // The naive swap performs better than the xor version
33 #define SWAP(t, x, y) do { \
34         t temp = x; \
35         x = y; \
36         y = temp; \
37 } while (0)
38
39
40 inline s16 getContainerPos(s16 p, s16 d)
41 {
42         return (p >= 0 ? p : p - d + 1) / d;
43 }
44
45 inline v2s16 getContainerPos(v2s16 p, s16 d)
46 {
47         return v2s16(
48                 getContainerPos(p.X, d),
49                 getContainerPos(p.Y, d)
50         );
51 }
52
53 inline v3s16 getContainerPos(v3s16 p, s16 d)
54 {
55         return v3s16(
56                 getContainerPos(p.X, d),
57                 getContainerPos(p.Y, d),
58                 getContainerPos(p.Z, d)
59         );
60 }
61
62 inline v2s16 getContainerPos(v2s16 p, v2s16 d)
63 {
64         return v2s16(
65                 getContainerPos(p.X, d.X),
66                 getContainerPos(p.Y, d.Y)
67         );
68 }
69
70 inline v3s16 getContainerPos(v3s16 p, v3s16 d)
71 {
72         return v3s16(
73                 getContainerPos(p.X, d.X),
74                 getContainerPos(p.Y, d.Y),
75                 getContainerPos(p.Z, d.Z)
76         );
77 }
78
79 inline void getContainerPosWithOffset(s16 p, s16 d, s16 &container, s16 &offset)
80 {
81         container = (p >= 0 ? p : p - d + 1) / d;
82         offset = p & (d - 1);
83 }
84
85 inline void getContainerPosWithOffset(const v2s16 &p, s16 d, v2s16 &container, v2s16 &offset)
86 {
87         getContainerPosWithOffset(p.X, d, container.X, offset.X);
88         getContainerPosWithOffset(p.Y, d, container.Y, offset.Y);
89 }
90
91 inline void getContainerPosWithOffset(const v3s16 &p, s16 d, v3s16 &container, v3s16 &offset)
92 {
93         getContainerPosWithOffset(p.X, d, container.X, offset.X);
94         getContainerPosWithOffset(p.Y, d, container.Y, offset.Y);
95         getContainerPosWithOffset(p.Z, d, container.Z, offset.Z);
96 }
97
98
99 inline bool isInArea(v3s16 p, s16 d)
100 {
101         return (
102                 p.X >= 0 && p.X < d &&
103                 p.Y >= 0 && p.Y < d &&
104                 p.Z >= 0 && p.Z < d
105         );
106 }
107
108 inline bool isInArea(v2s16 p, s16 d)
109 {
110         return (
111                 p.X >= 0 && p.X < d &&
112                 p.Y >= 0 && p.Y < d
113         );
114 }
115
116 inline bool isInArea(v3s16 p, v3s16 d)
117 {
118         return (
119                 p.X >= 0 && p.X < d.X &&
120                 p.Y >= 0 && p.Y < d.Y &&
121                 p.Z >= 0 && p.Z < d.Z
122         );
123 }
124
125 inline void sortBoxVerticies(v3s16 &p1, v3s16 &p2) {
126         if (p1.X > p2.X)
127                 SWAP(s16, p1.X, p2.X);
128         if (p1.Y > p2.Y)
129                 SWAP(s16, p1.Y, p2.Y);
130         if (p1.Z > p2.Z)
131                 SWAP(s16, p1.Z, p2.Z);
132 }
133
134 inline v3s16 componentwise_min(const v3s16 &a, const v3s16 &b)
135 {
136         return v3s16(MYMIN(a.X, b.X), MYMIN(a.Y, b.Y), MYMIN(a.Z, b.Z));
137 }
138
139 inline v3s16 componentwise_max(const v3s16 &a, const v3s16 &b)
140 {
141         return v3s16(MYMAX(a.X, b.X), MYMAX(a.Y, b.Y), MYMAX(a.Z, b.Z));
142 }
143
144
145 /** Returns \p f wrapped to the range [-360, 360]
146  *
147  *  See test.cpp for example cases.
148  *
149  *  \note This is also used in cases where degrees wrapped to the range [0, 360]
150  *  is innapropriate (e.g. pitch needs negative values)
151  *
152  *  \internal functionally equivalent -- although precision may vary slightly --
153  *  to fmodf((f), 360.0f) however empirical tests indicate that this approach is
154  *  faster.
155  */
156 inline float modulo360f(float f)
157 {
158         int sign;
159         int whole;
160         float fraction;
161
162         if (f < 0) {
163                 f = -f;
164                 sign = -1;
165         } else {
166                 sign = 1;
167         }
168
169         whole = f;
170
171         fraction = f - whole;
172         whole %= 360;
173
174         return sign * (whole + fraction);
175 }
176
177
178 /** Returns \p f wrapped to the range [0, 360]
179   */
180 inline float wrapDegrees_0_360(float f)
181 {
182         float value = modulo360f(f);
183         return value < 0 ? value + 360 : value;
184 }
185
186
187 /** Returns \p f wrapped to the range [-180, 180]
188   */
189 inline float wrapDegrees_180(float f)
190 {
191         float value = modulo360f(f + 180);
192         if (value < 0)
193                 value += 360;
194         return value - 180;
195 }
196
197 /*
198         Pseudo-random (VC++ rand() sucks)
199 */
200 #define MYRAND_RANGE 0xffffffff
201 u32 myrand();
202 void mysrand(unsigned int seed);
203 void myrand_bytes(void *out, size_t len);
204 int myrand_range(int min, int max);
205
206 /*
207         Miscellaneous functions
208 */
209
210 inline u32 get_bits(u32 x, u32 pos, u32 len)
211 {
212         u32 mask = (1 << len) - 1;
213         return (x >> pos) & mask;
214 }
215
216 inline void set_bits(u32 *x, u32 pos, u32 len, u32 val)
217 {
218         u32 mask = (1 << len) - 1;
219         *x &= ~(mask << pos);
220         *x |= (val & mask) << pos;
221 }
222
223 inline u32 calc_parity(u32 v)
224 {
225         v ^= v >> 16;
226         v ^= v >> 8;
227         v ^= v >> 4;
228         v &= 0xf;
229         return (0x6996 >> v) & 1;
230 }
231
232 u64 murmur_hash_64_ua(const void *key, int len, unsigned int seed);
233
234 bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
235                 f32 camera_fov, f32 range, f32 *distance_ptr=NULL);
236
237 /*
238         Returns nearest 32-bit integer for given floating point number.
239         <cmath> and <math.h> in VC++ don't provide round().
240 */
241 inline s32 myround(f32 f)
242 {
243         return (s32)(f < 0.f ? (f - 0.5f) : (f + 0.5f));
244 }
245
246 /*
247         Returns integer position of node in given floating point position
248 */
249 inline v3s16 floatToInt(v3f p, f32 d)
250 {
251         return v3s16(
252                 (p.X + (p.X > 0 ? d / 2 : -d / 2)) / d,
253                 (p.Y + (p.Y > 0 ? d / 2 : -d / 2)) / d,
254                 (p.Z + (p.Z > 0 ? d / 2 : -d / 2)) / d);
255 }
256
257 /*
258         Returns floating point position of node in given integer position
259 */
260 inline v3f intToFloat(v3s16 p, f32 d)
261 {
262         return v3f(
263                 (f32)p.X * d,
264                 (f32)p.Y * d,
265                 (f32)p.Z * d
266         );
267 }
268
269 // Random helper. Usually d=BS
270 inline aabb3f getNodeBox(v3s16 p, float d)
271 {
272         return aabb3f(
273                 (float)p.X * d - 0.5 * d,
274                 (float)p.Y * d - 0.5 * d,
275                 (float)p.Z * d - 0.5 * d,
276                 (float)p.X * d + 0.5 * d,
277                 (float)p.Y * d + 0.5 * d,
278                 (float)p.Z * d + 0.5 * d
279         );
280 }
281
282
283 class IntervalLimiter
284 {
285 public:
286         IntervalLimiter() : m_accumulator(0) {}
287         /*
288                 dtime: time from last call to this method
289                 wanted_interval: interval wanted
290                 return value:
291                         true: action should be skipped
292                         false: action should be done
293         */
294         bool step(float dtime, float wanted_interval)
295         {
296                 m_accumulator += dtime;
297                 if (m_accumulator < wanted_interval)
298                         return false;
299                 m_accumulator -= wanted_interval;
300                 return true;
301         }
302
303 private:
304         float m_accumulator;
305 };
306
307
308 /*
309         Splits a list into "pages". For example, the list [1,2,3,4,5] split
310         into two pages would be [1,2,3],[4,5]. This function computes the
311         minimum and maximum indices of a single page.
312
313         length: Length of the list that should be split
314         page: Page number, 1 <= page <= pagecount
315         pagecount: The number of pages, >= 1
316         minindex: Receives the minimum index (inclusive).
317         maxindex: Receives the maximum index (exclusive).
318
319         Ensures 0 <= minindex <= maxindex <= length.
320 */
321 inline void paging(u32 length, u32 page, u32 pagecount, u32 &minindex, u32 &maxindex)
322 {
323         if (length < 1 || pagecount < 1 || page < 1 || page > pagecount) {
324                 // Special cases or invalid parameters
325                 minindex = maxindex = 0;
326         } else if(pagecount <= length) {
327                 // Less pages than entries in the list:
328                 // Each page contains at least one entry
329                 minindex = (length * (page-1) + (pagecount-1)) / pagecount;
330                 maxindex = (length * page + (pagecount-1)) / pagecount;
331         } else {
332                 // More pages than entries in the list:
333                 // Make sure the empty pages are at the end
334                 if (page < length) {
335                         minindex = page-1;
336                         maxindex = page;
337                 } else {
338                         minindex = 0;
339                         maxindex = 0;
340                 }
341         }
342 }
343
344 inline float cycle_shift(float value, float by = 0, float max = 1)
345 {
346     if (value + by < 0)   return value + by + max;
347     if (value + by > max) return value + by - max;
348     return value + by;
349 }
350
351 inline bool is_power_of_two(u32 n)
352 {
353         return n != 0 && (n & (n - 1)) == 0;
354 }
355
356 // Compute next-higher power of 2 efficiently, e.g. for power-of-2 texture sizes.
357 // Public Domain: https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
358 inline u32 npot2(u32 orig) {
359         orig--;
360         orig |= orig >> 1;
361         orig |= orig >> 2;
362         orig |= orig >> 4;
363         orig |= orig >> 8;
364         orig |= orig >> 16;
365         return orig + 1;
366 }
367
368 #endif