math_funcs.h 26 KB

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