math_funcs.h 29 KB

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