]> git.lizzy.rs Git - minetest.git/blob - src/util/numeric.h
Use fixed size for builtin menus on non-android platforms
[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 "../irrlichttypes.h"
24 #include "../irr_v2d.h"
25 #include "../irr_v3d.h"
26 #include "../irr_aabb3d.h"
27 #include <list>
28 #include <map>
29 #include <vector>
30 #include <algorithm>
31
32
33 /*
34  * This class permits to cache getFacePosition call results
35  * This reduces CPU usage and vector calls
36  */
37 class FacePositionCache
38 {
39 public:
40         static std::vector<v3s16> getFacePositions(u16 d);
41 private:
42         static void generateFacePosition(u16 d);
43         static std::map<u16, std::vector<v3s16> > m_cache;
44 };
45
46 class IndentationRaiser
47 {
48 public:
49         IndentationRaiser(u16 *indentation)
50         {
51                 m_indentation = indentation;
52                 (*m_indentation)++;
53         }
54         ~IndentationRaiser()
55         {
56                 (*m_indentation)--;
57         }
58 private:
59         u16 *m_indentation;
60 };
61
62 inline s16 getContainerPos(s16 p, s16 d)
63 {
64         return (p>=0 ? p : p-d+1) / d;
65 }
66
67 inline v2s16 getContainerPos(v2s16 p, s16 d)
68 {
69         return v2s16(
70                 getContainerPos(p.X, d),
71                 getContainerPos(p.Y, d)
72         );
73 }
74
75 inline v3s16 getContainerPos(v3s16 p, s16 d)
76 {
77         return v3s16(
78                 getContainerPos(p.X, d),
79                 getContainerPos(p.Y, d),
80                 getContainerPos(p.Z, d)
81         );
82 }
83
84 inline v2s16 getContainerPos(v2s16 p, v2s16 d)
85 {
86         return v2s16(
87                 getContainerPos(p.X, d.X),
88                 getContainerPos(p.Y, d.Y)
89         );
90 }
91
92 inline v3s16 getContainerPos(v3s16 p, v3s16 d)
93 {
94         return v3s16(
95                 getContainerPos(p.X, d.X),
96                 getContainerPos(p.Y, d.Y),
97                 getContainerPos(p.Z, d.Z)
98         );
99 }
100
101 inline void getContainerPosWithOffset(s16 p, s16 d, s16 &container, s16 &offset)
102 {
103         container = (p >= 0 ? p : p - d + 1) / d;
104         offset = p & (d - 1);
105 }
106
107 inline void getContainerPosWithOffset(const v2s16 &p, s16 d, v2s16 &container, v2s16 &offset)
108 {
109         getContainerPosWithOffset(p.X, d, container.X, offset.X);
110         getContainerPosWithOffset(p.Y, d, container.Y, offset.Y);
111 }
112
113 inline void getContainerPosWithOffset(const v3s16 &p, s16 d, v3s16 &container, v3s16 &offset)
114 {
115         getContainerPosWithOffset(p.X, d, container.X, offset.X);
116         getContainerPosWithOffset(p.Y, d, container.Y, offset.Y);
117         getContainerPosWithOffset(p.Z, d, container.Z, offset.Z);
118 }
119
120
121 inline bool isInArea(v3s16 p, s16 d)
122 {
123         return (
124                 p.X >= 0 && p.X < d &&
125                 p.Y >= 0 && p.Y < d &&
126                 p.Z >= 0 && p.Z < d
127         );
128 }
129
130 inline bool isInArea(v2s16 p, s16 d)
131 {
132         return (
133                 p.X >= 0 && p.X < d &&
134                 p.Y >= 0 && p.Y < d
135         );
136 }
137
138 inline bool isInArea(v3s16 p, v3s16 d)
139 {
140         return (
141                 p.X >= 0 && p.X < d.X &&
142                 p.Y >= 0 && p.Y < d.Y &&
143                 p.Z >= 0 && p.Z < d.Z
144         );
145 }
146
147 #define rangelim(d, min, max) ((d) < (min) ? (min) : ((d)>(max)?(max):(d)))
148 #define myfloor(x) ((x) > 0.0 ? (int)(x) : (int)(x) - 1)
149
150 inline v3s16 arealim(v3s16 p, s16 d)
151 {
152         if(p.X < 0)
153                 p.X = 0;
154         if(p.Y < 0)
155                 p.Y = 0;
156         if(p.Z < 0)
157                 p.Z = 0;
158         if(p.X > d-1)
159                 p.X = d-1;
160         if(p.Y > d-1)
161                 p.Y = d-1;
162         if(p.Z > d-1)
163                 p.Z = d-1;
164         return p;
165 }
166
167 #define ARRLEN(x) (sizeof(x) / sizeof((x)[0]))
168 #define CONTAINS(c, v) (std::find((c).begin(), (c).end(), (v)) != (c).end())
169
170 // The naive swap performs better than the xor version
171 #define SWAP(t, x, y) do { \
172         t temp = x;            \
173         x = y;                 \
174         y = temp;              \
175 } while (0)
176
177 inline void sortBoxVerticies(v3s16 &p1, v3s16 &p2) {
178         if (p1.X > p2.X)
179                 SWAP(s16, p1.X, p2.X);
180         if (p1.Y > p2.Y)
181                 SWAP(s16, p1.Y, p2.Y);
182         if (p1.Z > p2.Z)
183                 SWAP(s16, p1.Z, p2.Z);
184 }
185
186
187 /*
188         See test.cpp for example cases.
189         wraps degrees to the range of -360...360
190         NOTE: Wrapping to 0...360 is not used because pitch needs negative values.
191 */
192 inline float wrapDegrees(float f)
193 {
194         // Take examples of f=10, f=720.5, f=-0.5, f=-360.5
195         // This results in
196         // 10, 720, -1, -361
197         int i = floor(f);
198         // 0, 2, 0, -1
199         int l = i / 360;
200         // NOTE: This would be used for wrapping to 0...360
201         // 0, 2, -1, -2
202         /*if(i < 0)
203                 l -= 1;*/
204         // 0, 720, 0, -360
205         int k = l * 360;
206         // 10, 0.5, -0.5, -0.5
207         f -= float(k);
208         return f;
209 }
210
211 /* Wrap to 0...360 */
212 inline float wrapDegrees_0_360(float f)
213 {
214         // Take examples of f=10, f=720.5, f=-0.5, f=-360.5
215         // This results in
216         // 10, 720, -1, -361
217         int i = floor(f);
218         // 0, 2, 0, -1
219         int l = i / 360;
220         // Wrap to 0...360
221         // 0, 2, -1, -2
222         if(i < 0)
223                 l -= 1;
224         // 0, 720, 0, -360
225         int k = l * 360;
226         // 10, 0.5, -0.5, -0.5
227         f -= float(k);
228         return f;
229 }
230
231 /* Wrap to -180...180 */
232 inline float wrapDegrees_180(float f)
233 {
234         f += 180;
235         f = wrapDegrees_0_360(f);
236         f -= 180;
237         return f;
238 }
239
240 /*
241         Pseudo-random (VC++ rand() sucks)
242 */
243 int myrand(void);
244 void mysrand(unsigned seed);
245 #define MYRAND_MAX 32767
246
247 int myrand_range(int min, int max);
248
249 /*
250         Miscellaneous functions
251 */
252
253 u64 murmur_hash_64_ua(const void *key, int len, unsigned int seed);
254
255 bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
256                 f32 camera_fov, f32 range, f32 *distance_ptr=NULL);
257
258 /*
259         Some helper stuff
260 */
261 #define MYMIN(a,b) ((a)<(b)?(a):(b))
262 #define MYMAX(a,b) ((a)>(b)?(a):(b))
263
264 /*
265         Returns nearest 32-bit integer for given floating point number.
266         <cmath> and <math.h> in VC++ don't provide round().
267 */
268 inline s32 myround(f32 f)
269 {
270         return floor(f + 0.5);
271 }
272
273 /*
274         Returns integer position of node in given floating point position
275 */
276 inline v3s16 floatToInt(v3f p, f32 d)
277 {
278         v3s16 p2(
279                 (p.X + (p.X>0 ? d/2 : -d/2))/d,
280                 (p.Y + (p.Y>0 ? d/2 : -d/2))/d,
281                 (p.Z + (p.Z>0 ? d/2 : -d/2))/d);
282         return p2;
283 }
284
285 /*
286         Returns floating point position of node in given integer position
287 */
288 inline v3f intToFloat(v3s16 p, f32 d)
289 {
290         v3f p2(
291                 (f32)p.X * d,
292                 (f32)p.Y * d,
293                 (f32)p.Z * d
294         );
295         return p2;
296 }
297
298 // Random helper. Usually d=BS
299 inline core::aabbox3d<f32> getNodeBox(v3s16 p, float d)
300 {
301         return core::aabbox3d<f32>(
302                 (float)p.X * d - 0.5*d,
303                 (float)p.Y * d - 0.5*d,
304                 (float)p.Z * d - 0.5*d,
305                 (float)p.X * d + 0.5*d,
306                 (float)p.Y * d + 0.5*d,
307                 (float)p.Z * d + 0.5*d
308         );
309 }
310
311 class IntervalLimiter
312 {
313 public:
314         IntervalLimiter():
315                 m_accumulator(0)
316         {
317         }
318         /*
319                 dtime: time from last call to this method
320                 wanted_interval: interval wanted
321                 return value:
322                         true: action should be skipped
323                         false: action should be done
324         */
325         bool step(float dtime, float wanted_interval)
326         {
327                 m_accumulator += dtime;
328                 if(m_accumulator < wanted_interval)
329                         return false;
330                 m_accumulator -= wanted_interval;
331                 return true;
332         }
333 protected:
334         float m_accumulator;
335 };
336
337 /*
338         Splits a list into "pages". For example, the list [1,2,3,4,5] split
339         into two pages would be [1,2,3],[4,5]. This function computes the
340         minimum and maximum indices of a single page.
341
342         length: Length of the list that should be split
343         page: Page number, 1 <= page <= pagecount
344         pagecount: The number of pages, >= 1
345         minindex: Receives the minimum index (inclusive).
346         maxindex: Receives the maximum index (exclusive).
347
348         Ensures 0 <= minindex <= maxindex <= length.
349 */
350 inline void paging(u32 length, u32 page, u32 pagecount, u32 &minindex, u32 &maxindex)
351 {
352         if(length < 1 || pagecount < 1 || page < 1 || page > pagecount)
353         {
354                 // Special cases or invalid parameters
355                 minindex = maxindex = 0;
356         }
357         else if(pagecount <= length)
358         {
359                 // Less pages than entries in the list:
360                 // Each page contains at least one entry
361                 minindex = (length * (page-1) + (pagecount-1)) / pagecount;
362                 maxindex = (length * page + (pagecount-1)) / pagecount;
363         }
364         else
365         {
366                 // More pages than entries in the list:
367                 // Make sure the empty pages are at the end
368                 if(page < length)
369                 {
370                         minindex = page-1;
371                         maxindex = page;
372                 }
373                 else
374                 {
375                         minindex = 0;
376                         maxindex = 0;
377                 }
378         }
379 }
380
381 inline float cycle_shift(float value, float by = 0, float max = 1)
382 {
383     if (value + by < 0) return max + by + value;
384     if (value + by > max) return value + by - max;
385     return value + by;
386 }
387
388 inline bool is_power_of_two(u32 n)
389 {
390         return n != 0 && (n & (n-1)) == 0;
391 }
392
393 #endif
394