math_funcs.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*************************************************************************/
  2. /* math_funcs.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "math_funcs.h"
  30. #include "core/os/os.h"
  31. #include <math.h>
  32. #include "float.h"
  33. uint32_t Math::default_seed=1;
  34. #define PHI 0x9e3779b9
  35. #if 0
  36. static uint32_t Q[4096];
  37. #endif
  38. uint32_t Math::rand_from_seed(uint32_t *seed) {
  39. #if 1
  40. uint32_t k;
  41. uint32_t s = (*seed);
  42. if (s == 0)
  43. s = 0x12345987;
  44. k = s / 127773;
  45. s = 16807 * (s - k * 127773) - 2836 * k;
  46. // if (s < 0)
  47. // s += 2147483647;
  48. (*seed) = s;
  49. return (s & Math::RANDOM_MAX);
  50. #else
  51. *seed = *seed * 1103515245 + 12345;
  52. return (*seed % ((unsigned int)RANDOM_MAX + 1));
  53. #endif
  54. }
  55. void Math::seed(uint32_t x) {
  56. #if 0
  57. int i;
  58. Q[0] = x;
  59. Q[1] = x + PHI;
  60. Q[2] = x + PHI + PHI;
  61. for (i = 3; i < 4096; i++)
  62. Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;
  63. #else
  64. default_seed=x;
  65. #endif
  66. }
  67. void Math::randomize() {
  68. OS::Time time = OS::get_singleton()->get_time();
  69. 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 */
  70. }
  71. uint32_t Math::rand() {
  72. return rand_from_seed(&default_seed)&0x7FFFFFFF;
  73. }
  74. double Math::randf() {
  75. return (double)rand() / (double)RANDOM_MAX;
  76. }
  77. double Math::sin(double p_x) {
  78. return ::sin(p_x);
  79. }
  80. double Math::cos(double p_x) {
  81. return ::cos(p_x);
  82. }
  83. double Math::tan(double p_x) {
  84. return ::tan(p_x);
  85. }
  86. double Math::sinh(double p_x) {
  87. return ::sinh(p_x);
  88. }
  89. double Math::cosh(double p_x) {
  90. return ::cosh(p_x);
  91. }
  92. double Math::tanh(double p_x) {
  93. return ::tanh(p_x);
  94. }
  95. double Math::deg2rad(double p_y) {
  96. return p_y*Math_PI/180.0;
  97. }
  98. double Math::rad2deg(double p_y) {
  99. return p_y*180.0/Math_PI;
  100. }
  101. double Math::round(double p_val) {
  102. if (p_val>0) {
  103. return ::floor(p_val+0.5);
  104. } else {
  105. p_val=-p_val;
  106. return -::floor(p_val+0.5);
  107. }
  108. }
  109. double Math::asin(double p_x) {
  110. return ::asin(p_x);
  111. }
  112. double Math::acos(double p_x) {
  113. return ::acos(p_x);
  114. }
  115. double Math::atan(double p_x) {
  116. return ::atan(p_x);
  117. }
  118. double Math::dectime(double p_value,double p_amount, double p_step) {
  119. float sgn = p_value < 0 ? -1.0 : 1.0;
  120. float val = absf(p_value);
  121. val-=p_amount*p_step;
  122. if (val<0.0)
  123. val=0.0;
  124. return val*sgn;
  125. }
  126. double Math::atan2(double p_y, double p_x) {
  127. return ::atan2(p_y,p_x);
  128. }
  129. double Math::sqrt(double p_x) {
  130. return ::sqrt(p_x);
  131. }
  132. double Math::fmod(double p_x,double p_y) {
  133. return ::fmod(p_x,p_y);
  134. }
  135. double Math::fposmod(double p_x,double p_y) {
  136. if (p_x>=0) {
  137. return Math::fmod(p_x,p_y);
  138. } else {
  139. return p_y-Math::fmod(-p_x,p_y);
  140. }
  141. }
  142. double Math::floor(double p_x) {
  143. return ::floor(p_x);
  144. }
  145. double Math::ceil(double p_x) {
  146. return ::ceil(p_x);
  147. }
  148. int Math::decimals(double p_step) {
  149. int max=4;
  150. double llimit = Math::pow(0.1,max);
  151. double ulimit = 1.0-llimit;
  152. int i=0;
  153. while( max) {
  154. float d = absf(p_step) - Math::floor(absf(p_step));
  155. if (d<llimit || d>ulimit)
  156. break;
  157. p_step*=10.0;
  158. max--;
  159. i++;
  160. }
  161. return i;
  162. }
  163. double Math::ease(double p_x, double p_c) {
  164. if (p_x<0)
  165. p_x=0;
  166. else if (p_x>1.0)
  167. p_x=1.0;
  168. if (p_c>0) {
  169. if (p_c<1.0) {
  170. return 1.0-Math::pow(1.0-p_x,1.0/p_c);
  171. } else {
  172. return Math::pow(p_x,p_c);
  173. }
  174. } else if (p_c<0) {
  175. //inout ease
  176. if (p_x<0.5) {
  177. return Math::pow(p_x*2.0,-p_c)*0.5;
  178. } else {
  179. return (1.0-Math::pow(1.0-(p_x-0.5)*2.0,-p_c))*0.5+0.5;
  180. }
  181. } else
  182. return 0; // no ease (raw)
  183. }
  184. double Math::stepify(double p_value,double p_step) {
  185. if (p_step!=0) {
  186. p_value=floor( p_value / p_step + 0.5 ) * p_step;
  187. }
  188. return p_value;
  189. }
  190. bool Math::is_nan(double p_val) {
  191. return (p_val!=p_val);
  192. }
  193. bool Math::is_inf(double p_val) {
  194. #ifdef _MSC_VER
  195. return !_finite(p_val);
  196. #else
  197. return isinf(p_val);
  198. #endif
  199. }
  200. uint32_t Math::larger_prime(uint32_t p_val) {
  201. static const uint32_t primes[] = {
  202. 5,
  203. 13,
  204. 23,
  205. 47,
  206. 97,
  207. 193,
  208. 389,
  209. 769,
  210. 1543,
  211. 3079,
  212. 6151,
  213. 12289,
  214. 24593,
  215. 49157,
  216. 98317,
  217. 196613,
  218. 393241,
  219. 786433,
  220. 1572869,
  221. 3145739,
  222. 6291469,
  223. 12582917,
  224. 25165843,
  225. 50331653,
  226. 100663319,
  227. 201326611,
  228. 402653189,
  229. 805306457,
  230. 1610612741,
  231. 0,
  232. };
  233. int idx=0;
  234. while (true) {
  235. ERR_FAIL_COND_V(primes[idx]==0,0);
  236. if (primes[idx]>p_val)
  237. return primes[idx];
  238. idx++;
  239. }
  240. return 0;
  241. }
  242. double Math::random(double from, double to) {
  243. unsigned int r = Math::rand();
  244. double ret = (double)r/(double)RANDOM_MAX;
  245. return (ret)*(to-from) + from;
  246. }
  247. double Math::pow(double x, double y) {
  248. return ::pow(x,y);
  249. }
  250. double Math::log(double x) {
  251. return ::log(x);
  252. }
  253. double Math::exp(double x) {
  254. return ::exp(x);
  255. }