math_funcs.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*************************************************************************/
  2. /* math_funcs.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #include "math_funcs.h"
  31. #include "core/os/os.h"
  32. #include "float.h"
  33. #include <math.h>
  34. uint32_t Math::default_seed = 1;
  35. #define PHI 0x9e3779b9
  36. #if 0
  37. static uint32_t Q[4096];
  38. #endif
  39. uint32_t Math::rand_from_seed(uint32_t *seed) {
  40. #if 1
  41. uint32_t k;
  42. uint32_t s = (*seed);
  43. if (s == 0)
  44. s = 0x12345987;
  45. k = s / 127773;
  46. s = 16807 * (s - k * 127773) - 2836 * k;
  47. // if (s < 0)
  48. // s += 2147483647;
  49. (*seed) = s;
  50. return (s & Math::RANDOM_MAX);
  51. #else
  52. *seed = *seed * 1103515245 + 12345;
  53. return (*seed % ((unsigned int)RANDOM_MAX + 1));
  54. #endif
  55. }
  56. void Math::seed(uint32_t x) {
  57. #if 0
  58. int i;
  59. Q[0] = x;
  60. Q[1] = x + PHI;
  61. Q[2] = x + PHI + PHI;
  62. for (i = 3; i < 4096; i++)
  63. Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;
  64. #else
  65. default_seed = x;
  66. #endif
  67. }
  68. void Math::randomize() {
  69. OS::Time time = OS::get_singleton()->get_time();
  70. seed(OS::get_singleton()->get_ticks_usec() * (time.hour + 1) * (time.min + 1) * (time.sec + 1) * rand()); /* *OS::get_singleton()->get_time().sec); // windows doesn't have get_time(), returns always 0 */
  71. }
  72. uint32_t Math::rand() {
  73. return rand_from_seed(&default_seed) & 0x7FFFFFFF;
  74. }
  75. double Math::randf() {
  76. return (double)rand() / (double)RANDOM_MAX;
  77. }
  78. double Math::sin(double p_x) {
  79. return ::sin(p_x);
  80. }
  81. double Math::cos(double p_x) {
  82. return ::cos(p_x);
  83. }
  84. double Math::tan(double p_x) {
  85. return ::tan(p_x);
  86. }
  87. double Math::sinh(double p_x) {
  88. return ::sinh(p_x);
  89. }
  90. double Math::cosh(double p_x) {
  91. return ::cosh(p_x);
  92. }
  93. double Math::tanh(double p_x) {
  94. return ::tanh(p_x);
  95. }
  96. double Math::deg2rad(double p_y) {
  97. return p_y * Math_PI / 180.0;
  98. }
  99. double Math::rad2deg(double p_y) {
  100. return p_y * 180.0 / Math_PI;
  101. }
  102. double Math::round(double p_val) {
  103. if (p_val >= 0) {
  104. return ::floor(p_val + 0.5);
  105. } else {
  106. p_val = -p_val;
  107. return -::floor(p_val + 0.5);
  108. }
  109. }
  110. double Math::asin(double p_x) {
  111. return ::asin(p_x);
  112. }
  113. double Math::acos(double p_x) {
  114. return ::acos(p_x);
  115. }
  116. double Math::atan(double p_x) {
  117. return ::atan(p_x);
  118. }
  119. double Math::dectime(double p_value, double p_amount, double p_step) {
  120. float sgn = p_value < 0 ? -1.0 : 1.0;
  121. float val = absf(p_value);
  122. val -= p_amount * p_step;
  123. if (val < 0.0)
  124. val = 0.0;
  125. return val * sgn;
  126. }
  127. double Math::atan2(double p_y, double p_x) {
  128. return ::atan2(p_y, p_x);
  129. }
  130. double Math::sqrt(double p_x) {
  131. return ::sqrt(p_x);
  132. }
  133. double Math::fmod(double p_x, double p_y) {
  134. return ::fmod(p_x, p_y);
  135. }
  136. double Math::fposmod(double p_x, double p_y) {
  137. if (p_x >= 0) {
  138. return Math::fmod(p_x, p_y);
  139. } else {
  140. return p_y - Math::fmod(-p_x, p_y);
  141. }
  142. }
  143. double Math::floor(double p_x) {
  144. return ::floor(p_x);
  145. }
  146. double Math::ceil(double p_x) {
  147. return ::ceil(p_x);
  148. }
  149. int Math::step_decimals(double p_step) {
  150. static const int maxn = 9;
  151. static const double sd[maxn] = {
  152. 0.9999, // somehow compensate for floating point error
  153. 0.09999,
  154. 0.009999,
  155. 0.0009999,
  156. 0.00009999,
  157. 0.000009999,
  158. 0.0000009999,
  159. 0.00000009999,
  160. 0.000000009999
  161. };
  162. double as = absf(p_step);
  163. for (int i = 0; i < maxn; i++) {
  164. if (as >= sd[i]) {
  165. return i;
  166. }
  167. }
  168. return maxn;
  169. }
  170. double Math::ease(double p_x, double p_c) {
  171. if (p_x < 0)
  172. p_x = 0;
  173. else if (p_x > 1.0)
  174. p_x = 1.0;
  175. if (p_c > 0) {
  176. if (p_c < 1.0) {
  177. return 1.0 - Math::pow(1.0 - p_x, 1.0 / p_c);
  178. } else {
  179. return Math::pow(p_x, p_c);
  180. }
  181. } else if (p_c < 0) {
  182. //inout ease
  183. if (p_x < 0.5) {
  184. return Math::pow(p_x * 2.0, -p_c) * 0.5;
  185. } else {
  186. return (1.0 - Math::pow(1.0 - (p_x - 0.5) * 2.0, -p_c)) * 0.5 + 0.5;
  187. }
  188. } else
  189. return 0; // no ease (raw)
  190. }
  191. double Math::stepify(double p_value, double p_step) {
  192. if (p_step != 0) {
  193. p_value = floor(p_value / p_step + 0.5) * p_step;
  194. }
  195. return p_value;
  196. }
  197. bool Math::is_nan(double p_val) {
  198. return (p_val != p_val);
  199. }
  200. bool Math::is_inf(double p_val) {
  201. #ifdef _MSC_VER
  202. return !_finite(p_val);
  203. #else
  204. return isinf(p_val);
  205. #endif
  206. }
  207. uint32_t Math::larger_prime(uint32_t p_val) {
  208. static const uint32_t primes[] = {
  209. 5,
  210. 13,
  211. 23,
  212. 47,
  213. 97,
  214. 193,
  215. 389,
  216. 769,
  217. 1543,
  218. 3079,
  219. 6151,
  220. 12289,
  221. 24593,
  222. 49157,
  223. 98317,
  224. 196613,
  225. 393241,
  226. 786433,
  227. 1572869,
  228. 3145739,
  229. 6291469,
  230. 12582917,
  231. 25165843,
  232. 50331653,
  233. 100663319,
  234. 201326611,
  235. 402653189,
  236. 805306457,
  237. 1610612741,
  238. 0,
  239. };
  240. int idx = 0;
  241. while (true) {
  242. ERR_FAIL_COND_V(primes[idx] == 0, 0);
  243. if (primes[idx] > p_val)
  244. return primes[idx];
  245. idx++;
  246. }
  247. return 0;
  248. }
  249. double Math::random(double from, double to) {
  250. unsigned int r = Math::rand();
  251. double ret = (double)r / (double)RANDOM_MAX;
  252. return (ret) * (to - from) + from;
  253. }
  254. double Math::pow(double x, double y) {
  255. return ::pow(x, y);
  256. }
  257. double Math::log(double x) {
  258. return ::log(x);
  259. }
  260. double Math::exp(double x) {
  261. return ::exp(x);
  262. }