math.hpp 23 KB

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