math.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /*************************************************************************/
  2. /* math.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef GODOT_MATH_HPP
  31. #define GODOT_MATH_HPP
  32. #include <godot_cpp/core/defs.hpp>
  33. #include <godot/gdnative_interface.h>
  34. #include <cmath>
  35. namespace godot {
  36. namespace Math {
  37. // This epsilon should match the one used by Godot for consistency.
  38. // Using `f` when `real_t` is float.
  39. #define CMP_EPSILON 0.00001f
  40. #define CMP_EPSILON2 (CMP_EPSILON * CMP_EPSILON)
  41. // This epsilon is for values related to a unit size (scalar or vector len).
  42. #ifdef PRECISE_MATH_CHECKS
  43. #define UNIT_EPSILON 0.00001
  44. #else
  45. // Tolerate some more floating point error normally.
  46. #define UNIT_EPSILON 0.001
  47. #endif
  48. #define Math_SQRT12 0.7071067811865475244008443621048490
  49. #define Math_SQRT2 1.4142135623730950488016887242
  50. #define Math_LN2 0.6931471805599453094172321215
  51. #define Math_PI 3.1415926535897932384626433833
  52. #define Math_TAU 6.2831853071795864769252867666
  53. #define Math_E 2.7182818284590452353602874714
  54. #define Math_INF INFINITY
  55. #define Math_NAN NAN
  56. // Windows badly defines a lot of stuff we'll never use. Undefine it.
  57. #ifdef _WIN32
  58. #undef MIN // override standard definition
  59. #undef MAX // override standard definition
  60. #undef CLAMP // override standard definition
  61. #endif
  62. // Generic ABS function, for math uses please use Math::abs.
  63. #ifndef ABS
  64. #define ABS(m_v) (((m_v) < 0) ? (-(m_v)) : (m_v))
  65. #endif
  66. #ifndef SIGN
  67. #define SIGN(m_v) (((m_v) == 0) ? (0.0) : (((m_v) < 0) ? (-1.0) : (+1.0)))
  68. #endif
  69. #ifndef MIN
  70. #define MIN(m_a, m_b) (((m_a) < (m_b)) ? (m_a) : (m_b))
  71. #endif
  72. #ifndef MAX
  73. #define MAX(m_a, m_b) (((m_a) > (m_b)) ? (m_a) : (m_b))
  74. #endif
  75. #ifndef CLAMP
  76. #define CLAMP(m_a, m_min, m_max) (((m_a) < (m_min)) ? (m_min) : (((m_a) > (m_max)) ? m_max : m_a))
  77. #endif
  78. // Functions reproduced as in Godot's source code `math_funcs.h`.
  79. // Some are overloads to automatically support changing real_t into either double or float in the way Godot does.
  80. inline double fmod(double p_x, double p_y) {
  81. return ::fmod(p_x, p_y);
  82. }
  83. inline float fmod(float p_x, float p_y) {
  84. return ::fmodf(p_x, p_y);
  85. }
  86. inline double fposmod(double p_x, double p_y) {
  87. double value = Math::fmod(p_x, p_y);
  88. if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
  89. value += p_y;
  90. }
  91. value += 0.0;
  92. return value;
  93. }
  94. inline float fposmod(float p_x, float p_y) {
  95. float value = Math::fmod(p_x, p_y);
  96. if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
  97. value += p_y;
  98. }
  99. value += 0.0;
  100. return value;
  101. }
  102. inline float fposmodp(float p_x, float p_y) {
  103. float value = Math::fmod(p_x, p_y);
  104. if (value < 0) {
  105. value += p_y;
  106. }
  107. value += 0.0;
  108. return value;
  109. }
  110. inline double fposmodp(double p_x, double p_y) {
  111. double value = Math::fmod(p_x, p_y);
  112. if (value < 0) {
  113. value += p_y;
  114. }
  115. value += 0.0;
  116. return value;
  117. }
  118. inline double floor(double p_x) {
  119. return ::floor(p_x);
  120. }
  121. inline float floor(float p_x) {
  122. return ::floorf(p_x);
  123. }
  124. inline double ceil(double p_x) {
  125. return ::ceil(p_x);
  126. }
  127. inline float ceil(float p_x) {
  128. return ::ceilf(p_x);
  129. }
  130. inline double exp(double p_x) {
  131. return ::exp(p_x);
  132. }
  133. inline float exp(float p_x) {
  134. return ::expf(p_x);
  135. }
  136. inline double sin(double p_x) {
  137. return ::sin(p_x);
  138. }
  139. inline float sin(float p_x) {
  140. return ::sinf(p_x);
  141. }
  142. inline double cos(double p_x) {
  143. return ::cos(p_x);
  144. }
  145. inline float cos(float p_x) {
  146. return ::cosf(p_x);
  147. }
  148. inline double tan(double p_x) {
  149. return ::tan(p_x);
  150. }
  151. inline float tan(float p_x) {
  152. return ::tanf(p_x);
  153. }
  154. inline double sinh(double p_x) {
  155. return ::sinh(p_x);
  156. }
  157. inline float sinh(float p_x) {
  158. return ::sinhf(p_x);
  159. }
  160. inline float sinc(float p_x) {
  161. return p_x == 0 ? 1 : ::sin(p_x) / p_x;
  162. }
  163. inline double sinc(double p_x) {
  164. return p_x == 0 ? 1 : ::sin(p_x) / p_x;
  165. }
  166. inline float sincn(float p_x) {
  167. return (float)sinc(Math_PI * p_x);
  168. }
  169. inline double sincn(double p_x) {
  170. return sinc(Math_PI * p_x);
  171. }
  172. inline double cosh(double p_x) {
  173. return ::cosh(p_x);
  174. }
  175. inline float cosh(float p_x) {
  176. return ::coshf(p_x);
  177. }
  178. inline double tanh(double p_x) {
  179. return ::tanh(p_x);
  180. }
  181. inline float tanh(float p_x) {
  182. return ::tanhf(p_x);
  183. }
  184. inline double asin(double p_x) {
  185. return ::asin(p_x);
  186. }
  187. inline float asin(float p_x) {
  188. return ::asinf(p_x);
  189. }
  190. inline double acos(double p_x) {
  191. return ::acos(p_x);
  192. }
  193. inline float acos(float p_x) {
  194. return ::acosf(p_x);
  195. }
  196. inline double atan(double p_x) {
  197. return ::atan(p_x);
  198. }
  199. inline float atan(float p_x) {
  200. return ::atanf(p_x);
  201. }
  202. inline double atan2(double p_y, double p_x) {
  203. return ::atan2(p_y, p_x);
  204. }
  205. inline float atan2(float p_y, float p_x) {
  206. return ::atan2f(p_y, p_x);
  207. }
  208. inline double sqrt(double p_x) {
  209. return ::sqrt(p_x);
  210. }
  211. inline float sqrt(float p_x) {
  212. return ::sqrtf(p_x);
  213. }
  214. inline double pow(double p_x, double p_y) {
  215. return ::pow(p_x, p_y);
  216. }
  217. inline float pow(float p_x, float p_y) {
  218. return ::powf(p_x, p_y);
  219. }
  220. inline double log(double p_x) {
  221. return ::log(p_x);
  222. }
  223. inline float log(float p_x) {
  224. return ::logf(p_x);
  225. }
  226. inline float lerp(float minv, float maxv, float t) {
  227. return minv + t * (maxv - minv);
  228. }
  229. inline double lerp(double minv, double maxv, double t) {
  230. return minv + t * (maxv - minv);
  231. }
  232. inline double lerp_angle(double p_from, double p_to, double p_weight) {
  233. double difference = fmod(p_to - p_from, Math_TAU);
  234. double distance = fmod(2.0 * difference, Math_TAU) - difference;
  235. return p_from + distance * p_weight;
  236. }
  237. inline float lerp_angle(float p_from, float p_to, float p_weight) {
  238. float difference = fmod(p_to - p_from, (float)Math_TAU);
  239. float distance = fmod(2.0f * difference, (float)Math_TAU) - difference;
  240. return p_from + distance * p_weight;
  241. }
  242. inline double cubic_interpolate(double p_from, double p_to, double p_pre, double p_post, double p_weight) {
  243. return 0.5 *
  244. ((p_from * 2.0) +
  245. (-p_pre + p_to) * p_weight +
  246. (2.0 * p_pre - 5.0 * p_from + 4.0 * p_to - p_post) * (p_weight * p_weight) +
  247. (-p_pre + 3.0 * p_from - 3.0 * p_to + p_post) * (p_weight * p_weight * p_weight));
  248. }
  249. inline float cubic_interpolate(float p_from, float p_to, float p_pre, float p_post, float p_weight) {
  250. return 0.5f *
  251. ((p_from * 2.0f) +
  252. (-p_pre + p_to) * p_weight +
  253. (2.0f * p_pre - 5.0f * p_from + 4.0f * p_to - p_post) * (p_weight * p_weight) +
  254. (-p_pre + 3.0f * p_from - 3.0f * p_to + p_post) * (p_weight * p_weight * p_weight));
  255. }
  256. inline double cubic_interpolate_angle(double p_from, double p_to, double p_pre, double p_post, double p_weight) {
  257. double from_rot = fmod(p_from, Math_TAU);
  258. double pre_diff = fmod(p_pre - from_rot, Math_TAU);
  259. double pre_rot = from_rot + fmod(2.0 * pre_diff, Math_TAU) - pre_diff;
  260. double to_diff = fmod(p_to - from_rot, Math_TAU);
  261. double to_rot = from_rot + fmod(2.0 * to_diff, Math_TAU) - to_diff;
  262. double post_diff = fmod(p_post - to_rot, Math_TAU);
  263. double post_rot = to_rot + fmod(2.0 * post_diff, Math_TAU) - post_diff;
  264. return cubic_interpolate(from_rot, to_rot, pre_rot, post_rot, p_weight);
  265. }
  266. inline float cubic_interpolate_angle(float p_from, float p_to, float p_pre, float p_post, float p_weight) {
  267. float from_rot = fmod(p_from, (float)Math_TAU);
  268. float pre_diff = fmod(p_pre - from_rot, (float)Math_TAU);
  269. float pre_rot = from_rot + fmod(2.0f * pre_diff, (float)Math_TAU) - pre_diff;
  270. float to_diff = fmod(p_to - from_rot, (float)Math_TAU);
  271. float to_rot = from_rot + fmod(2.0f * to_diff, (float)Math_TAU) - to_diff;
  272. float post_diff = fmod(p_post - to_rot, (float)Math_TAU);
  273. float post_rot = to_rot + fmod(2.0f * post_diff, (float)Math_TAU) - post_diff;
  274. return cubic_interpolate(from_rot, to_rot, pre_rot, post_rot, p_weight);
  275. }
  276. inline double cubic_interpolate_in_time(double p_from, double p_to, double p_pre, double p_post, double p_weight,
  277. double p_to_t, double p_pre_t, double p_post_t) {
  278. /* Barry-Goldman method */
  279. double t = Math::lerp(0.0, p_to_t, p_weight);
  280. double a1 = Math::lerp(p_pre, p_from, p_pre_t == 0 ? 0.0 : (t - p_pre_t) / -p_pre_t);
  281. double a2 = Math::lerp(p_from, p_to, p_to_t == 0 ? 0.5 : t / p_to_t);
  282. double a3 = Math::lerp(p_to, p_post, p_post_t - p_to_t == 0 ? 1.0 : (t - p_to_t) / (p_post_t - p_to_t));
  283. double b1 = Math::lerp(a1, a2, p_to_t - p_pre_t == 0 ? 0.0 : (t - p_pre_t) / (p_to_t - p_pre_t));
  284. double b2 = Math::lerp(a2, a3, p_post_t == 0 ? 1.0 : t / p_post_t);
  285. return Math::lerp(b1, b2, p_to_t == 0 ? 0.5 : t / p_to_t);
  286. }
  287. inline float cubic_interpolate_in_time(float p_from, float p_to, float p_pre, float p_post, float p_weight,
  288. float p_to_t, float p_pre_t, float p_post_t) {
  289. /* Barry-Goldman method */
  290. float t = Math::lerp(0.0f, p_to_t, p_weight);
  291. float a1 = Math::lerp(p_pre, p_from, p_pre_t == 0 ? 0.0f : (t - p_pre_t) / -p_pre_t);
  292. float a2 = Math::lerp(p_from, p_to, p_to_t == 0 ? 0.5f : t / p_to_t);
  293. float a3 = Math::lerp(p_to, p_post, p_post_t - p_to_t == 0 ? 1.0f : (t - p_to_t) / (p_post_t - p_to_t));
  294. float b1 = Math::lerp(a1, a2, p_to_t - p_pre_t == 0 ? 0.0f : (t - p_pre_t) / (p_to_t - p_pre_t));
  295. float b2 = Math::lerp(a2, a3, p_post_t == 0 ? 1.0f : t / p_post_t);
  296. return Math::lerp(b1, b2, p_to_t == 0 ? 0.5f : t / p_to_t);
  297. }
  298. inline double cubic_interpolate_angle_in_time(double p_from, double p_to, double p_pre, double p_post, double p_weight,
  299. double p_to_t, double p_pre_t, double p_post_t) {
  300. double from_rot = fmod(p_from, Math_TAU);
  301. double pre_diff = fmod(p_pre - from_rot, Math_TAU);
  302. double pre_rot = from_rot + fmod(2.0 * pre_diff, Math_TAU) - pre_diff;
  303. double to_diff = fmod(p_to - from_rot, Math_TAU);
  304. double to_rot = from_rot + fmod(2.0 * to_diff, Math_TAU) - to_diff;
  305. double post_diff = fmod(p_post - to_rot, Math_TAU);
  306. double post_rot = to_rot + fmod(2.0 * post_diff, Math_TAU) - post_diff;
  307. return cubic_interpolate_in_time(from_rot, to_rot, pre_rot, post_rot, p_weight, p_to_t, p_pre_t, p_post_t);
  308. }
  309. inline float cubic_interpolate_angle_in_time(float p_from, float p_to, float p_pre, float p_post, float p_weight,
  310. float p_to_t, float p_pre_t, float p_post_t) {
  311. float from_rot = fmod(p_from, (float)Math_TAU);
  312. float pre_diff = fmod(p_pre - from_rot, (float)Math_TAU);
  313. float pre_rot = from_rot + fmod(2.0f * pre_diff, (float)Math_TAU) - pre_diff;
  314. float to_diff = fmod(p_to - from_rot, (float)Math_TAU);
  315. float to_rot = from_rot + fmod(2.0f * to_diff, (float)Math_TAU) - to_diff;
  316. float post_diff = fmod(p_post - to_rot, (float)Math_TAU);
  317. float post_rot = to_rot + fmod(2.0f * post_diff, (float)Math_TAU) - post_diff;
  318. return cubic_interpolate_in_time(from_rot, to_rot, pre_rot, post_rot, p_weight, p_to_t, p_pre_t, p_post_t);
  319. }
  320. inline double bezier_interpolate(double p_start, double p_control_1, double p_control_2, double p_end, double p_t) {
  321. /* Formula from Wikipedia article on Bezier curves. */
  322. double omt = (1.0 - p_t);
  323. double omt2 = omt * omt;
  324. double omt3 = omt2 * omt;
  325. double t2 = p_t * p_t;
  326. double t3 = t2 * p_t;
  327. return p_start * omt3 + p_control_1 * omt2 * p_t * 3.0 + p_control_2 * omt * t2 * 3.0 + p_end * t3;
  328. }
  329. inline float bezier_interpolate(float p_start, float p_control_1, float p_control_2, float p_end, float p_t) {
  330. /* Formula from Wikipedia article on Bezier curves. */
  331. float omt = (1.0f - p_t);
  332. float omt2 = omt * omt;
  333. float omt3 = omt2 * omt;
  334. float t2 = p_t * p_t;
  335. float t3 = t2 * p_t;
  336. return p_start * omt3 + p_control_1 * omt2 * p_t * 3.0f + p_control_2 * omt * t2 * 3.0f + p_end * t3;
  337. }
  338. template <typename T>
  339. inline T clamp(T x, T minv, T maxv) {
  340. if (x < minv) {
  341. return minv;
  342. }
  343. if (x > maxv) {
  344. return maxv;
  345. }
  346. return x;
  347. }
  348. template <typename T>
  349. inline T min(T a, T b) {
  350. return a < b ? a : b;
  351. }
  352. template <typename T>
  353. inline T max(T a, T b) {
  354. return a > b ? a : b;
  355. }
  356. template <typename T>
  357. inline T sign(T x) {
  358. return static_cast<T>(x < 0 ? -1 : 1);
  359. }
  360. template <typename T>
  361. inline T abs(T x) {
  362. return std::abs(x);
  363. }
  364. inline double deg2rad(double p_y) {
  365. return p_y * Math_PI / 180.0;
  366. }
  367. inline float deg2rad(float p_y) {
  368. return p_y * static_cast<float>(Math_PI) / 180.f;
  369. }
  370. inline double rad2deg(double p_y) {
  371. return p_y * 180.0 / Math_PI;
  372. }
  373. inline float rad2deg(float p_y) {
  374. return p_y * 180.f / static_cast<float>(Math_PI);
  375. }
  376. inline double inverse_lerp(double p_from, double p_to, double p_value) {
  377. return (p_value - p_from) / (p_to - p_from);
  378. }
  379. inline float inverse_lerp(float p_from, float p_to, float p_value) {
  380. return (p_value - p_from) / (p_to - p_from);
  381. }
  382. inline double range_lerp(double p_value, double p_istart, double p_istop, double p_ostart, double p_ostop) {
  383. return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value));
  384. }
  385. inline float range_lerp(float p_value, float p_istart, float p_istop, float p_ostart, float p_ostop) {
  386. return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value));
  387. }
  388. inline bool is_nan(float p_val) {
  389. return std::isnan(p_val);
  390. }
  391. inline bool is_nan(double p_val) {
  392. return std::isnan(p_val);
  393. }
  394. inline bool is_inf(float p_val) {
  395. return std::isinf(p_val);
  396. }
  397. inline bool is_inf(double p_val) {
  398. return std::isinf(p_val);
  399. }
  400. inline bool is_equal_approx(float a, float b) {
  401. // Check for exact equality first, required to handle "infinity" values.
  402. if (a == b) {
  403. return true;
  404. }
  405. // Then check for approximate equality.
  406. float tolerance = (float)CMP_EPSILON * abs(a);
  407. if (tolerance < (float)CMP_EPSILON) {
  408. tolerance = (float)CMP_EPSILON;
  409. }
  410. return abs(a - b) < tolerance;
  411. }
  412. inline bool is_equal_approx(float a, float b, float tolerance) {
  413. // Check for exact equality first, required to handle "infinity" values.
  414. if (a == b) {
  415. return true;
  416. }
  417. // Then check for approximate equality.
  418. return abs(a - b) < tolerance;
  419. }
  420. inline bool is_zero_approx(float s) {
  421. return abs(s) < (float)CMP_EPSILON;
  422. }
  423. inline bool is_equal_approx(double a, double b) {
  424. // Check for exact equality first, required to handle "infinity" values.
  425. if (a == b) {
  426. return true;
  427. }
  428. // Then check for approximate equality.
  429. double tolerance = CMP_EPSILON * abs(a);
  430. if (tolerance < CMP_EPSILON) {
  431. tolerance = CMP_EPSILON;
  432. }
  433. return abs(a - b) < tolerance;
  434. }
  435. inline bool is_equal_approx(double a, double b, double tolerance) {
  436. // Check for exact equality first, required to handle "infinity" values.
  437. if (a == b) {
  438. return true;
  439. }
  440. // Then check for approximate equality.
  441. return abs(a - b) < tolerance;
  442. }
  443. inline bool is_zero_approx(double s) {
  444. return abs(s) < CMP_EPSILON;
  445. }
  446. inline double smoothstep(double p_from, double p_to, double p_weight) {
  447. if (is_equal_approx(static_cast<real_t>(p_from), static_cast<real_t>(p_to))) {
  448. return p_from;
  449. }
  450. double x = clamp((p_weight - p_from) / (p_to - p_from), 0.0, 1.0);
  451. return x * x * (3.0 - 2.0 * x);
  452. }
  453. inline float smoothstep(float p_from, float p_to, float p_weight) {
  454. if (is_equal_approx(p_from, p_to)) {
  455. return p_from;
  456. }
  457. float x = clamp((p_weight - p_from) / (p_to - p_from), 0.0f, 1.0f);
  458. return x * x * (3.0f - 2.0f * x);
  459. }
  460. inline double move_toward(double p_from, double p_to, double p_delta) {
  461. return std::abs(p_to - p_from) <= p_delta ? p_to : p_from + sign(p_to - p_from) * p_delta;
  462. }
  463. inline float move_toward(float p_from, float p_to, float p_delta) {
  464. return std::abs(p_to - p_from) <= p_delta ? p_to : p_from + sign(p_to - p_from) * p_delta;
  465. }
  466. inline double linear2db(double p_linear) {
  467. return log(p_linear) * 8.6858896380650365530225783783321;
  468. }
  469. inline float linear2db(float p_linear) {
  470. return log(p_linear) * 8.6858896380650365530225783783321f;
  471. }
  472. inline double db2linear(double p_db) {
  473. return exp(p_db * 0.11512925464970228420089957273422);
  474. }
  475. inline float db2linear(float p_db) {
  476. return exp(p_db * 0.11512925464970228420089957273422f);
  477. }
  478. inline double round(double p_val) {
  479. return (p_val >= 0) ? floor(p_val + 0.5) : -floor(-p_val + 0.5);
  480. }
  481. inline float round(float p_val) {
  482. return (p_val >= 0) ? floor(p_val + 0.5f) : -floor(-p_val + 0.5f);
  483. }
  484. inline int64_t wrapi(int64_t value, int64_t min, int64_t max) {
  485. int64_t range = max - min;
  486. return range == 0 ? min : min + ((((value - min) % range) + range) % range);
  487. }
  488. inline float wrapf(real_t value, real_t min, real_t max) {
  489. const real_t range = max - min;
  490. return is_zero_approx(range) ? min : value - (range * floor((value - min) / range));
  491. }
  492. inline float fract(float value) {
  493. return value - floor(value);
  494. }
  495. inline double fract(double value) {
  496. return value - floor(value);
  497. }
  498. inline float pingpong(float value, float length) {
  499. return (length != 0.0f) ? abs(fract((value - length) / (length * 2.0f)) * length * 2.0f - length) : 0.0f;
  500. }
  501. inline double pingpong(double value, double length) {
  502. return (length != 0.0) ? abs(fract((value - length) / (length * 2.0)) * length * 2.0 - length) : 0.0;
  503. }
  504. inline unsigned int next_power_of_2(unsigned int x) {
  505. if (x == 0)
  506. return 0;
  507. --x;
  508. x |= x >> 1;
  509. x |= x >> 2;
  510. x |= x >> 4;
  511. x |= x >> 8;
  512. x |= x >> 16;
  513. return ++x;
  514. }
  515. // This function should be as fast as possible and rounding mode should not matter.
  516. inline int fast_ftoi(float a) {
  517. static int b;
  518. #if (defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0603) || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP // windows 8 phone?
  519. b = (int)((a > 0.0) ? (a + 0.5) : (a - 0.5));
  520. #elif defined(_MSC_VER) && _MSC_VER < 1800
  521. __asm fld a __asm fistp b
  522. /*#elif defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) )
  523. // use AT&T inline assembly style, document that
  524. // we use memory as output (=m) and input (m)
  525. __asm__ __volatile__ (
  526. "flds %1 \n\t"
  527. "fistpl %0 \n\t"
  528. : "=m" (b)
  529. : "m" (a));*/
  530. #else
  531. b = lrintf(a); // assuming everything but msvc 2012 or earlier has lrint
  532. #endif
  533. return b;
  534. }
  535. inline double snapped(double p_value, double p_step) {
  536. if (p_step != 0) {
  537. p_value = Math::floor(p_value / p_step + 0.5) * p_step;
  538. }
  539. return p_value;
  540. }
  541. inline float snap_scalar(float p_offset, float p_step, float p_target) {
  542. return p_step != 0 ? Math::snapped(p_target - p_offset, p_step) + p_offset : p_target;
  543. }
  544. inline float snap_scalar_separation(float p_offset, float p_step, float p_target, float p_separation) {
  545. if (p_step != 0) {
  546. float a = Math::snapped(p_target - p_offset, p_step + p_separation) + p_offset;
  547. float b = a;
  548. if (p_target >= 0) {
  549. b -= p_separation;
  550. } else {
  551. b += p_step;
  552. }
  553. return (Math::abs(p_target - a) < Math::abs(p_target - b)) ? a : b;
  554. }
  555. return p_target;
  556. }
  557. } // namespace Math
  558. } // namespace godot
  559. #endif // GODOT_MATH_HPP