math.hpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /**************************************************************************/
  2. /* math.hpp */
  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 <godot_cpp/core/defs.hpp>
  32. #include <gdextension_interface.h>
  33. #include <cmath>
  34. namespace godot {
  35. #define Math_SQRT12 0.7071067811865475244008443621048490
  36. #define Math_SQRT2 1.4142135623730950488016887242
  37. #define Math_LN2 0.6931471805599453094172321215
  38. #define Math_PI 3.1415926535897932384626433833
  39. #define Math_TAU 6.2831853071795864769252867666
  40. #define Math_E 2.7182818284590452353602874714
  41. #define Math_INF INFINITY
  42. #define Math_NAN NAN
  43. // Make room for our constexpr's below by overriding potential system-specific macros.
  44. #undef ABS
  45. #undef SIGN
  46. #undef MIN
  47. #undef MAX
  48. #undef CLAMP
  49. // Generic ABS function, for math uses please use Math::abs.
  50. template <typename T>
  51. constexpr T ABS(T m_v) {
  52. return m_v < 0 ? -m_v : m_v;
  53. }
  54. template <typename T>
  55. constexpr const T SIGN(const T m_v) {
  56. return m_v == 0 ? 0.0f : (m_v < 0 ? -1.0f : +1.0f);
  57. }
  58. template <typename T, typename T2>
  59. constexpr auto MIN(const T m_a, const T2 m_b) {
  60. return m_a < m_b ? m_a : m_b;
  61. }
  62. template <typename T, typename T2>
  63. constexpr auto MAX(const T m_a, const T2 m_b) {
  64. return m_a > m_b ? m_a : m_b;
  65. }
  66. template <typename T, typename T2, typename T3>
  67. constexpr auto CLAMP(const T m_a, const T2 m_min, const T3 m_max) {
  68. return m_a < m_min ? m_min : (m_a > m_max ? m_max : m_a);
  69. }
  70. // Generic swap template.
  71. #ifndef SWAP
  72. #define SWAP(m_x, m_y) __swap_tmpl((m_x), (m_y))
  73. template <typename T>
  74. inline void __swap_tmpl(T &x, T &y) {
  75. T aux = x;
  76. x = y;
  77. y = aux;
  78. }
  79. #endif // SWAP
  80. /* Functions to handle powers of 2 and shifting. */
  81. // Function to find the next power of 2 to an integer.
  82. static _FORCE_INLINE_ unsigned int next_power_of_2(unsigned int x) {
  83. if (x == 0) {
  84. return 0;
  85. }
  86. --x;
  87. x |= x >> 1;
  88. x |= x >> 2;
  89. x |= x >> 4;
  90. x |= x >> 8;
  91. x |= x >> 16;
  92. return ++x;
  93. }
  94. // Function to find the previous power of 2 to an integer.
  95. static _FORCE_INLINE_ unsigned int previous_power_of_2(unsigned int x) {
  96. x |= x >> 1;
  97. x |= x >> 2;
  98. x |= x >> 4;
  99. x |= x >> 8;
  100. x |= x >> 16;
  101. return x - (x >> 1);
  102. }
  103. // Function to find the closest power of 2 to an integer.
  104. static _FORCE_INLINE_ unsigned int closest_power_of_2(unsigned int x) {
  105. unsigned int nx = next_power_of_2(x);
  106. unsigned int px = previous_power_of_2(x);
  107. return (nx - x) > (x - px) ? px : nx;
  108. }
  109. // Get a shift value from a power of 2.
  110. static inline int get_shift_from_power_of_2(unsigned int p_bits) {
  111. for (unsigned int i = 0; i < 32; i++) {
  112. if (p_bits == (unsigned int)(1 << i)) {
  113. return i;
  114. }
  115. }
  116. return -1;
  117. }
  118. template <typename T>
  119. static _FORCE_INLINE_ T nearest_power_of_2_templated(T x) {
  120. --x;
  121. // The number of operations on x is the base two logarithm
  122. // of the number of bits in the type. Add three to account
  123. // for sizeof(T) being in bytes.
  124. size_t num = get_shift_from_power_of_2(sizeof(T)) + 3;
  125. // If the compiler is smart, it unrolls this loop.
  126. // If it's dumb, this is a bit slow.
  127. for (size_t i = 0; i < num; i++) {
  128. x |= x >> (1 << i);
  129. }
  130. return ++x;
  131. }
  132. // Function to find the nearest (bigger) power of 2 to an integer.
  133. static inline unsigned int nearest_shift(unsigned int p_number) {
  134. for (int i = 30; i >= 0; i--) {
  135. if (p_number & (1 << i)) {
  136. return i + 1;
  137. }
  138. }
  139. return 0;
  140. }
  141. // constexpr function to find the floored log2 of a number
  142. template <typename T>
  143. constexpr T floor_log2(T x) {
  144. return x < 2 ? x : 1 + floor_log2(x >> 1);
  145. }
  146. // Get the number of bits needed to represent the number.
  147. // IE, if you pass in 8, you will get 4.
  148. // If you want to know how many bits are needed to store 8 values however, pass in (8 - 1).
  149. template <typename T>
  150. constexpr T get_num_bits(T x) {
  151. return floor_log2(x);
  152. }
  153. // Swap 16, 32 and 64 bits value for endianness.
  154. #if defined(__GNUC__)
  155. #define BSWAP16(x) __builtin_bswap16(x)
  156. #define BSWAP32(x) __builtin_bswap32(x)
  157. #define BSWAP64(x) __builtin_bswap64(x)
  158. #else
  159. static inline uint16_t BSWAP16(uint16_t x) {
  160. return (x >> 8) | (x << 8);
  161. }
  162. static inline uint32_t BSWAP32(uint32_t x) {
  163. return ((x << 24) | ((x << 8) & 0x00FF0000) | ((x >> 8) & 0x0000FF00) | (x >> 24));
  164. }
  165. static inline uint64_t BSWAP64(uint64_t x) {
  166. x = (x & 0x00000000FFFFFFFF) << 32 | (x & 0xFFFFFFFF00000000) >> 32;
  167. x = (x & 0x0000FFFF0000FFFF) << 16 | (x & 0xFFFF0000FFFF0000) >> 16;
  168. x = (x & 0x00FF00FF00FF00FF) << 8 | (x & 0xFF00FF00FF00FF00) >> 8;
  169. return x;
  170. }
  171. #endif
  172. namespace Math {
  173. // This epsilon should match the one used by Godot for consistency.
  174. // Using `f` when `real_t` is float.
  175. #define CMP_EPSILON 0.00001f
  176. #define CMP_EPSILON2 (CMP_EPSILON * CMP_EPSILON)
  177. // This epsilon is for values related to a unit size (scalar or vector len).
  178. #ifdef PRECISE_MATH_CHECKS
  179. #define UNIT_EPSILON 0.00001
  180. #else
  181. // Tolerate some more floating point error normally.
  182. #define UNIT_EPSILON 0.001
  183. #endif
  184. // Functions reproduced as in Godot's source code `math_funcs.h`.
  185. // Some are overloads to automatically support changing real_t into either double or float in the way Godot does.
  186. inline double fmod(double p_x, double p_y) {
  187. return ::fmod(p_x, p_y);
  188. }
  189. inline float fmod(float p_x, float p_y) {
  190. return ::fmodf(p_x, p_y);
  191. }
  192. inline double fposmod(double p_x, double p_y) {
  193. double value = Math::fmod(p_x, p_y);
  194. if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
  195. value += p_y;
  196. }
  197. value += 0.0;
  198. return value;
  199. }
  200. inline float fposmod(float p_x, float p_y) {
  201. float value = Math::fmod(p_x, p_y);
  202. if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
  203. value += p_y;
  204. }
  205. value += 0.0f;
  206. return value;
  207. }
  208. inline float fposmodp(float p_x, float p_y) {
  209. float value = Math::fmod(p_x, p_y);
  210. if (value < 0) {
  211. value += p_y;
  212. }
  213. value += 0.0f;
  214. return value;
  215. }
  216. inline double fposmodp(double p_x, double p_y) {
  217. double value = Math::fmod(p_x, p_y);
  218. if (value < 0) {
  219. value += p_y;
  220. }
  221. value += 0.0;
  222. return value;
  223. }
  224. inline int64_t posmod(int64_t p_x, int64_t p_y) {
  225. int64_t value = p_x % p_y;
  226. if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
  227. value += p_y;
  228. }
  229. return value;
  230. }
  231. inline double floor(double p_x) {
  232. return ::floor(p_x);
  233. }
  234. inline float floor(float p_x) {
  235. return ::floorf(p_x);
  236. }
  237. inline double ceil(double p_x) {
  238. return ::ceil(p_x);
  239. }
  240. inline float ceil(float p_x) {
  241. return ::ceilf(p_x);
  242. }
  243. inline double exp(double p_x) {
  244. return ::exp(p_x);
  245. }
  246. inline float exp(float p_x) {
  247. return ::expf(p_x);
  248. }
  249. inline double sin(double p_x) {
  250. return ::sin(p_x);
  251. }
  252. inline float sin(float p_x) {
  253. return ::sinf(p_x);
  254. }
  255. inline double cos(double p_x) {
  256. return ::cos(p_x);
  257. }
  258. inline float cos(float p_x) {
  259. return ::cosf(p_x);
  260. }
  261. inline double tan(double p_x) {
  262. return ::tan(p_x);
  263. }
  264. inline float tan(float p_x) {
  265. return ::tanf(p_x);
  266. }
  267. inline double sinh(double p_x) {
  268. return ::sinh(p_x);
  269. }
  270. inline float sinh(float p_x) {
  271. return ::sinhf(p_x);
  272. }
  273. inline float sinc(float p_x) {
  274. return p_x == 0 ? 1 : ::sin(p_x) / p_x;
  275. }
  276. inline double sinc(double p_x) {
  277. return p_x == 0 ? 1 : ::sin(p_x) / p_x;
  278. }
  279. inline float sincn(float p_x) {
  280. return (float)sinc(Math_PI * p_x);
  281. }
  282. inline double sincn(double p_x) {
  283. return sinc(Math_PI * p_x);
  284. }
  285. inline double cosh(double p_x) {
  286. return ::cosh(p_x);
  287. }
  288. inline float cosh(float p_x) {
  289. return ::coshf(p_x);
  290. }
  291. inline double tanh(double p_x) {
  292. return ::tanh(p_x);
  293. }
  294. inline float tanh(float p_x) {
  295. return ::tanhf(p_x);
  296. }
  297. inline double asin(double p_x) {
  298. return ::asin(p_x);
  299. }
  300. inline float asin(float p_x) {
  301. return ::asinf(p_x);
  302. }
  303. inline double acos(double p_x) {
  304. return ::acos(p_x);
  305. }
  306. inline float acos(float p_x) {
  307. return ::acosf(p_x);
  308. }
  309. inline double atan(double p_x) {
  310. return ::atan(p_x);
  311. }
  312. inline float atan(float p_x) {
  313. return ::atanf(p_x);
  314. }
  315. inline double atan2(double p_y, double p_x) {
  316. return ::atan2(p_y, p_x);
  317. }
  318. inline float atan2(float p_y, float p_x) {
  319. return ::atan2f(p_y, p_x);
  320. }
  321. inline double sqrt(double p_x) {
  322. return ::sqrt(p_x);
  323. }
  324. inline float sqrt(float p_x) {
  325. return ::sqrtf(p_x);
  326. }
  327. inline double pow(double p_x, double p_y) {
  328. return ::pow(p_x, p_y);
  329. }
  330. inline float pow(float p_x, float p_y) {
  331. return ::powf(p_x, p_y);
  332. }
  333. inline double log(double p_x) {
  334. return ::log(p_x);
  335. }
  336. inline float log(float p_x) {
  337. return ::logf(p_x);
  338. }
  339. inline float lerp(float minv, float maxv, float t) {
  340. return minv + t * (maxv - minv);
  341. }
  342. inline double lerp(double minv, double maxv, double t) {
  343. return minv + t * (maxv - minv);
  344. }
  345. inline double lerp_angle(double p_from, double p_to, double p_weight) {
  346. double difference = fmod(p_to - p_from, Math_TAU);
  347. double distance = fmod(2.0 * difference, Math_TAU) - difference;
  348. return p_from + distance * p_weight;
  349. }
  350. inline float lerp_angle(float p_from, float p_to, float p_weight) {
  351. float difference = fmod(p_to - p_from, (float)Math_TAU);
  352. float distance = fmod(2.0f * difference, (float)Math_TAU) - difference;
  353. return p_from + distance * p_weight;
  354. }
  355. inline double cubic_interpolate(double p_from, double p_to, double p_pre, double p_post, double p_weight) {
  356. return 0.5 *
  357. ((p_from * 2.0) +
  358. (-p_pre + p_to) * p_weight +
  359. (2.0 * p_pre - 5.0 * p_from + 4.0 * p_to - p_post) * (p_weight * p_weight) +
  360. (-p_pre + 3.0 * p_from - 3.0 * p_to + p_post) * (p_weight * p_weight * p_weight));
  361. }
  362. inline float cubic_interpolate(float p_from, float p_to, float p_pre, float p_post, float p_weight) {
  363. return 0.5f *
  364. ((p_from * 2.0f) +
  365. (-p_pre + p_to) * p_weight +
  366. (2.0f * p_pre - 5.0f * p_from + 4.0f * p_to - p_post) * (p_weight * p_weight) +
  367. (-p_pre + 3.0f * p_from - 3.0f * p_to + p_post) * (p_weight * p_weight * p_weight));
  368. }
  369. inline double cubic_interpolate_angle(double p_from, double p_to, double p_pre, double p_post, double p_weight) {
  370. double from_rot = fmod(p_from, Math_TAU);
  371. double pre_diff = fmod(p_pre - from_rot, Math_TAU);
  372. double pre_rot = from_rot + fmod(2.0 * pre_diff, Math_TAU) - pre_diff;
  373. double to_diff = fmod(p_to - from_rot, Math_TAU);
  374. double to_rot = from_rot + fmod(2.0 * to_diff, Math_TAU) - to_diff;
  375. double post_diff = fmod(p_post - to_rot, Math_TAU);
  376. double post_rot = to_rot + fmod(2.0 * post_diff, Math_TAU) - post_diff;
  377. return cubic_interpolate(from_rot, to_rot, pre_rot, post_rot, p_weight);
  378. }
  379. inline float cubic_interpolate_angle(float p_from, float p_to, float p_pre, float p_post, float p_weight) {
  380. float from_rot = fmod(p_from, (float)Math_TAU);
  381. float pre_diff = fmod(p_pre - from_rot, (float)Math_TAU);
  382. float pre_rot = from_rot + fmod(2.0f * pre_diff, (float)Math_TAU) - pre_diff;
  383. float to_diff = fmod(p_to - from_rot, (float)Math_TAU);
  384. float to_rot = from_rot + fmod(2.0f * to_diff, (float)Math_TAU) - to_diff;
  385. float post_diff = fmod(p_post - to_rot, (float)Math_TAU);
  386. float post_rot = to_rot + fmod(2.0f * post_diff, (float)Math_TAU) - post_diff;
  387. return cubic_interpolate(from_rot, to_rot, pre_rot, post_rot, p_weight);
  388. }
  389. inline double cubic_interpolate_in_time(double p_from, double p_to, double p_pre, double p_post, double p_weight,
  390. double p_to_t, double p_pre_t, double p_post_t) {
  391. /* Barry-Goldman method */
  392. double t = Math::lerp(0.0, p_to_t, p_weight);
  393. double a1 = Math::lerp(p_pre, p_from, p_pre_t == 0 ? 0.0 : (t - p_pre_t) / -p_pre_t);
  394. double a2 = Math::lerp(p_from, p_to, p_to_t == 0 ? 0.5 : t / p_to_t);
  395. 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));
  396. 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));
  397. double b2 = Math::lerp(a2, a3, p_post_t == 0 ? 1.0 : t / p_post_t);
  398. return Math::lerp(b1, b2, p_to_t == 0 ? 0.5 : t / p_to_t);
  399. }
  400. inline float cubic_interpolate_in_time(float p_from, float p_to, float p_pre, float p_post, float p_weight,
  401. float p_to_t, float p_pre_t, float p_post_t) {
  402. /* Barry-Goldman method */
  403. float t = Math::lerp(0.0f, p_to_t, p_weight);
  404. float a1 = Math::lerp(p_pre, p_from, p_pre_t == 0 ? 0.0f : (t - p_pre_t) / -p_pre_t);
  405. float a2 = Math::lerp(p_from, p_to, p_to_t == 0 ? 0.5f : t / p_to_t);
  406. 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));
  407. 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));
  408. float b2 = Math::lerp(a2, a3, p_post_t == 0 ? 1.0f : t / p_post_t);
  409. return Math::lerp(b1, b2, p_to_t == 0 ? 0.5f : t / p_to_t);
  410. }
  411. inline double cubic_interpolate_angle_in_time(double p_from, double p_to, double p_pre, double p_post, double p_weight,
  412. double p_to_t, double p_pre_t, double p_post_t) {
  413. double from_rot = fmod(p_from, Math_TAU);
  414. double pre_diff = fmod(p_pre - from_rot, Math_TAU);
  415. double pre_rot = from_rot + fmod(2.0 * pre_diff, Math_TAU) - pre_diff;
  416. double to_diff = fmod(p_to - from_rot, Math_TAU);
  417. double to_rot = from_rot + fmod(2.0 * to_diff, Math_TAU) - to_diff;
  418. double post_diff = fmod(p_post - to_rot, Math_TAU);
  419. double post_rot = to_rot + fmod(2.0 * post_diff, Math_TAU) - post_diff;
  420. return cubic_interpolate_in_time(from_rot, to_rot, pre_rot, post_rot, p_weight, p_to_t, p_pre_t, p_post_t);
  421. }
  422. inline float cubic_interpolate_angle_in_time(float p_from, float p_to, float p_pre, float p_post, float p_weight,
  423. float p_to_t, float p_pre_t, float p_post_t) {
  424. float from_rot = fmod(p_from, (float)Math_TAU);
  425. float pre_diff = fmod(p_pre - from_rot, (float)Math_TAU);
  426. float pre_rot = from_rot + fmod(2.0f * pre_diff, (float)Math_TAU) - pre_diff;
  427. float to_diff = fmod(p_to - from_rot, (float)Math_TAU);
  428. float to_rot = from_rot + fmod(2.0f * to_diff, (float)Math_TAU) - to_diff;
  429. float post_diff = fmod(p_post - to_rot, (float)Math_TAU);
  430. float post_rot = to_rot + fmod(2.0f * post_diff, (float)Math_TAU) - post_diff;
  431. return cubic_interpolate_in_time(from_rot, to_rot, pre_rot, post_rot, p_weight, p_to_t, p_pre_t, p_post_t);
  432. }
  433. inline double bezier_interpolate(double p_start, double p_control_1, double p_control_2, double p_end, double p_t) {
  434. /* Formula from Wikipedia article on Bezier curves. */
  435. double omt = (1.0 - p_t);
  436. double omt2 = omt * omt;
  437. double omt3 = omt2 * omt;
  438. double t2 = p_t * p_t;
  439. double t3 = t2 * p_t;
  440. return p_start * omt3 + p_control_1 * omt2 * p_t * 3.0 + p_control_2 * omt * t2 * 3.0 + p_end * t3;
  441. }
  442. inline float bezier_interpolate(float p_start, float p_control_1, float p_control_2, float p_end, float p_t) {
  443. /* Formula from Wikipedia article on Bezier curves. */
  444. float omt = (1.0f - p_t);
  445. float omt2 = omt * omt;
  446. float omt3 = omt2 * omt;
  447. float t2 = p_t * p_t;
  448. float t3 = t2 * p_t;
  449. return p_start * omt3 + p_control_1 * omt2 * p_t * 3.0f + p_control_2 * omt * t2 * 3.0f + p_end * t3;
  450. }
  451. inline double bezier_derivative(double p_start, double p_control_1, double p_control_2, double p_end, double p_t) {
  452. /* Formula from Wikipedia article on Bezier curves. */
  453. double omt = (1.0 - p_t);
  454. double omt2 = omt * omt;
  455. double t2 = p_t * p_t;
  456. 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;
  457. return d;
  458. }
  459. inline float bezier_derivative(float p_start, float p_control_1, float p_control_2, float p_end, float p_t) {
  460. /* Formula from Wikipedia article on Bezier curves. */
  461. float omt = (1.0f - p_t);
  462. float omt2 = omt * omt;
  463. float t2 = p_t * p_t;
  464. 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;
  465. return d;
  466. }
  467. template <typename T>
  468. inline T clamp(T x, T minv, T maxv) {
  469. if (x < minv) {
  470. return minv;
  471. }
  472. if (x > maxv) {
  473. return maxv;
  474. }
  475. return x;
  476. }
  477. template <typename T>
  478. inline T min(T a, T b) {
  479. return a < b ? a : b;
  480. }
  481. template <typename T>
  482. inline T max(T a, T b) {
  483. return a > b ? a : b;
  484. }
  485. template <typename T>
  486. inline T sign(T x) {
  487. return static_cast<T>(SIGN(x));
  488. }
  489. template <typename T>
  490. inline T abs(T x) {
  491. return std::abs(x);
  492. }
  493. inline double deg_to_rad(double p_y) {
  494. return p_y * Math_PI / 180.0;
  495. }
  496. inline float deg_to_rad(float p_y) {
  497. return p_y * static_cast<float>(Math_PI) / 180.f;
  498. }
  499. inline double rad_to_deg(double p_y) {
  500. return p_y * 180.0 / Math_PI;
  501. }
  502. inline float rad_to_deg(float p_y) {
  503. return p_y * 180.f / static_cast<float>(Math_PI);
  504. }
  505. inline double inverse_lerp(double p_from, double p_to, double p_value) {
  506. return (p_value - p_from) / (p_to - p_from);
  507. }
  508. inline float inverse_lerp(float p_from, float p_to, float p_value) {
  509. return (p_value - p_from) / (p_to - p_from);
  510. }
  511. inline double remap(double p_value, double p_istart, double p_istop, double p_ostart, double p_ostop) {
  512. return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value));
  513. }
  514. inline float remap(float p_value, float p_istart, float p_istop, float p_ostart, float p_ostop) {
  515. return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value));
  516. }
  517. inline bool is_nan(float p_val) {
  518. return std::isnan(p_val);
  519. }
  520. inline bool is_nan(double p_val) {
  521. return std::isnan(p_val);
  522. }
  523. inline bool is_inf(float p_val) {
  524. return std::isinf(p_val);
  525. }
  526. inline bool is_inf(double p_val) {
  527. return std::isinf(p_val);
  528. }
  529. inline bool is_finite(float p_val) {
  530. return std::isfinite(p_val);
  531. }
  532. inline bool is_finite(double p_val) {
  533. return std::isfinite(p_val);
  534. }
  535. inline bool is_equal_approx(float a, float b) {
  536. // Check for exact equality first, required to handle "infinity" values.
  537. if (a == b) {
  538. return true;
  539. }
  540. // Then check for approximate equality.
  541. float tolerance = (float)CMP_EPSILON * abs(a);
  542. if (tolerance < (float)CMP_EPSILON) {
  543. tolerance = (float)CMP_EPSILON;
  544. }
  545. return abs(a - b) < tolerance;
  546. }
  547. inline bool is_equal_approx(float a, float b, float tolerance) {
  548. // Check for exact equality first, required to handle "infinity" values.
  549. if (a == b) {
  550. return true;
  551. }
  552. // Then check for approximate equality.
  553. return abs(a - b) < tolerance;
  554. }
  555. inline bool is_zero_approx(float s) {
  556. return abs(s) < (float)CMP_EPSILON;
  557. }
  558. inline bool is_equal_approx(double a, double b) {
  559. // Check for exact equality first, required to handle "infinity" values.
  560. if (a == b) {
  561. return true;
  562. }
  563. // Then check for approximate equality.
  564. double tolerance = CMP_EPSILON * abs(a);
  565. if (tolerance < CMP_EPSILON) {
  566. tolerance = CMP_EPSILON;
  567. }
  568. return abs(a - b) < tolerance;
  569. }
  570. inline bool is_equal_approx(double a, double b, double tolerance) {
  571. // Check for exact equality first, required to handle "infinity" values.
  572. if (a == b) {
  573. return true;
  574. }
  575. // Then check for approximate equality.
  576. return abs(a - b) < tolerance;
  577. }
  578. inline bool is_zero_approx(double s) {
  579. return abs(s) < CMP_EPSILON;
  580. }
  581. inline float absf(float g) {
  582. union {
  583. float f;
  584. uint32_t i;
  585. } u;
  586. u.f = g;
  587. u.i &= 2147483647u;
  588. return u.f;
  589. }
  590. inline double absd(double g) {
  591. union {
  592. double d;
  593. uint64_t i;
  594. } u;
  595. u.d = g;
  596. u.i &= (uint64_t)9223372036854775807ull;
  597. return u.d;
  598. }
  599. inline double smoothstep(double p_from, double p_to, double p_weight) {
  600. if (is_equal_approx(static_cast<real_t>(p_from), static_cast<real_t>(p_to))) {
  601. return p_from;
  602. }
  603. double x = clamp((p_weight - p_from) / (p_to - p_from), 0.0, 1.0);
  604. return x * x * (3.0 - 2.0 * x);
  605. }
  606. inline float smoothstep(float p_from, float p_to, float p_weight) {
  607. if (is_equal_approx(p_from, p_to)) {
  608. return p_from;
  609. }
  610. float x = clamp((p_weight - p_from) / (p_to - p_from), 0.0f, 1.0f);
  611. return x * x * (3.0f - 2.0f * x);
  612. }
  613. inline double move_toward(double p_from, double p_to, double p_delta) {
  614. return std::abs(p_to - p_from) <= p_delta ? p_to : p_from + sign(p_to - p_from) * p_delta;
  615. }
  616. inline float move_toward(float p_from, float p_to, float p_delta) {
  617. return std::abs(p_to - p_from) <= p_delta ? p_to : p_from + sign(p_to - p_from) * p_delta;
  618. }
  619. inline double linear2db(double p_linear) {
  620. return log(p_linear) * 8.6858896380650365530225783783321;
  621. }
  622. inline float linear2db(float p_linear) {
  623. return log(p_linear) * 8.6858896380650365530225783783321f;
  624. }
  625. inline double db2linear(double p_db) {
  626. return exp(p_db * 0.11512925464970228420089957273422);
  627. }
  628. inline float db2linear(float p_db) {
  629. return exp(p_db * 0.11512925464970228420089957273422f);
  630. }
  631. inline double round(double p_val) {
  632. return (p_val >= 0) ? floor(p_val + 0.5) : -floor(-p_val + 0.5);
  633. }
  634. inline float round(float p_val) {
  635. return (p_val >= 0) ? floor(p_val + 0.5f) : -floor(-p_val + 0.5f);
  636. }
  637. inline int64_t wrapi(int64_t value, int64_t min, int64_t max) {
  638. int64_t range = max - min;
  639. return range == 0 ? min : min + ((((value - min) % range) + range) % range);
  640. }
  641. inline float wrapf(real_t value, real_t min, real_t max) {
  642. const real_t range = max - min;
  643. return is_zero_approx(range) ? min : value - (range * floor((value - min) / range));
  644. }
  645. inline float fract(float value) {
  646. return value - floor(value);
  647. }
  648. inline double fract(double value) {
  649. return value - floor(value);
  650. }
  651. inline float pingpong(float value, float length) {
  652. return (length != 0.0f) ? abs(fract((value - length) / (length * 2.0f)) * length * 2.0f - length) : 0.0f;
  653. }
  654. inline double pingpong(double value, double length) {
  655. return (length != 0.0) ? abs(fract((value - length) / (length * 2.0)) * length * 2.0 - length) : 0.0;
  656. }
  657. // This function should be as fast as possible and rounding mode should not matter.
  658. inline int fast_ftoi(float a) {
  659. static int b;
  660. #if (defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0603) || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP // windows 8 phone?
  661. b = (int)((a > 0.0) ? (a + 0.5) : (a - 0.5));
  662. #elif defined(_MSC_VER) && _MSC_VER < 1800
  663. __asm fld a __asm fistp b
  664. /*#elif defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) )
  665. // use AT&T inline assembly style, document that
  666. // we use memory as output (=m) and input (m)
  667. __asm__ __volatile__ (
  668. "flds %1 \n\t"
  669. "fistpl %0 \n\t"
  670. : "=m" (b)
  671. : "m" (a));*/
  672. #else
  673. b = lrintf(a); // assuming everything but msvc 2012 or earlier has lrint
  674. #endif
  675. return b;
  676. }
  677. inline double snapped(double p_value, double p_step) {
  678. if (p_step != 0) {
  679. p_value = Math::floor(p_value / p_step + 0.5) * p_step;
  680. }
  681. return p_value;
  682. }
  683. inline float snap_scalar(float p_offset, float p_step, float p_target) {
  684. return p_step != 0 ? Math::snapped(p_target - p_offset, p_step) + p_offset : p_target;
  685. }
  686. inline float snap_scalar_separation(float p_offset, float p_step, float p_target, float p_separation) {
  687. if (p_step != 0) {
  688. float a = Math::snapped(p_target - p_offset, p_step + p_separation) + p_offset;
  689. float b = a;
  690. if (p_target >= 0) {
  691. b -= p_separation;
  692. } else {
  693. b += p_step;
  694. }
  695. return (Math::abs(p_target - a) < Math::abs(p_target - b)) ? a : b;
  696. }
  697. return p_target;
  698. }
  699. } // namespace Math
  700. } // namespace godot