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"
30 // Calculate the borders of a "d-radius" cube
31 void getFacePositions(std::list<v3s16> &list, u16 d);
33 class IndentationRaiser
36 IndentationRaiser(u16 *indentation)
38 m_indentation = indentation;
49 inline s16 getContainerPos(s16 p, s16 d)
51 return (p>=0 ? p : p-d+1) / d;
54 inline v2s16 getContainerPos(v2s16 p, s16 d)
57 getContainerPos(p.X, d),
58 getContainerPos(p.Y, d)
62 inline v3s16 getContainerPos(v3s16 p, s16 d)
65 getContainerPos(p.X, d),
66 getContainerPos(p.Y, d),
67 getContainerPos(p.Z, d)
71 inline v2s16 getContainerPos(v2s16 p, v2s16 d)
74 getContainerPos(p.X, d.X),
75 getContainerPos(p.Y, d.Y)
79 inline v3s16 getContainerPos(v3s16 p, v3s16 d)
82 getContainerPos(p.X, d.X),
83 getContainerPos(p.Y, d.Y),
84 getContainerPos(p.Z, d.Z)
88 inline bool isInArea(v3s16 p, s16 d)
91 p.X >= 0 && p.X < d &&
92 p.Y >= 0 && p.Y < d &&
97 inline bool isInArea(v2s16 p, s16 d)
100 p.X >= 0 && p.X < d &&
105 inline bool isInArea(v3s16 p, v3s16 d)
108 p.X >= 0 && p.X < d.X &&
109 p.Y >= 0 && p.Y < d.Y &&
110 p.Z >= 0 && p.Z < d.Z
114 inline s16 rangelim(s16 i, s16 max)
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)
126 inline v3s16 arealim(v3s16 p, s16 d)
143 // The naive swap performs better than the xor version
144 #define SWAP(t, x, y) do { \
150 inline void sortBoxVerticies(v3s16 &p1, v3s16 &p2) {
152 SWAP(s16, p1.X, p2.X);
154 SWAP(s16, p1.Y, p2.Y);
156 SWAP(s16, p1.Z, p2.Z);
161 See test.cpp for example cases.
162 wraps degrees to the range of -360...360
163 NOTE: Wrapping to 0...360 is not used because pitch needs negative values.
165 inline float wrapDegrees(float f)
167 // Take examples of f=10, f=720.5, f=-0.5, f=-360.5
173 // NOTE: This would be used for wrapping to 0...360
179 // 10, 0.5, -0.5, -0.5
184 /* Wrap to 0...360 */
185 inline float wrapDegrees_0_360(float f)
187 // Take examples of f=10, f=720.5, f=-0.5, f=-360.5
199 // 10, 0.5, -0.5, -0.5
204 /* Wrap to -180...180 */
205 inline float wrapDegrees_180(float f)
208 f = wrapDegrees_0_360(f);
214 Pseudo-random (VC++ rand() sucks)
217 void mysrand(unsigned seed);
218 #define MYRAND_MAX 32767
220 int myrand_range(int min, int max);
223 Miscellaneous functions
226 bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
227 f32 camera_fov, f32 range, f32 *distance_ptr=NULL);
232 #define MYMIN(a,b) ((a)<(b)?(a):(b))
233 #define MYMAX(a,b) ((a)>(b)?(a):(b))
236 Returns nearest 32-bit integer for given floating point number.
237 <cmath> and <math.h> in VC++ don't provide round().
239 inline s32 myround(f32 f)
241 return floor(f + 0.5);
245 Returns integer position of node in given floating point position
247 inline v3s16 floatToInt(v3f p, f32 d)
250 (p.X + (p.X>0 ? d/2 : -d/2))/d,
251 (p.Y + (p.Y>0 ? d/2 : -d/2))/d,
252 (p.Z + (p.Z>0 ? d/2 : -d/2))/d);
257 Returns floating point position of node in given integer position
259 inline v3f intToFloat(v3s16 p, f32 d)
269 // Random helper. Usually d=BS
270 inline core::aabbox3d<f32> getNodeBox(v3s16 p, float d)
272 return core::aabbox3d<f32>(
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
282 class IntervalLimiter
290 dtime: time from last call to this method
291 wanted_interval: interval wanted
293 true: action should be skipped
294 false: action should be done
296 bool step(float dtime, float wanted_interval)
298 m_accumulator += dtime;
299 if(m_accumulator < wanted_interval)
301 m_accumulator -= wanted_interval;
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.
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).
319 Ensures 0 <= minindex <= maxindex <= length.
321 inline void paging(u32 length, u32 page, u32 pagecount, u32 &minindex, u32 &maxindex)
323 if(length < 1 || pagecount < 1 || page < 1 || page > pagecount)
325 // Special cases or invalid parameters
326 minindex = maxindex = 0;
328 else if(pagecount <= length)
330 // Less pages than entries in the list:
331 // Each page contains at least one entry
332 minindex = (length * (page-1) + (pagecount-1)) / pagecount;
333 maxindex = (length * page + (pagecount-1)) / pagecount;
337 // More pages than entries in the list:
338 // Make sure the empty pages are at the end
352 inline float cycle_shift(float value, float by = 0, float max = 1)
354 if (value + by < 0) return max + by + value;
355 if (value + by > max) return value + by - max;