math.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*************************************************************************/
  2. /* math.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 <godot/gdnative_interface.h>
  34. #include <cmath>
  35. namespace godot {
  36. namespace Math {
  37. // This epsilon should match the one used by Godot for consistency.
  38. // Using `f` when `real_t` is float.
  39. #define CMP_EPSILON 0.00001f
  40. #define CMP_EPSILON2 (CMP_EPSILON * CMP_EPSILON)
  41. // This epsilon is for values related to a unit size (scalar or vector len).
  42. #ifdef PRECISE_MATH_CHECKS
  43. #define UNIT_EPSILON 0.00001
  44. #else
  45. // Tolerate some more floating point error normally.
  46. #define UNIT_EPSILON 0.001
  47. #endif
  48. #define Math_SQRT12 0.7071067811865475244008443621048490
  49. #define Math_SQRT2 1.4142135623730950488016887242
  50. #define Math_LN2 0.6931471805599453094172321215
  51. #define Math_PI 3.1415926535897932384626433833
  52. #define Math_TAU 6.2831853071795864769252867666
  53. #define Math_E 2.7182818284590452353602874714
  54. #define Math_INF INFINITY
  55. #define Math_NAN NAN
  56. // Windows badly defines a lot of stuff we'll never use. Undefine it.
  57. #ifdef _WIN32
  58. #undef MIN // override standard definition
  59. #undef MAX // override standard definition
  60. #undef CLAMP // override standard definition
  61. #endif
  62. // Generic ABS function, for math uses please use Math::abs.
  63. #ifndef ABS
  64. #define ABS(m_v) (((m_v) < 0) ? (-(m_v)) : (m_v))
  65. #endif
  66. #ifndef SIGN
  67. #define SIGN(m_v) (((m_v) == 0) ? (0.0) : (((m_v) < 0) ? (-1.0) : (+1.0)))
  68. #endif
  69. #ifndef MIN
  70. #define MIN(m_a, m_b) (((m_a) < (m_b)) ? (m_a) : (m_b))
  71. #endif
  72. #ifndef MAX
  73. #define MAX(m_a, m_b) (((m_a) > (m_b)) ? (m_a) : (m_b))
  74. #endif
  75. #ifndef CLAMP
  76. #define CLAMP(m_a, m_min, m_max) (((m_a) < (m_min)) ? (m_min) : (((m_a) > (m_max)) ? m_max : m_a))
  77. #endif
  78. // Functions reproduced as in Godot's source code `math_funcs.h`.
  79. // Some are overloads to automatically support changing real_t into either double or float in the way Godot does.
  80. inline double fmod(double p_x, double p_y) {
  81. return ::fmod(p_x, p_y);
  82. }
  83. inline float fmod(float p_x, float p_y) {
  84. return ::fmodf(p_x, p_y);
  85. }
  86. inline double fposmod(double p_x, double p_y) {
  87. double value = Math::fmod(p_x, p_y);
  88. if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
  89. value += p_y;
  90. }
  91. value += 0.0;
  92. return value;
  93. }
  94. inline float fposmod(float p_x, float p_y) {
  95. float value = Math::fmod(p_x, p_y);
  96. if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
  97. value += p_y;
  98. }
  99. value += 0.0;
  100. return value;
  101. }
  102. inline float fposmodp(float p_x, float p_y) {
  103. float value = Math::fmod(p_x, p_y);
  104. if (value < 0) {
  105. value += p_y;
  106. }
  107. value += 0.0;
  108. return value;
  109. }
  110. inline double fposmodp(double p_x, double p_y) {
  111. double value = Math::fmod(p_x, p_y);
  112. if (value < 0) {
  113. value += p_y;
  114. }
  115. value += 0.0;
  116. return value;
  117. }
  118. inline double floor(double p_x) {
  119. return ::floor(p_x);
  120. }
  121. inline float floor(float p_x) {
  122. return ::floorf(p_x);
  123. }
  124. inline double ceil(double p_x) {
  125. return ::ceil(p_x);
  126. }
  127. inline float ceil(float p_x) {
  128. return ::ceilf(p_x);
  129. }
  130. inline double exp(double p_x) {
  131. return ::exp(p_x);
  132. }
  133. inline float exp(float p_x) {
  134. return ::expf(p_x);
  135. }
  136. inline double sin(double p_x) {
  137. return ::sin(p_x);
  138. }
  139. inline float sin(float p_x) {
  140. return ::sinf(p_x);
  141. }
  142. inline double cos(double p_x) {
  143. return ::cos(p_x);
  144. }
  145. inline float cos(float p_x) {
  146. return ::cosf(p_x);
  147. }
  148. inline double tan(double p_x) {
  149. return ::tan(p_x);
  150. }
  151. inline float tan(float p_x) {
  152. return ::tanf(p_x);
  153. }
  154. inline double sinh(double p_x) {
  155. return ::sinh(p_x);
  156. }
  157. inline float sinh(float p_x) {
  158. return ::sinhf(p_x);
  159. }
  160. inline float sinc(float p_x) {
  161. return p_x == 0 ? 1 : ::sin(p_x) / p_x;
  162. }
  163. inline double sinc(double p_x) {
  164. return p_x == 0 ? 1 : ::sin(p_x) / p_x;
  165. }
  166. inline float sincn(float p_x) {
  167. return (float)sinc(Math_PI * p_x);
  168. }
  169. inline double sincn(double p_x) {
  170. return sinc(Math_PI * p_x);
  171. }
  172. inline double cosh(double p_x) {
  173. return ::cosh(p_x);
  174. }
  175. inline float cosh(float p_x) {
  176. return ::coshf(p_x);
  177. }
  178. inline double tanh(double p_x) {
  179. return ::tanh(p_x);
  180. }
  181. inline float tanh(float p_x) {
  182. return ::tanhf(p_x);
  183. }
  184. inline double asin(double p_x) {
  185. return ::asin(p_x);
  186. }
  187. inline float asin(float p_x) {
  188. return ::asinf(p_x);
  189. }
  190. inline double acos(double p_x) {
  191. return ::acos(p_x);
  192. }
  193. inline float acos(float p_x) {
  194. return ::acosf(p_x);
  195. }
  196. inline double atan(double p_x) {
  197. return ::atan(p_x);
  198. }
  199. inline float atan(float p_x) {
  200. return ::atanf(p_x);
  201. }
  202. inline double atan2(double p_y, double p_x) {
  203. return ::atan2(p_y, p_x);
  204. }
  205. inline float atan2(float p_y, float p_x) {
  206. return ::atan2f(p_y, p_x);
  207. }
  208. inline double sqrt(double p_x) {
  209. return ::sqrt(p_x);
  210. }
  211. inline float sqrt(float p_x) {
  212. return ::sqrtf(p_x);
  213. }
  214. inline double pow(double p_x, double p_y) {
  215. return ::pow(p_x, p_y);
  216. }
  217. inline float pow(float p_x, float p_y) {
  218. return ::powf(p_x, p_y);
  219. }
  220. inline double log(double p_x) {
  221. return ::log(p_x);
  222. }
  223. inline float log(float p_x) {
  224. return ::logf(p_x);
  225. }
  226. inline float lerp(float minv, float maxv, float t) {
  227. return minv + t * (maxv - minv);
  228. }
  229. inline double lerp(double minv, double maxv, double t) {
  230. return minv + t * (maxv - minv);
  231. }
  232. inline double lerp_angle(double p_from, double p_to, double p_weight) {
  233. double difference = fmod(p_to - p_from, Math_TAU);
  234. double distance = fmod(2.0 * difference, Math_TAU) - difference;
  235. return p_from + distance * p_weight;
  236. }
  237. inline float lerp_angle(float p_from, float p_to, float p_weight) {
  238. float difference = fmod(p_to - p_from, (float)Math_TAU);
  239. float distance = fmod(2.0f * difference, (float)Math_TAU) - difference;
  240. return p_from + distance * p_weight;
  241. }
  242. inline double cubic_interpolate(double p_from, double p_to, double p_pre, double p_post, double p_weight) {
  243. return 0.5 *
  244. ((p_from * 2.0) +
  245. (-p_pre + p_to) * p_weight +
  246. (2.0 * p_pre - 5.0 * p_from + 4.0 * p_to - p_post) * (p_weight * p_weight) +
  247. (-p_pre + 3.0 * p_from - 3.0 * p_to + p_post) * (p_weight * p_weight * p_weight));
  248. }
  249. inline float cubic_interpolate(float p_from, float p_to, float p_pre, float p_post, float p_weight) {
  250. return 0.5f *
  251. ((p_from * 2.0f) +
  252. (-p_pre + p_to) * p_weight +
  253. (2.0f * p_pre - 5.0f * p_from + 4.0f * p_to - p_post) * (p_weight * p_weight) +
  254. (-p_pre + 3.0f * p_from - 3.0f * p_to + p_post) * (p_weight * p_weight * p_weight));
  255. }
  256. template <typename T>
  257. inline T clamp(T x, T minv, T maxv) {
  258. if (x < minv) {
  259. return minv;
  260. }
  261. if (x > maxv) {
  262. return maxv;
  263. }
  264. return x;
  265. }
  266. template <typename T>
  267. inline T min(T a, T b) {
  268. return a < b ? a : b;
  269. }
  270. template <typename T>
  271. inline T max(T a, T b) {
  272. return a > b ? a : b;
  273. }
  274. template <typename T>
  275. inline T sign(T x) {
  276. return static_cast<T>(x < 0 ? -1 : 1);
  277. }
  278. template <typename T>
  279. inline T abs(T x) {
  280. return std::abs(x);
  281. }
  282. inline double deg2rad(double p_y) {
  283. return p_y * Math_PI / 180.0;
  284. }
  285. inline float deg2rad(float p_y) {
  286. return p_y * static_cast<float>(Math_PI) / 180.f;
  287. }
  288. inline double rad2deg(double p_y) {
  289. return p_y * 180.0 / Math_PI;
  290. }
  291. inline float rad2deg(float p_y) {
  292. return p_y * 180.f / static_cast<float>(Math_PI);
  293. }
  294. inline double inverse_lerp(double p_from, double p_to, double p_value) {
  295. return (p_value - p_from) / (p_to - p_from);
  296. }
  297. inline float inverse_lerp(float p_from, float p_to, float p_value) {
  298. return (p_value - p_from) / (p_to - p_from);
  299. }
  300. inline double range_lerp(double p_value, double p_istart, double p_istop, double p_ostart, double p_ostop) {
  301. return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value));
  302. }
  303. inline float range_lerp(float p_value, float p_istart, float p_istop, float p_ostart, float p_ostop) {
  304. return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value));
  305. }
  306. inline bool is_nan(float p_val) {
  307. return std::isnan(p_val);
  308. }
  309. inline bool is_nan(double p_val) {
  310. return std::isnan(p_val);
  311. }
  312. inline bool is_inf(float p_val) {
  313. return std::isinf(p_val);
  314. }
  315. inline bool is_inf(double p_val) {
  316. return std::isinf(p_val);
  317. }
  318. inline bool is_equal_approx(float a, float b) {
  319. // Check for exact equality first, required to handle "infinity" values.
  320. if (a == b) {
  321. return true;
  322. }
  323. // Then check for approximate equality.
  324. float tolerance = (float)CMP_EPSILON * abs(a);
  325. if (tolerance < (float)CMP_EPSILON) {
  326. tolerance = (float)CMP_EPSILON;
  327. }
  328. return abs(a - b) < tolerance;
  329. }
  330. inline bool is_equal_approx(float a, float b, float tolerance) {
  331. // Check for exact equality first, required to handle "infinity" values.
  332. if (a == b) {
  333. return true;
  334. }
  335. // Then check for approximate equality.
  336. return abs(a - b) < tolerance;
  337. }
  338. inline bool is_zero_approx(float s) {
  339. return abs(s) < (float)CMP_EPSILON;
  340. }
  341. inline bool is_equal_approx(double a, double b) {
  342. // Check for exact equality first, required to handle "infinity" values.
  343. if (a == b) {
  344. return true;
  345. }
  346. // Then check for approximate equality.
  347. double tolerance = CMP_EPSILON * abs(a);
  348. if (tolerance < CMP_EPSILON) {
  349. tolerance = CMP_EPSILON;
  350. }
  351. return abs(a - b) < tolerance;
  352. }
  353. inline bool is_equal_approx(double a, double b, double tolerance) {
  354. // Check for exact equality first, required to handle "infinity" values.
  355. if (a == b) {
  356. return true;
  357. }
  358. // Then check for approximate equality.
  359. return abs(a - b) < tolerance;
  360. }
  361. inline bool is_zero_approx(double s) {
  362. return abs(s) < CMP_EPSILON;
  363. }
  364. inline double smoothstep(double p_from, double p_to, double p_weight) {
  365. if (is_equal_approx(static_cast<real_t>(p_from), static_cast<real_t>(p_to))) {
  366. return p_from;
  367. }
  368. double x = clamp((p_weight - p_from) / (p_to - p_from), 0.0, 1.0);
  369. return x * x * (3.0 - 2.0 * x);
  370. }
  371. inline float smoothstep(float p_from, float p_to, float p_weight) {
  372. if (is_equal_approx(p_from, p_to)) {
  373. return p_from;
  374. }
  375. float x = clamp((p_weight - p_from) / (p_to - p_from), 0.0f, 1.0f);
  376. return x * x * (3.0f - 2.0f * x);
  377. }
  378. inline double move_toward(double p_from, double p_to, double p_delta) {
  379. return std::abs(p_to - p_from) <= p_delta ? p_to : p_from + sign(p_to - p_from) * p_delta;
  380. }
  381. inline float move_toward(float p_from, float p_to, float p_delta) {
  382. return std::abs(p_to - p_from) <= p_delta ? p_to : p_from + sign(p_to - p_from) * p_delta;
  383. }
  384. inline double linear2db(double p_linear) {
  385. return log(p_linear) * 8.6858896380650365530225783783321;
  386. }
  387. inline float linear2db(float p_linear) {
  388. return log(p_linear) * 8.6858896380650365530225783783321f;
  389. }
  390. inline double db2linear(double p_db) {
  391. return exp(p_db * 0.11512925464970228420089957273422);
  392. }
  393. inline float db2linear(float p_db) {
  394. return exp(p_db * 0.11512925464970228420089957273422f);
  395. }
  396. inline double round(double p_val) {
  397. return (p_val >= 0) ? floor(p_val + 0.5) : -floor(-p_val + 0.5);
  398. }
  399. inline float round(float p_val) {
  400. return (p_val >= 0) ? floor(p_val + 0.5f) : -floor(-p_val + 0.5f);
  401. }
  402. inline int64_t wrapi(int64_t value, int64_t min, int64_t max) {
  403. int64_t range = max - min;
  404. return range == 0 ? min : min + ((((value - min) % range) + range) % range);
  405. }
  406. inline float wrapf(real_t value, real_t min, real_t max) {
  407. const real_t range = max - min;
  408. return is_zero_approx(range) ? min : value - (range * floor((value - min) / range));
  409. }
  410. inline float stepify(float p_value, float p_step) {
  411. if (p_step != 0) {
  412. p_value = floor(p_value / p_step + 0.5f) * p_step;
  413. }
  414. return p_value;
  415. }
  416. inline double stepify(double p_value, double p_step) {
  417. if (p_step != 0) {
  418. p_value = floor(p_value / p_step + 0.5) * p_step;
  419. }
  420. return p_value;
  421. }
  422. inline unsigned int next_power_of_2(unsigned int x) {
  423. if (x == 0)
  424. return 0;
  425. --x;
  426. x |= x >> 1;
  427. x |= x >> 2;
  428. x |= x >> 4;
  429. x |= x >> 8;
  430. x |= x >> 16;
  431. return ++x;
  432. }
  433. // This function should be as fast as possible and rounding mode should not matter.
  434. inline int fast_ftoi(float a) {
  435. static int b;
  436. #if (defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0603) || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP // windows 8 phone?
  437. b = (int)((a > 0.0) ? (a + 0.5) : (a - 0.5));
  438. #elif defined(_MSC_VER) && _MSC_VER < 1800
  439. __asm fld a __asm fistp b
  440. /*#elif defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) )
  441. // use AT&T inline assembly style, document that
  442. // we use memory as output (=m) and input (m)
  443. __asm__ __volatile__ (
  444. "flds %1 \n\t"
  445. "fistpl %0 \n\t"
  446. : "=m" (b)
  447. : "m" (a));*/
  448. #else
  449. b = lrintf(a); // assuming everything but msvc 2012 or earlier has lrint
  450. #endif
  451. return b;
  452. }
  453. inline double snapped(double p_value, double p_step) {
  454. if (p_step != 0) {
  455. p_value = Math::floor(p_value / p_step + 0.5) * p_step;
  456. }
  457. return p_value;
  458. }
  459. } // namespace Math
  460. } // namespace godot
  461. #endif // GODOT_MATH_HPP