math.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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 <godot_cpp/core/math_defs.hpp>
  33. #include <gdextension_interface.h>
  34. #include <cmath>
  35. namespace godot {
  36. namespace Math {
  37. // Functions reproduced as in Godot's source code `math_funcs.h`.
  38. // Some are overloads to automatically support changing real_t into either double or float in the way Godot does.
  39. inline double fmod(double p_x, double p_y) {
  40. return ::fmod(p_x, p_y);
  41. }
  42. inline float fmod(float p_x, float p_y) {
  43. return ::fmodf(p_x, p_y);
  44. }
  45. inline double fposmod(double p_x, double p_y) {
  46. double value = Math::fmod(p_x, p_y);
  47. if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
  48. value += p_y;
  49. }
  50. value += 0.0;
  51. return value;
  52. }
  53. inline float fposmod(float p_x, float p_y) {
  54. float value = Math::fmod(p_x, p_y);
  55. if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
  56. value += p_y;
  57. }
  58. value += 0.0f;
  59. return value;
  60. }
  61. inline float fposmodp(float p_x, float p_y) {
  62. float value = Math::fmod(p_x, p_y);
  63. if (value < 0) {
  64. value += p_y;
  65. }
  66. value += 0.0f;
  67. return value;
  68. }
  69. inline double fposmodp(double p_x, double p_y) {
  70. double value = Math::fmod(p_x, p_y);
  71. if (value < 0) {
  72. value += p_y;
  73. }
  74. value += 0.0;
  75. return value;
  76. }
  77. inline int64_t posmod(int64_t p_x, int64_t p_y) {
  78. int64_t value = p_x % p_y;
  79. if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
  80. value += p_y;
  81. }
  82. return value;
  83. }
  84. inline double floor(double p_x) {
  85. return ::floor(p_x);
  86. }
  87. inline float floor(float p_x) {
  88. return ::floorf(p_x);
  89. }
  90. inline double ceil(double p_x) {
  91. return ::ceil(p_x);
  92. }
  93. inline float ceil(float p_x) {
  94. return ::ceilf(p_x);
  95. }
  96. inline double exp(double p_x) {
  97. return ::exp(p_x);
  98. }
  99. inline float exp(float p_x) {
  100. return ::expf(p_x);
  101. }
  102. inline double sin(double p_x) {
  103. return ::sin(p_x);
  104. }
  105. inline float sin(float p_x) {
  106. return ::sinf(p_x);
  107. }
  108. inline double cos(double p_x) {
  109. return ::cos(p_x);
  110. }
  111. inline float cos(float p_x) {
  112. return ::cosf(p_x);
  113. }
  114. inline double tan(double p_x) {
  115. return ::tan(p_x);
  116. }
  117. inline float tan(float p_x) {
  118. return ::tanf(p_x);
  119. }
  120. inline double sinh(double p_x) {
  121. return ::sinh(p_x);
  122. }
  123. inline float sinh(float p_x) {
  124. return ::sinhf(p_x);
  125. }
  126. inline float sinc(float p_x) {
  127. return p_x == 0 ? 1 : ::sin(p_x) / p_x;
  128. }
  129. inline double sinc(double p_x) {
  130. return p_x == 0 ? 1 : ::sin(p_x) / p_x;
  131. }
  132. inline float sincn(float p_x) {
  133. return (float)sinc(Math_PI * p_x);
  134. }
  135. inline double sincn(double p_x) {
  136. return sinc(Math_PI * p_x);
  137. }
  138. inline double cosh(double p_x) {
  139. return ::cosh(p_x);
  140. }
  141. inline float cosh(float p_x) {
  142. return ::coshf(p_x);
  143. }
  144. inline double tanh(double p_x) {
  145. return ::tanh(p_x);
  146. }
  147. inline float tanh(float p_x) {
  148. return ::tanhf(p_x);
  149. }
  150. inline double asin(double p_x) {
  151. return ::asin(p_x);
  152. }
  153. inline float asin(float p_x) {
  154. return ::asinf(p_x);
  155. }
  156. inline double acos(double p_x) {
  157. return ::acos(p_x);
  158. }
  159. inline float acos(float p_x) {
  160. return ::acosf(p_x);
  161. }
  162. inline double atan(double p_x) {
  163. return ::atan(p_x);
  164. }
  165. inline float atan(float p_x) {
  166. return ::atanf(p_x);
  167. }
  168. inline double atan2(double p_y, double p_x) {
  169. return ::atan2(p_y, p_x);
  170. }
  171. inline float atan2(float p_y, float p_x) {
  172. return ::atan2f(p_y, p_x);
  173. }
  174. inline double sqrt(double p_x) {
  175. return ::sqrt(p_x);
  176. }
  177. inline float sqrt(float p_x) {
  178. return ::sqrtf(p_x);
  179. }
  180. inline double pow(double p_x, double p_y) {
  181. return ::pow(p_x, p_y);
  182. }
  183. inline float pow(float p_x, float p_y) {
  184. return ::powf(p_x, p_y);
  185. }
  186. inline double log(double p_x) {
  187. return ::log(p_x);
  188. }
  189. inline float log(float p_x) {
  190. return ::logf(p_x);
  191. }
  192. inline float lerp(float minv, float maxv, float t) {
  193. return minv + t * (maxv - minv);
  194. }
  195. inline double lerp(double minv, double maxv, double t) {
  196. return minv + t * (maxv - minv);
  197. }
  198. inline double lerp_angle(double p_from, double p_to, double p_weight) {
  199. double difference = fmod(p_to - p_from, Math_TAU);
  200. double distance = fmod(2.0 * difference, Math_TAU) - difference;
  201. return p_from + distance * p_weight;
  202. }
  203. inline float lerp_angle(float p_from, float p_to, float p_weight) {
  204. float difference = fmod(p_to - p_from, (float)Math_TAU);
  205. float distance = fmod(2.0f * difference, (float)Math_TAU) - difference;
  206. return p_from + distance * p_weight;
  207. }
  208. inline double cubic_interpolate(double p_from, double p_to, double p_pre, double p_post, double p_weight) {
  209. return 0.5 *
  210. ((p_from * 2.0) +
  211. (-p_pre + p_to) * p_weight +
  212. (2.0 * p_pre - 5.0 * p_from + 4.0 * p_to - p_post) * (p_weight * p_weight) +
  213. (-p_pre + 3.0 * p_from - 3.0 * p_to + p_post) * (p_weight * p_weight * p_weight));
  214. }
  215. inline float cubic_interpolate(float p_from, float p_to, float p_pre, float p_post, float p_weight) {
  216. return 0.5f *
  217. ((p_from * 2.0f) +
  218. (-p_pre + p_to) * p_weight +
  219. (2.0f * p_pre - 5.0f * p_from + 4.0f * p_to - p_post) * (p_weight * p_weight) +
  220. (-p_pre + 3.0f * p_from - 3.0f * p_to + p_post) * (p_weight * p_weight * p_weight));
  221. }
  222. inline double cubic_interpolate_angle(double p_from, double p_to, double p_pre, double p_post, double p_weight) {
  223. double from_rot = fmod(p_from, Math_TAU);
  224. double pre_diff = fmod(p_pre - from_rot, Math_TAU);
  225. double pre_rot = from_rot + fmod(2.0 * pre_diff, Math_TAU) - pre_diff;
  226. double to_diff = fmod(p_to - from_rot, Math_TAU);
  227. double to_rot = from_rot + fmod(2.0 * to_diff, Math_TAU) - to_diff;
  228. double post_diff = fmod(p_post - to_rot, Math_TAU);
  229. double post_rot = to_rot + fmod(2.0 * post_diff, Math_TAU) - post_diff;
  230. return cubic_interpolate(from_rot, to_rot, pre_rot, post_rot, p_weight);
  231. }
  232. inline float cubic_interpolate_angle(float p_from, float p_to, float p_pre, float p_post, float p_weight) {
  233. float from_rot = fmod(p_from, (float)Math_TAU);
  234. float pre_diff = fmod(p_pre - from_rot, (float)Math_TAU);
  235. float pre_rot = from_rot + fmod(2.0f * pre_diff, (float)Math_TAU) - pre_diff;
  236. float to_diff = fmod(p_to - from_rot, (float)Math_TAU);
  237. float to_rot = from_rot + fmod(2.0f * to_diff, (float)Math_TAU) - to_diff;
  238. float post_diff = fmod(p_post - to_rot, (float)Math_TAU);
  239. float post_rot = to_rot + fmod(2.0f * post_diff, (float)Math_TAU) - post_diff;
  240. return cubic_interpolate(from_rot, to_rot, pre_rot, post_rot, p_weight);
  241. }
  242. inline double cubic_interpolate_in_time(double p_from, double p_to, double p_pre, double p_post, double p_weight,
  243. double p_to_t, double p_pre_t, double p_post_t) {
  244. /* Barry-Goldman method */
  245. double t = Math::lerp(0.0, p_to_t, p_weight);
  246. double a1 = Math::lerp(p_pre, p_from, p_pre_t == 0 ? 0.0 : (t - p_pre_t) / -p_pre_t);
  247. double a2 = Math::lerp(p_from, p_to, p_to_t == 0 ? 0.5 : t / p_to_t);
  248. 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));
  249. 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));
  250. double b2 = Math::lerp(a2, a3, p_post_t == 0 ? 1.0 : t / p_post_t);
  251. return Math::lerp(b1, b2, p_to_t == 0 ? 0.5 : t / p_to_t);
  252. }
  253. inline float cubic_interpolate_in_time(float p_from, float p_to, float p_pre, float p_post, float p_weight,
  254. float p_to_t, float p_pre_t, float p_post_t) {
  255. /* Barry-Goldman method */
  256. float t = Math::lerp(0.0f, p_to_t, p_weight);
  257. float a1 = Math::lerp(p_pre, p_from, p_pre_t == 0 ? 0.0f : (t - p_pre_t) / -p_pre_t);
  258. float a2 = Math::lerp(p_from, p_to, p_to_t == 0 ? 0.5f : t / p_to_t);
  259. 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));
  260. 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));
  261. float b2 = Math::lerp(a2, a3, p_post_t == 0 ? 1.0f : t / p_post_t);
  262. return Math::lerp(b1, b2, p_to_t == 0 ? 0.5f : t / p_to_t);
  263. }
  264. inline double cubic_interpolate_angle_in_time(double p_from, double p_to, double p_pre, double p_post, double p_weight,
  265. double p_to_t, double p_pre_t, double p_post_t) {
  266. double from_rot = fmod(p_from, Math_TAU);
  267. double pre_diff = fmod(p_pre - from_rot, Math_TAU);
  268. double pre_rot = from_rot + fmod(2.0 * pre_diff, Math_TAU) - pre_diff;
  269. double to_diff = fmod(p_to - from_rot, Math_TAU);
  270. double to_rot = from_rot + fmod(2.0 * to_diff, Math_TAU) - to_diff;
  271. double post_diff = fmod(p_post - to_rot, Math_TAU);
  272. double post_rot = to_rot + fmod(2.0 * post_diff, Math_TAU) - post_diff;
  273. return cubic_interpolate_in_time(from_rot, to_rot, pre_rot, post_rot, p_weight, p_to_t, p_pre_t, p_post_t);
  274. }
  275. inline float cubic_interpolate_angle_in_time(float p_from, float p_to, float p_pre, float p_post, float p_weight,
  276. float p_to_t, float p_pre_t, float p_post_t) {
  277. float from_rot = fmod(p_from, (float)Math_TAU);
  278. float pre_diff = fmod(p_pre - from_rot, (float)Math_TAU);
  279. float pre_rot = from_rot + fmod(2.0f * pre_diff, (float)Math_TAU) - pre_diff;
  280. float to_diff = fmod(p_to - from_rot, (float)Math_TAU);
  281. float to_rot = from_rot + fmod(2.0f * to_diff, (float)Math_TAU) - to_diff;
  282. float post_diff = fmod(p_post - to_rot, (float)Math_TAU);
  283. float post_rot = to_rot + fmod(2.0f * post_diff, (float)Math_TAU) - post_diff;
  284. return cubic_interpolate_in_time(from_rot, to_rot, pre_rot, post_rot, p_weight, p_to_t, p_pre_t, p_post_t);
  285. }
  286. inline double bezier_interpolate(double p_start, double p_control_1, double p_control_2, double p_end, double p_t) {
  287. /* Formula from Wikipedia article on Bezier curves. */
  288. double omt = (1.0 - p_t);
  289. double omt2 = omt * omt;
  290. double omt3 = omt2 * omt;
  291. double t2 = p_t * p_t;
  292. double t3 = t2 * p_t;
  293. return p_start * omt3 + p_control_1 * omt2 * p_t * 3.0 + p_control_2 * omt * t2 * 3.0 + p_end * t3;
  294. }
  295. inline float bezier_interpolate(float p_start, float p_control_1, float p_control_2, float p_end, float p_t) {
  296. /* Formula from Wikipedia article on Bezier curves. */
  297. float omt = (1.0f - p_t);
  298. float omt2 = omt * omt;
  299. float omt3 = omt2 * omt;
  300. float t2 = p_t * p_t;
  301. float t3 = t2 * p_t;
  302. return p_start * omt3 + p_control_1 * omt2 * p_t * 3.0f + p_control_2 * omt * t2 * 3.0f + p_end * t3;
  303. }
  304. inline double bezier_derivative(double p_start, double p_control_1, double p_control_2, double p_end, double p_t) {
  305. /* Formula from Wikipedia article on Bezier curves. */
  306. double omt = (1.0 - p_t);
  307. double omt2 = omt * omt;
  308. double t2 = p_t * p_t;
  309. 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;
  310. return d;
  311. }
  312. inline float bezier_derivative(float p_start, float p_control_1, float p_control_2, float p_end, float p_t) {
  313. /* Formula from Wikipedia article on Bezier curves. */
  314. float omt = (1.0f - p_t);
  315. float omt2 = omt * omt;
  316. float t2 = p_t * p_t;
  317. 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;
  318. return d;
  319. }
  320. template <typename T>
  321. inline T clamp(T x, T minv, T maxv) {
  322. if (x < minv) {
  323. return minv;
  324. }
  325. if (x > maxv) {
  326. return maxv;
  327. }
  328. return x;
  329. }
  330. template <typename T>
  331. inline T min(T a, T b) {
  332. return a < b ? a : b;
  333. }
  334. template <typename T>
  335. inline T max(T a, T b) {
  336. return a > b ? a : b;
  337. }
  338. template <typename T>
  339. inline T sign(T x) {
  340. return static_cast<T>(SIGN(x));
  341. }
  342. template <typename T>
  343. inline T abs(T x) {
  344. return std::abs(x);
  345. }
  346. inline double deg_to_rad(double p_y) {
  347. return p_y * Math_PI / 180.0;
  348. }
  349. inline float deg_to_rad(float p_y) {
  350. return p_y * static_cast<float>(Math_PI) / 180.f;
  351. }
  352. inline double rad_to_deg(double p_y) {
  353. return p_y * 180.0 / Math_PI;
  354. }
  355. inline float rad_to_deg(float p_y) {
  356. return p_y * 180.f / static_cast<float>(Math_PI);
  357. }
  358. inline double inverse_lerp(double p_from, double p_to, double p_value) {
  359. return (p_value - p_from) / (p_to - p_from);
  360. }
  361. inline float inverse_lerp(float p_from, float p_to, float p_value) {
  362. return (p_value - p_from) / (p_to - p_from);
  363. }
  364. inline double remap(double p_value, double p_istart, double p_istop, double p_ostart, double p_ostop) {
  365. return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value));
  366. }
  367. inline float remap(float p_value, float p_istart, float p_istop, float p_ostart, float p_ostop) {
  368. return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value));
  369. }
  370. inline bool is_nan(float p_val) {
  371. return std::isnan(p_val);
  372. }
  373. inline bool is_nan(double p_val) {
  374. return std::isnan(p_val);
  375. }
  376. inline bool is_inf(float p_val) {
  377. return std::isinf(p_val);
  378. }
  379. inline bool is_inf(double p_val) {
  380. return std::isinf(p_val);
  381. }
  382. inline bool is_finite(float p_val) {
  383. return std::isfinite(p_val);
  384. }
  385. inline bool is_finite(double p_val) {
  386. return std::isfinite(p_val);
  387. }
  388. inline bool is_equal_approx(float a, float b) {
  389. // Check for exact equality first, required to handle "infinity" values.
  390. if (a == b) {
  391. return true;
  392. }
  393. // Then check for approximate equality.
  394. float tolerance = (float)CMP_EPSILON * abs(a);
  395. if (tolerance < (float)CMP_EPSILON) {
  396. tolerance = (float)CMP_EPSILON;
  397. }
  398. return abs(a - b) < tolerance;
  399. }
  400. inline bool is_equal_approx(float a, float b, float tolerance) {
  401. // Check for exact equality first, required to handle "infinity" values.
  402. if (a == b) {
  403. return true;
  404. }
  405. // Then check for approximate equality.
  406. return abs(a - b) < tolerance;
  407. }
  408. inline bool is_zero_approx(float s) {
  409. return abs(s) < (float)CMP_EPSILON;
  410. }
  411. inline bool is_equal_approx(double a, double b) {
  412. // Check for exact equality first, required to handle "infinity" values.
  413. if (a == b) {
  414. return true;
  415. }
  416. // Then check for approximate equality.
  417. double tolerance = CMP_EPSILON * abs(a);
  418. if (tolerance < CMP_EPSILON) {
  419. tolerance = CMP_EPSILON;
  420. }
  421. return abs(a - b) < tolerance;
  422. }
  423. inline bool is_equal_approx(double a, double b, double tolerance) {
  424. // Check for exact equality first, required to handle "infinity" values.
  425. if (a == b) {
  426. return true;
  427. }
  428. // Then check for approximate equality.
  429. return abs(a - b) < tolerance;
  430. }
  431. inline bool is_zero_approx(double s) {
  432. return abs(s) < CMP_EPSILON;
  433. }
  434. inline float absf(float g) {
  435. union {
  436. float f;
  437. uint32_t i;
  438. } u;
  439. u.f = g;
  440. u.i &= 2147483647u;
  441. return u.f;
  442. }
  443. inline double absd(double g) {
  444. union {
  445. double d;
  446. uint64_t i;
  447. } u;
  448. u.d = g;
  449. u.i &= (uint64_t)9223372036854775807ull;
  450. return u.d;
  451. }
  452. inline double smoothstep(double p_from, double p_to, double p_weight) {
  453. if (is_equal_approx(static_cast<real_t>(p_from), static_cast<real_t>(p_to))) {
  454. return p_from;
  455. }
  456. double x = clamp((p_weight - p_from) / (p_to - p_from), 0.0, 1.0);
  457. return x * x * (3.0 - 2.0 * x);
  458. }
  459. inline float smoothstep(float p_from, float p_to, float p_weight) {
  460. if (is_equal_approx(p_from, p_to)) {
  461. return p_from;
  462. }
  463. float x = clamp((p_weight - p_from) / (p_to - p_from), 0.0f, 1.0f);
  464. return x * x * (3.0f - 2.0f * x);
  465. }
  466. inline double move_toward(double p_from, double p_to, double p_delta) {
  467. return std::abs(p_to - p_from) <= p_delta ? p_to : p_from + sign(p_to - p_from) * p_delta;
  468. }
  469. inline float move_toward(float p_from, float p_to, float p_delta) {
  470. return std::abs(p_to - p_from) <= p_delta ? p_to : p_from + sign(p_to - p_from) * p_delta;
  471. }
  472. inline double linear2db(double p_linear) {
  473. return log(p_linear) * 8.6858896380650365530225783783321;
  474. }
  475. inline float linear2db(float p_linear) {
  476. return log(p_linear) * 8.6858896380650365530225783783321f;
  477. }
  478. inline double db2linear(double p_db) {
  479. return exp(p_db * 0.11512925464970228420089957273422);
  480. }
  481. inline float db2linear(float p_db) {
  482. return exp(p_db * 0.11512925464970228420089957273422f);
  483. }
  484. inline double round(double p_val) {
  485. return (p_val >= 0) ? floor(p_val + 0.5) : -floor(-p_val + 0.5);
  486. }
  487. inline float round(float p_val) {
  488. return (p_val >= 0) ? floor(p_val + 0.5f) : -floor(-p_val + 0.5f);
  489. }
  490. inline int64_t wrapi(int64_t value, int64_t min, int64_t max) {
  491. int64_t range = max - min;
  492. return range == 0 ? min : min + ((((value - min) % range) + range) % range);
  493. }
  494. inline float wrapf(real_t value, real_t min, real_t max) {
  495. const real_t range = max - min;
  496. return is_zero_approx(range) ? min : value - (range * floor((value - min) / range));
  497. }
  498. inline float fract(float value) {
  499. return value - floor(value);
  500. }
  501. inline double fract(double value) {
  502. return value - floor(value);
  503. }
  504. inline float pingpong(float value, float length) {
  505. return (length != 0.0f) ? abs(fract((value - length) / (length * 2.0f)) * length * 2.0f - length) : 0.0f;
  506. }
  507. inline double pingpong(double value, double length) {
  508. return (length != 0.0) ? abs(fract((value - length) / (length * 2.0)) * length * 2.0 - length) : 0.0;
  509. }
  510. // This function should be as fast as possible and rounding mode should not matter.
  511. inline int fast_ftoi(float a) {
  512. static int b;
  513. #if (defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0603) || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP // windows 8 phone?
  514. b = (int)((a > 0.0) ? (a + 0.5) : (a - 0.5));
  515. #elif defined(_MSC_VER) && _MSC_VER < 1800
  516. __asm fld a __asm fistp b
  517. /*#elif defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) )
  518. // use AT&T inline assembly style, document that
  519. // we use memory as output (=m) and input (m)
  520. __asm__ __volatile__ (
  521. "flds %1 \n\t"
  522. "fistpl %0 \n\t"
  523. : "=m" (b)
  524. : "m" (a));*/
  525. #else
  526. b = lrintf(a); // assuming everything but msvc 2012 or earlier has lrint
  527. #endif
  528. return b;
  529. }
  530. inline double snapped(double p_value, double p_step) {
  531. if (p_step != 0) {
  532. p_value = Math::floor(p_value / p_step + 0.5) * p_step;
  533. }
  534. return p_value;
  535. }
  536. inline float snap_scalar(float p_offset, float p_step, float p_target) {
  537. return p_step != 0 ? Math::snapped(p_target - p_offset, p_step) + p_offset : p_target;
  538. }
  539. inline float snap_scalar_separation(float p_offset, float p_step, float p_target, float p_separation) {
  540. if (p_step != 0) {
  541. float a = Math::snapped(p_target - p_offset, p_step + p_separation) + p_offset;
  542. float b = a;
  543. if (p_target >= 0) {
  544. b -= p_separation;
  545. } else {
  546. b += p_step;
  547. }
  548. return (Math::abs(p_target - a) < Math::abs(p_target - b)) ? a : b;
  549. }
  550. return p_target;
  551. }
  552. } // namespace Math
  553. } // namespace godot
  554. #include "math.compat.inc"