math.hpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. #ifndef GODOT_MATH_H
  2. #define GODOT_MATH_H
  3. #include <godot_cpp/core/defs.hpp>
  4. #include <godot/gdnative_interface.h>
  5. #include <cmath>
  6. namespace godot {
  7. namespace Math {
  8. // This epsilon should match the one used by Godot for consistency.
  9. // Using `f` when `real_t` is float.
  10. #define CMP_EPSILON 0.00001f
  11. #define CMP_EPSILON2 (CMP_EPSILON * CMP_EPSILON)
  12. // This epsilon is for values related to a unit size (scalar or vector len).
  13. #ifdef PRECISE_MATH_CHECKS
  14. #define UNIT_EPSILON 0.00001
  15. #else
  16. // Tolerate some more floating point error normally.
  17. #define UNIT_EPSILON 0.001
  18. #endif
  19. #define Math_SQRT12 0.7071067811865475244008443621048490
  20. #define Math_SQRT2 1.4142135623730950488016887242
  21. #define Math_LN2 0.6931471805599453094172321215
  22. #define Math_PI 3.1415926535897932384626433833
  23. #define Math_TAU 6.2831853071795864769252867666
  24. #define Math_E 2.7182818284590452353602874714
  25. #define Math_INF INFINITY
  26. #define Math_NAN NAN
  27. // Functions reproduced as in Godot's source code `math_funcs.h`.
  28. // Some are overloads to automatically support changing real_t into either double or float in the way Godot does.
  29. inline double fmod(double p_x, double p_y) {
  30. return ::fmod(p_x, p_y);
  31. }
  32. inline float fmod(float p_x, float p_y) {
  33. return ::fmodf(p_x, p_y);
  34. }
  35. inline double fposmod(double p_x, double p_y) {
  36. double value = Math::fmod(p_x, p_y);
  37. if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
  38. value += p_y;
  39. }
  40. value += 0.0;
  41. return value;
  42. }
  43. inline float fposmod(float p_x, float p_y) {
  44. float value = Math::fmod(p_x, p_y);
  45. if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
  46. value += p_y;
  47. }
  48. value += 0.0;
  49. return value;
  50. }
  51. inline float fposmodp(float p_x, float p_y) {
  52. float value = Math::fmod(p_x, p_y);
  53. if (value < 0) {
  54. value += p_y;
  55. }
  56. value += 0.0;
  57. return value;
  58. }
  59. inline double fposmodp(double p_x, double p_y) {
  60. double value = Math::fmod(p_x, p_y);
  61. if (value < 0) {
  62. value += p_y;
  63. }
  64. value += 0.0;
  65. return value;
  66. }
  67. inline double floor(double p_x) {
  68. return ::floor(p_x);
  69. }
  70. inline float floor(float p_x) {
  71. return ::floorf(p_x);
  72. }
  73. inline double ceil(double p_x) {
  74. return ::ceil(p_x);
  75. }
  76. inline float ceil(float p_x) {
  77. return ::ceilf(p_x);
  78. }
  79. inline double exp(double p_x) {
  80. return ::exp(p_x);
  81. }
  82. inline float exp(float p_x) {
  83. return ::expf(p_x);
  84. }
  85. inline double sin(double p_x) {
  86. return ::sin(p_x);
  87. }
  88. inline float sin(float p_x) {
  89. return ::sinf(p_x);
  90. }
  91. inline double cos(double p_x) {
  92. return ::cos(p_x);
  93. }
  94. inline float cos(float p_x) {
  95. return ::cosf(p_x);
  96. }
  97. inline double tan(double p_x) {
  98. return ::tan(p_x);
  99. }
  100. inline float tan(float p_x) {
  101. return ::tanf(p_x);
  102. }
  103. inline double sinh(double p_x) {
  104. return ::sinh(p_x);
  105. }
  106. inline float sinh(float p_x) {
  107. return ::sinhf(p_x);
  108. }
  109. inline float sinc(float p_x) {
  110. return p_x == 0 ? 1 : ::sin(p_x) / p_x;
  111. }
  112. inline double sinc(double p_x) {
  113. return p_x == 0 ? 1 : ::sin(p_x) / p_x;
  114. }
  115. inline float sincn(float p_x) {
  116. return sinc(Math_PI * p_x);
  117. }
  118. inline double sincn(double p_x) {
  119. return sinc(Math_PI * p_x);
  120. }
  121. inline double cosh(double p_x) {
  122. return ::cosh(p_x);
  123. }
  124. inline float cosh(float p_x) {
  125. return ::coshf(p_x);
  126. }
  127. inline double tanh(double p_x) {
  128. return ::tanh(p_x);
  129. }
  130. inline float tanh(float p_x) {
  131. return ::tanhf(p_x);
  132. }
  133. inline double asin(double p_x) {
  134. return ::asin(p_x);
  135. }
  136. inline float asin(float p_x) {
  137. return ::asinf(p_x);
  138. }
  139. inline double acos(double p_x) {
  140. return ::acos(p_x);
  141. }
  142. inline float acos(float p_x) {
  143. return ::acosf(p_x);
  144. }
  145. inline double atan(double p_x) {
  146. return ::atan(p_x);
  147. }
  148. inline float atan(float p_x) {
  149. return ::atanf(p_x);
  150. }
  151. inline double atan2(double p_y, double p_x) {
  152. return ::atan2(p_y, p_x);
  153. }
  154. inline float atan2(float p_y, float p_x) {
  155. return ::atan2f(p_y, p_x);
  156. }
  157. inline double sqrt(double p_x) {
  158. return ::sqrt(p_x);
  159. }
  160. inline float sqrt(float p_x) {
  161. return ::sqrtf(p_x);
  162. }
  163. inline double pow(double p_x, double p_y) {
  164. return ::pow(p_x, p_y);
  165. }
  166. inline float pow(float p_x, float p_y) {
  167. return ::powf(p_x, p_y);
  168. }
  169. inline double log(double p_x) {
  170. return ::log(p_x);
  171. }
  172. inline float log(float p_x) {
  173. return ::logf(p_x);
  174. }
  175. inline float lerp(float minv, float maxv, float t) {
  176. return minv + t * (maxv - minv);
  177. }
  178. inline double lerp(double minv, double maxv, double t) {
  179. return minv + t * (maxv - minv);
  180. }
  181. inline double lerp_angle(double p_from, double p_to, double p_weight) {
  182. double difference = fmod(p_to - p_from, Math_TAU);
  183. double distance = fmod(2.0 * difference, Math_TAU) - difference;
  184. return p_from + distance * p_weight;
  185. }
  186. inline float lerp_angle(float p_from, float p_to, float p_weight) {
  187. float difference = fmod(p_to - p_from, (float)Math_TAU);
  188. float distance = fmod(2.0f * difference, (float)Math_TAU) - difference;
  189. return p_from + distance * p_weight;
  190. }
  191. template <typename T>
  192. inline T clamp(T x, T minv, T maxv) {
  193. if (x < minv) {
  194. return minv;
  195. }
  196. if (x > maxv) {
  197. return maxv;
  198. }
  199. return x;
  200. }
  201. template <typename T>
  202. inline T min(T a, T b) {
  203. return a < b ? a : b;
  204. }
  205. template <typename T>
  206. inline T max(T a, T b) {
  207. return a > b ? a : b;
  208. }
  209. template <typename T>
  210. inline T sign(T x) {
  211. return static_cast<T>(x < 0 ? -1 : 1);
  212. }
  213. template <typename T>
  214. inline T abs(T x) {
  215. return std::abs(x);
  216. }
  217. inline double deg2rad(double p_y) {
  218. return p_y * Math_PI / 180.0;
  219. }
  220. inline float deg2rad(float p_y) {
  221. return p_y * static_cast<float>(Math_PI) / 180.f;
  222. }
  223. inline double rad2deg(double p_y) {
  224. return p_y * 180.0 / Math_PI;
  225. }
  226. inline float rad2deg(float p_y) {
  227. return p_y * 180.f / static_cast<float>(Math_PI);
  228. }
  229. inline double inverse_lerp(double p_from, double p_to, double p_value) {
  230. return (p_value - p_from) / (p_to - p_from);
  231. }
  232. inline float inverse_lerp(float p_from, float p_to, float p_value) {
  233. return (p_value - p_from) / (p_to - p_from);
  234. }
  235. inline double range_lerp(double p_value, double p_istart, double p_istop, double p_ostart, double p_ostop) {
  236. return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value));
  237. }
  238. inline float range_lerp(float p_value, float p_istart, float p_istop, float p_ostart, float p_ostop) {
  239. return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value));
  240. }
  241. inline bool is_equal_approx(real_t a, real_t b) {
  242. // Check for exact equality first, required to handle "infinity" values.
  243. if (a == b) {
  244. return true;
  245. }
  246. // Then check for approximate equality.
  247. real_t tolerance = CMP_EPSILON * std::abs(a);
  248. if (tolerance < CMP_EPSILON) {
  249. tolerance = CMP_EPSILON;
  250. }
  251. return std::abs(a - b) < tolerance;
  252. }
  253. inline bool is_equal_approx(real_t a, real_t b, real_t tolerance) {
  254. // Check for exact equality first, required to handle "infinity" values.
  255. if (a == b) {
  256. return true;
  257. }
  258. // Then check for approximate equality.
  259. return std::abs(a - b) < tolerance;
  260. }
  261. inline bool is_zero_approx(real_t s) {
  262. return std::abs(s) < CMP_EPSILON;
  263. }
  264. inline double smoothstep(double p_from, double p_to, double p_weight) {
  265. if (is_equal_approx(static_cast<real_t>(p_from), static_cast<real_t>(p_to))) {
  266. return p_from;
  267. }
  268. double x = clamp((p_weight - p_from) / (p_to - p_from), 0.0, 1.0);
  269. return x * x * (3.0 - 2.0 * x);
  270. }
  271. inline float smoothstep(float p_from, float p_to, float p_weight) {
  272. if (is_equal_approx(p_from, p_to)) {
  273. return p_from;
  274. }
  275. float x = clamp((p_weight - p_from) / (p_to - p_from), 0.0f, 1.0f);
  276. return x * x * (3.0f - 2.0f * x);
  277. }
  278. inline double move_toward(double p_from, double p_to, double p_delta) {
  279. return std::abs(p_to - p_from) <= p_delta ? p_to : p_from + sign(p_to - p_from) * p_delta;
  280. }
  281. inline float move_toward(float p_from, float p_to, float p_delta) {
  282. return std::abs(p_to - p_from) <= p_delta ? p_to : p_from + sign(p_to - p_from) * p_delta;
  283. }
  284. inline double linear2db(double p_linear) {
  285. return log(p_linear) * 8.6858896380650365530225783783321;
  286. }
  287. inline float linear2db(float p_linear) {
  288. return log(p_linear) * 8.6858896380650365530225783783321f;
  289. }
  290. inline double db2linear(double p_db) {
  291. return exp(p_db * 0.11512925464970228420089957273422);
  292. }
  293. inline float db2linear(float p_db) {
  294. return exp(p_db * 0.11512925464970228420089957273422f);
  295. }
  296. inline double round(double p_val) {
  297. return (p_val >= 0) ? floor(p_val + 0.5) : -floor(-p_val + 0.5);
  298. }
  299. inline float round(float p_val) {
  300. return (p_val >= 0) ? floor(p_val + 0.5f) : -floor(-p_val + 0.5f);
  301. }
  302. inline int64_t wrapi(int64_t value, int64_t min, int64_t max) {
  303. int64_t range = max - min;
  304. return range == 0 ? min : min + ((((value - min) % range) + range) % range);
  305. }
  306. inline float wrapf(real_t value, real_t min, real_t max) {
  307. const real_t range = max - min;
  308. return is_zero_approx(range) ? min : value - (range * floor((value - min) / range));
  309. }
  310. inline float stepify(float p_value, float p_step) {
  311. if (p_step != 0) {
  312. p_value = floor(p_value / p_step + 0.5f) * p_step;
  313. }
  314. return p_value;
  315. }
  316. inline double stepify(double p_value, double p_step) {
  317. if (p_step != 0) {
  318. p_value = floor(p_value / p_step + 0.5) * p_step;
  319. }
  320. return p_value;
  321. }
  322. inline unsigned int next_power_of_2(unsigned int x) {
  323. if (x == 0)
  324. return 0;
  325. --x;
  326. x |= x >> 1;
  327. x |= x >> 2;
  328. x |= x >> 4;
  329. x |= x >> 8;
  330. x |= x >> 16;
  331. return ++x;
  332. }
  333. // This function should be as fast as possible and rounding mode should not matter.
  334. inline int fast_ftoi(float a) {
  335. static int b;
  336. #if (defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0603) || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP // windows 8 phone?
  337. b = (int)((a > 0.0) ? (a + 0.5) : (a - 0.5));
  338. #elif defined(_MSC_VER) && _MSC_VER < 1800
  339. __asm fld a __asm fistp b
  340. /*#elif defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) )
  341. // use AT&T inline assembly style, document that
  342. // we use memory as output (=m) and input (m)
  343. __asm__ __volatile__ (
  344. "flds %1 \n\t"
  345. "fistpl %0 \n\t"
  346. : "=m" (b)
  347. : "m" (a));*/
  348. #else
  349. b = lrintf(a); //assuming everything but msvc 2012 or earlier has lrint
  350. #endif
  351. return b;
  352. }
  353. inline double snapped(double p_value, double p_step) {
  354. if (p_step != 0) {
  355. p_value = Math::floor(p_value / p_step + 0.5) * p_step;
  356. }
  357. return p_value;
  358. }
  359. } // namespace Math
  360. } // namespace godot
  361. #endif // GODOT_MATH_H