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