2
0

math.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*************************************************************************/
  2. /* math.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_H
  31. #define GODOT_MATH_H
  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. // Functions reproduced as in Godot's source code `math_funcs.h`.
  57. // Some are overloads to automatically support changing real_t into either double or float in the way Godot does.
  58. inline double fmod(double p_x, double p_y) {
  59. return ::fmod(p_x, p_y);
  60. }
  61. inline float fmod(float p_x, float p_y) {
  62. return ::fmodf(p_x, p_y);
  63. }
  64. inline double fposmod(double p_x, double p_y) {
  65. double value = Math::fmod(p_x, p_y);
  66. if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
  67. value += p_y;
  68. }
  69. value += 0.0;
  70. return value;
  71. }
  72. inline float fposmod(float p_x, float p_y) {
  73. float value = Math::fmod(p_x, p_y);
  74. if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
  75. value += p_y;
  76. }
  77. value += 0.0;
  78. return value;
  79. }
  80. inline float fposmodp(float p_x, float p_y) {
  81. float value = Math::fmod(p_x, p_y);
  82. if (value < 0) {
  83. value += p_y;
  84. }
  85. value += 0.0;
  86. return value;
  87. }
  88. inline double fposmodp(double p_x, double p_y) {
  89. double value = Math::fmod(p_x, p_y);
  90. if (value < 0) {
  91. value += p_y;
  92. }
  93. value += 0.0;
  94. return value;
  95. }
  96. inline double floor(double p_x) {
  97. return ::floor(p_x);
  98. }
  99. inline float floor(float p_x) {
  100. return ::floorf(p_x);
  101. }
  102. inline double ceil(double p_x) {
  103. return ::ceil(p_x);
  104. }
  105. inline float ceil(float p_x) {
  106. return ::ceilf(p_x);
  107. }
  108. inline double exp(double p_x) {
  109. return ::exp(p_x);
  110. }
  111. inline float exp(float p_x) {
  112. return ::expf(p_x);
  113. }
  114. inline double sin(double p_x) {
  115. return ::sin(p_x);
  116. }
  117. inline float sin(float p_x) {
  118. return ::sinf(p_x);
  119. }
  120. inline double cos(double p_x) {
  121. return ::cos(p_x);
  122. }
  123. inline float cos(float p_x) {
  124. return ::cosf(p_x);
  125. }
  126. inline double tan(double p_x) {
  127. return ::tan(p_x);
  128. }
  129. inline float tan(float p_x) {
  130. return ::tanf(p_x);
  131. }
  132. inline double sinh(double p_x) {
  133. return ::sinh(p_x);
  134. }
  135. inline float sinh(float p_x) {
  136. return ::sinhf(p_x);
  137. }
  138. inline float sinc(float p_x) {
  139. return p_x == 0 ? 1 : ::sin(p_x) / p_x;
  140. }
  141. inline double sinc(double p_x) {
  142. return p_x == 0 ? 1 : ::sin(p_x) / p_x;
  143. }
  144. inline float sincn(float p_x) {
  145. return sinc(Math_PI * p_x);
  146. }
  147. inline double sincn(double p_x) {
  148. return sinc(Math_PI * p_x);
  149. }
  150. inline double cosh(double p_x) {
  151. return ::cosh(p_x);
  152. }
  153. inline float cosh(float p_x) {
  154. return ::coshf(p_x);
  155. }
  156. inline double tanh(double p_x) {
  157. return ::tanh(p_x);
  158. }
  159. inline float tanh(float p_x) {
  160. return ::tanhf(p_x);
  161. }
  162. inline double asin(double p_x) {
  163. return ::asin(p_x);
  164. }
  165. inline float asin(float p_x) {
  166. return ::asinf(p_x);
  167. }
  168. inline double acos(double p_x) {
  169. return ::acos(p_x);
  170. }
  171. inline float acos(float p_x) {
  172. return ::acosf(p_x);
  173. }
  174. inline double atan(double p_x) {
  175. return ::atan(p_x);
  176. }
  177. inline float atan(float p_x) {
  178. return ::atanf(p_x);
  179. }
  180. inline double atan2(double p_y, double p_x) {
  181. return ::atan2(p_y, p_x);
  182. }
  183. inline float atan2(float p_y, float p_x) {
  184. return ::atan2f(p_y, p_x);
  185. }
  186. inline double sqrt(double p_x) {
  187. return ::sqrt(p_x);
  188. }
  189. inline float sqrt(float p_x) {
  190. return ::sqrtf(p_x);
  191. }
  192. inline double pow(double p_x, double p_y) {
  193. return ::pow(p_x, p_y);
  194. }
  195. inline float pow(float p_x, float p_y) {
  196. return ::powf(p_x, p_y);
  197. }
  198. inline double log(double p_x) {
  199. return ::log(p_x);
  200. }
  201. inline float log(float p_x) {
  202. return ::logf(p_x);
  203. }
  204. inline float lerp(float minv, float maxv, float t) {
  205. return minv + t * (maxv - minv);
  206. }
  207. inline double lerp(double minv, double maxv, double t) {
  208. return minv + t * (maxv - minv);
  209. }
  210. inline double lerp_angle(double p_from, double p_to, double p_weight) {
  211. double difference = fmod(p_to - p_from, Math_TAU);
  212. double distance = fmod(2.0 * difference, Math_TAU) - difference;
  213. return p_from + distance * p_weight;
  214. }
  215. inline float lerp_angle(float p_from, float p_to, float p_weight) {
  216. float difference = fmod(p_to - p_from, (float)Math_TAU);
  217. float distance = fmod(2.0f * difference, (float)Math_TAU) - difference;
  218. return p_from + distance * p_weight;
  219. }
  220. template <typename T>
  221. inline T clamp(T x, T minv, T maxv) {
  222. if (x < minv) {
  223. return minv;
  224. }
  225. if (x > maxv) {
  226. return maxv;
  227. }
  228. return x;
  229. }
  230. template <typename T>
  231. inline T min(T a, T b) {
  232. return a < b ? a : b;
  233. }
  234. template <typename T>
  235. inline T max(T a, T b) {
  236. return a > b ? a : b;
  237. }
  238. template <typename T>
  239. inline T sign(T x) {
  240. return static_cast<T>(x < 0 ? -1 : 1);
  241. }
  242. template <typename T>
  243. inline T abs(T x) {
  244. return std::abs(x);
  245. }
  246. inline double deg2rad(double p_y) {
  247. return p_y * Math_PI / 180.0;
  248. }
  249. inline float deg2rad(float p_y) {
  250. return p_y * static_cast<float>(Math_PI) / 180.f;
  251. }
  252. inline double rad2deg(double p_y) {
  253. return p_y * 180.0 / Math_PI;
  254. }
  255. inline float rad2deg(float p_y) {
  256. return p_y * 180.f / static_cast<float>(Math_PI);
  257. }
  258. inline double inverse_lerp(double p_from, double p_to, double p_value) {
  259. return (p_value - p_from) / (p_to - p_from);
  260. }
  261. inline float inverse_lerp(float p_from, float p_to, float p_value) {
  262. return (p_value - p_from) / (p_to - p_from);
  263. }
  264. inline double range_lerp(double p_value, double p_istart, double p_istop, double p_ostart, double p_ostop) {
  265. return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value));
  266. }
  267. inline float range_lerp(float p_value, float p_istart, float p_istop, float p_ostart, float p_ostop) {
  268. return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value));
  269. }
  270. inline bool is_equal_approx(real_t a, real_t b) {
  271. // Check for exact equality first, required to handle "infinity" values.
  272. if (a == b) {
  273. return true;
  274. }
  275. // Then check for approximate equality.
  276. real_t tolerance = CMP_EPSILON * std::abs(a);
  277. if (tolerance < CMP_EPSILON) {
  278. tolerance = CMP_EPSILON;
  279. }
  280. return std::abs(a - b) < tolerance;
  281. }
  282. inline bool is_equal_approx(real_t a, real_t b, real_t tolerance) {
  283. // Check for exact equality first, required to handle "infinity" values.
  284. if (a == b) {
  285. return true;
  286. }
  287. // Then check for approximate equality.
  288. return std::abs(a - b) < tolerance;
  289. }
  290. inline bool is_zero_approx(real_t s) {
  291. return std::abs(s) < CMP_EPSILON;
  292. }
  293. inline double smoothstep(double p_from, double p_to, double p_weight) {
  294. if (is_equal_approx(static_cast<real_t>(p_from), static_cast<real_t>(p_to))) {
  295. return p_from;
  296. }
  297. double x = clamp((p_weight - p_from) / (p_to - p_from), 0.0, 1.0);
  298. return x * x * (3.0 - 2.0 * x);
  299. }
  300. inline float smoothstep(float p_from, float p_to, float p_weight) {
  301. if (is_equal_approx(p_from, p_to)) {
  302. return p_from;
  303. }
  304. float x = clamp((p_weight - p_from) / (p_to - p_from), 0.0f, 1.0f);
  305. return x * x * (3.0f - 2.0f * x);
  306. }
  307. inline double move_toward(double p_from, double p_to, double p_delta) {
  308. return std::abs(p_to - p_from) <= p_delta ? p_to : p_from + sign(p_to - p_from) * p_delta;
  309. }
  310. inline float move_toward(float p_from, float p_to, float p_delta) {
  311. return std::abs(p_to - p_from) <= p_delta ? p_to : p_from + sign(p_to - p_from) * p_delta;
  312. }
  313. inline double linear2db(double p_linear) {
  314. return log(p_linear) * 8.6858896380650365530225783783321;
  315. }
  316. inline float linear2db(float p_linear) {
  317. return log(p_linear) * 8.6858896380650365530225783783321f;
  318. }
  319. inline double db2linear(double p_db) {
  320. return exp(p_db * 0.11512925464970228420089957273422);
  321. }
  322. inline float db2linear(float p_db) {
  323. return exp(p_db * 0.11512925464970228420089957273422f);
  324. }
  325. inline double round(double p_val) {
  326. return (p_val >= 0) ? floor(p_val + 0.5) : -floor(-p_val + 0.5);
  327. }
  328. inline float round(float p_val) {
  329. return (p_val >= 0) ? floor(p_val + 0.5f) : -floor(-p_val + 0.5f);
  330. }
  331. inline int64_t wrapi(int64_t value, int64_t min, int64_t max) {
  332. int64_t range = max - min;
  333. return range == 0 ? min : min + ((((value - min) % range) + range) % range);
  334. }
  335. inline float wrapf(real_t value, real_t min, real_t max) {
  336. const real_t range = max - min;
  337. return is_zero_approx(range) ? min : value - (range * floor((value - min) / range));
  338. }
  339. inline float stepify(float p_value, float p_step) {
  340. if (p_step != 0) {
  341. p_value = floor(p_value / p_step + 0.5f) * p_step;
  342. }
  343. return p_value;
  344. }
  345. inline double stepify(double p_value, double p_step) {
  346. if (p_step != 0) {
  347. p_value = floor(p_value / p_step + 0.5) * p_step;
  348. }
  349. return p_value;
  350. }
  351. inline unsigned int next_power_of_2(unsigned int x) {
  352. if (x == 0)
  353. return 0;
  354. --x;
  355. x |= x >> 1;
  356. x |= x >> 2;
  357. x |= x >> 4;
  358. x |= x >> 8;
  359. x |= x >> 16;
  360. return ++x;
  361. }
  362. // This function should be as fast as possible and rounding mode should not matter.
  363. inline int fast_ftoi(float a) {
  364. static int b;
  365. #if (defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0603) || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP // windows 8 phone?
  366. b = (int)((a > 0.0) ? (a + 0.5) : (a - 0.5));
  367. #elif defined(_MSC_VER) && _MSC_VER < 1800
  368. __asm fld a __asm fistp b
  369. /*#elif defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) )
  370. // use AT&T inline assembly style, document that
  371. // we use memory as output (=m) and input (m)
  372. __asm__ __volatile__ (
  373. "flds %1 \n\t"
  374. "fistpl %0 \n\t"
  375. : "=m" (b)
  376. : "m" (a));*/
  377. #else
  378. b = lrintf(a); //assuming everything but msvc 2012 or earlier has lrint
  379. #endif
  380. return b;
  381. }
  382. inline double snapped(double p_value, double p_step) {
  383. if (p_step != 0) {
  384. p_value = Math::floor(p_value / p_step + 0.5) * p_step;
  385. }
  386. return p_value;
  387. }
  388. } // namespace Math
  389. } // namespace godot
  390. #endif // GODOT_MATH_H