math_funcs.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /**************************************************************************/
  2. /* math_funcs.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 MATH_FUNCS_H
  31. #define MATH_FUNCS_H
  32. #include "core/error/error_macros.h"
  33. #include "core/math/math_defs.h"
  34. #include "core/math/random_pcg.h"
  35. #include "core/typedefs.h"
  36. #include "thirdparty/misc/pcg.h"
  37. #include <float.h>
  38. #include <math.h>
  39. class Math {
  40. static RandomPCG default_rand;
  41. public:
  42. Math() {} // useless to instance
  43. // Not using 'RANDOM_MAX' to avoid conflict with system headers on some OSes (at least NetBSD).
  44. static const uint64_t RANDOM_32BIT_MAX = 0xFFFFFFFF;
  45. static _ALWAYS_INLINE_ double sin(double p_x) { return ::sin(p_x); }
  46. static _ALWAYS_INLINE_ float sin(float p_x) { return ::sinf(p_x); }
  47. static _ALWAYS_INLINE_ double cos(double p_x) { return ::cos(p_x); }
  48. static _ALWAYS_INLINE_ float cos(float p_x) { return ::cosf(p_x); }
  49. static _ALWAYS_INLINE_ double tan(double p_x) { return ::tan(p_x); }
  50. static _ALWAYS_INLINE_ float tan(float p_x) { return ::tanf(p_x); }
  51. static _ALWAYS_INLINE_ double sinh(double p_x) { return ::sinh(p_x); }
  52. static _ALWAYS_INLINE_ float sinh(float p_x) { return ::sinhf(p_x); }
  53. static _ALWAYS_INLINE_ float sinc(float p_x) { return p_x == 0 ? 1 : ::sin(p_x) / p_x; }
  54. static _ALWAYS_INLINE_ double sinc(double p_x) { return p_x == 0 ? 1 : ::sin(p_x) / p_x; }
  55. static _ALWAYS_INLINE_ float sincn(float p_x) { return sinc((float)Math_PI * p_x); }
  56. static _ALWAYS_INLINE_ double sincn(double p_x) { return sinc(Math_PI * p_x); }
  57. static _ALWAYS_INLINE_ double cosh(double p_x) { return ::cosh(p_x); }
  58. static _ALWAYS_INLINE_ float cosh(float p_x) { return ::coshf(p_x); }
  59. static _ALWAYS_INLINE_ double tanh(double p_x) { return ::tanh(p_x); }
  60. static _ALWAYS_INLINE_ float tanh(float p_x) { return ::tanhf(p_x); }
  61. // Always does clamping so always safe to use.
  62. static _ALWAYS_INLINE_ double asin(double p_x) { return p_x < -1 ? (-Math_PI / 2) : (p_x > 1 ? (Math_PI / 2) : ::asin(p_x)); }
  63. static _ALWAYS_INLINE_ float asin(float p_x) { return p_x < -1 ? (-Math_PI / 2) : (p_x > 1 ? (Math_PI / 2) : ::asinf(p_x)); }
  64. // Always does clamping so always safe to use.
  65. static _ALWAYS_INLINE_ double acos(double p_x) { return p_x < -1 ? Math_PI : (p_x > 1 ? 0 : ::acos(p_x)); }
  66. static _ALWAYS_INLINE_ float acos(float p_x) { return p_x < -1 ? Math_PI : (p_x > 1 ? 0 : ::acosf(p_x)); }
  67. static _ALWAYS_INLINE_ double atan(double p_x) { return ::atan(p_x); }
  68. static _ALWAYS_INLINE_ float atan(float p_x) { return ::atanf(p_x); }
  69. static _ALWAYS_INLINE_ double atan2(double p_y, double p_x) { return ::atan2(p_y, p_x); }
  70. static _ALWAYS_INLINE_ float atan2(float p_y, float p_x) { return ::atan2f(p_y, p_x); }
  71. static _ALWAYS_INLINE_ double sqrt(double p_x) { return ::sqrt(p_x); }
  72. static _ALWAYS_INLINE_ float sqrt(float p_x) { return ::sqrtf(p_x); }
  73. static _ALWAYS_INLINE_ double fmod(double p_x, double p_y) { return ::fmod(p_x, p_y); }
  74. static _ALWAYS_INLINE_ float fmod(float p_x, float p_y) { return ::fmodf(p_x, p_y); }
  75. static _ALWAYS_INLINE_ double floor(double p_x) { return ::floor(p_x); }
  76. static _ALWAYS_INLINE_ float floor(float p_x) { return ::floorf(p_x); }
  77. static _ALWAYS_INLINE_ double ceil(double p_x) { return ::ceil(p_x); }
  78. static _ALWAYS_INLINE_ float ceil(float p_x) { return ::ceilf(p_x); }
  79. static _ALWAYS_INLINE_ double pow(double p_x, double p_y) { return ::pow(p_x, p_y); }
  80. static _ALWAYS_INLINE_ float pow(float p_x, float p_y) { return ::powf(p_x, p_y); }
  81. static _ALWAYS_INLINE_ double log(double p_x) { return ::log(p_x); }
  82. static _ALWAYS_INLINE_ float log(float p_x) { return ::logf(p_x); }
  83. static _ALWAYS_INLINE_ double log1p(double p_x) { return ::log1p(p_x); }
  84. static _ALWAYS_INLINE_ float log1p(float p_x) { return ::log1pf(p_x); }
  85. static _ALWAYS_INLINE_ double log2(double p_x) { return ::log2(p_x); }
  86. static _ALWAYS_INLINE_ float log2(float p_x) { return ::log2f(p_x); }
  87. static _ALWAYS_INLINE_ double exp(double p_x) { return ::exp(p_x); }
  88. static _ALWAYS_INLINE_ float exp(float p_x) { return ::expf(p_x); }
  89. static _ALWAYS_INLINE_ bool is_nan(double p_val) {
  90. #ifdef _MSC_VER
  91. return _isnan(p_val);
  92. #elif defined(__GNUC__) && __GNUC__ < 6
  93. union {
  94. uint64_t u;
  95. double f;
  96. } ieee754;
  97. ieee754.f = p_val;
  98. // (unsigned)(0x7ff0000000000001 >> 32) : 0x7ff00000
  99. return ((((unsigned)(ieee754.u >> 32) & 0x7fffffff) + ((unsigned)ieee754.u != 0)) > 0x7ff00000);
  100. #else
  101. return isnan(p_val);
  102. #endif
  103. }
  104. static _ALWAYS_INLINE_ bool is_nan(float p_val) {
  105. #ifdef _MSC_VER
  106. return _isnan(p_val);
  107. #elif defined(__GNUC__) && __GNUC__ < 6
  108. union {
  109. uint32_t u;
  110. float f;
  111. } ieee754;
  112. ieee754.f = p_val;
  113. // -----------------------------------
  114. // (single-precision floating-point)
  115. // NaN : s111 1111 1xxx xxxx xxxx xxxx xxxx xxxx
  116. // : (> 0x7f800000)
  117. // where,
  118. // s : sign
  119. // x : non-zero number
  120. // -----------------------------------
  121. return ((ieee754.u & 0x7fffffff) > 0x7f800000);
  122. #else
  123. return isnan(p_val);
  124. #endif
  125. }
  126. static _ALWAYS_INLINE_ bool is_inf(double p_val) {
  127. #ifdef _MSC_VER
  128. return !_finite(p_val);
  129. // use an inline implementation of isinf as a workaround for problematic libstdc++ versions from gcc 5.x era
  130. #elif defined(__GNUC__) && __GNUC__ < 6
  131. union {
  132. uint64_t u;
  133. double f;
  134. } ieee754;
  135. ieee754.f = p_val;
  136. return ((unsigned)(ieee754.u >> 32) & 0x7fffffff) == 0x7ff00000 &&
  137. ((unsigned)ieee754.u == 0);
  138. #else
  139. return isinf(p_val);
  140. #endif
  141. }
  142. static _ALWAYS_INLINE_ bool is_inf(float p_val) {
  143. #ifdef _MSC_VER
  144. return !_finite(p_val);
  145. // use an inline implementation of isinf as a workaround for problematic libstdc++ versions from gcc 5.x era
  146. #elif defined(__GNUC__) && __GNUC__ < 6
  147. union {
  148. uint32_t u;
  149. float f;
  150. } ieee754;
  151. ieee754.f = p_val;
  152. return (ieee754.u & 0x7fffffff) == 0x7f800000;
  153. #else
  154. return isinf(p_val);
  155. #endif
  156. }
  157. static _ALWAYS_INLINE_ bool is_finite(double p_val) { return isfinite(p_val); }
  158. static _ALWAYS_INLINE_ bool is_finite(float p_val) { return isfinite(p_val); }
  159. static _ALWAYS_INLINE_ double abs(double g) { return absd(g); }
  160. static _ALWAYS_INLINE_ float abs(float g) { return absf(g); }
  161. static _ALWAYS_INLINE_ int abs(int g) { return g > 0 ? g : -g; }
  162. static _ALWAYS_INLINE_ double fposmod(double p_x, double p_y) {
  163. double value = Math::fmod(p_x, p_y);
  164. if (((value < 0) && (p_y > 0)) || ((value > 0) && (p_y < 0))) {
  165. value += p_y;
  166. }
  167. value += 0.0;
  168. return value;
  169. }
  170. static _ALWAYS_INLINE_ float fposmod(float p_x, float p_y) {
  171. float value = Math::fmod(p_x, p_y);
  172. if (((value < 0) && (p_y > 0)) || ((value > 0) && (p_y < 0))) {
  173. value += p_y;
  174. }
  175. value += 0.0f;
  176. return value;
  177. }
  178. static _ALWAYS_INLINE_ float fposmodp(float p_x, float p_y) {
  179. float value = Math::fmod(p_x, p_y);
  180. if (value < 0) {
  181. value += p_y;
  182. }
  183. value += 0.0f;
  184. return value;
  185. }
  186. static _ALWAYS_INLINE_ double fposmodp(double p_x, double p_y) {
  187. double value = Math::fmod(p_x, p_y);
  188. if (value < 0) {
  189. value += p_y;
  190. }
  191. value += 0.0;
  192. return value;
  193. }
  194. static _ALWAYS_INLINE_ int64_t posmod(int64_t p_x, int64_t p_y) {
  195. ERR_FAIL_COND_V_MSG(p_y == 0, 0, "Division by zero in posmod is undefined. Returning 0 as fallback.");
  196. int64_t value = p_x % p_y;
  197. if (((value < 0) && (p_y > 0)) || ((value > 0) && (p_y < 0))) {
  198. value += p_y;
  199. }
  200. return value;
  201. }
  202. static _ALWAYS_INLINE_ double deg_to_rad(double p_y) { return p_y * (Math_PI / 180.0); }
  203. static _ALWAYS_INLINE_ float deg_to_rad(float p_y) { return p_y * (float)(Math_PI / 180.0); }
  204. static _ALWAYS_INLINE_ double rad_to_deg(double p_y) { return p_y * (180.0 / Math_PI); }
  205. static _ALWAYS_INLINE_ float rad_to_deg(float p_y) { return p_y * (float)(180.0 / Math_PI); }
  206. static _ALWAYS_INLINE_ double lerp(double p_from, double p_to, double p_weight) { return p_from + (p_to - p_from) * p_weight; }
  207. static _ALWAYS_INLINE_ float lerp(float p_from, float p_to, float p_weight) { return p_from + (p_to - p_from) * p_weight; }
  208. static _ALWAYS_INLINE_ double cubic_interpolate(double p_from, double p_to, double p_pre, double p_post, double p_weight) {
  209. return 0.5 *
  210. ((p_from * 2.0) +
  211. (-p_pre + p_to) * p_weight +
  212. (2.0 * p_pre - 5.0 * p_from + 4.0 * p_to - p_post) * (p_weight * p_weight) +
  213. (-p_pre + 3.0 * p_from - 3.0 * p_to + p_post) * (p_weight * p_weight * p_weight));
  214. }
  215. static _ALWAYS_INLINE_ float cubic_interpolate(float p_from, float p_to, float p_pre, float p_post, float p_weight) {
  216. return 0.5f *
  217. ((p_from * 2.0f) +
  218. (-p_pre + p_to) * p_weight +
  219. (2.0f * p_pre - 5.0f * p_from + 4.0f * p_to - p_post) * (p_weight * p_weight) +
  220. (-p_pre + 3.0f * p_from - 3.0f * p_to + p_post) * (p_weight * p_weight * p_weight));
  221. }
  222. static _ALWAYS_INLINE_ double cubic_interpolate_angle(double p_from, double p_to, double p_pre, double p_post, double p_weight) {
  223. double from_rot = fmod(p_from, Math_TAU);
  224. double pre_diff = fmod(p_pre - from_rot, Math_TAU);
  225. double pre_rot = from_rot + fmod(2.0 * pre_diff, Math_TAU) - pre_diff;
  226. double to_diff = fmod(p_to - from_rot, Math_TAU);
  227. double to_rot = from_rot + fmod(2.0 * to_diff, Math_TAU) - to_diff;
  228. double post_diff = fmod(p_post - to_rot, Math_TAU);
  229. double post_rot = to_rot + fmod(2.0 * post_diff, Math_TAU) - post_diff;
  230. return cubic_interpolate(from_rot, to_rot, pre_rot, post_rot, p_weight);
  231. }
  232. static _ALWAYS_INLINE_ float cubic_interpolate_angle(float p_from, float p_to, float p_pre, float p_post, float p_weight) {
  233. float from_rot = fmod(p_from, (float)Math_TAU);
  234. float pre_diff = fmod(p_pre - from_rot, (float)Math_TAU);
  235. float pre_rot = from_rot + fmod(2.0f * pre_diff, (float)Math_TAU) - pre_diff;
  236. float to_diff = fmod(p_to - from_rot, (float)Math_TAU);
  237. float to_rot = from_rot + fmod(2.0f * to_diff, (float)Math_TAU) - to_diff;
  238. float post_diff = fmod(p_post - to_rot, (float)Math_TAU);
  239. float post_rot = to_rot + fmod(2.0f * post_diff, (float)Math_TAU) - post_diff;
  240. return cubic_interpolate(from_rot, to_rot, pre_rot, post_rot, p_weight);
  241. }
  242. static _ALWAYS_INLINE_ double cubic_interpolate_in_time(double p_from, double p_to, double p_pre, double p_post, double p_weight,
  243. double p_to_t, double p_pre_t, double p_post_t) {
  244. /* Barry-Goldman method */
  245. double t = Math::lerp(0.0, p_to_t, p_weight);
  246. double a1 = Math::lerp(p_pre, p_from, p_pre_t == 0 ? 0.0 : (t - p_pre_t) / -p_pre_t);
  247. double a2 = Math::lerp(p_from, p_to, p_to_t == 0 ? 0.5 : t / p_to_t);
  248. 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));
  249. 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));
  250. double b2 = Math::lerp(a2, a3, p_post_t == 0 ? 1.0 : t / p_post_t);
  251. return Math::lerp(b1, b2, p_to_t == 0 ? 0.5 : t / p_to_t);
  252. }
  253. static _ALWAYS_INLINE_ float cubic_interpolate_in_time(float p_from, float p_to, float p_pre, float p_post, float p_weight,
  254. float p_to_t, float p_pre_t, float p_post_t) {
  255. /* Barry-Goldman method */
  256. float t = Math::lerp(0.0f, p_to_t, p_weight);
  257. float a1 = Math::lerp(p_pre, p_from, p_pre_t == 0 ? 0.0f : (t - p_pre_t) / -p_pre_t);
  258. float a2 = Math::lerp(p_from, p_to, p_to_t == 0 ? 0.5f : t / p_to_t);
  259. 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));
  260. 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));
  261. float b2 = Math::lerp(a2, a3, p_post_t == 0 ? 1.0f : t / p_post_t);
  262. return Math::lerp(b1, b2, p_to_t == 0 ? 0.5f : t / p_to_t);
  263. }
  264. static _ALWAYS_INLINE_ double cubic_interpolate_angle_in_time(double p_from, double p_to, double p_pre, double p_post, double p_weight,
  265. double p_to_t, double p_pre_t, double p_post_t) {
  266. double from_rot = fmod(p_from, Math_TAU);
  267. double pre_diff = fmod(p_pre - from_rot, Math_TAU);
  268. double pre_rot = from_rot + fmod(2.0 * pre_diff, Math_TAU) - pre_diff;
  269. double to_diff = fmod(p_to - from_rot, Math_TAU);
  270. double to_rot = from_rot + fmod(2.0 * to_diff, Math_TAU) - to_diff;
  271. double post_diff = fmod(p_post - to_rot, Math_TAU);
  272. double post_rot = to_rot + fmod(2.0 * post_diff, Math_TAU) - post_diff;
  273. return cubic_interpolate_in_time(from_rot, to_rot, pre_rot, post_rot, p_weight, p_to_t, p_pre_t, p_post_t);
  274. }
  275. static _ALWAYS_INLINE_ float cubic_interpolate_angle_in_time(float p_from, float p_to, float p_pre, float p_post, float p_weight,
  276. float p_to_t, float p_pre_t, float p_post_t) {
  277. float from_rot = fmod(p_from, (float)Math_TAU);
  278. float pre_diff = fmod(p_pre - from_rot, (float)Math_TAU);
  279. float pre_rot = from_rot + fmod(2.0f * pre_diff, (float)Math_TAU) - pre_diff;
  280. float to_diff = fmod(p_to - from_rot, (float)Math_TAU);
  281. float to_rot = from_rot + fmod(2.0f * to_diff, (float)Math_TAU) - to_diff;
  282. float post_diff = fmod(p_post - to_rot, (float)Math_TAU);
  283. float post_rot = to_rot + fmod(2.0f * post_diff, (float)Math_TAU) - post_diff;
  284. return cubic_interpolate_in_time(from_rot, to_rot, pre_rot, post_rot, p_weight, p_to_t, p_pre_t, p_post_t);
  285. }
  286. static _ALWAYS_INLINE_ double bezier_interpolate(double p_start, double p_control_1, double p_control_2, double p_end, double p_t) {
  287. /* Formula from Wikipedia article on Bezier curves. */
  288. double omt = (1.0 - p_t);
  289. double omt2 = omt * omt;
  290. double omt3 = omt2 * omt;
  291. double t2 = p_t * p_t;
  292. double t3 = t2 * p_t;
  293. return p_start * omt3 + p_control_1 * omt2 * p_t * 3.0 + p_control_2 * omt * t2 * 3.0 + p_end * t3;
  294. }
  295. static _ALWAYS_INLINE_ float bezier_interpolate(float p_start, float p_control_1, float p_control_2, float p_end, float p_t) {
  296. /* Formula from Wikipedia article on Bezier curves. */
  297. float omt = (1.0f - p_t);
  298. float omt2 = omt * omt;
  299. float omt3 = omt2 * omt;
  300. float t2 = p_t * p_t;
  301. float t3 = t2 * p_t;
  302. return p_start * omt3 + p_control_1 * omt2 * p_t * 3.0f + p_control_2 * omt * t2 * 3.0f + p_end * t3;
  303. }
  304. static _ALWAYS_INLINE_ double bezier_derivative(double p_start, double p_control_1, double p_control_2, double p_end, double p_t) {
  305. /* Formula from Wikipedia article on Bezier curves. */
  306. double omt = (1.0 - p_t);
  307. double omt2 = omt * omt;
  308. double t2 = p_t * p_t;
  309. double d = (p_control_1 - p_start) * 3.0 * omt2 + (p_control_2 - p_control_1) * 6.0 * omt * p_t + (p_end - p_control_2) * 3.0 * t2;
  310. return d;
  311. }
  312. static _ALWAYS_INLINE_ float bezier_derivative(float p_start, float p_control_1, float p_control_2, float p_end, float p_t) {
  313. /* Formula from Wikipedia article on Bezier curves. */
  314. float omt = (1.0f - p_t);
  315. float omt2 = omt * omt;
  316. float t2 = p_t * p_t;
  317. float d = (p_control_1 - p_start) * 3.0f * omt2 + (p_control_2 - p_control_1) * 6.0f * omt * p_t + (p_end - p_control_2) * 3.0f * t2;
  318. return d;
  319. }
  320. static _ALWAYS_INLINE_ double lerp_angle(double p_from, double p_to, double p_weight) {
  321. double difference = fmod(p_to - p_from, Math_TAU);
  322. double distance = fmod(2.0 * difference, Math_TAU) - difference;
  323. return p_from + distance * p_weight;
  324. }
  325. static _ALWAYS_INLINE_ float lerp_angle(float p_from, float p_to, float p_weight) {
  326. float difference = fmod(p_to - p_from, (float)Math_TAU);
  327. float distance = fmod(2.0f * difference, (float)Math_TAU) - difference;
  328. return p_from + distance * p_weight;
  329. }
  330. static _ALWAYS_INLINE_ double inverse_lerp(double p_from, double p_to, double p_value) {
  331. return (p_value - p_from) / (p_to - p_from);
  332. }
  333. static _ALWAYS_INLINE_ float inverse_lerp(float p_from, float p_to, float p_value) {
  334. return (p_value - p_from) / (p_to - p_from);
  335. }
  336. static _ALWAYS_INLINE_ double remap(double p_value, double p_istart, double p_istop, double p_ostart, double p_ostop) {
  337. return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value));
  338. }
  339. static _ALWAYS_INLINE_ float remap(float p_value, float p_istart, float p_istop, float p_ostart, float p_ostop) {
  340. return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value));
  341. }
  342. static _ALWAYS_INLINE_ double smoothstep(double p_from, double p_to, double p_s) {
  343. if (is_equal_approx(p_from, p_to)) {
  344. return p_from;
  345. }
  346. double s = CLAMP((p_s - p_from) / (p_to - p_from), 0.0, 1.0);
  347. return s * s * (3.0 - 2.0 * s);
  348. }
  349. static _ALWAYS_INLINE_ float smoothstep(float p_from, float p_to, float p_s) {
  350. if (is_equal_approx(p_from, p_to)) {
  351. return p_from;
  352. }
  353. float s = CLAMP((p_s - p_from) / (p_to - p_from), 0.0f, 1.0f);
  354. return s * s * (3.0f - 2.0f * s);
  355. }
  356. static _ALWAYS_INLINE_ double move_toward(double p_from, double p_to, double p_delta) {
  357. return abs(p_to - p_from) <= p_delta ? p_to : p_from + SIGN(p_to - p_from) * p_delta;
  358. }
  359. static _ALWAYS_INLINE_ float move_toward(float p_from, float p_to, float p_delta) {
  360. return abs(p_to - p_from) <= p_delta ? p_to : p_from + SIGN(p_to - p_from) * p_delta;
  361. }
  362. static _ALWAYS_INLINE_ double linear_to_db(double p_linear) {
  363. return Math::log(p_linear) * 8.6858896380650365530225783783321;
  364. }
  365. static _ALWAYS_INLINE_ float linear_to_db(float p_linear) {
  366. return Math::log(p_linear) * (float)8.6858896380650365530225783783321;
  367. }
  368. static _ALWAYS_INLINE_ double db_to_linear(double p_db) {
  369. return Math::exp(p_db * 0.11512925464970228420089957273422);
  370. }
  371. static _ALWAYS_INLINE_ float db_to_linear(float p_db) {
  372. return Math::exp(p_db * (float)0.11512925464970228420089957273422);
  373. }
  374. static _ALWAYS_INLINE_ double round(double p_val) { return ::round(p_val); }
  375. static _ALWAYS_INLINE_ float round(float p_val) { return ::roundf(p_val); }
  376. static _ALWAYS_INLINE_ int64_t wrapi(int64_t value, int64_t min, int64_t max) {
  377. int64_t range = max - min;
  378. return range == 0 ? min : min + ((((value - min) % range) + range) % range);
  379. }
  380. static _ALWAYS_INLINE_ double wrapf(double value, double min, double max) {
  381. double range = max - min;
  382. if (is_zero_approx(range)) {
  383. return min;
  384. }
  385. double result = value - (range * Math::floor((value - min) / range));
  386. if (is_equal_approx(result, max)) {
  387. return min;
  388. }
  389. return result;
  390. }
  391. static _ALWAYS_INLINE_ float wrapf(float value, float min, float max) {
  392. float range = max - min;
  393. if (is_zero_approx(range)) {
  394. return min;
  395. }
  396. float result = value - (range * Math::floor((value - min) / range));
  397. if (is_equal_approx(result, max)) {
  398. return min;
  399. }
  400. return result;
  401. }
  402. static _ALWAYS_INLINE_ float fract(float value) {
  403. return value - floor(value);
  404. }
  405. static _ALWAYS_INLINE_ double fract(double value) {
  406. return value - floor(value);
  407. }
  408. static _ALWAYS_INLINE_ float pingpong(float value, float length) {
  409. return (length != 0.0f) ? abs(fract((value - length) / (length * 2.0f)) * length * 2.0f - length) : 0.0f;
  410. }
  411. static _ALWAYS_INLINE_ double pingpong(double value, double length) {
  412. return (length != 0.0) ? abs(fract((value - length) / (length * 2.0)) * length * 2.0 - length) : 0.0;
  413. }
  414. // double only, as these functions are mainly used by the editor and not performance-critical,
  415. static double ease(double p_x, double p_c);
  416. static int step_decimals(double p_step);
  417. static int range_step_decimals(double p_step); // For editor use only.
  418. static double snapped(double p_value, double p_step);
  419. static uint32_t larger_prime(uint32_t p_val);
  420. static void seed(uint64_t x);
  421. static void randomize();
  422. static uint32_t rand_from_seed(uint64_t *seed);
  423. static uint32_t rand();
  424. static _ALWAYS_INLINE_ double randd() { return (double)rand() / (double)Math::RANDOM_32BIT_MAX; }
  425. static _ALWAYS_INLINE_ float randf() { return (float)rand() / (float)Math::RANDOM_32BIT_MAX; }
  426. static double randfn(double mean, double deviation);
  427. static double random(double from, double to);
  428. static float random(float from, float to);
  429. static int random(int from, int to);
  430. static _ALWAYS_INLINE_ bool is_equal_approx(float a, float 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. float tolerance = (float)CMP_EPSILON * abs(a);
  437. if (tolerance < (float)CMP_EPSILON) {
  438. tolerance = (float)CMP_EPSILON;
  439. }
  440. return abs(a - b) < tolerance;
  441. }
  442. static _ALWAYS_INLINE_ bool is_equal_approx(float a, float b, float 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. static _ALWAYS_INLINE_ bool is_zero_approx(float s) {
  451. return abs(s) < (float)CMP_EPSILON;
  452. }
  453. static _ALWAYS_INLINE_ bool is_equal_approx(double a, double b) {
  454. // Check for exact equality first, required to handle "infinity" values.
  455. if (a == b) {
  456. return true;
  457. }
  458. // Then check for approximate equality.
  459. double tolerance = CMP_EPSILON * abs(a);
  460. if (tolerance < CMP_EPSILON) {
  461. tolerance = CMP_EPSILON;
  462. }
  463. return abs(a - b) < tolerance;
  464. }
  465. static _ALWAYS_INLINE_ bool is_equal_approx(double a, double b, double tolerance) {
  466. // Check for exact equality first, required to handle "infinity" values.
  467. if (a == b) {
  468. return true;
  469. }
  470. // Then check for approximate equality.
  471. return abs(a - b) < tolerance;
  472. }
  473. static _ALWAYS_INLINE_ bool is_zero_approx(double s) {
  474. return abs(s) < CMP_EPSILON;
  475. }
  476. static _ALWAYS_INLINE_ float absf(float g) {
  477. union {
  478. float f;
  479. uint32_t i;
  480. } u;
  481. u.f = g;
  482. u.i &= 2147483647u;
  483. return u.f;
  484. }
  485. static _ALWAYS_INLINE_ double absd(double g) {
  486. union {
  487. double d;
  488. uint64_t i;
  489. } u;
  490. u.d = g;
  491. u.i &= (uint64_t)9223372036854775807ll;
  492. return u.d;
  493. }
  494. // This function should be as fast as possible and rounding mode should not matter.
  495. static _ALWAYS_INLINE_ int fast_ftoi(float a) {
  496. // Assuming every supported compiler has `lrint()`.
  497. return lrintf(a);
  498. }
  499. static _ALWAYS_INLINE_ uint32_t halfbits_to_floatbits(uint16_t h) {
  500. uint16_t h_exp, h_sig;
  501. uint32_t f_sgn, f_exp, f_sig;
  502. h_exp = (h & 0x7c00u);
  503. f_sgn = ((uint32_t)h & 0x8000u) << 16;
  504. switch (h_exp) {
  505. case 0x0000u: /* 0 or subnormal */
  506. h_sig = (h & 0x03ffu);
  507. /* Signed zero */
  508. if (h_sig == 0) {
  509. return f_sgn;
  510. }
  511. /* Subnormal */
  512. h_sig <<= 1;
  513. while ((h_sig & 0x0400u) == 0) {
  514. h_sig <<= 1;
  515. h_exp++;
  516. }
  517. f_exp = ((uint32_t)(127 - 15 - h_exp)) << 23;
  518. f_sig = ((uint32_t)(h_sig & 0x03ffu)) << 13;
  519. return f_sgn + f_exp + f_sig;
  520. case 0x7c00u: /* inf or NaN */
  521. /* All-ones exponent and a copy of the significand */
  522. return f_sgn + 0x7f800000u + (((uint32_t)(h & 0x03ffu)) << 13);
  523. default: /* normalized */
  524. /* Just need to adjust the exponent and shift */
  525. return f_sgn + (((uint32_t)(h & 0x7fffu) + 0x1c000u) << 13);
  526. }
  527. }
  528. static _ALWAYS_INLINE_ float halfptr_to_float(const uint16_t *h) {
  529. union {
  530. uint32_t u32;
  531. float f32;
  532. } u;
  533. u.u32 = halfbits_to_floatbits(*h);
  534. return u.f32;
  535. }
  536. static _ALWAYS_INLINE_ float half_to_float(const uint16_t h) {
  537. return halfptr_to_float(&h);
  538. }
  539. static _ALWAYS_INLINE_ uint16_t make_half_float(float f) {
  540. union {
  541. float fv;
  542. uint32_t ui;
  543. } ci;
  544. ci.fv = f;
  545. uint32_t x = ci.ui;
  546. uint32_t sign = (unsigned short)(x >> 31);
  547. uint32_t mantissa;
  548. uint32_t exponent;
  549. uint16_t hf;
  550. // get mantissa
  551. mantissa = x & ((1 << 23) - 1);
  552. // get exponent bits
  553. exponent = x & (0xFF << 23);
  554. if (exponent >= 0x47800000) {
  555. // check if the original single precision float number is a NaN
  556. if (mantissa && (exponent == (0xFF << 23))) {
  557. // we have a single precision NaN
  558. mantissa = (1 << 23) - 1;
  559. } else {
  560. // 16-bit half-float representation stores number as Inf
  561. mantissa = 0;
  562. }
  563. hf = (((uint16_t)sign) << 15) | (uint16_t)((0x1F << 10)) |
  564. (uint16_t)(mantissa >> 13);
  565. }
  566. // check if exponent is <= -15
  567. else if (exponent <= 0x38000000) {
  568. /*
  569. // store a denorm half-float value or zero
  570. exponent = (0x38000000 - exponent) >> 23;
  571. mantissa >>= (14 + exponent);
  572. hf = (((uint16_t)sign) << 15) | (uint16_t)(mantissa);
  573. */
  574. hf = 0; //denormals do not work for 3D, convert to zero
  575. } else {
  576. hf = (((uint16_t)sign) << 15) |
  577. (uint16_t)((exponent - 0x38000000) >> 13) |
  578. (uint16_t)(mantissa >> 13);
  579. }
  580. return hf;
  581. }
  582. static _ALWAYS_INLINE_ float snap_scalar(float p_offset, float p_step, float p_target) {
  583. return p_step != 0 ? Math::snapped(p_target - p_offset, p_step) + p_offset : p_target;
  584. }
  585. static _ALWAYS_INLINE_ float snap_scalar_separation(float p_offset, float p_step, float p_target, float p_separation) {
  586. if (p_step != 0) {
  587. float a = Math::snapped(p_target - p_offset, p_step + p_separation) + p_offset;
  588. float b = a;
  589. if (p_target >= 0) {
  590. b -= p_separation;
  591. } else {
  592. b += p_step;
  593. }
  594. return (Math::abs(p_target - a) < Math::abs(p_target - b)) ? a : b;
  595. }
  596. return p_target;
  597. }
  598. };
  599. #endif // MATH_FUNCS_H