3 Copyright (C) 2010-2012 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"
29 // Calculate the borders of a "d-radius" cube
30 void getFacePositions(core::list<v3s16> &list, u16 d);
32 class IndentationRaiser
35 IndentationRaiser(u16 *indentation)
37 m_indentation = indentation;
48 inline s16 getContainerPos(s16 p, s16 d)
50 return (p>=0 ? p : p-d+1) / d;
53 inline v2s16 getContainerPos(v2s16 p, s16 d)
56 getContainerPos(p.X, d),
57 getContainerPos(p.Y, d)
61 inline v3s16 getContainerPos(v3s16 p, s16 d)
64 getContainerPos(p.X, d),
65 getContainerPos(p.Y, d),
66 getContainerPos(p.Z, d)
70 inline v2s16 getContainerPos(v2s16 p, v2s16 d)
73 getContainerPos(p.X, d.X),
74 getContainerPos(p.Y, d.Y)
78 inline v3s16 getContainerPos(v3s16 p, v3s16 d)
81 getContainerPos(p.X, d.X),
82 getContainerPos(p.Y, d.Y),
83 getContainerPos(p.Z, d.Z)
87 inline bool isInArea(v3s16 p, s16 d)
90 p.X >= 0 && p.X < d &&
91 p.Y >= 0 && p.Y < d &&
96 inline bool isInArea(v2s16 p, s16 d)
99 p.X >= 0 && p.X < d &&
104 inline bool isInArea(v3s16 p, v3s16 d)
107 p.X >= 0 && p.X < d.X &&
108 p.Y >= 0 && p.Y < d.Y &&
109 p.Z >= 0 && p.Z < d.Z
113 inline s16 rangelim(s16 i, s16 max)
122 #define rangelim(d, min, max) ((d) < (min) ? (min) : ((d)>(max)?(max):(d)))
124 inline v3s16 arealim(v3s16 p, s16 d)
143 See test.cpp for example cases.
144 wraps degrees to the range of -360...360
145 NOTE: Wrapping to 0...360 is not used because pitch needs negative values.
147 inline float wrapDegrees(float f)
149 // Take examples of f=10, f=720.5, f=-0.5, f=-360.5
155 // NOTE: This would be used for wrapping to 0...360
161 // 10, 0.5, -0.5, -0.5
166 /* Wrap to 0...360 */
167 inline float wrapDegrees_0_360(float f)
169 // Take examples of f=10, f=720.5, f=-0.5, f=-360.5
181 // 10, 0.5, -0.5, -0.5
186 /* Wrap to -180...180 */
187 inline float wrapDegrees_180(float f)
190 f = wrapDegrees_0_360(f);
196 Pseudo-random (VC++ rand() sucks)
199 void mysrand(unsigned seed);
200 #define MYRAND_MAX 32767
202 int myrand_range(int min, int max);
205 Miscellaneous functions
208 bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
209 f32 camera_fov, f32 range, f32 *distance_ptr=NULL);
214 #define MYMIN(a,b) ((a)<(b)?(a):(b))
215 #define MYMAX(a,b) ((a)>(b)?(a):(b))
218 Returns nearest 32-bit integer for given floating point number.
219 <cmath> and <math.h> in VC++ don't provide round().
221 inline s32 myround(f32 f)
223 return floor(f + 0.5);
227 Returns integer position of node in given floating point position
229 inline v3s16 floatToInt(v3f p, f32 d)
232 (p.X + (p.X>0 ? d/2 : -d/2))/d,
233 (p.Y + (p.Y>0 ? d/2 : -d/2))/d,
234 (p.Z + (p.Z>0 ? d/2 : -d/2))/d);
239 Returns floating point position of node in given integer position
241 inline v3f intToFloat(v3s16 p, f32 d)
251 // Random helper. Usually d=BS
252 inline core::aabbox3d<f32> getNodeBox(v3s16 p, float d)
254 return core::aabbox3d<f32>(
255 (float)p.X * d - 0.5*d,
256 (float)p.Y * d - 0.5*d,
257 (float)p.Z * d - 0.5*d,
258 (float)p.X * d + 0.5*d,
259 (float)p.Y * d + 0.5*d,
260 (float)p.Z * d + 0.5*d
264 class IntervalLimiter
272 dtime: time from last call to this method
273 wanted_interval: interval wanted
275 true: action should be skipped
276 false: action should be done
278 bool step(float dtime, float wanted_interval)
280 m_accumulator += dtime;
281 if(m_accumulator < wanted_interval)
283 m_accumulator -= wanted_interval;
291 Splits a list into "pages". For example, the list [1,2,3,4,5] split
292 into two pages would be [1,2,3],[4,5]. This function computes the
293 minimum and maximum indices of a single page.
295 length: Length of the list that should be split
296 page: Page number, 1 <= page <= pagecount
297 pagecount: The number of pages, >= 1
298 minindex: Receives the minimum index (inclusive).
299 maxindex: Receives the maximum index (exclusive).
301 Ensures 0 <= minindex <= maxindex <= length.
303 inline void paging(u32 length, u32 page, u32 pagecount, u32 &minindex, u32 &maxindex)
305 if(length < 1 || pagecount < 1 || page < 1 || page > pagecount)
307 // Special cases or invalid parameters
308 minindex = maxindex = 0;
310 else if(pagecount <= length)
312 // Less pages than entries in the list:
313 // Each page contains at least one entry
314 minindex = (length * (page-1) + (pagecount-1)) / pagecount;
315 maxindex = (length * page + (pagecount-1)) / pagecount;
319 // More pages than entries in the list:
320 // Make sure the empty pages are at the end