math_funcs.cpp 6.3 KB

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