math_funcs.h 26 KB

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