math_funcs.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*************************************************************************/
  2. /* math_funcs.h */
  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 MATH_FUNCS_H
  31. #define MATH_FUNCS_H
  32. #include "core/math/math_defs.h"
  33. #include "core/math/random_pcg.h"
  34. #include "core/typedefs.h"
  35. #include "thirdparty/misc/pcg.h"
  36. #include <float.h>
  37. #include <math.h>
  38. class Math {
  39. static RandomPCG default_rand;
  40. public:
  41. Math() {} // useless to instance
  42. // Not using 'RANDOM_MAX' to avoid conflict with system headers on some OSes (at least NetBSD).
  43. static const uint64_t RANDOM_32BIT_MAX = 0xFFFFFFFF;
  44. static _ALWAYS_INLINE_ double sin(double p_x) { return ::sin(p_x); }
  45. static _ALWAYS_INLINE_ float sin(float p_x) { return ::sinf(p_x); }
  46. static _ALWAYS_INLINE_ double cos(double p_x) { return ::cos(p_x); }
  47. static _ALWAYS_INLINE_ float cos(float p_x) { return ::cosf(p_x); }
  48. static _ALWAYS_INLINE_ double tan(double p_x) { return ::tan(p_x); }
  49. static _ALWAYS_INLINE_ float tan(float p_x) { return ::tanf(p_x); }
  50. static _ALWAYS_INLINE_ double sinh(double p_x) { return ::sinh(p_x); }
  51. static _ALWAYS_INLINE_ float sinh(float p_x) { return ::sinhf(p_x); }
  52. static _ALWAYS_INLINE_ float sinc(float p_x) { return p_x == 0 ? 1 : ::sin(p_x) / p_x; }
  53. static _ALWAYS_INLINE_ double sinc(double p_x) { return p_x == 0 ? 1 : ::sin(p_x) / p_x; }
  54. static _ALWAYS_INLINE_ float sincn(float p_x) { return sinc(Math_PI * p_x); }
  55. static _ALWAYS_INLINE_ double sincn(double p_x) { return sinc(Math_PI * p_x); }
  56. static _ALWAYS_INLINE_ double cosh(double p_x) { return ::cosh(p_x); }
  57. static _ALWAYS_INLINE_ float cosh(float p_x) { return ::coshf(p_x); }
  58. static _ALWAYS_INLINE_ double tanh(double p_x) { return ::tanh(p_x); }
  59. static _ALWAYS_INLINE_ float tanh(float p_x) { return ::tanhf(p_x); }
  60. static _ALWAYS_INLINE_ double asin(double p_x) { return ::asin(p_x); }
  61. static _ALWAYS_INLINE_ float asin(float p_x) { return ::asinf(p_x); }
  62. static _ALWAYS_INLINE_ double acos(double p_x) { return ::acos(p_x); }
  63. static _ALWAYS_INLINE_ float acos(float p_x) { return ::acosf(p_x); }
  64. static _ALWAYS_INLINE_ double atan(double p_x) { return ::atan(p_x); }
  65. static _ALWAYS_INLINE_ float atan(float p_x) { return ::atanf(p_x); }
  66. static _ALWAYS_INLINE_ double atan2(double p_y, double p_x) { return ::atan2(p_y, p_x); }
  67. static _ALWAYS_INLINE_ float atan2(float p_y, float p_x) { return ::atan2f(p_y, p_x); }
  68. static _ALWAYS_INLINE_ double sqrt(double p_x) { return ::sqrt(p_x); }
  69. static _ALWAYS_INLINE_ float sqrt(float p_x) { return ::sqrtf(p_x); }
  70. static _ALWAYS_INLINE_ double fmod(double p_x, double p_y) { return ::fmod(p_x, p_y); }
  71. static _ALWAYS_INLINE_ float fmod(float p_x, float p_y) { return ::fmodf(p_x, p_y); }
  72. static _ALWAYS_INLINE_ double floor(double p_x) { return ::floor(p_x); }
  73. static _ALWAYS_INLINE_ float floor(float p_x) { return ::floorf(p_x); }
  74. static _ALWAYS_INLINE_ double ceil(double p_x) { return ::ceil(p_x); }
  75. static _ALWAYS_INLINE_ float ceil(float p_x) { return ::ceilf(p_x); }
  76. static _ALWAYS_INLINE_ double pow(double p_x, double p_y) { return ::pow(p_x, p_y); }
  77. static _ALWAYS_INLINE_ float pow(float p_x, float p_y) { return ::powf(p_x, p_y); }
  78. static _ALWAYS_INLINE_ double log(double p_x) { return ::log(p_x); }
  79. static _ALWAYS_INLINE_ float log(float p_x) { return ::logf(p_x); }
  80. static _ALWAYS_INLINE_ double log2(double p_x) { return ::log2(p_x); }
  81. static _ALWAYS_INLINE_ float log2(float p_x) { return ::log2f(p_x); }
  82. static _ALWAYS_INLINE_ double exp(double p_x) { return ::exp(p_x); }
  83. static _ALWAYS_INLINE_ float exp(float p_x) { return ::expf(p_x); }
  84. static _ALWAYS_INLINE_ bool is_nan(double p_val) {
  85. #ifdef _MSC_VER
  86. return _isnan(p_val);
  87. #elif defined(__GNUC__) && __GNUC__ < 6
  88. union {
  89. uint64_t u;
  90. double f;
  91. } ieee754;
  92. ieee754.f = p_val;
  93. // (unsigned)(0x7ff0000000000001 >> 32) : 0x7ff00000
  94. return ((((unsigned)(ieee754.u >> 32) & 0x7fffffff) + ((unsigned)ieee754.u != 0)) > 0x7ff00000);
  95. #else
  96. return isnan(p_val);
  97. #endif
  98. }
  99. static _ALWAYS_INLINE_ bool is_nan(float p_val) {
  100. #ifdef _MSC_VER
  101. return _isnan(p_val);
  102. #elif defined(__GNUC__) && __GNUC__ < 6
  103. union {
  104. uint32_t u;
  105. float f;
  106. } ieee754;
  107. ieee754.f = p_val;
  108. // -----------------------------------
  109. // (single-precision floating-point)
  110. // NaN : s111 1111 1xxx xxxx xxxx xxxx xxxx xxxx
  111. // : (> 0x7f800000)
  112. // where,
  113. // s : sign
  114. // x : non-zero number
  115. // -----------------------------------
  116. return ((ieee754.u & 0x7fffffff) > 0x7f800000);
  117. #else
  118. return isnan(p_val);
  119. #endif
  120. }
  121. static _ALWAYS_INLINE_ bool is_inf(double p_val) {
  122. #ifdef _MSC_VER
  123. return !_finite(p_val);
  124. // use an inline implementation of isinf as a workaround for problematic libstdc++ versions from gcc 5.x era
  125. #elif defined(__GNUC__) && __GNUC__ < 6
  126. union {
  127. uint64_t u;
  128. double f;
  129. } ieee754;
  130. ieee754.f = p_val;
  131. return ((unsigned)(ieee754.u >> 32) & 0x7fffffff) == 0x7ff00000 &&
  132. ((unsigned)ieee754.u == 0);
  133. #else
  134. return isinf(p_val);
  135. #endif
  136. }
  137. static _ALWAYS_INLINE_ bool is_inf(float p_val) {
  138. #ifdef _MSC_VER
  139. return !_finite(p_val);
  140. // use an inline implementation of isinf as a workaround for problematic libstdc++ versions from gcc 5.x era
  141. #elif defined(__GNUC__) && __GNUC__ < 6
  142. union {
  143. uint32_t u;
  144. float f;
  145. } ieee754;
  146. ieee754.f = p_val;
  147. return (ieee754.u & 0x7fffffff) == 0x7f800000;
  148. #else
  149. return isinf(p_val);
  150. #endif
  151. }
  152. static _ALWAYS_INLINE_ double abs(double g) { return absd(g); }
  153. static _ALWAYS_INLINE_ float abs(float g) { return absf(g); }
  154. static _ALWAYS_INLINE_ int abs(int g) { return g > 0 ? g : -g; }
  155. static _ALWAYS_INLINE_ double fposmod(double p_x, double p_y) {
  156. double value = Math::fmod(p_x, p_y);
  157. if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
  158. value += p_y;
  159. }
  160. value += 0.0;
  161. return value;
  162. }
  163. static _ALWAYS_INLINE_ float fposmod(float p_x, float p_y) {
  164. float value = Math::fmod(p_x, p_y);
  165. if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
  166. value += p_y;
  167. }
  168. value += 0.0;
  169. return value;
  170. }
  171. static _ALWAYS_INLINE_ float fposmodp(float p_x, float p_y) {
  172. float value = Math::fmod(p_x, p_y);
  173. if (value < 0) {
  174. value += p_y;
  175. }
  176. value += 0.0;
  177. return value;
  178. }
  179. static _ALWAYS_INLINE_ double fposmodp(double p_x, double p_y) {
  180. double value = Math::fmod(p_x, p_y);
  181. if (value < 0) {
  182. value += p_y;
  183. }
  184. value += 0.0;
  185. return value;
  186. }
  187. static _ALWAYS_INLINE_ int64_t posmod(int64_t p_x, int64_t p_y) {
  188. int64_t value = p_x % p_y;
  189. if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
  190. value += p_y;
  191. }
  192. return value;
  193. }
  194. static _ALWAYS_INLINE_ double deg2rad(double p_y) { return p_y * (Math_PI / 180.0); }
  195. static _ALWAYS_INLINE_ float deg2rad(float p_y) { return p_y * (Math_PI / 180.0); }
  196. static _ALWAYS_INLINE_ double rad2deg(double p_y) { return p_y * (180.0 / Math_PI); }
  197. static _ALWAYS_INLINE_ float rad2deg(float p_y) { return p_y * (180.0 / Math_PI); }
  198. static _ALWAYS_INLINE_ double lerp(double p_from, double p_to, double p_weight) { return p_from + (p_to - p_from) * p_weight; }
  199. static _ALWAYS_INLINE_ float lerp(float p_from, float p_to, float p_weight) { return p_from + (p_to - p_from) * p_weight; }
  200. static _ALWAYS_INLINE_ double lerp_angle(double p_from, double p_to, double p_weight) {
  201. double difference = fmod(p_to - p_from, Math_TAU);
  202. double distance = fmod(2.0 * difference, Math_TAU) - difference;
  203. return p_from + distance * p_weight;
  204. }
  205. static _ALWAYS_INLINE_ float lerp_angle(float p_from, float p_to, float p_weight) {
  206. float difference = fmod(p_to - p_from, (float)Math_TAU);
  207. float distance = fmod(2.0f * difference, (float)Math_TAU) - difference;
  208. return p_from + distance * p_weight;
  209. }
  210. static _ALWAYS_INLINE_ double inverse_lerp(double p_from, double p_to, double p_value) { return (p_value - p_from) / (p_to - p_from); }
  211. static _ALWAYS_INLINE_ float inverse_lerp(float p_from, float p_to, float p_value) { return (p_value - p_from) / (p_to - p_from); }
  212. static _ALWAYS_INLINE_ double range_lerp(double p_value, double p_istart, double p_istop, double p_ostart, double p_ostop) { return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value)); }
  213. static _ALWAYS_INLINE_ float range_lerp(float p_value, float p_istart, float p_istop, float p_ostart, float p_ostop) { return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value)); }
  214. static _ALWAYS_INLINE_ double smoothstep(double p_from, double p_to, double p_s) {
  215. if (is_equal_approx(p_from, p_to)) {
  216. return p_from;
  217. }
  218. double s = CLAMP((p_s - p_from) / (p_to - p_from), 0.0, 1.0);
  219. return s * s * (3.0 - 2.0 * s);
  220. }
  221. static _ALWAYS_INLINE_ float smoothstep(float p_from, float p_to, float p_s) {
  222. if (is_equal_approx(p_from, p_to)) {
  223. return p_from;
  224. }
  225. float s = CLAMP((p_s - p_from) / (p_to - p_from), 0.0f, 1.0f);
  226. return s * s * (3.0f - 2.0f * s);
  227. }
  228. static _ALWAYS_INLINE_ double move_toward(double p_from, double p_to, double p_delta) { return abs(p_to - p_from) <= p_delta ? p_to : p_from + SGN(p_to - p_from) * p_delta; }
  229. static _ALWAYS_INLINE_ float move_toward(float p_from, float p_to, float p_delta) { return abs(p_to - p_from) <= p_delta ? p_to : p_from + SGN(p_to - p_from) * p_delta; }
  230. static _ALWAYS_INLINE_ double linear2db(double p_linear) { return Math::log(p_linear) * 8.6858896380650365530225783783321; }
  231. static _ALWAYS_INLINE_ float linear2db(float p_linear) { return Math::log(p_linear) * 8.6858896380650365530225783783321; }
  232. static _ALWAYS_INLINE_ double db2linear(double p_db) { return Math::exp(p_db * 0.11512925464970228420089957273422); }
  233. static _ALWAYS_INLINE_ float db2linear(float p_db) { return Math::exp(p_db * 0.11512925464970228420089957273422); }
  234. static _ALWAYS_INLINE_ double round(double p_val) { return (p_val >= 0) ? Math::floor(p_val + 0.5) : -Math::floor(-p_val + 0.5); }
  235. static _ALWAYS_INLINE_ float round(float p_val) { return (p_val >= 0) ? Math::floor(p_val + 0.5) : -Math::floor(-p_val + 0.5); }
  236. static _ALWAYS_INLINE_ int64_t wrapi(int64_t value, int64_t min, int64_t max) {
  237. int64_t range = max - min;
  238. return range == 0 ? min : min + ((((value - min) % range) + range) % range);
  239. }
  240. static _ALWAYS_INLINE_ double wrapf(double value, double min, double max) {
  241. double range = max - min;
  242. return is_zero_approx(range) ? min : value - (range * Math::floor((value - min) / range));
  243. }
  244. static _ALWAYS_INLINE_ float wrapf(float value, float min, float max) {
  245. float range = max - min;
  246. return is_zero_approx(range) ? min : value - (range * Math::floor((value - min) / range));
  247. }
  248. // double only, as these functions are mainly used by the editor and not performance-critical,
  249. static double ease(double p_x, double p_c);
  250. static int step_decimals(double p_step);
  251. static int range_step_decimals(double p_step);
  252. static double snapped(double p_value, double p_step);
  253. static double dectime(double p_value, double p_amount, double p_step);
  254. static uint32_t larger_prime(uint32_t p_val);
  255. static void seed(uint64_t x);
  256. static void randomize();
  257. static uint32_t rand_from_seed(uint64_t *seed);
  258. static uint32_t rand();
  259. static _ALWAYS_INLINE_ double randd() { return (double)rand() / (double)Math::RANDOM_32BIT_MAX; }
  260. static _ALWAYS_INLINE_ float randf() { return (float)rand() / (float)Math::RANDOM_32BIT_MAX; }
  261. static double random(double from, double to);
  262. static float random(float from, float to);
  263. static int random(int from, int to);
  264. static _ALWAYS_INLINE_ bool is_equal_approx(float a, float b) {
  265. // Check for exact equality first, required to handle "infinity" values.
  266. if (a == b) {
  267. return true;
  268. }
  269. // Then check for approximate equality.
  270. float tolerance = CMP_EPSILON * abs(a);
  271. if (tolerance < CMP_EPSILON) {
  272. tolerance = CMP_EPSILON;
  273. }
  274. return abs(a - b) < tolerance;
  275. }
  276. static _ALWAYS_INLINE_ bool is_equal_approx(float a, float b, float tolerance) {
  277. // Check for exact equality first, required to handle "infinity" values.
  278. if (a == b) {
  279. return true;
  280. }
  281. // Then check for approximate equality.
  282. return abs(a - b) < tolerance;
  283. }
  284. static _ALWAYS_INLINE_ bool is_zero_approx(float s) {
  285. return abs(s) < CMP_EPSILON;
  286. }
  287. static _ALWAYS_INLINE_ bool is_equal_approx(double a, double b) {
  288. // Check for exact equality first, required to handle "infinity" values.
  289. if (a == b) {
  290. return true;
  291. }
  292. // Then check for approximate equality.
  293. double tolerance = CMP_EPSILON * abs(a);
  294. if (tolerance < CMP_EPSILON) {
  295. tolerance = CMP_EPSILON;
  296. }
  297. return abs(a - b) < tolerance;
  298. }
  299. static _ALWAYS_INLINE_ bool is_equal_approx(double a, double b, double tolerance) {
  300. // Check for exact equality first, required to handle "infinity" values.
  301. if (a == b) {
  302. return true;
  303. }
  304. // Then check for approximate equality.
  305. return abs(a - b) < tolerance;
  306. }
  307. static _ALWAYS_INLINE_ bool is_zero_approx(double s) {
  308. return abs(s) < CMP_EPSILON;
  309. }
  310. static _ALWAYS_INLINE_ float absf(float g) {
  311. union {
  312. float f;
  313. uint32_t i;
  314. } u;
  315. u.f = g;
  316. u.i &= 2147483647u;
  317. return u.f;
  318. }
  319. static _ALWAYS_INLINE_ double absd(double g) {
  320. union {
  321. double d;
  322. uint64_t i;
  323. } u;
  324. u.d = g;
  325. u.i &= (uint64_t)9223372036854775807ll;
  326. return u.d;
  327. }
  328. // This function should be as fast as possible and rounding mode should not matter.
  329. static _ALWAYS_INLINE_ int fast_ftoi(float a) {
  330. // Assuming every supported compiler has `lrint()`.
  331. return lrintf(a);
  332. }
  333. static _ALWAYS_INLINE_ uint32_t halfbits_to_floatbits(uint16_t h) {
  334. uint16_t h_exp, h_sig;
  335. uint32_t f_sgn, f_exp, f_sig;
  336. h_exp = (h & 0x7c00u);
  337. f_sgn = ((uint32_t)h & 0x8000u) << 16;
  338. switch (h_exp) {
  339. case 0x0000u: /* 0 or subnormal */
  340. h_sig = (h & 0x03ffu);
  341. /* Signed zero */
  342. if (h_sig == 0) {
  343. return f_sgn;
  344. }
  345. /* Subnormal */
  346. h_sig <<= 1;
  347. while ((h_sig & 0x0400u) == 0) {
  348. h_sig <<= 1;
  349. h_exp++;
  350. }
  351. f_exp = ((uint32_t)(127 - 15 - h_exp)) << 23;
  352. f_sig = ((uint32_t)(h_sig & 0x03ffu)) << 13;
  353. return f_sgn + f_exp + f_sig;
  354. case 0x7c00u: /* inf or NaN */
  355. /* All-ones exponent and a copy of the significand */
  356. return f_sgn + 0x7f800000u + (((uint32_t)(h & 0x03ffu)) << 13);
  357. default: /* normalized */
  358. /* Just need to adjust the exponent and shift */
  359. return f_sgn + (((uint32_t)(h & 0x7fffu) + 0x1c000u) << 13);
  360. }
  361. }
  362. static _ALWAYS_INLINE_ float halfptr_to_float(const uint16_t *h) {
  363. union {
  364. uint32_t u32;
  365. float f32;
  366. } u;
  367. u.u32 = halfbits_to_floatbits(*h);
  368. return u.f32;
  369. }
  370. static _ALWAYS_INLINE_ float half_to_float(const uint16_t h) {
  371. return halfptr_to_float(&h);
  372. }
  373. static _ALWAYS_INLINE_ uint16_t make_half_float(float f) {
  374. union {
  375. float fv;
  376. uint32_t ui;
  377. } ci;
  378. ci.fv = f;
  379. uint32_t x = ci.ui;
  380. uint32_t sign = (unsigned short)(x >> 31);
  381. uint32_t mantissa;
  382. uint32_t exp;
  383. uint16_t hf;
  384. // get mantissa
  385. mantissa = x & ((1 << 23) - 1);
  386. // get exponent bits
  387. exp = x & (0xFF << 23);
  388. if (exp >= 0x47800000) {
  389. // check if the original single precision float number is a NaN
  390. if (mantissa && (exp == (0xFF << 23))) {
  391. // we have a single precision NaN
  392. mantissa = (1 << 23) - 1;
  393. } else {
  394. // 16-bit half-float representation stores number as Inf
  395. mantissa = 0;
  396. }
  397. hf = (((uint16_t)sign) << 15) | (uint16_t)((0x1F << 10)) |
  398. (uint16_t)(mantissa >> 13);
  399. }
  400. // check if exponent is <= -15
  401. else if (exp <= 0x38000000) {
  402. /*// store a denorm half-float value or zero
  403. exp = (0x38000000 - exp) >> 23;
  404. mantissa >>= (14 + exp);
  405. hf = (((uint16_t)sign) << 15) | (uint16_t)(mantissa);
  406. */
  407. hf = 0; //denormals do not work for 3D, convert to zero
  408. } else {
  409. hf = (((uint16_t)sign) << 15) |
  410. (uint16_t)((exp - 0x38000000) >> 13) |
  411. (uint16_t)(mantissa >> 13);
  412. }
  413. return hf;
  414. }
  415. static _ALWAYS_INLINE_ float snap_scalar(float p_offset, float p_step, float p_target) {
  416. return p_step != 0 ? Math::snapped(p_target - p_offset, p_step) + p_offset : p_target;
  417. }
  418. static _ALWAYS_INLINE_ float snap_scalar_separation(float p_offset, float p_step, float p_target, float p_separation) {
  419. if (p_step != 0) {
  420. float a = Math::snapped(p_target - p_offset, p_step + p_separation) + p_offset;
  421. float b = a;
  422. if (p_target >= 0) {
  423. b -= p_separation;
  424. } else {
  425. b += p_step;
  426. }
  427. return (Math::abs(p_target - a) < Math::abs(p_target - b)) ? a : b;
  428. }
  429. return p_target;
  430. }
  431. };
  432. #endif // MATH_FUNCS_H