math_funcs.h 28 KB

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