3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #ifndef UTIL_NUMERIC_HEADER
21 #define UTIL_NUMERIC_HEADER
23 #include "../irrlichttypes.h"
24 #include "../irr_v2d.h"
25 #include "../irr_v3d.h"
26 #include "../irr_aabb3d.h"
34 * This class permits to cache getFacePosition call results
35 * This reduces CPU usage and vector calls
37 class FacePositionCache
40 static std::vector<v3s16> getFacePositions(u16 d);
42 static void generateFacePosition(u16 d);
43 static std::map<u16, std::vector<v3s16> > m_cache;
46 class IndentationRaiser
49 IndentationRaiser(u16 *indentation)
51 m_indentation = indentation;
62 inline s16 getContainerPos(s16 p, s16 d)
64 return (p>=0 ? p : p-d+1) / d;
67 inline v2s16 getContainerPos(v2s16 p, s16 d)
70 getContainerPos(p.X, d),
71 getContainerPos(p.Y, d)
75 inline v3s16 getContainerPos(v3s16 p, s16 d)
78 getContainerPos(p.X, d),
79 getContainerPos(p.Y, d),
80 getContainerPos(p.Z, d)
84 inline v2s16 getContainerPos(v2s16 p, v2s16 d)
87 getContainerPos(p.X, d.X),
88 getContainerPos(p.Y, d.Y)
92 inline v3s16 getContainerPos(v3s16 p, v3s16 d)
95 getContainerPos(p.X, d.X),
96 getContainerPos(p.Y, d.Y),
97 getContainerPos(p.Z, d.Z)
101 inline void getContainerPosWithOffset(s16 p, s16 d, s16 &container, s16 &offset)
103 container = (p >= 0 ? p : p - d + 1) / d;
104 offset = p & (d - 1);
107 inline void getContainerPosWithOffset(const v2s16 &p, s16 d, v2s16 &container, v2s16 &offset)
109 getContainerPosWithOffset(p.X, d, container.X, offset.X);
110 getContainerPosWithOffset(p.Y, d, container.Y, offset.Y);
113 inline void getContainerPosWithOffset(const v3s16 &p, s16 d, v3s16 &container, v3s16 &offset)
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);
121 inline bool isInArea(v3s16 p, s16 d)
124 p.X >= 0 && p.X < d &&
125 p.Y >= 0 && p.Y < d &&
130 inline bool isInArea(v2s16 p, s16 d)
133 p.X >= 0 && p.X < d &&
138 inline bool isInArea(v3s16 p, v3s16 d)
141 p.X >= 0 && p.X < d.X &&
142 p.Y >= 0 && p.Y < d.Y &&
143 p.Z >= 0 && p.Z < d.Z
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)
150 inline v3s16 arealim(v3s16 p, s16 d)
167 #define ARRLEN(x) (sizeof(x) / sizeof((x)[0]))
168 #define CONTAINS(c, v) (std::find((c).begin(), (c).end(), (v)) != (c).end())
170 // The naive swap performs better than the xor version
171 #define SWAP(t, x, y) do { \
177 inline void sortBoxVerticies(v3s16 &p1, v3s16 &p2) {
179 SWAP(s16, p1.X, p2.X);
181 SWAP(s16, p1.Y, p2.Y);
183 SWAP(s16, p1.Z, p2.Z);
187 /** Returns \p f wrapped to the range [-360, 360]
189 * See test.cpp for example cases.
191 * \note This is also used in cases where degrees wrapped to the range [0, 360]
192 * is innapropriate (e.g. pitch needs negative values)
194 * \internal functionally equivalent -- although precision may vary slightly --
195 * to fmodf((f), 360.0f) however empirical tests indicate that this approach is
198 inline float modulo360f(float f)
213 fraction = f - whole;
216 return sign * (whole + fraction);
220 /** Returns \p f wrapped to the range [0, 360]
222 inline float wrapDegrees_0_360(float f)
224 float value = modulo360f(f);
225 return value < 0 ? value + 360 : value;
229 /** Returns \p f wrapped to the range [-180, 180]
231 inline float wrapDegrees_180(float f)
233 float value = modulo360f(f + 180);
240 Pseudo-random (VC++ rand() sucks)
242 #define MYRAND_RANGE 0xffffffff
244 void mysrand(unsigned int seed);
245 void myrand_bytes(void *out, size_t len);
246 int myrand_range(int min, int max);
249 Miscellaneous functions
252 inline u32 get_bits(u32 x, u32 pos, u32 len)
254 u32 mask = (1 << len) - 1;
255 return (x >> pos) & mask;
258 inline void set_bits(u32 *x, u32 pos, u32 len, u32 val)
260 u32 mask = (1 << len) - 1;
261 *x &= ~(mask << pos);
262 *x |= (val & mask) << pos;
265 inline u32 calc_parity(u32 v)
271 return (0x6996 >> v) & 1;
274 u64 murmur_hash_64_ua(const void *key, int len, unsigned int seed);
276 bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
277 f32 camera_fov, f32 range, f32 *distance_ptr=NULL);
282 #define MYMIN(a,b) ((a)<(b)?(a):(b))
283 #define MYMAX(a,b) ((a)>(b)?(a):(b))
286 Returns nearest 32-bit integer for given floating point number.
287 <cmath> and <math.h> in VC++ don't provide round().
289 inline s32 myround(f32 f)
291 return floor(f + 0.5);
295 Returns integer position of node in given floating point position
297 inline v3s16 floatToInt(v3f p, f32 d)
300 (p.X + (p.X>0 ? d/2 : -d/2))/d,
301 (p.Y + (p.Y>0 ? d/2 : -d/2))/d,
302 (p.Z + (p.Z>0 ? d/2 : -d/2))/d);
307 Returns floating point position of node in given integer position
309 inline v3f intToFloat(v3s16 p, f32 d)
319 // Random helper. Usually d=BS
320 inline core::aabbox3d<f32> getNodeBox(v3s16 p, float d)
322 return core::aabbox3d<f32>(
323 (float)p.X * d - 0.5*d,
324 (float)p.Y * d - 0.5*d,
325 (float)p.Z * d - 0.5*d,
326 (float)p.X * d + 0.5*d,
327 (float)p.Y * d + 0.5*d,
328 (float)p.Z * d + 0.5*d
332 class IntervalLimiter
340 dtime: time from last call to this method
341 wanted_interval: interval wanted
343 true: action should be skipped
344 false: action should be done
346 bool step(float dtime, float wanted_interval)
348 m_accumulator += dtime;
349 if(m_accumulator < wanted_interval)
351 m_accumulator -= wanted_interval;
359 Splits a list into "pages". For example, the list [1,2,3,4,5] split
360 into two pages would be [1,2,3],[4,5]. This function computes the
361 minimum and maximum indices of a single page.
363 length: Length of the list that should be split
364 page: Page number, 1 <= page <= pagecount
365 pagecount: The number of pages, >= 1
366 minindex: Receives the minimum index (inclusive).
367 maxindex: Receives the maximum index (exclusive).
369 Ensures 0 <= minindex <= maxindex <= length.
371 inline void paging(u32 length, u32 page, u32 pagecount, u32 &minindex, u32 &maxindex)
373 if(length < 1 || pagecount < 1 || page < 1 || page > pagecount)
375 // Special cases or invalid parameters
376 minindex = maxindex = 0;
378 else if(pagecount <= length)
380 // Less pages than entries in the list:
381 // Each page contains at least one entry
382 minindex = (length * (page-1) + (pagecount-1)) / pagecount;
383 maxindex = (length * page + (pagecount-1)) / pagecount;
387 // More pages than entries in the list:
388 // Make sure the empty pages are at the end
402 inline float cycle_shift(float value, float by = 0, float max = 1)
404 if (value + by < 0) return max + by + value;
405 if (value + by > max) return value + by - max;
409 inline bool is_power_of_two(u32 n)
411 return n != 0 && (n & (n-1)) == 0;
414 // Compute next-higher power of 2 efficiently, e.g. for power-of-2 texture sizes.
415 // Public Domain: https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
416 inline u32 npot2(u32 orig) {