2
0

math.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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.0f;
  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.0f;
  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 int64_t posmod(int64_t p_x, int64_t p_y) {
  119. int64_t value = p_x % p_y;
  120. if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
  121. value += p_y;
  122. }
  123. return value;
  124. }
  125. inline double floor(double p_x) {
  126. return ::floor(p_x);
  127. }
  128. inline float floor(float p_x) {
  129. return ::floorf(p_x);
  130. }
  131. inline double ceil(double p_x) {
  132. return ::ceil(p_x);
  133. }
  134. inline float ceil(float p_x) {
  135. return ::ceilf(p_x);
  136. }
  137. inline double exp(double p_x) {
  138. return ::exp(p_x);
  139. }
  140. inline float exp(float p_x) {
  141. return ::expf(p_x);
  142. }
  143. inline double sin(double p_x) {
  144. return ::sin(p_x);
  145. }
  146. inline float sin(float p_x) {
  147. return ::sinf(p_x);
  148. }
  149. inline double cos(double p_x) {
  150. return ::cos(p_x);
  151. }
  152. inline float cos(float p_x) {
  153. return ::cosf(p_x);
  154. }
  155. inline double tan(double p_x) {
  156. return ::tan(p_x);
  157. }
  158. inline float tan(float p_x) {
  159. return ::tanf(p_x);
  160. }
  161. inline double sinh(double p_x) {
  162. return ::sinh(p_x);
  163. }
  164. inline float sinh(float p_x) {
  165. return ::sinhf(p_x);
  166. }
  167. inline float sinc(float p_x) {
  168. return p_x == 0 ? 1 : ::sin(p_x) / p_x;
  169. }
  170. inline double sinc(double p_x) {
  171. return p_x == 0 ? 1 : ::sin(p_x) / p_x;
  172. }
  173. inline float sincn(float p_x) {
  174. return (float)sinc(Math_PI * p_x);
  175. }
  176. inline double sincn(double p_x) {
  177. return sinc(Math_PI * p_x);
  178. }
  179. inline double cosh(double p_x) {
  180. return ::cosh(p_x);
  181. }
  182. inline float cosh(float p_x) {
  183. return ::coshf(p_x);
  184. }
  185. inline double tanh(double p_x) {
  186. return ::tanh(p_x);
  187. }
  188. inline float tanh(float p_x) {
  189. return ::tanhf(p_x);
  190. }
  191. inline double asin(double p_x) {
  192. return ::asin(p_x);
  193. }
  194. inline float asin(float p_x) {
  195. return ::asinf(p_x);
  196. }
  197. inline double acos(double p_x) {
  198. return ::acos(p_x);
  199. }
  200. inline float acos(float p_x) {
  201. return ::acosf(p_x);
  202. }
  203. inline double atan(double p_x) {
  204. return ::atan(p_x);
  205. }
  206. inline float atan(float p_x) {
  207. return ::atanf(p_x);
  208. }
  209. inline double atan2(double p_y, double p_x) {
  210. return ::atan2(p_y, p_x);
  211. }
  212. inline float atan2(float p_y, float p_x) {
  213. return ::atan2f(p_y, p_x);
  214. }
  215. inline double sqrt(double p_x) {
  216. return ::sqrt(p_x);
  217. }
  218. inline float sqrt(float p_x) {
  219. return ::sqrtf(p_x);
  220. }
  221. inline double pow(double p_x, double p_y) {
  222. return ::pow(p_x, p_y);
  223. }
  224. inline float pow(float p_x, float p_y) {
  225. return ::powf(p_x, p_y);
  226. }
  227. inline double log(double p_x) {
  228. return ::log(p_x);
  229. }
  230. inline float log(float p_x) {
  231. return ::logf(p_x);
  232. }
  233. inline float lerp(float minv, float maxv, float t) {
  234. return minv + t * (maxv - minv);
  235. }
  236. inline double lerp(double minv, double maxv, double t) {
  237. return minv + t * (maxv - minv);
  238. }
  239. inline double lerp_angle(double p_from, double p_to, double p_weight) {
  240. double difference = fmod(p_to - p_from, Math_TAU);
  241. double distance = fmod(2.0 * difference, Math_TAU) - difference;
  242. return p_from + distance * p_weight;
  243. }
  244. inline float lerp_angle(float p_from, float p_to, float p_weight) {
  245. float difference = fmod(p_to - p_from, (float)Math_TAU);
  246. float distance = fmod(2.0f * difference, (float)Math_TAU) - difference;
  247. return p_from + distance * p_weight;
  248. }
  249. inline double cubic_interpolate(double p_from, double p_to, double p_pre, double p_post, double p_weight) {
  250. return 0.5 *
  251. ((p_from * 2.0) +
  252. (-p_pre + p_to) * p_weight +
  253. (2.0 * p_pre - 5.0 * p_from + 4.0 * p_to - p_post) * (p_weight * p_weight) +
  254. (-p_pre + 3.0 * p_from - 3.0 * p_to + p_post) * (p_weight * p_weight * p_weight));
  255. }
  256. inline float cubic_interpolate(float p_from, float p_to, float p_pre, float p_post, float p_weight) {
  257. return 0.5f *
  258. ((p_from * 2.0f) +
  259. (-p_pre + p_to) * p_weight +
  260. (2.0f * p_pre - 5.0f * p_from + 4.0f * p_to - p_post) * (p_weight * p_weight) +
  261. (-p_pre + 3.0f * p_from - 3.0f * p_to + p_post) * (p_weight * p_weight * p_weight));
  262. }
  263. inline double cubic_interpolate_angle(double p_from, double p_to, double p_pre, double p_post, double p_weight) {
  264. double from_rot = fmod(p_from, Math_TAU);
  265. double pre_diff = fmod(p_pre - from_rot, Math_TAU);
  266. double pre_rot = from_rot + fmod(2.0 * pre_diff, Math_TAU) - pre_diff;
  267. double to_diff = fmod(p_to - from_rot, Math_TAU);
  268. double to_rot = from_rot + fmod(2.0 * to_diff, Math_TAU) - to_diff;
  269. double post_diff = fmod(p_post - to_rot, Math_TAU);
  270. double post_rot = to_rot + fmod(2.0 * post_diff, Math_TAU) - post_diff;
  271. return cubic_interpolate(from_rot, to_rot, pre_rot, post_rot, p_weight);
  272. }
  273. inline float cubic_interpolate_angle(float p_from, float p_to, float p_pre, float p_post, float p_weight) {
  274. float from_rot = fmod(p_from, (float)Math_TAU);
  275. float pre_diff = fmod(p_pre - from_rot, (float)Math_TAU);
  276. float pre_rot = from_rot + fmod(2.0f * pre_diff, (float)Math_TAU) - pre_diff;
  277. float to_diff = fmod(p_to - from_rot, (float)Math_TAU);
  278. float to_rot = from_rot + fmod(2.0f * to_diff, (float)Math_TAU) - to_diff;
  279. float post_diff = fmod(p_post - to_rot, (float)Math_TAU);
  280. float post_rot = to_rot + fmod(2.0f * post_diff, (float)Math_TAU) - post_diff;
  281. return cubic_interpolate(from_rot, to_rot, pre_rot, post_rot, p_weight);
  282. }
  283. inline double cubic_interpolate_in_time(double p_from, double p_to, double p_pre, double p_post, double p_weight,
  284. double p_to_t, double p_pre_t, double p_post_t) {
  285. /* Barry-Goldman method */
  286. double t = Math::lerp(0.0, p_to_t, p_weight);
  287. double a1 = Math::lerp(p_pre, p_from, p_pre_t == 0 ? 0.0 : (t - p_pre_t) / -p_pre_t);
  288. double a2 = Math::lerp(p_from, p_to, p_to_t == 0 ? 0.5 : t / p_to_t);
  289. 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));
  290. 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));
  291. double b2 = Math::lerp(a2, a3, p_post_t == 0 ? 1.0 : t / p_post_t);
  292. return Math::lerp(b1, b2, p_to_t == 0 ? 0.5 : t / p_to_t);
  293. }
  294. inline float cubic_interpolate_in_time(float p_from, float p_to, float p_pre, float p_post, float p_weight,
  295. float p_to_t, float p_pre_t, float p_post_t) {
  296. /* Barry-Goldman method */
  297. float t = Math::lerp(0.0f, p_to_t, p_weight);
  298. float a1 = Math::lerp(p_pre, p_from, p_pre_t == 0 ? 0.0f : (t - p_pre_t) / -p_pre_t);
  299. float a2 = Math::lerp(p_from, p_to, p_to_t == 0 ? 0.5f : t / p_to_t);
  300. 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));
  301. 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));
  302. float b2 = Math::lerp(a2, a3, p_post_t == 0 ? 1.0f : t / p_post_t);
  303. return Math::lerp(b1, b2, p_to_t == 0 ? 0.5f : t / p_to_t);
  304. }
  305. inline double cubic_interpolate_angle_in_time(double p_from, double p_to, double p_pre, double p_post, double p_weight,
  306. double p_to_t, double p_pre_t, double p_post_t) {
  307. double from_rot = fmod(p_from, Math_TAU);
  308. double pre_diff = fmod(p_pre - from_rot, Math_TAU);
  309. double pre_rot = from_rot + fmod(2.0 * pre_diff, Math_TAU) - pre_diff;
  310. double to_diff = fmod(p_to - from_rot, Math_TAU);
  311. double to_rot = from_rot + fmod(2.0 * to_diff, Math_TAU) - to_diff;
  312. double post_diff = fmod(p_post - to_rot, Math_TAU);
  313. double post_rot = to_rot + fmod(2.0 * post_diff, Math_TAU) - post_diff;
  314. return cubic_interpolate_in_time(from_rot, to_rot, pre_rot, post_rot, p_weight, p_to_t, p_pre_t, p_post_t);
  315. }
  316. inline float cubic_interpolate_angle_in_time(float p_from, float p_to, float p_pre, float p_post, float p_weight,
  317. float p_to_t, float p_pre_t, float p_post_t) {
  318. float from_rot = fmod(p_from, (float)Math_TAU);
  319. float pre_diff = fmod(p_pre - from_rot, (float)Math_TAU);
  320. float pre_rot = from_rot + fmod(2.0f * pre_diff, (float)Math_TAU) - pre_diff;
  321. float to_diff = fmod(p_to - from_rot, (float)Math_TAU);
  322. float to_rot = from_rot + fmod(2.0f * to_diff, (float)Math_TAU) - to_diff;
  323. float post_diff = fmod(p_post - to_rot, (float)Math_TAU);
  324. float post_rot = to_rot + fmod(2.0f * post_diff, (float)Math_TAU) - post_diff;
  325. return cubic_interpolate_in_time(from_rot, to_rot, pre_rot, post_rot, p_weight, p_to_t, p_pre_t, p_post_t);
  326. }
  327. inline double bezier_interpolate(double p_start, double p_control_1, double p_control_2, double p_end, double p_t) {
  328. /* Formula from Wikipedia article on Bezier curves. */
  329. double omt = (1.0 - p_t);
  330. double omt2 = omt * omt;
  331. double omt3 = omt2 * omt;
  332. double t2 = p_t * p_t;
  333. double t3 = t2 * p_t;
  334. return p_start * omt3 + p_control_1 * omt2 * p_t * 3.0 + p_control_2 * omt * t2 * 3.0 + p_end * t3;
  335. }
  336. inline float bezier_interpolate(float p_start, float p_control_1, float p_control_2, float p_end, float p_t) {
  337. /* Formula from Wikipedia article on Bezier curves. */
  338. float omt = (1.0f - p_t);
  339. float omt2 = omt * omt;
  340. float omt3 = omt2 * omt;
  341. float t2 = p_t * p_t;
  342. float t3 = t2 * p_t;
  343. return p_start * omt3 + p_control_1 * omt2 * p_t * 3.0f + p_control_2 * omt * t2 * 3.0f + p_end * t3;
  344. }
  345. template <typename T>
  346. inline T clamp(T x, T minv, T maxv) {
  347. if (x < minv) {
  348. return minv;
  349. }
  350. if (x > maxv) {
  351. return maxv;
  352. }
  353. return x;
  354. }
  355. template <typename T>
  356. inline T min(T a, T b) {
  357. return a < b ? a : b;
  358. }
  359. template <typename T>
  360. inline T max(T a, T b) {
  361. return a > b ? a : b;
  362. }
  363. template <typename T>
  364. inline T sign(T x) {
  365. return static_cast<T>(x < 0 ? -1 : 1);
  366. }
  367. template <typename T>
  368. inline T abs(T x) {
  369. return std::abs(x);
  370. }
  371. inline double deg_to_rad(double p_y) {
  372. return p_y * Math_PI / 180.0;
  373. }
  374. inline float deg_to_rad(float p_y) {
  375. return p_y * static_cast<float>(Math_PI) / 180.f;
  376. }
  377. inline double rad_to_deg(double p_y) {
  378. return p_y * 180.0 / Math_PI;
  379. }
  380. inline float rad_to_deg(float p_y) {
  381. return p_y * 180.f / static_cast<float>(Math_PI);
  382. }
  383. inline double inverse_lerp(double p_from, double p_to, double p_value) {
  384. return (p_value - p_from) / (p_to - p_from);
  385. }
  386. inline float inverse_lerp(float p_from, float p_to, float p_value) {
  387. return (p_value - p_from) / (p_to - p_from);
  388. }
  389. inline double remap(double p_value, double p_istart, double p_istop, double p_ostart, double p_ostop) {
  390. return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value));
  391. }
  392. inline float remap(float p_value, float p_istart, float p_istop, float p_ostart, float p_ostop) {
  393. return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value));
  394. }
  395. inline bool is_nan(float p_val) {
  396. return std::isnan(p_val);
  397. }
  398. inline bool is_nan(double p_val) {
  399. return std::isnan(p_val);
  400. }
  401. inline bool is_inf(float p_val) {
  402. return std::isinf(p_val);
  403. }
  404. inline bool is_inf(double p_val) {
  405. return std::isinf(p_val);
  406. }
  407. inline bool is_equal_approx(float a, float b) {
  408. // Check for exact equality first, required to handle "infinity" values.
  409. if (a == b) {
  410. return true;
  411. }
  412. // Then check for approximate equality.
  413. float tolerance = (float)CMP_EPSILON * abs(a);
  414. if (tolerance < (float)CMP_EPSILON) {
  415. tolerance = (float)CMP_EPSILON;
  416. }
  417. return abs(a - b) < tolerance;
  418. }
  419. inline bool is_equal_approx(float a, float b, float tolerance) {
  420. // Check for exact equality first, required to handle "infinity" values.
  421. if (a == b) {
  422. return true;
  423. }
  424. // Then check for approximate equality.
  425. return abs(a - b) < tolerance;
  426. }
  427. inline bool is_zero_approx(float s) {
  428. return abs(s) < (float)CMP_EPSILON;
  429. }
  430. inline bool is_equal_approx(double a, double b) {
  431. // Check for exact equality first, required to handle "infinity" values.
  432. if (a == b) {
  433. return true;
  434. }
  435. // Then check for approximate equality.
  436. double tolerance = CMP_EPSILON * abs(a);
  437. if (tolerance < CMP_EPSILON) {
  438. tolerance = CMP_EPSILON;
  439. }
  440. return abs(a - b) < tolerance;
  441. }
  442. inline bool is_equal_approx(double a, double b, double tolerance) {
  443. // Check for exact equality first, required to handle "infinity" values.
  444. if (a == b) {
  445. return true;
  446. }
  447. // Then check for approximate equality.
  448. return abs(a - b) < tolerance;
  449. }
  450. inline bool is_zero_approx(double s) {
  451. return abs(s) < CMP_EPSILON;
  452. }
  453. inline float absf(float g) {
  454. union {
  455. float f;
  456. uint32_t i;
  457. } u;
  458. u.f = g;
  459. u.i &= 2147483647u;
  460. return u.f;
  461. }
  462. inline double absd(double g) {
  463. union {
  464. double d;
  465. uint64_t i;
  466. } u;
  467. u.d = g;
  468. u.i &= (uint64_t)9223372036854775807ull;
  469. return u.d;
  470. }
  471. inline double smoothstep(double p_from, double p_to, double p_weight) {
  472. if (is_equal_approx(static_cast<real_t>(p_from), static_cast<real_t>(p_to))) {
  473. return p_from;
  474. }
  475. double x = clamp((p_weight - p_from) / (p_to - p_from), 0.0, 1.0);
  476. return x * x * (3.0 - 2.0 * x);
  477. }
  478. inline float smoothstep(float p_from, float p_to, float p_weight) {
  479. if (is_equal_approx(p_from, p_to)) {
  480. return p_from;
  481. }
  482. float x = clamp((p_weight - p_from) / (p_to - p_from), 0.0f, 1.0f);
  483. return x * x * (3.0f - 2.0f * x);
  484. }
  485. inline double move_toward(double p_from, double p_to, double p_delta) {
  486. return std::abs(p_to - p_from) <= p_delta ? p_to : p_from + sign(p_to - p_from) * p_delta;
  487. }
  488. inline float move_toward(float p_from, float p_to, float p_delta) {
  489. return std::abs(p_to - p_from) <= p_delta ? p_to : p_from + sign(p_to - p_from) * p_delta;
  490. }
  491. inline double linear2db(double p_linear) {
  492. return log(p_linear) * 8.6858896380650365530225783783321;
  493. }
  494. inline float linear2db(float p_linear) {
  495. return log(p_linear) * 8.6858896380650365530225783783321f;
  496. }
  497. inline double db2linear(double p_db) {
  498. return exp(p_db * 0.11512925464970228420089957273422);
  499. }
  500. inline float db2linear(float p_db) {
  501. return exp(p_db * 0.11512925464970228420089957273422f);
  502. }
  503. inline double round(double p_val) {
  504. return (p_val >= 0) ? floor(p_val + 0.5) : -floor(-p_val + 0.5);
  505. }
  506. inline float round(float p_val) {
  507. return (p_val >= 0) ? floor(p_val + 0.5f) : -floor(-p_val + 0.5f);
  508. }
  509. inline int64_t wrapi(int64_t value, int64_t min, int64_t max) {
  510. int64_t range = max - min;
  511. return range == 0 ? min : min + ((((value - min) % range) + range) % range);
  512. }
  513. inline float wrapf(real_t value, real_t min, real_t max) {
  514. const real_t range = max - min;
  515. return is_zero_approx(range) ? min : value - (range * floor((value - min) / range));
  516. }
  517. inline float fract(float value) {
  518. return value - floor(value);
  519. }
  520. inline double fract(double value) {
  521. return value - floor(value);
  522. }
  523. inline float pingpong(float value, float length) {
  524. return (length != 0.0f) ? abs(fract((value - length) / (length * 2.0f)) * length * 2.0f - length) : 0.0f;
  525. }
  526. inline double pingpong(double value, double length) {
  527. return (length != 0.0) ? abs(fract((value - length) / (length * 2.0)) * length * 2.0 - length) : 0.0;
  528. }
  529. inline unsigned int next_power_of_2(unsigned int x) {
  530. if (x == 0)
  531. return 0;
  532. --x;
  533. x |= x >> 1;
  534. x |= x >> 2;
  535. x |= x >> 4;
  536. x |= x >> 8;
  537. x |= x >> 16;
  538. return ++x;
  539. }
  540. // This function should be as fast as possible and rounding mode should not matter.
  541. inline int fast_ftoi(float a) {
  542. static int b;
  543. #if (defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0603) || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP // windows 8 phone?
  544. b = (int)((a > 0.0) ? (a + 0.5) : (a - 0.5));
  545. #elif defined(_MSC_VER) && _MSC_VER < 1800
  546. __asm fld a __asm fistp b
  547. /*#elif defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) )
  548. // use AT&T inline assembly style, document that
  549. // we use memory as output (=m) and input (m)
  550. __asm__ __volatile__ (
  551. "flds %1 \n\t"
  552. "fistpl %0 \n\t"
  553. : "=m" (b)
  554. : "m" (a));*/
  555. #else
  556. b = lrintf(a); // assuming everything but msvc 2012 or earlier has lrint
  557. #endif
  558. return b;
  559. }
  560. inline double snapped(double p_value, double p_step) {
  561. if (p_step != 0) {
  562. p_value = Math::floor(p_value / p_step + 0.5) * p_step;
  563. }
  564. return p_value;
  565. }
  566. inline float snap_scalar(float p_offset, float p_step, float p_target) {
  567. return p_step != 0 ? Math::snapped(p_target - p_offset, p_step) + p_offset : p_target;
  568. }
  569. inline float snap_scalar_separation(float p_offset, float p_step, float p_target, float p_separation) {
  570. if (p_step != 0) {
  571. float a = Math::snapped(p_target - p_offset, p_step + p_separation) + p_offset;
  572. float b = a;
  573. if (p_target >= 0) {
  574. b -= p_separation;
  575. } else {
  576. b += p_step;
  577. }
  578. return (Math::abs(p_target - a) < Math::abs(p_target - b)) ? a : b;
  579. }
  580. return p_target;
  581. }
  582. } // namespace Math
  583. } // namespace godot
  584. #endif // GODOT_MATH_HPP