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"
25 // Calculate the borders of a "d-radius" cube
26 void getFacePositions(core::list<v3s16> &list, u16 d);
28 class IndentationRaiser
31 IndentationRaiser(u16 *indentation)
33 m_indentation = indentation;
44 inline s16 getContainerPos(s16 p, s16 d)
46 return (p>=0 ? p : p-d+1) / d;
49 inline v2s16 getContainerPos(v2s16 p, s16 d)
52 getContainerPos(p.X, d),
53 getContainerPos(p.Y, d)
57 inline v3s16 getContainerPos(v3s16 p, s16 d)
60 getContainerPos(p.X, d),
61 getContainerPos(p.Y, d),
62 getContainerPos(p.Z, d)
66 inline v2s16 getContainerPos(v2s16 p, v2s16 d)
69 getContainerPos(p.X, d.X),
70 getContainerPos(p.Y, d.Y)
74 inline v3s16 getContainerPos(v3s16 p, v3s16 d)
77 getContainerPos(p.X, d.X),
78 getContainerPos(p.Y, d.Y),
79 getContainerPos(p.Z, d.Z)
83 inline bool isInArea(v3s16 p, s16 d)
86 p.X >= 0 && p.X < d &&
87 p.Y >= 0 && p.Y < d &&
92 inline bool isInArea(v2s16 p, s16 d)
95 p.X >= 0 && p.X < d &&
100 inline bool isInArea(v3s16 p, v3s16 d)
103 p.X >= 0 && p.X < d.X &&
104 p.Y >= 0 && p.Y < d.Y &&
105 p.Z >= 0 && p.Z < d.Z
109 inline s16 rangelim(s16 i, s16 max)
118 #define rangelim(d, min, max) ((d) < (min) ? (min) : ((d)>(max)?(max):(d)))
120 inline v3s16 arealim(v3s16 p, s16 d)
139 See test.cpp for example cases.
140 wraps degrees to the range of -360...360
141 NOTE: Wrapping to 0...360 is not used because pitch needs negative values.
143 inline float wrapDegrees(float f)
145 // Take examples of f=10, f=720.5, f=-0.5, f=-360.5
151 // NOTE: This would be used for wrapping to 0...360
157 // 10, 0.5, -0.5, -0.5
162 /* Wrap to 0...360 */
163 inline float wrapDegrees_0_360(float f)
165 // Take examples of f=10, f=720.5, f=-0.5, f=-360.5
177 // 10, 0.5, -0.5, -0.5
182 /* Wrap to -180...180 */
183 inline float wrapDegrees_180(float f)
186 f = wrapDegrees_0_360(f);
192 Pseudo-random (VC++ rand() sucks)
195 void mysrand(unsigned seed);
196 #define MYRAND_MAX 32767
198 int myrand_range(int min, int max);
201 Miscellaneous functions
204 bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
205 f32 camera_fov, f32 range, f32 *distance_ptr=NULL);
210 #define MYMIN(a,b) ((a)<(b)?(a):(b))
211 #define MYMAX(a,b) ((a)>(b)?(a):(b))
214 Returns nearest 32-bit integer for given floating point number.
215 <cmath> and <math.h> in VC++ don't provide round().
217 inline s32 myround(f32 f)
219 return floor(f + 0.5);
223 Returns integer position of node in given floating point position
225 inline v3s16 floatToInt(v3f p, f32 d)
228 (p.X + (p.X>0 ? d/2 : -d/2))/d,
229 (p.Y + (p.Y>0 ? d/2 : -d/2))/d,
230 (p.Z + (p.Z>0 ? d/2 : -d/2))/d);
235 Returns floating point position of node in given integer position
237 inline v3f intToFloat(v3s16 p, f32 d)
247 // Random helper. Usually d=BS
248 inline core::aabbox3d<f32> getNodeBox(v3s16 p, float d)
250 return core::aabbox3d<f32>(
251 (float)p.X * d - 0.5*d,
252 (float)p.Y * d - 0.5*d,
253 (float)p.Z * d - 0.5*d,
254 (float)p.X * d + 0.5*d,
255 (float)p.Y * d + 0.5*d,
256 (float)p.Z * d + 0.5*d
260 class IntervalLimiter
268 dtime: time from last call to this method
269 wanted_interval: interval wanted
271 true: action should be skipped
272 false: action should be done
274 bool step(float dtime, float wanted_interval)
276 m_accumulator += dtime;
277 if(m_accumulator < wanted_interval)
279 m_accumulator -= wanted_interval;
287 Splits a list into "pages". For example, the list [1,2,3,4,5] split
288 into two pages would be [1,2,3],[4,5]. This function computes the
289 minimum and maximum indices of a single page.
291 length: Length of the list that should be split
292 page: Page number, 1 <= page <= pagecount
293 pagecount: The number of pages, >= 1
294 minindex: Receives the minimum index (inclusive).
295 maxindex: Receives the maximum index (exclusive).
297 Ensures 0 <= minindex <= maxindex <= length.
299 inline void paging(u32 length, u32 page, u32 pagecount, u32 &minindex, u32 &maxindex)
301 if(length < 1 || pagecount < 1 || page < 1 || page > pagecount)
303 // Special cases or invalid parameters
304 minindex = maxindex = 0;
306 else if(pagecount <= length)
308 // Less pages than entries in the list:
309 // Each page contains at least one entry
310 minindex = (length * (page-1) + (pagecount-1)) / pagecount;
311 maxindex = (length * page + (pagecount-1)) / pagecount;
315 // More pages than entries in the list:
316 // Make sure the empty pages are at the end