numeric.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "basic_macros.h"
  18. #include "irrlichttypes.h"
  19. #include "irr_v2d.h"
  20. #include "irr_v3d.h"
  21. #include "irr_aabb3d.h"
  22. #define rangelim(d, min, max) ((d) < (min) ? (min) : ((d) > (max) ? (max) : (d)))
  23. #define myfloor(x) ((x) < 0.0 ? (int)(x) - 1 : (int)(x))
  24. // The naive swap performs better than the xor version
  25. #define SWAP(t, x, y) do { \
  26. t temp = x; \
  27. x = y; \
  28. y = temp; \
  29. } while (0)
  30. inline s16 getContainerPos(s16 p, s16 d)
  31. {
  32. return (p >= 0 ? p : p - d + 1) / d;
  33. }
  34. inline v2s16 getContainerPos(v2s16 p, s16 d)
  35. {
  36. return v2s16(
  37. getContainerPos(p.X, d),
  38. getContainerPos(p.Y, d)
  39. );
  40. }
  41. inline v3s16 getContainerPos(v3s16 p, s16 d)
  42. {
  43. return v3s16(
  44. getContainerPos(p.X, d),
  45. getContainerPos(p.Y, d),
  46. getContainerPos(p.Z, d)
  47. );
  48. }
  49. inline v2s16 getContainerPos(v2s16 p, v2s16 d)
  50. {
  51. return v2s16(
  52. getContainerPos(p.X, d.X),
  53. getContainerPos(p.Y, d.Y)
  54. );
  55. }
  56. inline v3s16 getContainerPos(v3s16 p, v3s16 d)
  57. {
  58. return v3s16(
  59. getContainerPos(p.X, d.X),
  60. getContainerPos(p.Y, d.Y),
  61. getContainerPos(p.Z, d.Z)
  62. );
  63. }
  64. inline void getContainerPosWithOffset(s16 p, s16 d, s16 &container, s16 &offset)
  65. {
  66. container = (p >= 0 ? p : p - d + 1) / d;
  67. offset = p & (d - 1);
  68. }
  69. inline void getContainerPosWithOffset(const v2s16 &p, s16 d, v2s16 &container, v2s16 &offset)
  70. {
  71. getContainerPosWithOffset(p.X, d, container.X, offset.X);
  72. getContainerPosWithOffset(p.Y, d, container.Y, offset.Y);
  73. }
  74. inline void getContainerPosWithOffset(const v3s16 &p, s16 d, v3s16 &container, v3s16 &offset)
  75. {
  76. getContainerPosWithOffset(p.X, d, container.X, offset.X);
  77. getContainerPosWithOffset(p.Y, d, container.Y, offset.Y);
  78. getContainerPosWithOffset(p.Z, d, container.Z, offset.Z);
  79. }
  80. inline bool isInArea(v3s16 p, s16 d)
  81. {
  82. return (
  83. p.X >= 0 && p.X < d &&
  84. p.Y >= 0 && p.Y < d &&
  85. p.Z >= 0 && p.Z < d
  86. );
  87. }
  88. inline bool isInArea(v2s16 p, s16 d)
  89. {
  90. return (
  91. p.X >= 0 && p.X < d &&
  92. p.Y >= 0 && p.Y < d
  93. );
  94. }
  95. inline bool isInArea(v3s16 p, v3s16 d)
  96. {
  97. return (
  98. p.X >= 0 && p.X < d.X &&
  99. p.Y >= 0 && p.Y < d.Y &&
  100. p.Z >= 0 && p.Z < d.Z
  101. );
  102. }
  103. inline void sortBoxVerticies(v3s16 &p1, v3s16 &p2) {
  104. if (p1.X > p2.X)
  105. SWAP(s16, p1.X, p2.X);
  106. if (p1.Y > p2.Y)
  107. SWAP(s16, p1.Y, p2.Y);
  108. if (p1.Z > p2.Z)
  109. SWAP(s16, p1.Z, p2.Z);
  110. }
  111. inline v3s16 componentwise_min(const v3s16 &a, const v3s16 &b)
  112. {
  113. return v3s16(MYMIN(a.X, b.X), MYMIN(a.Y, b.Y), MYMIN(a.Z, b.Z));
  114. }
  115. inline v3s16 componentwise_max(const v3s16 &a, const v3s16 &b)
  116. {
  117. return v3s16(MYMAX(a.X, b.X), MYMAX(a.Y, b.Y), MYMAX(a.Z, b.Z));
  118. }
  119. /** Returns \p f wrapped to the range [-360, 360]
  120. *
  121. * See test.cpp for example cases.
  122. *
  123. * \note This is also used in cases where degrees wrapped to the range [0, 360]
  124. * is innapropriate (e.g. pitch needs negative values)
  125. *
  126. * \internal functionally equivalent -- although precision may vary slightly --
  127. * to fmodf((f), 360.0f) however empirical tests indicate that this approach is
  128. * faster.
  129. */
  130. inline float modulo360f(float f)
  131. {
  132. int sign;
  133. int whole;
  134. float fraction;
  135. if (f < 0) {
  136. f = -f;
  137. sign = -1;
  138. } else {
  139. sign = 1;
  140. }
  141. whole = f;
  142. fraction = f - whole;
  143. whole %= 360;
  144. return sign * (whole + fraction);
  145. }
  146. /** Returns \p f wrapped to the range [0, 360]
  147. */
  148. inline float wrapDegrees_0_360(float f)
  149. {
  150. float value = modulo360f(f);
  151. return value < 0 ? value + 360 : value;
  152. }
  153. /** Returns \p f wrapped to the range [-180, 180]
  154. */
  155. inline float wrapDegrees_180(float f)
  156. {
  157. float value = modulo360f(f + 180);
  158. if (value < 0)
  159. value += 360;
  160. return value - 180;
  161. }
  162. /*
  163. Pseudo-random (VC++ rand() sucks)
  164. */
  165. #define MYRAND_RANGE 0xffffffff
  166. u32 myrand();
  167. void mysrand(unsigned int seed);
  168. void myrand_bytes(void *out, size_t len);
  169. int myrand_range(int min, int max);
  170. /*
  171. Miscellaneous functions
  172. */
  173. inline u32 get_bits(u32 x, u32 pos, u32 len)
  174. {
  175. u32 mask = (1 << len) - 1;
  176. return (x >> pos) & mask;
  177. }
  178. inline void set_bits(u32 *x, u32 pos, u32 len, u32 val)
  179. {
  180. u32 mask = (1 << len) - 1;
  181. *x &= ~(mask << pos);
  182. *x |= (val & mask) << pos;
  183. }
  184. inline u32 calc_parity(u32 v)
  185. {
  186. v ^= v >> 16;
  187. v ^= v >> 8;
  188. v ^= v >> 4;
  189. v &= 0xf;
  190. return (0x6996 >> v) & 1;
  191. }
  192. u64 murmur_hash_64_ua(const void *key, int len, unsigned int seed);
  193. bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
  194. f32 camera_fov, f32 range, f32 *distance_ptr=NULL);
  195. s16 adjustDist(s16 dist, float zoom_fov);
  196. /*
  197. Returns nearest 32-bit integer for given floating point number.
  198. <cmath> and <math.h> in VC++ don't provide round().
  199. */
  200. inline s32 myround(f32 f)
  201. {
  202. return (s32)(f < 0.f ? (f - 0.5f) : (f + 0.5f));
  203. }
  204. /*
  205. Returns integer position of node in given floating point position
  206. */
  207. inline v3s16 floatToInt(v3f p, f32 d)
  208. {
  209. return v3s16(
  210. (p.X + (p.X > 0 ? d / 2 : -d / 2)) / d,
  211. (p.Y + (p.Y > 0 ? d / 2 : -d / 2)) / d,
  212. (p.Z + (p.Z > 0 ? d / 2 : -d / 2)) / d);
  213. }
  214. /*
  215. Returns integer position of node in given double precision position
  216. */
  217. inline v3s16 doubleToInt(v3d p, double d)
  218. {
  219. return v3s16(
  220. (p.X + (p.X > 0 ? d / 2 : -d / 2)) / d,
  221. (p.Y + (p.Y > 0 ? d / 2 : -d / 2)) / d,
  222. (p.Z + (p.Z > 0 ? d / 2 : -d / 2)) / d);
  223. }
  224. /*
  225. Returns floating point position of node in given integer position
  226. */
  227. inline v3f intToFloat(v3s16 p, f32 d)
  228. {
  229. return v3f(
  230. (f32)p.X * d,
  231. (f32)p.Y * d,
  232. (f32)p.Z * d
  233. );
  234. }
  235. // Random helper. Usually d=BS
  236. inline aabb3f getNodeBox(v3s16 p, float d)
  237. {
  238. return aabb3f(
  239. (float)p.X * d - 0.5f * d,
  240. (float)p.Y * d - 0.5f * d,
  241. (float)p.Z * d - 0.5f * d,
  242. (float)p.X * d + 0.5f * d,
  243. (float)p.Y * d + 0.5f * d,
  244. (float)p.Z * d + 0.5f * d
  245. );
  246. }
  247. class IntervalLimiter
  248. {
  249. public:
  250. IntervalLimiter() = default;
  251. /*
  252. dtime: time from last call to this method
  253. wanted_interval: interval wanted
  254. return value:
  255. true: action should be skipped
  256. false: action should be done
  257. */
  258. bool step(float dtime, float wanted_interval)
  259. {
  260. m_accumulator += dtime;
  261. if (m_accumulator < wanted_interval)
  262. return false;
  263. m_accumulator -= wanted_interval;
  264. return true;
  265. }
  266. private:
  267. float m_accumulator = 0.0f;
  268. };
  269. /*
  270. Splits a list into "pages". For example, the list [1,2,3,4,5] split
  271. into two pages would be [1,2,3],[4,5]. This function computes the
  272. minimum and maximum indices of a single page.
  273. length: Length of the list that should be split
  274. page: Page number, 1 <= page <= pagecount
  275. pagecount: The number of pages, >= 1
  276. minindex: Receives the minimum index (inclusive).
  277. maxindex: Receives the maximum index (exclusive).
  278. Ensures 0 <= minindex <= maxindex <= length.
  279. */
  280. inline void paging(u32 length, u32 page, u32 pagecount, u32 &minindex, u32 &maxindex)
  281. {
  282. if (length < 1 || pagecount < 1 || page < 1 || page > pagecount) {
  283. // Special cases or invalid parameters
  284. minindex = maxindex = 0;
  285. } else if(pagecount <= length) {
  286. // Less pages than entries in the list:
  287. // Each page contains at least one entry
  288. minindex = (length * (page-1) + (pagecount-1)) / pagecount;
  289. maxindex = (length * page + (pagecount-1)) / pagecount;
  290. } else {
  291. // More pages than entries in the list:
  292. // Make sure the empty pages are at the end
  293. if (page < length) {
  294. minindex = page-1;
  295. maxindex = page;
  296. } else {
  297. minindex = 0;
  298. maxindex = 0;
  299. }
  300. }
  301. }
  302. inline float cycle_shift(float value, float by = 0, float max = 1)
  303. {
  304. if (value + by < 0) return value + by + max;
  305. if (value + by > max) return value + by - max;
  306. return value + by;
  307. }
  308. inline bool is_power_of_two(u32 n)
  309. {
  310. return n != 0 && (n & (n - 1)) == 0;
  311. }
  312. // Compute next-higher power of 2 efficiently, e.g. for power-of-2 texture sizes.
  313. // Public Domain: https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
  314. inline u32 npot2(u32 orig) {
  315. orig--;
  316. orig |= orig >> 1;
  317. orig |= orig >> 2;
  318. orig |= orig >> 4;
  319. orig |= orig >> 8;
  320. orig |= orig >> 16;
  321. return orig + 1;
  322. }