math_funcs.h 28 KB

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